OSDN Git Service

PR c++/13950, DR 176
[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   /* Avoid duplicate error about ambiguous lookup.  */
2434   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2435     {
2436       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2437       if (next->type == CPP_NAME && next->ambiguous_p)
2438         goto out;
2439     }
2440
2441   cp_parser_parse_tentatively (parser);
2442   id = cp_parser_id_expression (parser,
2443                                 /*template_keyword_p=*/false,
2444                                 /*check_dependency_p=*/true,
2445                                 /*template_p=*/NULL,
2446                                 /*declarator_p=*/true,
2447                                 /*optional_p=*/false);
2448   /* If the next token is a (, this is a function with no explicit return
2449      type, i.e. constructor, destructor or conversion op.  */
2450   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2451       || TREE_CODE (id) == TYPE_DECL)
2452     {
2453       cp_parser_abort_tentative_parse (parser);
2454       return false;
2455     }
2456   if (!cp_parser_parse_definitely (parser))
2457     return false;
2458
2459   /* Emit a diagnostic for the invalid type.  */
2460   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2461                                         id, token->location);
2462  out:
2463   /* If we aren't in the middle of a declarator (i.e. in a
2464      parameter-declaration-clause), skip to the end of the declaration;
2465      there's no point in trying to process it.  */
2466   if (!parser->in_declarator_p)
2467     cp_parser_skip_to_end_of_block_or_statement (parser);
2468   return true;
2469 }
2470
2471 /* Consume tokens up to, and including, the next non-nested closing `)'.
2472    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2473    are doing error recovery. Returns -1 if OR_COMMA is true and we
2474    found an unnested comma.  */
2475
2476 static int
2477 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2478                                        bool recovering,
2479                                        bool or_comma,
2480                                        bool consume_paren)
2481 {
2482   unsigned paren_depth = 0;
2483   unsigned brace_depth = 0;
2484   unsigned square_depth = 0;
2485
2486   if (recovering && !or_comma
2487       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2488     return 0;
2489
2490   while (true)
2491     {
2492       cp_token * token = cp_lexer_peek_token (parser->lexer);
2493
2494       switch (token->type)
2495         {
2496         case CPP_EOF:
2497         case CPP_PRAGMA_EOL:
2498           /* If we've run out of tokens, then there is no closing `)'.  */
2499           return 0;
2500
2501         /* This is good for lambda expression capture-lists.  */
2502         case CPP_OPEN_SQUARE:
2503           ++square_depth;
2504           break;
2505         case CPP_CLOSE_SQUARE:
2506           if (!square_depth--)
2507             return 0;
2508           break;
2509
2510         case CPP_SEMICOLON:
2511           /* This matches the processing in skip_to_end_of_statement.  */
2512           if (!brace_depth)
2513             return 0;
2514           break;
2515
2516         case CPP_OPEN_BRACE:
2517           ++brace_depth;
2518           break;
2519         case CPP_CLOSE_BRACE:
2520           if (!brace_depth--)
2521             return 0;
2522           break;
2523
2524         case CPP_COMMA:
2525           if (recovering && or_comma && !brace_depth && !paren_depth
2526               && !square_depth)
2527             return -1;
2528           break;
2529
2530         case CPP_OPEN_PAREN:
2531           if (!brace_depth)
2532             ++paren_depth;
2533           break;
2534
2535         case CPP_CLOSE_PAREN:
2536           if (!brace_depth && !paren_depth--)
2537             {
2538               if (consume_paren)
2539                 cp_lexer_consume_token (parser->lexer);
2540               return 1;
2541             }
2542           break;
2543
2544         default:
2545           break;
2546         }
2547
2548       /* Consume the token.  */
2549       cp_lexer_consume_token (parser->lexer);
2550     }
2551 }
2552
2553 /* Consume tokens until we reach the end of the current statement.
2554    Normally, that will be just before consuming a `;'.  However, if a
2555    non-nested `}' comes first, then we stop before consuming that.  */
2556
2557 static void
2558 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2559 {
2560   unsigned nesting_depth = 0;
2561
2562   while (true)
2563     {
2564       cp_token *token = cp_lexer_peek_token (parser->lexer);
2565
2566       switch (token->type)
2567         {
2568         case CPP_EOF:
2569         case CPP_PRAGMA_EOL:
2570           /* If we've run out of tokens, stop.  */
2571           return;
2572
2573         case CPP_SEMICOLON:
2574           /* If the next token is a `;', we have reached the end of the
2575              statement.  */
2576           if (!nesting_depth)
2577             return;
2578           break;
2579
2580         case CPP_CLOSE_BRACE:
2581           /* If this is a non-nested '}', stop before consuming it.
2582              That way, when confronted with something like:
2583
2584                { 3 + }
2585
2586              we stop before consuming the closing '}', even though we
2587              have not yet reached a `;'.  */
2588           if (nesting_depth == 0)
2589             return;
2590
2591           /* If it is the closing '}' for a block that we have
2592              scanned, stop -- but only after consuming the token.
2593              That way given:
2594
2595                 void f g () { ... }
2596                 typedef int I;
2597
2598              we will stop after the body of the erroneously declared
2599              function, but before consuming the following `typedef'
2600              declaration.  */
2601           if (--nesting_depth == 0)
2602             {
2603               cp_lexer_consume_token (parser->lexer);
2604               return;
2605             }
2606
2607         case CPP_OPEN_BRACE:
2608           ++nesting_depth;
2609           break;
2610
2611         default:
2612           break;
2613         }
2614
2615       /* Consume the token.  */
2616       cp_lexer_consume_token (parser->lexer);
2617     }
2618 }
2619
2620 /* This function is called at the end of a statement or declaration.
2621    If the next token is a semicolon, it is consumed; otherwise, error
2622    recovery is attempted.  */
2623
2624 static void
2625 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2626 {
2627   /* Look for the trailing `;'.  */
2628   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2629     {
2630       /* If there is additional (erroneous) input, skip to the end of
2631          the statement.  */
2632       cp_parser_skip_to_end_of_statement (parser);
2633       /* If the next token is now a `;', consume it.  */
2634       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2635         cp_lexer_consume_token (parser->lexer);
2636     }
2637 }
2638
2639 /* Skip tokens until we have consumed an entire block, or until we
2640    have consumed a non-nested `;'.  */
2641
2642 static void
2643 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2644 {
2645   int nesting_depth = 0;
2646
2647   while (nesting_depth >= 0)
2648     {
2649       cp_token *token = cp_lexer_peek_token (parser->lexer);
2650
2651       switch (token->type)
2652         {
2653         case CPP_EOF:
2654         case CPP_PRAGMA_EOL:
2655           /* If we've run out of tokens, stop.  */
2656           return;
2657
2658         case CPP_SEMICOLON:
2659           /* Stop if this is an unnested ';'. */
2660           if (!nesting_depth)
2661             nesting_depth = -1;
2662           break;
2663
2664         case CPP_CLOSE_BRACE:
2665           /* Stop if this is an unnested '}', or closes the outermost
2666              nesting level.  */
2667           nesting_depth--;
2668           if (nesting_depth < 0)
2669             return;
2670           if (!nesting_depth)
2671             nesting_depth = -1;
2672           break;
2673
2674         case CPP_OPEN_BRACE:
2675           /* Nest. */
2676           nesting_depth++;
2677           break;
2678
2679         default:
2680           break;
2681         }
2682
2683       /* Consume the token.  */
2684       cp_lexer_consume_token (parser->lexer);
2685     }
2686 }
2687
2688 /* Skip tokens until a non-nested closing curly brace is the next
2689    token, or there are no more tokens. Return true in the first case,
2690    false otherwise.  */
2691
2692 static bool
2693 cp_parser_skip_to_closing_brace (cp_parser *parser)
2694 {
2695   unsigned nesting_depth = 0;
2696
2697   while (true)
2698     {
2699       cp_token *token = cp_lexer_peek_token (parser->lexer);
2700
2701       switch (token->type)
2702         {
2703         case CPP_EOF:
2704         case CPP_PRAGMA_EOL:
2705           /* If we've run out of tokens, stop.  */
2706           return false;
2707
2708         case CPP_CLOSE_BRACE:
2709           /* If the next token is a non-nested `}', then we have reached
2710              the end of the current block.  */
2711           if (nesting_depth-- == 0)
2712             return true;
2713           break;
2714
2715         case CPP_OPEN_BRACE:
2716           /* If it the next token is a `{', then we are entering a new
2717              block.  Consume the entire block.  */
2718           ++nesting_depth;
2719           break;
2720
2721         default:
2722           break;
2723         }
2724
2725       /* Consume the token.  */
2726       cp_lexer_consume_token (parser->lexer);
2727     }
2728 }
2729
2730 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2731    parameter is the PRAGMA token, allowing us to purge the entire pragma
2732    sequence.  */
2733
2734 static void
2735 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2736 {
2737   cp_token *token;
2738
2739   parser->lexer->in_pragma = false;
2740
2741   do
2742     token = cp_lexer_consume_token (parser->lexer);
2743   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2744
2745   /* Ensure that the pragma is not parsed again.  */
2746   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2747 }
2748
2749 /* Require pragma end of line, resyncing with it as necessary.  The
2750    arguments are as for cp_parser_skip_to_pragma_eol.  */
2751
2752 static void
2753 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2754 {
2755   parser->lexer->in_pragma = false;
2756   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2757     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2758 }
2759
2760 /* This is a simple wrapper around make_typename_type. When the id is
2761    an unresolved identifier node, we can provide a superior diagnostic
2762    using cp_parser_diagnose_invalid_type_name.  */
2763
2764 static tree
2765 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2766                               tree id, location_t id_location)
2767 {
2768   tree result;
2769   if (TREE_CODE (id) == IDENTIFIER_NODE)
2770     {
2771       result = make_typename_type (scope, id, typename_type,
2772                                    /*complain=*/tf_none);
2773       if (result == error_mark_node)
2774         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2775       return result;
2776     }
2777   return make_typename_type (scope, id, typename_type, tf_error);
2778 }
2779
2780 /* This is a wrapper around the
2781    make_{pointer,ptrmem,reference}_declarator functions that decides
2782    which one to call based on the CODE and CLASS_TYPE arguments. The
2783    CODE argument should be one of the values returned by
2784    cp_parser_ptr_operator. */
2785 static cp_declarator *
2786 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2787                                     cp_cv_quals cv_qualifiers,
2788                                     cp_declarator *target)
2789 {
2790   if (code == ERROR_MARK)
2791     return cp_error_declarator;
2792
2793   if (code == INDIRECT_REF)
2794     if (class_type == NULL_TREE)
2795       return make_pointer_declarator (cv_qualifiers, target);
2796     else
2797       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2798   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2799     return make_reference_declarator (cv_qualifiers, target, false);
2800   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2801     return make_reference_declarator (cv_qualifiers, target, true);
2802   gcc_unreachable ();
2803 }
2804
2805 /* Create a new C++ parser.  */
2806
2807 static cp_parser *
2808 cp_parser_new (void)
2809 {
2810   cp_parser *parser;
2811   cp_lexer *lexer;
2812   unsigned i;
2813
2814   /* cp_lexer_new_main is called before calling ggc_alloc because
2815      cp_lexer_new_main might load a PCH file.  */
2816   lexer = cp_lexer_new_main ();
2817
2818   /* Initialize the binops_by_token so that we can get the tree
2819      directly from the token.  */
2820   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2821     binops_by_token[binops[i].token_type] = binops[i];
2822
2823   parser = GGC_CNEW (cp_parser);
2824   parser->lexer = lexer;
2825   parser->context = cp_parser_context_new (NULL);
2826
2827   /* For now, we always accept GNU extensions.  */
2828   parser->allow_gnu_extensions_p = 1;
2829
2830   /* The `>' token is a greater-than operator, not the end of a
2831      template-id.  */
2832   parser->greater_than_is_operator_p = true;
2833
2834   parser->default_arg_ok_p = true;
2835
2836   /* We are not parsing a constant-expression.  */
2837   parser->integral_constant_expression_p = false;
2838   parser->allow_non_integral_constant_expression_p = false;
2839   parser->non_integral_constant_expression_p = false;
2840
2841   /* Local variable names are not forbidden.  */
2842   parser->local_variables_forbidden_p = false;
2843
2844   /* We are not processing an `extern "C"' declaration.  */
2845   parser->in_unbraced_linkage_specification_p = false;
2846
2847   /* We are not processing a declarator.  */
2848   parser->in_declarator_p = false;
2849
2850   /* We are not processing a template-argument-list.  */
2851   parser->in_template_argument_list_p = false;
2852
2853   /* We are not in an iteration statement.  */
2854   parser->in_statement = 0;
2855
2856   /* We are not in a switch statement.  */
2857   parser->in_switch_statement_p = false;
2858
2859   /* We are not parsing a type-id inside an expression.  */
2860   parser->in_type_id_in_expr_p = false;
2861
2862   /* Declarations aren't implicitly extern "C".  */
2863   parser->implicit_extern_c = false;
2864
2865   /* String literals should be translated to the execution character set.  */
2866   parser->translate_strings_p = true;
2867
2868   /* We are not parsing a function body.  */
2869   parser->in_function_body = false;
2870
2871   /* The unparsed function queue is empty.  */
2872   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2873
2874   /* There are no classes being defined.  */
2875   parser->num_classes_being_defined = 0;
2876
2877   /* No template parameters apply.  */
2878   parser->num_template_parameter_lists = 0;
2879
2880   return parser;
2881 }
2882
2883 /* Create a cp_lexer structure which will emit the tokens in CACHE
2884    and push it onto the parser's lexer stack.  This is used for delayed
2885    parsing of in-class method bodies and default arguments, and should
2886    not be confused with tentative parsing.  */
2887 static void
2888 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2889 {
2890   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2891   lexer->next = parser->lexer;
2892   parser->lexer = lexer;
2893
2894   /* Move the current source position to that of the first token in the
2895      new lexer.  */
2896   cp_lexer_set_source_position_from_token (lexer->next_token);
2897 }
2898
2899 /* Pop the top lexer off the parser stack.  This is never used for the
2900    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2901 static void
2902 cp_parser_pop_lexer (cp_parser *parser)
2903 {
2904   cp_lexer *lexer = parser->lexer;
2905   parser->lexer = lexer->next;
2906   cp_lexer_destroy (lexer);
2907
2908   /* Put the current source position back where it was before this
2909      lexer was pushed.  */
2910   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2911 }
2912
2913 /* Lexical conventions [gram.lex]  */
2914
2915 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2916    identifier.  */
2917
2918 static tree
2919 cp_parser_identifier (cp_parser* parser)
2920 {
2921   cp_token *token;
2922
2923   /* Look for the identifier.  */
2924   token = cp_parser_require (parser, CPP_NAME, "identifier");
2925   /* Return the value.  */
2926   return token ? token->u.value : error_mark_node;
2927 }
2928
2929 /* Parse a sequence of adjacent string constants.  Returns a
2930    TREE_STRING representing the combined, nul-terminated string
2931    constant.  If TRANSLATE is true, translate the string to the
2932    execution character set.  If WIDE_OK is true, a wide string is
2933    invalid here.
2934
2935    C++98 [lex.string] says that if a narrow string literal token is
2936    adjacent to a wide string literal token, the behavior is undefined.
2937    However, C99 6.4.5p4 says that this results in a wide string literal.
2938    We follow C99 here, for consistency with the C front end.
2939
2940    This code is largely lifted from lex_string() in c-lex.c.
2941
2942    FUTURE: ObjC++ will need to handle @-strings here.  */
2943 static tree
2944 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2945 {
2946   tree value;
2947   size_t count;
2948   struct obstack str_ob;
2949   cpp_string str, istr, *strs;
2950   cp_token *tok;
2951   enum cpp_ttype type;
2952
2953   tok = cp_lexer_peek_token (parser->lexer);
2954   if (!cp_parser_is_string_literal (tok))
2955     {
2956       cp_parser_error (parser, "expected string-literal");
2957       return error_mark_node;
2958     }
2959
2960   type = tok->type;
2961
2962   /* Try to avoid the overhead of creating and destroying an obstack
2963      for the common case of just one string.  */
2964   if (!cp_parser_is_string_literal
2965       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2966     {
2967       cp_lexer_consume_token (parser->lexer);
2968
2969       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2970       str.len = TREE_STRING_LENGTH (tok->u.value);
2971       count = 1;
2972
2973       strs = &str;
2974     }
2975   else
2976     {
2977       gcc_obstack_init (&str_ob);
2978       count = 0;
2979
2980       do
2981         {
2982           cp_lexer_consume_token (parser->lexer);
2983           count++;
2984           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2985           str.len = TREE_STRING_LENGTH (tok->u.value);
2986
2987           if (type != tok->type)
2988             {
2989               if (type == CPP_STRING)
2990                 type = tok->type;
2991               else if (tok->type != CPP_STRING)
2992                 error_at (tok->location,
2993                           "unsupported non-standard concatenation "
2994                           "of string literals");
2995             }
2996
2997           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2998
2999           tok = cp_lexer_peek_token (parser->lexer);
3000         }
3001       while (cp_parser_is_string_literal (tok));
3002
3003       strs = (cpp_string *) obstack_finish (&str_ob);
3004     }
3005
3006   if (type != CPP_STRING && !wide_ok)
3007     {
3008       cp_parser_error (parser, "a wide string is invalid in this context");
3009       type = CPP_STRING;
3010     }
3011
3012   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3013       (parse_in, strs, count, &istr, type))
3014     {
3015       value = build_string (istr.len, (const char *)istr.text);
3016       free (CONST_CAST (unsigned char *, istr.text));
3017
3018       switch (type)
3019         {
3020         default:
3021         case CPP_STRING:
3022         case CPP_UTF8STRING:
3023           TREE_TYPE (value) = char_array_type_node;
3024           break;
3025         case CPP_STRING16:
3026           TREE_TYPE (value) = char16_array_type_node;
3027           break;
3028         case CPP_STRING32:
3029           TREE_TYPE (value) = char32_array_type_node;
3030           break;
3031         case CPP_WSTRING:
3032           TREE_TYPE (value) = wchar_array_type_node;
3033           break;
3034         }
3035
3036       value = fix_string_type (value);
3037     }
3038   else
3039     /* cpp_interpret_string has issued an error.  */
3040     value = error_mark_node;
3041
3042   if (count > 1)
3043     obstack_free (&str_ob, 0);
3044
3045   return value;
3046 }
3047
3048
3049 /* Basic concepts [gram.basic]  */
3050
3051 /* Parse a translation-unit.
3052
3053    translation-unit:
3054      declaration-seq [opt]
3055
3056    Returns TRUE if all went well.  */
3057
3058 static bool
3059 cp_parser_translation_unit (cp_parser* parser)
3060 {
3061   /* The address of the first non-permanent object on the declarator
3062      obstack.  */
3063   static void *declarator_obstack_base;
3064
3065   bool success;
3066
3067   /* Create the declarator obstack, if necessary.  */
3068   if (!cp_error_declarator)
3069     {
3070       gcc_obstack_init (&declarator_obstack);
3071       /* Create the error declarator.  */
3072       cp_error_declarator = make_declarator (cdk_error);
3073       /* Create the empty parameter list.  */
3074       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3075       /* Remember where the base of the declarator obstack lies.  */
3076       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3077     }
3078
3079   cp_parser_declaration_seq_opt (parser);
3080
3081   /* If there are no tokens left then all went well.  */
3082   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3083     {
3084       /* Get rid of the token array; we don't need it any more.  */
3085       cp_lexer_destroy (parser->lexer);
3086       parser->lexer = NULL;
3087
3088       /* This file might have been a context that's implicitly extern
3089          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3090       if (parser->implicit_extern_c)
3091         {
3092           pop_lang_context ();
3093           parser->implicit_extern_c = false;
3094         }
3095
3096       /* Finish up.  */
3097       finish_translation_unit ();
3098
3099       success = true;
3100     }
3101   else
3102     {
3103       cp_parser_error (parser, "expected declaration");
3104       success = false;
3105     }
3106
3107   /* Make sure the declarator obstack was fully cleaned up.  */
3108   gcc_assert (obstack_next_free (&declarator_obstack)
3109               == declarator_obstack_base);
3110
3111   /* All went well.  */
3112   return success;
3113 }
3114
3115 /* Expressions [gram.expr] */
3116
3117 /* Parse a primary-expression.
3118
3119    primary-expression:
3120      literal
3121      this
3122      ( expression )
3123      id-expression
3124
3125    GNU Extensions:
3126
3127    primary-expression:
3128      ( compound-statement )
3129      __builtin_va_arg ( assignment-expression , type-id )
3130      __builtin_offsetof ( type-id , offsetof-expression )
3131
3132    C++ Extensions:
3133      __has_nothrow_assign ( type-id )   
3134      __has_nothrow_constructor ( type-id )
3135      __has_nothrow_copy ( type-id )
3136      __has_trivial_assign ( type-id )   
3137      __has_trivial_constructor ( type-id )
3138      __has_trivial_copy ( type-id )
3139      __has_trivial_destructor ( type-id )
3140      __has_virtual_destructor ( type-id )     
3141      __is_abstract ( type-id )
3142      __is_base_of ( type-id , type-id )
3143      __is_class ( type-id )
3144      __is_convertible_to ( type-id , type-id )     
3145      __is_empty ( type-id )
3146      __is_enum ( type-id )
3147      __is_pod ( type-id )
3148      __is_polymorphic ( type-id )
3149      __is_union ( type-id )
3150
3151    Objective-C++ Extension:
3152
3153    primary-expression:
3154      objc-expression
3155
3156    literal:
3157      __null
3158
3159    ADDRESS_P is true iff this expression was immediately preceded by
3160    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3161    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3162    true iff this expression is a template argument.
3163
3164    Returns a representation of the expression.  Upon return, *IDK
3165    indicates what kind of id-expression (if any) was present.  */
3166
3167 static tree
3168 cp_parser_primary_expression (cp_parser *parser,
3169                               bool address_p,
3170                               bool cast_p,
3171                               bool template_arg_p,
3172                               cp_id_kind *idk)
3173 {
3174   cp_token *token = NULL;
3175
3176   /* Assume the primary expression is not an id-expression.  */
3177   *idk = CP_ID_KIND_NONE;
3178
3179   /* Peek at the next token.  */
3180   token = cp_lexer_peek_token (parser->lexer);
3181   switch (token->type)
3182     {
3183       /* literal:
3184            integer-literal
3185            character-literal
3186            floating-literal
3187            string-literal
3188            boolean-literal  */
3189     case CPP_CHAR:
3190     case CPP_CHAR16:
3191     case CPP_CHAR32:
3192     case CPP_WCHAR:
3193     case CPP_NUMBER:
3194       token = cp_lexer_consume_token (parser->lexer);
3195       if (TREE_CODE (token->u.value) == FIXED_CST)
3196         {
3197           error_at (token->location,
3198                     "fixed-point types not supported in C++");
3199           return error_mark_node;
3200         }
3201       /* Floating-point literals are only allowed in an integral
3202          constant expression if they are cast to an integral or
3203          enumeration type.  */
3204       if (TREE_CODE (token->u.value) == REAL_CST
3205           && parser->integral_constant_expression_p
3206           && pedantic)
3207         {
3208           /* CAST_P will be set even in invalid code like "int(2.7 +
3209              ...)".   Therefore, we have to check that the next token
3210              is sure to end the cast.  */
3211           if (cast_p)
3212             {
3213               cp_token *next_token;
3214
3215               next_token = cp_lexer_peek_token (parser->lexer);
3216               if (/* The comma at the end of an
3217                      enumerator-definition.  */
3218                   next_token->type != CPP_COMMA
3219                   /* The curly brace at the end of an enum-specifier.  */
3220                   && next_token->type != CPP_CLOSE_BRACE
3221                   /* The end of a statement.  */
3222                   && next_token->type != CPP_SEMICOLON
3223                   /* The end of the cast-expression.  */
3224                   && next_token->type != CPP_CLOSE_PAREN
3225                   /* The end of an array bound.  */
3226                   && next_token->type != CPP_CLOSE_SQUARE
3227                   /* The closing ">" in a template-argument-list.  */
3228                   && (next_token->type != CPP_GREATER
3229                       || parser->greater_than_is_operator_p)
3230                   /* C++0x only: A ">>" treated like two ">" tokens,
3231                      in a template-argument-list.  */
3232                   && (next_token->type != CPP_RSHIFT
3233                       || (cxx_dialect == cxx98)
3234                       || parser->greater_than_is_operator_p))
3235                 cast_p = false;
3236             }
3237
3238           /* If we are within a cast, then the constraint that the
3239              cast is to an integral or enumeration type will be
3240              checked at that point.  If we are not within a cast, then
3241              this code is invalid.  */
3242           if (!cast_p)
3243             cp_parser_non_integral_constant_expression
3244               (parser, "floating-point literal");
3245         }
3246       return token->u.value;
3247
3248     case CPP_STRING:
3249     case CPP_STRING16:
3250     case CPP_STRING32:
3251     case CPP_WSTRING:
3252     case CPP_UTF8STRING:
3253       /* ??? Should wide strings be allowed when parser->translate_strings_p
3254          is false (i.e. in attributes)?  If not, we can kill the third
3255          argument to cp_parser_string_literal.  */
3256       return cp_parser_string_literal (parser,
3257                                        parser->translate_strings_p,
3258                                        true);
3259
3260     case CPP_OPEN_PAREN:
3261       {
3262         tree expr;
3263         bool saved_greater_than_is_operator_p;
3264
3265         /* Consume the `('.  */
3266         cp_lexer_consume_token (parser->lexer);
3267         /* Within a parenthesized expression, a `>' token is always
3268            the greater-than operator.  */
3269         saved_greater_than_is_operator_p
3270           = parser->greater_than_is_operator_p;
3271         parser->greater_than_is_operator_p = true;
3272         /* If we see `( { ' then we are looking at the beginning of
3273            a GNU statement-expression.  */
3274         if (cp_parser_allow_gnu_extensions_p (parser)
3275             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3276           {
3277             /* Statement-expressions are not allowed by the standard.  */
3278             pedwarn (token->location, OPT_pedantic, 
3279                      "ISO C++ forbids braced-groups within expressions");
3280
3281             /* And they're not allowed outside of a function-body; you
3282                cannot, for example, write:
3283
3284                  int i = ({ int j = 3; j + 1; });
3285
3286                at class or namespace scope.  */
3287             if (!parser->in_function_body
3288                 || parser->in_template_argument_list_p)
3289               {
3290                 error_at (token->location,
3291                           "statement-expressions are not allowed outside "
3292                           "functions nor in template-argument lists");
3293                 cp_parser_skip_to_end_of_block_or_statement (parser);
3294                 expr = error_mark_node;
3295               }
3296             else
3297               {
3298                 /* Start the statement-expression.  */
3299                 expr = begin_stmt_expr ();
3300                 /* Parse the compound-statement.  */
3301                 cp_parser_compound_statement (parser, expr, false);
3302                 /* Finish up.  */
3303                 expr = finish_stmt_expr (expr, false);
3304               }
3305           }
3306         else
3307           {
3308             /* Parse the parenthesized expression.  */
3309             expr = cp_parser_expression (parser, cast_p, idk);
3310             /* Let the front end know that this expression was
3311                enclosed in parentheses. This matters in case, for
3312                example, the expression is of the form `A::B', since
3313                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3314                not.  */
3315             finish_parenthesized_expr (expr);
3316           }
3317         /* The `>' token might be the end of a template-id or
3318            template-parameter-list now.  */
3319         parser->greater_than_is_operator_p
3320           = saved_greater_than_is_operator_p;
3321         /* Consume the `)'.  */
3322         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3323           cp_parser_skip_to_end_of_statement (parser);
3324
3325         return expr;
3326       }
3327
3328     case CPP_OPEN_SQUARE:
3329       if (c_dialect_objc ())
3330         /* We have an Objective-C++ message. */
3331         return cp_parser_objc_expression (parser);
3332       maybe_warn_cpp0x ("lambda expressions");
3333       return cp_parser_lambda_expression (parser);
3334
3335     case CPP_OBJC_STRING:
3336       if (c_dialect_objc ())
3337         /* We have an Objective-C++ string literal. */
3338         return cp_parser_objc_expression (parser);
3339       cp_parser_error (parser, "expected primary-expression");
3340       return error_mark_node;
3341
3342     case CPP_KEYWORD:
3343       switch (token->keyword)
3344         {
3345           /* These two are the boolean literals.  */
3346         case RID_TRUE:
3347           cp_lexer_consume_token (parser->lexer);
3348           return boolean_true_node;
3349         case RID_FALSE:
3350           cp_lexer_consume_token (parser->lexer);
3351           return boolean_false_node;
3352
3353           /* The `__null' literal.  */
3354         case RID_NULL:
3355           cp_lexer_consume_token (parser->lexer);
3356           return null_node;
3357
3358           /* Recognize the `this' keyword.  */
3359         case RID_THIS:
3360           cp_lexer_consume_token (parser->lexer);
3361           if (parser->local_variables_forbidden_p)
3362             {
3363               error_at (token->location,
3364                         "%<this%> may not be used in this context");
3365               return error_mark_node;
3366             }
3367           /* Pointers cannot appear in constant-expressions.  */
3368           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3369             return error_mark_node;
3370           return finish_this_expr ();
3371
3372           /* The `operator' keyword can be the beginning of an
3373              id-expression.  */
3374         case RID_OPERATOR:
3375           goto id_expression;
3376
3377         case RID_FUNCTION_NAME:
3378         case RID_PRETTY_FUNCTION_NAME:
3379         case RID_C99_FUNCTION_NAME:
3380           {
3381             const char *name;
3382
3383             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3384                __func__ are the names of variables -- but they are
3385                treated specially.  Therefore, they are handled here,
3386                rather than relying on the generic id-expression logic
3387                below.  Grammatically, these names are id-expressions.
3388
3389                Consume the token.  */
3390             token = cp_lexer_consume_token (parser->lexer);
3391
3392             switch (token->keyword)
3393               {
3394               case RID_FUNCTION_NAME:
3395                 name = "%<__FUNCTION__%>";
3396                 break;
3397               case RID_PRETTY_FUNCTION_NAME:
3398                 name = "%<__PRETTY_FUNCTION__%>";
3399                 break;
3400               case RID_C99_FUNCTION_NAME:
3401                 name = "%<__func__%>";
3402                 break;
3403               default:
3404                 gcc_unreachable ();
3405               }
3406
3407             if (cp_parser_non_integral_constant_expression (parser, name))
3408               return error_mark_node;
3409
3410             /* Look up the name.  */
3411             return finish_fname (token->u.value);
3412           }
3413
3414         case RID_VA_ARG:
3415           {
3416             tree expression;
3417             tree type;
3418
3419             /* The `__builtin_va_arg' construct is used to handle
3420                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3421             cp_lexer_consume_token (parser->lexer);
3422             /* Look for the opening `('.  */
3423             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3424             /* Now, parse the assignment-expression.  */
3425             expression = cp_parser_assignment_expression (parser,
3426                                                           /*cast_p=*/false, NULL);
3427             /* Look for the `,'.  */
3428             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3429             /* Parse the type-id.  */
3430             type = cp_parser_type_id (parser);
3431             /* Look for the closing `)'.  */
3432             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3433             /* Using `va_arg' in a constant-expression is not
3434                allowed.  */
3435             if (cp_parser_non_integral_constant_expression (parser,
3436                                                             "%<va_arg%>"))
3437               return error_mark_node;
3438             return build_x_va_arg (expression, type);
3439           }
3440
3441         case RID_OFFSETOF:
3442           return cp_parser_builtin_offsetof (parser);
3443
3444         case RID_HAS_NOTHROW_ASSIGN:
3445         case RID_HAS_NOTHROW_CONSTRUCTOR:
3446         case RID_HAS_NOTHROW_COPY:        
3447         case RID_HAS_TRIVIAL_ASSIGN:
3448         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3449         case RID_HAS_TRIVIAL_COPY:        
3450         case RID_HAS_TRIVIAL_DESTRUCTOR:
3451         case RID_HAS_VIRTUAL_DESTRUCTOR:
3452         case RID_IS_ABSTRACT:
3453         case RID_IS_BASE_OF:
3454         case RID_IS_CLASS:
3455         case RID_IS_CONVERTIBLE_TO:
3456         case RID_IS_EMPTY:
3457         case RID_IS_ENUM:
3458         case RID_IS_POD:
3459         case RID_IS_POLYMORPHIC:
3460         case RID_IS_STD_LAYOUT:
3461         case RID_IS_TRIVIAL:
3462         case RID_IS_UNION:
3463           return cp_parser_trait_expr (parser, token->keyword);
3464
3465         /* Objective-C++ expressions.  */
3466         case RID_AT_ENCODE:
3467         case RID_AT_PROTOCOL:
3468         case RID_AT_SELECTOR:
3469           return cp_parser_objc_expression (parser);
3470
3471         default:
3472           cp_parser_error (parser, "expected primary-expression");
3473           return error_mark_node;
3474         }
3475
3476       /* An id-expression can start with either an identifier, a
3477          `::' as the beginning of a qualified-id, or the "operator"
3478          keyword.  */
3479     case CPP_NAME:
3480     case CPP_SCOPE:
3481     case CPP_TEMPLATE_ID:
3482     case CPP_NESTED_NAME_SPECIFIER:
3483       {
3484         tree id_expression;
3485         tree decl;
3486         const char *error_msg;
3487         bool template_p;
3488         bool done;
3489         cp_token *id_expr_token;
3490
3491       id_expression:
3492         /* Parse the id-expression.  */
3493         id_expression
3494           = cp_parser_id_expression (parser,
3495                                      /*template_keyword_p=*/false,
3496                                      /*check_dependency_p=*/true,
3497                                      &template_p,
3498                                      /*declarator_p=*/false,
3499                                      /*optional_p=*/false);
3500         if (id_expression == error_mark_node)
3501           return error_mark_node;
3502         id_expr_token = token;
3503         token = cp_lexer_peek_token (parser->lexer);
3504         done = (token->type != CPP_OPEN_SQUARE
3505                 && token->type != CPP_OPEN_PAREN
3506                 && token->type != CPP_DOT
3507                 && token->type != CPP_DEREF
3508                 && token->type != CPP_PLUS_PLUS
3509                 && token->type != CPP_MINUS_MINUS);
3510         /* If we have a template-id, then no further lookup is
3511            required.  If the template-id was for a template-class, we
3512            will sometimes have a TYPE_DECL at this point.  */
3513         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3514                  || TREE_CODE (id_expression) == TYPE_DECL)
3515           decl = id_expression;
3516         /* Look up the name.  */
3517         else
3518           {
3519             tree ambiguous_decls;
3520
3521             /* If we already know that this lookup is ambiguous, then
3522                we've already issued an error message; there's no reason
3523                to check again.  */
3524             if (id_expr_token->type == CPP_NAME
3525                 && id_expr_token->ambiguous_p)
3526               {
3527                 cp_parser_simulate_error (parser);
3528                 return error_mark_node;
3529               }
3530
3531             decl = cp_parser_lookup_name (parser, id_expression,
3532                                           none_type,
3533                                           template_p,
3534                                           /*is_namespace=*/false,
3535                                           /*check_dependency=*/true,
3536                                           &ambiguous_decls,
3537                                           id_expr_token->location);
3538             /* If the lookup was ambiguous, an error will already have
3539                been issued.  */
3540             if (ambiguous_decls)
3541               return error_mark_node;
3542
3543             /* In Objective-C++, an instance variable (ivar) may be preferred
3544                to whatever cp_parser_lookup_name() found.  */
3545             decl = objc_lookup_ivar (decl, id_expression);
3546
3547             /* If name lookup gives us a SCOPE_REF, then the
3548                qualifying scope was dependent.  */
3549             if (TREE_CODE (decl) == SCOPE_REF)
3550               {
3551                 /* At this point, we do not know if DECL is a valid
3552                    integral constant expression.  We assume that it is
3553                    in fact such an expression, so that code like:
3554
3555                       template <int N> struct A {
3556                         int a[B<N>::i];
3557                       };
3558                      
3559                    is accepted.  At template-instantiation time, we
3560                    will check that B<N>::i is actually a constant.  */
3561                 return decl;
3562               }
3563             /* Check to see if DECL is a local variable in a context
3564                where that is forbidden.  */
3565             if (parser->local_variables_forbidden_p
3566                 && local_variable_p (decl))
3567               {
3568                 /* It might be that we only found DECL because we are
3569                    trying to be generous with pre-ISO scoping rules.
3570                    For example, consider:
3571
3572                      int i;
3573                      void g() {
3574                        for (int i = 0; i < 10; ++i) {}
3575                        extern void f(int j = i);
3576                      }
3577
3578                    Here, name look up will originally find the out
3579                    of scope `i'.  We need to issue a warning message,
3580                    but then use the global `i'.  */
3581                 decl = check_for_out_of_scope_variable (decl);
3582                 if (local_variable_p (decl))
3583                   {
3584                     error_at (id_expr_token->location,
3585                               "local variable %qD may not appear in this context",
3586                               decl);
3587                     return error_mark_node;
3588                   }
3589               }
3590           }
3591
3592         decl = (finish_id_expression
3593                 (id_expression, decl, parser->scope,
3594                  idk,
3595                  parser->integral_constant_expression_p,
3596                  parser->allow_non_integral_constant_expression_p,
3597                  &parser->non_integral_constant_expression_p,
3598                  template_p, done, address_p,
3599                  template_arg_p,
3600                  &error_msg,
3601                  id_expr_token->location));
3602         if (error_msg)
3603           cp_parser_error (parser, error_msg);
3604         return decl;
3605       }
3606
3607       /* Anything else is an error.  */
3608     default:
3609       cp_parser_error (parser, "expected primary-expression");
3610       return error_mark_node;
3611     }
3612 }
3613
3614 /* Parse an id-expression.
3615
3616    id-expression:
3617      unqualified-id
3618      qualified-id
3619
3620    qualified-id:
3621      :: [opt] nested-name-specifier template [opt] unqualified-id
3622      :: identifier
3623      :: operator-function-id
3624      :: template-id
3625
3626    Return a representation of the unqualified portion of the
3627    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3628    a `::' or nested-name-specifier.
3629
3630    Often, if the id-expression was a qualified-id, the caller will
3631    want to make a SCOPE_REF to represent the qualified-id.  This
3632    function does not do this in order to avoid wastefully creating
3633    SCOPE_REFs when they are not required.
3634
3635    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3636    `template' keyword.
3637
3638    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3639    uninstantiated templates.
3640
3641    If *TEMPLATE_P is non-NULL, it is set to true iff the
3642    `template' keyword is used to explicitly indicate that the entity
3643    named is a template.
3644
3645    If DECLARATOR_P is true, the id-expression is appearing as part of
3646    a declarator, rather than as part of an expression.  */
3647
3648 static tree
3649 cp_parser_id_expression (cp_parser *parser,
3650                          bool template_keyword_p,
3651                          bool check_dependency_p,
3652                          bool *template_p,
3653                          bool declarator_p,
3654                          bool optional_p)
3655 {
3656   bool global_scope_p;
3657   bool nested_name_specifier_p;
3658
3659   /* Assume the `template' keyword was not used.  */
3660   if (template_p)
3661     *template_p = template_keyword_p;
3662
3663   /* Look for the optional `::' operator.  */
3664   global_scope_p
3665     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3666        != NULL_TREE);
3667   /* Look for the optional nested-name-specifier.  */
3668   nested_name_specifier_p
3669     = (cp_parser_nested_name_specifier_opt (parser,
3670                                             /*typename_keyword_p=*/false,
3671                                             check_dependency_p,
3672                                             /*type_p=*/false,
3673                                             declarator_p)
3674        != NULL_TREE);
3675   /* If there is a nested-name-specifier, then we are looking at
3676      the first qualified-id production.  */
3677   if (nested_name_specifier_p)
3678     {
3679       tree saved_scope;
3680       tree saved_object_scope;
3681       tree saved_qualifying_scope;
3682       tree unqualified_id;
3683       bool is_template;
3684
3685       /* See if the next token is the `template' keyword.  */
3686       if (!template_p)
3687         template_p = &is_template;
3688       *template_p = cp_parser_optional_template_keyword (parser);
3689       /* Name lookup we do during the processing of the
3690          unqualified-id might obliterate SCOPE.  */
3691       saved_scope = parser->scope;
3692       saved_object_scope = parser->object_scope;
3693       saved_qualifying_scope = parser->qualifying_scope;
3694       /* Process the final unqualified-id.  */
3695       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3696                                                  check_dependency_p,
3697                                                  declarator_p,
3698                                                  /*optional_p=*/false);
3699       /* Restore the SAVED_SCOPE for our caller.  */
3700       parser->scope = saved_scope;
3701       parser->object_scope = saved_object_scope;
3702       parser->qualifying_scope = saved_qualifying_scope;
3703
3704       return unqualified_id;
3705     }
3706   /* Otherwise, if we are in global scope, then we are looking at one
3707      of the other qualified-id productions.  */
3708   else if (global_scope_p)
3709     {
3710       cp_token *token;
3711       tree id;
3712
3713       /* Peek at the next token.  */
3714       token = cp_lexer_peek_token (parser->lexer);
3715
3716       /* If it's an identifier, and the next token is not a "<", then
3717          we can avoid the template-id case.  This is an optimization
3718          for this common case.  */
3719       if (token->type == CPP_NAME
3720           && !cp_parser_nth_token_starts_template_argument_list_p
3721                (parser, 2))
3722         return cp_parser_identifier (parser);
3723
3724       cp_parser_parse_tentatively (parser);
3725       /* Try a template-id.  */
3726       id = cp_parser_template_id (parser,
3727                                   /*template_keyword_p=*/false,
3728                                   /*check_dependency_p=*/true,
3729                                   declarator_p);
3730       /* If that worked, we're done.  */
3731       if (cp_parser_parse_definitely (parser))
3732         return id;
3733
3734       /* Peek at the next token.  (Changes in the token buffer may
3735          have invalidated the pointer obtained above.)  */
3736       token = cp_lexer_peek_token (parser->lexer);
3737
3738       switch (token->type)
3739         {
3740         case CPP_NAME:
3741           return cp_parser_identifier (parser);
3742
3743         case CPP_KEYWORD:
3744           if (token->keyword == RID_OPERATOR)
3745             return cp_parser_operator_function_id (parser);
3746           /* Fall through.  */
3747
3748         default:
3749           cp_parser_error (parser, "expected id-expression");
3750           return error_mark_node;
3751         }
3752     }
3753   else
3754     return cp_parser_unqualified_id (parser, template_keyword_p,
3755                                      /*check_dependency_p=*/true,
3756                                      declarator_p,
3757                                      optional_p);
3758 }
3759
3760 /* Parse an unqualified-id.
3761
3762    unqualified-id:
3763      identifier
3764      operator-function-id
3765      conversion-function-id
3766      ~ class-name
3767      template-id
3768
3769    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3770    keyword, in a construct like `A::template ...'.
3771
3772    Returns a representation of unqualified-id.  For the `identifier'
3773    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3774    production a BIT_NOT_EXPR is returned; the operand of the
3775    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3776    other productions, see the documentation accompanying the
3777    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3778    names are looked up in uninstantiated templates.  If DECLARATOR_P
3779    is true, the unqualified-id is appearing as part of a declarator,
3780    rather than as part of an expression.  */
3781
3782 static tree
3783 cp_parser_unqualified_id (cp_parser* parser,
3784                           bool template_keyword_p,
3785                           bool check_dependency_p,
3786                           bool declarator_p,
3787                           bool optional_p)
3788 {
3789   cp_token *token;
3790
3791   /* Peek at the next token.  */
3792   token = cp_lexer_peek_token (parser->lexer);
3793
3794   switch (token->type)
3795     {
3796     case CPP_NAME:
3797       {
3798         tree id;
3799
3800         /* We don't know yet whether or not this will be a
3801            template-id.  */
3802         cp_parser_parse_tentatively (parser);
3803         /* Try a template-id.  */
3804         id = cp_parser_template_id (parser, template_keyword_p,
3805                                     check_dependency_p,
3806                                     declarator_p);
3807         /* If it worked, we're done.  */
3808         if (cp_parser_parse_definitely (parser))
3809           return id;
3810         /* Otherwise, it's an ordinary identifier.  */
3811         return cp_parser_identifier (parser);
3812       }
3813
3814     case CPP_TEMPLATE_ID:
3815       return cp_parser_template_id (parser, template_keyword_p,
3816                                     check_dependency_p,
3817                                     declarator_p);
3818
3819     case CPP_COMPL:
3820       {
3821         tree type_decl;
3822         tree qualifying_scope;
3823         tree object_scope;
3824         tree scope;
3825         bool done;
3826
3827         /* Consume the `~' token.  */
3828         cp_lexer_consume_token (parser->lexer);
3829         /* Parse the class-name.  The standard, as written, seems to
3830            say that:
3831
3832              template <typename T> struct S { ~S (); };
3833              template <typename T> S<T>::~S() {}
3834
3835            is invalid, since `~' must be followed by a class-name, but
3836            `S<T>' is dependent, and so not known to be a class.
3837            That's not right; we need to look in uninstantiated
3838            templates.  A further complication arises from:
3839
3840              template <typename T> void f(T t) {
3841                t.T::~T();
3842              }
3843
3844            Here, it is not possible to look up `T' in the scope of `T'
3845            itself.  We must look in both the current scope, and the
3846            scope of the containing complete expression.
3847
3848            Yet another issue is:
3849
3850              struct S {
3851                int S;
3852                ~S();
3853              };
3854
3855              S::~S() {}
3856
3857            The standard does not seem to say that the `S' in `~S'
3858            should refer to the type `S' and not the data member
3859            `S::S'.  */
3860
3861         /* DR 244 says that we look up the name after the "~" in the
3862            same scope as we looked up the qualifying name.  That idea
3863            isn't fully worked out; it's more complicated than that.  */
3864         scope = parser->scope;
3865         object_scope = parser->object_scope;
3866         qualifying_scope = parser->qualifying_scope;
3867
3868         /* Check for invalid scopes.  */
3869         if (scope == error_mark_node)
3870           {
3871             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3872               cp_lexer_consume_token (parser->lexer);
3873             return error_mark_node;
3874           }
3875         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3876           {
3877             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878               error_at (token->location,
3879                         "scope %qT before %<~%> is not a class-name",
3880                         scope);
3881             cp_parser_simulate_error (parser);
3882             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3883               cp_lexer_consume_token (parser->lexer);
3884             return error_mark_node;
3885           }
3886         gcc_assert (!scope || TYPE_P (scope));
3887
3888         /* If the name is of the form "X::~X" it's OK.  */
3889         token = cp_lexer_peek_token (parser->lexer);
3890         if (scope
3891             && token->type == CPP_NAME
3892             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3893                 == CPP_OPEN_PAREN)
3894             && constructor_name_p (token->u.value, scope))
3895           {
3896             cp_lexer_consume_token (parser->lexer);
3897             return build_nt (BIT_NOT_EXPR, scope);
3898           }
3899
3900         /* If there was an explicit qualification (S::~T), first look
3901            in the scope given by the qualification (i.e., S).  */
3902         done = false;
3903         type_decl = NULL_TREE;
3904         if (scope)
3905           {
3906             cp_parser_parse_tentatively (parser);
3907             type_decl = cp_parser_class_name (parser,
3908                                               /*typename_keyword_p=*/false,
3909                                               /*template_keyword_p=*/false,
3910                                               none_type,
3911                                               /*check_dependency=*/false,
3912                                               /*class_head_p=*/false,
3913                                               declarator_p);
3914             if (cp_parser_parse_definitely (parser))
3915               done = true;
3916           }
3917         /* In "N::S::~S", look in "N" as well.  */
3918         if (!done && scope && qualifying_scope)
3919           {
3920             cp_parser_parse_tentatively (parser);
3921             parser->scope = qualifying_scope;
3922             parser->object_scope = NULL_TREE;
3923             parser->qualifying_scope = NULL_TREE;
3924             type_decl
3925               = cp_parser_class_name (parser,
3926                                       /*typename_keyword_p=*/false,
3927                                       /*template_keyword_p=*/false,
3928                                       none_type,
3929                                       /*check_dependency=*/false,
3930                                       /*class_head_p=*/false,
3931                                       declarator_p);
3932             if (cp_parser_parse_definitely (parser))
3933               done = true;
3934           }
3935         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3936         else if (!done && object_scope)
3937           {
3938             cp_parser_parse_tentatively (parser);
3939             parser->scope = object_scope;
3940             parser->object_scope = NULL_TREE;
3941             parser->qualifying_scope = NULL_TREE;
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 (cp_parser_parse_definitely (parser))
3951               done = true;
3952           }
3953         /* Look in the surrounding context.  */
3954         if (!done)
3955           {
3956             parser->scope = NULL_TREE;
3957             parser->object_scope = NULL_TREE;
3958             parser->qualifying_scope = NULL_TREE;
3959             if (processing_template_decl)
3960               cp_parser_parse_tentatively (parser);
3961             type_decl
3962               = cp_parser_class_name (parser,
3963                                       /*typename_keyword_p=*/false,
3964                                       /*template_keyword_p=*/false,
3965                                       none_type,
3966                                       /*check_dependency=*/false,
3967                                       /*class_head_p=*/false,
3968                                       declarator_p);
3969             if (processing_template_decl
3970                 && ! cp_parser_parse_definitely (parser))
3971               {
3972                 /* We couldn't find a type with this name, so just accept
3973                    it and check for a match at instantiation time.  */
3974                 type_decl = cp_parser_identifier (parser);
3975                 if (type_decl != error_mark_node)
3976                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3977                 return type_decl;
3978               }
3979           }
3980         /* If an error occurred, assume that the name of the
3981            destructor is the same as the name of the qualifying
3982            class.  That allows us to keep parsing after running
3983            into ill-formed destructor names.  */
3984         if (type_decl == error_mark_node && scope)
3985           return build_nt (BIT_NOT_EXPR, scope);
3986         else if (type_decl == error_mark_node)
3987           return error_mark_node;
3988
3989         /* Check that destructor name and scope match.  */
3990         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3991           {
3992             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3993               error_at (token->location,
3994                         "declaration of %<~%T%> as member of %qT",
3995                         type_decl, scope);
3996             cp_parser_simulate_error (parser);
3997             return error_mark_node;
3998           }
3999
4000         /* [class.dtor]
4001
4002            A typedef-name that names a class shall not be used as the
4003            identifier in the declarator for a destructor declaration.  */
4004         if (declarator_p
4005             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4006             && !DECL_SELF_REFERENCE_P (type_decl)
4007             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4008           error_at (token->location,
4009                     "typedef-name %qD used as destructor declarator",
4010                     type_decl);
4011
4012         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4013       }
4014
4015     case CPP_KEYWORD:
4016       if (token->keyword == RID_OPERATOR)
4017         {
4018           tree id;
4019
4020           /* This could be a template-id, so we try that first.  */
4021           cp_parser_parse_tentatively (parser);
4022           /* Try a template-id.  */
4023           id = cp_parser_template_id (parser, template_keyword_p,
4024                                       /*check_dependency_p=*/true,
4025                                       declarator_p);
4026           /* If that worked, we're done.  */
4027           if (cp_parser_parse_definitely (parser))
4028             return id;
4029           /* We still don't know whether we're looking at an
4030              operator-function-id or a conversion-function-id.  */
4031           cp_parser_parse_tentatively (parser);
4032           /* Try an operator-function-id.  */
4033           id = cp_parser_operator_function_id (parser);
4034           /* If that didn't work, try a conversion-function-id.  */
4035           if (!cp_parser_parse_definitely (parser))
4036             id = cp_parser_conversion_function_id (parser);
4037
4038           return id;
4039         }
4040       /* Fall through.  */
4041
4042     default:
4043       if (optional_p)
4044         return NULL_TREE;
4045       cp_parser_error (parser, "expected unqualified-id");
4046       return error_mark_node;
4047     }
4048 }
4049
4050 /* Parse an (optional) nested-name-specifier.
4051
4052    nested-name-specifier: [C++98]
4053      class-or-namespace-name :: nested-name-specifier [opt]
4054      class-or-namespace-name :: template nested-name-specifier [opt]
4055
4056    nested-name-specifier: [C++0x]
4057      type-name ::
4058      namespace-name ::
4059      nested-name-specifier identifier ::
4060      nested-name-specifier template [opt] simple-template-id ::
4061
4062    PARSER->SCOPE should be set appropriately before this function is
4063    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4064    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4065    in name lookups.
4066
4067    Sets PARSER->SCOPE to the class (TYPE) or namespace
4068    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4069    it unchanged if there is no nested-name-specifier.  Returns the new
4070    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4071
4072    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4073    part of a declaration and/or decl-specifier.  */
4074
4075 static tree
4076 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4077                                      bool typename_keyword_p,
4078                                      bool check_dependency_p,
4079                                      bool type_p,
4080                                      bool is_declaration)
4081 {
4082   bool success = false;
4083   cp_token_position start = 0;
4084   cp_token *token;
4085
4086   /* Remember where the nested-name-specifier starts.  */
4087   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4088     {
4089       start = cp_lexer_token_position (parser->lexer, false);
4090       push_deferring_access_checks (dk_deferred);
4091     }
4092
4093   while (true)
4094     {
4095       tree new_scope;
4096       tree old_scope;
4097       tree saved_qualifying_scope;
4098       bool template_keyword_p;
4099
4100       /* Spot cases that cannot be the beginning of a
4101          nested-name-specifier.  */
4102       token = cp_lexer_peek_token (parser->lexer);
4103
4104       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4105          the already parsed nested-name-specifier.  */
4106       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4107         {
4108           /* Grab the nested-name-specifier and continue the loop.  */
4109           cp_parser_pre_parsed_nested_name_specifier (parser);
4110           /* If we originally encountered this nested-name-specifier
4111              with IS_DECLARATION set to false, we will not have
4112              resolved TYPENAME_TYPEs, so we must do so here.  */
4113           if (is_declaration
4114               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4115             {
4116               new_scope = resolve_typename_type (parser->scope,
4117                                                  /*only_current_p=*/false);
4118               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4119                 parser->scope = new_scope;
4120             }
4121           success = true;
4122           continue;
4123         }
4124
4125       /* Spot cases that cannot be the beginning of a
4126          nested-name-specifier.  On the second and subsequent times
4127          through the loop, we look for the `template' keyword.  */
4128       if (success && token->keyword == RID_TEMPLATE)
4129         ;
4130       /* A template-id can start a nested-name-specifier.  */
4131       else if (token->type == CPP_TEMPLATE_ID)
4132         ;
4133       else
4134         {
4135           /* If the next token is not an identifier, then it is
4136              definitely not a type-name or namespace-name.  */
4137           if (token->type != CPP_NAME)
4138             break;
4139           /* If the following token is neither a `<' (to begin a
4140              template-id), nor a `::', then we are not looking at a
4141              nested-name-specifier.  */
4142           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4143           if (token->type != CPP_SCOPE
4144               && !cp_parser_nth_token_starts_template_argument_list_p
4145                   (parser, 2))
4146             break;
4147         }
4148
4149       /* The nested-name-specifier is optional, so we parse
4150          tentatively.  */
4151       cp_parser_parse_tentatively (parser);
4152
4153       /* Look for the optional `template' keyword, if this isn't the
4154          first time through the loop.  */
4155       if (success)
4156         template_keyword_p = cp_parser_optional_template_keyword (parser);
4157       else
4158         template_keyword_p = false;
4159
4160       /* Save the old scope since the name lookup we are about to do
4161          might destroy it.  */
4162       old_scope = parser->scope;
4163       saved_qualifying_scope = parser->qualifying_scope;
4164       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4165          look up names in "X<T>::I" in order to determine that "Y" is
4166          a template.  So, if we have a typename at this point, we make
4167          an effort to look through it.  */
4168       if (is_declaration
4169           && !typename_keyword_p
4170           && parser->scope
4171           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4172         parser->scope = resolve_typename_type (parser->scope,
4173                                                /*only_current_p=*/false);
4174       /* Parse the qualifying entity.  */
4175       new_scope
4176         = cp_parser_qualifying_entity (parser,
4177                                        typename_keyword_p,
4178                                        template_keyword_p,
4179                                        check_dependency_p,
4180                                        type_p,
4181                                        is_declaration);
4182       /* Look for the `::' token.  */
4183       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4184
4185       /* If we found what we wanted, we keep going; otherwise, we're
4186          done.  */
4187       if (!cp_parser_parse_definitely (parser))
4188         {
4189           bool error_p = false;
4190
4191           /* Restore the OLD_SCOPE since it was valid before the
4192              failed attempt at finding the last
4193              class-or-namespace-name.  */
4194           parser->scope = old_scope;
4195           parser->qualifying_scope = saved_qualifying_scope;
4196           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4197             break;
4198           /* If the next token is an identifier, and the one after
4199              that is a `::', then any valid interpretation would have
4200              found a class-or-namespace-name.  */
4201           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4202                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4203                      == CPP_SCOPE)
4204                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4205                      != CPP_COMPL))
4206             {
4207               token = cp_lexer_consume_token (parser->lexer);
4208               if (!error_p)
4209                 {
4210                   if (!token->ambiguous_p)
4211                     {
4212                       tree decl;
4213                       tree ambiguous_decls;
4214
4215                       decl = cp_parser_lookup_name (parser, token->u.value,
4216                                                     none_type,
4217                                                     /*is_template=*/false,
4218                                                     /*is_namespace=*/false,
4219                                                     /*check_dependency=*/true,
4220                                                     &ambiguous_decls,
4221                                                     token->location);
4222                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4223                         error_at (token->location,
4224                                   "%qD used without template parameters",
4225                                   decl);
4226                       else if (ambiguous_decls)
4227                         {
4228                           error_at (token->location,
4229                                     "reference to %qD is ambiguous",
4230                                     token->u.value);
4231                           print_candidates (ambiguous_decls);
4232                           decl = error_mark_node;
4233                         }
4234                       else
4235                         {
4236                           const char* msg = "is not a class or namespace";
4237                           if (cxx_dialect != cxx98)
4238                             msg = "is not a class, namespace, or enumeration";
4239                           cp_parser_name_lookup_error
4240                             (parser, token->u.value, decl, msg,
4241                              token->location);
4242                         }
4243                     }
4244                   parser->scope = error_mark_node;
4245                   error_p = true;
4246                   /* Treat this as a successful nested-name-specifier
4247                      due to:
4248
4249                      [basic.lookup.qual]
4250
4251                      If the name found is not a class-name (clause
4252                      _class_) or namespace-name (_namespace.def_), the
4253                      program is ill-formed.  */
4254                   success = true;
4255                 }
4256               cp_lexer_consume_token (parser->lexer);
4257             }
4258           break;
4259         }
4260       /* We've found one valid nested-name-specifier.  */
4261       success = true;
4262       /* Name lookup always gives us a DECL.  */
4263       if (TREE_CODE (new_scope) == TYPE_DECL)
4264         new_scope = TREE_TYPE (new_scope);
4265       /* Uses of "template" must be followed by actual templates.  */
4266       if (template_keyword_p
4267           && !(CLASS_TYPE_P (new_scope)
4268                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4269                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4270                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4271           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4272                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4273                    == TEMPLATE_ID_EXPR)))
4274         permerror (input_location, TYPE_P (new_scope)
4275                    ? "%qT is not a template"
4276                    : "%qD is not a template",
4277                    new_scope);
4278       /* If it is a class scope, try to complete it; we are about to
4279          be looking up names inside the class.  */
4280       if (TYPE_P (new_scope)
4281           /* Since checking types for dependency can be expensive,
4282              avoid doing it if the type is already complete.  */
4283           && !COMPLETE_TYPE_P (new_scope)
4284           /* Do not try to complete dependent types.  */
4285           && !dependent_type_p (new_scope))
4286         {
4287           new_scope = complete_type (new_scope);
4288           /* If it is a typedef to current class, use the current
4289              class instead, as the typedef won't have any names inside
4290              it yet.  */
4291           if (!COMPLETE_TYPE_P (new_scope)
4292               && currently_open_class (new_scope))
4293             new_scope = TYPE_MAIN_VARIANT (new_scope);
4294         }
4295       /* Make sure we look in the right scope the next time through
4296          the loop.  */
4297       parser->scope = new_scope;
4298     }
4299
4300   /* If parsing tentatively, replace the sequence of tokens that makes
4301      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4302      token.  That way, should we re-parse the token stream, we will
4303      not have to repeat the effort required to do the parse, nor will
4304      we issue duplicate error messages.  */
4305   if (success && start)
4306     {
4307       cp_token *token;
4308
4309       token = cp_lexer_token_at (parser->lexer, start);
4310       /* Reset the contents of the START token.  */
4311       token->type = CPP_NESTED_NAME_SPECIFIER;
4312       /* Retrieve any deferred checks.  Do not pop this access checks yet
4313          so the memory will not be reclaimed during token replacing below.  */
4314       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4315       token->u.tree_check_value->value = parser->scope;
4316       token->u.tree_check_value->checks = get_deferred_access_checks ();
4317       token->u.tree_check_value->qualifying_scope =
4318         parser->qualifying_scope;
4319       token->keyword = RID_MAX;
4320
4321       /* Purge all subsequent tokens.  */
4322       cp_lexer_purge_tokens_after (parser->lexer, start);
4323     }
4324
4325   if (start)
4326     pop_to_parent_deferring_access_checks ();
4327
4328   return success ? parser->scope : NULL_TREE;
4329 }
4330
4331 /* Parse a nested-name-specifier.  See
4332    cp_parser_nested_name_specifier_opt for details.  This function
4333    behaves identically, except that it will an issue an error if no
4334    nested-name-specifier is present.  */
4335
4336 static tree
4337 cp_parser_nested_name_specifier (cp_parser *parser,
4338                                  bool typename_keyword_p,
4339                                  bool check_dependency_p,
4340                                  bool type_p,
4341                                  bool is_declaration)
4342 {
4343   tree scope;
4344
4345   /* Look for the nested-name-specifier.  */
4346   scope = cp_parser_nested_name_specifier_opt (parser,
4347                                                typename_keyword_p,
4348                                                check_dependency_p,
4349                                                type_p,
4350                                                is_declaration);
4351   /* If it was not present, issue an error message.  */
4352   if (!scope)
4353     {
4354       cp_parser_error (parser, "expected nested-name-specifier");
4355       parser->scope = NULL_TREE;
4356     }
4357
4358   return scope;
4359 }
4360
4361 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4362    this is either a class-name or a namespace-name (which corresponds
4363    to the class-or-namespace-name production in the grammar). For
4364    C++0x, it can also be a type-name that refers to an enumeration
4365    type.
4366
4367    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4368    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4369    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4370    TYPE_P is TRUE iff the next name should be taken as a class-name,
4371    even the same name is declared to be another entity in the same
4372    scope.
4373
4374    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4375    specified by the class-or-namespace-name.  If neither is found the
4376    ERROR_MARK_NODE is returned.  */
4377
4378 static tree
4379 cp_parser_qualifying_entity (cp_parser *parser,
4380                              bool typename_keyword_p,
4381                              bool template_keyword_p,
4382                              bool check_dependency_p,
4383                              bool type_p,
4384                              bool is_declaration)
4385 {
4386   tree saved_scope;
4387   tree saved_qualifying_scope;
4388   tree saved_object_scope;
4389   tree scope;
4390   bool only_class_p;
4391   bool successful_parse_p;
4392
4393   /* Before we try to parse the class-name, we must save away the
4394      current PARSER->SCOPE since cp_parser_class_name will destroy
4395      it.  */
4396   saved_scope = parser->scope;
4397   saved_qualifying_scope = parser->qualifying_scope;
4398   saved_object_scope = parser->object_scope;
4399   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4400      there is no need to look for a namespace-name.  */
4401   only_class_p = template_keyword_p 
4402     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4403   if (!only_class_p)
4404     cp_parser_parse_tentatively (parser);
4405   scope = cp_parser_class_name (parser,
4406                                 typename_keyword_p,
4407                                 template_keyword_p,
4408                                 type_p ? class_type : none_type,
4409                                 check_dependency_p,
4410                                 /*class_head_p=*/false,
4411                                 is_declaration);
4412   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4413   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4414   if (!only_class_p 
4415       && cxx_dialect != cxx98
4416       && !successful_parse_p)
4417     {
4418       /* Restore the saved scope.  */
4419       parser->scope = saved_scope;
4420       parser->qualifying_scope = saved_qualifying_scope;
4421       parser->object_scope = saved_object_scope;
4422
4423       /* Parse tentatively.  */
4424       cp_parser_parse_tentatively (parser);
4425      
4426       /* Parse a typedef-name or enum-name.  */
4427       scope = cp_parser_nonclass_name (parser);
4428       successful_parse_p = cp_parser_parse_definitely (parser);
4429     }
4430   /* If that didn't work, try for a namespace-name.  */
4431   if (!only_class_p && !successful_parse_p)
4432     {
4433       /* Restore the saved scope.  */
4434       parser->scope = saved_scope;
4435       parser->qualifying_scope = saved_qualifying_scope;
4436       parser->object_scope = saved_object_scope;
4437       /* If we are not looking at an identifier followed by the scope
4438          resolution operator, then this is not part of a
4439          nested-name-specifier.  (Note that this function is only used
4440          to parse the components of a nested-name-specifier.)  */
4441       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4442           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4443         return error_mark_node;
4444       scope = cp_parser_namespace_name (parser);
4445     }
4446
4447   return scope;
4448 }
4449
4450 /* Parse a postfix-expression.
4451
4452    postfix-expression:
4453      primary-expression
4454      postfix-expression [ expression ]
4455      postfix-expression ( expression-list [opt] )
4456      simple-type-specifier ( expression-list [opt] )
4457      typename :: [opt] nested-name-specifier identifier
4458        ( expression-list [opt] )
4459      typename :: [opt] nested-name-specifier template [opt] template-id
4460        ( expression-list [opt] )
4461      postfix-expression . template [opt] id-expression
4462      postfix-expression -> template [opt] id-expression
4463      postfix-expression . pseudo-destructor-name
4464      postfix-expression -> pseudo-destructor-name
4465      postfix-expression ++
4466      postfix-expression --
4467      dynamic_cast < type-id > ( expression )
4468      static_cast < type-id > ( expression )
4469      reinterpret_cast < type-id > ( expression )
4470      const_cast < type-id > ( expression )
4471      typeid ( expression )
4472      typeid ( type-id )
4473
4474    GNU Extension:
4475
4476    postfix-expression:
4477      ( type-id ) { initializer-list , [opt] }
4478
4479    This extension is a GNU version of the C99 compound-literal
4480    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4481    but they are essentially the same concept.)
4482
4483    If ADDRESS_P is true, the postfix expression is the operand of the
4484    `&' operator.  CAST_P is true if this expression is the target of a
4485    cast.
4486
4487    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4488    class member access expressions [expr.ref].
4489
4490    Returns a representation of the expression.  */
4491
4492 static tree
4493 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4494                               bool member_access_only_p,
4495                               cp_id_kind * pidk_return)
4496 {
4497   cp_token *token;
4498   enum rid keyword;
4499   cp_id_kind idk = CP_ID_KIND_NONE;
4500   tree postfix_expression = NULL_TREE;
4501   bool is_member_access = false;
4502
4503   /* Peek at the next token.  */
4504   token = cp_lexer_peek_token (parser->lexer);
4505   /* Some of the productions are determined by keywords.  */
4506   keyword = token->keyword;
4507   switch (keyword)
4508     {
4509     case RID_DYNCAST:
4510     case RID_STATCAST:
4511     case RID_REINTCAST:
4512     case RID_CONSTCAST:
4513       {
4514         tree type;
4515         tree expression;
4516         const char *saved_message;
4517
4518         /* All of these can be handled in the same way from the point
4519            of view of parsing.  Begin by consuming the token
4520            identifying the cast.  */
4521         cp_lexer_consume_token (parser->lexer);
4522
4523         /* New types cannot be defined in the cast.  */
4524         saved_message = parser->type_definition_forbidden_message;
4525         parser->type_definition_forbidden_message
4526           = "types may not be defined in casts";
4527
4528         /* Look for the opening `<'.  */
4529         cp_parser_require (parser, CPP_LESS, "%<<%>");
4530         /* Parse the type to which we are casting.  */
4531         type = cp_parser_type_id (parser);
4532         /* Look for the closing `>'.  */
4533         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4534         /* Restore the old message.  */
4535         parser->type_definition_forbidden_message = saved_message;
4536
4537         /* And the expression which is being cast.  */
4538         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4539         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4540         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541
4542         /* Only type conversions to integral or enumeration types
4543            can be used in constant-expressions.  */
4544         if (!cast_valid_in_integral_constant_expression_p (type)
4545             && (cp_parser_non_integral_constant_expression
4546                 (parser,
4547                  "a cast to a type other than an integral or "
4548                  "enumeration type")))
4549           return error_mark_node;
4550
4551         switch (keyword)
4552           {
4553           case RID_DYNCAST:
4554             postfix_expression
4555               = build_dynamic_cast (type, expression, tf_warning_or_error);
4556             break;
4557           case RID_STATCAST:
4558             postfix_expression
4559               = build_static_cast (type, expression, tf_warning_or_error);
4560             break;
4561           case RID_REINTCAST:
4562             postfix_expression
4563               = build_reinterpret_cast (type, expression, 
4564                                         tf_warning_or_error);
4565             break;
4566           case RID_CONSTCAST:
4567             postfix_expression
4568               = build_const_cast (type, expression, tf_warning_or_error);
4569             break;
4570           default:
4571             gcc_unreachable ();
4572           }
4573       }
4574       break;
4575
4576     case RID_TYPEID:
4577       {
4578         tree type;
4579         const char *saved_message;
4580         bool saved_in_type_id_in_expr_p;
4581
4582         /* Consume the `typeid' token.  */
4583         cp_lexer_consume_token (parser->lexer);
4584         /* Look for the `(' token.  */
4585         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4586         /* Types cannot be defined in a `typeid' expression.  */
4587         saved_message = parser->type_definition_forbidden_message;
4588         parser->type_definition_forbidden_message
4589           = "types may not be defined in a %<typeid%> expression";
4590         /* We can't be sure yet whether we're looking at a type-id or an
4591            expression.  */
4592         cp_parser_parse_tentatively (parser);
4593         /* Try a type-id first.  */
4594         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4595         parser->in_type_id_in_expr_p = true;
4596         type = cp_parser_type_id (parser);
4597         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4598         /* Look for the `)' token.  Otherwise, we can't be sure that
4599            we're not looking at an expression: consider `typeid (int
4600            (3))', for example.  */
4601         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4602         /* If all went well, simply lookup the type-id.  */
4603         if (cp_parser_parse_definitely (parser))
4604           postfix_expression = get_typeid (type);
4605         /* Otherwise, fall back to the expression variant.  */
4606         else
4607           {
4608             tree expression;
4609
4610             /* Look for an expression.  */
4611             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4612             /* Compute its typeid.  */
4613             postfix_expression = build_typeid (expression);
4614             /* Look for the `)' token.  */
4615             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4616           }
4617         /* Restore the saved message.  */
4618         parser->type_definition_forbidden_message = saved_message;
4619         /* `typeid' may not appear in an integral constant expression.  */
4620         if (cp_parser_non_integral_constant_expression(parser,
4621                                                        "%<typeid%> operator"))
4622           return error_mark_node;
4623       }
4624       break;
4625
4626     case RID_TYPENAME:
4627       {
4628         tree type;
4629         /* The syntax permitted here is the same permitted for an
4630            elaborated-type-specifier.  */
4631         type = cp_parser_elaborated_type_specifier (parser,
4632                                                     /*is_friend=*/false,
4633                                                     /*is_declaration=*/false);
4634         postfix_expression = cp_parser_functional_cast (parser, type);
4635       }
4636       break;
4637
4638     default:
4639       {
4640         tree type;
4641
4642         /* If the next thing is a simple-type-specifier, we may be
4643            looking at a functional cast.  We could also be looking at
4644            an id-expression.  So, we try the functional cast, and if
4645            that doesn't work we fall back to the primary-expression.  */
4646         cp_parser_parse_tentatively (parser);
4647         /* Look for the simple-type-specifier.  */
4648         type = cp_parser_simple_type_specifier (parser,
4649                                                 /*decl_specs=*/NULL,
4650                                                 CP_PARSER_FLAGS_NONE);
4651         /* Parse the cast itself.  */
4652         if (!cp_parser_error_occurred (parser))
4653           postfix_expression
4654             = cp_parser_functional_cast (parser, type);
4655         /* If that worked, we're done.  */
4656         if (cp_parser_parse_definitely (parser))
4657           break;
4658
4659         /* If the functional-cast didn't work out, try a
4660            compound-literal.  */
4661         if (cp_parser_allow_gnu_extensions_p (parser)
4662             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4663           {
4664             VEC(constructor_elt,gc) *initializer_list = NULL;
4665             bool saved_in_type_id_in_expr_p;
4666
4667             cp_parser_parse_tentatively (parser);
4668             /* Consume the `('.  */
4669             cp_lexer_consume_token (parser->lexer);
4670             /* Parse the type.  */
4671             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4672             parser->in_type_id_in_expr_p = true;
4673             type = cp_parser_type_id (parser);
4674             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4675             /* Look for the `)'.  */
4676             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4677             /* Look for the `{'.  */
4678             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4679             /* If things aren't going well, there's no need to
4680                keep going.  */
4681             if (!cp_parser_error_occurred (parser))
4682               {
4683                 bool non_constant_p;
4684                 /* Parse the initializer-list.  */
4685                 initializer_list
4686                   = cp_parser_initializer_list (parser, &non_constant_p);
4687                 /* Allow a trailing `,'.  */
4688                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4689                   cp_lexer_consume_token (parser->lexer);
4690                 /* Look for the final `}'.  */
4691                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4692               }
4693             /* If that worked, we're definitely looking at a
4694                compound-literal expression.  */
4695             if (cp_parser_parse_definitely (parser))
4696               {
4697                 /* Warn the user that a compound literal is not
4698                    allowed in standard C++.  */
4699                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4700                 /* For simplicity, we disallow compound literals in
4701                    constant-expressions.  We could
4702                    allow compound literals of integer type, whose
4703                    initializer was a constant, in constant
4704                    expressions.  Permitting that usage, as a further
4705                    extension, would not change the meaning of any
4706                    currently accepted programs.  (Of course, as
4707                    compound literals are not part of ISO C++, the
4708                    standard has nothing to say.)  */
4709                 if (cp_parser_non_integral_constant_expression 
4710                     (parser, "non-constant compound literals"))
4711                   {
4712                     postfix_expression = error_mark_node;
4713                     break;
4714                   }
4715                 /* Form the representation of the compound-literal.  */
4716                 postfix_expression
4717                   = (finish_compound_literal
4718                      (type, build_constructor (init_list_type_node,
4719                                                initializer_list)));
4720                 break;
4721               }
4722           }
4723
4724         /* It must be a primary-expression.  */
4725         postfix_expression
4726           = cp_parser_primary_expression (parser, address_p, cast_p,
4727                                           /*template_arg_p=*/false,
4728                                           &idk);
4729       }
4730       break;
4731     }
4732
4733   /* Keep looping until the postfix-expression is complete.  */
4734   while (true)
4735     {
4736       if (idk == CP_ID_KIND_UNQUALIFIED
4737           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4738           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4739         /* It is not a Koenig lookup function call.  */
4740         postfix_expression
4741           = unqualified_name_lookup_error (postfix_expression);
4742
4743       /* Peek at the next token.  */
4744       token = cp_lexer_peek_token (parser->lexer);
4745
4746       switch (token->type)
4747         {
4748         case CPP_OPEN_SQUARE:
4749           postfix_expression
4750             = cp_parser_postfix_open_square_expression (parser,
4751                                                         postfix_expression,
4752                                                         false);
4753           idk = CP_ID_KIND_NONE;
4754           is_member_access = false;
4755           break;
4756
4757         case CPP_OPEN_PAREN:
4758           /* postfix-expression ( expression-list [opt] ) */
4759           {
4760             bool koenig_p;
4761             bool is_builtin_constant_p;
4762             bool saved_integral_constant_expression_p = false;
4763             bool saved_non_integral_constant_expression_p = false;
4764             VEC(tree,gc) *args;
4765
4766             is_member_access = false;
4767
4768             is_builtin_constant_p
4769               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4770             if (is_builtin_constant_p)
4771               {
4772                 /* The whole point of __builtin_constant_p is to allow
4773                    non-constant expressions to appear as arguments.  */
4774                 saved_integral_constant_expression_p
4775                   = parser->integral_constant_expression_p;
4776                 saved_non_integral_constant_expression_p
4777                   = parser->non_integral_constant_expression_p;
4778                 parser->integral_constant_expression_p = false;
4779               }
4780             args = (cp_parser_parenthesized_expression_list
4781                     (parser, /*is_attribute_list=*/false,
4782                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4783                      /*non_constant_p=*/NULL));
4784             if (is_builtin_constant_p)
4785               {
4786                 parser->integral_constant_expression_p
4787                   = saved_integral_constant_expression_p;
4788                 parser->non_integral_constant_expression_p
4789                   = saved_non_integral_constant_expression_p;
4790               }
4791
4792             if (args == NULL)
4793               {
4794                 postfix_expression = error_mark_node;
4795                 break;
4796               }
4797
4798             /* Function calls are not permitted in
4799                constant-expressions.  */
4800             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4801                 && cp_parser_non_integral_constant_expression (parser,
4802                                                                "a function call"))
4803               {
4804                 postfix_expression = error_mark_node;
4805                 release_tree_vector (args);
4806                 break;
4807               }
4808
4809             koenig_p = false;
4810             if (idk == CP_ID_KIND_UNQUALIFIED
4811                 || idk == CP_ID_KIND_TEMPLATE_ID)
4812               {
4813                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4814                   {
4815                     if (!VEC_empty (tree, args))
4816                       {
4817                         koenig_p = true;
4818                         if (!any_type_dependent_arguments_p (args))
4819                           postfix_expression
4820                             = perform_koenig_lookup (postfix_expression, args);
4821                       }
4822                     else
4823                       postfix_expression
4824                         = unqualified_fn_lookup_error (postfix_expression);
4825                   }
4826                 /* We do not perform argument-dependent lookup if
4827                    normal lookup finds a non-function, in accordance
4828                    with the expected resolution of DR 218.  */
4829                 else if (!VEC_empty (tree, args)
4830                          && is_overloaded_fn (postfix_expression))
4831                   {
4832                     tree fn = get_first_fn (postfix_expression);
4833
4834                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4835                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4836
4837                     /* Only do argument dependent lookup if regular
4838                        lookup does not find a set of member functions.
4839                        [basic.lookup.koenig]/2a  */
4840                     if (!DECL_FUNCTION_MEMBER_P (fn))
4841                       {
4842                         koenig_p = true;
4843                         if (!any_type_dependent_arguments_p (args))
4844                           postfix_expression
4845                             = perform_koenig_lookup (postfix_expression, args);
4846                       }
4847                   }
4848               }
4849
4850             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4851               {
4852                 tree instance = TREE_OPERAND (postfix_expression, 0);
4853                 tree fn = TREE_OPERAND (postfix_expression, 1);
4854
4855                 if (processing_template_decl
4856                     && (type_dependent_expression_p (instance)
4857                         || (!BASELINK_P (fn)
4858                             && TREE_CODE (fn) != FIELD_DECL)
4859                         || type_dependent_expression_p (fn)
4860                         || any_type_dependent_arguments_p (args)))
4861                   {
4862                     postfix_expression
4863                       = build_nt_call_vec (postfix_expression, args);
4864                     release_tree_vector (args);
4865                     break;
4866                   }
4867
4868                 if (BASELINK_P (fn))
4869                   {
4870                   postfix_expression
4871                     = (build_new_method_call
4872                        (instance, fn, &args, NULL_TREE,
4873                         (idk == CP_ID_KIND_QUALIFIED
4874                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4875                         /*fn_p=*/NULL,
4876                         tf_warning_or_error));
4877                   }
4878                 else
4879                   postfix_expression
4880                     = finish_call_expr (postfix_expression, &args,
4881                                         /*disallow_virtual=*/false,
4882                                         /*koenig_p=*/false,
4883                                         tf_warning_or_error);
4884               }
4885             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4886                      || TREE_CODE (postfix_expression) == MEMBER_REF
4887                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4888               postfix_expression = (build_offset_ref_call_from_tree
4889                                     (postfix_expression, &args));
4890             else if (idk == CP_ID_KIND_QUALIFIED)
4891               /* A call to a static class member, or a namespace-scope
4892                  function.  */
4893               postfix_expression
4894                 = finish_call_expr (postfix_expression, &args,
4895                                     /*disallow_virtual=*/true,
4896                                     koenig_p,
4897                                     tf_warning_or_error);
4898             else
4899               /* All other function calls.  */
4900               postfix_expression
4901                 = finish_call_expr (postfix_expression, &args,
4902                                     /*disallow_virtual=*/false,
4903                                     koenig_p,
4904                                     tf_warning_or_error);
4905
4906             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4907             idk = CP_ID_KIND_NONE;
4908
4909             release_tree_vector (args);
4910           }
4911           break;
4912
4913         case CPP_DOT:
4914         case CPP_DEREF:
4915           /* postfix-expression . template [opt] id-expression
4916              postfix-expression . pseudo-destructor-name
4917              postfix-expression -> template [opt] id-expression
4918              postfix-expression -> pseudo-destructor-name */
4919
4920           /* Consume the `.' or `->' operator.  */
4921           cp_lexer_consume_token (parser->lexer);
4922
4923           postfix_expression
4924             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4925                                                       postfix_expression,
4926                                                       false, &idk,
4927                                                       token->location);
4928
4929           is_member_access = true;
4930           break;
4931
4932         case CPP_PLUS_PLUS:
4933           /* postfix-expression ++  */
4934           /* Consume the `++' token.  */
4935           cp_lexer_consume_token (parser->lexer);
4936           /* Generate a representation for the complete expression.  */
4937           postfix_expression
4938             = finish_increment_expr (postfix_expression,
4939                                      POSTINCREMENT_EXPR);
4940           /* Increments may not appear in constant-expressions.  */
4941           if (cp_parser_non_integral_constant_expression (parser,
4942                                                           "an increment"))
4943             postfix_expression = error_mark_node;
4944           idk = CP_ID_KIND_NONE;
4945           is_member_access = false;
4946           break;
4947
4948         case CPP_MINUS_MINUS:
4949           /* postfix-expression -- */
4950           /* Consume the `--' token.  */
4951           cp_lexer_consume_token (parser->lexer);
4952           /* Generate a representation for the complete expression.  */
4953           postfix_expression
4954             = finish_increment_expr (postfix_expression,
4955                                      POSTDECREMENT_EXPR);
4956           /* Decrements may not appear in constant-expressions.  */
4957           if (cp_parser_non_integral_constant_expression (parser,
4958                                                           "a decrement"))
4959             postfix_expression = error_mark_node;
4960           idk = CP_ID_KIND_NONE;
4961           is_member_access = false;
4962           break;
4963
4964         default:
4965           if (pidk_return != NULL)
4966             * pidk_return = idk;
4967           if (member_access_only_p)
4968             return is_member_access? postfix_expression : error_mark_node;
4969           else
4970             return postfix_expression;
4971         }
4972     }
4973
4974   /* We should never get here.  */
4975   gcc_unreachable ();
4976   return error_mark_node;
4977 }
4978
4979 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4980    by cp_parser_builtin_offsetof.  We're looking for
4981
4982      postfix-expression [ expression ]
4983
4984    FOR_OFFSETOF is set if we're being called in that context, which
4985    changes how we deal with integer constant expressions.  */
4986
4987 static tree
4988 cp_parser_postfix_open_square_expression (cp_parser *parser,
4989                                           tree postfix_expression,
4990                                           bool for_offsetof)
4991 {
4992   tree index;
4993
4994   /* Consume the `[' token.  */
4995   cp_lexer_consume_token (parser->lexer);
4996
4997   /* Parse the index expression.  */
4998   /* ??? For offsetof, there is a question of what to allow here.  If
4999      offsetof is not being used in an integral constant expression context,
5000      then we *could* get the right answer by computing the value at runtime.
5001      If we are in an integral constant expression context, then we might
5002      could accept any constant expression; hard to say without analysis.
5003      Rather than open the barn door too wide right away, allow only integer
5004      constant expressions here.  */
5005   if (for_offsetof)
5006     index = cp_parser_constant_expression (parser, false, NULL);
5007   else
5008     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5009
5010   /* Look for the closing `]'.  */
5011   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5012
5013   /* Build the ARRAY_REF.  */
5014   postfix_expression = grok_array_decl (postfix_expression, index);
5015
5016   /* When not doing offsetof, array references are not permitted in
5017      constant-expressions.  */
5018   if (!for_offsetof
5019       && (cp_parser_non_integral_constant_expression
5020           (parser, "an array reference")))
5021     postfix_expression = error_mark_node;
5022
5023   return postfix_expression;
5024 }
5025
5026 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5027    by cp_parser_builtin_offsetof.  We're looking for
5028
5029      postfix-expression . template [opt] id-expression
5030      postfix-expression . pseudo-destructor-name
5031      postfix-expression -> template [opt] id-expression
5032      postfix-expression -> pseudo-destructor-name
5033
5034    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5035    limits what of the above we'll actually accept, but nevermind.
5036    TOKEN_TYPE is the "." or "->" token, which will already have been
5037    removed from the stream.  */
5038
5039 static tree
5040 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5041                                         enum cpp_ttype token_type,
5042                                         tree postfix_expression,
5043                                         bool for_offsetof, cp_id_kind *idk,
5044                                         location_t location)
5045 {
5046   tree name;
5047   bool dependent_p;
5048   bool pseudo_destructor_p;
5049   tree scope = NULL_TREE;
5050
5051   /* If this is a `->' operator, dereference the pointer.  */
5052   if (token_type == CPP_DEREF)
5053     postfix_expression = build_x_arrow (postfix_expression);
5054   /* Check to see whether or not the expression is type-dependent.  */
5055   dependent_p = type_dependent_expression_p (postfix_expression);
5056   /* The identifier following the `->' or `.' is not qualified.  */
5057   parser->scope = NULL_TREE;
5058   parser->qualifying_scope = NULL_TREE;
5059   parser->object_scope = NULL_TREE;
5060   *idk = CP_ID_KIND_NONE;
5061
5062   /* Enter the scope corresponding to the type of the object
5063      given by the POSTFIX_EXPRESSION.  */
5064   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5065     {
5066       scope = TREE_TYPE (postfix_expression);
5067       /* According to the standard, no expression should ever have
5068          reference type.  Unfortunately, we do not currently match
5069          the standard in this respect in that our internal representation
5070          of an expression may have reference type even when the standard
5071          says it does not.  Therefore, we have to manually obtain the
5072          underlying type here.  */
5073       scope = non_reference (scope);
5074       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5075       if (scope == unknown_type_node)
5076         {
5077           error_at (location, "%qE does not have class type",
5078                     postfix_expression);
5079           scope = NULL_TREE;
5080         }
5081       else
5082         scope = complete_type_or_else (scope, NULL_TREE);
5083       /* Let the name lookup machinery know that we are processing a
5084          class member access expression.  */
5085       parser->context->object_type = scope;
5086       /* If something went wrong, we want to be able to discern that case,
5087          as opposed to the case where there was no SCOPE due to the type
5088          of expression being dependent.  */
5089       if (!scope)
5090         scope = error_mark_node;
5091       /* If the SCOPE was erroneous, make the various semantic analysis
5092          functions exit quickly -- and without issuing additional error
5093          messages.  */
5094       if (scope == error_mark_node)
5095         postfix_expression = error_mark_node;
5096     }
5097
5098   /* Assume this expression is not a pseudo-destructor access.  */
5099   pseudo_destructor_p = false;
5100
5101   /* If the SCOPE is a scalar type, then, if this is a valid program,
5102      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5103      is type dependent, it can be pseudo-destructor-name or something else.
5104      Try to parse it as pseudo-destructor-name first.  */
5105   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5106     {
5107       tree s;
5108       tree type;
5109
5110       cp_parser_parse_tentatively (parser);
5111       /* Parse the pseudo-destructor-name.  */
5112       s = NULL_TREE;
5113       cp_parser_pseudo_destructor_name (parser, &s, &type);
5114       if (dependent_p
5115           && (cp_parser_error_occurred (parser)
5116               || TREE_CODE (type) != TYPE_DECL
5117               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5118         cp_parser_abort_tentative_parse (parser);
5119       else if (cp_parser_parse_definitely (parser))
5120         {
5121           pseudo_destructor_p = true;
5122           postfix_expression
5123             = finish_pseudo_destructor_expr (postfix_expression,
5124                                              s, TREE_TYPE (type));
5125         }
5126     }
5127
5128   if (!pseudo_destructor_p)
5129     {
5130       /* If the SCOPE is not a scalar type, we are looking at an
5131          ordinary class member access expression, rather than a
5132          pseudo-destructor-name.  */
5133       bool template_p;
5134       cp_token *token = cp_lexer_peek_token (parser->lexer);
5135       /* Parse the id-expression.  */
5136       name = (cp_parser_id_expression
5137               (parser,
5138                cp_parser_optional_template_keyword (parser),
5139                /*check_dependency_p=*/true,
5140                &template_p,
5141                /*declarator_p=*/false,
5142                /*optional_p=*/false));
5143       /* In general, build a SCOPE_REF if the member name is qualified.
5144          However, if the name was not dependent and has already been
5145          resolved; there is no need to build the SCOPE_REF.  For example;
5146
5147              struct X { void f(); };
5148              template <typename T> void f(T* t) { t->X::f(); }
5149
5150          Even though "t" is dependent, "X::f" is not and has been resolved
5151          to a BASELINK; there is no need to include scope information.  */
5152
5153       /* But we do need to remember that there was an explicit scope for
5154          virtual function calls.  */
5155       if (parser->scope)
5156         *idk = CP_ID_KIND_QUALIFIED;
5157
5158       /* If the name is a template-id that names a type, we will get a
5159          TYPE_DECL here.  That is invalid code.  */
5160       if (TREE_CODE (name) == TYPE_DECL)
5161         {
5162           error_at (token->location, "invalid use of %qD", name);
5163           postfix_expression = error_mark_node;
5164         }
5165       else
5166         {
5167           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5168             {
5169               name = build_qualified_name (/*type=*/NULL_TREE,
5170                                            parser->scope,
5171                                            name,
5172                                            template_p);
5173               parser->scope = NULL_TREE;
5174               parser->qualifying_scope = NULL_TREE;
5175               parser->object_scope = NULL_TREE;
5176             }
5177           if (scope && name && BASELINK_P (name))
5178             adjust_result_of_qualified_name_lookup
5179               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5180           postfix_expression
5181             = finish_class_member_access_expr (postfix_expression, name,
5182                                                template_p, 
5183                                                tf_warning_or_error);
5184         }
5185     }
5186
5187   /* We no longer need to look up names in the scope of the object on
5188      the left-hand side of the `.' or `->' operator.  */
5189   parser->context->object_type = NULL_TREE;
5190
5191   /* Outside of offsetof, these operators may not appear in
5192      constant-expressions.  */
5193   if (!for_offsetof
5194       && (cp_parser_non_integral_constant_expression
5195           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5196     postfix_expression = error_mark_node;
5197
5198   return postfix_expression;
5199 }
5200
5201 /* Parse a parenthesized expression-list.
5202
5203    expression-list:
5204      assignment-expression
5205      expression-list, assignment-expression
5206
5207    attribute-list:
5208      expression-list
5209      identifier
5210      identifier, expression-list
5211
5212    CAST_P is true if this expression is the target of a cast.
5213
5214    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5215    argument pack.
5216
5217    Returns a vector of trees.  Each element is a representation of an
5218    assignment-expression.  NULL is returned if the ( and or ) are
5219    missing.  An empty, but allocated, vector is returned on no
5220    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5221    if this is really an attribute list being parsed.  If
5222    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5223    not all of the expressions in the list were constant.  */
5224
5225 static VEC(tree,gc) *
5226 cp_parser_parenthesized_expression_list (cp_parser* parser,
5227                                          bool is_attribute_list,
5228                                          bool cast_p,
5229                                          bool allow_expansion_p,
5230                                          bool *non_constant_p)
5231 {
5232   VEC(tree,gc) *expression_list;
5233   bool fold_expr_p = is_attribute_list;
5234   tree identifier = NULL_TREE;
5235   bool saved_greater_than_is_operator_p;
5236
5237   /* Assume all the expressions will be constant.  */
5238   if (non_constant_p)
5239     *non_constant_p = false;
5240
5241   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5242     return NULL;
5243
5244   expression_list = make_tree_vector ();
5245
5246   /* Within a parenthesized expression, a `>' token is always
5247      the greater-than operator.  */
5248   saved_greater_than_is_operator_p
5249     = parser->greater_than_is_operator_p;
5250   parser->greater_than_is_operator_p = true;
5251
5252   /* Consume expressions until there are no more.  */
5253   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5254     while (true)
5255       {
5256         tree expr;
5257
5258         /* At the beginning of attribute lists, check to see if the
5259            next token is an identifier.  */
5260         if (is_attribute_list
5261             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5262           {
5263             cp_token *token;
5264
5265             /* Consume the identifier.  */
5266             token = cp_lexer_consume_token (parser->lexer);
5267             /* Save the identifier.  */
5268             identifier = token->u.value;
5269           }
5270         else
5271           {
5272             bool expr_non_constant_p;
5273
5274             /* Parse the next assignment-expression.  */
5275             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5276               {
5277                 /* A braced-init-list.  */
5278                 maybe_warn_cpp0x ("extended initializer lists");
5279                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5280                 if (non_constant_p && expr_non_constant_p)
5281                   *non_constant_p = true;
5282               }
5283             else if (non_constant_p)
5284               {
5285                 expr = (cp_parser_constant_expression
5286                         (parser, /*allow_non_constant_p=*/true,
5287                          &expr_non_constant_p));
5288                 if (expr_non_constant_p)
5289                   *non_constant_p = true;
5290               }
5291             else
5292               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5293
5294             if (fold_expr_p)
5295               expr = fold_non_dependent_expr (expr);
5296
5297             /* If we have an ellipsis, then this is an expression
5298                expansion.  */
5299             if (allow_expansion_p
5300                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5301               {
5302                 /* Consume the `...'.  */
5303                 cp_lexer_consume_token (parser->lexer);
5304
5305                 /* Build the argument pack.  */
5306                 expr = make_pack_expansion (expr);
5307               }
5308
5309              /* Add it to the list.  We add error_mark_node
5310                 expressions to the list, so that we can still tell if
5311                 the correct form for a parenthesized expression-list
5312                 is found. That gives better errors.  */
5313             VEC_safe_push (tree, gc, expression_list, expr);
5314
5315             if (expr == error_mark_node)
5316               goto skip_comma;
5317           }
5318
5319         /* After the first item, attribute lists look the same as
5320            expression lists.  */
5321         is_attribute_list = false;
5322
5323       get_comma:;
5324         /* If the next token isn't a `,', then we are done.  */
5325         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5326           break;
5327
5328         /* Otherwise, consume the `,' and keep going.  */
5329         cp_lexer_consume_token (parser->lexer);
5330       }
5331
5332   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5333     {
5334       int ending;
5335
5336     skip_comma:;
5337       /* We try and resync to an unnested comma, as that will give the
5338          user better diagnostics.  */
5339       ending = cp_parser_skip_to_closing_parenthesis (parser,
5340                                                       /*recovering=*/true,
5341                                                       /*or_comma=*/true,
5342                                                       /*consume_paren=*/true);
5343       if (ending < 0)
5344         goto get_comma;
5345       if (!ending)
5346         {
5347           parser->greater_than_is_operator_p
5348             = saved_greater_than_is_operator_p;
5349           return NULL;
5350         }
5351     }
5352
5353   parser->greater_than_is_operator_p
5354     = saved_greater_than_is_operator_p;
5355
5356   if (identifier)
5357     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5358
5359   return expression_list;
5360 }
5361
5362 /* Parse a pseudo-destructor-name.
5363
5364    pseudo-destructor-name:
5365      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5366      :: [opt] nested-name-specifier template template-id :: ~ type-name
5367      :: [opt] nested-name-specifier [opt] ~ type-name
5368
5369    If either of the first two productions is used, sets *SCOPE to the
5370    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5371    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5372    or ERROR_MARK_NODE if the parse fails.  */
5373
5374 static void
5375 cp_parser_pseudo_destructor_name (cp_parser* parser,
5376                                   tree* scope,
5377                                   tree* type)
5378 {
5379   bool nested_name_specifier_p;
5380
5381   /* Assume that things will not work out.  */
5382   *type = error_mark_node;
5383
5384   /* Look for the optional `::' operator.  */
5385   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5386   /* Look for the optional nested-name-specifier.  */
5387   nested_name_specifier_p
5388     = (cp_parser_nested_name_specifier_opt (parser,
5389                                             /*typename_keyword_p=*/false,
5390                                             /*check_dependency_p=*/true,
5391                                             /*type_p=*/false,
5392                                             /*is_declaration=*/false)
5393        != NULL_TREE);
5394   /* Now, if we saw a nested-name-specifier, we might be doing the
5395      second production.  */
5396   if (nested_name_specifier_p
5397       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5398     {
5399       /* Consume the `template' keyword.  */
5400       cp_lexer_consume_token (parser->lexer);
5401       /* Parse the template-id.  */
5402       cp_parser_template_id (parser,
5403                              /*template_keyword_p=*/true,
5404                              /*check_dependency_p=*/false,
5405                              /*is_declaration=*/true);
5406       /* Look for the `::' token.  */
5407       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5408     }
5409   /* If the next token is not a `~', then there might be some
5410      additional qualification.  */
5411   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5412     {
5413       /* At this point, we're looking for "type-name :: ~".  The type-name
5414          must not be a class-name, since this is a pseudo-destructor.  So,
5415          it must be either an enum-name, or a typedef-name -- both of which
5416          are just identifiers.  So, we peek ahead to check that the "::"
5417          and "~" tokens are present; if they are not, then we can avoid
5418          calling type_name.  */
5419       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5420           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5421           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5422         {
5423           cp_parser_error (parser, "non-scalar type");
5424           return;
5425         }
5426
5427       /* Look for the type-name.  */
5428       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5429       if (*scope == error_mark_node)
5430         return;
5431
5432       /* Look for the `::' token.  */
5433       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5434     }
5435   else
5436     *scope = NULL_TREE;
5437
5438   /* Look for the `~'.  */
5439   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5440   /* Look for the type-name again.  We are not responsible for
5441      checking that it matches the first type-name.  */
5442   *type = cp_parser_nonclass_name (parser);
5443 }
5444
5445 /* Parse a unary-expression.
5446
5447    unary-expression:
5448      postfix-expression
5449      ++ cast-expression
5450      -- cast-expression
5451      unary-operator cast-expression
5452      sizeof unary-expression
5453      sizeof ( type-id )
5454      new-expression
5455      delete-expression
5456
5457    GNU Extensions:
5458
5459    unary-expression:
5460      __extension__ cast-expression
5461      __alignof__ unary-expression
5462      __alignof__ ( type-id )
5463      __real__ cast-expression
5464      __imag__ cast-expression
5465      && identifier
5466
5467    ADDRESS_P is true iff the unary-expression is appearing as the
5468    operand of the `&' operator.   CAST_P is true if this expression is
5469    the target of a cast.
5470
5471    Returns a representation of the expression.  */
5472
5473 static tree
5474 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5475                             cp_id_kind * pidk)
5476 {
5477   cp_token *token;
5478   enum tree_code unary_operator;
5479
5480   /* Peek at the next token.  */
5481   token = cp_lexer_peek_token (parser->lexer);
5482   /* Some keywords give away the kind of expression.  */
5483   if (token->type == CPP_KEYWORD)
5484     {
5485       enum rid keyword = token->keyword;
5486
5487       switch (keyword)
5488         {
5489         case RID_ALIGNOF:
5490         case RID_SIZEOF:
5491           {
5492             tree operand;
5493             enum tree_code op;
5494
5495             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5496             /* Consume the token.  */
5497             cp_lexer_consume_token (parser->lexer);
5498             /* Parse the operand.  */
5499             operand = cp_parser_sizeof_operand (parser, keyword);
5500
5501             if (TYPE_P (operand))
5502               return cxx_sizeof_or_alignof_type (operand, op, true);
5503             else
5504               return cxx_sizeof_or_alignof_expr (operand, op, true);
5505           }
5506
5507         case RID_NEW:
5508           return cp_parser_new_expression (parser);
5509
5510         case RID_DELETE:
5511           return cp_parser_delete_expression (parser);
5512
5513         case RID_EXTENSION:
5514           {
5515             /* The saved value of the PEDANTIC flag.  */
5516             int saved_pedantic;
5517             tree expr;
5518
5519             /* Save away the PEDANTIC flag.  */
5520             cp_parser_extension_opt (parser, &saved_pedantic);
5521             /* Parse the cast-expression.  */
5522             expr = cp_parser_simple_cast_expression (parser);
5523             /* Restore the PEDANTIC flag.  */
5524             pedantic = saved_pedantic;
5525
5526             return expr;
5527           }
5528
5529         case RID_REALPART:
5530         case RID_IMAGPART:
5531           {
5532             tree expression;
5533
5534             /* Consume the `__real__' or `__imag__' token.  */
5535             cp_lexer_consume_token (parser->lexer);
5536             /* Parse the cast-expression.  */
5537             expression = cp_parser_simple_cast_expression (parser);
5538             /* Create the complete representation.  */
5539             return build_x_unary_op ((keyword == RID_REALPART
5540                                       ? REALPART_EXPR : IMAGPART_EXPR),
5541                                      expression,
5542                                      tf_warning_or_error);
5543           }
5544           break;
5545
5546         default:
5547           break;
5548         }
5549     }
5550
5551   /* Look for the `:: new' and `:: delete', which also signal the
5552      beginning of a new-expression, or delete-expression,
5553      respectively.  If the next token is `::', then it might be one of
5554      these.  */
5555   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5556     {
5557       enum rid keyword;
5558
5559       /* See if the token after the `::' is one of the keywords in
5560          which we're interested.  */
5561       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5562       /* If it's `new', we have a new-expression.  */
5563       if (keyword == RID_NEW)
5564         return cp_parser_new_expression (parser);
5565       /* Similarly, for `delete'.  */
5566       else if (keyword == RID_DELETE)
5567         return cp_parser_delete_expression (parser);
5568     }
5569
5570   /* Look for a unary operator.  */
5571   unary_operator = cp_parser_unary_operator (token);
5572   /* The `++' and `--' operators can be handled similarly, even though
5573      they are not technically unary-operators in the grammar.  */
5574   if (unary_operator == ERROR_MARK)
5575     {
5576       if (token->type == CPP_PLUS_PLUS)
5577         unary_operator = PREINCREMENT_EXPR;
5578       else if (token->type == CPP_MINUS_MINUS)
5579         unary_operator = PREDECREMENT_EXPR;
5580       /* Handle the GNU address-of-label extension.  */
5581       else if (cp_parser_allow_gnu_extensions_p (parser)
5582                && token->type == CPP_AND_AND)
5583         {
5584           tree identifier;
5585           tree expression;
5586           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5587
5588           /* Consume the '&&' token.  */
5589           cp_lexer_consume_token (parser->lexer);
5590           /* Look for the identifier.  */
5591           identifier = cp_parser_identifier (parser);
5592           /* Create an expression representing the address.  */
5593           expression = finish_label_address_expr (identifier, loc);
5594           if (cp_parser_non_integral_constant_expression (parser,
5595                                                 "the address of a label"))
5596             expression = error_mark_node;
5597           return expression;
5598         }
5599     }
5600   if (unary_operator != ERROR_MARK)
5601     {
5602       tree cast_expression;
5603       tree expression = error_mark_node;
5604       const char *non_constant_p = NULL;
5605
5606       /* Consume the operator token.  */
5607       token = cp_lexer_consume_token (parser->lexer);
5608       /* Parse the cast-expression.  */
5609       cast_expression
5610         = cp_parser_cast_expression (parser,
5611                                      unary_operator == ADDR_EXPR,
5612                                      /*cast_p=*/false, pidk);
5613       /* Now, build an appropriate representation.  */
5614       switch (unary_operator)
5615         {
5616         case INDIRECT_REF:
5617           non_constant_p = "%<*%>";
5618           expression = build_x_indirect_ref (cast_expression, "unary *",
5619                                              tf_warning_or_error);
5620           break;
5621
5622         case ADDR_EXPR:
5623           non_constant_p = "%<&%>";
5624           /* Fall through.  */
5625         case BIT_NOT_EXPR:
5626           expression = build_x_unary_op (unary_operator, cast_expression,
5627                                          tf_warning_or_error);
5628           break;
5629
5630         case PREINCREMENT_EXPR:
5631         case PREDECREMENT_EXPR:
5632           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5633                             ? "%<++%>" : "%<--%>");
5634           /* Fall through.  */
5635         case UNARY_PLUS_EXPR:
5636         case NEGATE_EXPR:
5637         case TRUTH_NOT_EXPR:
5638           expression = finish_unary_op_expr (unary_operator, cast_expression);
5639           break;
5640
5641         default:
5642           gcc_unreachable ();
5643         }
5644
5645       if (non_constant_p
5646           && cp_parser_non_integral_constant_expression (parser,
5647                                                          non_constant_p))
5648         expression = error_mark_node;
5649
5650       return expression;
5651     }
5652
5653   return cp_parser_postfix_expression (parser, address_p, cast_p,
5654                                        /*member_access_only_p=*/false,
5655                                        pidk);
5656 }
5657
5658 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5659    unary-operator, the corresponding tree code is returned.  */
5660
5661 static enum tree_code
5662 cp_parser_unary_operator (cp_token* token)
5663 {
5664   switch (token->type)
5665     {
5666     case CPP_MULT:
5667       return INDIRECT_REF;
5668
5669     case CPP_AND:
5670       return ADDR_EXPR;
5671
5672     case CPP_PLUS:
5673       return UNARY_PLUS_EXPR;
5674
5675     case CPP_MINUS:
5676       return NEGATE_EXPR;
5677
5678     case CPP_NOT:
5679       return TRUTH_NOT_EXPR;
5680
5681     case CPP_COMPL:
5682       return BIT_NOT_EXPR;
5683
5684     default:
5685       return ERROR_MARK;
5686     }
5687 }
5688
5689 /* Parse a new-expression.
5690
5691    new-expression:
5692      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5693      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5694
5695    Returns a representation of the expression.  */
5696
5697 static tree
5698 cp_parser_new_expression (cp_parser* parser)
5699 {
5700   bool global_scope_p;
5701   VEC(tree,gc) *placement;
5702   tree type;
5703   VEC(tree,gc) *initializer;
5704   tree nelts;
5705   tree ret;
5706
5707   /* Look for the optional `::' operator.  */
5708   global_scope_p
5709     = (cp_parser_global_scope_opt (parser,
5710                                    /*current_scope_valid_p=*/false)
5711        != NULL_TREE);
5712   /* Look for the `new' operator.  */
5713   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5714   /* There's no easy way to tell a new-placement from the
5715      `( type-id )' construct.  */
5716   cp_parser_parse_tentatively (parser);
5717   /* Look for a new-placement.  */
5718   placement = cp_parser_new_placement (parser);
5719   /* If that didn't work out, there's no new-placement.  */
5720   if (!cp_parser_parse_definitely (parser))
5721     {
5722       if (placement != NULL)
5723         release_tree_vector (placement);
5724       placement = NULL;
5725     }
5726
5727   /* If the next token is a `(', then we have a parenthesized
5728      type-id.  */
5729   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5730     {
5731       cp_token *token;
5732       /* Consume the `('.  */
5733       cp_lexer_consume_token (parser->lexer);
5734       /* Parse the type-id.  */
5735       type = cp_parser_type_id (parser);
5736       /* Look for the closing `)'.  */
5737       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5738       token = cp_lexer_peek_token (parser->lexer);
5739       /* There should not be a direct-new-declarator in this production,
5740          but GCC used to allowed this, so we check and emit a sensible error
5741          message for this case.  */
5742       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5743         {
5744           error_at (token->location,
5745                     "array bound forbidden after parenthesized type-id");
5746           inform (token->location, 
5747                   "try removing the parentheses around the type-id");
5748           cp_parser_direct_new_declarator (parser);
5749         }
5750       nelts = NULL_TREE;
5751     }
5752   /* Otherwise, there must be a new-type-id.  */
5753   else
5754     type = cp_parser_new_type_id (parser, &nelts);
5755
5756   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5757   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5758       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5759     initializer = cp_parser_new_initializer (parser);
5760   else
5761     initializer = NULL;
5762
5763   /* A new-expression may not appear in an integral constant
5764      expression.  */
5765   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5766     ret = error_mark_node;
5767   else
5768     {
5769       /* Create a representation of the new-expression.  */
5770       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5771                        tf_warning_or_error);
5772     }
5773
5774   if (placement != NULL)
5775     release_tree_vector (placement);
5776   if (initializer != NULL)
5777     release_tree_vector (initializer);
5778
5779   return ret;
5780 }
5781
5782 /* Parse a new-placement.
5783
5784    new-placement:
5785      ( expression-list )
5786
5787    Returns the same representation as for an expression-list.  */
5788
5789 static VEC(tree,gc) *
5790 cp_parser_new_placement (cp_parser* parser)
5791 {
5792   VEC(tree,gc) *expression_list;
5793
5794   /* Parse the expression-list.  */
5795   expression_list = (cp_parser_parenthesized_expression_list
5796                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5797                       /*non_constant_p=*/NULL));
5798
5799   return expression_list;
5800 }
5801
5802 /* Parse a new-type-id.
5803
5804    new-type-id:
5805      type-specifier-seq new-declarator [opt]
5806
5807    Returns the TYPE allocated.  If the new-type-id indicates an array
5808    type, *NELTS is set to the number of elements in the last array
5809    bound; the TYPE will not include the last array bound.  */
5810
5811 static tree
5812 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5813 {
5814   cp_decl_specifier_seq type_specifier_seq;
5815   cp_declarator *new_declarator;
5816   cp_declarator *declarator;
5817   cp_declarator *outer_declarator;
5818   const char *saved_message;
5819   tree type;
5820
5821   /* The type-specifier sequence must not contain type definitions.
5822      (It cannot contain declarations of new types either, but if they
5823      are not definitions we will catch that because they are not
5824      complete.)  */
5825   saved_message = parser->type_definition_forbidden_message;
5826   parser->type_definition_forbidden_message
5827     = "types may not be defined in a new-type-id";
5828   /* Parse the type-specifier-seq.  */
5829   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5830                                 /*is_trailing_return=*/false,
5831                                 &type_specifier_seq);
5832   /* Restore the old message.  */
5833   parser->type_definition_forbidden_message = saved_message;
5834   /* Parse the new-declarator.  */
5835   new_declarator = cp_parser_new_declarator_opt (parser);
5836
5837   /* Determine the number of elements in the last array dimension, if
5838      any.  */
5839   *nelts = NULL_TREE;
5840   /* Skip down to the last array dimension.  */
5841   declarator = new_declarator;
5842   outer_declarator = NULL;
5843   while (declarator && (declarator->kind == cdk_pointer
5844                         || declarator->kind == cdk_ptrmem))
5845     {
5846       outer_declarator = declarator;
5847       declarator = declarator->declarator;
5848     }
5849   while (declarator
5850          && declarator->kind == cdk_array
5851          && declarator->declarator
5852          && declarator->declarator->kind == cdk_array)
5853     {
5854       outer_declarator = declarator;
5855       declarator = declarator->declarator;
5856     }
5857
5858   if (declarator && declarator->kind == cdk_array)
5859     {
5860       *nelts = declarator->u.array.bounds;
5861       if (*nelts == error_mark_node)
5862         *nelts = integer_one_node;
5863
5864       if (outer_declarator)
5865         outer_declarator->declarator = declarator->declarator;
5866       else
5867         new_declarator = NULL;
5868     }
5869
5870   type = groktypename (&type_specifier_seq, new_declarator, false);
5871   return type;
5872 }
5873
5874 /* Parse an (optional) new-declarator.
5875
5876    new-declarator:
5877      ptr-operator new-declarator [opt]
5878      direct-new-declarator
5879
5880    Returns the declarator.  */
5881
5882 static cp_declarator *
5883 cp_parser_new_declarator_opt (cp_parser* parser)
5884 {
5885   enum tree_code code;
5886   tree type;
5887   cp_cv_quals cv_quals;
5888
5889   /* We don't know if there's a ptr-operator next, or not.  */
5890   cp_parser_parse_tentatively (parser);
5891   /* Look for a ptr-operator.  */
5892   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5893   /* If that worked, look for more new-declarators.  */
5894   if (cp_parser_parse_definitely (parser))
5895     {
5896       cp_declarator *declarator;
5897
5898       /* Parse another optional declarator.  */
5899       declarator = cp_parser_new_declarator_opt (parser);
5900
5901       return cp_parser_make_indirect_declarator
5902         (code, type, cv_quals, declarator);
5903     }
5904
5905   /* If the next token is a `[', there is a direct-new-declarator.  */
5906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5907     return cp_parser_direct_new_declarator (parser);
5908
5909   return NULL;
5910 }
5911
5912 /* Parse a direct-new-declarator.
5913
5914    direct-new-declarator:
5915      [ expression ]
5916      direct-new-declarator [constant-expression]
5917
5918    */
5919
5920 static cp_declarator *
5921 cp_parser_direct_new_declarator (cp_parser* parser)
5922 {
5923   cp_declarator *declarator = NULL;
5924
5925   while (true)
5926     {
5927       tree expression;
5928
5929       /* Look for the opening `['.  */
5930       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5931       /* The first expression is not required to be constant.  */
5932       if (!declarator)
5933         {
5934           cp_token *token = cp_lexer_peek_token (parser->lexer);
5935           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5936           /* The standard requires that the expression have integral
5937              type.  DR 74 adds enumeration types.  We believe that the
5938              real intent is that these expressions be handled like the
5939              expression in a `switch' condition, which also allows
5940              classes with a single conversion to integral or
5941              enumeration type.  */
5942           if (!processing_template_decl)
5943             {
5944               expression
5945                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5946                                               expression,
5947                                               /*complain=*/true);
5948               if (!expression)
5949                 {
5950                   error_at (token->location,
5951                             "expression in new-declarator must have integral "
5952                             "or enumeration type");
5953                   expression = error_mark_node;
5954                 }
5955             }
5956         }
5957       /* But all the other expressions must be.  */
5958       else
5959         expression
5960           = cp_parser_constant_expression (parser,
5961                                            /*allow_non_constant=*/false,
5962                                            NULL);
5963       /* Look for the closing `]'.  */
5964       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5965
5966       /* Add this bound to the declarator.  */
5967       declarator = make_array_declarator (declarator, expression);
5968
5969       /* If the next token is not a `[', then there are no more
5970          bounds.  */
5971       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5972         break;
5973     }
5974
5975   return declarator;
5976 }
5977
5978 /* Parse a new-initializer.
5979
5980    new-initializer:
5981      ( expression-list [opt] )
5982      braced-init-list
5983
5984    Returns a representation of the expression-list.  */
5985
5986 static VEC(tree,gc) *
5987 cp_parser_new_initializer (cp_parser* parser)
5988 {
5989   VEC(tree,gc) *expression_list;
5990
5991   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5992     {
5993       tree t;
5994       bool expr_non_constant_p;
5995       maybe_warn_cpp0x ("extended initializer lists");
5996       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5997       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5998       expression_list = make_tree_vector_single (t);
5999     }
6000   else
6001     expression_list = (cp_parser_parenthesized_expression_list
6002                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6003                         /*non_constant_p=*/NULL));
6004
6005   return expression_list;
6006 }
6007
6008 /* Parse a delete-expression.
6009
6010    delete-expression:
6011      :: [opt] delete cast-expression
6012      :: [opt] delete [ ] cast-expression
6013
6014    Returns a representation of the expression.  */
6015
6016 static tree
6017 cp_parser_delete_expression (cp_parser* parser)
6018 {
6019   bool global_scope_p;
6020   bool array_p;
6021   tree expression;
6022
6023   /* Look for the optional `::' operator.  */
6024   global_scope_p
6025     = (cp_parser_global_scope_opt (parser,
6026                                    /*current_scope_valid_p=*/false)
6027        != NULL_TREE);
6028   /* Look for the `delete' keyword.  */
6029   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6030   /* See if the array syntax is in use.  */
6031   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6032     {
6033       /* Consume the `[' token.  */
6034       cp_lexer_consume_token (parser->lexer);
6035       /* Look for the `]' token.  */
6036       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6037       /* Remember that this is the `[]' construct.  */
6038       array_p = true;
6039     }
6040   else
6041     array_p = false;
6042
6043   /* Parse the cast-expression.  */
6044   expression = cp_parser_simple_cast_expression (parser);
6045
6046   /* A delete-expression may not appear in an integral constant
6047      expression.  */
6048   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6049     return error_mark_node;
6050
6051   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6052 }
6053
6054 /* Returns true if TOKEN may start a cast-expression and false
6055    otherwise.  */
6056
6057 static bool
6058 cp_parser_token_starts_cast_expression (cp_token *token)
6059 {
6060   switch (token->type)
6061     {
6062     case CPP_COMMA:
6063     case CPP_SEMICOLON:
6064     case CPP_QUERY:
6065     case CPP_COLON:
6066     case CPP_CLOSE_SQUARE:
6067     case CPP_CLOSE_PAREN:
6068     case CPP_CLOSE_BRACE:
6069     case CPP_DOT:
6070     case CPP_DOT_STAR:
6071     case CPP_DEREF:
6072     case CPP_DEREF_STAR:
6073     case CPP_DIV:
6074     case CPP_MOD:
6075     case CPP_LSHIFT:
6076     case CPP_RSHIFT:
6077     case CPP_LESS:
6078     case CPP_GREATER:
6079     case CPP_LESS_EQ:
6080     case CPP_GREATER_EQ:
6081     case CPP_EQ_EQ:
6082     case CPP_NOT_EQ:
6083     case CPP_EQ:
6084     case CPP_MULT_EQ:
6085     case CPP_DIV_EQ:
6086     case CPP_MOD_EQ:
6087     case CPP_PLUS_EQ:
6088     case CPP_MINUS_EQ:
6089     case CPP_RSHIFT_EQ:
6090     case CPP_LSHIFT_EQ:
6091     case CPP_AND_EQ:
6092     case CPP_XOR_EQ:
6093     case CPP_OR_EQ:
6094     case CPP_XOR:
6095     case CPP_OR:
6096     case CPP_OR_OR:
6097     case CPP_EOF:
6098       return false;
6099
6100       /* '[' may start a primary-expression in obj-c++.  */
6101     case CPP_OPEN_SQUARE:
6102       return c_dialect_objc ();
6103
6104     default:
6105       return true;
6106     }
6107 }
6108
6109 /* Parse a cast-expression.
6110
6111    cast-expression:
6112      unary-expression
6113      ( type-id ) cast-expression
6114
6115    ADDRESS_P is true iff the unary-expression is appearing as the
6116    operand of the `&' operator.   CAST_P is true if this expression is
6117    the target of a cast.
6118
6119    Returns a representation of the expression.  */
6120
6121 static tree
6122 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6123                            cp_id_kind * pidk)
6124 {
6125   /* If it's a `(', then we might be looking at a cast.  */
6126   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6127     {
6128       tree type = NULL_TREE;
6129       tree expr = NULL_TREE;
6130       bool compound_literal_p;
6131       const char *saved_message;
6132
6133       /* There's no way to know yet whether or not this is a cast.
6134          For example, `(int (3))' is a unary-expression, while `(int)
6135          3' is a cast.  So, we resort to parsing tentatively.  */
6136       cp_parser_parse_tentatively (parser);
6137       /* Types may not be defined in a cast.  */
6138       saved_message = parser->type_definition_forbidden_message;
6139       parser->type_definition_forbidden_message
6140         = "types may not be defined in casts";
6141       /* Consume the `('.  */
6142       cp_lexer_consume_token (parser->lexer);
6143       /* A very tricky bit is that `(struct S) { 3 }' is a
6144          compound-literal (which we permit in C++ as an extension).
6145          But, that construct is not a cast-expression -- it is a
6146          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6147          is legal; if the compound-literal were a cast-expression,
6148          you'd need an extra set of parentheses.)  But, if we parse
6149          the type-id, and it happens to be a class-specifier, then we
6150          will commit to the parse at that point, because we cannot
6151          undo the action that is done when creating a new class.  So,
6152          then we cannot back up and do a postfix-expression.
6153
6154          Therefore, we scan ahead to the closing `)', and check to see
6155          if the token after the `)' is a `{'.  If so, we are not
6156          looking at a cast-expression.
6157
6158          Save tokens so that we can put them back.  */
6159       cp_lexer_save_tokens (parser->lexer);
6160       /* Skip tokens until the next token is a closing parenthesis.
6161          If we find the closing `)', and the next token is a `{', then
6162          we are looking at a compound-literal.  */
6163       compound_literal_p
6164         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6165                                                   /*consume_paren=*/true)
6166            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6167       /* Roll back the tokens we skipped.  */
6168       cp_lexer_rollback_tokens (parser->lexer);
6169       /* If we were looking at a compound-literal, simulate an error
6170          so that the call to cp_parser_parse_definitely below will
6171          fail.  */
6172       if (compound_literal_p)
6173         cp_parser_simulate_error (parser);
6174       else
6175         {
6176           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6177           parser->in_type_id_in_expr_p = true;
6178           /* Look for the type-id.  */
6179           type = cp_parser_type_id (parser);
6180           /* Look for the closing `)'.  */
6181           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6182           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6183         }
6184
6185       /* Restore the saved message.  */
6186       parser->type_definition_forbidden_message = saved_message;
6187
6188       /* At this point this can only be either a cast or a
6189          parenthesized ctor such as `(T ())' that looks like a cast to
6190          function returning T.  */
6191       if (!cp_parser_error_occurred (parser)
6192           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6193                                                      (parser->lexer)))
6194         {
6195           cp_parser_parse_definitely (parser);
6196           expr = cp_parser_cast_expression (parser,
6197                                             /*address_p=*/false,
6198                                             /*cast_p=*/true, pidk);
6199
6200           /* Warn about old-style casts, if so requested.  */
6201           if (warn_old_style_cast
6202               && !in_system_header
6203               && !VOID_TYPE_P (type)
6204               && current_lang_name != lang_name_c)
6205             warning (OPT_Wold_style_cast, "use of old-style cast");
6206
6207           /* Only type conversions to integral or enumeration types
6208              can be used in constant-expressions.  */
6209           if (!cast_valid_in_integral_constant_expression_p (type)
6210               && (cp_parser_non_integral_constant_expression
6211                   (parser,
6212                    "a cast to a type other than an integral or "
6213                    "enumeration type")))
6214             return error_mark_node;
6215
6216           /* Perform the cast.  */
6217           expr = build_c_cast (input_location, type, expr);
6218           return expr;
6219         }
6220       else 
6221         cp_parser_abort_tentative_parse (parser);
6222     }
6223
6224   /* If we get here, then it's not a cast, so it must be a
6225      unary-expression.  */
6226   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6227 }
6228
6229 /* Parse a binary expression of the general form:
6230
6231    pm-expression:
6232      cast-expression
6233      pm-expression .* cast-expression
6234      pm-expression ->* cast-expression
6235
6236    multiplicative-expression:
6237      pm-expression
6238      multiplicative-expression * pm-expression
6239      multiplicative-expression / pm-expression
6240      multiplicative-expression % pm-expression
6241
6242    additive-expression:
6243      multiplicative-expression
6244      additive-expression + multiplicative-expression
6245      additive-expression - multiplicative-expression
6246
6247    shift-expression:
6248      additive-expression
6249      shift-expression << additive-expression
6250      shift-expression >> additive-expression
6251
6252    relational-expression:
6253      shift-expression
6254      relational-expression < shift-expression
6255      relational-expression > shift-expression
6256      relational-expression <= shift-expression
6257      relational-expression >= shift-expression
6258
6259   GNU Extension:
6260
6261    relational-expression:
6262      relational-expression <? shift-expression
6263      relational-expression >? shift-expression
6264
6265    equality-expression:
6266      relational-expression
6267      equality-expression == relational-expression
6268      equality-expression != relational-expression
6269
6270    and-expression:
6271      equality-expression
6272      and-expression & equality-expression
6273
6274    exclusive-or-expression:
6275      and-expression
6276      exclusive-or-expression ^ and-expression
6277
6278    inclusive-or-expression:
6279      exclusive-or-expression
6280      inclusive-or-expression | exclusive-or-expression
6281
6282    logical-and-expression:
6283      inclusive-or-expression
6284      logical-and-expression && inclusive-or-expression
6285
6286    logical-or-expression:
6287      logical-and-expression
6288      logical-or-expression || logical-and-expression
6289
6290    All these are implemented with a single function like:
6291
6292    binary-expression:
6293      simple-cast-expression
6294      binary-expression <token> binary-expression
6295
6296    CAST_P is true if this expression is the target of a cast.
6297
6298    The binops_by_token map is used to get the tree codes for each <token> type.
6299    binary-expressions are associated according to a precedence table.  */
6300
6301 #define TOKEN_PRECEDENCE(token)                              \
6302 (((token->type == CPP_GREATER                                \
6303    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6304   && !parser->greater_than_is_operator_p)                    \
6305  ? PREC_NOT_OPERATOR                                         \
6306  : binops_by_token[token->type].prec)
6307
6308 static tree
6309 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6310                              bool no_toplevel_fold_p,
6311                              enum cp_parser_prec prec,
6312                              cp_id_kind * pidk)
6313 {
6314   cp_parser_expression_stack stack;
6315   cp_parser_expression_stack_entry *sp = &stack[0];
6316   tree lhs, rhs;
6317   cp_token *token;
6318   enum tree_code tree_type, lhs_type, rhs_type;
6319   enum cp_parser_prec new_prec, lookahead_prec;
6320   bool overloaded_p;
6321
6322   /* Parse the first expression.  */
6323   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6324   lhs_type = ERROR_MARK;
6325
6326   for (;;)
6327     {
6328       /* Get an operator token.  */
6329       token = cp_lexer_peek_token (parser->lexer);
6330
6331       if (warn_cxx0x_compat
6332           && token->type == CPP_RSHIFT
6333           && !parser->greater_than_is_operator_p)
6334         {
6335           if (warning_at (token->location, OPT_Wc__0x_compat, 
6336                           "%<>>%> operator will be treated as"
6337                           " two right angle brackets in C++0x"))
6338             inform (token->location,
6339                     "suggest parentheses around %<>>%> expression");
6340         }
6341
6342       new_prec = TOKEN_PRECEDENCE (token);
6343
6344       /* Popping an entry off the stack means we completed a subexpression:
6345          - either we found a token which is not an operator (`>' where it is not
6346            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6347            will happen repeatedly;
6348          - or, we found an operator which has lower priority.  This is the case
6349            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6350            parsing `3 * 4'.  */
6351       if (new_prec <= prec)
6352         {
6353           if (sp == stack)
6354             break;
6355           else
6356             goto pop;
6357         }
6358
6359      get_rhs:
6360       tree_type = binops_by_token[token->type].tree_type;
6361
6362       /* We used the operator token.  */
6363       cp_lexer_consume_token (parser->lexer);
6364
6365       /* For "false && x" or "true || x", x will never be executed;
6366          disable warnings while evaluating it.  */
6367       if (tree_type == TRUTH_ANDIF_EXPR)
6368         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6369       else if (tree_type == TRUTH_ORIF_EXPR)
6370         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6371
6372       /* Extract another operand.  It may be the RHS of this expression
6373          or the LHS of a new, higher priority expression.  */
6374       rhs = cp_parser_simple_cast_expression (parser);
6375       rhs_type = ERROR_MARK;
6376
6377       /* Get another operator token.  Look up its precedence to avoid
6378          building a useless (immediately popped) stack entry for common
6379          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6380       token = cp_lexer_peek_token (parser->lexer);
6381       lookahead_prec = TOKEN_PRECEDENCE (token);
6382       if (lookahead_prec > new_prec)
6383         {
6384           /* ... and prepare to parse the RHS of the new, higher priority
6385              expression.  Since precedence levels on the stack are
6386              monotonically increasing, we do not have to care about
6387              stack overflows.  */
6388           sp->prec = prec;
6389           sp->tree_type = tree_type;
6390           sp->lhs = lhs;
6391           sp->lhs_type = lhs_type;
6392           sp++;
6393           lhs = rhs;
6394           lhs_type = rhs_type;
6395           prec = new_prec;
6396           new_prec = lookahead_prec;
6397           goto get_rhs;
6398
6399          pop:
6400           lookahead_prec = new_prec;
6401           /* If the stack is not empty, we have parsed into LHS the right side
6402              (`4' in the example above) of an expression we had suspended.
6403              We can use the information on the stack to recover the LHS (`3')
6404              from the stack together with the tree code (`MULT_EXPR'), and
6405              the precedence of the higher level subexpression
6406              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6407              which will be used to actually build the additive expression.  */
6408           --sp;
6409           prec = sp->prec;
6410           tree_type = sp->tree_type;
6411           rhs = lhs;
6412           rhs_type = lhs_type;
6413           lhs = sp->lhs;
6414           lhs_type = sp->lhs_type;
6415         }
6416
6417       /* Undo the disabling of warnings done above.  */
6418       if (tree_type == TRUTH_ANDIF_EXPR)
6419         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6420       else if (tree_type == TRUTH_ORIF_EXPR)
6421         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6422
6423       overloaded_p = false;
6424       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6425          ERROR_MARK for everything that is not a binary expression.
6426          This makes warn_about_parentheses miss some warnings that
6427          involve unary operators.  For unary expressions we should
6428          pass the correct tree_code unless the unary expression was
6429          surrounded by parentheses.
6430       */
6431       if (no_toplevel_fold_p
6432           && lookahead_prec <= prec
6433           && sp == stack
6434           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6435         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6436       else
6437         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6438                                  &overloaded_p, tf_warning_or_error);
6439       lhs_type = tree_type;
6440
6441       /* If the binary operator required the use of an overloaded operator,
6442          then this expression cannot be an integral constant-expression.
6443          An overloaded operator can be used even if both operands are
6444          otherwise permissible in an integral constant-expression if at
6445          least one of the operands is of enumeration type.  */
6446
6447       if (overloaded_p
6448           && (cp_parser_non_integral_constant_expression
6449               (parser, "calls to overloaded operators")))
6450         return error_mark_node;
6451     }
6452
6453   return lhs;
6454 }
6455
6456
6457 /* Parse the `? expression : assignment-expression' part of a
6458    conditional-expression.  The LOGICAL_OR_EXPR is the
6459    logical-or-expression that started the conditional-expression.
6460    Returns a representation of the entire conditional-expression.
6461
6462    This routine is used by cp_parser_assignment_expression.
6463
6464      ? expression : assignment-expression
6465
6466    GNU Extensions:
6467
6468      ? : assignment-expression */
6469
6470 static tree
6471 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6472 {
6473   tree expr;
6474   tree assignment_expr;
6475
6476   /* Consume the `?' token.  */
6477   cp_lexer_consume_token (parser->lexer);
6478   if (cp_parser_allow_gnu_extensions_p (parser)
6479       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6480     {
6481       /* Implicit true clause.  */
6482       expr = NULL_TREE;
6483       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6484     }
6485   else
6486     {
6487       /* Parse the expression.  */
6488       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6489       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6490       c_inhibit_evaluation_warnings +=
6491         ((logical_or_expr == truthvalue_true_node)
6492          - (logical_or_expr == truthvalue_false_node));
6493     }
6494
6495   /* The next token should be a `:'.  */
6496   cp_parser_require (parser, CPP_COLON, "%<:%>");
6497   /* Parse the assignment-expression.  */
6498   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6499   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6500
6501   /* Build the conditional-expression.  */
6502   return build_x_conditional_expr (logical_or_expr,
6503                                    expr,
6504                                    assignment_expr,
6505                                    tf_warning_or_error);
6506 }
6507
6508 /* Parse an assignment-expression.
6509
6510    assignment-expression:
6511      conditional-expression
6512      logical-or-expression assignment-operator assignment_expression
6513      throw-expression
6514
6515    CAST_P is true if this expression is the target of a cast.
6516
6517    Returns a representation for the expression.  */
6518
6519 static tree
6520 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6521                                  cp_id_kind * pidk)
6522 {
6523   tree expr;
6524
6525   /* If the next token is the `throw' keyword, then we're looking at
6526      a throw-expression.  */
6527   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6528     expr = cp_parser_throw_expression (parser);
6529   /* Otherwise, it must be that we are looking at a
6530      logical-or-expression.  */
6531   else
6532     {
6533       /* Parse the binary expressions (logical-or-expression).  */
6534       expr = cp_parser_binary_expression (parser, cast_p, false,
6535                                           PREC_NOT_OPERATOR, pidk);
6536       /* If the next token is a `?' then we're actually looking at a
6537          conditional-expression.  */
6538       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6539         return cp_parser_question_colon_clause (parser, expr);
6540       else
6541         {
6542           enum tree_code assignment_operator;
6543
6544           /* If it's an assignment-operator, we're using the second
6545              production.  */
6546           assignment_operator
6547             = cp_parser_assignment_operator_opt (parser);
6548           if (assignment_operator != ERROR_MARK)
6549             {
6550               bool non_constant_p;
6551
6552               /* Parse the right-hand side of the assignment.  */
6553               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6554
6555               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6556                 maybe_warn_cpp0x ("extended initializer lists");
6557
6558               /* An assignment may not appear in a
6559                  constant-expression.  */
6560               if (cp_parser_non_integral_constant_expression (parser,
6561                                                               "an assignment"))
6562                 return error_mark_node;
6563               /* Build the assignment expression.  */
6564               expr = build_x_modify_expr (expr,
6565                                           assignment_operator,
6566                                           rhs,
6567                                           tf_warning_or_error);
6568             }
6569         }
6570     }
6571
6572   return expr;
6573 }
6574
6575 /* Parse an (optional) assignment-operator.
6576
6577    assignment-operator: one of
6578      = *= /= %= += -= >>= <<= &= ^= |=
6579
6580    GNU Extension:
6581
6582    assignment-operator: one of
6583      <?= >?=
6584
6585    If the next token is an assignment operator, the corresponding tree
6586    code is returned, and the token is consumed.  For example, for
6587    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6588    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6589    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6590    operator, ERROR_MARK is returned.  */
6591
6592 static enum tree_code
6593 cp_parser_assignment_operator_opt (cp_parser* parser)
6594 {
6595   enum tree_code op;
6596   cp_token *token;
6597
6598   /* Peek at the next token.  */
6599   token = cp_lexer_peek_token (parser->lexer);
6600
6601   switch (token->type)
6602     {
6603     case CPP_EQ:
6604       op = NOP_EXPR;
6605       break;
6606
6607     case CPP_MULT_EQ:
6608       op = MULT_EXPR;
6609       break;
6610
6611     case CPP_DIV_EQ:
6612       op = TRUNC_DIV_EXPR;
6613       break;
6614
6615     case CPP_MOD_EQ:
6616       op = TRUNC_MOD_EXPR;
6617       break;
6618
6619     case CPP_PLUS_EQ:
6620       op = PLUS_EXPR;
6621       break;
6622
6623     case CPP_MINUS_EQ:
6624       op = MINUS_EXPR;
6625       break;
6626
6627     case CPP_RSHIFT_EQ:
6628       op = RSHIFT_EXPR;
6629       break;
6630
6631     case CPP_LSHIFT_EQ:
6632       op = LSHIFT_EXPR;
6633       break;
6634
6635     case CPP_AND_EQ:
6636       op = BIT_AND_EXPR;
6637       break;
6638
6639     case CPP_XOR_EQ:
6640       op = BIT_XOR_EXPR;
6641       break;
6642
6643     case CPP_OR_EQ:
6644       op = BIT_IOR_EXPR;
6645       break;
6646
6647     default:
6648       /* Nothing else is an assignment operator.  */
6649       op = ERROR_MARK;
6650     }
6651
6652   /* If it was an assignment operator, consume it.  */
6653   if (op != ERROR_MARK)
6654     cp_lexer_consume_token (parser->lexer);
6655
6656   return op;
6657 }
6658
6659 /* Parse an expression.
6660
6661    expression:
6662      assignment-expression
6663      expression , assignment-expression
6664
6665    CAST_P is true if this expression is the target of a cast.
6666
6667    Returns a representation of the expression.  */
6668
6669 static tree
6670 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6671 {
6672   tree expression = NULL_TREE;
6673
6674   while (true)
6675     {
6676       tree assignment_expression;
6677
6678       /* Parse the next assignment-expression.  */
6679       assignment_expression
6680         = cp_parser_assignment_expression (parser, cast_p, pidk);
6681       /* If this is the first assignment-expression, we can just
6682          save it away.  */
6683       if (!expression)
6684         expression = assignment_expression;
6685       else
6686         expression = build_x_compound_expr (expression,
6687                                             assignment_expression,
6688                                             tf_warning_or_error);
6689       /* If the next token is not a comma, then we are done with the
6690          expression.  */
6691       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6692         break;
6693       /* Consume the `,'.  */
6694       cp_lexer_consume_token (parser->lexer);
6695       /* A comma operator cannot appear in a constant-expression.  */
6696       if (cp_parser_non_integral_constant_expression (parser,
6697                                                       "a comma operator"))
6698         expression = error_mark_node;
6699     }
6700
6701   return expression;
6702 }
6703
6704 /* Parse a constant-expression.
6705
6706    constant-expression:
6707      conditional-expression
6708
6709   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6710   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6711   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6712   is false, NON_CONSTANT_P should be NULL.  */
6713
6714 static tree
6715 cp_parser_constant_expression (cp_parser* parser,
6716                                bool allow_non_constant_p,
6717                                bool *non_constant_p)
6718 {
6719   bool saved_integral_constant_expression_p;
6720   bool saved_allow_non_integral_constant_expression_p;
6721   bool saved_non_integral_constant_expression_p;
6722   tree expression;
6723
6724   /* It might seem that we could simply parse the
6725      conditional-expression, and then check to see if it were
6726      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6727      one that the compiler can figure out is constant, possibly after
6728      doing some simplifications or optimizations.  The standard has a
6729      precise definition of constant-expression, and we must honor
6730      that, even though it is somewhat more restrictive.
6731
6732      For example:
6733
6734        int i[(2, 3)];
6735
6736      is not a legal declaration, because `(2, 3)' is not a
6737      constant-expression.  The `,' operator is forbidden in a
6738      constant-expression.  However, GCC's constant-folding machinery
6739      will fold this operation to an INTEGER_CST for `3'.  */
6740
6741   /* Save the old settings.  */
6742   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6743   saved_allow_non_integral_constant_expression_p
6744     = parser->allow_non_integral_constant_expression_p;
6745   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6746   /* We are now parsing a constant-expression.  */
6747   parser->integral_constant_expression_p = true;
6748   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6749   parser->non_integral_constant_expression_p = false;
6750   /* Although the grammar says "conditional-expression", we parse an
6751      "assignment-expression", which also permits "throw-expression"
6752      and the use of assignment operators.  In the case that
6753      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6754      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6755      actually essential that we look for an assignment-expression.
6756      For example, cp_parser_initializer_clauses uses this function to
6757      determine whether a particular assignment-expression is in fact
6758      constant.  */
6759   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6760   /* Restore the old settings.  */
6761   parser->integral_constant_expression_p
6762     = saved_integral_constant_expression_p;
6763   parser->allow_non_integral_constant_expression_p
6764     = saved_allow_non_integral_constant_expression_p;
6765   if (allow_non_constant_p)
6766     *non_constant_p = parser->non_integral_constant_expression_p;
6767   else if (parser->non_integral_constant_expression_p)
6768     expression = error_mark_node;
6769   parser->non_integral_constant_expression_p
6770     = saved_non_integral_constant_expression_p;
6771
6772   return expression;
6773 }
6774
6775 /* Parse __builtin_offsetof.
6776
6777    offsetof-expression:
6778      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6779
6780    offsetof-member-designator:
6781      id-expression
6782      | offsetof-member-designator "." id-expression
6783      | offsetof-member-designator "[" expression "]"
6784      | offsetof-member-designator "->" id-expression  */
6785
6786 static tree
6787 cp_parser_builtin_offsetof (cp_parser *parser)
6788 {
6789   int save_ice_p, save_non_ice_p;
6790   tree type, expr;
6791   cp_id_kind dummy;
6792   cp_token *token;
6793
6794   /* We're about to accept non-integral-constant things, but will
6795      definitely yield an integral constant expression.  Save and
6796      restore these values around our local parsing.  */
6797   save_ice_p = parser->integral_constant_expression_p;
6798   save_non_ice_p = parser->non_integral_constant_expression_p;
6799
6800   /* Consume the "__builtin_offsetof" token.  */
6801   cp_lexer_consume_token (parser->lexer);
6802   /* Consume the opening `('.  */
6803   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6804   /* Parse the type-id.  */
6805   type = cp_parser_type_id (parser);
6806   /* Look for the `,'.  */
6807   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6808   token = cp_lexer_peek_token (parser->lexer);
6809
6810   /* Build the (type *)null that begins the traditional offsetof macro.  */
6811   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6812                             tf_warning_or_error);
6813
6814   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6815   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6816                                                  true, &dummy, token->location);
6817   while (true)
6818     {
6819       token = cp_lexer_peek_token (parser->lexer);
6820       switch (token->type)
6821         {
6822         case CPP_OPEN_SQUARE:
6823           /* offsetof-member-designator "[" expression "]" */
6824           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6825           break;
6826
6827         case CPP_DEREF:
6828           /* offsetof-member-designator "->" identifier */
6829           expr = grok_array_decl (expr, integer_zero_node);
6830           /* FALLTHRU */
6831
6832         case CPP_DOT:
6833           /* offsetof-member-designator "." identifier */
6834           cp_lexer_consume_token (parser->lexer);
6835           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6836                                                          expr, true, &dummy,
6837                                                          token->location);
6838           break;
6839
6840         case CPP_CLOSE_PAREN:
6841           /* Consume the ")" token.  */
6842           cp_lexer_consume_token (parser->lexer);
6843           goto success;
6844
6845         default:
6846           /* Error.  We know the following require will fail, but
6847              that gives the proper error message.  */
6848           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6849           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6850           expr = error_mark_node;
6851           goto failure;
6852         }
6853     }
6854
6855  success:
6856   /* If we're processing a template, we can't finish the semantics yet.
6857      Otherwise we can fold the entire expression now.  */
6858   if (processing_template_decl)
6859     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6860   else
6861     expr = finish_offsetof (expr);
6862
6863  failure:
6864   parser->integral_constant_expression_p = save_ice_p;
6865   parser->non_integral_constant_expression_p = save_non_ice_p;
6866
6867   return expr;
6868 }
6869
6870 /* Parse a trait expression.  */
6871
6872 static tree
6873 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6874 {
6875   cp_trait_kind kind;
6876   tree type1, type2 = NULL_TREE;
6877   bool binary = false;
6878   cp_decl_specifier_seq decl_specs;
6879
6880   switch (keyword)
6881     {
6882     case RID_HAS_NOTHROW_ASSIGN:
6883       kind = CPTK_HAS_NOTHROW_ASSIGN;
6884       break;
6885     case RID_HAS_NOTHROW_CONSTRUCTOR:
6886       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6887       break;
6888     case RID_HAS_NOTHROW_COPY:
6889       kind = CPTK_HAS_NOTHROW_COPY;
6890       break;
6891     case RID_HAS_TRIVIAL_ASSIGN:
6892       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6893       break;
6894     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6895       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6896       break;
6897     case RID_HAS_TRIVIAL_COPY:
6898       kind = CPTK_HAS_TRIVIAL_COPY;
6899       break;
6900     case RID_HAS_TRIVIAL_DESTRUCTOR:
6901       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6902       break;
6903     case RID_HAS_VIRTUAL_DESTRUCTOR:
6904       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6905       break;
6906     case RID_IS_ABSTRACT:
6907       kind = CPTK_IS_ABSTRACT;
6908       break;
6909     case RID_IS_BASE_OF:
6910       kind = CPTK_IS_BASE_OF;
6911       binary = true;
6912       break;
6913     case RID_IS_CLASS:
6914       kind = CPTK_IS_CLASS;
6915       break;
6916     case RID_IS_CONVERTIBLE_TO:
6917       kind = CPTK_IS_CONVERTIBLE_TO;
6918       binary = true;
6919       break;
6920     case RID_IS_EMPTY:
6921       kind = CPTK_IS_EMPTY;
6922       break;
6923     case RID_IS_ENUM:
6924       kind = CPTK_IS_ENUM;
6925       break;
6926     case RID_IS_POD:
6927       kind = CPTK_IS_POD;
6928       break;
6929     case RID_IS_POLYMORPHIC:
6930       kind = CPTK_IS_POLYMORPHIC;
6931       break;
6932     case RID_IS_STD_LAYOUT:
6933       kind = CPTK_IS_STD_LAYOUT;
6934       break;
6935     case RID_IS_TRIVIAL:
6936       kind = CPTK_IS_TRIVIAL;
6937       break;
6938     case RID_IS_UNION:
6939       kind = CPTK_IS_UNION;
6940       break;
6941     default:
6942       gcc_unreachable ();
6943     }
6944
6945   /* Consume the token.  */
6946   cp_lexer_consume_token (parser->lexer);
6947
6948   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6949
6950   type1 = cp_parser_type_id (parser);
6951
6952   if (type1 == error_mark_node)
6953     return error_mark_node;
6954
6955   /* Build a trivial decl-specifier-seq.  */
6956   clear_decl_specs (&decl_specs);
6957   decl_specs.type = type1;
6958
6959   /* Call grokdeclarator to figure out what type this is.  */
6960   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6961                           /*initialized=*/0, /*attrlist=*/NULL);
6962
6963   if (binary)
6964     {
6965       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6966  
6967       type2 = cp_parser_type_id (parser);
6968
6969       if (type2 == error_mark_node)
6970         return error_mark_node;
6971
6972       /* Build a trivial decl-specifier-seq.  */
6973       clear_decl_specs (&decl_specs);
6974       decl_specs.type = type2;
6975
6976       /* Call grokdeclarator to figure out what type this is.  */
6977       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6978                               /*initialized=*/0, /*attrlist=*/NULL);
6979     }
6980
6981   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6982
6983   /* Complete the trait expression, which may mean either processing
6984      the trait expr now or saving it for template instantiation.  */
6985   return finish_trait_expr (kind, type1, type2);
6986 }
6987
6988 /* Lambdas that appear in variable initializer or default argument scope
6989    get that in their mangling, so we need to record it.  We might as well
6990    use the count for function and namespace scopes as well.  */
6991 static GTY(()) tree lambda_scope;
6992 static GTY(()) int lambda_count;
6993 typedef struct GTY(()) tree_int
6994 {
6995   tree t;
6996   int i;
6997 } tree_int;
6998 DEF_VEC_O(tree_int);
6999 DEF_VEC_ALLOC_O(tree_int,gc);
7000 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7001
7002 static void
7003 start_lambda_scope (tree decl)
7004 {
7005   tree_int ti;
7006   gcc_assert (decl);
7007   /* Once we're inside a function, we ignore other scopes and just push
7008      the function again so that popping works properly.  */
7009   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7010     decl = current_function_decl;
7011   ti.t = lambda_scope;
7012   ti.i = lambda_count;
7013   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7014   if (lambda_scope != decl)
7015     {
7016       /* Don't reset the count if we're still in the same function.  */
7017       lambda_scope = decl;
7018       lambda_count = 0;
7019     }
7020 }
7021
7022 static void
7023 record_lambda_scope (tree lambda)
7024 {
7025   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7026   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7027 }
7028
7029 static void
7030 finish_lambda_scope (void)
7031 {
7032   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7033   if (lambda_scope != p->t)
7034     {
7035       lambda_scope = p->t;
7036       lambda_count = p->i;
7037     }
7038   VEC_pop (tree_int, lambda_scope_stack);
7039 }
7040
7041 /* Parse a lambda expression.
7042
7043    lambda-expression:
7044      lambda-introducer lambda-declarator [opt] compound-statement
7045
7046    Returns a representation of the expression.  */
7047
7048 static tree
7049 cp_parser_lambda_expression (cp_parser* parser)
7050 {
7051   tree lambda_expr = build_lambda_expr ();
7052   tree type;
7053
7054   LAMBDA_EXPR_LOCATION (lambda_expr)
7055     = cp_lexer_peek_token (parser->lexer)->location;
7056
7057   /* We may be in the middle of deferred access check.  Disable
7058      it now.  */
7059   push_deferring_access_checks (dk_no_deferred);
7060
7061   type = begin_lambda_type (lambda_expr);
7062
7063   record_lambda_scope (lambda_expr);
7064
7065   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7066   determine_visibility (TYPE_NAME (type));
7067
7068   {
7069     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7070     unsigned int saved_num_template_parameter_lists
7071         = parser->num_template_parameter_lists;
7072
7073     parser->num_template_parameter_lists = 0;
7074
7075     cp_parser_lambda_introducer (parser, lambda_expr);
7076
7077     /* By virtue of defining a local class, a lambda expression has access to
7078        the private variables of enclosing classes.  */
7079
7080     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7081
7082     cp_parser_lambda_body (parser, lambda_expr);
7083
7084     /* The capture list was built up in reverse order; fix that now.  */
7085     {
7086       tree newlist = NULL_TREE;
7087       tree elt, next;
7088
7089       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7090            elt; elt = next)
7091         {
7092           tree field = TREE_PURPOSE (elt);
7093           char *buf;
7094
7095           next = TREE_CHAIN (elt);
7096           TREE_CHAIN (elt) = newlist;
7097           newlist = elt;
7098
7099           /* Also add __ to the beginning of the field name so that code
7100              outside the lambda body can't see the captured name.  We could
7101              just remove the name entirely, but this is more useful for
7102              debugging.  */
7103           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7104             /* The 'this' capture already starts with __.  */
7105             continue;
7106
7107           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7108           buf[1] = buf[0] = '_';
7109           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7110                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7111           DECL_NAME (field) = get_identifier (buf);
7112         }
7113       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7114     }
7115
7116     maybe_add_lambda_conv_op (type);
7117
7118     type = finish_struct (type, /*attributes=*/NULL_TREE);
7119
7120     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7121   }
7122
7123   pop_deferring_access_checks ();
7124
7125   return build_lambda_object (lambda_expr);
7126 }
7127
7128 /* Parse the beginning of a lambda expression.
7129
7130    lambda-introducer:
7131      [ lambda-capture [opt] ]
7132
7133    LAMBDA_EXPR is the current representation of the lambda expression.  */
7134
7135 static void
7136 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7137 {
7138   /* Need commas after the first capture.  */
7139   bool first = true;
7140
7141   /* Eat the leading `['.  */
7142   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7143
7144   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7145   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7146       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7147     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7148   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7149     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7150
7151   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7152     {
7153       cp_lexer_consume_token (parser->lexer);
7154       first = false;
7155     }
7156
7157   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7158     {
7159       cp_token* capture_token;
7160       tree capture_id;
7161       tree capture_init_expr;
7162       cp_id_kind idk = CP_ID_KIND_NONE;
7163       bool explicit_init_p = false;
7164
7165       enum capture_kind_type
7166       {
7167         BY_COPY,
7168         BY_REFERENCE
7169       };
7170       enum capture_kind_type capture_kind = BY_COPY;
7171
7172       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7173         {
7174           error ("expected end of capture-list");
7175           return;
7176         }
7177
7178       if (first)
7179         first = false;
7180       else
7181         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7182
7183       /* Possibly capture `this'.  */
7184       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7185         {
7186           cp_lexer_consume_token (parser->lexer);
7187           add_capture (lambda_expr,
7188                        /*id=*/get_identifier ("__this"),
7189                        /*initializer=*/finish_this_expr(),
7190                        /*by_reference_p=*/false,
7191                        explicit_init_p);
7192           continue;
7193         }
7194
7195       /* Remember whether we want to capture as a reference or not.  */
7196       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7197         {
7198           capture_kind = BY_REFERENCE;
7199           cp_lexer_consume_token (parser->lexer);
7200         }
7201
7202       /* Get the identifier.  */
7203       capture_token = cp_lexer_peek_token (parser->lexer);
7204       capture_id = cp_parser_identifier (parser);
7205
7206       if (capture_id == error_mark_node)
7207         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7208            delimiters, but I modified this to stop on unnested ']' as well.  It
7209            was already changed to stop on unnested '}', so the
7210            "closing_parenthesis" name is no more misleading with my change.  */
7211         {
7212           cp_parser_skip_to_closing_parenthesis (parser,
7213                                                  /*recovering=*/true,
7214                                                  /*or_comma=*/true,
7215                                                  /*consume_paren=*/true);
7216           break;
7217         }
7218
7219       /* Find the initializer for this capture.  */
7220       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7221         {
7222           /* An explicit expression exists.  */
7223           cp_lexer_consume_token (parser->lexer);
7224           pedwarn (input_location, OPT_pedantic,
7225                    "ISO C++ does not allow initializers "
7226                    "in lambda expression capture lists");
7227           capture_init_expr = cp_parser_assignment_expression (parser,
7228                                                                /*cast_p=*/true,
7229                                                                &idk);
7230           explicit_init_p = true;
7231         }
7232       else
7233         {
7234           const char* error_msg;
7235
7236           /* Turn the identifier into an id-expression.  */
7237           capture_init_expr
7238             = cp_parser_lookup_name
7239                 (parser,
7240                  capture_id,
7241                  none_type,
7242                  /*is_template=*/false,
7243                  /*is_namespace=*/false,
7244                  /*check_dependency=*/true,
7245                  /*ambiguous_decls=*/NULL,
7246                  capture_token->location);
7247
7248           capture_init_expr
7249             = finish_id_expression
7250                 (capture_id,
7251                  capture_init_expr,
7252                  parser->scope,
7253                  &idk,
7254                  /*integral_constant_expression_p=*/false,
7255                  /*allow_non_integral_constant_expression_p=*/false,
7256                  /*non_integral_constant_expression_p=*/NULL,
7257                  /*template_p=*/false,
7258                  /*done=*/true,
7259                  /*address_p=*/false,
7260                  /*template_arg_p=*/false,
7261                  &error_msg,
7262                  capture_token->location);
7263         }
7264
7265       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7266         capture_init_expr
7267           = unqualified_name_lookup_error (capture_init_expr);
7268
7269       add_capture (lambda_expr,
7270                    capture_id,
7271                    capture_init_expr,
7272                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7273                    explicit_init_p);
7274     }
7275
7276   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7277 }
7278
7279 /* Parse the (optional) middle of a lambda expression.
7280
7281    lambda-declarator:
7282      ( parameter-declaration-clause [opt] )
7283        attribute-specifier [opt]
7284        mutable [opt]
7285        exception-specification [opt]
7286        lambda-return-type-clause [opt]
7287
7288    LAMBDA_EXPR is the current representation of the lambda expression.  */
7289
7290 static void
7291 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7292 {
7293   /* 5.1.1.4 of the standard says:
7294        If a lambda-expression does not include a lambda-declarator, it is as if
7295        the lambda-declarator were ().
7296      This means an empty parameter list, no attributes, and no exception
7297      specification.  */
7298   tree param_list = void_list_node;
7299   tree attributes = NULL_TREE;
7300   tree exception_spec = NULL_TREE;
7301   tree t;
7302
7303   /* The lambda-declarator is optional, but must begin with an opening
7304      parenthesis if present.  */
7305   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7306     {
7307       cp_lexer_consume_token (parser->lexer);
7308
7309       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7310
7311       /* Parse parameters.  */
7312       param_list = cp_parser_parameter_declaration_clause (parser);
7313
7314       /* Default arguments shall not be specified in the
7315          parameter-declaration-clause of a lambda-declarator.  */
7316       for (t = param_list; t; t = TREE_CHAIN (t))
7317         if (TREE_PURPOSE (t))
7318           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7319                    "default argument specified for lambda parameter");
7320
7321       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7322
7323       attributes = cp_parser_attributes_opt (parser);
7324
7325       /* Parse optional `mutable' keyword.  */
7326       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7327         {
7328           cp_lexer_consume_token (parser->lexer);
7329           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7330         }
7331
7332       /* Parse optional exception specification.  */
7333       exception_spec = cp_parser_exception_specification_opt (parser);
7334
7335       /* Parse optional trailing return type.  */
7336       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7337         {
7338           cp_lexer_consume_token (parser->lexer);
7339           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7340         }
7341
7342       /* The function parameters must be in scope all the way until after the
7343          trailing-return-type in case of decltype.  */
7344       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7345         pop_binding (DECL_NAME (t), t);
7346
7347       leave_scope ();
7348     }
7349
7350   /* Create the function call operator.
7351
7352      Messing with declarators like this is no uglier than building up the
7353      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7354      other code.  */
7355   {
7356     cp_decl_specifier_seq return_type_specs;
7357     cp_declarator* declarator;
7358     tree fco;
7359     int quals;
7360     void *p;
7361
7362     clear_decl_specs (&return_type_specs);
7363     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7364       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7365     else
7366       /* Maybe we will deduce the return type later, but we can use void
7367          as a placeholder return type anyways.  */
7368       return_type_specs.type = void_type_node;
7369
7370     p = obstack_alloc (&declarator_obstack, 0);
7371
7372     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7373                                      sfk_none);
7374
7375     quals = TYPE_UNQUALIFIED;
7376     if (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) == NULL_TREE
7377         && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_NONE)
7378       {
7379         /* A lambda with no captures has a static op() and a conversion op
7380            to function type.  */
7381         if (LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7382           error ("lambda expression with no captures declared mutable");
7383         return_type_specs.storage_class = sc_static;
7384       }
7385     else if (!LAMBDA_EXPR_MUTABLE_P (lambda_expr))
7386       quals = TYPE_QUAL_CONST;
7387     declarator = make_call_declarator (declarator, param_list, quals,
7388                                        exception_spec,
7389                                        /*late_return_type=*/NULL_TREE);
7390
7391     fco = grokmethod (&return_type_specs,
7392                       declarator,
7393                       attributes);
7394     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7395     DECL_ARTIFICIAL (fco) = 1;
7396
7397     finish_member_declaration (fco);
7398
7399     obstack_free (&declarator_obstack, p);
7400   }
7401 }
7402
7403 /* Parse the body of a lambda expression, which is simply
7404
7405    compound-statement
7406
7407    but which requires special handling.
7408    LAMBDA_EXPR is the current representation of the lambda expression.  */
7409
7410 static void
7411 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7412 {
7413   bool nested = (current_function_decl != NULL_TREE);
7414   if (nested)
7415     push_function_context ();
7416
7417   /* Finish the function call operator
7418      - class_specifier
7419      + late_parsing_for_member
7420      + function_definition_after_declarator
7421      + ctor_initializer_opt_and_function_body  */
7422   {
7423     tree fco = lambda_function (lambda_expr);
7424     tree body;
7425     bool done = false;
7426
7427     /* Let the front end know that we are going to be defining this
7428        function.  */
7429     start_preparsed_function (fco,
7430                               NULL_TREE,
7431                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7432
7433     start_lambda_scope (fco);
7434     body = begin_function_body ();
7435
7436     /* 5.1.1.4 of the standard says:
7437          If a lambda-expression does not include a trailing-return-type, it
7438          is as if the trailing-return-type denotes the following type:
7439           * if the compound-statement is of the form
7440                { return attribute-specifier [opt] expression ; }
7441              the type of the returned expression after lvalue-to-rvalue
7442              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7443              (_conv.array_ 4.2), and function-to-pointer conversion
7444              (_conv.func_ 4.3);
7445           * otherwise, void.  */
7446
7447     /* In a lambda that has neither a lambda-return-type-clause
7448        nor a deducible form, errors should be reported for return statements
7449        in the body.  Since we used void as the placeholder return type, parsing
7450        the body as usual will give such desired behavior.  */
7451     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7452         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7453         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7454         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7455       {
7456         tree compound_stmt;
7457         tree expr = NULL_TREE;
7458         cp_id_kind idk = CP_ID_KIND_NONE;
7459
7460         /* Parse tentatively in case there's more after the initial return
7461            statement.  */
7462         cp_parser_parse_tentatively (parser);
7463
7464         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7465         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7466
7467         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7468
7469         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7470         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7471
7472         if (cp_parser_parse_definitely (parser))
7473           {
7474             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7475
7476             compound_stmt = begin_compound_stmt (0);
7477             /* Will get error here if type not deduced yet.  */
7478             finish_return_stmt (expr);
7479             finish_compound_stmt (compound_stmt);
7480
7481             done = true;
7482           }
7483       }
7484
7485     if (!done)
7486       {
7487         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7488           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7489         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7490            cp_parser_compound_stmt does not pass it.  */
7491         cp_parser_function_body (parser);
7492         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7493       }
7494
7495     finish_function_body (body);
7496     finish_lambda_scope ();
7497
7498     /* Finish the function and generate code for it if necessary.  */
7499     expand_or_defer_fn (finish_function (/*inline*/2));
7500   }
7501
7502   if (nested)
7503     pop_function_context();
7504 }
7505
7506 /* Statements [gram.stmt.stmt]  */
7507
7508 /* Parse a statement.
7509
7510    statement:
7511      labeled-statement
7512      expression-statement
7513      compound-statement
7514      selection-statement
7515      iteration-statement
7516      jump-statement
7517      declaration-statement
7518      try-block
7519
7520   IN_COMPOUND is true when the statement is nested inside a
7521   cp_parser_compound_statement; this matters for certain pragmas.
7522
7523   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7524   is a (possibly labeled) if statement which is not enclosed in braces
7525   and has an else clause.  This is used to implement -Wparentheses.  */
7526
7527 static void
7528 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7529                      bool in_compound, bool *if_p)
7530 {
7531   tree statement;
7532   cp_token *token;
7533   location_t statement_location;
7534
7535  restart:
7536   if (if_p != NULL)
7537     *if_p = false;
7538   /* There is no statement yet.  */
7539   statement = NULL_TREE;
7540   /* Peek at the next token.  */
7541   token = cp_lexer_peek_token (parser->lexer);
7542   /* Remember the location of the first token in the statement.  */
7543   statement_location = token->location;
7544   /* If this is a keyword, then that will often determine what kind of
7545      statement we have.  */
7546   if (token->type == CPP_KEYWORD)
7547     {
7548       enum rid keyword = token->keyword;
7549
7550       switch (keyword)
7551         {
7552         case RID_CASE:
7553         case RID_DEFAULT:
7554           /* Looks like a labeled-statement with a case label.
7555              Parse the label, and then use tail recursion to parse
7556              the statement.  */
7557           cp_parser_label_for_labeled_statement (parser);
7558           goto restart;
7559
7560         case RID_IF:
7561         case RID_SWITCH:
7562           statement = cp_parser_selection_statement (parser, if_p);
7563           break;
7564
7565         case RID_WHILE:
7566         case RID_DO:
7567         case RID_FOR:
7568           statement = cp_parser_iteration_statement (parser);
7569           break;
7570
7571         case RID_BREAK:
7572         case RID_CONTINUE:
7573         case RID_RETURN:
7574         case RID_GOTO:
7575           statement = cp_parser_jump_statement (parser);
7576           break;
7577
7578           /* Objective-C++ exception-handling constructs.  */
7579         case RID_AT_TRY:
7580         case RID_AT_CATCH:
7581         case RID_AT_FINALLY:
7582         case RID_AT_SYNCHRONIZED:
7583         case RID_AT_THROW:
7584           statement = cp_parser_objc_statement (parser);
7585           break;
7586
7587         case RID_TRY:
7588           statement = cp_parser_try_block (parser);
7589           break;
7590
7591         case RID_NAMESPACE:
7592           /* This must be a namespace alias definition.  */
7593           cp_parser_declaration_statement (parser);
7594           return;
7595           
7596         default:
7597           /* It might be a keyword like `int' that can start a
7598              declaration-statement.  */
7599           break;
7600         }
7601     }
7602   else if (token->type == CPP_NAME)
7603     {
7604       /* If the next token is a `:', then we are looking at a
7605          labeled-statement.  */
7606       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7607       if (token->type == CPP_COLON)
7608         {
7609           /* Looks like a labeled-statement with an ordinary label.
7610              Parse the label, and then use tail recursion to parse
7611              the statement.  */
7612           cp_parser_label_for_labeled_statement (parser);
7613           goto restart;
7614         }
7615     }
7616   /* Anything that starts with a `{' must be a compound-statement.  */
7617   else if (token->type == CPP_OPEN_BRACE)
7618     statement = cp_parser_compound_statement (parser, NULL, false);
7619   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7620      a statement all its own.  */
7621   else if (token->type == CPP_PRAGMA)
7622     {
7623       /* Only certain OpenMP pragmas are attached to statements, and thus
7624          are considered statements themselves.  All others are not.  In
7625          the context of a compound, accept the pragma as a "statement" and
7626          return so that we can check for a close brace.  Otherwise we
7627          require a real statement and must go back and read one.  */
7628       if (in_compound)
7629         cp_parser_pragma (parser, pragma_compound);
7630       else if (!cp_parser_pragma (parser, pragma_stmt))
7631         goto restart;
7632       return;
7633     }
7634   else if (token->type == CPP_EOF)
7635     {
7636       cp_parser_error (parser, "expected statement");
7637       return;
7638     }
7639
7640   /* Everything else must be a declaration-statement or an
7641      expression-statement.  Try for the declaration-statement
7642      first, unless we are looking at a `;', in which case we know that
7643      we have an expression-statement.  */
7644   if (!statement)
7645     {
7646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7647         {
7648           cp_parser_parse_tentatively (parser);
7649           /* Try to parse the declaration-statement.  */
7650           cp_parser_declaration_statement (parser);
7651           /* If that worked, we're done.  */
7652           if (cp_parser_parse_definitely (parser))
7653             return;
7654         }
7655       /* Look for an expression-statement instead.  */
7656       statement = cp_parser_expression_statement (parser, in_statement_expr);
7657     }
7658
7659   /* Set the line number for the statement.  */
7660   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7661     SET_EXPR_LOCATION (statement, statement_location);
7662 }
7663
7664 /* Parse the label for a labeled-statement, i.e.
7665
7666    identifier :
7667    case constant-expression :
7668    default :
7669
7670    GNU Extension:
7671    case constant-expression ... constant-expression : statement
7672
7673    When a label is parsed without errors, the label is added to the
7674    parse tree by the finish_* functions, so this function doesn't
7675    have to return the label.  */
7676
7677 static void
7678 cp_parser_label_for_labeled_statement (cp_parser* parser)
7679 {
7680   cp_token *token;
7681   tree label = NULL_TREE;
7682
7683   /* The next token should be an identifier.  */
7684   token = cp_lexer_peek_token (parser->lexer);
7685   if (token->type != CPP_NAME
7686       && token->type != CPP_KEYWORD)
7687     {
7688       cp_parser_error (parser, "expected labeled-statement");
7689       return;
7690     }
7691
7692   switch (token->keyword)
7693     {
7694     case RID_CASE:
7695       {
7696         tree expr, expr_hi;
7697         cp_token *ellipsis;
7698
7699         /* Consume the `case' token.  */
7700         cp_lexer_consume_token (parser->lexer);
7701         /* Parse the constant-expression.  */
7702         expr = cp_parser_constant_expression (parser,
7703                                               /*allow_non_constant_p=*/false,
7704                                               NULL);
7705
7706         ellipsis = cp_lexer_peek_token (parser->lexer);
7707         if (ellipsis->type == CPP_ELLIPSIS)
7708           {
7709             /* Consume the `...' token.  */
7710             cp_lexer_consume_token (parser->lexer);
7711             expr_hi =
7712               cp_parser_constant_expression (parser,
7713                                              /*allow_non_constant_p=*/false,
7714                                              NULL);
7715             /* We don't need to emit warnings here, as the common code
7716                will do this for us.  */
7717           }
7718         else
7719           expr_hi = NULL_TREE;
7720
7721         if (parser->in_switch_statement_p)
7722           finish_case_label (token->location, expr, expr_hi);
7723         else
7724           error_at (token->location,
7725                     "case label %qE not within a switch statement",
7726                     expr);
7727       }
7728       break;
7729
7730     case RID_DEFAULT:
7731       /* Consume the `default' token.  */
7732       cp_lexer_consume_token (parser->lexer);
7733
7734       if (parser->in_switch_statement_p)
7735         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7736       else
7737         error_at (token->location, "case label not within a switch statement");
7738       break;
7739
7740     default:
7741       /* Anything else must be an ordinary label.  */
7742       label = finish_label_stmt (cp_parser_identifier (parser));
7743       break;
7744     }
7745
7746   /* Require the `:' token.  */
7747   cp_parser_require (parser, CPP_COLON, "%<:%>");
7748
7749   /* An ordinary label may optionally be followed by attributes.
7750      However, this is only permitted if the attributes are then
7751      followed by a semicolon.  This is because, for backward
7752      compatibility, when parsing
7753        lab: __attribute__ ((unused)) int i;
7754      we want the attribute to attach to "i", not "lab".  */
7755   if (label != NULL_TREE
7756       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7757     {
7758       tree attrs;
7759
7760       cp_parser_parse_tentatively (parser);
7761       attrs = cp_parser_attributes_opt (parser);
7762       if (attrs == NULL_TREE
7763           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7764         cp_parser_abort_tentative_parse (parser);
7765       else if (!cp_parser_parse_definitely (parser))
7766         ;
7767       else
7768         cplus_decl_attributes (&label, attrs, 0);
7769     }
7770 }
7771
7772 /* Parse an expression-statement.
7773
7774    expression-statement:
7775      expression [opt] ;
7776
7777    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7778    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7779    indicates whether this expression-statement is part of an
7780    expression statement.  */
7781
7782 static tree
7783 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7784 {
7785   tree statement = NULL_TREE;
7786   cp_token *token = cp_lexer_peek_token (parser->lexer);
7787
7788   /* If the next token is a ';', then there is no expression
7789      statement.  */
7790   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7791     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7792
7793   /* Give a helpful message for "A<T>::type t;"  */
7794   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7795       && !cp_parser_uncommitted_to_tentative_parse_p (parser)
7796       && TREE_CODE (statement) == SCOPE_REF)
7797     error_at (token->location, "need %<typename%> before %qE because "
7798               "%qT is a dependent scope",
7799               statement, TREE_OPERAND (statement, 0));
7800
7801   /* Consume the final `;'.  */
7802   cp_parser_consume_semicolon_at_end_of_statement (parser);
7803
7804   if (in_statement_expr
7805       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7806     /* This is the final expression statement of a statement
7807        expression.  */
7808     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7809   else if (statement)
7810     statement = finish_expr_stmt (statement);
7811   else
7812     finish_stmt ();
7813
7814   return statement;
7815 }
7816
7817 /* Parse a compound-statement.
7818
7819    compound-statement:
7820      { statement-seq [opt] }
7821
7822    GNU extension:
7823
7824    compound-statement:
7825      { label-declaration-seq [opt] statement-seq [opt] }
7826
7827    label-declaration-seq:
7828      label-declaration
7829      label-declaration-seq label-declaration
7830
7831    Returns a tree representing the statement.  */
7832
7833 static tree
7834 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7835                               bool in_try)
7836 {
7837   tree compound_stmt;
7838
7839   /* Consume the `{'.  */
7840   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7841     return error_mark_node;
7842   /* Begin the compound-statement.  */
7843   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7844   /* If the next keyword is `__label__' we have a label declaration.  */
7845   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7846     cp_parser_label_declaration (parser);
7847   /* Parse an (optional) statement-seq.  */
7848   cp_parser_statement_seq_opt (parser, in_statement_expr);
7849   /* Finish the compound-statement.  */
7850   finish_compound_stmt (compound_stmt);
7851   /* Consume the `}'.  */
7852   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7853
7854   return compound_stmt;
7855 }
7856
7857 /* Parse an (optional) statement-seq.
7858
7859    statement-seq:
7860      statement
7861      statement-seq [opt] statement  */
7862
7863 static void
7864 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7865 {
7866   /* Scan statements until there aren't any more.  */
7867   while (true)
7868     {
7869       cp_token *token = cp_lexer_peek_token (parser->lexer);
7870
7871       /* If we're looking at a `}', then we've run out of statements.  */
7872       if (token->type == CPP_CLOSE_BRACE
7873           || token->type == CPP_EOF
7874           || token->type == CPP_PRAGMA_EOL)
7875         break;
7876       
7877       /* If we are in a compound statement and find 'else' then
7878          something went wrong.  */
7879       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7880         {
7881           if (parser->in_statement & IN_IF_STMT) 
7882             break;
7883           else
7884             {
7885               token = cp_lexer_consume_token (parser->lexer);
7886               error_at (token->location, "%<else%> without a previous %<if%>");
7887             }
7888         }
7889
7890       /* Parse the statement.  */
7891       cp_parser_statement (parser, in_statement_expr, true, NULL);
7892     }
7893 }
7894
7895 /* Parse a selection-statement.
7896
7897    selection-statement:
7898      if ( condition ) statement
7899      if ( condition ) statement else statement
7900      switch ( condition ) statement
7901
7902    Returns the new IF_STMT or SWITCH_STMT.
7903
7904    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7905    is a (possibly labeled) if statement which is not enclosed in
7906    braces and has an else clause.  This is used to implement
7907    -Wparentheses.  */
7908
7909 static tree
7910 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7911 {
7912   cp_token *token;
7913   enum rid keyword;
7914
7915   if (if_p != NULL)
7916     *if_p = false;
7917
7918   /* Peek at the next token.  */
7919   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7920
7921   /* See what kind of keyword it is.  */
7922   keyword = token->keyword;
7923   switch (keyword)
7924     {
7925     case RID_IF:
7926     case RID_SWITCH:
7927       {
7928         tree statement;
7929         tree condition;
7930
7931         /* Look for the `('.  */
7932         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7933           {
7934             cp_parser_skip_to_end_of_statement (parser);
7935             return error_mark_node;
7936           }
7937
7938         /* Begin the selection-statement.  */
7939         if (keyword == RID_IF)
7940           statement = begin_if_stmt ();
7941         else
7942           statement = begin_switch_stmt ();
7943
7944         /* Parse the condition.  */
7945         condition = cp_parser_condition (parser);
7946         /* Look for the `)'.  */
7947         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7948           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7949                                                  /*consume_paren=*/true);
7950
7951         if (keyword == RID_IF)
7952           {
7953             bool nested_if;
7954             unsigned char in_statement;
7955
7956             /* Add the condition.  */
7957             finish_if_stmt_cond (condition, statement);
7958
7959             /* Parse the then-clause.  */
7960             in_statement = parser->in_statement;
7961             parser->in_statement |= IN_IF_STMT;
7962             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7963               {
7964                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7965                 add_stmt (build_empty_stmt (loc));
7966                 cp_lexer_consume_token (parser->lexer);
7967                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7968                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7969                               "empty body in an %<if%> statement");
7970                 nested_if = false;
7971               }
7972             else
7973               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7974             parser->in_statement = in_statement;
7975
7976             finish_then_clause (statement);
7977
7978             /* If the next token is `else', parse the else-clause.  */
7979             if (cp_lexer_next_token_is_keyword (parser->lexer,
7980                                                 RID_ELSE))
7981               {
7982                 /* Consume the `else' keyword.  */
7983                 cp_lexer_consume_token (parser->lexer);
7984                 begin_else_clause (statement);
7985                 /* Parse the else-clause.  */
7986                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7987                   {
7988                     location_t loc;
7989                     loc = cp_lexer_peek_token (parser->lexer)->location;
7990                     warning_at (loc,
7991                                 OPT_Wempty_body, "suggest braces around "
7992                                 "empty body in an %<else%> statement");
7993                     add_stmt (build_empty_stmt (loc));
7994                     cp_lexer_consume_token (parser->lexer);
7995                   }
7996                 else
7997                   cp_parser_implicitly_scoped_statement (parser, NULL);
7998
7999                 finish_else_clause (statement);
8000
8001                 /* If we are currently parsing a then-clause, then
8002                    IF_P will not be NULL.  We set it to true to
8003                    indicate that this if statement has an else clause.
8004                    This may trigger the Wparentheses warning below
8005                    when we get back up to the parent if statement.  */
8006                 if (if_p != NULL)
8007                   *if_p = true;
8008               }
8009             else
8010               {
8011                 /* This if statement does not have an else clause.  If
8012                    NESTED_IF is true, then the then-clause is an if
8013                    statement which does have an else clause.  We warn
8014                    about the potential ambiguity.  */
8015                 if (nested_if)
8016                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8017                               "suggest explicit braces to avoid ambiguous"
8018                               " %<else%>");
8019               }
8020
8021             /* Now we're all done with the if-statement.  */
8022             finish_if_stmt (statement);
8023           }
8024         else
8025           {
8026             bool in_switch_statement_p;
8027             unsigned char in_statement;
8028
8029             /* Add the condition.  */
8030             finish_switch_cond (condition, statement);
8031
8032             /* Parse the body of the switch-statement.  */
8033             in_switch_statement_p = parser->in_switch_statement_p;
8034             in_statement = parser->in_statement;
8035             parser->in_switch_statement_p = true;
8036             parser->in_statement |= IN_SWITCH_STMT;
8037             cp_parser_implicitly_scoped_statement (parser, NULL);
8038             parser->in_switch_statement_p = in_switch_statement_p;
8039             parser->in_statement = in_statement;
8040
8041             /* Now we're all done with the switch-statement.  */
8042             finish_switch_stmt (statement);
8043           }
8044
8045         return statement;
8046       }
8047       break;
8048
8049     default:
8050       cp_parser_error (parser, "expected selection-statement");
8051       return error_mark_node;
8052     }
8053 }
8054
8055 /* Parse a condition.
8056
8057    condition:
8058      expression
8059      type-specifier-seq declarator = initializer-clause
8060      type-specifier-seq declarator braced-init-list
8061
8062    GNU Extension:
8063
8064    condition:
8065      type-specifier-seq declarator asm-specification [opt]
8066        attributes [opt] = assignment-expression
8067
8068    Returns the expression that should be tested.  */
8069
8070 static tree
8071 cp_parser_condition (cp_parser* parser)
8072 {
8073   cp_decl_specifier_seq type_specifiers;
8074   const char *saved_message;
8075
8076   /* Try the declaration first.  */
8077   cp_parser_parse_tentatively (parser);
8078   /* New types are not allowed in the type-specifier-seq for a
8079      condition.  */
8080   saved_message = parser->type_definition_forbidden_message;
8081   parser->type_definition_forbidden_message
8082     = "types may not be defined in conditions";
8083   /* Parse the type-specifier-seq.  */
8084   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8085                                 /*is_trailing_return=*/false,
8086                                 &type_specifiers);
8087   /* Restore the saved message.  */
8088   parser->type_definition_forbidden_message = saved_message;
8089   /* If all is well, we might be looking at a declaration.  */
8090   if (!cp_parser_error_occurred (parser))
8091     {
8092       tree decl;
8093       tree asm_specification;
8094       tree attributes;
8095       cp_declarator *declarator;
8096       tree initializer = NULL_TREE;
8097
8098       /* Parse the declarator.  */
8099       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8100                                          /*ctor_dtor_or_conv_p=*/NULL,
8101                                          /*parenthesized_p=*/NULL,
8102                                          /*member_p=*/false);
8103       /* Parse the attributes.  */
8104       attributes = cp_parser_attributes_opt (parser);
8105       /* Parse the asm-specification.  */
8106       asm_specification = cp_parser_asm_specification_opt (parser);
8107       /* If the next token is not an `=' or '{', then we might still be
8108          looking at an expression.  For example:
8109
8110            if (A(a).x)
8111
8112          looks like a decl-specifier-seq and a declarator -- but then
8113          there is no `=', so this is an expression.  */
8114       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8115           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8116         cp_parser_simulate_error (parser);
8117         
8118       /* If we did see an `=' or '{', then we are looking at a declaration
8119          for sure.  */
8120       if (cp_parser_parse_definitely (parser))
8121         {
8122           tree pushed_scope;
8123           bool non_constant_p;
8124           bool flags = LOOKUP_ONLYCONVERTING;
8125
8126           /* Create the declaration.  */
8127           decl = start_decl (declarator, &type_specifiers,
8128                              /*initialized_p=*/true,
8129                              attributes, /*prefix_attributes=*/NULL_TREE,
8130                              &pushed_scope);
8131
8132           /* Parse the initializer.  */
8133           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8134             {
8135               initializer = cp_parser_braced_list (parser, &non_constant_p);
8136               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8137               flags = 0;
8138             }
8139           else
8140             {
8141               /* Consume the `='.  */
8142               cp_parser_require (parser, CPP_EQ, "%<=%>");
8143               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8144             }
8145           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8146             maybe_warn_cpp0x ("extended initializer lists");
8147
8148           if (!non_constant_p)
8149             initializer = fold_non_dependent_expr (initializer);
8150
8151           /* Process the initializer.  */
8152           cp_finish_decl (decl,
8153                           initializer, !non_constant_p,
8154                           asm_specification,
8155                           flags);
8156
8157           if (pushed_scope)
8158             pop_scope (pushed_scope);
8159
8160           return convert_from_reference (decl);
8161         }
8162     }
8163   /* If we didn't even get past the declarator successfully, we are
8164      definitely not looking at a declaration.  */
8165   else
8166     cp_parser_abort_tentative_parse (parser);
8167
8168   /* Otherwise, we are looking at an expression.  */
8169   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8170 }
8171
8172 /* Parse an iteration-statement.
8173
8174    iteration-statement:
8175      while ( condition ) statement
8176      do statement while ( expression ) ;
8177      for ( for-init-statement condition [opt] ; expression [opt] )
8178        statement
8179
8180    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8181
8182 static tree
8183 cp_parser_iteration_statement (cp_parser* parser)
8184 {
8185   cp_token *token;
8186   enum rid keyword;
8187   tree statement;
8188   unsigned char in_statement;
8189
8190   /* Peek at the next token.  */
8191   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8192   if (!token)
8193     return error_mark_node;
8194
8195   /* Remember whether or not we are already within an iteration
8196      statement.  */
8197   in_statement = parser->in_statement;
8198
8199   /* See what kind of keyword it is.  */
8200   keyword = token->keyword;
8201   switch (keyword)
8202     {
8203     case RID_WHILE:
8204       {
8205         tree condition;
8206
8207         /* Begin the while-statement.  */
8208         statement = begin_while_stmt ();
8209         /* Look for the `('.  */
8210         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8211         /* Parse the condition.  */
8212         condition = cp_parser_condition (parser);
8213         finish_while_stmt_cond (condition, statement);
8214         /* Look for the `)'.  */
8215         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8216         /* Parse the dependent statement.  */
8217         parser->in_statement = IN_ITERATION_STMT;
8218         cp_parser_already_scoped_statement (parser);
8219         parser->in_statement = in_statement;
8220         /* We're done with the while-statement.  */
8221         finish_while_stmt (statement);
8222       }
8223       break;
8224
8225     case RID_DO:
8226       {
8227         tree expression;
8228
8229         /* Begin the do-statement.  */
8230         statement = begin_do_stmt ();
8231         /* Parse the body of the do-statement.  */
8232         parser->in_statement = IN_ITERATION_STMT;
8233         cp_parser_implicitly_scoped_statement (parser, NULL);
8234         parser->in_statement = in_statement;
8235         finish_do_body (statement);
8236         /* Look for the `while' keyword.  */
8237         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8238         /* Look for the `('.  */
8239         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8240         /* Parse the expression.  */
8241         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8242         /* We're done with the do-statement.  */
8243         finish_do_stmt (expression, statement);
8244         /* Look for the `)'.  */
8245         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8246         /* Look for the `;'.  */
8247         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8248       }
8249       break;
8250
8251     case RID_FOR:
8252       {
8253         tree condition = NULL_TREE;
8254         tree expression = NULL_TREE;
8255
8256         /* Begin the for-statement.  */
8257         statement = begin_for_stmt ();
8258         /* Look for the `('.  */
8259         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8260         /* Parse the initialization.  */
8261         cp_parser_for_init_statement (parser);
8262         finish_for_init_stmt (statement);
8263
8264         /* If there's a condition, process it.  */
8265         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8266           condition = cp_parser_condition (parser);
8267         finish_for_cond (condition, statement);
8268         /* Look for the `;'.  */
8269         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8270
8271         /* If there's an expression, process it.  */
8272         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8273           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8274         finish_for_expr (expression, statement);
8275         /* Look for the `)'.  */
8276         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8277
8278         /* Parse the body of the for-statement.  */
8279         parser->in_statement = IN_ITERATION_STMT;
8280         cp_parser_already_scoped_statement (parser);
8281         parser->in_statement = in_statement;
8282
8283         /* We're done with the for-statement.  */
8284         finish_for_stmt (statement);
8285       }
8286       break;
8287
8288     default:
8289       cp_parser_error (parser, "expected iteration-statement");
8290       statement = error_mark_node;
8291       break;
8292     }
8293
8294   return statement;
8295 }
8296
8297 /* Parse a for-init-statement.
8298
8299    for-init-statement:
8300      expression-statement
8301      simple-declaration  */
8302
8303 static void
8304 cp_parser_for_init_statement (cp_parser* parser)
8305 {
8306   /* If the next token is a `;', then we have an empty
8307      expression-statement.  Grammatically, this is also a
8308      simple-declaration, but an invalid one, because it does not
8309      declare anything.  Therefore, if we did not handle this case
8310      specially, we would issue an error message about an invalid
8311      declaration.  */
8312   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8313     {
8314       /* We're going to speculatively look for a declaration, falling back
8315          to an expression, if necessary.  */
8316       cp_parser_parse_tentatively (parser);
8317       /* Parse the declaration.  */
8318       cp_parser_simple_declaration (parser,
8319                                     /*function_definition_allowed_p=*/false);
8320       /* If the tentative parse failed, then we shall need to look for an
8321          expression-statement.  */
8322       if (cp_parser_parse_definitely (parser))
8323         return;
8324     }
8325
8326   cp_parser_expression_statement (parser, false);
8327 }
8328
8329 /* Parse a jump-statement.
8330
8331    jump-statement:
8332      break ;
8333      continue ;
8334      return expression [opt] ;
8335      return braced-init-list ;
8336      goto identifier ;
8337
8338    GNU extension:
8339
8340    jump-statement:
8341      goto * expression ;
8342
8343    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8344
8345 static tree
8346 cp_parser_jump_statement (cp_parser* parser)
8347 {
8348   tree statement = error_mark_node;
8349   cp_token *token;
8350   enum rid keyword;
8351   unsigned char in_statement;
8352
8353   /* Peek at the next token.  */
8354   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8355   if (!token)
8356     return error_mark_node;
8357
8358   /* See what kind of keyword it is.  */
8359   keyword = token->keyword;
8360   switch (keyword)
8361     {
8362     case RID_BREAK:
8363       in_statement = parser->in_statement & ~IN_IF_STMT;      
8364       switch (in_statement)
8365         {
8366         case 0:
8367           error_at (token->location, "break statement not within loop or switch");
8368           break;
8369         default:
8370           gcc_assert ((in_statement & IN_SWITCH_STMT)
8371                       || in_statement == IN_ITERATION_STMT);
8372           statement = finish_break_stmt ();
8373           break;
8374         case IN_OMP_BLOCK:
8375           error_at (token->location, "invalid exit from OpenMP structured block");
8376           break;
8377         case IN_OMP_FOR:
8378           error_at (token->location, "break statement used with OpenMP for loop");
8379           break;
8380         }
8381       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8382       break;
8383
8384     case RID_CONTINUE:
8385       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8386         {
8387         case 0:
8388           error_at (token->location, "continue statement not within a loop");
8389           break;
8390         case IN_ITERATION_STMT:
8391         case IN_OMP_FOR:
8392           statement = finish_continue_stmt ();
8393           break;
8394         case IN_OMP_BLOCK:
8395           error_at (token->location, "invalid exit from OpenMP structured block");
8396           break;
8397         default:
8398           gcc_unreachable ();
8399         }
8400       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8401       break;
8402
8403     case RID_RETURN:
8404       {
8405         tree expr;
8406         bool expr_non_constant_p;
8407
8408         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8409           {
8410             maybe_warn_cpp0x ("extended initializer lists");
8411             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8412           }
8413         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8414           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8415         else
8416           /* If the next token is a `;', then there is no
8417              expression.  */
8418           expr = NULL_TREE;
8419         /* Build the return-statement.  */
8420         statement = finish_return_stmt (expr);
8421         /* Look for the final `;'.  */
8422         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8423       }
8424       break;
8425
8426     case RID_GOTO:
8427       /* Create the goto-statement.  */
8428       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8429         {
8430           /* Issue a warning about this use of a GNU extension.  */
8431           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8432           /* Consume the '*' token.  */
8433           cp_lexer_consume_token (parser->lexer);
8434           /* Parse the dependent expression.  */
8435           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8436         }
8437       else
8438         finish_goto_stmt (cp_parser_identifier (parser));
8439       /* Look for the final `;'.  */
8440       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8441       break;
8442
8443     default:
8444       cp_parser_error (parser, "expected jump-statement");
8445       break;
8446     }
8447
8448   return statement;
8449 }
8450
8451 /* Parse a declaration-statement.
8452
8453    declaration-statement:
8454      block-declaration  */
8455
8456 static void
8457 cp_parser_declaration_statement (cp_parser* parser)
8458 {
8459   void *p;
8460
8461   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8462   p = obstack_alloc (&declarator_obstack, 0);
8463
8464  /* Parse the block-declaration.  */
8465   cp_parser_block_declaration (parser, /*statement_p=*/true);
8466
8467   /* Free any declarators allocated.  */
8468   obstack_free (&declarator_obstack, p);
8469
8470   /* Finish off the statement.  */
8471   finish_stmt ();
8472 }
8473
8474 /* Some dependent statements (like `if (cond) statement'), are
8475    implicitly in their own scope.  In other words, if the statement is
8476    a single statement (as opposed to a compound-statement), it is
8477    none-the-less treated as if it were enclosed in braces.  Any
8478    declarations appearing in the dependent statement are out of scope
8479    after control passes that point.  This function parses a statement,
8480    but ensures that is in its own scope, even if it is not a
8481    compound-statement.
8482
8483    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8484    is a (possibly labeled) if statement which is not enclosed in
8485    braces and has an else clause.  This is used to implement
8486    -Wparentheses.
8487
8488    Returns the new statement.  */
8489
8490 static tree
8491 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8492 {
8493   tree statement;
8494
8495   if (if_p != NULL)
8496     *if_p = false;
8497
8498   /* Mark if () ; with a special NOP_EXPR.  */
8499   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8500     {
8501       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8502       cp_lexer_consume_token (parser->lexer);
8503       statement = add_stmt (build_empty_stmt (loc));
8504     }
8505   /* if a compound is opened, we simply parse the statement directly.  */
8506   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8507     statement = cp_parser_compound_statement (parser, NULL, false);
8508   /* If the token is not a `{', then we must take special action.  */
8509   else
8510     {
8511       /* Create a compound-statement.  */
8512       statement = begin_compound_stmt (0);
8513       /* Parse the dependent-statement.  */
8514       cp_parser_statement (parser, NULL_TREE, false, if_p);
8515       /* Finish the dummy compound-statement.  */
8516       finish_compound_stmt (statement);
8517     }
8518
8519   /* Return the statement.  */
8520   return statement;
8521 }
8522
8523 /* For some dependent statements (like `while (cond) statement'), we
8524    have already created a scope.  Therefore, even if the dependent
8525    statement is a compound-statement, we do not want to create another
8526    scope.  */
8527
8528 static void
8529 cp_parser_already_scoped_statement (cp_parser* parser)
8530 {
8531   /* If the token is a `{', then we must take special action.  */
8532   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8533     cp_parser_statement (parser, NULL_TREE, false, NULL);
8534   else
8535     {
8536       /* Avoid calling cp_parser_compound_statement, so that we
8537          don't create a new scope.  Do everything else by hand.  */
8538       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8539       /* If the next keyword is `__label__' we have a label declaration.  */
8540       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8541         cp_parser_label_declaration (parser);
8542       /* Parse an (optional) statement-seq.  */
8543       cp_parser_statement_seq_opt (parser, NULL_TREE);
8544       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8545     }
8546 }
8547
8548 /* Declarations [gram.dcl.dcl] */
8549
8550 /* Parse an optional declaration-sequence.
8551
8552    declaration-seq:
8553      declaration
8554      declaration-seq declaration  */
8555
8556 static void
8557 cp_parser_declaration_seq_opt (cp_parser* parser)
8558 {
8559   while (true)
8560     {
8561       cp_token *token;
8562
8563       token = cp_lexer_peek_token (parser->lexer);
8564
8565       if (token->type == CPP_CLOSE_BRACE
8566           || token->type == CPP_EOF
8567           || token->type == CPP_PRAGMA_EOL)
8568         break;
8569
8570       if (token->type == CPP_SEMICOLON)
8571         {
8572           /* A declaration consisting of a single semicolon is
8573              invalid.  Allow it unless we're being pedantic.  */
8574           cp_lexer_consume_token (parser->lexer);
8575           if (!in_system_header)
8576             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8577           continue;
8578         }
8579
8580       /* If we're entering or exiting a region that's implicitly
8581          extern "C", modify the lang context appropriately.  */
8582       if (!parser->implicit_extern_c && token->implicit_extern_c)
8583         {
8584           push_lang_context (lang_name_c);
8585           parser->implicit_extern_c = true;
8586         }
8587       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8588         {
8589           pop_lang_context ();
8590           parser->implicit_extern_c = false;
8591         }
8592
8593       if (token->type == CPP_PRAGMA)
8594         {
8595           /* A top-level declaration can consist solely of a #pragma.
8596              A nested declaration cannot, so this is done here and not
8597              in cp_parser_declaration.  (A #pragma at block scope is
8598              handled in cp_parser_statement.)  */
8599           cp_parser_pragma (parser, pragma_external);
8600           continue;
8601         }
8602
8603       /* Parse the declaration itself.  */
8604       cp_parser_declaration (parser);
8605     }
8606 }
8607
8608 /* Parse a declaration.
8609
8610    declaration:
8611      block-declaration
8612      function-definition
8613      template-declaration
8614      explicit-instantiation
8615      explicit-specialization
8616      linkage-specification
8617      namespace-definition
8618
8619    GNU extension:
8620
8621    declaration:
8622       __extension__ declaration */
8623
8624 static void
8625 cp_parser_declaration (cp_parser* parser)
8626 {
8627   cp_token token1;
8628   cp_token token2;
8629   int saved_pedantic;
8630   void *p;
8631
8632   /* Check for the `__extension__' keyword.  */
8633   if (cp_parser_extension_opt (parser, &saved_pedantic))
8634     {
8635       /* Parse the qualified declaration.  */
8636       cp_parser_declaration (parser);
8637       /* Restore the PEDANTIC flag.  */
8638       pedantic = saved_pedantic;
8639
8640       return;
8641     }
8642
8643   /* Try to figure out what kind of declaration is present.  */
8644   token1 = *cp_lexer_peek_token (parser->lexer);
8645
8646   if (token1.type != CPP_EOF)
8647     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8648   else
8649     {
8650       token2.type = CPP_EOF;
8651       token2.keyword = RID_MAX;
8652     }
8653
8654   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8655   p = obstack_alloc (&declarator_obstack, 0);
8656
8657   /* If the next token is `extern' and the following token is a string
8658      literal, then we have a linkage specification.  */
8659   if (token1.keyword == RID_EXTERN
8660       && cp_parser_is_string_literal (&token2))
8661     cp_parser_linkage_specification (parser);
8662   /* If the next token is `template', then we have either a template
8663      declaration, an explicit instantiation, or an explicit
8664      specialization.  */
8665   else if (token1.keyword == RID_TEMPLATE)
8666     {
8667       /* `template <>' indicates a template specialization.  */
8668       if (token2.type == CPP_LESS
8669           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8670         cp_parser_explicit_specialization (parser);
8671       /* `template <' indicates a template declaration.  */
8672       else if (token2.type == CPP_LESS)
8673         cp_parser_template_declaration (parser, /*member_p=*/false);
8674       /* Anything else must be an explicit instantiation.  */
8675       else
8676         cp_parser_explicit_instantiation (parser);
8677     }
8678   /* If the next token is `export', then we have a template
8679      declaration.  */
8680   else if (token1.keyword == RID_EXPORT)
8681     cp_parser_template_declaration (parser, /*member_p=*/false);
8682   /* If the next token is `extern', 'static' or 'inline' and the one
8683      after that is `template', we have a GNU extended explicit
8684      instantiation directive.  */
8685   else if (cp_parser_allow_gnu_extensions_p (parser)
8686            && (token1.keyword == RID_EXTERN
8687                || token1.keyword == RID_STATIC
8688                || token1.keyword == RID_INLINE)
8689            && token2.keyword == RID_TEMPLATE)
8690     cp_parser_explicit_instantiation (parser);
8691   /* If the next token is `namespace', check for a named or unnamed
8692      namespace definition.  */
8693   else if (token1.keyword == RID_NAMESPACE
8694            && (/* A named namespace definition.  */
8695                (token2.type == CPP_NAME
8696                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8697                     != CPP_EQ))
8698                /* An unnamed namespace definition.  */
8699                || token2.type == CPP_OPEN_BRACE
8700                || token2.keyword == RID_ATTRIBUTE))
8701     cp_parser_namespace_definition (parser);
8702   /* An inline (associated) namespace definition.  */
8703   else if (token1.keyword == RID_INLINE
8704            && token2.keyword == RID_NAMESPACE)
8705     cp_parser_namespace_definition (parser);
8706   /* Objective-C++ declaration/definition.  */
8707   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8708     cp_parser_objc_declaration (parser);
8709   /* We must have either a block declaration or a function
8710      definition.  */
8711   else
8712     /* Try to parse a block-declaration, or a function-definition.  */
8713     cp_parser_block_declaration (parser, /*statement_p=*/false);
8714
8715   /* Free any declarators allocated.  */
8716   obstack_free (&declarator_obstack, p);
8717 }
8718
8719 /* Parse a block-declaration.
8720
8721    block-declaration:
8722      simple-declaration
8723      asm-definition
8724      namespace-alias-definition
8725      using-declaration
8726      using-directive
8727
8728    GNU Extension:
8729
8730    block-declaration:
8731      __extension__ block-declaration
8732
8733    C++0x Extension:
8734
8735    block-declaration:
8736      static_assert-declaration
8737
8738    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8739    part of a declaration-statement.  */
8740
8741 static void
8742 cp_parser_block_declaration (cp_parser *parser,
8743                              bool      statement_p)
8744 {
8745   cp_token *token1;
8746   int saved_pedantic;
8747
8748   /* Check for the `__extension__' keyword.  */
8749   if (cp_parser_extension_opt (parser, &saved_pedantic))
8750     {
8751       /* Parse the qualified declaration.  */
8752       cp_parser_block_declaration (parser, statement_p);
8753       /* Restore the PEDANTIC flag.  */
8754       pedantic = saved_pedantic;
8755
8756       return;
8757     }
8758
8759   /* Peek at the next token to figure out which kind of declaration is
8760      present.  */
8761   token1 = cp_lexer_peek_token (parser->lexer);
8762
8763   /* If the next keyword is `asm', we have an asm-definition.  */
8764   if (token1->keyword == RID_ASM)
8765     {
8766       if (statement_p)
8767         cp_parser_commit_to_tentative_parse (parser);
8768       cp_parser_asm_definition (parser);
8769     }
8770   /* If the next keyword is `namespace', we have a
8771      namespace-alias-definition.  */
8772   else if (token1->keyword == RID_NAMESPACE)
8773     cp_parser_namespace_alias_definition (parser);
8774   /* If the next keyword is `using', we have either a
8775      using-declaration or a using-directive.  */
8776   else if (token1->keyword == RID_USING)
8777     {
8778       cp_token *token2;
8779
8780       if (statement_p)
8781         cp_parser_commit_to_tentative_parse (parser);
8782       /* If the token after `using' is `namespace', then we have a
8783          using-directive.  */
8784       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8785       if (token2->keyword == RID_NAMESPACE)
8786         cp_parser_using_directive (parser);
8787       /* Otherwise, it's a using-declaration.  */
8788       else
8789         cp_parser_using_declaration (parser,
8790                                      /*access_declaration_p=*/false);
8791     }
8792   /* If the next keyword is `__label__' we have a misplaced label
8793      declaration.  */
8794   else if (token1->keyword == RID_LABEL)
8795     {
8796       cp_lexer_consume_token (parser->lexer);
8797       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8798       cp_parser_skip_to_end_of_statement (parser);
8799       /* If the next token is now a `;', consume it.  */
8800       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8801         cp_lexer_consume_token (parser->lexer);
8802     }
8803   /* If the next token is `static_assert' we have a static assertion.  */
8804   else if (token1->keyword == RID_STATIC_ASSERT)
8805     cp_parser_static_assert (parser, /*member_p=*/false);
8806   /* Anything else must be a simple-declaration.  */
8807   else
8808     cp_parser_simple_declaration (parser, !statement_p);
8809 }
8810
8811 /* Parse a simple-declaration.
8812
8813    simple-declaration:
8814      decl-specifier-seq [opt] init-declarator-list [opt] ;
8815
8816    init-declarator-list:
8817      init-declarator
8818      init-declarator-list , init-declarator
8819
8820    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8821    function-definition as a simple-declaration.  */
8822
8823 static void
8824 cp_parser_simple_declaration (cp_parser* parser,
8825                               bool function_definition_allowed_p)
8826 {
8827   cp_decl_specifier_seq decl_specifiers;
8828   int declares_class_or_enum;
8829   bool saw_declarator;
8830
8831   /* Defer access checks until we know what is being declared; the
8832      checks for names appearing in the decl-specifier-seq should be
8833      done as if we were in the scope of the thing being declared.  */
8834   push_deferring_access_checks (dk_deferred);
8835
8836   /* Parse the decl-specifier-seq.  We have to keep track of whether
8837      or not the decl-specifier-seq declares a named class or
8838      enumeration type, since that is the only case in which the
8839      init-declarator-list is allowed to be empty.
8840
8841      [dcl.dcl]
8842
8843      In a simple-declaration, the optional init-declarator-list can be
8844      omitted only when declaring a class or enumeration, that is when
8845      the decl-specifier-seq contains either a class-specifier, an
8846      elaborated-type-specifier, or an enum-specifier.  */
8847   cp_parser_decl_specifier_seq (parser,
8848                                 CP_PARSER_FLAGS_OPTIONAL,
8849                                 &decl_specifiers,
8850                                 &declares_class_or_enum);
8851   /* We no longer need to defer access checks.  */
8852   stop_deferring_access_checks ();
8853
8854   /* In a block scope, a valid declaration must always have a
8855      decl-specifier-seq.  By not trying to parse declarators, we can
8856      resolve the declaration/expression ambiguity more quickly.  */
8857   if (!function_definition_allowed_p
8858       && !decl_specifiers.any_specifiers_p)
8859     {
8860       cp_parser_error (parser, "expected declaration");
8861       goto done;
8862     }
8863
8864   /* If the next two tokens are both identifiers, the code is
8865      erroneous. The usual cause of this situation is code like:
8866
8867        T t;
8868
8869      where "T" should name a type -- but does not.  */
8870   if (!decl_specifiers.any_type_specifiers_p
8871       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8872     {
8873       /* If parsing tentatively, we should commit; we really are
8874          looking at a declaration.  */
8875       cp_parser_commit_to_tentative_parse (parser);
8876       /* Give up.  */
8877       goto done;
8878     }
8879
8880   /* If we have seen at least one decl-specifier, and the next token
8881      is not a parenthesis, then we must be looking at a declaration.
8882      (After "int (" we might be looking at a functional cast.)  */
8883   if (decl_specifiers.any_specifiers_p
8884       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8885       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8886       && !cp_parser_error_occurred (parser))
8887     cp_parser_commit_to_tentative_parse (parser);
8888
8889   /* Keep going until we hit the `;' at the end of the simple
8890      declaration.  */
8891   saw_declarator = false;
8892   while (cp_lexer_next_token_is_not (parser->lexer,
8893                                      CPP_SEMICOLON))
8894     {
8895       cp_token *token;
8896       bool function_definition_p;
8897       tree decl;
8898
8899       if (saw_declarator)
8900         {
8901           /* If we are processing next declarator, coma is expected */
8902           token = cp_lexer_peek_token (parser->lexer);
8903           gcc_assert (token->type == CPP_COMMA);
8904           cp_lexer_consume_token (parser->lexer);
8905         }
8906       else
8907         saw_declarator = true;
8908
8909       /* Parse the init-declarator.  */
8910       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8911                                         /*checks=*/NULL,
8912                                         function_definition_allowed_p,
8913                                         /*member_p=*/false,
8914                                         declares_class_or_enum,
8915                                         &function_definition_p);
8916       /* If an error occurred while parsing tentatively, exit quickly.
8917          (That usually happens when in the body of a function; each
8918          statement is treated as a declaration-statement until proven
8919          otherwise.)  */
8920       if (cp_parser_error_occurred (parser))
8921         goto done;
8922       /* Handle function definitions specially.  */
8923       if (function_definition_p)
8924         {
8925           /* If the next token is a `,', then we are probably
8926              processing something like:
8927
8928                void f() {}, *p;
8929
8930              which is erroneous.  */
8931           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8932             {
8933               cp_token *token = cp_lexer_peek_token (parser->lexer);
8934               error_at (token->location,
8935                         "mixing"
8936                         " declarations and function-definitions is forbidden");
8937             }
8938           /* Otherwise, we're done with the list of declarators.  */
8939           else
8940             {
8941               pop_deferring_access_checks ();
8942               return;
8943             }
8944         }
8945       /* The next token should be either a `,' or a `;'.  */
8946       token = cp_lexer_peek_token (parser->lexer);
8947       /* If it's a `,', there are more declarators to come.  */
8948       if (token->type == CPP_COMMA)
8949         /* will be consumed next time around */;
8950       /* If it's a `;', we are done.  */
8951       else if (token->type == CPP_SEMICOLON)
8952         break;
8953       /* Anything else is an error.  */
8954       else
8955         {
8956           /* If we have already issued an error message we don't need
8957              to issue another one.  */
8958           if (decl != error_mark_node
8959               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8960             cp_parser_error (parser, "expected %<,%> or %<;%>");
8961           /* Skip tokens until we reach the end of the statement.  */
8962           cp_parser_skip_to_end_of_statement (parser);
8963           /* If the next token is now a `;', consume it.  */
8964           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8965             cp_lexer_consume_token (parser->lexer);
8966           goto done;
8967         }
8968       /* After the first time around, a function-definition is not
8969          allowed -- even if it was OK at first.  For example:
8970
8971            int i, f() {}
8972
8973          is not valid.  */
8974       function_definition_allowed_p = false;
8975     }
8976
8977   /* Issue an error message if no declarators are present, and the
8978      decl-specifier-seq does not itself declare a class or
8979      enumeration.  */
8980   if (!saw_declarator)
8981     {
8982       if (cp_parser_declares_only_class_p (parser))
8983         shadow_tag (&decl_specifiers);
8984       /* Perform any deferred access checks.  */
8985       perform_deferred_access_checks ();
8986     }
8987
8988   /* Consume the `;'.  */
8989   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8990
8991  done:
8992   pop_deferring_access_checks ();
8993 }
8994
8995 /* Parse a decl-specifier-seq.
8996
8997    decl-specifier-seq:
8998      decl-specifier-seq [opt] decl-specifier
8999
9000    decl-specifier:
9001      storage-class-specifier
9002      type-specifier
9003      function-specifier
9004      friend
9005      typedef
9006
9007    GNU Extension:
9008
9009    decl-specifier:
9010      attributes
9011
9012    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9013
9014    The parser flags FLAGS is used to control type-specifier parsing.
9015
9016    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9017    flags:
9018
9019      1: one of the decl-specifiers is an elaborated-type-specifier
9020         (i.e., a type declaration)
9021      2: one of the decl-specifiers is an enum-specifier or a
9022         class-specifier (i.e., a type definition)
9023
9024    */
9025
9026 static void
9027 cp_parser_decl_specifier_seq (cp_parser* parser,
9028                               cp_parser_flags flags,
9029                               cp_decl_specifier_seq *decl_specs,
9030                               int* declares_class_or_enum)
9031 {
9032   bool constructor_possible_p = !parser->in_declarator_p;
9033   cp_token *start_token = NULL;
9034
9035   /* Clear DECL_SPECS.  */
9036   clear_decl_specs (decl_specs);
9037
9038   /* Assume no class or enumeration type is declared.  */
9039   *declares_class_or_enum = 0;
9040
9041   /* Keep reading specifiers until there are no more to read.  */
9042   while (true)
9043     {
9044       bool constructor_p;
9045       bool found_decl_spec;
9046       cp_token *token;
9047
9048       /* Peek at the next token.  */
9049       token = cp_lexer_peek_token (parser->lexer);
9050
9051       /* Save the first token of the decl spec list for error
9052          reporting.  */
9053       if (!start_token)
9054         start_token = token;
9055       /* Handle attributes.  */
9056       if (token->keyword == RID_ATTRIBUTE)
9057         {
9058           /* Parse the attributes.  */
9059           decl_specs->attributes
9060             = chainon (decl_specs->attributes,
9061                        cp_parser_attributes_opt (parser));
9062           continue;
9063         }
9064       /* Assume we will find a decl-specifier keyword.  */
9065       found_decl_spec = true;
9066       /* If the next token is an appropriate keyword, we can simply
9067          add it to the list.  */
9068       switch (token->keyword)
9069         {
9070           /* decl-specifier:
9071                friend
9072                constexpr */
9073         case RID_FRIEND:
9074           if (!at_class_scope_p ())
9075             {
9076               error_at (token->location, "%<friend%> used outside of class");
9077               cp_lexer_purge_token (parser->lexer);
9078             }
9079           else
9080             {
9081               ++decl_specs->specs[(int) ds_friend];
9082               /* Consume the token.  */
9083               cp_lexer_consume_token (parser->lexer);
9084             }
9085           break;
9086
9087         case RID_CONSTEXPR:
9088           ++decl_specs->specs[(int) ds_constexpr];
9089           cp_lexer_consume_token (parser->lexer);
9090           break;
9091
9092           /* function-specifier:
9093                inline
9094                virtual
9095                explicit  */
9096         case RID_INLINE:
9097         case RID_VIRTUAL:
9098         case RID_EXPLICIT:
9099           cp_parser_function_specifier_opt (parser, decl_specs);
9100           break;
9101
9102           /* decl-specifier:
9103                typedef  */
9104         case RID_TYPEDEF:
9105           ++decl_specs->specs[(int) ds_typedef];
9106           /* Consume the token.  */
9107           cp_lexer_consume_token (parser->lexer);
9108           /* A constructor declarator cannot appear in a typedef.  */
9109           constructor_possible_p = false;
9110           /* The "typedef" keyword can only occur in a declaration; we
9111              may as well commit at this point.  */
9112           cp_parser_commit_to_tentative_parse (parser);
9113
9114           if (decl_specs->storage_class != sc_none)
9115             decl_specs->conflicting_specifiers_p = true;
9116           break;
9117
9118           /* storage-class-specifier:
9119                auto
9120                register
9121                static
9122                extern
9123                mutable
9124
9125              GNU Extension:
9126                thread  */
9127         case RID_AUTO:
9128           if (cxx_dialect == cxx98) 
9129             {
9130               /* Consume the token.  */
9131               cp_lexer_consume_token (parser->lexer);
9132
9133               /* Complain about `auto' as a storage specifier, if
9134                  we're complaining about C++0x compatibility.  */
9135               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9136                           " will change meaning in C++0x; please remove it");
9137
9138               /* Set the storage class anyway.  */
9139               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9140                                            token->location);
9141             }
9142           else
9143             /* C++0x auto type-specifier.  */
9144             found_decl_spec = false;
9145           break;
9146
9147         case RID_REGISTER:
9148         case RID_STATIC:
9149         case RID_EXTERN:
9150         case RID_MUTABLE:
9151           /* Consume the token.  */
9152           cp_lexer_consume_token (parser->lexer);
9153           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9154                                        token->location);
9155           break;
9156         case RID_THREAD:
9157           /* Consume the token.  */
9158           cp_lexer_consume_token (parser->lexer);
9159           ++decl_specs->specs[(int) ds_thread];
9160           break;
9161
9162         default:
9163           /* We did not yet find a decl-specifier yet.  */
9164           found_decl_spec = false;
9165           break;
9166         }
9167
9168       /* Constructors are a special case.  The `S' in `S()' is not a
9169          decl-specifier; it is the beginning of the declarator.  */
9170       constructor_p
9171         = (!found_decl_spec
9172            && constructor_possible_p
9173            && (cp_parser_constructor_declarator_p
9174                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9175
9176       /* If we don't have a DECL_SPEC yet, then we must be looking at
9177          a type-specifier.  */
9178       if (!found_decl_spec && !constructor_p)
9179         {
9180           int decl_spec_declares_class_or_enum;
9181           bool is_cv_qualifier;
9182           tree type_spec;
9183
9184           type_spec
9185             = cp_parser_type_specifier (parser, flags,
9186                                         decl_specs,
9187                                         /*is_declaration=*/true,
9188                                         &decl_spec_declares_class_or_enum,
9189                                         &is_cv_qualifier);
9190           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9191
9192           /* If this type-specifier referenced a user-defined type
9193              (a typedef, class-name, etc.), then we can't allow any
9194              more such type-specifiers henceforth.
9195
9196              [dcl.spec]
9197
9198              The longest sequence of decl-specifiers that could
9199              possibly be a type name is taken as the
9200              decl-specifier-seq of a declaration.  The sequence shall
9201              be self-consistent as described below.
9202
9203              [dcl.type]
9204
9205              As a general rule, at most one type-specifier is allowed
9206              in the complete decl-specifier-seq of a declaration.  The
9207              only exceptions are the following:
9208
9209              -- const or volatile can be combined with any other
9210                 type-specifier.
9211
9212              -- signed or unsigned can be combined with char, long,
9213                 short, or int.
9214
9215              -- ..
9216
9217              Example:
9218
9219                typedef char* Pc;
9220                void g (const int Pc);
9221
9222              Here, Pc is *not* part of the decl-specifier seq; it's
9223              the declarator.  Therefore, once we see a type-specifier
9224              (other than a cv-qualifier), we forbid any additional
9225              user-defined types.  We *do* still allow things like `int
9226              int' to be considered a decl-specifier-seq, and issue the
9227              error message later.  */
9228           if (type_spec && !is_cv_qualifier)
9229             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9230           /* A constructor declarator cannot follow a type-specifier.  */
9231           if (type_spec)
9232             {
9233               constructor_possible_p = false;
9234               found_decl_spec = true;
9235               if (!is_cv_qualifier)
9236                 decl_specs->any_type_specifiers_p = true;
9237             }
9238         }
9239
9240       /* If we still do not have a DECL_SPEC, then there are no more
9241          decl-specifiers.  */
9242       if (!found_decl_spec)
9243         break;
9244
9245       decl_specs->any_specifiers_p = true;
9246       /* After we see one decl-specifier, further decl-specifiers are
9247          always optional.  */
9248       flags |= CP_PARSER_FLAGS_OPTIONAL;
9249     }
9250
9251   cp_parser_check_decl_spec (decl_specs, start_token->location);
9252
9253   /* Don't allow a friend specifier with a class definition.  */
9254   if (decl_specs->specs[(int) ds_friend] != 0
9255       && (*declares_class_or_enum & 2))
9256     error_at (start_token->location,
9257               "class definition may not be declared a friend");
9258 }
9259
9260 /* Parse an (optional) storage-class-specifier.
9261
9262    storage-class-specifier:
9263      auto
9264      register
9265      static
9266      extern
9267      mutable
9268
9269    GNU Extension:
9270
9271    storage-class-specifier:
9272      thread
9273
9274    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9275
9276 static tree
9277 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9278 {
9279   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9280     {
9281     case RID_AUTO:
9282       if (cxx_dialect != cxx98)
9283         return NULL_TREE;
9284       /* Fall through for C++98.  */
9285
9286     case RID_REGISTER:
9287     case RID_STATIC:
9288     case RID_EXTERN:
9289     case RID_MUTABLE:
9290     case RID_THREAD:
9291       /* Consume the token.  */
9292       return cp_lexer_consume_token (parser->lexer)->u.value;
9293
9294     default:
9295       return NULL_TREE;
9296     }
9297 }
9298
9299 /* Parse an (optional) function-specifier.
9300
9301    function-specifier:
9302      inline
9303      virtual
9304      explicit
9305
9306    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9307    Updates DECL_SPECS, if it is non-NULL.  */
9308
9309 static tree
9310 cp_parser_function_specifier_opt (cp_parser* parser,
9311                                   cp_decl_specifier_seq *decl_specs)
9312 {
9313   cp_token *token = cp_lexer_peek_token (parser->lexer);
9314   switch (token->keyword)
9315     {
9316     case RID_INLINE:
9317       if (decl_specs)
9318         ++decl_specs->specs[(int) ds_inline];
9319       break;
9320
9321     case RID_VIRTUAL:
9322       /* 14.5.2.3 [temp.mem]
9323
9324          A member function template shall not be virtual.  */
9325       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9326         error_at (token->location, "templates may not be %<virtual%>");
9327       else if (decl_specs)
9328         ++decl_specs->specs[(int) ds_virtual];
9329       break;
9330
9331     case RID_EXPLICIT:
9332       if (decl_specs)
9333         ++decl_specs->specs[(int) ds_explicit];
9334       break;
9335
9336     default:
9337       return NULL_TREE;
9338     }
9339
9340   /* Consume the token.  */
9341   return cp_lexer_consume_token (parser->lexer)->u.value;
9342 }
9343
9344 /* Parse a linkage-specification.
9345
9346    linkage-specification:
9347      extern string-literal { declaration-seq [opt] }
9348      extern string-literal declaration  */
9349
9350 static void
9351 cp_parser_linkage_specification (cp_parser* parser)
9352 {
9353   tree linkage;
9354
9355   /* Look for the `extern' keyword.  */
9356   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9357
9358   /* Look for the string-literal.  */
9359   linkage = cp_parser_string_literal (parser, false, false);
9360
9361   /* Transform the literal into an identifier.  If the literal is a
9362      wide-character string, or contains embedded NULs, then we can't
9363      handle it as the user wants.  */
9364   if (strlen (TREE_STRING_POINTER (linkage))
9365       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9366     {
9367       cp_parser_error (parser, "invalid linkage-specification");
9368       /* Assume C++ linkage.  */
9369       linkage = lang_name_cplusplus;
9370     }
9371   else
9372     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9373
9374   /* We're now using the new linkage.  */
9375   push_lang_context (linkage);
9376
9377   /* If the next token is a `{', then we're using the first
9378      production.  */
9379   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9380     {
9381       /* Consume the `{' token.  */
9382       cp_lexer_consume_token (parser->lexer);
9383       /* Parse the declarations.  */
9384       cp_parser_declaration_seq_opt (parser);
9385       /* Look for the closing `}'.  */
9386       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9387     }
9388   /* Otherwise, there's just one declaration.  */
9389   else
9390     {
9391       bool saved_in_unbraced_linkage_specification_p;
9392
9393       saved_in_unbraced_linkage_specification_p
9394         = parser->in_unbraced_linkage_specification_p;
9395       parser->in_unbraced_linkage_specification_p = true;
9396       cp_parser_declaration (parser);
9397       parser->in_unbraced_linkage_specification_p
9398         = saved_in_unbraced_linkage_specification_p;
9399     }
9400
9401   /* We're done with the linkage-specification.  */
9402   pop_lang_context ();
9403 }
9404
9405 /* Parse a static_assert-declaration.
9406
9407    static_assert-declaration:
9408      static_assert ( constant-expression , string-literal ) ; 
9409
9410    If MEMBER_P, this static_assert is a class member.  */
9411
9412 static void 
9413 cp_parser_static_assert(cp_parser *parser, bool member_p)
9414 {
9415   tree condition;
9416   tree message;
9417   cp_token *token;
9418   location_t saved_loc;
9419
9420   /* Peek at the `static_assert' token so we can keep track of exactly
9421      where the static assertion started.  */
9422   token = cp_lexer_peek_token (parser->lexer);
9423   saved_loc = token->location;
9424
9425   /* Look for the `static_assert' keyword.  */
9426   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9427                                   "%<static_assert%>"))
9428     return;
9429
9430   /*  We know we are in a static assertion; commit to any tentative
9431       parse.  */
9432   if (cp_parser_parsing_tentatively (parser))
9433     cp_parser_commit_to_tentative_parse (parser);
9434
9435   /* Parse the `(' starting the static assertion condition.  */
9436   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9437
9438   /* Parse the constant-expression.  */
9439   condition = 
9440     cp_parser_constant_expression (parser,
9441                                    /*allow_non_constant_p=*/false,
9442                                    /*non_constant_p=*/NULL);
9443
9444   /* Parse the separating `,'.  */
9445   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9446
9447   /* Parse the string-literal message.  */
9448   message = cp_parser_string_literal (parser, 
9449                                       /*translate=*/false,
9450                                       /*wide_ok=*/true);
9451
9452   /* A `)' completes the static assertion.  */
9453   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9454     cp_parser_skip_to_closing_parenthesis (parser, 
9455                                            /*recovering=*/true, 
9456                                            /*or_comma=*/false,
9457                                            /*consume_paren=*/true);
9458
9459   /* A semicolon terminates the declaration.  */
9460   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9461
9462   /* Complete the static assertion, which may mean either processing 
9463      the static assert now or saving it for template instantiation.  */
9464   finish_static_assert (condition, message, saved_loc, member_p);
9465 }
9466
9467 /* Parse a `decltype' type. Returns the type. 
9468
9469    simple-type-specifier:
9470      decltype ( expression )  */
9471
9472 static tree
9473 cp_parser_decltype (cp_parser *parser)
9474 {
9475   tree expr;
9476   bool id_expression_or_member_access_p = false;
9477   const char *saved_message;
9478   bool saved_integral_constant_expression_p;
9479   bool saved_non_integral_constant_expression_p;
9480   cp_token *id_expr_start_token;
9481
9482   /* Look for the `decltype' token.  */
9483   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9484     return error_mark_node;
9485
9486   /* Types cannot be defined in a `decltype' expression.  Save away the
9487      old message.  */
9488   saved_message = parser->type_definition_forbidden_message;
9489
9490   /* And create the new one.  */
9491   parser->type_definition_forbidden_message
9492     = "types may not be defined in %<decltype%> expressions";
9493
9494   /* The restrictions on constant-expressions do not apply inside
9495      decltype expressions.  */
9496   saved_integral_constant_expression_p
9497     = parser->integral_constant_expression_p;
9498   saved_non_integral_constant_expression_p
9499     = parser->non_integral_constant_expression_p;
9500   parser->integral_constant_expression_p = false;
9501
9502   /* Do not actually evaluate the expression.  */
9503   ++cp_unevaluated_operand;
9504
9505   /* Do not warn about problems with the expression.  */
9506   ++c_inhibit_evaluation_warnings;
9507
9508   /* Parse the opening `('.  */
9509   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9510     return error_mark_node;
9511   
9512   /* First, try parsing an id-expression.  */
9513   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9514   cp_parser_parse_tentatively (parser);
9515   expr = cp_parser_id_expression (parser,
9516                                   /*template_keyword_p=*/false,
9517                                   /*check_dependency_p=*/true,
9518                                   /*template_p=*/NULL,
9519                                   /*declarator_p=*/false,
9520                                   /*optional_p=*/false);
9521
9522   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9523     {
9524       bool non_integral_constant_expression_p = false;
9525       tree id_expression = expr;
9526       cp_id_kind idk;
9527       const char *error_msg;
9528
9529       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9530         /* Lookup the name we got back from the id-expression.  */
9531         expr = cp_parser_lookup_name (parser, expr,
9532                                       none_type,
9533                                       /*is_template=*/false,
9534                                       /*is_namespace=*/false,
9535                                       /*check_dependency=*/true,
9536                                       /*ambiguous_decls=*/NULL,
9537                                       id_expr_start_token->location);
9538
9539       if (expr
9540           && expr != error_mark_node
9541           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9542           && TREE_CODE (expr) != TYPE_DECL
9543           && (TREE_CODE (expr) != BIT_NOT_EXPR
9544               || !TYPE_P (TREE_OPERAND (expr, 0)))
9545           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9546         {
9547           /* Complete lookup of the id-expression.  */
9548           expr = (finish_id_expression
9549                   (id_expression, expr, parser->scope, &idk,
9550                    /*integral_constant_expression_p=*/false,
9551                    /*allow_non_integral_constant_expression_p=*/true,
9552                    &non_integral_constant_expression_p,
9553                    /*template_p=*/false,
9554                    /*done=*/true,
9555                    /*address_p=*/false,
9556                    /*template_arg_p=*/false,
9557                    &error_msg,
9558                    id_expr_start_token->location));
9559
9560           if (expr == error_mark_node)
9561             /* We found an id-expression, but it was something that we
9562                should not have found. This is an error, not something
9563                we can recover from, so note that we found an
9564                id-expression and we'll recover as gracefully as
9565                possible.  */
9566             id_expression_or_member_access_p = true;
9567         }
9568
9569       if (expr 
9570           && expr != error_mark_node
9571           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9572         /* We have an id-expression.  */
9573         id_expression_or_member_access_p = true;
9574     }
9575
9576   if (!id_expression_or_member_access_p)
9577     {
9578       /* Abort the id-expression parse.  */
9579       cp_parser_abort_tentative_parse (parser);
9580
9581       /* Parsing tentatively, again.  */
9582       cp_parser_parse_tentatively (parser);
9583
9584       /* Parse a class member access.  */
9585       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9586                                            /*cast_p=*/false,
9587                                            /*member_access_only_p=*/true, NULL);
9588
9589       if (expr 
9590           && expr != error_mark_node
9591           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9592         /* We have an id-expression.  */
9593         id_expression_or_member_access_p = true;
9594     }
9595
9596   if (id_expression_or_member_access_p)
9597     /* We have parsed the complete id-expression or member access.  */
9598     cp_parser_parse_definitely (parser);
9599   else
9600     {
9601       bool saved_greater_than_is_operator_p;
9602
9603       /* Abort our attempt to parse an id-expression or member access
9604          expression.  */
9605       cp_parser_abort_tentative_parse (parser);
9606
9607       /* Within a parenthesized expression, a `>' token is always
9608          the greater-than operator.  */
9609       saved_greater_than_is_operator_p
9610         = parser->greater_than_is_operator_p;
9611       parser->greater_than_is_operator_p = true;
9612
9613       /* Parse a full expression.  */
9614       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9615
9616       /* The `>' token might be the end of a template-id or
9617          template-parameter-list now.  */
9618       parser->greater_than_is_operator_p
9619         = saved_greater_than_is_operator_p;
9620     }
9621
9622   /* Go back to evaluating expressions.  */
9623   --cp_unevaluated_operand;
9624   --c_inhibit_evaluation_warnings;
9625
9626   /* Restore the old message and the integral constant expression
9627      flags.  */
9628   parser->type_definition_forbidden_message = saved_message;
9629   parser->integral_constant_expression_p
9630     = saved_integral_constant_expression_p;
9631   parser->non_integral_constant_expression_p
9632     = saved_non_integral_constant_expression_p;
9633
9634   if (expr == error_mark_node)
9635     {
9636       /* Skip everything up to the closing `)'.  */
9637       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9638                                              /*consume_paren=*/true);
9639       return error_mark_node;
9640     }
9641   
9642   /* Parse to the closing `)'.  */
9643   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9644     {
9645       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9646                                              /*consume_paren=*/true);
9647       return error_mark_node;
9648     }
9649
9650   return finish_decltype_type (expr, id_expression_or_member_access_p);
9651 }
9652
9653 /* Special member functions [gram.special] */
9654
9655 /* Parse a conversion-function-id.
9656
9657    conversion-function-id:
9658      operator conversion-type-id
9659
9660    Returns an IDENTIFIER_NODE representing the operator.  */
9661
9662 static tree
9663 cp_parser_conversion_function_id (cp_parser* parser)
9664 {
9665   tree type;
9666   tree saved_scope;
9667   tree saved_qualifying_scope;
9668   tree saved_object_scope;
9669   tree pushed_scope = NULL_TREE;
9670
9671   /* Look for the `operator' token.  */
9672   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9673     return error_mark_node;
9674   /* When we parse the conversion-type-id, the current scope will be
9675      reset.  However, we need that information in able to look up the
9676      conversion function later, so we save it here.  */
9677   saved_scope = parser->scope;
9678   saved_qualifying_scope = parser->qualifying_scope;
9679   saved_object_scope = parser->object_scope;
9680   /* We must enter the scope of the class so that the names of
9681      entities declared within the class are available in the
9682      conversion-type-id.  For example, consider:
9683
9684        struct S {
9685          typedef int I;
9686          operator I();
9687        };
9688
9689        S::operator I() { ... }
9690
9691      In order to see that `I' is a type-name in the definition, we
9692      must be in the scope of `S'.  */
9693   if (saved_scope)
9694     pushed_scope = push_scope (saved_scope);
9695   /* Parse the conversion-type-id.  */
9696   type = cp_parser_conversion_type_id (parser);
9697   /* Leave the scope of the class, if any.  */
9698   if (pushed_scope)
9699     pop_scope (pushed_scope);
9700   /* Restore the saved scope.  */
9701   parser->scope = saved_scope;
9702   parser->qualifying_scope = saved_qualifying_scope;
9703   parser->object_scope = saved_object_scope;
9704   /* If the TYPE is invalid, indicate failure.  */
9705   if (type == error_mark_node)
9706     return error_mark_node;
9707   return mangle_conv_op_name_for_type (type);
9708 }
9709
9710 /* Parse a conversion-type-id:
9711
9712    conversion-type-id:
9713      type-specifier-seq conversion-declarator [opt]
9714
9715    Returns the TYPE specified.  */
9716
9717 static tree
9718 cp_parser_conversion_type_id (cp_parser* parser)
9719 {
9720   tree attributes;
9721   cp_decl_specifier_seq type_specifiers;
9722   cp_declarator *declarator;
9723   tree type_specified;
9724
9725   /* Parse the attributes.  */
9726   attributes = cp_parser_attributes_opt (parser);
9727   /* Parse the type-specifiers.  */
9728   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9729                                 /*is_trailing_return=*/false,
9730                                 &type_specifiers);
9731   /* If that didn't work, stop.  */
9732   if (type_specifiers.type == error_mark_node)
9733     return error_mark_node;
9734   /* Parse the conversion-declarator.  */
9735   declarator = cp_parser_conversion_declarator_opt (parser);
9736
9737   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9738                                     /*initialized=*/0, &attributes);
9739   if (attributes)
9740     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9741
9742   /* Don't give this error when parsing tentatively.  This happens to
9743      work because we always parse this definitively once.  */
9744   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9745       && type_uses_auto (type_specified))
9746     {
9747       error ("invalid use of %<auto%> in conversion operator");
9748       return error_mark_node;
9749     }
9750
9751   return type_specified;
9752 }
9753
9754 /* Parse an (optional) conversion-declarator.
9755
9756    conversion-declarator:
9757      ptr-operator conversion-declarator [opt]
9758
9759    */
9760
9761 static cp_declarator *
9762 cp_parser_conversion_declarator_opt (cp_parser* parser)
9763 {
9764   enum tree_code code;
9765   tree class_type;
9766   cp_cv_quals cv_quals;
9767
9768   /* We don't know if there's a ptr-operator next, or not.  */
9769   cp_parser_parse_tentatively (parser);
9770   /* Try the ptr-operator.  */
9771   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9772   /* If it worked, look for more conversion-declarators.  */
9773   if (cp_parser_parse_definitely (parser))
9774     {
9775       cp_declarator *declarator;
9776
9777       /* Parse another optional declarator.  */
9778       declarator = cp_parser_conversion_declarator_opt (parser);
9779
9780       return cp_parser_make_indirect_declarator
9781         (code, class_type, cv_quals, declarator);
9782    }
9783
9784   return NULL;
9785 }
9786
9787 /* Parse an (optional) ctor-initializer.
9788
9789    ctor-initializer:
9790      : mem-initializer-list
9791
9792    Returns TRUE iff the ctor-initializer was actually present.  */
9793
9794 static bool
9795 cp_parser_ctor_initializer_opt (cp_parser* parser)
9796 {
9797   /* If the next token is not a `:', then there is no
9798      ctor-initializer.  */
9799   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9800     {
9801       /* Do default initialization of any bases and members.  */
9802       if (DECL_CONSTRUCTOR_P (current_function_decl))
9803         finish_mem_initializers (NULL_TREE);
9804
9805       return false;
9806     }
9807
9808   /* Consume the `:' token.  */
9809   cp_lexer_consume_token (parser->lexer);
9810   /* And the mem-initializer-list.  */
9811   cp_parser_mem_initializer_list (parser);
9812
9813   return true;
9814 }
9815
9816 /* Parse a mem-initializer-list.
9817
9818    mem-initializer-list:
9819      mem-initializer ... [opt]
9820      mem-initializer ... [opt] , mem-initializer-list  */
9821
9822 static void
9823 cp_parser_mem_initializer_list (cp_parser* parser)
9824 {
9825   tree mem_initializer_list = NULL_TREE;
9826   cp_token *token = cp_lexer_peek_token (parser->lexer);
9827
9828   /* Let the semantic analysis code know that we are starting the
9829      mem-initializer-list.  */
9830   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9831     error_at (token->location,
9832               "only constructors take base initializers");
9833
9834   /* Loop through the list.  */
9835   while (true)
9836     {
9837       tree mem_initializer;
9838
9839       token = cp_lexer_peek_token (parser->lexer);
9840       /* Parse the mem-initializer.  */
9841       mem_initializer = cp_parser_mem_initializer (parser);
9842       /* If the next token is a `...', we're expanding member initializers. */
9843       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9844         {
9845           /* Consume the `...'. */
9846           cp_lexer_consume_token (parser->lexer);
9847
9848           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9849              can be expanded but members cannot. */
9850           if (mem_initializer != error_mark_node
9851               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9852             {
9853               error_at (token->location,
9854                         "cannot expand initializer for member %<%D%>",
9855                         TREE_PURPOSE (mem_initializer));
9856               mem_initializer = error_mark_node;
9857             }
9858
9859           /* Construct the pack expansion type. */
9860           if (mem_initializer != error_mark_node)
9861             mem_initializer = make_pack_expansion (mem_initializer);
9862         }
9863       /* Add it to the list, unless it was erroneous.  */
9864       if (mem_initializer != error_mark_node)
9865         {
9866           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9867           mem_initializer_list = mem_initializer;
9868         }
9869       /* If the next token is not a `,', we're done.  */
9870       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9871         break;
9872       /* Consume the `,' token.  */
9873       cp_lexer_consume_token (parser->lexer);
9874     }
9875
9876   /* Perform semantic analysis.  */
9877   if (DECL_CONSTRUCTOR_P (current_function_decl))
9878     finish_mem_initializers (mem_initializer_list);
9879 }
9880
9881 /* Parse a mem-initializer.
9882
9883    mem-initializer:
9884      mem-initializer-id ( expression-list [opt] )
9885      mem-initializer-id braced-init-list
9886
9887    GNU extension:
9888
9889    mem-initializer:
9890      ( expression-list [opt] )
9891
9892    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9893    class) or FIELD_DECL (for a non-static data member) to initialize;
9894    the TREE_VALUE is the expression-list.  An empty initialization
9895    list is represented by void_list_node.  */
9896
9897 static tree
9898 cp_parser_mem_initializer (cp_parser* parser)
9899 {
9900   tree mem_initializer_id;
9901   tree expression_list;
9902   tree member;
9903   cp_token *token = cp_lexer_peek_token (parser->lexer);
9904
9905   /* Find out what is being initialized.  */
9906   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9907     {
9908       permerror (token->location,
9909                  "anachronistic old-style base class initializer");
9910       mem_initializer_id = NULL_TREE;
9911     }
9912   else
9913     {
9914       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9915       if (mem_initializer_id == error_mark_node)
9916         return mem_initializer_id;
9917     }
9918   member = expand_member_init (mem_initializer_id);
9919   if (member && !DECL_P (member))
9920     in_base_initializer = 1;
9921
9922   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9923     {
9924       bool expr_non_constant_p;
9925       maybe_warn_cpp0x ("extended initializer lists");
9926       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9927       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9928       expression_list = build_tree_list (NULL_TREE, expression_list);
9929     }
9930   else
9931     {
9932       VEC(tree,gc)* vec;
9933       vec = cp_parser_parenthesized_expression_list (parser, false,
9934                                                      /*cast_p=*/false,
9935                                                      /*allow_expansion_p=*/true,
9936                                                      /*non_constant_p=*/NULL);
9937       if (vec == NULL)
9938         return error_mark_node;
9939       expression_list = build_tree_list_vec (vec);
9940       release_tree_vector (vec);
9941     }
9942
9943   if (expression_list == error_mark_node)
9944     return error_mark_node;
9945   if (!expression_list)
9946     expression_list = void_type_node;
9947
9948   in_base_initializer = 0;
9949
9950   return member ? build_tree_list (member, expression_list) : error_mark_node;
9951 }
9952
9953 /* Parse a mem-initializer-id.
9954
9955    mem-initializer-id:
9956      :: [opt] nested-name-specifier [opt] class-name
9957      identifier
9958
9959    Returns a TYPE indicating the class to be initializer for the first
9960    production.  Returns an IDENTIFIER_NODE indicating the data member
9961    to be initialized for the second production.  */
9962
9963 static tree
9964 cp_parser_mem_initializer_id (cp_parser* parser)
9965 {
9966   bool global_scope_p;
9967   bool nested_name_specifier_p;
9968   bool template_p = false;
9969   tree id;
9970
9971   cp_token *token = cp_lexer_peek_token (parser->lexer);
9972
9973   /* `typename' is not allowed in this context ([temp.res]).  */
9974   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9975     {
9976       error_at (token->location, 
9977                 "keyword %<typename%> not allowed in this context (a qualified "
9978                 "member initializer is implicitly a type)");
9979       cp_lexer_consume_token (parser->lexer);
9980     }
9981   /* Look for the optional `::' operator.  */
9982   global_scope_p
9983     = (cp_parser_global_scope_opt (parser,
9984                                    /*current_scope_valid_p=*/false)
9985        != NULL_TREE);
9986   /* Look for the optional nested-name-specifier.  The simplest way to
9987      implement:
9988
9989        [temp.res]
9990
9991        The keyword `typename' is not permitted in a base-specifier or
9992        mem-initializer; in these contexts a qualified name that
9993        depends on a template-parameter is implicitly assumed to be a
9994        type name.
9995
9996      is to assume that we have seen the `typename' keyword at this
9997      point.  */
9998   nested_name_specifier_p
9999     = (cp_parser_nested_name_specifier_opt (parser,
10000                                             /*typename_keyword_p=*/true,
10001                                             /*check_dependency_p=*/true,
10002                                             /*type_p=*/true,
10003                                             /*is_declaration=*/true)
10004        != NULL_TREE);
10005   if (nested_name_specifier_p)
10006     template_p = cp_parser_optional_template_keyword (parser);
10007   /* If there is a `::' operator or a nested-name-specifier, then we
10008      are definitely looking for a class-name.  */
10009   if (global_scope_p || nested_name_specifier_p)
10010     return cp_parser_class_name (parser,
10011                                  /*typename_keyword_p=*/true,
10012                                  /*template_keyword_p=*/template_p,
10013                                  none_type,
10014                                  /*check_dependency_p=*/true,
10015                                  /*class_head_p=*/false,
10016                                  /*is_declaration=*/true);
10017   /* Otherwise, we could also be looking for an ordinary identifier.  */
10018   cp_parser_parse_tentatively (parser);
10019   /* Try a class-name.  */
10020   id = cp_parser_class_name (parser,
10021                              /*typename_keyword_p=*/true,
10022                              /*template_keyword_p=*/false,
10023                              none_type,
10024                              /*check_dependency_p=*/true,
10025                              /*class_head_p=*/false,
10026                              /*is_declaration=*/true);
10027   /* If we found one, we're done.  */
10028   if (cp_parser_parse_definitely (parser))
10029     return id;
10030   /* Otherwise, look for an ordinary identifier.  */
10031   return cp_parser_identifier (parser);
10032 }
10033
10034 /* Overloading [gram.over] */
10035
10036 /* Parse an operator-function-id.
10037
10038    operator-function-id:
10039      operator operator
10040
10041    Returns an IDENTIFIER_NODE for the operator which is a
10042    human-readable spelling of the identifier, e.g., `operator +'.  */
10043
10044 static tree
10045 cp_parser_operator_function_id (cp_parser* parser)
10046 {
10047   /* Look for the `operator' keyword.  */
10048   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10049     return error_mark_node;
10050   /* And then the name of the operator itself.  */
10051   return cp_parser_operator (parser);
10052 }
10053
10054 /* Parse an operator.
10055
10056    operator:
10057      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10058      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10059      || ++ -- , ->* -> () []
10060
10061    GNU Extensions:
10062
10063    operator:
10064      <? >? <?= >?=
10065
10066    Returns an IDENTIFIER_NODE for the operator which is a
10067    human-readable spelling of the identifier, e.g., `operator +'.  */
10068
10069 static tree
10070 cp_parser_operator (cp_parser* parser)
10071 {
10072   tree id = NULL_TREE;
10073   cp_token *token;
10074
10075   /* Peek at the next token.  */
10076   token = cp_lexer_peek_token (parser->lexer);
10077   /* Figure out which operator we have.  */
10078   switch (token->type)
10079     {
10080     case CPP_KEYWORD:
10081       {
10082         enum tree_code op;
10083
10084         /* The keyword should be either `new' or `delete'.  */
10085         if (token->keyword == RID_NEW)
10086           op = NEW_EXPR;
10087         else if (token->keyword == RID_DELETE)
10088           op = DELETE_EXPR;
10089         else
10090           break;
10091
10092         /* Consume the `new' or `delete' token.  */
10093         cp_lexer_consume_token (parser->lexer);
10094
10095         /* Peek at the next token.  */
10096         token = cp_lexer_peek_token (parser->lexer);
10097         /* If it's a `[' token then this is the array variant of the
10098            operator.  */
10099         if (token->type == CPP_OPEN_SQUARE)
10100           {
10101             /* Consume the `[' token.  */
10102             cp_lexer_consume_token (parser->lexer);
10103             /* Look for the `]' token.  */
10104             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10105             id = ansi_opname (op == NEW_EXPR
10106                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10107           }
10108         /* Otherwise, we have the non-array variant.  */
10109         else
10110           id = ansi_opname (op);
10111
10112         return id;
10113       }
10114
10115     case CPP_PLUS:
10116       id = ansi_opname (PLUS_EXPR);
10117       break;
10118
10119     case CPP_MINUS:
10120       id = ansi_opname (MINUS_EXPR);
10121       break;
10122
10123     case CPP_MULT:
10124       id = ansi_opname (MULT_EXPR);
10125       break;
10126
10127     case CPP_DIV:
10128       id = ansi_opname (TRUNC_DIV_EXPR);
10129       break;
10130
10131     case CPP_MOD:
10132       id = ansi_opname (TRUNC_MOD_EXPR);
10133       break;
10134
10135     case CPP_XOR:
10136       id = ansi_opname (BIT_XOR_EXPR);
10137       break;
10138
10139     case CPP_AND:
10140       id = ansi_opname (BIT_AND_EXPR);
10141       break;
10142
10143     case CPP_OR:
10144       id = ansi_opname (BIT_IOR_EXPR);
10145       break;
10146
10147     case CPP_COMPL:
10148       id = ansi_opname (BIT_NOT_EXPR);
10149       break;
10150
10151     case CPP_NOT:
10152       id = ansi_opname (TRUTH_NOT_EXPR);
10153       break;
10154
10155     case CPP_EQ:
10156       id = ansi_assopname (NOP_EXPR);
10157       break;
10158
10159     case CPP_LESS:
10160       id = ansi_opname (LT_EXPR);
10161       break;
10162
10163     case CPP_GREATER:
10164       id = ansi_opname (GT_EXPR);
10165       break;
10166
10167     case CPP_PLUS_EQ:
10168       id = ansi_assopname (PLUS_EXPR);
10169       break;
10170
10171     case CPP_MINUS_EQ:
10172       id = ansi_assopname (MINUS_EXPR);
10173       break;
10174
10175     case CPP_MULT_EQ:
10176       id = ansi_assopname (MULT_EXPR);
10177       break;
10178
10179     case CPP_DIV_EQ:
10180       id = ansi_assopname (TRUNC_DIV_EXPR);
10181       break;
10182
10183     case CPP_MOD_EQ:
10184       id = ansi_assopname (TRUNC_MOD_EXPR);
10185       break;
10186
10187     case CPP_XOR_EQ:
10188       id = ansi_assopname (BIT_XOR_EXPR);
10189       break;
10190
10191     case CPP_AND_EQ:
10192       id = ansi_assopname (BIT_AND_EXPR);
10193       break;
10194
10195     case CPP_OR_EQ:
10196       id = ansi_assopname (BIT_IOR_EXPR);
10197       break;
10198
10199     case CPP_LSHIFT:
10200       id = ansi_opname (LSHIFT_EXPR);
10201       break;
10202
10203     case CPP_RSHIFT:
10204       id = ansi_opname (RSHIFT_EXPR);
10205       break;
10206
10207     case CPP_LSHIFT_EQ:
10208       id = ansi_assopname (LSHIFT_EXPR);
10209       break;
10210
10211     case CPP_RSHIFT_EQ:
10212       id = ansi_assopname (RSHIFT_EXPR);
10213       break;
10214
10215     case CPP_EQ_EQ:
10216       id = ansi_opname (EQ_EXPR);
10217       break;
10218
10219     case CPP_NOT_EQ:
10220       id = ansi_opname (NE_EXPR);
10221       break;
10222
10223     case CPP_LESS_EQ:
10224       id = ansi_opname (LE_EXPR);
10225       break;
10226
10227     case CPP_GREATER_EQ:
10228       id = ansi_opname (GE_EXPR);
10229       break;
10230
10231     case CPP_AND_AND:
10232       id = ansi_opname (TRUTH_ANDIF_EXPR);
10233       break;
10234
10235     case CPP_OR_OR:
10236       id = ansi_opname (TRUTH_ORIF_EXPR);
10237       break;
10238
10239     case CPP_PLUS_PLUS:
10240       id = ansi_opname (POSTINCREMENT_EXPR);
10241       break;
10242
10243     case CPP_MINUS_MINUS:
10244       id = ansi_opname (PREDECREMENT_EXPR);
10245       break;
10246
10247     case CPP_COMMA:
10248       id = ansi_opname (COMPOUND_EXPR);
10249       break;
10250
10251     case CPP_DEREF_STAR:
10252       id = ansi_opname (MEMBER_REF);
10253       break;
10254
10255     case CPP_DEREF:
10256       id = ansi_opname (COMPONENT_REF);
10257       break;
10258
10259     case CPP_OPEN_PAREN:
10260       /* Consume the `('.  */
10261       cp_lexer_consume_token (parser->lexer);
10262       /* Look for the matching `)'.  */
10263       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10264       return ansi_opname (CALL_EXPR);
10265
10266     case CPP_OPEN_SQUARE:
10267       /* Consume the `['.  */
10268       cp_lexer_consume_token (parser->lexer);
10269       /* Look for the matching `]'.  */
10270       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10271       return ansi_opname (ARRAY_REF);
10272
10273     default:
10274       /* Anything else is an error.  */
10275       break;
10276     }
10277
10278   /* If we have selected an identifier, we need to consume the
10279      operator token.  */
10280   if (id)
10281     cp_lexer_consume_token (parser->lexer);
10282   /* Otherwise, no valid operator name was present.  */
10283   else
10284     {
10285       cp_parser_error (parser, "expected operator");
10286       id = error_mark_node;
10287     }
10288
10289   return id;
10290 }
10291
10292 /* Parse a template-declaration.
10293
10294    template-declaration:
10295      export [opt] template < template-parameter-list > declaration
10296
10297    If MEMBER_P is TRUE, this template-declaration occurs within a
10298    class-specifier.
10299
10300    The grammar rule given by the standard isn't correct.  What
10301    is really meant is:
10302
10303    template-declaration:
10304      export [opt] template-parameter-list-seq
10305        decl-specifier-seq [opt] init-declarator [opt] ;
10306      export [opt] template-parameter-list-seq
10307        function-definition
10308
10309    template-parameter-list-seq:
10310      template-parameter-list-seq [opt]
10311      template < template-parameter-list >  */
10312
10313 static void
10314 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10315 {
10316   /* Check for `export'.  */
10317   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10318     {
10319       /* Consume the `export' token.  */
10320       cp_lexer_consume_token (parser->lexer);
10321       /* Warn that we do not support `export'.  */
10322       warning (0, "keyword %<export%> not implemented, and will be ignored");
10323     }
10324
10325   cp_parser_template_declaration_after_export (parser, member_p);
10326 }
10327
10328 /* Parse a template-parameter-list.
10329
10330    template-parameter-list:
10331      template-parameter
10332      template-parameter-list , template-parameter
10333
10334    Returns a TREE_LIST.  Each node represents a template parameter.
10335    The nodes are connected via their TREE_CHAINs.  */
10336
10337 static tree
10338 cp_parser_template_parameter_list (cp_parser* parser)
10339 {
10340   tree parameter_list = NULL_TREE;
10341
10342   begin_template_parm_list ();
10343   while (true)
10344     {
10345       tree parameter;
10346       bool is_non_type;
10347       bool is_parameter_pack;
10348       location_t parm_loc;
10349
10350       /* Parse the template-parameter.  */
10351       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10352       parameter = cp_parser_template_parameter (parser, 
10353                                                 &is_non_type,
10354                                                 &is_parameter_pack);
10355       /* Add it to the list.  */
10356       if (parameter != error_mark_node)
10357         parameter_list = process_template_parm (parameter_list,
10358                                                 parm_loc,
10359                                                 parameter,
10360                                                 is_non_type,
10361                                                 is_parameter_pack);
10362       else
10363        {
10364          tree err_parm = build_tree_list (parameter, parameter);
10365          TREE_VALUE (err_parm) = error_mark_node;
10366          parameter_list = chainon (parameter_list, err_parm);
10367        }
10368
10369       /* If the next token is not a `,', we're done.  */
10370       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10371         break;
10372       /* Otherwise, consume the `,' token.  */
10373       cp_lexer_consume_token (parser->lexer);
10374     }
10375
10376   return end_template_parm_list (parameter_list);
10377 }
10378
10379 /* Parse a template-parameter.
10380
10381    template-parameter:
10382      type-parameter
10383      parameter-declaration
10384
10385    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10386    the parameter.  The TREE_PURPOSE is the default value, if any.
10387    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10388    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10389    set to true iff this parameter is a parameter pack. */
10390
10391 static tree
10392 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10393                               bool *is_parameter_pack)
10394 {
10395   cp_token *token;
10396   cp_parameter_declarator *parameter_declarator;
10397   cp_declarator *id_declarator;
10398   tree parm;
10399
10400   /* Assume it is a type parameter or a template parameter.  */
10401   *is_non_type = false;
10402   /* Assume it not a parameter pack. */
10403   *is_parameter_pack = false;
10404   /* Peek at the next token.  */
10405   token = cp_lexer_peek_token (parser->lexer);
10406   /* If it is `class' or `template', we have a type-parameter.  */
10407   if (token->keyword == RID_TEMPLATE)
10408     return cp_parser_type_parameter (parser, is_parameter_pack);
10409   /* If it is `class' or `typename' we do not know yet whether it is a
10410      type parameter or a non-type parameter.  Consider:
10411
10412        template <typename T, typename T::X X> ...
10413
10414      or:
10415
10416        template <class C, class D*> ...
10417
10418      Here, the first parameter is a type parameter, and the second is
10419      a non-type parameter.  We can tell by looking at the token after
10420      the identifier -- if it is a `,', `=', or `>' then we have a type
10421      parameter.  */
10422   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10423     {
10424       /* Peek at the token after `class' or `typename'.  */
10425       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10426       /* If it's an ellipsis, we have a template type parameter
10427          pack. */
10428       if (token->type == CPP_ELLIPSIS)
10429         return cp_parser_type_parameter (parser, is_parameter_pack);
10430       /* If it's an identifier, skip it.  */
10431       if (token->type == CPP_NAME)
10432         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10433       /* Now, see if the token looks like the end of a template
10434          parameter.  */
10435       if (token->type == CPP_COMMA
10436           || token->type == CPP_EQ
10437           || token->type == CPP_GREATER)
10438         return cp_parser_type_parameter (parser, is_parameter_pack);
10439     }
10440
10441   /* Otherwise, it is a non-type parameter.
10442
10443      [temp.param]
10444
10445      When parsing a default template-argument for a non-type
10446      template-parameter, the first non-nested `>' is taken as the end
10447      of the template parameter-list rather than a greater-than
10448      operator.  */
10449   *is_non_type = true;
10450   parameter_declarator
10451      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10452                                         /*parenthesized_p=*/NULL);
10453
10454   /* If the parameter declaration is marked as a parameter pack, set
10455      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10456      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10457      grokdeclarator. */
10458   if (parameter_declarator
10459       && parameter_declarator->declarator
10460       && parameter_declarator->declarator->parameter_pack_p)
10461     {
10462       *is_parameter_pack = true;
10463       parameter_declarator->declarator->parameter_pack_p = false;
10464     }
10465
10466   /* If the next token is an ellipsis, and we don't already have it
10467      marked as a parameter pack, then we have a parameter pack (that
10468      has no declarator).  */
10469   if (!*is_parameter_pack
10470       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10471       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10472     {
10473       /* Consume the `...'.  */
10474       cp_lexer_consume_token (parser->lexer);
10475       maybe_warn_variadic_templates ();
10476       
10477       *is_parameter_pack = true;
10478     }
10479   /* We might end up with a pack expansion as the type of the non-type
10480      template parameter, in which case this is a non-type template
10481      parameter pack.  */
10482   else if (parameter_declarator
10483            && parameter_declarator->decl_specifiers.type
10484            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10485     {
10486       *is_parameter_pack = true;
10487       parameter_declarator->decl_specifiers.type = 
10488         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10489     }
10490
10491   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10492     {
10493       /* Parameter packs cannot have default arguments.  However, a
10494          user may try to do so, so we'll parse them and give an
10495          appropriate diagnostic here.  */
10496
10497       /* Consume the `='.  */
10498       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10499       cp_lexer_consume_token (parser->lexer);
10500       
10501       /* Find the name of the parameter pack.  */     
10502       id_declarator = parameter_declarator->declarator;
10503       while (id_declarator && id_declarator->kind != cdk_id)
10504         id_declarator = id_declarator->declarator;
10505       
10506       if (id_declarator && id_declarator->kind == cdk_id)
10507         error_at (start_token->location,
10508                   "template parameter pack %qD cannot have a default argument",
10509                   id_declarator->u.id.unqualified_name);
10510       else
10511         error_at (start_token->location,
10512                   "template parameter pack cannot have a default argument");
10513       
10514       /* Parse the default argument, but throw away the result.  */
10515       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10516     }
10517
10518   parm = grokdeclarator (parameter_declarator->declarator,
10519                          &parameter_declarator->decl_specifiers,
10520                          TPARM, /*initialized=*/0,
10521                          /*attrlist=*/NULL);
10522   if (parm == error_mark_node)
10523     return error_mark_node;
10524
10525   return build_tree_list (parameter_declarator->default_argument, parm);
10526 }
10527
10528 /* Parse a type-parameter.
10529
10530    type-parameter:
10531      class identifier [opt]
10532      class identifier [opt] = type-id
10533      typename identifier [opt]
10534      typename identifier [opt] = type-id
10535      template < template-parameter-list > class identifier [opt]
10536      template < template-parameter-list > class identifier [opt]
10537        = id-expression
10538
10539    GNU Extension (variadic templates):
10540
10541    type-parameter:
10542      class ... identifier [opt]
10543      typename ... identifier [opt]
10544
10545    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10546    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10547    the declaration of the parameter.
10548
10549    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10550
10551 static tree
10552 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10553 {
10554   cp_token *token;
10555   tree parameter;
10556
10557   /* Look for a keyword to tell us what kind of parameter this is.  */
10558   token = cp_parser_require (parser, CPP_KEYWORD,
10559                              "%<class%>, %<typename%>, or %<template%>");
10560   if (!token)
10561     return error_mark_node;
10562
10563   switch (token->keyword)
10564     {
10565     case RID_CLASS:
10566     case RID_TYPENAME:
10567       {
10568         tree identifier;
10569         tree default_argument;
10570
10571         /* If the next token is an ellipsis, we have a template
10572            argument pack. */
10573         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10574           {
10575             /* Consume the `...' token. */
10576             cp_lexer_consume_token (parser->lexer);
10577             maybe_warn_variadic_templates ();
10578
10579             *is_parameter_pack = true;
10580           }
10581
10582         /* If the next token is an identifier, then it names the
10583            parameter.  */
10584         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10585           identifier = cp_parser_identifier (parser);
10586         else
10587           identifier = NULL_TREE;
10588
10589         /* Create the parameter.  */
10590         parameter = finish_template_type_parm (class_type_node, identifier);
10591
10592         /* If the next token is an `=', we have a default argument.  */
10593         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10594           {
10595             /* Consume the `=' token.  */
10596             cp_lexer_consume_token (parser->lexer);
10597             /* Parse the default-argument.  */
10598             push_deferring_access_checks (dk_no_deferred);
10599             default_argument = cp_parser_type_id (parser);
10600
10601             /* Template parameter packs cannot have default
10602                arguments. */
10603             if (*is_parameter_pack)
10604               {
10605                 if (identifier)
10606                   error_at (token->location,
10607                             "template parameter pack %qD cannot have a "
10608                             "default argument", identifier);
10609                 else
10610                   error_at (token->location,
10611                             "template parameter packs cannot have "
10612                             "default arguments");
10613                 default_argument = NULL_TREE;
10614               }
10615             pop_deferring_access_checks ();
10616           }
10617         else
10618           default_argument = NULL_TREE;
10619
10620         /* Create the combined representation of the parameter and the
10621            default argument.  */
10622         parameter = build_tree_list (default_argument, parameter);
10623       }
10624       break;
10625
10626     case RID_TEMPLATE:
10627       {
10628         tree parameter_list;
10629         tree identifier;
10630         tree default_argument;
10631
10632         /* Look for the `<'.  */
10633         cp_parser_require (parser, CPP_LESS, "%<<%>");
10634         /* Parse the template-parameter-list.  */
10635         parameter_list = cp_parser_template_parameter_list (parser);
10636         /* Look for the `>'.  */
10637         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10638         /* Look for the `class' keyword.  */
10639         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10640         /* If the next token is an ellipsis, we have a template
10641            argument pack. */
10642         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10643           {
10644             /* Consume the `...' token. */
10645             cp_lexer_consume_token (parser->lexer);
10646             maybe_warn_variadic_templates ();
10647
10648             *is_parameter_pack = true;
10649           }
10650         /* If the next token is an `=', then there is a
10651            default-argument.  If the next token is a `>', we are at
10652            the end of the parameter-list.  If the next token is a `,',
10653            then we are at the end of this parameter.  */
10654         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10655             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10656             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10657           {
10658             identifier = cp_parser_identifier (parser);
10659             /* Treat invalid names as if the parameter were nameless.  */
10660             if (identifier == error_mark_node)
10661               identifier = NULL_TREE;
10662           }
10663         else
10664           identifier = NULL_TREE;
10665
10666         /* Create the template parameter.  */
10667         parameter = finish_template_template_parm (class_type_node,
10668                                                    identifier);
10669
10670         /* If the next token is an `=', then there is a
10671            default-argument.  */
10672         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10673           {
10674             bool is_template;
10675
10676             /* Consume the `='.  */
10677             cp_lexer_consume_token (parser->lexer);
10678             /* Parse the id-expression.  */
10679             push_deferring_access_checks (dk_no_deferred);
10680             /* save token before parsing the id-expression, for error
10681                reporting */
10682             token = cp_lexer_peek_token (parser->lexer);
10683             default_argument
10684               = cp_parser_id_expression (parser,
10685                                          /*template_keyword_p=*/false,
10686                                          /*check_dependency_p=*/true,
10687                                          /*template_p=*/&is_template,
10688                                          /*declarator_p=*/false,
10689                                          /*optional_p=*/false);
10690             if (TREE_CODE (default_argument) == TYPE_DECL)
10691               /* If the id-expression was a template-id that refers to
10692                  a template-class, we already have the declaration here,
10693                  so no further lookup is needed.  */
10694                  ;
10695             else
10696               /* Look up the name.  */
10697               default_argument
10698                 = cp_parser_lookup_name (parser, default_argument,
10699                                          none_type,
10700                                          /*is_template=*/is_template,
10701                                          /*is_namespace=*/false,
10702                                          /*check_dependency=*/true,
10703                                          /*ambiguous_decls=*/NULL,
10704                                          token->location);
10705             /* See if the default argument is valid.  */
10706             default_argument
10707               = check_template_template_default_arg (default_argument);
10708
10709             /* Template parameter packs cannot have default
10710                arguments. */
10711             if (*is_parameter_pack)
10712               {
10713                 if (identifier)
10714                   error_at (token->location,
10715                             "template parameter pack %qD cannot "
10716                             "have a default argument",
10717                             identifier);
10718                 else
10719                   error_at (token->location, "template parameter packs cannot "
10720                             "have default arguments");
10721                 default_argument = NULL_TREE;
10722               }
10723             pop_deferring_access_checks ();
10724           }
10725         else
10726           default_argument = NULL_TREE;
10727
10728         /* Create the combined representation of the parameter and the
10729            default argument.  */
10730         parameter = build_tree_list (default_argument, parameter);
10731       }
10732       break;
10733
10734     default:
10735       gcc_unreachable ();
10736       break;
10737     }
10738
10739   return parameter;
10740 }
10741
10742 /* Parse a template-id.
10743
10744    template-id:
10745      template-name < template-argument-list [opt] >
10746
10747    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10748    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10749    returned.  Otherwise, if the template-name names a function, or set
10750    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10751    names a class, returns a TYPE_DECL for the specialization.
10752
10753    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10754    uninstantiated templates.  */
10755
10756 static tree
10757 cp_parser_template_id (cp_parser *parser,
10758                        bool template_keyword_p,
10759                        bool check_dependency_p,
10760                        bool is_declaration)
10761 {
10762   int i;
10763   tree templ;
10764   tree arguments;
10765   tree template_id;
10766   cp_token_position start_of_id = 0;
10767   deferred_access_check *chk;
10768   VEC (deferred_access_check,gc) *access_check;
10769   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10770   bool is_identifier;
10771
10772   /* If the next token corresponds to a template-id, there is no need
10773      to reparse it.  */
10774   next_token = cp_lexer_peek_token (parser->lexer);
10775   if (next_token->type == CPP_TEMPLATE_ID)
10776     {
10777       struct tree_check *check_value;
10778
10779       /* Get the stored value.  */
10780       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10781       /* Perform any access checks that were deferred.  */
10782       access_check = check_value->checks;
10783       if (access_check)
10784         {
10785           for (i = 0 ;
10786                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10787                ++i)
10788             {
10789               perform_or_defer_access_check (chk->binfo,
10790                                              chk->decl,
10791                                              chk->diag_decl);
10792             }
10793         }
10794       /* Return the stored value.  */
10795       return check_value->value;
10796     }
10797
10798   /* Avoid performing name lookup if there is no possibility of
10799      finding a template-id.  */
10800   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10801       || (next_token->type == CPP_NAME
10802           && !cp_parser_nth_token_starts_template_argument_list_p
10803                (parser, 2)))
10804     {
10805       cp_parser_error (parser, "expected template-id");
10806       return error_mark_node;
10807     }
10808
10809   /* Remember where the template-id starts.  */
10810   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10811     start_of_id = cp_lexer_token_position (parser->lexer, false);
10812
10813   push_deferring_access_checks (dk_deferred);
10814
10815   /* Parse the template-name.  */
10816   is_identifier = false;
10817   token = cp_lexer_peek_token (parser->lexer);
10818   templ = cp_parser_template_name (parser, template_keyword_p,
10819                                    check_dependency_p,
10820                                    is_declaration,
10821                                    &is_identifier);
10822   if (templ == error_mark_node || is_identifier)
10823     {
10824       pop_deferring_access_checks ();
10825       return templ;
10826     }
10827
10828   /* If we find the sequence `[:' after a template-name, it's probably
10829      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10830      parse correctly the argument list.  */
10831   next_token = cp_lexer_peek_token (parser->lexer);
10832   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10833   if (next_token->type == CPP_OPEN_SQUARE
10834       && next_token->flags & DIGRAPH
10835       && next_token_2->type == CPP_COLON
10836       && !(next_token_2->flags & PREV_WHITE))
10837     {
10838       cp_parser_parse_tentatively (parser);
10839       /* Change `:' into `::'.  */
10840       next_token_2->type = CPP_SCOPE;
10841       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10842          CPP_LESS.  */
10843       cp_lexer_consume_token (parser->lexer);
10844
10845       /* Parse the arguments.  */
10846       arguments = cp_parser_enclosed_template_argument_list (parser);
10847       if (!cp_parser_parse_definitely (parser))
10848         {
10849           /* If we couldn't parse an argument list, then we revert our changes
10850              and return simply an error. Maybe this is not a template-id
10851              after all.  */
10852           next_token_2->type = CPP_COLON;
10853           cp_parser_error (parser, "expected %<<%>");
10854           pop_deferring_access_checks ();
10855           return error_mark_node;
10856         }
10857       /* Otherwise, emit an error about the invalid digraph, but continue
10858          parsing because we got our argument list.  */
10859       if (permerror (next_token->location,
10860                      "%<<::%> cannot begin a template-argument list"))
10861         {
10862           static bool hint = false;
10863           inform (next_token->location,
10864                   "%<<:%> is an alternate spelling for %<[%>."
10865                   " Insert whitespace between %<<%> and %<::%>");
10866           if (!hint && !flag_permissive)
10867             {
10868               inform (next_token->location, "(if you use %<-fpermissive%>"
10869                       " G++ will accept your code)");
10870               hint = true;
10871             }
10872         }
10873     }
10874   else
10875     {
10876       /* Look for the `<' that starts the template-argument-list.  */
10877       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10878         {
10879           pop_deferring_access_checks ();
10880           return error_mark_node;
10881         }
10882       /* Parse the arguments.  */
10883       arguments = cp_parser_enclosed_template_argument_list (parser);
10884     }
10885
10886   /* Build a representation of the specialization.  */
10887   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10888     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10889   else if (DECL_CLASS_TEMPLATE_P (templ)
10890            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10891     {
10892       bool entering_scope;
10893       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10894          template (rather than some instantiation thereof) only if
10895          is not nested within some other construct.  For example, in
10896          "template <typename T> void f(T) { A<T>::", A<T> is just an
10897          instantiation of A.  */
10898       entering_scope = (template_parm_scope_p ()
10899                         && cp_lexer_next_token_is (parser->lexer,
10900                                                    CPP_SCOPE));
10901       template_id
10902         = finish_template_type (templ, arguments, entering_scope);
10903     }
10904   else
10905     {
10906       /* If it's not a class-template or a template-template, it should be
10907          a function-template.  */
10908       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10909                    || TREE_CODE (templ) == OVERLOAD
10910                    || BASELINK_P (templ)));
10911
10912       template_id = lookup_template_function (templ, arguments);
10913     }
10914
10915   /* If parsing tentatively, replace the sequence of tokens that makes
10916      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10917      should we re-parse the token stream, we will not have to repeat
10918      the effort required to do the parse, nor will we issue duplicate
10919      error messages about problems during instantiation of the
10920      template.  */
10921   if (start_of_id)
10922     {
10923       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10924
10925       /* Reset the contents of the START_OF_ID token.  */
10926       token->type = CPP_TEMPLATE_ID;
10927       /* Retrieve any deferred checks.  Do not pop this access checks yet
10928          so the memory will not be reclaimed during token replacing below.  */
10929       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10930       token->u.tree_check_value->value = template_id;
10931       token->u.tree_check_value->checks = get_deferred_access_checks ();
10932       token->keyword = RID_MAX;
10933
10934       /* Purge all subsequent tokens.  */
10935       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10936
10937       /* ??? Can we actually assume that, if template_id ==
10938          error_mark_node, we will have issued a diagnostic to the
10939          user, as opposed to simply marking the tentative parse as
10940          failed?  */
10941       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10942         error_at (token->location, "parse error in template argument list");
10943     }
10944
10945   pop_deferring_access_checks ();
10946   return template_id;
10947 }
10948
10949 /* Parse a template-name.
10950
10951    template-name:
10952      identifier
10953
10954    The standard should actually say:
10955
10956    template-name:
10957      identifier
10958      operator-function-id
10959
10960    A defect report has been filed about this issue.
10961
10962    A conversion-function-id cannot be a template name because they cannot
10963    be part of a template-id. In fact, looking at this code:
10964
10965    a.operator K<int>()
10966
10967    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10968    It is impossible to call a templated conversion-function-id with an
10969    explicit argument list, since the only allowed template parameter is
10970    the type to which it is converting.
10971
10972    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10973    `template' keyword, in a construction like:
10974
10975      T::template f<3>()
10976
10977    In that case `f' is taken to be a template-name, even though there
10978    is no way of knowing for sure.
10979
10980    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10981    name refers to a set of overloaded functions, at least one of which
10982    is a template, or an IDENTIFIER_NODE with the name of the template,
10983    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10984    names are looked up inside uninstantiated templates.  */
10985
10986 static tree
10987 cp_parser_template_name (cp_parser* parser,
10988                          bool template_keyword_p,
10989                          bool check_dependency_p,
10990                          bool is_declaration,
10991                          bool *is_identifier)
10992 {
10993   tree identifier;
10994   tree decl;
10995   tree fns;
10996   cp_token *token = cp_lexer_peek_token (parser->lexer);
10997
10998   /* If the next token is `operator', then we have either an
10999      operator-function-id or a conversion-function-id.  */
11000   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11001     {
11002       /* We don't know whether we're looking at an
11003          operator-function-id or a conversion-function-id.  */
11004       cp_parser_parse_tentatively (parser);
11005       /* Try an operator-function-id.  */
11006       identifier = cp_parser_operator_function_id (parser);
11007       /* If that didn't work, try a conversion-function-id.  */
11008       if (!cp_parser_parse_definitely (parser))
11009         {
11010           cp_parser_error (parser, "expected template-name");
11011           return error_mark_node;
11012         }
11013     }
11014   /* Look for the identifier.  */
11015   else
11016     identifier = cp_parser_identifier (parser);
11017
11018   /* If we didn't find an identifier, we don't have a template-id.  */
11019   if (identifier == error_mark_node)
11020     return error_mark_node;
11021
11022   /* If the name immediately followed the `template' keyword, then it
11023      is a template-name.  However, if the next token is not `<', then
11024      we do not treat it as a template-name, since it is not being used
11025      as part of a template-id.  This enables us to handle constructs
11026      like:
11027
11028        template <typename T> struct S { S(); };
11029        template <typename T> S<T>::S();
11030
11031      correctly.  We would treat `S' as a template -- if it were `S<T>'
11032      -- but we do not if there is no `<'.  */
11033
11034   if (processing_template_decl
11035       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11036     {
11037       /* In a declaration, in a dependent context, we pretend that the
11038          "template" keyword was present in order to improve error
11039          recovery.  For example, given:
11040
11041            template <typename T> void f(T::X<int>);
11042
11043          we want to treat "X<int>" as a template-id.  */
11044       if (is_declaration
11045           && !template_keyword_p
11046           && parser->scope && TYPE_P (parser->scope)
11047           && check_dependency_p
11048           && dependent_scope_p (parser->scope)
11049           /* Do not do this for dtors (or ctors), since they never
11050              need the template keyword before their name.  */
11051           && !constructor_name_p (identifier, parser->scope))
11052         {
11053           cp_token_position start = 0;
11054
11055           /* Explain what went wrong.  */
11056           error_at (token->location, "non-template %qD used as template",
11057                     identifier);
11058           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11059                   parser->scope, identifier);
11060           /* If parsing tentatively, find the location of the "<" token.  */
11061           if (cp_parser_simulate_error (parser))
11062             start = cp_lexer_token_position (parser->lexer, true);
11063           /* Parse the template arguments so that we can issue error
11064              messages about them.  */
11065           cp_lexer_consume_token (parser->lexer);
11066           cp_parser_enclosed_template_argument_list (parser);
11067           /* Skip tokens until we find a good place from which to
11068              continue parsing.  */
11069           cp_parser_skip_to_closing_parenthesis (parser,
11070                                                  /*recovering=*/true,
11071                                                  /*or_comma=*/true,
11072                                                  /*consume_paren=*/false);
11073           /* If parsing tentatively, permanently remove the
11074              template argument list.  That will prevent duplicate
11075              error messages from being issued about the missing
11076              "template" keyword.  */
11077           if (start)
11078             cp_lexer_purge_tokens_after (parser->lexer, start);
11079           if (is_identifier)
11080             *is_identifier = true;
11081           return identifier;
11082         }
11083
11084       /* If the "template" keyword is present, then there is generally
11085          no point in doing name-lookup, so we just return IDENTIFIER.
11086          But, if the qualifying scope is non-dependent then we can
11087          (and must) do name-lookup normally.  */
11088       if (template_keyword_p
11089           && (!parser->scope
11090               || (TYPE_P (parser->scope)
11091                   && dependent_type_p (parser->scope))))
11092         return identifier;
11093     }
11094
11095   /* Look up the name.  */
11096   decl = cp_parser_lookup_name (parser, identifier,
11097                                 none_type,
11098                                 /*is_template=*/true,
11099                                 /*is_namespace=*/false,
11100                                 check_dependency_p,
11101                                 /*ambiguous_decls=*/NULL,
11102                                 token->location);
11103
11104   /* If DECL is a template, then the name was a template-name.  */
11105   if (TREE_CODE (decl) == TEMPLATE_DECL)
11106     ;
11107   else
11108     {
11109       tree fn = NULL_TREE;
11110
11111       /* The standard does not explicitly indicate whether a name that
11112          names a set of overloaded declarations, some of which are
11113          templates, is a template-name.  However, such a name should
11114          be a template-name; otherwise, there is no way to form a
11115          template-id for the overloaded templates.  */
11116       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11117       if (TREE_CODE (fns) == OVERLOAD)
11118         for (fn = fns; fn; fn = OVL_NEXT (fn))
11119           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11120             break;
11121
11122       if (!fn)
11123         {
11124           /* The name does not name a template.  */
11125           cp_parser_error (parser, "expected template-name");
11126           return error_mark_node;
11127         }
11128     }
11129
11130   /* If DECL is dependent, and refers to a function, then just return
11131      its name; we will look it up again during template instantiation.  */
11132   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11133     {
11134       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11135       if (TYPE_P (scope) && dependent_type_p (scope))
11136         return identifier;
11137     }
11138
11139   return decl;
11140 }
11141
11142 /* Parse a template-argument-list.
11143
11144    template-argument-list:
11145      template-argument ... [opt]
11146      template-argument-list , template-argument ... [opt]
11147
11148    Returns a TREE_VEC containing the arguments.  */
11149
11150 static tree
11151 cp_parser_template_argument_list (cp_parser* parser)
11152 {
11153   tree fixed_args[10];
11154   unsigned n_args = 0;
11155   unsigned alloced = 10;
11156   tree *arg_ary = fixed_args;
11157   tree vec;
11158   bool saved_in_template_argument_list_p;
11159   bool saved_ice_p;
11160   bool saved_non_ice_p;
11161
11162   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11163   parser->in_template_argument_list_p = true;
11164   /* Even if the template-id appears in an integral
11165      constant-expression, the contents of the argument list do
11166      not.  */
11167   saved_ice_p = parser->integral_constant_expression_p;
11168   parser->integral_constant_expression_p = false;
11169   saved_non_ice_p = parser->non_integral_constant_expression_p;
11170   parser->non_integral_constant_expression_p = false;
11171   /* Parse the arguments.  */
11172   do
11173     {
11174       tree argument;
11175
11176       if (n_args)
11177         /* Consume the comma.  */
11178         cp_lexer_consume_token (parser->lexer);
11179
11180       /* Parse the template-argument.  */
11181       argument = cp_parser_template_argument (parser);
11182
11183       /* If the next token is an ellipsis, we're expanding a template
11184          argument pack. */
11185       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11186         {
11187           if (argument == error_mark_node)
11188             {
11189               cp_token *token = cp_lexer_peek_token (parser->lexer);
11190               error_at (token->location,
11191                         "expected parameter pack before %<...%>");
11192             }
11193           /* Consume the `...' token. */
11194           cp_lexer_consume_token (parser->lexer);
11195
11196           /* Make the argument into a TYPE_PACK_EXPANSION or
11197              EXPR_PACK_EXPANSION. */
11198           argument = make_pack_expansion (argument);
11199         }
11200
11201       if (n_args == alloced)
11202         {
11203           alloced *= 2;
11204
11205           if (arg_ary == fixed_args)
11206             {
11207               arg_ary = XNEWVEC (tree, alloced);
11208               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11209             }
11210           else
11211             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11212         }
11213       arg_ary[n_args++] = argument;
11214     }
11215   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11216
11217   vec = make_tree_vec (n_args);
11218
11219   while (n_args--)
11220     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11221
11222   if (arg_ary != fixed_args)
11223     free (arg_ary);
11224   parser->non_integral_constant_expression_p = saved_non_ice_p;
11225   parser->integral_constant_expression_p = saved_ice_p;
11226   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11227   return vec;
11228 }
11229
11230 /* Parse a template-argument.
11231
11232    template-argument:
11233      assignment-expression
11234      type-id
11235      id-expression
11236
11237    The representation is that of an assignment-expression, type-id, or
11238    id-expression -- except that the qualified id-expression is
11239    evaluated, so that the value returned is either a DECL or an
11240    OVERLOAD.
11241
11242    Although the standard says "assignment-expression", it forbids
11243    throw-expressions or assignments in the template argument.
11244    Therefore, we use "conditional-expression" instead.  */
11245
11246 static tree
11247 cp_parser_template_argument (cp_parser* parser)
11248 {
11249   tree argument;
11250   bool template_p;
11251   bool address_p;
11252   bool maybe_type_id = false;
11253   cp_token *token = NULL, *argument_start_token = NULL;
11254   cp_id_kind idk;
11255
11256   /* There's really no way to know what we're looking at, so we just
11257      try each alternative in order.
11258
11259        [temp.arg]
11260
11261        In a template-argument, an ambiguity between a type-id and an
11262        expression is resolved to a type-id, regardless of the form of
11263        the corresponding template-parameter.
11264
11265      Therefore, we try a type-id first.  */
11266   cp_parser_parse_tentatively (parser);
11267   argument = cp_parser_template_type_arg (parser);
11268   /* If there was no error parsing the type-id but the next token is a
11269      '>>', our behavior depends on which dialect of C++ we're
11270      parsing. In C++98, we probably found a typo for '> >'. But there
11271      are type-id which are also valid expressions. For instance:
11272
11273      struct X { int operator >> (int); };
11274      template <int V> struct Foo {};
11275      Foo<X () >> 5> r;
11276
11277      Here 'X()' is a valid type-id of a function type, but the user just
11278      wanted to write the expression "X() >> 5". Thus, we remember that we
11279      found a valid type-id, but we still try to parse the argument as an
11280      expression to see what happens. 
11281
11282      In C++0x, the '>>' will be considered two separate '>'
11283      tokens.  */
11284   if (!cp_parser_error_occurred (parser)
11285       && cxx_dialect == cxx98
11286       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11287     {
11288       maybe_type_id = true;
11289       cp_parser_abort_tentative_parse (parser);
11290     }
11291   else
11292     {
11293       /* If the next token isn't a `,' or a `>', then this argument wasn't
11294       really finished. This means that the argument is not a valid
11295       type-id.  */
11296       if (!cp_parser_next_token_ends_template_argument_p (parser))
11297         cp_parser_error (parser, "expected template-argument");
11298       /* If that worked, we're done.  */
11299       if (cp_parser_parse_definitely (parser))
11300         return argument;
11301     }
11302   /* We're still not sure what the argument will be.  */
11303   cp_parser_parse_tentatively (parser);
11304   /* Try a template.  */
11305   argument_start_token = cp_lexer_peek_token (parser->lexer);
11306   argument = cp_parser_id_expression (parser,
11307                                       /*template_keyword_p=*/false,
11308                                       /*check_dependency_p=*/true,
11309                                       &template_p,
11310                                       /*declarator_p=*/false,
11311                                       /*optional_p=*/false);
11312   /* If the next token isn't a `,' or a `>', then this argument wasn't
11313      really finished.  */
11314   if (!cp_parser_next_token_ends_template_argument_p (parser))
11315     cp_parser_error (parser, "expected template-argument");
11316   if (!cp_parser_error_occurred (parser))
11317     {
11318       /* Figure out what is being referred to.  If the id-expression
11319          was for a class template specialization, then we will have a
11320          TYPE_DECL at this point.  There is no need to do name lookup
11321          at this point in that case.  */
11322       if (TREE_CODE (argument) != TYPE_DECL)
11323         argument = cp_parser_lookup_name (parser, argument,
11324                                           none_type,
11325                                           /*is_template=*/template_p,
11326                                           /*is_namespace=*/false,
11327                                           /*check_dependency=*/true,
11328                                           /*ambiguous_decls=*/NULL,
11329                                           argument_start_token->location);
11330       if (TREE_CODE (argument) != TEMPLATE_DECL
11331           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11332         cp_parser_error (parser, "expected template-name");
11333     }
11334   if (cp_parser_parse_definitely (parser))
11335     return argument;
11336   /* It must be a non-type argument.  There permitted cases are given
11337      in [temp.arg.nontype]:
11338
11339      -- an integral constant-expression of integral or enumeration
11340         type; or
11341
11342      -- the name of a non-type template-parameter; or
11343
11344      -- the name of an object or function with external linkage...
11345
11346      -- the address of an object or function with external linkage...
11347
11348      -- a pointer to member...  */
11349   /* Look for a non-type template parameter.  */
11350   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11351     {
11352       cp_parser_parse_tentatively (parser);
11353       argument = cp_parser_primary_expression (parser,
11354                                                /*address_p=*/false,
11355                                                /*cast_p=*/false,
11356                                                /*template_arg_p=*/true,
11357                                                &idk);
11358       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11359           || !cp_parser_next_token_ends_template_argument_p (parser))
11360         cp_parser_simulate_error (parser);
11361       if (cp_parser_parse_definitely (parser))
11362         return argument;
11363     }
11364
11365   /* If the next token is "&", the argument must be the address of an
11366      object or function with external linkage.  */
11367   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11368   if (address_p)
11369     cp_lexer_consume_token (parser->lexer);
11370   /* See if we might have an id-expression.  */
11371   token = cp_lexer_peek_token (parser->lexer);
11372   if (token->type == CPP_NAME
11373       || token->keyword == RID_OPERATOR
11374       || token->type == CPP_SCOPE
11375       || token->type == CPP_TEMPLATE_ID
11376       || token->type == CPP_NESTED_NAME_SPECIFIER)
11377     {
11378       cp_parser_parse_tentatively (parser);
11379       argument = cp_parser_primary_expression (parser,
11380                                                address_p,
11381                                                /*cast_p=*/false,
11382                                                /*template_arg_p=*/true,
11383                                                &idk);
11384       if (cp_parser_error_occurred (parser)
11385           || !cp_parser_next_token_ends_template_argument_p (parser))
11386         cp_parser_abort_tentative_parse (parser);
11387       else
11388         {
11389           tree probe;
11390
11391           if (TREE_CODE (argument) == INDIRECT_REF)
11392             {
11393               gcc_assert (REFERENCE_REF_P (argument));
11394               argument = TREE_OPERAND (argument, 0);
11395             }
11396
11397           /* If we're in a template, we represent a qualified-id referring
11398              to a static data member as a SCOPE_REF even if the scope isn't
11399              dependent so that we can check access control later.  */
11400           probe = argument;
11401           if (TREE_CODE (probe) == SCOPE_REF)
11402             probe = TREE_OPERAND (probe, 1);
11403           if (TREE_CODE (probe) == VAR_DECL)
11404             {
11405               /* A variable without external linkage might still be a
11406                  valid constant-expression, so no error is issued here
11407                  if the external-linkage check fails.  */
11408               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11409                 cp_parser_simulate_error (parser);
11410             }
11411           else if (is_overloaded_fn (argument))
11412             /* All overloaded functions are allowed; if the external
11413                linkage test does not pass, an error will be issued
11414                later.  */
11415             ;
11416           else if (address_p
11417                    && (TREE_CODE (argument) == OFFSET_REF
11418                        || TREE_CODE (argument) == SCOPE_REF))
11419             /* A pointer-to-member.  */
11420             ;
11421           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11422             ;
11423           else
11424             cp_parser_simulate_error (parser);
11425
11426           if (cp_parser_parse_definitely (parser))
11427             {
11428               if (address_p)
11429                 argument = build_x_unary_op (ADDR_EXPR, argument,
11430                                              tf_warning_or_error);
11431               return argument;
11432             }
11433         }
11434     }
11435   /* If the argument started with "&", there are no other valid
11436      alternatives at this point.  */
11437   if (address_p)
11438     {
11439       cp_parser_error (parser, "invalid non-type template argument");
11440       return error_mark_node;
11441     }
11442
11443   /* If the argument wasn't successfully parsed as a type-id followed
11444      by '>>', the argument can only be a constant expression now.
11445      Otherwise, we try parsing the constant-expression tentatively,
11446      because the argument could really be a type-id.  */
11447   if (maybe_type_id)
11448     cp_parser_parse_tentatively (parser);
11449   argument = cp_parser_constant_expression (parser,
11450                                             /*allow_non_constant_p=*/false,
11451                                             /*non_constant_p=*/NULL);
11452   argument = fold_non_dependent_expr (argument);
11453   if (!maybe_type_id)
11454     return argument;
11455   if (!cp_parser_next_token_ends_template_argument_p (parser))
11456     cp_parser_error (parser, "expected template-argument");
11457   if (cp_parser_parse_definitely (parser))
11458     return argument;
11459   /* We did our best to parse the argument as a non type-id, but that
11460      was the only alternative that matched (albeit with a '>' after
11461      it). We can assume it's just a typo from the user, and a
11462      diagnostic will then be issued.  */
11463   return cp_parser_template_type_arg (parser);
11464 }
11465
11466 /* Parse an explicit-instantiation.
11467
11468    explicit-instantiation:
11469      template declaration
11470
11471    Although the standard says `declaration', what it really means is:
11472
11473    explicit-instantiation:
11474      template decl-specifier-seq [opt] declarator [opt] ;
11475
11476    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11477    supposed to be allowed.  A defect report has been filed about this
11478    issue.
11479
11480    GNU Extension:
11481
11482    explicit-instantiation:
11483      storage-class-specifier template
11484        decl-specifier-seq [opt] declarator [opt] ;
11485      function-specifier template
11486        decl-specifier-seq [opt] declarator [opt] ;  */
11487
11488 static void
11489 cp_parser_explicit_instantiation (cp_parser* parser)
11490 {
11491   int declares_class_or_enum;
11492   cp_decl_specifier_seq decl_specifiers;
11493   tree extension_specifier = NULL_TREE;
11494   cp_token *token;
11495
11496   /* Look for an (optional) storage-class-specifier or
11497      function-specifier.  */
11498   if (cp_parser_allow_gnu_extensions_p (parser))
11499     {
11500       extension_specifier
11501         = cp_parser_storage_class_specifier_opt (parser);
11502       if (!extension_specifier)
11503         extension_specifier
11504           = cp_parser_function_specifier_opt (parser,
11505                                               /*decl_specs=*/NULL);
11506     }
11507
11508   /* Look for the `template' keyword.  */
11509   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11510   /* Let the front end know that we are processing an explicit
11511      instantiation.  */
11512   begin_explicit_instantiation ();
11513   /* [temp.explicit] says that we are supposed to ignore access
11514      control while processing explicit instantiation directives.  */
11515   push_deferring_access_checks (dk_no_check);
11516   /* Parse a decl-specifier-seq.  */
11517   token = cp_lexer_peek_token (parser->lexer);
11518   cp_parser_decl_specifier_seq (parser,
11519                                 CP_PARSER_FLAGS_OPTIONAL,
11520                                 &decl_specifiers,
11521                                 &declares_class_or_enum);
11522   /* If there was exactly one decl-specifier, and it declared a class,
11523      and there's no declarator, then we have an explicit type
11524      instantiation.  */
11525   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11526     {
11527       tree type;
11528
11529       type = check_tag_decl (&decl_specifiers);
11530       /* Turn access control back on for names used during
11531          template instantiation.  */
11532       pop_deferring_access_checks ();
11533       if (type)
11534         do_type_instantiation (type, extension_specifier,
11535                                /*complain=*/tf_error);
11536     }
11537   else
11538     {
11539       cp_declarator *declarator;
11540       tree decl;
11541
11542       /* Parse the declarator.  */
11543       declarator
11544         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11545                                 /*ctor_dtor_or_conv_p=*/NULL,
11546                                 /*parenthesized_p=*/NULL,
11547                                 /*member_p=*/false);
11548       if (declares_class_or_enum & 2)
11549         cp_parser_check_for_definition_in_return_type (declarator,
11550                                                        decl_specifiers.type,
11551                                                        decl_specifiers.type_location);
11552       if (declarator != cp_error_declarator)
11553         {
11554           decl = grokdeclarator (declarator, &decl_specifiers,
11555                                  NORMAL, 0, &decl_specifiers.attributes);
11556           /* Turn access control back on for names used during
11557              template instantiation.  */
11558           pop_deferring_access_checks ();
11559           /* Do the explicit instantiation.  */
11560           do_decl_instantiation (decl, extension_specifier);
11561         }
11562       else
11563         {
11564           pop_deferring_access_checks ();
11565           /* Skip the body of the explicit instantiation.  */
11566           cp_parser_skip_to_end_of_statement (parser);
11567         }
11568     }
11569   /* We're done with the instantiation.  */
11570   end_explicit_instantiation ();
11571
11572   cp_parser_consume_semicolon_at_end_of_statement (parser);
11573 }
11574
11575 /* Parse an explicit-specialization.
11576
11577    explicit-specialization:
11578      template < > declaration
11579
11580    Although the standard says `declaration', what it really means is:
11581
11582    explicit-specialization:
11583      template <> decl-specifier [opt] init-declarator [opt] ;
11584      template <> function-definition
11585      template <> explicit-specialization
11586      template <> template-declaration  */
11587
11588 static void
11589 cp_parser_explicit_specialization (cp_parser* parser)
11590 {
11591   bool need_lang_pop;
11592   cp_token *token = cp_lexer_peek_token (parser->lexer);
11593
11594   /* Look for the `template' keyword.  */
11595   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11596   /* Look for the `<'.  */
11597   cp_parser_require (parser, CPP_LESS, "%<<%>");
11598   /* Look for the `>'.  */
11599   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11600   /* We have processed another parameter list.  */
11601   ++parser->num_template_parameter_lists;
11602   /* [temp]
11603
11604      A template ... explicit specialization ... shall not have C
11605      linkage.  */
11606   if (current_lang_name == lang_name_c)
11607     {
11608       error_at (token->location, "template specialization with C linkage");
11609       /* Give it C++ linkage to avoid confusing other parts of the
11610          front end.  */
11611       push_lang_context (lang_name_cplusplus);
11612       need_lang_pop = true;
11613     }
11614   else
11615     need_lang_pop = false;
11616   /* Let the front end know that we are beginning a specialization.  */
11617   if (!begin_specialization ())
11618     {
11619       end_specialization ();
11620       return;
11621     }
11622
11623   /* If the next keyword is `template', we need to figure out whether
11624      or not we're looking a template-declaration.  */
11625   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11626     {
11627       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11628           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11629         cp_parser_template_declaration_after_export (parser,
11630                                                      /*member_p=*/false);
11631       else
11632         cp_parser_explicit_specialization (parser);
11633     }
11634   else
11635     /* Parse the dependent declaration.  */
11636     cp_parser_single_declaration (parser,
11637                                   /*checks=*/NULL,
11638                                   /*member_p=*/false,
11639                                   /*explicit_specialization_p=*/true,
11640                                   /*friend_p=*/NULL);
11641   /* We're done with the specialization.  */
11642   end_specialization ();
11643   /* For the erroneous case of a template with C linkage, we pushed an
11644      implicit C++ linkage scope; exit that scope now.  */
11645   if (need_lang_pop)
11646     pop_lang_context ();
11647   /* We're done with this parameter list.  */
11648   --parser->num_template_parameter_lists;
11649 }
11650
11651 /* Parse a type-specifier.
11652
11653    type-specifier:
11654      simple-type-specifier
11655      class-specifier
11656      enum-specifier
11657      elaborated-type-specifier
11658      cv-qualifier
11659
11660    GNU Extension:
11661
11662    type-specifier:
11663      __complex__
11664
11665    Returns a representation of the type-specifier.  For a
11666    class-specifier, enum-specifier, or elaborated-type-specifier, a
11667    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11668
11669    The parser flags FLAGS is used to control type-specifier parsing.
11670
11671    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11672    in a decl-specifier-seq.
11673
11674    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11675    class-specifier, enum-specifier, or elaborated-type-specifier, then
11676    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11677    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11678    zero.
11679
11680    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11681    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11682    is set to FALSE.  */
11683
11684 static tree
11685 cp_parser_type_specifier (cp_parser* parser,
11686                           cp_parser_flags flags,
11687                           cp_decl_specifier_seq *decl_specs,
11688                           bool is_declaration,
11689                           int* declares_class_or_enum,
11690                           bool* is_cv_qualifier)
11691 {
11692   tree type_spec = NULL_TREE;
11693   cp_token *token;
11694   enum rid keyword;
11695   cp_decl_spec ds = ds_last;
11696
11697   /* Assume this type-specifier does not declare a new type.  */
11698   if (declares_class_or_enum)
11699     *declares_class_or_enum = 0;
11700   /* And that it does not specify a cv-qualifier.  */
11701   if (is_cv_qualifier)
11702     *is_cv_qualifier = false;
11703   /* Peek at the next token.  */
11704   token = cp_lexer_peek_token (parser->lexer);
11705
11706   /* If we're looking at a keyword, we can use that to guide the
11707      production we choose.  */
11708   keyword = token->keyword;
11709   switch (keyword)
11710     {
11711     case RID_ENUM:
11712       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11713         goto elaborated_type_specifier;
11714
11715       /* Look for the enum-specifier.  */
11716       type_spec = cp_parser_enum_specifier (parser);
11717       /* If that worked, we're done.  */
11718       if (type_spec)
11719         {
11720           if (declares_class_or_enum)
11721             *declares_class_or_enum = 2;
11722           if (decl_specs)
11723             cp_parser_set_decl_spec_type (decl_specs,
11724                                           type_spec,
11725                                           token->location,
11726                                           /*user_defined_p=*/true);
11727           return type_spec;
11728         }
11729       else
11730         goto elaborated_type_specifier;
11731
11732       /* Any of these indicate either a class-specifier, or an
11733          elaborated-type-specifier.  */
11734     case RID_CLASS:
11735     case RID_STRUCT:
11736     case RID_UNION:
11737       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11738         goto elaborated_type_specifier;
11739
11740       /* Parse tentatively so that we can back up if we don't find a
11741          class-specifier.  */
11742       cp_parser_parse_tentatively (parser);
11743       /* Look for the class-specifier.  */
11744       type_spec = cp_parser_class_specifier (parser);
11745       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11746       /* If that worked, we're done.  */
11747       if (cp_parser_parse_definitely (parser))
11748         {
11749           if (declares_class_or_enum)
11750             *declares_class_or_enum = 2;
11751           if (decl_specs)
11752             cp_parser_set_decl_spec_type (decl_specs,
11753                                           type_spec,
11754                                           token->location,
11755                                           /*user_defined_p=*/true);
11756           return type_spec;
11757         }
11758
11759       /* Fall through.  */
11760     elaborated_type_specifier:
11761       /* We're declaring (not defining) a class or enum.  */
11762       if (declares_class_or_enum)
11763         *declares_class_or_enum = 1;
11764
11765       /* Fall through.  */
11766     case RID_TYPENAME:
11767       /* Look for an elaborated-type-specifier.  */
11768       type_spec
11769         = (cp_parser_elaborated_type_specifier
11770            (parser,
11771             decl_specs && decl_specs->specs[(int) ds_friend],
11772             is_declaration));
11773       if (decl_specs)
11774         cp_parser_set_decl_spec_type (decl_specs,
11775                                       type_spec,
11776                                       token->location,
11777                                       /*user_defined_p=*/true);
11778       return type_spec;
11779
11780     case RID_CONST:
11781       ds = ds_const;
11782       if (is_cv_qualifier)
11783         *is_cv_qualifier = true;
11784       break;
11785
11786     case RID_VOLATILE:
11787       ds = ds_volatile;
11788       if (is_cv_qualifier)
11789         *is_cv_qualifier = true;
11790       break;
11791
11792     case RID_RESTRICT:
11793       ds = ds_restrict;
11794       if (is_cv_qualifier)
11795         *is_cv_qualifier = true;
11796       break;
11797
11798     case RID_COMPLEX:
11799       /* The `__complex__' keyword is a GNU extension.  */
11800       ds = ds_complex;
11801       break;
11802
11803     default:
11804       break;
11805     }
11806
11807   /* Handle simple keywords.  */
11808   if (ds != ds_last)
11809     {
11810       if (decl_specs)
11811         {
11812           ++decl_specs->specs[(int)ds];
11813           decl_specs->any_specifiers_p = true;
11814         }
11815       return cp_lexer_consume_token (parser->lexer)->u.value;
11816     }
11817
11818   /* If we do not already have a type-specifier, assume we are looking
11819      at a simple-type-specifier.  */
11820   type_spec = cp_parser_simple_type_specifier (parser,
11821                                                decl_specs,
11822                                                flags);
11823
11824   /* If we didn't find a type-specifier, and a type-specifier was not
11825      optional in this context, issue an error message.  */
11826   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11827     {
11828       cp_parser_error (parser, "expected type specifier");
11829       return error_mark_node;
11830     }
11831
11832   return type_spec;
11833 }
11834
11835 /* Parse a simple-type-specifier.
11836
11837    simple-type-specifier:
11838      :: [opt] nested-name-specifier [opt] type-name
11839      :: [opt] nested-name-specifier template template-id
11840      char
11841      wchar_t
11842      bool
11843      short
11844      int
11845      long
11846      signed
11847      unsigned
11848      float
11849      double
11850      void
11851
11852    C++0x Extension:
11853
11854    simple-type-specifier:
11855      auto
11856      decltype ( expression )   
11857      char16_t
11858      char32_t
11859
11860    GNU Extension:
11861
11862    simple-type-specifier:
11863      __typeof__ unary-expression
11864      __typeof__ ( type-id )
11865
11866    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11867    appropriately updated.  */
11868
11869 static tree
11870 cp_parser_simple_type_specifier (cp_parser* parser,
11871                                  cp_decl_specifier_seq *decl_specs,
11872                                  cp_parser_flags flags)
11873 {
11874   tree type = NULL_TREE;
11875   cp_token *token;
11876
11877   /* Peek at the next token.  */
11878   token = cp_lexer_peek_token (parser->lexer);
11879
11880   /* If we're looking at a keyword, things are easy.  */
11881   switch (token->keyword)
11882     {
11883     case RID_CHAR:
11884       if (decl_specs)
11885         decl_specs->explicit_char_p = true;
11886       type = char_type_node;
11887       break;
11888     case RID_CHAR16:
11889       type = char16_type_node;
11890       break;
11891     case RID_CHAR32:
11892       type = char32_type_node;
11893       break;
11894     case RID_WCHAR:
11895       type = wchar_type_node;
11896       break;
11897     case RID_BOOL:
11898       type = boolean_type_node;
11899       break;
11900     case RID_SHORT:
11901       if (decl_specs)
11902         ++decl_specs->specs[(int) ds_short];
11903       type = short_integer_type_node;
11904       break;
11905     case RID_INT:
11906       if (decl_specs)
11907         decl_specs->explicit_int_p = true;
11908       type = integer_type_node;
11909       break;
11910     case RID_LONG:
11911       if (decl_specs)
11912         ++decl_specs->specs[(int) ds_long];
11913       type = long_integer_type_node;
11914       break;
11915     case RID_SIGNED:
11916       if (decl_specs)
11917         ++decl_specs->specs[(int) ds_signed];
11918       type = integer_type_node;
11919       break;
11920     case RID_UNSIGNED:
11921       if (decl_specs)
11922         ++decl_specs->specs[(int) ds_unsigned];
11923       type = unsigned_type_node;
11924       break;
11925     case RID_FLOAT:
11926       type = float_type_node;
11927       break;
11928     case RID_DOUBLE:
11929       type = double_type_node;
11930       break;
11931     case RID_VOID:
11932       type = void_type_node;
11933       break;
11934       
11935     case RID_AUTO:
11936       maybe_warn_cpp0x ("C++0x auto");
11937       type = make_auto ();
11938       break;
11939
11940     case RID_DECLTYPE:
11941       /* Parse the `decltype' type.  */
11942       type = cp_parser_decltype (parser);
11943
11944       if (decl_specs)
11945         cp_parser_set_decl_spec_type (decl_specs, type,
11946                                       token->location,
11947                                       /*user_defined_p=*/true);
11948
11949       return type;
11950
11951     case RID_TYPEOF:
11952       /* Consume the `typeof' token.  */
11953       cp_lexer_consume_token (parser->lexer);
11954       /* Parse the operand to `typeof'.  */
11955       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11956       /* If it is not already a TYPE, take its type.  */
11957       if (!TYPE_P (type))
11958         type = finish_typeof (type);
11959
11960       if (decl_specs)
11961         cp_parser_set_decl_spec_type (decl_specs, type,
11962                                       token->location,
11963                                       /*user_defined_p=*/true);
11964
11965       return type;
11966
11967     default:
11968       break;
11969     }
11970
11971   /* If the type-specifier was for a built-in type, we're done.  */
11972   if (type)
11973     {
11974       tree id;
11975
11976       /* Record the type.  */
11977       if (decl_specs
11978           && (token->keyword != RID_SIGNED
11979               && token->keyword != RID_UNSIGNED
11980               && token->keyword != RID_SHORT
11981               && token->keyword != RID_LONG))
11982         cp_parser_set_decl_spec_type (decl_specs,
11983                                       type,
11984                                       token->location,
11985                                       /*user_defined=*/false);
11986       if (decl_specs)
11987         decl_specs->any_specifiers_p = true;
11988
11989       /* Consume the token.  */
11990       id = cp_lexer_consume_token (parser->lexer)->u.value;
11991
11992       /* There is no valid C++ program where a non-template type is
11993          followed by a "<".  That usually indicates that the user thought
11994          that the type was a template.  */
11995       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11996
11997       return TYPE_NAME (type);
11998     }
11999
12000   /* The type-specifier must be a user-defined type.  */
12001   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12002     {
12003       bool qualified_p;
12004       bool global_p;
12005
12006       /* Don't gobble tokens or issue error messages if this is an
12007          optional type-specifier.  */
12008       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12009         cp_parser_parse_tentatively (parser);
12010
12011       /* Look for the optional `::' operator.  */
12012       global_p
12013         = (cp_parser_global_scope_opt (parser,
12014                                        /*current_scope_valid_p=*/false)
12015            != NULL_TREE);
12016       /* Look for the nested-name specifier.  */
12017       qualified_p
12018         = (cp_parser_nested_name_specifier_opt (parser,
12019                                                 /*typename_keyword_p=*/false,
12020                                                 /*check_dependency_p=*/true,
12021                                                 /*type_p=*/false,
12022                                                 /*is_declaration=*/false)
12023            != NULL_TREE);
12024       token = cp_lexer_peek_token (parser->lexer);
12025       /* If we have seen a nested-name-specifier, and the next token
12026          is `template', then we are using the template-id production.  */
12027       if (parser->scope
12028           && cp_parser_optional_template_keyword (parser))
12029         {
12030           /* Look for the template-id.  */
12031           type = cp_parser_template_id (parser,
12032                                         /*template_keyword_p=*/true,
12033                                         /*check_dependency_p=*/true,
12034                                         /*is_declaration=*/false);
12035           /* If the template-id did not name a type, we are out of
12036              luck.  */
12037           if (TREE_CODE (type) != TYPE_DECL)
12038             {
12039               cp_parser_error (parser, "expected template-id for type");
12040               type = NULL_TREE;
12041             }
12042         }
12043       /* Otherwise, look for a type-name.  */
12044       else
12045         type = cp_parser_type_name (parser);
12046       /* Keep track of all name-lookups performed in class scopes.  */
12047       if (type
12048           && !global_p
12049           && !qualified_p
12050           && TREE_CODE (type) == TYPE_DECL
12051           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12052         maybe_note_name_used_in_class (DECL_NAME (type), type);
12053       /* If it didn't work out, we don't have a TYPE.  */
12054       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12055           && !cp_parser_parse_definitely (parser))
12056         type = NULL_TREE;
12057       if (type && decl_specs)
12058         cp_parser_set_decl_spec_type (decl_specs, type,
12059                                       token->location,
12060                                       /*user_defined=*/true);
12061     }
12062
12063   /* If we didn't get a type-name, issue an error message.  */
12064   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12065     {
12066       cp_parser_error (parser, "expected type-name");
12067       return error_mark_node;
12068     }
12069
12070   /* There is no valid C++ program where a non-template type is
12071      followed by a "<".  That usually indicates that the user thought
12072      that the type was a template.  */
12073   if (type && type != error_mark_node)
12074     {
12075       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12076          If it is, then the '<'...'>' enclose protocol names rather than
12077          template arguments, and so everything is fine.  */
12078       if (c_dialect_objc ()
12079           && (objc_is_id (type) || objc_is_class_name (type)))
12080         {
12081           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12082           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12083
12084           /* Clobber the "unqualified" type previously entered into
12085              DECL_SPECS with the new, improved protocol-qualified version.  */
12086           if (decl_specs)
12087             decl_specs->type = qual_type;
12088
12089           return qual_type;
12090         }
12091
12092       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12093                                                token->location);
12094     }
12095
12096   return type;
12097 }
12098
12099 /* Parse a type-name.
12100
12101    type-name:
12102      class-name
12103      enum-name
12104      typedef-name
12105
12106    enum-name:
12107      identifier
12108
12109    typedef-name:
12110      identifier
12111
12112    Returns a TYPE_DECL for the type.  */
12113
12114 static tree
12115 cp_parser_type_name (cp_parser* parser)
12116 {
12117   tree type_decl;
12118
12119   /* We can't know yet whether it is a class-name or not.  */
12120   cp_parser_parse_tentatively (parser);
12121   /* Try a class-name.  */
12122   type_decl = cp_parser_class_name (parser,
12123                                     /*typename_keyword_p=*/false,
12124                                     /*template_keyword_p=*/false,
12125                                     none_type,
12126                                     /*check_dependency_p=*/true,
12127                                     /*class_head_p=*/false,
12128                                     /*is_declaration=*/false);
12129   /* If it's not a class-name, keep looking.  */
12130   if (!cp_parser_parse_definitely (parser))
12131     {
12132       /* It must be a typedef-name or an enum-name.  */
12133       return cp_parser_nonclass_name (parser);
12134     }
12135
12136   return type_decl;
12137 }
12138
12139 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12140
12141    enum-name:
12142      identifier
12143
12144    typedef-name:
12145      identifier
12146
12147    Returns a TYPE_DECL for the type.  */
12148
12149 static tree
12150 cp_parser_nonclass_name (cp_parser* parser)
12151 {
12152   tree type_decl;
12153   tree identifier;
12154
12155   cp_token *token = cp_lexer_peek_token (parser->lexer);
12156   identifier = cp_parser_identifier (parser);
12157   if (identifier == error_mark_node)
12158     return error_mark_node;
12159
12160   /* Look up the type-name.  */
12161   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12162
12163   if (TREE_CODE (type_decl) != TYPE_DECL
12164       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12165     {
12166       /* See if this is an Objective-C type.  */
12167       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12168       tree type = objc_get_protocol_qualified_type (identifier, protos);
12169       if (type)
12170         type_decl = TYPE_NAME (type);
12171     }
12172   
12173   /* Issue an error if we did not find a type-name.  */
12174   if (TREE_CODE (type_decl) != TYPE_DECL)
12175     {
12176       if (!cp_parser_simulate_error (parser))
12177         cp_parser_name_lookup_error (parser, identifier, type_decl,
12178                                      "is not a type", token->location);
12179       return error_mark_node;
12180     }
12181   /* Remember that the name was used in the definition of the
12182      current class so that we can check later to see if the
12183      meaning would have been different after the class was
12184      entirely defined.  */
12185   else if (type_decl != error_mark_node
12186            && !parser->scope)
12187     maybe_note_name_used_in_class (identifier, type_decl);
12188   
12189   return type_decl;
12190 }
12191
12192 /* Parse an elaborated-type-specifier.  Note that the grammar given
12193    here incorporates the resolution to DR68.
12194
12195    elaborated-type-specifier:
12196      class-key :: [opt] nested-name-specifier [opt] identifier
12197      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12198      enum-key :: [opt] nested-name-specifier [opt] identifier
12199      typename :: [opt] nested-name-specifier identifier
12200      typename :: [opt] nested-name-specifier template [opt]
12201        template-id
12202
12203    GNU extension:
12204
12205    elaborated-type-specifier:
12206      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12207      class-key attributes :: [opt] nested-name-specifier [opt]
12208                template [opt] template-id
12209      enum attributes :: [opt] nested-name-specifier [opt] identifier
12210
12211    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12212    declared `friend'.  If IS_DECLARATION is TRUE, then this
12213    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12214    something is being declared.
12215
12216    Returns the TYPE specified.  */
12217
12218 static tree
12219 cp_parser_elaborated_type_specifier (cp_parser* parser,
12220                                      bool is_friend,
12221                                      bool is_declaration)
12222 {
12223   enum tag_types tag_type;
12224   tree identifier;
12225   tree type = NULL_TREE;
12226   tree attributes = NULL_TREE;
12227   tree globalscope;
12228   cp_token *token = NULL;
12229
12230   /* See if we're looking at the `enum' keyword.  */
12231   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12232     {
12233       /* Consume the `enum' token.  */
12234       cp_lexer_consume_token (parser->lexer);
12235       /* Remember that it's an enumeration type.  */
12236       tag_type = enum_type;
12237       /* Parse the optional `struct' or `class' key (for C++0x scoped
12238          enums).  */
12239       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12240           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12241         {
12242           if (cxx_dialect == cxx98)
12243             maybe_warn_cpp0x ("scoped enums");
12244
12245           /* Consume the `struct' or `class'.  */
12246           cp_lexer_consume_token (parser->lexer);
12247         }
12248       /* Parse the attributes.  */
12249       attributes = cp_parser_attributes_opt (parser);
12250     }
12251   /* Or, it might be `typename'.  */
12252   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12253                                            RID_TYPENAME))
12254     {
12255       /* Consume the `typename' token.  */
12256       cp_lexer_consume_token (parser->lexer);
12257       /* Remember that it's a `typename' type.  */
12258       tag_type = typename_type;
12259     }
12260   /* Otherwise it must be a class-key.  */
12261   else
12262     {
12263       tag_type = cp_parser_class_key (parser);
12264       if (tag_type == none_type)
12265         return error_mark_node;
12266       /* Parse the attributes.  */
12267       attributes = cp_parser_attributes_opt (parser);
12268     }
12269
12270   /* Look for the `::' operator.  */
12271   globalscope =  cp_parser_global_scope_opt (parser,
12272                                              /*current_scope_valid_p=*/false);
12273   /* Look for the nested-name-specifier.  */
12274   if (tag_type == typename_type && !globalscope)
12275     {
12276       if (!cp_parser_nested_name_specifier (parser,
12277                                            /*typename_keyword_p=*/true,
12278                                            /*check_dependency_p=*/true,
12279                                            /*type_p=*/true,
12280                                             is_declaration))
12281         return error_mark_node;
12282     }
12283   else
12284     /* Even though `typename' is not present, the proposed resolution
12285        to Core Issue 180 says that in `class A<T>::B', `B' should be
12286        considered a type-name, even if `A<T>' is dependent.  */
12287     cp_parser_nested_name_specifier_opt (parser,
12288                                          /*typename_keyword_p=*/true,
12289                                          /*check_dependency_p=*/true,
12290                                          /*type_p=*/true,
12291                                          is_declaration);
12292  /* For everything but enumeration types, consider a template-id.
12293     For an enumeration type, consider only a plain identifier.  */
12294   if (tag_type != enum_type)
12295     {
12296       bool template_p = false;
12297       tree decl;
12298
12299       /* Allow the `template' keyword.  */
12300       template_p = cp_parser_optional_template_keyword (parser);
12301       /* If we didn't see `template', we don't know if there's a
12302          template-id or not.  */
12303       if (!template_p)
12304         cp_parser_parse_tentatively (parser);
12305       /* Parse the template-id.  */
12306       token = cp_lexer_peek_token (parser->lexer);
12307       decl = cp_parser_template_id (parser, template_p,
12308                                     /*check_dependency_p=*/true,
12309                                     is_declaration);
12310       /* If we didn't find a template-id, look for an ordinary
12311          identifier.  */
12312       if (!template_p && !cp_parser_parse_definitely (parser))
12313         ;
12314       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12315          in effect, then we must assume that, upon instantiation, the
12316          template will correspond to a class.  */
12317       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12318                && tag_type == typename_type)
12319         type = make_typename_type (parser->scope, decl,
12320                                    typename_type,
12321                                    /*complain=*/tf_error);
12322       /* If the `typename' keyword is in effect and DECL is not a type
12323          decl. Then type is non existant.   */
12324       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12325         type = NULL_TREE; 
12326       else 
12327         type = TREE_TYPE (decl);
12328     }
12329
12330   if (!type)
12331     {
12332       token = cp_lexer_peek_token (parser->lexer);
12333       identifier = cp_parser_identifier (parser);
12334
12335       if (identifier == error_mark_node)
12336         {
12337           parser->scope = NULL_TREE;
12338           return error_mark_node;
12339         }
12340
12341       /* For a `typename', we needn't call xref_tag.  */
12342       if (tag_type == typename_type
12343           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12344         return cp_parser_make_typename_type (parser, parser->scope,
12345                                              identifier,
12346                                              token->location);
12347       /* Look up a qualified name in the usual way.  */
12348       if (parser->scope)
12349         {
12350           tree decl;
12351           tree ambiguous_decls;
12352
12353           decl = cp_parser_lookup_name (parser, identifier,
12354                                         tag_type,
12355                                         /*is_template=*/false,
12356                                         /*is_namespace=*/false,
12357                                         /*check_dependency=*/true,
12358                                         &ambiguous_decls,
12359                                         token->location);
12360
12361           /* If the lookup was ambiguous, an error will already have been
12362              issued.  */
12363           if (ambiguous_decls)
12364             return error_mark_node;
12365
12366           /* If we are parsing friend declaration, DECL may be a
12367              TEMPLATE_DECL tree node here.  However, we need to check
12368              whether this TEMPLATE_DECL results in valid code.  Consider
12369              the following example:
12370
12371                namespace N {
12372                  template <class T> class C {};
12373                }
12374                class X {
12375                  template <class T> friend class N::C; // #1, valid code
12376                };
12377                template <class T> class Y {
12378                  friend class N::C;                    // #2, invalid code
12379                };
12380
12381              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12382              name lookup of `N::C'.  We see that friend declaration must
12383              be template for the code to be valid.  Note that
12384              processing_template_decl does not work here since it is
12385              always 1 for the above two cases.  */
12386
12387           decl = (cp_parser_maybe_treat_template_as_class
12388                   (decl, /*tag_name_p=*/is_friend
12389                          && parser->num_template_parameter_lists));
12390
12391           if (TREE_CODE (decl) != TYPE_DECL)
12392             {
12393               cp_parser_diagnose_invalid_type_name (parser,
12394                                                     parser->scope,
12395                                                     identifier,
12396                                                     token->location);
12397               return error_mark_node;
12398             }
12399
12400           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12401             {
12402               bool allow_template = (parser->num_template_parameter_lists
12403                                       || DECL_SELF_REFERENCE_P (decl));
12404               type = check_elaborated_type_specifier (tag_type, decl, 
12405                                                       allow_template);
12406
12407               if (type == error_mark_node)
12408                 return error_mark_node;
12409             }
12410
12411           /* Forward declarations of nested types, such as
12412
12413                class C1::C2;
12414                class C1::C2::C3;
12415
12416              are invalid unless all components preceding the final '::'
12417              are complete.  If all enclosing types are complete, these
12418              declarations become merely pointless.
12419
12420              Invalid forward declarations of nested types are errors
12421              caught elsewhere in parsing.  Those that are pointless arrive
12422              here.  */
12423
12424           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12425               && !is_friend && !processing_explicit_instantiation)
12426             warning (0, "declaration %qD does not declare anything", decl);
12427
12428           type = TREE_TYPE (decl);
12429         }
12430       else
12431         {
12432           /* An elaborated-type-specifier sometimes introduces a new type and
12433              sometimes names an existing type.  Normally, the rule is that it
12434              introduces a new type only if there is not an existing type of
12435              the same name already in scope.  For example, given:
12436
12437                struct S {};
12438                void f() { struct S s; }
12439
12440              the `struct S' in the body of `f' is the same `struct S' as in
12441              the global scope; the existing definition is used.  However, if
12442              there were no global declaration, this would introduce a new
12443              local class named `S'.
12444
12445              An exception to this rule applies to the following code:
12446
12447                namespace N { struct S; }
12448
12449              Here, the elaborated-type-specifier names a new type
12450              unconditionally; even if there is already an `S' in the
12451              containing scope this declaration names a new type.
12452              This exception only applies if the elaborated-type-specifier
12453              forms the complete declaration:
12454
12455                [class.name]
12456
12457                A declaration consisting solely of `class-key identifier ;' is
12458                either a redeclaration of the name in the current scope or a
12459                forward declaration of the identifier as a class name.  It
12460                introduces the name into the current scope.
12461
12462              We are in this situation precisely when the next token is a `;'.
12463
12464              An exception to the exception is that a `friend' declaration does
12465              *not* name a new type; i.e., given:
12466
12467                struct S { friend struct T; };
12468
12469              `T' is not a new type in the scope of `S'.
12470
12471              Also, `new struct S' or `sizeof (struct S)' never results in the
12472              definition of a new type; a new type can only be declared in a
12473              declaration context.  */
12474
12475           tag_scope ts;
12476           bool template_p;
12477
12478           if (is_friend)
12479             /* Friends have special name lookup rules.  */
12480             ts = ts_within_enclosing_non_class;
12481           else if (is_declaration
12482                    && cp_lexer_next_token_is (parser->lexer,
12483                                               CPP_SEMICOLON))
12484             /* This is a `class-key identifier ;' */
12485             ts = ts_current;
12486           else
12487             ts = ts_global;
12488
12489           template_p =
12490             (parser->num_template_parameter_lists
12491              && (cp_parser_next_token_starts_class_definition_p (parser)
12492                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12493           /* An unqualified name was used to reference this type, so
12494              there were no qualifying templates.  */
12495           if (!cp_parser_check_template_parameters (parser,
12496                                                     /*num_templates=*/0,
12497                                                     token->location,
12498                                                     /*declarator=*/NULL))
12499             return error_mark_node;
12500           type = xref_tag (tag_type, identifier, ts, template_p);
12501         }
12502     }
12503
12504   if (type == error_mark_node)
12505     return error_mark_node;
12506
12507   /* Allow attributes on forward declarations of classes.  */
12508   if (attributes)
12509     {
12510       if (TREE_CODE (type) == TYPENAME_TYPE)
12511         warning (OPT_Wattributes,
12512                  "attributes ignored on uninstantiated type");
12513       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12514                && ! processing_explicit_instantiation)
12515         warning (OPT_Wattributes,
12516                  "attributes ignored on template instantiation");
12517       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12518         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12519       else
12520         warning (OPT_Wattributes,
12521                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12522     }
12523
12524   if (tag_type != enum_type)
12525     cp_parser_check_class_key (tag_type, type);
12526
12527   /* A "<" cannot follow an elaborated type specifier.  If that
12528      happens, the user was probably trying to form a template-id.  */
12529   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12530
12531   return type;
12532 }
12533
12534 /* Parse an enum-specifier.
12535
12536    enum-specifier:
12537      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12538
12539    enum-key:
12540      enum
12541      enum class   [C++0x]
12542      enum struct  [C++0x]
12543
12544    enum-base:   [C++0x]
12545      : type-specifier-seq
12546
12547    GNU Extensions:
12548      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12549        { enumerator-list [opt] }attributes[opt]
12550
12551    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12552    if the token stream isn't an enum-specifier after all.  */
12553
12554 static tree
12555 cp_parser_enum_specifier (cp_parser* parser)
12556 {
12557   tree identifier;
12558   tree type;
12559   tree attributes;
12560   bool scoped_enum_p = false;
12561   bool has_underlying_type = false;
12562   tree underlying_type = NULL_TREE;
12563
12564   /* Parse tentatively so that we can back up if we don't find a
12565      enum-specifier.  */
12566   cp_parser_parse_tentatively (parser);
12567
12568   /* Caller guarantees that the current token is 'enum', an identifier
12569      possibly follows, and the token after that is an opening brace.
12570      If we don't have an identifier, fabricate an anonymous name for
12571      the enumeration being defined.  */
12572   cp_lexer_consume_token (parser->lexer);
12573
12574   /* Parse the "class" or "struct", which indicates a scoped
12575      enumeration type in C++0x.  */
12576   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12577       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12578     {
12579       if (cxx_dialect == cxx98)
12580         maybe_warn_cpp0x ("scoped enums");
12581
12582       /* Consume the `struct' or `class' token.  */
12583       cp_lexer_consume_token (parser->lexer);
12584
12585       scoped_enum_p = true;
12586     }
12587
12588   attributes = cp_parser_attributes_opt (parser);
12589
12590   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12591     identifier = cp_parser_identifier (parser);
12592   else
12593     identifier = make_anon_name ();
12594
12595   /* Check for the `:' that denotes a specified underlying type in C++0x.
12596      Note that a ':' could also indicate a bitfield width, however.  */
12597   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12598     {
12599       cp_decl_specifier_seq type_specifiers;
12600
12601       /* Consume the `:'.  */
12602       cp_lexer_consume_token (parser->lexer);
12603
12604       /* Parse the type-specifier-seq.  */
12605       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12606                                     /*is_trailing_return=*/false,
12607                                     &type_specifiers);
12608
12609       /* At this point this is surely not elaborated type specifier.  */
12610       if (!cp_parser_parse_definitely (parser))
12611         return NULL_TREE;
12612
12613       if (cxx_dialect == cxx98)
12614         maybe_warn_cpp0x ("scoped enums");
12615
12616       has_underlying_type = true;
12617
12618       /* If that didn't work, stop.  */
12619       if (type_specifiers.type != error_mark_node)
12620         {
12621           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12622                                             /*initialized=*/0, NULL);
12623           if (underlying_type == error_mark_node)
12624             underlying_type = NULL_TREE;
12625         }
12626     }
12627
12628   /* Look for the `{' but don't consume it yet.  */
12629   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12630     {
12631       cp_parser_error (parser, "expected %<{%>");
12632       if (has_underlying_type)
12633         return NULL_TREE;
12634     }
12635
12636   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12637     return NULL_TREE;
12638
12639   /* Issue an error message if type-definitions are forbidden here.  */
12640   if (!cp_parser_check_type_definition (parser))
12641     type = error_mark_node;
12642   else
12643     /* Create the new type.  We do this before consuming the opening
12644        brace so the enum will be recorded as being on the line of its
12645        tag (or the 'enum' keyword, if there is no tag).  */
12646     type = start_enum (identifier, underlying_type, scoped_enum_p);
12647   
12648   /* Consume the opening brace.  */
12649   cp_lexer_consume_token (parser->lexer);
12650
12651   if (type == error_mark_node)
12652     {
12653       cp_parser_skip_to_end_of_block_or_statement (parser);
12654       return error_mark_node;
12655     }
12656
12657   /* If the next token is not '}', then there are some enumerators.  */
12658   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12659     cp_parser_enumerator_list (parser, type);
12660
12661   /* Consume the final '}'.  */
12662   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12663
12664   /* Look for trailing attributes to apply to this enumeration, and
12665      apply them if appropriate.  */
12666   if (cp_parser_allow_gnu_extensions_p (parser))
12667     {
12668       tree trailing_attr = cp_parser_attributes_opt (parser);
12669       trailing_attr = chainon (trailing_attr, attributes);
12670       cplus_decl_attributes (&type,
12671                              trailing_attr,
12672                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12673     }
12674
12675   /* Finish up the enumeration.  */
12676   finish_enum (type);
12677
12678   return type;
12679 }
12680
12681 /* Parse an enumerator-list.  The enumerators all have the indicated
12682    TYPE.
12683
12684    enumerator-list:
12685      enumerator-definition
12686      enumerator-list , enumerator-definition  */
12687
12688 static void
12689 cp_parser_enumerator_list (cp_parser* parser, tree type)
12690 {
12691   while (true)
12692     {
12693       /* Parse an enumerator-definition.  */
12694       cp_parser_enumerator_definition (parser, type);
12695
12696       /* If the next token is not a ',', we've reached the end of
12697          the list.  */
12698       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12699         break;
12700       /* Otherwise, consume the `,' and keep going.  */
12701       cp_lexer_consume_token (parser->lexer);
12702       /* If the next token is a `}', there is a trailing comma.  */
12703       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12704         {
12705           if (!in_system_header)
12706             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12707           break;
12708         }
12709     }
12710 }
12711
12712 /* Parse an enumerator-definition.  The enumerator has the indicated
12713    TYPE.
12714
12715    enumerator-definition:
12716      enumerator
12717      enumerator = constant-expression
12718
12719    enumerator:
12720      identifier  */
12721
12722 static void
12723 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12724 {
12725   tree identifier;
12726   tree value;
12727
12728   /* Look for the identifier.  */
12729   identifier = cp_parser_identifier (parser);
12730   if (identifier == error_mark_node)
12731     return;
12732
12733   /* If the next token is an '=', then there is an explicit value.  */
12734   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12735     {
12736       /* Consume the `=' token.  */
12737       cp_lexer_consume_token (parser->lexer);
12738       /* Parse the value.  */
12739       value = cp_parser_constant_expression (parser,
12740                                              /*allow_non_constant_p=*/false,
12741                                              NULL);
12742     }
12743   else
12744     value = NULL_TREE;
12745
12746   /* If we are processing a template, make sure the initializer of the
12747      enumerator doesn't contain any bare template parameter pack.  */
12748   if (check_for_bare_parameter_packs (value))
12749     value = error_mark_node;
12750
12751   /* Create the enumerator.  */
12752   build_enumerator (identifier, value, type);
12753 }
12754
12755 /* Parse a namespace-name.
12756
12757    namespace-name:
12758      original-namespace-name
12759      namespace-alias
12760
12761    Returns the NAMESPACE_DECL for the namespace.  */
12762
12763 static tree
12764 cp_parser_namespace_name (cp_parser* parser)
12765 {
12766   tree identifier;
12767   tree namespace_decl;
12768
12769   cp_token *token = cp_lexer_peek_token (parser->lexer);
12770
12771   /* Get the name of the namespace.  */
12772   identifier = cp_parser_identifier (parser);
12773   if (identifier == error_mark_node)
12774     return error_mark_node;
12775
12776   /* Look up the identifier in the currently active scope.  Look only
12777      for namespaces, due to:
12778
12779        [basic.lookup.udir]
12780
12781        When looking up a namespace-name in a using-directive or alias
12782        definition, only namespace names are considered.
12783
12784      And:
12785
12786        [basic.lookup.qual]
12787
12788        During the lookup of a name preceding the :: scope resolution
12789        operator, object, function, and enumerator names are ignored.
12790
12791      (Note that cp_parser_qualifying_entity only calls this
12792      function if the token after the name is the scope resolution
12793      operator.)  */
12794   namespace_decl = cp_parser_lookup_name (parser, identifier,
12795                                           none_type,
12796                                           /*is_template=*/false,
12797                                           /*is_namespace=*/true,
12798                                           /*check_dependency=*/true,
12799                                           /*ambiguous_decls=*/NULL,
12800                                           token->location);
12801   /* If it's not a namespace, issue an error.  */
12802   if (namespace_decl == error_mark_node
12803       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12804     {
12805       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12806         error_at (token->location, "%qD is not a namespace-name", identifier);
12807       cp_parser_error (parser, "expected namespace-name");
12808       namespace_decl = error_mark_node;
12809     }
12810
12811   return namespace_decl;
12812 }
12813
12814 /* Parse a namespace-definition.
12815
12816    namespace-definition:
12817      named-namespace-definition
12818      unnamed-namespace-definition
12819
12820    named-namespace-definition:
12821      original-namespace-definition
12822      extension-namespace-definition
12823
12824    original-namespace-definition:
12825      namespace identifier { namespace-body }
12826
12827    extension-namespace-definition:
12828      namespace original-namespace-name { namespace-body }
12829
12830    unnamed-namespace-definition:
12831      namespace { namespace-body } */
12832
12833 static void
12834 cp_parser_namespace_definition (cp_parser* parser)
12835 {
12836   tree identifier, attribs;
12837   bool has_visibility;
12838   bool is_inline;
12839
12840   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12841     {
12842       is_inline = true;
12843       cp_lexer_consume_token (parser->lexer);
12844     }
12845   else
12846     is_inline = false;
12847
12848   /* Look for the `namespace' keyword.  */
12849   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12850
12851   /* Get the name of the namespace.  We do not attempt to distinguish
12852      between an original-namespace-definition and an
12853      extension-namespace-definition at this point.  The semantic
12854      analysis routines are responsible for that.  */
12855   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12856     identifier = cp_parser_identifier (parser);
12857   else
12858     identifier = NULL_TREE;
12859
12860   /* Parse any specified attributes.  */
12861   attribs = cp_parser_attributes_opt (parser);
12862
12863   /* Look for the `{' to start the namespace.  */
12864   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12865   /* Start the namespace.  */
12866   push_namespace (identifier);
12867
12868   /* "inline namespace" is equivalent to a stub namespace definition
12869      followed by a strong using directive.  */
12870   if (is_inline)
12871     {
12872       tree name_space = current_namespace;
12873       /* Set up namespace association.  */
12874       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12875         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12876                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12877       /* Import the contents of the inline namespace.  */
12878       pop_namespace ();
12879       do_using_directive (name_space);
12880       push_namespace (identifier);
12881     }
12882
12883   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12884
12885   /* Parse the body of the namespace.  */
12886   cp_parser_namespace_body (parser);
12887
12888 #ifdef HANDLE_PRAGMA_VISIBILITY
12889   if (has_visibility)
12890     pop_visibility (1);
12891 #endif
12892
12893   /* Finish the namespace.  */
12894   pop_namespace ();
12895   /* Look for the final `}'.  */
12896   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12897 }
12898
12899 /* Parse a namespace-body.
12900
12901    namespace-body:
12902      declaration-seq [opt]  */
12903
12904 static void
12905 cp_parser_namespace_body (cp_parser* parser)
12906 {
12907   cp_parser_declaration_seq_opt (parser);
12908 }
12909
12910 /* Parse a namespace-alias-definition.
12911
12912    namespace-alias-definition:
12913      namespace identifier = qualified-namespace-specifier ;  */
12914
12915 static void
12916 cp_parser_namespace_alias_definition (cp_parser* parser)
12917 {
12918   tree identifier;
12919   tree namespace_specifier;
12920
12921   cp_token *token = cp_lexer_peek_token (parser->lexer);
12922
12923   /* Look for the `namespace' keyword.  */
12924   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12925   /* Look for the identifier.  */
12926   identifier = cp_parser_identifier (parser);
12927   if (identifier == error_mark_node)
12928     return;
12929   /* Look for the `=' token.  */
12930   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12931       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12932     {
12933       error_at (token->location, "%<namespace%> definition is not allowed here");
12934       /* Skip the definition.  */
12935       cp_lexer_consume_token (parser->lexer);
12936       if (cp_parser_skip_to_closing_brace (parser))
12937         cp_lexer_consume_token (parser->lexer);
12938       return;
12939     }
12940   cp_parser_require (parser, CPP_EQ, "%<=%>");
12941   /* Look for the qualified-namespace-specifier.  */
12942   namespace_specifier
12943     = cp_parser_qualified_namespace_specifier (parser);
12944   /* Look for the `;' token.  */
12945   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12946
12947   /* Register the alias in the symbol table.  */
12948   do_namespace_alias (identifier, namespace_specifier);
12949 }
12950
12951 /* Parse a qualified-namespace-specifier.
12952
12953    qualified-namespace-specifier:
12954      :: [opt] nested-name-specifier [opt] namespace-name
12955
12956    Returns a NAMESPACE_DECL corresponding to the specified
12957    namespace.  */
12958
12959 static tree
12960 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12961 {
12962   /* Look for the optional `::'.  */
12963   cp_parser_global_scope_opt (parser,
12964                               /*current_scope_valid_p=*/false);
12965
12966   /* Look for the optional nested-name-specifier.  */
12967   cp_parser_nested_name_specifier_opt (parser,
12968                                        /*typename_keyword_p=*/false,
12969                                        /*check_dependency_p=*/true,
12970                                        /*type_p=*/false,
12971                                        /*is_declaration=*/true);
12972
12973   return cp_parser_namespace_name (parser);
12974 }
12975
12976 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12977    access declaration.
12978
12979    using-declaration:
12980      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12981      using :: unqualified-id ;  
12982
12983    access-declaration:
12984      qualified-id ;  
12985
12986    */
12987
12988 static bool
12989 cp_parser_using_declaration (cp_parser* parser, 
12990                              bool access_declaration_p)
12991 {
12992   cp_token *token;
12993   bool typename_p = false;
12994   bool global_scope_p;
12995   tree decl;
12996   tree identifier;
12997   tree qscope;
12998
12999   if (access_declaration_p)
13000     cp_parser_parse_tentatively (parser);
13001   else
13002     {
13003       /* Look for the `using' keyword.  */
13004       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13005       
13006       /* Peek at the next token.  */
13007       token = cp_lexer_peek_token (parser->lexer);
13008       /* See if it's `typename'.  */
13009       if (token->keyword == RID_TYPENAME)
13010         {
13011           /* Remember that we've seen it.  */
13012           typename_p = true;
13013           /* Consume the `typename' token.  */
13014           cp_lexer_consume_token (parser->lexer);
13015         }
13016     }
13017
13018   /* Look for the optional global scope qualification.  */
13019   global_scope_p
13020     = (cp_parser_global_scope_opt (parser,
13021                                    /*current_scope_valid_p=*/false)
13022        != NULL_TREE);
13023
13024   /* If we saw `typename', or didn't see `::', then there must be a
13025      nested-name-specifier present.  */
13026   if (typename_p || !global_scope_p)
13027     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13028                                               /*check_dependency_p=*/true,
13029                                               /*type_p=*/false,
13030                                               /*is_declaration=*/true);
13031   /* Otherwise, we could be in either of the two productions.  In that
13032      case, treat the nested-name-specifier as optional.  */
13033   else
13034     qscope = cp_parser_nested_name_specifier_opt (parser,
13035                                                   /*typename_keyword_p=*/false,
13036                                                   /*check_dependency_p=*/true,
13037                                                   /*type_p=*/false,
13038                                                   /*is_declaration=*/true);
13039   if (!qscope)
13040     qscope = global_namespace;
13041
13042   if (access_declaration_p && cp_parser_error_occurred (parser))
13043     /* Something has already gone wrong; there's no need to parse
13044        further.  Since an error has occurred, the return value of
13045        cp_parser_parse_definitely will be false, as required.  */
13046     return cp_parser_parse_definitely (parser);
13047
13048   token = cp_lexer_peek_token (parser->lexer);
13049   /* Parse the unqualified-id.  */
13050   identifier = cp_parser_unqualified_id (parser,
13051                                          /*template_keyword_p=*/false,
13052                                          /*check_dependency_p=*/true,
13053                                          /*declarator_p=*/true,
13054                                          /*optional_p=*/false);
13055
13056   if (access_declaration_p)
13057     {
13058       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13059         cp_parser_simulate_error (parser);
13060       if (!cp_parser_parse_definitely (parser))
13061         return false;
13062     }
13063
13064   /* The function we call to handle a using-declaration is different
13065      depending on what scope we are in.  */
13066   if (qscope == error_mark_node || identifier == error_mark_node)
13067     ;
13068   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13069            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13070     /* [namespace.udecl]
13071
13072        A using declaration shall not name a template-id.  */
13073     error_at (token->location,
13074               "a template-id may not appear in a using-declaration");
13075   else
13076     {
13077       if (at_class_scope_p ())
13078         {
13079           /* Create the USING_DECL.  */
13080           decl = do_class_using_decl (parser->scope, identifier);
13081
13082           if (check_for_bare_parameter_packs (decl))
13083             return false;
13084           else
13085             /* Add it to the list of members in this class.  */
13086             finish_member_declaration (decl);
13087         }
13088       else
13089         {
13090           decl = cp_parser_lookup_name_simple (parser,
13091                                                identifier,
13092                                                token->location);
13093           if (decl == error_mark_node)
13094             cp_parser_name_lookup_error (parser, identifier,
13095                                          decl, NULL,
13096                                          token->location);
13097           else if (check_for_bare_parameter_packs (decl))
13098             return false;
13099           else if (!at_namespace_scope_p ())
13100             do_local_using_decl (decl, qscope, identifier);
13101           else
13102             do_toplevel_using_decl (decl, qscope, identifier);
13103         }
13104     }
13105
13106   /* Look for the final `;'.  */
13107   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13108   
13109   return true;
13110 }
13111
13112 /* Parse a using-directive.
13113
13114    using-directive:
13115      using namespace :: [opt] nested-name-specifier [opt]
13116        namespace-name ;  */
13117
13118 static void
13119 cp_parser_using_directive (cp_parser* parser)
13120 {
13121   tree namespace_decl;
13122   tree attribs;
13123
13124   /* Look for the `using' keyword.  */
13125   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13126   /* And the `namespace' keyword.  */
13127   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13128   /* Look for the optional `::' operator.  */
13129   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13130   /* And the optional nested-name-specifier.  */
13131   cp_parser_nested_name_specifier_opt (parser,
13132                                        /*typename_keyword_p=*/false,
13133                                        /*check_dependency_p=*/true,
13134                                        /*type_p=*/false,
13135                                        /*is_declaration=*/true);
13136   /* Get the namespace being used.  */
13137   namespace_decl = cp_parser_namespace_name (parser);
13138   /* And any specified attributes.  */
13139   attribs = cp_parser_attributes_opt (parser);
13140   /* Update the symbol table.  */
13141   parse_using_directive (namespace_decl, attribs);
13142   /* Look for the final `;'.  */
13143   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13144 }
13145
13146 /* Parse an asm-definition.
13147
13148    asm-definition:
13149      asm ( string-literal ) ;
13150
13151    GNU Extension:
13152
13153    asm-definition:
13154      asm volatile [opt] ( string-literal ) ;
13155      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13156      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13157                           : asm-operand-list [opt] ) ;
13158      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13159                           : asm-operand-list [opt]
13160                           : asm-clobber-list [opt] ) ;
13161      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13162                                : asm-clobber-list [opt]
13163                                : asm-goto-list ) ;  */
13164
13165 static void
13166 cp_parser_asm_definition (cp_parser* parser)
13167 {
13168   tree string;
13169   tree outputs = NULL_TREE;
13170   tree inputs = NULL_TREE;
13171   tree clobbers = NULL_TREE;
13172   tree labels = NULL_TREE;
13173   tree asm_stmt;
13174   bool volatile_p = false;
13175   bool extended_p = false;
13176   bool invalid_inputs_p = false;
13177   bool invalid_outputs_p = false;
13178   bool goto_p = false;
13179   const char *missing = NULL;
13180
13181   /* Look for the `asm' keyword.  */
13182   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13183   /* See if the next token is `volatile'.  */
13184   if (cp_parser_allow_gnu_extensions_p (parser)
13185       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13186     {
13187       /* Remember that we saw the `volatile' keyword.  */
13188       volatile_p = true;
13189       /* Consume the token.  */
13190       cp_lexer_consume_token (parser->lexer);
13191     }
13192   if (cp_parser_allow_gnu_extensions_p (parser)
13193       && parser->in_function_body
13194       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13195     {
13196       /* Remember that we saw the `goto' keyword.  */
13197       goto_p = true;
13198       /* Consume the token.  */
13199       cp_lexer_consume_token (parser->lexer);
13200     }
13201   /* Look for the opening `('.  */
13202   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13203     return;
13204   /* Look for the string.  */
13205   string = cp_parser_string_literal (parser, false, false);
13206   if (string == error_mark_node)
13207     {
13208       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13209                                              /*consume_paren=*/true);
13210       return;
13211     }
13212
13213   /* If we're allowing GNU extensions, check for the extended assembly
13214      syntax.  Unfortunately, the `:' tokens need not be separated by
13215      a space in C, and so, for compatibility, we tolerate that here
13216      too.  Doing that means that we have to treat the `::' operator as
13217      two `:' tokens.  */
13218   if (cp_parser_allow_gnu_extensions_p (parser)
13219       && parser->in_function_body
13220       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13221           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13222     {
13223       bool inputs_p = false;
13224       bool clobbers_p = false;
13225       bool labels_p = false;
13226
13227       /* The extended syntax was used.  */
13228       extended_p = true;
13229
13230       /* Look for outputs.  */
13231       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13232         {
13233           /* Consume the `:'.  */
13234           cp_lexer_consume_token (parser->lexer);
13235           /* Parse the output-operands.  */
13236           if (cp_lexer_next_token_is_not (parser->lexer,
13237                                           CPP_COLON)
13238               && cp_lexer_next_token_is_not (parser->lexer,
13239                                              CPP_SCOPE)
13240               && cp_lexer_next_token_is_not (parser->lexer,
13241                                              CPP_CLOSE_PAREN)
13242               && !goto_p)
13243             outputs = cp_parser_asm_operand_list (parser);
13244
13245             if (outputs == error_mark_node)
13246               invalid_outputs_p = true;
13247         }
13248       /* If the next token is `::', there are no outputs, and the
13249          next token is the beginning of the inputs.  */
13250       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13251         /* The inputs are coming next.  */
13252         inputs_p = true;
13253
13254       /* Look for inputs.  */
13255       if (inputs_p
13256           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13257         {
13258           /* Consume the `:' or `::'.  */
13259           cp_lexer_consume_token (parser->lexer);
13260           /* Parse the output-operands.  */
13261           if (cp_lexer_next_token_is_not (parser->lexer,
13262                                           CPP_COLON)
13263               && cp_lexer_next_token_is_not (parser->lexer,
13264                                              CPP_SCOPE)
13265               && cp_lexer_next_token_is_not (parser->lexer,
13266                                              CPP_CLOSE_PAREN))
13267             inputs = cp_parser_asm_operand_list (parser);
13268
13269             if (inputs == error_mark_node)
13270               invalid_inputs_p = true;
13271         }
13272       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13273         /* The clobbers are coming next.  */
13274         clobbers_p = true;
13275
13276       /* Look for clobbers.  */
13277       if (clobbers_p
13278           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13279         {
13280           clobbers_p = true;
13281           /* Consume the `:' or `::'.  */
13282           cp_lexer_consume_token (parser->lexer);
13283           /* Parse the clobbers.  */
13284           if (cp_lexer_next_token_is_not (parser->lexer,
13285                                           CPP_COLON)
13286               && cp_lexer_next_token_is_not (parser->lexer,
13287                                              CPP_CLOSE_PAREN))
13288             clobbers = cp_parser_asm_clobber_list (parser);
13289         }
13290       else if (goto_p
13291                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13292         /* The labels are coming next.  */
13293         labels_p = true;
13294
13295       /* Look for labels.  */
13296       if (labels_p
13297           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13298         {
13299           labels_p = true;
13300           /* Consume the `:' or `::'.  */
13301           cp_lexer_consume_token (parser->lexer);
13302           /* Parse the labels.  */
13303           labels = cp_parser_asm_label_list (parser);
13304         }
13305
13306       if (goto_p && !labels_p)
13307         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13308     }
13309   else if (goto_p)
13310     missing = "%<:%> or %<::%>";
13311
13312   /* Look for the closing `)'.  */
13313   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13314                           missing ? missing : "%<)%>"))
13315     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13316                                            /*consume_paren=*/true);
13317   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13318
13319   if (!invalid_inputs_p && !invalid_outputs_p)
13320     {
13321       /* Create the ASM_EXPR.  */
13322       if (parser->in_function_body)
13323         {
13324           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13325                                       inputs, clobbers, labels);
13326           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13327           if (!extended_p)
13328             {
13329               tree temp = asm_stmt;
13330               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13331                 temp = TREE_OPERAND (temp, 0);
13332
13333               ASM_INPUT_P (temp) = 1;
13334             }
13335         }
13336       else
13337         cgraph_add_asm_node (string);
13338     }
13339 }
13340
13341 /* Declarators [gram.dcl.decl] */
13342
13343 /* Parse an init-declarator.
13344
13345    init-declarator:
13346      declarator initializer [opt]
13347
13348    GNU Extension:
13349
13350    init-declarator:
13351      declarator asm-specification [opt] attributes [opt] initializer [opt]
13352
13353    function-definition:
13354      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13355        function-body
13356      decl-specifier-seq [opt] declarator function-try-block
13357
13358    GNU Extension:
13359
13360    function-definition:
13361      __extension__ function-definition
13362
13363    The DECL_SPECIFIERS apply to this declarator.  Returns a
13364    representation of the entity declared.  If MEMBER_P is TRUE, then
13365    this declarator appears in a class scope.  The new DECL created by
13366    this declarator is returned.
13367
13368    The CHECKS are access checks that should be performed once we know
13369    what entity is being declared (and, therefore, what classes have
13370    befriended it).
13371
13372    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13373    for a function-definition here as well.  If the declarator is a
13374    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13375    be TRUE upon return.  By that point, the function-definition will
13376    have been completely parsed.
13377
13378    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13379    is FALSE.  */
13380
13381 static tree
13382 cp_parser_init_declarator (cp_parser* parser,
13383                            cp_decl_specifier_seq *decl_specifiers,
13384                            VEC (deferred_access_check,gc)* checks,
13385                            bool function_definition_allowed_p,
13386                            bool member_p,
13387                            int declares_class_or_enum,
13388                            bool* function_definition_p)
13389 {
13390   cp_token *token = NULL, *asm_spec_start_token = NULL,
13391            *attributes_start_token = NULL;
13392   cp_declarator *declarator;
13393   tree prefix_attributes;
13394   tree attributes;
13395   tree asm_specification;
13396   tree initializer;
13397   tree decl = NULL_TREE;
13398   tree scope;
13399   int is_initialized;
13400   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13401      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13402      "(...)".  */
13403   enum cpp_ttype initialization_kind;
13404   bool is_direct_init = false;
13405   bool is_non_constant_init;
13406   int ctor_dtor_or_conv_p;
13407   bool friend_p;
13408   tree pushed_scope = NULL;
13409
13410   /* Gather the attributes that were provided with the
13411      decl-specifiers.  */
13412   prefix_attributes = decl_specifiers->attributes;
13413
13414   /* Assume that this is not the declarator for a function
13415      definition.  */
13416   if (function_definition_p)
13417     *function_definition_p = false;
13418
13419   /* Defer access checks while parsing the declarator; we cannot know
13420      what names are accessible until we know what is being
13421      declared.  */
13422   resume_deferring_access_checks ();
13423
13424   /* Parse the declarator.  */
13425   token = cp_lexer_peek_token (parser->lexer);
13426   declarator
13427     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13428                             &ctor_dtor_or_conv_p,
13429                             /*parenthesized_p=*/NULL,
13430                             /*member_p=*/false);
13431   /* Gather up the deferred checks.  */
13432   stop_deferring_access_checks ();
13433
13434   /* If the DECLARATOR was erroneous, there's no need to go
13435      further.  */
13436   if (declarator == cp_error_declarator)
13437     return error_mark_node;
13438
13439   /* Check that the number of template-parameter-lists is OK.  */
13440   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13441                                                        token->location))
13442     return error_mark_node;
13443
13444   if (declares_class_or_enum & 2)
13445     cp_parser_check_for_definition_in_return_type (declarator,
13446                                                    decl_specifiers->type,
13447                                                    decl_specifiers->type_location);
13448
13449   /* Figure out what scope the entity declared by the DECLARATOR is
13450      located in.  `grokdeclarator' sometimes changes the scope, so
13451      we compute it now.  */
13452   scope = get_scope_of_declarator (declarator);
13453
13454   /* If we're allowing GNU extensions, look for an asm-specification
13455      and attributes.  */
13456   if (cp_parser_allow_gnu_extensions_p (parser))
13457     {
13458       /* Look for an asm-specification.  */
13459       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13460       asm_specification = cp_parser_asm_specification_opt (parser);
13461       /* And attributes.  */
13462       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13463       attributes = cp_parser_attributes_opt (parser);
13464     }
13465   else
13466     {
13467       asm_specification = NULL_TREE;
13468       attributes = NULL_TREE;
13469     }
13470
13471   /* Peek at the next token.  */
13472   token = cp_lexer_peek_token (parser->lexer);
13473   /* Check to see if the token indicates the start of a
13474      function-definition.  */
13475   if (function_declarator_p (declarator)
13476       && cp_parser_token_starts_function_definition_p (token))
13477     {
13478       if (!function_definition_allowed_p)
13479         {
13480           /* If a function-definition should not appear here, issue an
13481              error message.  */
13482           cp_parser_error (parser,
13483                            "a function-definition is not allowed here");
13484           return error_mark_node;
13485         }
13486       else
13487         {
13488           location_t func_brace_location
13489             = cp_lexer_peek_token (parser->lexer)->location;
13490
13491           /* Neither attributes nor an asm-specification are allowed
13492              on a function-definition.  */
13493           if (asm_specification)
13494             error_at (asm_spec_start_token->location,
13495                       "an asm-specification is not allowed "
13496                       "on a function-definition");
13497           if (attributes)
13498             error_at (attributes_start_token->location,
13499                       "attributes are not allowed on a function-definition");
13500           /* This is a function-definition.  */
13501           *function_definition_p = true;
13502
13503           /* Parse the function definition.  */
13504           if (member_p)
13505             decl = cp_parser_save_member_function_body (parser,
13506                                                         decl_specifiers,
13507                                                         declarator,
13508                                                         prefix_attributes);
13509           else
13510             decl
13511               = (cp_parser_function_definition_from_specifiers_and_declarator
13512                  (parser, decl_specifiers, prefix_attributes, declarator));
13513
13514           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13515             {
13516               /* This is where the prologue starts...  */
13517               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13518                 = func_brace_location;
13519             }
13520
13521           return decl;
13522         }
13523     }
13524
13525   /* [dcl.dcl]
13526
13527      Only in function declarations for constructors, destructors, and
13528      type conversions can the decl-specifier-seq be omitted.
13529
13530      We explicitly postpone this check past the point where we handle
13531      function-definitions because we tolerate function-definitions
13532      that are missing their return types in some modes.  */
13533   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13534     {
13535       cp_parser_error (parser,
13536                        "expected constructor, destructor, or type conversion");
13537       return error_mark_node;
13538     }
13539
13540   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13541   if (token->type == CPP_EQ
13542       || token->type == CPP_OPEN_PAREN
13543       || token->type == CPP_OPEN_BRACE)
13544     {
13545       is_initialized = SD_INITIALIZED;
13546       initialization_kind = token->type;
13547
13548       if (token->type == CPP_EQ
13549           && function_declarator_p (declarator))
13550         {
13551           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13552           if (t2->keyword == RID_DEFAULT)
13553             is_initialized = SD_DEFAULTED;
13554           else if (t2->keyword == RID_DELETE)
13555             is_initialized = SD_DELETED;
13556         }
13557     }
13558   else
13559     {
13560       /* If the init-declarator isn't initialized and isn't followed by a
13561          `,' or `;', it's not a valid init-declarator.  */
13562       if (token->type != CPP_COMMA
13563           && token->type != CPP_SEMICOLON)
13564         {
13565           cp_parser_error (parser, "expected initializer");
13566           return error_mark_node;
13567         }
13568       is_initialized = SD_UNINITIALIZED;
13569       initialization_kind = CPP_EOF;
13570     }
13571
13572   /* Because start_decl has side-effects, we should only call it if we
13573      know we're going ahead.  By this point, we know that we cannot
13574      possibly be looking at any other construct.  */
13575   cp_parser_commit_to_tentative_parse (parser);
13576
13577   /* If the decl specifiers were bad, issue an error now that we're
13578      sure this was intended to be a declarator.  Then continue
13579      declaring the variable(s), as int, to try to cut down on further
13580      errors.  */
13581   if (decl_specifiers->any_specifiers_p
13582       && decl_specifiers->type == error_mark_node)
13583     {
13584       cp_parser_error (parser, "invalid type in declaration");
13585       decl_specifiers->type = integer_type_node;
13586     }
13587
13588   /* Check to see whether or not this declaration is a friend.  */
13589   friend_p = cp_parser_friend_p (decl_specifiers);
13590
13591   /* Enter the newly declared entry in the symbol table.  If we're
13592      processing a declaration in a class-specifier, we wait until
13593      after processing the initializer.  */
13594   if (!member_p)
13595     {
13596       if (parser->in_unbraced_linkage_specification_p)
13597         decl_specifiers->storage_class = sc_extern;
13598       decl = start_decl (declarator, decl_specifiers,
13599                          is_initialized, attributes, prefix_attributes,
13600                          &pushed_scope);
13601     }
13602   else if (scope)
13603     /* Enter the SCOPE.  That way unqualified names appearing in the
13604        initializer will be looked up in SCOPE.  */
13605     pushed_scope = push_scope (scope);
13606
13607   /* Perform deferred access control checks, now that we know in which
13608      SCOPE the declared entity resides.  */
13609   if (!member_p && decl)
13610     {
13611       tree saved_current_function_decl = NULL_TREE;
13612
13613       /* If the entity being declared is a function, pretend that we
13614          are in its scope.  If it is a `friend', it may have access to
13615          things that would not otherwise be accessible.  */
13616       if (TREE_CODE (decl) == FUNCTION_DECL)
13617         {
13618           saved_current_function_decl = current_function_decl;
13619           current_function_decl = decl;
13620         }
13621
13622       /* Perform access checks for template parameters.  */
13623       cp_parser_perform_template_parameter_access_checks (checks);
13624
13625       /* Perform the access control checks for the declarator and the
13626          decl-specifiers.  */
13627       perform_deferred_access_checks ();
13628
13629       /* Restore the saved value.  */
13630       if (TREE_CODE (decl) == FUNCTION_DECL)
13631         current_function_decl = saved_current_function_decl;
13632     }
13633
13634   /* Parse the initializer.  */
13635   initializer = NULL_TREE;
13636   is_direct_init = false;
13637   is_non_constant_init = true;
13638   if (is_initialized)
13639     {
13640       if (function_declarator_p (declarator))
13641         {
13642           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13643            if (initialization_kind == CPP_EQ)
13644              initializer = cp_parser_pure_specifier (parser);
13645            else
13646              {
13647                /* If the declaration was erroneous, we don't really
13648                   know what the user intended, so just silently
13649                   consume the initializer.  */
13650                if (decl != error_mark_node)
13651                  error_at (initializer_start_token->location,
13652                            "initializer provided for function");
13653                cp_parser_skip_to_closing_parenthesis (parser,
13654                                                       /*recovering=*/true,
13655                                                       /*or_comma=*/false,
13656                                                       /*consume_paren=*/true);
13657              }
13658         }
13659       else
13660         {
13661           /* We want to record the extra mangling scope for in-class
13662              initializers of class members and initializers of static data
13663              member templates.  The former is a C++0x feature which isn't
13664              implemented yet, and I expect it will involve deferring
13665              parsing of the initializer until end of class as with default
13666              arguments.  So right here we only handle the latter.  */
13667           if (!member_p && processing_template_decl)
13668             start_lambda_scope (decl);
13669           initializer = cp_parser_initializer (parser,
13670                                                &is_direct_init,
13671                                                &is_non_constant_init);
13672           if (!member_p && processing_template_decl)
13673             finish_lambda_scope ();
13674         }
13675     }
13676
13677   /* The old parser allows attributes to appear after a parenthesized
13678      initializer.  Mark Mitchell proposed removing this functionality
13679      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13680      attributes -- but ignores them.  */
13681   if (cp_parser_allow_gnu_extensions_p (parser)
13682       && initialization_kind == CPP_OPEN_PAREN)
13683     if (cp_parser_attributes_opt (parser))
13684       warning (OPT_Wattributes,
13685                "attributes after parenthesized initializer ignored");
13686
13687   /* For an in-class declaration, use `grokfield' to create the
13688      declaration.  */
13689   if (member_p)
13690     {
13691       if (pushed_scope)
13692         {
13693           pop_scope (pushed_scope);
13694           pushed_scope = false;
13695         }
13696       decl = grokfield (declarator, decl_specifiers,
13697                         initializer, !is_non_constant_init,
13698                         /*asmspec=*/NULL_TREE,
13699                         prefix_attributes);
13700       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13701         cp_parser_save_default_args (parser, decl);
13702     }
13703
13704   /* Finish processing the declaration.  But, skip friend
13705      declarations.  */
13706   if (!friend_p && decl && decl != error_mark_node)
13707     {
13708       cp_finish_decl (decl,
13709                       initializer, !is_non_constant_init,
13710                       asm_specification,
13711                       /* If the initializer is in parentheses, then this is
13712                          a direct-initialization, which means that an
13713                          `explicit' constructor is OK.  Otherwise, an
13714                          `explicit' constructor cannot be used.  */
13715                       ((is_direct_init || !is_initialized)
13716                        ? 0 : LOOKUP_ONLYCONVERTING));
13717     }
13718   else if ((cxx_dialect != cxx98) && friend_p
13719            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13720     /* Core issue #226 (C++0x only): A default template-argument
13721        shall not be specified in a friend class template
13722        declaration. */
13723     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13724                              /*is_partial=*/0, /*is_friend_decl=*/1);
13725
13726   if (!friend_p && pushed_scope)
13727     pop_scope (pushed_scope);
13728
13729   return decl;
13730 }
13731
13732 /* Parse a declarator.
13733
13734    declarator:
13735      direct-declarator
13736      ptr-operator declarator
13737
13738    abstract-declarator:
13739      ptr-operator abstract-declarator [opt]
13740      direct-abstract-declarator
13741
13742    GNU Extensions:
13743
13744    declarator:
13745      attributes [opt] direct-declarator
13746      attributes [opt] ptr-operator declarator
13747
13748    abstract-declarator:
13749      attributes [opt] ptr-operator abstract-declarator [opt]
13750      attributes [opt] direct-abstract-declarator
13751
13752    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13753    detect constructor, destructor or conversion operators. It is set
13754    to -1 if the declarator is a name, and +1 if it is a
13755    function. Otherwise it is set to zero. Usually you just want to
13756    test for >0, but internally the negative value is used.
13757
13758    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13759    a decl-specifier-seq unless it declares a constructor, destructor,
13760    or conversion.  It might seem that we could check this condition in
13761    semantic analysis, rather than parsing, but that makes it difficult
13762    to handle something like `f()'.  We want to notice that there are
13763    no decl-specifiers, and therefore realize that this is an
13764    expression, not a declaration.)
13765
13766    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13767    the declarator is a direct-declarator of the form "(...)".
13768
13769    MEMBER_P is true iff this declarator is a member-declarator.  */
13770
13771 static cp_declarator *
13772 cp_parser_declarator (cp_parser* parser,
13773                       cp_parser_declarator_kind dcl_kind,
13774                       int* ctor_dtor_or_conv_p,
13775                       bool* parenthesized_p,
13776                       bool member_p)
13777 {
13778   cp_token *token;
13779   cp_declarator *declarator;
13780   enum tree_code code;
13781   cp_cv_quals cv_quals;
13782   tree class_type;
13783   tree attributes = NULL_TREE;
13784
13785   /* Assume this is not a constructor, destructor, or type-conversion
13786      operator.  */
13787   if (ctor_dtor_or_conv_p)
13788     *ctor_dtor_or_conv_p = 0;
13789
13790   if (cp_parser_allow_gnu_extensions_p (parser))
13791     attributes = cp_parser_attributes_opt (parser);
13792
13793   /* Peek at the next token.  */
13794   token = cp_lexer_peek_token (parser->lexer);
13795
13796   /* Check for the ptr-operator production.  */
13797   cp_parser_parse_tentatively (parser);
13798   /* Parse the ptr-operator.  */
13799   code = cp_parser_ptr_operator (parser,
13800                                  &class_type,
13801                                  &cv_quals);
13802   /* If that worked, then we have a ptr-operator.  */
13803   if (cp_parser_parse_definitely (parser))
13804     {
13805       /* If a ptr-operator was found, then this declarator was not
13806          parenthesized.  */
13807       if (parenthesized_p)
13808         *parenthesized_p = true;
13809       /* The dependent declarator is optional if we are parsing an
13810          abstract-declarator.  */
13811       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13812         cp_parser_parse_tentatively (parser);
13813
13814       /* Parse the dependent declarator.  */
13815       declarator = cp_parser_declarator (parser, dcl_kind,
13816                                          /*ctor_dtor_or_conv_p=*/NULL,
13817                                          /*parenthesized_p=*/NULL,
13818                                          /*member_p=*/false);
13819
13820       /* If we are parsing an abstract-declarator, we must handle the
13821          case where the dependent declarator is absent.  */
13822       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13823           && !cp_parser_parse_definitely (parser))
13824         declarator = NULL;
13825
13826       declarator = cp_parser_make_indirect_declarator
13827         (code, class_type, cv_quals, declarator);
13828     }
13829   /* Everything else is a direct-declarator.  */
13830   else
13831     {
13832       if (parenthesized_p)
13833         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13834                                                    CPP_OPEN_PAREN);
13835       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13836                                                 ctor_dtor_or_conv_p,
13837                                                 member_p);
13838     }
13839
13840   if (attributes && declarator && declarator != cp_error_declarator)
13841     declarator->attributes = attributes;
13842
13843   return declarator;
13844 }
13845
13846 /* Parse a direct-declarator or direct-abstract-declarator.
13847
13848    direct-declarator:
13849      declarator-id
13850      direct-declarator ( parameter-declaration-clause )
13851        cv-qualifier-seq [opt]
13852        exception-specification [opt]
13853      direct-declarator [ constant-expression [opt] ]
13854      ( declarator )
13855
13856    direct-abstract-declarator:
13857      direct-abstract-declarator [opt]
13858        ( parameter-declaration-clause )
13859        cv-qualifier-seq [opt]
13860        exception-specification [opt]
13861      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13862      ( abstract-declarator )
13863
13864    Returns a representation of the declarator.  DCL_KIND is
13865    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13866    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13867    we are parsing a direct-declarator.  It is
13868    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13869    of ambiguity we prefer an abstract declarator, as per
13870    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13871    cp_parser_declarator.  */
13872
13873 static cp_declarator *
13874 cp_parser_direct_declarator (cp_parser* parser,
13875                              cp_parser_declarator_kind dcl_kind,
13876                              int* ctor_dtor_or_conv_p,
13877                              bool member_p)
13878 {
13879   cp_token *token;
13880   cp_declarator *declarator = NULL;
13881   tree scope = NULL_TREE;
13882   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13883   bool saved_in_declarator_p = parser->in_declarator_p;
13884   bool first = true;
13885   tree pushed_scope = NULL_TREE;
13886
13887   while (true)
13888     {
13889       /* Peek at the next token.  */
13890       token = cp_lexer_peek_token (parser->lexer);
13891       if (token->type == CPP_OPEN_PAREN)
13892         {
13893           /* This is either a parameter-declaration-clause, or a
13894              parenthesized declarator. When we know we are parsing a
13895              named declarator, it must be a parenthesized declarator
13896              if FIRST is true. For instance, `(int)' is a
13897              parameter-declaration-clause, with an omitted
13898              direct-abstract-declarator. But `((*))', is a
13899              parenthesized abstract declarator. Finally, when T is a
13900              template parameter `(T)' is a
13901              parameter-declaration-clause, and not a parenthesized
13902              named declarator.
13903
13904              We first try and parse a parameter-declaration-clause,
13905              and then try a nested declarator (if FIRST is true).
13906
13907              It is not an error for it not to be a
13908              parameter-declaration-clause, even when FIRST is
13909              false. Consider,
13910
13911                int i (int);
13912                int i (3);
13913
13914              The first is the declaration of a function while the
13915              second is the definition of a variable, including its
13916              initializer.
13917
13918              Having seen only the parenthesis, we cannot know which of
13919              these two alternatives should be selected.  Even more
13920              complex are examples like:
13921
13922                int i (int (a));
13923                int i (int (3));
13924
13925              The former is a function-declaration; the latter is a
13926              variable initialization.
13927
13928              Thus again, we try a parameter-declaration-clause, and if
13929              that fails, we back out and return.  */
13930
13931           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13932             {
13933               tree params;
13934               unsigned saved_num_template_parameter_lists;
13935               bool is_declarator = false;
13936               tree t;
13937
13938               /* In a member-declarator, the only valid interpretation
13939                  of a parenthesis is the start of a
13940                  parameter-declaration-clause.  (It is invalid to
13941                  initialize a static data member with a parenthesized
13942                  initializer; only the "=" form of initialization is
13943                  permitted.)  */
13944               if (!member_p)
13945                 cp_parser_parse_tentatively (parser);
13946
13947               /* Consume the `('.  */
13948               cp_lexer_consume_token (parser->lexer);
13949               if (first)
13950                 {
13951                   /* If this is going to be an abstract declarator, we're
13952                      in a declarator and we can't have default args.  */
13953                   parser->default_arg_ok_p = false;
13954                   parser->in_declarator_p = true;
13955                 }
13956
13957               /* Inside the function parameter list, surrounding
13958                  template-parameter-lists do not apply.  */
13959               saved_num_template_parameter_lists
13960                 = parser->num_template_parameter_lists;
13961               parser->num_template_parameter_lists = 0;
13962
13963               begin_scope (sk_function_parms, NULL_TREE);
13964
13965               /* Parse the parameter-declaration-clause.  */
13966               params = cp_parser_parameter_declaration_clause (parser);
13967
13968               parser->num_template_parameter_lists
13969                 = saved_num_template_parameter_lists;
13970
13971               /* If all went well, parse the cv-qualifier-seq and the
13972                  exception-specification.  */
13973               if (member_p || cp_parser_parse_definitely (parser))
13974                 {
13975                   cp_cv_quals cv_quals;
13976                   tree exception_specification;
13977                   tree late_return;
13978
13979                   is_declarator = true;
13980
13981                   if (ctor_dtor_or_conv_p)
13982                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13983                   first = false;
13984                   /* Consume the `)'.  */
13985                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13986
13987                   /* Parse the cv-qualifier-seq.  */
13988                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13989                   /* And the exception-specification.  */
13990                   exception_specification
13991                     = cp_parser_exception_specification_opt (parser);
13992
13993                   late_return
13994                     = cp_parser_late_return_type_opt (parser);
13995
13996                   /* Create the function-declarator.  */
13997                   declarator = make_call_declarator (declarator,
13998                                                      params,
13999                                                      cv_quals,
14000                                                      exception_specification,
14001                                                      late_return);
14002                   /* Any subsequent parameter lists are to do with
14003                      return type, so are not those of the declared
14004                      function.  */
14005                   parser->default_arg_ok_p = false;
14006                 }
14007
14008               /* Remove the function parms from scope.  */
14009               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14010                 pop_binding (DECL_NAME (t), t);
14011               leave_scope();
14012
14013               if (is_declarator)
14014                 /* Repeat the main loop.  */
14015                 continue;
14016             }
14017
14018           /* If this is the first, we can try a parenthesized
14019              declarator.  */
14020           if (first)
14021             {
14022               bool saved_in_type_id_in_expr_p;
14023
14024               parser->default_arg_ok_p = saved_default_arg_ok_p;
14025               parser->in_declarator_p = saved_in_declarator_p;
14026
14027               /* Consume the `('.  */
14028               cp_lexer_consume_token (parser->lexer);
14029               /* Parse the nested declarator.  */
14030               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14031               parser->in_type_id_in_expr_p = true;
14032               declarator
14033                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14034                                         /*parenthesized_p=*/NULL,
14035                                         member_p);
14036               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14037               first = false;
14038               /* Expect a `)'.  */
14039               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14040                 declarator = cp_error_declarator;
14041               if (declarator == cp_error_declarator)
14042                 break;
14043
14044               goto handle_declarator;
14045             }
14046           /* Otherwise, we must be done.  */
14047           else
14048             break;
14049         }
14050       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14051                && token->type == CPP_OPEN_SQUARE)
14052         {
14053           /* Parse an array-declarator.  */
14054           tree bounds;
14055
14056           if (ctor_dtor_or_conv_p)
14057             *ctor_dtor_or_conv_p = 0;
14058
14059           first = false;
14060           parser->default_arg_ok_p = false;
14061           parser->in_declarator_p = true;
14062           /* Consume the `['.  */
14063           cp_lexer_consume_token (parser->lexer);
14064           /* Peek at the next token.  */
14065           token = cp_lexer_peek_token (parser->lexer);
14066           /* If the next token is `]', then there is no
14067              constant-expression.  */
14068           if (token->type != CPP_CLOSE_SQUARE)
14069             {
14070               bool non_constant_p;
14071
14072               bounds
14073                 = cp_parser_constant_expression (parser,
14074                                                  /*allow_non_constant=*/true,
14075                                                  &non_constant_p);
14076               if (!non_constant_p)
14077                 bounds = fold_non_dependent_expr (bounds);
14078               /* Normally, the array bound must be an integral constant
14079                  expression.  However, as an extension, we allow VLAs
14080                  in function scopes.  */
14081               else if (!parser->in_function_body)
14082                 {
14083                   error_at (token->location,
14084                             "array bound is not an integer constant");
14085                   bounds = error_mark_node;
14086                 }
14087               else if (processing_template_decl && !error_operand_p (bounds))
14088                 {
14089                   /* Remember this wasn't a constant-expression.  */
14090                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14091                   TREE_SIDE_EFFECTS (bounds) = 1;
14092                 }
14093             }
14094           else
14095             bounds = NULL_TREE;
14096           /* Look for the closing `]'.  */
14097           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14098             {
14099               declarator = cp_error_declarator;
14100               break;
14101             }
14102
14103           declarator = make_array_declarator (declarator, bounds);
14104         }
14105       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14106         {
14107           {
14108             tree qualifying_scope;
14109             tree unqualified_name;
14110             special_function_kind sfk;
14111             bool abstract_ok;
14112             bool pack_expansion_p = false;
14113             cp_token *declarator_id_start_token;
14114
14115             /* Parse a declarator-id */
14116             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14117             if (abstract_ok)
14118               {
14119                 cp_parser_parse_tentatively (parser);
14120
14121                 /* If we see an ellipsis, we should be looking at a
14122                    parameter pack. */
14123                 if (token->type == CPP_ELLIPSIS)
14124                   {
14125                     /* Consume the `...' */
14126                     cp_lexer_consume_token (parser->lexer);
14127
14128                     pack_expansion_p = true;
14129                   }
14130               }
14131
14132             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14133             unqualified_name
14134               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14135             qualifying_scope = parser->scope;
14136             if (abstract_ok)
14137               {
14138                 bool okay = false;
14139
14140                 if (!unqualified_name && pack_expansion_p)
14141                   {
14142                     /* Check whether an error occurred. */
14143                     okay = !cp_parser_error_occurred (parser);
14144
14145                     /* We already consumed the ellipsis to mark a
14146                        parameter pack, but we have no way to report it,
14147                        so abort the tentative parse. We will be exiting
14148                        immediately anyway. */
14149                     cp_parser_abort_tentative_parse (parser);
14150                   }
14151                 else
14152                   okay = cp_parser_parse_definitely (parser);
14153
14154                 if (!okay)
14155                   unqualified_name = error_mark_node;
14156                 else if (unqualified_name
14157                          && (qualifying_scope
14158                              || (TREE_CODE (unqualified_name)
14159                                  != IDENTIFIER_NODE)))
14160                   {
14161                     cp_parser_error (parser, "expected unqualified-id");
14162                     unqualified_name = error_mark_node;
14163                   }
14164               }
14165
14166             if (!unqualified_name)
14167               return NULL;
14168             if (unqualified_name == error_mark_node)
14169               {
14170                 declarator = cp_error_declarator;
14171                 pack_expansion_p = false;
14172                 declarator->parameter_pack_p = false;
14173                 break;
14174               }
14175
14176             if (qualifying_scope && at_namespace_scope_p ()
14177                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14178               {
14179                 /* In the declaration of a member of a template class
14180                    outside of the class itself, the SCOPE will sometimes
14181                    be a TYPENAME_TYPE.  For example, given:
14182
14183                    template <typename T>
14184                    int S<T>::R::i = 3;
14185
14186                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14187                    this context, we must resolve S<T>::R to an ordinary
14188                    type, rather than a typename type.
14189
14190                    The reason we normally avoid resolving TYPENAME_TYPEs
14191                    is that a specialization of `S' might render
14192                    `S<T>::R' not a type.  However, if `S' is
14193                    specialized, then this `i' will not be used, so there
14194                    is no harm in resolving the types here.  */
14195                 tree type;
14196
14197                 /* Resolve the TYPENAME_TYPE.  */
14198                 type = resolve_typename_type (qualifying_scope,
14199                                               /*only_current_p=*/false);
14200                 /* If that failed, the declarator is invalid.  */
14201                 if (TREE_CODE (type) == TYPENAME_TYPE)
14202                   {
14203                     if (typedef_variant_p (type))
14204                       error_at (declarator_id_start_token->location,
14205                                 "cannot define member of dependent typedef "
14206                                 "%qT", type);
14207                     else
14208                       error_at (declarator_id_start_token->location,
14209                                 "%<%T::%E%> is not a type",
14210                                 TYPE_CONTEXT (qualifying_scope),
14211                                 TYPE_IDENTIFIER (qualifying_scope));
14212                   }
14213                 qualifying_scope = type;
14214               }
14215
14216             sfk = sfk_none;
14217
14218             if (unqualified_name)
14219               {
14220                 tree class_type;
14221
14222                 if (qualifying_scope
14223                     && CLASS_TYPE_P (qualifying_scope))
14224                   class_type = qualifying_scope;
14225                 else
14226                   class_type = current_class_type;
14227
14228                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14229                   {
14230                     tree name_type = TREE_TYPE (unqualified_name);
14231                     if (class_type && same_type_p (name_type, class_type))
14232                       {
14233                         if (qualifying_scope
14234                             && CLASSTYPE_USE_TEMPLATE (name_type))
14235                           {
14236                             error_at (declarator_id_start_token->location,
14237                                       "invalid use of constructor as a template");
14238                             inform (declarator_id_start_token->location,
14239                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14240                                     "name the constructor in a qualified name",
14241                                     class_type,
14242                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14243                                     class_type, name_type);
14244                             declarator = cp_error_declarator;
14245                             break;
14246                           }
14247                         else
14248                           unqualified_name = constructor_name (class_type);
14249                       }
14250                     else
14251                       {
14252                         /* We do not attempt to print the declarator
14253                            here because we do not have enough
14254                            information about its original syntactic
14255                            form.  */
14256                         cp_parser_error (parser, "invalid declarator");
14257                         declarator = cp_error_declarator;
14258                         break;
14259                       }
14260                   }
14261
14262                 if (class_type)
14263                   {
14264                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14265                       sfk = sfk_destructor;
14266                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14267                       sfk = sfk_conversion;
14268                     else if (/* There's no way to declare a constructor
14269                                 for an anonymous type, even if the type
14270                                 got a name for linkage purposes.  */
14271                              !TYPE_WAS_ANONYMOUS (class_type)
14272                              && constructor_name_p (unqualified_name,
14273                                                     class_type))
14274                       {
14275                         unqualified_name = constructor_name (class_type);
14276                         sfk = sfk_constructor;
14277                       }
14278
14279                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14280                       *ctor_dtor_or_conv_p = -1;
14281                   }
14282               }
14283             declarator = make_id_declarator (qualifying_scope,
14284                                              unqualified_name,
14285                                              sfk);
14286             declarator->id_loc = token->location;
14287             declarator->parameter_pack_p = pack_expansion_p;
14288
14289             if (pack_expansion_p)
14290               maybe_warn_variadic_templates ();
14291           }
14292
14293         handle_declarator:;
14294           scope = get_scope_of_declarator (declarator);
14295           if (scope)
14296             /* Any names that appear after the declarator-id for a
14297                member are looked up in the containing scope.  */
14298             pushed_scope = push_scope (scope);
14299           parser->in_declarator_p = true;
14300           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14301               || (declarator && declarator->kind == cdk_id))
14302             /* Default args are only allowed on function
14303                declarations.  */
14304             parser->default_arg_ok_p = saved_default_arg_ok_p;
14305           else
14306             parser->default_arg_ok_p = false;
14307
14308           first = false;
14309         }
14310       /* We're done.  */
14311       else
14312         break;
14313     }
14314
14315   /* For an abstract declarator, we might wind up with nothing at this
14316      point.  That's an error; the declarator is not optional.  */
14317   if (!declarator)
14318     cp_parser_error (parser, "expected declarator");
14319
14320   /* If we entered a scope, we must exit it now.  */
14321   if (pushed_scope)
14322     pop_scope (pushed_scope);
14323
14324   parser->default_arg_ok_p = saved_default_arg_ok_p;
14325   parser->in_declarator_p = saved_in_declarator_p;
14326
14327   return declarator;
14328 }
14329
14330 /* Parse a ptr-operator.
14331
14332    ptr-operator:
14333      * cv-qualifier-seq [opt]
14334      &
14335      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14336
14337    GNU Extension:
14338
14339    ptr-operator:
14340      & cv-qualifier-seq [opt]
14341
14342    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14343    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14344    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14345    filled in with the TYPE containing the member.  *CV_QUALS is
14346    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14347    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14348    Note that the tree codes returned by this function have nothing
14349    to do with the types of trees that will be eventually be created
14350    to represent the pointer or reference type being parsed. They are
14351    just constants with suggestive names. */
14352 static enum tree_code
14353 cp_parser_ptr_operator (cp_parser* parser,
14354                         tree* type,
14355                         cp_cv_quals *cv_quals)
14356 {
14357   enum tree_code code = ERROR_MARK;
14358   cp_token *token;
14359
14360   /* Assume that it's not a pointer-to-member.  */
14361   *type = NULL_TREE;
14362   /* And that there are no cv-qualifiers.  */
14363   *cv_quals = TYPE_UNQUALIFIED;
14364
14365   /* Peek at the next token.  */
14366   token = cp_lexer_peek_token (parser->lexer);
14367
14368   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14369   if (token->type == CPP_MULT)
14370     code = INDIRECT_REF;
14371   else if (token->type == CPP_AND)
14372     code = ADDR_EXPR;
14373   else if ((cxx_dialect != cxx98) &&
14374            token->type == CPP_AND_AND) /* C++0x only */
14375     code = NON_LVALUE_EXPR;
14376
14377   if (code != ERROR_MARK)
14378     {
14379       /* Consume the `*', `&' or `&&'.  */
14380       cp_lexer_consume_token (parser->lexer);
14381
14382       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14383          `&', if we are allowing GNU extensions.  (The only qualifier
14384          that can legally appear after `&' is `restrict', but that is
14385          enforced during semantic analysis.  */
14386       if (code == INDIRECT_REF
14387           || cp_parser_allow_gnu_extensions_p (parser))
14388         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14389     }
14390   else
14391     {
14392       /* Try the pointer-to-member case.  */
14393       cp_parser_parse_tentatively (parser);
14394       /* Look for the optional `::' operator.  */
14395       cp_parser_global_scope_opt (parser,
14396                                   /*current_scope_valid_p=*/false);
14397       /* Look for the nested-name specifier.  */
14398       token = cp_lexer_peek_token (parser->lexer);
14399       cp_parser_nested_name_specifier (parser,
14400                                        /*typename_keyword_p=*/false,
14401                                        /*check_dependency_p=*/true,
14402                                        /*type_p=*/false,
14403                                        /*is_declaration=*/false);
14404       /* If we found it, and the next token is a `*', then we are
14405          indeed looking at a pointer-to-member operator.  */
14406       if (!cp_parser_error_occurred (parser)
14407           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14408         {
14409           /* Indicate that the `*' operator was used.  */
14410           code = INDIRECT_REF;
14411
14412           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14413             error_at (token->location, "%qD is a namespace", parser->scope);
14414           else
14415             {
14416               /* The type of which the member is a member is given by the
14417                  current SCOPE.  */
14418               *type = parser->scope;
14419               /* The next name will not be qualified.  */
14420               parser->scope = NULL_TREE;
14421               parser->qualifying_scope = NULL_TREE;
14422               parser->object_scope = NULL_TREE;
14423               /* Look for the optional cv-qualifier-seq.  */
14424               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14425             }
14426         }
14427       /* If that didn't work we don't have a ptr-operator.  */
14428       if (!cp_parser_parse_definitely (parser))
14429         cp_parser_error (parser, "expected ptr-operator");
14430     }
14431
14432   return code;
14433 }
14434
14435 /* Parse an (optional) cv-qualifier-seq.
14436
14437    cv-qualifier-seq:
14438      cv-qualifier cv-qualifier-seq [opt]
14439
14440    cv-qualifier:
14441      const
14442      volatile
14443
14444    GNU Extension:
14445
14446    cv-qualifier:
14447      __restrict__
14448
14449    Returns a bitmask representing the cv-qualifiers.  */
14450
14451 static cp_cv_quals
14452 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14453 {
14454   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14455
14456   while (true)
14457     {
14458       cp_token *token;
14459       cp_cv_quals cv_qualifier;
14460
14461       /* Peek at the next token.  */
14462       token = cp_lexer_peek_token (parser->lexer);
14463       /* See if it's a cv-qualifier.  */
14464       switch (token->keyword)
14465         {
14466         case RID_CONST:
14467           cv_qualifier = TYPE_QUAL_CONST;
14468           break;
14469
14470         case RID_VOLATILE:
14471           cv_qualifier = TYPE_QUAL_VOLATILE;
14472           break;
14473
14474         case RID_RESTRICT:
14475           cv_qualifier = TYPE_QUAL_RESTRICT;
14476           break;
14477
14478         default:
14479           cv_qualifier = TYPE_UNQUALIFIED;
14480           break;
14481         }
14482
14483       if (!cv_qualifier)
14484         break;
14485
14486       if (cv_quals & cv_qualifier)
14487         {
14488           error_at (token->location, "duplicate cv-qualifier");
14489           cp_lexer_purge_token (parser->lexer);
14490         }
14491       else
14492         {
14493           cp_lexer_consume_token (parser->lexer);
14494           cv_quals |= cv_qualifier;
14495         }
14496     }
14497
14498   return cv_quals;
14499 }
14500
14501 /* Parse a late-specified return type, if any.  This is not a separate
14502    non-terminal, but part of a function declarator, which looks like
14503
14504    -> trailing-type-specifier-seq abstract-declarator(opt)
14505
14506    Returns the type indicated by the type-id.  */
14507
14508 static tree
14509 cp_parser_late_return_type_opt (cp_parser* parser)
14510 {
14511   cp_token *token;
14512
14513   /* Peek at the next token.  */
14514   token = cp_lexer_peek_token (parser->lexer);
14515   /* A late-specified return type is indicated by an initial '->'. */
14516   if (token->type != CPP_DEREF)
14517     return NULL_TREE;
14518
14519   /* Consume the ->.  */
14520   cp_lexer_consume_token (parser->lexer);
14521
14522   return cp_parser_trailing_type_id (parser);
14523 }
14524
14525 /* Parse a declarator-id.
14526
14527    declarator-id:
14528      id-expression
14529      :: [opt] nested-name-specifier [opt] type-name
14530
14531    In the `id-expression' case, the value returned is as for
14532    cp_parser_id_expression if the id-expression was an unqualified-id.
14533    If the id-expression was a qualified-id, then a SCOPE_REF is
14534    returned.  The first operand is the scope (either a NAMESPACE_DECL
14535    or TREE_TYPE), but the second is still just a representation of an
14536    unqualified-id.  */
14537
14538 static tree
14539 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14540 {
14541   tree id;
14542   /* The expression must be an id-expression.  Assume that qualified
14543      names are the names of types so that:
14544
14545        template <class T>
14546        int S<T>::R::i = 3;
14547
14548      will work; we must treat `S<T>::R' as the name of a type.
14549      Similarly, assume that qualified names are templates, where
14550      required, so that:
14551
14552        template <class T>
14553        int S<T>::R<T>::i = 3;
14554
14555      will work, too.  */
14556   id = cp_parser_id_expression (parser,
14557                                 /*template_keyword_p=*/false,
14558                                 /*check_dependency_p=*/false,
14559                                 /*template_p=*/NULL,
14560                                 /*declarator_p=*/true,
14561                                 optional_p);
14562   if (id && BASELINK_P (id))
14563     id = BASELINK_FUNCTIONS (id);
14564   return id;
14565 }
14566
14567 /* Parse a type-id.
14568
14569    type-id:
14570      type-specifier-seq abstract-declarator [opt]
14571
14572    Returns the TYPE specified.  */
14573
14574 static tree
14575 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14576                      bool is_trailing_return)
14577 {
14578   cp_decl_specifier_seq type_specifier_seq;
14579   cp_declarator *abstract_declarator;
14580
14581   /* Parse the type-specifier-seq.  */
14582   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14583                                 is_trailing_return,
14584                                 &type_specifier_seq);
14585   if (type_specifier_seq.type == error_mark_node)
14586     return error_mark_node;
14587
14588   /* There might or might not be an abstract declarator.  */
14589   cp_parser_parse_tentatively (parser);
14590   /* Look for the declarator.  */
14591   abstract_declarator
14592     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14593                             /*parenthesized_p=*/NULL,
14594                             /*member_p=*/false);
14595   /* Check to see if there really was a declarator.  */
14596   if (!cp_parser_parse_definitely (parser))
14597     abstract_declarator = NULL;
14598
14599   if (type_specifier_seq.type
14600       && type_uses_auto (type_specifier_seq.type))
14601     {
14602       /* A type-id with type 'auto' is only ok if the abstract declarator
14603          is a function declarator with a late-specified return type.  */
14604       if (abstract_declarator
14605           && abstract_declarator->kind == cdk_function
14606           && abstract_declarator->u.function.late_return_type)
14607         /* OK */;
14608       else
14609         {
14610           error ("invalid use of %<auto%>");
14611           return error_mark_node;
14612         }
14613     }
14614   
14615   return groktypename (&type_specifier_seq, abstract_declarator,
14616                        is_template_arg);
14617 }
14618
14619 static tree cp_parser_type_id (cp_parser *parser)
14620 {
14621   return cp_parser_type_id_1 (parser, false, false);
14622 }
14623
14624 static tree cp_parser_template_type_arg (cp_parser *parser)
14625 {
14626   return cp_parser_type_id_1 (parser, true, false);
14627 }
14628
14629 static tree cp_parser_trailing_type_id (cp_parser *parser)
14630 {
14631   return cp_parser_type_id_1 (parser, false, true);
14632 }
14633
14634 /* Parse a type-specifier-seq.
14635
14636    type-specifier-seq:
14637      type-specifier type-specifier-seq [opt]
14638
14639    GNU extension:
14640
14641    type-specifier-seq:
14642      attributes type-specifier-seq [opt]
14643
14644    If IS_DECLARATION is true, we are at the start of a "condition" or
14645    exception-declaration, so we might be followed by a declarator-id.
14646
14647    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14648    i.e. we've just seen "->".
14649
14650    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14651
14652 static void
14653 cp_parser_type_specifier_seq (cp_parser* parser,
14654                               bool is_declaration,
14655                               bool is_trailing_return,
14656                               cp_decl_specifier_seq *type_specifier_seq)
14657 {
14658   bool seen_type_specifier = false;
14659   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14660   cp_token *start_token = NULL;
14661
14662   /* Clear the TYPE_SPECIFIER_SEQ.  */
14663   clear_decl_specs (type_specifier_seq);
14664
14665   /* In the context of a trailing return type, enum E { } is an
14666      elaborated-type-specifier followed by a function-body, not an
14667      enum-specifier.  */
14668   if (is_trailing_return)
14669     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14670
14671   /* Parse the type-specifiers and attributes.  */
14672   while (true)
14673     {
14674       tree type_specifier;
14675       bool is_cv_qualifier;
14676
14677       /* Check for attributes first.  */
14678       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14679         {
14680           type_specifier_seq->attributes =
14681             chainon (type_specifier_seq->attributes,
14682                      cp_parser_attributes_opt (parser));
14683           continue;
14684         }
14685
14686       /* record the token of the beginning of the type specifier seq,
14687          for error reporting purposes*/
14688      if (!start_token)
14689        start_token = cp_lexer_peek_token (parser->lexer);
14690
14691       /* Look for the type-specifier.  */
14692       type_specifier = cp_parser_type_specifier (parser,
14693                                                  flags,
14694                                                  type_specifier_seq,
14695                                                  /*is_declaration=*/false,
14696                                                  NULL,
14697                                                  &is_cv_qualifier);
14698       if (!type_specifier)
14699         {
14700           /* If the first type-specifier could not be found, this is not a
14701              type-specifier-seq at all.  */
14702           if (!seen_type_specifier)
14703             {
14704               cp_parser_error (parser, "expected type-specifier");
14705               type_specifier_seq->type = error_mark_node;
14706               return;
14707             }
14708           /* If subsequent type-specifiers could not be found, the
14709              type-specifier-seq is complete.  */
14710           break;
14711         }
14712
14713       seen_type_specifier = true;
14714       /* The standard says that a condition can be:
14715
14716             type-specifier-seq declarator = assignment-expression
14717
14718          However, given:
14719
14720            struct S {};
14721            if (int S = ...)
14722
14723          we should treat the "S" as a declarator, not as a
14724          type-specifier.  The standard doesn't say that explicitly for
14725          type-specifier-seq, but it does say that for
14726          decl-specifier-seq in an ordinary declaration.  Perhaps it
14727          would be clearer just to allow a decl-specifier-seq here, and
14728          then add a semantic restriction that if any decl-specifiers
14729          that are not type-specifiers appear, the program is invalid.  */
14730       if (is_declaration && !is_cv_qualifier)
14731         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14732     }
14733
14734   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14735 }
14736
14737 /* Parse a parameter-declaration-clause.
14738
14739    parameter-declaration-clause:
14740      parameter-declaration-list [opt] ... [opt]
14741      parameter-declaration-list , ...
14742
14743    Returns a representation for the parameter declarations.  A return
14744    value of NULL indicates a parameter-declaration-clause consisting
14745    only of an ellipsis.  */
14746
14747 static tree
14748 cp_parser_parameter_declaration_clause (cp_parser* parser)
14749 {
14750   tree parameters;
14751   cp_token *token;
14752   bool ellipsis_p;
14753   bool is_error;
14754
14755   /* Peek at the next token.  */
14756   token = cp_lexer_peek_token (parser->lexer);
14757   /* Check for trivial parameter-declaration-clauses.  */
14758   if (token->type == CPP_ELLIPSIS)
14759     {
14760       /* Consume the `...' token.  */
14761       cp_lexer_consume_token (parser->lexer);
14762       return NULL_TREE;
14763     }
14764   else if (token->type == CPP_CLOSE_PAREN)
14765     /* There are no parameters.  */
14766     {
14767 #ifndef NO_IMPLICIT_EXTERN_C
14768       if (in_system_header && current_class_type == NULL
14769           && current_lang_name == lang_name_c)
14770         return NULL_TREE;
14771       else
14772 #endif
14773         return void_list_node;
14774     }
14775   /* Check for `(void)', too, which is a special case.  */
14776   else if (token->keyword == RID_VOID
14777            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14778                == CPP_CLOSE_PAREN))
14779     {
14780       /* Consume the `void' token.  */
14781       cp_lexer_consume_token (parser->lexer);
14782       /* There are no parameters.  */
14783       return void_list_node;
14784     }
14785
14786   /* Parse the parameter-declaration-list.  */
14787   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14788   /* If a parse error occurred while parsing the
14789      parameter-declaration-list, then the entire
14790      parameter-declaration-clause is erroneous.  */
14791   if (is_error)
14792     return NULL;
14793
14794   /* Peek at the next token.  */
14795   token = cp_lexer_peek_token (parser->lexer);
14796   /* If it's a `,', the clause should terminate with an ellipsis.  */
14797   if (token->type == CPP_COMMA)
14798     {
14799       /* Consume the `,'.  */
14800       cp_lexer_consume_token (parser->lexer);
14801       /* Expect an ellipsis.  */
14802       ellipsis_p
14803         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14804     }
14805   /* It might also be `...' if the optional trailing `,' was
14806      omitted.  */
14807   else if (token->type == CPP_ELLIPSIS)
14808     {
14809       /* Consume the `...' token.  */
14810       cp_lexer_consume_token (parser->lexer);
14811       /* And remember that we saw it.  */
14812       ellipsis_p = true;
14813     }
14814   else
14815     ellipsis_p = false;
14816
14817   /* Finish the parameter list.  */
14818   if (!ellipsis_p)
14819     parameters = chainon (parameters, void_list_node);
14820
14821   return parameters;
14822 }
14823
14824 /* Parse a parameter-declaration-list.
14825
14826    parameter-declaration-list:
14827      parameter-declaration
14828      parameter-declaration-list , parameter-declaration
14829
14830    Returns a representation of the parameter-declaration-list, as for
14831    cp_parser_parameter_declaration_clause.  However, the
14832    `void_list_node' is never appended to the list.  Upon return,
14833    *IS_ERROR will be true iff an error occurred.  */
14834
14835 static tree
14836 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14837 {
14838   tree parameters = NULL_TREE;
14839   tree *tail = &parameters; 
14840   bool saved_in_unbraced_linkage_specification_p;
14841   int index = 0;
14842
14843   /* Assume all will go well.  */
14844   *is_error = false;
14845   /* The special considerations that apply to a function within an
14846      unbraced linkage specifications do not apply to the parameters
14847      to the function.  */
14848   saved_in_unbraced_linkage_specification_p 
14849     = parser->in_unbraced_linkage_specification_p;
14850   parser->in_unbraced_linkage_specification_p = false;
14851
14852   /* Look for more parameters.  */
14853   while (true)
14854     {
14855       cp_parameter_declarator *parameter;
14856       tree decl = error_mark_node;
14857       bool parenthesized_p;
14858       /* Parse the parameter.  */
14859       parameter
14860         = cp_parser_parameter_declaration (parser,
14861                                            /*template_parm_p=*/false,
14862                                            &parenthesized_p);
14863
14864       /* We don't know yet if the enclosing context is deprecated, so wait
14865          and warn in grokparms if appropriate.  */
14866       deprecated_state = DEPRECATED_SUPPRESS;
14867
14868       if (parameter)
14869         decl = grokdeclarator (parameter->declarator,
14870                                &parameter->decl_specifiers,
14871                                PARM,
14872                                parameter->default_argument != NULL_TREE,
14873                                &parameter->decl_specifiers.attributes);
14874
14875       deprecated_state = DEPRECATED_NORMAL;
14876
14877       /* If a parse error occurred parsing the parameter declaration,
14878          then the entire parameter-declaration-list is erroneous.  */
14879       if (decl == error_mark_node)
14880         {
14881           *is_error = true;
14882           parameters = error_mark_node;
14883           break;
14884         }
14885
14886       if (parameter->decl_specifiers.attributes)
14887         cplus_decl_attributes (&decl,
14888                                parameter->decl_specifiers.attributes,
14889                                0);
14890       if (DECL_NAME (decl))
14891         decl = pushdecl (decl);
14892
14893       if (decl != error_mark_node)
14894         {
14895           retrofit_lang_decl (decl);
14896           DECL_PARM_INDEX (decl) = ++index;
14897         }
14898
14899       /* Add the new parameter to the list.  */
14900       *tail = build_tree_list (parameter->default_argument, decl);
14901       tail = &TREE_CHAIN (*tail);
14902
14903       /* Peek at the next token.  */
14904       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14905           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14906           /* These are for Objective-C++ */
14907           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14908           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14909         /* The parameter-declaration-list is complete.  */
14910         break;
14911       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14912         {
14913           cp_token *token;
14914
14915           /* Peek at the next token.  */
14916           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14917           /* If it's an ellipsis, then the list is complete.  */
14918           if (token->type == CPP_ELLIPSIS)
14919             break;
14920           /* Otherwise, there must be more parameters.  Consume the
14921              `,'.  */
14922           cp_lexer_consume_token (parser->lexer);
14923           /* When parsing something like:
14924
14925                 int i(float f, double d)
14926
14927              we can tell after seeing the declaration for "f" that we
14928              are not looking at an initialization of a variable "i",
14929              but rather at the declaration of a function "i".
14930
14931              Due to the fact that the parsing of template arguments
14932              (as specified to a template-id) requires backtracking we
14933              cannot use this technique when inside a template argument
14934              list.  */
14935           if (!parser->in_template_argument_list_p
14936               && !parser->in_type_id_in_expr_p
14937               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14938               /* However, a parameter-declaration of the form
14939                  "foat(f)" (which is a valid declaration of a
14940                  parameter "f") can also be interpreted as an
14941                  expression (the conversion of "f" to "float").  */
14942               && !parenthesized_p)
14943             cp_parser_commit_to_tentative_parse (parser);
14944         }
14945       else
14946         {
14947           cp_parser_error (parser, "expected %<,%> or %<...%>");
14948           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14949             cp_parser_skip_to_closing_parenthesis (parser,
14950                                                    /*recovering=*/true,
14951                                                    /*or_comma=*/false,
14952                                                    /*consume_paren=*/false);
14953           break;
14954         }
14955     }
14956
14957   parser->in_unbraced_linkage_specification_p
14958     = saved_in_unbraced_linkage_specification_p;
14959
14960   return parameters;
14961 }
14962
14963 /* Parse a parameter declaration.
14964
14965    parameter-declaration:
14966      decl-specifier-seq ... [opt] declarator
14967      decl-specifier-seq declarator = assignment-expression
14968      decl-specifier-seq ... [opt] abstract-declarator [opt]
14969      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14970
14971    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14972    declares a template parameter.  (In that case, a non-nested `>'
14973    token encountered during the parsing of the assignment-expression
14974    is not interpreted as a greater-than operator.)
14975
14976    Returns a representation of the parameter, or NULL if an error
14977    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14978    true iff the declarator is of the form "(p)".  */
14979
14980 static cp_parameter_declarator *
14981 cp_parser_parameter_declaration (cp_parser *parser,
14982                                  bool template_parm_p,
14983                                  bool *parenthesized_p)
14984 {
14985   int declares_class_or_enum;
14986   bool greater_than_is_operator_p;
14987   cp_decl_specifier_seq decl_specifiers;
14988   cp_declarator *declarator;
14989   tree default_argument;
14990   cp_token *token = NULL, *declarator_token_start = NULL;
14991   const char *saved_message;
14992
14993   /* In a template parameter, `>' is not an operator.
14994
14995      [temp.param]
14996
14997      When parsing a default template-argument for a non-type
14998      template-parameter, the first non-nested `>' is taken as the end
14999      of the template parameter-list rather than a greater-than
15000      operator.  */
15001   greater_than_is_operator_p = !template_parm_p;
15002
15003   /* Type definitions may not appear in parameter types.  */
15004   saved_message = parser->type_definition_forbidden_message;
15005   parser->type_definition_forbidden_message
15006     = "types may not be defined in parameter types";
15007
15008   /* Parse the declaration-specifiers.  */
15009   cp_parser_decl_specifier_seq (parser,
15010                                 CP_PARSER_FLAGS_NONE,
15011                                 &decl_specifiers,
15012                                 &declares_class_or_enum);
15013
15014   /* Complain about missing 'typename' or other invalid type names.  */
15015   if (!decl_specifiers.any_type_specifiers_p)
15016     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15017
15018   /* If an error occurred, there's no reason to attempt to parse the
15019      rest of the declaration.  */
15020   if (cp_parser_error_occurred (parser))
15021     {
15022       parser->type_definition_forbidden_message = saved_message;
15023       return NULL;
15024     }
15025
15026   /* Peek at the next token.  */
15027   token = cp_lexer_peek_token (parser->lexer);
15028
15029   /* If the next token is a `)', `,', `=', `>', or `...', then there
15030      is no declarator. However, when variadic templates are enabled,
15031      there may be a declarator following `...'.  */
15032   if (token->type == CPP_CLOSE_PAREN
15033       || token->type == CPP_COMMA
15034       || token->type == CPP_EQ
15035       || token->type == CPP_GREATER)
15036     {
15037       declarator = NULL;
15038       if (parenthesized_p)
15039         *parenthesized_p = false;
15040     }
15041   /* Otherwise, there should be a declarator.  */
15042   else
15043     {
15044       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15045       parser->default_arg_ok_p = false;
15046
15047       /* After seeing a decl-specifier-seq, if the next token is not a
15048          "(", there is no possibility that the code is a valid
15049          expression.  Therefore, if parsing tentatively, we commit at
15050          this point.  */
15051       if (!parser->in_template_argument_list_p
15052           /* In an expression context, having seen:
15053
15054                (int((char ...
15055
15056              we cannot be sure whether we are looking at a
15057              function-type (taking a "char" as a parameter) or a cast
15058              of some object of type "char" to "int".  */
15059           && !parser->in_type_id_in_expr_p
15060           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15061           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15062         cp_parser_commit_to_tentative_parse (parser);
15063       /* Parse the declarator.  */
15064       declarator_token_start = token;
15065       declarator = cp_parser_declarator (parser,
15066                                          CP_PARSER_DECLARATOR_EITHER,
15067                                          /*ctor_dtor_or_conv_p=*/NULL,
15068                                          parenthesized_p,
15069                                          /*member_p=*/false);
15070       parser->default_arg_ok_p = saved_default_arg_ok_p;
15071       /* After the declarator, allow more attributes.  */
15072       decl_specifiers.attributes
15073         = chainon (decl_specifiers.attributes,
15074                    cp_parser_attributes_opt (parser));
15075     }
15076
15077   /* If the next token is an ellipsis, and we have not seen a
15078      declarator name, and the type of the declarator contains parameter
15079      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15080      a parameter pack expansion expression. Otherwise, leave the
15081      ellipsis for a C-style variadic function. */
15082   token = cp_lexer_peek_token (parser->lexer);
15083   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15084     {
15085       tree type = decl_specifiers.type;
15086
15087       if (type && DECL_P (type))
15088         type = TREE_TYPE (type);
15089
15090       if (type
15091           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15092           && declarator_can_be_parameter_pack (declarator)
15093           && (!declarator || !declarator->parameter_pack_p)
15094           && uses_parameter_packs (type))
15095         {
15096           /* Consume the `...'. */
15097           cp_lexer_consume_token (parser->lexer);
15098           maybe_warn_variadic_templates ();
15099           
15100           /* Build a pack expansion type */
15101           if (declarator)
15102             declarator->parameter_pack_p = true;
15103           else
15104             decl_specifiers.type = make_pack_expansion (type);
15105         }
15106     }
15107
15108   /* The restriction on defining new types applies only to the type
15109      of the parameter, not to the default argument.  */
15110   parser->type_definition_forbidden_message = saved_message;
15111
15112   /* If the next token is `=', then process a default argument.  */
15113   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15114     {
15115       /* Consume the `='.  */
15116       cp_lexer_consume_token (parser->lexer);
15117
15118       /* If we are defining a class, then the tokens that make up the
15119          default argument must be saved and processed later.  */
15120       if (!template_parm_p && at_class_scope_p ()
15121           && TYPE_BEING_DEFINED (current_class_type)
15122           && !LAMBDA_TYPE_P (current_class_type))
15123         {
15124           unsigned depth = 0;
15125           int maybe_template_id = 0;
15126           cp_token *first_token;
15127           cp_token *token;
15128
15129           /* Add tokens until we have processed the entire default
15130              argument.  We add the range [first_token, token).  */
15131           first_token = cp_lexer_peek_token (parser->lexer);
15132           while (true)
15133             {
15134               bool done = false;
15135
15136               /* Peek at the next token.  */
15137               token = cp_lexer_peek_token (parser->lexer);
15138               /* What we do depends on what token we have.  */
15139               switch (token->type)
15140                 {
15141                   /* In valid code, a default argument must be
15142                      immediately followed by a `,' `)', or `...'.  */
15143                 case CPP_COMMA:
15144                   if (depth == 0 && maybe_template_id)
15145                     {
15146                       /* If we've seen a '<', we might be in a
15147                          template-argument-list.  Until Core issue 325 is
15148                          resolved, we don't know how this situation ought
15149                          to be handled, so try to DTRT.  We check whether
15150                          what comes after the comma is a valid parameter
15151                          declaration list.  If it is, then the comma ends
15152                          the default argument; otherwise the default
15153                          argument continues.  */
15154                       bool error = false;
15155
15156                       /* Set ITALP so cp_parser_parameter_declaration_list
15157                          doesn't decide to commit to this parse.  */
15158                       bool saved_italp = parser->in_template_argument_list_p;
15159                       parser->in_template_argument_list_p = true;
15160
15161                       cp_parser_parse_tentatively (parser);
15162                       cp_lexer_consume_token (parser->lexer);
15163                       cp_parser_parameter_declaration_list (parser, &error);
15164                       if (!cp_parser_error_occurred (parser) && !error)
15165                         done = true;
15166                       cp_parser_abort_tentative_parse (parser);
15167
15168                       parser->in_template_argument_list_p = saved_italp;
15169                       break;
15170                     }
15171                 case CPP_CLOSE_PAREN:
15172                 case CPP_ELLIPSIS:
15173                   /* If we run into a non-nested `;', `}', or `]',
15174                      then the code is invalid -- but the default
15175                      argument is certainly over.  */
15176                 case CPP_SEMICOLON:
15177                 case CPP_CLOSE_BRACE:
15178                 case CPP_CLOSE_SQUARE:
15179                   if (depth == 0)
15180                     done = true;
15181                   /* Update DEPTH, if necessary.  */
15182                   else if (token->type == CPP_CLOSE_PAREN
15183                            || token->type == CPP_CLOSE_BRACE
15184                            || token->type == CPP_CLOSE_SQUARE)
15185                     --depth;
15186                   break;
15187
15188                 case CPP_OPEN_PAREN:
15189                 case CPP_OPEN_SQUARE:
15190                 case CPP_OPEN_BRACE:
15191                   ++depth;
15192                   break;
15193
15194                 case CPP_LESS:
15195                   if (depth == 0)
15196                     /* This might be the comparison operator, or it might
15197                        start a template argument list.  */
15198                     ++maybe_template_id;
15199                   break;
15200
15201                 case CPP_RSHIFT:
15202                   if (cxx_dialect == cxx98)
15203                     break;
15204                   /* Fall through for C++0x, which treats the `>>'
15205                      operator like two `>' tokens in certain
15206                      cases.  */
15207
15208                 case CPP_GREATER:
15209                   if (depth == 0)
15210                     {
15211                       /* This might be an operator, or it might close a
15212                          template argument list.  But if a previous '<'
15213                          started a template argument list, this will have
15214                          closed it, so we can't be in one anymore.  */
15215                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15216                       if (maybe_template_id < 0)
15217                         maybe_template_id = 0;
15218                     }
15219                   break;
15220
15221                   /* If we run out of tokens, issue an error message.  */
15222                 case CPP_EOF:
15223                 case CPP_PRAGMA_EOL:
15224                   error_at (token->location, "file ends in default argument");
15225                   done = true;
15226                   break;
15227
15228                 case CPP_NAME:
15229                 case CPP_SCOPE:
15230                   /* In these cases, we should look for template-ids.
15231                      For example, if the default argument is
15232                      `X<int, double>()', we need to do name lookup to
15233                      figure out whether or not `X' is a template; if
15234                      so, the `,' does not end the default argument.
15235
15236                      That is not yet done.  */
15237                   break;
15238
15239                 default:
15240                   break;
15241                 }
15242
15243               /* If we've reached the end, stop.  */
15244               if (done)
15245                 break;
15246
15247               /* Add the token to the token block.  */
15248               token = cp_lexer_consume_token (parser->lexer);
15249             }
15250
15251           /* Create a DEFAULT_ARG to represent the unparsed default
15252              argument.  */
15253           default_argument = make_node (DEFAULT_ARG);
15254           DEFARG_TOKENS (default_argument)
15255             = cp_token_cache_new (first_token, token);
15256           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15257         }
15258       /* Outside of a class definition, we can just parse the
15259          assignment-expression.  */
15260       else
15261         {
15262           token = cp_lexer_peek_token (parser->lexer);
15263           default_argument 
15264             = cp_parser_default_argument (parser, template_parm_p);
15265         }
15266
15267       if (!parser->default_arg_ok_p)
15268         {
15269           if (flag_permissive)
15270             warning (0, "deprecated use of default argument for parameter of non-function");
15271           else
15272             {
15273               error_at (token->location,
15274                         "default arguments are only "
15275                         "permitted for function parameters");
15276               default_argument = NULL_TREE;
15277             }
15278         }
15279       else if ((declarator && declarator->parameter_pack_p)
15280                || (decl_specifiers.type
15281                    && PACK_EXPANSION_P (decl_specifiers.type)))
15282         {
15283           /* Find the name of the parameter pack.  */     
15284           cp_declarator *id_declarator = declarator;
15285           while (id_declarator && id_declarator->kind != cdk_id)
15286             id_declarator = id_declarator->declarator;
15287           
15288           if (id_declarator && id_declarator->kind == cdk_id)
15289             error_at (declarator_token_start->location,
15290                       template_parm_p 
15291                       ? "template parameter pack %qD"
15292                       " cannot have a default argument"
15293                       : "parameter pack %qD cannot have a default argument",
15294                       id_declarator->u.id.unqualified_name);
15295           else
15296             error_at (declarator_token_start->location,
15297                       template_parm_p 
15298                       ? "template parameter pack cannot have a default argument"
15299                       : "parameter pack cannot have a default argument");
15300           
15301           default_argument = NULL_TREE;
15302         }
15303     }
15304   else
15305     default_argument = NULL_TREE;
15306
15307   return make_parameter_declarator (&decl_specifiers,
15308                                     declarator,
15309                                     default_argument);
15310 }
15311
15312 /* Parse a default argument and return it.
15313
15314    TEMPLATE_PARM_P is true if this is a default argument for a
15315    non-type template parameter.  */
15316 static tree
15317 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15318 {
15319   tree default_argument = NULL_TREE;
15320   bool saved_greater_than_is_operator_p;
15321   bool saved_local_variables_forbidden_p;
15322
15323   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15324      set correctly.  */
15325   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15326   parser->greater_than_is_operator_p = !template_parm_p;
15327   /* Local variable names (and the `this' keyword) may not
15328      appear in a default argument.  */
15329   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15330   parser->local_variables_forbidden_p = true;
15331   /* Parse the assignment-expression.  */
15332   if (template_parm_p)
15333     push_deferring_access_checks (dk_no_deferred);
15334   default_argument
15335     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15336   if (template_parm_p)
15337     pop_deferring_access_checks ();
15338   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15339   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15340
15341   return default_argument;
15342 }
15343
15344 /* Parse a function-body.
15345
15346    function-body:
15347      compound_statement  */
15348
15349 static void
15350 cp_parser_function_body (cp_parser *parser)
15351 {
15352   cp_parser_compound_statement (parser, NULL, false);
15353 }
15354
15355 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15356    true if a ctor-initializer was present.  */
15357
15358 static bool
15359 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15360 {
15361   tree body;
15362   bool ctor_initializer_p;
15363
15364   /* Begin the function body.  */
15365   body = begin_function_body ();
15366   /* Parse the optional ctor-initializer.  */
15367   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15368   /* Parse the function-body.  */
15369   cp_parser_function_body (parser);
15370   /* Finish the function body.  */
15371   finish_function_body (body);
15372
15373   return ctor_initializer_p;
15374 }
15375
15376 /* Parse an initializer.
15377
15378    initializer:
15379      = initializer-clause
15380      ( expression-list )
15381
15382    Returns an expression representing the initializer.  If no
15383    initializer is present, NULL_TREE is returned.
15384
15385    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15386    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15387    set to TRUE if there is no initializer present.  If there is an
15388    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15389    is set to true; otherwise it is set to false.  */
15390
15391 static tree
15392 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15393                        bool* non_constant_p)
15394 {
15395   cp_token *token;
15396   tree init;
15397
15398   /* Peek at the next token.  */
15399   token = cp_lexer_peek_token (parser->lexer);
15400
15401   /* Let our caller know whether or not this initializer was
15402      parenthesized.  */
15403   *is_direct_init = (token->type != CPP_EQ);
15404   /* Assume that the initializer is constant.  */
15405   *non_constant_p = false;
15406
15407   if (token->type == CPP_EQ)
15408     {
15409       /* Consume the `='.  */
15410       cp_lexer_consume_token (parser->lexer);
15411       /* Parse the initializer-clause.  */
15412       init = cp_parser_initializer_clause (parser, non_constant_p);
15413     }
15414   else if (token->type == CPP_OPEN_PAREN)
15415     {
15416       VEC(tree,gc) *vec;
15417       vec = cp_parser_parenthesized_expression_list (parser, false,
15418                                                      /*cast_p=*/false,
15419                                                      /*allow_expansion_p=*/true,
15420                                                      non_constant_p);
15421       if (vec == NULL)
15422         return error_mark_node;
15423       init = build_tree_list_vec (vec);
15424       release_tree_vector (vec);
15425     }
15426   else if (token->type == CPP_OPEN_BRACE)
15427     {
15428       maybe_warn_cpp0x ("extended initializer lists");
15429       init = cp_parser_braced_list (parser, non_constant_p);
15430       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15431     }
15432   else
15433     {
15434       /* Anything else is an error.  */
15435       cp_parser_error (parser, "expected initializer");
15436       init = error_mark_node;
15437     }
15438
15439   return init;
15440 }
15441
15442 /* Parse an initializer-clause.
15443
15444    initializer-clause:
15445      assignment-expression
15446      braced-init-list
15447
15448    Returns an expression representing the initializer.
15449
15450    If the `assignment-expression' production is used the value
15451    returned is simply a representation for the expression.
15452
15453    Otherwise, calls cp_parser_braced_list.  */
15454
15455 static tree
15456 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15457 {
15458   tree initializer;
15459
15460   /* Assume the expression is constant.  */
15461   *non_constant_p = false;
15462
15463   /* If it is not a `{', then we are looking at an
15464      assignment-expression.  */
15465   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15466     {
15467       initializer
15468         = cp_parser_constant_expression (parser,
15469                                         /*allow_non_constant_p=*/true,
15470                                         non_constant_p);
15471       if (!*non_constant_p)
15472         initializer = fold_non_dependent_expr (initializer);
15473     }
15474   else
15475     initializer = cp_parser_braced_list (parser, non_constant_p);
15476
15477   return initializer;
15478 }
15479
15480 /* Parse a brace-enclosed initializer list.
15481
15482    braced-init-list:
15483      { initializer-list , [opt] }
15484      { }
15485
15486    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15487    the elements of the initializer-list (or NULL, if the last
15488    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15489    NULL_TREE.  There is no way to detect whether or not the optional
15490    trailing `,' was provided.  NON_CONSTANT_P is as for
15491    cp_parser_initializer.  */     
15492
15493 static tree
15494 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15495 {
15496   tree initializer;
15497
15498   /* Consume the `{' token.  */
15499   cp_lexer_consume_token (parser->lexer);
15500   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15501   initializer = make_node (CONSTRUCTOR);
15502   /* If it's not a `}', then there is a non-trivial initializer.  */
15503   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15504     {
15505       /* Parse the initializer list.  */
15506       CONSTRUCTOR_ELTS (initializer)
15507         = cp_parser_initializer_list (parser, non_constant_p);
15508       /* A trailing `,' token is allowed.  */
15509       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15510         cp_lexer_consume_token (parser->lexer);
15511     }
15512   /* Now, there should be a trailing `}'.  */
15513   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15514   TREE_TYPE (initializer) = init_list_type_node;
15515   return initializer;
15516 }
15517
15518 /* Parse an initializer-list.
15519
15520    initializer-list:
15521      initializer-clause ... [opt]
15522      initializer-list , initializer-clause ... [opt]
15523
15524    GNU Extension:
15525
15526    initializer-list:
15527      identifier : initializer-clause
15528      initializer-list, identifier : initializer-clause
15529
15530    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15531    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15532    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15533    as for cp_parser_initializer.  */
15534
15535 static VEC(constructor_elt,gc) *
15536 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15537 {
15538   VEC(constructor_elt,gc) *v = NULL;
15539
15540   /* Assume all of the expressions are constant.  */
15541   *non_constant_p = false;
15542
15543   /* Parse the rest of the list.  */
15544   while (true)
15545     {
15546       cp_token *token;
15547       tree identifier;
15548       tree initializer;
15549       bool clause_non_constant_p;
15550
15551       /* If the next token is an identifier and the following one is a
15552          colon, we are looking at the GNU designated-initializer
15553          syntax.  */
15554       if (cp_parser_allow_gnu_extensions_p (parser)
15555           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15556           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15557         {
15558           /* Warn the user that they are using an extension.  */
15559           pedwarn (input_location, OPT_pedantic, 
15560                    "ISO C++ does not allow designated initializers");
15561           /* Consume the identifier.  */
15562           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15563           /* Consume the `:'.  */
15564           cp_lexer_consume_token (parser->lexer);
15565         }
15566       else
15567         identifier = NULL_TREE;
15568
15569       /* Parse the initializer.  */
15570       initializer = cp_parser_initializer_clause (parser,
15571                                                   &clause_non_constant_p);
15572       /* If any clause is non-constant, so is the entire initializer.  */
15573       if (clause_non_constant_p)
15574         *non_constant_p = true;
15575
15576       /* If we have an ellipsis, this is an initializer pack
15577          expansion.  */
15578       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15579         {
15580           /* Consume the `...'.  */
15581           cp_lexer_consume_token (parser->lexer);
15582
15583           /* Turn the initializer into an initializer expansion.  */
15584           initializer = make_pack_expansion (initializer);
15585         }
15586
15587       /* Add it to the vector.  */
15588       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15589
15590       /* If the next token is not a comma, we have reached the end of
15591          the list.  */
15592       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15593         break;
15594
15595       /* Peek at the next token.  */
15596       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15597       /* If the next token is a `}', then we're still done.  An
15598          initializer-clause can have a trailing `,' after the
15599          initializer-list and before the closing `}'.  */
15600       if (token->type == CPP_CLOSE_BRACE)
15601         break;
15602
15603       /* Consume the `,' token.  */
15604       cp_lexer_consume_token (parser->lexer);
15605     }
15606
15607   return v;
15608 }
15609
15610 /* Classes [gram.class] */
15611
15612 /* Parse a class-name.
15613
15614    class-name:
15615      identifier
15616      template-id
15617
15618    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15619    to indicate that names looked up in dependent types should be
15620    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15621    keyword has been used to indicate that the name that appears next
15622    is a template.  TAG_TYPE indicates the explicit tag given before
15623    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15624    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15625    is the class being defined in a class-head.
15626
15627    Returns the TYPE_DECL representing the class.  */
15628
15629 static tree
15630 cp_parser_class_name (cp_parser *parser,
15631                       bool typename_keyword_p,
15632                       bool template_keyword_p,
15633                       enum tag_types tag_type,
15634                       bool check_dependency_p,
15635                       bool class_head_p,
15636                       bool is_declaration)
15637 {
15638   tree decl;
15639   tree scope;
15640   bool typename_p;
15641   cp_token *token;
15642   tree identifier = NULL_TREE;
15643
15644   /* All class-names start with an identifier.  */
15645   token = cp_lexer_peek_token (parser->lexer);
15646   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15647     {
15648       cp_parser_error (parser, "expected class-name");
15649       return error_mark_node;
15650     }
15651
15652   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15653      to a template-id, so we save it here.  */
15654   scope = parser->scope;
15655   if (scope == error_mark_node)
15656     return error_mark_node;
15657
15658   /* Any name names a type if we're following the `typename' keyword
15659      in a qualified name where the enclosing scope is type-dependent.  */
15660   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15661                 && dependent_type_p (scope));
15662   /* Handle the common case (an identifier, but not a template-id)
15663      efficiently.  */
15664   if (token->type == CPP_NAME
15665       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15666     {
15667       cp_token *identifier_token;
15668       bool ambiguous_p;
15669
15670       /* Look for the identifier.  */
15671       identifier_token = cp_lexer_peek_token (parser->lexer);
15672       ambiguous_p = identifier_token->ambiguous_p;
15673       identifier = cp_parser_identifier (parser);
15674       /* If the next token isn't an identifier, we are certainly not
15675          looking at a class-name.  */
15676       if (identifier == error_mark_node)
15677         decl = error_mark_node;
15678       /* If we know this is a type-name, there's no need to look it
15679          up.  */
15680       else if (typename_p)
15681         decl = identifier;
15682       else
15683         {
15684           tree ambiguous_decls;
15685           /* If we already know that this lookup is ambiguous, then
15686              we've already issued an error message; there's no reason
15687              to check again.  */
15688           if (ambiguous_p)
15689             {
15690               cp_parser_simulate_error (parser);
15691               return error_mark_node;
15692             }
15693           /* If the next token is a `::', then the name must be a type
15694              name.
15695
15696              [basic.lookup.qual]
15697
15698              During the lookup for a name preceding the :: scope
15699              resolution operator, object, function, and enumerator
15700              names are ignored.  */
15701           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15702             tag_type = typename_type;
15703           /* Look up the name.  */
15704           decl = cp_parser_lookup_name (parser, identifier,
15705                                         tag_type,
15706                                         /*is_template=*/false,
15707                                         /*is_namespace=*/false,
15708                                         check_dependency_p,
15709                                         &ambiguous_decls,
15710                                         identifier_token->location);
15711           if (ambiguous_decls)
15712             {
15713               error_at (identifier_token->location,
15714                         "reference to %qD is ambiguous", identifier);
15715               print_candidates (ambiguous_decls);
15716               if (cp_parser_parsing_tentatively (parser))
15717                 {
15718                   identifier_token->ambiguous_p = true;
15719                   cp_parser_simulate_error (parser);
15720                 }
15721               return error_mark_node;
15722             }
15723         }
15724     }
15725   else
15726     {
15727       /* Try a template-id.  */
15728       decl = cp_parser_template_id (parser, template_keyword_p,
15729                                     check_dependency_p,
15730                                     is_declaration);
15731       if (decl == error_mark_node)
15732         return error_mark_node;
15733     }
15734
15735   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15736
15737   /* If this is a typename, create a TYPENAME_TYPE.  */
15738   if (typename_p && decl != error_mark_node)
15739     {
15740       decl = make_typename_type (scope, decl, typename_type,
15741                                  /*complain=*/tf_error);
15742       if (decl != error_mark_node)
15743         decl = TYPE_NAME (decl);
15744     }
15745
15746   /* Check to see that it is really the name of a class.  */
15747   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15748       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15749       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15750     /* Situations like this:
15751
15752          template <typename T> struct A {
15753            typename T::template X<int>::I i;
15754          };
15755
15756        are problematic.  Is `T::template X<int>' a class-name?  The
15757        standard does not seem to be definitive, but there is no other
15758        valid interpretation of the following `::'.  Therefore, those
15759        names are considered class-names.  */
15760     {
15761       decl = make_typename_type (scope, decl, tag_type, tf_error);
15762       if (decl != error_mark_node)
15763         decl = TYPE_NAME (decl);
15764     }
15765   else if (TREE_CODE (decl) != TYPE_DECL
15766            || TREE_TYPE (decl) == error_mark_node
15767            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15768     decl = error_mark_node;
15769
15770   if (decl == error_mark_node)
15771     cp_parser_error (parser, "expected class-name");
15772   else if (identifier && !parser->scope)
15773     maybe_note_name_used_in_class (identifier, decl);
15774
15775   return decl;
15776 }
15777
15778 /* Parse a class-specifier.
15779
15780    class-specifier:
15781      class-head { member-specification [opt] }
15782
15783    Returns the TREE_TYPE representing the class.  */
15784
15785 static tree
15786 cp_parser_class_specifier (cp_parser* parser)
15787 {
15788   tree type;
15789   tree attributes = NULL_TREE;
15790   bool nested_name_specifier_p;
15791   unsigned saved_num_template_parameter_lists;
15792   bool saved_in_function_body;
15793   bool saved_in_unbraced_linkage_specification_p;
15794   tree old_scope = NULL_TREE;
15795   tree scope = NULL_TREE;
15796   tree bases;
15797
15798   push_deferring_access_checks (dk_no_deferred);
15799
15800   /* Parse the class-head.  */
15801   type = cp_parser_class_head (parser,
15802                                &nested_name_specifier_p,
15803                                &attributes,
15804                                &bases);
15805   /* If the class-head was a semantic disaster, skip the entire body
15806      of the class.  */
15807   if (!type)
15808     {
15809       cp_parser_skip_to_end_of_block_or_statement (parser);
15810       pop_deferring_access_checks ();
15811       return error_mark_node;
15812     }
15813
15814   /* Look for the `{'.  */
15815   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15816     {
15817       pop_deferring_access_checks ();
15818       return error_mark_node;
15819     }
15820
15821   /* Process the base classes. If they're invalid, skip the 
15822      entire class body.  */
15823   if (!xref_basetypes (type, bases))
15824     {
15825       /* Consuming the closing brace yields better error messages
15826          later on.  */
15827       if (cp_parser_skip_to_closing_brace (parser))
15828         cp_lexer_consume_token (parser->lexer);
15829       pop_deferring_access_checks ();
15830       return error_mark_node;
15831     }
15832
15833   /* Issue an error message if type-definitions are forbidden here.  */
15834   cp_parser_check_type_definition (parser);
15835   /* Remember that we are defining one more class.  */
15836   ++parser->num_classes_being_defined;
15837   /* Inside the class, surrounding template-parameter-lists do not
15838      apply.  */
15839   saved_num_template_parameter_lists
15840     = parser->num_template_parameter_lists;
15841   parser->num_template_parameter_lists = 0;
15842   /* We are not in a function body.  */
15843   saved_in_function_body = parser->in_function_body;
15844   parser->in_function_body = false;
15845   /* We are not immediately inside an extern "lang" block.  */
15846   saved_in_unbraced_linkage_specification_p
15847     = parser->in_unbraced_linkage_specification_p;
15848   parser->in_unbraced_linkage_specification_p = false;
15849
15850   /* Start the class.  */
15851   if (nested_name_specifier_p)
15852     {
15853       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15854       old_scope = push_inner_scope (scope);
15855     }
15856   type = begin_class_definition (type, attributes);
15857
15858   if (type == error_mark_node)
15859     /* If the type is erroneous, skip the entire body of the class.  */
15860     cp_parser_skip_to_closing_brace (parser);
15861   else
15862     /* Parse the member-specification.  */
15863     cp_parser_member_specification_opt (parser);
15864
15865   /* Look for the trailing `}'.  */
15866   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15867   /* Look for trailing attributes to apply to this class.  */
15868   if (cp_parser_allow_gnu_extensions_p (parser))
15869     attributes = cp_parser_attributes_opt (parser);
15870   if (type != error_mark_node)
15871     type = finish_struct (type, attributes);
15872   if (nested_name_specifier_p)
15873     pop_inner_scope (old_scope, scope);
15874   /* If this class is not itself within the scope of another class,
15875      then we need to parse the bodies of all of the queued function
15876      definitions.  Note that the queued functions defined in a class
15877      are not always processed immediately following the
15878      class-specifier for that class.  Consider:
15879
15880        struct A {
15881          struct B { void f() { sizeof (A); } };
15882        };
15883
15884      If `f' were processed before the processing of `A' were
15885      completed, there would be no way to compute the size of `A'.
15886      Note that the nesting we are interested in here is lexical --
15887      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15888      for:
15889
15890        struct A { struct B; };
15891        struct A::B { void f() { } };
15892
15893      there is no need to delay the parsing of `A::B::f'.  */
15894   if (--parser->num_classes_being_defined == 0)
15895     {
15896       tree queue_entry;
15897       tree fn;
15898       tree class_type = NULL_TREE;
15899       tree pushed_scope = NULL_TREE;
15900
15901       /* In a first pass, parse default arguments to the functions.
15902          Then, in a second pass, parse the bodies of the functions.
15903          This two-phased approach handles cases like:
15904
15905             struct S {
15906               void f() { g(); }
15907               void g(int i = 3);
15908             };
15909
15910          */
15911       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15912              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15913            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15914            TREE_PURPOSE (parser->unparsed_functions_queues)
15915              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15916         {
15917           fn = TREE_VALUE (queue_entry);
15918           /* If there are default arguments that have not yet been processed,
15919              take care of them now.  */
15920           if (class_type != TREE_PURPOSE (queue_entry))
15921             {
15922               if (pushed_scope)
15923                 pop_scope (pushed_scope);
15924               class_type = TREE_PURPOSE (queue_entry);
15925               pushed_scope = push_scope (class_type);
15926             }
15927           /* Make sure that any template parameters are in scope.  */
15928           maybe_begin_member_template_processing (fn);
15929           /* Parse the default argument expressions.  */
15930           cp_parser_late_parsing_default_args (parser, fn);
15931           /* Remove any template parameters from the symbol table.  */
15932           maybe_end_member_template_processing ();
15933         }
15934       if (pushed_scope)
15935         pop_scope (pushed_scope);
15936       /* Now parse the body of the functions.  */
15937       for (TREE_VALUE (parser->unparsed_functions_queues)
15938              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15939            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15940            TREE_VALUE (parser->unparsed_functions_queues)
15941              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15942         {
15943           /* Figure out which function we need to process.  */
15944           fn = TREE_VALUE (queue_entry);
15945           /* Parse the function.  */
15946           cp_parser_late_parsing_for_member (parser, fn);
15947         }
15948     }
15949
15950   /* Put back any saved access checks.  */
15951   pop_deferring_access_checks ();
15952
15953   /* Restore saved state.  */
15954   parser->in_function_body = saved_in_function_body;
15955   parser->num_template_parameter_lists
15956     = saved_num_template_parameter_lists;
15957   parser->in_unbraced_linkage_specification_p
15958     = saved_in_unbraced_linkage_specification_p;
15959
15960   return type;
15961 }
15962
15963 /* Parse a class-head.
15964
15965    class-head:
15966      class-key identifier [opt] base-clause [opt]
15967      class-key nested-name-specifier identifier base-clause [opt]
15968      class-key nested-name-specifier [opt] template-id
15969        base-clause [opt]
15970
15971    GNU Extensions:
15972      class-key attributes identifier [opt] base-clause [opt]
15973      class-key attributes nested-name-specifier identifier base-clause [opt]
15974      class-key attributes nested-name-specifier [opt] template-id
15975        base-clause [opt]
15976
15977    Upon return BASES is initialized to the list of base classes (or
15978    NULL, if there are none) in the same form returned by
15979    cp_parser_base_clause.
15980
15981    Returns the TYPE of the indicated class.  Sets
15982    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15983    involving a nested-name-specifier was used, and FALSE otherwise.
15984
15985    Returns error_mark_node if this is not a class-head.
15986
15987    Returns NULL_TREE if the class-head is syntactically valid, but
15988    semantically invalid in a way that means we should skip the entire
15989    body of the class.  */
15990
15991 static tree
15992 cp_parser_class_head (cp_parser* parser,
15993                       bool* nested_name_specifier_p,
15994                       tree *attributes_p,
15995                       tree *bases)
15996 {
15997   tree nested_name_specifier;
15998   enum tag_types class_key;
15999   tree id = NULL_TREE;
16000   tree type = NULL_TREE;
16001   tree attributes;
16002   bool template_id_p = false;
16003   bool qualified_p = false;
16004   bool invalid_nested_name_p = false;
16005   bool invalid_explicit_specialization_p = false;
16006   tree pushed_scope = NULL_TREE;
16007   unsigned num_templates;
16008   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16009   /* Assume no nested-name-specifier will be present.  */
16010   *nested_name_specifier_p = false;
16011   /* Assume no template parameter lists will be used in defining the
16012      type.  */
16013   num_templates = 0;
16014
16015   *bases = NULL_TREE;
16016
16017   /* Look for the class-key.  */
16018   class_key = cp_parser_class_key (parser);
16019   if (class_key == none_type)
16020     return error_mark_node;
16021
16022   /* Parse the attributes.  */
16023   attributes = cp_parser_attributes_opt (parser);
16024
16025   /* If the next token is `::', that is invalid -- but sometimes
16026      people do try to write:
16027
16028        struct ::S {};
16029
16030      Handle this gracefully by accepting the extra qualifier, and then
16031      issuing an error about it later if this really is a
16032      class-head.  If it turns out just to be an elaborated type
16033      specifier, remain silent.  */
16034   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16035     qualified_p = true;
16036
16037   push_deferring_access_checks (dk_no_check);
16038
16039   /* Determine the name of the class.  Begin by looking for an
16040      optional nested-name-specifier.  */
16041   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16042   nested_name_specifier
16043     = cp_parser_nested_name_specifier_opt (parser,
16044                                            /*typename_keyword_p=*/false,
16045                                            /*check_dependency_p=*/false,
16046                                            /*type_p=*/false,
16047                                            /*is_declaration=*/false);
16048   /* If there was a nested-name-specifier, then there *must* be an
16049      identifier.  */
16050   if (nested_name_specifier)
16051     {
16052       type_start_token = cp_lexer_peek_token (parser->lexer);
16053       /* Although the grammar says `identifier', it really means
16054          `class-name' or `template-name'.  You are only allowed to
16055          define a class that has already been declared with this
16056          syntax.
16057
16058          The proposed resolution for Core Issue 180 says that wherever
16059          you see `class T::X' you should treat `X' as a type-name.
16060
16061          It is OK to define an inaccessible class; for example:
16062
16063            class A { class B; };
16064            class A::B {};
16065
16066          We do not know if we will see a class-name, or a
16067          template-name.  We look for a class-name first, in case the
16068          class-name is a template-id; if we looked for the
16069          template-name first we would stop after the template-name.  */
16070       cp_parser_parse_tentatively (parser);
16071       type = cp_parser_class_name (parser,
16072                                    /*typename_keyword_p=*/false,
16073                                    /*template_keyword_p=*/false,
16074                                    class_type,
16075                                    /*check_dependency_p=*/false,
16076                                    /*class_head_p=*/true,
16077                                    /*is_declaration=*/false);
16078       /* If that didn't work, ignore the nested-name-specifier.  */
16079       if (!cp_parser_parse_definitely (parser))
16080         {
16081           invalid_nested_name_p = true;
16082           type_start_token = cp_lexer_peek_token (parser->lexer);
16083           id = cp_parser_identifier (parser);
16084           if (id == error_mark_node)
16085             id = NULL_TREE;
16086         }
16087       /* If we could not find a corresponding TYPE, treat this
16088          declaration like an unqualified declaration.  */
16089       if (type == error_mark_node)
16090         nested_name_specifier = NULL_TREE;
16091       /* Otherwise, count the number of templates used in TYPE and its
16092          containing scopes.  */
16093       else
16094         {
16095           tree scope;
16096
16097           for (scope = TREE_TYPE (type);
16098                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16099                scope = (TYPE_P (scope)
16100                         ? TYPE_CONTEXT (scope)
16101                         : DECL_CONTEXT (scope)))
16102             if (TYPE_P (scope)
16103                 && CLASS_TYPE_P (scope)
16104                 && CLASSTYPE_TEMPLATE_INFO (scope)
16105                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16106                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16107               ++num_templates;
16108         }
16109     }
16110   /* Otherwise, the identifier is optional.  */
16111   else
16112     {
16113       /* We don't know whether what comes next is a template-id,
16114          an identifier, or nothing at all.  */
16115       cp_parser_parse_tentatively (parser);
16116       /* Check for a template-id.  */
16117       type_start_token = cp_lexer_peek_token (parser->lexer);
16118       id = cp_parser_template_id (parser,
16119                                   /*template_keyword_p=*/false,
16120                                   /*check_dependency_p=*/true,
16121                                   /*is_declaration=*/true);
16122       /* If that didn't work, it could still be an identifier.  */
16123       if (!cp_parser_parse_definitely (parser))
16124         {
16125           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16126             {
16127               type_start_token = cp_lexer_peek_token (parser->lexer);
16128               id = cp_parser_identifier (parser);
16129             }
16130           else
16131             id = NULL_TREE;
16132         }
16133       else
16134         {
16135           template_id_p = true;
16136           ++num_templates;
16137         }
16138     }
16139
16140   pop_deferring_access_checks ();
16141
16142   if (id)
16143     cp_parser_check_for_invalid_template_id (parser, id,
16144                                              type_start_token->location);
16145
16146   /* If it's not a `:' or a `{' then we can't really be looking at a
16147      class-head, since a class-head only appears as part of a
16148      class-specifier.  We have to detect this situation before calling
16149      xref_tag, since that has irreversible side-effects.  */
16150   if (!cp_parser_next_token_starts_class_definition_p (parser))
16151     {
16152       cp_parser_error (parser, "expected %<{%> or %<:%>");
16153       return error_mark_node;
16154     }
16155
16156   /* At this point, we're going ahead with the class-specifier, even
16157      if some other problem occurs.  */
16158   cp_parser_commit_to_tentative_parse (parser);
16159   /* Issue the error about the overly-qualified name now.  */
16160   if (qualified_p)
16161     {
16162       cp_parser_error (parser,
16163                        "global qualification of class name is invalid");
16164       return error_mark_node;
16165     }
16166   else if (invalid_nested_name_p)
16167     {
16168       cp_parser_error (parser,
16169                        "qualified name does not name a class");
16170       return error_mark_node;
16171     }
16172   else if (nested_name_specifier)
16173     {
16174       tree scope;
16175
16176       /* Reject typedef-names in class heads.  */
16177       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16178         {
16179           error_at (type_start_token->location,
16180                     "invalid class name in declaration of %qD",
16181                     type);
16182           type = NULL_TREE;
16183           goto done;
16184         }
16185
16186       /* Figure out in what scope the declaration is being placed.  */
16187       scope = current_scope ();
16188       /* If that scope does not contain the scope in which the
16189          class was originally declared, the program is invalid.  */
16190       if (scope && !is_ancestor (scope, nested_name_specifier))
16191         {
16192           if (at_namespace_scope_p ())
16193             error_at (type_start_token->location,
16194                       "declaration of %qD in namespace %qD which does not "
16195                       "enclose %qD",
16196                       type, scope, nested_name_specifier);
16197           else
16198             error_at (type_start_token->location,
16199                       "declaration of %qD in %qD which does not enclose %qD",
16200                       type, scope, nested_name_specifier);
16201           type = NULL_TREE;
16202           goto done;
16203         }
16204       /* [dcl.meaning]
16205
16206          A declarator-id shall not be qualified except for the
16207          definition of a ... nested class outside of its class
16208          ... [or] the definition or explicit instantiation of a
16209          class member of a namespace outside of its namespace.  */
16210       if (scope == nested_name_specifier)
16211         {
16212           permerror (nested_name_specifier_token_start->location,
16213                      "extra qualification not allowed");
16214           nested_name_specifier = NULL_TREE;
16215           num_templates = 0;
16216         }
16217     }
16218   /* An explicit-specialization must be preceded by "template <>".  If
16219      it is not, try to recover gracefully.  */
16220   if (at_namespace_scope_p ()
16221       && parser->num_template_parameter_lists == 0
16222       && template_id_p)
16223     {
16224       error_at (type_start_token->location,
16225                 "an explicit specialization must be preceded by %<template <>%>");
16226       invalid_explicit_specialization_p = true;
16227       /* Take the same action that would have been taken by
16228          cp_parser_explicit_specialization.  */
16229       ++parser->num_template_parameter_lists;
16230       begin_specialization ();
16231     }
16232   /* There must be no "return" statements between this point and the
16233      end of this function; set "type "to the correct return value and
16234      use "goto done;" to return.  */
16235   /* Make sure that the right number of template parameters were
16236      present.  */
16237   if (!cp_parser_check_template_parameters (parser, num_templates,
16238                                             type_start_token->location,
16239                                             /*declarator=*/NULL))
16240     {
16241       /* If something went wrong, there is no point in even trying to
16242          process the class-definition.  */
16243       type = NULL_TREE;
16244       goto done;
16245     }
16246
16247   /* Look up the type.  */
16248   if (template_id_p)
16249     {
16250       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16251           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16252               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16253         {
16254           error_at (type_start_token->location,
16255                     "function template %qD redeclared as a class template", id);
16256           type = error_mark_node;
16257         }
16258       else
16259         {
16260           type = TREE_TYPE (id);
16261           type = maybe_process_partial_specialization (type);
16262         }
16263       if (nested_name_specifier)
16264         pushed_scope = push_scope (nested_name_specifier);
16265     }
16266   else if (nested_name_specifier)
16267     {
16268       tree class_type;
16269
16270       /* Given:
16271
16272             template <typename T> struct S { struct T };
16273             template <typename T> struct S<T>::T { };
16274
16275          we will get a TYPENAME_TYPE when processing the definition of
16276          `S::T'.  We need to resolve it to the actual type before we
16277          try to define it.  */
16278       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16279         {
16280           class_type = resolve_typename_type (TREE_TYPE (type),
16281                                               /*only_current_p=*/false);
16282           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16283             type = TYPE_NAME (class_type);
16284           else
16285             {
16286               cp_parser_error (parser, "could not resolve typename type");
16287               type = error_mark_node;
16288             }
16289         }
16290
16291       if (maybe_process_partial_specialization (TREE_TYPE (type))
16292           == error_mark_node)
16293         {
16294           type = NULL_TREE;
16295           goto done;
16296         }
16297
16298       class_type = current_class_type;
16299       /* Enter the scope indicated by the nested-name-specifier.  */
16300       pushed_scope = push_scope (nested_name_specifier);
16301       /* Get the canonical version of this type.  */
16302       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16303       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16304           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16305         {
16306           type = push_template_decl (type);
16307           if (type == error_mark_node)
16308             {
16309               type = NULL_TREE;
16310               goto done;
16311             }
16312         }
16313
16314       type = TREE_TYPE (type);
16315       *nested_name_specifier_p = true;
16316     }
16317   else      /* The name is not a nested name.  */
16318     {
16319       /* If the class was unnamed, create a dummy name.  */
16320       if (!id)
16321         id = make_anon_name ();
16322       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16323                        parser->num_template_parameter_lists);
16324     }
16325
16326   /* Indicate whether this class was declared as a `class' or as a
16327      `struct'.  */
16328   if (TREE_CODE (type) == RECORD_TYPE)
16329     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16330   cp_parser_check_class_key (class_key, type);
16331
16332   /* If this type was already complete, and we see another definition,
16333      that's an error.  */
16334   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16335     {
16336       error_at (type_start_token->location, "redefinition of %q#T",
16337                 type);
16338       error_at (type_start_token->location, "previous definition of %q+#T",
16339                 type);
16340       type = NULL_TREE;
16341       goto done;
16342     }
16343   else if (type == error_mark_node)
16344     type = NULL_TREE;
16345
16346   /* We will have entered the scope containing the class; the names of
16347      base classes should be looked up in that context.  For example:
16348
16349        struct A { struct B {}; struct C; };
16350        struct A::C : B {};
16351
16352      is valid.  */
16353
16354   /* Get the list of base-classes, if there is one.  */
16355   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16356     *bases = cp_parser_base_clause (parser);
16357
16358  done:
16359   /* Leave the scope given by the nested-name-specifier.  We will
16360      enter the class scope itself while processing the members.  */
16361   if (pushed_scope)
16362     pop_scope (pushed_scope);
16363
16364   if (invalid_explicit_specialization_p)
16365     {
16366       end_specialization ();
16367       --parser->num_template_parameter_lists;
16368     }
16369   *attributes_p = attributes;
16370   return type;
16371 }
16372
16373 /* Parse a class-key.
16374
16375    class-key:
16376      class
16377      struct
16378      union
16379
16380    Returns the kind of class-key specified, or none_type to indicate
16381    error.  */
16382
16383 static enum tag_types
16384 cp_parser_class_key (cp_parser* parser)
16385 {
16386   cp_token *token;
16387   enum tag_types tag_type;
16388
16389   /* Look for the class-key.  */
16390   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16391   if (!token)
16392     return none_type;
16393
16394   /* Check to see if the TOKEN is a class-key.  */
16395   tag_type = cp_parser_token_is_class_key (token);
16396   if (!tag_type)
16397     cp_parser_error (parser, "expected class-key");
16398   return tag_type;
16399 }
16400
16401 /* Parse an (optional) member-specification.
16402
16403    member-specification:
16404      member-declaration member-specification [opt]
16405      access-specifier : member-specification [opt]  */
16406
16407 static void
16408 cp_parser_member_specification_opt (cp_parser* parser)
16409 {
16410   while (true)
16411     {
16412       cp_token *token;
16413       enum rid keyword;
16414
16415       /* Peek at the next token.  */
16416       token = cp_lexer_peek_token (parser->lexer);
16417       /* If it's a `}', or EOF then we've seen all the members.  */
16418       if (token->type == CPP_CLOSE_BRACE
16419           || token->type == CPP_EOF
16420           || token->type == CPP_PRAGMA_EOL)
16421         break;
16422
16423       /* See if this token is a keyword.  */
16424       keyword = token->keyword;
16425       switch (keyword)
16426         {
16427         case RID_PUBLIC:
16428         case RID_PROTECTED:
16429         case RID_PRIVATE:
16430           /* Consume the access-specifier.  */
16431           cp_lexer_consume_token (parser->lexer);
16432           /* Remember which access-specifier is active.  */
16433           current_access_specifier = token->u.value;
16434           /* Look for the `:'.  */
16435           cp_parser_require (parser, CPP_COLON, "%<:%>");
16436           break;
16437
16438         default:
16439           /* Accept #pragmas at class scope.  */
16440           if (token->type == CPP_PRAGMA)
16441             {
16442               cp_parser_pragma (parser, pragma_external);
16443               break;
16444             }
16445
16446           /* Otherwise, the next construction must be a
16447              member-declaration.  */
16448           cp_parser_member_declaration (parser);
16449         }
16450     }
16451 }
16452
16453 /* Parse a member-declaration.
16454
16455    member-declaration:
16456      decl-specifier-seq [opt] member-declarator-list [opt] ;
16457      function-definition ; [opt]
16458      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16459      using-declaration
16460      template-declaration
16461
16462    member-declarator-list:
16463      member-declarator
16464      member-declarator-list , member-declarator
16465
16466    member-declarator:
16467      declarator pure-specifier [opt]
16468      declarator constant-initializer [opt]
16469      identifier [opt] : constant-expression
16470
16471    GNU Extensions:
16472
16473    member-declaration:
16474      __extension__ member-declaration
16475
16476    member-declarator:
16477      declarator attributes [opt] pure-specifier [opt]
16478      declarator attributes [opt] constant-initializer [opt]
16479      identifier [opt] attributes [opt] : constant-expression  
16480
16481    C++0x Extensions:
16482
16483    member-declaration:
16484      static_assert-declaration  */
16485
16486 static void
16487 cp_parser_member_declaration (cp_parser* parser)
16488 {
16489   cp_decl_specifier_seq decl_specifiers;
16490   tree prefix_attributes;
16491   tree decl;
16492   int declares_class_or_enum;
16493   bool friend_p;
16494   cp_token *token = NULL;
16495   cp_token *decl_spec_token_start = NULL;
16496   cp_token *initializer_token_start = NULL;
16497   int saved_pedantic;
16498
16499   /* Check for the `__extension__' keyword.  */
16500   if (cp_parser_extension_opt (parser, &saved_pedantic))
16501     {
16502       /* Recurse.  */
16503       cp_parser_member_declaration (parser);
16504       /* Restore the old value of the PEDANTIC flag.  */
16505       pedantic = saved_pedantic;
16506
16507       return;
16508     }
16509
16510   /* Check for a template-declaration.  */
16511   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16512     {
16513       /* An explicit specialization here is an error condition, and we
16514          expect the specialization handler to detect and report this.  */
16515       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16516           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16517         cp_parser_explicit_specialization (parser);
16518       else
16519         cp_parser_template_declaration (parser, /*member_p=*/true);
16520
16521       return;
16522     }
16523
16524   /* Check for a using-declaration.  */
16525   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16526     {
16527       /* Parse the using-declaration.  */
16528       cp_parser_using_declaration (parser,
16529                                    /*access_declaration_p=*/false);
16530       return;
16531     }
16532
16533   /* Check for @defs.  */
16534   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16535     {
16536       tree ivar, member;
16537       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16538       ivar = ivar_chains;
16539       while (ivar)
16540         {
16541           member = ivar;
16542           ivar = TREE_CHAIN (member);
16543           TREE_CHAIN (member) = NULL_TREE;
16544           finish_member_declaration (member);
16545         }
16546       return;
16547     }
16548
16549   /* If the next token is `static_assert' we have a static assertion.  */
16550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16551     {
16552       cp_parser_static_assert (parser, /*member_p=*/true);
16553       return;
16554     }
16555
16556   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16557     return;
16558
16559   /* Parse the decl-specifier-seq.  */
16560   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16561   cp_parser_decl_specifier_seq (parser,
16562                                 CP_PARSER_FLAGS_OPTIONAL,
16563                                 &decl_specifiers,
16564                                 &declares_class_or_enum);
16565   prefix_attributes = decl_specifiers.attributes;
16566   decl_specifiers.attributes = NULL_TREE;
16567   /* Check for an invalid type-name.  */
16568   if (!decl_specifiers.any_type_specifiers_p
16569       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16570     return;
16571   /* If there is no declarator, then the decl-specifier-seq should
16572      specify a type.  */
16573   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16574     {
16575       /* If there was no decl-specifier-seq, and the next token is a
16576          `;', then we have something like:
16577
16578            struct S { ; };
16579
16580          [class.mem]
16581
16582          Each member-declaration shall declare at least one member
16583          name of the class.  */
16584       if (!decl_specifiers.any_specifiers_p)
16585         {
16586           cp_token *token = cp_lexer_peek_token (parser->lexer);
16587           if (!in_system_header_at (token->location))
16588             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16589         }
16590       else
16591         {
16592           tree type;
16593
16594           /* See if this declaration is a friend.  */
16595           friend_p = cp_parser_friend_p (&decl_specifiers);
16596           /* If there were decl-specifiers, check to see if there was
16597              a class-declaration.  */
16598           type = check_tag_decl (&decl_specifiers);
16599           /* Nested classes have already been added to the class, but
16600              a `friend' needs to be explicitly registered.  */
16601           if (friend_p)
16602             {
16603               /* If the `friend' keyword was present, the friend must
16604                  be introduced with a class-key.  */
16605                if (!declares_class_or_enum)
16606                  error_at (decl_spec_token_start->location,
16607                            "a class-key must be used when declaring a friend");
16608                /* In this case:
16609
16610                     template <typename T> struct A {
16611                       friend struct A<T>::B;
16612                     };
16613
16614                   A<T>::B will be represented by a TYPENAME_TYPE, and
16615                   therefore not recognized by check_tag_decl.  */
16616                if (!type
16617                    && decl_specifiers.type
16618                    && TYPE_P (decl_specifiers.type))
16619                  type = decl_specifiers.type;
16620                if (!type || !TYPE_P (type))
16621                  error_at (decl_spec_token_start->location,
16622                            "friend declaration does not name a class or "
16623                            "function");
16624                else
16625                  make_friend_class (current_class_type, type,
16626                                     /*complain=*/true);
16627             }
16628           /* If there is no TYPE, an error message will already have
16629              been issued.  */
16630           else if (!type || type == error_mark_node)
16631             ;
16632           /* An anonymous aggregate has to be handled specially; such
16633              a declaration really declares a data member (with a
16634              particular type), as opposed to a nested class.  */
16635           else if (ANON_AGGR_TYPE_P (type))
16636             {
16637               /* Remove constructors and such from TYPE, now that we
16638                  know it is an anonymous aggregate.  */
16639               fixup_anonymous_aggr (type);
16640               /* And make the corresponding data member.  */
16641               decl = build_decl (decl_spec_token_start->location,
16642                                  FIELD_DECL, NULL_TREE, type);
16643               /* Add it to the class.  */
16644               finish_member_declaration (decl);
16645             }
16646           else
16647             cp_parser_check_access_in_redeclaration
16648                                               (TYPE_NAME (type),
16649                                                decl_spec_token_start->location);
16650         }
16651     }
16652   else
16653     {
16654       /* See if these declarations will be friends.  */
16655       friend_p = cp_parser_friend_p (&decl_specifiers);
16656
16657       /* Keep going until we hit the `;' at the end of the
16658          declaration.  */
16659       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16660         {
16661           tree attributes = NULL_TREE;
16662           tree first_attribute;
16663
16664           /* Peek at the next token.  */
16665           token = cp_lexer_peek_token (parser->lexer);
16666
16667           /* Check for a bitfield declaration.  */
16668           if (token->type == CPP_COLON
16669               || (token->type == CPP_NAME
16670                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16671                   == CPP_COLON))
16672             {
16673               tree identifier;
16674               tree width;
16675
16676               /* Get the name of the bitfield.  Note that we cannot just
16677                  check TOKEN here because it may have been invalidated by
16678                  the call to cp_lexer_peek_nth_token above.  */
16679               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16680                 identifier = cp_parser_identifier (parser);
16681               else
16682                 identifier = NULL_TREE;
16683
16684               /* Consume the `:' token.  */
16685               cp_lexer_consume_token (parser->lexer);
16686               /* Get the width of the bitfield.  */
16687               width
16688                 = cp_parser_constant_expression (parser,
16689                                                  /*allow_non_constant=*/false,
16690                                                  NULL);
16691
16692               /* Look for attributes that apply to the bitfield.  */
16693               attributes = cp_parser_attributes_opt (parser);
16694               /* Remember which attributes are prefix attributes and
16695                  which are not.  */
16696               first_attribute = attributes;
16697               /* Combine the attributes.  */
16698               attributes = chainon (prefix_attributes, attributes);
16699
16700               /* Create the bitfield declaration.  */
16701               decl = grokbitfield (identifier
16702                                    ? make_id_declarator (NULL_TREE,
16703                                                          identifier,
16704                                                          sfk_none)
16705                                    : NULL,
16706                                    &decl_specifiers,
16707                                    width,
16708                                    attributes);
16709             }
16710           else
16711             {
16712               cp_declarator *declarator;
16713               tree initializer;
16714               tree asm_specification;
16715               int ctor_dtor_or_conv_p;
16716
16717               /* Parse the declarator.  */
16718               declarator
16719                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16720                                         &ctor_dtor_or_conv_p,
16721                                         /*parenthesized_p=*/NULL,
16722                                         /*member_p=*/true);
16723
16724               /* If something went wrong parsing the declarator, make sure
16725                  that we at least consume some tokens.  */
16726               if (declarator == cp_error_declarator)
16727                 {
16728                   /* Skip to the end of the statement.  */
16729                   cp_parser_skip_to_end_of_statement (parser);
16730                   /* If the next token is not a semicolon, that is
16731                      probably because we just skipped over the body of
16732                      a function.  So, we consume a semicolon if
16733                      present, but do not issue an error message if it
16734                      is not present.  */
16735                   if (cp_lexer_next_token_is (parser->lexer,
16736                                               CPP_SEMICOLON))
16737                     cp_lexer_consume_token (parser->lexer);
16738                   return;
16739                 }
16740
16741               if (declares_class_or_enum & 2)
16742                 cp_parser_check_for_definition_in_return_type
16743                                             (declarator, decl_specifiers.type,
16744                                              decl_specifiers.type_location);
16745
16746               /* Look for an asm-specification.  */
16747               asm_specification = cp_parser_asm_specification_opt (parser);
16748               /* Look for attributes that apply to the declaration.  */
16749               attributes = cp_parser_attributes_opt (parser);
16750               /* Remember which attributes are prefix attributes and
16751                  which are not.  */
16752               first_attribute = attributes;
16753               /* Combine the attributes.  */
16754               attributes = chainon (prefix_attributes, attributes);
16755
16756               /* If it's an `=', then we have a constant-initializer or a
16757                  pure-specifier.  It is not correct to parse the
16758                  initializer before registering the member declaration
16759                  since the member declaration should be in scope while
16760                  its initializer is processed.  However, the rest of the
16761                  front end does not yet provide an interface that allows
16762                  us to handle this correctly.  */
16763               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16764                 {
16765                   /* In [class.mem]:
16766
16767                      A pure-specifier shall be used only in the declaration of
16768                      a virtual function.
16769
16770                      A member-declarator can contain a constant-initializer
16771                      only if it declares a static member of integral or
16772                      enumeration type.
16773
16774                      Therefore, if the DECLARATOR is for a function, we look
16775                      for a pure-specifier; otherwise, we look for a
16776                      constant-initializer.  When we call `grokfield', it will
16777                      perform more stringent semantics checks.  */
16778                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16779                   if (function_declarator_p (declarator))
16780                     initializer = cp_parser_pure_specifier (parser);
16781                   else
16782                     /* Parse the initializer.  */
16783                     initializer = cp_parser_constant_initializer (parser);
16784                 }
16785               /* Otherwise, there is no initializer.  */
16786               else
16787                 initializer = NULL_TREE;
16788
16789               /* See if we are probably looking at a function
16790                  definition.  We are certainly not looking at a
16791                  member-declarator.  Calling `grokfield' has
16792                  side-effects, so we must not do it unless we are sure
16793                  that we are looking at a member-declarator.  */
16794               if (cp_parser_token_starts_function_definition_p
16795                   (cp_lexer_peek_token (parser->lexer)))
16796                 {
16797                   /* The grammar does not allow a pure-specifier to be
16798                      used when a member function is defined.  (It is
16799                      possible that this fact is an oversight in the
16800                      standard, since a pure function may be defined
16801                      outside of the class-specifier.  */
16802                   if (initializer)
16803                     error_at (initializer_token_start->location,
16804                               "pure-specifier on function-definition");
16805                   decl = cp_parser_save_member_function_body (parser,
16806                                                               &decl_specifiers,
16807                                                               declarator,
16808                                                               attributes);
16809                   /* If the member was not a friend, declare it here.  */
16810                   if (!friend_p)
16811                     finish_member_declaration (decl);
16812                   /* Peek at the next token.  */
16813                   token = cp_lexer_peek_token (parser->lexer);
16814                   /* If the next token is a semicolon, consume it.  */
16815                   if (token->type == CPP_SEMICOLON)
16816                     cp_lexer_consume_token (parser->lexer);
16817                   return;
16818                 }
16819               else
16820                 if (declarator->kind == cdk_function)
16821                   declarator->id_loc = token->location;
16822                 /* Create the declaration.  */
16823                 decl = grokfield (declarator, &decl_specifiers,
16824                                   initializer, /*init_const_expr_p=*/true,
16825                                   asm_specification,
16826                                   attributes);
16827             }
16828
16829           /* Reset PREFIX_ATTRIBUTES.  */
16830           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16831             attributes = TREE_CHAIN (attributes);
16832           if (attributes)
16833             TREE_CHAIN (attributes) = NULL_TREE;
16834
16835           /* If there is any qualification still in effect, clear it
16836              now; we will be starting fresh with the next declarator.  */
16837           parser->scope = NULL_TREE;
16838           parser->qualifying_scope = NULL_TREE;
16839           parser->object_scope = NULL_TREE;
16840           /* If it's a `,', then there are more declarators.  */
16841           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16842             cp_lexer_consume_token (parser->lexer);
16843           /* If the next token isn't a `;', then we have a parse error.  */
16844           else if (cp_lexer_next_token_is_not (parser->lexer,
16845                                                CPP_SEMICOLON))
16846             {
16847               cp_parser_error (parser, "expected %<;%>");
16848               /* Skip tokens until we find a `;'.  */
16849               cp_parser_skip_to_end_of_statement (parser);
16850
16851               break;
16852             }
16853
16854           if (decl)
16855             {
16856               /* Add DECL to the list of members.  */
16857               if (!friend_p)
16858                 finish_member_declaration (decl);
16859
16860               if (TREE_CODE (decl) == FUNCTION_DECL)
16861                 cp_parser_save_default_args (parser, decl);
16862             }
16863         }
16864     }
16865
16866   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16867 }
16868
16869 /* Parse a pure-specifier.
16870
16871    pure-specifier:
16872      = 0
16873
16874    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16875    Otherwise, ERROR_MARK_NODE is returned.  */
16876
16877 static tree
16878 cp_parser_pure_specifier (cp_parser* parser)
16879 {
16880   cp_token *token;
16881
16882   /* Look for the `=' token.  */
16883   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16884     return error_mark_node;
16885   /* Look for the `0' token.  */
16886   token = cp_lexer_peek_token (parser->lexer);
16887
16888   if (token->type == CPP_EOF
16889       || token->type == CPP_PRAGMA_EOL)
16890     return error_mark_node;
16891
16892   cp_lexer_consume_token (parser->lexer);
16893
16894   /* Accept = default or = delete in c++0x mode.  */
16895   if (token->keyword == RID_DEFAULT
16896       || token->keyword == RID_DELETE)
16897     {
16898       maybe_warn_cpp0x ("defaulted and deleted functions");
16899       return token->u.value;
16900     }
16901
16902   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16903   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16904     {
16905       cp_parser_error (parser,
16906                        "invalid pure specifier (only %<= 0%> is allowed)");
16907       cp_parser_skip_to_end_of_statement (parser);
16908       return error_mark_node;
16909     }
16910   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16911     {
16912       error_at (token->location, "templates may not be %<virtual%>");
16913       return error_mark_node;
16914     }
16915
16916   return integer_zero_node;
16917 }
16918
16919 /* Parse a constant-initializer.
16920
16921    constant-initializer:
16922      = constant-expression
16923
16924    Returns a representation of the constant-expression.  */
16925
16926 static tree
16927 cp_parser_constant_initializer (cp_parser* parser)
16928 {
16929   /* Look for the `=' token.  */
16930   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16931     return error_mark_node;
16932
16933   /* It is invalid to write:
16934
16935        struct S { static const int i = { 7 }; };
16936
16937      */
16938   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16939     {
16940       cp_parser_error (parser,
16941                        "a brace-enclosed initializer is not allowed here");
16942       /* Consume the opening brace.  */
16943       cp_lexer_consume_token (parser->lexer);
16944       /* Skip the initializer.  */
16945       cp_parser_skip_to_closing_brace (parser);
16946       /* Look for the trailing `}'.  */
16947       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16948
16949       return error_mark_node;
16950     }
16951
16952   return cp_parser_constant_expression (parser,
16953                                         /*allow_non_constant=*/false,
16954                                         NULL);
16955 }
16956
16957 /* Derived classes [gram.class.derived] */
16958
16959 /* Parse a base-clause.
16960
16961    base-clause:
16962      : base-specifier-list
16963
16964    base-specifier-list:
16965      base-specifier ... [opt]
16966      base-specifier-list , base-specifier ... [opt]
16967
16968    Returns a TREE_LIST representing the base-classes, in the order in
16969    which they were declared.  The representation of each node is as
16970    described by cp_parser_base_specifier.
16971
16972    In the case that no bases are specified, this function will return
16973    NULL_TREE, not ERROR_MARK_NODE.  */
16974
16975 static tree
16976 cp_parser_base_clause (cp_parser* parser)
16977 {
16978   tree bases = NULL_TREE;
16979
16980   /* Look for the `:' that begins the list.  */
16981   cp_parser_require (parser, CPP_COLON, "%<:%>");
16982
16983   /* Scan the base-specifier-list.  */
16984   while (true)
16985     {
16986       cp_token *token;
16987       tree base;
16988       bool pack_expansion_p = false;
16989
16990       /* Look for the base-specifier.  */
16991       base = cp_parser_base_specifier (parser);
16992       /* Look for the (optional) ellipsis. */
16993       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16994         {
16995           /* Consume the `...'. */
16996           cp_lexer_consume_token (parser->lexer);
16997
16998           pack_expansion_p = true;
16999         }
17000
17001       /* Add BASE to the front of the list.  */
17002       if (base != error_mark_node)
17003         {
17004           if (pack_expansion_p)
17005             /* Make this a pack expansion type. */
17006             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17007           
17008
17009           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17010             {
17011               TREE_CHAIN (base) = bases;
17012               bases = base;
17013             }
17014         }
17015       /* Peek at the next token.  */
17016       token = cp_lexer_peek_token (parser->lexer);
17017       /* If it's not a comma, then the list is complete.  */
17018       if (token->type != CPP_COMMA)
17019         break;
17020       /* Consume the `,'.  */
17021       cp_lexer_consume_token (parser->lexer);
17022     }
17023
17024   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17025      base class had a qualified name.  However, the next name that
17026      appears is certainly not qualified.  */
17027   parser->scope = NULL_TREE;
17028   parser->qualifying_scope = NULL_TREE;
17029   parser->object_scope = NULL_TREE;
17030
17031   return nreverse (bases);
17032 }
17033
17034 /* Parse a base-specifier.
17035
17036    base-specifier:
17037      :: [opt] nested-name-specifier [opt] class-name
17038      virtual access-specifier [opt] :: [opt] nested-name-specifier
17039        [opt] class-name
17040      access-specifier virtual [opt] :: [opt] nested-name-specifier
17041        [opt] class-name
17042
17043    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17044    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17045    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17046    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17047
17048 static tree
17049 cp_parser_base_specifier (cp_parser* parser)
17050 {
17051   cp_token *token;
17052   bool done = false;
17053   bool virtual_p = false;
17054   bool duplicate_virtual_error_issued_p = false;
17055   bool duplicate_access_error_issued_p = false;
17056   bool class_scope_p, template_p;
17057   tree access = access_default_node;
17058   tree type;
17059
17060   /* Process the optional `virtual' and `access-specifier'.  */
17061   while (!done)
17062     {
17063       /* Peek at the next token.  */
17064       token = cp_lexer_peek_token (parser->lexer);
17065       /* Process `virtual'.  */
17066       switch (token->keyword)
17067         {
17068         case RID_VIRTUAL:
17069           /* If `virtual' appears more than once, issue an error.  */
17070           if (virtual_p && !duplicate_virtual_error_issued_p)
17071             {
17072               cp_parser_error (parser,
17073                                "%<virtual%> specified more than once in base-specified");
17074               duplicate_virtual_error_issued_p = true;
17075             }
17076
17077           virtual_p = true;
17078
17079           /* Consume the `virtual' token.  */
17080           cp_lexer_consume_token (parser->lexer);
17081
17082           break;
17083
17084         case RID_PUBLIC:
17085         case RID_PROTECTED:
17086         case RID_PRIVATE:
17087           /* If more than one access specifier appears, issue an
17088              error.  */
17089           if (access != access_default_node
17090               && !duplicate_access_error_issued_p)
17091             {
17092               cp_parser_error (parser,
17093                                "more than one access specifier in base-specified");
17094               duplicate_access_error_issued_p = true;
17095             }
17096
17097           access = ridpointers[(int) token->keyword];
17098
17099           /* Consume the access-specifier.  */
17100           cp_lexer_consume_token (parser->lexer);
17101
17102           break;
17103
17104         default:
17105           done = true;
17106           break;
17107         }
17108     }
17109   /* It is not uncommon to see programs mechanically, erroneously, use
17110      the 'typename' keyword to denote (dependent) qualified types
17111      as base classes.  */
17112   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17113     {
17114       token = cp_lexer_peek_token (parser->lexer);
17115       if (!processing_template_decl)
17116         error_at (token->location,
17117                   "keyword %<typename%> not allowed outside of templates");
17118       else
17119         error_at (token->location,
17120                   "keyword %<typename%> not allowed in this context "
17121                   "(the base class is implicitly a type)");
17122       cp_lexer_consume_token (parser->lexer);
17123     }
17124
17125   /* Look for the optional `::' operator.  */
17126   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17127   /* Look for the nested-name-specifier.  The simplest way to
17128      implement:
17129
17130        [temp.res]
17131
17132        The keyword `typename' is not permitted in a base-specifier or
17133        mem-initializer; in these contexts a qualified name that
17134        depends on a template-parameter is implicitly assumed to be a
17135        type name.
17136
17137      is to pretend that we have seen the `typename' keyword at this
17138      point.  */
17139   cp_parser_nested_name_specifier_opt (parser,
17140                                        /*typename_keyword_p=*/true,
17141                                        /*check_dependency_p=*/true,
17142                                        typename_type,
17143                                        /*is_declaration=*/true);
17144   /* If the base class is given by a qualified name, assume that names
17145      we see are type names or templates, as appropriate.  */
17146   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17147   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17148
17149   /* Finally, look for the class-name.  */
17150   type = cp_parser_class_name (parser,
17151                                class_scope_p,
17152                                template_p,
17153                                typename_type,
17154                                /*check_dependency_p=*/true,
17155                                /*class_head_p=*/false,
17156                                /*is_declaration=*/true);
17157
17158   if (type == error_mark_node)
17159     return error_mark_node;
17160
17161   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17162 }
17163
17164 /* Exception handling [gram.exception] */
17165
17166 /* Parse an (optional) exception-specification.
17167
17168    exception-specification:
17169      throw ( type-id-list [opt] )
17170
17171    Returns a TREE_LIST representing the exception-specification.  The
17172    TREE_VALUE of each node is a type.  */
17173
17174 static tree
17175 cp_parser_exception_specification_opt (cp_parser* parser)
17176 {
17177   cp_token *token;
17178   tree type_id_list;
17179
17180   /* Peek at the next token.  */
17181   token = cp_lexer_peek_token (parser->lexer);
17182   /* If it's not `throw', then there's no exception-specification.  */
17183   if (!cp_parser_is_keyword (token, RID_THROW))
17184     return NULL_TREE;
17185
17186   /* Consume the `throw'.  */
17187   cp_lexer_consume_token (parser->lexer);
17188
17189   /* Look for the `('.  */
17190   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17191
17192   /* Peek at the next token.  */
17193   token = cp_lexer_peek_token (parser->lexer);
17194   /* If it's not a `)', then there is a type-id-list.  */
17195   if (token->type != CPP_CLOSE_PAREN)
17196     {
17197       const char *saved_message;
17198
17199       /* Types may not be defined in an exception-specification.  */
17200       saved_message = parser->type_definition_forbidden_message;
17201       parser->type_definition_forbidden_message
17202         = "types may not be defined in an exception-specification";
17203       /* Parse the type-id-list.  */
17204       type_id_list = cp_parser_type_id_list (parser);
17205       /* Restore the saved message.  */
17206       parser->type_definition_forbidden_message = saved_message;
17207     }
17208   else
17209     type_id_list = empty_except_spec;
17210
17211   /* Look for the `)'.  */
17212   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17213
17214   return type_id_list;
17215 }
17216
17217 /* Parse an (optional) type-id-list.
17218
17219    type-id-list:
17220      type-id ... [opt]
17221      type-id-list , type-id ... [opt]
17222
17223    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17224    in the order that the types were presented.  */
17225
17226 static tree
17227 cp_parser_type_id_list (cp_parser* parser)
17228 {
17229   tree types = NULL_TREE;
17230
17231   while (true)
17232     {
17233       cp_token *token;
17234       tree type;
17235
17236       /* Get the next type-id.  */
17237       type = cp_parser_type_id (parser);
17238       /* Parse the optional ellipsis. */
17239       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17240         {
17241           /* Consume the `...'. */
17242           cp_lexer_consume_token (parser->lexer);
17243
17244           /* Turn the type into a pack expansion expression. */
17245           type = make_pack_expansion (type);
17246         }
17247       /* Add it to the list.  */
17248       types = add_exception_specifier (types, type, /*complain=*/1);
17249       /* Peek at the next token.  */
17250       token = cp_lexer_peek_token (parser->lexer);
17251       /* If it is not a `,', we are done.  */
17252       if (token->type != CPP_COMMA)
17253         break;
17254       /* Consume the `,'.  */
17255       cp_lexer_consume_token (parser->lexer);
17256     }
17257
17258   return nreverse (types);
17259 }
17260
17261 /* Parse a try-block.
17262
17263    try-block:
17264      try compound-statement handler-seq  */
17265
17266 static tree
17267 cp_parser_try_block (cp_parser* parser)
17268 {
17269   tree try_block;
17270
17271   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17272   try_block = begin_try_block ();
17273   cp_parser_compound_statement (parser, NULL, true);
17274   finish_try_block (try_block);
17275   cp_parser_handler_seq (parser);
17276   finish_handler_sequence (try_block);
17277
17278   return try_block;
17279 }
17280
17281 /* Parse a function-try-block.
17282
17283    function-try-block:
17284      try ctor-initializer [opt] function-body handler-seq  */
17285
17286 static bool
17287 cp_parser_function_try_block (cp_parser* parser)
17288 {
17289   tree compound_stmt;
17290   tree try_block;
17291   bool ctor_initializer_p;
17292
17293   /* Look for the `try' keyword.  */
17294   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17295     return false;
17296   /* Let the rest of the front end know where we are.  */
17297   try_block = begin_function_try_block (&compound_stmt);
17298   /* Parse the function-body.  */
17299   ctor_initializer_p
17300     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17301   /* We're done with the `try' part.  */
17302   finish_function_try_block (try_block);
17303   /* Parse the handlers.  */
17304   cp_parser_handler_seq (parser);
17305   /* We're done with the handlers.  */
17306   finish_function_handler_sequence (try_block, compound_stmt);
17307
17308   return ctor_initializer_p;
17309 }
17310
17311 /* Parse a handler-seq.
17312
17313    handler-seq:
17314      handler handler-seq [opt]  */
17315
17316 static void
17317 cp_parser_handler_seq (cp_parser* parser)
17318 {
17319   while (true)
17320     {
17321       cp_token *token;
17322
17323       /* Parse the handler.  */
17324       cp_parser_handler (parser);
17325       /* Peek at the next token.  */
17326       token = cp_lexer_peek_token (parser->lexer);
17327       /* If it's not `catch' then there are no more handlers.  */
17328       if (!cp_parser_is_keyword (token, RID_CATCH))
17329         break;
17330     }
17331 }
17332
17333 /* Parse a handler.
17334
17335    handler:
17336      catch ( exception-declaration ) compound-statement  */
17337
17338 static void
17339 cp_parser_handler (cp_parser* parser)
17340 {
17341   tree handler;
17342   tree declaration;
17343
17344   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17345   handler = begin_handler ();
17346   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17347   declaration = cp_parser_exception_declaration (parser);
17348   finish_handler_parms (declaration, handler);
17349   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17350   cp_parser_compound_statement (parser, NULL, false);
17351   finish_handler (handler);
17352 }
17353
17354 /* Parse an exception-declaration.
17355
17356    exception-declaration:
17357      type-specifier-seq declarator
17358      type-specifier-seq abstract-declarator
17359      type-specifier-seq
17360      ...
17361
17362    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17363    ellipsis variant is used.  */
17364
17365 static tree
17366 cp_parser_exception_declaration (cp_parser* parser)
17367 {
17368   cp_decl_specifier_seq type_specifiers;
17369   cp_declarator *declarator;
17370   const char *saved_message;
17371
17372   /* If it's an ellipsis, it's easy to handle.  */
17373   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17374     {
17375       /* Consume the `...' token.  */
17376       cp_lexer_consume_token (parser->lexer);
17377       return NULL_TREE;
17378     }
17379
17380   /* Types may not be defined in exception-declarations.  */
17381   saved_message = parser->type_definition_forbidden_message;
17382   parser->type_definition_forbidden_message
17383     = "types may not be defined in exception-declarations";
17384
17385   /* Parse the type-specifier-seq.  */
17386   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17387                                 /*is_trailing_return=*/false,
17388                                 &type_specifiers);
17389   /* If it's a `)', then there is no declarator.  */
17390   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17391     declarator = NULL;
17392   else
17393     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17394                                        /*ctor_dtor_or_conv_p=*/NULL,
17395                                        /*parenthesized_p=*/NULL,
17396                                        /*member_p=*/false);
17397
17398   /* Restore the saved message.  */
17399   parser->type_definition_forbidden_message = saved_message;
17400
17401   if (!type_specifiers.any_specifiers_p)
17402     return error_mark_node;
17403
17404   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17405 }
17406
17407 /* Parse a throw-expression.
17408
17409    throw-expression:
17410      throw assignment-expression [opt]
17411
17412    Returns a THROW_EXPR representing the throw-expression.  */
17413
17414 static tree
17415 cp_parser_throw_expression (cp_parser* parser)
17416 {
17417   tree expression;
17418   cp_token* token;
17419
17420   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17421   token = cp_lexer_peek_token (parser->lexer);
17422   /* Figure out whether or not there is an assignment-expression
17423      following the "throw" keyword.  */
17424   if (token->type == CPP_COMMA
17425       || token->type == CPP_SEMICOLON
17426       || token->type == CPP_CLOSE_PAREN
17427       || token->type == CPP_CLOSE_SQUARE
17428       || token->type == CPP_CLOSE_BRACE
17429       || token->type == CPP_COLON)
17430     expression = NULL_TREE;
17431   else
17432     expression = cp_parser_assignment_expression (parser,
17433                                                   /*cast_p=*/false, NULL);
17434
17435   return build_throw (expression);
17436 }
17437
17438 /* GNU Extensions */
17439
17440 /* Parse an (optional) asm-specification.
17441
17442    asm-specification:
17443      asm ( string-literal )
17444
17445    If the asm-specification is present, returns a STRING_CST
17446    corresponding to the string-literal.  Otherwise, returns
17447    NULL_TREE.  */
17448
17449 static tree
17450 cp_parser_asm_specification_opt (cp_parser* parser)
17451 {
17452   cp_token *token;
17453   tree asm_specification;
17454
17455   /* Peek at the next token.  */
17456   token = cp_lexer_peek_token (parser->lexer);
17457   /* If the next token isn't the `asm' keyword, then there's no
17458      asm-specification.  */
17459   if (!cp_parser_is_keyword (token, RID_ASM))
17460     return NULL_TREE;
17461
17462   /* Consume the `asm' token.  */
17463   cp_lexer_consume_token (parser->lexer);
17464   /* Look for the `('.  */
17465   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17466
17467   /* Look for the string-literal.  */
17468   asm_specification = cp_parser_string_literal (parser, false, false);
17469
17470   /* Look for the `)'.  */
17471   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17472
17473   return asm_specification;
17474 }
17475
17476 /* Parse an asm-operand-list.
17477
17478    asm-operand-list:
17479      asm-operand
17480      asm-operand-list , asm-operand
17481
17482    asm-operand:
17483      string-literal ( expression )
17484      [ string-literal ] string-literal ( expression )
17485
17486    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17487    each node is the expression.  The TREE_PURPOSE is itself a
17488    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17489    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17490    is a STRING_CST for the string literal before the parenthesis. Returns
17491    ERROR_MARK_NODE if any of the operands are invalid.  */
17492
17493 static tree
17494 cp_parser_asm_operand_list (cp_parser* parser)
17495 {
17496   tree asm_operands = NULL_TREE;
17497   bool invalid_operands = false;
17498
17499   while (true)
17500     {
17501       tree string_literal;
17502       tree expression;
17503       tree name;
17504
17505       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17506         {
17507           /* Consume the `[' token.  */
17508           cp_lexer_consume_token (parser->lexer);
17509           /* Read the operand name.  */
17510           name = cp_parser_identifier (parser);
17511           if (name != error_mark_node)
17512             name = build_string (IDENTIFIER_LENGTH (name),
17513                                  IDENTIFIER_POINTER (name));
17514           /* Look for the closing `]'.  */
17515           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17516         }
17517       else
17518         name = NULL_TREE;
17519       /* Look for the string-literal.  */
17520       string_literal = cp_parser_string_literal (parser, false, false);
17521
17522       /* Look for the `('.  */
17523       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17524       /* Parse the expression.  */
17525       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17526       /* Look for the `)'.  */
17527       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17528
17529       if (name == error_mark_node 
17530           || string_literal == error_mark_node 
17531           || expression == error_mark_node)
17532         invalid_operands = true;
17533
17534       /* Add this operand to the list.  */
17535       asm_operands = tree_cons (build_tree_list (name, string_literal),
17536                                 expression,
17537                                 asm_operands);
17538       /* If the next token is not a `,', there are no more
17539          operands.  */
17540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17541         break;
17542       /* Consume the `,'.  */
17543       cp_lexer_consume_token (parser->lexer);
17544     }
17545
17546   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17547 }
17548
17549 /* Parse an asm-clobber-list.
17550
17551    asm-clobber-list:
17552      string-literal
17553      asm-clobber-list , string-literal
17554
17555    Returns a TREE_LIST, indicating the clobbers in the order that they
17556    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17557
17558 static tree
17559 cp_parser_asm_clobber_list (cp_parser* parser)
17560 {
17561   tree clobbers = NULL_TREE;
17562
17563   while (true)
17564     {
17565       tree string_literal;
17566
17567       /* Look for the string literal.  */
17568       string_literal = cp_parser_string_literal (parser, false, false);
17569       /* Add it to the list.  */
17570       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17571       /* If the next token is not a `,', then the list is
17572          complete.  */
17573       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17574         break;
17575       /* Consume the `,' token.  */
17576       cp_lexer_consume_token (parser->lexer);
17577     }
17578
17579   return clobbers;
17580 }
17581
17582 /* Parse an asm-label-list.
17583
17584    asm-label-list:
17585      identifier
17586      asm-label-list , identifier
17587
17588    Returns a TREE_LIST, indicating the labels in the order that they
17589    appeared.  The TREE_VALUE of each node is a label.  */
17590
17591 static tree
17592 cp_parser_asm_label_list (cp_parser* parser)
17593 {
17594   tree labels = NULL_TREE;
17595
17596   while (true)
17597     {
17598       tree identifier, label, name;
17599
17600       /* Look for the identifier.  */
17601       identifier = cp_parser_identifier (parser);
17602       if (!error_operand_p (identifier))
17603         {
17604           label = lookup_label (identifier);
17605           if (TREE_CODE (label) == LABEL_DECL)
17606             {
17607               TREE_USED (label) = 1;
17608               check_goto (label);
17609               name = build_string (IDENTIFIER_LENGTH (identifier),
17610                                    IDENTIFIER_POINTER (identifier));
17611               labels = tree_cons (name, label, labels);
17612             }
17613         }
17614       /* If the next token is not a `,', then the list is
17615          complete.  */
17616       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17617         break;
17618       /* Consume the `,' token.  */
17619       cp_lexer_consume_token (parser->lexer);
17620     }
17621
17622   return nreverse (labels);
17623 }
17624
17625 /* Parse an (optional) series of attributes.
17626
17627    attributes:
17628      attributes attribute
17629
17630    attribute:
17631      __attribute__ (( attribute-list [opt] ))
17632
17633    The return value is as for cp_parser_attribute_list.  */
17634
17635 static tree
17636 cp_parser_attributes_opt (cp_parser* parser)
17637 {
17638   tree attributes = NULL_TREE;
17639
17640   while (true)
17641     {
17642       cp_token *token;
17643       tree attribute_list;
17644
17645       /* Peek at the next token.  */
17646       token = cp_lexer_peek_token (parser->lexer);
17647       /* If it's not `__attribute__', then we're done.  */
17648       if (token->keyword != RID_ATTRIBUTE)
17649         break;
17650
17651       /* Consume the `__attribute__' keyword.  */
17652       cp_lexer_consume_token (parser->lexer);
17653       /* Look for the two `(' tokens.  */
17654       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17655       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17656
17657       /* Peek at the next token.  */
17658       token = cp_lexer_peek_token (parser->lexer);
17659       if (token->type != CPP_CLOSE_PAREN)
17660         /* Parse the attribute-list.  */
17661         attribute_list = cp_parser_attribute_list (parser);
17662       else
17663         /* If the next token is a `)', then there is no attribute
17664            list.  */
17665         attribute_list = NULL;
17666
17667       /* Look for the two `)' tokens.  */
17668       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17669       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17670
17671       /* Add these new attributes to the list.  */
17672       attributes = chainon (attributes, attribute_list);
17673     }
17674
17675   return attributes;
17676 }
17677
17678 /* Parse an attribute-list.
17679
17680    attribute-list:
17681      attribute
17682      attribute-list , attribute
17683
17684    attribute:
17685      identifier
17686      identifier ( identifier )
17687      identifier ( identifier , expression-list )
17688      identifier ( expression-list )
17689
17690    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17691    to an attribute.  The TREE_PURPOSE of each node is the identifier
17692    indicating which attribute is in use.  The TREE_VALUE represents
17693    the arguments, if any.  */
17694
17695 static tree
17696 cp_parser_attribute_list (cp_parser* parser)
17697 {
17698   tree attribute_list = NULL_TREE;
17699   bool save_translate_strings_p = parser->translate_strings_p;
17700
17701   parser->translate_strings_p = false;
17702   while (true)
17703     {
17704       cp_token *token;
17705       tree identifier;
17706       tree attribute;
17707
17708       /* Look for the identifier.  We also allow keywords here; for
17709          example `__attribute__ ((const))' is legal.  */
17710       token = cp_lexer_peek_token (parser->lexer);
17711       if (token->type == CPP_NAME
17712           || token->type == CPP_KEYWORD)
17713         {
17714           tree arguments = NULL_TREE;
17715
17716           /* Consume the token.  */
17717           token = cp_lexer_consume_token (parser->lexer);
17718
17719           /* Save away the identifier that indicates which attribute
17720              this is.  */
17721           identifier = (token->type == CPP_KEYWORD) 
17722             /* For keywords, use the canonical spelling, not the
17723                parsed identifier.  */
17724             ? ridpointers[(int) token->keyword]
17725             : token->u.value;
17726           
17727           attribute = build_tree_list (identifier, NULL_TREE);
17728
17729           /* Peek at the next token.  */
17730           token = cp_lexer_peek_token (parser->lexer);
17731           /* If it's an `(', then parse the attribute arguments.  */
17732           if (token->type == CPP_OPEN_PAREN)
17733             {
17734               VEC(tree,gc) *vec;
17735               vec = cp_parser_parenthesized_expression_list
17736                     (parser, true, /*cast_p=*/false,
17737                      /*allow_expansion_p=*/false,
17738                      /*non_constant_p=*/NULL);
17739               if (vec == NULL)
17740                 arguments = error_mark_node;
17741               else
17742                 {
17743                   arguments = build_tree_list_vec (vec);
17744                   release_tree_vector (vec);
17745                 }
17746               /* Save the arguments away.  */
17747               TREE_VALUE (attribute) = arguments;
17748             }
17749
17750           if (arguments != error_mark_node)
17751             {
17752               /* Add this attribute to the list.  */
17753               TREE_CHAIN (attribute) = attribute_list;
17754               attribute_list = attribute;
17755             }
17756
17757           token = cp_lexer_peek_token (parser->lexer);
17758         }
17759       /* Now, look for more attributes.  If the next token isn't a
17760          `,', we're done.  */
17761       if (token->type != CPP_COMMA)
17762         break;
17763
17764       /* Consume the comma and keep going.  */
17765       cp_lexer_consume_token (parser->lexer);
17766     }
17767   parser->translate_strings_p = save_translate_strings_p;
17768
17769   /* We built up the list in reverse order.  */
17770   return nreverse (attribute_list);
17771 }
17772
17773 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17774    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17775    current value of the PEDANTIC flag, regardless of whether or not
17776    the `__extension__' keyword is present.  The caller is responsible
17777    for restoring the value of the PEDANTIC flag.  */
17778
17779 static bool
17780 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17781 {
17782   /* Save the old value of the PEDANTIC flag.  */
17783   *saved_pedantic = pedantic;
17784
17785   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17786     {
17787       /* Consume the `__extension__' token.  */
17788       cp_lexer_consume_token (parser->lexer);
17789       /* We're not being pedantic while the `__extension__' keyword is
17790          in effect.  */
17791       pedantic = 0;
17792
17793       return true;
17794     }
17795
17796   return false;
17797 }
17798
17799 /* Parse a label declaration.
17800
17801    label-declaration:
17802      __label__ label-declarator-seq ;
17803
17804    label-declarator-seq:
17805      identifier , label-declarator-seq
17806      identifier  */
17807
17808 static void
17809 cp_parser_label_declaration (cp_parser* parser)
17810 {
17811   /* Look for the `__label__' keyword.  */
17812   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17813
17814   while (true)
17815     {
17816       tree identifier;
17817
17818       /* Look for an identifier.  */
17819       identifier = cp_parser_identifier (parser);
17820       /* If we failed, stop.  */
17821       if (identifier == error_mark_node)
17822         break;
17823       /* Declare it as a label.  */
17824       finish_label_decl (identifier);
17825       /* If the next token is a `;', stop.  */
17826       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17827         break;
17828       /* Look for the `,' separating the label declarations.  */
17829       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17830     }
17831
17832   /* Look for the final `;'.  */
17833   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17834 }
17835
17836 /* Support Functions */
17837
17838 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17839    NAME should have one of the representations used for an
17840    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17841    is returned.  If PARSER->SCOPE is a dependent type, then a
17842    SCOPE_REF is returned.
17843
17844    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17845    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17846    was formed.  Abstractly, such entities should not be passed to this
17847    function, because they do not need to be looked up, but it is
17848    simpler to check for this special case here, rather than at the
17849    call-sites.
17850
17851    In cases not explicitly covered above, this function returns a
17852    DECL, OVERLOAD, or baselink representing the result of the lookup.
17853    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17854    is returned.
17855
17856    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17857    (e.g., "struct") that was used.  In that case bindings that do not
17858    refer to types are ignored.
17859
17860    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17861    ignored.
17862
17863    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17864    are ignored.
17865
17866    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17867    types.
17868
17869    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17870    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17871    NULL_TREE otherwise.  */
17872
17873 static tree
17874 cp_parser_lookup_name (cp_parser *parser, tree name,
17875                        enum tag_types tag_type,
17876                        bool is_template,
17877                        bool is_namespace,
17878                        bool check_dependency,
17879                        tree *ambiguous_decls,
17880                        location_t name_location)
17881 {
17882   int flags = 0;
17883   tree decl;
17884   tree object_type = parser->context->object_type;
17885
17886   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17887     flags |= LOOKUP_COMPLAIN;
17888
17889   /* Assume that the lookup will be unambiguous.  */
17890   if (ambiguous_decls)
17891     *ambiguous_decls = NULL_TREE;
17892
17893   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17894      no longer valid.  Note that if we are parsing tentatively, and
17895      the parse fails, OBJECT_TYPE will be automatically restored.  */
17896   parser->context->object_type = NULL_TREE;
17897
17898   if (name == error_mark_node)
17899     return error_mark_node;
17900
17901   /* A template-id has already been resolved; there is no lookup to
17902      do.  */
17903   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17904     return name;
17905   if (BASELINK_P (name))
17906     {
17907       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17908                   == TEMPLATE_ID_EXPR);
17909       return name;
17910     }
17911
17912   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17913      it should already have been checked to make sure that the name
17914      used matches the type being destroyed.  */
17915   if (TREE_CODE (name) == BIT_NOT_EXPR)
17916     {
17917       tree type;
17918
17919       /* Figure out to which type this destructor applies.  */
17920       if (parser->scope)
17921         type = parser->scope;
17922       else if (object_type)
17923         type = object_type;
17924       else
17925         type = current_class_type;
17926       /* If that's not a class type, there is no destructor.  */
17927       if (!type || !CLASS_TYPE_P (type))
17928         return error_mark_node;
17929       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17930         lazily_declare_fn (sfk_destructor, type);
17931       if (!CLASSTYPE_DESTRUCTORS (type))
17932           return error_mark_node;
17933       /* If it was a class type, return the destructor.  */
17934       return CLASSTYPE_DESTRUCTORS (type);
17935     }
17936
17937   /* By this point, the NAME should be an ordinary identifier.  If
17938      the id-expression was a qualified name, the qualifying scope is
17939      stored in PARSER->SCOPE at this point.  */
17940   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17941
17942   /* Perform the lookup.  */
17943   if (parser->scope)
17944     {
17945       bool dependent_p;
17946
17947       if (parser->scope == error_mark_node)
17948         return error_mark_node;
17949
17950       /* If the SCOPE is dependent, the lookup must be deferred until
17951          the template is instantiated -- unless we are explicitly
17952          looking up names in uninstantiated templates.  Even then, we
17953          cannot look up the name if the scope is not a class type; it
17954          might, for example, be a template type parameter.  */
17955       dependent_p = (TYPE_P (parser->scope)
17956                      && dependent_scope_p (parser->scope));
17957       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17958           && dependent_p)
17959         /* Defer lookup.  */
17960         decl = error_mark_node;
17961       else
17962         {
17963           tree pushed_scope = NULL_TREE;
17964
17965           /* If PARSER->SCOPE is a dependent type, then it must be a
17966              class type, and we must not be checking dependencies;
17967              otherwise, we would have processed this lookup above.  So
17968              that PARSER->SCOPE is not considered a dependent base by
17969              lookup_member, we must enter the scope here.  */
17970           if (dependent_p)
17971             pushed_scope = push_scope (parser->scope);
17972           /* If the PARSER->SCOPE is a template specialization, it
17973              may be instantiated during name lookup.  In that case,
17974              errors may be issued.  Even if we rollback the current
17975              tentative parse, those errors are valid.  */
17976           decl = lookup_qualified_name (parser->scope, name,
17977                                         tag_type != none_type,
17978                                         /*complain=*/true);
17979
17980           /* If we have a single function from a using decl, pull it out.  */
17981           if (TREE_CODE (decl) == OVERLOAD
17982               && !really_overloaded_fn (decl))
17983             decl = OVL_FUNCTION (decl);
17984
17985           if (pushed_scope)
17986             pop_scope (pushed_scope);
17987         }
17988
17989       /* If the scope is a dependent type and either we deferred lookup or
17990          we did lookup but didn't find the name, rememeber the name.  */
17991       if (decl == error_mark_node && TYPE_P (parser->scope)
17992           && dependent_type_p (parser->scope))
17993         {
17994           if (tag_type)
17995             {
17996               tree type;
17997
17998               /* The resolution to Core Issue 180 says that `struct
17999                  A::B' should be considered a type-name, even if `A'
18000                  is dependent.  */
18001               type = make_typename_type (parser->scope, name, tag_type,
18002                                          /*complain=*/tf_error);
18003               decl = TYPE_NAME (type);
18004             }
18005           else if (is_template
18006                    && (cp_parser_next_token_ends_template_argument_p (parser)
18007                        || cp_lexer_next_token_is (parser->lexer,
18008                                                   CPP_CLOSE_PAREN)))
18009             decl = make_unbound_class_template (parser->scope,
18010                                                 name, NULL_TREE,
18011                                                 /*complain=*/tf_error);
18012           else
18013             decl = build_qualified_name (/*type=*/NULL_TREE,
18014                                          parser->scope, name,
18015                                          is_template);
18016         }
18017       parser->qualifying_scope = parser->scope;
18018       parser->object_scope = NULL_TREE;
18019     }
18020   else if (object_type)
18021     {
18022       tree object_decl = NULL_TREE;
18023       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18024          OBJECT_TYPE is not a class.  */
18025       if (CLASS_TYPE_P (object_type))
18026         /* If the OBJECT_TYPE is a template specialization, it may
18027            be instantiated during name lookup.  In that case, errors
18028            may be issued.  Even if we rollback the current tentative
18029            parse, those errors are valid.  */
18030         object_decl = lookup_member (object_type,
18031                                      name,
18032                                      /*protect=*/0,
18033                                      tag_type != none_type);
18034       /* Look it up in the enclosing context, too.  */
18035       decl = lookup_name_real (name, tag_type != none_type,
18036                                /*nonclass=*/0,
18037                                /*block_p=*/true, is_namespace, flags);
18038       parser->object_scope = object_type;
18039       parser->qualifying_scope = NULL_TREE;
18040       if (object_decl)
18041         decl = object_decl;
18042     }
18043   else
18044     {
18045       decl = lookup_name_real (name, tag_type != none_type,
18046                                /*nonclass=*/0,
18047                                /*block_p=*/true, is_namespace, flags);
18048       parser->qualifying_scope = NULL_TREE;
18049       parser->object_scope = NULL_TREE;
18050     }
18051
18052   /* If the lookup failed, let our caller know.  */
18053   if (!decl || decl == error_mark_node)
18054     return error_mark_node;
18055
18056   /* Pull out the template from an injected-class-name (or multiple).  */
18057   if (is_template)
18058     decl = maybe_get_template_decl_from_type_decl (decl);
18059
18060   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18061   if (TREE_CODE (decl) == TREE_LIST)
18062     {
18063       if (ambiguous_decls)
18064         *ambiguous_decls = decl;
18065       /* The error message we have to print is too complicated for
18066          cp_parser_error, so we incorporate its actions directly.  */
18067       if (!cp_parser_simulate_error (parser))
18068         {
18069           error_at (name_location, "reference to %qD is ambiguous",
18070                     name);
18071           print_candidates (decl);
18072         }
18073       return error_mark_node;
18074     }
18075
18076   gcc_assert (DECL_P (decl)
18077               || TREE_CODE (decl) == OVERLOAD
18078               || TREE_CODE (decl) == SCOPE_REF
18079               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18080               || BASELINK_P (decl));
18081
18082   /* If we have resolved the name of a member declaration, check to
18083      see if the declaration is accessible.  When the name resolves to
18084      set of overloaded functions, accessibility is checked when
18085      overload resolution is done.
18086
18087      During an explicit instantiation, access is not checked at all,
18088      as per [temp.explicit].  */
18089   if (DECL_P (decl))
18090     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18091
18092   return decl;
18093 }
18094
18095 /* Like cp_parser_lookup_name, but for use in the typical case where
18096    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18097    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18098
18099 static tree
18100 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18101 {
18102   return cp_parser_lookup_name (parser, name,
18103                                 none_type,
18104                                 /*is_template=*/false,
18105                                 /*is_namespace=*/false,
18106                                 /*check_dependency=*/true,
18107                                 /*ambiguous_decls=*/NULL,
18108                                 location);
18109 }
18110
18111 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18112    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18113    true, the DECL indicates the class being defined in a class-head,
18114    or declared in an elaborated-type-specifier.
18115
18116    Otherwise, return DECL.  */
18117
18118 static tree
18119 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18120 {
18121   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18122      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18123
18124        struct A {
18125          template <typename T> struct B;
18126        };
18127
18128        template <typename T> struct A::B {};
18129
18130      Similarly, in an elaborated-type-specifier:
18131
18132        namespace N { struct X{}; }
18133
18134        struct A {
18135          template <typename T> friend struct N::X;
18136        };
18137
18138      However, if the DECL refers to a class type, and we are in
18139      the scope of the class, then the name lookup automatically
18140      finds the TYPE_DECL created by build_self_reference rather
18141      than a TEMPLATE_DECL.  For example, in:
18142
18143        template <class T> struct S {
18144          S s;
18145        };
18146
18147      there is no need to handle such case.  */
18148
18149   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18150     return DECL_TEMPLATE_RESULT (decl);
18151
18152   return decl;
18153 }
18154
18155 /* If too many, or too few, template-parameter lists apply to the
18156    declarator, issue an error message.  Returns TRUE if all went well,
18157    and FALSE otherwise.  */
18158
18159 static bool
18160 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18161                                                 cp_declarator *declarator,
18162                                                 location_t declarator_location)
18163 {
18164   unsigned num_templates;
18165
18166   /* We haven't seen any classes that involve template parameters yet.  */
18167   num_templates = 0;
18168
18169   switch (declarator->kind)
18170     {
18171     case cdk_id:
18172       if (declarator->u.id.qualifying_scope)
18173         {
18174           tree scope;
18175           tree member;
18176
18177           scope = declarator->u.id.qualifying_scope;
18178           member = declarator->u.id.unqualified_name;
18179
18180           while (scope && CLASS_TYPE_P (scope))
18181             {
18182               /* You're supposed to have one `template <...>'
18183                  for every template class, but you don't need one
18184                  for a full specialization.  For example:
18185
18186                  template <class T> struct S{};
18187                  template <> struct S<int> { void f(); };
18188                  void S<int>::f () {}
18189
18190                  is correct; there shouldn't be a `template <>' for
18191                  the definition of `S<int>::f'.  */
18192               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18193                 /* If SCOPE does not have template information of any
18194                    kind, then it is not a template, nor is it nested
18195                    within a template.  */
18196                 break;
18197               if (explicit_class_specialization_p (scope))
18198                 break;
18199               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18200                 ++num_templates;
18201
18202               scope = TYPE_CONTEXT (scope);
18203             }
18204         }
18205       else if (TREE_CODE (declarator->u.id.unqualified_name)
18206                == TEMPLATE_ID_EXPR)
18207         /* If the DECLARATOR has the form `X<y>' then it uses one
18208            additional level of template parameters.  */
18209         ++num_templates;
18210
18211       return cp_parser_check_template_parameters 
18212         (parser, num_templates, declarator_location, declarator);
18213
18214
18215     case cdk_function:
18216     case cdk_array:
18217     case cdk_pointer:
18218     case cdk_reference:
18219     case cdk_ptrmem:
18220       return (cp_parser_check_declarator_template_parameters
18221               (parser, declarator->declarator, declarator_location));
18222
18223     case cdk_error:
18224       return true;
18225
18226     default:
18227       gcc_unreachable ();
18228     }
18229   return false;
18230 }
18231
18232 /* NUM_TEMPLATES were used in the current declaration.  If that is
18233    invalid, return FALSE and issue an error messages.  Otherwise,
18234    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18235    declarator and we can print more accurate diagnostics.  */
18236
18237 static bool
18238 cp_parser_check_template_parameters (cp_parser* parser,
18239                                      unsigned num_templates,
18240                                      location_t location,
18241                                      cp_declarator *declarator)
18242 {
18243   /* If there are the same number of template classes and parameter
18244      lists, that's OK.  */
18245   if (parser->num_template_parameter_lists == num_templates)
18246     return true;
18247   /* If there are more, but only one more, then we are referring to a
18248      member template.  That's OK too.  */
18249   if (parser->num_template_parameter_lists == num_templates + 1)
18250     return true;
18251   /* If there are more template classes than parameter lists, we have
18252      something like:
18253
18254        template <class T> void S<T>::R<T>::f ();  */
18255   if (parser->num_template_parameter_lists < num_templates)
18256     {
18257       if (declarator && !current_function_decl)
18258         error_at (location, "specializing member %<%T::%E%> "
18259                   "requires %<template<>%> syntax", 
18260                   declarator->u.id.qualifying_scope,
18261                   declarator->u.id.unqualified_name);
18262       else if (declarator)
18263         error_at (location, "invalid declaration of %<%T::%E%>",
18264                   declarator->u.id.qualifying_scope,
18265                   declarator->u.id.unqualified_name);
18266       else 
18267         error_at (location, "too few template-parameter-lists");
18268       return false;
18269     }
18270   /* Otherwise, there are too many template parameter lists.  We have
18271      something like:
18272
18273      template <class T> template <class U> void S::f();  */
18274   error_at (location, "too many template-parameter-lists");
18275   return false;
18276 }
18277
18278 /* Parse an optional `::' token indicating that the following name is
18279    from the global namespace.  If so, PARSER->SCOPE is set to the
18280    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18281    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18282    Returns the new value of PARSER->SCOPE, if the `::' token is
18283    present, and NULL_TREE otherwise.  */
18284
18285 static tree
18286 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18287 {
18288   cp_token *token;
18289
18290   /* Peek at the next token.  */
18291   token = cp_lexer_peek_token (parser->lexer);
18292   /* If we're looking at a `::' token then we're starting from the
18293      global namespace, not our current location.  */
18294   if (token->type == CPP_SCOPE)
18295     {
18296       /* Consume the `::' token.  */
18297       cp_lexer_consume_token (parser->lexer);
18298       /* Set the SCOPE so that we know where to start the lookup.  */
18299       parser->scope = global_namespace;
18300       parser->qualifying_scope = global_namespace;
18301       parser->object_scope = NULL_TREE;
18302
18303       return parser->scope;
18304     }
18305   else if (!current_scope_valid_p)
18306     {
18307       parser->scope = NULL_TREE;
18308       parser->qualifying_scope = NULL_TREE;
18309       parser->object_scope = NULL_TREE;
18310     }
18311
18312   return NULL_TREE;
18313 }
18314
18315 /* Returns TRUE if the upcoming token sequence is the start of a
18316    constructor declarator.  If FRIEND_P is true, the declarator is
18317    preceded by the `friend' specifier.  */
18318
18319 static bool
18320 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18321 {
18322   bool constructor_p;
18323   tree type_decl = NULL_TREE;
18324   bool nested_name_p;
18325   cp_token *next_token;
18326
18327   /* The common case is that this is not a constructor declarator, so
18328      try to avoid doing lots of work if at all possible.  It's not
18329      valid declare a constructor at function scope.  */
18330   if (parser->in_function_body)
18331     return false;
18332   /* And only certain tokens can begin a constructor declarator.  */
18333   next_token = cp_lexer_peek_token (parser->lexer);
18334   if (next_token->type != CPP_NAME
18335       && next_token->type != CPP_SCOPE
18336       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18337       && next_token->type != CPP_TEMPLATE_ID)
18338     return false;
18339
18340   /* Parse tentatively; we are going to roll back all of the tokens
18341      consumed here.  */
18342   cp_parser_parse_tentatively (parser);
18343   /* Assume that we are looking at a constructor declarator.  */
18344   constructor_p = true;
18345
18346   /* Look for the optional `::' operator.  */
18347   cp_parser_global_scope_opt (parser,
18348                               /*current_scope_valid_p=*/false);
18349   /* Look for the nested-name-specifier.  */
18350   nested_name_p
18351     = (cp_parser_nested_name_specifier_opt (parser,
18352                                             /*typename_keyword_p=*/false,
18353                                             /*check_dependency_p=*/false,
18354                                             /*type_p=*/false,
18355                                             /*is_declaration=*/false)
18356        != NULL_TREE);
18357   /* Outside of a class-specifier, there must be a
18358      nested-name-specifier.  */
18359   if (!nested_name_p &&
18360       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18361        || friend_p))
18362     constructor_p = false;
18363   /* If we still think that this might be a constructor-declarator,
18364      look for a class-name.  */
18365   if (constructor_p)
18366     {
18367       /* If we have:
18368
18369            template <typename T> struct S { S(); };
18370            template <typename T> S<T>::S ();
18371
18372          we must recognize that the nested `S' names a class.
18373          Similarly, for:
18374
18375            template <typename T> S<T>::S<T> ();
18376
18377          we must recognize that the nested `S' names a template.  */
18378       type_decl = cp_parser_class_name (parser,
18379                                         /*typename_keyword_p=*/false,
18380                                         /*template_keyword_p=*/false,
18381                                         none_type,
18382                                         /*check_dependency_p=*/false,
18383                                         /*class_head_p=*/false,
18384                                         /*is_declaration=*/false);
18385       /* If there was no class-name, then this is not a constructor.  */
18386       constructor_p = !cp_parser_error_occurred (parser);
18387     }
18388
18389   /* If we're still considering a constructor, we have to see a `(',
18390      to begin the parameter-declaration-clause, followed by either a
18391      `)', an `...', or a decl-specifier.  We need to check for a
18392      type-specifier to avoid being fooled into thinking that:
18393
18394        S::S (f) (int);
18395
18396      is a constructor.  (It is actually a function named `f' that
18397      takes one parameter (of type `int') and returns a value of type
18398      `S::S'.  */
18399   if (constructor_p
18400       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18401     {
18402       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18403           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18404           /* A parameter declaration begins with a decl-specifier,
18405              which is either the "attribute" keyword, a storage class
18406              specifier, or (usually) a type-specifier.  */
18407           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18408         {
18409           tree type;
18410           tree pushed_scope = NULL_TREE;
18411           unsigned saved_num_template_parameter_lists;
18412
18413           /* Names appearing in the type-specifier should be looked up
18414              in the scope of the class.  */
18415           if (current_class_type)
18416             type = NULL_TREE;
18417           else
18418             {
18419               type = TREE_TYPE (type_decl);
18420               if (TREE_CODE (type) == TYPENAME_TYPE)
18421                 {
18422                   type = resolve_typename_type (type,
18423                                                 /*only_current_p=*/false);
18424                   if (TREE_CODE (type) == TYPENAME_TYPE)
18425                     {
18426                       cp_parser_abort_tentative_parse (parser);
18427                       return false;
18428                     }
18429                 }
18430               pushed_scope = push_scope (type);
18431             }
18432
18433           /* Inside the constructor parameter list, surrounding
18434              template-parameter-lists do not apply.  */
18435           saved_num_template_parameter_lists
18436             = parser->num_template_parameter_lists;
18437           parser->num_template_parameter_lists = 0;
18438
18439           /* Look for the type-specifier.  */
18440           cp_parser_type_specifier (parser,
18441                                     CP_PARSER_FLAGS_NONE,
18442                                     /*decl_specs=*/NULL,
18443                                     /*is_declarator=*/true,
18444                                     /*declares_class_or_enum=*/NULL,
18445                                     /*is_cv_qualifier=*/NULL);
18446
18447           parser->num_template_parameter_lists
18448             = saved_num_template_parameter_lists;
18449
18450           /* Leave the scope of the class.  */
18451           if (pushed_scope)
18452             pop_scope (pushed_scope);
18453
18454           constructor_p = !cp_parser_error_occurred (parser);
18455         }
18456     }
18457   else
18458     constructor_p = false;
18459   /* We did not really want to consume any tokens.  */
18460   cp_parser_abort_tentative_parse (parser);
18461
18462   return constructor_p;
18463 }
18464
18465 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18466    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18467    they must be performed once we are in the scope of the function.
18468
18469    Returns the function defined.  */
18470
18471 static tree
18472 cp_parser_function_definition_from_specifiers_and_declarator
18473   (cp_parser* parser,
18474    cp_decl_specifier_seq *decl_specifiers,
18475    tree attributes,
18476    const cp_declarator *declarator)
18477 {
18478   tree fn;
18479   bool success_p;
18480
18481   /* Begin the function-definition.  */
18482   success_p = start_function (decl_specifiers, declarator, attributes);
18483
18484   /* The things we're about to see are not directly qualified by any
18485      template headers we've seen thus far.  */
18486   reset_specialization ();
18487
18488   /* If there were names looked up in the decl-specifier-seq that we
18489      did not check, check them now.  We must wait until we are in the
18490      scope of the function to perform the checks, since the function
18491      might be a friend.  */
18492   perform_deferred_access_checks ();
18493
18494   if (!success_p)
18495     {
18496       /* Skip the entire function.  */
18497       cp_parser_skip_to_end_of_block_or_statement (parser);
18498       fn = error_mark_node;
18499     }
18500   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18501     {
18502       /* Seen already, skip it.  An error message has already been output.  */
18503       cp_parser_skip_to_end_of_block_or_statement (parser);
18504       fn = current_function_decl;
18505       current_function_decl = NULL_TREE;
18506       /* If this is a function from a class, pop the nested class.  */
18507       if (current_class_name)
18508         pop_nested_class ();
18509     }
18510   else
18511     fn = cp_parser_function_definition_after_declarator (parser,
18512                                                          /*inline_p=*/false);
18513
18514   return fn;
18515 }
18516
18517 /* Parse the part of a function-definition that follows the
18518    declarator.  INLINE_P is TRUE iff this function is an inline
18519    function defined within a class-specifier.
18520
18521    Returns the function defined.  */
18522
18523 static tree
18524 cp_parser_function_definition_after_declarator (cp_parser* parser,
18525                                                 bool inline_p)
18526 {
18527   tree fn;
18528   bool ctor_initializer_p = false;
18529   bool saved_in_unbraced_linkage_specification_p;
18530   bool saved_in_function_body;
18531   unsigned saved_num_template_parameter_lists;
18532   cp_token *token;
18533
18534   saved_in_function_body = parser->in_function_body;
18535   parser->in_function_body = true;
18536   /* If the next token is `return', then the code may be trying to
18537      make use of the "named return value" extension that G++ used to
18538      support.  */
18539   token = cp_lexer_peek_token (parser->lexer);
18540   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18541     {
18542       /* Consume the `return' keyword.  */
18543       cp_lexer_consume_token (parser->lexer);
18544       /* Look for the identifier that indicates what value is to be
18545          returned.  */
18546       cp_parser_identifier (parser);
18547       /* Issue an error message.  */
18548       error_at (token->location,
18549                 "named return values are no longer supported");
18550       /* Skip tokens until we reach the start of the function body.  */
18551       while (true)
18552         {
18553           cp_token *token = cp_lexer_peek_token (parser->lexer);
18554           if (token->type == CPP_OPEN_BRACE
18555               || token->type == CPP_EOF
18556               || token->type == CPP_PRAGMA_EOL)
18557             break;
18558           cp_lexer_consume_token (parser->lexer);
18559         }
18560     }
18561   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18562      anything declared inside `f'.  */
18563   saved_in_unbraced_linkage_specification_p
18564     = parser->in_unbraced_linkage_specification_p;
18565   parser->in_unbraced_linkage_specification_p = false;
18566   /* Inside the function, surrounding template-parameter-lists do not
18567      apply.  */
18568   saved_num_template_parameter_lists
18569     = parser->num_template_parameter_lists;
18570   parser->num_template_parameter_lists = 0;
18571
18572   start_lambda_scope (current_function_decl);
18573
18574   /* If the next token is `try', then we are looking at a
18575      function-try-block.  */
18576   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18577     ctor_initializer_p = cp_parser_function_try_block (parser);
18578   /* A function-try-block includes the function-body, so we only do
18579      this next part if we're not processing a function-try-block.  */
18580   else
18581     ctor_initializer_p
18582       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18583
18584   finish_lambda_scope ();
18585
18586   /* Finish the function.  */
18587   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18588                         (inline_p ? 2 : 0));
18589   /* Generate code for it, if necessary.  */
18590   expand_or_defer_fn (fn);
18591   /* Restore the saved values.  */
18592   parser->in_unbraced_linkage_specification_p
18593     = saved_in_unbraced_linkage_specification_p;
18594   parser->num_template_parameter_lists
18595     = saved_num_template_parameter_lists;
18596   parser->in_function_body = saved_in_function_body;
18597
18598   return fn;
18599 }
18600
18601 /* Parse a template-declaration, assuming that the `export' (and
18602    `extern') keywords, if present, has already been scanned.  MEMBER_P
18603    is as for cp_parser_template_declaration.  */
18604
18605 static void
18606 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18607 {
18608   tree decl = NULL_TREE;
18609   VEC (deferred_access_check,gc) *checks;
18610   tree parameter_list;
18611   bool friend_p = false;
18612   bool need_lang_pop;
18613   cp_token *token;
18614
18615   /* Look for the `template' keyword.  */
18616   token = cp_lexer_peek_token (parser->lexer);
18617   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18618     return;
18619
18620   /* And the `<'.  */
18621   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18622     return;
18623   if (at_class_scope_p () && current_function_decl)
18624     {
18625       /* 14.5.2.2 [temp.mem]
18626
18627          A local class shall not have member templates.  */
18628       error_at (token->location,
18629                 "invalid declaration of member template in local class");
18630       cp_parser_skip_to_end_of_block_or_statement (parser);
18631       return;
18632     }
18633   /* [temp]
18634
18635      A template ... shall not have C linkage.  */
18636   if (current_lang_name == lang_name_c)
18637     {
18638       error_at (token->location, "template with C linkage");
18639       /* Give it C++ linkage to avoid confusing other parts of the
18640          front end.  */
18641       push_lang_context (lang_name_cplusplus);
18642       need_lang_pop = true;
18643     }
18644   else
18645     need_lang_pop = false;
18646
18647   /* We cannot perform access checks on the template parameter
18648      declarations until we know what is being declared, just as we
18649      cannot check the decl-specifier list.  */
18650   push_deferring_access_checks (dk_deferred);
18651
18652   /* If the next token is `>', then we have an invalid
18653      specialization.  Rather than complain about an invalid template
18654      parameter, issue an error message here.  */
18655   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18656     {
18657       cp_parser_error (parser, "invalid explicit specialization");
18658       begin_specialization ();
18659       parameter_list = NULL_TREE;
18660     }
18661   else
18662     /* Parse the template parameters.  */
18663     parameter_list = cp_parser_template_parameter_list (parser);
18664
18665   /* Get the deferred access checks from the parameter list.  These
18666      will be checked once we know what is being declared, as for a
18667      member template the checks must be performed in the scope of the
18668      class containing the member.  */
18669   checks = get_deferred_access_checks ();
18670
18671   /* Look for the `>'.  */
18672   cp_parser_skip_to_end_of_template_parameter_list (parser);
18673   /* We just processed one more parameter list.  */
18674   ++parser->num_template_parameter_lists;
18675   /* If the next token is `template', there are more template
18676      parameters.  */
18677   if (cp_lexer_next_token_is_keyword (parser->lexer,
18678                                       RID_TEMPLATE))
18679     cp_parser_template_declaration_after_export (parser, member_p);
18680   else
18681     {
18682       /* There are no access checks when parsing a template, as we do not
18683          know if a specialization will be a friend.  */
18684       push_deferring_access_checks (dk_no_check);
18685       token = cp_lexer_peek_token (parser->lexer);
18686       decl = cp_parser_single_declaration (parser,
18687                                            checks,
18688                                            member_p,
18689                                            /*explicit_specialization_p=*/false,
18690                                            &friend_p);
18691       pop_deferring_access_checks ();
18692
18693       /* If this is a member template declaration, let the front
18694          end know.  */
18695       if (member_p && !friend_p && decl)
18696         {
18697           if (TREE_CODE (decl) == TYPE_DECL)
18698             cp_parser_check_access_in_redeclaration (decl, token->location);
18699
18700           decl = finish_member_template_decl (decl);
18701         }
18702       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18703         make_friend_class (current_class_type, TREE_TYPE (decl),
18704                            /*complain=*/true);
18705     }
18706   /* We are done with the current parameter list.  */
18707   --parser->num_template_parameter_lists;
18708
18709   pop_deferring_access_checks ();
18710
18711   /* Finish up.  */
18712   finish_template_decl (parameter_list);
18713
18714   /* Register member declarations.  */
18715   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18716     finish_member_declaration (decl);
18717   /* For the erroneous case of a template with C linkage, we pushed an
18718      implicit C++ linkage scope; exit that scope now.  */
18719   if (need_lang_pop)
18720     pop_lang_context ();
18721   /* If DECL is a function template, we must return to parse it later.
18722      (Even though there is no definition, there might be default
18723      arguments that need handling.)  */
18724   if (member_p && decl
18725       && (TREE_CODE (decl) == FUNCTION_DECL
18726           || DECL_FUNCTION_TEMPLATE_P (decl)))
18727     TREE_VALUE (parser->unparsed_functions_queues)
18728       = tree_cons (NULL_TREE, decl,
18729                    TREE_VALUE (parser->unparsed_functions_queues));
18730 }
18731
18732 /* Perform the deferred access checks from a template-parameter-list.
18733    CHECKS is a TREE_LIST of access checks, as returned by
18734    get_deferred_access_checks.  */
18735
18736 static void
18737 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18738 {
18739   ++processing_template_parmlist;
18740   perform_access_checks (checks);
18741   --processing_template_parmlist;
18742 }
18743
18744 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18745    `function-definition' sequence.  MEMBER_P is true, this declaration
18746    appears in a class scope.
18747
18748    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18749    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18750
18751 static tree
18752 cp_parser_single_declaration (cp_parser* parser,
18753                               VEC (deferred_access_check,gc)* checks,
18754                               bool member_p,
18755                               bool explicit_specialization_p,
18756                               bool* friend_p)
18757 {
18758   int declares_class_or_enum;
18759   tree decl = NULL_TREE;
18760   cp_decl_specifier_seq decl_specifiers;
18761   bool function_definition_p = false;
18762   cp_token *decl_spec_token_start;
18763
18764   /* This function is only used when processing a template
18765      declaration.  */
18766   gcc_assert (innermost_scope_kind () == sk_template_parms
18767               || innermost_scope_kind () == sk_template_spec);
18768
18769   /* Defer access checks until we know what is being declared.  */
18770   push_deferring_access_checks (dk_deferred);
18771
18772   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18773      alternative.  */
18774   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18775   cp_parser_decl_specifier_seq (parser,
18776                                 CP_PARSER_FLAGS_OPTIONAL,
18777                                 &decl_specifiers,
18778                                 &declares_class_or_enum);
18779   if (friend_p)
18780     *friend_p = cp_parser_friend_p (&decl_specifiers);
18781
18782   /* There are no template typedefs.  */
18783   if (decl_specifiers.specs[(int) ds_typedef])
18784     {
18785       error_at (decl_spec_token_start->location,
18786                 "template declaration of %<typedef%>");
18787       decl = error_mark_node;
18788     }
18789
18790   /* Gather up the access checks that occurred the
18791      decl-specifier-seq.  */
18792   stop_deferring_access_checks ();
18793
18794   /* Check for the declaration of a template class.  */
18795   if (declares_class_or_enum)
18796     {
18797       if (cp_parser_declares_only_class_p (parser))
18798         {
18799           decl = shadow_tag (&decl_specifiers);
18800
18801           /* In this case:
18802
18803                struct C {
18804                  friend template <typename T> struct A<T>::B;
18805                };
18806
18807              A<T>::B will be represented by a TYPENAME_TYPE, and
18808              therefore not recognized by shadow_tag.  */
18809           if (friend_p && *friend_p
18810               && !decl
18811               && decl_specifiers.type
18812               && TYPE_P (decl_specifiers.type))
18813             decl = decl_specifiers.type;
18814
18815           if (decl && decl != error_mark_node)
18816             decl = TYPE_NAME (decl);
18817           else
18818             decl = error_mark_node;
18819
18820           /* Perform access checks for template parameters.  */
18821           cp_parser_perform_template_parameter_access_checks (checks);
18822         }
18823     }
18824
18825   /* Complain about missing 'typename' or other invalid type names.  */
18826   if (!decl_specifiers.any_type_specifiers_p)
18827     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18828
18829   /* If it's not a template class, try for a template function.  If
18830      the next token is a `;', then this declaration does not declare
18831      anything.  But, if there were errors in the decl-specifiers, then
18832      the error might well have come from an attempted class-specifier.
18833      In that case, there's no need to warn about a missing declarator.  */
18834   if (!decl
18835       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18836           || decl_specifiers.type != error_mark_node))
18837     {
18838       decl = cp_parser_init_declarator (parser,
18839                                         &decl_specifiers,
18840                                         checks,
18841                                         /*function_definition_allowed_p=*/true,
18842                                         member_p,
18843                                         declares_class_or_enum,
18844                                         &function_definition_p);
18845
18846     /* 7.1.1-1 [dcl.stc]
18847
18848        A storage-class-specifier shall not be specified in an explicit
18849        specialization...  */
18850     if (decl
18851         && explicit_specialization_p
18852         && decl_specifiers.storage_class != sc_none)
18853       {
18854         error_at (decl_spec_token_start->location,
18855                   "explicit template specialization cannot have a storage class");
18856         decl = error_mark_node;
18857       }
18858     }
18859
18860   pop_deferring_access_checks ();
18861
18862   /* Clear any current qualification; whatever comes next is the start
18863      of something new.  */
18864   parser->scope = NULL_TREE;
18865   parser->qualifying_scope = NULL_TREE;
18866   parser->object_scope = NULL_TREE;
18867   /* Look for a trailing `;' after the declaration.  */
18868   if (!function_definition_p
18869       && (decl == error_mark_node
18870           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18871     cp_parser_skip_to_end_of_block_or_statement (parser);
18872
18873   return decl;
18874 }
18875
18876 /* Parse a cast-expression that is not the operand of a unary "&".  */
18877
18878 static tree
18879 cp_parser_simple_cast_expression (cp_parser *parser)
18880 {
18881   return cp_parser_cast_expression (parser, /*address_p=*/false,
18882                                     /*cast_p=*/false, NULL);
18883 }
18884
18885 /* Parse a functional cast to TYPE.  Returns an expression
18886    representing the cast.  */
18887
18888 static tree
18889 cp_parser_functional_cast (cp_parser* parser, tree type)
18890 {
18891   VEC(tree,gc) *vec;
18892   tree expression_list;
18893   tree cast;
18894   bool nonconst_p;
18895
18896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18897     {
18898       maybe_warn_cpp0x ("extended initializer lists");
18899       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18900       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18901       if (TREE_CODE (type) == TYPE_DECL)
18902         type = TREE_TYPE (type);
18903       return finish_compound_literal (type, expression_list);
18904     }
18905
18906
18907   vec = cp_parser_parenthesized_expression_list (parser, false,
18908                                                  /*cast_p=*/true,
18909                                                  /*allow_expansion_p=*/true,
18910                                                  /*non_constant_p=*/NULL);
18911   if (vec == NULL)
18912     expression_list = error_mark_node;
18913   else
18914     {
18915       expression_list = build_tree_list_vec (vec);
18916       release_tree_vector (vec);
18917     }
18918
18919   cast = build_functional_cast (type, expression_list,
18920                                 tf_warning_or_error);
18921   /* [expr.const]/1: In an integral constant expression "only type
18922      conversions to integral or enumeration type can be used".  */
18923   if (TREE_CODE (type) == TYPE_DECL)
18924     type = TREE_TYPE (type);
18925   if (cast != error_mark_node
18926       && !cast_valid_in_integral_constant_expression_p (type)
18927       && (cp_parser_non_integral_constant_expression
18928           (parser, "a call to a constructor")))
18929     return error_mark_node;
18930   return cast;
18931 }
18932
18933 /* Save the tokens that make up the body of a member function defined
18934    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18935    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18936    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18937    for the member function.  */
18938
18939 static tree
18940 cp_parser_save_member_function_body (cp_parser* parser,
18941                                      cp_decl_specifier_seq *decl_specifiers,
18942                                      cp_declarator *declarator,
18943                                      tree attributes)
18944 {
18945   cp_token *first;
18946   cp_token *last;
18947   tree fn;
18948
18949   /* Create the FUNCTION_DECL.  */
18950   fn = grokmethod (decl_specifiers, declarator, attributes);
18951   /* If something went badly wrong, bail out now.  */
18952   if (fn == error_mark_node)
18953     {
18954       /* If there's a function-body, skip it.  */
18955       if (cp_parser_token_starts_function_definition_p
18956           (cp_lexer_peek_token (parser->lexer)))
18957         cp_parser_skip_to_end_of_block_or_statement (parser);
18958       return error_mark_node;
18959     }
18960
18961   /* Remember it, if there default args to post process.  */
18962   cp_parser_save_default_args (parser, fn);
18963
18964   /* Save away the tokens that make up the body of the
18965      function.  */
18966   first = parser->lexer->next_token;
18967   /* We can have braced-init-list mem-initializers before the fn body.  */
18968   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18969     {
18970       cp_lexer_consume_token (parser->lexer);
18971       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18972              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18973         {
18974           /* cache_group will stop after an un-nested { } pair, too.  */
18975           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18976             break;
18977
18978           /* variadic mem-inits have ... after the ')'.  */
18979           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18980             cp_lexer_consume_token (parser->lexer);
18981         }
18982     }
18983   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18984   /* Handle function try blocks.  */
18985   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18986     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18987   last = parser->lexer->next_token;
18988
18989   /* Save away the inline definition; we will process it when the
18990      class is complete.  */
18991   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18992   DECL_PENDING_INLINE_P (fn) = 1;
18993
18994   /* We need to know that this was defined in the class, so that
18995      friend templates are handled correctly.  */
18996   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18997
18998   /* Add FN to the queue of functions to be parsed later.  */
18999   TREE_VALUE (parser->unparsed_functions_queues)
19000     = tree_cons (NULL_TREE, fn,
19001                  TREE_VALUE (parser->unparsed_functions_queues));
19002
19003   return fn;
19004 }
19005
19006 /* Parse a template-argument-list, as well as the trailing ">" (but
19007    not the opening ">").  See cp_parser_template_argument_list for the
19008    return value.  */
19009
19010 static tree
19011 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19012 {
19013   tree arguments;
19014   tree saved_scope;
19015   tree saved_qualifying_scope;
19016   tree saved_object_scope;
19017   bool saved_greater_than_is_operator_p;
19018   int saved_unevaluated_operand;
19019   int saved_inhibit_evaluation_warnings;
19020
19021   /* [temp.names]
19022
19023      When parsing a template-id, the first non-nested `>' is taken as
19024      the end of the template-argument-list rather than a greater-than
19025      operator.  */
19026   saved_greater_than_is_operator_p
19027     = parser->greater_than_is_operator_p;
19028   parser->greater_than_is_operator_p = false;
19029   /* Parsing the argument list may modify SCOPE, so we save it
19030      here.  */
19031   saved_scope = parser->scope;
19032   saved_qualifying_scope = parser->qualifying_scope;
19033   saved_object_scope = parser->object_scope;
19034   /* We need to evaluate the template arguments, even though this
19035      template-id may be nested within a "sizeof".  */
19036   saved_unevaluated_operand = cp_unevaluated_operand;
19037   cp_unevaluated_operand = 0;
19038   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19039   c_inhibit_evaluation_warnings = 0;
19040   /* Parse the template-argument-list itself.  */
19041   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19042       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19043     arguments = NULL_TREE;
19044   else
19045     arguments = cp_parser_template_argument_list (parser);
19046   /* Look for the `>' that ends the template-argument-list. If we find
19047      a '>>' instead, it's probably just a typo.  */
19048   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19049     {
19050       if (cxx_dialect != cxx98)
19051         {
19052           /* In C++0x, a `>>' in a template argument list or cast
19053              expression is considered to be two separate `>'
19054              tokens. So, change the current token to a `>', but don't
19055              consume it: it will be consumed later when the outer
19056              template argument list (or cast expression) is parsed.
19057              Note that this replacement of `>' for `>>' is necessary
19058              even if we are parsing tentatively: in the tentative
19059              case, after calling
19060              cp_parser_enclosed_template_argument_list we will always
19061              throw away all of the template arguments and the first
19062              closing `>', either because the template argument list
19063              was erroneous or because we are replacing those tokens
19064              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19065              not have been thrown away) is needed either to close an
19066              outer template argument list or to complete a new-style
19067              cast.  */
19068           cp_token *token = cp_lexer_peek_token (parser->lexer);
19069           token->type = CPP_GREATER;
19070         }
19071       else if (!saved_greater_than_is_operator_p)
19072         {
19073           /* If we're in a nested template argument list, the '>>' has
19074             to be a typo for '> >'. We emit the error message, but we
19075             continue parsing and we push a '>' as next token, so that
19076             the argument list will be parsed correctly.  Note that the
19077             global source location is still on the token before the
19078             '>>', so we need to say explicitly where we want it.  */
19079           cp_token *token = cp_lexer_peek_token (parser->lexer);
19080           error_at (token->location, "%<>>%> should be %<> >%> "
19081                     "within a nested template argument list");
19082
19083           token->type = CPP_GREATER;
19084         }
19085       else
19086         {
19087           /* If this is not a nested template argument list, the '>>'
19088             is a typo for '>'. Emit an error message and continue.
19089             Same deal about the token location, but here we can get it
19090             right by consuming the '>>' before issuing the diagnostic.  */
19091           cp_token *token = cp_lexer_consume_token (parser->lexer);
19092           error_at (token->location,
19093                     "spurious %<>>%>, use %<>%> to terminate "
19094                     "a template argument list");
19095         }
19096     }
19097   else
19098     cp_parser_skip_to_end_of_template_parameter_list (parser);
19099   /* The `>' token might be a greater-than operator again now.  */
19100   parser->greater_than_is_operator_p
19101     = saved_greater_than_is_operator_p;
19102   /* Restore the SAVED_SCOPE.  */
19103   parser->scope = saved_scope;
19104   parser->qualifying_scope = saved_qualifying_scope;
19105   parser->object_scope = saved_object_scope;
19106   cp_unevaluated_operand = saved_unevaluated_operand;
19107   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19108
19109   return arguments;
19110 }
19111
19112 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19113    arguments, or the body of the function have not yet been parsed,
19114    parse them now.  */
19115
19116 static void
19117 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19118 {
19119   /* If this member is a template, get the underlying
19120      FUNCTION_DECL.  */
19121   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19122     member_function = DECL_TEMPLATE_RESULT (member_function);
19123
19124   /* There should not be any class definitions in progress at this
19125      point; the bodies of members are only parsed outside of all class
19126      definitions.  */
19127   gcc_assert (parser->num_classes_being_defined == 0);
19128   /* While we're parsing the member functions we might encounter more
19129      classes.  We want to handle them right away, but we don't want
19130      them getting mixed up with functions that are currently in the
19131      queue.  */
19132   parser->unparsed_functions_queues
19133     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19134
19135   /* Make sure that any template parameters are in scope.  */
19136   maybe_begin_member_template_processing (member_function);
19137
19138   /* If the body of the function has not yet been parsed, parse it
19139      now.  */
19140   if (DECL_PENDING_INLINE_P (member_function))
19141     {
19142       tree function_scope;
19143       cp_token_cache *tokens;
19144
19145       /* The function is no longer pending; we are processing it.  */
19146       tokens = DECL_PENDING_INLINE_INFO (member_function);
19147       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19148       DECL_PENDING_INLINE_P (member_function) = 0;
19149
19150       /* If this is a local class, enter the scope of the containing
19151          function.  */
19152       function_scope = current_function_decl;
19153       if (function_scope)
19154         push_function_context ();
19155
19156       /* Push the body of the function onto the lexer stack.  */
19157       cp_parser_push_lexer_for_tokens (parser, tokens);
19158
19159       /* Let the front end know that we going to be defining this
19160          function.  */
19161       start_preparsed_function (member_function, NULL_TREE,
19162                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19163
19164       /* Don't do access checking if it is a templated function.  */
19165       if (processing_template_decl)
19166         push_deferring_access_checks (dk_no_check);
19167
19168       /* Now, parse the body of the function.  */
19169       cp_parser_function_definition_after_declarator (parser,
19170                                                       /*inline_p=*/true);
19171
19172       if (processing_template_decl)
19173         pop_deferring_access_checks ();
19174
19175       /* Leave the scope of the containing function.  */
19176       if (function_scope)
19177         pop_function_context ();
19178       cp_parser_pop_lexer (parser);
19179     }
19180
19181   /* Remove any template parameters from the symbol table.  */
19182   maybe_end_member_template_processing ();
19183
19184   /* Restore the queue.  */
19185   parser->unparsed_functions_queues
19186     = TREE_CHAIN (parser->unparsed_functions_queues);
19187 }
19188
19189 /* If DECL contains any default args, remember it on the unparsed
19190    functions queue.  */
19191
19192 static void
19193 cp_parser_save_default_args (cp_parser* parser, tree decl)
19194 {
19195   tree probe;
19196
19197   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19198        probe;
19199        probe = TREE_CHAIN (probe))
19200     if (TREE_PURPOSE (probe))
19201       {
19202         TREE_PURPOSE (parser->unparsed_functions_queues)
19203           = tree_cons (current_class_type, decl,
19204                        TREE_PURPOSE (parser->unparsed_functions_queues));
19205         break;
19206       }
19207 }
19208
19209 /* FN is a FUNCTION_DECL which may contains a parameter with an
19210    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19211    assumes that the current scope is the scope in which the default
19212    argument should be processed.  */
19213
19214 static void
19215 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19216 {
19217   bool saved_local_variables_forbidden_p;
19218   tree parm, parmdecl;
19219
19220   /* While we're parsing the default args, we might (due to the
19221      statement expression extension) encounter more classes.  We want
19222      to handle them right away, but we don't want them getting mixed
19223      up with default args that are currently in the queue.  */
19224   parser->unparsed_functions_queues
19225     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19226
19227   /* Local variable names (and the `this' keyword) may not appear
19228      in a default argument.  */
19229   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19230   parser->local_variables_forbidden_p = true;
19231
19232   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19233          parmdecl = DECL_ARGUMENTS (fn);
19234        parm && parm != void_list_node;
19235        parm = TREE_CHAIN (parm),
19236          parmdecl = TREE_CHAIN (parmdecl))
19237     {
19238       cp_token_cache *tokens;
19239       tree default_arg = TREE_PURPOSE (parm);
19240       tree parsed_arg;
19241       VEC(tree,gc) *insts;
19242       tree copy;
19243       unsigned ix;
19244
19245       if (!default_arg)
19246         continue;
19247
19248       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19249         /* This can happen for a friend declaration for a function
19250            already declared with default arguments.  */
19251         continue;
19252
19253        /* Push the saved tokens for the default argument onto the parser's
19254           lexer stack.  */
19255       tokens = DEFARG_TOKENS (default_arg);
19256       cp_parser_push_lexer_for_tokens (parser, tokens);
19257
19258       start_lambda_scope (parmdecl);
19259
19260       /* Parse the assignment-expression.  */
19261       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19262       if (parsed_arg == error_mark_node)
19263         {
19264           cp_parser_pop_lexer (parser);
19265           continue;
19266         }
19267
19268       if (!processing_template_decl)
19269         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19270
19271       TREE_PURPOSE (parm) = parsed_arg;
19272
19273       /* Update any instantiations we've already created.  */
19274       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19275            VEC_iterate (tree, insts, ix, copy); ix++)
19276         TREE_PURPOSE (copy) = parsed_arg;
19277
19278       finish_lambda_scope ();
19279
19280       /* If the token stream has not been completely used up, then
19281          there was extra junk after the end of the default
19282          argument.  */
19283       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19284         cp_parser_error (parser, "expected %<,%>");
19285
19286       /* Revert to the main lexer.  */
19287       cp_parser_pop_lexer (parser);
19288     }
19289
19290   /* Make sure no default arg is missing.  */
19291   check_default_args (fn);
19292
19293   /* Restore the state of local_variables_forbidden_p.  */
19294   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19295
19296   /* Restore the queue.  */
19297   parser->unparsed_functions_queues
19298     = TREE_CHAIN (parser->unparsed_functions_queues);
19299 }
19300
19301 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19302    either a TYPE or an expression, depending on the form of the
19303    input.  The KEYWORD indicates which kind of expression we have
19304    encountered.  */
19305
19306 static tree
19307 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19308 {
19309   tree expr = NULL_TREE;
19310   const char *saved_message;
19311   char *tmp;
19312   bool saved_integral_constant_expression_p;
19313   bool saved_non_integral_constant_expression_p;
19314   bool pack_expansion_p = false;
19315
19316   /* Types cannot be defined in a `sizeof' expression.  Save away the
19317      old message.  */
19318   saved_message = parser->type_definition_forbidden_message;
19319   /* And create the new one.  */
19320   tmp = concat ("types may not be defined in %<",
19321                 IDENTIFIER_POINTER (ridpointers[keyword]),
19322                 "%> expressions", NULL);
19323   parser->type_definition_forbidden_message = tmp;
19324
19325   /* The restrictions on constant-expressions do not apply inside
19326      sizeof expressions.  */
19327   saved_integral_constant_expression_p
19328     = parser->integral_constant_expression_p;
19329   saved_non_integral_constant_expression_p
19330     = parser->non_integral_constant_expression_p;
19331   parser->integral_constant_expression_p = false;
19332
19333   /* If it's a `...', then we are computing the length of a parameter
19334      pack.  */
19335   if (keyword == RID_SIZEOF
19336       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19337     {
19338       /* Consume the `...'.  */
19339       cp_lexer_consume_token (parser->lexer);
19340       maybe_warn_variadic_templates ();
19341
19342       /* Note that this is an expansion.  */
19343       pack_expansion_p = true;
19344     }
19345
19346   /* Do not actually evaluate the expression.  */
19347   ++cp_unevaluated_operand;
19348   ++c_inhibit_evaluation_warnings;
19349   /* If it's a `(', then we might be looking at the type-id
19350      construction.  */
19351   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19352     {
19353       tree type;
19354       bool saved_in_type_id_in_expr_p;
19355
19356       /* We can't be sure yet whether we're looking at a type-id or an
19357          expression.  */
19358       cp_parser_parse_tentatively (parser);
19359       /* Consume the `('.  */
19360       cp_lexer_consume_token (parser->lexer);
19361       /* Parse the type-id.  */
19362       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19363       parser->in_type_id_in_expr_p = true;
19364       type = cp_parser_type_id (parser);
19365       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19366       /* Now, look for the trailing `)'.  */
19367       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19368       /* If all went well, then we're done.  */
19369       if (cp_parser_parse_definitely (parser))
19370         {
19371           cp_decl_specifier_seq decl_specs;
19372
19373           /* Build a trivial decl-specifier-seq.  */
19374           clear_decl_specs (&decl_specs);
19375           decl_specs.type = type;
19376
19377           /* Call grokdeclarator to figure out what type this is.  */
19378           expr = grokdeclarator (NULL,
19379                                  &decl_specs,
19380                                  TYPENAME,
19381                                  /*initialized=*/0,
19382                                  /*attrlist=*/NULL);
19383         }
19384     }
19385
19386   /* If the type-id production did not work out, then we must be
19387      looking at the unary-expression production.  */
19388   if (!expr)
19389     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19390                                        /*cast_p=*/false, NULL);
19391
19392   if (pack_expansion_p)
19393     /* Build a pack expansion. */
19394     expr = make_pack_expansion (expr);
19395
19396   /* Go back to evaluating expressions.  */
19397   --cp_unevaluated_operand;
19398   --c_inhibit_evaluation_warnings;
19399
19400   /* Free the message we created.  */
19401   free (tmp);
19402   /* And restore the old one.  */
19403   parser->type_definition_forbidden_message = saved_message;
19404   parser->integral_constant_expression_p
19405     = saved_integral_constant_expression_p;
19406   parser->non_integral_constant_expression_p
19407     = saved_non_integral_constant_expression_p;
19408
19409   return expr;
19410 }
19411
19412 /* If the current declaration has no declarator, return true.  */
19413
19414 static bool
19415 cp_parser_declares_only_class_p (cp_parser *parser)
19416 {
19417   /* If the next token is a `;' or a `,' then there is no
19418      declarator.  */
19419   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19420           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19421 }
19422
19423 /* Update the DECL_SPECS to reflect the storage class indicated by
19424    KEYWORD.  */
19425
19426 static void
19427 cp_parser_set_storage_class (cp_parser *parser,
19428                              cp_decl_specifier_seq *decl_specs,
19429                              enum rid keyword,
19430                              location_t location)
19431 {
19432   cp_storage_class storage_class;
19433
19434   if (parser->in_unbraced_linkage_specification_p)
19435     {
19436       error_at (location, "invalid use of %qD in linkage specification",
19437                 ridpointers[keyword]);
19438       return;
19439     }
19440   else if (decl_specs->storage_class != sc_none)
19441     {
19442       decl_specs->conflicting_specifiers_p = true;
19443       return;
19444     }
19445
19446   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19447       && decl_specs->specs[(int) ds_thread])
19448     {
19449       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19450       decl_specs->specs[(int) ds_thread] = 0;
19451     }
19452
19453   switch (keyword)
19454     {
19455     case RID_AUTO:
19456       storage_class = sc_auto;
19457       break;
19458     case RID_REGISTER:
19459       storage_class = sc_register;
19460       break;
19461     case RID_STATIC:
19462       storage_class = sc_static;
19463       break;
19464     case RID_EXTERN:
19465       storage_class = sc_extern;
19466       break;
19467     case RID_MUTABLE:
19468       storage_class = sc_mutable;
19469       break;
19470     default:
19471       gcc_unreachable ();
19472     }
19473   decl_specs->storage_class = storage_class;
19474
19475   /* A storage class specifier cannot be applied alongside a typedef 
19476      specifier. If there is a typedef specifier present then set 
19477      conflicting_specifiers_p which will trigger an error later
19478      on in grokdeclarator. */
19479   if (decl_specs->specs[(int)ds_typedef])
19480     decl_specs->conflicting_specifiers_p = true;
19481 }
19482
19483 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19484    is true, the type is a user-defined type; otherwise it is a
19485    built-in type specified by a keyword.  */
19486
19487 static void
19488 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19489                               tree type_spec,
19490                               location_t location,
19491                               bool user_defined_p)
19492 {
19493   decl_specs->any_specifiers_p = true;
19494
19495   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19496      (with, for example, in "typedef int wchar_t;") we remember that
19497      this is what happened.  In system headers, we ignore these
19498      declarations so that G++ can work with system headers that are not
19499      C++-safe.  */
19500   if (decl_specs->specs[(int) ds_typedef]
19501       && !user_defined_p
19502       && (type_spec == boolean_type_node
19503           || type_spec == char16_type_node
19504           || type_spec == char32_type_node
19505           || type_spec == wchar_type_node)
19506       && (decl_specs->type
19507           || decl_specs->specs[(int) ds_long]
19508           || decl_specs->specs[(int) ds_short]
19509           || decl_specs->specs[(int) ds_unsigned]
19510           || decl_specs->specs[(int) ds_signed]))
19511     {
19512       decl_specs->redefined_builtin_type = type_spec;
19513       if (!decl_specs->type)
19514         {
19515           decl_specs->type = type_spec;
19516           decl_specs->user_defined_type_p = false;
19517           decl_specs->type_location = location;
19518         }
19519     }
19520   else if (decl_specs->type)
19521     decl_specs->multiple_types_p = true;
19522   else
19523     {
19524       decl_specs->type = type_spec;
19525       decl_specs->user_defined_type_p = user_defined_p;
19526       decl_specs->redefined_builtin_type = NULL_TREE;
19527       decl_specs->type_location = location;
19528     }
19529 }
19530
19531 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19532    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19533
19534 static bool
19535 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19536 {
19537   return decl_specifiers->specs[(int) ds_friend] != 0;
19538 }
19539
19540 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19541    issue an error message indicating that TOKEN_DESC was expected.
19542
19543    Returns the token consumed, if the token had the appropriate type.
19544    Otherwise, returns NULL.  */
19545
19546 static cp_token *
19547 cp_parser_require (cp_parser* parser,
19548                    enum cpp_ttype type,
19549                    const char* token_desc)
19550 {
19551   if (cp_lexer_next_token_is (parser->lexer, type))
19552     return cp_lexer_consume_token (parser->lexer);
19553   else
19554     {
19555       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19556       if (!cp_parser_simulate_error (parser))
19557         {
19558           char *message = concat ("expected ", token_desc, NULL);
19559           cp_parser_error (parser, message);
19560           free (message);
19561         }
19562       return NULL;
19563     }
19564 }
19565
19566 /* An error message is produced if the next token is not '>'.
19567    All further tokens are skipped until the desired token is
19568    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19569
19570 static void
19571 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19572 {
19573   /* Current level of '< ... >'.  */
19574   unsigned level = 0;
19575   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19576   unsigned nesting_depth = 0;
19577
19578   /* Are we ready, yet?  If not, issue error message.  */
19579   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19580     return;
19581
19582   /* Skip tokens until the desired token is found.  */
19583   while (true)
19584     {
19585       /* Peek at the next token.  */
19586       switch (cp_lexer_peek_token (parser->lexer)->type)
19587         {
19588         case CPP_LESS:
19589           if (!nesting_depth)
19590             ++level;
19591           break;
19592
19593         case CPP_RSHIFT:
19594           if (cxx_dialect == cxx98)
19595             /* C++0x views the `>>' operator as two `>' tokens, but
19596                C++98 does not. */
19597             break;
19598           else if (!nesting_depth && level-- == 0)
19599             {
19600               /* We've hit a `>>' where the first `>' closes the
19601                  template argument list, and the second `>' is
19602                  spurious.  Just consume the `>>' and stop; we've
19603                  already produced at least one error.  */
19604               cp_lexer_consume_token (parser->lexer);
19605               return;
19606             }
19607           /* Fall through for C++0x, so we handle the second `>' in
19608              the `>>'.  */
19609
19610         case CPP_GREATER:
19611           if (!nesting_depth && level-- == 0)
19612             {
19613               /* We've reached the token we want, consume it and stop.  */
19614               cp_lexer_consume_token (parser->lexer);
19615               return;
19616             }
19617           break;
19618
19619         case CPP_OPEN_PAREN:
19620         case CPP_OPEN_SQUARE:
19621           ++nesting_depth;
19622           break;
19623
19624         case CPP_CLOSE_PAREN:
19625         case CPP_CLOSE_SQUARE:
19626           if (nesting_depth-- == 0)
19627             return;
19628           break;
19629
19630         case CPP_EOF:
19631         case CPP_PRAGMA_EOL:
19632         case CPP_SEMICOLON:
19633         case CPP_OPEN_BRACE:
19634         case CPP_CLOSE_BRACE:
19635           /* The '>' was probably forgotten, don't look further.  */
19636           return;
19637
19638         default:
19639           break;
19640         }
19641
19642       /* Consume this token.  */
19643       cp_lexer_consume_token (parser->lexer);
19644     }
19645 }
19646
19647 /* If the next token is the indicated keyword, consume it.  Otherwise,
19648    issue an error message indicating that TOKEN_DESC was expected.
19649
19650    Returns the token consumed, if the token had the appropriate type.
19651    Otherwise, returns NULL.  */
19652
19653 static cp_token *
19654 cp_parser_require_keyword (cp_parser* parser,
19655                            enum rid keyword,
19656                            const char* token_desc)
19657 {
19658   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19659
19660   if (token && token->keyword != keyword)
19661     {
19662       dyn_string_t error_msg;
19663
19664       /* Format the error message.  */
19665       error_msg = dyn_string_new (0);
19666       dyn_string_append_cstr (error_msg, "expected ");
19667       dyn_string_append_cstr (error_msg, token_desc);
19668       cp_parser_error (parser, error_msg->s);
19669       dyn_string_delete (error_msg);
19670       return NULL;
19671     }
19672
19673   return token;
19674 }
19675
19676 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19677    function-definition.  */
19678
19679 static bool
19680 cp_parser_token_starts_function_definition_p (cp_token* token)
19681 {
19682   return (/* An ordinary function-body begins with an `{'.  */
19683           token->type == CPP_OPEN_BRACE
19684           /* A ctor-initializer begins with a `:'.  */
19685           || token->type == CPP_COLON
19686           /* A function-try-block begins with `try'.  */
19687           || token->keyword == RID_TRY
19688           /* The named return value extension begins with `return'.  */
19689           || token->keyword == RID_RETURN);
19690 }
19691
19692 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19693    definition.  */
19694
19695 static bool
19696 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19697 {
19698   cp_token *token;
19699
19700   token = cp_lexer_peek_token (parser->lexer);
19701   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19702 }
19703
19704 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19705    C++0x) ending a template-argument.  */
19706
19707 static bool
19708 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19709 {
19710   cp_token *token;
19711
19712   token = cp_lexer_peek_token (parser->lexer);
19713   return (token->type == CPP_COMMA 
19714           || token->type == CPP_GREATER
19715           || token->type == CPP_ELLIPSIS
19716           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19717 }
19718
19719 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19720    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19721
19722 static bool
19723 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19724                                                      size_t n)
19725 {
19726   cp_token *token;
19727
19728   token = cp_lexer_peek_nth_token (parser->lexer, n);
19729   if (token->type == CPP_LESS)
19730     return true;
19731   /* Check for the sequence `<::' in the original code. It would be lexed as
19732      `[:', where `[' is a digraph, and there is no whitespace before
19733      `:'.  */
19734   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19735     {
19736       cp_token *token2;
19737       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19738       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19739         return true;
19740     }
19741   return false;
19742 }
19743
19744 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19745    or none_type otherwise.  */
19746
19747 static enum tag_types
19748 cp_parser_token_is_class_key (cp_token* token)
19749 {
19750   switch (token->keyword)
19751     {
19752     case RID_CLASS:
19753       return class_type;
19754     case RID_STRUCT:
19755       return record_type;
19756     case RID_UNION:
19757       return union_type;
19758
19759     default:
19760       return none_type;
19761     }
19762 }
19763
19764 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19765
19766 static void
19767 cp_parser_check_class_key (enum tag_types class_key, tree type)
19768 {
19769   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19770     permerror (input_location, "%qs tag used in naming %q#T",
19771             class_key == union_type ? "union"
19772              : class_key == record_type ? "struct" : "class",
19773              type);
19774 }
19775
19776 /* Issue an error message if DECL is redeclared with different
19777    access than its original declaration [class.access.spec/3].
19778    This applies to nested classes and nested class templates.
19779    [class.mem/1].  */
19780
19781 static void
19782 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19783 {
19784   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19785     return;
19786
19787   if ((TREE_PRIVATE (decl)
19788        != (current_access_specifier == access_private_node))
19789       || (TREE_PROTECTED (decl)
19790           != (current_access_specifier == access_protected_node)))
19791     error_at (location, "%qD redeclared with different access", decl);
19792 }
19793
19794 /* Look for the `template' keyword, as a syntactic disambiguator.
19795    Return TRUE iff it is present, in which case it will be
19796    consumed.  */
19797
19798 static bool
19799 cp_parser_optional_template_keyword (cp_parser *parser)
19800 {
19801   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19802     {
19803       /* The `template' keyword can only be used within templates;
19804          outside templates the parser can always figure out what is a
19805          template and what is not.  */
19806       if (!processing_template_decl)
19807         {
19808           cp_token *token = cp_lexer_peek_token (parser->lexer);
19809           error_at (token->location,
19810                     "%<template%> (as a disambiguator) is only allowed "
19811                     "within templates");
19812           /* If this part of the token stream is rescanned, the same
19813              error message would be generated.  So, we purge the token
19814              from the stream.  */
19815           cp_lexer_purge_token (parser->lexer);
19816           return false;
19817         }
19818       else
19819         {
19820           /* Consume the `template' keyword.  */
19821           cp_lexer_consume_token (parser->lexer);
19822           return true;
19823         }
19824     }
19825
19826   return false;
19827 }
19828
19829 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19830    set PARSER->SCOPE, and perform other related actions.  */
19831
19832 static void
19833 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19834 {
19835   int i;
19836   struct tree_check *check_value;
19837   deferred_access_check *chk;
19838   VEC (deferred_access_check,gc) *checks;
19839
19840   /* Get the stored value.  */
19841   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19842   /* Perform any access checks that were deferred.  */
19843   checks = check_value->checks;
19844   if (checks)
19845     {
19846       for (i = 0 ;
19847            VEC_iterate (deferred_access_check, checks, i, chk) ;
19848            ++i)
19849         {
19850           perform_or_defer_access_check (chk->binfo,
19851                                          chk->decl,
19852                                          chk->diag_decl);
19853         }
19854     }
19855   /* Set the scope from the stored value.  */
19856   parser->scope = check_value->value;
19857   parser->qualifying_scope = check_value->qualifying_scope;
19858   parser->object_scope = NULL_TREE;
19859 }
19860
19861 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19862    encounter the end of a block before what we were looking for.  */
19863
19864 static bool
19865 cp_parser_cache_group (cp_parser *parser,
19866                        enum cpp_ttype end,
19867                        unsigned depth)
19868 {
19869   while (true)
19870     {
19871       cp_token *token = cp_lexer_peek_token (parser->lexer);
19872
19873       /* Abort a parenthesized expression if we encounter a semicolon.  */
19874       if ((end == CPP_CLOSE_PAREN || depth == 0)
19875           && token->type == CPP_SEMICOLON)
19876         return true;
19877       /* If we've reached the end of the file, stop.  */
19878       if (token->type == CPP_EOF
19879           || (end != CPP_PRAGMA_EOL
19880               && token->type == CPP_PRAGMA_EOL))
19881         return true;
19882       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19883         /* We've hit the end of an enclosing block, so there's been some
19884            kind of syntax error.  */
19885         return true;
19886
19887       /* Consume the token.  */
19888       cp_lexer_consume_token (parser->lexer);
19889       /* See if it starts a new group.  */
19890       if (token->type == CPP_OPEN_BRACE)
19891         {
19892           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19893           /* In theory this should probably check end == '}', but
19894              cp_parser_save_member_function_body needs it to exit
19895              after either '}' or ')' when called with ')'.  */
19896           if (depth == 0)
19897             return false;
19898         }
19899       else if (token->type == CPP_OPEN_PAREN)
19900         {
19901           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19902           if (depth == 0 && end == CPP_CLOSE_PAREN)
19903             return false;
19904         }
19905       else if (token->type == CPP_PRAGMA)
19906         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19907       else if (token->type == end)
19908         return false;
19909     }
19910 }
19911
19912 /* Begin parsing tentatively.  We always save tokens while parsing
19913    tentatively so that if the tentative parsing fails we can restore the
19914    tokens.  */
19915
19916 static void
19917 cp_parser_parse_tentatively (cp_parser* parser)
19918 {
19919   /* Enter a new parsing context.  */
19920   parser->context = cp_parser_context_new (parser->context);
19921   /* Begin saving tokens.  */
19922   cp_lexer_save_tokens (parser->lexer);
19923   /* In order to avoid repetitive access control error messages,
19924      access checks are queued up until we are no longer parsing
19925      tentatively.  */
19926   push_deferring_access_checks (dk_deferred);
19927 }
19928
19929 /* Commit to the currently active tentative parse.  */
19930
19931 static void
19932 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19933 {
19934   cp_parser_context *context;
19935   cp_lexer *lexer;
19936
19937   /* Mark all of the levels as committed.  */
19938   lexer = parser->lexer;
19939   for (context = parser->context; context->next; context = context->next)
19940     {
19941       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19942         break;
19943       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19944       while (!cp_lexer_saving_tokens (lexer))
19945         lexer = lexer->next;
19946       cp_lexer_commit_tokens (lexer);
19947     }
19948 }
19949
19950 /* Abort the currently active tentative parse.  All consumed tokens
19951    will be rolled back, and no diagnostics will be issued.  */
19952
19953 static void
19954 cp_parser_abort_tentative_parse (cp_parser* parser)
19955 {
19956   cp_parser_simulate_error (parser);
19957   /* Now, pretend that we want to see if the construct was
19958      successfully parsed.  */
19959   cp_parser_parse_definitely (parser);
19960 }
19961
19962 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19963    token stream.  Otherwise, commit to the tokens we have consumed.
19964    Returns true if no error occurred; false otherwise.  */
19965
19966 static bool
19967 cp_parser_parse_definitely (cp_parser* parser)
19968 {
19969   bool error_occurred;
19970   cp_parser_context *context;
19971
19972   /* Remember whether or not an error occurred, since we are about to
19973      destroy that information.  */
19974   error_occurred = cp_parser_error_occurred (parser);
19975   /* Remove the topmost context from the stack.  */
19976   context = parser->context;
19977   parser->context = context->next;
19978   /* If no parse errors occurred, commit to the tentative parse.  */
19979   if (!error_occurred)
19980     {
19981       /* Commit to the tokens read tentatively, unless that was
19982          already done.  */
19983       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19984         cp_lexer_commit_tokens (parser->lexer);
19985
19986       pop_to_parent_deferring_access_checks ();
19987     }
19988   /* Otherwise, if errors occurred, roll back our state so that things
19989      are just as they were before we began the tentative parse.  */
19990   else
19991     {
19992       cp_lexer_rollback_tokens (parser->lexer);
19993       pop_deferring_access_checks ();
19994     }
19995   /* Add the context to the front of the free list.  */
19996   context->next = cp_parser_context_free_list;
19997   cp_parser_context_free_list = context;
19998
19999   return !error_occurred;
20000 }
20001
20002 /* Returns true if we are parsing tentatively and are not committed to
20003    this tentative parse.  */
20004
20005 static bool
20006 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20007 {
20008   return (cp_parser_parsing_tentatively (parser)
20009           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20010 }
20011
20012 /* Returns nonzero iff an error has occurred during the most recent
20013    tentative parse.  */
20014
20015 static bool
20016 cp_parser_error_occurred (cp_parser* parser)
20017 {
20018   return (cp_parser_parsing_tentatively (parser)
20019           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20020 }
20021
20022 /* Returns nonzero if GNU extensions are allowed.  */
20023
20024 static bool
20025 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20026 {
20027   return parser->allow_gnu_extensions_p;
20028 }
20029 \f
20030 /* Objective-C++ Productions */
20031
20032
20033 /* Parse an Objective-C expression, which feeds into a primary-expression
20034    above.
20035
20036    objc-expression:
20037      objc-message-expression
20038      objc-string-literal
20039      objc-encode-expression
20040      objc-protocol-expression
20041      objc-selector-expression
20042
20043   Returns a tree representation of the expression.  */
20044
20045 static tree
20046 cp_parser_objc_expression (cp_parser* parser)
20047 {
20048   /* Try to figure out what kind of declaration is present.  */
20049   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20050
20051   switch (kwd->type)
20052     {
20053     case CPP_OPEN_SQUARE:
20054       return cp_parser_objc_message_expression (parser);
20055
20056     case CPP_OBJC_STRING:
20057       kwd = cp_lexer_consume_token (parser->lexer);
20058       return objc_build_string_object (kwd->u.value);
20059
20060     case CPP_KEYWORD:
20061       switch (kwd->keyword)
20062         {
20063         case RID_AT_ENCODE:
20064           return cp_parser_objc_encode_expression (parser);
20065
20066         case RID_AT_PROTOCOL:
20067           return cp_parser_objc_protocol_expression (parser);
20068
20069         case RID_AT_SELECTOR:
20070           return cp_parser_objc_selector_expression (parser);
20071
20072         default:
20073           break;
20074         }
20075     default:
20076       error_at (kwd->location,
20077                 "misplaced %<@%D%> Objective-C++ construct",
20078                 kwd->u.value);
20079       cp_parser_skip_to_end_of_block_or_statement (parser);
20080     }
20081
20082   return error_mark_node;
20083 }
20084
20085 /* Parse an Objective-C message expression.
20086
20087    objc-message-expression:
20088      [ objc-message-receiver objc-message-args ]
20089
20090    Returns a representation of an Objective-C message.  */
20091
20092 static tree
20093 cp_parser_objc_message_expression (cp_parser* parser)
20094 {
20095   tree receiver, messageargs;
20096
20097   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20098   receiver = cp_parser_objc_message_receiver (parser);
20099   messageargs = cp_parser_objc_message_args (parser);
20100   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20101
20102   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20103 }
20104
20105 /* Parse an objc-message-receiver.
20106
20107    objc-message-receiver:
20108      expression
20109      simple-type-specifier
20110
20111   Returns a representation of the type or expression.  */
20112
20113 static tree
20114 cp_parser_objc_message_receiver (cp_parser* parser)
20115 {
20116   tree rcv;
20117
20118   /* An Objective-C message receiver may be either (1) a type
20119      or (2) an expression.  */
20120   cp_parser_parse_tentatively (parser);
20121   rcv = cp_parser_expression (parser, false, NULL);
20122
20123   if (cp_parser_parse_definitely (parser))
20124     return rcv;
20125
20126   rcv = cp_parser_simple_type_specifier (parser,
20127                                          /*decl_specs=*/NULL,
20128                                          CP_PARSER_FLAGS_NONE);
20129
20130   return objc_get_class_reference (rcv);
20131 }
20132
20133 /* Parse the arguments and selectors comprising an Objective-C message.
20134
20135    objc-message-args:
20136      objc-selector
20137      objc-selector-args
20138      objc-selector-args , objc-comma-args
20139
20140    objc-selector-args:
20141      objc-selector [opt] : assignment-expression
20142      objc-selector-args objc-selector [opt] : assignment-expression
20143
20144    objc-comma-args:
20145      assignment-expression
20146      objc-comma-args , assignment-expression
20147
20148    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20149    selector arguments and TREE_VALUE containing a list of comma
20150    arguments.  */
20151
20152 static tree
20153 cp_parser_objc_message_args (cp_parser* parser)
20154 {
20155   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20156   bool maybe_unary_selector_p = true;
20157   cp_token *token = cp_lexer_peek_token (parser->lexer);
20158
20159   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20160     {
20161       tree selector = NULL_TREE, arg;
20162
20163       if (token->type != CPP_COLON)
20164         selector = cp_parser_objc_selector (parser);
20165
20166       /* Detect if we have a unary selector.  */
20167       if (maybe_unary_selector_p
20168           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20169         return build_tree_list (selector, NULL_TREE);
20170
20171       maybe_unary_selector_p = false;
20172       cp_parser_require (parser, CPP_COLON, "%<:%>");
20173       arg = cp_parser_assignment_expression (parser, false, NULL);
20174
20175       sel_args
20176         = chainon (sel_args,
20177                    build_tree_list (selector, arg));
20178
20179       token = cp_lexer_peek_token (parser->lexer);
20180     }
20181
20182   /* Handle non-selector arguments, if any. */
20183   while (token->type == CPP_COMMA)
20184     {
20185       tree arg;
20186
20187       cp_lexer_consume_token (parser->lexer);
20188       arg = cp_parser_assignment_expression (parser, false, NULL);
20189
20190       addl_args
20191         = chainon (addl_args,
20192                    build_tree_list (NULL_TREE, arg));
20193
20194       token = cp_lexer_peek_token (parser->lexer);
20195     }
20196
20197   return build_tree_list (sel_args, addl_args);
20198 }
20199
20200 /* Parse an Objective-C encode expression.
20201
20202    objc-encode-expression:
20203      @encode objc-typename
20204
20205    Returns an encoded representation of the type argument.  */
20206
20207 static tree
20208 cp_parser_objc_encode_expression (cp_parser* parser)
20209 {
20210   tree type;
20211   cp_token *token;
20212
20213   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20214   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20215   token = cp_lexer_peek_token (parser->lexer);
20216   type = complete_type (cp_parser_type_id (parser));
20217   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20218
20219   if (!type)
20220     {
20221       error_at (token->location, 
20222                 "%<@encode%> must specify a type as an argument");
20223       return error_mark_node;
20224     }
20225
20226   return objc_build_encode_expr (type);
20227 }
20228
20229 /* Parse an Objective-C @defs expression.  */
20230
20231 static tree
20232 cp_parser_objc_defs_expression (cp_parser *parser)
20233 {
20234   tree name;
20235
20236   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20237   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20238   name = cp_parser_identifier (parser);
20239   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20240
20241   return objc_get_class_ivars (name);
20242 }
20243
20244 /* Parse an Objective-C protocol expression.
20245
20246   objc-protocol-expression:
20247     @protocol ( identifier )
20248
20249   Returns a representation of the protocol expression.  */
20250
20251 static tree
20252 cp_parser_objc_protocol_expression (cp_parser* parser)
20253 {
20254   tree proto;
20255
20256   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20257   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20258   proto = cp_parser_identifier (parser);
20259   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20260
20261   return objc_build_protocol_expr (proto);
20262 }
20263
20264 /* Parse an Objective-C selector expression.
20265
20266    objc-selector-expression:
20267      @selector ( objc-method-signature )
20268
20269    objc-method-signature:
20270      objc-selector
20271      objc-selector-seq
20272
20273    objc-selector-seq:
20274      objc-selector :
20275      objc-selector-seq objc-selector :
20276
20277   Returns a representation of the method selector.  */
20278
20279 static tree
20280 cp_parser_objc_selector_expression (cp_parser* parser)
20281 {
20282   tree sel_seq = NULL_TREE;
20283   bool maybe_unary_selector_p = true;
20284   cp_token *token;
20285   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20286
20287   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20288   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20289   token = cp_lexer_peek_token (parser->lexer);
20290
20291   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20292          || token->type == CPP_SCOPE)
20293     {
20294       tree selector = NULL_TREE;
20295
20296       if (token->type != CPP_COLON
20297           || token->type == CPP_SCOPE)
20298         selector = cp_parser_objc_selector (parser);
20299
20300       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20301           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20302         {
20303           /* Detect if we have a unary selector.  */
20304           if (maybe_unary_selector_p)
20305             {
20306               sel_seq = selector;
20307               goto finish_selector;
20308             }
20309           else
20310             {
20311               cp_parser_error (parser, "expected %<:%>");
20312             }
20313         }
20314       maybe_unary_selector_p = false;
20315       token = cp_lexer_consume_token (parser->lexer);
20316
20317       if (token->type == CPP_SCOPE)
20318         {
20319           sel_seq
20320             = chainon (sel_seq,
20321                        build_tree_list (selector, NULL_TREE));
20322           sel_seq
20323             = chainon (sel_seq,
20324                        build_tree_list (NULL_TREE, NULL_TREE));
20325         }
20326       else
20327         sel_seq
20328           = chainon (sel_seq,
20329                      build_tree_list (selector, NULL_TREE));
20330
20331       token = cp_lexer_peek_token (parser->lexer);
20332     }
20333
20334  finish_selector:
20335   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20336
20337   return objc_build_selector_expr (loc, sel_seq);
20338 }
20339
20340 /* Parse a list of identifiers.
20341
20342    objc-identifier-list:
20343      identifier
20344      objc-identifier-list , identifier
20345
20346    Returns a TREE_LIST of identifier nodes.  */
20347
20348 static tree
20349 cp_parser_objc_identifier_list (cp_parser* parser)
20350 {
20351   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20352   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20353
20354   while (sep->type == CPP_COMMA)
20355     {
20356       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20357       list = chainon (list,
20358                       build_tree_list (NULL_TREE,
20359                                        cp_parser_identifier (parser)));
20360       sep = cp_lexer_peek_token (parser->lexer);
20361     }
20362
20363   return list;
20364 }
20365
20366 /* Parse an Objective-C alias declaration.
20367
20368    objc-alias-declaration:
20369      @compatibility_alias identifier identifier ;
20370
20371    This function registers the alias mapping with the Objective-C front end.
20372    It returns nothing.  */
20373
20374 static void
20375 cp_parser_objc_alias_declaration (cp_parser* parser)
20376 {
20377   tree alias, orig;
20378
20379   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20380   alias = cp_parser_identifier (parser);
20381   orig = cp_parser_identifier (parser);
20382   objc_declare_alias (alias, orig);
20383   cp_parser_consume_semicolon_at_end_of_statement (parser);
20384 }
20385
20386 /* Parse an Objective-C class forward-declaration.
20387
20388    objc-class-declaration:
20389      @class objc-identifier-list ;
20390
20391    The function registers the forward declarations with the Objective-C
20392    front end.  It returns nothing.  */
20393
20394 static void
20395 cp_parser_objc_class_declaration (cp_parser* parser)
20396 {
20397   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20398   objc_declare_class (cp_parser_objc_identifier_list (parser));
20399   cp_parser_consume_semicolon_at_end_of_statement (parser);
20400 }
20401
20402 /* Parse a list of Objective-C protocol references.
20403
20404    objc-protocol-refs-opt:
20405      objc-protocol-refs [opt]
20406
20407    objc-protocol-refs:
20408      < objc-identifier-list >
20409
20410    Returns a TREE_LIST of identifiers, if any.  */
20411
20412 static tree
20413 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20414 {
20415   tree protorefs = NULL_TREE;
20416
20417   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20418     {
20419       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20420       protorefs = cp_parser_objc_identifier_list (parser);
20421       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20422     }
20423
20424   return protorefs;
20425 }
20426
20427 /* Parse a Objective-C visibility specification.  */
20428
20429 static void
20430 cp_parser_objc_visibility_spec (cp_parser* parser)
20431 {
20432   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20433
20434   switch (vis->keyword)
20435     {
20436     case RID_AT_PRIVATE:
20437       objc_set_visibility (2);
20438       break;
20439     case RID_AT_PROTECTED:
20440       objc_set_visibility (0);
20441       break;
20442     case RID_AT_PUBLIC:
20443       objc_set_visibility (1);
20444       break;
20445     default:
20446       return;
20447     }
20448
20449   /* Eat '@private'/'@protected'/'@public'.  */
20450   cp_lexer_consume_token (parser->lexer);
20451 }
20452
20453 /* Parse an Objective-C method type.  */
20454
20455 static void
20456 cp_parser_objc_method_type (cp_parser* parser)
20457 {
20458   objc_set_method_type
20459    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20460     ? PLUS_EXPR
20461     : MINUS_EXPR);
20462 }
20463
20464 /* Parse an Objective-C protocol qualifier.  */
20465
20466 static tree
20467 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20468 {
20469   tree quals = NULL_TREE, node;
20470   cp_token *token = cp_lexer_peek_token (parser->lexer);
20471
20472   node = token->u.value;
20473
20474   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20475          && (node == ridpointers [(int) RID_IN]
20476              || node == ridpointers [(int) RID_OUT]
20477              || node == ridpointers [(int) RID_INOUT]
20478              || node == ridpointers [(int) RID_BYCOPY]
20479              || node == ridpointers [(int) RID_BYREF]
20480              || node == ridpointers [(int) RID_ONEWAY]))
20481     {
20482       quals = tree_cons (NULL_TREE, node, quals);
20483       cp_lexer_consume_token (parser->lexer);
20484       token = cp_lexer_peek_token (parser->lexer);
20485       node = token->u.value;
20486     }
20487
20488   return quals;
20489 }
20490
20491 /* Parse an Objective-C typename.  */
20492
20493 static tree
20494 cp_parser_objc_typename (cp_parser* parser)
20495 {
20496   tree type_name = NULL_TREE;
20497
20498   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20499     {
20500       tree proto_quals, cp_type = NULL_TREE;
20501
20502       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20503       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20504
20505       /* An ObjC type name may consist of just protocol qualifiers, in which
20506          case the type shall default to 'id'.  */
20507       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20508         cp_type = cp_parser_type_id (parser);
20509
20510       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20511       type_name = build_tree_list (proto_quals, cp_type);
20512     }
20513
20514   return type_name;
20515 }
20516
20517 /* Check to see if TYPE refers to an Objective-C selector name.  */
20518
20519 static bool
20520 cp_parser_objc_selector_p (enum cpp_ttype type)
20521 {
20522   return (type == CPP_NAME || type == CPP_KEYWORD
20523           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20524           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20525           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20526           || type == CPP_XOR || type == CPP_XOR_EQ);
20527 }
20528
20529 /* Parse an Objective-C selector.  */
20530
20531 static tree
20532 cp_parser_objc_selector (cp_parser* parser)
20533 {
20534   cp_token *token = cp_lexer_consume_token (parser->lexer);
20535
20536   if (!cp_parser_objc_selector_p (token->type))
20537     {
20538       error_at (token->location, "invalid Objective-C++ selector name");
20539       return error_mark_node;
20540     }
20541
20542   /* C++ operator names are allowed to appear in ObjC selectors.  */
20543   switch (token->type)
20544     {
20545     case CPP_AND_AND: return get_identifier ("and");
20546     case CPP_AND_EQ: return get_identifier ("and_eq");
20547     case CPP_AND: return get_identifier ("bitand");
20548     case CPP_OR: return get_identifier ("bitor");
20549     case CPP_COMPL: return get_identifier ("compl");
20550     case CPP_NOT: return get_identifier ("not");
20551     case CPP_NOT_EQ: return get_identifier ("not_eq");
20552     case CPP_OR_OR: return get_identifier ("or");
20553     case CPP_OR_EQ: return get_identifier ("or_eq");
20554     case CPP_XOR: return get_identifier ("xor");
20555     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20556     default: return token->u.value;
20557     }
20558 }
20559
20560 /* Parse an Objective-C params list.  */
20561
20562 static tree
20563 cp_parser_objc_method_keyword_params (cp_parser* parser)
20564 {
20565   tree params = NULL_TREE;
20566   bool maybe_unary_selector_p = true;
20567   cp_token *token = cp_lexer_peek_token (parser->lexer);
20568
20569   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20570     {
20571       tree selector = NULL_TREE, type_name, identifier;
20572
20573       if (token->type != CPP_COLON)
20574         selector = cp_parser_objc_selector (parser);
20575
20576       /* Detect if we have a unary selector.  */
20577       if (maybe_unary_selector_p
20578           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20579         return selector;
20580
20581       maybe_unary_selector_p = false;
20582       cp_parser_require (parser, CPP_COLON, "%<:%>");
20583       type_name = cp_parser_objc_typename (parser);
20584       identifier = cp_parser_identifier (parser);
20585
20586       params
20587         = chainon (params,
20588                    objc_build_keyword_decl (selector,
20589                                             type_name,
20590                                             identifier));
20591
20592       token = cp_lexer_peek_token (parser->lexer);
20593     }
20594
20595   return params;
20596 }
20597
20598 /* Parse the non-keyword Objective-C params.  */
20599
20600 static tree
20601 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20602 {
20603   tree params = make_node (TREE_LIST);
20604   cp_token *token = cp_lexer_peek_token (parser->lexer);
20605   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20606
20607   while (token->type == CPP_COMMA)
20608     {
20609       cp_parameter_declarator *parmdecl;
20610       tree parm;
20611
20612       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20613       token = cp_lexer_peek_token (parser->lexer);
20614
20615       if (token->type == CPP_ELLIPSIS)
20616         {
20617           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20618           *ellipsisp = true;
20619           break;
20620         }
20621
20622       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20623       parm = grokdeclarator (parmdecl->declarator,
20624                              &parmdecl->decl_specifiers,
20625                              PARM, /*initialized=*/0,
20626                              /*attrlist=*/NULL);
20627
20628       chainon (params, build_tree_list (NULL_TREE, parm));
20629       token = cp_lexer_peek_token (parser->lexer);
20630     }
20631
20632   return params;
20633 }
20634
20635 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20636
20637 static void
20638 cp_parser_objc_interstitial_code (cp_parser* parser)
20639 {
20640   cp_token *token = cp_lexer_peek_token (parser->lexer);
20641
20642   /* If the next token is `extern' and the following token is a string
20643      literal, then we have a linkage specification.  */
20644   if (token->keyword == RID_EXTERN
20645       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20646     cp_parser_linkage_specification (parser);
20647   /* Handle #pragma, if any.  */
20648   else if (token->type == CPP_PRAGMA)
20649     cp_parser_pragma (parser, pragma_external);
20650   /* Allow stray semicolons.  */
20651   else if (token->type == CPP_SEMICOLON)
20652     cp_lexer_consume_token (parser->lexer);
20653   /* Finally, try to parse a block-declaration, or a function-definition.  */
20654   else
20655     cp_parser_block_declaration (parser, /*statement_p=*/false);
20656 }
20657
20658 /* Parse a method signature.  */
20659
20660 static tree
20661 cp_parser_objc_method_signature (cp_parser* parser)
20662 {
20663   tree rettype, kwdparms, optparms;
20664   bool ellipsis = false;
20665
20666   cp_parser_objc_method_type (parser);
20667   rettype = cp_parser_objc_typename (parser);
20668   kwdparms = cp_parser_objc_method_keyword_params (parser);
20669   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20670
20671   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20672 }
20673
20674 /* Pars an Objective-C method prototype list.  */
20675
20676 static void
20677 cp_parser_objc_method_prototype_list (cp_parser* parser)
20678 {
20679   cp_token *token = cp_lexer_peek_token (parser->lexer);
20680
20681   while (token->keyword != RID_AT_END)
20682     {
20683       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20684         {
20685           objc_add_method_declaration
20686            (cp_parser_objc_method_signature (parser));
20687           cp_parser_consume_semicolon_at_end_of_statement (parser);
20688         }
20689       else
20690         /* Allow for interspersed non-ObjC++ code.  */
20691         cp_parser_objc_interstitial_code (parser);
20692
20693       token = cp_lexer_peek_token (parser->lexer);
20694     }
20695
20696   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20697   objc_finish_interface ();
20698 }
20699
20700 /* Parse an Objective-C method definition list.  */
20701
20702 static void
20703 cp_parser_objc_method_definition_list (cp_parser* parser)
20704 {
20705   cp_token *token = cp_lexer_peek_token (parser->lexer);
20706
20707   while (token->keyword != RID_AT_END)
20708     {
20709       tree meth;
20710
20711       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20712         {
20713           push_deferring_access_checks (dk_deferred);
20714           objc_start_method_definition
20715            (cp_parser_objc_method_signature (parser));
20716
20717           /* For historical reasons, we accept an optional semicolon.  */
20718           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20719             cp_lexer_consume_token (parser->lexer);
20720
20721           perform_deferred_access_checks ();
20722           stop_deferring_access_checks ();
20723           meth = cp_parser_function_definition_after_declarator (parser,
20724                                                                  false);
20725           pop_deferring_access_checks ();
20726           objc_finish_method_definition (meth);
20727         }
20728       else
20729         /* Allow for interspersed non-ObjC++ code.  */
20730         cp_parser_objc_interstitial_code (parser);
20731
20732       token = cp_lexer_peek_token (parser->lexer);
20733     }
20734
20735   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20736   objc_finish_implementation ();
20737 }
20738
20739 /* Parse Objective-C ivars.  */
20740
20741 static void
20742 cp_parser_objc_class_ivars (cp_parser* parser)
20743 {
20744   cp_token *token = cp_lexer_peek_token (parser->lexer);
20745
20746   if (token->type != CPP_OPEN_BRACE)
20747     return;     /* No ivars specified.  */
20748
20749   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20750   token = cp_lexer_peek_token (parser->lexer);
20751
20752   while (token->type != CPP_CLOSE_BRACE)
20753     {
20754       cp_decl_specifier_seq declspecs;
20755       int decl_class_or_enum_p;
20756       tree prefix_attributes;
20757
20758       cp_parser_objc_visibility_spec (parser);
20759
20760       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20761         break;
20762
20763       cp_parser_decl_specifier_seq (parser,
20764                                     CP_PARSER_FLAGS_OPTIONAL,
20765                                     &declspecs,
20766                                     &decl_class_or_enum_p);
20767       prefix_attributes = declspecs.attributes;
20768       declspecs.attributes = NULL_TREE;
20769
20770       /* Keep going until we hit the `;' at the end of the
20771          declaration.  */
20772       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20773         {
20774           tree width = NULL_TREE, attributes, first_attribute, decl;
20775           cp_declarator *declarator = NULL;
20776           int ctor_dtor_or_conv_p;
20777
20778           /* Check for a (possibly unnamed) bitfield declaration.  */
20779           token = cp_lexer_peek_token (parser->lexer);
20780           if (token->type == CPP_COLON)
20781             goto eat_colon;
20782
20783           if (token->type == CPP_NAME
20784               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20785                   == CPP_COLON))
20786             {
20787               /* Get the name of the bitfield.  */
20788               declarator = make_id_declarator (NULL_TREE,
20789                                                cp_parser_identifier (parser),
20790                                                sfk_none);
20791
20792              eat_colon:
20793               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20794               /* Get the width of the bitfield.  */
20795               width
20796                 = cp_parser_constant_expression (parser,
20797                                                  /*allow_non_constant=*/false,
20798                                                  NULL);
20799             }
20800           else
20801             {
20802               /* Parse the declarator.  */
20803               declarator
20804                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20805                                         &ctor_dtor_or_conv_p,
20806                                         /*parenthesized_p=*/NULL,
20807                                         /*member_p=*/false);
20808             }
20809
20810           /* Look for attributes that apply to the ivar.  */
20811           attributes = cp_parser_attributes_opt (parser);
20812           /* Remember which attributes are prefix attributes and
20813              which are not.  */
20814           first_attribute = attributes;
20815           /* Combine the attributes.  */
20816           attributes = chainon (prefix_attributes, attributes);
20817
20818           if (width)
20819               /* Create the bitfield declaration.  */
20820               decl = grokbitfield (declarator, &declspecs,
20821                                    width,
20822                                    attributes);
20823           else
20824             decl = grokfield (declarator, &declspecs,
20825                               NULL_TREE, /*init_const_expr_p=*/false,
20826                               NULL_TREE, attributes);
20827
20828           /* Add the instance variable.  */
20829           objc_add_instance_variable (decl);
20830
20831           /* Reset PREFIX_ATTRIBUTES.  */
20832           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20833             attributes = TREE_CHAIN (attributes);
20834           if (attributes)
20835             TREE_CHAIN (attributes) = NULL_TREE;
20836
20837           token = cp_lexer_peek_token (parser->lexer);
20838
20839           if (token->type == CPP_COMMA)
20840             {
20841               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20842               continue;
20843             }
20844           break;
20845         }
20846
20847       cp_parser_consume_semicolon_at_end_of_statement (parser);
20848       token = cp_lexer_peek_token (parser->lexer);
20849     }
20850
20851   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20852   /* For historical reasons, we accept an optional semicolon.  */
20853   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20854     cp_lexer_consume_token (parser->lexer);
20855 }
20856
20857 /* Parse an Objective-C protocol declaration.  */
20858
20859 static void
20860 cp_parser_objc_protocol_declaration (cp_parser* parser)
20861 {
20862   tree proto, protorefs;
20863   cp_token *tok;
20864
20865   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20866   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20867     {
20868       tok = cp_lexer_peek_token (parser->lexer);
20869       error_at (tok->location, "identifier expected after %<@protocol%>");
20870       goto finish;
20871     }
20872
20873   /* See if we have a forward declaration or a definition.  */
20874   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20875
20876   /* Try a forward declaration first.  */
20877   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20878     {
20879       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20880      finish:
20881       cp_parser_consume_semicolon_at_end_of_statement (parser);
20882     }
20883
20884   /* Ok, we got a full-fledged definition (or at least should).  */
20885   else
20886     {
20887       proto = cp_parser_identifier (parser);
20888       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20889       objc_start_protocol (proto, protorefs);
20890       cp_parser_objc_method_prototype_list (parser);
20891     }
20892 }
20893
20894 /* Parse an Objective-C superclass or category.  */
20895
20896 static void
20897 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20898                                                           tree *categ)
20899 {
20900   cp_token *next = cp_lexer_peek_token (parser->lexer);
20901
20902   *super = *categ = NULL_TREE;
20903   if (next->type == CPP_COLON)
20904     {
20905       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20906       *super = cp_parser_identifier (parser);
20907     }
20908   else if (next->type == CPP_OPEN_PAREN)
20909     {
20910       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20911       *categ = cp_parser_identifier (parser);
20912       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20913     }
20914 }
20915
20916 /* Parse an Objective-C class interface.  */
20917
20918 static void
20919 cp_parser_objc_class_interface (cp_parser* parser)
20920 {
20921   tree name, super, categ, protos;
20922
20923   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20924   name = cp_parser_identifier (parser);
20925   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20926   protos = cp_parser_objc_protocol_refs_opt (parser);
20927
20928   /* We have either a class or a category on our hands.  */
20929   if (categ)
20930     objc_start_category_interface (name, categ, protos);
20931   else
20932     {
20933       objc_start_class_interface (name, super, protos);
20934       /* Handle instance variable declarations, if any.  */
20935       cp_parser_objc_class_ivars (parser);
20936       objc_continue_interface ();
20937     }
20938
20939   cp_parser_objc_method_prototype_list (parser);
20940 }
20941
20942 /* Parse an Objective-C class implementation.  */
20943
20944 static void
20945 cp_parser_objc_class_implementation (cp_parser* parser)
20946 {
20947   tree name, super, categ;
20948
20949   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20950   name = cp_parser_identifier (parser);
20951   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20952
20953   /* We have either a class or a category on our hands.  */
20954   if (categ)
20955     objc_start_category_implementation (name, categ);
20956   else
20957     {
20958       objc_start_class_implementation (name, super);
20959       /* Handle instance variable declarations, if any.  */
20960       cp_parser_objc_class_ivars (parser);
20961       objc_continue_implementation ();
20962     }
20963
20964   cp_parser_objc_method_definition_list (parser);
20965 }
20966
20967 /* Consume the @end token and finish off the implementation.  */
20968
20969 static void
20970 cp_parser_objc_end_implementation (cp_parser* parser)
20971 {
20972   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20973   objc_finish_implementation ();
20974 }
20975
20976 /* Parse an Objective-C declaration.  */
20977
20978 static void
20979 cp_parser_objc_declaration (cp_parser* parser)
20980 {
20981   /* Try to figure out what kind of declaration is present.  */
20982   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20983
20984   switch (kwd->keyword)
20985     {
20986     case RID_AT_ALIAS:
20987       cp_parser_objc_alias_declaration (parser);
20988       break;
20989     case RID_AT_CLASS:
20990       cp_parser_objc_class_declaration (parser);
20991       break;
20992     case RID_AT_PROTOCOL:
20993       cp_parser_objc_protocol_declaration (parser);
20994       break;
20995     case RID_AT_INTERFACE:
20996       cp_parser_objc_class_interface (parser);
20997       break;
20998     case RID_AT_IMPLEMENTATION:
20999       cp_parser_objc_class_implementation (parser);
21000       break;
21001     case RID_AT_END:
21002       cp_parser_objc_end_implementation (parser);
21003       break;
21004     default:
21005       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21006                 kwd->u.value);
21007       cp_parser_skip_to_end_of_block_or_statement (parser);
21008     }
21009 }
21010
21011 /* Parse an Objective-C try-catch-finally statement.
21012
21013    objc-try-catch-finally-stmt:
21014      @try compound-statement objc-catch-clause-seq [opt]
21015        objc-finally-clause [opt]
21016
21017    objc-catch-clause-seq:
21018      objc-catch-clause objc-catch-clause-seq [opt]
21019
21020    objc-catch-clause:
21021      @catch ( exception-declaration ) compound-statement
21022
21023    objc-finally-clause
21024      @finally compound-statement
21025
21026    Returns NULL_TREE.  */
21027
21028 static tree
21029 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21030   location_t location;
21031   tree stmt;
21032
21033   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21034   location = cp_lexer_peek_token (parser->lexer)->location;
21035   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21036      node, lest it get absorbed into the surrounding block.  */
21037   stmt = push_stmt_list ();
21038   cp_parser_compound_statement (parser, NULL, false);
21039   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21040
21041   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21042     {
21043       cp_parameter_declarator *parmdecl;
21044       tree parm;
21045
21046       cp_lexer_consume_token (parser->lexer);
21047       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21048       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21049       parm = grokdeclarator (parmdecl->declarator,
21050                              &parmdecl->decl_specifiers,
21051                              PARM, /*initialized=*/0,
21052                              /*attrlist=*/NULL);
21053       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21054       objc_begin_catch_clause (parm);
21055       cp_parser_compound_statement (parser, NULL, false);
21056       objc_finish_catch_clause ();
21057     }
21058
21059   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21060     {
21061       cp_lexer_consume_token (parser->lexer);
21062       location = cp_lexer_peek_token (parser->lexer)->location;
21063       /* NB: The @finally 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       objc_build_finally_clause (location, pop_stmt_list (stmt));
21068     }
21069
21070   return objc_finish_try_stmt ();
21071 }
21072
21073 /* Parse an Objective-C synchronized statement.
21074
21075    objc-synchronized-stmt:
21076      @synchronized ( expression ) compound-statement
21077
21078    Returns NULL_TREE.  */
21079
21080 static tree
21081 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21082   location_t location;
21083   tree lock, stmt;
21084
21085   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21086
21087   location = cp_lexer_peek_token (parser->lexer)->location;
21088   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21089   lock = cp_parser_expression (parser, false, NULL);
21090   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21091
21092   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21093      node, lest it get absorbed into the surrounding block.  */
21094   stmt = push_stmt_list ();
21095   cp_parser_compound_statement (parser, NULL, false);
21096
21097   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21098 }
21099
21100 /* Parse an Objective-C throw statement.
21101
21102    objc-throw-stmt:
21103      @throw assignment-expression [opt] ;
21104
21105    Returns a constructed '@throw' statement.  */
21106
21107 static tree
21108 cp_parser_objc_throw_statement (cp_parser *parser) {
21109   tree expr = NULL_TREE;
21110   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21111
21112   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21113
21114   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21115     expr = cp_parser_assignment_expression (parser, false, NULL);
21116
21117   cp_parser_consume_semicolon_at_end_of_statement (parser);
21118
21119   return objc_build_throw_stmt (loc, expr);
21120 }
21121
21122 /* Parse an Objective-C statement.  */
21123
21124 static tree
21125 cp_parser_objc_statement (cp_parser * parser) {
21126   /* Try to figure out what kind of declaration is present.  */
21127   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21128
21129   switch (kwd->keyword)
21130     {
21131     case RID_AT_TRY:
21132       return cp_parser_objc_try_catch_finally_statement (parser);
21133     case RID_AT_SYNCHRONIZED:
21134       return cp_parser_objc_synchronized_statement (parser);
21135     case RID_AT_THROW:
21136       return cp_parser_objc_throw_statement (parser);
21137     default:
21138       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21139                kwd->u.value);
21140       cp_parser_skip_to_end_of_block_or_statement (parser);
21141     }
21142
21143   return error_mark_node;
21144 }
21145 \f
21146 /* OpenMP 2.5 parsing routines.  */
21147
21148 /* Returns name of the next clause.
21149    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21150    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21151    returned and the token is consumed.  */
21152
21153 static pragma_omp_clause
21154 cp_parser_omp_clause_name (cp_parser *parser)
21155 {
21156   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21157
21158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21159     result = PRAGMA_OMP_CLAUSE_IF;
21160   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21161     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21162   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21163     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21164   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21165     {
21166       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21167       const char *p = IDENTIFIER_POINTER (id);
21168
21169       switch (p[0])
21170         {
21171         case 'c':
21172           if (!strcmp ("collapse", p))
21173             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21174           else if (!strcmp ("copyin", p))
21175             result = PRAGMA_OMP_CLAUSE_COPYIN;
21176           else if (!strcmp ("copyprivate", p))
21177             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21178           break;
21179         case 'f':
21180           if (!strcmp ("firstprivate", p))
21181             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21182           break;
21183         case 'l':
21184           if (!strcmp ("lastprivate", p))
21185             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21186           break;
21187         case 'n':
21188           if (!strcmp ("nowait", p))
21189             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21190           else if (!strcmp ("num_threads", p))
21191             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21192           break;
21193         case 'o':
21194           if (!strcmp ("ordered", p))
21195             result = PRAGMA_OMP_CLAUSE_ORDERED;
21196           break;
21197         case 'r':
21198           if (!strcmp ("reduction", p))
21199             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21200           break;
21201         case 's':
21202           if (!strcmp ("schedule", p))
21203             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21204           else if (!strcmp ("shared", p))
21205             result = PRAGMA_OMP_CLAUSE_SHARED;
21206           break;
21207         case 'u':
21208           if (!strcmp ("untied", p))
21209             result = PRAGMA_OMP_CLAUSE_UNTIED;
21210           break;
21211         }
21212     }
21213
21214   if (result != PRAGMA_OMP_CLAUSE_NONE)
21215     cp_lexer_consume_token (parser->lexer);
21216
21217   return result;
21218 }
21219
21220 /* Validate that a clause of the given type does not already exist.  */
21221
21222 static void
21223 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21224                            const char *name, location_t location)
21225 {
21226   tree c;
21227
21228   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21229     if (OMP_CLAUSE_CODE (c) == code)
21230       {
21231         error_at (location, "too many %qs clauses", name);
21232         break;
21233       }
21234 }
21235
21236 /* OpenMP 2.5:
21237    variable-list:
21238      identifier
21239      variable-list , identifier
21240
21241    In addition, we match a closing parenthesis.  An opening parenthesis
21242    will have been consumed by the caller.
21243
21244    If KIND is nonzero, create the appropriate node and install the decl
21245    in OMP_CLAUSE_DECL and add the node to the head of the list.
21246
21247    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21248    return the list created.  */
21249
21250 static tree
21251 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21252                                 tree list)
21253 {
21254   cp_token *token;
21255   while (1)
21256     {
21257       tree name, decl;
21258
21259       token = cp_lexer_peek_token (parser->lexer);
21260       name = cp_parser_id_expression (parser, /*template_p=*/false,
21261                                       /*check_dependency_p=*/true,
21262                                       /*template_p=*/NULL,
21263                                       /*declarator_p=*/false,
21264                                       /*optional_p=*/false);
21265       if (name == error_mark_node)
21266         goto skip_comma;
21267
21268       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21269       if (decl == error_mark_node)
21270         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21271       else if (kind != 0)
21272         {
21273           tree u = build_omp_clause (token->location, kind);
21274           OMP_CLAUSE_DECL (u) = decl;
21275           OMP_CLAUSE_CHAIN (u) = list;
21276           list = u;
21277         }
21278       else
21279         list = tree_cons (decl, NULL_TREE, list);
21280
21281     get_comma:
21282       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21283         break;
21284       cp_lexer_consume_token (parser->lexer);
21285     }
21286
21287   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21288     {
21289       int ending;
21290
21291       /* Try to resync to an unnested comma.  Copied from
21292          cp_parser_parenthesized_expression_list.  */
21293     skip_comma:
21294       ending = cp_parser_skip_to_closing_parenthesis (parser,
21295                                                       /*recovering=*/true,
21296                                                       /*or_comma=*/true,
21297                                                       /*consume_paren=*/true);
21298       if (ending < 0)
21299         goto get_comma;
21300     }
21301
21302   return list;
21303 }
21304
21305 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21306    common case for omp clauses.  */
21307
21308 static tree
21309 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21310 {
21311   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21312     return cp_parser_omp_var_list_no_open (parser, kind, list);
21313   return list;
21314 }
21315
21316 /* OpenMP 3.0:
21317    collapse ( constant-expression ) */
21318
21319 static tree
21320 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21321 {
21322   tree c, num;
21323   location_t loc;
21324   HOST_WIDE_INT n;
21325
21326   loc = cp_lexer_peek_token (parser->lexer)->location;
21327   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21328     return list;
21329
21330   num = cp_parser_constant_expression (parser, false, NULL);
21331
21332   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21333     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21334                                            /*or_comma=*/false,
21335                                            /*consume_paren=*/true);
21336
21337   if (num == error_mark_node)
21338     return list;
21339   num = fold_non_dependent_expr (num);
21340   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21341       || !host_integerp (num, 0)
21342       || (n = tree_low_cst (num, 0)) <= 0
21343       || (int) n != n)
21344     {
21345       error_at (loc, "collapse argument needs positive constant integer expression");
21346       return list;
21347     }
21348
21349   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21350   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21351   OMP_CLAUSE_CHAIN (c) = list;
21352   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21353
21354   return c;
21355 }
21356
21357 /* OpenMP 2.5:
21358    default ( shared | none ) */
21359
21360 static tree
21361 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21362 {
21363   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21364   tree c;
21365
21366   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21367     return list;
21368   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21369     {
21370       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21371       const char *p = IDENTIFIER_POINTER (id);
21372
21373       switch (p[0])
21374         {
21375         case 'n':
21376           if (strcmp ("none", p) != 0)
21377             goto invalid_kind;
21378           kind = OMP_CLAUSE_DEFAULT_NONE;
21379           break;
21380
21381         case 's':
21382           if (strcmp ("shared", p) != 0)
21383             goto invalid_kind;
21384           kind = OMP_CLAUSE_DEFAULT_SHARED;
21385           break;
21386
21387         default:
21388           goto invalid_kind;
21389         }
21390
21391       cp_lexer_consume_token (parser->lexer);
21392     }
21393   else
21394     {
21395     invalid_kind:
21396       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21397     }
21398
21399   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21400     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21401                                            /*or_comma=*/false,
21402                                            /*consume_paren=*/true);
21403
21404   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21405     return list;
21406
21407   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21408   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21409   OMP_CLAUSE_CHAIN (c) = list;
21410   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21411
21412   return c;
21413 }
21414
21415 /* OpenMP 2.5:
21416    if ( expression ) */
21417
21418 static tree
21419 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21420 {
21421   tree t, c;
21422
21423   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21424     return list;
21425
21426   t = cp_parser_condition (parser);
21427
21428   if (t == error_mark_node
21429       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21430     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21431                                            /*or_comma=*/false,
21432                                            /*consume_paren=*/true);
21433
21434   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21435
21436   c = build_omp_clause (location, OMP_CLAUSE_IF);
21437   OMP_CLAUSE_IF_EXPR (c) = t;
21438   OMP_CLAUSE_CHAIN (c) = list;
21439
21440   return c;
21441 }
21442
21443 /* OpenMP 2.5:
21444    nowait */
21445
21446 static tree
21447 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21448                              tree list, location_t location)
21449 {
21450   tree c;
21451
21452   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21453
21454   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21455   OMP_CLAUSE_CHAIN (c) = list;
21456   return c;
21457 }
21458
21459 /* OpenMP 2.5:
21460    num_threads ( expression ) */
21461
21462 static tree
21463 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21464                                   location_t location)
21465 {
21466   tree t, c;
21467
21468   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21469     return list;
21470
21471   t = cp_parser_expression (parser, false, NULL);
21472
21473   if (t == error_mark_node
21474       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21475     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21476                                            /*or_comma=*/false,
21477                                            /*consume_paren=*/true);
21478
21479   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21480                              "num_threads", location);
21481
21482   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21483   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21484   OMP_CLAUSE_CHAIN (c) = list;
21485
21486   return c;
21487 }
21488
21489 /* OpenMP 2.5:
21490    ordered */
21491
21492 static tree
21493 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21494                               tree list, location_t location)
21495 {
21496   tree c;
21497
21498   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21499                              "ordered", location);
21500
21501   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21502   OMP_CLAUSE_CHAIN (c) = list;
21503   return c;
21504 }
21505
21506 /* OpenMP 2.5:
21507    reduction ( reduction-operator : variable-list )
21508
21509    reduction-operator:
21510      One of: + * - & ^ | && || */
21511
21512 static tree
21513 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21514 {
21515   enum tree_code code;
21516   tree nlist, c;
21517
21518   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21519     return list;
21520
21521   switch (cp_lexer_peek_token (parser->lexer)->type)
21522     {
21523     case CPP_PLUS:
21524       code = PLUS_EXPR;
21525       break;
21526     case CPP_MULT:
21527       code = MULT_EXPR;
21528       break;
21529     case CPP_MINUS:
21530       code = MINUS_EXPR;
21531       break;
21532     case CPP_AND:
21533       code = BIT_AND_EXPR;
21534       break;
21535     case CPP_XOR:
21536       code = BIT_XOR_EXPR;
21537       break;
21538     case CPP_OR:
21539       code = BIT_IOR_EXPR;
21540       break;
21541     case CPP_AND_AND:
21542       code = TRUTH_ANDIF_EXPR;
21543       break;
21544     case CPP_OR_OR:
21545       code = TRUTH_ORIF_EXPR;
21546       break;
21547     default:
21548       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21549                                "%<|%>, %<&&%>, or %<||%>");
21550     resync_fail:
21551       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21552                                              /*or_comma=*/false,
21553                                              /*consume_paren=*/true);
21554       return list;
21555     }
21556   cp_lexer_consume_token (parser->lexer);
21557
21558   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21559     goto resync_fail;
21560
21561   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21562   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21563     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21564
21565   return nlist;
21566 }
21567
21568 /* OpenMP 2.5:
21569    schedule ( schedule-kind )
21570    schedule ( schedule-kind , expression )
21571
21572    schedule-kind:
21573      static | dynamic | guided | runtime | auto  */
21574
21575 static tree
21576 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21577 {
21578   tree c, t;
21579
21580   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21581     return list;
21582
21583   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21584
21585   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21586     {
21587       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21588       const char *p = IDENTIFIER_POINTER (id);
21589
21590       switch (p[0])
21591         {
21592         case 'd':
21593           if (strcmp ("dynamic", p) != 0)
21594             goto invalid_kind;
21595           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21596           break;
21597
21598         case 'g':
21599           if (strcmp ("guided", p) != 0)
21600             goto invalid_kind;
21601           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21602           break;
21603
21604         case 'r':
21605           if (strcmp ("runtime", p) != 0)
21606             goto invalid_kind;
21607           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21608           break;
21609
21610         default:
21611           goto invalid_kind;
21612         }
21613     }
21614   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21615     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21616   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21617     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21618   else
21619     goto invalid_kind;
21620   cp_lexer_consume_token (parser->lexer);
21621
21622   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21623     {
21624       cp_token *token;
21625       cp_lexer_consume_token (parser->lexer);
21626
21627       token = cp_lexer_peek_token (parser->lexer);
21628       t = cp_parser_assignment_expression (parser, false, NULL);
21629
21630       if (t == error_mark_node)
21631         goto resync_fail;
21632       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21633         error_at (token->location, "schedule %<runtime%> does not take "
21634                   "a %<chunk_size%> parameter");
21635       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21636         error_at (token->location, "schedule %<auto%> does not take "
21637                   "a %<chunk_size%> parameter");
21638       else
21639         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21640
21641       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21642         goto resync_fail;
21643     }
21644   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21645     goto resync_fail;
21646
21647   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21648   OMP_CLAUSE_CHAIN (c) = list;
21649   return c;
21650
21651  invalid_kind:
21652   cp_parser_error (parser, "invalid schedule kind");
21653  resync_fail:
21654   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21655                                          /*or_comma=*/false,
21656                                          /*consume_paren=*/true);
21657   return list;
21658 }
21659
21660 /* OpenMP 3.0:
21661    untied */
21662
21663 static tree
21664 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21665                              tree list, location_t location)
21666 {
21667   tree c;
21668
21669   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21670
21671   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21672   OMP_CLAUSE_CHAIN (c) = list;
21673   return c;
21674 }
21675
21676 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21677    is a bitmask in MASK.  Return the list of clauses found; the result
21678    of clause default goes in *pdefault.  */
21679
21680 static tree
21681 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21682                            const char *where, cp_token *pragma_tok)
21683 {
21684   tree clauses = NULL;
21685   bool first = true;
21686   cp_token *token = NULL;
21687
21688   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21689     {
21690       pragma_omp_clause c_kind;
21691       const char *c_name;
21692       tree prev = clauses;
21693
21694       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21695         cp_lexer_consume_token (parser->lexer);
21696
21697       token = cp_lexer_peek_token (parser->lexer);
21698       c_kind = cp_parser_omp_clause_name (parser);
21699       first = false;
21700
21701       switch (c_kind)
21702         {
21703         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21704           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21705                                                    token->location);
21706           c_name = "collapse";
21707           break;
21708         case PRAGMA_OMP_CLAUSE_COPYIN:
21709           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21710           c_name = "copyin";
21711           break;
21712         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21713           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21714                                             clauses);
21715           c_name = "copyprivate";
21716           break;
21717         case PRAGMA_OMP_CLAUSE_DEFAULT:
21718           clauses = cp_parser_omp_clause_default (parser, clauses,
21719                                                   token->location);
21720           c_name = "default";
21721           break;
21722         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21723           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21724                                             clauses);
21725           c_name = "firstprivate";
21726           break;
21727         case PRAGMA_OMP_CLAUSE_IF:
21728           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21729           c_name = "if";
21730           break;
21731         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21732           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21733                                             clauses);
21734           c_name = "lastprivate";
21735           break;
21736         case PRAGMA_OMP_CLAUSE_NOWAIT:
21737           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21738           c_name = "nowait";
21739           break;
21740         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21741           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21742                                                       token->location);
21743           c_name = "num_threads";
21744           break;
21745         case PRAGMA_OMP_CLAUSE_ORDERED:
21746           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21747                                                   token->location);
21748           c_name = "ordered";
21749           break;
21750         case PRAGMA_OMP_CLAUSE_PRIVATE:
21751           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21752                                             clauses);
21753           c_name = "private";
21754           break;
21755         case PRAGMA_OMP_CLAUSE_REDUCTION:
21756           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21757           c_name = "reduction";
21758           break;
21759         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21760           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21761                                                    token->location);
21762           c_name = "schedule";
21763           break;
21764         case PRAGMA_OMP_CLAUSE_SHARED:
21765           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21766                                             clauses);
21767           c_name = "shared";
21768           break;
21769         case PRAGMA_OMP_CLAUSE_UNTIED:
21770           clauses = cp_parser_omp_clause_untied (parser, clauses,
21771                                                  token->location);
21772           c_name = "nowait";
21773           break;
21774         default:
21775           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21776           goto saw_error;
21777         }
21778
21779       if (((mask >> c_kind) & 1) == 0)
21780         {
21781           /* Remove the invalid clause(s) from the list to avoid
21782              confusing the rest of the compiler.  */
21783           clauses = prev;
21784           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21785         }
21786     }
21787  saw_error:
21788   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21789   return finish_omp_clauses (clauses);
21790 }
21791
21792 /* OpenMP 2.5:
21793    structured-block:
21794      statement
21795
21796    In practice, we're also interested in adding the statement to an
21797    outer node.  So it is convenient if we work around the fact that
21798    cp_parser_statement calls add_stmt.  */
21799
21800 static unsigned
21801 cp_parser_begin_omp_structured_block (cp_parser *parser)
21802 {
21803   unsigned save = parser->in_statement;
21804
21805   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21806      This preserves the "not within loop or switch" style error messages
21807      for nonsense cases like
21808         void foo() {
21809         #pragma omp single
21810           break;
21811         }
21812   */
21813   if (parser->in_statement)
21814     parser->in_statement = IN_OMP_BLOCK;
21815
21816   return save;
21817 }
21818
21819 static void
21820 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21821 {
21822   parser->in_statement = save;
21823 }
21824
21825 static tree
21826 cp_parser_omp_structured_block (cp_parser *parser)
21827 {
21828   tree stmt = begin_omp_structured_block ();
21829   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21830
21831   cp_parser_statement (parser, NULL_TREE, false, NULL);
21832
21833   cp_parser_end_omp_structured_block (parser, save);
21834   return finish_omp_structured_block (stmt);
21835 }
21836
21837 /* OpenMP 2.5:
21838    # pragma omp atomic new-line
21839      expression-stmt
21840
21841    expression-stmt:
21842      x binop= expr | x++ | ++x | x-- | --x
21843    binop:
21844      +, *, -, /, &, ^, |, <<, >>
21845
21846   where x is an lvalue expression with scalar type.  */
21847
21848 static void
21849 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21850 {
21851   tree lhs, rhs;
21852   enum tree_code code;
21853
21854   cp_parser_require_pragma_eol (parser, pragma_tok);
21855
21856   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21857                                     /*cast_p=*/false, NULL);
21858   switch (TREE_CODE (lhs))
21859     {
21860     case ERROR_MARK:
21861       goto saw_error;
21862
21863     case PREINCREMENT_EXPR:
21864     case POSTINCREMENT_EXPR:
21865       lhs = TREE_OPERAND (lhs, 0);
21866       code = PLUS_EXPR;
21867       rhs = integer_one_node;
21868       break;
21869
21870     case PREDECREMENT_EXPR:
21871     case POSTDECREMENT_EXPR:
21872       lhs = TREE_OPERAND (lhs, 0);
21873       code = MINUS_EXPR;
21874       rhs = integer_one_node;
21875       break;
21876
21877     default:
21878       switch (cp_lexer_peek_token (parser->lexer)->type)
21879         {
21880         case CPP_MULT_EQ:
21881           code = MULT_EXPR;
21882           break;
21883         case CPP_DIV_EQ:
21884           code = TRUNC_DIV_EXPR;
21885           break;
21886         case CPP_PLUS_EQ:
21887           code = PLUS_EXPR;
21888           break;
21889         case CPP_MINUS_EQ:
21890           code = MINUS_EXPR;
21891           break;
21892         case CPP_LSHIFT_EQ:
21893           code = LSHIFT_EXPR;
21894           break;
21895         case CPP_RSHIFT_EQ:
21896           code = RSHIFT_EXPR;
21897           break;
21898         case CPP_AND_EQ:
21899           code = BIT_AND_EXPR;
21900           break;
21901         case CPP_OR_EQ:
21902           code = BIT_IOR_EXPR;
21903           break;
21904         case CPP_XOR_EQ:
21905           code = BIT_XOR_EXPR;
21906           break;
21907         default:
21908           cp_parser_error (parser,
21909                            "invalid operator for %<#pragma omp atomic%>");
21910           goto saw_error;
21911         }
21912       cp_lexer_consume_token (parser->lexer);
21913
21914       rhs = cp_parser_expression (parser, false, NULL);
21915       if (rhs == error_mark_node)
21916         goto saw_error;
21917       break;
21918     }
21919   finish_omp_atomic (code, lhs, rhs);
21920   cp_parser_consume_semicolon_at_end_of_statement (parser);
21921   return;
21922
21923  saw_error:
21924   cp_parser_skip_to_end_of_block_or_statement (parser);
21925 }
21926
21927
21928 /* OpenMP 2.5:
21929    # pragma omp barrier new-line  */
21930
21931 static void
21932 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21933 {
21934   cp_parser_require_pragma_eol (parser, pragma_tok);
21935   finish_omp_barrier ();
21936 }
21937
21938 /* OpenMP 2.5:
21939    # pragma omp critical [(name)] new-line
21940      structured-block  */
21941
21942 static tree
21943 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21944 {
21945   tree stmt, name = NULL;
21946
21947   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21948     {
21949       cp_lexer_consume_token (parser->lexer);
21950
21951       name = cp_parser_identifier (parser);
21952
21953       if (name == error_mark_node
21954           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21955         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21956                                                /*or_comma=*/false,
21957                                                /*consume_paren=*/true);
21958       if (name == error_mark_node)
21959         name = NULL;
21960     }
21961   cp_parser_require_pragma_eol (parser, pragma_tok);
21962
21963   stmt = cp_parser_omp_structured_block (parser);
21964   return c_finish_omp_critical (input_location, stmt, name);
21965 }
21966
21967 /* OpenMP 2.5:
21968    # pragma omp flush flush-vars[opt] new-line
21969
21970    flush-vars:
21971      ( variable-list ) */
21972
21973 static void
21974 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21975 {
21976   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21977     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21978   cp_parser_require_pragma_eol (parser, pragma_tok);
21979
21980   finish_omp_flush ();
21981 }
21982
21983 /* Helper function, to parse omp for increment expression.  */
21984
21985 static tree
21986 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21987 {
21988   tree cond = cp_parser_binary_expression (parser, false, true,
21989                                            PREC_NOT_OPERATOR, NULL);
21990   bool overloaded_p;
21991
21992   if (cond == error_mark_node
21993       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21994     {
21995       cp_parser_skip_to_end_of_statement (parser);
21996       return error_mark_node;
21997     }
21998
21999   switch (TREE_CODE (cond))
22000     {
22001     case GT_EXPR:
22002     case GE_EXPR:
22003     case LT_EXPR:
22004     case LE_EXPR:
22005       break;
22006     default:
22007       return error_mark_node;
22008     }
22009
22010   /* If decl is an iterator, preserve LHS and RHS of the relational
22011      expr until finish_omp_for.  */
22012   if (decl
22013       && (type_dependent_expression_p (decl)
22014           || CLASS_TYPE_P (TREE_TYPE (decl))))
22015     return cond;
22016
22017   return build_x_binary_op (TREE_CODE (cond),
22018                             TREE_OPERAND (cond, 0), ERROR_MARK,
22019                             TREE_OPERAND (cond, 1), ERROR_MARK,
22020                             &overloaded_p, tf_warning_or_error);
22021 }
22022
22023 /* Helper function, to parse omp for increment expression.  */
22024
22025 static tree
22026 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22027 {
22028   cp_token *token = cp_lexer_peek_token (parser->lexer);
22029   enum tree_code op;
22030   tree lhs, rhs;
22031   cp_id_kind idk;
22032   bool decl_first;
22033
22034   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22035     {
22036       op = (token->type == CPP_PLUS_PLUS
22037             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22038       cp_lexer_consume_token (parser->lexer);
22039       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22040       if (lhs != decl)
22041         return error_mark_node;
22042       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22043     }
22044
22045   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22046   if (lhs != decl)
22047     return error_mark_node;
22048
22049   token = cp_lexer_peek_token (parser->lexer);
22050   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22051     {
22052       op = (token->type == CPP_PLUS_PLUS
22053             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22054       cp_lexer_consume_token (parser->lexer);
22055       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22056     }
22057
22058   op = cp_parser_assignment_operator_opt (parser);
22059   if (op == ERROR_MARK)
22060     return error_mark_node;
22061
22062   if (op != NOP_EXPR)
22063     {
22064       rhs = cp_parser_assignment_expression (parser, false, NULL);
22065       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22066       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22067     }
22068
22069   lhs = cp_parser_binary_expression (parser, false, false,
22070                                      PREC_ADDITIVE_EXPRESSION, NULL);
22071   token = cp_lexer_peek_token (parser->lexer);
22072   decl_first = lhs == decl;
22073   if (decl_first)
22074     lhs = NULL_TREE;
22075   if (token->type != CPP_PLUS
22076       && token->type != CPP_MINUS)
22077     return error_mark_node;
22078
22079   do
22080     {
22081       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22082       cp_lexer_consume_token (parser->lexer);
22083       rhs = cp_parser_binary_expression (parser, false, false,
22084                                          PREC_ADDITIVE_EXPRESSION, NULL);
22085       token = cp_lexer_peek_token (parser->lexer);
22086       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22087         {
22088           if (lhs == NULL_TREE)
22089             {
22090               if (op == PLUS_EXPR)
22091                 lhs = rhs;
22092               else
22093                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22094             }
22095           else
22096             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22097                                      NULL, tf_warning_or_error);
22098         }
22099     }
22100   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22101
22102   if (!decl_first)
22103     {
22104       if (rhs != decl || op == MINUS_EXPR)
22105         return error_mark_node;
22106       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22107     }
22108   else
22109     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22110
22111   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22112 }
22113
22114 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22115
22116 static tree
22117 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22118 {
22119   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22120   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22121   tree this_pre_body, cl;
22122   location_t loc_first;
22123   bool collapse_err = false;
22124   int i, collapse = 1, nbraces = 0;
22125
22126   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22127     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22128       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22129
22130   gcc_assert (collapse >= 1);
22131
22132   declv = make_tree_vec (collapse);
22133   initv = make_tree_vec (collapse);
22134   condv = make_tree_vec (collapse);
22135   incrv = make_tree_vec (collapse);
22136
22137   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22138
22139   for (i = 0; i < collapse; i++)
22140     {
22141       int bracecount = 0;
22142       bool add_private_clause = false;
22143       location_t loc;
22144
22145       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22146         {
22147           cp_parser_error (parser, "for statement expected");
22148           return NULL;
22149         }
22150       loc = cp_lexer_consume_token (parser->lexer)->location;
22151
22152       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22153         return NULL;
22154
22155       init = decl = real_decl = NULL;
22156       this_pre_body = push_stmt_list ();
22157       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22158         {
22159           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22160
22161              init-expr:
22162                        var = lb
22163                        integer-type var = lb
22164                        random-access-iterator-type var = lb
22165                        pointer-type var = lb
22166           */
22167           cp_decl_specifier_seq type_specifiers;
22168
22169           /* First, try to parse as an initialized declaration.  See
22170              cp_parser_condition, from whence the bulk of this is copied.  */
22171
22172           cp_parser_parse_tentatively (parser);
22173           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22174                                         /*is_trailing_return=*/false,
22175                                         &type_specifiers);
22176           if (cp_parser_parse_definitely (parser))
22177             {
22178               /* If parsing a type specifier seq succeeded, then this
22179                  MUST be a initialized declaration.  */
22180               tree asm_specification, attributes;
22181               cp_declarator *declarator;
22182
22183               declarator = cp_parser_declarator (parser,
22184                                                  CP_PARSER_DECLARATOR_NAMED,
22185                                                  /*ctor_dtor_or_conv_p=*/NULL,
22186                                                  /*parenthesized_p=*/NULL,
22187                                                  /*member_p=*/false);
22188               attributes = cp_parser_attributes_opt (parser);
22189               asm_specification = cp_parser_asm_specification_opt (parser);
22190
22191               if (declarator == cp_error_declarator) 
22192                 cp_parser_skip_to_end_of_statement (parser);
22193
22194               else 
22195                 {
22196                   tree pushed_scope, auto_node;
22197
22198                   decl = start_decl (declarator, &type_specifiers,
22199                                      SD_INITIALIZED, attributes,
22200                                      /*prefix_attributes=*/NULL_TREE,
22201                                      &pushed_scope);
22202
22203                   auto_node = type_uses_auto (TREE_TYPE (decl));
22204                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22205                     {
22206                       if (cp_lexer_next_token_is (parser->lexer, 
22207                                                   CPP_OPEN_PAREN))
22208                         error ("parenthesized initialization is not allowed in "
22209                                "OpenMP %<for%> loop");
22210                       else
22211                         /* Trigger an error.  */
22212                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22213
22214                       init = error_mark_node;
22215                       cp_parser_skip_to_end_of_statement (parser);
22216                     }
22217                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22218                            || type_dependent_expression_p (decl)
22219                            || auto_node)
22220                     {
22221                       bool is_direct_init, is_non_constant_init;
22222
22223                       init = cp_parser_initializer (parser,
22224                                                     &is_direct_init,
22225                                                     &is_non_constant_init);
22226
22227                       if (auto_node && describable_type (init))
22228                         {
22229                           TREE_TYPE (decl)
22230                             = do_auto_deduction (TREE_TYPE (decl), init,
22231                                                  auto_node);
22232
22233                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22234                               && !type_dependent_expression_p (decl))
22235                             goto non_class;
22236                         }
22237                       
22238                       cp_finish_decl (decl, init, !is_non_constant_init,
22239                                       asm_specification,
22240                                       LOOKUP_ONLYCONVERTING);
22241                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22242                         {
22243                           for_block
22244                             = tree_cons (NULL, this_pre_body, for_block);
22245                           init = NULL_TREE;
22246                         }
22247                       else
22248                         init = pop_stmt_list (this_pre_body);
22249                       this_pre_body = NULL_TREE;
22250                     }
22251                   else
22252                     {
22253                       /* Consume '='.  */
22254                       cp_lexer_consume_token (parser->lexer);
22255                       init = cp_parser_assignment_expression (parser, false, NULL);
22256
22257                     non_class:
22258                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22259                         init = error_mark_node;
22260                       else
22261                         cp_finish_decl (decl, NULL_TREE,
22262                                         /*init_const_expr_p=*/false,
22263                                         asm_specification,
22264                                         LOOKUP_ONLYCONVERTING);
22265                     }
22266
22267                   if (pushed_scope)
22268                     pop_scope (pushed_scope);
22269                 }
22270             }
22271           else 
22272             {
22273               cp_id_kind idk;
22274               /* If parsing a type specifier sequence failed, then
22275                  this MUST be a simple expression.  */
22276               cp_parser_parse_tentatively (parser);
22277               decl = cp_parser_primary_expression (parser, false, false,
22278                                                    false, &idk);
22279               if (!cp_parser_error_occurred (parser)
22280                   && decl
22281                   && DECL_P (decl)
22282                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22283                 {
22284                   tree rhs;
22285
22286                   cp_parser_parse_definitely (parser);
22287                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22288                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22289                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22290                                                          rhs,
22291                                                          tf_warning_or_error));
22292                   add_private_clause = true;
22293                 }
22294               else
22295                 {
22296                   decl = NULL;
22297                   cp_parser_abort_tentative_parse (parser);
22298                   init = cp_parser_expression (parser, false, NULL);
22299                   if (init)
22300                     {
22301                       if (TREE_CODE (init) == MODIFY_EXPR
22302                           || TREE_CODE (init) == MODOP_EXPR)
22303                         real_decl = TREE_OPERAND (init, 0);
22304                     }
22305                 }
22306             }
22307         }
22308       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22309       if (this_pre_body)
22310         {
22311           this_pre_body = pop_stmt_list (this_pre_body);
22312           if (pre_body)
22313             {
22314               tree t = pre_body;
22315               pre_body = push_stmt_list ();
22316               add_stmt (t);
22317               add_stmt (this_pre_body);
22318               pre_body = pop_stmt_list (pre_body);
22319             }
22320           else
22321             pre_body = this_pre_body;
22322         }
22323
22324       if (decl)
22325         real_decl = decl;
22326       if (par_clauses != NULL && real_decl != NULL_TREE)
22327         {
22328           tree *c;
22329           for (c = par_clauses; *c ; )
22330             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22331                 && OMP_CLAUSE_DECL (*c) == real_decl)
22332               {
22333                 error_at (loc, "iteration variable %qD"
22334                           " should not be firstprivate", real_decl);
22335                 *c = OMP_CLAUSE_CHAIN (*c);
22336               }
22337             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22338                      && OMP_CLAUSE_DECL (*c) == real_decl)
22339               {
22340                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22341                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22342                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22343                 OMP_CLAUSE_DECL (l) = real_decl;
22344                 OMP_CLAUSE_CHAIN (l) = clauses;
22345                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22346                 clauses = l;
22347                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22348                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22349                 add_private_clause = false;
22350               }
22351             else
22352               {
22353                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22354                     && OMP_CLAUSE_DECL (*c) == real_decl)
22355                   add_private_clause = false;
22356                 c = &OMP_CLAUSE_CHAIN (*c);
22357               }
22358         }
22359
22360       if (add_private_clause)
22361         {
22362           tree c;
22363           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22364             {
22365               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22366                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22367                   && OMP_CLAUSE_DECL (c) == decl)
22368                 break;
22369               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22370                        && OMP_CLAUSE_DECL (c) == decl)
22371                 error_at (loc, "iteration variable %qD "
22372                           "should not be firstprivate",
22373                           decl);
22374               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22375                        && OMP_CLAUSE_DECL (c) == decl)
22376                 error_at (loc, "iteration variable %qD should not be reduction",
22377                           decl);
22378             }
22379           if (c == NULL)
22380             {
22381               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22382               OMP_CLAUSE_DECL (c) = decl;
22383               c = finish_omp_clauses (c);
22384               if (c)
22385                 {
22386                   OMP_CLAUSE_CHAIN (c) = clauses;
22387                   clauses = c;
22388                 }
22389             }
22390         }
22391
22392       cond = NULL;
22393       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22394         cond = cp_parser_omp_for_cond (parser, decl);
22395       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22396
22397       incr = NULL;
22398       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22399         {
22400           /* If decl is an iterator, preserve the operator on decl
22401              until finish_omp_for.  */
22402           if (decl
22403               && (type_dependent_expression_p (decl)
22404                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22405             incr = cp_parser_omp_for_incr (parser, decl);
22406           else
22407             incr = cp_parser_expression (parser, false, NULL);
22408         }
22409
22410       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22411         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22412                                                /*or_comma=*/false,
22413                                                /*consume_paren=*/true);
22414
22415       TREE_VEC_ELT (declv, i) = decl;
22416       TREE_VEC_ELT (initv, i) = init;
22417       TREE_VEC_ELT (condv, i) = cond;
22418       TREE_VEC_ELT (incrv, i) = incr;
22419
22420       if (i == collapse - 1)
22421         break;
22422
22423       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22424          in between the collapsed for loops to be still considered perfectly
22425          nested.  Hopefully the final version clarifies this.
22426          For now handle (multiple) {'s and empty statements.  */
22427       cp_parser_parse_tentatively (parser);
22428       do
22429         {
22430           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22431             break;
22432           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22433             {
22434               cp_lexer_consume_token (parser->lexer);
22435               bracecount++;
22436             }
22437           else if (bracecount
22438                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22439             cp_lexer_consume_token (parser->lexer);
22440           else
22441             {
22442               loc = cp_lexer_peek_token (parser->lexer)->location;
22443               error_at (loc, "not enough collapsed for loops");
22444               collapse_err = true;
22445               cp_parser_abort_tentative_parse (parser);
22446               declv = NULL_TREE;
22447               break;
22448             }
22449         }
22450       while (1);
22451
22452       if (declv)
22453         {
22454           cp_parser_parse_definitely (parser);
22455           nbraces += bracecount;
22456         }
22457     }
22458
22459   /* Note that we saved the original contents of this flag when we entered
22460      the structured block, and so we don't need to re-save it here.  */
22461   parser->in_statement = IN_OMP_FOR;
22462
22463   /* Note that the grammar doesn't call for a structured block here,
22464      though the loop as a whole is a structured block.  */
22465   body = push_stmt_list ();
22466   cp_parser_statement (parser, NULL_TREE, false, NULL);
22467   body = pop_stmt_list (body);
22468
22469   if (declv == NULL_TREE)
22470     ret = NULL_TREE;
22471   else
22472     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22473                           pre_body, clauses);
22474
22475   while (nbraces)
22476     {
22477       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22478         {
22479           cp_lexer_consume_token (parser->lexer);
22480           nbraces--;
22481         }
22482       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22483         cp_lexer_consume_token (parser->lexer);
22484       else
22485         {
22486           if (!collapse_err)
22487             {
22488               error_at (cp_lexer_peek_token (parser->lexer)->location,
22489                         "collapsed loops not perfectly nested");
22490             }
22491           collapse_err = true;
22492           cp_parser_statement_seq_opt (parser, NULL);
22493           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22494             break;
22495         }
22496     }
22497
22498   while (for_block)
22499     {
22500       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22501       for_block = TREE_CHAIN (for_block);
22502     }
22503
22504   return ret;
22505 }
22506
22507 /* OpenMP 2.5:
22508    #pragma omp for for-clause[optseq] new-line
22509      for-loop  */
22510
22511 #define OMP_FOR_CLAUSE_MASK                             \
22512         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22513         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22514         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22515         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22516         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22517         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22518         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22519         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22520
22521 static tree
22522 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22523 {
22524   tree clauses, sb, ret;
22525   unsigned int save;
22526
22527   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22528                                        "#pragma omp for", pragma_tok);
22529
22530   sb = begin_omp_structured_block ();
22531   save = cp_parser_begin_omp_structured_block (parser);
22532
22533   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22534
22535   cp_parser_end_omp_structured_block (parser, save);
22536   add_stmt (finish_omp_structured_block (sb));
22537
22538   return ret;
22539 }
22540
22541 /* OpenMP 2.5:
22542    # pragma omp master new-line
22543      structured-block  */
22544
22545 static tree
22546 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22547 {
22548   cp_parser_require_pragma_eol (parser, pragma_tok);
22549   return c_finish_omp_master (input_location,
22550                               cp_parser_omp_structured_block (parser));
22551 }
22552
22553 /* OpenMP 2.5:
22554    # pragma omp ordered new-line
22555      structured-block  */
22556
22557 static tree
22558 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22559 {
22560   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22561   cp_parser_require_pragma_eol (parser, pragma_tok);
22562   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22563 }
22564
22565 /* OpenMP 2.5:
22566
22567    section-scope:
22568      { section-sequence }
22569
22570    section-sequence:
22571      section-directive[opt] structured-block
22572      section-sequence section-directive structured-block  */
22573
22574 static tree
22575 cp_parser_omp_sections_scope (cp_parser *parser)
22576 {
22577   tree stmt, substmt;
22578   bool error_suppress = false;
22579   cp_token *tok;
22580
22581   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22582     return NULL_TREE;
22583
22584   stmt = push_stmt_list ();
22585
22586   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22587     {
22588       unsigned save;
22589
22590       substmt = begin_omp_structured_block ();
22591       save = cp_parser_begin_omp_structured_block (parser);
22592
22593       while (1)
22594         {
22595           cp_parser_statement (parser, NULL_TREE, false, NULL);
22596
22597           tok = cp_lexer_peek_token (parser->lexer);
22598           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22599             break;
22600           if (tok->type == CPP_CLOSE_BRACE)
22601             break;
22602           if (tok->type == CPP_EOF)
22603             break;
22604         }
22605
22606       cp_parser_end_omp_structured_block (parser, save);
22607       substmt = finish_omp_structured_block (substmt);
22608       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22609       add_stmt (substmt);
22610     }
22611
22612   while (1)
22613     {
22614       tok = cp_lexer_peek_token (parser->lexer);
22615       if (tok->type == CPP_CLOSE_BRACE)
22616         break;
22617       if (tok->type == CPP_EOF)
22618         break;
22619
22620       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22621         {
22622           cp_lexer_consume_token (parser->lexer);
22623           cp_parser_require_pragma_eol (parser, tok);
22624           error_suppress = false;
22625         }
22626       else if (!error_suppress)
22627         {
22628           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22629           error_suppress = true;
22630         }
22631
22632       substmt = cp_parser_omp_structured_block (parser);
22633       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22634       add_stmt (substmt);
22635     }
22636   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22637
22638   substmt = pop_stmt_list (stmt);
22639
22640   stmt = make_node (OMP_SECTIONS);
22641   TREE_TYPE (stmt) = void_type_node;
22642   OMP_SECTIONS_BODY (stmt) = substmt;
22643
22644   add_stmt (stmt);
22645   return stmt;
22646 }
22647
22648 /* OpenMP 2.5:
22649    # pragma omp sections sections-clause[optseq] newline
22650      sections-scope  */
22651
22652 #define OMP_SECTIONS_CLAUSE_MASK                        \
22653         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22654         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22655         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22656         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22657         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22658
22659 static tree
22660 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22661 {
22662   tree clauses, ret;
22663
22664   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22665                                        "#pragma omp sections", pragma_tok);
22666
22667   ret = cp_parser_omp_sections_scope (parser);
22668   if (ret)
22669     OMP_SECTIONS_CLAUSES (ret) = clauses;
22670
22671   return ret;
22672 }
22673
22674 /* OpenMP 2.5:
22675    # pragma parallel parallel-clause new-line
22676    # pragma parallel for parallel-for-clause new-line
22677    # pragma parallel sections parallel-sections-clause new-line  */
22678
22679 #define OMP_PARALLEL_CLAUSE_MASK                        \
22680         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22681         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22682         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22683         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22684         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22685         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22686         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22687         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22688
22689 static tree
22690 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22691 {
22692   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22693   const char *p_name = "#pragma omp parallel";
22694   tree stmt, clauses, par_clause, ws_clause, block;
22695   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22696   unsigned int save;
22697   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22698
22699   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22700     {
22701       cp_lexer_consume_token (parser->lexer);
22702       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22703       p_name = "#pragma omp parallel for";
22704       mask |= OMP_FOR_CLAUSE_MASK;
22705       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22706     }
22707   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22708     {
22709       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22710       const char *p = IDENTIFIER_POINTER (id);
22711       if (strcmp (p, "sections") == 0)
22712         {
22713           cp_lexer_consume_token (parser->lexer);
22714           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22715           p_name = "#pragma omp parallel sections";
22716           mask |= OMP_SECTIONS_CLAUSE_MASK;
22717           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22718         }
22719     }
22720
22721   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22722   block = begin_omp_parallel ();
22723   save = cp_parser_begin_omp_structured_block (parser);
22724
22725   switch (p_kind)
22726     {
22727     case PRAGMA_OMP_PARALLEL:
22728       cp_parser_statement (parser, NULL_TREE, false, NULL);
22729       par_clause = clauses;
22730       break;
22731
22732     case PRAGMA_OMP_PARALLEL_FOR:
22733       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22734       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22735       break;
22736
22737     case PRAGMA_OMP_PARALLEL_SECTIONS:
22738       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22739       stmt = cp_parser_omp_sections_scope (parser);
22740       if (stmt)
22741         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22742       break;
22743
22744     default:
22745       gcc_unreachable ();
22746     }
22747
22748   cp_parser_end_omp_structured_block (parser, save);
22749   stmt = finish_omp_parallel (par_clause, block);
22750   if (p_kind != PRAGMA_OMP_PARALLEL)
22751     OMP_PARALLEL_COMBINED (stmt) = 1;
22752   return stmt;
22753 }
22754
22755 /* OpenMP 2.5:
22756    # pragma omp single single-clause[optseq] new-line
22757      structured-block  */
22758
22759 #define OMP_SINGLE_CLAUSE_MASK                          \
22760         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22761         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22762         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22763         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22764
22765 static tree
22766 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22767 {
22768   tree stmt = make_node (OMP_SINGLE);
22769   TREE_TYPE (stmt) = void_type_node;
22770
22771   OMP_SINGLE_CLAUSES (stmt)
22772     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22773                                  "#pragma omp single", pragma_tok);
22774   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22775
22776   return add_stmt (stmt);
22777 }
22778
22779 /* OpenMP 3.0:
22780    # pragma omp task task-clause[optseq] new-line
22781      structured-block  */
22782
22783 #define OMP_TASK_CLAUSE_MASK                            \
22784         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22785         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22786         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22787         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22788         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22789         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22790
22791 static tree
22792 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22793 {
22794   tree clauses, block;
22795   unsigned int save;
22796
22797   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22798                                        "#pragma omp task", pragma_tok);
22799   block = begin_omp_task ();
22800   save = cp_parser_begin_omp_structured_block (parser);
22801   cp_parser_statement (parser, NULL_TREE, false, NULL);
22802   cp_parser_end_omp_structured_block (parser, save);
22803   return finish_omp_task (clauses, block);
22804 }
22805
22806 /* OpenMP 3.0:
22807    # pragma omp taskwait new-line  */
22808
22809 static void
22810 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22811 {
22812   cp_parser_require_pragma_eol (parser, pragma_tok);
22813   finish_omp_taskwait ();
22814 }
22815
22816 /* OpenMP 2.5:
22817    # pragma omp threadprivate (variable-list) */
22818
22819 static void
22820 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22821 {
22822   tree vars;
22823
22824   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22825   cp_parser_require_pragma_eol (parser, pragma_tok);
22826
22827   finish_omp_threadprivate (vars);
22828 }
22829
22830 /* Main entry point to OpenMP statement pragmas.  */
22831
22832 static void
22833 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22834 {
22835   tree stmt;
22836
22837   switch (pragma_tok->pragma_kind)
22838     {
22839     case PRAGMA_OMP_ATOMIC:
22840       cp_parser_omp_atomic (parser, pragma_tok);
22841       return;
22842     case PRAGMA_OMP_CRITICAL:
22843       stmt = cp_parser_omp_critical (parser, pragma_tok);
22844       break;
22845     case PRAGMA_OMP_FOR:
22846       stmt = cp_parser_omp_for (parser, pragma_tok);
22847       break;
22848     case PRAGMA_OMP_MASTER:
22849       stmt = cp_parser_omp_master (parser, pragma_tok);
22850       break;
22851     case PRAGMA_OMP_ORDERED:
22852       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22853       break;
22854     case PRAGMA_OMP_PARALLEL:
22855       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22856       break;
22857     case PRAGMA_OMP_SECTIONS:
22858       stmt = cp_parser_omp_sections (parser, pragma_tok);
22859       break;
22860     case PRAGMA_OMP_SINGLE:
22861       stmt = cp_parser_omp_single (parser, pragma_tok);
22862       break;
22863     case PRAGMA_OMP_TASK:
22864       stmt = cp_parser_omp_task (parser, pragma_tok);
22865       break;
22866     default:
22867       gcc_unreachable ();
22868     }
22869
22870   if (stmt)
22871     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22872 }
22873 \f
22874 /* The parser.  */
22875
22876 static GTY (()) cp_parser *the_parser;
22877
22878 \f
22879 /* Special handling for the first token or line in the file.  The first
22880    thing in the file might be #pragma GCC pch_preprocess, which loads a
22881    PCH file, which is a GC collection point.  So we need to handle this
22882    first pragma without benefit of an existing lexer structure.
22883
22884    Always returns one token to the caller in *FIRST_TOKEN.  This is
22885    either the true first token of the file, or the first token after
22886    the initial pragma.  */
22887
22888 static void
22889 cp_parser_initial_pragma (cp_token *first_token)
22890 {
22891   tree name = NULL;
22892
22893   cp_lexer_get_preprocessor_token (NULL, first_token);
22894   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22895     return;
22896
22897   cp_lexer_get_preprocessor_token (NULL, first_token);
22898   if (first_token->type == CPP_STRING)
22899     {
22900       name = first_token->u.value;
22901
22902       cp_lexer_get_preprocessor_token (NULL, first_token);
22903       if (first_token->type != CPP_PRAGMA_EOL)
22904         error_at (first_token->location,
22905                   "junk at end of %<#pragma GCC pch_preprocess%>");
22906     }
22907   else
22908     error_at (first_token->location, "expected string literal");
22909
22910   /* Skip to the end of the pragma.  */
22911   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22912     cp_lexer_get_preprocessor_token (NULL, first_token);
22913
22914   /* Now actually load the PCH file.  */
22915   if (name)
22916     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22917
22918   /* Read one more token to return to our caller.  We have to do this
22919      after reading the PCH file in, since its pointers have to be
22920      live.  */
22921   cp_lexer_get_preprocessor_token (NULL, first_token);
22922 }
22923
22924 /* Normal parsing of a pragma token.  Here we can (and must) use the
22925    regular lexer.  */
22926
22927 static bool
22928 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22929 {
22930   cp_token *pragma_tok;
22931   unsigned int id;
22932
22933   pragma_tok = cp_lexer_consume_token (parser->lexer);
22934   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22935   parser->lexer->in_pragma = true;
22936
22937   id = pragma_tok->pragma_kind;
22938   switch (id)
22939     {
22940     case PRAGMA_GCC_PCH_PREPROCESS:
22941       error_at (pragma_tok->location,
22942                 "%<#pragma GCC pch_preprocess%> must be first");
22943       break;
22944
22945     case PRAGMA_OMP_BARRIER:
22946       switch (context)
22947         {
22948         case pragma_compound:
22949           cp_parser_omp_barrier (parser, pragma_tok);
22950           return false;
22951         case pragma_stmt:
22952           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22953                     "used in compound statements");
22954           break;
22955         default:
22956           goto bad_stmt;
22957         }
22958       break;
22959
22960     case PRAGMA_OMP_FLUSH:
22961       switch (context)
22962         {
22963         case pragma_compound:
22964           cp_parser_omp_flush (parser, pragma_tok);
22965           return false;
22966         case pragma_stmt:
22967           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22968                     "used in compound statements");
22969           break;
22970         default:
22971           goto bad_stmt;
22972         }
22973       break;
22974
22975     case PRAGMA_OMP_TASKWAIT:
22976       switch (context)
22977         {
22978         case pragma_compound:
22979           cp_parser_omp_taskwait (parser, pragma_tok);
22980           return false;
22981         case pragma_stmt:
22982           error_at (pragma_tok->location,
22983                     "%<#pragma omp taskwait%> may only be "
22984                     "used in compound statements");
22985           break;
22986         default:
22987           goto bad_stmt;
22988         }
22989       break;
22990
22991     case PRAGMA_OMP_THREADPRIVATE:
22992       cp_parser_omp_threadprivate (parser, pragma_tok);
22993       return false;
22994
22995     case PRAGMA_OMP_ATOMIC:
22996     case PRAGMA_OMP_CRITICAL:
22997     case PRAGMA_OMP_FOR:
22998     case PRAGMA_OMP_MASTER:
22999     case PRAGMA_OMP_ORDERED:
23000     case PRAGMA_OMP_PARALLEL:
23001     case PRAGMA_OMP_SECTIONS:
23002     case PRAGMA_OMP_SINGLE:
23003     case PRAGMA_OMP_TASK:
23004       if (context == pragma_external)
23005         goto bad_stmt;
23006       cp_parser_omp_construct (parser, pragma_tok);
23007       return true;
23008
23009     case PRAGMA_OMP_SECTION:
23010       error_at (pragma_tok->location, 
23011                 "%<#pragma omp section%> may only be used in "
23012                 "%<#pragma omp sections%> construct");
23013       break;
23014
23015     default:
23016       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23017       c_invoke_pragma_handler (id);
23018       break;
23019
23020     bad_stmt:
23021       cp_parser_error (parser, "expected declaration specifiers");
23022       break;
23023     }
23024
23025   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23026   return false;
23027 }
23028
23029 /* The interface the pragma parsers have to the lexer.  */
23030
23031 enum cpp_ttype
23032 pragma_lex (tree *value)
23033 {
23034   cp_token *tok;
23035   enum cpp_ttype ret;
23036
23037   tok = cp_lexer_peek_token (the_parser->lexer);
23038
23039   ret = tok->type;
23040   *value = tok->u.value;
23041
23042   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23043     ret = CPP_EOF;
23044   else if (ret == CPP_STRING)
23045     *value = cp_parser_string_literal (the_parser, false, false);
23046   else
23047     {
23048       cp_lexer_consume_token (the_parser->lexer);
23049       if (ret == CPP_KEYWORD)
23050         ret = CPP_NAME;
23051     }
23052
23053   return ret;
23054 }
23055
23056 \f
23057 /* External interface.  */
23058
23059 /* Parse one entire translation unit.  */
23060
23061 void
23062 c_parse_file (void)
23063 {
23064   bool error_occurred;
23065   static bool already_called = false;
23066
23067   if (already_called)
23068     {
23069       sorry ("inter-module optimizations not implemented for C++");
23070       return;
23071     }
23072   already_called = true;
23073
23074   the_parser = cp_parser_new ();
23075   push_deferring_access_checks (flag_access_control
23076                                 ? dk_no_deferred : dk_no_check);
23077   error_occurred = cp_parser_translation_unit (the_parser);
23078   the_parser = NULL;
23079 }
23080
23081 #include "gt-cp-parser.h"