OSDN Git Service

* parser.c (cp_parser_lambda_expression): Compute visibility.
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251    sizeof, typeof, or alignof.  */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   done_lexing = true;
314
315   gcc_assert (lexer->next_token->type != CPP_PURGED);
316   return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325   cp_token *first = cache->first;
326   cp_token *last = cache->last;
327   cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329   /* We do not own the buffer.  */
330   lexer->buffer = NULL;
331   lexer->buffer_length = 0;
332   lexer->next_token = first == last ? &eof_token : first;
333   lexer->last_token = last;
334
335   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336                                    CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339   /* Initially we are not debugging.  */
340   lexer->debugging_p = false;
341 #endif
342
343   gcc_assert (lexer->next_token->type != CPP_PURGED);
344   return lexer;
345 }
346
347 /* Frees all resources associated with LEXER.  */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352   if (lexer->buffer)
353     ggc_free (lexer->buffer);
354   VEC_free (cp_token_position, heap, lexer->saved_tokens);
355   ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output.  */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365   return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373   gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375   return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381   return pos;
382 }
383
384 /* nonzero if we are presently saving tokens.  */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN.  Return true
393    if we reach EOF.  If LEXER is NULL, assume we are handling an
394    initial #pragma pch_preprocess, and thus want the lexer to return
395    processed strings.  */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400   static int is_extern_c = 0;
401
402    /* Get a new token from the preprocessor.  */
403   token->type
404     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
406   token->keyword = RID_MAX;
407   token->pragma_kind = PRAGMA_NONE;
408
409   /* On some systems, some header files are surrounded by an
410      implicit extern "C" block.  Set a flag in the token if it
411      comes from such a header.  */
412   is_extern_c += pending_lang_change;
413   pending_lang_change = 0;
414   token->implicit_extern_c = is_extern_c > 0;
415
416   /* Check to see if this token is a keyword.  */
417   if (token->type == CPP_NAME)
418     {
419       if (C_IS_RESERVED_WORD (token->u.value))
420         {
421           /* Mark this token as a keyword.  */
422           token->type = CPP_KEYWORD;
423           /* Record which keyword.  */
424           token->keyword = C_RID_CODE (token->u.value);
425         }
426       else
427         {
428           if (warn_cxx0x_compat
429               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431             {
432               /* Warn about the C++0x keyword (but still treat it as
433                  an identifier).  */
434               warning (OPT_Wc__0x_compat, 
435                        "identifier %qE will become a keyword in C++0x",
436                        token->u.value);
437
438               /* Clear out the C_RID_CODE so we don't warn about this
439                  particular identifier-turned-keyword again.  */
440               C_SET_RID_CODE (token->u.value, RID_MAX);
441             }
442
443           token->ambiguous_p = false;
444           token->keyword = RID_MAX;
445         }
446     }
447   /* Handle Objective-C++ keywords.  */
448   else if (token->type == CPP_AT_NAME)
449     {
450       token->type = CPP_KEYWORD;
451       switch (C_RID_CODE (token->u.value))
452         {
453         /* Map 'class' to '@class', 'private' to '@private', etc.  */
454         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458         case RID_THROW: token->keyword = RID_AT_THROW; break;
459         case RID_TRY: token->keyword = RID_AT_TRY; break;
460         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461         default: token->keyword = C_RID_CODE (token->u.value);
462         }
463     }
464   else if (token->type == CPP_PRAGMA)
465     {
466       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
467       token->pragma_kind = ((enum pragma_kind)
468                             TREE_INT_CST_LOW (token->u.value));
469       token->u.value = NULL_TREE;
470     }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN.  */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477   if (token->type != CPP_EOF)
478     {
479       input_location = token->location;
480     }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484    consume it.  */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489   if (cp_lexer_debugging_p (lexer))
490     {
491       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493       putc ('\n', cp_lexer_debug_stream);
494     }
495   return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE.  */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503   return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE.  */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511   return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD.  */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519   return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD.  */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527   return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier.  */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535   cp_token *token;
536
537   token = cp_lexer_peek_token (lexer);
538   switch (token->keyword) 
539     {
540       /* auto specifier: storage-class-specifier in C++,
541          simple-type-specifier in C++0x.  */
542     case RID_AUTO:
543       /* Storage classes.  */
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_CHAR16:
558     case RID_CHAR32:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_STRING16:
793     case CPP_STRING32:
794     case CPP_WSTRING:
795       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
796       break;
797
798     default:
799       break;
800     }
801 }
802
803 /* Start emitting debugging information.  */
804
805 static void
806 cp_lexer_start_debugging (cp_lexer* lexer)
807 {
808   lexer->debugging_p = true;
809 }
810
811 /* Stop emitting debugging information.  */
812
813 static void
814 cp_lexer_stop_debugging (cp_lexer* lexer)
815 {
816   lexer->debugging_p = false;
817 }
818
819 #endif /* ENABLE_CHECKING */
820
821 /* Create a new cp_token_cache, representing a range of tokens.  */
822
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
825 {
826   cp_token_cache *cache = GGC_NEW (cp_token_cache);
827   cache->first = first;
828   cache->last = last;
829   return cache;
830 }
831
832 \f
833 /* Decl-specifiers.  */
834
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
836
837 static void
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
839 {
840   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 }
842
843 /* Declarators.  */
844
845 /* Nothing other than the parser should be creating declarators;
846    declarators are a semi-syntactic representation of C++ entities.
847    Other parts of the front end that need to create entities (like
848    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
849
850 static cp_declarator *make_call_declarator
851   (cp_declarator *, tree, cp_cv_quals, tree, tree);
852 static cp_declarator *make_array_declarator
853   (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855   (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857   (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859   (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861   (cp_cv_quals, tree, cp_declarator *);
862
863 /* An erroneous declarator.  */
864 static cp_declarator *cp_error_declarator;
865
866 /* The obstack on which declarators and related data structures are
867    allocated.  */
868 static struct obstack declarator_obstack;
869
870 /* Alloc BYTES from the declarator memory pool.  */
871
872 static inline void *
873 alloc_declarator (size_t bytes)
874 {
875   return obstack_alloc (&declarator_obstack, bytes);
876 }
877
878 /* Allocate a declarator of the indicated KIND.  Clear fields that are
879    common to all declarators.  */
880
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
883 {
884   cp_declarator *declarator;
885
886   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887   declarator->kind = kind;
888   declarator->attributes = NULL_TREE;
889   declarator->declarator = NULL;
890   declarator->parameter_pack_p = false;
891
892   return declarator;
893 }
894
895 /* Make a declarator for a generalized identifier.  If
896    QUALIFYING_SCOPE is non-NULL, the identifier is
897    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
899    is, if any.   */
900
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903                     special_function_kind sfk)
904 {
905   cp_declarator *declarator;
906
907   /* It is valid to write:
908
909        class C { void f(); };
910        typedef C D;
911        void D::f();
912
913      The standard is not clear about whether `typedef const C D' is
914      legal; as of 2002-09-15 the committee is considering that
915      question.  EDG 3.0 allows that syntax.  Therefore, we do as
916      well.  */
917   if (qualifying_scope && TYPE_P (qualifying_scope))
918     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
919
920   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
923
924   declarator = make_declarator (cdk_id);
925   declarator->u.id.qualifying_scope = qualifying_scope;
926   declarator->u.id.unqualified_name = unqualified_name;
927   declarator->u.id.sfk = sfk;
928   
929   return declarator;
930 }
931
932 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
933    of modifiers such as const or volatile to apply to the pointer
934    type, represented as identifiers.  */
935
936 cp_declarator *
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
938 {
939   cp_declarator *declarator;
940
941   declarator = make_declarator (cdk_pointer);
942   declarator->declarator = target;
943   declarator->u.pointer.qualifiers = cv_qualifiers;
944   declarator->u.pointer.class_type = NULL_TREE;
945   if (target)
946     {
947       declarator->parameter_pack_p = target->parameter_pack_p;
948       target->parameter_pack_p = false;
949     }
950   else
951     declarator->parameter_pack_p = false;
952
953   return declarator;
954 }
955
956 /* Like make_pointer_declarator -- but for references.  */
957
958 cp_declarator *
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
960                            bool rvalue_ref)
961 {
962   cp_declarator *declarator;
963
964   declarator = make_declarator (cdk_reference);
965   declarator->declarator = target;
966   declarator->u.reference.qualifiers = cv_qualifiers;
967   declarator->u.reference.rvalue_ref = rvalue_ref;
968   if (target)
969     {
970       declarator->parameter_pack_p = target->parameter_pack_p;
971       target->parameter_pack_p = false;
972     }
973   else
974     declarator->parameter_pack_p = false;
975
976   return declarator;
977 }
978
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980    member of CLASS_TYPE.  */
981
982 cp_declarator *
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984                         cp_declarator *pointee)
985 {
986   cp_declarator *declarator;
987
988   declarator = make_declarator (cdk_ptrmem);
989   declarator->declarator = pointee;
990   declarator->u.pointer.qualifiers = cv_qualifiers;
991   declarator->u.pointer.class_type = class_type;
992
993   if (pointee)
994     {
995       declarator->parameter_pack_p = pointee->parameter_pack_p;
996       pointee->parameter_pack_p = false;
997     }
998   else
999     declarator->parameter_pack_p = false;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for the function given by TARGET, with the
1005    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1006    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1007    indicates what exceptions can be thrown.  */
1008
1009 cp_declarator *
1010 make_call_declarator (cp_declarator *target,
1011                       tree parms,
1012                       cp_cv_quals cv_qualifiers,
1013                       tree exception_specification,
1014                       tree late_return_type)
1015 {
1016   cp_declarator *declarator;
1017
1018   declarator = make_declarator (cdk_function);
1019   declarator->declarator = target;
1020   declarator->u.function.parameters = parms;
1021   declarator->u.function.qualifiers = cv_qualifiers;
1022   declarator->u.function.exception_specification = exception_specification;
1023   declarator->u.function.late_return_type = late_return_type;
1024   if (target)
1025     {
1026       declarator->parameter_pack_p = target->parameter_pack_p;
1027       target->parameter_pack_p = false;
1028     }
1029   else
1030     declarator->parameter_pack_p = false;
1031
1032   return declarator;
1033 }
1034
1035 /* Make a declarator for an array of BOUNDS elements, each of which is
1036    defined by ELEMENT.  */
1037
1038 cp_declarator *
1039 make_array_declarator (cp_declarator *element, tree bounds)
1040 {
1041   cp_declarator *declarator;
1042
1043   declarator = make_declarator (cdk_array);
1044   declarator->declarator = element;
1045   declarator->u.array.bounds = bounds;
1046   if (element)
1047     {
1048       declarator->parameter_pack_p = element->parameter_pack_p;
1049       element->parameter_pack_p = false;
1050     }
1051   else
1052     declarator->parameter_pack_p = false;
1053
1054   return declarator;
1055 }
1056
1057 /* Determine whether the declarator we've seen so far can be a
1058    parameter pack, when followed by an ellipsis.  */
1059 static bool 
1060 declarator_can_be_parameter_pack (cp_declarator *declarator)
1061 {
1062   /* Search for a declarator name, or any other declarator that goes
1063      after the point where the ellipsis could appear in a parameter
1064      pack. If we find any of these, then this declarator can not be
1065      made into a parameter pack.  */
1066   bool found = false;
1067   while (declarator && !found)
1068     {
1069       switch ((int)declarator->kind)
1070         {
1071         case cdk_id:
1072         case cdk_array:
1073           found = true;
1074           break;
1075
1076         case cdk_error:
1077           return true;
1078
1079         default:
1080           declarator = declarator->declarator;
1081           break;
1082         }
1083     }
1084
1085   return !found;
1086 }
1087
1088 cp_parameter_declarator *no_parameters;
1089
1090 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1091    DECLARATOR and DEFAULT_ARGUMENT.  */
1092
1093 cp_parameter_declarator *
1094 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1095                            cp_declarator *declarator,
1096                            tree default_argument)
1097 {
1098   cp_parameter_declarator *parameter;
1099
1100   parameter = ((cp_parameter_declarator *)
1101                alloc_declarator (sizeof (cp_parameter_declarator)));
1102   parameter->next = NULL;
1103   if (decl_specifiers)
1104     parameter->decl_specifiers = *decl_specifiers;
1105   else
1106     clear_decl_specs (&parameter->decl_specifiers);
1107   parameter->declarator = declarator;
1108   parameter->default_argument = default_argument;
1109   parameter->ellipsis_p = false;
1110
1111   return parameter;
1112 }
1113
1114 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1115
1116 static bool
1117 function_declarator_p (const cp_declarator *declarator)
1118 {
1119   while (declarator)
1120     {
1121       if (declarator->kind == cdk_function
1122           && declarator->declarator->kind == cdk_id)
1123         return true;
1124       if (declarator->kind == cdk_id
1125           || declarator->kind == cdk_error)
1126         return false;
1127       declarator = declarator->declarator;
1128     }
1129   return false;
1130 }
1131  
1132 /* The parser.  */
1133
1134 /* Overview
1135    --------
1136
1137    A cp_parser parses the token stream as specified by the C++
1138    grammar.  Its job is purely parsing, not semantic analysis.  For
1139    example, the parser breaks the token stream into declarators,
1140    expressions, statements, and other similar syntactic constructs.
1141    It does not check that the types of the expressions on either side
1142    of an assignment-statement are compatible, or that a function is
1143    not declared with a parameter of type `void'.
1144
1145    The parser invokes routines elsewhere in the compiler to perform
1146    semantic analysis and to build up the abstract syntax tree for the
1147    code processed.
1148
1149    The parser (and the template instantiation code, which is, in a
1150    way, a close relative of parsing) are the only parts of the
1151    compiler that should be calling push_scope and pop_scope, or
1152    related functions.  The parser (and template instantiation code)
1153    keeps track of what scope is presently active; everything else
1154    should simply honor that.  (The code that generates static
1155    initializers may also need to set the scope, in order to check
1156    access control correctly when emitting the initializers.)
1157
1158    Methodology
1159    -----------
1160
1161    The parser is of the standard recursive-descent variety.  Upcoming
1162    tokens in the token stream are examined in order to determine which
1163    production to use when parsing a non-terminal.  Some C++ constructs
1164    require arbitrary look ahead to disambiguate.  For example, it is
1165    impossible, in the general case, to tell whether a statement is an
1166    expression or declaration without scanning the entire statement.
1167    Therefore, the parser is capable of "parsing tentatively."  When the
1168    parser is not sure what construct comes next, it enters this mode.
1169    Then, while we attempt to parse the construct, the parser queues up
1170    error messages, rather than issuing them immediately, and saves the
1171    tokens it consumes.  If the construct is parsed successfully, the
1172    parser "commits", i.e., it issues any queued error messages and
1173    the tokens that were being preserved are permanently discarded.
1174    If, however, the construct is not parsed successfully, the parser
1175    rolls back its state completely so that it can resume parsing using
1176    a different alternative.
1177
1178    Future Improvements
1179    -------------------
1180
1181    The performance of the parser could probably be improved substantially.
1182    We could often eliminate the need to parse tentatively by looking ahead
1183    a little bit.  In some places, this approach might not entirely eliminate
1184    the need to parse tentatively, but it might still speed up the average
1185    case.  */
1186
1187 /* Flags that are passed to some parsing functions.  These values can
1188    be bitwise-ored together.  */
1189
1190 enum
1191 {
1192   /* No flags.  */
1193   CP_PARSER_FLAGS_NONE = 0x0,
1194   /* The construct is optional.  If it is not present, then no error
1195      should be issued.  */
1196   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1197   /* When parsing a type-specifier, do not allow user-defined types.  */
1198   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 };
1200
1201 /* This type is used for parameters and variables which hold
1202    combinations of the above flags.  */
1203 typedef int cp_parser_flags;
1204
1205 /* The different kinds of declarators we want to parse.  */
1206
1207 typedef enum cp_parser_declarator_kind
1208 {
1209   /* We want an abstract declarator.  */
1210   CP_PARSER_DECLARATOR_ABSTRACT,
1211   /* We want a named declarator.  */
1212   CP_PARSER_DECLARATOR_NAMED,
1213   /* We don't mind, but the name must be an unqualified-id.  */
1214   CP_PARSER_DECLARATOR_EITHER
1215 } cp_parser_declarator_kind;
1216
1217 /* The precedence values used to parse binary expressions.  The minimum value
1218    of PREC must be 1, because zero is reserved to quickly discriminate
1219    binary operators from other tokens.  */
1220
1221 enum cp_parser_prec
1222 {
1223   PREC_NOT_OPERATOR,
1224   PREC_LOGICAL_OR_EXPRESSION,
1225   PREC_LOGICAL_AND_EXPRESSION,
1226   PREC_INCLUSIVE_OR_EXPRESSION,
1227   PREC_EXCLUSIVE_OR_EXPRESSION,
1228   PREC_AND_EXPRESSION,
1229   PREC_EQUALITY_EXPRESSION,
1230   PREC_RELATIONAL_EXPRESSION,
1231   PREC_SHIFT_EXPRESSION,
1232   PREC_ADDITIVE_EXPRESSION,
1233   PREC_MULTIPLICATIVE_EXPRESSION,
1234   PREC_PM_EXPRESSION,
1235   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1236 };
1237
1238 /* A mapping from a token type to a corresponding tree node type, with a
1239    precedence value.  */
1240
1241 typedef struct cp_parser_binary_operations_map_node
1242 {
1243   /* The token type.  */
1244   enum cpp_ttype token_type;
1245   /* The corresponding tree code.  */
1246   enum tree_code tree_type;
1247   /* The precedence of this operator.  */
1248   enum cp_parser_prec prec;
1249 } cp_parser_binary_operations_map_node;
1250
1251 /* The status of a tentative parse.  */
1252
1253 typedef enum cp_parser_status_kind
1254 {
1255   /* No errors have occurred.  */
1256   CP_PARSER_STATUS_KIND_NO_ERROR,
1257   /* An error has occurred.  */
1258   CP_PARSER_STATUS_KIND_ERROR,
1259   /* We are committed to this tentative parse, whether or not an error
1260      has occurred.  */
1261   CP_PARSER_STATUS_KIND_COMMITTED
1262 } cp_parser_status_kind;
1263
1264 typedef struct cp_parser_expression_stack_entry
1265 {
1266   /* Left hand side of the binary operation we are currently
1267      parsing.  */
1268   tree lhs;
1269   /* Original tree code for left hand side, if it was a binary
1270      expression itself (used for -Wparentheses).  */
1271   enum tree_code lhs_type;
1272   /* Tree code for the binary operation we are parsing.  */
1273   enum tree_code tree_type;
1274   /* Precedence of the binary operation we are parsing.  */
1275   enum cp_parser_prec prec;
1276 } cp_parser_expression_stack_entry;
1277
1278 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1279    entries because precedence levels on the stack are monotonically
1280    increasing.  */
1281 typedef struct cp_parser_expression_stack_entry
1282   cp_parser_expression_stack[NUM_PREC_VALUES];
1283
1284 /* Context that is saved and restored when parsing tentatively.  */
1285 typedef struct GTY (()) cp_parser_context {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct GTY(()) cp_parser {
1392   /* The lexer from which we are obtaining tokens.  */
1393   cp_lexer *lexer;
1394
1395   /* The scope in which names should be looked up.  If NULL_TREE, then
1396      we look up names in the scope that is currently open in the
1397      source program.  If non-NULL, this is either a TYPE or
1398      NAMESPACE_DECL for the scope in which we should look.  It can
1399      also be ERROR_MARK, when we've parsed a bogus scope.
1400
1401      This value is not cleared automatically after a name is looked
1402      up, so we must be careful to clear it before starting a new look
1403      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1404      will look up `Z' in the scope of `X', rather than the current
1405      scope.)  Unfortunately, it is difficult to tell when name lookup
1406      is complete, because we sometimes peek at a token, look it up,
1407      and then decide not to consume it.   */
1408   tree scope;
1409
1410   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1411      last lookup took place.  OBJECT_SCOPE is used if an expression
1412      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1413      respectively.  QUALIFYING_SCOPE is used for an expression of the
1414      form "X::Y"; it refers to X.  */
1415   tree object_scope;
1416   tree qualifying_scope;
1417
1418   /* A stack of parsing contexts.  All but the bottom entry on the
1419      stack will be tentative contexts.
1420
1421      We parse tentatively in order to determine which construct is in
1422      use in some situations.  For example, in order to determine
1423      whether a statement is an expression-statement or a
1424      declaration-statement we parse it tentatively as a
1425      declaration-statement.  If that fails, we then reparse the same
1426      token stream as an expression-statement.  */
1427   cp_parser_context *context;
1428
1429   /* True if we are parsing GNU C++.  If this flag is not set, then
1430      GNU extensions are not recognized.  */
1431   bool allow_gnu_extensions_p;
1432
1433   /* TRUE if the `>' token should be interpreted as the greater-than
1434      operator.  FALSE if it is the end of a template-id or
1435      template-parameter-list. In C++0x mode, this flag also applies to
1436      `>>' tokens, which are viewed as two consecutive `>' tokens when
1437      this flag is FALSE.  */
1438   bool greater_than_is_operator_p;
1439
1440   /* TRUE if default arguments are allowed within a parameter list
1441      that starts at this point. FALSE if only a gnu extension makes
1442      them permissible.  */
1443   bool default_arg_ok_p;
1444
1445   /* TRUE if we are parsing an integral constant-expression.  See
1446      [expr.const] for a precise definition.  */
1447   bool integral_constant_expression_p;
1448
1449   /* TRUE if we are parsing an integral constant-expression -- but a
1450      non-constant expression should be permitted as well.  This flag
1451      is used when parsing an array bound so that GNU variable-length
1452      arrays are tolerated.  */
1453   bool allow_non_integral_constant_expression_p;
1454
1455   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1456      been seen that makes the expression non-constant.  */
1457   bool non_integral_constant_expression_p;
1458
1459   /* TRUE if local variable names and `this' are forbidden in the
1460      current context.  */
1461   bool local_variables_forbidden_p;
1462
1463   /* TRUE if the declaration we are parsing is part of a
1464      linkage-specification of the form `extern string-literal
1465      declaration'.  */
1466   bool in_unbraced_linkage_specification_p;
1467
1468   /* TRUE if we are presently parsing a declarator, after the
1469      direct-declarator.  */
1470   bool in_declarator_p;
1471
1472   /* TRUE if we are presently parsing a template-argument-list.  */
1473   bool in_template_argument_list_p;
1474
1475   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1476      to IN_OMP_BLOCK if parsing OpenMP structured block and
1477      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1478      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1479      iteration-statement, OpenMP block or loop within that switch.  */
1480 #define IN_SWITCH_STMT          1
1481 #define IN_ITERATION_STMT       2
1482 #define IN_OMP_BLOCK            4
1483 #define IN_OMP_FOR              8
1484 #define IN_IF_STMT             16
1485   unsigned char in_statement;
1486
1487   /* TRUE if we are presently parsing the body of a switch statement.
1488      Note that this doesn't quite overlap with in_statement above.
1489      The difference relates to giving the right sets of error messages:
1490      "case not in switch" vs "break statement used with OpenMP...".  */
1491   bool in_switch_statement_p;
1492
1493   /* TRUE if we are parsing a type-id in an expression context.  In
1494      such a situation, both "type (expr)" and "type (type)" are valid
1495      alternatives.  */
1496   bool in_type_id_in_expr_p;
1497
1498   /* TRUE if we are currently in a header file where declarations are
1499      implicitly extern "C".  */
1500   bool implicit_extern_c;
1501
1502   /* TRUE if strings in expressions should be translated to the execution
1503      character set.  */
1504   bool translate_strings_p;
1505
1506   /* TRUE if we are presently parsing the body of a function, but not
1507      a local class.  */
1508   bool in_function_body;
1509
1510   /* If non-NULL, then we are parsing a construct where new type
1511      definitions are not permitted.  The string stored here will be
1512      issued as an error message if a type is defined.  */
1513   const char *type_definition_forbidden_message;
1514
1515   /* A list of lists. The outer list is a stack, used for member
1516      functions of local classes. At each level there are two sub-list,
1517      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1518      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1519      TREE_VALUE's. The functions are chained in reverse declaration
1520      order.
1521
1522      The TREE_PURPOSE sublist contains those functions with default
1523      arguments that need post processing, and the TREE_VALUE sublist
1524      contains those functions with definitions that need post
1525      processing.
1526
1527      These lists can only be processed once the outermost class being
1528      defined is complete.  */
1529   tree unparsed_functions_queues;
1530
1531   /* The number of classes whose definitions are currently in
1532      progress.  */
1533   unsigned num_classes_being_defined;
1534
1535   /* The number of template parameter lists that apply directly to the
1536      current declaration.  */
1537   unsigned num_template_parameter_lists;
1538 } cp_parser;
1539
1540 /* Prototypes.  */
1541
1542 /* Constructors and destructors.  */
1543
1544 static cp_parser *cp_parser_new
1545   (void);
1546
1547 /* Routines to parse various constructs.
1548
1549    Those that return `tree' will return the error_mark_node (rather
1550    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1551    Sometimes, they will return an ordinary node if error-recovery was
1552    attempted, even though a parse error occurred.  So, to check
1553    whether or not a parse error occurred, you should always use
1554    cp_parser_error_occurred.  If the construct is optional (indicated
1555    either by an `_opt' in the name of the function that does the
1556    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1557    the construct is not present.  */
1558
1559 /* Lexical conventions [gram.lex]  */
1560
1561 static tree cp_parser_identifier
1562   (cp_parser *);
1563 static tree cp_parser_string_literal
1564   (cp_parser *, bool, bool);
1565
1566 /* Basic concepts [gram.basic]  */
1567
1568 static bool cp_parser_translation_unit
1569   (cp_parser *);
1570
1571 /* Expressions [gram.expr]  */
1572
1573 static tree cp_parser_primary_expression
1574   (cp_parser *, bool, bool, bool, cp_id_kind *);
1575 static tree cp_parser_id_expression
1576   (cp_parser *, bool, bool, bool *, bool, bool);
1577 static tree cp_parser_unqualified_id
1578   (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_nested_name_specifier_opt
1580   (cp_parser *, bool, bool, bool, bool);
1581 static tree cp_parser_nested_name_specifier
1582   (cp_parser *, bool, bool, bool, bool);
1583 static tree cp_parser_qualifying_entity
1584   (cp_parser *, bool, bool, bool, bool, bool);
1585 static tree cp_parser_postfix_expression
1586   (cp_parser *, bool, bool, bool, cp_id_kind *);
1587 static tree cp_parser_postfix_open_square_expression
1588   (cp_parser *, tree, bool);
1589 static tree cp_parser_postfix_dot_deref_expression
1590   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1591 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1592   (cp_parser *, bool, bool, bool, bool *);
1593 static void cp_parser_pseudo_destructor_name
1594   (cp_parser *, tree *, tree *);
1595 static tree cp_parser_unary_expression
1596   (cp_parser *, bool, bool, cp_id_kind *);
1597 static enum tree_code cp_parser_unary_operator
1598   (cp_token *);
1599 static tree cp_parser_new_expression
1600   (cp_parser *);
1601 static VEC(tree,gc) *cp_parser_new_placement
1602   (cp_parser *);
1603 static tree cp_parser_new_type_id
1604   (cp_parser *, tree *);
1605 static cp_declarator *cp_parser_new_declarator_opt
1606   (cp_parser *);
1607 static cp_declarator *cp_parser_direct_new_declarator
1608   (cp_parser *);
1609 static VEC(tree,gc) *cp_parser_new_initializer
1610   (cp_parser *);
1611 static tree cp_parser_delete_expression
1612   (cp_parser *);
1613 static tree cp_parser_cast_expression
1614   (cp_parser *, bool, bool, cp_id_kind *);
1615 static tree cp_parser_binary_expression
1616   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1617 static tree cp_parser_question_colon_clause
1618   (cp_parser *, tree);
1619 static tree cp_parser_assignment_expression
1620   (cp_parser *, bool, cp_id_kind *);
1621 static enum tree_code cp_parser_assignment_operator_opt
1622   (cp_parser *);
1623 static tree cp_parser_expression
1624   (cp_parser *, bool, cp_id_kind *);
1625 static tree cp_parser_constant_expression
1626   (cp_parser *, bool, bool *);
1627 static tree cp_parser_builtin_offsetof
1628   (cp_parser *);
1629 static tree cp_parser_lambda_expression
1630   (cp_parser *);
1631 static void cp_parser_lambda_introducer
1632   (cp_parser *, tree);
1633 static void cp_parser_lambda_declarator_opt
1634   (cp_parser *, tree);
1635 static void cp_parser_lambda_body
1636   (cp_parser *, tree);
1637
1638 /* Statements [gram.stmt.stmt]  */
1639
1640 static void cp_parser_statement
1641   (cp_parser *, tree, bool, bool *);
1642 static void cp_parser_label_for_labeled_statement
1643   (cp_parser *);
1644 static tree cp_parser_expression_statement
1645   (cp_parser *, tree);
1646 static tree cp_parser_compound_statement
1647   (cp_parser *, tree, bool);
1648 static void cp_parser_statement_seq_opt
1649   (cp_parser *, tree);
1650 static tree cp_parser_selection_statement
1651   (cp_parser *, bool *);
1652 static tree cp_parser_condition
1653   (cp_parser *);
1654 static tree cp_parser_iteration_statement
1655   (cp_parser *);
1656 static void cp_parser_for_init_statement
1657   (cp_parser *);
1658 static tree cp_parser_jump_statement
1659   (cp_parser *);
1660 static void cp_parser_declaration_statement
1661   (cp_parser *);
1662
1663 static tree cp_parser_implicitly_scoped_statement
1664   (cp_parser *, bool *);
1665 static void cp_parser_already_scoped_statement
1666   (cp_parser *);
1667
1668 /* Declarations [gram.dcl.dcl] */
1669
1670 static void cp_parser_declaration_seq_opt
1671   (cp_parser *);
1672 static void cp_parser_declaration
1673   (cp_parser *);
1674 static void cp_parser_block_declaration
1675   (cp_parser *, bool);
1676 static void cp_parser_simple_declaration
1677   (cp_parser *, bool);
1678 static void cp_parser_decl_specifier_seq
1679   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1680 static tree cp_parser_storage_class_specifier_opt
1681   (cp_parser *);
1682 static tree cp_parser_function_specifier_opt
1683   (cp_parser *, cp_decl_specifier_seq *);
1684 static tree cp_parser_type_specifier
1685   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1686    int *, bool *);
1687 static tree cp_parser_simple_type_specifier
1688   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1689 static tree cp_parser_type_name
1690   (cp_parser *);
1691 static tree cp_parser_nonclass_name 
1692   (cp_parser* parser);
1693 static tree cp_parser_elaborated_type_specifier
1694   (cp_parser *, bool, bool);
1695 static tree cp_parser_enum_specifier
1696   (cp_parser *);
1697 static void cp_parser_enumerator_list
1698   (cp_parser *, tree);
1699 static void cp_parser_enumerator_definition
1700   (cp_parser *, tree);
1701 static tree cp_parser_namespace_name
1702   (cp_parser *);
1703 static void cp_parser_namespace_definition
1704   (cp_parser *);
1705 static void cp_parser_namespace_body
1706   (cp_parser *);
1707 static tree cp_parser_qualified_namespace_specifier
1708   (cp_parser *);
1709 static void cp_parser_namespace_alias_definition
1710   (cp_parser *);
1711 static bool cp_parser_using_declaration
1712   (cp_parser *, bool);
1713 static void cp_parser_using_directive
1714   (cp_parser *);
1715 static void cp_parser_asm_definition
1716   (cp_parser *);
1717 static void cp_parser_linkage_specification
1718   (cp_parser *);
1719 static void cp_parser_static_assert
1720   (cp_parser *, bool);
1721 static tree cp_parser_decltype
1722   (cp_parser *);
1723
1724 /* Declarators [gram.dcl.decl] */
1725
1726 static tree cp_parser_init_declarator
1727   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1728 static cp_declarator *cp_parser_declarator
1729   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1730 static cp_declarator *cp_parser_direct_declarator
1731   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1732 static enum tree_code cp_parser_ptr_operator
1733   (cp_parser *, tree *, cp_cv_quals *);
1734 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1735   (cp_parser *);
1736 static tree cp_parser_late_return_type_opt
1737   (cp_parser *);
1738 static tree cp_parser_declarator_id
1739   (cp_parser *, bool);
1740 static tree cp_parser_type_id
1741   (cp_parser *);
1742 static tree cp_parser_template_type_arg
1743   (cp_parser *);
1744 static tree cp_parser_type_id_1
1745   (cp_parser *, bool);
1746 static void cp_parser_type_specifier_seq
1747   (cp_parser *, bool, cp_decl_specifier_seq *);
1748 static tree cp_parser_parameter_declaration_clause
1749   (cp_parser *);
1750 static tree cp_parser_parameter_declaration_list
1751   (cp_parser *, bool *);
1752 static cp_parameter_declarator *cp_parser_parameter_declaration
1753   (cp_parser *, bool, bool *);
1754 static tree cp_parser_default_argument 
1755   (cp_parser *, bool);
1756 static void cp_parser_function_body
1757   (cp_parser *);
1758 static tree cp_parser_initializer
1759   (cp_parser *, bool *, bool *);
1760 static tree cp_parser_initializer_clause
1761   (cp_parser *, bool *);
1762 static tree cp_parser_braced_list
1763   (cp_parser*, bool*);
1764 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1765   (cp_parser *, bool *);
1766
1767 static bool cp_parser_ctor_initializer_opt_and_function_body
1768   (cp_parser *);
1769
1770 /* Classes [gram.class] */
1771
1772 static tree cp_parser_class_name
1773   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1774 static tree cp_parser_class_specifier
1775   (cp_parser *);
1776 static tree cp_parser_class_head
1777   (cp_parser *, bool *, tree *, tree *);
1778 static enum tag_types cp_parser_class_key
1779   (cp_parser *);
1780 static void cp_parser_member_specification_opt
1781   (cp_parser *);
1782 static void cp_parser_member_declaration
1783   (cp_parser *);
1784 static tree cp_parser_pure_specifier
1785   (cp_parser *);
1786 static tree cp_parser_constant_initializer
1787   (cp_parser *);
1788
1789 /* Derived classes [gram.class.derived] */
1790
1791 static tree cp_parser_base_clause
1792   (cp_parser *);
1793 static tree cp_parser_base_specifier
1794   (cp_parser *);
1795
1796 /* Special member functions [gram.special] */
1797
1798 static tree cp_parser_conversion_function_id
1799   (cp_parser *);
1800 static tree cp_parser_conversion_type_id
1801   (cp_parser *);
1802 static cp_declarator *cp_parser_conversion_declarator_opt
1803   (cp_parser *);
1804 static bool cp_parser_ctor_initializer_opt
1805   (cp_parser *);
1806 static void cp_parser_mem_initializer_list
1807   (cp_parser *);
1808 static tree cp_parser_mem_initializer
1809   (cp_parser *);
1810 static tree cp_parser_mem_initializer_id
1811   (cp_parser *);
1812
1813 /* Overloading [gram.over] */
1814
1815 static tree cp_parser_operator_function_id
1816   (cp_parser *);
1817 static tree cp_parser_operator
1818   (cp_parser *);
1819
1820 /* Templates [gram.temp] */
1821
1822 static void cp_parser_template_declaration
1823   (cp_parser *, bool);
1824 static tree cp_parser_template_parameter_list
1825   (cp_parser *);
1826 static tree cp_parser_template_parameter
1827   (cp_parser *, bool *, bool *);
1828 static tree cp_parser_type_parameter
1829   (cp_parser *, bool *);
1830 static tree cp_parser_template_id
1831   (cp_parser *, bool, bool, bool);
1832 static tree cp_parser_template_name
1833   (cp_parser *, bool, bool, bool, bool *);
1834 static tree cp_parser_template_argument_list
1835   (cp_parser *);
1836 static tree cp_parser_template_argument
1837   (cp_parser *);
1838 static void cp_parser_explicit_instantiation
1839   (cp_parser *);
1840 static void cp_parser_explicit_specialization
1841   (cp_parser *);
1842
1843 /* Exception handling [gram.exception] */
1844
1845 static tree cp_parser_try_block
1846   (cp_parser *);
1847 static bool cp_parser_function_try_block
1848   (cp_parser *);
1849 static void cp_parser_handler_seq
1850   (cp_parser *);
1851 static void cp_parser_handler
1852   (cp_parser *);
1853 static tree cp_parser_exception_declaration
1854   (cp_parser *);
1855 static tree cp_parser_throw_expression
1856   (cp_parser *);
1857 static tree cp_parser_exception_specification_opt
1858   (cp_parser *);
1859 static tree cp_parser_type_id_list
1860   (cp_parser *);
1861
1862 /* GNU Extensions */
1863
1864 static tree cp_parser_asm_specification_opt
1865   (cp_parser *);
1866 static tree cp_parser_asm_operand_list
1867   (cp_parser *);
1868 static tree cp_parser_asm_clobber_list
1869   (cp_parser *);
1870 static tree cp_parser_asm_label_list
1871   (cp_parser *);
1872 static tree cp_parser_attributes_opt
1873   (cp_parser *);
1874 static tree cp_parser_attribute_list
1875   (cp_parser *);
1876 static bool cp_parser_extension_opt
1877   (cp_parser *, int *);
1878 static void cp_parser_label_declaration
1879   (cp_parser *);
1880
1881 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1882 static bool cp_parser_pragma
1883   (cp_parser *, enum pragma_context);
1884
1885 /* Objective-C++ Productions */
1886
1887 static tree cp_parser_objc_message_receiver
1888   (cp_parser *);
1889 static tree cp_parser_objc_message_args
1890   (cp_parser *);
1891 static tree cp_parser_objc_message_expression
1892   (cp_parser *);
1893 static tree cp_parser_objc_encode_expression
1894   (cp_parser *);
1895 static tree cp_parser_objc_defs_expression
1896   (cp_parser *);
1897 static tree cp_parser_objc_protocol_expression
1898   (cp_parser *);
1899 static tree cp_parser_objc_selector_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_expression
1902   (cp_parser *);
1903 static bool cp_parser_objc_selector_p
1904   (enum cpp_ttype);
1905 static tree cp_parser_objc_selector
1906   (cp_parser *);
1907 static tree cp_parser_objc_protocol_refs_opt
1908   (cp_parser *);
1909 static void cp_parser_objc_declaration
1910   (cp_parser *);
1911 static tree cp_parser_objc_statement
1912   (cp_parser *);
1913
1914 /* Utility Routines */
1915
1916 static tree cp_parser_lookup_name
1917   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1918 static tree cp_parser_lookup_name_simple
1919   (cp_parser *, tree, location_t);
1920 static tree cp_parser_maybe_treat_template_as_class
1921   (tree, bool);
1922 static bool cp_parser_check_declarator_template_parameters
1923   (cp_parser *, cp_declarator *, location_t);
1924 static bool cp_parser_check_template_parameters
1925   (cp_parser *, unsigned, location_t, cp_declarator *);
1926 static tree cp_parser_simple_cast_expression
1927   (cp_parser *);
1928 static tree cp_parser_global_scope_opt
1929   (cp_parser *, bool);
1930 static bool cp_parser_constructor_declarator_p
1931   (cp_parser *, bool);
1932 static tree cp_parser_function_definition_from_specifiers_and_declarator
1933   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1934 static tree cp_parser_function_definition_after_declarator
1935   (cp_parser *, bool);
1936 static void cp_parser_template_declaration_after_export
1937   (cp_parser *, bool);
1938 static void cp_parser_perform_template_parameter_access_checks
1939   (VEC (deferred_access_check,gc)*);
1940 static tree cp_parser_single_declaration
1941   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1942 static tree cp_parser_functional_cast
1943   (cp_parser *, tree);
1944 static tree cp_parser_save_member_function_body
1945   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1946 static tree cp_parser_enclosed_template_argument_list
1947   (cp_parser *);
1948 static void cp_parser_save_default_args
1949   (cp_parser *, tree);
1950 static void cp_parser_late_parsing_for_member
1951   (cp_parser *, tree);
1952 static void cp_parser_late_parsing_default_args
1953   (cp_parser *, tree);
1954 static tree cp_parser_sizeof_operand
1955   (cp_parser *, enum rid);
1956 static tree cp_parser_trait_expr
1957   (cp_parser *, enum rid);
1958 static bool cp_parser_declares_only_class_p
1959   (cp_parser *);
1960 static void cp_parser_set_storage_class
1961   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1962 static void cp_parser_set_decl_spec_type
1963   (cp_decl_specifier_seq *, tree, location_t, bool);
1964 static bool cp_parser_friend_p
1965   (const cp_decl_specifier_seq *);
1966 static cp_token *cp_parser_require
1967   (cp_parser *, enum cpp_ttype, const char *);
1968 static cp_token *cp_parser_require_keyword
1969   (cp_parser *, enum rid, const char *);
1970 static bool cp_parser_token_starts_function_definition_p
1971   (cp_token *);
1972 static bool cp_parser_next_token_starts_class_definition_p
1973   (cp_parser *);
1974 static bool cp_parser_next_token_ends_template_argument_p
1975   (cp_parser *);
1976 static bool cp_parser_nth_token_starts_template_argument_list_p
1977   (cp_parser *, size_t);
1978 static enum tag_types cp_parser_token_is_class_key
1979   (cp_token *);
1980 static void cp_parser_check_class_key
1981   (enum tag_types, tree type);
1982 static void cp_parser_check_access_in_redeclaration
1983   (tree type, location_t location);
1984 static bool cp_parser_optional_template_keyword
1985   (cp_parser *);
1986 static void cp_parser_pre_parsed_nested_name_specifier
1987   (cp_parser *);
1988 static bool cp_parser_cache_group
1989   (cp_parser *, enum cpp_ttype, unsigned);
1990 static void cp_parser_parse_tentatively
1991   (cp_parser *);
1992 static void cp_parser_commit_to_tentative_parse
1993   (cp_parser *);
1994 static void cp_parser_abort_tentative_parse
1995   (cp_parser *);
1996 static bool cp_parser_parse_definitely
1997   (cp_parser *);
1998 static inline bool cp_parser_parsing_tentatively
1999   (cp_parser *);
2000 static bool cp_parser_uncommitted_to_tentative_parse_p
2001   (cp_parser *);
2002 static void cp_parser_error
2003   (cp_parser *, const char *);
2004 static void cp_parser_name_lookup_error
2005   (cp_parser *, tree, tree, const char *, location_t);
2006 static bool cp_parser_simulate_error
2007   (cp_parser *);
2008 static bool cp_parser_check_type_definition
2009   (cp_parser *);
2010 static void cp_parser_check_for_definition_in_return_type
2011   (cp_declarator *, tree, location_t type_location);
2012 static void cp_parser_check_for_invalid_template_id
2013   (cp_parser *, tree, location_t location);
2014 static bool cp_parser_non_integral_constant_expression
2015   (cp_parser *, const char *);
2016 static void cp_parser_diagnose_invalid_type_name
2017   (cp_parser *, tree, tree, location_t);
2018 static bool cp_parser_parse_and_diagnose_invalid_type_name
2019   (cp_parser *);
2020 static int cp_parser_skip_to_closing_parenthesis
2021   (cp_parser *, bool, bool, bool);
2022 static void cp_parser_skip_to_end_of_statement
2023   (cp_parser *);
2024 static void cp_parser_consume_semicolon_at_end_of_statement
2025   (cp_parser *);
2026 static void cp_parser_skip_to_end_of_block_or_statement
2027   (cp_parser *);
2028 static bool cp_parser_skip_to_closing_brace
2029   (cp_parser *);
2030 static void cp_parser_skip_to_end_of_template_parameter_list
2031   (cp_parser *);
2032 static void cp_parser_skip_to_pragma_eol
2033   (cp_parser*, cp_token *);
2034 static bool cp_parser_error_occurred
2035   (cp_parser *);
2036 static bool cp_parser_allow_gnu_extensions_p
2037   (cp_parser *);
2038 static bool cp_parser_is_string_literal
2039   (cp_token *);
2040 static bool cp_parser_is_keyword
2041   (cp_token *, enum rid);
2042 static tree cp_parser_make_typename_type
2043   (cp_parser *, tree, tree, location_t location);
2044 static cp_declarator * cp_parser_make_indirect_declarator
2045   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2046
2047 /* Returns nonzero if we are parsing tentatively.  */
2048
2049 static inline bool
2050 cp_parser_parsing_tentatively (cp_parser* parser)
2051 {
2052   return parser->context->next != NULL;
2053 }
2054
2055 /* Returns nonzero if TOKEN is a string literal.  */
2056
2057 static bool
2058 cp_parser_is_string_literal (cp_token* token)
2059 {
2060   return (token->type == CPP_STRING ||
2061           token->type == CPP_STRING16 ||
2062           token->type == CPP_STRING32 ||
2063           token->type == CPP_WSTRING);
2064 }
2065
2066 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2067
2068 static bool
2069 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2070 {
2071   return token->keyword == keyword;
2072 }
2073
2074 /* If not parsing tentatively, issue a diagnostic of the form
2075       FILE:LINE: MESSAGE before TOKEN
2076    where TOKEN is the next token in the input stream.  MESSAGE
2077    (specified by the caller) is usually of the form "expected
2078    OTHER-TOKEN".  */
2079
2080 static void
2081 cp_parser_error (cp_parser* parser, const char* message)
2082 {
2083   if (!cp_parser_simulate_error (parser))
2084     {
2085       cp_token *token = cp_lexer_peek_token (parser->lexer);
2086       /* This diagnostic makes more sense if it is tagged to the line
2087          of the token we just peeked at.  */
2088       cp_lexer_set_source_position_from_token (token);
2089
2090       if (token->type == CPP_PRAGMA)
2091         {
2092           error_at (token->location,
2093                     "%<#pragma%> is not allowed here");
2094           cp_parser_skip_to_pragma_eol (parser, token);
2095           return;
2096         }
2097
2098       c_parse_error (message,
2099                      /* Because c_parser_error does not understand
2100                         CPP_KEYWORD, keywords are treated like
2101                         identifiers.  */
2102                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2103                      token->u.value, token->flags);
2104     }
2105 }
2106
2107 /* Issue an error about name-lookup failing.  NAME is the
2108    IDENTIFIER_NODE DECL is the result of
2109    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2110    the thing that we hoped to find.  */
2111
2112 static void
2113 cp_parser_name_lookup_error (cp_parser* parser,
2114                              tree name,
2115                              tree decl,
2116                              const char* desired,
2117                              location_t location)
2118 {
2119   /* If name lookup completely failed, tell the user that NAME was not
2120      declared.  */
2121   if (decl == error_mark_node)
2122     {
2123       if (parser->scope && parser->scope != global_namespace)
2124         error_at (location, "%<%E::%E%> has not been declared",
2125                   parser->scope, name);
2126       else if (parser->scope == global_namespace)
2127         error_at (location, "%<::%E%> has not been declared", name);
2128       else if (parser->object_scope
2129                && !CLASS_TYPE_P (parser->object_scope))
2130         error_at (location, "request for member %qE in non-class type %qT",
2131                   name, parser->object_scope);
2132       else if (parser->object_scope)
2133         error_at (location, "%<%T::%E%> has not been declared",
2134                   parser->object_scope, name);
2135       else
2136         error_at (location, "%qE has not been declared", name);
2137     }
2138   else if (parser->scope && parser->scope != global_namespace)
2139     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2140   else if (parser->scope == global_namespace)
2141     error_at (location, "%<::%E%> %s", name, desired);
2142   else
2143     error_at (location, "%qE %s", name, desired);
2144 }
2145
2146 /* If we are parsing tentatively, remember that an error has occurred
2147    during this tentative parse.  Returns true if the error was
2148    simulated; false if a message should be issued by the caller.  */
2149
2150 static bool
2151 cp_parser_simulate_error (cp_parser* parser)
2152 {
2153   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2154     {
2155       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2156       return true;
2157     }
2158   return false;
2159 }
2160
2161 /* Check for repeated decl-specifiers.  */
2162
2163 static void
2164 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2165                            location_t location)
2166 {
2167   int ds;
2168
2169   for (ds = ds_first; ds != ds_last; ++ds)
2170     {
2171       unsigned count = decl_specs->specs[ds];
2172       if (count < 2)
2173         continue;
2174       /* The "long" specifier is a special case because of "long long".  */
2175       if (ds == ds_long)
2176         {
2177           if (count > 2)
2178             error_at (location, "%<long long long%> is too long for GCC");
2179           else 
2180             pedwarn_cxx98 (location, OPT_Wlong_long, 
2181                            "ISO C++ 1998 does not support %<long long%>");
2182         }
2183       else if (count > 1)
2184         {
2185           static const char *const decl_spec_names[] = {
2186             "signed",
2187             "unsigned",
2188             "short",
2189             "long",
2190             "const",
2191             "volatile",
2192             "restrict",
2193             "inline",
2194             "virtual",
2195             "explicit",
2196             "friend",
2197             "typedef",
2198             "constexpr",
2199             "__complex",
2200             "__thread"
2201           };
2202           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2203         }
2204     }
2205 }
2206
2207 /* This function is called when a type is defined.  If type
2208    definitions are forbidden at this point, an error message is
2209    issued.  */
2210
2211 static bool
2212 cp_parser_check_type_definition (cp_parser* parser)
2213 {
2214   /* If types are forbidden here, issue a message.  */
2215   if (parser->type_definition_forbidden_message)
2216     {
2217       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2218          in the message need to be interpreted.  */
2219       error (parser->type_definition_forbidden_message);
2220       return false;
2221     }
2222   return true;
2223 }
2224
2225 /* This function is called when the DECLARATOR is processed.  The TYPE
2226    was a type defined in the decl-specifiers.  If it is invalid to
2227    define a type in the decl-specifiers for DECLARATOR, an error is
2228    issued. TYPE_LOCATION is the location of TYPE and is used
2229    for error reporting.  */
2230
2231 static void
2232 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2233                                                tree type, location_t type_location)
2234 {
2235   /* [dcl.fct] forbids type definitions in return types.
2236      Unfortunately, it's not easy to know whether or not we are
2237      processing a return type until after the fact.  */
2238   while (declarator
2239          && (declarator->kind == cdk_pointer
2240              || declarator->kind == cdk_reference
2241              || declarator->kind == cdk_ptrmem))
2242     declarator = declarator->declarator;
2243   if (declarator
2244       && declarator->kind == cdk_function)
2245     {
2246       error_at (type_location,
2247                 "new types may not be defined in a return type");
2248       inform (type_location, 
2249               "(perhaps a semicolon is missing after the definition of %qT)",
2250               type);
2251     }
2252 }
2253
2254 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2255    "<" in any valid C++ program.  If the next token is indeed "<",
2256    issue a message warning the user about what appears to be an
2257    invalid attempt to form a template-id. LOCATION is the location
2258    of the type-specifier (TYPE) */
2259
2260 static void
2261 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2262                                          tree type, location_t location)
2263 {
2264   cp_token_position start = 0;
2265
2266   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2267     {
2268       if (TYPE_P (type))
2269         error_at (location, "%qT is not a template", type);
2270       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2271         error_at (location, "%qE is not a template", type);
2272       else
2273         error_at (location, "invalid template-id");
2274       /* Remember the location of the invalid "<".  */
2275       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2276         start = cp_lexer_token_position (parser->lexer, true);
2277       /* Consume the "<".  */
2278       cp_lexer_consume_token (parser->lexer);
2279       /* Parse the template arguments.  */
2280       cp_parser_enclosed_template_argument_list (parser);
2281       /* Permanently remove the invalid template arguments so that
2282          this error message is not issued again.  */
2283       if (start)
2284         cp_lexer_purge_tokens_after (parser->lexer, start);
2285     }
2286 }
2287
2288 /* If parsing an integral constant-expression, issue an error message
2289    about the fact that THING appeared and return true.  Otherwise,
2290    return false.  In either case, set
2291    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2292
2293 static bool
2294 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2295                                             const char *thing)
2296 {
2297   parser->non_integral_constant_expression_p = true;
2298   if (parser->integral_constant_expression_p)
2299     {
2300       if (!parser->allow_non_integral_constant_expression_p)
2301         {
2302           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2303              in the message need to be interpreted.  */
2304           char *message = concat (thing,
2305                                   " cannot appear in a constant-expression",
2306                                   NULL);
2307           error (message);
2308           free (message);
2309           return true;
2310         }
2311     }
2312   return false;
2313 }
2314
2315 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2316    qualifying scope (or NULL, if none) for ID.  This function commits
2317    to the current active tentative parse, if any.  (Otherwise, the
2318    problematic construct might be encountered again later, resulting
2319    in duplicate error messages.) LOCATION is the location of ID.  */
2320
2321 static void
2322 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2323                                       tree scope, tree id,
2324                                       location_t location)
2325 {
2326   tree decl, old_scope;
2327   /* Try to lookup the identifier.  */
2328   old_scope = parser->scope;
2329   parser->scope = scope;
2330   decl = cp_parser_lookup_name_simple (parser, id, location);
2331   parser->scope = old_scope;
2332   /* If the lookup found a template-name, it means that the user forgot
2333   to specify an argument list. Emit a useful error message.  */
2334   if (TREE_CODE (decl) == TEMPLATE_DECL)
2335     error_at (location,
2336               "invalid use of template-name %qE without an argument list",
2337               decl);
2338   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2339     error_at (location, "invalid use of destructor %qD as a type", id);
2340   else if (TREE_CODE (decl) == TYPE_DECL)
2341     /* Something like 'unsigned A a;'  */
2342     error_at (location, "invalid combination of multiple type-specifiers");
2343   else if (!parser->scope)
2344     {
2345       /* Issue an error message.  */
2346       error_at (location, "%qE does not name a type", id);
2347       /* If we're in a template class, it's possible that the user was
2348          referring to a type from a base class.  For example:
2349
2350            template <typename T> struct A { typedef T X; };
2351            template <typename T> struct B : public A<T> { X x; };
2352
2353          The user should have said "typename A<T>::X".  */
2354       if (processing_template_decl && current_class_type
2355           && TYPE_BINFO (current_class_type))
2356         {
2357           tree b;
2358
2359           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2360                b;
2361                b = TREE_CHAIN (b))
2362             {
2363               tree base_type = BINFO_TYPE (b);
2364               if (CLASS_TYPE_P (base_type)
2365                   && dependent_type_p (base_type))
2366                 {
2367                   tree field;
2368                   /* Go from a particular instantiation of the
2369                      template (which will have an empty TYPE_FIELDs),
2370                      to the main version.  */
2371                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2372                   for (field = TYPE_FIELDS (base_type);
2373                        field;
2374                        field = TREE_CHAIN (field))
2375                     if (TREE_CODE (field) == TYPE_DECL
2376                         && DECL_NAME (field) == id)
2377                       {
2378                         inform (location, 
2379                                 "(perhaps %<typename %T::%E%> was intended)",
2380                                 BINFO_TYPE (b), id);
2381                         break;
2382                       }
2383                   if (field)
2384                     break;
2385                 }
2386             }
2387         }
2388     }
2389   /* Here we diagnose qualified-ids where the scope is actually correct,
2390      but the identifier does not resolve to a valid type name.  */
2391   else if (parser->scope != error_mark_node)
2392     {
2393       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2394         error_at (location, "%qE in namespace %qE does not name a type",
2395                   id, parser->scope);
2396       else if (TYPE_P (parser->scope))
2397         error_at (location, "%qE in class %qT does not name a type",
2398                   id, parser->scope);
2399       else
2400         gcc_unreachable ();
2401     }
2402   cp_parser_commit_to_tentative_parse (parser);
2403 }
2404
2405 /* Check for a common situation where a type-name should be present,
2406    but is not, and issue a sensible error message.  Returns true if an
2407    invalid type-name was detected.
2408
2409    The situation handled by this function are variable declarations of the
2410    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2411    Usually, `ID' should name a type, but if we got here it means that it
2412    does not. We try to emit the best possible error message depending on
2413    how exactly the id-expression looks like.  */
2414
2415 static bool
2416 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2417 {
2418   tree id;
2419   cp_token *token = cp_lexer_peek_token (parser->lexer);
2420
2421   cp_parser_parse_tentatively (parser);
2422   id = cp_parser_id_expression (parser,
2423                                 /*template_keyword_p=*/false,
2424                                 /*check_dependency_p=*/true,
2425                                 /*template_p=*/NULL,
2426                                 /*declarator_p=*/true,
2427                                 /*optional_p=*/false);
2428   /* After the id-expression, there should be a plain identifier,
2429      otherwise this is not a simple variable declaration. Also, if
2430      the scope is dependent, we cannot do much.  */
2431   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2432       || (parser->scope && TYPE_P (parser->scope)
2433           && dependent_type_p (parser->scope))
2434       || TREE_CODE (id) == TYPE_DECL)
2435     {
2436       cp_parser_abort_tentative_parse (parser);
2437       return false;
2438     }
2439   if (!cp_parser_parse_definitely (parser))
2440     return false;
2441
2442   /* Emit a diagnostic for the invalid type.  */
2443   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2444                                         id, token->location);
2445   /* Skip to the end of the declaration; there's no point in
2446      trying to process it.  */
2447   cp_parser_skip_to_end_of_block_or_statement (parser);
2448   return true;
2449 }
2450
2451 /* Consume tokens up to, and including, the next non-nested closing `)'.
2452    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2453    are doing error recovery. Returns -1 if OR_COMMA is true and we
2454    found an unnested comma.  */
2455
2456 static int
2457 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2458                                        bool recovering,
2459                                        bool or_comma,
2460                                        bool consume_paren)
2461 {
2462   unsigned paren_depth = 0;
2463   unsigned brace_depth = 0;
2464   unsigned square_depth = 0;
2465
2466   if (recovering && !or_comma
2467       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2468     return 0;
2469
2470   while (true)
2471     {
2472       cp_token * token = cp_lexer_peek_token (parser->lexer);
2473
2474       switch (token->type)
2475         {
2476         case CPP_EOF:
2477         case CPP_PRAGMA_EOL:
2478           /* If we've run out of tokens, then there is no closing `)'.  */
2479           return 0;
2480
2481         /* This is good for lambda expression capture-lists.  */
2482         case CPP_OPEN_SQUARE:
2483           ++square_depth;
2484           break;
2485         case CPP_CLOSE_SQUARE:
2486           if (!square_depth--)
2487             return 0;
2488           break;
2489
2490         case CPP_SEMICOLON:
2491           /* This matches the processing in skip_to_end_of_statement.  */
2492           if (!brace_depth)
2493             return 0;
2494           break;
2495
2496         case CPP_OPEN_BRACE:
2497           ++brace_depth;
2498           break;
2499         case CPP_CLOSE_BRACE:
2500           if (!brace_depth--)
2501             return 0;
2502           break;
2503
2504         case CPP_COMMA:
2505           if (recovering && or_comma && !brace_depth && !paren_depth
2506               && !square_depth)
2507             return -1;
2508           break;
2509
2510         case CPP_OPEN_PAREN:
2511           if (!brace_depth)
2512             ++paren_depth;
2513           break;
2514
2515         case CPP_CLOSE_PAREN:
2516           if (!brace_depth && !paren_depth--)
2517             {
2518               if (consume_paren)
2519                 cp_lexer_consume_token (parser->lexer);
2520               return 1;
2521             }
2522           break;
2523
2524         default:
2525           break;
2526         }
2527
2528       /* Consume the token.  */
2529       cp_lexer_consume_token (parser->lexer);
2530     }
2531 }
2532
2533 /* Consume tokens until we reach the end of the current statement.
2534    Normally, that will be just before consuming a `;'.  However, if a
2535    non-nested `}' comes first, then we stop before consuming that.  */
2536
2537 static void
2538 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2539 {
2540   unsigned nesting_depth = 0;
2541
2542   while (true)
2543     {
2544       cp_token *token = cp_lexer_peek_token (parser->lexer);
2545
2546       switch (token->type)
2547         {
2548         case CPP_EOF:
2549         case CPP_PRAGMA_EOL:
2550           /* If we've run out of tokens, stop.  */
2551           return;
2552
2553         case CPP_SEMICOLON:
2554           /* If the next token is a `;', we have reached the end of the
2555              statement.  */
2556           if (!nesting_depth)
2557             return;
2558           break;
2559
2560         case CPP_CLOSE_BRACE:
2561           /* If this is a non-nested '}', stop before consuming it.
2562              That way, when confronted with something like:
2563
2564                { 3 + }
2565
2566              we stop before consuming the closing '}', even though we
2567              have not yet reached a `;'.  */
2568           if (nesting_depth == 0)
2569             return;
2570
2571           /* If it is the closing '}' for a block that we have
2572              scanned, stop -- but only after consuming the token.
2573              That way given:
2574
2575                 void f g () { ... }
2576                 typedef int I;
2577
2578              we will stop after the body of the erroneously declared
2579              function, but before consuming the following `typedef'
2580              declaration.  */
2581           if (--nesting_depth == 0)
2582             {
2583               cp_lexer_consume_token (parser->lexer);
2584               return;
2585             }
2586
2587         case CPP_OPEN_BRACE:
2588           ++nesting_depth;
2589           break;
2590
2591         default:
2592           break;
2593         }
2594
2595       /* Consume the token.  */
2596       cp_lexer_consume_token (parser->lexer);
2597     }
2598 }
2599
2600 /* This function is called at the end of a statement or declaration.
2601    If the next token is a semicolon, it is consumed; otherwise, error
2602    recovery is attempted.  */
2603
2604 static void
2605 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2606 {
2607   /* Look for the trailing `;'.  */
2608   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2609     {
2610       /* If there is additional (erroneous) input, skip to the end of
2611          the statement.  */
2612       cp_parser_skip_to_end_of_statement (parser);
2613       /* If the next token is now a `;', consume it.  */
2614       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2615         cp_lexer_consume_token (parser->lexer);
2616     }
2617 }
2618
2619 /* Skip tokens until we have consumed an entire block, or until we
2620    have consumed a non-nested `;'.  */
2621
2622 static void
2623 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2624 {
2625   int nesting_depth = 0;
2626
2627   while (nesting_depth >= 0)
2628     {
2629       cp_token *token = cp_lexer_peek_token (parser->lexer);
2630
2631       switch (token->type)
2632         {
2633         case CPP_EOF:
2634         case CPP_PRAGMA_EOL:
2635           /* If we've run out of tokens, stop.  */
2636           return;
2637
2638         case CPP_SEMICOLON:
2639           /* Stop if this is an unnested ';'. */
2640           if (!nesting_depth)
2641             nesting_depth = -1;
2642           break;
2643
2644         case CPP_CLOSE_BRACE:
2645           /* Stop if this is an unnested '}', or closes the outermost
2646              nesting level.  */
2647           nesting_depth--;
2648           if (nesting_depth < 0)
2649             return;
2650           if (!nesting_depth)
2651             nesting_depth = -1;
2652           break;
2653
2654         case CPP_OPEN_BRACE:
2655           /* Nest. */
2656           nesting_depth++;
2657           break;
2658
2659         default:
2660           break;
2661         }
2662
2663       /* Consume the token.  */
2664       cp_lexer_consume_token (parser->lexer);
2665     }
2666 }
2667
2668 /* Skip tokens until a non-nested closing curly brace is the next
2669    token, or there are no more tokens. Return true in the first case,
2670    false otherwise.  */
2671
2672 static bool
2673 cp_parser_skip_to_closing_brace (cp_parser *parser)
2674 {
2675   unsigned nesting_depth = 0;
2676
2677   while (true)
2678     {
2679       cp_token *token = cp_lexer_peek_token (parser->lexer);
2680
2681       switch (token->type)
2682         {
2683         case CPP_EOF:
2684         case CPP_PRAGMA_EOL:
2685           /* If we've run out of tokens, stop.  */
2686           return false;
2687
2688         case CPP_CLOSE_BRACE:
2689           /* If the next token is a non-nested `}', then we have reached
2690              the end of the current block.  */
2691           if (nesting_depth-- == 0)
2692             return true;
2693           break;
2694
2695         case CPP_OPEN_BRACE:
2696           /* If it the next token is a `{', then we are entering a new
2697              block.  Consume the entire block.  */
2698           ++nesting_depth;
2699           break;
2700
2701         default:
2702           break;
2703         }
2704
2705       /* Consume the token.  */
2706       cp_lexer_consume_token (parser->lexer);
2707     }
2708 }
2709
2710 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2711    parameter is the PRAGMA token, allowing us to purge the entire pragma
2712    sequence.  */
2713
2714 static void
2715 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2716 {
2717   cp_token *token;
2718
2719   parser->lexer->in_pragma = false;
2720
2721   do
2722     token = cp_lexer_consume_token (parser->lexer);
2723   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2724
2725   /* Ensure that the pragma is not parsed again.  */
2726   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2727 }
2728
2729 /* Require pragma end of line, resyncing with it as necessary.  The
2730    arguments are as for cp_parser_skip_to_pragma_eol.  */
2731
2732 static void
2733 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2734 {
2735   parser->lexer->in_pragma = false;
2736   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2737     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2738 }
2739
2740 /* This is a simple wrapper around make_typename_type. When the id is
2741    an unresolved identifier node, we can provide a superior diagnostic
2742    using cp_parser_diagnose_invalid_type_name.  */
2743
2744 static tree
2745 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2746                               tree id, location_t id_location)
2747 {
2748   tree result;
2749   if (TREE_CODE (id) == IDENTIFIER_NODE)
2750     {
2751       result = make_typename_type (scope, id, typename_type,
2752                                    /*complain=*/tf_none);
2753       if (result == error_mark_node)
2754         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2755       return result;
2756     }
2757   return make_typename_type (scope, id, typename_type, tf_error);
2758 }
2759
2760 /* This is a wrapper around the
2761    make_{pointer,ptrmem,reference}_declarator functions that decides
2762    which one to call based on the CODE and CLASS_TYPE arguments. The
2763    CODE argument should be one of the values returned by
2764    cp_parser_ptr_operator. */
2765 static cp_declarator *
2766 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2767                                     cp_cv_quals cv_qualifiers,
2768                                     cp_declarator *target)
2769 {
2770   if (code == ERROR_MARK)
2771     return cp_error_declarator;
2772
2773   if (code == INDIRECT_REF)
2774     if (class_type == NULL_TREE)
2775       return make_pointer_declarator (cv_qualifiers, target);
2776     else
2777       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2778   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2779     return make_reference_declarator (cv_qualifiers, target, false);
2780   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2781     return make_reference_declarator (cv_qualifiers, target, true);
2782   gcc_unreachable ();
2783 }
2784
2785 /* Create a new C++ parser.  */
2786
2787 static cp_parser *
2788 cp_parser_new (void)
2789 {
2790   cp_parser *parser;
2791   cp_lexer *lexer;
2792   unsigned i;
2793
2794   /* cp_lexer_new_main is called before calling ggc_alloc because
2795      cp_lexer_new_main might load a PCH file.  */
2796   lexer = cp_lexer_new_main ();
2797
2798   /* Initialize the binops_by_token so that we can get the tree
2799      directly from the token.  */
2800   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2801     binops_by_token[binops[i].token_type] = binops[i];
2802
2803   parser = GGC_CNEW (cp_parser);
2804   parser->lexer = lexer;
2805   parser->context = cp_parser_context_new (NULL);
2806
2807   /* For now, we always accept GNU extensions.  */
2808   parser->allow_gnu_extensions_p = 1;
2809
2810   /* The `>' token is a greater-than operator, not the end of a
2811      template-id.  */
2812   parser->greater_than_is_operator_p = true;
2813
2814   parser->default_arg_ok_p = true;
2815
2816   /* We are not parsing a constant-expression.  */
2817   parser->integral_constant_expression_p = false;
2818   parser->allow_non_integral_constant_expression_p = false;
2819   parser->non_integral_constant_expression_p = false;
2820
2821   /* Local variable names are not forbidden.  */
2822   parser->local_variables_forbidden_p = false;
2823
2824   /* We are not processing an `extern "C"' declaration.  */
2825   parser->in_unbraced_linkage_specification_p = false;
2826
2827   /* We are not processing a declarator.  */
2828   parser->in_declarator_p = false;
2829
2830   /* We are not processing a template-argument-list.  */
2831   parser->in_template_argument_list_p = false;
2832
2833   /* We are not in an iteration statement.  */
2834   parser->in_statement = 0;
2835
2836   /* We are not in a switch statement.  */
2837   parser->in_switch_statement_p = false;
2838
2839   /* We are not parsing a type-id inside an expression.  */
2840   parser->in_type_id_in_expr_p = false;
2841
2842   /* Declarations aren't implicitly extern "C".  */
2843   parser->implicit_extern_c = false;
2844
2845   /* String literals should be translated to the execution character set.  */
2846   parser->translate_strings_p = true;
2847
2848   /* We are not parsing a function body.  */
2849   parser->in_function_body = false;
2850
2851   /* The unparsed function queue is empty.  */
2852   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2853
2854   /* There are no classes being defined.  */
2855   parser->num_classes_being_defined = 0;
2856
2857   /* No template parameters apply.  */
2858   parser->num_template_parameter_lists = 0;
2859
2860   return parser;
2861 }
2862
2863 /* Create a cp_lexer structure which will emit the tokens in CACHE
2864    and push it onto the parser's lexer stack.  This is used for delayed
2865    parsing of in-class method bodies and default arguments, and should
2866    not be confused with tentative parsing.  */
2867 static void
2868 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2869 {
2870   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2871   lexer->next = parser->lexer;
2872   parser->lexer = lexer;
2873
2874   /* Move the current source position to that of the first token in the
2875      new lexer.  */
2876   cp_lexer_set_source_position_from_token (lexer->next_token);
2877 }
2878
2879 /* Pop the top lexer off the parser stack.  This is never used for the
2880    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2881 static void
2882 cp_parser_pop_lexer (cp_parser *parser)
2883 {
2884   cp_lexer *lexer = parser->lexer;
2885   parser->lexer = lexer->next;
2886   cp_lexer_destroy (lexer);
2887
2888   /* Put the current source position back where it was before this
2889      lexer was pushed.  */
2890   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2891 }
2892
2893 /* Lexical conventions [gram.lex]  */
2894
2895 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2896    identifier.  */
2897
2898 static tree
2899 cp_parser_identifier (cp_parser* parser)
2900 {
2901   cp_token *token;
2902
2903   /* Look for the identifier.  */
2904   token = cp_parser_require (parser, CPP_NAME, "identifier");
2905   /* Return the value.  */
2906   return token ? token->u.value : error_mark_node;
2907 }
2908
2909 /* Parse a sequence of adjacent string constants.  Returns a
2910    TREE_STRING representing the combined, nul-terminated string
2911    constant.  If TRANSLATE is true, translate the string to the
2912    execution character set.  If WIDE_OK is true, a wide string is
2913    invalid here.
2914
2915    C++98 [lex.string] says that if a narrow string literal token is
2916    adjacent to a wide string literal token, the behavior is undefined.
2917    However, C99 6.4.5p4 says that this results in a wide string literal.
2918    We follow C99 here, for consistency with the C front end.
2919
2920    This code is largely lifted from lex_string() in c-lex.c.
2921
2922    FUTURE: ObjC++ will need to handle @-strings here.  */
2923 static tree
2924 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2925 {
2926   tree value;
2927   size_t count;
2928   struct obstack str_ob;
2929   cpp_string str, istr, *strs;
2930   cp_token *tok;
2931   enum cpp_ttype type;
2932
2933   tok = cp_lexer_peek_token (parser->lexer);
2934   if (!cp_parser_is_string_literal (tok))
2935     {
2936       cp_parser_error (parser, "expected string-literal");
2937       return error_mark_node;
2938     }
2939
2940   type = tok->type;
2941
2942   /* Try to avoid the overhead of creating and destroying an obstack
2943      for the common case of just one string.  */
2944   if (!cp_parser_is_string_literal
2945       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2946     {
2947       cp_lexer_consume_token (parser->lexer);
2948
2949       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2950       str.len = TREE_STRING_LENGTH (tok->u.value);
2951       count = 1;
2952
2953       strs = &str;
2954     }
2955   else
2956     {
2957       gcc_obstack_init (&str_ob);
2958       count = 0;
2959
2960       do
2961         {
2962           cp_lexer_consume_token (parser->lexer);
2963           count++;
2964           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2965           str.len = TREE_STRING_LENGTH (tok->u.value);
2966
2967           if (type != tok->type)
2968             {
2969               if (type == CPP_STRING)
2970                 type = tok->type;
2971               else if (tok->type != CPP_STRING)
2972                 error_at (tok->location,
2973                           "unsupported non-standard concatenation "
2974                           "of string literals");
2975             }
2976
2977           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2978
2979           tok = cp_lexer_peek_token (parser->lexer);
2980         }
2981       while (cp_parser_is_string_literal (tok));
2982
2983       strs = (cpp_string *) obstack_finish (&str_ob);
2984     }
2985
2986   if (type != CPP_STRING && !wide_ok)
2987     {
2988       cp_parser_error (parser, "a wide string is invalid in this context");
2989       type = CPP_STRING;
2990     }
2991
2992   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2993       (parse_in, strs, count, &istr, type))
2994     {
2995       value = build_string (istr.len, (const char *)istr.text);
2996       free (CONST_CAST (unsigned char *, istr.text));
2997
2998       switch (type)
2999         {
3000         default:
3001         case CPP_STRING:
3002           TREE_TYPE (value) = char_array_type_node;
3003           break;
3004         case CPP_STRING16:
3005           TREE_TYPE (value) = char16_array_type_node;
3006           break;
3007         case CPP_STRING32:
3008           TREE_TYPE (value) = char32_array_type_node;
3009           break;
3010         case CPP_WSTRING:
3011           TREE_TYPE (value) = wchar_array_type_node;
3012           break;
3013         }
3014
3015       value = fix_string_type (value);
3016     }
3017   else
3018     /* cpp_interpret_string has issued an error.  */
3019     value = error_mark_node;
3020
3021   if (count > 1)
3022     obstack_free (&str_ob, 0);
3023
3024   return value;
3025 }
3026
3027
3028 /* Basic concepts [gram.basic]  */
3029
3030 /* Parse a translation-unit.
3031
3032    translation-unit:
3033      declaration-seq [opt]
3034
3035    Returns TRUE if all went well.  */
3036
3037 static bool
3038 cp_parser_translation_unit (cp_parser* parser)
3039 {
3040   /* The address of the first non-permanent object on the declarator
3041      obstack.  */
3042   static void *declarator_obstack_base;
3043
3044   bool success;
3045
3046   /* Create the declarator obstack, if necessary.  */
3047   if (!cp_error_declarator)
3048     {
3049       gcc_obstack_init (&declarator_obstack);
3050       /* Create the error declarator.  */
3051       cp_error_declarator = make_declarator (cdk_error);
3052       /* Create the empty parameter list.  */
3053       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3054       /* Remember where the base of the declarator obstack lies.  */
3055       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3056     }
3057
3058   cp_parser_declaration_seq_opt (parser);
3059
3060   /* If there are no tokens left then all went well.  */
3061   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3062     {
3063       /* Get rid of the token array; we don't need it any more.  */
3064       cp_lexer_destroy (parser->lexer);
3065       parser->lexer = NULL;
3066
3067       /* This file might have been a context that's implicitly extern
3068          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3069       if (parser->implicit_extern_c)
3070         {
3071           pop_lang_context ();
3072           parser->implicit_extern_c = false;
3073         }
3074
3075       /* Finish up.  */
3076       finish_translation_unit ();
3077
3078       success = true;
3079     }
3080   else
3081     {
3082       cp_parser_error (parser, "expected declaration");
3083       success = false;
3084     }
3085
3086   /* Make sure the declarator obstack was fully cleaned up.  */
3087   gcc_assert (obstack_next_free (&declarator_obstack)
3088               == declarator_obstack_base);
3089
3090   /* All went well.  */
3091   return success;
3092 }
3093
3094 /* Expressions [gram.expr] */
3095
3096 /* Parse a primary-expression.
3097
3098    primary-expression:
3099      literal
3100      this
3101      ( expression )
3102      id-expression
3103
3104    GNU Extensions:
3105
3106    primary-expression:
3107      ( compound-statement )
3108      __builtin_va_arg ( assignment-expression , type-id )
3109      __builtin_offsetof ( type-id , offsetof-expression )
3110
3111    C++ Extensions:
3112      __has_nothrow_assign ( type-id )   
3113      __has_nothrow_constructor ( type-id )
3114      __has_nothrow_copy ( type-id )
3115      __has_trivial_assign ( type-id )   
3116      __has_trivial_constructor ( type-id )
3117      __has_trivial_copy ( type-id )
3118      __has_trivial_destructor ( type-id )
3119      __has_virtual_destructor ( type-id )     
3120      __is_abstract ( type-id )
3121      __is_base_of ( type-id , type-id )
3122      __is_class ( type-id )
3123      __is_convertible_to ( type-id , type-id )     
3124      __is_empty ( type-id )
3125      __is_enum ( type-id )
3126      __is_pod ( type-id )
3127      __is_polymorphic ( type-id )
3128      __is_union ( type-id )
3129
3130    Objective-C++ Extension:
3131
3132    primary-expression:
3133      objc-expression
3134
3135    literal:
3136      __null
3137
3138    ADDRESS_P is true iff this expression was immediately preceded by
3139    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3140    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3141    true iff this expression is a template argument.
3142
3143    Returns a representation of the expression.  Upon return, *IDK
3144    indicates what kind of id-expression (if any) was present.  */
3145
3146 static tree
3147 cp_parser_primary_expression (cp_parser *parser,
3148                               bool address_p,
3149                               bool cast_p,
3150                               bool template_arg_p,
3151                               cp_id_kind *idk)
3152 {
3153   cp_token *token = NULL;
3154
3155   /* Assume the primary expression is not an id-expression.  */
3156   *idk = CP_ID_KIND_NONE;
3157
3158   /* Peek at the next token.  */
3159   token = cp_lexer_peek_token (parser->lexer);
3160   switch (token->type)
3161     {
3162       /* literal:
3163            integer-literal
3164            character-literal
3165            floating-literal
3166            string-literal
3167            boolean-literal  */
3168     case CPP_CHAR:
3169     case CPP_CHAR16:
3170     case CPP_CHAR32:
3171     case CPP_WCHAR:
3172     case CPP_NUMBER:
3173       token = cp_lexer_consume_token (parser->lexer);
3174       if (TREE_CODE (token->u.value) == FIXED_CST)
3175         {
3176           error_at (token->location,
3177                     "fixed-point types not supported in C++");
3178           return error_mark_node;
3179         }
3180       /* Floating-point literals are only allowed in an integral
3181          constant expression if they are cast to an integral or
3182          enumeration type.  */
3183       if (TREE_CODE (token->u.value) == REAL_CST
3184           && parser->integral_constant_expression_p
3185           && pedantic)
3186         {
3187           /* CAST_P will be set even in invalid code like "int(2.7 +
3188              ...)".   Therefore, we have to check that the next token
3189              is sure to end the cast.  */
3190           if (cast_p)
3191             {
3192               cp_token *next_token;
3193
3194               next_token = cp_lexer_peek_token (parser->lexer);
3195               if (/* The comma at the end of an
3196                      enumerator-definition.  */
3197                   next_token->type != CPP_COMMA
3198                   /* The curly brace at the end of an enum-specifier.  */
3199                   && next_token->type != CPP_CLOSE_BRACE
3200                   /* The end of a statement.  */
3201                   && next_token->type != CPP_SEMICOLON
3202                   /* The end of the cast-expression.  */
3203                   && next_token->type != CPP_CLOSE_PAREN
3204                   /* The end of an array bound.  */
3205                   && next_token->type != CPP_CLOSE_SQUARE
3206                   /* The closing ">" in a template-argument-list.  */
3207                   && (next_token->type != CPP_GREATER
3208                       || parser->greater_than_is_operator_p)
3209                   /* C++0x only: A ">>" treated like two ">" tokens,
3210                      in a template-argument-list.  */
3211                   && (next_token->type != CPP_RSHIFT
3212                       || (cxx_dialect == cxx98)
3213                       || parser->greater_than_is_operator_p))
3214                 cast_p = false;
3215             }
3216
3217           /* If we are within a cast, then the constraint that the
3218              cast is to an integral or enumeration type will be
3219              checked at that point.  If we are not within a cast, then
3220              this code is invalid.  */
3221           if (!cast_p)
3222             cp_parser_non_integral_constant_expression
3223               (parser, "floating-point literal");
3224         }
3225       return token->u.value;
3226
3227     case CPP_STRING:
3228     case CPP_STRING16:
3229     case CPP_STRING32:
3230     case CPP_WSTRING:
3231       /* ??? Should wide strings be allowed when parser->translate_strings_p
3232          is false (i.e. in attributes)?  If not, we can kill the third
3233          argument to cp_parser_string_literal.  */
3234       return cp_parser_string_literal (parser,
3235                                        parser->translate_strings_p,
3236                                        true);
3237
3238     case CPP_OPEN_PAREN:
3239       {
3240         tree expr;
3241         bool saved_greater_than_is_operator_p;
3242
3243         /* Consume the `('.  */
3244         cp_lexer_consume_token (parser->lexer);
3245         /* Within a parenthesized expression, a `>' token is always
3246            the greater-than operator.  */
3247         saved_greater_than_is_operator_p
3248           = parser->greater_than_is_operator_p;
3249         parser->greater_than_is_operator_p = true;
3250         /* If we see `( { ' then we are looking at the beginning of
3251            a GNU statement-expression.  */
3252         if (cp_parser_allow_gnu_extensions_p (parser)
3253             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3254           {
3255             /* Statement-expressions are not allowed by the standard.  */
3256             pedwarn (token->location, OPT_pedantic, 
3257                      "ISO C++ forbids braced-groups within expressions");
3258
3259             /* And they're not allowed outside of a function-body; you
3260                cannot, for example, write:
3261
3262                  int i = ({ int j = 3; j + 1; });
3263
3264                at class or namespace scope.  */
3265             if (!parser->in_function_body
3266                 || parser->in_template_argument_list_p)
3267               {
3268                 error_at (token->location,
3269                           "statement-expressions are not allowed outside "
3270                           "functions nor in template-argument lists");
3271                 cp_parser_skip_to_end_of_block_or_statement (parser);
3272                 expr = error_mark_node;
3273               }
3274             else
3275               {
3276                 /* Start the statement-expression.  */
3277                 expr = begin_stmt_expr ();
3278                 /* Parse the compound-statement.  */
3279                 cp_parser_compound_statement (parser, expr, false);
3280                 /* Finish up.  */
3281                 expr = finish_stmt_expr (expr, false);
3282               }
3283           }
3284         else
3285           {
3286             /* Parse the parenthesized expression.  */
3287             expr = cp_parser_expression (parser, cast_p, idk);
3288             /* Let the front end know that this expression was
3289                enclosed in parentheses. This matters in case, for
3290                example, the expression is of the form `A::B', since
3291                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3292                not.  */
3293             finish_parenthesized_expr (expr);
3294           }
3295         /* The `>' token might be the end of a template-id or
3296            template-parameter-list now.  */
3297         parser->greater_than_is_operator_p
3298           = saved_greater_than_is_operator_p;
3299         /* Consume the `)'.  */
3300         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3301           cp_parser_skip_to_end_of_statement (parser);
3302
3303         return expr;
3304       }
3305
3306     case CPP_OPEN_SQUARE:
3307       if (c_dialect_objc ())
3308         /* We have an Objective-C++ message. */
3309         return cp_parser_objc_expression (parser);
3310       maybe_warn_cpp0x ("lambda expressions");
3311       return cp_parser_lambda_expression (parser);
3312
3313     case CPP_OBJC_STRING:
3314       if (c_dialect_objc ())
3315         /* We have an Objective-C++ string literal. */
3316         return cp_parser_objc_expression (parser);
3317       cp_parser_error (parser, "expected primary-expression");
3318       return error_mark_node;
3319
3320     case CPP_KEYWORD:
3321       switch (token->keyword)
3322         {
3323           /* These two are the boolean literals.  */
3324         case RID_TRUE:
3325           cp_lexer_consume_token (parser->lexer);
3326           return boolean_true_node;
3327         case RID_FALSE:
3328           cp_lexer_consume_token (parser->lexer);
3329           return boolean_false_node;
3330
3331           /* The `__null' literal.  */
3332         case RID_NULL:
3333           cp_lexer_consume_token (parser->lexer);
3334           return null_node;
3335
3336           /* Recognize the `this' keyword.  */
3337         case RID_THIS:
3338           cp_lexer_consume_token (parser->lexer);
3339           if (parser->local_variables_forbidden_p)
3340             {
3341               error_at (token->location,
3342                         "%<this%> may not be used in this context");
3343               return error_mark_node;
3344             }
3345           /* Pointers cannot appear in constant-expressions.  */
3346           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3347             return error_mark_node;
3348           return finish_this_expr ();
3349
3350           /* The `operator' keyword can be the beginning of an
3351              id-expression.  */
3352         case RID_OPERATOR:
3353           goto id_expression;
3354
3355         case RID_FUNCTION_NAME:
3356         case RID_PRETTY_FUNCTION_NAME:
3357         case RID_C99_FUNCTION_NAME:
3358           {
3359             const char *name;
3360
3361             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3362                __func__ are the names of variables -- but they are
3363                treated specially.  Therefore, they are handled here,
3364                rather than relying on the generic id-expression logic
3365                below.  Grammatically, these names are id-expressions.
3366
3367                Consume the token.  */
3368             token = cp_lexer_consume_token (parser->lexer);
3369
3370             switch (token->keyword)
3371               {
3372               case RID_FUNCTION_NAME:
3373                 name = "%<__FUNCTION__%>";
3374                 break;
3375               case RID_PRETTY_FUNCTION_NAME:
3376                 name = "%<__PRETTY_FUNCTION__%>";
3377                 break;
3378               case RID_C99_FUNCTION_NAME:
3379                 name = "%<__func__%>";
3380                 break;
3381               default:
3382                 gcc_unreachable ();
3383               }
3384
3385             if (cp_parser_non_integral_constant_expression (parser, name))
3386               return error_mark_node;
3387
3388             /* Look up the name.  */
3389             return finish_fname (token->u.value);
3390           }
3391
3392         case RID_VA_ARG:
3393           {
3394             tree expression;
3395             tree type;
3396
3397             /* The `__builtin_va_arg' construct is used to handle
3398                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3399             cp_lexer_consume_token (parser->lexer);
3400             /* Look for the opening `('.  */
3401             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3402             /* Now, parse the assignment-expression.  */
3403             expression = cp_parser_assignment_expression (parser,
3404                                                           /*cast_p=*/false, NULL);
3405             /* Look for the `,'.  */
3406             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3407             /* Parse the type-id.  */
3408             type = cp_parser_type_id (parser);
3409             /* Look for the closing `)'.  */
3410             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3411             /* Using `va_arg' in a constant-expression is not
3412                allowed.  */
3413             if (cp_parser_non_integral_constant_expression (parser,
3414                                                             "%<va_arg%>"))
3415               return error_mark_node;
3416             return build_x_va_arg (expression, type);
3417           }
3418
3419         case RID_OFFSETOF:
3420           return cp_parser_builtin_offsetof (parser);
3421
3422         case RID_HAS_NOTHROW_ASSIGN:
3423         case RID_HAS_NOTHROW_CONSTRUCTOR:
3424         case RID_HAS_NOTHROW_COPY:        
3425         case RID_HAS_TRIVIAL_ASSIGN:
3426         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3427         case RID_HAS_TRIVIAL_COPY:        
3428         case RID_HAS_TRIVIAL_DESTRUCTOR:
3429         case RID_HAS_VIRTUAL_DESTRUCTOR:
3430         case RID_IS_ABSTRACT:
3431         case RID_IS_BASE_OF:
3432         case RID_IS_CLASS:
3433         case RID_IS_CONVERTIBLE_TO:
3434         case RID_IS_EMPTY:
3435         case RID_IS_ENUM:
3436         case RID_IS_POD:
3437         case RID_IS_POLYMORPHIC:
3438         case RID_IS_STD_LAYOUT:
3439         case RID_IS_TRIVIAL:
3440         case RID_IS_UNION:
3441           return cp_parser_trait_expr (parser, token->keyword);
3442
3443         /* Objective-C++ expressions.  */
3444         case RID_AT_ENCODE:
3445         case RID_AT_PROTOCOL:
3446         case RID_AT_SELECTOR:
3447           return cp_parser_objc_expression (parser);
3448
3449         default:
3450           cp_parser_error (parser, "expected primary-expression");
3451           return error_mark_node;
3452         }
3453
3454       /* An id-expression can start with either an identifier, a
3455          `::' as the beginning of a qualified-id, or the "operator"
3456          keyword.  */
3457     case CPP_NAME:
3458     case CPP_SCOPE:
3459     case CPP_TEMPLATE_ID:
3460     case CPP_NESTED_NAME_SPECIFIER:
3461       {
3462         tree id_expression;
3463         tree decl;
3464         const char *error_msg;
3465         bool template_p;
3466         bool done;
3467         cp_token *id_expr_token;
3468
3469       id_expression:
3470         /* Parse the id-expression.  */
3471         id_expression
3472           = cp_parser_id_expression (parser,
3473                                      /*template_keyword_p=*/false,
3474                                      /*check_dependency_p=*/true,
3475                                      &template_p,
3476                                      /*declarator_p=*/false,
3477                                      /*optional_p=*/false);
3478         if (id_expression == error_mark_node)
3479           return error_mark_node;
3480         id_expr_token = token;
3481         token = cp_lexer_peek_token (parser->lexer);
3482         done = (token->type != CPP_OPEN_SQUARE
3483                 && token->type != CPP_OPEN_PAREN
3484                 && token->type != CPP_DOT
3485                 && token->type != CPP_DEREF
3486                 && token->type != CPP_PLUS_PLUS
3487                 && token->type != CPP_MINUS_MINUS);
3488         /* If we have a template-id, then no further lookup is
3489            required.  If the template-id was for a template-class, we
3490            will sometimes have a TYPE_DECL at this point.  */
3491         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3492                  || TREE_CODE (id_expression) == TYPE_DECL)
3493           decl = id_expression;
3494         /* Look up the name.  */
3495         else
3496           {
3497             tree ambiguous_decls;
3498
3499             decl = cp_parser_lookup_name (parser, id_expression,
3500                                           none_type,
3501                                           template_p,
3502                                           /*is_namespace=*/false,
3503                                           /*check_dependency=*/true,
3504                                           &ambiguous_decls,
3505                                           id_expr_token->location);
3506             /* If the lookup was ambiguous, an error will already have
3507                been issued.  */
3508             if (ambiguous_decls)
3509               return error_mark_node;
3510
3511             /* In Objective-C++, an instance variable (ivar) may be preferred
3512                to whatever cp_parser_lookup_name() found.  */
3513             decl = objc_lookup_ivar (decl, id_expression);
3514
3515             /* If name lookup gives us a SCOPE_REF, then the
3516                qualifying scope was dependent.  */
3517             if (TREE_CODE (decl) == SCOPE_REF)
3518               {
3519                 /* At this point, we do not know if DECL is a valid
3520                    integral constant expression.  We assume that it is
3521                    in fact such an expression, so that code like:
3522
3523                       template <int N> struct A {
3524                         int a[B<N>::i];
3525                       };
3526                      
3527                    is accepted.  At template-instantiation time, we
3528                    will check that B<N>::i is actually a constant.  */
3529                 return decl;
3530               }
3531             /* Check to see if DECL is a local variable in a context
3532                where that is forbidden.  */
3533             if (parser->local_variables_forbidden_p
3534                 && local_variable_p (decl))
3535               {
3536                 /* It might be that we only found DECL because we are
3537                    trying to be generous with pre-ISO scoping rules.
3538                    For example, consider:
3539
3540                      int i;
3541                      void g() {
3542                        for (int i = 0; i < 10; ++i) {}
3543                        extern void f(int j = i);
3544                      }
3545
3546                    Here, name look up will originally find the out
3547                    of scope `i'.  We need to issue a warning message,
3548                    but then use the global `i'.  */
3549                 decl = check_for_out_of_scope_variable (decl);
3550                 if (local_variable_p (decl))
3551                   {
3552                     error_at (id_expr_token->location,
3553                               "local variable %qD may not appear in this context",
3554                               decl);
3555                     return error_mark_node;
3556                   }
3557               }
3558           }
3559
3560         decl = (finish_id_expression
3561                 (id_expression, decl, parser->scope,
3562                  idk,
3563                  parser->integral_constant_expression_p,
3564                  parser->allow_non_integral_constant_expression_p,
3565                  &parser->non_integral_constant_expression_p,
3566                  template_p, done, address_p,
3567                  template_arg_p,
3568                  &error_msg,
3569                  id_expr_token->location));
3570         if (error_msg)
3571           cp_parser_error (parser, error_msg);
3572         return decl;
3573       }
3574
3575       /* Anything else is an error.  */
3576     default:
3577       cp_parser_error (parser, "expected primary-expression");
3578       return error_mark_node;
3579     }
3580 }
3581
3582 /* Parse an id-expression.
3583
3584    id-expression:
3585      unqualified-id
3586      qualified-id
3587
3588    qualified-id:
3589      :: [opt] nested-name-specifier template [opt] unqualified-id
3590      :: identifier
3591      :: operator-function-id
3592      :: template-id
3593
3594    Return a representation of the unqualified portion of the
3595    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3596    a `::' or nested-name-specifier.
3597
3598    Often, if the id-expression was a qualified-id, the caller will
3599    want to make a SCOPE_REF to represent the qualified-id.  This
3600    function does not do this in order to avoid wastefully creating
3601    SCOPE_REFs when they are not required.
3602
3603    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3604    `template' keyword.
3605
3606    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3607    uninstantiated templates.
3608
3609    If *TEMPLATE_P is non-NULL, it is set to true iff the
3610    `template' keyword is used to explicitly indicate that the entity
3611    named is a template.
3612
3613    If DECLARATOR_P is true, the id-expression is appearing as part of
3614    a declarator, rather than as part of an expression.  */
3615
3616 static tree
3617 cp_parser_id_expression (cp_parser *parser,
3618                          bool template_keyword_p,
3619                          bool check_dependency_p,
3620                          bool *template_p,
3621                          bool declarator_p,
3622                          bool optional_p)
3623 {
3624   bool global_scope_p;
3625   bool nested_name_specifier_p;
3626
3627   /* Assume the `template' keyword was not used.  */
3628   if (template_p)
3629     *template_p = template_keyword_p;
3630
3631   /* Look for the optional `::' operator.  */
3632   global_scope_p
3633     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3634        != NULL_TREE);
3635   /* Look for the optional nested-name-specifier.  */
3636   nested_name_specifier_p
3637     = (cp_parser_nested_name_specifier_opt (parser,
3638                                             /*typename_keyword_p=*/false,
3639                                             check_dependency_p,
3640                                             /*type_p=*/false,
3641                                             declarator_p)
3642        != NULL_TREE);
3643   /* If there is a nested-name-specifier, then we are looking at
3644      the first qualified-id production.  */
3645   if (nested_name_specifier_p)
3646     {
3647       tree saved_scope;
3648       tree saved_object_scope;
3649       tree saved_qualifying_scope;
3650       tree unqualified_id;
3651       bool is_template;
3652
3653       /* See if the next token is the `template' keyword.  */
3654       if (!template_p)
3655         template_p = &is_template;
3656       *template_p = cp_parser_optional_template_keyword (parser);
3657       /* Name lookup we do during the processing of the
3658          unqualified-id might obliterate SCOPE.  */
3659       saved_scope = parser->scope;
3660       saved_object_scope = parser->object_scope;
3661       saved_qualifying_scope = parser->qualifying_scope;
3662       /* Process the final unqualified-id.  */
3663       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3664                                                  check_dependency_p,
3665                                                  declarator_p,
3666                                                  /*optional_p=*/false);
3667       /* Restore the SAVED_SCOPE for our caller.  */
3668       parser->scope = saved_scope;
3669       parser->object_scope = saved_object_scope;
3670       parser->qualifying_scope = saved_qualifying_scope;
3671
3672       return unqualified_id;
3673     }
3674   /* Otherwise, if we are in global scope, then we are looking at one
3675      of the other qualified-id productions.  */
3676   else if (global_scope_p)
3677     {
3678       cp_token *token;
3679       tree id;
3680
3681       /* Peek at the next token.  */
3682       token = cp_lexer_peek_token (parser->lexer);
3683
3684       /* If it's an identifier, and the next token is not a "<", then
3685          we can avoid the template-id case.  This is an optimization
3686          for this common case.  */
3687       if (token->type == CPP_NAME
3688           && !cp_parser_nth_token_starts_template_argument_list_p
3689                (parser, 2))
3690         return cp_parser_identifier (parser);
3691
3692       cp_parser_parse_tentatively (parser);
3693       /* Try a template-id.  */
3694       id = cp_parser_template_id (parser,
3695                                   /*template_keyword_p=*/false,
3696                                   /*check_dependency_p=*/true,
3697                                   declarator_p);
3698       /* If that worked, we're done.  */
3699       if (cp_parser_parse_definitely (parser))
3700         return id;
3701
3702       /* Peek at the next token.  (Changes in the token buffer may
3703          have invalidated the pointer obtained above.)  */
3704       token = cp_lexer_peek_token (parser->lexer);
3705
3706       switch (token->type)
3707         {
3708         case CPP_NAME:
3709           return cp_parser_identifier (parser);
3710
3711         case CPP_KEYWORD:
3712           if (token->keyword == RID_OPERATOR)
3713             return cp_parser_operator_function_id (parser);
3714           /* Fall through.  */
3715
3716         default:
3717           cp_parser_error (parser, "expected id-expression");
3718           return error_mark_node;
3719         }
3720     }
3721   else
3722     return cp_parser_unqualified_id (parser, template_keyword_p,
3723                                      /*check_dependency_p=*/true,
3724                                      declarator_p,
3725                                      optional_p);
3726 }
3727
3728 /* Parse an unqualified-id.
3729
3730    unqualified-id:
3731      identifier
3732      operator-function-id
3733      conversion-function-id
3734      ~ class-name
3735      template-id
3736
3737    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3738    keyword, in a construct like `A::template ...'.
3739
3740    Returns a representation of unqualified-id.  For the `identifier'
3741    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3742    production a BIT_NOT_EXPR is returned; the operand of the
3743    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3744    other productions, see the documentation accompanying the
3745    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3746    names are looked up in uninstantiated templates.  If DECLARATOR_P
3747    is true, the unqualified-id is appearing as part of a declarator,
3748    rather than as part of an expression.  */
3749
3750 static tree
3751 cp_parser_unqualified_id (cp_parser* parser,
3752                           bool template_keyword_p,
3753                           bool check_dependency_p,
3754                           bool declarator_p,
3755                           bool optional_p)
3756 {
3757   cp_token *token;
3758
3759   /* Peek at the next token.  */
3760   token = cp_lexer_peek_token (parser->lexer);
3761
3762   switch (token->type)
3763     {
3764     case CPP_NAME:
3765       {
3766         tree id;
3767
3768         /* We don't know yet whether or not this will be a
3769            template-id.  */
3770         cp_parser_parse_tentatively (parser);
3771         /* Try a template-id.  */
3772         id = cp_parser_template_id (parser, template_keyword_p,
3773                                     check_dependency_p,
3774                                     declarator_p);
3775         /* If it worked, we're done.  */
3776         if (cp_parser_parse_definitely (parser))
3777           return id;
3778         /* Otherwise, it's an ordinary identifier.  */
3779         return cp_parser_identifier (parser);
3780       }
3781
3782     case CPP_TEMPLATE_ID:
3783       return cp_parser_template_id (parser, template_keyword_p,
3784                                     check_dependency_p,
3785                                     declarator_p);
3786
3787     case CPP_COMPL:
3788       {
3789         tree type_decl;
3790         tree qualifying_scope;
3791         tree object_scope;
3792         tree scope;
3793         bool done;
3794
3795         /* Consume the `~' token.  */
3796         cp_lexer_consume_token (parser->lexer);
3797         /* Parse the class-name.  The standard, as written, seems to
3798            say that:
3799
3800              template <typename T> struct S { ~S (); };
3801              template <typename T> S<T>::~S() {}
3802
3803            is invalid, since `~' must be followed by a class-name, but
3804            `S<T>' is dependent, and so not known to be a class.
3805            That's not right; we need to look in uninstantiated
3806            templates.  A further complication arises from:
3807
3808              template <typename T> void f(T t) {
3809                t.T::~T();
3810              }
3811
3812            Here, it is not possible to look up `T' in the scope of `T'
3813            itself.  We must look in both the current scope, and the
3814            scope of the containing complete expression.
3815
3816            Yet another issue is:
3817
3818              struct S {
3819                int S;
3820                ~S();
3821              };
3822
3823              S::~S() {}
3824
3825            The standard does not seem to say that the `S' in `~S'
3826            should refer to the type `S' and not the data member
3827            `S::S'.  */
3828
3829         /* DR 244 says that we look up the name after the "~" in the
3830            same scope as we looked up the qualifying name.  That idea
3831            isn't fully worked out; it's more complicated than that.  */
3832         scope = parser->scope;
3833         object_scope = parser->object_scope;
3834         qualifying_scope = parser->qualifying_scope;
3835
3836         /* Check for invalid scopes.  */
3837         if (scope == error_mark_node)
3838           {
3839             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3840               cp_lexer_consume_token (parser->lexer);
3841             return error_mark_node;
3842           }
3843         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3844           {
3845             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3846               error_at (token->location,
3847                         "scope %qT before %<~%> is not a class-name",
3848                         scope);
3849             cp_parser_simulate_error (parser);
3850             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3851               cp_lexer_consume_token (parser->lexer);
3852             return error_mark_node;
3853           }
3854         gcc_assert (!scope || TYPE_P (scope));
3855
3856         /* If the name is of the form "X::~X" it's OK.  */
3857         token = cp_lexer_peek_token (parser->lexer);
3858         if (scope
3859             && token->type == CPP_NAME
3860             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3861                 == CPP_OPEN_PAREN)
3862             && constructor_name_p (token->u.value, scope))
3863           {
3864             cp_lexer_consume_token (parser->lexer);
3865             return build_nt (BIT_NOT_EXPR, scope);
3866           }
3867
3868         /* If there was an explicit qualification (S::~T), first look
3869            in the scope given by the qualification (i.e., S).  */
3870         done = false;
3871         type_decl = NULL_TREE;
3872         if (scope)
3873           {
3874             cp_parser_parse_tentatively (parser);
3875             type_decl = cp_parser_class_name (parser,
3876                                               /*typename_keyword_p=*/false,
3877                                               /*template_keyword_p=*/false,
3878                                               none_type,
3879                                               /*check_dependency=*/false,
3880                                               /*class_head_p=*/false,
3881                                               declarator_p);
3882             if (cp_parser_parse_definitely (parser))
3883               done = true;
3884           }
3885         /* In "N::S::~S", look in "N" as well.  */
3886         if (!done && scope && qualifying_scope)
3887           {
3888             cp_parser_parse_tentatively (parser);
3889             parser->scope = qualifying_scope;
3890             parser->object_scope = NULL_TREE;
3891             parser->qualifying_scope = NULL_TREE;
3892             type_decl
3893               = cp_parser_class_name (parser,
3894                                       /*typename_keyword_p=*/false,
3895                                       /*template_keyword_p=*/false,
3896                                       none_type,
3897                                       /*check_dependency=*/false,
3898                                       /*class_head_p=*/false,
3899                                       declarator_p);
3900             if (cp_parser_parse_definitely (parser))
3901               done = true;
3902           }
3903         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3904         else if (!done && object_scope)
3905           {
3906             cp_parser_parse_tentatively (parser);
3907             parser->scope = object_scope;
3908             parser->object_scope = NULL_TREE;
3909             parser->qualifying_scope = NULL_TREE;
3910             type_decl
3911               = cp_parser_class_name (parser,
3912                                       /*typename_keyword_p=*/false,
3913                                       /*template_keyword_p=*/false,
3914                                       none_type,
3915                                       /*check_dependency=*/false,
3916                                       /*class_head_p=*/false,
3917                                       declarator_p);
3918             if (cp_parser_parse_definitely (parser))
3919               done = true;
3920           }
3921         /* Look in the surrounding context.  */
3922         if (!done)
3923           {
3924             parser->scope = NULL_TREE;
3925             parser->object_scope = NULL_TREE;
3926             parser->qualifying_scope = NULL_TREE;
3927             if (processing_template_decl)
3928               cp_parser_parse_tentatively (parser);
3929             type_decl
3930               = cp_parser_class_name (parser,
3931                                       /*typename_keyword_p=*/false,
3932                                       /*template_keyword_p=*/false,
3933                                       none_type,
3934                                       /*check_dependency=*/false,
3935                                       /*class_head_p=*/false,
3936                                       declarator_p);
3937             if (processing_template_decl
3938                 && ! cp_parser_parse_definitely (parser))
3939               {
3940                 /* We couldn't find a type with this name, so just accept
3941                    it and check for a match at instantiation time.  */
3942                 type_decl = cp_parser_identifier (parser);
3943                 if (type_decl != error_mark_node)
3944                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3945                 return type_decl;
3946               }
3947           }
3948         /* If an error occurred, assume that the name of the
3949            destructor is the same as the name of the qualifying
3950            class.  That allows us to keep parsing after running
3951            into ill-formed destructor names.  */
3952         if (type_decl == error_mark_node && scope)
3953           return build_nt (BIT_NOT_EXPR, scope);
3954         else if (type_decl == error_mark_node)
3955           return error_mark_node;
3956
3957         /* Check that destructor name and scope match.  */
3958         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3959           {
3960             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3961               error_at (token->location,
3962                         "declaration of %<~%T%> as member of %qT",
3963                         type_decl, scope);
3964             cp_parser_simulate_error (parser);
3965             return error_mark_node;
3966           }
3967
3968         /* [class.dtor]
3969
3970            A typedef-name that names a class shall not be used as the
3971            identifier in the declarator for a destructor declaration.  */
3972         if (declarator_p
3973             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3974             && !DECL_SELF_REFERENCE_P (type_decl)
3975             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3976           error_at (token->location,
3977                     "typedef-name %qD used as destructor declarator",
3978                     type_decl);
3979
3980         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3981       }
3982
3983     case CPP_KEYWORD:
3984       if (token->keyword == RID_OPERATOR)
3985         {
3986           tree id;
3987
3988           /* This could be a template-id, so we try that first.  */
3989           cp_parser_parse_tentatively (parser);
3990           /* Try a template-id.  */
3991           id = cp_parser_template_id (parser, template_keyword_p,
3992                                       /*check_dependency_p=*/true,
3993                                       declarator_p);
3994           /* If that worked, we're done.  */
3995           if (cp_parser_parse_definitely (parser))
3996             return id;
3997           /* We still don't know whether we're looking at an
3998              operator-function-id or a conversion-function-id.  */
3999           cp_parser_parse_tentatively (parser);
4000           /* Try an operator-function-id.  */
4001           id = cp_parser_operator_function_id (parser);
4002           /* If that didn't work, try a conversion-function-id.  */
4003           if (!cp_parser_parse_definitely (parser))
4004             id = cp_parser_conversion_function_id (parser);
4005
4006           return id;
4007         }
4008       /* Fall through.  */
4009
4010     default:
4011       if (optional_p)
4012         return NULL_TREE;
4013       cp_parser_error (parser, "expected unqualified-id");
4014       return error_mark_node;
4015     }
4016 }
4017
4018 /* Parse an (optional) nested-name-specifier.
4019
4020    nested-name-specifier: [C++98]
4021      class-or-namespace-name :: nested-name-specifier [opt]
4022      class-or-namespace-name :: template nested-name-specifier [opt]
4023
4024    nested-name-specifier: [C++0x]
4025      type-name ::
4026      namespace-name ::
4027      nested-name-specifier identifier ::
4028      nested-name-specifier template [opt] simple-template-id ::
4029
4030    PARSER->SCOPE should be set appropriately before this function is
4031    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4032    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4033    in name lookups.
4034
4035    Sets PARSER->SCOPE to the class (TYPE) or namespace
4036    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4037    it unchanged if there is no nested-name-specifier.  Returns the new
4038    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4039
4040    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4041    part of a declaration and/or decl-specifier.  */
4042
4043 static tree
4044 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4045                                      bool typename_keyword_p,
4046                                      bool check_dependency_p,
4047                                      bool type_p,
4048                                      bool is_declaration)
4049 {
4050   bool success = false;
4051   cp_token_position start = 0;
4052   cp_token *token;
4053
4054   /* Remember where the nested-name-specifier starts.  */
4055   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4056     {
4057       start = cp_lexer_token_position (parser->lexer, false);
4058       push_deferring_access_checks (dk_deferred);
4059     }
4060
4061   while (true)
4062     {
4063       tree new_scope;
4064       tree old_scope;
4065       tree saved_qualifying_scope;
4066       bool template_keyword_p;
4067
4068       /* Spot cases that cannot be the beginning of a
4069          nested-name-specifier.  */
4070       token = cp_lexer_peek_token (parser->lexer);
4071
4072       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4073          the already parsed nested-name-specifier.  */
4074       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4075         {
4076           /* Grab the nested-name-specifier and continue the loop.  */
4077           cp_parser_pre_parsed_nested_name_specifier (parser);
4078           /* If we originally encountered this nested-name-specifier
4079              with IS_DECLARATION set to false, we will not have
4080              resolved TYPENAME_TYPEs, so we must do so here.  */
4081           if (is_declaration
4082               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4083             {
4084               new_scope = resolve_typename_type (parser->scope,
4085                                                  /*only_current_p=*/false);
4086               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4087                 parser->scope = new_scope;
4088             }
4089           success = true;
4090           continue;
4091         }
4092
4093       /* Spot cases that cannot be the beginning of a
4094          nested-name-specifier.  On the second and subsequent times
4095          through the loop, we look for the `template' keyword.  */
4096       if (success && token->keyword == RID_TEMPLATE)
4097         ;
4098       /* A template-id can start a nested-name-specifier.  */
4099       else if (token->type == CPP_TEMPLATE_ID)
4100         ;
4101       else
4102         {
4103           /* If the next token is not an identifier, then it is
4104              definitely not a type-name or namespace-name.  */
4105           if (token->type != CPP_NAME)
4106             break;
4107           /* If the following token is neither a `<' (to begin a
4108              template-id), nor a `::', then we are not looking at a
4109              nested-name-specifier.  */
4110           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4111           if (token->type != CPP_SCOPE
4112               && !cp_parser_nth_token_starts_template_argument_list_p
4113                   (parser, 2))
4114             break;
4115         }
4116
4117       /* The nested-name-specifier is optional, so we parse
4118          tentatively.  */
4119       cp_parser_parse_tentatively (parser);
4120
4121       /* Look for the optional `template' keyword, if this isn't the
4122          first time through the loop.  */
4123       if (success)
4124         template_keyword_p = cp_parser_optional_template_keyword (parser);
4125       else
4126         template_keyword_p = false;
4127
4128       /* Save the old scope since the name lookup we are about to do
4129          might destroy it.  */
4130       old_scope = parser->scope;
4131       saved_qualifying_scope = parser->qualifying_scope;
4132       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4133          look up names in "X<T>::I" in order to determine that "Y" is
4134          a template.  So, if we have a typename at this point, we make
4135          an effort to look through it.  */
4136       if (is_declaration
4137           && !typename_keyword_p
4138           && parser->scope
4139           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4140         parser->scope = resolve_typename_type (parser->scope,
4141                                                /*only_current_p=*/false);
4142       /* Parse the qualifying entity.  */
4143       new_scope
4144         = cp_parser_qualifying_entity (parser,
4145                                        typename_keyword_p,
4146                                        template_keyword_p,
4147                                        check_dependency_p,
4148                                        type_p,
4149                                        is_declaration);
4150       /* Look for the `::' token.  */
4151       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4152
4153       /* If we found what we wanted, we keep going; otherwise, we're
4154          done.  */
4155       if (!cp_parser_parse_definitely (parser))
4156         {
4157           bool error_p = false;
4158
4159           /* Restore the OLD_SCOPE since it was valid before the
4160              failed attempt at finding the last
4161              class-or-namespace-name.  */
4162           parser->scope = old_scope;
4163           parser->qualifying_scope = saved_qualifying_scope;
4164           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4165             break;
4166           /* If the next token is an identifier, and the one after
4167              that is a `::', then any valid interpretation would have
4168              found a class-or-namespace-name.  */
4169           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4170                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4171                      == CPP_SCOPE)
4172                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4173                      != CPP_COMPL))
4174             {
4175               token = cp_lexer_consume_token (parser->lexer);
4176               if (!error_p)
4177                 {
4178                   if (!token->ambiguous_p)
4179                     {
4180                       tree decl;
4181                       tree ambiguous_decls;
4182
4183                       decl = cp_parser_lookup_name (parser, token->u.value,
4184                                                     none_type,
4185                                                     /*is_template=*/false,
4186                                                     /*is_namespace=*/false,
4187                                                     /*check_dependency=*/true,
4188                                                     &ambiguous_decls,
4189                                                     token->location);
4190                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4191                         error_at (token->location,
4192                                   "%qD used without template parameters",
4193                                   decl);
4194                       else if (ambiguous_decls)
4195                         {
4196                           error_at (token->location,
4197                                     "reference to %qD is ambiguous",
4198                                     token->u.value);
4199                           print_candidates (ambiguous_decls);
4200                           decl = error_mark_node;
4201                         }
4202                       else
4203                         {
4204                           const char* msg = "is not a class or namespace";
4205                           if (cxx_dialect != cxx98)
4206                             msg = "is not a class, namespace, or enumeration";
4207                           cp_parser_name_lookup_error
4208                             (parser, token->u.value, decl, msg,
4209                              token->location);
4210                         }
4211                     }
4212                   parser->scope = error_mark_node;
4213                   error_p = true;
4214                   /* Treat this as a successful nested-name-specifier
4215                      due to:
4216
4217                      [basic.lookup.qual]
4218
4219                      If the name found is not a class-name (clause
4220                      _class_) or namespace-name (_namespace.def_), the
4221                      program is ill-formed.  */
4222                   success = true;
4223                 }
4224               cp_lexer_consume_token (parser->lexer);
4225             }
4226           break;
4227         }
4228       /* We've found one valid nested-name-specifier.  */
4229       success = true;
4230       /* Name lookup always gives us a DECL.  */
4231       if (TREE_CODE (new_scope) == TYPE_DECL)
4232         new_scope = TREE_TYPE (new_scope);
4233       /* Uses of "template" must be followed by actual templates.  */
4234       if (template_keyword_p
4235           && !(CLASS_TYPE_P (new_scope)
4236                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4237                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4238                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4239           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4240                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4241                    == TEMPLATE_ID_EXPR)))
4242         permerror (input_location, TYPE_P (new_scope)
4243                    ? "%qT is not a template"
4244                    : "%qD is not a template",
4245                    new_scope);
4246       /* If it is a class scope, try to complete it; we are about to
4247          be looking up names inside the class.  */
4248       if (TYPE_P (new_scope)
4249           /* Since checking types for dependency can be expensive,
4250              avoid doing it if the type is already complete.  */
4251           && !COMPLETE_TYPE_P (new_scope)
4252           /* Do not try to complete dependent types.  */
4253           && !dependent_type_p (new_scope))
4254         {
4255           new_scope = complete_type (new_scope);
4256           /* If it is a typedef to current class, use the current
4257              class instead, as the typedef won't have any names inside
4258              it yet.  */
4259           if (!COMPLETE_TYPE_P (new_scope)
4260               && currently_open_class (new_scope))
4261             new_scope = TYPE_MAIN_VARIANT (new_scope);
4262         }
4263       /* Make sure we look in the right scope the next time through
4264          the loop.  */
4265       parser->scope = new_scope;
4266     }
4267
4268   /* If parsing tentatively, replace the sequence of tokens that makes
4269      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4270      token.  That way, should we re-parse the token stream, we will
4271      not have to repeat the effort required to do the parse, nor will
4272      we issue duplicate error messages.  */
4273   if (success && start)
4274     {
4275       cp_token *token;
4276
4277       token = cp_lexer_token_at (parser->lexer, start);
4278       /* Reset the contents of the START token.  */
4279       token->type = CPP_NESTED_NAME_SPECIFIER;
4280       /* Retrieve any deferred checks.  Do not pop this access checks yet
4281          so the memory will not be reclaimed during token replacing below.  */
4282       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4283       token->u.tree_check_value->value = parser->scope;
4284       token->u.tree_check_value->checks = get_deferred_access_checks ();
4285       token->u.tree_check_value->qualifying_scope =
4286         parser->qualifying_scope;
4287       token->keyword = RID_MAX;
4288
4289       /* Purge all subsequent tokens.  */
4290       cp_lexer_purge_tokens_after (parser->lexer, start);
4291     }
4292
4293   if (start)
4294     pop_to_parent_deferring_access_checks ();
4295
4296   return success ? parser->scope : NULL_TREE;
4297 }
4298
4299 /* Parse a nested-name-specifier.  See
4300    cp_parser_nested_name_specifier_opt for details.  This function
4301    behaves identically, except that it will an issue an error if no
4302    nested-name-specifier is present.  */
4303
4304 static tree
4305 cp_parser_nested_name_specifier (cp_parser *parser,
4306                                  bool typename_keyword_p,
4307                                  bool check_dependency_p,
4308                                  bool type_p,
4309                                  bool is_declaration)
4310 {
4311   tree scope;
4312
4313   /* Look for the nested-name-specifier.  */
4314   scope = cp_parser_nested_name_specifier_opt (parser,
4315                                                typename_keyword_p,
4316                                                check_dependency_p,
4317                                                type_p,
4318                                                is_declaration);
4319   /* If it was not present, issue an error message.  */
4320   if (!scope)
4321     {
4322       cp_parser_error (parser, "expected nested-name-specifier");
4323       parser->scope = NULL_TREE;
4324     }
4325
4326   return scope;
4327 }
4328
4329 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4330    this is either a class-name or a namespace-name (which corresponds
4331    to the class-or-namespace-name production in the grammar). For
4332    C++0x, it can also be a type-name that refers to an enumeration
4333    type.
4334
4335    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4336    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4337    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4338    TYPE_P is TRUE iff the next name should be taken as a class-name,
4339    even the same name is declared to be another entity in the same
4340    scope.
4341
4342    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4343    specified by the class-or-namespace-name.  If neither is found the
4344    ERROR_MARK_NODE is returned.  */
4345
4346 static tree
4347 cp_parser_qualifying_entity (cp_parser *parser,
4348                              bool typename_keyword_p,
4349                              bool template_keyword_p,
4350                              bool check_dependency_p,
4351                              bool type_p,
4352                              bool is_declaration)
4353 {
4354   tree saved_scope;
4355   tree saved_qualifying_scope;
4356   tree saved_object_scope;
4357   tree scope;
4358   bool only_class_p;
4359   bool successful_parse_p;
4360
4361   /* Before we try to parse the class-name, we must save away the
4362      current PARSER->SCOPE since cp_parser_class_name will destroy
4363      it.  */
4364   saved_scope = parser->scope;
4365   saved_qualifying_scope = parser->qualifying_scope;
4366   saved_object_scope = parser->object_scope;
4367   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4368      there is no need to look for a namespace-name.  */
4369   only_class_p = template_keyword_p 
4370     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4371   if (!only_class_p)
4372     cp_parser_parse_tentatively (parser);
4373   scope = cp_parser_class_name (parser,
4374                                 typename_keyword_p,
4375                                 template_keyword_p,
4376                                 type_p ? class_type : none_type,
4377                                 check_dependency_p,
4378                                 /*class_head_p=*/false,
4379                                 is_declaration);
4380   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4381   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4382   if (!only_class_p 
4383       && cxx_dialect != cxx98
4384       && !successful_parse_p)
4385     {
4386       /* Restore the saved scope.  */
4387       parser->scope = saved_scope;
4388       parser->qualifying_scope = saved_qualifying_scope;
4389       parser->object_scope = saved_object_scope;
4390
4391       /* Parse tentatively.  */
4392       cp_parser_parse_tentatively (parser);
4393      
4394       /* Parse a typedef-name or enum-name.  */
4395       scope = cp_parser_nonclass_name (parser);
4396       successful_parse_p = cp_parser_parse_definitely (parser);
4397     }
4398   /* If that didn't work, try for a namespace-name.  */
4399   if (!only_class_p && !successful_parse_p)
4400     {
4401       /* Restore the saved scope.  */
4402       parser->scope = saved_scope;
4403       parser->qualifying_scope = saved_qualifying_scope;
4404       parser->object_scope = saved_object_scope;
4405       /* If we are not looking at an identifier followed by the scope
4406          resolution operator, then this is not part of a
4407          nested-name-specifier.  (Note that this function is only used
4408          to parse the components of a nested-name-specifier.)  */
4409       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4410           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4411         return error_mark_node;
4412       scope = cp_parser_namespace_name (parser);
4413     }
4414
4415   return scope;
4416 }
4417
4418 /* Parse a postfix-expression.
4419
4420    postfix-expression:
4421      primary-expression
4422      postfix-expression [ expression ]
4423      postfix-expression ( expression-list [opt] )
4424      simple-type-specifier ( expression-list [opt] )
4425      typename :: [opt] nested-name-specifier identifier
4426        ( expression-list [opt] )
4427      typename :: [opt] nested-name-specifier template [opt] template-id
4428        ( expression-list [opt] )
4429      postfix-expression . template [opt] id-expression
4430      postfix-expression -> template [opt] id-expression
4431      postfix-expression . pseudo-destructor-name
4432      postfix-expression -> pseudo-destructor-name
4433      postfix-expression ++
4434      postfix-expression --
4435      dynamic_cast < type-id > ( expression )
4436      static_cast < type-id > ( expression )
4437      reinterpret_cast < type-id > ( expression )
4438      const_cast < type-id > ( expression )
4439      typeid ( expression )
4440      typeid ( type-id )
4441
4442    GNU Extension:
4443
4444    postfix-expression:
4445      ( type-id ) { initializer-list , [opt] }
4446
4447    This extension is a GNU version of the C99 compound-literal
4448    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4449    but they are essentially the same concept.)
4450
4451    If ADDRESS_P is true, the postfix expression is the operand of the
4452    `&' operator.  CAST_P is true if this expression is the target of a
4453    cast.
4454
4455    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4456    class member access expressions [expr.ref].
4457
4458    Returns a representation of the expression.  */
4459
4460 static tree
4461 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4462                               bool member_access_only_p,
4463                               cp_id_kind * pidk_return)
4464 {
4465   cp_token *token;
4466   enum rid keyword;
4467   cp_id_kind idk = CP_ID_KIND_NONE;
4468   tree postfix_expression = NULL_TREE;
4469   bool is_member_access = false;
4470
4471   /* Peek at the next token.  */
4472   token = cp_lexer_peek_token (parser->lexer);
4473   /* Some of the productions are determined by keywords.  */
4474   keyword = token->keyword;
4475   switch (keyword)
4476     {
4477     case RID_DYNCAST:
4478     case RID_STATCAST:
4479     case RID_REINTCAST:
4480     case RID_CONSTCAST:
4481       {
4482         tree type;
4483         tree expression;
4484         const char *saved_message;
4485
4486         /* All of these can be handled in the same way from the point
4487            of view of parsing.  Begin by consuming the token
4488            identifying the cast.  */
4489         cp_lexer_consume_token (parser->lexer);
4490
4491         /* New types cannot be defined in the cast.  */
4492         saved_message = parser->type_definition_forbidden_message;
4493         parser->type_definition_forbidden_message
4494           = "types may not be defined in casts";
4495
4496         /* Look for the opening `<'.  */
4497         cp_parser_require (parser, CPP_LESS, "%<<%>");
4498         /* Parse the type to which we are casting.  */
4499         type = cp_parser_type_id (parser);
4500         /* Look for the closing `>'.  */
4501         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4502         /* Restore the old message.  */
4503         parser->type_definition_forbidden_message = saved_message;
4504
4505         /* And the expression which is being cast.  */
4506         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4507         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4508         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4509
4510         /* Only type conversions to integral or enumeration types
4511            can be used in constant-expressions.  */
4512         if (!cast_valid_in_integral_constant_expression_p (type)
4513             && (cp_parser_non_integral_constant_expression
4514                 (parser,
4515                  "a cast to a type other than an integral or "
4516                  "enumeration type")))
4517           return error_mark_node;
4518
4519         switch (keyword)
4520           {
4521           case RID_DYNCAST:
4522             postfix_expression
4523               = build_dynamic_cast (type, expression, tf_warning_or_error);
4524             break;
4525           case RID_STATCAST:
4526             postfix_expression
4527               = build_static_cast (type, expression, tf_warning_or_error);
4528             break;
4529           case RID_REINTCAST:
4530             postfix_expression
4531               = build_reinterpret_cast (type, expression, 
4532                                         tf_warning_or_error);
4533             break;
4534           case RID_CONSTCAST:
4535             postfix_expression
4536               = build_const_cast (type, expression, tf_warning_or_error);
4537             break;
4538           default:
4539             gcc_unreachable ();
4540           }
4541       }
4542       break;
4543
4544     case RID_TYPEID:
4545       {
4546         tree type;
4547         const char *saved_message;
4548         bool saved_in_type_id_in_expr_p;
4549
4550         /* Consume the `typeid' token.  */
4551         cp_lexer_consume_token (parser->lexer);
4552         /* Look for the `(' token.  */
4553         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4554         /* Types cannot be defined in a `typeid' expression.  */
4555         saved_message = parser->type_definition_forbidden_message;
4556         parser->type_definition_forbidden_message
4557           = "types may not be defined in a %<typeid%> expression";
4558         /* We can't be sure yet whether we're looking at a type-id or an
4559            expression.  */
4560         cp_parser_parse_tentatively (parser);
4561         /* Try a type-id first.  */
4562         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4563         parser->in_type_id_in_expr_p = true;
4564         type = cp_parser_type_id (parser);
4565         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4566         /* Look for the `)' token.  Otherwise, we can't be sure that
4567            we're not looking at an expression: consider `typeid (int
4568            (3))', for example.  */
4569         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4570         /* If all went well, simply lookup the type-id.  */
4571         if (cp_parser_parse_definitely (parser))
4572           postfix_expression = get_typeid (type);
4573         /* Otherwise, fall back to the expression variant.  */
4574         else
4575           {
4576             tree expression;
4577
4578             /* Look for an expression.  */
4579             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4580             /* Compute its typeid.  */
4581             postfix_expression = build_typeid (expression);
4582             /* Look for the `)' token.  */
4583             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4584           }
4585         /* Restore the saved message.  */
4586         parser->type_definition_forbidden_message = saved_message;
4587         /* `typeid' may not appear in an integral constant expression.  */
4588         if (cp_parser_non_integral_constant_expression(parser,
4589                                                        "%<typeid%> operator"))
4590           return error_mark_node;
4591       }
4592       break;
4593
4594     case RID_TYPENAME:
4595       {
4596         tree type;
4597         /* The syntax permitted here is the same permitted for an
4598            elaborated-type-specifier.  */
4599         type = cp_parser_elaborated_type_specifier (parser,
4600                                                     /*is_friend=*/false,
4601                                                     /*is_declaration=*/false);
4602         postfix_expression = cp_parser_functional_cast (parser, type);
4603       }
4604       break;
4605
4606     default:
4607       {
4608         tree type;
4609
4610         /* If the next thing is a simple-type-specifier, we may be
4611            looking at a functional cast.  We could also be looking at
4612            an id-expression.  So, we try the functional cast, and if
4613            that doesn't work we fall back to the primary-expression.  */
4614         cp_parser_parse_tentatively (parser);
4615         /* Look for the simple-type-specifier.  */
4616         type = cp_parser_simple_type_specifier (parser,
4617                                                 /*decl_specs=*/NULL,
4618                                                 CP_PARSER_FLAGS_NONE);
4619         /* Parse the cast itself.  */
4620         if (!cp_parser_error_occurred (parser))
4621           postfix_expression
4622             = cp_parser_functional_cast (parser, type);
4623         /* If that worked, we're done.  */
4624         if (cp_parser_parse_definitely (parser))
4625           break;
4626
4627         /* If the functional-cast didn't work out, try a
4628            compound-literal.  */
4629         if (cp_parser_allow_gnu_extensions_p (parser)
4630             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4631           {
4632             VEC(constructor_elt,gc) *initializer_list = NULL;
4633             bool saved_in_type_id_in_expr_p;
4634
4635             cp_parser_parse_tentatively (parser);
4636             /* Consume the `('.  */
4637             cp_lexer_consume_token (parser->lexer);
4638             /* Parse the type.  */
4639             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4640             parser->in_type_id_in_expr_p = true;
4641             type = cp_parser_type_id (parser);
4642             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4643             /* Look for the `)'.  */
4644             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4645             /* Look for the `{'.  */
4646             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4647             /* If things aren't going well, there's no need to
4648                keep going.  */
4649             if (!cp_parser_error_occurred (parser))
4650               {
4651                 bool non_constant_p;
4652                 /* Parse the initializer-list.  */
4653                 initializer_list
4654                   = cp_parser_initializer_list (parser, &non_constant_p);
4655                 /* Allow a trailing `,'.  */
4656                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4657                   cp_lexer_consume_token (parser->lexer);
4658                 /* Look for the final `}'.  */
4659                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4660               }
4661             /* If that worked, we're definitely looking at a
4662                compound-literal expression.  */
4663             if (cp_parser_parse_definitely (parser))
4664               {
4665                 /* Warn the user that a compound literal is not
4666                    allowed in standard C++.  */
4667                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4668                 /* For simplicity, we disallow compound literals in
4669                    constant-expressions.  We could
4670                    allow compound literals of integer type, whose
4671                    initializer was a constant, in constant
4672                    expressions.  Permitting that usage, as a further
4673                    extension, would not change the meaning of any
4674                    currently accepted programs.  (Of course, as
4675                    compound literals are not part of ISO C++, the
4676                    standard has nothing to say.)  */
4677                 if (cp_parser_non_integral_constant_expression 
4678                     (parser, "non-constant compound literals"))
4679                   {
4680                     postfix_expression = error_mark_node;
4681                     break;
4682                   }
4683                 /* Form the representation of the compound-literal.  */
4684                 postfix_expression
4685                   = (finish_compound_literal
4686                      (type, build_constructor (init_list_type_node,
4687                                                initializer_list)));
4688                 break;
4689               }
4690           }
4691
4692         /* It must be a primary-expression.  */
4693         postfix_expression
4694           = cp_parser_primary_expression (parser, address_p, cast_p,
4695                                           /*template_arg_p=*/false,
4696                                           &idk);
4697       }
4698       break;
4699     }
4700
4701   /* Keep looping until the postfix-expression is complete.  */
4702   while (true)
4703     {
4704       if (idk == CP_ID_KIND_UNQUALIFIED
4705           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4706           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4707         /* It is not a Koenig lookup function call.  */
4708         postfix_expression
4709           = unqualified_name_lookup_error (postfix_expression);
4710
4711       /* Peek at the next token.  */
4712       token = cp_lexer_peek_token (parser->lexer);
4713
4714       switch (token->type)
4715         {
4716         case CPP_OPEN_SQUARE:
4717           postfix_expression
4718             = cp_parser_postfix_open_square_expression (parser,
4719                                                         postfix_expression,
4720                                                         false);
4721           idk = CP_ID_KIND_NONE;
4722           is_member_access = false;
4723           break;
4724
4725         case CPP_OPEN_PAREN:
4726           /* postfix-expression ( expression-list [opt] ) */
4727           {
4728             bool koenig_p;
4729             bool is_builtin_constant_p;
4730             bool saved_integral_constant_expression_p = false;
4731             bool saved_non_integral_constant_expression_p = false;
4732             VEC(tree,gc) *args;
4733
4734             is_member_access = false;
4735
4736             is_builtin_constant_p
4737               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4738             if (is_builtin_constant_p)
4739               {
4740                 /* The whole point of __builtin_constant_p is to allow
4741                    non-constant expressions to appear as arguments.  */
4742                 saved_integral_constant_expression_p
4743                   = parser->integral_constant_expression_p;
4744                 saved_non_integral_constant_expression_p
4745                   = parser->non_integral_constant_expression_p;
4746                 parser->integral_constant_expression_p = false;
4747               }
4748             args = (cp_parser_parenthesized_expression_list
4749                     (parser, /*is_attribute_list=*/false,
4750                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4751                      /*non_constant_p=*/NULL));
4752             if (is_builtin_constant_p)
4753               {
4754                 parser->integral_constant_expression_p
4755                   = saved_integral_constant_expression_p;
4756                 parser->non_integral_constant_expression_p
4757                   = saved_non_integral_constant_expression_p;
4758               }
4759
4760             if (args == NULL)
4761               {
4762                 postfix_expression = error_mark_node;
4763                 break;
4764               }
4765
4766             /* Function calls are not permitted in
4767                constant-expressions.  */
4768             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4769                 && cp_parser_non_integral_constant_expression (parser,
4770                                                                "a function call"))
4771               {
4772                 postfix_expression = error_mark_node;
4773                 release_tree_vector (args);
4774                 break;
4775               }
4776
4777             koenig_p = false;
4778             if (idk == CP_ID_KIND_UNQUALIFIED
4779                 || idk == CP_ID_KIND_TEMPLATE_ID)
4780               {
4781                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4782                   {
4783                     if (!VEC_empty (tree, args))
4784                       {
4785                         koenig_p = true;
4786                         if (!any_type_dependent_arguments_p (args))
4787                           postfix_expression
4788                             = perform_koenig_lookup (postfix_expression, args);
4789                       }
4790                     else
4791                       postfix_expression
4792                         = unqualified_fn_lookup_error (postfix_expression);
4793                   }
4794                 /* We do not perform argument-dependent lookup if
4795                    normal lookup finds a non-function, in accordance
4796                    with the expected resolution of DR 218.  */
4797                 else if (!VEC_empty (tree, args)
4798                          && is_overloaded_fn (postfix_expression))
4799                   {
4800                     tree fn = get_first_fn (postfix_expression);
4801
4802                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4803                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4804
4805                     /* Only do argument dependent lookup if regular
4806                        lookup does not find a set of member functions.
4807                        [basic.lookup.koenig]/2a  */
4808                     if (!DECL_FUNCTION_MEMBER_P (fn))
4809                       {
4810                         koenig_p = true;
4811                         if (!any_type_dependent_arguments_p (args))
4812                           postfix_expression
4813                             = perform_koenig_lookup (postfix_expression, args);
4814                       }
4815                   }
4816               }
4817
4818             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4819               {
4820                 tree instance = TREE_OPERAND (postfix_expression, 0);
4821                 tree fn = TREE_OPERAND (postfix_expression, 1);
4822
4823                 if (processing_template_decl
4824                     && (type_dependent_expression_p (instance)
4825                         || (!BASELINK_P (fn)
4826                             && TREE_CODE (fn) != FIELD_DECL)
4827                         || type_dependent_expression_p (fn)
4828                         || any_type_dependent_arguments_p (args)))
4829                   {
4830                     postfix_expression
4831                       = build_nt_call_vec (postfix_expression, args);
4832                     release_tree_vector (args);
4833                     break;
4834                   }
4835
4836                 if (BASELINK_P (fn))
4837                   {
4838                   postfix_expression
4839                     = (build_new_method_call
4840                        (instance, fn, &args, NULL_TREE,
4841                         (idk == CP_ID_KIND_QUALIFIED
4842                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4843                         /*fn_p=*/NULL,
4844                         tf_warning_or_error));
4845                   }
4846                 else
4847                   postfix_expression
4848                     = finish_call_expr (postfix_expression, &args,
4849                                         /*disallow_virtual=*/false,
4850                                         /*koenig_p=*/false,
4851                                         tf_warning_or_error);
4852               }
4853             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4854                      || TREE_CODE (postfix_expression) == MEMBER_REF
4855                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4856               postfix_expression = (build_offset_ref_call_from_tree
4857                                     (postfix_expression, &args));
4858             else if (idk == CP_ID_KIND_QUALIFIED)
4859               /* A call to a static class member, or a namespace-scope
4860                  function.  */
4861               postfix_expression
4862                 = finish_call_expr (postfix_expression, &args,
4863                                     /*disallow_virtual=*/true,
4864                                     koenig_p,
4865                                     tf_warning_or_error);
4866             else
4867               /* All other function calls.  */
4868               postfix_expression
4869                 = finish_call_expr (postfix_expression, &args,
4870                                     /*disallow_virtual=*/false,
4871                                     koenig_p,
4872                                     tf_warning_or_error);
4873
4874             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4875             idk = CP_ID_KIND_NONE;
4876
4877             release_tree_vector (args);
4878           }
4879           break;
4880
4881         case CPP_DOT:
4882         case CPP_DEREF:
4883           /* postfix-expression . template [opt] id-expression
4884              postfix-expression . pseudo-destructor-name
4885              postfix-expression -> template [opt] id-expression
4886              postfix-expression -> pseudo-destructor-name */
4887
4888           /* Consume the `.' or `->' operator.  */
4889           cp_lexer_consume_token (parser->lexer);
4890
4891           postfix_expression
4892             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4893                                                       postfix_expression,
4894                                                       false, &idk,
4895                                                       token->location);
4896
4897           is_member_access = true;
4898           break;
4899
4900         case CPP_PLUS_PLUS:
4901           /* postfix-expression ++  */
4902           /* Consume the `++' token.  */
4903           cp_lexer_consume_token (parser->lexer);
4904           /* Generate a representation for the complete expression.  */
4905           postfix_expression
4906             = finish_increment_expr (postfix_expression,
4907                                      POSTINCREMENT_EXPR);
4908           /* Increments may not appear in constant-expressions.  */
4909           if (cp_parser_non_integral_constant_expression (parser,
4910                                                           "an increment"))
4911             postfix_expression = error_mark_node;
4912           idk = CP_ID_KIND_NONE;
4913           is_member_access = false;
4914           break;
4915
4916         case CPP_MINUS_MINUS:
4917           /* postfix-expression -- */
4918           /* Consume the `--' token.  */
4919           cp_lexer_consume_token (parser->lexer);
4920           /* Generate a representation for the complete expression.  */
4921           postfix_expression
4922             = finish_increment_expr (postfix_expression,
4923                                      POSTDECREMENT_EXPR);
4924           /* Decrements may not appear in constant-expressions.  */
4925           if (cp_parser_non_integral_constant_expression (parser,
4926                                                           "a decrement"))
4927             postfix_expression = error_mark_node;
4928           idk = CP_ID_KIND_NONE;
4929           is_member_access = false;
4930           break;
4931
4932         default:
4933           if (pidk_return != NULL)
4934             * pidk_return = idk;
4935           if (member_access_only_p)
4936             return is_member_access? postfix_expression : error_mark_node;
4937           else
4938             return postfix_expression;
4939         }
4940     }
4941
4942   /* We should never get here.  */
4943   gcc_unreachable ();
4944   return error_mark_node;
4945 }
4946
4947 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4948    by cp_parser_builtin_offsetof.  We're looking for
4949
4950      postfix-expression [ expression ]
4951
4952    FOR_OFFSETOF is set if we're being called in that context, which
4953    changes how we deal with integer constant expressions.  */
4954
4955 static tree
4956 cp_parser_postfix_open_square_expression (cp_parser *parser,
4957                                           tree postfix_expression,
4958                                           bool for_offsetof)
4959 {
4960   tree index;
4961
4962   /* Consume the `[' token.  */
4963   cp_lexer_consume_token (parser->lexer);
4964
4965   /* Parse the index expression.  */
4966   /* ??? For offsetof, there is a question of what to allow here.  If
4967      offsetof is not being used in an integral constant expression context,
4968      then we *could* get the right answer by computing the value at runtime.
4969      If we are in an integral constant expression context, then we might
4970      could accept any constant expression; hard to say without analysis.
4971      Rather than open the barn door too wide right away, allow only integer
4972      constant expressions here.  */
4973   if (for_offsetof)
4974     index = cp_parser_constant_expression (parser, false, NULL);
4975   else
4976     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4977
4978   /* Look for the closing `]'.  */
4979   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4980
4981   /* Build the ARRAY_REF.  */
4982   postfix_expression = grok_array_decl (postfix_expression, index);
4983
4984   /* When not doing offsetof, array references are not permitted in
4985      constant-expressions.  */
4986   if (!for_offsetof
4987       && (cp_parser_non_integral_constant_expression
4988           (parser, "an array reference")))
4989     postfix_expression = error_mark_node;
4990
4991   return postfix_expression;
4992 }
4993
4994 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4995    by cp_parser_builtin_offsetof.  We're looking for
4996
4997      postfix-expression . template [opt] id-expression
4998      postfix-expression . pseudo-destructor-name
4999      postfix-expression -> template [opt] id-expression
5000      postfix-expression -> pseudo-destructor-name
5001
5002    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5003    limits what of the above we'll actually accept, but nevermind.
5004    TOKEN_TYPE is the "." or "->" token, which will already have been
5005    removed from the stream.  */
5006
5007 static tree
5008 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5009                                         enum cpp_ttype token_type,
5010                                         tree postfix_expression,
5011                                         bool for_offsetof, cp_id_kind *idk,
5012                                         location_t location)
5013 {
5014   tree name;
5015   bool dependent_p;
5016   bool pseudo_destructor_p;
5017   tree scope = NULL_TREE;
5018
5019   /* If this is a `->' operator, dereference the pointer.  */
5020   if (token_type == CPP_DEREF)
5021     postfix_expression = build_x_arrow (postfix_expression);
5022   /* Check to see whether or not the expression is type-dependent.  */
5023   dependent_p = type_dependent_expression_p (postfix_expression);
5024   /* The identifier following the `->' or `.' is not qualified.  */
5025   parser->scope = NULL_TREE;
5026   parser->qualifying_scope = NULL_TREE;
5027   parser->object_scope = NULL_TREE;
5028   *idk = CP_ID_KIND_NONE;
5029
5030   /* Enter the scope corresponding to the type of the object
5031      given by the POSTFIX_EXPRESSION.  */
5032   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5033     {
5034       scope = TREE_TYPE (postfix_expression);
5035       /* According to the standard, no expression should ever have
5036          reference type.  Unfortunately, we do not currently match
5037          the standard in this respect in that our internal representation
5038          of an expression may have reference type even when the standard
5039          says it does not.  Therefore, we have to manually obtain the
5040          underlying type here.  */
5041       scope = non_reference (scope);
5042       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5043       if (scope == unknown_type_node)
5044         {
5045           error_at (location, "%qE does not have class type",
5046                     postfix_expression);
5047           scope = NULL_TREE;
5048         }
5049       else
5050         scope = complete_type_or_else (scope, NULL_TREE);
5051       /* Let the name lookup machinery know that we are processing a
5052          class member access expression.  */
5053       parser->context->object_type = scope;
5054       /* If something went wrong, we want to be able to discern that case,
5055          as opposed to the case where there was no SCOPE due to the type
5056          of expression being dependent.  */
5057       if (!scope)
5058         scope = error_mark_node;
5059       /* If the SCOPE was erroneous, make the various semantic analysis
5060          functions exit quickly -- and without issuing additional error
5061          messages.  */
5062       if (scope == error_mark_node)
5063         postfix_expression = error_mark_node;
5064     }
5065
5066   /* Assume this expression is not a pseudo-destructor access.  */
5067   pseudo_destructor_p = false;
5068
5069   /* If the SCOPE is a scalar type, then, if this is a valid program,
5070      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5071      is type dependent, it can be pseudo-destructor-name or something else.
5072      Try to parse it as pseudo-destructor-name first.  */
5073   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5074     {
5075       tree s;
5076       tree type;
5077
5078       cp_parser_parse_tentatively (parser);
5079       /* Parse the pseudo-destructor-name.  */
5080       s = NULL_TREE;
5081       cp_parser_pseudo_destructor_name (parser, &s, &type);
5082       if (dependent_p
5083           && (cp_parser_error_occurred (parser)
5084               || TREE_CODE (type) != TYPE_DECL
5085               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5086         cp_parser_abort_tentative_parse (parser);
5087       else if (cp_parser_parse_definitely (parser))
5088         {
5089           pseudo_destructor_p = true;
5090           postfix_expression
5091             = finish_pseudo_destructor_expr (postfix_expression,
5092                                              s, TREE_TYPE (type));
5093         }
5094     }
5095
5096   if (!pseudo_destructor_p)
5097     {
5098       /* If the SCOPE is not a scalar type, we are looking at an
5099          ordinary class member access expression, rather than a
5100          pseudo-destructor-name.  */
5101       bool template_p;
5102       cp_token *token = cp_lexer_peek_token (parser->lexer);
5103       /* Parse the id-expression.  */
5104       name = (cp_parser_id_expression
5105               (parser,
5106                cp_parser_optional_template_keyword (parser),
5107                /*check_dependency_p=*/true,
5108                &template_p,
5109                /*declarator_p=*/false,
5110                /*optional_p=*/false));
5111       /* In general, build a SCOPE_REF if the member name is qualified.
5112          However, if the name was not dependent and has already been
5113          resolved; there is no need to build the SCOPE_REF.  For example;
5114
5115              struct X { void f(); };
5116              template <typename T> void f(T* t) { t->X::f(); }
5117
5118          Even though "t" is dependent, "X::f" is not and has been resolved
5119          to a BASELINK; there is no need to include scope information.  */
5120
5121       /* But we do need to remember that there was an explicit scope for
5122          virtual function calls.  */
5123       if (parser->scope)
5124         *idk = CP_ID_KIND_QUALIFIED;
5125
5126       /* If the name is a template-id that names a type, we will get a
5127          TYPE_DECL here.  That is invalid code.  */
5128       if (TREE_CODE (name) == TYPE_DECL)
5129         {
5130           error_at (token->location, "invalid use of %qD", name);
5131           postfix_expression = error_mark_node;
5132         }
5133       else
5134         {
5135           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5136             {
5137               name = build_qualified_name (/*type=*/NULL_TREE,
5138                                            parser->scope,
5139                                            name,
5140                                            template_p);
5141               parser->scope = NULL_TREE;
5142               parser->qualifying_scope = NULL_TREE;
5143               parser->object_scope = NULL_TREE;
5144             }
5145           if (scope && name && BASELINK_P (name))
5146             adjust_result_of_qualified_name_lookup
5147               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5148           postfix_expression
5149             = finish_class_member_access_expr (postfix_expression, name,
5150                                                template_p, 
5151                                                tf_warning_or_error);
5152         }
5153     }
5154
5155   /* We no longer need to look up names in the scope of the object on
5156      the left-hand side of the `.' or `->' operator.  */
5157   parser->context->object_type = NULL_TREE;
5158
5159   /* Outside of offsetof, these operators may not appear in
5160      constant-expressions.  */
5161   if (!for_offsetof
5162       && (cp_parser_non_integral_constant_expression
5163           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5164     postfix_expression = error_mark_node;
5165
5166   return postfix_expression;
5167 }
5168
5169 /* Parse a parenthesized expression-list.
5170
5171    expression-list:
5172      assignment-expression
5173      expression-list, assignment-expression
5174
5175    attribute-list:
5176      expression-list
5177      identifier
5178      identifier, expression-list
5179
5180    CAST_P is true if this expression is the target of a cast.
5181
5182    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5183    argument pack.
5184
5185    Returns a vector of trees.  Each element is a representation of an
5186    assignment-expression.  NULL is returned if the ( and or ) are
5187    missing.  An empty, but allocated, vector is returned on no
5188    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5189    if this is really an attribute list being parsed.  If
5190    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5191    not all of the expressions in the list were constant.  */
5192
5193 static VEC(tree,gc) *
5194 cp_parser_parenthesized_expression_list (cp_parser* parser,
5195                                          bool is_attribute_list,
5196                                          bool cast_p,
5197                                          bool allow_expansion_p,
5198                                          bool *non_constant_p)
5199 {
5200   VEC(tree,gc) *expression_list;
5201   bool fold_expr_p = is_attribute_list;
5202   tree identifier = NULL_TREE;
5203   bool saved_greater_than_is_operator_p;
5204
5205   /* Assume all the expressions will be constant.  */
5206   if (non_constant_p)
5207     *non_constant_p = false;
5208
5209   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5210     return NULL;
5211
5212   expression_list = make_tree_vector ();
5213
5214   /* Within a parenthesized expression, a `>' token is always
5215      the greater-than operator.  */
5216   saved_greater_than_is_operator_p
5217     = parser->greater_than_is_operator_p;
5218   parser->greater_than_is_operator_p = true;
5219
5220   /* Consume expressions until there are no more.  */
5221   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5222     while (true)
5223       {
5224         tree expr;
5225
5226         /* At the beginning of attribute lists, check to see if the
5227            next token is an identifier.  */
5228         if (is_attribute_list
5229             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5230           {
5231             cp_token *token;
5232
5233             /* Consume the identifier.  */
5234             token = cp_lexer_consume_token (parser->lexer);
5235             /* Save the identifier.  */
5236             identifier = token->u.value;
5237           }
5238         else
5239           {
5240             bool expr_non_constant_p;
5241
5242             /* Parse the next assignment-expression.  */
5243             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5244               {
5245                 /* A braced-init-list.  */
5246                 maybe_warn_cpp0x ("extended initializer lists");
5247                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5248                 if (non_constant_p && expr_non_constant_p)
5249                   *non_constant_p = true;
5250               }
5251             else if (non_constant_p)
5252               {
5253                 expr = (cp_parser_constant_expression
5254                         (parser, /*allow_non_constant_p=*/true,
5255                          &expr_non_constant_p));
5256                 if (expr_non_constant_p)
5257                   *non_constant_p = true;
5258               }
5259             else
5260               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5261
5262             if (fold_expr_p)
5263               expr = fold_non_dependent_expr (expr);
5264
5265             /* If we have an ellipsis, then this is an expression
5266                expansion.  */
5267             if (allow_expansion_p
5268                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5269               {
5270                 /* Consume the `...'.  */
5271                 cp_lexer_consume_token (parser->lexer);
5272
5273                 /* Build the argument pack.  */
5274                 expr = make_pack_expansion (expr);
5275               }
5276
5277              /* Add it to the list.  We add error_mark_node
5278                 expressions to the list, so that we can still tell if
5279                 the correct form for a parenthesized expression-list
5280                 is found. That gives better errors.  */
5281             VEC_safe_push (tree, gc, expression_list, expr);
5282
5283             if (expr == error_mark_node)
5284               goto skip_comma;
5285           }
5286
5287         /* After the first item, attribute lists look the same as
5288            expression lists.  */
5289         is_attribute_list = false;
5290
5291       get_comma:;
5292         /* If the next token isn't a `,', then we are done.  */
5293         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5294           break;
5295
5296         /* Otherwise, consume the `,' and keep going.  */
5297         cp_lexer_consume_token (parser->lexer);
5298       }
5299
5300   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5301     {
5302       int ending;
5303
5304     skip_comma:;
5305       /* We try and resync to an unnested comma, as that will give the
5306          user better diagnostics.  */
5307       ending = cp_parser_skip_to_closing_parenthesis (parser,
5308                                                       /*recovering=*/true,
5309                                                       /*or_comma=*/true,
5310                                                       /*consume_paren=*/true);
5311       if (ending < 0)
5312         goto get_comma;
5313       if (!ending)
5314         {
5315           parser->greater_than_is_operator_p
5316             = saved_greater_than_is_operator_p;
5317           return NULL;
5318         }
5319     }
5320
5321   parser->greater_than_is_operator_p
5322     = saved_greater_than_is_operator_p;
5323
5324   if (identifier)
5325     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5326
5327   return expression_list;
5328 }
5329
5330 /* Parse a pseudo-destructor-name.
5331
5332    pseudo-destructor-name:
5333      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5334      :: [opt] nested-name-specifier template template-id :: ~ type-name
5335      :: [opt] nested-name-specifier [opt] ~ type-name
5336
5337    If either of the first two productions is used, sets *SCOPE to the
5338    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5339    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5340    or ERROR_MARK_NODE if the parse fails.  */
5341
5342 static void
5343 cp_parser_pseudo_destructor_name (cp_parser* parser,
5344                                   tree* scope,
5345                                   tree* type)
5346 {
5347   bool nested_name_specifier_p;
5348
5349   /* Assume that things will not work out.  */
5350   *type = error_mark_node;
5351
5352   /* Look for the optional `::' operator.  */
5353   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5354   /* Look for the optional nested-name-specifier.  */
5355   nested_name_specifier_p
5356     = (cp_parser_nested_name_specifier_opt (parser,
5357                                             /*typename_keyword_p=*/false,
5358                                             /*check_dependency_p=*/true,
5359                                             /*type_p=*/false,
5360                                             /*is_declaration=*/false)
5361        != NULL_TREE);
5362   /* Now, if we saw a nested-name-specifier, we might be doing the
5363      second production.  */
5364   if (nested_name_specifier_p
5365       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5366     {
5367       /* Consume the `template' keyword.  */
5368       cp_lexer_consume_token (parser->lexer);
5369       /* Parse the template-id.  */
5370       cp_parser_template_id (parser,
5371                              /*template_keyword_p=*/true,
5372                              /*check_dependency_p=*/false,
5373                              /*is_declaration=*/true);
5374       /* Look for the `::' token.  */
5375       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5376     }
5377   /* If the next token is not a `~', then there might be some
5378      additional qualification.  */
5379   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5380     {
5381       /* At this point, we're looking for "type-name :: ~".  The type-name
5382          must not be a class-name, since this is a pseudo-destructor.  So,
5383          it must be either an enum-name, or a typedef-name -- both of which
5384          are just identifiers.  So, we peek ahead to check that the "::"
5385          and "~" tokens are present; if they are not, then we can avoid
5386          calling type_name.  */
5387       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5388           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5389           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5390         {
5391           cp_parser_error (parser, "non-scalar type");
5392           return;
5393         }
5394
5395       /* Look for the type-name.  */
5396       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5397       if (*scope == error_mark_node)
5398         return;
5399
5400       /* Look for the `::' token.  */
5401       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5402     }
5403   else
5404     *scope = NULL_TREE;
5405
5406   /* Look for the `~'.  */
5407   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5408   /* Look for the type-name again.  We are not responsible for
5409      checking that it matches the first type-name.  */
5410   *type = cp_parser_nonclass_name (parser);
5411 }
5412
5413 /* Parse a unary-expression.
5414
5415    unary-expression:
5416      postfix-expression
5417      ++ cast-expression
5418      -- cast-expression
5419      unary-operator cast-expression
5420      sizeof unary-expression
5421      sizeof ( type-id )
5422      new-expression
5423      delete-expression
5424
5425    GNU Extensions:
5426
5427    unary-expression:
5428      __extension__ cast-expression
5429      __alignof__ unary-expression
5430      __alignof__ ( type-id )
5431      __real__ cast-expression
5432      __imag__ cast-expression
5433      && identifier
5434
5435    ADDRESS_P is true iff the unary-expression is appearing as the
5436    operand of the `&' operator.   CAST_P is true if this expression is
5437    the target of a cast.
5438
5439    Returns a representation of the expression.  */
5440
5441 static tree
5442 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5443                             cp_id_kind * pidk)
5444 {
5445   cp_token *token;
5446   enum tree_code unary_operator;
5447
5448   /* Peek at the next token.  */
5449   token = cp_lexer_peek_token (parser->lexer);
5450   /* Some keywords give away the kind of expression.  */
5451   if (token->type == CPP_KEYWORD)
5452     {
5453       enum rid keyword = token->keyword;
5454
5455       switch (keyword)
5456         {
5457         case RID_ALIGNOF:
5458         case RID_SIZEOF:
5459           {
5460             tree operand;
5461             enum tree_code op;
5462
5463             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5464             /* Consume the token.  */
5465             cp_lexer_consume_token (parser->lexer);
5466             /* Parse the operand.  */
5467             operand = cp_parser_sizeof_operand (parser, keyword);
5468
5469             if (TYPE_P (operand))
5470               return cxx_sizeof_or_alignof_type (operand, op, true);
5471             else
5472               return cxx_sizeof_or_alignof_expr (operand, op, true);
5473           }
5474
5475         case RID_NEW:
5476           return cp_parser_new_expression (parser);
5477
5478         case RID_DELETE:
5479           return cp_parser_delete_expression (parser);
5480
5481         case RID_EXTENSION:
5482           {
5483             /* The saved value of the PEDANTIC flag.  */
5484             int saved_pedantic;
5485             tree expr;
5486
5487             /* Save away the PEDANTIC flag.  */
5488             cp_parser_extension_opt (parser, &saved_pedantic);
5489             /* Parse the cast-expression.  */
5490             expr = cp_parser_simple_cast_expression (parser);
5491             /* Restore the PEDANTIC flag.  */
5492             pedantic = saved_pedantic;
5493
5494             return expr;
5495           }
5496
5497         case RID_REALPART:
5498         case RID_IMAGPART:
5499           {
5500             tree expression;
5501
5502             /* Consume the `__real__' or `__imag__' token.  */
5503             cp_lexer_consume_token (parser->lexer);
5504             /* Parse the cast-expression.  */
5505             expression = cp_parser_simple_cast_expression (parser);
5506             /* Create the complete representation.  */
5507             return build_x_unary_op ((keyword == RID_REALPART
5508                                       ? REALPART_EXPR : IMAGPART_EXPR),
5509                                      expression,
5510                                      tf_warning_or_error);
5511           }
5512           break;
5513
5514         default:
5515           break;
5516         }
5517     }
5518
5519   /* Look for the `:: new' and `:: delete', which also signal the
5520      beginning of a new-expression, or delete-expression,
5521      respectively.  If the next token is `::', then it might be one of
5522      these.  */
5523   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5524     {
5525       enum rid keyword;
5526
5527       /* See if the token after the `::' is one of the keywords in
5528          which we're interested.  */
5529       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5530       /* If it's `new', we have a new-expression.  */
5531       if (keyword == RID_NEW)
5532         return cp_parser_new_expression (parser);
5533       /* Similarly, for `delete'.  */
5534       else if (keyword == RID_DELETE)
5535         return cp_parser_delete_expression (parser);
5536     }
5537
5538   /* Look for a unary operator.  */
5539   unary_operator = cp_parser_unary_operator (token);
5540   /* The `++' and `--' operators can be handled similarly, even though
5541      they are not technically unary-operators in the grammar.  */
5542   if (unary_operator == ERROR_MARK)
5543     {
5544       if (token->type == CPP_PLUS_PLUS)
5545         unary_operator = PREINCREMENT_EXPR;
5546       else if (token->type == CPP_MINUS_MINUS)
5547         unary_operator = PREDECREMENT_EXPR;
5548       /* Handle the GNU address-of-label extension.  */
5549       else if (cp_parser_allow_gnu_extensions_p (parser)
5550                && token->type == CPP_AND_AND)
5551         {
5552           tree identifier;
5553           tree expression;
5554           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5555
5556           /* Consume the '&&' token.  */
5557           cp_lexer_consume_token (parser->lexer);
5558           /* Look for the identifier.  */
5559           identifier = cp_parser_identifier (parser);
5560           /* Create an expression representing the address.  */
5561           expression = finish_label_address_expr (identifier, loc);
5562           if (cp_parser_non_integral_constant_expression (parser,
5563                                                 "the address of a label"))
5564             expression = error_mark_node;
5565           return expression;
5566         }
5567     }
5568   if (unary_operator != ERROR_MARK)
5569     {
5570       tree cast_expression;
5571       tree expression = error_mark_node;
5572       const char *non_constant_p = NULL;
5573
5574       /* Consume the operator token.  */
5575       token = cp_lexer_consume_token (parser->lexer);
5576       /* Parse the cast-expression.  */
5577       cast_expression
5578         = cp_parser_cast_expression (parser,
5579                                      unary_operator == ADDR_EXPR,
5580                                      /*cast_p=*/false, pidk);
5581       /* Now, build an appropriate representation.  */
5582       switch (unary_operator)
5583         {
5584         case INDIRECT_REF:
5585           non_constant_p = "%<*%>";
5586           expression = build_x_indirect_ref (cast_expression, "unary *",
5587                                              tf_warning_or_error);
5588           break;
5589
5590         case ADDR_EXPR:
5591           non_constant_p = "%<&%>";
5592           /* Fall through.  */
5593         case BIT_NOT_EXPR:
5594           expression = build_x_unary_op (unary_operator, cast_expression,
5595                                          tf_warning_or_error);
5596           break;
5597
5598         case PREINCREMENT_EXPR:
5599         case PREDECREMENT_EXPR:
5600           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5601                             ? "%<++%>" : "%<--%>");
5602           /* Fall through.  */
5603         case UNARY_PLUS_EXPR:
5604         case NEGATE_EXPR:
5605         case TRUTH_NOT_EXPR:
5606           expression = finish_unary_op_expr (unary_operator, cast_expression);
5607           break;
5608
5609         default:
5610           gcc_unreachable ();
5611         }
5612
5613       if (non_constant_p
5614           && cp_parser_non_integral_constant_expression (parser,
5615                                                          non_constant_p))
5616         expression = error_mark_node;
5617
5618       return expression;
5619     }
5620
5621   return cp_parser_postfix_expression (parser, address_p, cast_p,
5622                                        /*member_access_only_p=*/false,
5623                                        pidk);
5624 }
5625
5626 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5627    unary-operator, the corresponding tree code is returned.  */
5628
5629 static enum tree_code
5630 cp_parser_unary_operator (cp_token* token)
5631 {
5632   switch (token->type)
5633     {
5634     case CPP_MULT:
5635       return INDIRECT_REF;
5636
5637     case CPP_AND:
5638       return ADDR_EXPR;
5639
5640     case CPP_PLUS:
5641       return UNARY_PLUS_EXPR;
5642
5643     case CPP_MINUS:
5644       return NEGATE_EXPR;
5645
5646     case CPP_NOT:
5647       return TRUTH_NOT_EXPR;
5648
5649     case CPP_COMPL:
5650       return BIT_NOT_EXPR;
5651
5652     default:
5653       return ERROR_MARK;
5654     }
5655 }
5656
5657 /* Parse a new-expression.
5658
5659    new-expression:
5660      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5661      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5662
5663    Returns a representation of the expression.  */
5664
5665 static tree
5666 cp_parser_new_expression (cp_parser* parser)
5667 {
5668   bool global_scope_p;
5669   VEC(tree,gc) *placement;
5670   tree type;
5671   VEC(tree,gc) *initializer;
5672   tree nelts;
5673   tree ret;
5674
5675   /* Look for the optional `::' operator.  */
5676   global_scope_p
5677     = (cp_parser_global_scope_opt (parser,
5678                                    /*current_scope_valid_p=*/false)
5679        != NULL_TREE);
5680   /* Look for the `new' operator.  */
5681   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5682   /* There's no easy way to tell a new-placement from the
5683      `( type-id )' construct.  */
5684   cp_parser_parse_tentatively (parser);
5685   /* Look for a new-placement.  */
5686   placement = cp_parser_new_placement (parser);
5687   /* If that didn't work out, there's no new-placement.  */
5688   if (!cp_parser_parse_definitely (parser))
5689     {
5690       if (placement != NULL)
5691         release_tree_vector (placement);
5692       placement = NULL;
5693     }
5694
5695   /* If the next token is a `(', then we have a parenthesized
5696      type-id.  */
5697   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5698     {
5699       cp_token *token;
5700       /* Consume the `('.  */
5701       cp_lexer_consume_token (parser->lexer);
5702       /* Parse the type-id.  */
5703       type = cp_parser_type_id (parser);
5704       /* Look for the closing `)'.  */
5705       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5706       token = cp_lexer_peek_token (parser->lexer);
5707       /* There should not be a direct-new-declarator in this production,
5708          but GCC used to allowed this, so we check and emit a sensible error
5709          message for this case.  */
5710       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5711         {
5712           error_at (token->location,
5713                     "array bound forbidden after parenthesized type-id");
5714           inform (token->location, 
5715                   "try removing the parentheses around the type-id");
5716           cp_parser_direct_new_declarator (parser);
5717         }
5718       nelts = NULL_TREE;
5719     }
5720   /* Otherwise, there must be a new-type-id.  */
5721   else
5722     type = cp_parser_new_type_id (parser, &nelts);
5723
5724   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5725   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5726       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5727     initializer = cp_parser_new_initializer (parser);
5728   else
5729     initializer = NULL;
5730
5731   /* A new-expression may not appear in an integral constant
5732      expression.  */
5733   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5734     ret = error_mark_node;
5735   else
5736     {
5737       /* Create a representation of the new-expression.  */
5738       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5739                        tf_warning_or_error);
5740     }
5741
5742   if (placement != NULL)
5743     release_tree_vector (placement);
5744   if (initializer != NULL)
5745     release_tree_vector (initializer);
5746
5747   return ret;
5748 }
5749
5750 /* Parse a new-placement.
5751
5752    new-placement:
5753      ( expression-list )
5754
5755    Returns the same representation as for an expression-list.  */
5756
5757 static VEC(tree,gc) *
5758 cp_parser_new_placement (cp_parser* parser)
5759 {
5760   VEC(tree,gc) *expression_list;
5761
5762   /* Parse the expression-list.  */
5763   expression_list = (cp_parser_parenthesized_expression_list
5764                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5765                       /*non_constant_p=*/NULL));
5766
5767   return expression_list;
5768 }
5769
5770 /* Parse a new-type-id.
5771
5772    new-type-id:
5773      type-specifier-seq new-declarator [opt]
5774
5775    Returns the TYPE allocated.  If the new-type-id indicates an array
5776    type, *NELTS is set to the number of elements in the last array
5777    bound; the TYPE will not include the last array bound.  */
5778
5779 static tree
5780 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5781 {
5782   cp_decl_specifier_seq type_specifier_seq;
5783   cp_declarator *new_declarator;
5784   cp_declarator *declarator;
5785   cp_declarator *outer_declarator;
5786   const char *saved_message;
5787   tree type;
5788
5789   /* The type-specifier sequence must not contain type definitions.
5790      (It cannot contain declarations of new types either, but if they
5791      are not definitions we will catch that because they are not
5792      complete.)  */
5793   saved_message = parser->type_definition_forbidden_message;
5794   parser->type_definition_forbidden_message
5795     = "types may not be defined in a new-type-id";
5796   /* Parse the type-specifier-seq.  */
5797   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5798                                 &type_specifier_seq);
5799   /* Restore the old message.  */
5800   parser->type_definition_forbidden_message = saved_message;
5801   /* Parse the new-declarator.  */
5802   new_declarator = cp_parser_new_declarator_opt (parser);
5803
5804   /* Determine the number of elements in the last array dimension, if
5805      any.  */
5806   *nelts = NULL_TREE;
5807   /* Skip down to the last array dimension.  */
5808   declarator = new_declarator;
5809   outer_declarator = NULL;
5810   while (declarator && (declarator->kind == cdk_pointer
5811                         || declarator->kind == cdk_ptrmem))
5812     {
5813       outer_declarator = declarator;
5814       declarator = declarator->declarator;
5815     }
5816   while (declarator
5817          && declarator->kind == cdk_array
5818          && declarator->declarator
5819          && declarator->declarator->kind == cdk_array)
5820     {
5821       outer_declarator = declarator;
5822       declarator = declarator->declarator;
5823     }
5824
5825   if (declarator && declarator->kind == cdk_array)
5826     {
5827       *nelts = declarator->u.array.bounds;
5828       if (*nelts == error_mark_node)
5829         *nelts = integer_one_node;
5830
5831       if (outer_declarator)
5832         outer_declarator->declarator = declarator->declarator;
5833       else
5834         new_declarator = NULL;
5835     }
5836
5837   type = groktypename (&type_specifier_seq, new_declarator, false);
5838   return type;
5839 }
5840
5841 /* Parse an (optional) new-declarator.
5842
5843    new-declarator:
5844      ptr-operator new-declarator [opt]
5845      direct-new-declarator
5846
5847    Returns the declarator.  */
5848
5849 static cp_declarator *
5850 cp_parser_new_declarator_opt (cp_parser* parser)
5851 {
5852   enum tree_code code;
5853   tree type;
5854   cp_cv_quals cv_quals;
5855
5856   /* We don't know if there's a ptr-operator next, or not.  */
5857   cp_parser_parse_tentatively (parser);
5858   /* Look for a ptr-operator.  */
5859   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5860   /* If that worked, look for more new-declarators.  */
5861   if (cp_parser_parse_definitely (parser))
5862     {
5863       cp_declarator *declarator;
5864
5865       /* Parse another optional declarator.  */
5866       declarator = cp_parser_new_declarator_opt (parser);
5867
5868       return cp_parser_make_indirect_declarator
5869         (code, type, cv_quals, declarator);
5870     }
5871
5872   /* If the next token is a `[', there is a direct-new-declarator.  */
5873   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5874     return cp_parser_direct_new_declarator (parser);
5875
5876   return NULL;
5877 }
5878
5879 /* Parse a direct-new-declarator.
5880
5881    direct-new-declarator:
5882      [ expression ]
5883      direct-new-declarator [constant-expression]
5884
5885    */
5886
5887 static cp_declarator *
5888 cp_parser_direct_new_declarator (cp_parser* parser)
5889 {
5890   cp_declarator *declarator = NULL;
5891
5892   while (true)
5893     {
5894       tree expression;
5895
5896       /* Look for the opening `['.  */
5897       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5898       /* The first expression is not required to be constant.  */
5899       if (!declarator)
5900         {
5901           cp_token *token = cp_lexer_peek_token (parser->lexer);
5902           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5903           /* The standard requires that the expression have integral
5904              type.  DR 74 adds enumeration types.  We believe that the
5905              real intent is that these expressions be handled like the
5906              expression in a `switch' condition, which also allows
5907              classes with a single conversion to integral or
5908              enumeration type.  */
5909           if (!processing_template_decl)
5910             {
5911               expression
5912                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5913                                               expression,
5914                                               /*complain=*/true);
5915               if (!expression)
5916                 {
5917                   error_at (token->location,
5918                             "expression in new-declarator must have integral "
5919                             "or enumeration type");
5920                   expression = error_mark_node;
5921                 }
5922             }
5923         }
5924       /* But all the other expressions must be.  */
5925       else
5926         expression
5927           = cp_parser_constant_expression (parser,
5928                                            /*allow_non_constant=*/false,
5929                                            NULL);
5930       /* Look for the closing `]'.  */
5931       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5932
5933       /* Add this bound to the declarator.  */
5934       declarator = make_array_declarator (declarator, expression);
5935
5936       /* If the next token is not a `[', then there are no more
5937          bounds.  */
5938       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5939         break;
5940     }
5941
5942   return declarator;
5943 }
5944
5945 /* Parse a new-initializer.
5946
5947    new-initializer:
5948      ( expression-list [opt] )
5949      braced-init-list
5950
5951    Returns a representation of the expression-list.  */
5952
5953 static VEC(tree,gc) *
5954 cp_parser_new_initializer (cp_parser* parser)
5955 {
5956   VEC(tree,gc) *expression_list;
5957
5958   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5959     {
5960       tree t;
5961       bool expr_non_constant_p;
5962       maybe_warn_cpp0x ("extended initializer lists");
5963       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5964       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5965       expression_list = make_tree_vector_single (t);
5966     }
5967   else
5968     expression_list = (cp_parser_parenthesized_expression_list
5969                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5970                         /*non_constant_p=*/NULL));
5971
5972   return expression_list;
5973 }
5974
5975 /* Parse a delete-expression.
5976
5977    delete-expression:
5978      :: [opt] delete cast-expression
5979      :: [opt] delete [ ] cast-expression
5980
5981    Returns a representation of the expression.  */
5982
5983 static tree
5984 cp_parser_delete_expression (cp_parser* parser)
5985 {
5986   bool global_scope_p;
5987   bool array_p;
5988   tree expression;
5989
5990   /* Look for the optional `::' operator.  */
5991   global_scope_p
5992     = (cp_parser_global_scope_opt (parser,
5993                                    /*current_scope_valid_p=*/false)
5994        != NULL_TREE);
5995   /* Look for the `delete' keyword.  */
5996   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5997   /* See if the array syntax is in use.  */
5998   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5999     {
6000       /* Consume the `[' token.  */
6001       cp_lexer_consume_token (parser->lexer);
6002       /* Look for the `]' token.  */
6003       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6004       /* Remember that this is the `[]' construct.  */
6005       array_p = true;
6006     }
6007   else
6008     array_p = false;
6009
6010   /* Parse the cast-expression.  */
6011   expression = cp_parser_simple_cast_expression (parser);
6012
6013   /* A delete-expression may not appear in an integral constant
6014      expression.  */
6015   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6016     return error_mark_node;
6017
6018   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6019 }
6020
6021 /* Returns true if TOKEN may start a cast-expression and false
6022    otherwise.  */
6023
6024 static bool
6025 cp_parser_token_starts_cast_expression (cp_token *token)
6026 {
6027   switch (token->type)
6028     {
6029     case CPP_COMMA:
6030     case CPP_SEMICOLON:
6031     case CPP_QUERY:
6032     case CPP_COLON:
6033     case CPP_CLOSE_SQUARE:
6034     case CPP_CLOSE_PAREN:
6035     case CPP_CLOSE_BRACE:
6036     case CPP_DOT:
6037     case CPP_DOT_STAR:
6038     case CPP_DEREF:
6039     case CPP_DEREF_STAR:
6040     case CPP_DIV:
6041     case CPP_MOD:
6042     case CPP_LSHIFT:
6043     case CPP_RSHIFT:
6044     case CPP_LESS:
6045     case CPP_GREATER:
6046     case CPP_LESS_EQ:
6047     case CPP_GREATER_EQ:
6048     case CPP_EQ_EQ:
6049     case CPP_NOT_EQ:
6050     case CPP_EQ:
6051     case CPP_MULT_EQ:
6052     case CPP_DIV_EQ:
6053     case CPP_MOD_EQ:
6054     case CPP_PLUS_EQ:
6055     case CPP_MINUS_EQ:
6056     case CPP_RSHIFT_EQ:
6057     case CPP_LSHIFT_EQ:
6058     case CPP_AND_EQ:
6059     case CPP_XOR_EQ:
6060     case CPP_OR_EQ:
6061     case CPP_XOR:
6062     case CPP_OR:
6063     case CPP_OR_OR:
6064     case CPP_EOF:
6065       return false;
6066
6067       /* '[' may start a primary-expression in obj-c++.  */
6068     case CPP_OPEN_SQUARE:
6069       return c_dialect_objc ();
6070
6071     default:
6072       return true;
6073     }
6074 }
6075
6076 /* Parse a cast-expression.
6077
6078    cast-expression:
6079      unary-expression
6080      ( type-id ) cast-expression
6081
6082    ADDRESS_P is true iff the unary-expression is appearing as the
6083    operand of the `&' operator.   CAST_P is true if this expression is
6084    the target of a cast.
6085
6086    Returns a representation of the expression.  */
6087
6088 static tree
6089 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6090                            cp_id_kind * pidk)
6091 {
6092   /* If it's a `(', then we might be looking at a cast.  */
6093   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6094     {
6095       tree type = NULL_TREE;
6096       tree expr = NULL_TREE;
6097       bool compound_literal_p;
6098       const char *saved_message;
6099
6100       /* There's no way to know yet whether or not this is a cast.
6101          For example, `(int (3))' is a unary-expression, while `(int)
6102          3' is a cast.  So, we resort to parsing tentatively.  */
6103       cp_parser_parse_tentatively (parser);
6104       /* Types may not be defined in a cast.  */
6105       saved_message = parser->type_definition_forbidden_message;
6106       parser->type_definition_forbidden_message
6107         = "types may not be defined in casts";
6108       /* Consume the `('.  */
6109       cp_lexer_consume_token (parser->lexer);
6110       /* A very tricky bit is that `(struct S) { 3 }' is a
6111          compound-literal (which we permit in C++ as an extension).
6112          But, that construct is not a cast-expression -- it is a
6113          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6114          is legal; if the compound-literal were a cast-expression,
6115          you'd need an extra set of parentheses.)  But, if we parse
6116          the type-id, and it happens to be a class-specifier, then we
6117          will commit to the parse at that point, because we cannot
6118          undo the action that is done when creating a new class.  So,
6119          then we cannot back up and do a postfix-expression.
6120
6121          Therefore, we scan ahead to the closing `)', and check to see
6122          if the token after the `)' is a `{'.  If so, we are not
6123          looking at a cast-expression.
6124
6125          Save tokens so that we can put them back.  */
6126       cp_lexer_save_tokens (parser->lexer);
6127       /* Skip tokens until the next token is a closing parenthesis.
6128          If we find the closing `)', and the next token is a `{', then
6129          we are looking at a compound-literal.  */
6130       compound_literal_p
6131         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6132                                                   /*consume_paren=*/true)
6133            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6134       /* Roll back the tokens we skipped.  */
6135       cp_lexer_rollback_tokens (parser->lexer);
6136       /* If we were looking at a compound-literal, simulate an error
6137          so that the call to cp_parser_parse_definitely below will
6138          fail.  */
6139       if (compound_literal_p)
6140         cp_parser_simulate_error (parser);
6141       else
6142         {
6143           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6144           parser->in_type_id_in_expr_p = true;
6145           /* Look for the type-id.  */
6146           type = cp_parser_type_id (parser);
6147           /* Look for the closing `)'.  */
6148           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6149           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6150         }
6151
6152       /* Restore the saved message.  */
6153       parser->type_definition_forbidden_message = saved_message;
6154
6155       /* At this point this can only be either a cast or a
6156          parenthesized ctor such as `(T ())' that looks like a cast to
6157          function returning T.  */
6158       if (!cp_parser_error_occurred (parser)
6159           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6160                                                      (parser->lexer)))
6161         {
6162           cp_parser_parse_definitely (parser);
6163           expr = cp_parser_cast_expression (parser,
6164                                             /*address_p=*/false,
6165                                             /*cast_p=*/true, pidk);
6166
6167           /* Warn about old-style casts, if so requested.  */
6168           if (warn_old_style_cast
6169               && !in_system_header
6170               && !VOID_TYPE_P (type)
6171               && current_lang_name != lang_name_c)
6172             warning (OPT_Wold_style_cast, "use of old-style cast");
6173
6174           /* Only type conversions to integral or enumeration types
6175              can be used in constant-expressions.  */
6176           if (!cast_valid_in_integral_constant_expression_p (type)
6177               && (cp_parser_non_integral_constant_expression
6178                   (parser,
6179                    "a cast to a type other than an integral or "
6180                    "enumeration type")))
6181             return error_mark_node;
6182
6183           /* Perform the cast.  */
6184           expr = build_c_cast (input_location, type, expr);
6185           return expr;
6186         }
6187       else 
6188         cp_parser_abort_tentative_parse (parser);
6189     }
6190
6191   /* If we get here, then it's not a cast, so it must be a
6192      unary-expression.  */
6193   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6194 }
6195
6196 /* Parse a binary expression of the general form:
6197
6198    pm-expression:
6199      cast-expression
6200      pm-expression .* cast-expression
6201      pm-expression ->* cast-expression
6202
6203    multiplicative-expression:
6204      pm-expression
6205      multiplicative-expression * pm-expression
6206      multiplicative-expression / pm-expression
6207      multiplicative-expression % pm-expression
6208
6209    additive-expression:
6210      multiplicative-expression
6211      additive-expression + multiplicative-expression
6212      additive-expression - multiplicative-expression
6213
6214    shift-expression:
6215      additive-expression
6216      shift-expression << additive-expression
6217      shift-expression >> additive-expression
6218
6219    relational-expression:
6220      shift-expression
6221      relational-expression < shift-expression
6222      relational-expression > shift-expression
6223      relational-expression <= shift-expression
6224      relational-expression >= shift-expression
6225
6226   GNU Extension:
6227
6228    relational-expression:
6229      relational-expression <? shift-expression
6230      relational-expression >? shift-expression
6231
6232    equality-expression:
6233      relational-expression
6234      equality-expression == relational-expression
6235      equality-expression != relational-expression
6236
6237    and-expression:
6238      equality-expression
6239      and-expression & equality-expression
6240
6241    exclusive-or-expression:
6242      and-expression
6243      exclusive-or-expression ^ and-expression
6244
6245    inclusive-or-expression:
6246      exclusive-or-expression
6247      inclusive-or-expression | exclusive-or-expression
6248
6249    logical-and-expression:
6250      inclusive-or-expression
6251      logical-and-expression && inclusive-or-expression
6252
6253    logical-or-expression:
6254      logical-and-expression
6255      logical-or-expression || logical-and-expression
6256
6257    All these are implemented with a single function like:
6258
6259    binary-expression:
6260      simple-cast-expression
6261      binary-expression <token> binary-expression
6262
6263    CAST_P is true if this expression is the target of a cast.
6264
6265    The binops_by_token map is used to get the tree codes for each <token> type.
6266    binary-expressions are associated according to a precedence table.  */
6267
6268 #define TOKEN_PRECEDENCE(token)                              \
6269 (((token->type == CPP_GREATER                                \
6270    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6271   && !parser->greater_than_is_operator_p)                    \
6272  ? PREC_NOT_OPERATOR                                         \
6273  : binops_by_token[token->type].prec)
6274
6275 static tree
6276 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6277                              bool no_toplevel_fold_p,
6278                              enum cp_parser_prec prec,
6279                              cp_id_kind * pidk)
6280 {
6281   cp_parser_expression_stack stack;
6282   cp_parser_expression_stack_entry *sp = &stack[0];
6283   tree lhs, rhs;
6284   cp_token *token;
6285   enum tree_code tree_type, lhs_type, rhs_type;
6286   enum cp_parser_prec new_prec, lookahead_prec;
6287   bool overloaded_p;
6288
6289   /* Parse the first expression.  */
6290   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6291   lhs_type = ERROR_MARK;
6292
6293   for (;;)
6294     {
6295       /* Get an operator token.  */
6296       token = cp_lexer_peek_token (parser->lexer);
6297
6298       if (warn_cxx0x_compat
6299           && token->type == CPP_RSHIFT
6300           && !parser->greater_than_is_operator_p)
6301         {
6302           if (warning_at (token->location, OPT_Wc__0x_compat, 
6303                           "%<>>%> operator will be treated as"
6304                           " two right angle brackets in C++0x"))
6305             inform (token->location,
6306                     "suggest parentheses around %<>>%> expression");
6307         }
6308
6309       new_prec = TOKEN_PRECEDENCE (token);
6310
6311       /* Popping an entry off the stack means we completed a subexpression:
6312          - either we found a token which is not an operator (`>' where it is not
6313            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6314            will happen repeatedly;
6315          - or, we found an operator which has lower priority.  This is the case
6316            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6317            parsing `3 * 4'.  */
6318       if (new_prec <= prec)
6319         {
6320           if (sp == stack)
6321             break;
6322           else
6323             goto pop;
6324         }
6325
6326      get_rhs:
6327       tree_type = binops_by_token[token->type].tree_type;
6328
6329       /* We used the operator token.  */
6330       cp_lexer_consume_token (parser->lexer);
6331
6332       /* For "false && x" or "true || x", x will never be executed;
6333          disable warnings while evaluating it.  */
6334       if (tree_type == TRUTH_ANDIF_EXPR)
6335         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6336       else if (tree_type == TRUTH_ORIF_EXPR)
6337         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6338
6339       /* Extract another operand.  It may be the RHS of this expression
6340          or the LHS of a new, higher priority expression.  */
6341       rhs = cp_parser_simple_cast_expression (parser);
6342       rhs_type = ERROR_MARK;
6343
6344       /* Get another operator token.  Look up its precedence to avoid
6345          building a useless (immediately popped) stack entry for common
6346          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6347       token = cp_lexer_peek_token (parser->lexer);
6348       lookahead_prec = TOKEN_PRECEDENCE (token);
6349       if (lookahead_prec > new_prec)
6350         {
6351           /* ... and prepare to parse the RHS of the new, higher priority
6352              expression.  Since precedence levels on the stack are
6353              monotonically increasing, we do not have to care about
6354              stack overflows.  */
6355           sp->prec = prec;
6356           sp->tree_type = tree_type;
6357           sp->lhs = lhs;
6358           sp->lhs_type = lhs_type;
6359           sp++;
6360           lhs = rhs;
6361           lhs_type = rhs_type;
6362           prec = new_prec;
6363           new_prec = lookahead_prec;
6364           goto get_rhs;
6365
6366          pop:
6367           lookahead_prec = new_prec;
6368           /* If the stack is not empty, we have parsed into LHS the right side
6369              (`4' in the example above) of an expression we had suspended.
6370              We can use the information on the stack to recover the LHS (`3')
6371              from the stack together with the tree code (`MULT_EXPR'), and
6372              the precedence of the higher level subexpression
6373              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6374              which will be used to actually build the additive expression.  */
6375           --sp;
6376           prec = sp->prec;
6377           tree_type = sp->tree_type;
6378           rhs = lhs;
6379           rhs_type = lhs_type;
6380           lhs = sp->lhs;
6381           lhs_type = sp->lhs_type;
6382         }
6383
6384       /* Undo the disabling of warnings done above.  */
6385       if (tree_type == TRUTH_ANDIF_EXPR)
6386         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6387       else if (tree_type == TRUTH_ORIF_EXPR)
6388         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6389
6390       overloaded_p = false;
6391       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6392          ERROR_MARK for everything that is not a binary expression.
6393          This makes warn_about_parentheses miss some warnings that
6394          involve unary operators.  For unary expressions we should
6395          pass the correct tree_code unless the unary expression was
6396          surrounded by parentheses.
6397       */
6398       if (no_toplevel_fold_p
6399           && lookahead_prec <= prec
6400           && sp == stack
6401           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6402         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6403       else
6404         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6405                                  &overloaded_p, tf_warning_or_error);
6406       lhs_type = tree_type;
6407
6408       /* If the binary operator required the use of an overloaded operator,
6409          then this expression cannot be an integral constant-expression.
6410          An overloaded operator can be used even if both operands are
6411          otherwise permissible in an integral constant-expression if at
6412          least one of the operands is of enumeration type.  */
6413
6414       if (overloaded_p
6415           && (cp_parser_non_integral_constant_expression
6416               (parser, "calls to overloaded operators")))
6417         return error_mark_node;
6418     }
6419
6420   return lhs;
6421 }
6422
6423
6424 /* Parse the `? expression : assignment-expression' part of a
6425    conditional-expression.  The LOGICAL_OR_EXPR is the
6426    logical-or-expression that started the conditional-expression.
6427    Returns a representation of the entire conditional-expression.
6428
6429    This routine is used by cp_parser_assignment_expression.
6430
6431      ? expression : assignment-expression
6432
6433    GNU Extensions:
6434
6435      ? : assignment-expression */
6436
6437 static tree
6438 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6439 {
6440   tree expr;
6441   tree assignment_expr;
6442
6443   /* Consume the `?' token.  */
6444   cp_lexer_consume_token (parser->lexer);
6445   if (cp_parser_allow_gnu_extensions_p (parser)
6446       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6447     {
6448       /* Implicit true clause.  */
6449       expr = NULL_TREE;
6450       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6451     }
6452   else
6453     {
6454       /* Parse the expression.  */
6455       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6456       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6457       c_inhibit_evaluation_warnings +=
6458         ((logical_or_expr == truthvalue_true_node)
6459          - (logical_or_expr == truthvalue_false_node));
6460     }
6461
6462   /* The next token should be a `:'.  */
6463   cp_parser_require (parser, CPP_COLON, "%<:%>");
6464   /* Parse the assignment-expression.  */
6465   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6466   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6467
6468   /* Build the conditional-expression.  */
6469   return build_x_conditional_expr (logical_or_expr,
6470                                    expr,
6471                                    assignment_expr,
6472                                    tf_warning_or_error);
6473 }
6474
6475 /* Parse an assignment-expression.
6476
6477    assignment-expression:
6478      conditional-expression
6479      logical-or-expression assignment-operator assignment_expression
6480      throw-expression
6481
6482    CAST_P is true if this expression is the target of a cast.
6483
6484    Returns a representation for the expression.  */
6485
6486 static tree
6487 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6488                                  cp_id_kind * pidk)
6489 {
6490   tree expr;
6491
6492   /* If the next token is the `throw' keyword, then we're looking at
6493      a throw-expression.  */
6494   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6495     expr = cp_parser_throw_expression (parser);
6496   /* Otherwise, it must be that we are looking at a
6497      logical-or-expression.  */
6498   else
6499     {
6500       /* Parse the binary expressions (logical-or-expression).  */
6501       expr = cp_parser_binary_expression (parser, cast_p, false,
6502                                           PREC_NOT_OPERATOR, pidk);
6503       /* If the next token is a `?' then we're actually looking at a
6504          conditional-expression.  */
6505       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6506         return cp_parser_question_colon_clause (parser, expr);
6507       else
6508         {
6509           enum tree_code assignment_operator;
6510
6511           /* If it's an assignment-operator, we're using the second
6512              production.  */
6513           assignment_operator
6514             = cp_parser_assignment_operator_opt (parser);
6515           if (assignment_operator != ERROR_MARK)
6516             {
6517               bool non_constant_p;
6518
6519               /* Parse the right-hand side of the assignment.  */
6520               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6521
6522               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6523                 maybe_warn_cpp0x ("extended initializer lists");
6524
6525               /* An assignment may not appear in a
6526                  constant-expression.  */
6527               if (cp_parser_non_integral_constant_expression (parser,
6528                                                               "an assignment"))
6529                 return error_mark_node;
6530               /* Build the assignment expression.  */
6531               expr = build_x_modify_expr (expr,
6532                                           assignment_operator,
6533                                           rhs,
6534                                           tf_warning_or_error);
6535             }
6536         }
6537     }
6538
6539   return expr;
6540 }
6541
6542 /* Parse an (optional) assignment-operator.
6543
6544    assignment-operator: one of
6545      = *= /= %= += -= >>= <<= &= ^= |=
6546
6547    GNU Extension:
6548
6549    assignment-operator: one of
6550      <?= >?=
6551
6552    If the next token is an assignment operator, the corresponding tree
6553    code is returned, and the token is consumed.  For example, for
6554    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6555    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6556    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6557    operator, ERROR_MARK is returned.  */
6558
6559 static enum tree_code
6560 cp_parser_assignment_operator_opt (cp_parser* parser)
6561 {
6562   enum tree_code op;
6563   cp_token *token;
6564
6565   /* Peek at the next token.  */
6566   token = cp_lexer_peek_token (parser->lexer);
6567
6568   switch (token->type)
6569     {
6570     case CPP_EQ:
6571       op = NOP_EXPR;
6572       break;
6573
6574     case CPP_MULT_EQ:
6575       op = MULT_EXPR;
6576       break;
6577
6578     case CPP_DIV_EQ:
6579       op = TRUNC_DIV_EXPR;
6580       break;
6581
6582     case CPP_MOD_EQ:
6583       op = TRUNC_MOD_EXPR;
6584       break;
6585
6586     case CPP_PLUS_EQ:
6587       op = PLUS_EXPR;
6588       break;
6589
6590     case CPP_MINUS_EQ:
6591       op = MINUS_EXPR;
6592       break;
6593
6594     case CPP_RSHIFT_EQ:
6595       op = RSHIFT_EXPR;
6596       break;
6597
6598     case CPP_LSHIFT_EQ:
6599       op = LSHIFT_EXPR;
6600       break;
6601
6602     case CPP_AND_EQ:
6603       op = BIT_AND_EXPR;
6604       break;
6605
6606     case CPP_XOR_EQ:
6607       op = BIT_XOR_EXPR;
6608       break;
6609
6610     case CPP_OR_EQ:
6611       op = BIT_IOR_EXPR;
6612       break;
6613
6614     default:
6615       /* Nothing else is an assignment operator.  */
6616       op = ERROR_MARK;
6617     }
6618
6619   /* If it was an assignment operator, consume it.  */
6620   if (op != ERROR_MARK)
6621     cp_lexer_consume_token (parser->lexer);
6622
6623   return op;
6624 }
6625
6626 /* Parse an expression.
6627
6628    expression:
6629      assignment-expression
6630      expression , assignment-expression
6631
6632    CAST_P is true if this expression is the target of a cast.
6633
6634    Returns a representation of the expression.  */
6635
6636 static tree
6637 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6638 {
6639   tree expression = NULL_TREE;
6640
6641   while (true)
6642     {
6643       tree assignment_expression;
6644
6645       /* Parse the next assignment-expression.  */
6646       assignment_expression
6647         = cp_parser_assignment_expression (parser, cast_p, pidk);
6648       /* If this is the first assignment-expression, we can just
6649          save it away.  */
6650       if (!expression)
6651         expression = assignment_expression;
6652       else
6653         expression = build_x_compound_expr (expression,
6654                                             assignment_expression,
6655                                             tf_warning_or_error);
6656       /* If the next token is not a comma, then we are done with the
6657          expression.  */
6658       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6659         break;
6660       /* Consume the `,'.  */
6661       cp_lexer_consume_token (parser->lexer);
6662       /* A comma operator cannot appear in a constant-expression.  */
6663       if (cp_parser_non_integral_constant_expression (parser,
6664                                                       "a comma operator"))
6665         expression = error_mark_node;
6666     }
6667
6668   return expression;
6669 }
6670
6671 /* Parse a constant-expression.
6672
6673    constant-expression:
6674      conditional-expression
6675
6676   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6677   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6678   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6679   is false, NON_CONSTANT_P should be NULL.  */
6680
6681 static tree
6682 cp_parser_constant_expression (cp_parser* parser,
6683                                bool allow_non_constant_p,
6684                                bool *non_constant_p)
6685 {
6686   bool saved_integral_constant_expression_p;
6687   bool saved_allow_non_integral_constant_expression_p;
6688   bool saved_non_integral_constant_expression_p;
6689   tree expression;
6690
6691   /* It might seem that we could simply parse the
6692      conditional-expression, and then check to see if it were
6693      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6694      one that the compiler can figure out is constant, possibly after
6695      doing some simplifications or optimizations.  The standard has a
6696      precise definition of constant-expression, and we must honor
6697      that, even though it is somewhat more restrictive.
6698
6699      For example:
6700
6701        int i[(2, 3)];
6702
6703      is not a legal declaration, because `(2, 3)' is not a
6704      constant-expression.  The `,' operator is forbidden in a
6705      constant-expression.  However, GCC's constant-folding machinery
6706      will fold this operation to an INTEGER_CST for `3'.  */
6707
6708   /* Save the old settings.  */
6709   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6710   saved_allow_non_integral_constant_expression_p
6711     = parser->allow_non_integral_constant_expression_p;
6712   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6713   /* We are now parsing a constant-expression.  */
6714   parser->integral_constant_expression_p = true;
6715   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6716   parser->non_integral_constant_expression_p = false;
6717   /* Although the grammar says "conditional-expression", we parse an
6718      "assignment-expression", which also permits "throw-expression"
6719      and the use of assignment operators.  In the case that
6720      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6721      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6722      actually essential that we look for an assignment-expression.
6723      For example, cp_parser_initializer_clauses uses this function to
6724      determine whether a particular assignment-expression is in fact
6725      constant.  */
6726   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6727   /* Restore the old settings.  */
6728   parser->integral_constant_expression_p
6729     = saved_integral_constant_expression_p;
6730   parser->allow_non_integral_constant_expression_p
6731     = saved_allow_non_integral_constant_expression_p;
6732   if (allow_non_constant_p)
6733     *non_constant_p = parser->non_integral_constant_expression_p;
6734   else if (parser->non_integral_constant_expression_p)
6735     expression = error_mark_node;
6736   parser->non_integral_constant_expression_p
6737     = saved_non_integral_constant_expression_p;
6738
6739   return expression;
6740 }
6741
6742 /* Parse __builtin_offsetof.
6743
6744    offsetof-expression:
6745      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6746
6747    offsetof-member-designator:
6748      id-expression
6749      | offsetof-member-designator "." id-expression
6750      | offsetof-member-designator "[" expression "]"
6751      | offsetof-member-designator "->" id-expression  */
6752
6753 static tree
6754 cp_parser_builtin_offsetof (cp_parser *parser)
6755 {
6756   int save_ice_p, save_non_ice_p;
6757   tree type, expr;
6758   cp_id_kind dummy;
6759   cp_token *token;
6760
6761   /* We're about to accept non-integral-constant things, but will
6762      definitely yield an integral constant expression.  Save and
6763      restore these values around our local parsing.  */
6764   save_ice_p = parser->integral_constant_expression_p;
6765   save_non_ice_p = parser->non_integral_constant_expression_p;
6766
6767   /* Consume the "__builtin_offsetof" token.  */
6768   cp_lexer_consume_token (parser->lexer);
6769   /* Consume the opening `('.  */
6770   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6771   /* Parse the type-id.  */
6772   type = cp_parser_type_id (parser);
6773   /* Look for the `,'.  */
6774   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6775   token = cp_lexer_peek_token (parser->lexer);
6776
6777   /* Build the (type *)null that begins the traditional offsetof macro.  */
6778   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6779                             tf_warning_or_error);
6780
6781   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6782   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6783                                                  true, &dummy, token->location);
6784   while (true)
6785     {
6786       token = cp_lexer_peek_token (parser->lexer);
6787       switch (token->type)
6788         {
6789         case CPP_OPEN_SQUARE:
6790           /* offsetof-member-designator "[" expression "]" */
6791           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6792           break;
6793
6794         case CPP_DEREF:
6795           /* offsetof-member-designator "->" identifier */
6796           expr = grok_array_decl (expr, integer_zero_node);
6797           /* FALLTHRU */
6798
6799         case CPP_DOT:
6800           /* offsetof-member-designator "." identifier */
6801           cp_lexer_consume_token (parser->lexer);
6802           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6803                                                          expr, true, &dummy,
6804                                                          token->location);
6805           break;
6806
6807         case CPP_CLOSE_PAREN:
6808           /* Consume the ")" token.  */
6809           cp_lexer_consume_token (parser->lexer);
6810           goto success;
6811
6812         default:
6813           /* Error.  We know the following require will fail, but
6814              that gives the proper error message.  */
6815           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6816           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6817           expr = error_mark_node;
6818           goto failure;
6819         }
6820     }
6821
6822  success:
6823   /* If we're processing a template, we can't finish the semantics yet.
6824      Otherwise we can fold the entire expression now.  */
6825   if (processing_template_decl)
6826     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6827   else
6828     expr = finish_offsetof (expr);
6829
6830  failure:
6831   parser->integral_constant_expression_p = save_ice_p;
6832   parser->non_integral_constant_expression_p = save_non_ice_p;
6833
6834   return expr;
6835 }
6836
6837 /* Parse a trait expression.  */
6838
6839 static tree
6840 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6841 {
6842   cp_trait_kind kind;
6843   tree type1, type2 = NULL_TREE;
6844   bool binary = false;
6845   cp_decl_specifier_seq decl_specs;
6846
6847   switch (keyword)
6848     {
6849     case RID_HAS_NOTHROW_ASSIGN:
6850       kind = CPTK_HAS_NOTHROW_ASSIGN;
6851       break;
6852     case RID_HAS_NOTHROW_CONSTRUCTOR:
6853       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6854       break;
6855     case RID_HAS_NOTHROW_COPY:
6856       kind = CPTK_HAS_NOTHROW_COPY;
6857       break;
6858     case RID_HAS_TRIVIAL_ASSIGN:
6859       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6860       break;
6861     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6862       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6863       break;
6864     case RID_HAS_TRIVIAL_COPY:
6865       kind = CPTK_HAS_TRIVIAL_COPY;
6866       break;
6867     case RID_HAS_TRIVIAL_DESTRUCTOR:
6868       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6869       break;
6870     case RID_HAS_VIRTUAL_DESTRUCTOR:
6871       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6872       break;
6873     case RID_IS_ABSTRACT:
6874       kind = CPTK_IS_ABSTRACT;
6875       break;
6876     case RID_IS_BASE_OF:
6877       kind = CPTK_IS_BASE_OF;
6878       binary = true;
6879       break;
6880     case RID_IS_CLASS:
6881       kind = CPTK_IS_CLASS;
6882       break;
6883     case RID_IS_CONVERTIBLE_TO:
6884       kind = CPTK_IS_CONVERTIBLE_TO;
6885       binary = true;
6886       break;
6887     case RID_IS_EMPTY:
6888       kind = CPTK_IS_EMPTY;
6889       break;
6890     case RID_IS_ENUM:
6891       kind = CPTK_IS_ENUM;
6892       break;
6893     case RID_IS_POD:
6894       kind = CPTK_IS_POD;
6895       break;
6896     case RID_IS_POLYMORPHIC:
6897       kind = CPTK_IS_POLYMORPHIC;
6898       break;
6899     case RID_IS_STD_LAYOUT:
6900       kind = CPTK_IS_STD_LAYOUT;
6901       break;
6902     case RID_IS_TRIVIAL:
6903       kind = CPTK_IS_TRIVIAL;
6904       break;
6905     case RID_IS_UNION:
6906       kind = CPTK_IS_UNION;
6907       break;
6908     default:
6909       gcc_unreachable ();
6910     }
6911
6912   /* Consume the token.  */
6913   cp_lexer_consume_token (parser->lexer);
6914
6915   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6916
6917   type1 = cp_parser_type_id (parser);
6918
6919   if (type1 == error_mark_node)
6920     return error_mark_node;
6921
6922   /* Build a trivial decl-specifier-seq.  */
6923   clear_decl_specs (&decl_specs);
6924   decl_specs.type = type1;
6925
6926   /* Call grokdeclarator to figure out what type this is.  */
6927   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6928                           /*initialized=*/0, /*attrlist=*/NULL);
6929
6930   if (binary)
6931     {
6932       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6933  
6934       type2 = cp_parser_type_id (parser);
6935
6936       if (type2 == error_mark_node)
6937         return error_mark_node;
6938
6939       /* Build a trivial decl-specifier-seq.  */
6940       clear_decl_specs (&decl_specs);
6941       decl_specs.type = type2;
6942
6943       /* Call grokdeclarator to figure out what type this is.  */
6944       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6945                               /*initialized=*/0, /*attrlist=*/NULL);
6946     }
6947
6948   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6949
6950   /* Complete the trait expression, which may mean either processing
6951      the trait expr now or saving it for template instantiation.  */
6952   return finish_trait_expr (kind, type1, type2);
6953 }
6954
6955 /* Lambdas that appear in variable initializer or default argument scope
6956    get that in their mangling, so we need to record it.  We might as well
6957    use the count for function and namespace scopes as well.  */
6958 static tree lambda_scope;
6959 static int lambda_count;
6960 typedef struct GTY(()) tree_int
6961 {
6962   tree t;
6963   int i;
6964 } tree_int;
6965 DEF_VEC_O(tree_int);
6966 DEF_VEC_ALLOC_O(tree_int,gc);
6967 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6968
6969 static void
6970 start_lambda_scope (tree decl)
6971 {
6972   tree_int ti;
6973   gcc_assert (decl);
6974   /* Once we're inside a function, we ignore other scopes and just push
6975      the function again so that popping works properly.  */
6976   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6977     decl = current_function_decl;
6978   ti.t = lambda_scope;
6979   ti.i = lambda_count;
6980   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6981   if (lambda_scope != decl)
6982     {
6983       /* Don't reset the count if we're still in the same function.  */
6984       lambda_scope = decl;
6985       lambda_count = 0;
6986     }
6987 }
6988
6989 static void
6990 record_lambda_scope (tree lambda)
6991 {
6992   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
6993   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
6994 }
6995
6996 static void
6997 finish_lambda_scope (void)
6998 {
6999   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7000   if (lambda_scope != p->t)
7001     {
7002       lambda_scope = p->t;
7003       lambda_count = p->i;
7004     }
7005   VEC_pop (tree_int, lambda_scope_stack);
7006 }
7007
7008 /* Parse a lambda expression.
7009
7010    lambda-expression:
7011      lambda-introducer lambda-declarator [opt] compound-statement
7012
7013    Returns a representation of the expression.  */
7014
7015 static tree
7016 cp_parser_lambda_expression (cp_parser* parser)
7017 {
7018   tree lambda_expr = build_lambda_expr ();
7019   tree type;
7020
7021   LAMBDA_EXPR_LOCATION (lambda_expr)
7022     = cp_lexer_peek_token (parser->lexer)->location;
7023
7024   /* We may be in the middle of deferred access check.  Disable
7025      it now.  */
7026   push_deferring_access_checks (dk_no_deferred);
7027
7028   type = begin_lambda_type (lambda_expr);
7029
7030   record_lambda_scope (lambda_expr);
7031
7032   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7033   determine_visibility (TYPE_NAME (type));
7034
7035   {
7036     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7037     unsigned int saved_num_template_parameter_lists
7038         = parser->num_template_parameter_lists;
7039
7040     parser->num_template_parameter_lists = 0;
7041
7042     cp_parser_lambda_introducer (parser, lambda_expr);
7043
7044     /* By virtue of defining a local class, a lambda expression has access to
7045        the private variables of enclosing classes.  */
7046
7047     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7048
7049     cp_parser_lambda_body (parser, lambda_expr);
7050
7051     /* The capture list was built up in reverse order; fix that now.  */
7052     {
7053       tree newlist = NULL_TREE;
7054       tree elt, next;
7055
7056       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7057            elt; elt = next)
7058         {
7059           tree field = TREE_PURPOSE (elt);
7060           char *buf;
7061
7062           next = TREE_CHAIN (elt);
7063           TREE_CHAIN (elt) = newlist;
7064           newlist = elt;
7065
7066           /* Also add __ to the beginning of the field name so that code
7067              outside the lambda body can't see the captured name.  We could
7068              just remove the name entirely, but this is more useful for
7069              debugging.  */
7070           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7071             /* The 'this' capture already starts with __.  */
7072             continue;
7073
7074           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7075           buf[1] = buf[0] = '_';
7076           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7077                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7078           DECL_NAME (field) = get_identifier (buf);
7079         }
7080       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7081     }
7082
7083     type = finish_struct (type, /*attributes=*/NULL_TREE);
7084
7085     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7086   }
7087
7088   pop_deferring_access_checks ();
7089
7090   return build_lambda_object (lambda_expr);
7091 }
7092
7093 /* Parse the beginning of a lambda expression.
7094
7095    lambda-introducer:
7096      [ lambda-capture [opt] ]
7097
7098    LAMBDA_EXPR is the current representation of the lambda expression.  */
7099
7100 static void
7101 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7102 {
7103   /* Need commas after the first capture.  */
7104   bool first = true;
7105
7106   /* Eat the leading `['.  */
7107   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7108
7109   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7110   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7111       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7112     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7113   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7114     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7115
7116   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7117     {
7118       cp_lexer_consume_token (parser->lexer);
7119       first = false;
7120     }
7121
7122   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7123     {
7124       cp_token* capture_token;
7125       tree capture_id;
7126       tree capture_init_expr;
7127       cp_id_kind idk = CP_ID_KIND_NONE;
7128
7129       enum capture_kind_type
7130       {
7131         BY_COPY,
7132         BY_REFERENCE
7133       };
7134       enum capture_kind_type capture_kind = BY_COPY;
7135
7136       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7137         {
7138           error ("expected end of capture-list");
7139           return;
7140         }
7141
7142       if (first)
7143         first = false;
7144       else
7145         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7146
7147       /* Possibly capture `this'.  */
7148       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7149         {
7150           cp_lexer_consume_token (parser->lexer);
7151           add_capture (lambda_expr,
7152                        /*id=*/get_identifier ("__this"),
7153                        /*initializer=*/finish_this_expr(),
7154                        /*by_reference_p=*/false);
7155           continue;
7156         }
7157
7158       /* Remember whether we want to capture as a reference or not.  */
7159       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7160         {
7161           capture_kind = BY_REFERENCE;
7162           cp_lexer_consume_token (parser->lexer);
7163         }
7164
7165       /* Get the identifier.  */
7166       capture_token = cp_lexer_peek_token (parser->lexer);
7167       capture_id = cp_parser_identifier (parser);
7168
7169       if (capture_id == error_mark_node)
7170         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7171            delimiters, but I modified this to stop on unnested ']' as well.  It
7172            was already changed to stop on unnested '}', so the
7173            "closing_parenthesis" name is no more misleading with my change.  */
7174         {
7175           cp_parser_skip_to_closing_parenthesis (parser,
7176                                                  /*recovering=*/true,
7177                                                  /*or_comma=*/true,
7178                                                  /*consume_paren=*/true);
7179           continue;
7180         }
7181
7182       /* Find the initializer for this capture.  */
7183       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7184         {
7185           /* An explicit expression exists.  */
7186           cp_lexer_consume_token (parser->lexer);
7187           pedwarn (input_location, OPT_pedantic,
7188                    "ISO C++ does not allow initializers "
7189                    "in lambda expression capture lists");
7190           capture_init_expr = cp_parser_assignment_expression (parser,
7191                                                                /*cast_p=*/true,
7192                                                                &idk);
7193         }
7194       else
7195         {
7196           const char* error_msg;
7197
7198           /* Turn the identifier into an id-expression.  */
7199           capture_init_expr
7200             = cp_parser_lookup_name
7201                 (parser,
7202                  capture_id,
7203                  none_type,
7204                  /*is_template=*/false,
7205                  /*is_namespace=*/false,
7206                  /*check_dependency=*/true,
7207                  /*ambiguous_decls=*/NULL,
7208                  capture_token->location);
7209
7210           capture_init_expr
7211             = finish_id_expression
7212                 (capture_id,
7213                  capture_init_expr,
7214                  parser->scope,
7215                  &idk,
7216                  /*integral_constant_expression_p=*/false,
7217                  /*allow_non_integral_constant_expression_p=*/false,
7218                  /*non_integral_constant_expression_p=*/NULL,
7219                  /*template_p=*/false,
7220                  /*done=*/true,
7221                  /*address_p=*/false,
7222                  /*template_arg_p=*/false,
7223                  &error_msg,
7224                  capture_token->location);
7225         }
7226
7227       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7228         capture_init_expr
7229           = unqualified_name_lookup_error (capture_init_expr);
7230
7231       add_capture (lambda_expr,
7232                    capture_id,
7233                    capture_init_expr,
7234                    /*by_reference_p=*/capture_kind == BY_REFERENCE);
7235     }
7236
7237   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7238 }
7239
7240 /* Parse the (optional) middle of a lambda expression.
7241
7242    lambda-declarator:
7243      ( parameter-declaration-clause [opt] )
7244        attribute-specifier [opt]
7245        mutable [opt]
7246        exception-specification [opt]
7247        lambda-return-type-clause [opt]
7248
7249    LAMBDA_EXPR is the current representation of the lambda expression.  */
7250
7251 static void
7252 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7253 {
7254   /* 5.1.1.4 of the standard says:
7255        If a lambda-expression does not include a lambda-declarator, it is as if
7256        the lambda-declarator were ().
7257      This means an empty parameter list, no attributes, and no exception
7258      specification.  */
7259   tree param_list = void_list_node;
7260   tree attributes = NULL_TREE;
7261   tree exception_spec = NULL_TREE;
7262   tree t;
7263
7264   /* The lambda-declarator is optional, but must begin with an opening
7265      parenthesis if present.  */
7266   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7267     {
7268       cp_lexer_consume_token (parser->lexer);
7269
7270       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7271
7272       /* Parse parameters.  */
7273       param_list = cp_parser_parameter_declaration_clause (parser);
7274
7275       /* Default arguments shall not be specified in the
7276          parameter-declaration-clause of a lambda-declarator.  */
7277       for (t = param_list; t; t = TREE_CHAIN (t))
7278         if (TREE_PURPOSE (t))
7279           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7280                    "default argument specified for lambda parameter");
7281
7282       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7283
7284       attributes = cp_parser_attributes_opt (parser);
7285
7286       /* Parse optional `mutable' keyword.  */
7287       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7288         {
7289           cp_lexer_consume_token (parser->lexer);
7290           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7291         }
7292
7293       /* Parse optional exception specification.  */
7294       exception_spec = cp_parser_exception_specification_opt (parser);
7295
7296       /* Parse optional trailing return type.  */
7297       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7298         {
7299           cp_lexer_consume_token (parser->lexer);
7300           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7301         }
7302
7303       /* The function parameters must be in scope all the way until after the
7304          trailing-return-type in case of decltype.  */
7305       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7306         pop_binding (DECL_NAME (t), t);
7307
7308       leave_scope ();
7309     }
7310
7311   /* Create the function call operator.
7312
7313      Messing with declarators like this is no uglier than building up the
7314      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7315      other code.  */
7316   {
7317     cp_decl_specifier_seq return_type_specs;
7318     cp_declarator* declarator;
7319     tree fco;
7320     int quals;
7321     void *p;
7322
7323     clear_decl_specs (&return_type_specs);
7324     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7325       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7326     else
7327       /* Maybe we will deduce the return type later, but we can use void
7328          as a placeholder return type anyways.  */
7329       return_type_specs.type = void_type_node;
7330
7331     p = obstack_alloc (&declarator_obstack, 0);
7332
7333     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7334                                      sfk_none);
7335
7336     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7337              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7338     declarator = make_call_declarator (declarator, param_list, quals,
7339                                        exception_spec,
7340                                        /*late_return_type=*/NULL_TREE);
7341
7342     fco = grokmethod (&return_type_specs,
7343                         declarator,
7344                         attributes);
7345     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7346     DECL_ARTIFICIAL (fco) = 1;
7347
7348     finish_member_declaration (fco);
7349
7350     obstack_free (&declarator_obstack, p);
7351   }
7352 }
7353
7354 /* Parse the body of a lambda expression, which is simply
7355
7356    compound-statement
7357
7358    but which requires special handling.
7359    LAMBDA_EXPR is the current representation of the lambda expression.  */
7360
7361 static void
7362 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7363 {
7364   bool nested = (current_function_decl != NULL_TREE);
7365   if (nested)
7366     push_function_context ();
7367
7368   /* Finish the function call operator
7369      - class_specifier
7370      + late_parsing_for_member
7371      + function_definition_after_declarator
7372      + ctor_initializer_opt_and_function_body  */
7373   {
7374     tree fco = lambda_function (lambda_expr);
7375     tree body;
7376     bool done = false;
7377
7378     /* Let the front end know that we are going to be defining this
7379        function.  */
7380     start_preparsed_function (fco,
7381                               NULL_TREE,
7382                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7383
7384     start_lambda_scope (fco);
7385     body = begin_function_body ();
7386
7387     /* 5.1.1.4 of the standard says:
7388          If a lambda-expression does not include a trailing-return-type, it
7389          is as if the trailing-return-type denotes the following type:
7390           * if the compound-statement is of the form
7391                { return attribute-specifier [opt] expression ; }
7392              the type of the returned expression after lvalue-to-rvalue
7393              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7394              (_conv.array_ 4.2), and function-to-pointer conversion
7395              (_conv.func_ 4.3);
7396           * otherwise, void.  */
7397
7398     /* In a lambda that has neither a lambda-return-type-clause
7399        nor a deducible form, errors should be reported for return statements
7400        in the body.  Since we used void as the placeholder return type, parsing
7401        the body as usual will give such desired behavior.  */
7402     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7403         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7404         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7405         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7406       {
7407         tree compound_stmt;
7408         tree expr = NULL_TREE;
7409         cp_id_kind idk = CP_ID_KIND_NONE;
7410
7411         /* Parse tentatively in case there's more after the initial return
7412            statement.  */
7413         cp_parser_parse_tentatively (parser);
7414
7415         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7416         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7417
7418         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7419
7420         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7421         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7422
7423         if (cp_parser_parse_definitely (parser))
7424           {
7425             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7426
7427             compound_stmt = begin_compound_stmt (0);
7428             /* Will get error here if type not deduced yet.  */
7429             finish_return_stmt (expr);
7430             finish_compound_stmt (compound_stmt);
7431
7432             done = true;
7433           }
7434       }
7435
7436     if (!done)
7437       {
7438         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7439           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7440         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7441            cp_parser_compound_stmt does not pass it.  */
7442         cp_parser_function_body (parser);
7443         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7444       }
7445
7446     finish_function_body (body);
7447     finish_lambda_scope ();
7448
7449     /* Finish the function and generate code for it if necessary.  */
7450     expand_or_defer_fn (finish_function (/*inline*/2));
7451   }
7452
7453   if (nested)
7454     pop_function_context();
7455 }
7456
7457 /* Statements [gram.stmt.stmt]  */
7458
7459 /* Parse a statement.
7460
7461    statement:
7462      labeled-statement
7463      expression-statement
7464      compound-statement
7465      selection-statement
7466      iteration-statement
7467      jump-statement
7468      declaration-statement
7469      try-block
7470
7471   IN_COMPOUND is true when the statement is nested inside a
7472   cp_parser_compound_statement; this matters for certain pragmas.
7473
7474   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7475   is a (possibly labeled) if statement which is not enclosed in braces
7476   and has an else clause.  This is used to implement -Wparentheses.  */
7477
7478 static void
7479 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7480                      bool in_compound, bool *if_p)
7481 {
7482   tree statement;
7483   cp_token *token;
7484   location_t statement_location;
7485
7486  restart:
7487   if (if_p != NULL)
7488     *if_p = false;
7489   /* There is no statement yet.  */
7490   statement = NULL_TREE;
7491   /* Peek at the next token.  */
7492   token = cp_lexer_peek_token (parser->lexer);
7493   /* Remember the location of the first token in the statement.  */
7494   statement_location = token->location;
7495   /* If this is a keyword, then that will often determine what kind of
7496      statement we have.  */
7497   if (token->type == CPP_KEYWORD)
7498     {
7499       enum rid keyword = token->keyword;
7500
7501       switch (keyword)
7502         {
7503         case RID_CASE:
7504         case RID_DEFAULT:
7505           /* Looks like a labeled-statement with a case label.
7506              Parse the label, and then use tail recursion to parse
7507              the statement.  */
7508           cp_parser_label_for_labeled_statement (parser);
7509           goto restart;
7510
7511         case RID_IF:
7512         case RID_SWITCH:
7513           statement = cp_parser_selection_statement (parser, if_p);
7514           break;
7515
7516         case RID_WHILE:
7517         case RID_DO:
7518         case RID_FOR:
7519           statement = cp_parser_iteration_statement (parser);
7520           break;
7521
7522         case RID_BREAK:
7523         case RID_CONTINUE:
7524         case RID_RETURN:
7525         case RID_GOTO:
7526           statement = cp_parser_jump_statement (parser);
7527           break;
7528
7529           /* Objective-C++ exception-handling constructs.  */
7530         case RID_AT_TRY:
7531         case RID_AT_CATCH:
7532         case RID_AT_FINALLY:
7533         case RID_AT_SYNCHRONIZED:
7534         case RID_AT_THROW:
7535           statement = cp_parser_objc_statement (parser);
7536           break;
7537
7538         case RID_TRY:
7539           statement = cp_parser_try_block (parser);
7540           break;
7541
7542         case RID_NAMESPACE:
7543           /* This must be a namespace alias definition.  */
7544           cp_parser_declaration_statement (parser);
7545           return;
7546           
7547         default:
7548           /* It might be a keyword like `int' that can start a
7549              declaration-statement.  */
7550           break;
7551         }
7552     }
7553   else if (token->type == CPP_NAME)
7554     {
7555       /* If the next token is a `:', then we are looking at a
7556          labeled-statement.  */
7557       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7558       if (token->type == CPP_COLON)
7559         {
7560           /* Looks like a labeled-statement with an ordinary label.
7561              Parse the label, and then use tail recursion to parse
7562              the statement.  */
7563           cp_parser_label_for_labeled_statement (parser);
7564           goto restart;
7565         }
7566     }
7567   /* Anything that starts with a `{' must be a compound-statement.  */
7568   else if (token->type == CPP_OPEN_BRACE)
7569     statement = cp_parser_compound_statement (parser, NULL, false);
7570   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7571      a statement all its own.  */
7572   else if (token->type == CPP_PRAGMA)
7573     {
7574       /* Only certain OpenMP pragmas are attached to statements, and thus
7575          are considered statements themselves.  All others are not.  In
7576          the context of a compound, accept the pragma as a "statement" and
7577          return so that we can check for a close brace.  Otherwise we
7578          require a real statement and must go back and read one.  */
7579       if (in_compound)
7580         cp_parser_pragma (parser, pragma_compound);
7581       else if (!cp_parser_pragma (parser, pragma_stmt))
7582         goto restart;
7583       return;
7584     }
7585   else if (token->type == CPP_EOF)
7586     {
7587       cp_parser_error (parser, "expected statement");
7588       return;
7589     }
7590
7591   /* Everything else must be a declaration-statement or an
7592      expression-statement.  Try for the declaration-statement
7593      first, unless we are looking at a `;', in which case we know that
7594      we have an expression-statement.  */
7595   if (!statement)
7596     {
7597       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7598         {
7599           cp_parser_parse_tentatively (parser);
7600           /* Try to parse the declaration-statement.  */
7601           cp_parser_declaration_statement (parser);
7602           /* If that worked, we're done.  */
7603           if (cp_parser_parse_definitely (parser))
7604             return;
7605         }
7606       /* Look for an expression-statement instead.  */
7607       statement = cp_parser_expression_statement (parser, in_statement_expr);
7608     }
7609
7610   /* Set the line number for the statement.  */
7611   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7612     SET_EXPR_LOCATION (statement, statement_location);
7613 }
7614
7615 /* Parse the label for a labeled-statement, i.e.
7616
7617    identifier :
7618    case constant-expression :
7619    default :
7620
7621    GNU Extension:
7622    case constant-expression ... constant-expression : statement
7623
7624    When a label is parsed without errors, the label is added to the
7625    parse tree by the finish_* functions, so this function doesn't
7626    have to return the label.  */
7627
7628 static void
7629 cp_parser_label_for_labeled_statement (cp_parser* parser)
7630 {
7631   cp_token *token;
7632   tree label = NULL_TREE;
7633
7634   /* The next token should be an identifier.  */
7635   token = cp_lexer_peek_token (parser->lexer);
7636   if (token->type != CPP_NAME
7637       && token->type != CPP_KEYWORD)
7638     {
7639       cp_parser_error (parser, "expected labeled-statement");
7640       return;
7641     }
7642
7643   switch (token->keyword)
7644     {
7645     case RID_CASE:
7646       {
7647         tree expr, expr_hi;
7648         cp_token *ellipsis;
7649
7650         /* Consume the `case' token.  */
7651         cp_lexer_consume_token (parser->lexer);
7652         /* Parse the constant-expression.  */
7653         expr = cp_parser_constant_expression (parser,
7654                                               /*allow_non_constant_p=*/false,
7655                                               NULL);
7656
7657         ellipsis = cp_lexer_peek_token (parser->lexer);
7658         if (ellipsis->type == CPP_ELLIPSIS)
7659           {
7660             /* Consume the `...' token.  */
7661             cp_lexer_consume_token (parser->lexer);
7662             expr_hi =
7663               cp_parser_constant_expression (parser,
7664                                              /*allow_non_constant_p=*/false,
7665                                              NULL);
7666             /* We don't need to emit warnings here, as the common code
7667                will do this for us.  */
7668           }
7669         else
7670           expr_hi = NULL_TREE;
7671
7672         if (parser->in_switch_statement_p)
7673           finish_case_label (token->location, expr, expr_hi);
7674         else
7675           error_at (token->location,
7676                     "case label %qE not within a switch statement",
7677                     expr);
7678       }
7679       break;
7680
7681     case RID_DEFAULT:
7682       /* Consume the `default' token.  */
7683       cp_lexer_consume_token (parser->lexer);
7684
7685       if (parser->in_switch_statement_p)
7686         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7687       else
7688         error_at (token->location, "case label not within a switch statement");
7689       break;
7690
7691     default:
7692       /* Anything else must be an ordinary label.  */
7693       label = finish_label_stmt (cp_parser_identifier (parser));
7694       break;
7695     }
7696
7697   /* Require the `:' token.  */
7698   cp_parser_require (parser, CPP_COLON, "%<:%>");
7699
7700   /* An ordinary label may optionally be followed by attributes.
7701      However, this is only permitted if the attributes are then
7702      followed by a semicolon.  This is because, for backward
7703      compatibility, when parsing
7704        lab: __attribute__ ((unused)) int i;
7705      we want the attribute to attach to "i", not "lab".  */
7706   if (label != NULL_TREE
7707       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7708     {
7709       tree attrs;
7710
7711       cp_parser_parse_tentatively (parser);
7712       attrs = cp_parser_attributes_opt (parser);
7713       if (attrs == NULL_TREE
7714           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7715         cp_parser_abort_tentative_parse (parser);
7716       else if (!cp_parser_parse_definitely (parser))
7717         ;
7718       else
7719         cplus_decl_attributes (&label, attrs, 0);
7720     }
7721 }
7722
7723 /* Parse an expression-statement.
7724
7725    expression-statement:
7726      expression [opt] ;
7727
7728    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7729    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7730    indicates whether this expression-statement is part of an
7731    expression statement.  */
7732
7733 static tree
7734 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7735 {
7736   tree statement = NULL_TREE;
7737
7738   /* If the next token is a ';', then there is no expression
7739      statement.  */
7740   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7741     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7742
7743   /* Consume the final `;'.  */
7744   cp_parser_consume_semicolon_at_end_of_statement (parser);
7745
7746   if (in_statement_expr
7747       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7748     /* This is the final expression statement of a statement
7749        expression.  */
7750     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7751   else if (statement)
7752     statement = finish_expr_stmt (statement);
7753   else
7754     finish_stmt ();
7755
7756   return statement;
7757 }
7758
7759 /* Parse a compound-statement.
7760
7761    compound-statement:
7762      { statement-seq [opt] }
7763
7764    GNU extension:
7765
7766    compound-statement:
7767      { label-declaration-seq [opt] statement-seq [opt] }
7768
7769    label-declaration-seq:
7770      label-declaration
7771      label-declaration-seq label-declaration
7772
7773    Returns a tree representing the statement.  */
7774
7775 static tree
7776 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7777                               bool in_try)
7778 {
7779   tree compound_stmt;
7780
7781   /* Consume the `{'.  */
7782   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7783     return error_mark_node;
7784   /* Begin the compound-statement.  */
7785   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7786   /* If the next keyword is `__label__' we have a label declaration.  */
7787   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7788     cp_parser_label_declaration (parser);
7789   /* Parse an (optional) statement-seq.  */
7790   cp_parser_statement_seq_opt (parser, in_statement_expr);
7791   /* Finish the compound-statement.  */
7792   finish_compound_stmt (compound_stmt);
7793   /* Consume the `}'.  */
7794   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7795
7796   return compound_stmt;
7797 }
7798
7799 /* Parse an (optional) statement-seq.
7800
7801    statement-seq:
7802      statement
7803      statement-seq [opt] statement  */
7804
7805 static void
7806 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7807 {
7808   /* Scan statements until there aren't any more.  */
7809   while (true)
7810     {
7811       cp_token *token = cp_lexer_peek_token (parser->lexer);
7812
7813       /* If we're looking at a `}', then we've run out of statements.  */
7814       if (token->type == CPP_CLOSE_BRACE
7815           || token->type == CPP_EOF
7816           || token->type == CPP_PRAGMA_EOL)
7817         break;
7818       
7819       /* If we are in a compound statement and find 'else' then
7820          something went wrong.  */
7821       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7822         {
7823           if (parser->in_statement & IN_IF_STMT) 
7824             break;
7825           else
7826             {
7827               token = cp_lexer_consume_token (parser->lexer);
7828               error_at (token->location, "%<else%> without a previous %<if%>");
7829             }
7830         }
7831
7832       /* Parse the statement.  */
7833       cp_parser_statement (parser, in_statement_expr, true, NULL);
7834     }
7835 }
7836
7837 /* Parse a selection-statement.
7838
7839    selection-statement:
7840      if ( condition ) statement
7841      if ( condition ) statement else statement
7842      switch ( condition ) statement
7843
7844    Returns the new IF_STMT or SWITCH_STMT.
7845
7846    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7847    is a (possibly labeled) if statement which is not enclosed in
7848    braces and has an else clause.  This is used to implement
7849    -Wparentheses.  */
7850
7851 static tree
7852 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7853 {
7854   cp_token *token;
7855   enum rid keyword;
7856
7857   if (if_p != NULL)
7858     *if_p = false;
7859
7860   /* Peek at the next token.  */
7861   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7862
7863   /* See what kind of keyword it is.  */
7864   keyword = token->keyword;
7865   switch (keyword)
7866     {
7867     case RID_IF:
7868     case RID_SWITCH:
7869       {
7870         tree statement;
7871         tree condition;
7872
7873         /* Look for the `('.  */
7874         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7875           {
7876             cp_parser_skip_to_end_of_statement (parser);
7877             return error_mark_node;
7878           }
7879
7880         /* Begin the selection-statement.  */
7881         if (keyword == RID_IF)
7882           statement = begin_if_stmt ();
7883         else
7884           statement = begin_switch_stmt ();
7885
7886         /* Parse the condition.  */
7887         condition = cp_parser_condition (parser);
7888         /* Look for the `)'.  */
7889         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7890           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7891                                                  /*consume_paren=*/true);
7892
7893         if (keyword == RID_IF)
7894           {
7895             bool nested_if;
7896             unsigned char in_statement;
7897
7898             /* Add the condition.  */
7899             finish_if_stmt_cond (condition, statement);
7900
7901             /* Parse the then-clause.  */
7902             in_statement = parser->in_statement;
7903             parser->in_statement |= IN_IF_STMT;
7904             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7905               {
7906                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7907                 add_stmt (build_empty_stmt (loc));
7908                 cp_lexer_consume_token (parser->lexer);
7909                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7910                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7911                               "empty body in an %<if%> statement");
7912                 nested_if = false;
7913               }
7914             else
7915               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7916             parser->in_statement = in_statement;
7917
7918             finish_then_clause (statement);
7919
7920             /* If the next token is `else', parse the else-clause.  */
7921             if (cp_lexer_next_token_is_keyword (parser->lexer,
7922                                                 RID_ELSE))
7923               {
7924                 /* Consume the `else' keyword.  */
7925                 cp_lexer_consume_token (parser->lexer);
7926                 begin_else_clause (statement);
7927                 /* Parse the else-clause.  */
7928                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7929                   {
7930                     location_t loc;
7931                     loc = cp_lexer_peek_token (parser->lexer)->location;
7932                     warning_at (loc,
7933                                 OPT_Wempty_body, "suggest braces around "
7934                                 "empty body in an %<else%> statement");
7935                     add_stmt (build_empty_stmt (loc));
7936                     cp_lexer_consume_token (parser->lexer);
7937                   }
7938                 else
7939                   cp_parser_implicitly_scoped_statement (parser, NULL);
7940
7941                 finish_else_clause (statement);
7942
7943                 /* If we are currently parsing a then-clause, then
7944                    IF_P will not be NULL.  We set it to true to
7945                    indicate that this if statement has an else clause.
7946                    This may trigger the Wparentheses warning below
7947                    when we get back up to the parent if statement.  */
7948                 if (if_p != NULL)
7949                   *if_p = true;
7950               }
7951             else
7952               {
7953                 /* This if statement does not have an else clause.  If
7954                    NESTED_IF is true, then the then-clause is an if
7955                    statement which does have an else clause.  We warn
7956                    about the potential ambiguity.  */
7957                 if (nested_if)
7958                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7959                               "suggest explicit braces to avoid ambiguous"
7960                               " %<else%>");
7961               }
7962
7963             /* Now we're all done with the if-statement.  */
7964             finish_if_stmt (statement);
7965           }
7966         else
7967           {
7968             bool in_switch_statement_p;
7969             unsigned char in_statement;
7970
7971             /* Add the condition.  */
7972             finish_switch_cond (condition, statement);
7973
7974             /* Parse the body of the switch-statement.  */
7975             in_switch_statement_p = parser->in_switch_statement_p;
7976             in_statement = parser->in_statement;
7977             parser->in_switch_statement_p = true;
7978             parser->in_statement |= IN_SWITCH_STMT;
7979             cp_parser_implicitly_scoped_statement (parser, NULL);
7980             parser->in_switch_statement_p = in_switch_statement_p;
7981             parser->in_statement = in_statement;
7982
7983             /* Now we're all done with the switch-statement.  */
7984             finish_switch_stmt (statement);
7985           }
7986
7987         return statement;
7988       }
7989       break;
7990
7991     default:
7992       cp_parser_error (parser, "expected selection-statement");
7993       return error_mark_node;
7994     }
7995 }
7996
7997 /* Parse a condition.
7998
7999    condition:
8000      expression
8001      type-specifier-seq declarator = initializer-clause
8002      type-specifier-seq declarator braced-init-list
8003
8004    GNU Extension:
8005
8006    condition:
8007      type-specifier-seq declarator asm-specification [opt]
8008        attributes [opt] = assignment-expression
8009
8010    Returns the expression that should be tested.  */
8011
8012 static tree
8013 cp_parser_condition (cp_parser* parser)
8014 {
8015   cp_decl_specifier_seq type_specifiers;
8016   const char *saved_message;
8017
8018   /* Try the declaration first.  */
8019   cp_parser_parse_tentatively (parser);
8020   /* New types are not allowed in the type-specifier-seq for a
8021      condition.  */
8022   saved_message = parser->type_definition_forbidden_message;
8023   parser->type_definition_forbidden_message
8024     = "types may not be defined in conditions";
8025   /* Parse the type-specifier-seq.  */
8026   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8027                                 &type_specifiers);
8028   /* Restore the saved message.  */
8029   parser->type_definition_forbidden_message = saved_message;
8030   /* If all is well, we might be looking at a declaration.  */
8031   if (!cp_parser_error_occurred (parser))
8032     {
8033       tree decl;
8034       tree asm_specification;
8035       tree attributes;
8036       cp_declarator *declarator;
8037       tree initializer = NULL_TREE;
8038
8039       /* Parse the declarator.  */
8040       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8041                                          /*ctor_dtor_or_conv_p=*/NULL,
8042                                          /*parenthesized_p=*/NULL,
8043                                          /*member_p=*/false);
8044       /* Parse the attributes.  */
8045       attributes = cp_parser_attributes_opt (parser);
8046       /* Parse the asm-specification.  */
8047       asm_specification = cp_parser_asm_specification_opt (parser);
8048       /* If the next token is not an `=' or '{', then we might still be
8049          looking at an expression.  For example:
8050
8051            if (A(a).x)
8052
8053          looks like a decl-specifier-seq and a declarator -- but then
8054          there is no `=', so this is an expression.  */
8055       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8056           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8057         cp_parser_simulate_error (parser);
8058         
8059       /* If we did see an `=' or '{', then we are looking at a declaration
8060          for sure.  */
8061       if (cp_parser_parse_definitely (parser))
8062         {
8063           tree pushed_scope;
8064           bool non_constant_p;
8065           bool flags = LOOKUP_ONLYCONVERTING;
8066
8067           /* Create the declaration.  */
8068           decl = start_decl (declarator, &type_specifiers,
8069                              /*initialized_p=*/true,
8070                              attributes, /*prefix_attributes=*/NULL_TREE,
8071                              &pushed_scope);
8072
8073           /* Parse the initializer.  */
8074           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8075             {
8076               initializer = cp_parser_braced_list (parser, &non_constant_p);
8077               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8078               flags = 0;
8079             }
8080           else
8081             {
8082               /* Consume the `='.  */
8083               cp_parser_require (parser, CPP_EQ, "%<=%>");
8084               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8085             }
8086           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8087             maybe_warn_cpp0x ("extended initializer lists");
8088
8089           if (!non_constant_p)
8090             initializer = fold_non_dependent_expr (initializer);
8091
8092           /* Process the initializer.  */
8093           cp_finish_decl (decl,
8094                           initializer, !non_constant_p,
8095                           asm_specification,
8096                           flags);
8097
8098           if (pushed_scope)
8099             pop_scope (pushed_scope);
8100
8101           return convert_from_reference (decl);
8102         }
8103     }
8104   /* If we didn't even get past the declarator successfully, we are
8105      definitely not looking at a declaration.  */
8106   else
8107     cp_parser_abort_tentative_parse (parser);
8108
8109   /* Otherwise, we are looking at an expression.  */
8110   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8111 }
8112
8113 /* Parse an iteration-statement.
8114
8115    iteration-statement:
8116      while ( condition ) statement
8117      do statement while ( expression ) ;
8118      for ( for-init-statement condition [opt] ; expression [opt] )
8119        statement
8120
8121    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8122
8123 static tree
8124 cp_parser_iteration_statement (cp_parser* parser)
8125 {
8126   cp_token *token;
8127   enum rid keyword;
8128   tree statement;
8129   unsigned char in_statement;
8130
8131   /* Peek at the next token.  */
8132   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8133   if (!token)
8134     return error_mark_node;
8135
8136   /* Remember whether or not we are already within an iteration
8137      statement.  */
8138   in_statement = parser->in_statement;
8139
8140   /* See what kind of keyword it is.  */
8141   keyword = token->keyword;
8142   switch (keyword)
8143     {
8144     case RID_WHILE:
8145       {
8146         tree condition;
8147
8148         /* Begin the while-statement.  */
8149         statement = begin_while_stmt ();
8150         /* Look for the `('.  */
8151         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8152         /* Parse the condition.  */
8153         condition = cp_parser_condition (parser);
8154         finish_while_stmt_cond (condition, statement);
8155         /* Look for the `)'.  */
8156         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8157         /* Parse the dependent statement.  */
8158         parser->in_statement = IN_ITERATION_STMT;
8159         cp_parser_already_scoped_statement (parser);
8160         parser->in_statement = in_statement;
8161         /* We're done with the while-statement.  */
8162         finish_while_stmt (statement);
8163       }
8164       break;
8165
8166     case RID_DO:
8167       {
8168         tree expression;
8169
8170         /* Begin the do-statement.  */
8171         statement = begin_do_stmt ();
8172         /* Parse the body of the do-statement.  */
8173         parser->in_statement = IN_ITERATION_STMT;
8174         cp_parser_implicitly_scoped_statement (parser, NULL);
8175         parser->in_statement = in_statement;
8176         finish_do_body (statement);
8177         /* Look for the `while' keyword.  */
8178         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8179         /* Look for the `('.  */
8180         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8181         /* Parse the expression.  */
8182         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8183         /* We're done with the do-statement.  */
8184         finish_do_stmt (expression, statement);
8185         /* Look for the `)'.  */
8186         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8187         /* Look for the `;'.  */
8188         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8189       }
8190       break;
8191
8192     case RID_FOR:
8193       {
8194         tree condition = NULL_TREE;
8195         tree expression = NULL_TREE;
8196
8197         /* Begin the for-statement.  */
8198         statement = begin_for_stmt ();
8199         /* Look for the `('.  */
8200         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8201         /* Parse the initialization.  */
8202         cp_parser_for_init_statement (parser);
8203         finish_for_init_stmt (statement);
8204
8205         /* If there's a condition, process it.  */
8206         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8207           condition = cp_parser_condition (parser);
8208         finish_for_cond (condition, statement);
8209         /* Look for the `;'.  */
8210         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8211
8212         /* If there's an expression, process it.  */
8213         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8214           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8215         finish_for_expr (expression, statement);
8216         /* Look for the `)'.  */
8217         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8218
8219         /* Parse the body of the for-statement.  */
8220         parser->in_statement = IN_ITERATION_STMT;
8221         cp_parser_already_scoped_statement (parser);
8222         parser->in_statement = in_statement;
8223
8224         /* We're done with the for-statement.  */
8225         finish_for_stmt (statement);
8226       }
8227       break;
8228
8229     default:
8230       cp_parser_error (parser, "expected iteration-statement");
8231       statement = error_mark_node;
8232       break;
8233     }
8234
8235   return statement;
8236 }
8237
8238 /* Parse a for-init-statement.
8239
8240    for-init-statement:
8241      expression-statement
8242      simple-declaration  */
8243
8244 static void
8245 cp_parser_for_init_statement (cp_parser* parser)
8246 {
8247   /* If the next token is a `;', then we have an empty
8248      expression-statement.  Grammatically, this is also a
8249      simple-declaration, but an invalid one, because it does not
8250      declare anything.  Therefore, if we did not handle this case
8251      specially, we would issue an error message about an invalid
8252      declaration.  */
8253   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8254     {
8255       /* We're going to speculatively look for a declaration, falling back
8256          to an expression, if necessary.  */
8257       cp_parser_parse_tentatively (parser);
8258       /* Parse the declaration.  */
8259       cp_parser_simple_declaration (parser,
8260                                     /*function_definition_allowed_p=*/false);
8261       /* If the tentative parse failed, then we shall need to look for an
8262          expression-statement.  */
8263       if (cp_parser_parse_definitely (parser))
8264         return;
8265     }
8266
8267   cp_parser_expression_statement (parser, false);
8268 }
8269
8270 /* Parse a jump-statement.
8271
8272    jump-statement:
8273      break ;
8274      continue ;
8275      return expression [opt] ;
8276      return braced-init-list ;
8277      goto identifier ;
8278
8279    GNU extension:
8280
8281    jump-statement:
8282      goto * expression ;
8283
8284    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8285
8286 static tree
8287 cp_parser_jump_statement (cp_parser* parser)
8288 {
8289   tree statement = error_mark_node;
8290   cp_token *token;
8291   enum rid keyword;
8292   unsigned char in_statement;
8293
8294   /* Peek at the next token.  */
8295   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8296   if (!token)
8297     return error_mark_node;
8298
8299   /* See what kind of keyword it is.  */
8300   keyword = token->keyword;
8301   switch (keyword)
8302     {
8303     case RID_BREAK:
8304       in_statement = parser->in_statement & ~IN_IF_STMT;      
8305       switch (in_statement)
8306         {
8307         case 0:
8308           error_at (token->location, "break statement not within loop or switch");
8309           break;
8310         default:
8311           gcc_assert ((in_statement & IN_SWITCH_STMT)
8312                       || in_statement == IN_ITERATION_STMT);
8313           statement = finish_break_stmt ();
8314           break;
8315         case IN_OMP_BLOCK:
8316           error_at (token->location, "invalid exit from OpenMP structured block");
8317           break;
8318         case IN_OMP_FOR:
8319           error_at (token->location, "break statement used with OpenMP for loop");
8320           break;
8321         }
8322       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8323       break;
8324
8325     case RID_CONTINUE:
8326       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8327         {
8328         case 0:
8329           error_at (token->location, "continue statement not within a loop");
8330           break;
8331         case IN_ITERATION_STMT:
8332         case IN_OMP_FOR:
8333           statement = finish_continue_stmt ();
8334           break;
8335         case IN_OMP_BLOCK:
8336           error_at (token->location, "invalid exit from OpenMP structured block");
8337           break;
8338         default:
8339           gcc_unreachable ();
8340         }
8341       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8342       break;
8343
8344     case RID_RETURN:
8345       {
8346         tree expr;
8347         bool expr_non_constant_p;
8348
8349         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8350           {
8351             maybe_warn_cpp0x ("extended initializer lists");
8352             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8353           }
8354         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8355           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8356         else
8357           /* If the next token is a `;', then there is no
8358              expression.  */
8359           expr = NULL_TREE;
8360         /* Build the return-statement.  */
8361         statement = finish_return_stmt (expr);
8362         /* Look for the final `;'.  */
8363         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8364       }
8365       break;
8366
8367     case RID_GOTO:
8368       /* Create the goto-statement.  */
8369       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8370         {
8371           /* Issue a warning about this use of a GNU extension.  */
8372           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8373           /* Consume the '*' token.  */
8374           cp_lexer_consume_token (parser->lexer);
8375           /* Parse the dependent expression.  */
8376           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8377         }
8378       else
8379         finish_goto_stmt (cp_parser_identifier (parser));
8380       /* Look for the final `;'.  */
8381       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8382       break;
8383
8384     default:
8385       cp_parser_error (parser, "expected jump-statement");
8386       break;
8387     }
8388
8389   return statement;
8390 }
8391
8392 /* Parse a declaration-statement.
8393
8394    declaration-statement:
8395      block-declaration  */
8396
8397 static void
8398 cp_parser_declaration_statement (cp_parser* parser)
8399 {
8400   void *p;
8401
8402   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8403   p = obstack_alloc (&declarator_obstack, 0);
8404
8405  /* Parse the block-declaration.  */
8406   cp_parser_block_declaration (parser, /*statement_p=*/true);
8407
8408   /* Free any declarators allocated.  */
8409   obstack_free (&declarator_obstack, p);
8410
8411   /* Finish off the statement.  */
8412   finish_stmt ();
8413 }
8414
8415 /* Some dependent statements (like `if (cond) statement'), are
8416    implicitly in their own scope.  In other words, if the statement is
8417    a single statement (as opposed to a compound-statement), it is
8418    none-the-less treated as if it were enclosed in braces.  Any
8419    declarations appearing in the dependent statement are out of scope
8420    after control passes that point.  This function parses a statement,
8421    but ensures that is in its own scope, even if it is not a
8422    compound-statement.
8423
8424    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8425    is a (possibly labeled) if statement which is not enclosed in
8426    braces and has an else clause.  This is used to implement
8427    -Wparentheses.
8428
8429    Returns the new statement.  */
8430
8431 static tree
8432 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8433 {
8434   tree statement;
8435
8436   if (if_p != NULL)
8437     *if_p = false;
8438
8439   /* Mark if () ; with a special NOP_EXPR.  */
8440   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8441     {
8442       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8443       cp_lexer_consume_token (parser->lexer);
8444       statement = add_stmt (build_empty_stmt (loc));
8445     }
8446   /* if a compound is opened, we simply parse the statement directly.  */
8447   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8448     statement = cp_parser_compound_statement (parser, NULL, false);
8449   /* If the token is not a `{', then we must take special action.  */
8450   else
8451     {
8452       /* Create a compound-statement.  */
8453       statement = begin_compound_stmt (0);
8454       /* Parse the dependent-statement.  */
8455       cp_parser_statement (parser, NULL_TREE, false, if_p);
8456       /* Finish the dummy compound-statement.  */
8457       finish_compound_stmt (statement);
8458     }
8459
8460   /* Return the statement.  */
8461   return statement;
8462 }
8463
8464 /* For some dependent statements (like `while (cond) statement'), we
8465    have already created a scope.  Therefore, even if the dependent
8466    statement is a compound-statement, we do not want to create another
8467    scope.  */
8468
8469 static void
8470 cp_parser_already_scoped_statement (cp_parser* parser)
8471 {
8472   /* If the token is a `{', then we must take special action.  */
8473   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8474     cp_parser_statement (parser, NULL_TREE, false, NULL);
8475   else
8476     {
8477       /* Avoid calling cp_parser_compound_statement, so that we
8478          don't create a new scope.  Do everything else by hand.  */
8479       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8480       /* If the next keyword is `__label__' we have a label declaration.  */
8481       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8482         cp_parser_label_declaration (parser);
8483       /* Parse an (optional) statement-seq.  */
8484       cp_parser_statement_seq_opt (parser, NULL_TREE);
8485       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8486     }
8487 }
8488
8489 /* Declarations [gram.dcl.dcl] */
8490
8491 /* Parse an optional declaration-sequence.
8492
8493    declaration-seq:
8494      declaration
8495      declaration-seq declaration  */
8496
8497 static void
8498 cp_parser_declaration_seq_opt (cp_parser* parser)
8499 {
8500   while (true)
8501     {
8502       cp_token *token;
8503
8504       token = cp_lexer_peek_token (parser->lexer);
8505
8506       if (token->type == CPP_CLOSE_BRACE
8507           || token->type == CPP_EOF
8508           || token->type == CPP_PRAGMA_EOL)
8509         break;
8510
8511       if (token->type == CPP_SEMICOLON)
8512         {
8513           /* A declaration consisting of a single semicolon is
8514              invalid.  Allow it unless we're being pedantic.  */
8515           cp_lexer_consume_token (parser->lexer);
8516           if (!in_system_header)
8517             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8518           continue;
8519         }
8520
8521       /* If we're entering or exiting a region that's implicitly
8522          extern "C", modify the lang context appropriately.  */
8523       if (!parser->implicit_extern_c && token->implicit_extern_c)
8524         {
8525           push_lang_context (lang_name_c);
8526           parser->implicit_extern_c = true;
8527         }
8528       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8529         {
8530           pop_lang_context ();
8531           parser->implicit_extern_c = false;
8532         }
8533
8534       if (token->type == CPP_PRAGMA)
8535         {
8536           /* A top-level declaration can consist solely of a #pragma.
8537              A nested declaration cannot, so this is done here and not
8538              in cp_parser_declaration.  (A #pragma at block scope is
8539              handled in cp_parser_statement.)  */
8540           cp_parser_pragma (parser, pragma_external);
8541           continue;
8542         }
8543
8544       /* Parse the declaration itself.  */
8545       cp_parser_declaration (parser);
8546     }
8547 }
8548
8549 /* Parse a declaration.
8550
8551    declaration:
8552      block-declaration
8553      function-definition
8554      template-declaration
8555      explicit-instantiation
8556      explicit-specialization
8557      linkage-specification
8558      namespace-definition
8559
8560    GNU extension:
8561
8562    declaration:
8563       __extension__ declaration */
8564
8565 static void
8566 cp_parser_declaration (cp_parser* parser)
8567 {
8568   cp_token token1;
8569   cp_token token2;
8570   int saved_pedantic;
8571   void *p;
8572
8573   /* Check for the `__extension__' keyword.  */
8574   if (cp_parser_extension_opt (parser, &saved_pedantic))
8575     {
8576       /* Parse the qualified declaration.  */
8577       cp_parser_declaration (parser);
8578       /* Restore the PEDANTIC flag.  */
8579       pedantic = saved_pedantic;
8580
8581       return;
8582     }
8583
8584   /* Try to figure out what kind of declaration is present.  */
8585   token1 = *cp_lexer_peek_token (parser->lexer);
8586
8587   if (token1.type != CPP_EOF)
8588     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8589   else
8590     {
8591       token2.type = CPP_EOF;
8592       token2.keyword = RID_MAX;
8593     }
8594
8595   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8596   p = obstack_alloc (&declarator_obstack, 0);
8597
8598   /* If the next token is `extern' and the following token is a string
8599      literal, then we have a linkage specification.  */
8600   if (token1.keyword == RID_EXTERN
8601       && cp_parser_is_string_literal (&token2))
8602     cp_parser_linkage_specification (parser);
8603   /* If the next token is `template', then we have either a template
8604      declaration, an explicit instantiation, or an explicit
8605      specialization.  */
8606   else if (token1.keyword == RID_TEMPLATE)
8607     {
8608       /* `template <>' indicates a template specialization.  */
8609       if (token2.type == CPP_LESS
8610           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8611         cp_parser_explicit_specialization (parser);
8612       /* `template <' indicates a template declaration.  */
8613       else if (token2.type == CPP_LESS)
8614         cp_parser_template_declaration (parser, /*member_p=*/false);
8615       /* Anything else must be an explicit instantiation.  */
8616       else
8617         cp_parser_explicit_instantiation (parser);
8618     }
8619   /* If the next token is `export', then we have a template
8620      declaration.  */
8621   else if (token1.keyword == RID_EXPORT)
8622     cp_parser_template_declaration (parser, /*member_p=*/false);
8623   /* If the next token is `extern', 'static' or 'inline' and the one
8624      after that is `template', we have a GNU extended explicit
8625      instantiation directive.  */
8626   else if (cp_parser_allow_gnu_extensions_p (parser)
8627            && (token1.keyword == RID_EXTERN
8628                || token1.keyword == RID_STATIC
8629                || token1.keyword == RID_INLINE)
8630            && token2.keyword == RID_TEMPLATE)
8631     cp_parser_explicit_instantiation (parser);
8632   /* If the next token is `namespace', check for a named or unnamed
8633      namespace definition.  */
8634   else if (token1.keyword == RID_NAMESPACE
8635            && (/* A named namespace definition.  */
8636                (token2.type == CPP_NAME
8637                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8638                     != CPP_EQ))
8639                /* An unnamed namespace definition.  */
8640                || token2.type == CPP_OPEN_BRACE
8641                || token2.keyword == RID_ATTRIBUTE))
8642     cp_parser_namespace_definition (parser);
8643   /* An inline (associated) namespace definition.  */
8644   else if (token1.keyword == RID_INLINE
8645            && token2.keyword == RID_NAMESPACE)
8646     cp_parser_namespace_definition (parser);
8647   /* Objective-C++ declaration/definition.  */
8648   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8649     cp_parser_objc_declaration (parser);
8650   /* We must have either a block declaration or a function
8651      definition.  */
8652   else
8653     /* Try to parse a block-declaration, or a function-definition.  */
8654     cp_parser_block_declaration (parser, /*statement_p=*/false);
8655
8656   /* Free any declarators allocated.  */
8657   obstack_free (&declarator_obstack, p);
8658 }
8659
8660 /* Parse a block-declaration.
8661
8662    block-declaration:
8663      simple-declaration
8664      asm-definition
8665      namespace-alias-definition
8666      using-declaration
8667      using-directive
8668
8669    GNU Extension:
8670
8671    block-declaration:
8672      __extension__ block-declaration
8673
8674    C++0x Extension:
8675
8676    block-declaration:
8677      static_assert-declaration
8678
8679    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8680    part of a declaration-statement.  */
8681
8682 static void
8683 cp_parser_block_declaration (cp_parser *parser,
8684                              bool      statement_p)
8685 {
8686   cp_token *token1;
8687   int saved_pedantic;
8688
8689   /* Check for the `__extension__' keyword.  */
8690   if (cp_parser_extension_opt (parser, &saved_pedantic))
8691     {
8692       /* Parse the qualified declaration.  */
8693       cp_parser_block_declaration (parser, statement_p);
8694       /* Restore the PEDANTIC flag.  */
8695       pedantic = saved_pedantic;
8696
8697       return;
8698     }
8699
8700   /* Peek at the next token to figure out which kind of declaration is
8701      present.  */
8702   token1 = cp_lexer_peek_token (parser->lexer);
8703
8704   /* If the next keyword is `asm', we have an asm-definition.  */
8705   if (token1->keyword == RID_ASM)
8706     {
8707       if (statement_p)
8708         cp_parser_commit_to_tentative_parse (parser);
8709       cp_parser_asm_definition (parser);
8710     }
8711   /* If the next keyword is `namespace', we have a
8712      namespace-alias-definition.  */
8713   else if (token1->keyword == RID_NAMESPACE)
8714     cp_parser_namespace_alias_definition (parser);
8715   /* If the next keyword is `using', we have either a
8716      using-declaration or a using-directive.  */
8717   else if (token1->keyword == RID_USING)
8718     {
8719       cp_token *token2;
8720
8721       if (statement_p)
8722         cp_parser_commit_to_tentative_parse (parser);
8723       /* If the token after `using' is `namespace', then we have a
8724          using-directive.  */
8725       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8726       if (token2->keyword == RID_NAMESPACE)
8727         cp_parser_using_directive (parser);
8728       /* Otherwise, it's a using-declaration.  */
8729       else
8730         cp_parser_using_declaration (parser,
8731                                      /*access_declaration_p=*/false);
8732     }
8733   /* If the next keyword is `__label__' we have a misplaced label
8734      declaration.  */
8735   else if (token1->keyword == RID_LABEL)
8736     {
8737       cp_lexer_consume_token (parser->lexer);
8738       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8739       cp_parser_skip_to_end_of_statement (parser);
8740       /* If the next token is now a `;', consume it.  */
8741       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8742         cp_lexer_consume_token (parser->lexer);
8743     }
8744   /* If the next token is `static_assert' we have a static assertion.  */
8745   else if (token1->keyword == RID_STATIC_ASSERT)
8746     cp_parser_static_assert (parser, /*member_p=*/false);
8747   /* Anything else must be a simple-declaration.  */
8748   else
8749     cp_parser_simple_declaration (parser, !statement_p);
8750 }
8751
8752 /* Parse a simple-declaration.
8753
8754    simple-declaration:
8755      decl-specifier-seq [opt] init-declarator-list [opt] ;
8756
8757    init-declarator-list:
8758      init-declarator
8759      init-declarator-list , init-declarator
8760
8761    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8762    function-definition as a simple-declaration.  */
8763
8764 static void
8765 cp_parser_simple_declaration (cp_parser* parser,
8766                               bool function_definition_allowed_p)
8767 {
8768   cp_decl_specifier_seq decl_specifiers;
8769   int declares_class_or_enum;
8770   bool saw_declarator;
8771
8772   /* Defer access checks until we know what is being declared; the
8773      checks for names appearing in the decl-specifier-seq should be
8774      done as if we were in the scope of the thing being declared.  */
8775   push_deferring_access_checks (dk_deferred);
8776
8777   /* Parse the decl-specifier-seq.  We have to keep track of whether
8778      or not the decl-specifier-seq declares a named class or
8779      enumeration type, since that is the only case in which the
8780      init-declarator-list is allowed to be empty.
8781
8782      [dcl.dcl]
8783
8784      In a simple-declaration, the optional init-declarator-list can be
8785      omitted only when declaring a class or enumeration, that is when
8786      the decl-specifier-seq contains either a class-specifier, an
8787      elaborated-type-specifier, or an enum-specifier.  */
8788   cp_parser_decl_specifier_seq (parser,
8789                                 CP_PARSER_FLAGS_OPTIONAL,
8790                                 &decl_specifiers,
8791                                 &declares_class_or_enum);
8792   /* We no longer need to defer access checks.  */
8793   stop_deferring_access_checks ();
8794
8795   /* In a block scope, a valid declaration must always have a
8796      decl-specifier-seq.  By not trying to parse declarators, we can
8797      resolve the declaration/expression ambiguity more quickly.  */
8798   if (!function_definition_allowed_p
8799       && !decl_specifiers.any_specifiers_p)
8800     {
8801       cp_parser_error (parser, "expected declaration");
8802       goto done;
8803     }
8804
8805   /* If the next two tokens are both identifiers, the code is
8806      erroneous. The usual cause of this situation is code like:
8807
8808        T t;
8809
8810      where "T" should name a type -- but does not.  */
8811   if (!decl_specifiers.type
8812       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8813     {
8814       /* If parsing tentatively, we should commit; we really are
8815          looking at a declaration.  */
8816       cp_parser_commit_to_tentative_parse (parser);
8817       /* Give up.  */
8818       goto done;
8819     }
8820
8821   /* If we have seen at least one decl-specifier, and the next token
8822      is not a parenthesis, then we must be looking at a declaration.
8823      (After "int (" we might be looking at a functional cast.)  */
8824   if (decl_specifiers.any_specifiers_p
8825       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8826       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8827       && !cp_parser_error_occurred (parser))
8828     cp_parser_commit_to_tentative_parse (parser);
8829
8830   /* Keep going until we hit the `;' at the end of the simple
8831      declaration.  */
8832   saw_declarator = false;
8833   while (cp_lexer_next_token_is_not (parser->lexer,
8834                                      CPP_SEMICOLON))
8835     {
8836       cp_token *token;
8837       bool function_definition_p;
8838       tree decl;
8839
8840       if (saw_declarator)
8841         {
8842           /* If we are processing next declarator, coma is expected */
8843           token = cp_lexer_peek_token (parser->lexer);
8844           gcc_assert (token->type == CPP_COMMA);
8845           cp_lexer_consume_token (parser->lexer);
8846         }
8847       else
8848         saw_declarator = true;
8849
8850       /* Parse the init-declarator.  */
8851       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8852                                         /*checks=*/NULL,
8853                                         function_definition_allowed_p,
8854                                         /*member_p=*/false,
8855                                         declares_class_or_enum,
8856                                         &function_definition_p);
8857       /* If an error occurred while parsing tentatively, exit quickly.
8858          (That usually happens when in the body of a function; each
8859          statement is treated as a declaration-statement until proven
8860          otherwise.)  */
8861       if (cp_parser_error_occurred (parser))
8862         goto done;
8863       /* Handle function definitions specially.  */
8864       if (function_definition_p)
8865         {
8866           /* If the next token is a `,', then we are probably
8867              processing something like:
8868
8869                void f() {}, *p;
8870
8871              which is erroneous.  */
8872           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8873             {
8874               cp_token *token = cp_lexer_peek_token (parser->lexer);
8875               error_at (token->location,
8876                         "mixing"
8877                         " declarations and function-definitions is forbidden");
8878             }
8879           /* Otherwise, we're done with the list of declarators.  */
8880           else
8881             {
8882               pop_deferring_access_checks ();
8883               return;
8884             }
8885         }
8886       /* The next token should be either a `,' or a `;'.  */
8887       token = cp_lexer_peek_token (parser->lexer);
8888       /* If it's a `,', there are more declarators to come.  */
8889       if (token->type == CPP_COMMA)
8890         /* will be consumed next time around */;
8891       /* If it's a `;', we are done.  */
8892       else if (token->type == CPP_SEMICOLON)
8893         break;
8894       /* Anything else is an error.  */
8895       else
8896         {
8897           /* If we have already issued an error message we don't need
8898              to issue another one.  */
8899           if (decl != error_mark_node
8900               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8901             cp_parser_error (parser, "expected %<,%> or %<;%>");
8902           /* Skip tokens until we reach the end of the statement.  */
8903           cp_parser_skip_to_end_of_statement (parser);
8904           /* If the next token is now a `;', consume it.  */
8905           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8906             cp_lexer_consume_token (parser->lexer);
8907           goto done;
8908         }
8909       /* After the first time around, a function-definition is not
8910          allowed -- even if it was OK at first.  For example:
8911
8912            int i, f() {}
8913
8914          is not valid.  */
8915       function_definition_allowed_p = false;
8916     }
8917
8918   /* Issue an error message if no declarators are present, and the
8919      decl-specifier-seq does not itself declare a class or
8920      enumeration.  */
8921   if (!saw_declarator)
8922     {
8923       if (cp_parser_declares_only_class_p (parser))
8924         shadow_tag (&decl_specifiers);
8925       /* Perform any deferred access checks.  */
8926       perform_deferred_access_checks ();
8927     }
8928
8929   /* Consume the `;'.  */
8930   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8931
8932  done:
8933   pop_deferring_access_checks ();
8934 }
8935
8936 /* Parse a decl-specifier-seq.
8937
8938    decl-specifier-seq:
8939      decl-specifier-seq [opt] decl-specifier
8940
8941    decl-specifier:
8942      storage-class-specifier
8943      type-specifier
8944      function-specifier
8945      friend
8946      typedef
8947
8948    GNU Extension:
8949
8950    decl-specifier:
8951      attributes
8952
8953    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8954
8955    The parser flags FLAGS is used to control type-specifier parsing.
8956
8957    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8958    flags:
8959
8960      1: one of the decl-specifiers is an elaborated-type-specifier
8961         (i.e., a type declaration)
8962      2: one of the decl-specifiers is an enum-specifier or a
8963         class-specifier (i.e., a type definition)
8964
8965    */
8966
8967 static void
8968 cp_parser_decl_specifier_seq (cp_parser* parser,
8969                               cp_parser_flags flags,
8970                               cp_decl_specifier_seq *decl_specs,
8971                               int* declares_class_or_enum)
8972 {
8973   bool constructor_possible_p = !parser->in_declarator_p;
8974   cp_token *start_token = NULL;
8975
8976   /* Clear DECL_SPECS.  */
8977   clear_decl_specs (decl_specs);
8978
8979   /* Assume no class or enumeration type is declared.  */
8980   *declares_class_or_enum = 0;
8981
8982   /* Keep reading specifiers until there are no more to read.  */
8983   while (true)
8984     {
8985       bool constructor_p;
8986       bool found_decl_spec;
8987       cp_token *token;
8988
8989       /* Peek at the next token.  */
8990       token = cp_lexer_peek_token (parser->lexer);
8991
8992       /* Save the first token of the decl spec list for error
8993          reporting.  */
8994       if (!start_token)
8995         start_token = token;
8996       /* Handle attributes.  */
8997       if (token->keyword == RID_ATTRIBUTE)
8998         {
8999           /* Parse the attributes.  */
9000           decl_specs->attributes
9001             = chainon (decl_specs->attributes,
9002                        cp_parser_attributes_opt (parser));
9003           continue;
9004         }
9005       /* Assume we will find a decl-specifier keyword.  */
9006       found_decl_spec = true;
9007       /* If the next token is an appropriate keyword, we can simply
9008          add it to the list.  */
9009       switch (token->keyword)
9010         {
9011           /* decl-specifier:
9012                friend
9013                constexpr */
9014         case RID_FRIEND:
9015           if (!at_class_scope_p ())
9016             {
9017               error_at (token->location, "%<friend%> used outside of class");
9018               cp_lexer_purge_token (parser->lexer);
9019             }
9020           else
9021             {
9022               ++decl_specs->specs[(int) ds_friend];
9023               /* Consume the token.  */
9024               cp_lexer_consume_token (parser->lexer);
9025             }
9026           break;
9027
9028         case RID_CONSTEXPR:
9029           ++decl_specs->specs[(int) ds_constexpr];
9030           cp_lexer_consume_token (parser->lexer);
9031           break;
9032
9033           /* function-specifier:
9034                inline
9035                virtual
9036                explicit  */
9037         case RID_INLINE:
9038         case RID_VIRTUAL:
9039         case RID_EXPLICIT:
9040           cp_parser_function_specifier_opt (parser, decl_specs);
9041           break;
9042
9043           /* decl-specifier:
9044                typedef  */
9045         case RID_TYPEDEF:
9046           ++decl_specs->specs[(int) ds_typedef];
9047           /* Consume the token.  */
9048           cp_lexer_consume_token (parser->lexer);
9049           /* A constructor declarator cannot appear in a typedef.  */
9050           constructor_possible_p = false;
9051           /* The "typedef" keyword can only occur in a declaration; we
9052              may as well commit at this point.  */
9053           cp_parser_commit_to_tentative_parse (parser);
9054
9055           if (decl_specs->storage_class != sc_none)
9056             decl_specs->conflicting_specifiers_p = true;
9057           break;
9058
9059           /* storage-class-specifier:
9060                auto
9061                register
9062                static
9063                extern
9064                mutable
9065
9066              GNU Extension:
9067                thread  */
9068         case RID_AUTO:
9069           if (cxx_dialect == cxx98) 
9070             {
9071               /* Consume the token.  */
9072               cp_lexer_consume_token (parser->lexer);
9073
9074               /* Complain about `auto' as a storage specifier, if
9075                  we're complaining about C++0x compatibility.  */
9076               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9077                           " will change meaning in C++0x; please remove it");
9078
9079               /* Set the storage class anyway.  */
9080               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9081                                            token->location);
9082             }
9083           else
9084             /* C++0x auto type-specifier.  */
9085             found_decl_spec = false;
9086           break;
9087
9088         case RID_REGISTER:
9089         case RID_STATIC:
9090         case RID_EXTERN:
9091         case RID_MUTABLE:
9092           /* Consume the token.  */
9093           cp_lexer_consume_token (parser->lexer);
9094           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9095                                        token->location);
9096           break;
9097         case RID_THREAD:
9098           /* Consume the token.  */
9099           cp_lexer_consume_token (parser->lexer);
9100           ++decl_specs->specs[(int) ds_thread];
9101           break;
9102
9103         default:
9104           /* We did not yet find a decl-specifier yet.  */
9105           found_decl_spec = false;
9106           break;
9107         }
9108
9109       /* Constructors are a special case.  The `S' in `S()' is not a
9110          decl-specifier; it is the beginning of the declarator.  */
9111       constructor_p
9112         = (!found_decl_spec
9113            && constructor_possible_p
9114            && (cp_parser_constructor_declarator_p
9115                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9116
9117       /* If we don't have a DECL_SPEC yet, then we must be looking at
9118          a type-specifier.  */
9119       if (!found_decl_spec && !constructor_p)
9120         {
9121           int decl_spec_declares_class_or_enum;
9122           bool is_cv_qualifier;
9123           tree type_spec;
9124
9125           type_spec
9126             = cp_parser_type_specifier (parser, flags,
9127                                         decl_specs,
9128                                         /*is_declaration=*/true,
9129                                         &decl_spec_declares_class_or_enum,
9130                                         &is_cv_qualifier);
9131           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9132
9133           /* If this type-specifier referenced a user-defined type
9134              (a typedef, class-name, etc.), then we can't allow any
9135              more such type-specifiers henceforth.
9136
9137              [dcl.spec]
9138
9139              The longest sequence of decl-specifiers that could
9140              possibly be a type name is taken as the
9141              decl-specifier-seq of a declaration.  The sequence shall
9142              be self-consistent as described below.
9143
9144              [dcl.type]
9145
9146              As a general rule, at most one type-specifier is allowed
9147              in the complete decl-specifier-seq of a declaration.  The
9148              only exceptions are the following:
9149
9150              -- const or volatile can be combined with any other
9151                 type-specifier.
9152
9153              -- signed or unsigned can be combined with char, long,
9154                 short, or int.
9155
9156              -- ..
9157
9158              Example:
9159
9160                typedef char* Pc;
9161                void g (const int Pc);
9162
9163              Here, Pc is *not* part of the decl-specifier seq; it's
9164              the declarator.  Therefore, once we see a type-specifier
9165              (other than a cv-qualifier), we forbid any additional
9166              user-defined types.  We *do* still allow things like `int
9167              int' to be considered a decl-specifier-seq, and issue the
9168              error message later.  */
9169           if (type_spec && !is_cv_qualifier)
9170             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9171           /* A constructor declarator cannot follow a type-specifier.  */
9172           if (type_spec)
9173             {
9174               constructor_possible_p = false;
9175               found_decl_spec = true;
9176             }
9177         }
9178
9179       /* If we still do not have a DECL_SPEC, then there are no more
9180          decl-specifiers.  */
9181       if (!found_decl_spec)
9182         break;
9183
9184       decl_specs->any_specifiers_p = true;
9185       /* After we see one decl-specifier, further decl-specifiers are
9186          always optional.  */
9187       flags |= CP_PARSER_FLAGS_OPTIONAL;
9188     }
9189
9190   cp_parser_check_decl_spec (decl_specs, start_token->location);
9191
9192   /* Don't allow a friend specifier with a class definition.  */
9193   if (decl_specs->specs[(int) ds_friend] != 0
9194       && (*declares_class_or_enum & 2))
9195     error_at (start_token->location,
9196               "class definition may not be declared a friend");
9197 }
9198
9199 /* Parse an (optional) storage-class-specifier.
9200
9201    storage-class-specifier:
9202      auto
9203      register
9204      static
9205      extern
9206      mutable
9207
9208    GNU Extension:
9209
9210    storage-class-specifier:
9211      thread
9212
9213    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9214
9215 static tree
9216 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9217 {
9218   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9219     {
9220     case RID_AUTO:
9221       if (cxx_dialect != cxx98)
9222         return NULL_TREE;
9223       /* Fall through for C++98.  */
9224
9225     case RID_REGISTER:
9226     case RID_STATIC:
9227     case RID_EXTERN:
9228     case RID_MUTABLE:
9229     case RID_THREAD:
9230       /* Consume the token.  */
9231       return cp_lexer_consume_token (parser->lexer)->u.value;
9232
9233     default:
9234       return NULL_TREE;
9235     }
9236 }
9237
9238 /* Parse an (optional) function-specifier.
9239
9240    function-specifier:
9241      inline
9242      virtual
9243      explicit
9244
9245    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9246    Updates DECL_SPECS, if it is non-NULL.  */
9247
9248 static tree
9249 cp_parser_function_specifier_opt (cp_parser* parser,
9250                                   cp_decl_specifier_seq *decl_specs)
9251 {
9252   cp_token *token = cp_lexer_peek_token (parser->lexer);
9253   switch (token->keyword)
9254     {
9255     case RID_INLINE:
9256       if (decl_specs)
9257         ++decl_specs->specs[(int) ds_inline];
9258       break;
9259
9260     case RID_VIRTUAL:
9261       /* 14.5.2.3 [temp.mem]
9262
9263          A member function template shall not be virtual.  */
9264       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9265         error_at (token->location, "templates may not be %<virtual%>");
9266       else if (decl_specs)
9267         ++decl_specs->specs[(int) ds_virtual];
9268       break;
9269
9270     case RID_EXPLICIT:
9271       if (decl_specs)
9272         ++decl_specs->specs[(int) ds_explicit];
9273       break;
9274
9275     default:
9276       return NULL_TREE;
9277     }
9278
9279   /* Consume the token.  */
9280   return cp_lexer_consume_token (parser->lexer)->u.value;
9281 }
9282
9283 /* Parse a linkage-specification.
9284
9285    linkage-specification:
9286      extern string-literal { declaration-seq [opt] }
9287      extern string-literal declaration  */
9288
9289 static void
9290 cp_parser_linkage_specification (cp_parser* parser)
9291 {
9292   tree linkage;
9293
9294   /* Look for the `extern' keyword.  */
9295   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9296
9297   /* Look for the string-literal.  */
9298   linkage = cp_parser_string_literal (parser, false, false);
9299
9300   /* Transform the literal into an identifier.  If the literal is a
9301      wide-character string, or contains embedded NULs, then we can't
9302      handle it as the user wants.  */
9303   if (strlen (TREE_STRING_POINTER (linkage))
9304       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9305     {
9306       cp_parser_error (parser, "invalid linkage-specification");
9307       /* Assume C++ linkage.  */
9308       linkage = lang_name_cplusplus;
9309     }
9310   else
9311     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9312
9313   /* We're now using the new linkage.  */
9314   push_lang_context (linkage);
9315
9316   /* If the next token is a `{', then we're using the first
9317      production.  */
9318   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9319     {
9320       /* Consume the `{' token.  */
9321       cp_lexer_consume_token (parser->lexer);
9322       /* Parse the declarations.  */
9323       cp_parser_declaration_seq_opt (parser);
9324       /* Look for the closing `}'.  */
9325       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9326     }
9327   /* Otherwise, there's just one declaration.  */
9328   else
9329     {
9330       bool saved_in_unbraced_linkage_specification_p;
9331
9332       saved_in_unbraced_linkage_specification_p
9333         = parser->in_unbraced_linkage_specification_p;
9334       parser->in_unbraced_linkage_specification_p = true;
9335       cp_parser_declaration (parser);
9336       parser->in_unbraced_linkage_specification_p
9337         = saved_in_unbraced_linkage_specification_p;
9338     }
9339
9340   /* We're done with the linkage-specification.  */
9341   pop_lang_context ();
9342 }
9343
9344 /* Parse a static_assert-declaration.
9345
9346    static_assert-declaration:
9347      static_assert ( constant-expression , string-literal ) ; 
9348
9349    If MEMBER_P, this static_assert is a class member.  */
9350
9351 static void 
9352 cp_parser_static_assert(cp_parser *parser, bool member_p)
9353 {
9354   tree condition;
9355   tree message;
9356   cp_token *token;
9357   location_t saved_loc;
9358
9359   /* Peek at the `static_assert' token so we can keep track of exactly
9360      where the static assertion started.  */
9361   token = cp_lexer_peek_token (parser->lexer);
9362   saved_loc = token->location;
9363
9364   /* Look for the `static_assert' keyword.  */
9365   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9366                                   "%<static_assert%>"))
9367     return;
9368
9369   /*  We know we are in a static assertion; commit to any tentative
9370       parse.  */
9371   if (cp_parser_parsing_tentatively (parser))
9372     cp_parser_commit_to_tentative_parse (parser);
9373
9374   /* Parse the `(' starting the static assertion condition.  */
9375   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9376
9377   /* Parse the constant-expression.  */
9378   condition = 
9379     cp_parser_constant_expression (parser,
9380                                    /*allow_non_constant_p=*/false,
9381                                    /*non_constant_p=*/NULL);
9382
9383   /* Parse the separating `,'.  */
9384   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9385
9386   /* Parse the string-literal message.  */
9387   message = cp_parser_string_literal (parser, 
9388                                       /*translate=*/false,
9389                                       /*wide_ok=*/true);
9390
9391   /* A `)' completes the static assertion.  */
9392   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9393     cp_parser_skip_to_closing_parenthesis (parser, 
9394                                            /*recovering=*/true, 
9395                                            /*or_comma=*/false,
9396                                            /*consume_paren=*/true);
9397
9398   /* A semicolon terminates the declaration.  */
9399   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9400
9401   /* Complete the static assertion, which may mean either processing 
9402      the static assert now or saving it for template instantiation.  */
9403   finish_static_assert (condition, message, saved_loc, member_p);
9404 }
9405
9406 /* Parse a `decltype' type. Returns the type. 
9407
9408    simple-type-specifier:
9409      decltype ( expression )  */
9410
9411 static tree
9412 cp_parser_decltype (cp_parser *parser)
9413 {
9414   tree expr;
9415   bool id_expression_or_member_access_p = false;
9416   const char *saved_message;
9417   bool saved_integral_constant_expression_p;
9418   bool saved_non_integral_constant_expression_p;
9419   cp_token *id_expr_start_token;
9420
9421   /* Look for the `decltype' token.  */
9422   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9423     return error_mark_node;
9424
9425   /* Types cannot be defined in a `decltype' expression.  Save away the
9426      old message.  */
9427   saved_message = parser->type_definition_forbidden_message;
9428
9429   /* And create the new one.  */
9430   parser->type_definition_forbidden_message
9431     = "types may not be defined in %<decltype%> expressions";
9432
9433   /* The restrictions on constant-expressions do not apply inside
9434      decltype expressions.  */
9435   saved_integral_constant_expression_p
9436     = parser->integral_constant_expression_p;
9437   saved_non_integral_constant_expression_p
9438     = parser->non_integral_constant_expression_p;
9439   parser->integral_constant_expression_p = false;
9440
9441   /* Do not actually evaluate the expression.  */
9442   ++cp_unevaluated_operand;
9443
9444   /* Do not warn about problems with the expression.  */
9445   ++c_inhibit_evaluation_warnings;
9446
9447   /* Parse the opening `('.  */
9448   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9449     return error_mark_node;
9450   
9451   /* First, try parsing an id-expression.  */
9452   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9453   cp_parser_parse_tentatively (parser);
9454   expr = cp_parser_id_expression (parser,
9455                                   /*template_keyword_p=*/false,
9456                                   /*check_dependency_p=*/true,
9457                                   /*template_p=*/NULL,
9458                                   /*declarator_p=*/false,
9459                                   /*optional_p=*/false);
9460
9461   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9462     {
9463       bool non_integral_constant_expression_p = false;
9464       tree id_expression = expr;
9465       cp_id_kind idk;
9466       const char *error_msg;
9467
9468       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9469         /* Lookup the name we got back from the id-expression.  */
9470         expr = cp_parser_lookup_name (parser, expr,
9471                                       none_type,
9472                                       /*is_template=*/false,
9473                                       /*is_namespace=*/false,
9474                                       /*check_dependency=*/true,
9475                                       /*ambiguous_decls=*/NULL,
9476                                       id_expr_start_token->location);
9477
9478       if (expr
9479           && expr != error_mark_node
9480           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9481           && TREE_CODE (expr) != TYPE_DECL
9482           && (TREE_CODE (expr) != BIT_NOT_EXPR
9483               || !TYPE_P (TREE_OPERAND (expr, 0)))
9484           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9485         {
9486           /* Complete lookup of the id-expression.  */
9487           expr = (finish_id_expression
9488                   (id_expression, expr, parser->scope, &idk,
9489                    /*integral_constant_expression_p=*/false,
9490                    /*allow_non_integral_constant_expression_p=*/true,
9491                    &non_integral_constant_expression_p,
9492                    /*template_p=*/false,
9493                    /*done=*/true,
9494                    /*address_p=*/false,
9495                    /*template_arg_p=*/false,
9496                    &error_msg,
9497                    id_expr_start_token->location));
9498
9499           if (expr == error_mark_node)
9500             /* We found an id-expression, but it was something that we
9501                should not have found. This is an error, not something
9502                we can recover from, so note that we found an
9503                id-expression and we'll recover as gracefully as
9504                possible.  */
9505             id_expression_or_member_access_p = true;
9506         }
9507
9508       if (expr 
9509           && expr != error_mark_node
9510           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9511         /* We have an id-expression.  */
9512         id_expression_or_member_access_p = true;
9513     }
9514
9515   if (!id_expression_or_member_access_p)
9516     {
9517       /* Abort the id-expression parse.  */
9518       cp_parser_abort_tentative_parse (parser);
9519
9520       /* Parsing tentatively, again.  */
9521       cp_parser_parse_tentatively (parser);
9522
9523       /* Parse a class member access.  */
9524       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9525                                            /*cast_p=*/false,
9526                                            /*member_access_only_p=*/true, NULL);
9527
9528       if (expr 
9529           && expr != error_mark_node
9530           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9531         /* We have an id-expression.  */
9532         id_expression_or_member_access_p = true;
9533     }
9534
9535   if (id_expression_or_member_access_p)
9536     /* We have parsed the complete id-expression or member access.  */
9537     cp_parser_parse_definitely (parser);
9538   else
9539     {
9540       /* Abort our attempt to parse an id-expression or member access
9541          expression.  */
9542       cp_parser_abort_tentative_parse (parser);
9543
9544       /* Parse a full expression.  */
9545       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9546     }
9547
9548   /* Go back to evaluating expressions.  */
9549   --cp_unevaluated_operand;
9550   --c_inhibit_evaluation_warnings;
9551
9552   /* Restore the old message and the integral constant expression
9553      flags.  */
9554   parser->type_definition_forbidden_message = saved_message;
9555   parser->integral_constant_expression_p
9556     = saved_integral_constant_expression_p;
9557   parser->non_integral_constant_expression_p
9558     = saved_non_integral_constant_expression_p;
9559
9560   if (expr == error_mark_node)
9561     {
9562       /* Skip everything up to the closing `)'.  */
9563       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9564                                              /*consume_paren=*/true);
9565       return error_mark_node;
9566     }
9567   
9568   /* Parse to the closing `)'.  */
9569   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9570     {
9571       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9572                                              /*consume_paren=*/true);
9573       return error_mark_node;
9574     }
9575
9576   return finish_decltype_type (expr, id_expression_or_member_access_p);
9577 }
9578
9579 /* Special member functions [gram.special] */
9580
9581 /* Parse a conversion-function-id.
9582
9583    conversion-function-id:
9584      operator conversion-type-id
9585
9586    Returns an IDENTIFIER_NODE representing the operator.  */
9587
9588 static tree
9589 cp_parser_conversion_function_id (cp_parser* parser)
9590 {
9591   tree type;
9592   tree saved_scope;
9593   tree saved_qualifying_scope;
9594   tree saved_object_scope;
9595   tree pushed_scope = NULL_TREE;
9596
9597   /* Look for the `operator' token.  */
9598   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9599     return error_mark_node;
9600   /* When we parse the conversion-type-id, the current scope will be
9601      reset.  However, we need that information in able to look up the
9602      conversion function later, so we save it here.  */
9603   saved_scope = parser->scope;
9604   saved_qualifying_scope = parser->qualifying_scope;
9605   saved_object_scope = parser->object_scope;
9606   /* We must enter the scope of the class so that the names of
9607      entities declared within the class are available in the
9608      conversion-type-id.  For example, consider:
9609
9610        struct S {
9611          typedef int I;
9612          operator I();
9613        };
9614
9615        S::operator I() { ... }
9616
9617      In order to see that `I' is a type-name in the definition, we
9618      must be in the scope of `S'.  */
9619   if (saved_scope)
9620     pushed_scope = push_scope (saved_scope);
9621   /* Parse the conversion-type-id.  */
9622   type = cp_parser_conversion_type_id (parser);
9623   /* Leave the scope of the class, if any.  */
9624   if (pushed_scope)
9625     pop_scope (pushed_scope);
9626   /* Restore the saved scope.  */
9627   parser->scope = saved_scope;
9628   parser->qualifying_scope = saved_qualifying_scope;
9629   parser->object_scope = saved_object_scope;
9630   /* If the TYPE is invalid, indicate failure.  */
9631   if (type == error_mark_node)
9632     return error_mark_node;
9633   return mangle_conv_op_name_for_type (type);
9634 }
9635
9636 /* Parse a conversion-type-id:
9637
9638    conversion-type-id:
9639      type-specifier-seq conversion-declarator [opt]
9640
9641    Returns the TYPE specified.  */
9642
9643 static tree
9644 cp_parser_conversion_type_id (cp_parser* parser)
9645 {
9646   tree attributes;
9647   cp_decl_specifier_seq type_specifiers;
9648   cp_declarator *declarator;
9649   tree type_specified;
9650
9651   /* Parse the attributes.  */
9652   attributes = cp_parser_attributes_opt (parser);
9653   /* Parse the type-specifiers.  */
9654   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9655                                 &type_specifiers);
9656   /* If that didn't work, stop.  */
9657   if (type_specifiers.type == error_mark_node)
9658     return error_mark_node;
9659   /* Parse the conversion-declarator.  */
9660   declarator = cp_parser_conversion_declarator_opt (parser);
9661
9662   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9663                                     /*initialized=*/0, &attributes);
9664   if (attributes)
9665     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9666
9667   /* Don't give this error when parsing tentatively.  This happens to
9668      work because we always parse this definitively once.  */
9669   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9670       && type_uses_auto (type_specified))
9671     {
9672       error ("invalid use of %<auto%> in conversion operator");
9673       return error_mark_node;
9674     }
9675
9676   return type_specified;
9677 }
9678
9679 /* Parse an (optional) conversion-declarator.
9680
9681    conversion-declarator:
9682      ptr-operator conversion-declarator [opt]
9683
9684    */
9685
9686 static cp_declarator *
9687 cp_parser_conversion_declarator_opt (cp_parser* parser)
9688 {
9689   enum tree_code code;
9690   tree class_type;
9691   cp_cv_quals cv_quals;
9692
9693   /* We don't know if there's a ptr-operator next, or not.  */
9694   cp_parser_parse_tentatively (parser);
9695   /* Try the ptr-operator.  */
9696   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9697   /* If it worked, look for more conversion-declarators.  */
9698   if (cp_parser_parse_definitely (parser))
9699     {
9700       cp_declarator *declarator;
9701
9702       /* Parse another optional declarator.  */
9703       declarator = cp_parser_conversion_declarator_opt (parser);
9704
9705       return cp_parser_make_indirect_declarator
9706         (code, class_type, cv_quals, declarator);
9707    }
9708
9709   return NULL;
9710 }
9711
9712 /* Parse an (optional) ctor-initializer.
9713
9714    ctor-initializer:
9715      : mem-initializer-list
9716
9717    Returns TRUE iff the ctor-initializer was actually present.  */
9718
9719 static bool
9720 cp_parser_ctor_initializer_opt (cp_parser* parser)
9721 {
9722   /* If the next token is not a `:', then there is no
9723      ctor-initializer.  */
9724   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9725     {
9726       /* Do default initialization of any bases and members.  */
9727       if (DECL_CONSTRUCTOR_P (current_function_decl))
9728         finish_mem_initializers (NULL_TREE);
9729
9730       return false;
9731     }
9732
9733   /* Consume the `:' token.  */
9734   cp_lexer_consume_token (parser->lexer);
9735   /* And the mem-initializer-list.  */
9736   cp_parser_mem_initializer_list (parser);
9737
9738   return true;
9739 }
9740
9741 /* Parse a mem-initializer-list.
9742
9743    mem-initializer-list:
9744      mem-initializer ... [opt]
9745      mem-initializer ... [opt] , mem-initializer-list  */
9746
9747 static void
9748 cp_parser_mem_initializer_list (cp_parser* parser)
9749 {
9750   tree mem_initializer_list = NULL_TREE;
9751   cp_token *token = cp_lexer_peek_token (parser->lexer);
9752
9753   /* Let the semantic analysis code know that we are starting the
9754      mem-initializer-list.  */
9755   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9756     error_at (token->location,
9757               "only constructors take base initializers");
9758
9759   /* Loop through the list.  */
9760   while (true)
9761     {
9762       tree mem_initializer;
9763
9764       token = cp_lexer_peek_token (parser->lexer);
9765       /* Parse the mem-initializer.  */
9766       mem_initializer = cp_parser_mem_initializer (parser);
9767       /* If the next token is a `...', we're expanding member initializers. */
9768       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9769         {
9770           /* Consume the `...'. */
9771           cp_lexer_consume_token (parser->lexer);
9772
9773           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9774              can be expanded but members cannot. */
9775           if (mem_initializer != error_mark_node
9776               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9777             {
9778               error_at (token->location,
9779                         "cannot expand initializer for member %<%D%>",
9780                         TREE_PURPOSE (mem_initializer));
9781               mem_initializer = error_mark_node;
9782             }
9783
9784           /* Construct the pack expansion type. */
9785           if (mem_initializer != error_mark_node)
9786             mem_initializer = make_pack_expansion (mem_initializer);
9787         }
9788       /* Add it to the list, unless it was erroneous.  */
9789       if (mem_initializer != error_mark_node)
9790         {
9791           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9792           mem_initializer_list = mem_initializer;
9793         }
9794       /* If the next token is not a `,', we're done.  */
9795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9796         break;
9797       /* Consume the `,' token.  */
9798       cp_lexer_consume_token (parser->lexer);
9799     }
9800
9801   /* Perform semantic analysis.  */
9802   if (DECL_CONSTRUCTOR_P (current_function_decl))
9803     finish_mem_initializers (mem_initializer_list);
9804 }
9805
9806 /* Parse a mem-initializer.
9807
9808    mem-initializer:
9809      mem-initializer-id ( expression-list [opt] )
9810      mem-initializer-id braced-init-list
9811
9812    GNU extension:
9813
9814    mem-initializer:
9815      ( expression-list [opt] )
9816
9817    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9818    class) or FIELD_DECL (for a non-static data member) to initialize;
9819    the TREE_VALUE is the expression-list.  An empty initialization
9820    list is represented by void_list_node.  */
9821
9822 static tree
9823 cp_parser_mem_initializer (cp_parser* parser)
9824 {
9825   tree mem_initializer_id;
9826   tree expression_list;
9827   tree member;
9828   cp_token *token = cp_lexer_peek_token (parser->lexer);
9829
9830   /* Find out what is being initialized.  */
9831   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9832     {
9833       permerror (token->location,
9834                  "anachronistic old-style base class initializer");
9835       mem_initializer_id = NULL_TREE;
9836     }
9837   else
9838     {
9839       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9840       if (mem_initializer_id == error_mark_node)
9841         return mem_initializer_id;
9842     }
9843   member = expand_member_init (mem_initializer_id);
9844   if (member && !DECL_P (member))
9845     in_base_initializer = 1;
9846
9847   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9848     {
9849       bool expr_non_constant_p;
9850       maybe_warn_cpp0x ("extended initializer lists");
9851       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9852       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9853       expression_list = build_tree_list (NULL_TREE, expression_list);
9854     }
9855   else
9856     {
9857       VEC(tree,gc)* vec;
9858       vec = cp_parser_parenthesized_expression_list (parser, false,
9859                                                      /*cast_p=*/false,
9860                                                      /*allow_expansion_p=*/true,
9861                                                      /*non_constant_p=*/NULL);
9862       if (vec == NULL)
9863         return error_mark_node;
9864       expression_list = build_tree_list_vec (vec);
9865       release_tree_vector (vec);
9866     }
9867
9868   if (expression_list == error_mark_node)
9869     return error_mark_node;
9870   if (!expression_list)
9871     expression_list = void_type_node;
9872
9873   in_base_initializer = 0;
9874
9875   return member ? build_tree_list (member, expression_list) : error_mark_node;
9876 }
9877
9878 /* Parse a mem-initializer-id.
9879
9880    mem-initializer-id:
9881      :: [opt] nested-name-specifier [opt] class-name
9882      identifier
9883
9884    Returns a TYPE indicating the class to be initializer for the first
9885    production.  Returns an IDENTIFIER_NODE indicating the data member
9886    to be initialized for the second production.  */
9887
9888 static tree
9889 cp_parser_mem_initializer_id (cp_parser* parser)
9890 {
9891   bool global_scope_p;
9892   bool nested_name_specifier_p;
9893   bool template_p = false;
9894   tree id;
9895
9896   cp_token *token = cp_lexer_peek_token (parser->lexer);
9897
9898   /* `typename' is not allowed in this context ([temp.res]).  */
9899   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9900     {
9901       error_at (token->location, 
9902                 "keyword %<typename%> not allowed in this context (a qualified "
9903                 "member initializer is implicitly a type)");
9904       cp_lexer_consume_token (parser->lexer);
9905     }
9906   /* Look for the optional `::' operator.  */
9907   global_scope_p
9908     = (cp_parser_global_scope_opt (parser,
9909                                    /*current_scope_valid_p=*/false)
9910        != NULL_TREE);
9911   /* Look for the optional nested-name-specifier.  The simplest way to
9912      implement:
9913
9914        [temp.res]
9915
9916        The keyword `typename' is not permitted in a base-specifier or
9917        mem-initializer; in these contexts a qualified name that
9918        depends on a template-parameter is implicitly assumed to be a
9919        type name.
9920
9921      is to assume that we have seen the `typename' keyword at this
9922      point.  */
9923   nested_name_specifier_p
9924     = (cp_parser_nested_name_specifier_opt (parser,
9925                                             /*typename_keyword_p=*/true,
9926                                             /*check_dependency_p=*/true,
9927                                             /*type_p=*/true,
9928                                             /*is_declaration=*/true)
9929        != NULL_TREE);
9930   if (nested_name_specifier_p)
9931     template_p = cp_parser_optional_template_keyword (parser);
9932   /* If there is a `::' operator or a nested-name-specifier, then we
9933      are definitely looking for a class-name.  */
9934   if (global_scope_p || nested_name_specifier_p)
9935     return cp_parser_class_name (parser,
9936                                  /*typename_keyword_p=*/true,
9937                                  /*template_keyword_p=*/template_p,
9938                                  none_type,
9939                                  /*check_dependency_p=*/true,
9940                                  /*class_head_p=*/false,
9941                                  /*is_declaration=*/true);
9942   /* Otherwise, we could also be looking for an ordinary identifier.  */
9943   cp_parser_parse_tentatively (parser);
9944   /* Try a class-name.  */
9945   id = cp_parser_class_name (parser,
9946                              /*typename_keyword_p=*/true,
9947                              /*template_keyword_p=*/false,
9948                              none_type,
9949                              /*check_dependency_p=*/true,
9950                              /*class_head_p=*/false,
9951                              /*is_declaration=*/true);
9952   /* If we found one, we're done.  */
9953   if (cp_parser_parse_definitely (parser))
9954     return id;
9955   /* Otherwise, look for an ordinary identifier.  */
9956   return cp_parser_identifier (parser);
9957 }
9958
9959 /* Overloading [gram.over] */
9960
9961 /* Parse an operator-function-id.
9962
9963    operator-function-id:
9964      operator operator
9965
9966    Returns an IDENTIFIER_NODE for the operator which is a
9967    human-readable spelling of the identifier, e.g., `operator +'.  */
9968
9969 static tree
9970 cp_parser_operator_function_id (cp_parser* parser)
9971 {
9972   /* Look for the `operator' keyword.  */
9973   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9974     return error_mark_node;
9975   /* And then the name of the operator itself.  */
9976   return cp_parser_operator (parser);
9977 }
9978
9979 /* Parse an operator.
9980
9981    operator:
9982      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9983      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9984      || ++ -- , ->* -> () []
9985
9986    GNU Extensions:
9987
9988    operator:
9989      <? >? <?= >?=
9990
9991    Returns an IDENTIFIER_NODE for the operator which is a
9992    human-readable spelling of the identifier, e.g., `operator +'.  */
9993
9994 static tree
9995 cp_parser_operator (cp_parser* parser)
9996 {
9997   tree id = NULL_TREE;
9998   cp_token *token;
9999
10000   /* Peek at the next token.  */
10001   token = cp_lexer_peek_token (parser->lexer);
10002   /* Figure out which operator we have.  */
10003   switch (token->type)
10004     {
10005     case CPP_KEYWORD:
10006       {
10007         enum tree_code op;
10008
10009         /* The keyword should be either `new' or `delete'.  */
10010         if (token->keyword == RID_NEW)
10011           op = NEW_EXPR;
10012         else if (token->keyword == RID_DELETE)
10013           op = DELETE_EXPR;
10014         else
10015           break;
10016
10017         /* Consume the `new' or `delete' token.  */
10018         cp_lexer_consume_token (parser->lexer);
10019
10020         /* Peek at the next token.  */
10021         token = cp_lexer_peek_token (parser->lexer);
10022         /* If it's a `[' token then this is the array variant of the
10023            operator.  */
10024         if (token->type == CPP_OPEN_SQUARE)
10025           {
10026             /* Consume the `[' token.  */
10027             cp_lexer_consume_token (parser->lexer);
10028             /* Look for the `]' token.  */
10029             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10030             id = ansi_opname (op == NEW_EXPR
10031                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10032           }
10033         /* Otherwise, we have the non-array variant.  */
10034         else
10035           id = ansi_opname (op);
10036
10037         return id;
10038       }
10039
10040     case CPP_PLUS:
10041       id = ansi_opname (PLUS_EXPR);
10042       break;
10043
10044     case CPP_MINUS:
10045       id = ansi_opname (MINUS_EXPR);
10046       break;
10047
10048     case CPP_MULT:
10049       id = ansi_opname (MULT_EXPR);
10050       break;
10051
10052     case CPP_DIV:
10053       id = ansi_opname (TRUNC_DIV_EXPR);
10054       break;
10055
10056     case CPP_MOD:
10057       id = ansi_opname (TRUNC_MOD_EXPR);
10058       break;
10059
10060     case CPP_XOR:
10061       id = ansi_opname (BIT_XOR_EXPR);
10062       break;
10063
10064     case CPP_AND:
10065       id = ansi_opname (BIT_AND_EXPR);
10066       break;
10067
10068     case CPP_OR:
10069       id = ansi_opname (BIT_IOR_EXPR);
10070       break;
10071
10072     case CPP_COMPL:
10073       id = ansi_opname (BIT_NOT_EXPR);
10074       break;
10075
10076     case CPP_NOT:
10077       id = ansi_opname (TRUTH_NOT_EXPR);
10078       break;
10079
10080     case CPP_EQ:
10081       id = ansi_assopname (NOP_EXPR);
10082       break;
10083
10084     case CPP_LESS:
10085       id = ansi_opname (LT_EXPR);
10086       break;
10087
10088     case CPP_GREATER:
10089       id = ansi_opname (GT_EXPR);
10090       break;
10091
10092     case CPP_PLUS_EQ:
10093       id = ansi_assopname (PLUS_EXPR);
10094       break;
10095
10096     case CPP_MINUS_EQ:
10097       id = ansi_assopname (MINUS_EXPR);
10098       break;
10099
10100     case CPP_MULT_EQ:
10101       id = ansi_assopname (MULT_EXPR);
10102       break;
10103
10104     case CPP_DIV_EQ:
10105       id = ansi_assopname (TRUNC_DIV_EXPR);
10106       break;
10107
10108     case CPP_MOD_EQ:
10109       id = ansi_assopname (TRUNC_MOD_EXPR);
10110       break;
10111
10112     case CPP_XOR_EQ:
10113       id = ansi_assopname (BIT_XOR_EXPR);
10114       break;
10115
10116     case CPP_AND_EQ:
10117       id = ansi_assopname (BIT_AND_EXPR);
10118       break;
10119
10120     case CPP_OR_EQ:
10121       id = ansi_assopname (BIT_IOR_EXPR);
10122       break;
10123
10124     case CPP_LSHIFT:
10125       id = ansi_opname (LSHIFT_EXPR);
10126       break;
10127
10128     case CPP_RSHIFT:
10129       id = ansi_opname (RSHIFT_EXPR);
10130       break;
10131
10132     case CPP_LSHIFT_EQ:
10133       id = ansi_assopname (LSHIFT_EXPR);
10134       break;
10135
10136     case CPP_RSHIFT_EQ:
10137       id = ansi_assopname (RSHIFT_EXPR);
10138       break;
10139
10140     case CPP_EQ_EQ:
10141       id = ansi_opname (EQ_EXPR);
10142       break;
10143
10144     case CPP_NOT_EQ:
10145       id = ansi_opname (NE_EXPR);
10146       break;
10147
10148     case CPP_LESS_EQ:
10149       id = ansi_opname (LE_EXPR);
10150       break;
10151
10152     case CPP_GREATER_EQ:
10153       id = ansi_opname (GE_EXPR);
10154       break;
10155
10156     case CPP_AND_AND:
10157       id = ansi_opname (TRUTH_ANDIF_EXPR);
10158       break;
10159
10160     case CPP_OR_OR:
10161       id = ansi_opname (TRUTH_ORIF_EXPR);
10162       break;
10163
10164     case CPP_PLUS_PLUS:
10165       id = ansi_opname (POSTINCREMENT_EXPR);
10166       break;
10167
10168     case CPP_MINUS_MINUS:
10169       id = ansi_opname (PREDECREMENT_EXPR);
10170       break;
10171
10172     case CPP_COMMA:
10173       id = ansi_opname (COMPOUND_EXPR);
10174       break;
10175
10176     case CPP_DEREF_STAR:
10177       id = ansi_opname (MEMBER_REF);
10178       break;
10179
10180     case CPP_DEREF:
10181       id = ansi_opname (COMPONENT_REF);
10182       break;
10183
10184     case CPP_OPEN_PAREN:
10185       /* Consume the `('.  */
10186       cp_lexer_consume_token (parser->lexer);
10187       /* Look for the matching `)'.  */
10188       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10189       return ansi_opname (CALL_EXPR);
10190
10191     case CPP_OPEN_SQUARE:
10192       /* Consume the `['.  */
10193       cp_lexer_consume_token (parser->lexer);
10194       /* Look for the matching `]'.  */
10195       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10196       return ansi_opname (ARRAY_REF);
10197
10198     default:
10199       /* Anything else is an error.  */
10200       break;
10201     }
10202
10203   /* If we have selected an identifier, we need to consume the
10204      operator token.  */
10205   if (id)
10206     cp_lexer_consume_token (parser->lexer);
10207   /* Otherwise, no valid operator name was present.  */
10208   else
10209     {
10210       cp_parser_error (parser, "expected operator");
10211       id = error_mark_node;
10212     }
10213
10214   return id;
10215 }
10216
10217 /* Parse a template-declaration.
10218
10219    template-declaration:
10220      export [opt] template < template-parameter-list > declaration
10221
10222    If MEMBER_P is TRUE, this template-declaration occurs within a
10223    class-specifier.
10224
10225    The grammar rule given by the standard isn't correct.  What
10226    is really meant is:
10227
10228    template-declaration:
10229      export [opt] template-parameter-list-seq
10230        decl-specifier-seq [opt] init-declarator [opt] ;
10231      export [opt] template-parameter-list-seq
10232        function-definition
10233
10234    template-parameter-list-seq:
10235      template-parameter-list-seq [opt]
10236      template < template-parameter-list >  */
10237
10238 static void
10239 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10240 {
10241   /* Check for `export'.  */
10242   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10243     {
10244       /* Consume the `export' token.  */
10245       cp_lexer_consume_token (parser->lexer);
10246       /* Warn that we do not support `export'.  */
10247       warning (0, "keyword %<export%> not implemented, and will be ignored");
10248     }
10249
10250   cp_parser_template_declaration_after_export (parser, member_p);
10251 }
10252
10253 /* Parse a template-parameter-list.
10254
10255    template-parameter-list:
10256      template-parameter
10257      template-parameter-list , template-parameter
10258
10259    Returns a TREE_LIST.  Each node represents a template parameter.
10260    The nodes are connected via their TREE_CHAINs.  */
10261
10262 static tree
10263 cp_parser_template_parameter_list (cp_parser* parser)
10264 {
10265   tree parameter_list = NULL_TREE;
10266
10267   begin_template_parm_list ();
10268   while (true)
10269     {
10270       tree parameter;
10271       bool is_non_type;
10272       bool is_parameter_pack;
10273       location_t parm_loc;
10274
10275       /* Parse the template-parameter.  */
10276       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10277       parameter = cp_parser_template_parameter (parser, 
10278                                                 &is_non_type,
10279                                                 &is_parameter_pack);
10280       /* Add it to the list.  */
10281       if (parameter != error_mark_node)
10282         parameter_list = process_template_parm (parameter_list,
10283                                                 parm_loc,
10284                                                 parameter,
10285                                                 is_non_type,
10286                                                 is_parameter_pack);
10287       else
10288        {
10289          tree err_parm = build_tree_list (parameter, parameter);
10290          TREE_VALUE (err_parm) = error_mark_node;
10291          parameter_list = chainon (parameter_list, err_parm);
10292        }
10293
10294       /* If the next token is not a `,', we're done.  */
10295       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10296         break;
10297       /* Otherwise, consume the `,' token.  */
10298       cp_lexer_consume_token (parser->lexer);
10299     }
10300
10301   return end_template_parm_list (parameter_list);
10302 }
10303
10304 /* Parse a template-parameter.
10305
10306    template-parameter:
10307      type-parameter
10308      parameter-declaration
10309
10310    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10311    the parameter.  The TREE_PURPOSE is the default value, if any.
10312    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10313    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10314    set to true iff this parameter is a parameter pack. */
10315
10316 static tree
10317 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10318                               bool *is_parameter_pack)
10319 {
10320   cp_token *token;
10321   cp_parameter_declarator *parameter_declarator;
10322   cp_declarator *id_declarator;
10323   tree parm;
10324
10325   /* Assume it is a type parameter or a template parameter.  */
10326   *is_non_type = false;
10327   /* Assume it not a parameter pack. */
10328   *is_parameter_pack = false;
10329   /* Peek at the next token.  */
10330   token = cp_lexer_peek_token (parser->lexer);
10331   /* If it is `class' or `template', we have a type-parameter.  */
10332   if (token->keyword == RID_TEMPLATE)
10333     return cp_parser_type_parameter (parser, is_parameter_pack);
10334   /* If it is `class' or `typename' we do not know yet whether it is a
10335      type parameter or a non-type parameter.  Consider:
10336
10337        template <typename T, typename T::X X> ...
10338
10339      or:
10340
10341        template <class C, class D*> ...
10342
10343      Here, the first parameter is a type parameter, and the second is
10344      a non-type parameter.  We can tell by looking at the token after
10345      the identifier -- if it is a `,', `=', or `>' then we have a type
10346      parameter.  */
10347   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10348     {
10349       /* Peek at the token after `class' or `typename'.  */
10350       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10351       /* If it's an ellipsis, we have a template type parameter
10352          pack. */
10353       if (token->type == CPP_ELLIPSIS)
10354         return cp_parser_type_parameter (parser, is_parameter_pack);
10355       /* If it's an identifier, skip it.  */
10356       if (token->type == CPP_NAME)
10357         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10358       /* Now, see if the token looks like the end of a template
10359          parameter.  */
10360       if (token->type == CPP_COMMA
10361           || token->type == CPP_EQ
10362           || token->type == CPP_GREATER)
10363         return cp_parser_type_parameter (parser, is_parameter_pack);
10364     }
10365
10366   /* Otherwise, it is a non-type parameter.
10367
10368      [temp.param]
10369
10370      When parsing a default template-argument for a non-type
10371      template-parameter, the first non-nested `>' is taken as the end
10372      of the template parameter-list rather than a greater-than
10373      operator.  */
10374   *is_non_type = true;
10375   parameter_declarator
10376      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10377                                         /*parenthesized_p=*/NULL);
10378
10379   /* If the parameter declaration is marked as a parameter pack, set
10380      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10381      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10382      grokdeclarator. */
10383   if (parameter_declarator
10384       && parameter_declarator->declarator
10385       && parameter_declarator->declarator->parameter_pack_p)
10386     {
10387       *is_parameter_pack = true;
10388       parameter_declarator->declarator->parameter_pack_p = false;
10389     }
10390
10391   /* If the next token is an ellipsis, and we don't already have it
10392      marked as a parameter pack, then we have a parameter pack (that
10393      has no declarator).  */
10394   if (!*is_parameter_pack
10395       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10396       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10397     {
10398       /* Consume the `...'.  */
10399       cp_lexer_consume_token (parser->lexer);
10400       maybe_warn_variadic_templates ();
10401       
10402       *is_parameter_pack = true;
10403     }
10404   /* We might end up with a pack expansion as the type of the non-type
10405      template parameter, in which case this is a non-type template
10406      parameter pack.  */
10407   else if (parameter_declarator
10408            && parameter_declarator->decl_specifiers.type
10409            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10410     {
10411       *is_parameter_pack = true;
10412       parameter_declarator->decl_specifiers.type = 
10413         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10414     }
10415
10416   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10417     {
10418       /* Parameter packs cannot have default arguments.  However, a
10419          user may try to do so, so we'll parse them and give an
10420          appropriate diagnostic here.  */
10421
10422       /* Consume the `='.  */
10423       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10424       cp_lexer_consume_token (parser->lexer);
10425       
10426       /* Find the name of the parameter pack.  */     
10427       id_declarator = parameter_declarator->declarator;
10428       while (id_declarator && id_declarator->kind != cdk_id)
10429         id_declarator = id_declarator->declarator;
10430       
10431       if (id_declarator && id_declarator->kind == cdk_id)
10432         error_at (start_token->location,
10433                   "template parameter pack %qD cannot have a default argument",
10434                   id_declarator->u.id.unqualified_name);
10435       else
10436         error_at (start_token->location,
10437                   "template parameter pack cannot have a default argument");
10438       
10439       /* Parse the default argument, but throw away the result.  */
10440       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10441     }
10442
10443   parm = grokdeclarator (parameter_declarator->declarator,
10444                          &parameter_declarator->decl_specifiers,
10445                          PARM, /*initialized=*/0,
10446                          /*attrlist=*/NULL);
10447   if (parm == error_mark_node)
10448     return error_mark_node;
10449
10450   return build_tree_list (parameter_declarator->default_argument, parm);
10451 }
10452
10453 /* Parse a type-parameter.
10454
10455    type-parameter:
10456      class identifier [opt]
10457      class identifier [opt] = type-id
10458      typename identifier [opt]
10459      typename identifier [opt] = type-id
10460      template < template-parameter-list > class identifier [opt]
10461      template < template-parameter-list > class identifier [opt]
10462        = id-expression
10463
10464    GNU Extension (variadic templates):
10465
10466    type-parameter:
10467      class ... identifier [opt]
10468      typename ... identifier [opt]
10469
10470    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10471    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10472    the declaration of the parameter.
10473
10474    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10475
10476 static tree
10477 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10478 {
10479   cp_token *token;
10480   tree parameter;
10481
10482   /* Look for a keyword to tell us what kind of parameter this is.  */
10483   token = cp_parser_require (parser, CPP_KEYWORD,
10484                              "%<class%>, %<typename%>, or %<template%>");
10485   if (!token)
10486     return error_mark_node;
10487
10488   switch (token->keyword)
10489     {
10490     case RID_CLASS:
10491     case RID_TYPENAME:
10492       {
10493         tree identifier;
10494         tree default_argument;
10495
10496         /* If the next token is an ellipsis, we have a template
10497            argument pack. */
10498         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10499           {
10500             /* Consume the `...' token. */
10501             cp_lexer_consume_token (parser->lexer);
10502             maybe_warn_variadic_templates ();
10503
10504             *is_parameter_pack = true;
10505           }
10506
10507         /* If the next token is an identifier, then it names the
10508            parameter.  */
10509         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10510           identifier = cp_parser_identifier (parser);
10511         else
10512           identifier = NULL_TREE;
10513
10514         /* Create the parameter.  */
10515         parameter = finish_template_type_parm (class_type_node, identifier);
10516
10517         /* If the next token is an `=', we have a default argument.  */
10518         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10519           {
10520             /* Consume the `=' token.  */
10521             cp_lexer_consume_token (parser->lexer);
10522             /* Parse the default-argument.  */
10523             push_deferring_access_checks (dk_no_deferred);
10524             default_argument = cp_parser_type_id (parser);
10525
10526             /* Template parameter packs cannot have default
10527                arguments. */
10528             if (*is_parameter_pack)
10529               {
10530                 if (identifier)
10531                   error_at (token->location,
10532                             "template parameter pack %qD cannot have a "
10533                             "default argument", identifier);
10534                 else
10535                   error_at (token->location,
10536                             "template parameter packs cannot have "
10537                             "default arguments");
10538                 default_argument = NULL_TREE;
10539               }
10540             pop_deferring_access_checks ();
10541           }
10542         else
10543           default_argument = NULL_TREE;
10544
10545         /* Create the combined representation of the parameter and the
10546            default argument.  */
10547         parameter = build_tree_list (default_argument, parameter);
10548       }
10549       break;
10550
10551     case RID_TEMPLATE:
10552       {
10553         tree parameter_list;
10554         tree identifier;
10555         tree default_argument;
10556
10557         /* Look for the `<'.  */
10558         cp_parser_require (parser, CPP_LESS, "%<<%>");
10559         /* Parse the template-parameter-list.  */
10560         parameter_list = cp_parser_template_parameter_list (parser);
10561         /* Look for the `>'.  */
10562         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10563         /* Look for the `class' keyword.  */
10564         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10565         /* If the next token is an ellipsis, we have a template
10566            argument pack. */
10567         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10568           {
10569             /* Consume the `...' token. */
10570             cp_lexer_consume_token (parser->lexer);
10571             maybe_warn_variadic_templates ();
10572
10573             *is_parameter_pack = true;
10574           }
10575         /* If the next token is an `=', then there is a
10576            default-argument.  If the next token is a `>', we are at
10577            the end of the parameter-list.  If the next token is a `,',
10578            then we are at the end of this parameter.  */
10579         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10580             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10581             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10582           {
10583             identifier = cp_parser_identifier (parser);
10584             /* Treat invalid names as if the parameter were nameless.  */
10585             if (identifier == error_mark_node)
10586               identifier = NULL_TREE;
10587           }
10588         else
10589           identifier = NULL_TREE;
10590
10591         /* Create the template parameter.  */
10592         parameter = finish_template_template_parm (class_type_node,
10593                                                    identifier);
10594
10595         /* If the next token is an `=', then there is a
10596            default-argument.  */
10597         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10598           {
10599             bool is_template;
10600
10601             /* Consume the `='.  */
10602             cp_lexer_consume_token (parser->lexer);
10603             /* Parse the id-expression.  */
10604             push_deferring_access_checks (dk_no_deferred);
10605             /* save token before parsing the id-expression, for error
10606                reporting */
10607             token = cp_lexer_peek_token (parser->lexer);
10608             default_argument
10609               = cp_parser_id_expression (parser,
10610                                          /*template_keyword_p=*/false,
10611                                          /*check_dependency_p=*/true,
10612                                          /*template_p=*/&is_template,
10613                                          /*declarator_p=*/false,
10614                                          /*optional_p=*/false);
10615             if (TREE_CODE (default_argument) == TYPE_DECL)
10616               /* If the id-expression was a template-id that refers to
10617                  a template-class, we already have the declaration here,
10618                  so no further lookup is needed.  */
10619                  ;
10620             else
10621               /* Look up the name.  */
10622               default_argument
10623                 = cp_parser_lookup_name (parser, default_argument,
10624                                          none_type,
10625                                          /*is_template=*/is_template,
10626                                          /*is_namespace=*/false,
10627                                          /*check_dependency=*/true,
10628                                          /*ambiguous_decls=*/NULL,
10629                                          token->location);
10630             /* See if the default argument is valid.  */
10631             default_argument
10632               = check_template_template_default_arg (default_argument);
10633
10634             /* Template parameter packs cannot have default
10635                arguments. */
10636             if (*is_parameter_pack)
10637               {
10638                 if (identifier)
10639                   error_at (token->location,
10640                             "template parameter pack %qD cannot "
10641                             "have a default argument",
10642                             identifier);
10643                 else
10644                   error_at (token->location, "template parameter packs cannot "
10645                             "have default arguments");
10646                 default_argument = NULL_TREE;
10647               }
10648             pop_deferring_access_checks ();
10649           }
10650         else
10651           default_argument = NULL_TREE;
10652
10653         /* Create the combined representation of the parameter and the
10654            default argument.  */
10655         parameter = build_tree_list (default_argument, parameter);
10656       }
10657       break;
10658
10659     default:
10660       gcc_unreachable ();
10661       break;
10662     }
10663
10664   return parameter;
10665 }
10666
10667 /* Parse a template-id.
10668
10669    template-id:
10670      template-name < template-argument-list [opt] >
10671
10672    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10673    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10674    returned.  Otherwise, if the template-name names a function, or set
10675    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10676    names a class, returns a TYPE_DECL for the specialization.
10677
10678    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10679    uninstantiated templates.  */
10680
10681 static tree
10682 cp_parser_template_id (cp_parser *parser,
10683                        bool template_keyword_p,
10684                        bool check_dependency_p,
10685                        bool is_declaration)
10686 {
10687   int i;
10688   tree templ;
10689   tree arguments;
10690   tree template_id;
10691   cp_token_position start_of_id = 0;
10692   deferred_access_check *chk;
10693   VEC (deferred_access_check,gc) *access_check;
10694   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10695   bool is_identifier;
10696
10697   /* If the next token corresponds to a template-id, there is no need
10698      to reparse it.  */
10699   next_token = cp_lexer_peek_token (parser->lexer);
10700   if (next_token->type == CPP_TEMPLATE_ID)
10701     {
10702       struct tree_check *check_value;
10703
10704       /* Get the stored value.  */
10705       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10706       /* Perform any access checks that were deferred.  */
10707       access_check = check_value->checks;
10708       if (access_check)
10709         {
10710           for (i = 0 ;
10711                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10712                ++i)
10713             {
10714               perform_or_defer_access_check (chk->binfo,
10715                                              chk->decl,
10716                                              chk->diag_decl);
10717             }
10718         }
10719       /* Return the stored value.  */
10720       return check_value->value;
10721     }
10722
10723   /* Avoid performing name lookup if there is no possibility of
10724      finding a template-id.  */
10725   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10726       || (next_token->type == CPP_NAME
10727           && !cp_parser_nth_token_starts_template_argument_list_p
10728                (parser, 2)))
10729     {
10730       cp_parser_error (parser, "expected template-id");
10731       return error_mark_node;
10732     }
10733
10734   /* Remember where the template-id starts.  */
10735   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10736     start_of_id = cp_lexer_token_position (parser->lexer, false);
10737
10738   push_deferring_access_checks (dk_deferred);
10739
10740   /* Parse the template-name.  */
10741   is_identifier = false;
10742   token = cp_lexer_peek_token (parser->lexer);
10743   templ = cp_parser_template_name (parser, template_keyword_p,
10744                                    check_dependency_p,
10745                                    is_declaration,
10746                                    &is_identifier);
10747   if (templ == error_mark_node || is_identifier)
10748     {
10749       pop_deferring_access_checks ();
10750       return templ;
10751     }
10752
10753   /* If we find the sequence `[:' after a template-name, it's probably
10754      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10755      parse correctly the argument list.  */
10756   next_token = cp_lexer_peek_token (parser->lexer);
10757   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10758   if (next_token->type == CPP_OPEN_SQUARE
10759       && next_token->flags & DIGRAPH
10760       && next_token_2->type == CPP_COLON
10761       && !(next_token_2->flags & PREV_WHITE))
10762     {
10763       cp_parser_parse_tentatively (parser);
10764       /* Change `:' into `::'.  */
10765       next_token_2->type = CPP_SCOPE;
10766       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10767          CPP_LESS.  */
10768       cp_lexer_consume_token (parser->lexer);
10769
10770       /* Parse the arguments.  */
10771       arguments = cp_parser_enclosed_template_argument_list (parser);
10772       if (!cp_parser_parse_definitely (parser))
10773         {
10774           /* If we couldn't parse an argument list, then we revert our changes
10775              and return simply an error. Maybe this is not a template-id
10776              after all.  */
10777           next_token_2->type = CPP_COLON;
10778           cp_parser_error (parser, "expected %<<%>");
10779           pop_deferring_access_checks ();
10780           return error_mark_node;
10781         }
10782       /* Otherwise, emit an error about the invalid digraph, but continue
10783          parsing because we got our argument list.  */
10784       if (permerror (next_token->location,
10785                      "%<<::%> cannot begin a template-argument list"))
10786         {
10787           static bool hint = false;
10788           inform (next_token->location,
10789                   "%<<:%> is an alternate spelling for %<[%>."
10790                   " Insert whitespace between %<<%> and %<::%>");
10791           if (!hint && !flag_permissive)
10792             {
10793               inform (next_token->location, "(if you use %<-fpermissive%>"
10794                       " G++ will accept your code)");
10795               hint = true;
10796             }
10797         }
10798     }
10799   else
10800     {
10801       /* Look for the `<' that starts the template-argument-list.  */
10802       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10803         {
10804           pop_deferring_access_checks ();
10805           return error_mark_node;
10806         }
10807       /* Parse the arguments.  */
10808       arguments = cp_parser_enclosed_template_argument_list (parser);
10809     }
10810
10811   /* Build a representation of the specialization.  */
10812   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10813     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10814   else if (DECL_CLASS_TEMPLATE_P (templ)
10815            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10816     {
10817       bool entering_scope;
10818       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10819          template (rather than some instantiation thereof) only if
10820          is not nested within some other construct.  For example, in
10821          "template <typename T> void f(T) { A<T>::", A<T> is just an
10822          instantiation of A.  */
10823       entering_scope = (template_parm_scope_p ()
10824                         && cp_lexer_next_token_is (parser->lexer,
10825                                                    CPP_SCOPE));
10826       template_id
10827         = finish_template_type (templ, arguments, entering_scope);
10828     }
10829   else
10830     {
10831       /* If it's not a class-template or a template-template, it should be
10832          a function-template.  */
10833       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10834                    || TREE_CODE (templ) == OVERLOAD
10835                    || BASELINK_P (templ)));
10836
10837       template_id = lookup_template_function (templ, arguments);
10838     }
10839
10840   /* If parsing tentatively, replace the sequence of tokens that makes
10841      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10842      should we re-parse the token stream, we will not have to repeat
10843      the effort required to do the parse, nor will we issue duplicate
10844      error messages about problems during instantiation of the
10845      template.  */
10846   if (start_of_id)
10847     {
10848       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10849
10850       /* Reset the contents of the START_OF_ID token.  */
10851       token->type = CPP_TEMPLATE_ID;
10852       /* Retrieve any deferred checks.  Do not pop this access checks yet
10853          so the memory will not be reclaimed during token replacing below.  */
10854       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10855       token->u.tree_check_value->value = template_id;
10856       token->u.tree_check_value->checks = get_deferred_access_checks ();
10857       token->keyword = RID_MAX;
10858
10859       /* Purge all subsequent tokens.  */
10860       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10861
10862       /* ??? Can we actually assume that, if template_id ==
10863          error_mark_node, we will have issued a diagnostic to the
10864          user, as opposed to simply marking the tentative parse as
10865          failed?  */
10866       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10867         error_at (token->location, "parse error in template argument list");
10868     }
10869
10870   pop_deferring_access_checks ();
10871   return template_id;
10872 }
10873
10874 /* Parse a template-name.
10875
10876    template-name:
10877      identifier
10878
10879    The standard should actually say:
10880
10881    template-name:
10882      identifier
10883      operator-function-id
10884
10885    A defect report has been filed about this issue.
10886
10887    A conversion-function-id cannot be a template name because they cannot
10888    be part of a template-id. In fact, looking at this code:
10889
10890    a.operator K<int>()
10891
10892    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10893    It is impossible to call a templated conversion-function-id with an
10894    explicit argument list, since the only allowed template parameter is
10895    the type to which it is converting.
10896
10897    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10898    `template' keyword, in a construction like:
10899
10900      T::template f<3>()
10901
10902    In that case `f' is taken to be a template-name, even though there
10903    is no way of knowing for sure.
10904
10905    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10906    name refers to a set of overloaded functions, at least one of which
10907    is a template, or an IDENTIFIER_NODE with the name of the template,
10908    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10909    names are looked up inside uninstantiated templates.  */
10910
10911 static tree
10912 cp_parser_template_name (cp_parser* parser,
10913                          bool template_keyword_p,
10914                          bool check_dependency_p,
10915                          bool is_declaration,
10916                          bool *is_identifier)
10917 {
10918   tree identifier;
10919   tree decl;
10920   tree fns;
10921   cp_token *token = cp_lexer_peek_token (parser->lexer);
10922
10923   /* If the next token is `operator', then we have either an
10924      operator-function-id or a conversion-function-id.  */
10925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10926     {
10927       /* We don't know whether we're looking at an
10928          operator-function-id or a conversion-function-id.  */
10929       cp_parser_parse_tentatively (parser);
10930       /* Try an operator-function-id.  */
10931       identifier = cp_parser_operator_function_id (parser);
10932       /* If that didn't work, try a conversion-function-id.  */
10933       if (!cp_parser_parse_definitely (parser))
10934         {
10935           cp_parser_error (parser, "expected template-name");
10936           return error_mark_node;
10937         }
10938     }
10939   /* Look for the identifier.  */
10940   else
10941     identifier = cp_parser_identifier (parser);
10942
10943   /* If we didn't find an identifier, we don't have a template-id.  */
10944   if (identifier == error_mark_node)
10945     return error_mark_node;
10946
10947   /* If the name immediately followed the `template' keyword, then it
10948      is a template-name.  However, if the next token is not `<', then
10949      we do not treat it as a template-name, since it is not being used
10950      as part of a template-id.  This enables us to handle constructs
10951      like:
10952
10953        template <typename T> struct S { S(); };
10954        template <typename T> S<T>::S();
10955
10956      correctly.  We would treat `S' as a template -- if it were `S<T>'
10957      -- but we do not if there is no `<'.  */
10958
10959   if (processing_template_decl
10960       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10961     {
10962       /* In a declaration, in a dependent context, we pretend that the
10963          "template" keyword was present in order to improve error
10964          recovery.  For example, given:
10965
10966            template <typename T> void f(T::X<int>);
10967
10968          we want to treat "X<int>" as a template-id.  */
10969       if (is_declaration
10970           && !template_keyword_p
10971           && parser->scope && TYPE_P (parser->scope)
10972           && check_dependency_p
10973           && dependent_scope_p (parser->scope)
10974           /* Do not do this for dtors (or ctors), since they never
10975              need the template keyword before their name.  */
10976           && !constructor_name_p (identifier, parser->scope))
10977         {
10978           cp_token_position start = 0;
10979
10980           /* Explain what went wrong.  */
10981           error_at (token->location, "non-template %qD used as template",
10982                     identifier);
10983           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
10984                   parser->scope, identifier);
10985           /* If parsing tentatively, find the location of the "<" token.  */
10986           if (cp_parser_simulate_error (parser))
10987             start = cp_lexer_token_position (parser->lexer, true);
10988           /* Parse the template arguments so that we can issue error
10989              messages about them.  */
10990           cp_lexer_consume_token (parser->lexer);
10991           cp_parser_enclosed_template_argument_list (parser);
10992           /* Skip tokens until we find a good place from which to
10993              continue parsing.  */
10994           cp_parser_skip_to_closing_parenthesis (parser,
10995                                                  /*recovering=*/true,
10996                                                  /*or_comma=*/true,
10997                                                  /*consume_paren=*/false);
10998           /* If parsing tentatively, permanently remove the
10999              template argument list.  That will prevent duplicate
11000              error messages from being issued about the missing
11001              "template" keyword.  */
11002           if (start)
11003             cp_lexer_purge_tokens_after (parser->lexer, start);
11004           if (is_identifier)
11005             *is_identifier = true;
11006           return identifier;
11007         }
11008
11009       /* If the "template" keyword is present, then there is generally
11010          no point in doing name-lookup, so we just return IDENTIFIER.
11011          But, if the qualifying scope is non-dependent then we can
11012          (and must) do name-lookup normally.  */
11013       if (template_keyword_p
11014           && (!parser->scope
11015               || (TYPE_P (parser->scope)
11016                   && dependent_type_p (parser->scope))))
11017         return identifier;
11018     }
11019
11020   /* Look up the name.  */
11021   decl = cp_parser_lookup_name (parser, identifier,
11022                                 none_type,
11023                                 /*is_template=*/false,
11024                                 /*is_namespace=*/false,
11025                                 check_dependency_p,
11026                                 /*ambiguous_decls=*/NULL,
11027                                 token->location);
11028   decl = maybe_get_template_decl_from_type_decl (decl);
11029
11030   /* If DECL is a template, then the name was a template-name.  */
11031   if (TREE_CODE (decl) == TEMPLATE_DECL)
11032     ;
11033   else
11034     {
11035       tree fn = NULL_TREE;
11036
11037       /* The standard does not explicitly indicate whether a name that
11038          names a set of overloaded declarations, some of which are
11039          templates, is a template-name.  However, such a name should
11040          be a template-name; otherwise, there is no way to form a
11041          template-id for the overloaded templates.  */
11042       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11043       if (TREE_CODE (fns) == OVERLOAD)
11044         for (fn = fns; fn; fn = OVL_NEXT (fn))
11045           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11046             break;
11047
11048       if (!fn)
11049         {
11050           /* The name does not name a template.  */
11051           cp_parser_error (parser, "expected template-name");
11052           return error_mark_node;
11053         }
11054     }
11055
11056   /* If DECL is dependent, and refers to a function, then just return
11057      its name; we will look it up again during template instantiation.  */
11058   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11059     {
11060       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11061       if (TYPE_P (scope) && dependent_type_p (scope))
11062         return identifier;
11063     }
11064
11065   return decl;
11066 }
11067
11068 /* Parse a template-argument-list.
11069
11070    template-argument-list:
11071      template-argument ... [opt]
11072      template-argument-list , template-argument ... [opt]
11073
11074    Returns a TREE_VEC containing the arguments.  */
11075
11076 static tree
11077 cp_parser_template_argument_list (cp_parser* parser)
11078 {
11079   tree fixed_args[10];
11080   unsigned n_args = 0;
11081   unsigned alloced = 10;
11082   tree *arg_ary = fixed_args;
11083   tree vec;
11084   bool saved_in_template_argument_list_p;
11085   bool saved_ice_p;
11086   bool saved_non_ice_p;
11087
11088   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11089   parser->in_template_argument_list_p = true;
11090   /* Even if the template-id appears in an integral
11091      constant-expression, the contents of the argument list do
11092      not.  */
11093   saved_ice_p = parser->integral_constant_expression_p;
11094   parser->integral_constant_expression_p = false;
11095   saved_non_ice_p = parser->non_integral_constant_expression_p;
11096   parser->non_integral_constant_expression_p = false;
11097   /* Parse the arguments.  */
11098   do
11099     {
11100       tree argument;
11101
11102       if (n_args)
11103         /* Consume the comma.  */
11104         cp_lexer_consume_token (parser->lexer);
11105
11106       /* Parse the template-argument.  */
11107       argument = cp_parser_template_argument (parser);
11108
11109       /* If the next token is an ellipsis, we're expanding a template
11110          argument pack. */
11111       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11112         {
11113           if (argument == error_mark_node)
11114             {
11115               cp_token *token = cp_lexer_peek_token (parser->lexer);
11116               error_at (token->location,
11117                         "expected parameter pack before %<...%>");
11118             }
11119           /* Consume the `...' token. */
11120           cp_lexer_consume_token (parser->lexer);
11121
11122           /* Make the argument into a TYPE_PACK_EXPANSION or
11123              EXPR_PACK_EXPANSION. */
11124           argument = make_pack_expansion (argument);
11125         }
11126
11127       if (n_args == alloced)
11128         {
11129           alloced *= 2;
11130
11131           if (arg_ary == fixed_args)
11132             {
11133               arg_ary = XNEWVEC (tree, alloced);
11134               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11135             }
11136           else
11137             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11138         }
11139       arg_ary[n_args++] = argument;
11140     }
11141   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11142
11143   vec = make_tree_vec (n_args);
11144
11145   while (n_args--)
11146     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11147
11148   if (arg_ary != fixed_args)
11149     free (arg_ary);
11150   parser->non_integral_constant_expression_p = saved_non_ice_p;
11151   parser->integral_constant_expression_p = saved_ice_p;
11152   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11153   return vec;
11154 }
11155
11156 /* Parse a template-argument.
11157
11158    template-argument:
11159      assignment-expression
11160      type-id
11161      id-expression
11162
11163    The representation is that of an assignment-expression, type-id, or
11164    id-expression -- except that the qualified id-expression is
11165    evaluated, so that the value returned is either a DECL or an
11166    OVERLOAD.
11167
11168    Although the standard says "assignment-expression", it forbids
11169    throw-expressions or assignments in the template argument.
11170    Therefore, we use "conditional-expression" instead.  */
11171
11172 static tree
11173 cp_parser_template_argument (cp_parser* parser)
11174 {
11175   tree argument;
11176   bool template_p;
11177   bool address_p;
11178   bool maybe_type_id = false;
11179   cp_token *token = NULL, *argument_start_token = NULL;
11180   cp_id_kind idk;
11181
11182   /* There's really no way to know what we're looking at, so we just
11183      try each alternative in order.
11184
11185        [temp.arg]
11186
11187        In a template-argument, an ambiguity between a type-id and an
11188        expression is resolved to a type-id, regardless of the form of
11189        the corresponding template-parameter.
11190
11191      Therefore, we try a type-id first.  */
11192   cp_parser_parse_tentatively (parser);
11193   argument = cp_parser_template_type_arg (parser);
11194   /* If there was no error parsing the type-id but the next token is a
11195      '>>', our behavior depends on which dialect of C++ we're
11196      parsing. In C++98, we probably found a typo for '> >'. But there
11197      are type-id which are also valid expressions. For instance:
11198
11199      struct X { int operator >> (int); };
11200      template <int V> struct Foo {};
11201      Foo<X () >> 5> r;
11202
11203      Here 'X()' is a valid type-id of a function type, but the user just
11204      wanted to write the expression "X() >> 5". Thus, we remember that we
11205      found a valid type-id, but we still try to parse the argument as an
11206      expression to see what happens. 
11207
11208      In C++0x, the '>>' will be considered two separate '>'
11209      tokens.  */
11210   if (!cp_parser_error_occurred (parser)
11211       && cxx_dialect == cxx98
11212       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11213     {
11214       maybe_type_id = true;
11215       cp_parser_abort_tentative_parse (parser);
11216     }
11217   else
11218     {
11219       /* If the next token isn't a `,' or a `>', then this argument wasn't
11220       really finished. This means that the argument is not a valid
11221       type-id.  */
11222       if (!cp_parser_next_token_ends_template_argument_p (parser))
11223         cp_parser_error (parser, "expected template-argument");
11224       /* If that worked, we're done.  */
11225       if (cp_parser_parse_definitely (parser))
11226         return argument;
11227     }
11228   /* We're still not sure what the argument will be.  */
11229   cp_parser_parse_tentatively (parser);
11230   /* Try a template.  */
11231   argument_start_token = cp_lexer_peek_token (parser->lexer);
11232   argument = cp_parser_id_expression (parser,
11233                                       /*template_keyword_p=*/false,
11234                                       /*check_dependency_p=*/true,
11235                                       &template_p,
11236                                       /*declarator_p=*/false,
11237                                       /*optional_p=*/false);
11238   /* If the next token isn't a `,' or a `>', then this argument wasn't
11239      really finished.  */
11240   if (!cp_parser_next_token_ends_template_argument_p (parser))
11241     cp_parser_error (parser, "expected template-argument");
11242   if (!cp_parser_error_occurred (parser))
11243     {
11244       /* Figure out what is being referred to.  If the id-expression
11245          was for a class template specialization, then we will have a
11246          TYPE_DECL at this point.  There is no need to do name lookup
11247          at this point in that case.  */
11248       if (TREE_CODE (argument) != TYPE_DECL)
11249         argument = cp_parser_lookup_name (parser, argument,
11250                                           none_type,
11251                                           /*is_template=*/template_p,
11252                                           /*is_namespace=*/false,
11253                                           /*check_dependency=*/true,
11254                                           /*ambiguous_decls=*/NULL,
11255                                           argument_start_token->location);
11256       if (TREE_CODE (argument) != TEMPLATE_DECL
11257           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11258         cp_parser_error (parser, "expected template-name");
11259     }
11260   if (cp_parser_parse_definitely (parser))
11261     return argument;
11262   /* It must be a non-type argument.  There permitted cases are given
11263      in [temp.arg.nontype]:
11264
11265      -- an integral constant-expression of integral or enumeration
11266         type; or
11267
11268      -- the name of a non-type template-parameter; or
11269
11270      -- the name of an object or function with external linkage...
11271
11272      -- the address of an object or function with external linkage...
11273
11274      -- a pointer to member...  */
11275   /* Look for a non-type template parameter.  */
11276   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11277     {
11278       cp_parser_parse_tentatively (parser);
11279       argument = cp_parser_primary_expression (parser,
11280                                                /*address_p=*/false,
11281                                                /*cast_p=*/false,
11282                                                /*template_arg_p=*/true,
11283                                                &idk);
11284       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11285           || !cp_parser_next_token_ends_template_argument_p (parser))
11286         cp_parser_simulate_error (parser);
11287       if (cp_parser_parse_definitely (parser))
11288         return argument;
11289     }
11290
11291   /* If the next token is "&", the argument must be the address of an
11292      object or function with external linkage.  */
11293   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11294   if (address_p)
11295     cp_lexer_consume_token (parser->lexer);
11296   /* See if we might have an id-expression.  */
11297   token = cp_lexer_peek_token (parser->lexer);
11298   if (token->type == CPP_NAME
11299       || token->keyword == RID_OPERATOR
11300       || token->type == CPP_SCOPE
11301       || token->type == CPP_TEMPLATE_ID
11302       || token->type == CPP_NESTED_NAME_SPECIFIER)
11303     {
11304       cp_parser_parse_tentatively (parser);
11305       argument = cp_parser_primary_expression (parser,
11306                                                address_p,
11307                                                /*cast_p=*/false,
11308                                                /*template_arg_p=*/true,
11309                                                &idk);
11310       if (cp_parser_error_occurred (parser)
11311           || !cp_parser_next_token_ends_template_argument_p (parser))
11312         cp_parser_abort_tentative_parse (parser);
11313       else
11314         {
11315           if (TREE_CODE (argument) == INDIRECT_REF)
11316             {
11317               gcc_assert (REFERENCE_REF_P (argument));
11318               argument = TREE_OPERAND (argument, 0);
11319             }
11320
11321           if (TREE_CODE (argument) == VAR_DECL)
11322             {
11323               /* A variable without external linkage might still be a
11324                  valid constant-expression, so no error is issued here
11325                  if the external-linkage check fails.  */
11326               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11327                 cp_parser_simulate_error (parser);
11328             }
11329           else if (is_overloaded_fn (argument))
11330             /* All overloaded functions are allowed; if the external
11331                linkage test does not pass, an error will be issued
11332                later.  */
11333             ;
11334           else if (address_p
11335                    && (TREE_CODE (argument) == OFFSET_REF
11336                        || TREE_CODE (argument) == SCOPE_REF))
11337             /* A pointer-to-member.  */
11338             ;
11339           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11340             ;
11341           else
11342             cp_parser_simulate_error (parser);
11343
11344           if (cp_parser_parse_definitely (parser))
11345             {
11346               if (address_p)
11347                 argument = build_x_unary_op (ADDR_EXPR, argument,
11348                                              tf_warning_or_error);
11349               return argument;
11350             }
11351         }
11352     }
11353   /* If the argument started with "&", there are no other valid
11354      alternatives at this point.  */
11355   if (address_p)
11356     {
11357       cp_parser_error (parser, "invalid non-type template argument");
11358       return error_mark_node;
11359     }
11360
11361   /* If the argument wasn't successfully parsed as a type-id followed
11362      by '>>', the argument can only be a constant expression now.
11363      Otherwise, we try parsing the constant-expression tentatively,
11364      because the argument could really be a type-id.  */
11365   if (maybe_type_id)
11366     cp_parser_parse_tentatively (parser);
11367   argument = cp_parser_constant_expression (parser,
11368                                             /*allow_non_constant_p=*/false,
11369                                             /*non_constant_p=*/NULL);
11370   argument = fold_non_dependent_expr (argument);
11371   if (!maybe_type_id)
11372     return argument;
11373   if (!cp_parser_next_token_ends_template_argument_p (parser))
11374     cp_parser_error (parser, "expected template-argument");
11375   if (cp_parser_parse_definitely (parser))
11376     return argument;
11377   /* We did our best to parse the argument as a non type-id, but that
11378      was the only alternative that matched (albeit with a '>' after
11379      it). We can assume it's just a typo from the user, and a
11380      diagnostic will then be issued.  */
11381   return cp_parser_template_type_arg (parser);
11382 }
11383
11384 /* Parse an explicit-instantiation.
11385
11386    explicit-instantiation:
11387      template declaration
11388
11389    Although the standard says `declaration', what it really means is:
11390
11391    explicit-instantiation:
11392      template decl-specifier-seq [opt] declarator [opt] ;
11393
11394    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11395    supposed to be allowed.  A defect report has been filed about this
11396    issue.
11397
11398    GNU Extension:
11399
11400    explicit-instantiation:
11401      storage-class-specifier template
11402        decl-specifier-seq [opt] declarator [opt] ;
11403      function-specifier template
11404        decl-specifier-seq [opt] declarator [opt] ;  */
11405
11406 static void
11407 cp_parser_explicit_instantiation (cp_parser* parser)
11408 {
11409   int declares_class_or_enum;
11410   cp_decl_specifier_seq decl_specifiers;
11411   tree extension_specifier = NULL_TREE;
11412   cp_token *token;
11413
11414   /* Look for an (optional) storage-class-specifier or
11415      function-specifier.  */
11416   if (cp_parser_allow_gnu_extensions_p (parser))
11417     {
11418       extension_specifier
11419         = cp_parser_storage_class_specifier_opt (parser);
11420       if (!extension_specifier)
11421         extension_specifier
11422           = cp_parser_function_specifier_opt (parser,
11423                                               /*decl_specs=*/NULL);
11424     }
11425
11426   /* Look for the `template' keyword.  */
11427   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11428   /* Let the front end know that we are processing an explicit
11429      instantiation.  */
11430   begin_explicit_instantiation ();
11431   /* [temp.explicit] says that we are supposed to ignore access
11432      control while processing explicit instantiation directives.  */
11433   push_deferring_access_checks (dk_no_check);
11434   /* Parse a decl-specifier-seq.  */
11435   token = cp_lexer_peek_token (parser->lexer);
11436   cp_parser_decl_specifier_seq (parser,
11437                                 CP_PARSER_FLAGS_OPTIONAL,
11438                                 &decl_specifiers,
11439                                 &declares_class_or_enum);
11440   /* If there was exactly one decl-specifier, and it declared a class,
11441      and there's no declarator, then we have an explicit type
11442      instantiation.  */
11443   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11444     {
11445       tree type;
11446
11447       type = check_tag_decl (&decl_specifiers);
11448       /* Turn access control back on for names used during
11449          template instantiation.  */
11450       pop_deferring_access_checks ();
11451       if (type)
11452         do_type_instantiation (type, extension_specifier,
11453                                /*complain=*/tf_error);
11454     }
11455   else
11456     {
11457       cp_declarator *declarator;
11458       tree decl;
11459
11460       /* Parse the declarator.  */
11461       declarator
11462         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11463                                 /*ctor_dtor_or_conv_p=*/NULL,
11464                                 /*parenthesized_p=*/NULL,
11465                                 /*member_p=*/false);
11466       if (declares_class_or_enum & 2)
11467         cp_parser_check_for_definition_in_return_type (declarator,
11468                                                        decl_specifiers.type,
11469                                                        decl_specifiers.type_location);
11470       if (declarator != cp_error_declarator)
11471         {
11472           decl = grokdeclarator (declarator, &decl_specifiers,
11473                                  NORMAL, 0, &decl_specifiers.attributes);
11474           /* Turn access control back on for names used during
11475              template instantiation.  */
11476           pop_deferring_access_checks ();
11477           /* Do the explicit instantiation.  */
11478           do_decl_instantiation (decl, extension_specifier);
11479         }
11480       else
11481         {
11482           pop_deferring_access_checks ();
11483           /* Skip the body of the explicit instantiation.  */
11484           cp_parser_skip_to_end_of_statement (parser);
11485         }
11486     }
11487   /* We're done with the instantiation.  */
11488   end_explicit_instantiation ();
11489
11490   cp_parser_consume_semicolon_at_end_of_statement (parser);
11491 }
11492
11493 /* Parse an explicit-specialization.
11494
11495    explicit-specialization:
11496      template < > declaration
11497
11498    Although the standard says `declaration', what it really means is:
11499
11500    explicit-specialization:
11501      template <> decl-specifier [opt] init-declarator [opt] ;
11502      template <> function-definition
11503      template <> explicit-specialization
11504      template <> template-declaration  */
11505
11506 static void
11507 cp_parser_explicit_specialization (cp_parser* parser)
11508 {
11509   bool need_lang_pop;
11510   cp_token *token = cp_lexer_peek_token (parser->lexer);
11511
11512   /* Look for the `template' keyword.  */
11513   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11514   /* Look for the `<'.  */
11515   cp_parser_require (parser, CPP_LESS, "%<<%>");
11516   /* Look for the `>'.  */
11517   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11518   /* We have processed another parameter list.  */
11519   ++parser->num_template_parameter_lists;
11520   /* [temp]
11521
11522      A template ... explicit specialization ... shall not have C
11523      linkage.  */
11524   if (current_lang_name == lang_name_c)
11525     {
11526       error_at (token->location, "template specialization with C linkage");
11527       /* Give it C++ linkage to avoid confusing other parts of the
11528          front end.  */
11529       push_lang_context (lang_name_cplusplus);
11530       need_lang_pop = true;
11531     }
11532   else
11533     need_lang_pop = false;
11534   /* Let the front end know that we are beginning a specialization.  */
11535   if (!begin_specialization ())
11536     {
11537       end_specialization ();
11538       return;
11539     }
11540
11541   /* If the next keyword is `template', we need to figure out whether
11542      or not we're looking a template-declaration.  */
11543   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11544     {
11545       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11546           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11547         cp_parser_template_declaration_after_export (parser,
11548                                                      /*member_p=*/false);
11549       else
11550         cp_parser_explicit_specialization (parser);
11551     }
11552   else
11553     /* Parse the dependent declaration.  */
11554     cp_parser_single_declaration (parser,
11555                                   /*checks=*/NULL,
11556                                   /*member_p=*/false,
11557                                   /*explicit_specialization_p=*/true,
11558                                   /*friend_p=*/NULL);
11559   /* We're done with the specialization.  */
11560   end_specialization ();
11561   /* For the erroneous case of a template with C linkage, we pushed an
11562      implicit C++ linkage scope; exit that scope now.  */
11563   if (need_lang_pop)
11564     pop_lang_context ();
11565   /* We're done with this parameter list.  */
11566   --parser->num_template_parameter_lists;
11567 }
11568
11569 /* Parse a type-specifier.
11570
11571    type-specifier:
11572      simple-type-specifier
11573      class-specifier
11574      enum-specifier
11575      elaborated-type-specifier
11576      cv-qualifier
11577
11578    GNU Extension:
11579
11580    type-specifier:
11581      __complex__
11582
11583    Returns a representation of the type-specifier.  For a
11584    class-specifier, enum-specifier, or elaborated-type-specifier, a
11585    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11586
11587    The parser flags FLAGS is used to control type-specifier parsing.
11588
11589    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11590    in a decl-specifier-seq.
11591
11592    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11593    class-specifier, enum-specifier, or elaborated-type-specifier, then
11594    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11595    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11596    zero.
11597
11598    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11599    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11600    is set to FALSE.  */
11601
11602 static tree
11603 cp_parser_type_specifier (cp_parser* parser,
11604                           cp_parser_flags flags,
11605                           cp_decl_specifier_seq *decl_specs,
11606                           bool is_declaration,
11607                           int* declares_class_or_enum,
11608                           bool* is_cv_qualifier)
11609 {
11610   tree type_spec = NULL_TREE;
11611   cp_token *token;
11612   enum rid keyword;
11613   cp_decl_spec ds = ds_last;
11614
11615   /* Assume this type-specifier does not declare a new type.  */
11616   if (declares_class_or_enum)
11617     *declares_class_or_enum = 0;
11618   /* And that it does not specify a cv-qualifier.  */
11619   if (is_cv_qualifier)
11620     *is_cv_qualifier = false;
11621   /* Peek at the next token.  */
11622   token = cp_lexer_peek_token (parser->lexer);
11623
11624   /* If we're looking at a keyword, we can use that to guide the
11625      production we choose.  */
11626   keyword = token->keyword;
11627   switch (keyword)
11628     {
11629     case RID_ENUM:
11630       /* Look for the enum-specifier.  */
11631       type_spec = cp_parser_enum_specifier (parser);
11632       /* If that worked, we're done.  */
11633       if (type_spec)
11634         {
11635           if (declares_class_or_enum)
11636             *declares_class_or_enum = 2;
11637           if (decl_specs)
11638             cp_parser_set_decl_spec_type (decl_specs,
11639                                           type_spec,
11640                                           token->location,
11641                                           /*user_defined_p=*/true);
11642           return type_spec;
11643         }
11644       else
11645         goto elaborated_type_specifier;
11646
11647       /* Any of these indicate either a class-specifier, or an
11648          elaborated-type-specifier.  */
11649     case RID_CLASS:
11650     case RID_STRUCT:
11651     case RID_UNION:
11652       /* Parse tentatively so that we can back up if we don't find a
11653          class-specifier.  */
11654       cp_parser_parse_tentatively (parser);
11655       /* Look for the class-specifier.  */
11656       type_spec = cp_parser_class_specifier (parser);
11657       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11658       /* If that worked, we're done.  */
11659       if (cp_parser_parse_definitely (parser))
11660         {
11661           if (declares_class_or_enum)
11662             *declares_class_or_enum = 2;
11663           if (decl_specs)
11664             cp_parser_set_decl_spec_type (decl_specs,
11665                                           type_spec,
11666                                           token->location,
11667                                           /*user_defined_p=*/true);
11668           return type_spec;
11669         }
11670
11671       /* Fall through.  */
11672     elaborated_type_specifier:
11673       /* We're declaring (not defining) a class or enum.  */
11674       if (declares_class_or_enum)
11675         *declares_class_or_enum = 1;
11676
11677       /* Fall through.  */
11678     case RID_TYPENAME:
11679       /* Look for an elaborated-type-specifier.  */
11680       type_spec
11681         = (cp_parser_elaborated_type_specifier
11682            (parser,
11683             decl_specs && decl_specs->specs[(int) ds_friend],
11684             is_declaration));
11685       if (decl_specs)
11686         cp_parser_set_decl_spec_type (decl_specs,
11687                                       type_spec,
11688                                       token->location,
11689                                       /*user_defined_p=*/true);
11690       return type_spec;
11691
11692     case RID_CONST:
11693       ds = ds_const;
11694       if (is_cv_qualifier)
11695         *is_cv_qualifier = true;
11696       break;
11697
11698     case RID_VOLATILE:
11699       ds = ds_volatile;
11700       if (is_cv_qualifier)
11701         *is_cv_qualifier = true;
11702       break;
11703
11704     case RID_RESTRICT:
11705       ds = ds_restrict;
11706       if (is_cv_qualifier)
11707         *is_cv_qualifier = true;
11708       break;
11709
11710     case RID_COMPLEX:
11711       /* The `__complex__' keyword is a GNU extension.  */
11712       ds = ds_complex;
11713       break;
11714
11715     default:
11716       break;
11717     }
11718
11719   /* Handle simple keywords.  */
11720   if (ds != ds_last)
11721     {
11722       if (decl_specs)
11723         {
11724           ++decl_specs->specs[(int)ds];
11725           decl_specs->any_specifiers_p = true;
11726         }
11727       return cp_lexer_consume_token (parser->lexer)->u.value;
11728     }
11729
11730   /* If we do not already have a type-specifier, assume we are looking
11731      at a simple-type-specifier.  */
11732   type_spec = cp_parser_simple_type_specifier (parser,
11733                                                decl_specs,
11734                                                flags);
11735
11736   /* If we didn't find a type-specifier, and a type-specifier was not
11737      optional in this context, issue an error message.  */
11738   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11739     {
11740       cp_parser_error (parser, "expected type specifier");
11741       return error_mark_node;
11742     }
11743
11744   return type_spec;
11745 }
11746
11747 /* Parse a simple-type-specifier.
11748
11749    simple-type-specifier:
11750      :: [opt] nested-name-specifier [opt] type-name
11751      :: [opt] nested-name-specifier template template-id
11752      char
11753      wchar_t
11754      bool
11755      short
11756      int
11757      long
11758      signed
11759      unsigned
11760      float
11761      double
11762      void
11763
11764    C++0x Extension:
11765
11766    simple-type-specifier:
11767      auto
11768      decltype ( expression )   
11769      char16_t
11770      char32_t
11771
11772    GNU Extension:
11773
11774    simple-type-specifier:
11775      __typeof__ unary-expression
11776      __typeof__ ( type-id )
11777
11778    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11779    appropriately updated.  */
11780
11781 static tree
11782 cp_parser_simple_type_specifier (cp_parser* parser,
11783                                  cp_decl_specifier_seq *decl_specs,
11784                                  cp_parser_flags flags)
11785 {
11786   tree type = NULL_TREE;
11787   cp_token *token;
11788
11789   /* Peek at the next token.  */
11790   token = cp_lexer_peek_token (parser->lexer);
11791
11792   /* If we're looking at a keyword, things are easy.  */
11793   switch (token->keyword)
11794     {
11795     case RID_CHAR:
11796       if (decl_specs)
11797         decl_specs->explicit_char_p = true;
11798       type = char_type_node;
11799       break;
11800     case RID_CHAR16:
11801       type = char16_type_node;
11802       break;
11803     case RID_CHAR32:
11804       type = char32_type_node;
11805       break;
11806     case RID_WCHAR:
11807       type = wchar_type_node;
11808       break;
11809     case RID_BOOL:
11810       type = boolean_type_node;
11811       break;
11812     case RID_SHORT:
11813       if (decl_specs)
11814         ++decl_specs->specs[(int) ds_short];
11815       type = short_integer_type_node;
11816       break;
11817     case RID_INT:
11818       if (decl_specs)
11819         decl_specs->explicit_int_p = true;
11820       type = integer_type_node;
11821       break;
11822     case RID_LONG:
11823       if (decl_specs)
11824         ++decl_specs->specs[(int) ds_long];
11825       type = long_integer_type_node;
11826       break;
11827     case RID_SIGNED:
11828       if (decl_specs)
11829         ++decl_specs->specs[(int) ds_signed];
11830       type = integer_type_node;
11831       break;
11832     case RID_UNSIGNED:
11833       if (decl_specs)
11834         ++decl_specs->specs[(int) ds_unsigned];
11835       type = unsigned_type_node;
11836       break;
11837     case RID_FLOAT:
11838       type = float_type_node;
11839       break;
11840     case RID_DOUBLE:
11841       type = double_type_node;
11842       break;
11843     case RID_VOID:
11844       type = void_type_node;
11845       break;
11846       
11847     case RID_AUTO:
11848       maybe_warn_cpp0x ("C++0x auto");
11849       type = make_auto ();
11850       break;
11851
11852     case RID_DECLTYPE:
11853       /* Parse the `decltype' type.  */
11854       type = cp_parser_decltype (parser);
11855
11856       if (decl_specs)
11857         cp_parser_set_decl_spec_type (decl_specs, type,
11858                                       token->location,
11859                                       /*user_defined_p=*/true);
11860
11861       return type;
11862
11863     case RID_TYPEOF:
11864       /* Consume the `typeof' token.  */
11865       cp_lexer_consume_token (parser->lexer);
11866       /* Parse the operand to `typeof'.  */
11867       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11868       /* If it is not already a TYPE, take its type.  */
11869       if (!TYPE_P (type))
11870         type = finish_typeof (type);
11871
11872       if (decl_specs)
11873         cp_parser_set_decl_spec_type (decl_specs, type,
11874                                       token->location,
11875                                       /*user_defined_p=*/true);
11876
11877       return type;
11878
11879     default:
11880       break;
11881     }
11882
11883   /* If the type-specifier was for a built-in type, we're done.  */
11884   if (type)
11885     {
11886       tree id;
11887
11888       /* Record the type.  */
11889       if (decl_specs
11890           && (token->keyword != RID_SIGNED
11891               && token->keyword != RID_UNSIGNED
11892               && token->keyword != RID_SHORT
11893               && token->keyword != RID_LONG))
11894         cp_parser_set_decl_spec_type (decl_specs,
11895                                       type,
11896                                       token->location,
11897                                       /*user_defined=*/false);
11898       if (decl_specs)
11899         decl_specs->any_specifiers_p = true;
11900
11901       /* Consume the token.  */
11902       id = cp_lexer_consume_token (parser->lexer)->u.value;
11903
11904       /* There is no valid C++ program where a non-template type is
11905          followed by a "<".  That usually indicates that the user thought
11906          that the type was a template.  */
11907       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11908
11909       return TYPE_NAME (type);
11910     }
11911
11912   /* The type-specifier must be a user-defined type.  */
11913   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11914     {
11915       bool qualified_p;
11916       bool global_p;
11917
11918       /* Don't gobble tokens or issue error messages if this is an
11919          optional type-specifier.  */
11920       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11921         cp_parser_parse_tentatively (parser);
11922
11923       /* Look for the optional `::' operator.  */
11924       global_p
11925         = (cp_parser_global_scope_opt (parser,
11926                                        /*current_scope_valid_p=*/false)
11927            != NULL_TREE);
11928       /* Look for the nested-name specifier.  */
11929       qualified_p
11930         = (cp_parser_nested_name_specifier_opt (parser,
11931                                                 /*typename_keyword_p=*/false,
11932                                                 /*check_dependency_p=*/true,
11933                                                 /*type_p=*/false,
11934                                                 /*is_declaration=*/false)
11935            != NULL_TREE);
11936       token = cp_lexer_peek_token (parser->lexer);
11937       /* If we have seen a nested-name-specifier, and the next token
11938          is `template', then we are using the template-id production.  */
11939       if (parser->scope
11940           && cp_parser_optional_template_keyword (parser))
11941         {
11942           /* Look for the template-id.  */
11943           type = cp_parser_template_id (parser,
11944                                         /*template_keyword_p=*/true,
11945                                         /*check_dependency_p=*/true,
11946                                         /*is_declaration=*/false);
11947           /* If the template-id did not name a type, we are out of
11948              luck.  */
11949           if (TREE_CODE (type) != TYPE_DECL)
11950             {
11951               cp_parser_error (parser, "expected template-id for type");
11952               type = NULL_TREE;
11953             }
11954         }
11955       /* Otherwise, look for a type-name.  */
11956       else
11957         type = cp_parser_type_name (parser);
11958       /* Keep track of all name-lookups performed in class scopes.  */
11959       if (type
11960           && !global_p
11961           && !qualified_p
11962           && TREE_CODE (type) == TYPE_DECL
11963           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11964         maybe_note_name_used_in_class (DECL_NAME (type), type);
11965       /* If it didn't work out, we don't have a TYPE.  */
11966       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11967           && !cp_parser_parse_definitely (parser))
11968         type = NULL_TREE;
11969       if (type && decl_specs)
11970         cp_parser_set_decl_spec_type (decl_specs, type,
11971                                       token->location,
11972                                       /*user_defined=*/true);
11973     }
11974
11975   /* If we didn't get a type-name, issue an error message.  */
11976   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11977     {
11978       cp_parser_error (parser, "expected type-name");
11979       return error_mark_node;
11980     }
11981
11982   /* There is no valid C++ program where a non-template type is
11983      followed by a "<".  That usually indicates that the user thought
11984      that the type was a template.  */
11985   if (type && type != error_mark_node)
11986     {
11987       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11988          If it is, then the '<'...'>' enclose protocol names rather than
11989          template arguments, and so everything is fine.  */
11990       if (c_dialect_objc ()
11991           && (objc_is_id (type) || objc_is_class_name (type)))
11992         {
11993           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11994           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11995
11996           /* Clobber the "unqualified" type previously entered into
11997              DECL_SPECS with the new, improved protocol-qualified version.  */
11998           if (decl_specs)
11999             decl_specs->type = qual_type;
12000
12001           return qual_type;
12002         }
12003
12004       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12005                                                token->location);
12006     }
12007
12008   return type;
12009 }
12010
12011 /* Parse a type-name.
12012
12013    type-name:
12014      class-name
12015      enum-name
12016      typedef-name
12017
12018    enum-name:
12019      identifier
12020
12021    typedef-name:
12022      identifier
12023
12024    Returns a TYPE_DECL for the type.  */
12025
12026 static tree
12027 cp_parser_type_name (cp_parser* parser)
12028 {
12029   tree type_decl;
12030
12031   /* We can't know yet whether it is a class-name or not.  */
12032   cp_parser_parse_tentatively (parser);
12033   /* Try a class-name.  */
12034   type_decl = cp_parser_class_name (parser,
12035                                     /*typename_keyword_p=*/false,
12036                                     /*template_keyword_p=*/false,
12037                                     none_type,
12038                                     /*check_dependency_p=*/true,
12039                                     /*class_head_p=*/false,
12040                                     /*is_declaration=*/false);
12041   /* If it's not a class-name, keep looking.  */
12042   if (!cp_parser_parse_definitely (parser))
12043     {
12044       /* It must be a typedef-name or an enum-name.  */
12045       return cp_parser_nonclass_name (parser);
12046     }
12047
12048   return type_decl;
12049 }
12050
12051 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12052
12053    enum-name:
12054      identifier
12055
12056    typedef-name:
12057      identifier
12058
12059    Returns a TYPE_DECL for the type.  */
12060
12061 static tree
12062 cp_parser_nonclass_name (cp_parser* parser)
12063 {
12064   tree type_decl;
12065   tree identifier;
12066
12067   cp_token *token = cp_lexer_peek_token (parser->lexer);
12068   identifier = cp_parser_identifier (parser);
12069   if (identifier == error_mark_node)
12070     return error_mark_node;
12071
12072   /* Look up the type-name.  */
12073   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12074
12075   if (TREE_CODE (type_decl) != TYPE_DECL
12076       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12077     {
12078       /* See if this is an Objective-C type.  */
12079       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12080       tree type = objc_get_protocol_qualified_type (identifier, protos);
12081       if (type)
12082         type_decl = TYPE_NAME (type);
12083     }
12084   
12085   /* Issue an error if we did not find a type-name.  */
12086   if (TREE_CODE (type_decl) != TYPE_DECL)
12087     {
12088       if (!cp_parser_simulate_error (parser))
12089         cp_parser_name_lookup_error (parser, identifier, type_decl,
12090                                      "is not a type", token->location);
12091       return error_mark_node;
12092     }
12093   /* Remember that the name was used in the definition of the
12094      current class so that we can check later to see if the
12095      meaning would have been different after the class was
12096      entirely defined.  */
12097   else if (type_decl != error_mark_node
12098            && !parser->scope)
12099     maybe_note_name_used_in_class (identifier, type_decl);
12100   
12101   return type_decl;
12102 }
12103
12104 /* Parse an elaborated-type-specifier.  Note that the grammar given
12105    here incorporates the resolution to DR68.
12106
12107    elaborated-type-specifier:
12108      class-key :: [opt] nested-name-specifier [opt] identifier
12109      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12110      enum-key :: [opt] nested-name-specifier [opt] identifier
12111      typename :: [opt] nested-name-specifier identifier
12112      typename :: [opt] nested-name-specifier template [opt]
12113        template-id
12114
12115    GNU extension:
12116
12117    elaborated-type-specifier:
12118      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12119      class-key attributes :: [opt] nested-name-specifier [opt]
12120                template [opt] template-id
12121      enum attributes :: [opt] nested-name-specifier [opt] identifier
12122
12123    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12124    declared `friend'.  If IS_DECLARATION is TRUE, then this
12125    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12126    something is being declared.
12127
12128    Returns the TYPE specified.  */
12129
12130 static tree
12131 cp_parser_elaborated_type_specifier (cp_parser* parser,
12132                                      bool is_friend,
12133                                      bool is_declaration)
12134 {
12135   enum tag_types tag_type;
12136   tree identifier;
12137   tree type = NULL_TREE;
12138   tree attributes = NULL_TREE;
12139   tree globalscope;
12140   cp_token *token = NULL;
12141
12142   /* See if we're looking at the `enum' keyword.  */
12143   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12144     {
12145       /* Consume the `enum' token.  */
12146       cp_lexer_consume_token (parser->lexer);
12147       /* Remember that it's an enumeration type.  */
12148       tag_type = enum_type;
12149       /* Parse the optional `struct' or `class' key (for C++0x scoped
12150          enums).  */
12151       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12152           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12153         {
12154           if (cxx_dialect == cxx98)
12155             maybe_warn_cpp0x ("scoped enums");
12156
12157           /* Consume the `struct' or `class'.  */
12158           cp_lexer_consume_token (parser->lexer);
12159         }
12160       /* Parse the attributes.  */
12161       attributes = cp_parser_attributes_opt (parser);
12162     }
12163   /* Or, it might be `typename'.  */
12164   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12165                                            RID_TYPENAME))
12166     {
12167       /* Consume the `typename' token.  */
12168       cp_lexer_consume_token (parser->lexer);
12169       /* Remember that it's a `typename' type.  */
12170       tag_type = typename_type;
12171     }
12172   /* Otherwise it must be a class-key.  */
12173   else
12174     {
12175       tag_type = cp_parser_class_key (parser);
12176       if (tag_type == none_type)
12177         return error_mark_node;
12178       /* Parse the attributes.  */
12179       attributes = cp_parser_attributes_opt (parser);
12180     }
12181
12182   /* Look for the `::' operator.  */
12183   globalscope =  cp_parser_global_scope_opt (parser,
12184                                              /*current_scope_valid_p=*/false);
12185   /* Look for the nested-name-specifier.  */
12186   if (tag_type == typename_type && !globalscope)
12187     {
12188       if (!cp_parser_nested_name_specifier (parser,
12189                                            /*typename_keyword_p=*/true,
12190                                            /*check_dependency_p=*/true,
12191                                            /*type_p=*/true,
12192                                             is_declaration))
12193         return error_mark_node;
12194     }
12195   else
12196     /* Even though `typename' is not present, the proposed resolution
12197        to Core Issue 180 says that in `class A<T>::B', `B' should be
12198        considered a type-name, even if `A<T>' is dependent.  */
12199     cp_parser_nested_name_specifier_opt (parser,
12200                                          /*typename_keyword_p=*/true,
12201                                          /*check_dependency_p=*/true,
12202                                          /*type_p=*/true,
12203                                          is_declaration);
12204  /* For everything but enumeration types, consider a template-id.
12205     For an enumeration type, consider only a plain identifier.  */
12206   if (tag_type != enum_type)
12207     {
12208       bool template_p = false;
12209       tree decl;
12210
12211       /* Allow the `template' keyword.  */
12212       template_p = cp_parser_optional_template_keyword (parser);
12213       /* If we didn't see `template', we don't know if there's a
12214          template-id or not.  */
12215       if (!template_p)
12216         cp_parser_parse_tentatively (parser);
12217       /* Parse the template-id.  */
12218       token = cp_lexer_peek_token (parser->lexer);
12219       decl = cp_parser_template_id (parser, template_p,
12220                                     /*check_dependency_p=*/true,
12221                                     is_declaration);
12222       /* If we didn't find a template-id, look for an ordinary
12223          identifier.  */
12224       if (!template_p && !cp_parser_parse_definitely (parser))
12225         ;
12226       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12227          in effect, then we must assume that, upon instantiation, the
12228          template will correspond to a class.  */
12229       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12230                && tag_type == typename_type)
12231         type = make_typename_type (parser->scope, decl,
12232                                    typename_type,
12233                                    /*complain=*/tf_error);
12234       /* If the `typename' keyword is in effect and DECL is not a type
12235          decl. Then type is non existant.   */
12236       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12237         type = NULL_TREE; 
12238       else 
12239         type = TREE_TYPE (decl);
12240     }
12241
12242   if (!type)
12243     {
12244       token = cp_lexer_peek_token (parser->lexer);
12245       identifier = cp_parser_identifier (parser);
12246
12247       if (identifier == error_mark_node)
12248         {
12249           parser->scope = NULL_TREE;
12250           return error_mark_node;
12251         }
12252
12253       /* For a `typename', we needn't call xref_tag.  */
12254       if (tag_type == typename_type
12255           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12256         return cp_parser_make_typename_type (parser, parser->scope,
12257                                              identifier,
12258                                              token->location);
12259       /* Look up a qualified name in the usual way.  */
12260       if (parser->scope)
12261         {
12262           tree decl;
12263           tree ambiguous_decls;
12264
12265           decl = cp_parser_lookup_name (parser, identifier,
12266                                         tag_type,
12267                                         /*is_template=*/false,
12268                                         /*is_namespace=*/false,
12269                                         /*check_dependency=*/true,
12270                                         &ambiguous_decls,
12271                                         token->location);
12272
12273           /* If the lookup was ambiguous, an error will already have been
12274              issued.  */
12275           if (ambiguous_decls)
12276             return error_mark_node;
12277
12278           /* If we are parsing friend declaration, DECL may be a
12279              TEMPLATE_DECL tree node here.  However, we need to check
12280              whether this TEMPLATE_DECL results in valid code.  Consider
12281              the following example:
12282
12283                namespace N {
12284                  template <class T> class C {};
12285                }
12286                class X {
12287                  template <class T> friend class N::C; // #1, valid code
12288                };
12289                template <class T> class Y {
12290                  friend class N::C;                    // #2, invalid code
12291                };
12292
12293              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12294              name lookup of `N::C'.  We see that friend declaration must
12295              be template for the code to be valid.  Note that
12296              processing_template_decl does not work here since it is
12297              always 1 for the above two cases.  */
12298
12299           decl = (cp_parser_maybe_treat_template_as_class
12300                   (decl, /*tag_name_p=*/is_friend
12301                          && parser->num_template_parameter_lists));
12302
12303           if (TREE_CODE (decl) != TYPE_DECL)
12304             {
12305               cp_parser_diagnose_invalid_type_name (parser,
12306                                                     parser->scope,
12307                                                     identifier,
12308                                                     token->location);
12309               return error_mark_node;
12310             }
12311
12312           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12313             {
12314               bool allow_template = (parser->num_template_parameter_lists
12315                                       || DECL_SELF_REFERENCE_P (decl));
12316               type = check_elaborated_type_specifier (tag_type, decl, 
12317                                                       allow_template);
12318
12319               if (type == error_mark_node)
12320                 return error_mark_node;
12321             }
12322
12323           /* Forward declarations of nested types, such as
12324
12325                class C1::C2;
12326                class C1::C2::C3;
12327
12328              are invalid unless all components preceding the final '::'
12329              are complete.  If all enclosing types are complete, these
12330              declarations become merely pointless.
12331
12332              Invalid forward declarations of nested types are errors
12333              caught elsewhere in parsing.  Those that are pointless arrive
12334              here.  */
12335
12336           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12337               && !is_friend && !processing_explicit_instantiation)
12338             warning (0, "declaration %qD does not declare anything", decl);
12339
12340           type = TREE_TYPE (decl);
12341         }
12342       else
12343         {
12344           /* An elaborated-type-specifier sometimes introduces a new type and
12345              sometimes names an existing type.  Normally, the rule is that it
12346              introduces a new type only if there is not an existing type of
12347              the same name already in scope.  For example, given:
12348
12349                struct S {};
12350                void f() { struct S s; }
12351
12352              the `struct S' in the body of `f' is the same `struct S' as in
12353              the global scope; the existing definition is used.  However, if
12354              there were no global declaration, this would introduce a new
12355              local class named `S'.
12356
12357              An exception to this rule applies to the following code:
12358
12359                namespace N { struct S; }
12360
12361              Here, the elaborated-type-specifier names a new type
12362              unconditionally; even if there is already an `S' in the
12363              containing scope this declaration names a new type.
12364              This exception only applies if the elaborated-type-specifier
12365              forms the complete declaration:
12366
12367                [class.name]
12368
12369                A declaration consisting solely of `class-key identifier ;' is
12370                either a redeclaration of the name in the current scope or a
12371                forward declaration of the identifier as a class name.  It
12372                introduces the name into the current scope.
12373
12374              We are in this situation precisely when the next token is a `;'.
12375
12376              An exception to the exception is that a `friend' declaration does
12377              *not* name a new type; i.e., given:
12378
12379                struct S { friend struct T; };
12380
12381              `T' is not a new type in the scope of `S'.
12382
12383              Also, `new struct S' or `sizeof (struct S)' never results in the
12384              definition of a new type; a new type can only be declared in a
12385              declaration context.  */
12386
12387           tag_scope ts;
12388           bool template_p;
12389
12390           if (is_friend)
12391             /* Friends have special name lookup rules.  */
12392             ts = ts_within_enclosing_non_class;
12393           else if (is_declaration
12394                    && cp_lexer_next_token_is (parser->lexer,
12395                                               CPP_SEMICOLON))
12396             /* This is a `class-key identifier ;' */
12397             ts = ts_current;
12398           else
12399             ts = ts_global;
12400
12401           template_p =
12402             (parser->num_template_parameter_lists
12403              && (cp_parser_next_token_starts_class_definition_p (parser)
12404                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12405           /* An unqualified name was used to reference this type, so
12406              there were no qualifying templates.  */
12407           if (!cp_parser_check_template_parameters (parser,
12408                                                     /*num_templates=*/0,
12409                                                     token->location,
12410                                                     /*declarator=*/NULL))
12411             return error_mark_node;
12412           type = xref_tag (tag_type, identifier, ts, template_p);
12413         }
12414     }
12415
12416   if (type == error_mark_node)
12417     return error_mark_node;
12418
12419   /* Allow attributes on forward declarations of classes.  */
12420   if (attributes)
12421     {
12422       if (TREE_CODE (type) == TYPENAME_TYPE)
12423         warning (OPT_Wattributes,
12424                  "attributes ignored on uninstantiated type");
12425       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12426                && ! processing_explicit_instantiation)
12427         warning (OPT_Wattributes,
12428                  "attributes ignored on template instantiation");
12429       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12430         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12431       else
12432         warning (OPT_Wattributes,
12433                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12434     }
12435
12436   if (tag_type != enum_type)
12437     cp_parser_check_class_key (tag_type, type);
12438
12439   /* A "<" cannot follow an elaborated type specifier.  If that
12440      happens, the user was probably trying to form a template-id.  */
12441   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12442
12443   return type;
12444 }
12445
12446 /* Parse an enum-specifier.
12447
12448    enum-specifier:
12449      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12450
12451    enum-key:
12452      enum
12453      enum class   [C++0x]
12454      enum struct  [C++0x]
12455
12456    enum-base:   [C++0x]
12457      : type-specifier-seq
12458
12459    GNU Extensions:
12460      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12461        { enumerator-list [opt] }attributes[opt]
12462
12463    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12464    if the token stream isn't an enum-specifier after all.  */
12465
12466 static tree
12467 cp_parser_enum_specifier (cp_parser* parser)
12468 {
12469   tree identifier;
12470   tree type;
12471   tree attributes;
12472   bool scoped_enum_p = false;
12473   bool has_underlying_type = false;
12474   tree underlying_type = NULL_TREE;
12475
12476   /* Parse tentatively so that we can back up if we don't find a
12477      enum-specifier.  */
12478   cp_parser_parse_tentatively (parser);
12479
12480   /* Caller guarantees that the current token is 'enum', an identifier
12481      possibly follows, and the token after that is an opening brace.
12482      If we don't have an identifier, fabricate an anonymous name for
12483      the enumeration being defined.  */
12484   cp_lexer_consume_token (parser->lexer);
12485
12486   /* Parse the "class" or "struct", which indicates a scoped
12487      enumeration type in C++0x.  */
12488   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12489       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12490     {
12491       if (cxx_dialect == cxx98)
12492         maybe_warn_cpp0x ("scoped enums");
12493
12494       /* Consume the `struct' or `class' token.  */
12495       cp_lexer_consume_token (parser->lexer);
12496
12497       scoped_enum_p = true;
12498     }
12499
12500   attributes = cp_parser_attributes_opt (parser);
12501
12502   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12503     identifier = cp_parser_identifier (parser);
12504   else
12505     identifier = make_anon_name ();
12506
12507   /* Check for the `:' that denotes a specified underlying type in C++0x.
12508      Note that a ':' could also indicate a bitfield width, however.  */
12509   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12510     {
12511       cp_decl_specifier_seq type_specifiers;
12512
12513       /* Consume the `:'.  */
12514       cp_lexer_consume_token (parser->lexer);
12515
12516       /* Parse the type-specifier-seq.  */
12517       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12518                                     &type_specifiers);
12519
12520       /* At this point this is surely not elaborated type specifier.  */
12521       if (!cp_parser_parse_definitely (parser))
12522         return NULL_TREE;
12523
12524       if (cxx_dialect == cxx98)
12525         maybe_warn_cpp0x ("scoped enums");
12526
12527       has_underlying_type = true;
12528
12529       /* If that didn't work, stop.  */
12530       if (type_specifiers.type != error_mark_node)
12531         {
12532           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12533                                             /*initialized=*/0, NULL);
12534           if (underlying_type == error_mark_node)
12535             underlying_type = NULL_TREE;
12536         }
12537     }
12538
12539   /* Look for the `{' but don't consume it yet.  */
12540   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12541     {
12542       cp_parser_error (parser, "expected %<{%>");
12543       if (has_underlying_type)
12544         return NULL_TREE;
12545     }
12546
12547   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12548     return NULL_TREE;
12549
12550   /* Issue an error message if type-definitions are forbidden here.  */
12551   if (!cp_parser_check_type_definition (parser))
12552     type = error_mark_node;
12553   else
12554     /* Create the new type.  We do this before consuming the opening
12555        brace so the enum will be recorded as being on the line of its
12556        tag (or the 'enum' keyword, if there is no tag).  */
12557     type = start_enum (identifier, underlying_type, scoped_enum_p);
12558   
12559   /* Consume the opening brace.  */
12560   cp_lexer_consume_token (parser->lexer);
12561
12562   if (type == error_mark_node)
12563     {
12564       cp_parser_skip_to_end_of_block_or_statement (parser);
12565       return error_mark_node;
12566     }
12567
12568   /* If the next token is not '}', then there are some enumerators.  */
12569   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12570     cp_parser_enumerator_list (parser, type);
12571
12572   /* Consume the final '}'.  */
12573   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12574
12575   /* Look for trailing attributes to apply to this enumeration, and
12576      apply them if appropriate.  */
12577   if (cp_parser_allow_gnu_extensions_p (parser))
12578     {
12579       tree trailing_attr = cp_parser_attributes_opt (parser);
12580       trailing_attr = chainon (trailing_attr, attributes);
12581       cplus_decl_attributes (&type,
12582                              trailing_attr,
12583                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12584     }
12585
12586   /* Finish up the enumeration.  */
12587   finish_enum (type);
12588
12589   return type;
12590 }
12591
12592 /* Parse an enumerator-list.  The enumerators all have the indicated
12593    TYPE.
12594
12595    enumerator-list:
12596      enumerator-definition
12597      enumerator-list , enumerator-definition  */
12598
12599 static void
12600 cp_parser_enumerator_list (cp_parser* parser, tree type)
12601 {
12602   while (true)
12603     {
12604       /* Parse an enumerator-definition.  */
12605       cp_parser_enumerator_definition (parser, type);
12606
12607       /* If the next token is not a ',', we've reached the end of
12608          the list.  */
12609       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12610         break;
12611       /* Otherwise, consume the `,' and keep going.  */
12612       cp_lexer_consume_token (parser->lexer);
12613       /* If the next token is a `}', there is a trailing comma.  */
12614       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12615         {
12616           if (!in_system_header)
12617             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12618           break;
12619         }
12620     }
12621 }
12622
12623 /* Parse an enumerator-definition.  The enumerator has the indicated
12624    TYPE.
12625
12626    enumerator-definition:
12627      enumerator
12628      enumerator = constant-expression
12629
12630    enumerator:
12631      identifier  */
12632
12633 static void
12634 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12635 {
12636   tree identifier;
12637   tree value;
12638
12639   /* Look for the identifier.  */
12640   identifier = cp_parser_identifier (parser);
12641   if (identifier == error_mark_node)
12642     return;
12643
12644   /* If the next token is an '=', then there is an explicit value.  */
12645   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12646     {
12647       /* Consume the `=' token.  */
12648       cp_lexer_consume_token (parser->lexer);
12649       /* Parse the value.  */
12650       value = cp_parser_constant_expression (parser,
12651                                              /*allow_non_constant_p=*/false,
12652                                              NULL);
12653     }
12654   else
12655     value = NULL_TREE;
12656
12657   /* If we are processing a template, make sure the initializer of the
12658      enumerator doesn't contain any bare template parameter pack.  */
12659   if (check_for_bare_parameter_packs (value))
12660     value = error_mark_node;
12661
12662   /* Create the enumerator.  */
12663   build_enumerator (identifier, value, type);
12664 }
12665
12666 /* Parse a namespace-name.
12667
12668    namespace-name:
12669      original-namespace-name
12670      namespace-alias
12671
12672    Returns the NAMESPACE_DECL for the namespace.  */
12673
12674 static tree
12675 cp_parser_namespace_name (cp_parser* parser)
12676 {
12677   tree identifier;
12678   tree namespace_decl;
12679
12680   cp_token *token = cp_lexer_peek_token (parser->lexer);
12681
12682   /* Get the name of the namespace.  */
12683   identifier = cp_parser_identifier (parser);
12684   if (identifier == error_mark_node)
12685     return error_mark_node;
12686
12687   /* Look up the identifier in the currently active scope.  Look only
12688      for namespaces, due to:
12689
12690        [basic.lookup.udir]
12691
12692        When looking up a namespace-name in a using-directive or alias
12693        definition, only namespace names are considered.
12694
12695      And:
12696
12697        [basic.lookup.qual]
12698
12699        During the lookup of a name preceding the :: scope resolution
12700        operator, object, function, and enumerator names are ignored.
12701
12702      (Note that cp_parser_qualifying_entity only calls this
12703      function if the token after the name is the scope resolution
12704      operator.)  */
12705   namespace_decl = cp_parser_lookup_name (parser, identifier,
12706                                           none_type,
12707                                           /*is_template=*/false,
12708                                           /*is_namespace=*/true,
12709                                           /*check_dependency=*/true,
12710                                           /*ambiguous_decls=*/NULL,
12711                                           token->location);
12712   /* If it's not a namespace, issue an error.  */
12713   if (namespace_decl == error_mark_node
12714       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12715     {
12716       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12717         error_at (token->location, "%qD is not a namespace-name", identifier);
12718       cp_parser_error (parser, "expected namespace-name");
12719       namespace_decl = error_mark_node;
12720     }
12721
12722   return namespace_decl;
12723 }
12724
12725 /* Parse a namespace-definition.
12726
12727    namespace-definition:
12728      named-namespace-definition
12729      unnamed-namespace-definition
12730
12731    named-namespace-definition:
12732      original-namespace-definition
12733      extension-namespace-definition
12734
12735    original-namespace-definition:
12736      namespace identifier { namespace-body }
12737
12738    extension-namespace-definition:
12739      namespace original-namespace-name { namespace-body }
12740
12741    unnamed-namespace-definition:
12742      namespace { namespace-body } */
12743
12744 static void
12745 cp_parser_namespace_definition (cp_parser* parser)
12746 {
12747   tree identifier, attribs;
12748   bool has_visibility;
12749   bool is_inline;
12750
12751   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12752     {
12753       is_inline = true;
12754       cp_lexer_consume_token (parser->lexer);
12755     }
12756   else
12757     is_inline = false;
12758
12759   /* Look for the `namespace' keyword.  */
12760   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12761
12762   /* Get the name of the namespace.  We do not attempt to distinguish
12763      between an original-namespace-definition and an
12764      extension-namespace-definition at this point.  The semantic
12765      analysis routines are responsible for that.  */
12766   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12767     identifier = cp_parser_identifier (parser);
12768   else
12769     identifier = NULL_TREE;
12770
12771   /* Parse any specified attributes.  */
12772   attribs = cp_parser_attributes_opt (parser);
12773
12774   /* Look for the `{' to start the namespace.  */
12775   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12776   /* Start the namespace.  */
12777   push_namespace (identifier);
12778
12779   /* "inline namespace" is equivalent to a stub namespace definition
12780      followed by a strong using directive.  */
12781   if (is_inline)
12782     {
12783       tree name_space = current_namespace;
12784       /* Set up namespace association.  */
12785       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12786         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12787                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12788       /* Import the contents of the inline namespace.  */
12789       pop_namespace ();
12790       do_using_directive (name_space);
12791       push_namespace (identifier);
12792     }
12793
12794   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12795
12796   /* Parse the body of the namespace.  */
12797   cp_parser_namespace_body (parser);
12798
12799 #ifdef HANDLE_PRAGMA_VISIBILITY
12800   if (has_visibility)
12801     pop_visibility ();
12802 #endif
12803
12804   /* Finish the namespace.  */
12805   pop_namespace ();
12806   /* Look for the final `}'.  */
12807   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12808 }
12809
12810 /* Parse a namespace-body.
12811
12812    namespace-body:
12813      declaration-seq [opt]  */
12814
12815 static void
12816 cp_parser_namespace_body (cp_parser* parser)
12817 {
12818   cp_parser_declaration_seq_opt (parser);
12819 }
12820
12821 /* Parse a namespace-alias-definition.
12822
12823    namespace-alias-definition:
12824      namespace identifier = qualified-namespace-specifier ;  */
12825
12826 static void
12827 cp_parser_namespace_alias_definition (cp_parser* parser)
12828 {
12829   tree identifier;
12830   tree namespace_specifier;
12831
12832   cp_token *token = cp_lexer_peek_token (parser->lexer);
12833
12834   /* Look for the `namespace' keyword.  */
12835   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12836   /* Look for the identifier.  */
12837   identifier = cp_parser_identifier (parser);
12838   if (identifier == error_mark_node)
12839     return;
12840   /* Look for the `=' token.  */
12841   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12842       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12843     {
12844       error_at (token->location, "%<namespace%> definition is not allowed here");
12845       /* Skip the definition.  */
12846       cp_lexer_consume_token (parser->lexer);
12847       if (cp_parser_skip_to_closing_brace (parser))
12848         cp_lexer_consume_token (parser->lexer);
12849       return;
12850     }
12851   cp_parser_require (parser, CPP_EQ, "%<=%>");
12852   /* Look for the qualified-namespace-specifier.  */
12853   namespace_specifier
12854     = cp_parser_qualified_namespace_specifier (parser);
12855   /* Look for the `;' token.  */
12856   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12857
12858   /* Register the alias in the symbol table.  */
12859   do_namespace_alias (identifier, namespace_specifier);
12860 }
12861
12862 /* Parse a qualified-namespace-specifier.
12863
12864    qualified-namespace-specifier:
12865      :: [opt] nested-name-specifier [opt] namespace-name
12866
12867    Returns a NAMESPACE_DECL corresponding to the specified
12868    namespace.  */
12869
12870 static tree
12871 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12872 {
12873   /* Look for the optional `::'.  */
12874   cp_parser_global_scope_opt (parser,
12875                               /*current_scope_valid_p=*/false);
12876
12877   /* Look for the optional nested-name-specifier.  */
12878   cp_parser_nested_name_specifier_opt (parser,
12879                                        /*typename_keyword_p=*/false,
12880                                        /*check_dependency_p=*/true,
12881                                        /*type_p=*/false,
12882                                        /*is_declaration=*/true);
12883
12884   return cp_parser_namespace_name (parser);
12885 }
12886
12887 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12888    access declaration.
12889
12890    using-declaration:
12891      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12892      using :: unqualified-id ;  
12893
12894    access-declaration:
12895      qualified-id ;  
12896
12897    */
12898
12899 static bool
12900 cp_parser_using_declaration (cp_parser* parser, 
12901                              bool access_declaration_p)
12902 {
12903   cp_token *token;
12904   bool typename_p = false;
12905   bool global_scope_p;
12906   tree decl;
12907   tree identifier;
12908   tree qscope;
12909
12910   if (access_declaration_p)
12911     cp_parser_parse_tentatively (parser);
12912   else
12913     {
12914       /* Look for the `using' keyword.  */
12915       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12916       
12917       /* Peek at the next token.  */
12918       token = cp_lexer_peek_token (parser->lexer);
12919       /* See if it's `typename'.  */
12920       if (token->keyword == RID_TYPENAME)
12921         {
12922           /* Remember that we've seen it.  */
12923           typename_p = true;
12924           /* Consume the `typename' token.  */
12925           cp_lexer_consume_token (parser->lexer);
12926         }
12927     }
12928
12929   /* Look for the optional global scope qualification.  */
12930   global_scope_p
12931     = (cp_parser_global_scope_opt (parser,
12932                                    /*current_scope_valid_p=*/false)
12933        != NULL_TREE);
12934
12935   /* If we saw `typename', or didn't see `::', then there must be a
12936      nested-name-specifier present.  */
12937   if (typename_p || !global_scope_p)
12938     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12939                                               /*check_dependency_p=*/true,
12940                                               /*type_p=*/false,
12941                                               /*is_declaration=*/true);
12942   /* Otherwise, we could be in either of the two productions.  In that
12943      case, treat the nested-name-specifier as optional.  */
12944   else
12945     qscope = cp_parser_nested_name_specifier_opt (parser,
12946                                                   /*typename_keyword_p=*/false,
12947                                                   /*check_dependency_p=*/true,
12948                                                   /*type_p=*/false,
12949                                                   /*is_declaration=*/true);
12950   if (!qscope)
12951     qscope = global_namespace;
12952
12953   if (access_declaration_p && cp_parser_error_occurred (parser))
12954     /* Something has already gone wrong; there's no need to parse
12955        further.  Since an error has occurred, the return value of
12956        cp_parser_parse_definitely will be false, as required.  */
12957     return cp_parser_parse_definitely (parser);
12958
12959   token = cp_lexer_peek_token (parser->lexer);
12960   /* Parse the unqualified-id.  */
12961   identifier = cp_parser_unqualified_id (parser,
12962                                          /*template_keyword_p=*/false,
12963                                          /*check_dependency_p=*/true,
12964                                          /*declarator_p=*/true,
12965                                          /*optional_p=*/false);
12966
12967   if (access_declaration_p)
12968     {
12969       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12970         cp_parser_simulate_error (parser);
12971       if (!cp_parser_parse_definitely (parser))
12972         return false;
12973     }
12974
12975   /* The function we call to handle a using-declaration is different
12976      depending on what scope we are in.  */
12977   if (qscope == error_mark_node || identifier == error_mark_node)
12978     ;
12979   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12980            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12981     /* [namespace.udecl]
12982
12983        A using declaration shall not name a template-id.  */
12984     error_at (token->location,
12985               "a template-id may not appear in a using-declaration");
12986   else
12987     {
12988       if (at_class_scope_p ())
12989         {
12990           /* Create the USING_DECL.  */
12991           decl = do_class_using_decl (parser->scope, identifier);
12992
12993           if (check_for_bare_parameter_packs (decl))
12994             return false;
12995           else
12996             /* Add it to the list of members in this class.  */
12997             finish_member_declaration (decl);
12998         }
12999       else
13000         {
13001           decl = cp_parser_lookup_name_simple (parser,
13002                                                identifier,
13003                                                token->location);
13004           if (decl == error_mark_node)
13005             cp_parser_name_lookup_error (parser, identifier,
13006                                          decl, NULL,
13007                                          token->location);
13008           else if (check_for_bare_parameter_packs (decl))
13009             return false;
13010           else if (!at_namespace_scope_p ())
13011             do_local_using_decl (decl, qscope, identifier);
13012           else
13013             do_toplevel_using_decl (decl, qscope, identifier);
13014         }
13015     }
13016
13017   /* Look for the final `;'.  */
13018   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13019   
13020   return true;
13021 }
13022
13023 /* Parse a using-directive.
13024
13025    using-directive:
13026      using namespace :: [opt] nested-name-specifier [opt]
13027        namespace-name ;  */
13028
13029 static void
13030 cp_parser_using_directive (cp_parser* parser)
13031 {
13032   tree namespace_decl;
13033   tree attribs;
13034
13035   /* Look for the `using' keyword.  */
13036   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13037   /* And the `namespace' keyword.  */
13038   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13039   /* Look for the optional `::' operator.  */
13040   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13041   /* And the optional nested-name-specifier.  */
13042   cp_parser_nested_name_specifier_opt (parser,
13043                                        /*typename_keyword_p=*/false,
13044                                        /*check_dependency_p=*/true,
13045                                        /*type_p=*/false,
13046                                        /*is_declaration=*/true);
13047   /* Get the namespace being used.  */
13048   namespace_decl = cp_parser_namespace_name (parser);
13049   /* And any specified attributes.  */
13050   attribs = cp_parser_attributes_opt (parser);
13051   /* Update the symbol table.  */
13052   parse_using_directive (namespace_decl, attribs);
13053   /* Look for the final `;'.  */
13054   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13055 }
13056
13057 /* Parse an asm-definition.
13058
13059    asm-definition:
13060      asm ( string-literal ) ;
13061
13062    GNU Extension:
13063
13064    asm-definition:
13065      asm volatile [opt] ( string-literal ) ;
13066      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13067      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13068                           : asm-operand-list [opt] ) ;
13069      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13070                           : asm-operand-list [opt]
13071                           : asm-clobber-list [opt] ) ;
13072      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13073                                : asm-clobber-list [opt]
13074                                : asm-goto-list ) ;  */
13075
13076 static void
13077 cp_parser_asm_definition (cp_parser* parser)
13078 {
13079   tree string;
13080   tree outputs = NULL_TREE;
13081   tree inputs = NULL_TREE;
13082   tree clobbers = NULL_TREE;
13083   tree labels = NULL_TREE;
13084   tree asm_stmt;
13085   bool volatile_p = false;
13086   bool extended_p = false;
13087   bool invalid_inputs_p = false;
13088   bool invalid_outputs_p = false;
13089   bool goto_p = false;
13090   const char *missing = NULL;
13091
13092   /* Look for the `asm' keyword.  */
13093   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13094   /* See if the next token is `volatile'.  */
13095   if (cp_parser_allow_gnu_extensions_p (parser)
13096       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13097     {
13098       /* Remember that we saw the `volatile' keyword.  */
13099       volatile_p = true;
13100       /* Consume the token.  */
13101       cp_lexer_consume_token (parser->lexer);
13102     }
13103   if (cp_parser_allow_gnu_extensions_p (parser)
13104       && parser->in_function_body
13105       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13106     {
13107       /* Remember that we saw the `goto' keyword.  */
13108       goto_p = true;
13109       /* Consume the token.  */
13110       cp_lexer_consume_token (parser->lexer);
13111     }
13112   /* Look for the opening `('.  */
13113   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13114     return;
13115   /* Look for the string.  */
13116   string = cp_parser_string_literal (parser, false, false);
13117   if (string == error_mark_node)
13118     {
13119       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13120                                              /*consume_paren=*/true);
13121       return;
13122     }
13123
13124   /* If we're allowing GNU extensions, check for the extended assembly
13125      syntax.  Unfortunately, the `:' tokens need not be separated by
13126      a space in C, and so, for compatibility, we tolerate that here
13127      too.  Doing that means that we have to treat the `::' operator as
13128      two `:' tokens.  */
13129   if (cp_parser_allow_gnu_extensions_p (parser)
13130       && parser->in_function_body
13131       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13132           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13133     {
13134       bool inputs_p = false;
13135       bool clobbers_p = false;
13136       bool labels_p = false;
13137
13138       /* The extended syntax was used.  */
13139       extended_p = true;
13140
13141       /* Look for outputs.  */
13142       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13143         {
13144           /* Consume the `:'.  */
13145           cp_lexer_consume_token (parser->lexer);
13146           /* Parse the output-operands.  */
13147           if (cp_lexer_next_token_is_not (parser->lexer,
13148                                           CPP_COLON)
13149               && cp_lexer_next_token_is_not (parser->lexer,
13150                                              CPP_SCOPE)
13151               && cp_lexer_next_token_is_not (parser->lexer,
13152                                              CPP_CLOSE_PAREN)
13153               && !goto_p)
13154             outputs = cp_parser_asm_operand_list (parser);
13155
13156             if (outputs == error_mark_node)
13157               invalid_outputs_p = true;
13158         }
13159       /* If the next token is `::', there are no outputs, and the
13160          next token is the beginning of the inputs.  */
13161       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13162         /* The inputs are coming next.  */
13163         inputs_p = true;
13164
13165       /* Look for inputs.  */
13166       if (inputs_p
13167           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13168         {
13169           /* Consume the `:' or `::'.  */
13170           cp_lexer_consume_token (parser->lexer);
13171           /* Parse the output-operands.  */
13172           if (cp_lexer_next_token_is_not (parser->lexer,
13173                                           CPP_COLON)
13174               && cp_lexer_next_token_is_not (parser->lexer,
13175                                              CPP_SCOPE)
13176               && cp_lexer_next_token_is_not (parser->lexer,
13177                                              CPP_CLOSE_PAREN))
13178             inputs = cp_parser_asm_operand_list (parser);
13179
13180             if (inputs == error_mark_node)
13181               invalid_inputs_p = true;
13182         }
13183       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13184         /* The clobbers are coming next.  */
13185         clobbers_p = true;
13186
13187       /* Look for clobbers.  */
13188       if (clobbers_p
13189           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13190         {
13191           clobbers_p = true;
13192           /* Consume the `:' or `::'.  */
13193           cp_lexer_consume_token (parser->lexer);
13194           /* Parse the clobbers.  */
13195           if (cp_lexer_next_token_is_not (parser->lexer,
13196                                           CPP_COLON)
13197               && cp_lexer_next_token_is_not (parser->lexer,
13198                                              CPP_CLOSE_PAREN))
13199             clobbers = cp_parser_asm_clobber_list (parser);
13200         }
13201       else if (goto_p
13202                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13203         /* The labels are coming next.  */
13204         labels_p = true;
13205
13206       /* Look for labels.  */
13207       if (labels_p
13208           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13209         {
13210           labels_p = true;
13211           /* Consume the `:' or `::'.  */
13212           cp_lexer_consume_token (parser->lexer);
13213           /* Parse the labels.  */
13214           labels = cp_parser_asm_label_list (parser);
13215         }
13216
13217       if (goto_p && !labels_p)
13218         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13219     }
13220   else if (goto_p)
13221     missing = "%<:%> or %<::%>";
13222
13223   /* Look for the closing `)'.  */
13224   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13225                           missing ? missing : "%<)%>"))
13226     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13227                                            /*consume_paren=*/true);
13228   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13229
13230   if (!invalid_inputs_p && !invalid_outputs_p)
13231     {
13232       /* Create the ASM_EXPR.  */
13233       if (parser->in_function_body)
13234         {
13235           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13236                                       inputs, clobbers, labels);
13237           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13238           if (!extended_p)
13239             {
13240               tree temp = asm_stmt;
13241               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13242                 temp = TREE_OPERAND (temp, 0);
13243
13244               ASM_INPUT_P (temp) = 1;
13245             }
13246         }
13247       else
13248         cgraph_add_asm_node (string);
13249     }
13250 }
13251
13252 /* Declarators [gram.dcl.decl] */
13253
13254 /* Parse an init-declarator.
13255
13256    init-declarator:
13257      declarator initializer [opt]
13258
13259    GNU Extension:
13260
13261    init-declarator:
13262      declarator asm-specification [opt] attributes [opt] initializer [opt]
13263
13264    function-definition:
13265      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13266        function-body
13267      decl-specifier-seq [opt] declarator function-try-block
13268
13269    GNU Extension:
13270
13271    function-definition:
13272      __extension__ function-definition
13273
13274    The DECL_SPECIFIERS apply to this declarator.  Returns a
13275    representation of the entity declared.  If MEMBER_P is TRUE, then
13276    this declarator appears in a class scope.  The new DECL created by
13277    this declarator is returned.
13278
13279    The CHECKS are access checks that should be performed once we know
13280    what entity is being declared (and, therefore, what classes have
13281    befriended it).
13282
13283    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13284    for a function-definition here as well.  If the declarator is a
13285    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13286    be TRUE upon return.  By that point, the function-definition will
13287    have been completely parsed.
13288
13289    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13290    is FALSE.  */
13291
13292 static tree
13293 cp_parser_init_declarator (cp_parser* parser,
13294                            cp_decl_specifier_seq *decl_specifiers,
13295                            VEC (deferred_access_check,gc)* checks,
13296                            bool function_definition_allowed_p,
13297                            bool member_p,
13298                            int declares_class_or_enum,
13299                            bool* function_definition_p)
13300 {
13301   cp_token *token = NULL, *asm_spec_start_token = NULL,
13302            *attributes_start_token = NULL;
13303   cp_declarator *declarator;
13304   tree prefix_attributes;
13305   tree attributes;
13306   tree asm_specification;
13307   tree initializer;
13308   tree decl = NULL_TREE;
13309   tree scope;
13310   int is_initialized;
13311   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13312      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13313      "(...)".  */
13314   enum cpp_ttype initialization_kind;
13315   bool is_direct_init = false;
13316   bool is_non_constant_init;
13317   int ctor_dtor_or_conv_p;
13318   bool friend_p;
13319   tree pushed_scope = NULL;
13320
13321   /* Gather the attributes that were provided with the
13322      decl-specifiers.  */
13323   prefix_attributes = decl_specifiers->attributes;
13324
13325   /* Assume that this is not the declarator for a function
13326      definition.  */
13327   if (function_definition_p)
13328     *function_definition_p = false;
13329
13330   /* Defer access checks while parsing the declarator; we cannot know
13331      what names are accessible until we know what is being
13332      declared.  */
13333   resume_deferring_access_checks ();
13334
13335   /* Parse the declarator.  */
13336   token = cp_lexer_peek_token (parser->lexer);
13337   declarator
13338     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13339                             &ctor_dtor_or_conv_p,
13340                             /*parenthesized_p=*/NULL,
13341                             /*member_p=*/false);
13342   /* Gather up the deferred checks.  */
13343   stop_deferring_access_checks ();
13344
13345   /* If the DECLARATOR was erroneous, there's no need to go
13346      further.  */
13347   if (declarator == cp_error_declarator)
13348     return error_mark_node;
13349
13350   /* Check that the number of template-parameter-lists is OK.  */
13351   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13352                                                        token->location))
13353     return error_mark_node;
13354
13355   if (declares_class_or_enum & 2)
13356     cp_parser_check_for_definition_in_return_type (declarator,
13357                                                    decl_specifiers->type,
13358                                                    decl_specifiers->type_location);
13359
13360   /* Figure out what scope the entity declared by the DECLARATOR is
13361      located in.  `grokdeclarator' sometimes changes the scope, so
13362      we compute it now.  */
13363   scope = get_scope_of_declarator (declarator);
13364
13365   /* If we're allowing GNU extensions, look for an asm-specification
13366      and attributes.  */
13367   if (cp_parser_allow_gnu_extensions_p (parser))
13368     {
13369       /* Look for an asm-specification.  */
13370       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13371       asm_specification = cp_parser_asm_specification_opt (parser);
13372       /* And attributes.  */
13373       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13374       attributes = cp_parser_attributes_opt (parser);
13375     }
13376   else
13377     {
13378       asm_specification = NULL_TREE;
13379       attributes = NULL_TREE;
13380     }
13381
13382   /* Peek at the next token.  */
13383   token = cp_lexer_peek_token (parser->lexer);
13384   /* Check to see if the token indicates the start of a
13385      function-definition.  */
13386   if (function_declarator_p (declarator)
13387       && cp_parser_token_starts_function_definition_p (token))
13388     {
13389       if (!function_definition_allowed_p)
13390         {
13391           /* If a function-definition should not appear here, issue an
13392              error message.  */
13393           cp_parser_error (parser,
13394                            "a function-definition is not allowed here");
13395           return error_mark_node;
13396         }
13397       else
13398         {
13399           location_t func_brace_location
13400             = cp_lexer_peek_token (parser->lexer)->location;
13401
13402           /* Neither attributes nor an asm-specification are allowed
13403              on a function-definition.  */
13404           if (asm_specification)
13405             error_at (asm_spec_start_token->location,
13406                       "an asm-specification is not allowed "
13407                       "on a function-definition");
13408           if (attributes)
13409             error_at (attributes_start_token->location,
13410                       "attributes are not allowed on a function-definition");
13411           /* This is a function-definition.  */
13412           *function_definition_p = true;
13413
13414           /* Parse the function definition.  */
13415           if (member_p)
13416             decl = cp_parser_save_member_function_body (parser,
13417                                                         decl_specifiers,
13418                                                         declarator,
13419                                                         prefix_attributes);
13420           else
13421             decl
13422               = (cp_parser_function_definition_from_specifiers_and_declarator
13423                  (parser, decl_specifiers, prefix_attributes, declarator));
13424
13425           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13426             {
13427               /* This is where the prologue starts...  */
13428               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13429                 = func_brace_location;
13430             }
13431
13432           return decl;
13433         }
13434     }
13435
13436   /* [dcl.dcl]
13437
13438      Only in function declarations for constructors, destructors, and
13439      type conversions can the decl-specifier-seq be omitted.
13440
13441      We explicitly postpone this check past the point where we handle
13442      function-definitions because we tolerate function-definitions
13443      that are missing their return types in some modes.  */
13444   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13445     {
13446       cp_parser_error (parser,
13447                        "expected constructor, destructor, or type conversion");
13448       return error_mark_node;
13449     }
13450
13451   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13452   if (token->type == CPP_EQ
13453       || token->type == CPP_OPEN_PAREN
13454       || token->type == CPP_OPEN_BRACE)
13455     {
13456       is_initialized = SD_INITIALIZED;
13457       initialization_kind = token->type;
13458
13459       if (token->type == CPP_EQ
13460           && function_declarator_p (declarator))
13461         {
13462           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13463           if (t2->keyword == RID_DEFAULT)
13464             is_initialized = SD_DEFAULTED;
13465           else if (t2->keyword == RID_DELETE)
13466             is_initialized = SD_DELETED;
13467         }
13468     }
13469   else
13470     {
13471       /* If the init-declarator isn't initialized and isn't followed by a
13472          `,' or `;', it's not a valid init-declarator.  */
13473       if (token->type != CPP_COMMA
13474           && token->type != CPP_SEMICOLON)
13475         {
13476           cp_parser_error (parser, "expected initializer");
13477           return error_mark_node;
13478         }
13479       is_initialized = SD_UNINITIALIZED;
13480       initialization_kind = CPP_EOF;
13481     }
13482
13483   /* Because start_decl has side-effects, we should only call it if we
13484      know we're going ahead.  By this point, we know that we cannot
13485      possibly be looking at any other construct.  */
13486   cp_parser_commit_to_tentative_parse (parser);
13487
13488   /* If the decl specifiers were bad, issue an error now that we're
13489      sure this was intended to be a declarator.  Then continue
13490      declaring the variable(s), as int, to try to cut down on further
13491      errors.  */
13492   if (decl_specifiers->any_specifiers_p
13493       && decl_specifiers->type == error_mark_node)
13494     {
13495       cp_parser_error (parser, "invalid type in declaration");
13496       decl_specifiers->type = integer_type_node;
13497     }
13498
13499   /* Check to see whether or not this declaration is a friend.  */
13500   friend_p = cp_parser_friend_p (decl_specifiers);
13501
13502   /* Enter the newly declared entry in the symbol table.  If we're
13503      processing a declaration in a class-specifier, we wait until
13504      after processing the initializer.  */
13505   if (!member_p)
13506     {
13507       if (parser->in_unbraced_linkage_specification_p)
13508         decl_specifiers->storage_class = sc_extern;
13509       decl = start_decl (declarator, decl_specifiers,
13510                          is_initialized, attributes, prefix_attributes,
13511                          &pushed_scope);
13512     }
13513   else if (scope)
13514     /* Enter the SCOPE.  That way unqualified names appearing in the
13515        initializer will be looked up in SCOPE.  */
13516     pushed_scope = push_scope (scope);
13517
13518   /* Perform deferred access control checks, now that we know in which
13519      SCOPE the declared entity resides.  */
13520   if (!member_p && decl)
13521     {
13522       tree saved_current_function_decl = NULL_TREE;
13523
13524       /* If the entity being declared is a function, pretend that we
13525          are in its scope.  If it is a `friend', it may have access to
13526          things that would not otherwise be accessible.  */
13527       if (TREE_CODE (decl) == FUNCTION_DECL)
13528         {
13529           saved_current_function_decl = current_function_decl;
13530           current_function_decl = decl;
13531         }
13532
13533       /* Perform access checks for template parameters.  */
13534       cp_parser_perform_template_parameter_access_checks (checks);
13535
13536       /* Perform the access control checks for the declarator and the
13537          decl-specifiers.  */
13538       perform_deferred_access_checks ();
13539
13540       /* Restore the saved value.  */
13541       if (TREE_CODE (decl) == FUNCTION_DECL)
13542         current_function_decl = saved_current_function_decl;
13543     }
13544
13545   /* Parse the initializer.  */
13546   initializer = NULL_TREE;
13547   is_direct_init = false;
13548   is_non_constant_init = true;
13549   if (is_initialized)
13550     {
13551       if (function_declarator_p (declarator))
13552         {
13553           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13554            if (initialization_kind == CPP_EQ)
13555              initializer = cp_parser_pure_specifier (parser);
13556            else
13557              {
13558                /* If the declaration was erroneous, we don't really
13559                   know what the user intended, so just silently
13560                   consume the initializer.  */
13561                if (decl != error_mark_node)
13562                  error_at (initializer_start_token->location,
13563                            "initializer provided for function");
13564                cp_parser_skip_to_closing_parenthesis (parser,
13565                                                       /*recovering=*/true,
13566                                                       /*or_comma=*/false,
13567                                                       /*consume_paren=*/true);
13568              }
13569         }
13570       else
13571         {
13572           /* We want to record the extra mangling scope for in-class
13573              initializers of class members and initializers of static data
13574              member templates.  The former is a C++0x feature which isn't
13575              implemented yet, and I expect it will involve deferring
13576              parsing of the initializer until end of class as with default
13577              arguments.  So right here we only handle the latter.  */
13578           if (!member_p && processing_template_decl)
13579             start_lambda_scope (decl);
13580           initializer = cp_parser_initializer (parser,
13581                                                &is_direct_init,
13582                                                &is_non_constant_init);
13583           if (!member_p && processing_template_decl)
13584             finish_lambda_scope ();
13585         }
13586     }
13587
13588   /* The old parser allows attributes to appear after a parenthesized
13589      initializer.  Mark Mitchell proposed removing this functionality
13590      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13591      attributes -- but ignores them.  */
13592   if (cp_parser_allow_gnu_extensions_p (parser)
13593       && initialization_kind == CPP_OPEN_PAREN)
13594     if (cp_parser_attributes_opt (parser))
13595       warning (OPT_Wattributes,
13596                "attributes after parenthesized initializer ignored");
13597
13598   /* For an in-class declaration, use `grokfield' to create the
13599      declaration.  */
13600   if (member_p)
13601     {
13602       if (pushed_scope)
13603         {
13604           pop_scope (pushed_scope);
13605           pushed_scope = false;
13606         }
13607       decl = grokfield (declarator, decl_specifiers,
13608                         initializer, !is_non_constant_init,
13609                         /*asmspec=*/NULL_TREE,
13610                         prefix_attributes);
13611       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13612         cp_parser_save_default_args (parser, decl);
13613     }
13614
13615   /* Finish processing the declaration.  But, skip friend
13616      declarations.  */
13617   if (!friend_p && decl && decl != error_mark_node)
13618     {
13619       cp_finish_decl (decl,
13620                       initializer, !is_non_constant_init,
13621                       asm_specification,
13622                       /* If the initializer is in parentheses, then this is
13623                          a direct-initialization, which means that an
13624                          `explicit' constructor is OK.  Otherwise, an
13625                          `explicit' constructor cannot be used.  */
13626                       ((is_direct_init || !is_initialized)
13627                        ? 0 : LOOKUP_ONLYCONVERTING));
13628     }
13629   else if ((cxx_dialect != cxx98) && friend_p
13630            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13631     /* Core issue #226 (C++0x only): A default template-argument
13632        shall not be specified in a friend class template
13633        declaration. */
13634     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13635                              /*is_partial=*/0, /*is_friend_decl=*/1);
13636
13637   if (!friend_p && pushed_scope)
13638     pop_scope (pushed_scope);
13639
13640   return decl;
13641 }
13642
13643 /* Parse a declarator.
13644
13645    declarator:
13646      direct-declarator
13647      ptr-operator declarator
13648
13649    abstract-declarator:
13650      ptr-operator abstract-declarator [opt]
13651      direct-abstract-declarator
13652
13653    GNU Extensions:
13654
13655    declarator:
13656      attributes [opt] direct-declarator
13657      attributes [opt] ptr-operator declarator
13658
13659    abstract-declarator:
13660      attributes [opt] ptr-operator abstract-declarator [opt]
13661      attributes [opt] direct-abstract-declarator
13662
13663    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13664    detect constructor, destructor or conversion operators. It is set
13665    to -1 if the declarator is a name, and +1 if it is a
13666    function. Otherwise it is set to zero. Usually you just want to
13667    test for >0, but internally the negative value is used.
13668
13669    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13670    a decl-specifier-seq unless it declares a constructor, destructor,
13671    or conversion.  It might seem that we could check this condition in
13672    semantic analysis, rather than parsing, but that makes it difficult
13673    to handle something like `f()'.  We want to notice that there are
13674    no decl-specifiers, and therefore realize that this is an
13675    expression, not a declaration.)
13676
13677    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13678    the declarator is a direct-declarator of the form "(...)".
13679
13680    MEMBER_P is true iff this declarator is a member-declarator.  */
13681
13682 static cp_declarator *
13683 cp_parser_declarator (cp_parser* parser,
13684                       cp_parser_declarator_kind dcl_kind,
13685                       int* ctor_dtor_or_conv_p,
13686                       bool* parenthesized_p,
13687                       bool member_p)
13688 {
13689   cp_token *token;
13690   cp_declarator *declarator;
13691   enum tree_code code;
13692   cp_cv_quals cv_quals;
13693   tree class_type;
13694   tree attributes = NULL_TREE;
13695
13696   /* Assume this is not a constructor, destructor, or type-conversion
13697      operator.  */
13698   if (ctor_dtor_or_conv_p)
13699     *ctor_dtor_or_conv_p = 0;
13700
13701   if (cp_parser_allow_gnu_extensions_p (parser))
13702     attributes = cp_parser_attributes_opt (parser);
13703
13704   /* Peek at the next token.  */
13705   token = cp_lexer_peek_token (parser->lexer);
13706
13707   /* Check for the ptr-operator production.  */
13708   cp_parser_parse_tentatively (parser);
13709   /* Parse the ptr-operator.  */
13710   code = cp_parser_ptr_operator (parser,
13711                                  &class_type,
13712                                  &cv_quals);
13713   /* If that worked, then we have a ptr-operator.  */
13714   if (cp_parser_parse_definitely (parser))
13715     {
13716       /* If a ptr-operator was found, then this declarator was not
13717          parenthesized.  */
13718       if (parenthesized_p)
13719         *parenthesized_p = true;
13720       /* The dependent declarator is optional if we are parsing an
13721          abstract-declarator.  */
13722       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13723         cp_parser_parse_tentatively (parser);
13724
13725       /* Parse the dependent declarator.  */
13726       declarator = cp_parser_declarator (parser, dcl_kind,
13727                                          /*ctor_dtor_or_conv_p=*/NULL,
13728                                          /*parenthesized_p=*/NULL,
13729                                          /*member_p=*/false);
13730
13731       /* If we are parsing an abstract-declarator, we must handle the
13732          case where the dependent declarator is absent.  */
13733       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13734           && !cp_parser_parse_definitely (parser))
13735         declarator = NULL;
13736
13737       declarator = cp_parser_make_indirect_declarator
13738         (code, class_type, cv_quals, declarator);
13739     }
13740   /* Everything else is a direct-declarator.  */
13741   else
13742     {
13743       if (parenthesized_p)
13744         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13745                                                    CPP_OPEN_PAREN);
13746       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13747                                                 ctor_dtor_or_conv_p,
13748                                                 member_p);
13749     }
13750
13751   if (attributes && declarator && declarator != cp_error_declarator)
13752     declarator->attributes = attributes;
13753
13754   return declarator;
13755 }
13756
13757 /* Parse a direct-declarator or direct-abstract-declarator.
13758
13759    direct-declarator:
13760      declarator-id
13761      direct-declarator ( parameter-declaration-clause )
13762        cv-qualifier-seq [opt]
13763        exception-specification [opt]
13764      direct-declarator [ constant-expression [opt] ]
13765      ( declarator )
13766
13767    direct-abstract-declarator:
13768      direct-abstract-declarator [opt]
13769        ( parameter-declaration-clause )
13770        cv-qualifier-seq [opt]
13771        exception-specification [opt]
13772      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13773      ( abstract-declarator )
13774
13775    Returns a representation of the declarator.  DCL_KIND is
13776    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13777    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13778    we are parsing a direct-declarator.  It is
13779    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13780    of ambiguity we prefer an abstract declarator, as per
13781    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13782    cp_parser_declarator.  */
13783
13784 static cp_declarator *
13785 cp_parser_direct_declarator (cp_parser* parser,
13786                              cp_parser_declarator_kind dcl_kind,
13787                              int* ctor_dtor_or_conv_p,
13788                              bool member_p)
13789 {
13790   cp_token *token;
13791   cp_declarator *declarator = NULL;
13792   tree scope = NULL_TREE;
13793   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13794   bool saved_in_declarator_p = parser->in_declarator_p;
13795   bool first = true;
13796   tree pushed_scope = NULL_TREE;
13797
13798   while (true)
13799     {
13800       /* Peek at the next token.  */
13801       token = cp_lexer_peek_token (parser->lexer);
13802       if (token->type == CPP_OPEN_PAREN)
13803         {
13804           /* This is either a parameter-declaration-clause, or a
13805              parenthesized declarator. When we know we are parsing a
13806              named declarator, it must be a parenthesized declarator
13807              if FIRST is true. For instance, `(int)' is a
13808              parameter-declaration-clause, with an omitted
13809              direct-abstract-declarator. But `((*))', is a
13810              parenthesized abstract declarator. Finally, when T is a
13811              template parameter `(T)' is a
13812              parameter-declaration-clause, and not a parenthesized
13813              named declarator.
13814
13815              We first try and parse a parameter-declaration-clause,
13816              and then try a nested declarator (if FIRST is true).
13817
13818              It is not an error for it not to be a
13819              parameter-declaration-clause, even when FIRST is
13820              false. Consider,
13821
13822                int i (int);
13823                int i (3);
13824
13825              The first is the declaration of a function while the
13826              second is the definition of a variable, including its
13827              initializer.
13828
13829              Having seen only the parenthesis, we cannot know which of
13830              these two alternatives should be selected.  Even more
13831              complex are examples like:
13832
13833                int i (int (a));
13834                int i (int (3));
13835
13836              The former is a function-declaration; the latter is a
13837              variable initialization.
13838
13839              Thus again, we try a parameter-declaration-clause, and if
13840              that fails, we back out and return.  */
13841
13842           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13843             {
13844               tree params;
13845               unsigned saved_num_template_parameter_lists;
13846               bool is_declarator = false;
13847               tree t;
13848
13849               /* In a member-declarator, the only valid interpretation
13850                  of a parenthesis is the start of a
13851                  parameter-declaration-clause.  (It is invalid to
13852                  initialize a static data member with a parenthesized
13853                  initializer; only the "=" form of initialization is
13854                  permitted.)  */
13855               if (!member_p)
13856                 cp_parser_parse_tentatively (parser);
13857
13858               /* Consume the `('.  */
13859               cp_lexer_consume_token (parser->lexer);
13860               if (first)
13861                 {
13862                   /* If this is going to be an abstract declarator, we're
13863                      in a declarator and we can't have default args.  */
13864                   parser->default_arg_ok_p = false;
13865                   parser->in_declarator_p = true;
13866                 }
13867
13868               /* Inside the function parameter list, surrounding
13869                  template-parameter-lists do not apply.  */
13870               saved_num_template_parameter_lists
13871                 = parser->num_template_parameter_lists;
13872               parser->num_template_parameter_lists = 0;
13873
13874               begin_scope (sk_function_parms, NULL_TREE);
13875
13876               /* Parse the parameter-declaration-clause.  */
13877               params = cp_parser_parameter_declaration_clause (parser);
13878
13879               parser->num_template_parameter_lists
13880                 = saved_num_template_parameter_lists;
13881
13882               /* If all went well, parse the cv-qualifier-seq and the
13883                  exception-specification.  */
13884               if (member_p || cp_parser_parse_definitely (parser))
13885                 {
13886                   cp_cv_quals cv_quals;
13887                   tree exception_specification;
13888                   tree late_return;
13889
13890                   is_declarator = true;
13891
13892                   if (ctor_dtor_or_conv_p)
13893                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13894                   first = false;
13895                   /* Consume the `)'.  */
13896                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13897
13898                   /* Parse the cv-qualifier-seq.  */
13899                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13900                   /* And the exception-specification.  */
13901                   exception_specification
13902                     = cp_parser_exception_specification_opt (parser);
13903
13904                   late_return
13905                     = cp_parser_late_return_type_opt (parser);
13906
13907                   /* Create the function-declarator.  */
13908                   declarator = make_call_declarator (declarator,
13909                                                      params,
13910                                                      cv_quals,
13911                                                      exception_specification,
13912                                                      late_return);
13913                   /* Any subsequent parameter lists are to do with
13914                      return type, so are not those of the declared
13915                      function.  */
13916                   parser->default_arg_ok_p = false;
13917                 }
13918
13919               /* Remove the function parms from scope.  */
13920               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13921                 pop_binding (DECL_NAME (t), t);
13922               leave_scope();
13923
13924               if (is_declarator)
13925                 /* Repeat the main loop.  */
13926                 continue;
13927             }
13928
13929           /* If this is the first, we can try a parenthesized
13930              declarator.  */
13931           if (first)
13932             {
13933               bool saved_in_type_id_in_expr_p;
13934
13935               parser->default_arg_ok_p = saved_default_arg_ok_p;
13936               parser->in_declarator_p = saved_in_declarator_p;
13937
13938               /* Consume the `('.  */
13939               cp_lexer_consume_token (parser->lexer);
13940               /* Parse the nested declarator.  */
13941               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13942               parser->in_type_id_in_expr_p = true;
13943               declarator
13944                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13945                                         /*parenthesized_p=*/NULL,
13946                                         member_p);
13947               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13948               first = false;
13949               /* Expect a `)'.  */
13950               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13951                 declarator = cp_error_declarator;
13952               if (declarator == cp_error_declarator)
13953                 break;
13954
13955               goto handle_declarator;
13956             }
13957           /* Otherwise, we must be done.  */
13958           else
13959             break;
13960         }
13961       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13962                && token->type == CPP_OPEN_SQUARE)
13963         {
13964           /* Parse an array-declarator.  */
13965           tree bounds;
13966
13967           if (ctor_dtor_or_conv_p)
13968             *ctor_dtor_or_conv_p = 0;
13969
13970           first = false;
13971           parser->default_arg_ok_p = false;
13972           parser->in_declarator_p = true;
13973           /* Consume the `['.  */
13974           cp_lexer_consume_token (parser->lexer);
13975           /* Peek at the next token.  */
13976           token = cp_lexer_peek_token (parser->lexer);
13977           /* If the next token is `]', then there is no
13978              constant-expression.  */
13979           if (token->type != CPP_CLOSE_SQUARE)
13980             {
13981               bool non_constant_p;
13982
13983               bounds
13984                 = cp_parser_constant_expression (parser,
13985                                                  /*allow_non_constant=*/true,
13986                                                  &non_constant_p);
13987               if (!non_constant_p)
13988                 bounds = fold_non_dependent_expr (bounds);
13989               /* Normally, the array bound must be an integral constant
13990                  expression.  However, as an extension, we allow VLAs
13991                  in function scopes.  */
13992               else if (!parser->in_function_body)
13993                 {
13994                   error_at (token->location,
13995                             "array bound is not an integer constant");
13996                   bounds = error_mark_node;
13997                 }
13998               else if (processing_template_decl && !error_operand_p (bounds))
13999                 {
14000                   /* Remember this wasn't a constant-expression.  */
14001                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14002                   TREE_SIDE_EFFECTS (bounds) = 1;
14003                 }
14004             }
14005           else
14006             bounds = NULL_TREE;
14007           /* Look for the closing `]'.  */
14008           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14009             {
14010               declarator = cp_error_declarator;
14011               break;
14012             }
14013
14014           declarator = make_array_declarator (declarator, bounds);
14015         }
14016       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14017         {
14018           {
14019             tree qualifying_scope;
14020             tree unqualified_name;
14021             special_function_kind sfk;
14022             bool abstract_ok;
14023             bool pack_expansion_p = false;
14024             cp_token *declarator_id_start_token;
14025
14026             /* Parse a declarator-id */
14027             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14028             if (abstract_ok)
14029               {
14030                 cp_parser_parse_tentatively (parser);
14031
14032                 /* If we see an ellipsis, we should be looking at a
14033                    parameter pack. */
14034                 if (token->type == CPP_ELLIPSIS)
14035                   {
14036                     /* Consume the `...' */
14037                     cp_lexer_consume_token (parser->lexer);
14038
14039                     pack_expansion_p = true;
14040                   }
14041               }
14042
14043             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14044             unqualified_name
14045               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14046             qualifying_scope = parser->scope;
14047             if (abstract_ok)
14048               {
14049                 bool okay = false;
14050
14051                 if (!unqualified_name && pack_expansion_p)
14052                   {
14053                     /* Check whether an error occurred. */
14054                     okay = !cp_parser_error_occurred (parser);
14055
14056                     /* We already consumed the ellipsis to mark a
14057                        parameter pack, but we have no way to report it,
14058                        so abort the tentative parse. We will be exiting
14059                        immediately anyway. */
14060                     cp_parser_abort_tentative_parse (parser);
14061                   }
14062                 else
14063                   okay = cp_parser_parse_definitely (parser);
14064
14065                 if (!okay)
14066                   unqualified_name = error_mark_node;
14067                 else if (unqualified_name
14068                          && (qualifying_scope
14069                              || (TREE_CODE (unqualified_name)
14070                                  != IDENTIFIER_NODE)))
14071                   {
14072                     cp_parser_error (parser, "expected unqualified-id");
14073                     unqualified_name = error_mark_node;
14074                   }
14075               }
14076
14077             if (!unqualified_name)
14078               return NULL;
14079             if (unqualified_name == error_mark_node)
14080               {
14081                 declarator = cp_error_declarator;
14082                 pack_expansion_p = false;
14083                 declarator->parameter_pack_p = false;
14084                 break;
14085               }
14086
14087             if (qualifying_scope && at_namespace_scope_p ()
14088                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14089               {
14090                 /* In the declaration of a member of a template class
14091                    outside of the class itself, the SCOPE will sometimes
14092                    be a TYPENAME_TYPE.  For example, given:
14093
14094                    template <typename T>
14095                    int S<T>::R::i = 3;
14096
14097                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14098                    this context, we must resolve S<T>::R to an ordinary
14099                    type, rather than a typename type.
14100
14101                    The reason we normally avoid resolving TYPENAME_TYPEs
14102                    is that a specialization of `S' might render
14103                    `S<T>::R' not a type.  However, if `S' is
14104                    specialized, then this `i' will not be used, so there
14105                    is no harm in resolving the types here.  */
14106                 tree type;
14107
14108                 /* Resolve the TYPENAME_TYPE.  */
14109                 type = resolve_typename_type (qualifying_scope,
14110                                               /*only_current_p=*/false);
14111                 /* If that failed, the declarator is invalid.  */
14112                 if (TREE_CODE (type) == TYPENAME_TYPE)
14113                   error_at (declarator_id_start_token->location,
14114                             "%<%T::%E%> is not a type",
14115                             TYPE_CONTEXT (qualifying_scope),
14116                             TYPE_IDENTIFIER (qualifying_scope));
14117                 qualifying_scope = type;
14118               }
14119
14120             sfk = sfk_none;
14121
14122             if (unqualified_name)
14123               {
14124                 tree class_type;
14125
14126                 if (qualifying_scope
14127                     && CLASS_TYPE_P (qualifying_scope))
14128                   class_type = qualifying_scope;
14129                 else
14130                   class_type = current_class_type;
14131
14132                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14133                   {
14134                     tree name_type = TREE_TYPE (unqualified_name);
14135                     if (class_type && same_type_p (name_type, class_type))
14136                       {
14137                         if (qualifying_scope
14138                             && CLASSTYPE_USE_TEMPLATE (name_type))
14139                           {
14140                             error_at (declarator_id_start_token->location,
14141                                       "invalid use of constructor as a template");
14142                             inform (declarator_id_start_token->location,
14143                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14144                                     "name the constructor in a qualified name",
14145                                     class_type,
14146                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14147                                     class_type, name_type);
14148                             declarator = cp_error_declarator;
14149                             break;
14150                           }
14151                         else
14152                           unqualified_name = constructor_name (class_type);
14153                       }
14154                     else
14155                       {
14156                         /* We do not attempt to print the declarator
14157                            here because we do not have enough
14158                            information about its original syntactic
14159                            form.  */
14160                         cp_parser_error (parser, "invalid declarator");
14161                         declarator = cp_error_declarator;
14162                         break;
14163                       }
14164                   }
14165
14166                 if (class_type)
14167                   {
14168                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14169                       sfk = sfk_destructor;
14170                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14171                       sfk = sfk_conversion;
14172                     else if (/* There's no way to declare a constructor
14173                                 for an anonymous type, even if the type
14174                                 got a name for linkage purposes.  */
14175                              !TYPE_WAS_ANONYMOUS (class_type)
14176                              && constructor_name_p (unqualified_name,
14177                                                     class_type))
14178                       {
14179                         unqualified_name = constructor_name (class_type);
14180                         sfk = sfk_constructor;
14181                       }
14182
14183                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14184                       *ctor_dtor_or_conv_p = -1;
14185                   }
14186               }
14187             declarator = make_id_declarator (qualifying_scope,
14188                                              unqualified_name,
14189                                              sfk);
14190             declarator->id_loc = token->location;
14191             declarator->parameter_pack_p = pack_expansion_p;
14192
14193             if (pack_expansion_p)
14194               maybe_warn_variadic_templates ();
14195           }
14196
14197         handle_declarator:;
14198           scope = get_scope_of_declarator (declarator);
14199           if (scope)
14200             /* Any names that appear after the declarator-id for a
14201                member are looked up in the containing scope.  */
14202             pushed_scope = push_scope (scope);
14203           parser->in_declarator_p = true;
14204           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14205               || (declarator && declarator->kind == cdk_id))
14206             /* Default args are only allowed on function
14207                declarations.  */
14208             parser->default_arg_ok_p = saved_default_arg_ok_p;
14209           else
14210             parser->default_arg_ok_p = false;
14211
14212           first = false;
14213         }
14214       /* We're done.  */
14215       else
14216         break;
14217     }
14218
14219   /* For an abstract declarator, we might wind up with nothing at this
14220      point.  That's an error; the declarator is not optional.  */
14221   if (!declarator)
14222     cp_parser_error (parser, "expected declarator");
14223
14224   /* If we entered a scope, we must exit it now.  */
14225   if (pushed_scope)
14226     pop_scope (pushed_scope);
14227
14228   parser->default_arg_ok_p = saved_default_arg_ok_p;
14229   parser->in_declarator_p = saved_in_declarator_p;
14230
14231   return declarator;
14232 }
14233
14234 /* Parse a ptr-operator.
14235
14236    ptr-operator:
14237      * cv-qualifier-seq [opt]
14238      &
14239      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14240
14241    GNU Extension:
14242
14243    ptr-operator:
14244      & cv-qualifier-seq [opt]
14245
14246    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14247    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14248    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14249    filled in with the TYPE containing the member.  *CV_QUALS is
14250    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14251    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14252    Note that the tree codes returned by this function have nothing
14253    to do with the types of trees that will be eventually be created
14254    to represent the pointer or reference type being parsed. They are
14255    just constants with suggestive names. */
14256 static enum tree_code
14257 cp_parser_ptr_operator (cp_parser* parser,
14258                         tree* type,
14259                         cp_cv_quals *cv_quals)
14260 {
14261   enum tree_code code = ERROR_MARK;
14262   cp_token *token;
14263
14264   /* Assume that it's not a pointer-to-member.  */
14265   *type = NULL_TREE;
14266   /* And that there are no cv-qualifiers.  */
14267   *cv_quals = TYPE_UNQUALIFIED;
14268
14269   /* Peek at the next token.  */
14270   token = cp_lexer_peek_token (parser->lexer);
14271
14272   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14273   if (token->type == CPP_MULT)
14274     code = INDIRECT_REF;
14275   else if (token->type == CPP_AND)
14276     code = ADDR_EXPR;
14277   else if ((cxx_dialect != cxx98) &&
14278            token->type == CPP_AND_AND) /* C++0x only */
14279     code = NON_LVALUE_EXPR;
14280
14281   if (code != ERROR_MARK)
14282     {
14283       /* Consume the `*', `&' or `&&'.  */
14284       cp_lexer_consume_token (parser->lexer);
14285
14286       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14287          `&', if we are allowing GNU extensions.  (The only qualifier
14288          that can legally appear after `&' is `restrict', but that is
14289          enforced during semantic analysis.  */
14290       if (code == INDIRECT_REF
14291           || cp_parser_allow_gnu_extensions_p (parser))
14292         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14293     }
14294   else
14295     {
14296       /* Try the pointer-to-member case.  */
14297       cp_parser_parse_tentatively (parser);
14298       /* Look for the optional `::' operator.  */
14299       cp_parser_global_scope_opt (parser,
14300                                   /*current_scope_valid_p=*/false);
14301       /* Look for the nested-name specifier.  */
14302       token = cp_lexer_peek_token (parser->lexer);
14303       cp_parser_nested_name_specifier (parser,
14304                                        /*typename_keyword_p=*/false,
14305                                        /*check_dependency_p=*/true,
14306                                        /*type_p=*/false,
14307                                        /*is_declaration=*/false);
14308       /* If we found it, and the next token is a `*', then we are
14309          indeed looking at a pointer-to-member operator.  */
14310       if (!cp_parser_error_occurred (parser)
14311           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14312         {
14313           /* Indicate that the `*' operator was used.  */
14314           code = INDIRECT_REF;
14315
14316           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14317             error_at (token->location, "%qD is a namespace", parser->scope);
14318           else
14319             {
14320               /* The type of which the member is a member is given by the
14321                  current SCOPE.  */
14322               *type = parser->scope;
14323               /* The next name will not be qualified.  */
14324               parser->scope = NULL_TREE;
14325               parser->qualifying_scope = NULL_TREE;
14326               parser->object_scope = NULL_TREE;
14327               /* Look for the optional cv-qualifier-seq.  */
14328               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14329             }
14330         }
14331       /* If that didn't work we don't have a ptr-operator.  */
14332       if (!cp_parser_parse_definitely (parser))
14333         cp_parser_error (parser, "expected ptr-operator");
14334     }
14335
14336   return code;
14337 }
14338
14339 /* Parse an (optional) cv-qualifier-seq.
14340
14341    cv-qualifier-seq:
14342      cv-qualifier cv-qualifier-seq [opt]
14343
14344    cv-qualifier:
14345      const
14346      volatile
14347
14348    GNU Extension:
14349
14350    cv-qualifier:
14351      __restrict__
14352
14353    Returns a bitmask representing the cv-qualifiers.  */
14354
14355 static cp_cv_quals
14356 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14357 {
14358   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14359
14360   while (true)
14361     {
14362       cp_token *token;
14363       cp_cv_quals cv_qualifier;
14364
14365       /* Peek at the next token.  */
14366       token = cp_lexer_peek_token (parser->lexer);
14367       /* See if it's a cv-qualifier.  */
14368       switch (token->keyword)
14369         {
14370         case RID_CONST:
14371           cv_qualifier = TYPE_QUAL_CONST;
14372           break;
14373
14374         case RID_VOLATILE:
14375           cv_qualifier = TYPE_QUAL_VOLATILE;
14376           break;
14377
14378         case RID_RESTRICT:
14379           cv_qualifier = TYPE_QUAL_RESTRICT;
14380           break;
14381
14382         default:
14383           cv_qualifier = TYPE_UNQUALIFIED;
14384           break;
14385         }
14386
14387       if (!cv_qualifier)
14388         break;
14389
14390       if (cv_quals & cv_qualifier)
14391         {
14392           error_at (token->location, "duplicate cv-qualifier");
14393           cp_lexer_purge_token (parser->lexer);
14394         }
14395       else
14396         {
14397           cp_lexer_consume_token (parser->lexer);
14398           cv_quals |= cv_qualifier;
14399         }
14400     }
14401
14402   return cv_quals;
14403 }
14404
14405 /* Parse a late-specified return type, if any.  This is not a separate
14406    non-terminal, but part of a function declarator, which looks like
14407
14408    -> type-id
14409
14410    Returns the type indicated by the type-id.  */
14411
14412 static tree
14413 cp_parser_late_return_type_opt (cp_parser* parser)
14414 {
14415   cp_token *token;
14416
14417   /* Peek at the next token.  */
14418   token = cp_lexer_peek_token (parser->lexer);
14419   /* A late-specified return type is indicated by an initial '->'. */
14420   if (token->type != CPP_DEREF)
14421     return NULL_TREE;
14422
14423   /* Consume the ->.  */
14424   cp_lexer_consume_token (parser->lexer);
14425
14426   return cp_parser_type_id (parser);
14427 }
14428
14429 /* Parse a declarator-id.
14430
14431    declarator-id:
14432      id-expression
14433      :: [opt] nested-name-specifier [opt] type-name
14434
14435    In the `id-expression' case, the value returned is as for
14436    cp_parser_id_expression if the id-expression was an unqualified-id.
14437    If the id-expression was a qualified-id, then a SCOPE_REF is
14438    returned.  The first operand is the scope (either a NAMESPACE_DECL
14439    or TREE_TYPE), but the second is still just a representation of an
14440    unqualified-id.  */
14441
14442 static tree
14443 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14444 {
14445   tree id;
14446   /* The expression must be an id-expression.  Assume that qualified
14447      names are the names of types so that:
14448
14449        template <class T>
14450        int S<T>::R::i = 3;
14451
14452      will work; we must treat `S<T>::R' as the name of a type.
14453      Similarly, assume that qualified names are templates, where
14454      required, so that:
14455
14456        template <class T>
14457        int S<T>::R<T>::i = 3;
14458
14459      will work, too.  */
14460   id = cp_parser_id_expression (parser,
14461                                 /*template_keyword_p=*/false,
14462                                 /*check_dependency_p=*/false,
14463                                 /*template_p=*/NULL,
14464                                 /*declarator_p=*/true,
14465                                 optional_p);
14466   if (id && BASELINK_P (id))
14467     id = BASELINK_FUNCTIONS (id);
14468   return id;
14469 }
14470
14471 /* Parse a type-id.
14472
14473    type-id:
14474      type-specifier-seq abstract-declarator [opt]
14475
14476    Returns the TYPE specified.  */
14477
14478 static tree
14479 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
14480 {
14481   cp_decl_specifier_seq type_specifier_seq;
14482   cp_declarator *abstract_declarator;
14483
14484   /* Parse the type-specifier-seq.  */
14485   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14486                                 &type_specifier_seq);
14487   if (type_specifier_seq.type == error_mark_node)
14488     return error_mark_node;
14489
14490   /* There might or might not be an abstract declarator.  */
14491   cp_parser_parse_tentatively (parser);
14492   /* Look for the declarator.  */
14493   abstract_declarator
14494     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14495                             /*parenthesized_p=*/NULL,
14496                             /*member_p=*/false);
14497   /* Check to see if there really was a declarator.  */
14498   if (!cp_parser_parse_definitely (parser))
14499     abstract_declarator = NULL;
14500
14501   if (type_specifier_seq.type
14502       && type_uses_auto (type_specifier_seq.type))
14503     {
14504       /* A type-id with type 'auto' is only ok if the abstract declarator
14505          is a function declarator with a late-specified return type.  */
14506       if (abstract_declarator
14507           && abstract_declarator->kind == cdk_function
14508           && abstract_declarator->u.function.late_return_type)
14509         /* OK */;
14510       else
14511         {
14512           error ("invalid use of %<auto%>");
14513           return error_mark_node;
14514         }
14515     }
14516   
14517   return groktypename (&type_specifier_seq, abstract_declarator,
14518                        is_template_arg);
14519 }
14520
14521 static tree cp_parser_type_id (cp_parser *parser)
14522 {
14523   return cp_parser_type_id_1 (parser, false);
14524 }
14525
14526 static tree cp_parser_template_type_arg (cp_parser *parser)
14527 {
14528   return cp_parser_type_id_1 (parser, true);
14529 }
14530
14531 /* Parse a type-specifier-seq.
14532
14533    type-specifier-seq:
14534      type-specifier type-specifier-seq [opt]
14535
14536    GNU extension:
14537
14538    type-specifier-seq:
14539      attributes type-specifier-seq [opt]
14540
14541    If IS_CONDITION is true, we are at the start of a "condition",
14542    e.g., we've just seen "if (".
14543
14544    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14545
14546 static void
14547 cp_parser_type_specifier_seq (cp_parser* parser,
14548                               bool is_condition,
14549                               cp_decl_specifier_seq *type_specifier_seq)
14550 {
14551   bool seen_type_specifier = false;
14552   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14553   cp_token *start_token = NULL;
14554
14555   /* Clear the TYPE_SPECIFIER_SEQ.  */
14556   clear_decl_specs (type_specifier_seq);
14557
14558   /* Parse the type-specifiers and attributes.  */
14559   while (true)
14560     {
14561       tree type_specifier;
14562       bool is_cv_qualifier;
14563
14564       /* Check for attributes first.  */
14565       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14566         {
14567           type_specifier_seq->attributes =
14568             chainon (type_specifier_seq->attributes,
14569                      cp_parser_attributes_opt (parser));
14570           continue;
14571         }
14572
14573       /* record the token of the beginning of the type specifier seq,
14574          for error reporting purposes*/
14575      if (!start_token)
14576        start_token = cp_lexer_peek_token (parser->lexer);
14577
14578       /* Look for the type-specifier.  */
14579       type_specifier = cp_parser_type_specifier (parser,
14580                                                  flags,
14581                                                  type_specifier_seq,
14582                                                  /*is_declaration=*/false,
14583                                                  NULL,
14584                                                  &is_cv_qualifier);
14585       if (!type_specifier)
14586         {
14587           /* If the first type-specifier could not be found, this is not a
14588              type-specifier-seq at all.  */
14589           if (!seen_type_specifier)
14590             {
14591               cp_parser_error (parser, "expected type-specifier");
14592               type_specifier_seq->type = error_mark_node;
14593               return;
14594             }
14595           /* If subsequent type-specifiers could not be found, the
14596              type-specifier-seq is complete.  */
14597           break;
14598         }
14599
14600       seen_type_specifier = true;
14601       /* The standard says that a condition can be:
14602
14603             type-specifier-seq declarator = assignment-expression
14604
14605          However, given:
14606
14607            struct S {};
14608            if (int S = ...)
14609
14610          we should treat the "S" as a declarator, not as a
14611          type-specifier.  The standard doesn't say that explicitly for
14612          type-specifier-seq, but it does say that for
14613          decl-specifier-seq in an ordinary declaration.  Perhaps it
14614          would be clearer just to allow a decl-specifier-seq here, and
14615          then add a semantic restriction that if any decl-specifiers
14616          that are not type-specifiers appear, the program is invalid.  */
14617       if (is_condition && !is_cv_qualifier)
14618         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14619     }
14620
14621   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14622 }
14623
14624 /* Parse a parameter-declaration-clause.
14625
14626    parameter-declaration-clause:
14627      parameter-declaration-list [opt] ... [opt]
14628      parameter-declaration-list , ...
14629
14630    Returns a representation for the parameter declarations.  A return
14631    value of NULL indicates a parameter-declaration-clause consisting
14632    only of an ellipsis.  */
14633
14634 static tree
14635 cp_parser_parameter_declaration_clause (cp_parser* parser)
14636 {
14637   tree parameters;
14638   cp_token *token;
14639   bool ellipsis_p;
14640   bool is_error;
14641
14642   /* Peek at the next token.  */
14643   token = cp_lexer_peek_token (parser->lexer);
14644   /* Check for trivial parameter-declaration-clauses.  */
14645   if (token->type == CPP_ELLIPSIS)
14646     {
14647       /* Consume the `...' token.  */
14648       cp_lexer_consume_token (parser->lexer);
14649       return NULL_TREE;
14650     }
14651   else if (token->type == CPP_CLOSE_PAREN)
14652     /* There are no parameters.  */
14653     {
14654 #ifndef NO_IMPLICIT_EXTERN_C
14655       if (in_system_header && current_class_type == NULL
14656           && current_lang_name == lang_name_c)
14657         return NULL_TREE;
14658       else
14659 #endif
14660         return void_list_node;
14661     }
14662   /* Check for `(void)', too, which is a special case.  */
14663   else if (token->keyword == RID_VOID
14664            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14665                == CPP_CLOSE_PAREN))
14666     {
14667       /* Consume the `void' token.  */
14668       cp_lexer_consume_token (parser->lexer);
14669       /* There are no parameters.  */
14670       return void_list_node;
14671     }
14672
14673   /* Parse the parameter-declaration-list.  */
14674   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14675   /* If a parse error occurred while parsing the
14676      parameter-declaration-list, then the entire
14677      parameter-declaration-clause is erroneous.  */
14678   if (is_error)
14679     return NULL;
14680
14681   /* Peek at the next token.  */
14682   token = cp_lexer_peek_token (parser->lexer);
14683   /* If it's a `,', the clause should terminate with an ellipsis.  */
14684   if (token->type == CPP_COMMA)
14685     {
14686       /* Consume the `,'.  */
14687       cp_lexer_consume_token (parser->lexer);
14688       /* Expect an ellipsis.  */
14689       ellipsis_p
14690         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14691     }
14692   /* It might also be `...' if the optional trailing `,' was
14693      omitted.  */
14694   else if (token->type == CPP_ELLIPSIS)
14695     {
14696       /* Consume the `...' token.  */
14697       cp_lexer_consume_token (parser->lexer);
14698       /* And remember that we saw it.  */
14699       ellipsis_p = true;
14700     }
14701   else
14702     ellipsis_p = false;
14703
14704   /* Finish the parameter list.  */
14705   if (!ellipsis_p)
14706     parameters = chainon (parameters, void_list_node);
14707
14708   return parameters;
14709 }
14710
14711 /* Parse a parameter-declaration-list.
14712
14713    parameter-declaration-list:
14714      parameter-declaration
14715      parameter-declaration-list , parameter-declaration
14716
14717    Returns a representation of the parameter-declaration-list, as for
14718    cp_parser_parameter_declaration_clause.  However, the
14719    `void_list_node' is never appended to the list.  Upon return,
14720    *IS_ERROR will be true iff an error occurred.  */
14721
14722 static tree
14723 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14724 {
14725   tree parameters = NULL_TREE;
14726   tree *tail = &parameters; 
14727   bool saved_in_unbraced_linkage_specification_p;
14728   int index = 0;
14729
14730   /* Assume all will go well.  */
14731   *is_error = false;
14732   /* The special considerations that apply to a function within an
14733      unbraced linkage specifications do not apply to the parameters
14734      to the function.  */
14735   saved_in_unbraced_linkage_specification_p 
14736     = parser->in_unbraced_linkage_specification_p;
14737   parser->in_unbraced_linkage_specification_p = false;
14738
14739   /* Look for more parameters.  */
14740   while (true)
14741     {
14742       cp_parameter_declarator *parameter;
14743       tree decl = error_mark_node;
14744       bool parenthesized_p;
14745       /* Parse the parameter.  */
14746       parameter
14747         = cp_parser_parameter_declaration (parser,
14748                                            /*template_parm_p=*/false,
14749                                            &parenthesized_p);
14750
14751       /* We don't know yet if the enclosing context is deprecated, so wait
14752          and warn in grokparms if appropriate.  */
14753       deprecated_state = DEPRECATED_SUPPRESS;
14754
14755       if (parameter)
14756         decl = grokdeclarator (parameter->declarator,
14757                                &parameter->decl_specifiers,
14758                                PARM,
14759                                parameter->default_argument != NULL_TREE,
14760                                &parameter->decl_specifiers.attributes);
14761
14762       deprecated_state = DEPRECATED_NORMAL;
14763
14764       /* If a parse error occurred parsing the parameter declaration,
14765          then the entire parameter-declaration-list is erroneous.  */
14766       if (decl == error_mark_node)
14767         {
14768           *is_error = true;
14769           parameters = error_mark_node;
14770           break;
14771         }
14772
14773       if (parameter->decl_specifiers.attributes)
14774         cplus_decl_attributes (&decl,
14775                                parameter->decl_specifiers.attributes,
14776                                0);
14777       if (DECL_NAME (decl))
14778         decl = pushdecl (decl);
14779
14780       if (decl != error_mark_node)
14781         {
14782           retrofit_lang_decl (decl);
14783           DECL_PARM_INDEX (decl) = ++index;
14784         }
14785
14786       /* Add the new parameter to the list.  */
14787       *tail = build_tree_list (parameter->default_argument, decl);
14788       tail = &TREE_CHAIN (*tail);
14789
14790       /* Peek at the next token.  */
14791       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14792           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14793           /* These are for Objective-C++ */
14794           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14795           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14796         /* The parameter-declaration-list is complete.  */
14797         break;
14798       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14799         {
14800           cp_token *token;
14801
14802           /* Peek at the next token.  */
14803           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14804           /* If it's an ellipsis, then the list is complete.  */
14805           if (token->type == CPP_ELLIPSIS)
14806             break;
14807           /* Otherwise, there must be more parameters.  Consume the
14808              `,'.  */
14809           cp_lexer_consume_token (parser->lexer);
14810           /* When parsing something like:
14811
14812                 int i(float f, double d)
14813
14814              we can tell after seeing the declaration for "f" that we
14815              are not looking at an initialization of a variable "i",
14816              but rather at the declaration of a function "i".
14817
14818              Due to the fact that the parsing of template arguments
14819              (as specified to a template-id) requires backtracking we
14820              cannot use this technique when inside a template argument
14821              list.  */
14822           if (!parser->in_template_argument_list_p
14823               && !parser->in_type_id_in_expr_p
14824               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14825               /* However, a parameter-declaration of the form
14826                  "foat(f)" (which is a valid declaration of a
14827                  parameter "f") can also be interpreted as an
14828                  expression (the conversion of "f" to "float").  */
14829               && !parenthesized_p)
14830             cp_parser_commit_to_tentative_parse (parser);
14831         }
14832       else
14833         {
14834           cp_parser_error (parser, "expected %<,%> or %<...%>");
14835           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14836             cp_parser_skip_to_closing_parenthesis (parser,
14837                                                    /*recovering=*/true,
14838                                                    /*or_comma=*/false,
14839                                                    /*consume_paren=*/false);
14840           break;
14841         }
14842     }
14843
14844   parser->in_unbraced_linkage_specification_p
14845     = saved_in_unbraced_linkage_specification_p;
14846
14847   return parameters;
14848 }
14849
14850 /* Parse a parameter declaration.
14851
14852    parameter-declaration:
14853      decl-specifier-seq ... [opt] declarator
14854      decl-specifier-seq declarator = assignment-expression
14855      decl-specifier-seq ... [opt] abstract-declarator [opt]
14856      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14857
14858    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14859    declares a template parameter.  (In that case, a non-nested `>'
14860    token encountered during the parsing of the assignment-expression
14861    is not interpreted as a greater-than operator.)
14862
14863    Returns a representation of the parameter, or NULL if an error
14864    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14865    true iff the declarator is of the form "(p)".  */
14866
14867 static cp_parameter_declarator *
14868 cp_parser_parameter_declaration (cp_parser *parser,
14869                                  bool template_parm_p,
14870                                  bool *parenthesized_p)
14871 {
14872   int declares_class_or_enum;
14873   bool greater_than_is_operator_p;
14874   cp_decl_specifier_seq decl_specifiers;
14875   cp_declarator *declarator;
14876   tree default_argument;
14877   cp_token *token = NULL, *declarator_token_start = NULL;
14878   const char *saved_message;
14879
14880   /* In a template parameter, `>' is not an operator.
14881
14882      [temp.param]
14883
14884      When parsing a default template-argument for a non-type
14885      template-parameter, the first non-nested `>' is taken as the end
14886      of the template parameter-list rather than a greater-than
14887      operator.  */
14888   greater_than_is_operator_p = !template_parm_p;
14889
14890   /* Type definitions may not appear in parameter types.  */
14891   saved_message = parser->type_definition_forbidden_message;
14892   parser->type_definition_forbidden_message
14893     = "types may not be defined in parameter types";
14894
14895   /* Parse the declaration-specifiers.  */
14896   cp_parser_decl_specifier_seq (parser,
14897                                 CP_PARSER_FLAGS_NONE,
14898                                 &decl_specifiers,
14899                                 &declares_class_or_enum);
14900   /* If an error occurred, there's no reason to attempt to parse the
14901      rest of the declaration.  */
14902   if (cp_parser_error_occurred (parser))
14903     {
14904       parser->type_definition_forbidden_message = saved_message;
14905       return NULL;
14906     }
14907
14908   /* Peek at the next token.  */
14909   token = cp_lexer_peek_token (parser->lexer);
14910
14911   /* If the next token is a `)', `,', `=', `>', or `...', then there
14912      is no declarator. However, when variadic templates are enabled,
14913      there may be a declarator following `...'.  */
14914   if (token->type == CPP_CLOSE_PAREN
14915       || token->type == CPP_COMMA
14916       || token->type == CPP_EQ
14917       || token->type == CPP_GREATER)
14918     {
14919       declarator = NULL;
14920       if (parenthesized_p)
14921         *parenthesized_p = false;
14922     }
14923   /* Otherwise, there should be a declarator.  */
14924   else
14925     {
14926       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14927       parser->default_arg_ok_p = false;
14928
14929       /* After seeing a decl-specifier-seq, if the next token is not a
14930          "(", there is no possibility that the code is a valid
14931          expression.  Therefore, if parsing tentatively, we commit at
14932          this point.  */
14933       if (!parser->in_template_argument_list_p
14934           /* In an expression context, having seen:
14935
14936                (int((char ...
14937
14938              we cannot be sure whether we are looking at a
14939              function-type (taking a "char" as a parameter) or a cast
14940              of some object of type "char" to "int".  */
14941           && !parser->in_type_id_in_expr_p
14942           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14943           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14944         cp_parser_commit_to_tentative_parse (parser);
14945       /* Parse the declarator.  */
14946       declarator_token_start = token;
14947       declarator = cp_parser_declarator (parser,
14948                                          CP_PARSER_DECLARATOR_EITHER,
14949                                          /*ctor_dtor_or_conv_p=*/NULL,
14950                                          parenthesized_p,
14951                                          /*member_p=*/false);
14952       parser->default_arg_ok_p = saved_default_arg_ok_p;
14953       /* After the declarator, allow more attributes.  */
14954       decl_specifiers.attributes
14955         = chainon (decl_specifiers.attributes,
14956                    cp_parser_attributes_opt (parser));
14957     }
14958
14959   /* If the next token is an ellipsis, and we have not seen a
14960      declarator name, and the type of the declarator contains parameter
14961      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14962      a parameter pack expansion expression. Otherwise, leave the
14963      ellipsis for a C-style variadic function. */
14964   token = cp_lexer_peek_token (parser->lexer);
14965   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14966     {
14967       tree type = decl_specifiers.type;
14968
14969       if (type && DECL_P (type))
14970         type = TREE_TYPE (type);
14971
14972       if (type
14973           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14974           && declarator_can_be_parameter_pack (declarator)
14975           && (!declarator || !declarator->parameter_pack_p)
14976           && uses_parameter_packs (type))
14977         {
14978           /* Consume the `...'. */
14979           cp_lexer_consume_token (parser->lexer);
14980           maybe_warn_variadic_templates ();
14981           
14982           /* Build a pack expansion type */
14983           if (declarator)
14984             declarator->parameter_pack_p = true;
14985           else
14986             decl_specifiers.type = make_pack_expansion (type);
14987         }
14988     }
14989
14990   /* The restriction on defining new types applies only to the type
14991      of the parameter, not to the default argument.  */
14992   parser->type_definition_forbidden_message = saved_message;
14993
14994   /* If the next token is `=', then process a default argument.  */
14995   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14996     {
14997       /* Consume the `='.  */
14998       cp_lexer_consume_token (parser->lexer);
14999
15000       /* If we are defining a class, then the tokens that make up the
15001          default argument must be saved and processed later.  */
15002       if (!template_parm_p && at_class_scope_p ()
15003           && TYPE_BEING_DEFINED (current_class_type)
15004           && !LAMBDA_TYPE_P (current_class_type))
15005         {
15006           unsigned depth = 0;
15007           int maybe_template_id = 0;
15008           cp_token *first_token;
15009           cp_token *token;
15010
15011           /* Add tokens until we have processed the entire default
15012              argument.  We add the range [first_token, token).  */
15013           first_token = cp_lexer_peek_token (parser->lexer);
15014           while (true)
15015             {
15016               bool done = false;
15017
15018               /* Peek at the next token.  */
15019               token = cp_lexer_peek_token (parser->lexer);
15020               /* What we do depends on what token we have.  */
15021               switch (token->type)
15022                 {
15023                   /* In valid code, a default argument must be
15024                      immediately followed by a `,' `)', or `...'.  */
15025                 case CPP_COMMA:
15026                   if (depth == 0 && maybe_template_id)
15027                     {
15028                       /* If we've seen a '<', we might be in a
15029                          template-argument-list.  Until Core issue 325 is
15030                          resolved, we don't know how this situation ought
15031                          to be handled, so try to DTRT.  We check whether
15032                          what comes after the comma is a valid parameter
15033                          declaration list.  If it is, then the comma ends
15034                          the default argument; otherwise the default
15035                          argument continues.  */
15036                       bool error = false;
15037
15038                       /* Set ITALP so cp_parser_parameter_declaration_list
15039                          doesn't decide to commit to this parse.  */
15040                       bool saved_italp = parser->in_template_argument_list_p;
15041                       parser->in_template_argument_list_p = true;
15042
15043                       cp_parser_parse_tentatively (parser);
15044                       cp_lexer_consume_token (parser->lexer);
15045                       cp_parser_parameter_declaration_list (parser, &error);
15046                       if (!cp_parser_error_occurred (parser) && !error)
15047                         done = true;
15048                       cp_parser_abort_tentative_parse (parser);
15049
15050                       parser->in_template_argument_list_p = saved_italp;
15051                       break;
15052                     }
15053                 case CPP_CLOSE_PAREN:
15054                 case CPP_ELLIPSIS:
15055                   /* If we run into a non-nested `;', `}', or `]',
15056                      then the code is invalid -- but the default
15057                      argument is certainly over.  */
15058                 case CPP_SEMICOLON:
15059                 case CPP_CLOSE_BRACE:
15060                 case CPP_CLOSE_SQUARE:
15061                   if (depth == 0)
15062                     done = true;
15063                   /* Update DEPTH, if necessary.  */
15064                   else if (token->type == CPP_CLOSE_PAREN
15065                            || token->type == CPP_CLOSE_BRACE
15066                            || token->type == CPP_CLOSE_SQUARE)
15067                     --depth;
15068                   break;
15069
15070                 case CPP_OPEN_PAREN:
15071                 case CPP_OPEN_SQUARE:
15072                 case CPP_OPEN_BRACE:
15073                   ++depth;
15074                   break;
15075
15076                 case CPP_LESS:
15077                   if (depth == 0)
15078                     /* This might be the comparison operator, or it might
15079                        start a template argument list.  */
15080                     ++maybe_template_id;
15081                   break;
15082
15083                 case CPP_RSHIFT:
15084                   if (cxx_dialect == cxx98)
15085                     break;
15086                   /* Fall through for C++0x, which treats the `>>'
15087                      operator like two `>' tokens in certain
15088                      cases.  */
15089
15090                 case CPP_GREATER:
15091                   if (depth == 0)
15092                     {
15093                       /* This might be an operator, or it might close a
15094                          template argument list.  But if a previous '<'
15095                          started a template argument list, this will have
15096                          closed it, so we can't be in one anymore.  */
15097                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15098                       if (maybe_template_id < 0)
15099                         maybe_template_id = 0;
15100                     }
15101                   break;
15102
15103                   /* If we run out of tokens, issue an error message.  */
15104                 case CPP_EOF:
15105                 case CPP_PRAGMA_EOL:
15106                   error_at (token->location, "file ends in default argument");
15107                   done = true;
15108                   break;
15109
15110                 case CPP_NAME:
15111                 case CPP_SCOPE:
15112                   /* In these cases, we should look for template-ids.
15113                      For example, if the default argument is
15114                      `X<int, double>()', we need to do name lookup to
15115                      figure out whether or not `X' is a template; if
15116                      so, the `,' does not end the default argument.
15117
15118                      That is not yet done.  */
15119                   break;
15120
15121                 default:
15122                   break;
15123                 }
15124
15125               /* If we've reached the end, stop.  */
15126               if (done)
15127                 break;
15128
15129               /* Add the token to the token block.  */
15130               token = cp_lexer_consume_token (parser->lexer);
15131             }
15132
15133           /* Create a DEFAULT_ARG to represent the unparsed default
15134              argument.  */
15135           default_argument = make_node (DEFAULT_ARG);
15136           DEFARG_TOKENS (default_argument)
15137             = cp_token_cache_new (first_token, token);
15138           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15139         }
15140       /* Outside of a class definition, we can just parse the
15141          assignment-expression.  */
15142       else
15143         {
15144           token = cp_lexer_peek_token (parser->lexer);
15145           default_argument 
15146             = cp_parser_default_argument (parser, template_parm_p);
15147         }
15148
15149       if (!parser->default_arg_ok_p)
15150         {
15151           if (flag_permissive)
15152             warning (0, "deprecated use of default argument for parameter of non-function");
15153           else
15154             {
15155               error_at (token->location,
15156                         "default arguments are only "
15157                         "permitted for function parameters");
15158               default_argument = NULL_TREE;
15159             }
15160         }
15161       else if ((declarator && declarator->parameter_pack_p)
15162                || (decl_specifiers.type
15163                    && PACK_EXPANSION_P (decl_specifiers.type)))
15164         {
15165           /* Find the name of the parameter pack.  */     
15166           cp_declarator *id_declarator = declarator;
15167           while (id_declarator && id_declarator->kind != cdk_id)
15168             id_declarator = id_declarator->declarator;
15169           
15170           if (id_declarator && id_declarator->kind == cdk_id)
15171             error_at (declarator_token_start->location,
15172                       template_parm_p 
15173                       ? "template parameter pack %qD"
15174                       " cannot have a default argument"
15175                       : "parameter pack %qD cannot have a default argument",
15176                       id_declarator->u.id.unqualified_name);
15177           else
15178             error_at (declarator_token_start->location,
15179                       template_parm_p 
15180                       ? "template parameter pack cannot have a default argument"
15181                       : "parameter pack cannot have a default argument");
15182           
15183           default_argument = NULL_TREE;
15184         }
15185     }
15186   else
15187     default_argument = NULL_TREE;
15188
15189   return make_parameter_declarator (&decl_specifiers,
15190                                     declarator,
15191                                     default_argument);
15192 }
15193
15194 /* Parse a default argument and return it.
15195
15196    TEMPLATE_PARM_P is true if this is a default argument for a
15197    non-type template parameter.  */
15198 static tree
15199 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15200 {
15201   tree default_argument = NULL_TREE;
15202   bool saved_greater_than_is_operator_p;
15203   bool saved_local_variables_forbidden_p;
15204
15205   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15206      set correctly.  */
15207   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15208   parser->greater_than_is_operator_p = !template_parm_p;
15209   /* Local variable names (and the `this' keyword) may not
15210      appear in a default argument.  */
15211   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15212   parser->local_variables_forbidden_p = true;
15213   /* Parse the assignment-expression.  */
15214   if (template_parm_p)
15215     push_deferring_access_checks (dk_no_deferred);
15216   default_argument
15217     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15218   if (template_parm_p)
15219     pop_deferring_access_checks ();
15220   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15221   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15222
15223   return default_argument;
15224 }
15225
15226 /* Parse a function-body.
15227
15228    function-body:
15229      compound_statement  */
15230
15231 static void
15232 cp_parser_function_body (cp_parser *parser)
15233 {
15234   cp_parser_compound_statement (parser, NULL, false);
15235 }
15236
15237 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15238    true if a ctor-initializer was present.  */
15239
15240 static bool
15241 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15242 {
15243   tree body;
15244   bool ctor_initializer_p;
15245
15246   /* Begin the function body.  */
15247   body = begin_function_body ();
15248   /* Parse the optional ctor-initializer.  */
15249   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15250   /* Parse the function-body.  */
15251   cp_parser_function_body (parser);
15252   /* Finish the function body.  */
15253   finish_function_body (body);
15254
15255   return ctor_initializer_p;
15256 }
15257
15258 /* Parse an initializer.
15259
15260    initializer:
15261      = initializer-clause
15262      ( expression-list )
15263
15264    Returns an expression representing the initializer.  If no
15265    initializer is present, NULL_TREE is returned.
15266
15267    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15268    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15269    set to TRUE if there is no initializer present.  If there is an
15270    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15271    is set to true; otherwise it is set to false.  */
15272
15273 static tree
15274 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15275                        bool* non_constant_p)
15276 {
15277   cp_token *token;
15278   tree init;
15279
15280   /* Peek at the next token.  */
15281   token = cp_lexer_peek_token (parser->lexer);
15282
15283   /* Let our caller know whether or not this initializer was
15284      parenthesized.  */
15285   *is_direct_init = (token->type != CPP_EQ);
15286   /* Assume that the initializer is constant.  */
15287   *non_constant_p = false;
15288
15289   if (token->type == CPP_EQ)
15290     {
15291       /* Consume the `='.  */
15292       cp_lexer_consume_token (parser->lexer);
15293       /* Parse the initializer-clause.  */
15294       init = cp_parser_initializer_clause (parser, non_constant_p);
15295     }
15296   else if (token->type == CPP_OPEN_PAREN)
15297     {
15298       VEC(tree,gc) *vec;
15299       vec = cp_parser_parenthesized_expression_list (parser, false,
15300                                                      /*cast_p=*/false,
15301                                                      /*allow_expansion_p=*/true,
15302                                                      non_constant_p);
15303       if (vec == NULL)
15304         return error_mark_node;
15305       init = build_tree_list_vec (vec);
15306       release_tree_vector (vec);
15307     }
15308   else if (token->type == CPP_OPEN_BRACE)
15309     {
15310       maybe_warn_cpp0x ("extended initializer lists");
15311       init = cp_parser_braced_list (parser, non_constant_p);
15312       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15313     }
15314   else
15315     {
15316       /* Anything else is an error.  */
15317       cp_parser_error (parser, "expected initializer");
15318       init = error_mark_node;
15319     }
15320
15321   return init;
15322 }
15323
15324 /* Parse an initializer-clause.
15325
15326    initializer-clause:
15327      assignment-expression
15328      braced-init-list
15329
15330    Returns an expression representing the initializer.
15331
15332    If the `assignment-expression' production is used the value
15333    returned is simply a representation for the expression.
15334
15335    Otherwise, calls cp_parser_braced_list.  */
15336
15337 static tree
15338 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15339 {
15340   tree initializer;
15341
15342   /* Assume the expression is constant.  */
15343   *non_constant_p = false;
15344
15345   /* If it is not a `{', then we are looking at an
15346      assignment-expression.  */
15347   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15348     {
15349       initializer
15350         = cp_parser_constant_expression (parser,
15351                                         /*allow_non_constant_p=*/true,
15352                                         non_constant_p);
15353       if (!*non_constant_p)
15354         initializer = fold_non_dependent_expr (initializer);
15355     }
15356   else
15357     initializer = cp_parser_braced_list (parser, non_constant_p);
15358
15359   return initializer;
15360 }
15361
15362 /* Parse a brace-enclosed initializer list.
15363
15364    braced-init-list:
15365      { initializer-list , [opt] }
15366      { }
15367
15368    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15369    the elements of the initializer-list (or NULL, if the last
15370    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15371    NULL_TREE.  There is no way to detect whether or not the optional
15372    trailing `,' was provided.  NON_CONSTANT_P is as for
15373    cp_parser_initializer.  */     
15374
15375 static tree
15376 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15377 {
15378   tree initializer;
15379
15380   /* Consume the `{' token.  */
15381   cp_lexer_consume_token (parser->lexer);
15382   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15383   initializer = make_node (CONSTRUCTOR);
15384   /* If it's not a `}', then there is a non-trivial initializer.  */
15385   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15386     {
15387       /* Parse the initializer list.  */
15388       CONSTRUCTOR_ELTS (initializer)
15389         = cp_parser_initializer_list (parser, non_constant_p);
15390       /* A trailing `,' token is allowed.  */
15391       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15392         cp_lexer_consume_token (parser->lexer);
15393     }
15394   /* Now, there should be a trailing `}'.  */
15395   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15396   TREE_TYPE (initializer) = init_list_type_node;
15397   return initializer;
15398 }
15399
15400 /* Parse an initializer-list.
15401
15402    initializer-list:
15403      initializer-clause ... [opt]
15404      initializer-list , initializer-clause ... [opt]
15405
15406    GNU Extension:
15407
15408    initializer-list:
15409      identifier : initializer-clause
15410      initializer-list, identifier : initializer-clause
15411
15412    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15413    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15414    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15415    as for cp_parser_initializer.  */
15416
15417 static VEC(constructor_elt,gc) *
15418 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15419 {
15420   VEC(constructor_elt,gc) *v = NULL;
15421
15422   /* Assume all of the expressions are constant.  */
15423   *non_constant_p = false;
15424
15425   /* Parse the rest of the list.  */
15426   while (true)
15427     {
15428       cp_token *token;
15429       tree identifier;
15430       tree initializer;
15431       bool clause_non_constant_p;
15432
15433       /* If the next token is an identifier and the following one is a
15434          colon, we are looking at the GNU designated-initializer
15435          syntax.  */
15436       if (cp_parser_allow_gnu_extensions_p (parser)
15437           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15438           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15439         {
15440           /* Warn the user that they are using an extension.  */
15441           pedwarn (input_location, OPT_pedantic, 
15442                    "ISO C++ does not allow designated initializers");
15443           /* Consume the identifier.  */
15444           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15445           /* Consume the `:'.  */
15446           cp_lexer_consume_token (parser->lexer);
15447         }
15448       else
15449         identifier = NULL_TREE;
15450
15451       /* Parse the initializer.  */
15452       initializer = cp_parser_initializer_clause (parser,
15453                                                   &clause_non_constant_p);
15454       /* If any clause is non-constant, so is the entire initializer.  */
15455       if (clause_non_constant_p)
15456         *non_constant_p = true;
15457
15458       /* If we have an ellipsis, this is an initializer pack
15459          expansion.  */
15460       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15461         {
15462           /* Consume the `...'.  */
15463           cp_lexer_consume_token (parser->lexer);
15464
15465           /* Turn the initializer into an initializer expansion.  */
15466           initializer = make_pack_expansion (initializer);
15467         }
15468
15469       /* Add it to the vector.  */
15470       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15471
15472       /* If the next token is not a comma, we have reached the end of
15473          the list.  */
15474       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15475         break;
15476
15477       /* Peek at the next token.  */
15478       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15479       /* If the next token is a `}', then we're still done.  An
15480          initializer-clause can have a trailing `,' after the
15481          initializer-list and before the closing `}'.  */
15482       if (token->type == CPP_CLOSE_BRACE)
15483         break;
15484
15485       /* Consume the `,' token.  */
15486       cp_lexer_consume_token (parser->lexer);
15487     }
15488
15489   return v;
15490 }
15491
15492 /* Classes [gram.class] */
15493
15494 /* Parse a class-name.
15495
15496    class-name:
15497      identifier
15498      template-id
15499
15500    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15501    to indicate that names looked up in dependent types should be
15502    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15503    keyword has been used to indicate that the name that appears next
15504    is a template.  TAG_TYPE indicates the explicit tag given before
15505    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15506    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15507    is the class being defined in a class-head.
15508
15509    Returns the TYPE_DECL representing the class.  */
15510
15511 static tree
15512 cp_parser_class_name (cp_parser *parser,
15513                       bool typename_keyword_p,
15514                       bool template_keyword_p,
15515                       enum tag_types tag_type,
15516                       bool check_dependency_p,
15517                       bool class_head_p,
15518                       bool is_declaration)
15519 {
15520   tree decl;
15521   tree scope;
15522   bool typename_p;
15523   cp_token *token;
15524   tree identifier = NULL_TREE;
15525
15526   /* All class-names start with an identifier.  */
15527   token = cp_lexer_peek_token (parser->lexer);
15528   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15529     {
15530       cp_parser_error (parser, "expected class-name");
15531       return error_mark_node;
15532     }
15533
15534   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15535      to a template-id, so we save it here.  */
15536   scope = parser->scope;
15537   if (scope == error_mark_node)
15538     return error_mark_node;
15539
15540   /* Any name names a type if we're following the `typename' keyword
15541      in a qualified name where the enclosing scope is type-dependent.  */
15542   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15543                 && dependent_type_p (scope));
15544   /* Handle the common case (an identifier, but not a template-id)
15545      efficiently.  */
15546   if (token->type == CPP_NAME
15547       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15548     {
15549       cp_token *identifier_token;
15550       bool ambiguous_p;
15551
15552       /* Look for the identifier.  */
15553       identifier_token = cp_lexer_peek_token (parser->lexer);
15554       ambiguous_p = identifier_token->ambiguous_p;
15555       identifier = cp_parser_identifier (parser);
15556       /* If the next token isn't an identifier, we are certainly not
15557          looking at a class-name.  */
15558       if (identifier == error_mark_node)
15559         decl = error_mark_node;
15560       /* If we know this is a type-name, there's no need to look it
15561          up.  */
15562       else if (typename_p)
15563         decl = identifier;
15564       else
15565         {
15566           tree ambiguous_decls;
15567           /* If we already know that this lookup is ambiguous, then
15568              we've already issued an error message; there's no reason
15569              to check again.  */
15570           if (ambiguous_p)
15571             {
15572               cp_parser_simulate_error (parser);
15573               return error_mark_node;
15574             }
15575           /* If the next token is a `::', then the name must be a type
15576              name.
15577
15578              [basic.lookup.qual]
15579
15580              During the lookup for a name preceding the :: scope
15581              resolution operator, object, function, and enumerator
15582              names are ignored.  */
15583           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15584             tag_type = typename_type;
15585           /* Look up the name.  */
15586           decl = cp_parser_lookup_name (parser, identifier,
15587                                         tag_type,
15588                                         /*is_template=*/false,
15589                                         /*is_namespace=*/false,
15590                                         check_dependency_p,
15591                                         &ambiguous_decls,
15592                                         identifier_token->location);
15593           if (ambiguous_decls)
15594             {
15595               error_at (identifier_token->location,
15596                         "reference to %qD is ambiguous", identifier);
15597               print_candidates (ambiguous_decls);
15598               if (cp_parser_parsing_tentatively (parser))
15599                 {
15600                   identifier_token->ambiguous_p = true;
15601                   cp_parser_simulate_error (parser);
15602                 }
15603               return error_mark_node;
15604             }
15605         }
15606     }
15607   else
15608     {
15609       /* Try a template-id.  */
15610       decl = cp_parser_template_id (parser, template_keyword_p,
15611                                     check_dependency_p,
15612                                     is_declaration);
15613       if (decl == error_mark_node)
15614         return error_mark_node;
15615     }
15616
15617   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15618
15619   /* If this is a typename, create a TYPENAME_TYPE.  */
15620   if (typename_p && decl != error_mark_node)
15621     {
15622       decl = make_typename_type (scope, decl, typename_type,
15623                                  /*complain=*/tf_error);
15624       if (decl != error_mark_node)
15625         decl = TYPE_NAME (decl);
15626     }
15627
15628   /* Check to see that it is really the name of a class.  */
15629   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15630       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15631       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15632     /* Situations like this:
15633
15634          template <typename T> struct A {
15635            typename T::template X<int>::I i;
15636          };
15637
15638        are problematic.  Is `T::template X<int>' a class-name?  The
15639        standard does not seem to be definitive, but there is no other
15640        valid interpretation of the following `::'.  Therefore, those
15641        names are considered class-names.  */
15642     {
15643       decl = make_typename_type (scope, decl, tag_type, tf_error);
15644       if (decl != error_mark_node)
15645         decl = TYPE_NAME (decl);
15646     }
15647   else if (TREE_CODE (decl) != TYPE_DECL
15648            || TREE_TYPE (decl) == error_mark_node
15649            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15650     decl = error_mark_node;
15651
15652   if (decl == error_mark_node)
15653     cp_parser_error (parser, "expected class-name");
15654   else if (identifier && !parser->scope)
15655     maybe_note_name_used_in_class (identifier, decl);
15656
15657   return decl;
15658 }
15659
15660 /* Parse a class-specifier.
15661
15662    class-specifier:
15663      class-head { member-specification [opt] }
15664
15665    Returns the TREE_TYPE representing the class.  */
15666
15667 static tree
15668 cp_parser_class_specifier (cp_parser* parser)
15669 {
15670   tree type;
15671   tree attributes = NULL_TREE;
15672   bool nested_name_specifier_p;
15673   unsigned saved_num_template_parameter_lists;
15674   bool saved_in_function_body;
15675   bool saved_in_unbraced_linkage_specification_p;
15676   tree old_scope = NULL_TREE;
15677   tree scope = NULL_TREE;
15678   tree bases;
15679
15680   push_deferring_access_checks (dk_no_deferred);
15681
15682   /* Parse the class-head.  */
15683   type = cp_parser_class_head (parser,
15684                                &nested_name_specifier_p,
15685                                &attributes,
15686                                &bases);
15687   /* If the class-head was a semantic disaster, skip the entire body
15688      of the class.  */
15689   if (!type)
15690     {
15691       cp_parser_skip_to_end_of_block_or_statement (parser);
15692       pop_deferring_access_checks ();
15693       return error_mark_node;
15694     }
15695
15696   /* Look for the `{'.  */
15697   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15698     {
15699       pop_deferring_access_checks ();
15700       return error_mark_node;
15701     }
15702
15703   /* Process the base classes. If they're invalid, skip the 
15704      entire class body.  */
15705   if (!xref_basetypes (type, bases))
15706     {
15707       /* Consuming the closing brace yields better error messages
15708          later on.  */
15709       if (cp_parser_skip_to_closing_brace (parser))
15710         cp_lexer_consume_token (parser->lexer);
15711       pop_deferring_access_checks ();
15712       return error_mark_node;
15713     }
15714
15715   /* Issue an error message if type-definitions are forbidden here.  */
15716   cp_parser_check_type_definition (parser);
15717   /* Remember that we are defining one more class.  */
15718   ++parser->num_classes_being_defined;
15719   /* Inside the class, surrounding template-parameter-lists do not
15720      apply.  */
15721   saved_num_template_parameter_lists
15722     = parser->num_template_parameter_lists;
15723   parser->num_template_parameter_lists = 0;
15724   /* We are not in a function body.  */
15725   saved_in_function_body = parser->in_function_body;
15726   parser->in_function_body = false;
15727   /* We are not immediately inside an extern "lang" block.  */
15728   saved_in_unbraced_linkage_specification_p
15729     = parser->in_unbraced_linkage_specification_p;
15730   parser->in_unbraced_linkage_specification_p = false;
15731
15732   /* Start the class.  */
15733   if (nested_name_specifier_p)
15734     {
15735       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15736       old_scope = push_inner_scope (scope);
15737     }
15738   type = begin_class_definition (type, attributes);
15739
15740   if (type == error_mark_node)
15741     /* If the type is erroneous, skip the entire body of the class.  */
15742     cp_parser_skip_to_closing_brace (parser);
15743   else
15744     /* Parse the member-specification.  */
15745     cp_parser_member_specification_opt (parser);
15746
15747   /* Look for the trailing `}'.  */
15748   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15749   /* Look for trailing attributes to apply to this class.  */
15750   if (cp_parser_allow_gnu_extensions_p (parser))
15751     attributes = cp_parser_attributes_opt (parser);
15752   if (type != error_mark_node)
15753     type = finish_struct (type, attributes);
15754   if (nested_name_specifier_p)
15755     pop_inner_scope (old_scope, scope);
15756   /* If this class is not itself within the scope of another class,
15757      then we need to parse the bodies of all of the queued function
15758      definitions.  Note that the queued functions defined in a class
15759      are not always processed immediately following the
15760      class-specifier for that class.  Consider:
15761
15762        struct A {
15763          struct B { void f() { sizeof (A); } };
15764        };
15765
15766      If `f' were processed before the processing of `A' were
15767      completed, there would be no way to compute the size of `A'.
15768      Note that the nesting we are interested in here is lexical --
15769      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15770      for:
15771
15772        struct A { struct B; };
15773        struct A::B { void f() { } };
15774
15775      there is no need to delay the parsing of `A::B::f'.  */
15776   if (--parser->num_classes_being_defined == 0)
15777     {
15778       tree queue_entry;
15779       tree fn;
15780       tree class_type = NULL_TREE;
15781       tree pushed_scope = NULL_TREE;
15782
15783       /* In a first pass, parse default arguments to the functions.
15784          Then, in a second pass, parse the bodies of the functions.
15785          This two-phased approach handles cases like:
15786
15787             struct S {
15788               void f() { g(); }
15789               void g(int i = 3);
15790             };
15791
15792          */
15793       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15794              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15795            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15796            TREE_PURPOSE (parser->unparsed_functions_queues)
15797              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15798         {
15799           fn = TREE_VALUE (queue_entry);
15800           /* If there are default arguments that have not yet been processed,
15801              take care of them now.  */
15802           if (class_type != TREE_PURPOSE (queue_entry))
15803             {
15804               if (pushed_scope)
15805                 pop_scope (pushed_scope);
15806               class_type = TREE_PURPOSE (queue_entry);
15807               pushed_scope = push_scope (class_type);
15808             }
15809           /* Make sure that any template parameters are in scope.  */
15810           maybe_begin_member_template_processing (fn);
15811           /* Parse the default argument expressions.  */
15812           cp_parser_late_parsing_default_args (parser, fn);
15813           /* Remove any template parameters from the symbol table.  */
15814           maybe_end_member_template_processing ();
15815         }
15816       if (pushed_scope)
15817         pop_scope (pushed_scope);
15818       /* Now parse the body of the functions.  */
15819       for (TREE_VALUE (parser->unparsed_functions_queues)
15820              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15821            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15822            TREE_VALUE (parser->unparsed_functions_queues)
15823              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15824         {
15825           /* Figure out which function we need to process.  */
15826           fn = TREE_VALUE (queue_entry);
15827           /* Parse the function.  */
15828           cp_parser_late_parsing_for_member (parser, fn);
15829         }
15830     }
15831
15832   /* Put back any saved access checks.  */
15833   pop_deferring_access_checks ();
15834
15835   /* Restore saved state.  */
15836   parser->in_function_body = saved_in_function_body;
15837   parser->num_template_parameter_lists
15838     = saved_num_template_parameter_lists;
15839   parser->in_unbraced_linkage_specification_p
15840     = saved_in_unbraced_linkage_specification_p;
15841
15842   return type;
15843 }
15844
15845 /* Parse a class-head.
15846
15847    class-head:
15848      class-key identifier [opt] base-clause [opt]
15849      class-key nested-name-specifier identifier base-clause [opt]
15850      class-key nested-name-specifier [opt] template-id
15851        base-clause [opt]
15852
15853    GNU Extensions:
15854      class-key attributes identifier [opt] base-clause [opt]
15855      class-key attributes nested-name-specifier identifier base-clause [opt]
15856      class-key attributes nested-name-specifier [opt] template-id
15857        base-clause [opt]
15858
15859    Upon return BASES is initialized to the list of base classes (or
15860    NULL, if there are none) in the same form returned by
15861    cp_parser_base_clause.
15862
15863    Returns the TYPE of the indicated class.  Sets
15864    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15865    involving a nested-name-specifier was used, and FALSE otherwise.
15866
15867    Returns error_mark_node if this is not a class-head.
15868
15869    Returns NULL_TREE if the class-head is syntactically valid, but
15870    semantically invalid in a way that means we should skip the entire
15871    body of the class.  */
15872
15873 static tree
15874 cp_parser_class_head (cp_parser* parser,
15875                       bool* nested_name_specifier_p,
15876                       tree *attributes_p,
15877                       tree *bases)
15878 {
15879   tree nested_name_specifier;
15880   enum tag_types class_key;
15881   tree id = NULL_TREE;
15882   tree type = NULL_TREE;
15883   tree attributes;
15884   bool template_id_p = false;
15885   bool qualified_p = false;
15886   bool invalid_nested_name_p = false;
15887   bool invalid_explicit_specialization_p = false;
15888   tree pushed_scope = NULL_TREE;
15889   unsigned num_templates;
15890   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15891   /* Assume no nested-name-specifier will be present.  */
15892   *nested_name_specifier_p = false;
15893   /* Assume no template parameter lists will be used in defining the
15894      type.  */
15895   num_templates = 0;
15896
15897   *bases = NULL_TREE;
15898
15899   /* Look for the class-key.  */
15900   class_key = cp_parser_class_key (parser);
15901   if (class_key == none_type)
15902     return error_mark_node;
15903
15904   /* Parse the attributes.  */
15905   attributes = cp_parser_attributes_opt (parser);
15906
15907   /* If the next token is `::', that is invalid -- but sometimes
15908      people do try to write:
15909
15910        struct ::S {};
15911
15912      Handle this gracefully by accepting the extra qualifier, and then
15913      issuing an error about it later if this really is a
15914      class-head.  If it turns out just to be an elaborated type
15915      specifier, remain silent.  */
15916   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15917     qualified_p = true;
15918
15919   push_deferring_access_checks (dk_no_check);
15920
15921   /* Determine the name of the class.  Begin by looking for an
15922      optional nested-name-specifier.  */
15923   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15924   nested_name_specifier
15925     = cp_parser_nested_name_specifier_opt (parser,
15926                                            /*typename_keyword_p=*/false,
15927                                            /*check_dependency_p=*/false,
15928                                            /*type_p=*/false,
15929                                            /*is_declaration=*/false);
15930   /* If there was a nested-name-specifier, then there *must* be an
15931      identifier.  */
15932   if (nested_name_specifier)
15933     {
15934       type_start_token = cp_lexer_peek_token (parser->lexer);
15935       /* Although the grammar says `identifier', it really means
15936          `class-name' or `template-name'.  You are only allowed to
15937          define a class that has already been declared with this
15938          syntax.
15939
15940          The proposed resolution for Core Issue 180 says that wherever
15941          you see `class T::X' you should treat `X' as a type-name.
15942
15943          It is OK to define an inaccessible class; for example:
15944
15945            class A { class B; };
15946            class A::B {};
15947
15948          We do not know if we will see a class-name, or a
15949          template-name.  We look for a class-name first, in case the
15950          class-name is a template-id; if we looked for the
15951          template-name first we would stop after the template-name.  */
15952       cp_parser_parse_tentatively (parser);
15953       type = cp_parser_class_name (parser,
15954                                    /*typename_keyword_p=*/false,
15955                                    /*template_keyword_p=*/false,
15956                                    class_type,
15957                                    /*check_dependency_p=*/false,
15958                                    /*class_head_p=*/true,
15959                                    /*is_declaration=*/false);
15960       /* If that didn't work, ignore the nested-name-specifier.  */
15961       if (!cp_parser_parse_definitely (parser))
15962         {
15963           invalid_nested_name_p = true;
15964           type_start_token = cp_lexer_peek_token (parser->lexer);
15965           id = cp_parser_identifier (parser);
15966           if (id == error_mark_node)
15967             id = NULL_TREE;
15968         }
15969       /* If we could not find a corresponding TYPE, treat this
15970          declaration like an unqualified declaration.  */
15971       if (type == error_mark_node)
15972         nested_name_specifier = NULL_TREE;
15973       /* Otherwise, count the number of templates used in TYPE and its
15974          containing scopes.  */
15975       else
15976         {
15977           tree scope;
15978
15979           for (scope = TREE_TYPE (type);
15980                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15981                scope = (TYPE_P (scope)
15982                         ? TYPE_CONTEXT (scope)
15983                         : DECL_CONTEXT (scope)))
15984             if (TYPE_P (scope)
15985                 && CLASS_TYPE_P (scope)
15986                 && CLASSTYPE_TEMPLATE_INFO (scope)
15987                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15988                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15989               ++num_templates;
15990         }
15991     }
15992   /* Otherwise, the identifier is optional.  */
15993   else
15994     {
15995       /* We don't know whether what comes next is a template-id,
15996          an identifier, or nothing at all.  */
15997       cp_parser_parse_tentatively (parser);
15998       /* Check for a template-id.  */
15999       type_start_token = cp_lexer_peek_token (parser->lexer);
16000       id = cp_parser_template_id (parser,
16001                                   /*template_keyword_p=*/false,
16002                                   /*check_dependency_p=*/true,
16003                                   /*is_declaration=*/true);
16004       /* If that didn't work, it could still be an identifier.  */
16005       if (!cp_parser_parse_definitely (parser))
16006         {
16007           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16008             {
16009               type_start_token = cp_lexer_peek_token (parser->lexer);
16010               id = cp_parser_identifier (parser);
16011             }
16012           else
16013             id = NULL_TREE;
16014         }
16015       else
16016         {
16017           template_id_p = true;
16018           ++num_templates;
16019         }
16020     }
16021
16022   pop_deferring_access_checks ();
16023
16024   if (id)
16025     cp_parser_check_for_invalid_template_id (parser, id,
16026                                              type_start_token->location);
16027
16028   /* If it's not a `:' or a `{' then we can't really be looking at a
16029      class-head, since a class-head only appears as part of a
16030      class-specifier.  We have to detect this situation before calling
16031      xref_tag, since that has irreversible side-effects.  */
16032   if (!cp_parser_next_token_starts_class_definition_p (parser))
16033     {
16034       cp_parser_error (parser, "expected %<{%> or %<:%>");
16035       return error_mark_node;
16036     }
16037
16038   /* At this point, we're going ahead with the class-specifier, even
16039      if some other problem occurs.  */
16040   cp_parser_commit_to_tentative_parse (parser);
16041   /* Issue the error about the overly-qualified name now.  */
16042   if (qualified_p)
16043     {
16044       cp_parser_error (parser,
16045                        "global qualification of class name is invalid");
16046       return error_mark_node;
16047     }
16048   else if (invalid_nested_name_p)
16049     {
16050       cp_parser_error (parser,
16051                        "qualified name does not name a class");
16052       return error_mark_node;
16053     }
16054   else if (nested_name_specifier)
16055     {
16056       tree scope;
16057
16058       /* Reject typedef-names in class heads.  */
16059       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16060         {
16061           error_at (type_start_token->location,
16062                     "invalid class name in declaration of %qD",
16063                     type);
16064           type = NULL_TREE;
16065           goto done;
16066         }
16067
16068       /* Figure out in what scope the declaration is being placed.  */
16069       scope = current_scope ();
16070       /* If that scope does not contain the scope in which the
16071          class was originally declared, the program is invalid.  */
16072       if (scope && !is_ancestor (scope, nested_name_specifier))
16073         {
16074           if (at_namespace_scope_p ())
16075             error_at (type_start_token->location,
16076                       "declaration of %qD in namespace %qD which does not "
16077                       "enclose %qD",
16078                       type, scope, nested_name_specifier);
16079           else
16080             error_at (type_start_token->location,
16081                       "declaration of %qD in %qD which does not enclose %qD",
16082                       type, scope, nested_name_specifier);
16083           type = NULL_TREE;
16084           goto done;
16085         }
16086       /* [dcl.meaning]
16087
16088          A declarator-id shall not be qualified except for the
16089          definition of a ... nested class outside of its class
16090          ... [or] the definition or explicit instantiation of a
16091          class member of a namespace outside of its namespace.  */
16092       if (scope == nested_name_specifier)
16093         {
16094           permerror (nested_name_specifier_token_start->location,
16095                      "extra qualification not allowed");
16096           nested_name_specifier = NULL_TREE;
16097           num_templates = 0;
16098         }
16099     }
16100   /* An explicit-specialization must be preceded by "template <>".  If
16101      it is not, try to recover gracefully.  */
16102   if (at_namespace_scope_p ()
16103       && parser->num_template_parameter_lists == 0
16104       && template_id_p)
16105     {
16106       error_at (type_start_token->location,
16107                 "an explicit specialization must be preceded by %<template <>%>");
16108       invalid_explicit_specialization_p = true;
16109       /* Take the same action that would have been taken by
16110          cp_parser_explicit_specialization.  */
16111       ++parser->num_template_parameter_lists;
16112       begin_specialization ();
16113     }
16114   /* There must be no "return" statements between this point and the
16115      end of this function; set "type "to the correct return value and
16116      use "goto done;" to return.  */
16117   /* Make sure that the right number of template parameters were
16118      present.  */
16119   if (!cp_parser_check_template_parameters (parser, num_templates,
16120                                             type_start_token->location,
16121                                             /*declarator=*/NULL))
16122     {
16123       /* If something went wrong, there is no point in even trying to
16124          process the class-definition.  */
16125       type = NULL_TREE;
16126       goto done;
16127     }
16128
16129   /* Look up the type.  */
16130   if (template_id_p)
16131     {
16132       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16133           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16134               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16135         {
16136           error_at (type_start_token->location,
16137                     "function template %qD redeclared as a class template", id);
16138           type = error_mark_node;
16139         }
16140       else
16141         {
16142           type = TREE_TYPE (id);
16143           type = maybe_process_partial_specialization (type);
16144         }
16145       if (nested_name_specifier)
16146         pushed_scope = push_scope (nested_name_specifier);
16147     }
16148   else if (nested_name_specifier)
16149     {
16150       tree class_type;
16151
16152       /* Given:
16153
16154             template <typename T> struct S { struct T };
16155             template <typename T> struct S<T>::T { };
16156
16157          we will get a TYPENAME_TYPE when processing the definition of
16158          `S::T'.  We need to resolve it to the actual type before we
16159          try to define it.  */
16160       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16161         {
16162           class_type = resolve_typename_type (TREE_TYPE (type),
16163                                               /*only_current_p=*/false);
16164           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16165             type = TYPE_NAME (class_type);
16166           else
16167             {
16168               cp_parser_error (parser, "could not resolve typename type");
16169               type = error_mark_node;
16170             }
16171         }
16172
16173       if (maybe_process_partial_specialization (TREE_TYPE (type))
16174           == error_mark_node)
16175         {
16176           type = NULL_TREE;
16177           goto done;
16178         }
16179
16180       class_type = current_class_type;
16181       /* Enter the scope indicated by the nested-name-specifier.  */
16182       pushed_scope = push_scope (nested_name_specifier);
16183       /* Get the canonical version of this type.  */
16184       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16185       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16186           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16187         {
16188           type = push_template_decl (type);
16189           if (type == error_mark_node)
16190             {
16191               type = NULL_TREE;
16192               goto done;
16193             }
16194         }
16195
16196       type = TREE_TYPE (type);
16197       *nested_name_specifier_p = true;
16198     }
16199   else      /* The name is not a nested name.  */
16200     {
16201       /* If the class was unnamed, create a dummy name.  */
16202       if (!id)
16203         id = make_anon_name ();
16204       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16205                        parser->num_template_parameter_lists);
16206     }
16207
16208   /* Indicate whether this class was declared as a `class' or as a
16209      `struct'.  */
16210   if (TREE_CODE (type) == RECORD_TYPE)
16211     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16212   cp_parser_check_class_key (class_key, type);
16213
16214   /* If this type was already complete, and we see another definition,
16215      that's an error.  */
16216   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16217     {
16218       error_at (type_start_token->location, "redefinition of %q#T",
16219                 type);
16220       error_at (type_start_token->location, "previous definition of %q+#T",
16221                 type);
16222       type = NULL_TREE;
16223       goto done;
16224     }
16225   else if (type == error_mark_node)
16226     type = NULL_TREE;
16227
16228   /* We will have entered the scope containing the class; the names of
16229      base classes should be looked up in that context.  For example:
16230
16231        struct A { struct B {}; struct C; };
16232        struct A::C : B {};
16233
16234      is valid.  */
16235
16236   /* Get the list of base-classes, if there is one.  */
16237   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16238     *bases = cp_parser_base_clause (parser);
16239
16240  done:
16241   /* Leave the scope given by the nested-name-specifier.  We will
16242      enter the class scope itself while processing the members.  */
16243   if (pushed_scope)
16244     pop_scope (pushed_scope);
16245
16246   if (invalid_explicit_specialization_p)
16247     {
16248       end_specialization ();
16249       --parser->num_template_parameter_lists;
16250     }
16251   *attributes_p = attributes;
16252   return type;
16253 }
16254
16255 /* Parse a class-key.
16256
16257    class-key:
16258      class
16259      struct
16260      union
16261
16262    Returns the kind of class-key specified, or none_type to indicate
16263    error.  */
16264
16265 static enum tag_types
16266 cp_parser_class_key (cp_parser* parser)
16267 {
16268   cp_token *token;
16269   enum tag_types tag_type;
16270
16271   /* Look for the class-key.  */
16272   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16273   if (!token)
16274     return none_type;
16275
16276   /* Check to see if the TOKEN is a class-key.  */
16277   tag_type = cp_parser_token_is_class_key (token);
16278   if (!tag_type)
16279     cp_parser_error (parser, "expected class-key");
16280   return tag_type;
16281 }
16282
16283 /* Parse an (optional) member-specification.
16284
16285    member-specification:
16286      member-declaration member-specification [opt]
16287      access-specifier : member-specification [opt]  */
16288
16289 static void
16290 cp_parser_member_specification_opt (cp_parser* parser)
16291 {
16292   while (true)
16293     {
16294       cp_token *token;
16295       enum rid keyword;
16296
16297       /* Peek at the next token.  */
16298       token = cp_lexer_peek_token (parser->lexer);
16299       /* If it's a `}', or EOF then we've seen all the members.  */
16300       if (token->type == CPP_CLOSE_BRACE
16301           || token->type == CPP_EOF
16302           || token->type == CPP_PRAGMA_EOL)
16303         break;
16304
16305       /* See if this token is a keyword.  */
16306       keyword = token->keyword;
16307       switch (keyword)
16308         {
16309         case RID_PUBLIC:
16310         case RID_PROTECTED:
16311         case RID_PRIVATE:
16312           /* Consume the access-specifier.  */
16313           cp_lexer_consume_token (parser->lexer);
16314           /* Remember which access-specifier is active.  */
16315           current_access_specifier = token->u.value;
16316           /* Look for the `:'.  */
16317           cp_parser_require (parser, CPP_COLON, "%<:%>");
16318           break;
16319
16320         default:
16321           /* Accept #pragmas at class scope.  */
16322           if (token->type == CPP_PRAGMA)
16323             {
16324               cp_parser_pragma (parser, pragma_external);
16325               break;
16326             }
16327
16328           /* Otherwise, the next construction must be a
16329              member-declaration.  */
16330           cp_parser_member_declaration (parser);
16331         }
16332     }
16333 }
16334
16335 /* Parse a member-declaration.
16336
16337    member-declaration:
16338      decl-specifier-seq [opt] member-declarator-list [opt] ;
16339      function-definition ; [opt]
16340      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16341      using-declaration
16342      template-declaration
16343
16344    member-declarator-list:
16345      member-declarator
16346      member-declarator-list , member-declarator
16347
16348    member-declarator:
16349      declarator pure-specifier [opt]
16350      declarator constant-initializer [opt]
16351      identifier [opt] : constant-expression
16352
16353    GNU Extensions:
16354
16355    member-declaration:
16356      __extension__ member-declaration
16357
16358    member-declarator:
16359      declarator attributes [opt] pure-specifier [opt]
16360      declarator attributes [opt] constant-initializer [opt]
16361      identifier [opt] attributes [opt] : constant-expression  
16362
16363    C++0x Extensions:
16364
16365    member-declaration:
16366      static_assert-declaration  */
16367
16368 static void
16369 cp_parser_member_declaration (cp_parser* parser)
16370 {
16371   cp_decl_specifier_seq decl_specifiers;
16372   tree prefix_attributes;
16373   tree decl;
16374   int declares_class_or_enum;
16375   bool friend_p;
16376   cp_token *token = NULL;
16377   cp_token *decl_spec_token_start = NULL;
16378   cp_token *initializer_token_start = NULL;
16379   int saved_pedantic;
16380
16381   /* Check for the `__extension__' keyword.  */
16382   if (cp_parser_extension_opt (parser, &saved_pedantic))
16383     {
16384       /* Recurse.  */
16385       cp_parser_member_declaration (parser);
16386       /* Restore the old value of the PEDANTIC flag.  */
16387       pedantic = saved_pedantic;
16388
16389       return;
16390     }
16391
16392   /* Check for a template-declaration.  */
16393   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16394     {
16395       /* An explicit specialization here is an error condition, and we
16396          expect the specialization handler to detect and report this.  */
16397       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16398           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16399         cp_parser_explicit_specialization (parser);
16400       else
16401         cp_parser_template_declaration (parser, /*member_p=*/true);
16402
16403       return;
16404     }
16405
16406   /* Check for a using-declaration.  */
16407   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16408     {
16409       /* Parse the using-declaration.  */
16410       cp_parser_using_declaration (parser,
16411                                    /*access_declaration_p=*/false);
16412       return;
16413     }
16414
16415   /* Check for @defs.  */
16416   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16417     {
16418       tree ivar, member;
16419       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16420       ivar = ivar_chains;
16421       while (ivar)
16422         {
16423           member = ivar;
16424           ivar = TREE_CHAIN (member);
16425           TREE_CHAIN (member) = NULL_TREE;
16426           finish_member_declaration (member);
16427         }
16428       return;
16429     }
16430
16431   /* If the next token is `static_assert' we have a static assertion.  */
16432   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16433     {
16434       cp_parser_static_assert (parser, /*member_p=*/true);
16435       return;
16436     }
16437
16438   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16439     return;
16440
16441   /* Parse the decl-specifier-seq.  */
16442   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16443   cp_parser_decl_specifier_seq (parser,
16444                                 CP_PARSER_FLAGS_OPTIONAL,
16445                                 &decl_specifiers,
16446                                 &declares_class_or_enum);
16447   prefix_attributes = decl_specifiers.attributes;
16448   decl_specifiers.attributes = NULL_TREE;
16449   /* Check for an invalid type-name.  */
16450   if (!decl_specifiers.type
16451       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16452     return;
16453   /* If there is no declarator, then the decl-specifier-seq should
16454      specify a type.  */
16455   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16456     {
16457       /* If there was no decl-specifier-seq, and the next token is a
16458          `;', then we have something like:
16459
16460            struct S { ; };
16461
16462          [class.mem]
16463
16464          Each member-declaration shall declare at least one member
16465          name of the class.  */
16466       if (!decl_specifiers.any_specifiers_p)
16467         {
16468           cp_token *token = cp_lexer_peek_token (parser->lexer);
16469           if (!in_system_header_at (token->location))
16470             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16471         }
16472       else
16473         {
16474           tree type;
16475
16476           /* See if this declaration is a friend.  */
16477           friend_p = cp_parser_friend_p (&decl_specifiers);
16478           /* If there were decl-specifiers, check to see if there was
16479              a class-declaration.  */
16480           type = check_tag_decl (&decl_specifiers);
16481           /* Nested classes have already been added to the class, but
16482              a `friend' needs to be explicitly registered.  */
16483           if (friend_p)
16484             {
16485               /* If the `friend' keyword was present, the friend must
16486                  be introduced with a class-key.  */
16487                if (!declares_class_or_enum)
16488                  error_at (decl_spec_token_start->location,
16489                            "a class-key must be used when declaring a friend");
16490                /* In this case:
16491
16492                     template <typename T> struct A {
16493                       friend struct A<T>::B;
16494                     };
16495
16496                   A<T>::B will be represented by a TYPENAME_TYPE, and
16497                   therefore not recognized by check_tag_decl.  */
16498                if (!type
16499                    && decl_specifiers.type
16500                    && TYPE_P (decl_specifiers.type))
16501                  type = decl_specifiers.type;
16502                if (!type || !TYPE_P (type))
16503                  error_at (decl_spec_token_start->location,
16504                            "friend declaration does not name a class or "
16505                            "function");
16506                else
16507                  make_friend_class (current_class_type, type,
16508                                     /*complain=*/true);
16509             }
16510           /* If there is no TYPE, an error message will already have
16511              been issued.  */
16512           else if (!type || type == error_mark_node)
16513             ;
16514           /* An anonymous aggregate has to be handled specially; such
16515              a declaration really declares a data member (with a
16516              particular type), as opposed to a nested class.  */
16517           else if (ANON_AGGR_TYPE_P (type))
16518             {
16519               /* Remove constructors and such from TYPE, now that we
16520                  know it is an anonymous aggregate.  */
16521               fixup_anonymous_aggr (type);
16522               /* And make the corresponding data member.  */
16523               decl = build_decl (decl_spec_token_start->location,
16524                                  FIELD_DECL, NULL_TREE, type);
16525               /* Add it to the class.  */
16526               finish_member_declaration (decl);
16527             }
16528           else
16529             cp_parser_check_access_in_redeclaration
16530                                               (TYPE_NAME (type),
16531                                                decl_spec_token_start->location);
16532         }
16533     }
16534   else
16535     {
16536       /* See if these declarations will be friends.  */
16537       friend_p = cp_parser_friend_p (&decl_specifiers);
16538
16539       /* Keep going until we hit the `;' at the end of the
16540          declaration.  */
16541       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16542         {
16543           tree attributes = NULL_TREE;
16544           tree first_attribute;
16545
16546           /* Peek at the next token.  */
16547           token = cp_lexer_peek_token (parser->lexer);
16548
16549           /* Check for a bitfield declaration.  */
16550           if (token->type == CPP_COLON
16551               || (token->type == CPP_NAME
16552                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16553                   == CPP_COLON))
16554             {
16555               tree identifier;
16556               tree width;
16557
16558               /* Get the name of the bitfield.  Note that we cannot just
16559                  check TOKEN here because it may have been invalidated by
16560                  the call to cp_lexer_peek_nth_token above.  */
16561               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16562                 identifier = cp_parser_identifier (parser);
16563               else
16564                 identifier = NULL_TREE;
16565
16566               /* Consume the `:' token.  */
16567               cp_lexer_consume_token (parser->lexer);
16568               /* Get the width of the bitfield.  */
16569               width
16570                 = cp_parser_constant_expression (parser,
16571                                                  /*allow_non_constant=*/false,
16572                                                  NULL);
16573
16574               /* Look for attributes that apply to the bitfield.  */
16575               attributes = cp_parser_attributes_opt (parser);
16576               /* Remember which attributes are prefix attributes and
16577                  which are not.  */
16578               first_attribute = attributes;
16579               /* Combine the attributes.  */
16580               attributes = chainon (prefix_attributes, attributes);
16581
16582               /* Create the bitfield declaration.  */
16583               decl = grokbitfield (identifier
16584                                    ? make_id_declarator (NULL_TREE,
16585                                                          identifier,
16586                                                          sfk_none)
16587                                    : NULL,
16588                                    &decl_specifiers,
16589                                    width,
16590                                    attributes);
16591             }
16592           else
16593             {
16594               cp_declarator *declarator;
16595               tree initializer;
16596               tree asm_specification;
16597               int ctor_dtor_or_conv_p;
16598
16599               /* Parse the declarator.  */
16600               declarator
16601                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16602                                         &ctor_dtor_or_conv_p,
16603                                         /*parenthesized_p=*/NULL,
16604                                         /*member_p=*/true);
16605
16606               /* If something went wrong parsing the declarator, make sure
16607                  that we at least consume some tokens.  */
16608               if (declarator == cp_error_declarator)
16609                 {
16610                   /* Skip to the end of the statement.  */
16611                   cp_parser_skip_to_end_of_statement (parser);
16612                   /* If the next token is not a semicolon, that is
16613                      probably because we just skipped over the body of
16614                      a function.  So, we consume a semicolon if
16615                      present, but do not issue an error message if it
16616                      is not present.  */
16617                   if (cp_lexer_next_token_is (parser->lexer,
16618                                               CPP_SEMICOLON))
16619                     cp_lexer_consume_token (parser->lexer);
16620                   return;
16621                 }
16622
16623               if (declares_class_or_enum & 2)
16624                 cp_parser_check_for_definition_in_return_type
16625                                             (declarator, decl_specifiers.type,
16626                                              decl_specifiers.type_location);
16627
16628               /* Look for an asm-specification.  */
16629               asm_specification = cp_parser_asm_specification_opt (parser);
16630               /* Look for attributes that apply to the declaration.  */
16631               attributes = cp_parser_attributes_opt (parser);
16632               /* Remember which attributes are prefix attributes and
16633                  which are not.  */
16634               first_attribute = attributes;
16635               /* Combine the attributes.  */
16636               attributes = chainon (prefix_attributes, attributes);
16637
16638               /* If it's an `=', then we have a constant-initializer or a
16639                  pure-specifier.  It is not correct to parse the
16640                  initializer before registering the member declaration
16641                  since the member declaration should be in scope while
16642                  its initializer is processed.  However, the rest of the
16643                  front end does not yet provide an interface that allows
16644                  us to handle this correctly.  */
16645               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16646                 {
16647                   /* In [class.mem]:
16648
16649                      A pure-specifier shall be used only in the declaration of
16650                      a virtual function.
16651
16652                      A member-declarator can contain a constant-initializer
16653                      only if it declares a static member of integral or
16654                      enumeration type.
16655
16656                      Therefore, if the DECLARATOR is for a function, we look
16657                      for a pure-specifier; otherwise, we look for a
16658                      constant-initializer.  When we call `grokfield', it will
16659                      perform more stringent semantics checks.  */
16660                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16661                   if (function_declarator_p (declarator))
16662                     initializer = cp_parser_pure_specifier (parser);
16663                   else
16664                     /* Parse the initializer.  */
16665                     initializer = cp_parser_constant_initializer (parser);
16666                 }
16667               /* Otherwise, there is no initializer.  */
16668               else
16669                 initializer = NULL_TREE;
16670
16671               /* See if we are probably looking at a function
16672                  definition.  We are certainly not looking at a
16673                  member-declarator.  Calling `grokfield' has
16674                  side-effects, so we must not do it unless we are sure
16675                  that we are looking at a member-declarator.  */
16676               if (cp_parser_token_starts_function_definition_p
16677                   (cp_lexer_peek_token (parser->lexer)))
16678                 {
16679                   /* The grammar does not allow a pure-specifier to be
16680                      used when a member function is defined.  (It is
16681                      possible that this fact is an oversight in the
16682                      standard, since a pure function may be defined
16683                      outside of the class-specifier.  */
16684                   if (initializer)
16685                     error_at (initializer_token_start->location,
16686                               "pure-specifier on function-definition");
16687                   decl = cp_parser_save_member_function_body (parser,
16688                                                               &decl_specifiers,
16689                                                               declarator,
16690                                                               attributes);
16691                   /* If the member was not a friend, declare it here.  */
16692                   if (!friend_p)
16693                     finish_member_declaration (decl);
16694                   /* Peek at the next token.  */
16695                   token = cp_lexer_peek_token (parser->lexer);
16696                   /* If the next token is a semicolon, consume it.  */
16697                   if (token->type == CPP_SEMICOLON)
16698                     cp_lexer_consume_token (parser->lexer);
16699                   return;
16700                 }
16701               else
16702                 if (declarator->kind == cdk_function)
16703                   declarator->id_loc = token->location;
16704                 /* Create the declaration.  */
16705                 decl = grokfield (declarator, &decl_specifiers,
16706                                   initializer, /*init_const_expr_p=*/true,
16707                                   asm_specification,
16708                                   attributes);
16709             }
16710
16711           /* Reset PREFIX_ATTRIBUTES.  */
16712           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16713             attributes = TREE_CHAIN (attributes);
16714           if (attributes)
16715             TREE_CHAIN (attributes) = NULL_TREE;
16716
16717           /* If there is any qualification still in effect, clear it
16718              now; we will be starting fresh with the next declarator.  */
16719           parser->scope = NULL_TREE;
16720           parser->qualifying_scope = NULL_TREE;
16721           parser->object_scope = NULL_TREE;
16722           /* If it's a `,', then there are more declarators.  */
16723           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16724             cp_lexer_consume_token (parser->lexer);
16725           /* If the next token isn't a `;', then we have a parse error.  */
16726           else if (cp_lexer_next_token_is_not (parser->lexer,
16727                                                CPP_SEMICOLON))
16728             {
16729               cp_parser_error (parser, "expected %<;%>");
16730               /* Skip tokens until we find a `;'.  */
16731               cp_parser_skip_to_end_of_statement (parser);
16732
16733               break;
16734             }
16735
16736           if (decl)
16737             {
16738               /* Add DECL to the list of members.  */
16739               if (!friend_p)
16740                 finish_member_declaration (decl);
16741
16742               if (TREE_CODE (decl) == FUNCTION_DECL)
16743                 cp_parser_save_default_args (parser, decl);
16744             }
16745         }
16746     }
16747
16748   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16749 }
16750
16751 /* Parse a pure-specifier.
16752
16753    pure-specifier:
16754      = 0
16755
16756    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16757    Otherwise, ERROR_MARK_NODE is returned.  */
16758
16759 static tree
16760 cp_parser_pure_specifier (cp_parser* parser)
16761 {
16762   cp_token *token;
16763
16764   /* Look for the `=' token.  */
16765   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16766     return error_mark_node;
16767   /* Look for the `0' token.  */
16768   token = cp_lexer_peek_token (parser->lexer);
16769
16770   if (token->type == CPP_EOF
16771       || token->type == CPP_PRAGMA_EOL)
16772     return error_mark_node;
16773
16774   cp_lexer_consume_token (parser->lexer);
16775
16776   /* Accept = default or = delete in c++0x mode.  */
16777   if (token->keyword == RID_DEFAULT
16778       || token->keyword == RID_DELETE)
16779     {
16780       maybe_warn_cpp0x ("defaulted and deleted functions");
16781       return token->u.value;
16782     }
16783
16784   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16785   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16786     {
16787       cp_parser_error (parser,
16788                        "invalid pure specifier (only %<= 0%> is allowed)");
16789       cp_parser_skip_to_end_of_statement (parser);
16790       return error_mark_node;
16791     }
16792   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16793     {
16794       error_at (token->location, "templates may not be %<virtual%>");
16795       return error_mark_node;
16796     }
16797
16798   return integer_zero_node;
16799 }
16800
16801 /* Parse a constant-initializer.
16802
16803    constant-initializer:
16804      = constant-expression
16805
16806    Returns a representation of the constant-expression.  */
16807
16808 static tree
16809 cp_parser_constant_initializer (cp_parser* parser)
16810 {
16811   /* Look for the `=' token.  */
16812   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16813     return error_mark_node;
16814
16815   /* It is invalid to write:
16816
16817        struct S { static const int i = { 7 }; };
16818
16819      */
16820   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16821     {
16822       cp_parser_error (parser,
16823                        "a brace-enclosed initializer is not allowed here");
16824       /* Consume the opening brace.  */
16825       cp_lexer_consume_token (parser->lexer);
16826       /* Skip the initializer.  */
16827       cp_parser_skip_to_closing_brace (parser);
16828       /* Look for the trailing `}'.  */
16829       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16830
16831       return error_mark_node;
16832     }
16833
16834   return cp_parser_constant_expression (parser,
16835                                         /*allow_non_constant=*/false,
16836                                         NULL);
16837 }
16838
16839 /* Derived classes [gram.class.derived] */
16840
16841 /* Parse a base-clause.
16842
16843    base-clause:
16844      : base-specifier-list
16845
16846    base-specifier-list:
16847      base-specifier ... [opt]
16848      base-specifier-list , base-specifier ... [opt]
16849
16850    Returns a TREE_LIST representing the base-classes, in the order in
16851    which they were declared.  The representation of each node is as
16852    described by cp_parser_base_specifier.
16853
16854    In the case that no bases are specified, this function will return
16855    NULL_TREE, not ERROR_MARK_NODE.  */
16856
16857 static tree
16858 cp_parser_base_clause (cp_parser* parser)
16859 {
16860   tree bases = NULL_TREE;
16861
16862   /* Look for the `:' that begins the list.  */
16863   cp_parser_require (parser, CPP_COLON, "%<:%>");
16864
16865   /* Scan the base-specifier-list.  */
16866   while (true)
16867     {
16868       cp_token *token;
16869       tree base;
16870       bool pack_expansion_p = false;
16871
16872       /* Look for the base-specifier.  */
16873       base = cp_parser_base_specifier (parser);
16874       /* Look for the (optional) ellipsis. */
16875       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16876         {
16877           /* Consume the `...'. */
16878           cp_lexer_consume_token (parser->lexer);
16879
16880           pack_expansion_p = true;
16881         }
16882
16883       /* Add BASE to the front of the list.  */
16884       if (base != error_mark_node)
16885         {
16886           if (pack_expansion_p)
16887             /* Make this a pack expansion type. */
16888             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16889           
16890
16891           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16892             {
16893               TREE_CHAIN (base) = bases;
16894               bases = base;
16895             }
16896         }
16897       /* Peek at the next token.  */
16898       token = cp_lexer_peek_token (parser->lexer);
16899       /* If it's not a comma, then the list is complete.  */
16900       if (token->type != CPP_COMMA)
16901         break;
16902       /* Consume the `,'.  */
16903       cp_lexer_consume_token (parser->lexer);
16904     }
16905
16906   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16907      base class had a qualified name.  However, the next name that
16908      appears is certainly not qualified.  */
16909   parser->scope = NULL_TREE;
16910   parser->qualifying_scope = NULL_TREE;
16911   parser->object_scope = NULL_TREE;
16912
16913   return nreverse (bases);
16914 }
16915
16916 /* Parse a base-specifier.
16917
16918    base-specifier:
16919      :: [opt] nested-name-specifier [opt] class-name
16920      virtual access-specifier [opt] :: [opt] nested-name-specifier
16921        [opt] class-name
16922      access-specifier virtual [opt] :: [opt] nested-name-specifier
16923        [opt] class-name
16924
16925    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16926    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16927    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16928    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16929
16930 static tree
16931 cp_parser_base_specifier (cp_parser* parser)
16932 {
16933   cp_token *token;
16934   bool done = false;
16935   bool virtual_p = false;
16936   bool duplicate_virtual_error_issued_p = false;
16937   bool duplicate_access_error_issued_p = false;
16938   bool class_scope_p, template_p;
16939   tree access = access_default_node;
16940   tree type;
16941
16942   /* Process the optional `virtual' and `access-specifier'.  */
16943   while (!done)
16944     {
16945       /* Peek at the next token.  */
16946       token = cp_lexer_peek_token (parser->lexer);
16947       /* Process `virtual'.  */
16948       switch (token->keyword)
16949         {
16950         case RID_VIRTUAL:
16951           /* If `virtual' appears more than once, issue an error.  */
16952           if (virtual_p && !duplicate_virtual_error_issued_p)
16953             {
16954               cp_parser_error (parser,
16955                                "%<virtual%> specified more than once in base-specified");
16956               duplicate_virtual_error_issued_p = true;
16957             }
16958
16959           virtual_p = true;
16960
16961           /* Consume the `virtual' token.  */
16962           cp_lexer_consume_token (parser->lexer);
16963
16964           break;
16965
16966         case RID_PUBLIC:
16967         case RID_PROTECTED:
16968         case RID_PRIVATE:
16969           /* If more than one access specifier appears, issue an
16970              error.  */
16971           if (access != access_default_node
16972               && !duplicate_access_error_issued_p)
16973             {
16974               cp_parser_error (parser,
16975                                "more than one access specifier in base-specified");
16976               duplicate_access_error_issued_p = true;
16977             }
16978
16979           access = ridpointers[(int) token->keyword];
16980
16981           /* Consume the access-specifier.  */
16982           cp_lexer_consume_token (parser->lexer);
16983
16984           break;
16985
16986         default:
16987           done = true;
16988           break;
16989         }
16990     }
16991   /* It is not uncommon to see programs mechanically, erroneously, use
16992      the 'typename' keyword to denote (dependent) qualified types
16993      as base classes.  */
16994   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16995     {
16996       token = cp_lexer_peek_token (parser->lexer);
16997       if (!processing_template_decl)
16998         error_at (token->location,
16999                   "keyword %<typename%> not allowed outside of templates");
17000       else
17001         error_at (token->location,
17002                   "keyword %<typename%> not allowed in this context "
17003                   "(the base class is implicitly a type)");
17004       cp_lexer_consume_token (parser->lexer);
17005     }
17006
17007   /* Look for the optional `::' operator.  */
17008   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17009   /* Look for the nested-name-specifier.  The simplest way to
17010      implement:
17011
17012        [temp.res]
17013
17014        The keyword `typename' is not permitted in a base-specifier or
17015        mem-initializer; in these contexts a qualified name that
17016        depends on a template-parameter is implicitly assumed to be a
17017        type name.
17018
17019      is to pretend that we have seen the `typename' keyword at this
17020      point.  */
17021   cp_parser_nested_name_specifier_opt (parser,
17022                                        /*typename_keyword_p=*/true,
17023                                        /*check_dependency_p=*/true,
17024                                        typename_type,
17025                                        /*is_declaration=*/true);
17026   /* If the base class is given by a qualified name, assume that names
17027      we see are type names or templates, as appropriate.  */
17028   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17029   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17030
17031   /* Finally, look for the class-name.  */
17032   type = cp_parser_class_name (parser,
17033                                class_scope_p,
17034                                template_p,
17035                                typename_type,
17036                                /*check_dependency_p=*/true,
17037                                /*class_head_p=*/false,
17038                                /*is_declaration=*/true);
17039
17040   if (type == error_mark_node)
17041     return error_mark_node;
17042
17043   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17044 }
17045
17046 /* Exception handling [gram.exception] */
17047
17048 /* Parse an (optional) exception-specification.
17049
17050    exception-specification:
17051      throw ( type-id-list [opt] )
17052
17053    Returns a TREE_LIST representing the exception-specification.  The
17054    TREE_VALUE of each node is a type.  */
17055
17056 static tree
17057 cp_parser_exception_specification_opt (cp_parser* parser)
17058 {
17059   cp_token *token;
17060   tree type_id_list;
17061
17062   /* Peek at the next token.  */
17063   token = cp_lexer_peek_token (parser->lexer);
17064   /* If it's not `throw', then there's no exception-specification.  */
17065   if (!cp_parser_is_keyword (token, RID_THROW))
17066     return NULL_TREE;
17067
17068   /* Consume the `throw'.  */
17069   cp_lexer_consume_token (parser->lexer);
17070
17071   /* Look for the `('.  */
17072   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17073
17074   /* Peek at the next token.  */
17075   token = cp_lexer_peek_token (parser->lexer);
17076   /* If it's not a `)', then there is a type-id-list.  */
17077   if (token->type != CPP_CLOSE_PAREN)
17078     {
17079       const char *saved_message;
17080
17081       /* Types may not be defined in an exception-specification.  */
17082       saved_message = parser->type_definition_forbidden_message;
17083       parser->type_definition_forbidden_message
17084         = "types may not be defined in an exception-specification";
17085       /* Parse the type-id-list.  */
17086       type_id_list = cp_parser_type_id_list (parser);
17087       /* Restore the saved message.  */
17088       parser->type_definition_forbidden_message = saved_message;
17089     }
17090   else
17091     type_id_list = empty_except_spec;
17092
17093   /* Look for the `)'.  */
17094   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17095
17096   return type_id_list;
17097 }
17098
17099 /* Parse an (optional) type-id-list.
17100
17101    type-id-list:
17102      type-id ... [opt]
17103      type-id-list , type-id ... [opt]
17104
17105    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17106    in the order that the types were presented.  */
17107
17108 static tree
17109 cp_parser_type_id_list (cp_parser* parser)
17110 {
17111   tree types = NULL_TREE;
17112
17113   while (true)
17114     {
17115       cp_token *token;
17116       tree type;
17117
17118       /* Get the next type-id.  */
17119       type = cp_parser_type_id (parser);
17120       /* Parse the optional ellipsis. */
17121       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17122         {
17123           /* Consume the `...'. */
17124           cp_lexer_consume_token (parser->lexer);
17125
17126           /* Turn the type into a pack expansion expression. */
17127           type = make_pack_expansion (type);
17128         }
17129       /* Add it to the list.  */
17130       types = add_exception_specifier (types, type, /*complain=*/1);
17131       /* Peek at the next token.  */
17132       token = cp_lexer_peek_token (parser->lexer);
17133       /* If it is not a `,', we are done.  */
17134       if (token->type != CPP_COMMA)
17135         break;
17136       /* Consume the `,'.  */
17137       cp_lexer_consume_token (parser->lexer);
17138     }
17139
17140   return nreverse (types);
17141 }
17142
17143 /* Parse a try-block.
17144
17145    try-block:
17146      try compound-statement handler-seq  */
17147
17148 static tree
17149 cp_parser_try_block (cp_parser* parser)
17150 {
17151   tree try_block;
17152
17153   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17154   try_block = begin_try_block ();
17155   cp_parser_compound_statement (parser, NULL, true);
17156   finish_try_block (try_block);
17157   cp_parser_handler_seq (parser);
17158   finish_handler_sequence (try_block);
17159
17160   return try_block;
17161 }
17162
17163 /* Parse a function-try-block.
17164
17165    function-try-block:
17166      try ctor-initializer [opt] function-body handler-seq  */
17167
17168 static bool
17169 cp_parser_function_try_block (cp_parser* parser)
17170 {
17171   tree compound_stmt;
17172   tree try_block;
17173   bool ctor_initializer_p;
17174
17175   /* Look for the `try' keyword.  */
17176   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17177     return false;
17178   /* Let the rest of the front end know where we are.  */
17179   try_block = begin_function_try_block (&compound_stmt);
17180   /* Parse the function-body.  */
17181   ctor_initializer_p
17182     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17183   /* We're done with the `try' part.  */
17184   finish_function_try_block (try_block);
17185   /* Parse the handlers.  */
17186   cp_parser_handler_seq (parser);
17187   /* We're done with the handlers.  */
17188   finish_function_handler_sequence (try_block, compound_stmt);
17189
17190   return ctor_initializer_p;
17191 }
17192
17193 /* Parse a handler-seq.
17194
17195    handler-seq:
17196      handler handler-seq [opt]  */
17197
17198 static void
17199 cp_parser_handler_seq (cp_parser* parser)
17200 {
17201   while (true)
17202     {
17203       cp_token *token;
17204
17205       /* Parse the handler.  */
17206       cp_parser_handler (parser);
17207       /* Peek at the next token.  */
17208       token = cp_lexer_peek_token (parser->lexer);
17209       /* If it's not `catch' then there are no more handlers.  */
17210       if (!cp_parser_is_keyword (token, RID_CATCH))
17211         break;
17212     }
17213 }
17214
17215 /* Parse a handler.
17216
17217    handler:
17218      catch ( exception-declaration ) compound-statement  */
17219
17220 static void
17221 cp_parser_handler (cp_parser* parser)
17222 {
17223   tree handler;
17224   tree declaration;
17225
17226   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17227   handler = begin_handler ();
17228   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17229   declaration = cp_parser_exception_declaration (parser);
17230   finish_handler_parms (declaration, handler);
17231   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17232   cp_parser_compound_statement (parser, NULL, false);
17233   finish_handler (handler);
17234 }
17235
17236 /* Parse an exception-declaration.
17237
17238    exception-declaration:
17239      type-specifier-seq declarator
17240      type-specifier-seq abstract-declarator
17241      type-specifier-seq
17242      ...
17243
17244    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17245    ellipsis variant is used.  */
17246
17247 static tree
17248 cp_parser_exception_declaration (cp_parser* parser)
17249 {
17250   cp_decl_specifier_seq type_specifiers;
17251   cp_declarator *declarator;
17252   const char *saved_message;
17253
17254   /* If it's an ellipsis, it's easy to handle.  */
17255   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17256     {
17257       /* Consume the `...' token.  */
17258       cp_lexer_consume_token (parser->lexer);
17259       return NULL_TREE;
17260     }
17261
17262   /* Types may not be defined in exception-declarations.  */
17263   saved_message = parser->type_definition_forbidden_message;
17264   parser->type_definition_forbidden_message
17265     = "types may not be defined in exception-declarations";
17266
17267   /* Parse the type-specifier-seq.  */
17268   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17269                                 &type_specifiers);
17270   /* If it's a `)', then there is no declarator.  */
17271   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17272     declarator = NULL;
17273   else
17274     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17275                                        /*ctor_dtor_or_conv_p=*/NULL,
17276                                        /*parenthesized_p=*/NULL,
17277                                        /*member_p=*/false);
17278
17279   /* Restore the saved message.  */
17280   parser->type_definition_forbidden_message = saved_message;
17281
17282   if (!type_specifiers.any_specifiers_p)
17283     return error_mark_node;
17284
17285   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17286 }
17287
17288 /* Parse a throw-expression.
17289
17290    throw-expression:
17291      throw assignment-expression [opt]
17292
17293    Returns a THROW_EXPR representing the throw-expression.  */
17294
17295 static tree
17296 cp_parser_throw_expression (cp_parser* parser)
17297 {
17298   tree expression;
17299   cp_token* token;
17300
17301   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17302   token = cp_lexer_peek_token (parser->lexer);
17303   /* Figure out whether or not there is an assignment-expression
17304      following the "throw" keyword.  */
17305   if (token->type == CPP_COMMA
17306       || token->type == CPP_SEMICOLON
17307       || token->type == CPP_CLOSE_PAREN
17308       || token->type == CPP_CLOSE_SQUARE
17309       || token->type == CPP_CLOSE_BRACE
17310       || token->type == CPP_COLON)
17311     expression = NULL_TREE;
17312   else
17313     expression = cp_parser_assignment_expression (parser,
17314                                                   /*cast_p=*/false, NULL);
17315
17316   return build_throw (expression);
17317 }
17318
17319 /* GNU Extensions */
17320
17321 /* Parse an (optional) asm-specification.
17322
17323    asm-specification:
17324      asm ( string-literal )
17325
17326    If the asm-specification is present, returns a STRING_CST
17327    corresponding to the string-literal.  Otherwise, returns
17328    NULL_TREE.  */
17329
17330 static tree
17331 cp_parser_asm_specification_opt (cp_parser* parser)
17332 {
17333   cp_token *token;
17334   tree asm_specification;
17335
17336   /* Peek at the next token.  */
17337   token = cp_lexer_peek_token (parser->lexer);
17338   /* If the next token isn't the `asm' keyword, then there's no
17339      asm-specification.  */
17340   if (!cp_parser_is_keyword (token, RID_ASM))
17341     return NULL_TREE;
17342
17343   /* Consume the `asm' token.  */
17344   cp_lexer_consume_token (parser->lexer);
17345   /* Look for the `('.  */
17346   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17347
17348   /* Look for the string-literal.  */
17349   asm_specification = cp_parser_string_literal (parser, false, false);
17350
17351   /* Look for the `)'.  */
17352   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17353
17354   return asm_specification;
17355 }
17356
17357 /* Parse an asm-operand-list.
17358
17359    asm-operand-list:
17360      asm-operand
17361      asm-operand-list , asm-operand
17362
17363    asm-operand:
17364      string-literal ( expression )
17365      [ string-literal ] string-literal ( expression )
17366
17367    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17368    each node is the expression.  The TREE_PURPOSE is itself a
17369    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17370    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17371    is a STRING_CST for the string literal before the parenthesis. Returns
17372    ERROR_MARK_NODE if any of the operands are invalid.  */
17373
17374 static tree
17375 cp_parser_asm_operand_list (cp_parser* parser)
17376 {
17377   tree asm_operands = NULL_TREE;
17378   bool invalid_operands = false;
17379
17380   while (true)
17381     {
17382       tree string_literal;
17383       tree expression;
17384       tree name;
17385
17386       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17387         {
17388           /* Consume the `[' token.  */
17389           cp_lexer_consume_token (parser->lexer);
17390           /* Read the operand name.  */
17391           name = cp_parser_identifier (parser);
17392           if (name != error_mark_node)
17393             name = build_string (IDENTIFIER_LENGTH (name),
17394                                  IDENTIFIER_POINTER (name));
17395           /* Look for the closing `]'.  */
17396           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17397         }
17398       else
17399         name = NULL_TREE;
17400       /* Look for the string-literal.  */
17401       string_literal = cp_parser_string_literal (parser, false, false);
17402
17403       /* Look for the `('.  */
17404       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17405       /* Parse the expression.  */
17406       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17407       /* Look for the `)'.  */
17408       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17409
17410       if (name == error_mark_node 
17411           || string_literal == error_mark_node 
17412           || expression == error_mark_node)
17413         invalid_operands = true;
17414
17415       /* Add this operand to the list.  */
17416       asm_operands = tree_cons (build_tree_list (name, string_literal),
17417                                 expression,
17418                                 asm_operands);
17419       /* If the next token is not a `,', there are no more
17420          operands.  */
17421       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17422         break;
17423       /* Consume the `,'.  */
17424       cp_lexer_consume_token (parser->lexer);
17425     }
17426
17427   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17428 }
17429
17430 /* Parse an asm-clobber-list.
17431
17432    asm-clobber-list:
17433      string-literal
17434      asm-clobber-list , string-literal
17435
17436    Returns a TREE_LIST, indicating the clobbers in the order that they
17437    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17438
17439 static tree
17440 cp_parser_asm_clobber_list (cp_parser* parser)
17441 {
17442   tree clobbers = NULL_TREE;
17443
17444   while (true)
17445     {
17446       tree string_literal;
17447
17448       /* Look for the string literal.  */
17449       string_literal = cp_parser_string_literal (parser, false, false);
17450       /* Add it to the list.  */
17451       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17452       /* If the next token is not a `,', then the list is
17453          complete.  */
17454       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17455         break;
17456       /* Consume the `,' token.  */
17457       cp_lexer_consume_token (parser->lexer);
17458     }
17459
17460   return clobbers;
17461 }
17462
17463 /* Parse an asm-label-list.
17464
17465    asm-label-list:
17466      identifier
17467      asm-label-list , identifier
17468
17469    Returns a TREE_LIST, indicating the labels in the order that they
17470    appeared.  The TREE_VALUE of each node is a label.  */
17471
17472 static tree
17473 cp_parser_asm_label_list (cp_parser* parser)
17474 {
17475   tree labels = NULL_TREE;
17476
17477   while (true)
17478     {
17479       tree identifier, label, name;
17480
17481       /* Look for the identifier.  */
17482       identifier = cp_parser_identifier (parser);
17483       if (!error_operand_p (identifier))
17484         {
17485           label = lookup_label (identifier);
17486           if (TREE_CODE (label) == LABEL_DECL)
17487             {
17488               TREE_USED (label) = 1;
17489               check_goto (label);
17490               name = build_string (IDENTIFIER_LENGTH (identifier),
17491                                    IDENTIFIER_POINTER (identifier));
17492               labels = tree_cons (name, label, labels);
17493             }
17494         }
17495       /* If the next token is not a `,', then the list is
17496          complete.  */
17497       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17498         break;
17499       /* Consume the `,' token.  */
17500       cp_lexer_consume_token (parser->lexer);
17501     }
17502
17503   return nreverse (labels);
17504 }
17505
17506 /* Parse an (optional) series of attributes.
17507
17508    attributes:
17509      attributes attribute
17510
17511    attribute:
17512      __attribute__ (( attribute-list [opt] ))
17513
17514    The return value is as for cp_parser_attribute_list.  */
17515
17516 static tree
17517 cp_parser_attributes_opt (cp_parser* parser)
17518 {
17519   tree attributes = NULL_TREE;
17520
17521   while (true)
17522     {
17523       cp_token *token;
17524       tree attribute_list;
17525
17526       /* Peek at the next token.  */
17527       token = cp_lexer_peek_token (parser->lexer);
17528       /* If it's not `__attribute__', then we're done.  */
17529       if (token->keyword != RID_ATTRIBUTE)
17530         break;
17531
17532       /* Consume the `__attribute__' keyword.  */
17533       cp_lexer_consume_token (parser->lexer);
17534       /* Look for the two `(' tokens.  */
17535       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17536       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17537
17538       /* Peek at the next token.  */
17539       token = cp_lexer_peek_token (parser->lexer);
17540       if (token->type != CPP_CLOSE_PAREN)
17541         /* Parse the attribute-list.  */
17542         attribute_list = cp_parser_attribute_list (parser);
17543       else
17544         /* If the next token is a `)', then there is no attribute
17545            list.  */
17546         attribute_list = NULL;
17547
17548       /* Look for the two `)' tokens.  */
17549       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17550       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17551
17552       /* Add these new attributes to the list.  */
17553       attributes = chainon (attributes, attribute_list);
17554     }
17555
17556   return attributes;
17557 }
17558
17559 /* Parse an attribute-list.
17560
17561    attribute-list:
17562      attribute
17563      attribute-list , attribute
17564
17565    attribute:
17566      identifier
17567      identifier ( identifier )
17568      identifier ( identifier , expression-list )
17569      identifier ( expression-list )
17570
17571    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17572    to an attribute.  The TREE_PURPOSE of each node is the identifier
17573    indicating which attribute is in use.  The TREE_VALUE represents
17574    the arguments, if any.  */
17575
17576 static tree
17577 cp_parser_attribute_list (cp_parser* parser)
17578 {
17579   tree attribute_list = NULL_TREE;
17580   bool save_translate_strings_p = parser->translate_strings_p;
17581
17582   parser->translate_strings_p = false;
17583   while (true)
17584     {
17585       cp_token *token;
17586       tree identifier;
17587       tree attribute;
17588
17589       /* Look for the identifier.  We also allow keywords here; for
17590          example `__attribute__ ((const))' is legal.  */
17591       token = cp_lexer_peek_token (parser->lexer);
17592       if (token->type == CPP_NAME
17593           || token->type == CPP_KEYWORD)
17594         {
17595           tree arguments = NULL_TREE;
17596
17597           /* Consume the token.  */
17598           token = cp_lexer_consume_token (parser->lexer);
17599
17600           /* Save away the identifier that indicates which attribute
17601              this is.  */
17602           identifier = (token->type == CPP_KEYWORD) 
17603             /* For keywords, use the canonical spelling, not the
17604                parsed identifier.  */
17605             ? ridpointers[(int) token->keyword]
17606             : token->u.value;
17607           
17608           attribute = build_tree_list (identifier, NULL_TREE);
17609
17610           /* Peek at the next token.  */
17611           token = cp_lexer_peek_token (parser->lexer);
17612           /* If it's an `(', then parse the attribute arguments.  */
17613           if (token->type == CPP_OPEN_PAREN)
17614             {
17615               VEC(tree,gc) *vec;
17616               vec = cp_parser_parenthesized_expression_list
17617                     (parser, true, /*cast_p=*/false,
17618                      /*allow_expansion_p=*/false,
17619                      /*non_constant_p=*/NULL);
17620               if (vec == NULL)
17621                 arguments = error_mark_node;
17622               else
17623                 {
17624                   arguments = build_tree_list_vec (vec);
17625                   release_tree_vector (vec);
17626                 }
17627               /* Save the arguments away.  */
17628               TREE_VALUE (attribute) = arguments;
17629             }
17630
17631           if (arguments != error_mark_node)
17632             {
17633               /* Add this attribute to the list.  */
17634               TREE_CHAIN (attribute) = attribute_list;
17635               attribute_list = attribute;
17636             }
17637
17638           token = cp_lexer_peek_token (parser->lexer);
17639         }
17640       /* Now, look for more attributes.  If the next token isn't a
17641          `,', we're done.  */
17642       if (token->type != CPP_COMMA)
17643         break;
17644
17645       /* Consume the comma and keep going.  */
17646       cp_lexer_consume_token (parser->lexer);
17647     }
17648   parser->translate_strings_p = save_translate_strings_p;
17649
17650   /* We built up the list in reverse order.  */
17651   return nreverse (attribute_list);
17652 }
17653
17654 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17655    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17656    current value of the PEDANTIC flag, regardless of whether or not
17657    the `__extension__' keyword is present.  The caller is responsible
17658    for restoring the value of the PEDANTIC flag.  */
17659
17660 static bool
17661 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17662 {
17663   /* Save the old value of the PEDANTIC flag.  */
17664   *saved_pedantic = pedantic;
17665
17666   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17667     {
17668       /* Consume the `__extension__' token.  */
17669       cp_lexer_consume_token (parser->lexer);
17670       /* We're not being pedantic while the `__extension__' keyword is
17671          in effect.  */
17672       pedantic = 0;
17673
17674       return true;
17675     }
17676
17677   return false;
17678 }
17679
17680 /* Parse a label declaration.
17681
17682    label-declaration:
17683      __label__ label-declarator-seq ;
17684
17685    label-declarator-seq:
17686      identifier , label-declarator-seq
17687      identifier  */
17688
17689 static void
17690 cp_parser_label_declaration (cp_parser* parser)
17691 {
17692   /* Look for the `__label__' keyword.  */
17693   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17694
17695   while (true)
17696     {
17697       tree identifier;
17698
17699       /* Look for an identifier.  */
17700       identifier = cp_parser_identifier (parser);
17701       /* If we failed, stop.  */
17702       if (identifier == error_mark_node)
17703         break;
17704       /* Declare it as a label.  */
17705       finish_label_decl (identifier);
17706       /* If the next token is a `;', stop.  */
17707       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17708         break;
17709       /* Look for the `,' separating the label declarations.  */
17710       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17711     }
17712
17713   /* Look for the final `;'.  */
17714   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17715 }
17716
17717 /* Support Functions */
17718
17719 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17720    NAME should have one of the representations used for an
17721    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17722    is returned.  If PARSER->SCOPE is a dependent type, then a
17723    SCOPE_REF is returned.
17724
17725    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17726    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17727    was formed.  Abstractly, such entities should not be passed to this
17728    function, because they do not need to be looked up, but it is
17729    simpler to check for this special case here, rather than at the
17730    call-sites.
17731
17732    In cases not explicitly covered above, this function returns a
17733    DECL, OVERLOAD, or baselink representing the result of the lookup.
17734    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17735    is returned.
17736
17737    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17738    (e.g., "struct") that was used.  In that case bindings that do not
17739    refer to types are ignored.
17740
17741    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17742    ignored.
17743
17744    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17745    are ignored.
17746
17747    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17748    types.
17749
17750    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17751    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17752    NULL_TREE otherwise.  */
17753
17754 static tree
17755 cp_parser_lookup_name (cp_parser *parser, tree name,
17756                        enum tag_types tag_type,
17757                        bool is_template,
17758                        bool is_namespace,
17759                        bool check_dependency,
17760                        tree *ambiguous_decls,
17761                        location_t name_location)
17762 {
17763   int flags = 0;
17764   tree decl;
17765   tree object_type = parser->context->object_type;
17766
17767   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17768     flags |= LOOKUP_COMPLAIN;
17769
17770   /* Assume that the lookup will be unambiguous.  */
17771   if (ambiguous_decls)
17772     *ambiguous_decls = NULL_TREE;
17773
17774   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17775      no longer valid.  Note that if we are parsing tentatively, and
17776      the parse fails, OBJECT_TYPE will be automatically restored.  */
17777   parser->context->object_type = NULL_TREE;
17778
17779   if (name == error_mark_node)
17780     return error_mark_node;
17781
17782   /* A template-id has already been resolved; there is no lookup to
17783      do.  */
17784   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17785     return name;
17786   if (BASELINK_P (name))
17787     {
17788       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17789                   == TEMPLATE_ID_EXPR);
17790       return name;
17791     }
17792
17793   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17794      it should already have been checked to make sure that the name
17795      used matches the type being destroyed.  */
17796   if (TREE_CODE (name) == BIT_NOT_EXPR)
17797     {
17798       tree type;
17799
17800       /* Figure out to which type this destructor applies.  */
17801       if (parser->scope)
17802         type = parser->scope;
17803       else if (object_type)
17804         type = object_type;
17805       else
17806         type = current_class_type;
17807       /* If that's not a class type, there is no destructor.  */
17808       if (!type || !CLASS_TYPE_P (type))
17809         return error_mark_node;
17810       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17811         lazily_declare_fn (sfk_destructor, type);
17812       if (!CLASSTYPE_DESTRUCTORS (type))
17813           return error_mark_node;
17814       /* If it was a class type, return the destructor.  */
17815       return CLASSTYPE_DESTRUCTORS (type);
17816     }
17817
17818   /* By this point, the NAME should be an ordinary identifier.  If
17819      the id-expression was a qualified name, the qualifying scope is
17820      stored in PARSER->SCOPE at this point.  */
17821   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17822
17823   /* Perform the lookup.  */
17824   if (parser->scope)
17825     {
17826       bool dependent_p;
17827
17828       if (parser->scope == error_mark_node)
17829         return error_mark_node;
17830
17831       /* If the SCOPE is dependent, the lookup must be deferred until
17832          the template is instantiated -- unless we are explicitly
17833          looking up names in uninstantiated templates.  Even then, we
17834          cannot look up the name if the scope is not a class type; it
17835          might, for example, be a template type parameter.  */
17836       dependent_p = (TYPE_P (parser->scope)
17837                      && dependent_scope_p (parser->scope));
17838       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17839           && dependent_p)
17840         /* Defer lookup.  */
17841         decl = error_mark_node;
17842       else
17843         {
17844           tree pushed_scope = NULL_TREE;
17845
17846           /* If PARSER->SCOPE is a dependent type, then it must be a
17847              class type, and we must not be checking dependencies;
17848              otherwise, we would have processed this lookup above.  So
17849              that PARSER->SCOPE is not considered a dependent base by
17850              lookup_member, we must enter the scope here.  */
17851           if (dependent_p)
17852             pushed_scope = push_scope (parser->scope);
17853           /* If the PARSER->SCOPE is a template specialization, it
17854              may be instantiated during name lookup.  In that case,
17855              errors may be issued.  Even if we rollback the current
17856              tentative parse, those errors are valid.  */
17857           decl = lookup_qualified_name (parser->scope, name,
17858                                         tag_type != none_type,
17859                                         /*complain=*/true);
17860
17861           /* If we have a single function from a using decl, pull it out.  */
17862           if (TREE_CODE (decl) == OVERLOAD
17863               && !really_overloaded_fn (decl))
17864             decl = OVL_FUNCTION (decl);
17865
17866           if (pushed_scope)
17867             pop_scope (pushed_scope);
17868         }
17869
17870       /* If the scope is a dependent type and either we deferred lookup or
17871          we did lookup but didn't find the name, rememeber the name.  */
17872       if (decl == error_mark_node && TYPE_P (parser->scope)
17873           && dependent_type_p (parser->scope))
17874         {
17875           if (tag_type)
17876             {
17877               tree type;
17878
17879               /* The resolution to Core Issue 180 says that `struct
17880                  A::B' should be considered a type-name, even if `A'
17881                  is dependent.  */
17882               type = make_typename_type (parser->scope, name, tag_type,
17883                                          /*complain=*/tf_error);
17884               decl = TYPE_NAME (type);
17885             }
17886           else if (is_template
17887                    && (cp_parser_next_token_ends_template_argument_p (parser)
17888                        || cp_lexer_next_token_is (parser->lexer,
17889                                                   CPP_CLOSE_PAREN)))
17890             decl = make_unbound_class_template (parser->scope,
17891                                                 name, NULL_TREE,
17892                                                 /*complain=*/tf_error);
17893           else
17894             decl = build_qualified_name (/*type=*/NULL_TREE,
17895                                          parser->scope, name,
17896                                          is_template);
17897         }
17898       parser->qualifying_scope = parser->scope;
17899       parser->object_scope = NULL_TREE;
17900     }
17901   else if (object_type)
17902     {
17903       tree object_decl = NULL_TREE;
17904       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17905          OBJECT_TYPE is not a class.  */
17906       if (CLASS_TYPE_P (object_type))
17907         /* If the OBJECT_TYPE is a template specialization, it may
17908            be instantiated during name lookup.  In that case, errors
17909            may be issued.  Even if we rollback the current tentative
17910            parse, those errors are valid.  */
17911         object_decl = lookup_member (object_type,
17912                                      name,
17913                                      /*protect=*/0,
17914                                      tag_type != none_type);
17915       /* Look it up in the enclosing context, too.  */
17916       decl = lookup_name_real (name, tag_type != none_type,
17917                                /*nonclass=*/0,
17918                                /*block_p=*/true, is_namespace, flags);
17919       parser->object_scope = object_type;
17920       parser->qualifying_scope = NULL_TREE;
17921       if (object_decl)
17922         decl = object_decl;
17923     }
17924   else
17925     {
17926       decl = lookup_name_real (name, tag_type != none_type,
17927                                /*nonclass=*/0,
17928                                /*block_p=*/true, is_namespace, flags);
17929       parser->qualifying_scope = NULL_TREE;
17930       parser->object_scope = NULL_TREE;
17931     }
17932
17933   /* If the lookup failed, let our caller know.  */
17934   if (!decl || decl == error_mark_node)
17935     return error_mark_node;
17936
17937   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17938   if (TREE_CODE (decl) == TREE_LIST)
17939     {
17940       if (ambiguous_decls)
17941         *ambiguous_decls = decl;
17942       /* The error message we have to print is too complicated for
17943          cp_parser_error, so we incorporate its actions directly.  */
17944       if (!cp_parser_simulate_error (parser))
17945         {
17946           error_at (name_location, "reference to %qD is ambiguous",
17947                     name);
17948           print_candidates (decl);
17949         }
17950       return error_mark_node;
17951     }
17952
17953   gcc_assert (DECL_P (decl)
17954               || TREE_CODE (decl) == OVERLOAD
17955               || TREE_CODE (decl) == SCOPE_REF
17956               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17957               || BASELINK_P (decl));
17958
17959   /* If we have resolved the name of a member declaration, check to
17960      see if the declaration is accessible.  When the name resolves to
17961      set of overloaded functions, accessibility is checked when
17962      overload resolution is done.
17963
17964      During an explicit instantiation, access is not checked at all,
17965      as per [temp.explicit].  */
17966   if (DECL_P (decl))
17967     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17968
17969   return decl;
17970 }
17971
17972 /* Like cp_parser_lookup_name, but for use in the typical case where
17973    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17974    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17975
17976 static tree
17977 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17978 {
17979   return cp_parser_lookup_name (parser, name,
17980                                 none_type,
17981                                 /*is_template=*/false,
17982                                 /*is_namespace=*/false,
17983                                 /*check_dependency=*/true,
17984                                 /*ambiguous_decls=*/NULL,
17985                                 location);
17986 }
17987
17988 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17989    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17990    true, the DECL indicates the class being defined in a class-head,
17991    or declared in an elaborated-type-specifier.
17992
17993    Otherwise, return DECL.  */
17994
17995 static tree
17996 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17997 {
17998   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17999      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18000
18001        struct A {
18002          template <typename T> struct B;
18003        };
18004
18005        template <typename T> struct A::B {};
18006
18007      Similarly, in an elaborated-type-specifier:
18008
18009        namespace N { struct X{}; }
18010
18011        struct A {
18012          template <typename T> friend struct N::X;
18013        };
18014
18015      However, if the DECL refers to a class type, and we are in
18016      the scope of the class, then the name lookup automatically
18017      finds the TYPE_DECL created by build_self_reference rather
18018      than a TEMPLATE_DECL.  For example, in:
18019
18020        template <class T> struct S {
18021          S s;
18022        };
18023
18024      there is no need to handle such case.  */
18025
18026   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18027     return DECL_TEMPLATE_RESULT (decl);
18028
18029   return decl;
18030 }
18031
18032 /* If too many, or too few, template-parameter lists apply to the
18033    declarator, issue an error message.  Returns TRUE if all went well,
18034    and FALSE otherwise.  */
18035
18036 static bool
18037 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18038                                                 cp_declarator *declarator,
18039                                                 location_t declarator_location)
18040 {
18041   unsigned num_templates;
18042
18043   /* We haven't seen any classes that involve template parameters yet.  */
18044   num_templates = 0;
18045
18046   switch (declarator->kind)
18047     {
18048     case cdk_id:
18049       if (declarator->u.id.qualifying_scope)
18050         {
18051           tree scope;
18052           tree member;
18053
18054           scope = declarator->u.id.qualifying_scope;
18055           member = declarator->u.id.unqualified_name;
18056
18057           while (scope && CLASS_TYPE_P (scope))
18058             {
18059               /* You're supposed to have one `template <...>'
18060                  for every template class, but you don't need one
18061                  for a full specialization.  For example:
18062
18063                  template <class T> struct S{};
18064                  template <> struct S<int> { void f(); };
18065                  void S<int>::f () {}
18066
18067                  is correct; there shouldn't be a `template <>' for
18068                  the definition of `S<int>::f'.  */
18069               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18070                 /* If SCOPE does not have template information of any
18071                    kind, then it is not a template, nor is it nested
18072                    within a template.  */
18073                 break;
18074               if (explicit_class_specialization_p (scope))
18075                 break;
18076               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18077                 ++num_templates;
18078
18079               scope = TYPE_CONTEXT (scope);
18080             }
18081         }
18082       else if (TREE_CODE (declarator->u.id.unqualified_name)
18083                == TEMPLATE_ID_EXPR)
18084         /* If the DECLARATOR has the form `X<y>' then it uses one
18085            additional level of template parameters.  */
18086         ++num_templates;
18087
18088       return cp_parser_check_template_parameters 
18089         (parser, num_templates, declarator_location, declarator);
18090
18091
18092     case cdk_function:
18093     case cdk_array:
18094     case cdk_pointer:
18095     case cdk_reference:
18096     case cdk_ptrmem:
18097       return (cp_parser_check_declarator_template_parameters
18098               (parser, declarator->declarator, declarator_location));
18099
18100     case cdk_error:
18101       return true;
18102
18103     default:
18104       gcc_unreachable ();
18105     }
18106   return false;
18107 }
18108
18109 /* NUM_TEMPLATES were used in the current declaration.  If that is
18110    invalid, return FALSE and issue an error messages.  Otherwise,
18111    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18112    declarator and we can print more accurate diagnostics.  */
18113
18114 static bool
18115 cp_parser_check_template_parameters (cp_parser* parser,
18116                                      unsigned num_templates,
18117                                      location_t location,
18118                                      cp_declarator *declarator)
18119 {
18120   /* If there are the same number of template classes and parameter
18121      lists, that's OK.  */
18122   if (parser->num_template_parameter_lists == num_templates)
18123     return true;
18124   /* If there are more, but only one more, then we are referring to a
18125      member template.  That's OK too.  */
18126   if (parser->num_template_parameter_lists == num_templates + 1)
18127     return true;
18128   /* If there are more template classes than parameter lists, we have
18129      something like:
18130
18131        template <class T> void S<T>::R<T>::f ();  */
18132   if (parser->num_template_parameter_lists < num_templates)
18133     {
18134       if (declarator)
18135         error_at (location, "specializing member %<%T::%E%> "
18136                   "requires %<template<>%> syntax", 
18137                   declarator->u.id.qualifying_scope,
18138                   declarator->u.id.unqualified_name);
18139       else 
18140         error_at (location, "too few template-parameter-lists");
18141       return false;
18142     }
18143   /* Otherwise, there are too many template parameter lists.  We have
18144      something like:
18145
18146      template <class T> template <class U> void S::f();  */
18147   error_at (location, "too many template-parameter-lists");
18148   return false;
18149 }
18150
18151 /* Parse an optional `::' token indicating that the following name is
18152    from the global namespace.  If so, PARSER->SCOPE is set to the
18153    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18154    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18155    Returns the new value of PARSER->SCOPE, if the `::' token is
18156    present, and NULL_TREE otherwise.  */
18157
18158 static tree
18159 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18160 {
18161   cp_token *token;
18162
18163   /* Peek at the next token.  */
18164   token = cp_lexer_peek_token (parser->lexer);
18165   /* If we're looking at a `::' token then we're starting from the
18166      global namespace, not our current location.  */
18167   if (token->type == CPP_SCOPE)
18168     {
18169       /* Consume the `::' token.  */
18170       cp_lexer_consume_token (parser->lexer);
18171       /* Set the SCOPE so that we know where to start the lookup.  */
18172       parser->scope = global_namespace;
18173       parser->qualifying_scope = global_namespace;
18174       parser->object_scope = NULL_TREE;
18175
18176       return parser->scope;
18177     }
18178   else if (!current_scope_valid_p)
18179     {
18180       parser->scope = NULL_TREE;
18181       parser->qualifying_scope = NULL_TREE;
18182       parser->object_scope = NULL_TREE;
18183     }
18184
18185   return NULL_TREE;
18186 }
18187
18188 /* Returns TRUE if the upcoming token sequence is the start of a
18189    constructor declarator.  If FRIEND_P is true, the declarator is
18190    preceded by the `friend' specifier.  */
18191
18192 static bool
18193 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18194 {
18195   bool constructor_p;
18196   tree type_decl = NULL_TREE;
18197   bool nested_name_p;
18198   cp_token *next_token;
18199
18200   /* The common case is that this is not a constructor declarator, so
18201      try to avoid doing lots of work if at all possible.  It's not
18202      valid declare a constructor at function scope.  */
18203   if (parser->in_function_body)
18204     return false;
18205   /* And only certain tokens can begin a constructor declarator.  */
18206   next_token = cp_lexer_peek_token (parser->lexer);
18207   if (next_token->type != CPP_NAME
18208       && next_token->type != CPP_SCOPE
18209       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18210       && next_token->type != CPP_TEMPLATE_ID)
18211     return false;
18212
18213   /* Parse tentatively; we are going to roll back all of the tokens
18214      consumed here.  */
18215   cp_parser_parse_tentatively (parser);
18216   /* Assume that we are looking at a constructor declarator.  */
18217   constructor_p = true;
18218
18219   /* Look for the optional `::' operator.  */
18220   cp_parser_global_scope_opt (parser,
18221                               /*current_scope_valid_p=*/false);
18222   /* Look for the nested-name-specifier.  */
18223   nested_name_p
18224     = (cp_parser_nested_name_specifier_opt (parser,
18225                                             /*typename_keyword_p=*/false,
18226                                             /*check_dependency_p=*/false,
18227                                             /*type_p=*/false,
18228                                             /*is_declaration=*/false)
18229        != NULL_TREE);
18230   /* Outside of a class-specifier, there must be a
18231      nested-name-specifier.  */
18232   if (!nested_name_p &&
18233       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18234        || friend_p))
18235     constructor_p = false;
18236   /* If we still think that this might be a constructor-declarator,
18237      look for a class-name.  */
18238   if (constructor_p)
18239     {
18240       /* If we have:
18241
18242            template <typename T> struct S { S(); };
18243            template <typename T> S<T>::S ();
18244
18245          we must recognize that the nested `S' names a class.
18246          Similarly, for:
18247
18248            template <typename T> S<T>::S<T> ();
18249
18250          we must recognize that the nested `S' names a template.  */
18251       type_decl = cp_parser_class_name (parser,
18252                                         /*typename_keyword_p=*/false,
18253                                         /*template_keyword_p=*/false,
18254                                         none_type,
18255                                         /*check_dependency_p=*/false,
18256                                         /*class_head_p=*/false,
18257                                         /*is_declaration=*/false);
18258       /* If there was no class-name, then this is not a constructor.  */
18259       constructor_p = !cp_parser_error_occurred (parser);
18260     }
18261
18262   /* If we're still considering a constructor, we have to see a `(',
18263      to begin the parameter-declaration-clause, followed by either a
18264      `)', an `...', or a decl-specifier.  We need to check for a
18265      type-specifier to avoid being fooled into thinking that:
18266
18267        S::S (f) (int);
18268
18269      is a constructor.  (It is actually a function named `f' that
18270      takes one parameter (of type `int') and returns a value of type
18271      `S::S'.  */
18272   if (constructor_p
18273       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18274     {
18275       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18276           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18277           /* A parameter declaration begins with a decl-specifier,
18278              which is either the "attribute" keyword, a storage class
18279              specifier, or (usually) a type-specifier.  */
18280           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18281         {
18282           tree type;
18283           tree pushed_scope = NULL_TREE;
18284           unsigned saved_num_template_parameter_lists;
18285
18286           /* Names appearing in the type-specifier should be looked up
18287              in the scope of the class.  */
18288           if (current_class_type)
18289             type = NULL_TREE;
18290           else
18291             {
18292               type = TREE_TYPE (type_decl);
18293               if (TREE_CODE (type) == TYPENAME_TYPE)
18294                 {
18295                   type = resolve_typename_type (type,
18296                                                 /*only_current_p=*/false);
18297                   if (TREE_CODE (type) == TYPENAME_TYPE)
18298                     {
18299                       cp_parser_abort_tentative_parse (parser);
18300                       return false;
18301                     }
18302                 }
18303               pushed_scope = push_scope (type);
18304             }
18305
18306           /* Inside the constructor parameter list, surrounding
18307              template-parameter-lists do not apply.  */
18308           saved_num_template_parameter_lists
18309             = parser->num_template_parameter_lists;
18310           parser->num_template_parameter_lists = 0;
18311
18312           /* Look for the type-specifier.  */
18313           cp_parser_type_specifier (parser,
18314                                     CP_PARSER_FLAGS_NONE,
18315                                     /*decl_specs=*/NULL,
18316                                     /*is_declarator=*/true,
18317                                     /*declares_class_or_enum=*/NULL,
18318                                     /*is_cv_qualifier=*/NULL);
18319
18320           parser->num_template_parameter_lists
18321             = saved_num_template_parameter_lists;
18322
18323           /* Leave the scope of the class.  */
18324           if (pushed_scope)
18325             pop_scope (pushed_scope);
18326
18327           constructor_p = !cp_parser_error_occurred (parser);
18328         }
18329     }
18330   else
18331     constructor_p = false;
18332   /* We did not really want to consume any tokens.  */
18333   cp_parser_abort_tentative_parse (parser);
18334
18335   return constructor_p;
18336 }
18337
18338 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18339    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18340    they must be performed once we are in the scope of the function.
18341
18342    Returns the function defined.  */
18343
18344 static tree
18345 cp_parser_function_definition_from_specifiers_and_declarator
18346   (cp_parser* parser,
18347    cp_decl_specifier_seq *decl_specifiers,
18348    tree attributes,
18349    const cp_declarator *declarator)
18350 {
18351   tree fn;
18352   bool success_p;
18353
18354   /* Begin the function-definition.  */
18355   success_p = start_function (decl_specifiers, declarator, attributes);
18356
18357   /* The things we're about to see are not directly qualified by any
18358      template headers we've seen thus far.  */
18359   reset_specialization ();
18360
18361   /* If there were names looked up in the decl-specifier-seq that we
18362      did not check, check them now.  We must wait until we are in the
18363      scope of the function to perform the checks, since the function
18364      might be a friend.  */
18365   perform_deferred_access_checks ();
18366
18367   if (!success_p)
18368     {
18369       /* Skip the entire function.  */
18370       cp_parser_skip_to_end_of_block_or_statement (parser);
18371       fn = error_mark_node;
18372     }
18373   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18374     {
18375       /* Seen already, skip it.  An error message has already been output.  */
18376       cp_parser_skip_to_end_of_block_or_statement (parser);
18377       fn = current_function_decl;
18378       current_function_decl = NULL_TREE;
18379       /* If this is a function from a class, pop the nested class.  */
18380       if (current_class_name)
18381         pop_nested_class ();
18382     }
18383   else
18384     fn = cp_parser_function_definition_after_declarator (parser,
18385                                                          /*inline_p=*/false);
18386
18387   return fn;
18388 }
18389
18390 /* Parse the part of a function-definition that follows the
18391    declarator.  INLINE_P is TRUE iff this function is an inline
18392    function defined within a class-specifier.
18393
18394    Returns the function defined.  */
18395
18396 static tree
18397 cp_parser_function_definition_after_declarator (cp_parser* parser,
18398                                                 bool inline_p)
18399 {
18400   tree fn;
18401   bool ctor_initializer_p = false;
18402   bool saved_in_unbraced_linkage_specification_p;
18403   bool saved_in_function_body;
18404   unsigned saved_num_template_parameter_lists;
18405   cp_token *token;
18406
18407   saved_in_function_body = parser->in_function_body;
18408   parser->in_function_body = true;
18409   /* If the next token is `return', then the code may be trying to
18410      make use of the "named return value" extension that G++ used to
18411      support.  */
18412   token = cp_lexer_peek_token (parser->lexer);
18413   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18414     {
18415       /* Consume the `return' keyword.  */
18416       cp_lexer_consume_token (parser->lexer);
18417       /* Look for the identifier that indicates what value is to be
18418          returned.  */
18419       cp_parser_identifier (parser);
18420       /* Issue an error message.  */
18421       error_at (token->location,
18422                 "named return values are no longer supported");
18423       /* Skip tokens until we reach the start of the function body.  */
18424       while (true)
18425         {
18426           cp_token *token = cp_lexer_peek_token (parser->lexer);
18427           if (token->type == CPP_OPEN_BRACE
18428               || token->type == CPP_EOF
18429               || token->type == CPP_PRAGMA_EOL)
18430             break;
18431           cp_lexer_consume_token (parser->lexer);
18432         }
18433     }
18434   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18435      anything declared inside `f'.  */
18436   saved_in_unbraced_linkage_specification_p
18437     = parser->in_unbraced_linkage_specification_p;
18438   parser->in_unbraced_linkage_specification_p = false;
18439   /* Inside the function, surrounding template-parameter-lists do not
18440      apply.  */
18441   saved_num_template_parameter_lists
18442     = parser->num_template_parameter_lists;
18443   parser->num_template_parameter_lists = 0;
18444
18445   start_lambda_scope (current_function_decl);
18446
18447   /* If the next token is `try', then we are looking at a
18448      function-try-block.  */
18449   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18450     ctor_initializer_p = cp_parser_function_try_block (parser);
18451   /* A function-try-block includes the function-body, so we only do
18452      this next part if we're not processing a function-try-block.  */
18453   else
18454     ctor_initializer_p
18455       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18456
18457   finish_lambda_scope ();
18458
18459   /* Finish the function.  */
18460   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18461                         (inline_p ? 2 : 0));
18462   /* Generate code for it, if necessary.  */
18463   expand_or_defer_fn (fn);
18464   /* Restore the saved values.  */
18465   parser->in_unbraced_linkage_specification_p
18466     = saved_in_unbraced_linkage_specification_p;
18467   parser->num_template_parameter_lists
18468     = saved_num_template_parameter_lists;
18469   parser->in_function_body = saved_in_function_body;
18470
18471   return fn;
18472 }
18473
18474 /* Parse a template-declaration, assuming that the `export' (and
18475    `extern') keywords, if present, has already been scanned.  MEMBER_P
18476    is as for cp_parser_template_declaration.  */
18477
18478 static void
18479 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18480 {
18481   tree decl = NULL_TREE;
18482   VEC (deferred_access_check,gc) *checks;
18483   tree parameter_list;
18484   bool friend_p = false;
18485   bool need_lang_pop;
18486   cp_token *token;
18487
18488   /* Look for the `template' keyword.  */
18489   token = cp_lexer_peek_token (parser->lexer);
18490   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18491     return;
18492
18493   /* And the `<'.  */
18494   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18495     return;
18496   if (at_class_scope_p () && current_function_decl)
18497     {
18498       /* 14.5.2.2 [temp.mem]
18499
18500          A local class shall not have member templates.  */
18501       error_at (token->location,
18502                 "invalid declaration of member template in local class");
18503       cp_parser_skip_to_end_of_block_or_statement (parser);
18504       return;
18505     }
18506   /* [temp]
18507
18508      A template ... shall not have C linkage.  */
18509   if (current_lang_name == lang_name_c)
18510     {
18511       error_at (token->location, "template with C linkage");
18512       /* Give it C++ linkage to avoid confusing other parts of the
18513          front end.  */
18514       push_lang_context (lang_name_cplusplus);
18515       need_lang_pop = true;
18516     }
18517   else
18518     need_lang_pop = false;
18519
18520   /* We cannot perform access checks on the template parameter
18521      declarations until we know what is being declared, just as we
18522      cannot check the decl-specifier list.  */
18523   push_deferring_access_checks (dk_deferred);
18524
18525   /* If the next token is `>', then we have an invalid
18526      specialization.  Rather than complain about an invalid template
18527      parameter, issue an error message here.  */
18528   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18529     {
18530       cp_parser_error (parser, "invalid explicit specialization");
18531       begin_specialization ();
18532       parameter_list = NULL_TREE;
18533     }
18534   else
18535     /* Parse the template parameters.  */
18536     parameter_list = cp_parser_template_parameter_list (parser);
18537
18538   /* Get the deferred access checks from the parameter list.  These
18539      will be checked once we know what is being declared, as for a
18540      member template the checks must be performed in the scope of the
18541      class containing the member.  */
18542   checks = get_deferred_access_checks ();
18543
18544   /* Look for the `>'.  */
18545   cp_parser_skip_to_end_of_template_parameter_list (parser);
18546   /* We just processed one more parameter list.  */
18547   ++parser->num_template_parameter_lists;
18548   /* If the next token is `template', there are more template
18549      parameters.  */
18550   if (cp_lexer_next_token_is_keyword (parser->lexer,
18551                                       RID_TEMPLATE))
18552     cp_parser_template_declaration_after_export (parser, member_p);
18553   else
18554     {
18555       /* There are no access checks when parsing a template, as we do not
18556          know if a specialization will be a friend.  */
18557       push_deferring_access_checks (dk_no_check);
18558       token = cp_lexer_peek_token (parser->lexer);
18559       decl = cp_parser_single_declaration (parser,
18560                                            checks,
18561                                            member_p,
18562                                            /*explicit_specialization_p=*/false,
18563                                            &friend_p);
18564       pop_deferring_access_checks ();
18565
18566       /* If this is a member template declaration, let the front
18567          end know.  */
18568       if (member_p && !friend_p && decl)
18569         {
18570           if (TREE_CODE (decl) == TYPE_DECL)
18571             cp_parser_check_access_in_redeclaration (decl, token->location);
18572
18573           decl = finish_member_template_decl (decl);
18574         }
18575       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18576         make_friend_class (current_class_type, TREE_TYPE (decl),
18577                            /*complain=*/true);
18578     }
18579   /* We are done with the current parameter list.  */
18580   --parser->num_template_parameter_lists;
18581
18582   pop_deferring_access_checks ();
18583
18584   /* Finish up.  */
18585   finish_template_decl (parameter_list);
18586
18587   /* Register member declarations.  */
18588   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18589     finish_member_declaration (decl);
18590   /* For the erroneous case of a template with C linkage, we pushed an
18591      implicit C++ linkage scope; exit that scope now.  */
18592   if (need_lang_pop)
18593     pop_lang_context ();
18594   /* If DECL is a function template, we must return to parse it later.
18595      (Even though there is no definition, there might be default
18596      arguments that need handling.)  */
18597   if (member_p && decl
18598       && (TREE_CODE (decl) == FUNCTION_DECL
18599           || DECL_FUNCTION_TEMPLATE_P (decl)))
18600     TREE_VALUE (parser->unparsed_functions_queues)
18601       = tree_cons (NULL_TREE, decl,
18602                    TREE_VALUE (parser->unparsed_functions_queues));
18603 }
18604
18605 /* Perform the deferred access checks from a template-parameter-list.
18606    CHECKS is a TREE_LIST of access checks, as returned by
18607    get_deferred_access_checks.  */
18608
18609 static void
18610 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18611 {
18612   ++processing_template_parmlist;
18613   perform_access_checks (checks);
18614   --processing_template_parmlist;
18615 }
18616
18617 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18618    `function-definition' sequence.  MEMBER_P is true, this declaration
18619    appears in a class scope.
18620
18621    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18622    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18623
18624 static tree
18625 cp_parser_single_declaration (cp_parser* parser,
18626                               VEC (deferred_access_check,gc)* checks,
18627                               bool member_p,
18628                               bool explicit_specialization_p,
18629                               bool* friend_p)
18630 {
18631   int declares_class_or_enum;
18632   tree decl = NULL_TREE;
18633   cp_decl_specifier_seq decl_specifiers;
18634   bool function_definition_p = false;
18635   cp_token *decl_spec_token_start;
18636
18637   /* This function is only used when processing a template
18638      declaration.  */
18639   gcc_assert (innermost_scope_kind () == sk_template_parms
18640               || innermost_scope_kind () == sk_template_spec);
18641
18642   /* Defer access checks until we know what is being declared.  */
18643   push_deferring_access_checks (dk_deferred);
18644
18645   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18646      alternative.  */
18647   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18648   cp_parser_decl_specifier_seq (parser,
18649                                 CP_PARSER_FLAGS_OPTIONAL,
18650                                 &decl_specifiers,
18651                                 &declares_class_or_enum);
18652   if (friend_p)
18653     *friend_p = cp_parser_friend_p (&decl_specifiers);
18654
18655   /* There are no template typedefs.  */
18656   if (decl_specifiers.specs[(int) ds_typedef])
18657     {
18658       error_at (decl_spec_token_start->location,
18659                 "template declaration of %<typedef%>");
18660       decl = error_mark_node;
18661     }
18662
18663   /* Gather up the access checks that occurred the
18664      decl-specifier-seq.  */
18665   stop_deferring_access_checks ();
18666
18667   /* Check for the declaration of a template class.  */
18668   if (declares_class_or_enum)
18669     {
18670       if (cp_parser_declares_only_class_p (parser))
18671         {
18672           decl = shadow_tag (&decl_specifiers);
18673
18674           /* In this case:
18675
18676                struct C {
18677                  friend template <typename T> struct A<T>::B;
18678                };
18679
18680              A<T>::B will be represented by a TYPENAME_TYPE, and
18681              therefore not recognized by shadow_tag.  */
18682           if (friend_p && *friend_p
18683               && !decl
18684               && decl_specifiers.type
18685               && TYPE_P (decl_specifiers.type))
18686             decl = decl_specifiers.type;
18687
18688           if (decl && decl != error_mark_node)
18689             decl = TYPE_NAME (decl);
18690           else
18691             decl = error_mark_node;
18692
18693           /* Perform access checks for template parameters.  */
18694           cp_parser_perform_template_parameter_access_checks (checks);
18695         }
18696     }
18697   /* If it's not a template class, try for a template function.  If
18698      the next token is a `;', then this declaration does not declare
18699      anything.  But, if there were errors in the decl-specifiers, then
18700      the error might well have come from an attempted class-specifier.
18701      In that case, there's no need to warn about a missing declarator.  */
18702   if (!decl
18703       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18704           || decl_specifiers.type != error_mark_node))
18705     {
18706       decl = cp_parser_init_declarator (parser,
18707                                         &decl_specifiers,
18708                                         checks,
18709                                         /*function_definition_allowed_p=*/true,
18710                                         member_p,
18711                                         declares_class_or_enum,
18712                                         &function_definition_p);
18713
18714     /* 7.1.1-1 [dcl.stc]
18715
18716        A storage-class-specifier shall not be specified in an explicit
18717        specialization...  */
18718     if (decl
18719         && explicit_specialization_p
18720         && decl_specifiers.storage_class != sc_none)
18721       {
18722         error_at (decl_spec_token_start->location,
18723                   "explicit template specialization cannot have a storage class");
18724         decl = error_mark_node;
18725       }
18726     }
18727
18728   pop_deferring_access_checks ();
18729
18730   /* Clear any current qualification; whatever comes next is the start
18731      of something new.  */
18732   parser->scope = NULL_TREE;
18733   parser->qualifying_scope = NULL_TREE;
18734   parser->object_scope = NULL_TREE;
18735   /* Look for a trailing `;' after the declaration.  */
18736   if (!function_definition_p
18737       && (decl == error_mark_node
18738           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18739     cp_parser_skip_to_end_of_block_or_statement (parser);
18740
18741   return decl;
18742 }
18743
18744 /* Parse a cast-expression that is not the operand of a unary "&".  */
18745
18746 static tree
18747 cp_parser_simple_cast_expression (cp_parser *parser)
18748 {
18749   return cp_parser_cast_expression (parser, /*address_p=*/false,
18750                                     /*cast_p=*/false, NULL);
18751 }
18752
18753 /* Parse a functional cast to TYPE.  Returns an expression
18754    representing the cast.  */
18755
18756 static tree
18757 cp_parser_functional_cast (cp_parser* parser, tree type)
18758 {
18759   VEC(tree,gc) *vec;
18760   tree expression_list;
18761   tree cast;
18762   bool nonconst_p;
18763
18764   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18765     {
18766       maybe_warn_cpp0x ("extended initializer lists");
18767       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18768       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18769       if (TREE_CODE (type) == TYPE_DECL)
18770         type = TREE_TYPE (type);
18771       return finish_compound_literal (type, expression_list);
18772     }
18773
18774
18775   vec = cp_parser_parenthesized_expression_list (parser, false,
18776                                                  /*cast_p=*/true,
18777                                                  /*allow_expansion_p=*/true,
18778                                                  /*non_constant_p=*/NULL);
18779   if (vec == NULL)
18780     expression_list = error_mark_node;
18781   else
18782     {
18783       expression_list = build_tree_list_vec (vec);
18784       release_tree_vector (vec);
18785     }
18786
18787   cast = build_functional_cast (type, expression_list,
18788                                 tf_warning_or_error);
18789   /* [expr.const]/1: In an integral constant expression "only type
18790      conversions to integral or enumeration type can be used".  */
18791   if (TREE_CODE (type) == TYPE_DECL)
18792     type = TREE_TYPE (type);
18793   if (cast != error_mark_node
18794       && !cast_valid_in_integral_constant_expression_p (type)
18795       && (cp_parser_non_integral_constant_expression
18796           (parser, "a call to a constructor")))
18797     return error_mark_node;
18798   return cast;
18799 }
18800
18801 /* Save the tokens that make up the body of a member function defined
18802    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18803    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18804    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18805    for the member function.  */
18806
18807 static tree
18808 cp_parser_save_member_function_body (cp_parser* parser,
18809                                      cp_decl_specifier_seq *decl_specifiers,
18810                                      cp_declarator *declarator,
18811                                      tree attributes)
18812 {
18813   cp_token *first;
18814   cp_token *last;
18815   tree fn;
18816
18817   /* Create the FUNCTION_DECL.  */
18818   fn = grokmethod (decl_specifiers, declarator, attributes);
18819   /* If something went badly wrong, bail out now.  */
18820   if (fn == error_mark_node)
18821     {
18822       /* If there's a function-body, skip it.  */
18823       if (cp_parser_token_starts_function_definition_p
18824           (cp_lexer_peek_token (parser->lexer)))
18825         cp_parser_skip_to_end_of_block_or_statement (parser);
18826       return error_mark_node;
18827     }
18828
18829   /* Remember it, if there default args to post process.  */
18830   cp_parser_save_default_args (parser, fn);
18831
18832   /* Save away the tokens that make up the body of the
18833      function.  */
18834   first = parser->lexer->next_token;
18835   /* We can have braced-init-list mem-initializers before the fn body.  */
18836   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18837     {
18838       cp_lexer_consume_token (parser->lexer);
18839       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18840              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18841         {
18842           /* cache_group will stop after an un-nested { } pair, too.  */
18843           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18844             break;
18845
18846           /* variadic mem-inits have ... after the ')'.  */
18847           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18848             cp_lexer_consume_token (parser->lexer);
18849         }
18850     }
18851   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18852   /* Handle function try blocks.  */
18853   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18854     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18855   last = parser->lexer->next_token;
18856
18857   /* Save away the inline definition; we will process it when the
18858      class is complete.  */
18859   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18860   DECL_PENDING_INLINE_P (fn) = 1;
18861
18862   /* We need to know that this was defined in the class, so that
18863      friend templates are handled correctly.  */
18864   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18865
18866   /* Add FN to the queue of functions to be parsed later.  */
18867   TREE_VALUE (parser->unparsed_functions_queues)
18868     = tree_cons (NULL_TREE, fn,
18869                  TREE_VALUE (parser->unparsed_functions_queues));
18870
18871   return fn;
18872 }
18873
18874 /* Parse a template-argument-list, as well as the trailing ">" (but
18875    not the opening ">").  See cp_parser_template_argument_list for the
18876    return value.  */
18877
18878 static tree
18879 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18880 {
18881   tree arguments;
18882   tree saved_scope;
18883   tree saved_qualifying_scope;
18884   tree saved_object_scope;
18885   bool saved_greater_than_is_operator_p;
18886   int saved_unevaluated_operand;
18887   int saved_inhibit_evaluation_warnings;
18888
18889   /* [temp.names]
18890
18891      When parsing a template-id, the first non-nested `>' is taken as
18892      the end of the template-argument-list rather than a greater-than
18893      operator.  */
18894   saved_greater_than_is_operator_p
18895     = parser->greater_than_is_operator_p;
18896   parser->greater_than_is_operator_p = false;
18897   /* Parsing the argument list may modify SCOPE, so we save it
18898      here.  */
18899   saved_scope = parser->scope;
18900   saved_qualifying_scope = parser->qualifying_scope;
18901   saved_object_scope = parser->object_scope;
18902   /* We need to evaluate the template arguments, even though this
18903      template-id may be nested within a "sizeof".  */
18904   saved_unevaluated_operand = cp_unevaluated_operand;
18905   cp_unevaluated_operand = 0;
18906   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18907   c_inhibit_evaluation_warnings = 0;
18908   /* Parse the template-argument-list itself.  */
18909   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18910       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18911     arguments = NULL_TREE;
18912   else
18913     arguments = cp_parser_template_argument_list (parser);
18914   /* Look for the `>' that ends the template-argument-list. If we find
18915      a '>>' instead, it's probably just a typo.  */
18916   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18917     {
18918       if (cxx_dialect != cxx98)
18919         {
18920           /* In C++0x, a `>>' in a template argument list or cast
18921              expression is considered to be two separate `>'
18922              tokens. So, change the current token to a `>', but don't
18923              consume it: it will be consumed later when the outer
18924              template argument list (or cast expression) is parsed.
18925              Note that this replacement of `>' for `>>' is necessary
18926              even if we are parsing tentatively: in the tentative
18927              case, after calling
18928              cp_parser_enclosed_template_argument_list we will always
18929              throw away all of the template arguments and the first
18930              closing `>', either because the template argument list
18931              was erroneous or because we are replacing those tokens
18932              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18933              not have been thrown away) is needed either to close an
18934              outer template argument list or to complete a new-style
18935              cast.  */
18936           cp_token *token = cp_lexer_peek_token (parser->lexer);
18937           token->type = CPP_GREATER;
18938         }
18939       else if (!saved_greater_than_is_operator_p)
18940         {
18941           /* If we're in a nested template argument list, the '>>' has
18942             to be a typo for '> >'. We emit the error message, but we
18943             continue parsing and we push a '>' as next token, so that
18944             the argument list will be parsed correctly.  Note that the
18945             global source location is still on the token before the
18946             '>>', so we need to say explicitly where we want it.  */
18947           cp_token *token = cp_lexer_peek_token (parser->lexer);
18948           error_at (token->location, "%<>>%> should be %<> >%> "
18949                     "within a nested template argument list");
18950
18951           token->type = CPP_GREATER;
18952         }
18953       else
18954         {
18955           /* If this is not a nested template argument list, the '>>'
18956             is a typo for '>'. Emit an error message and continue.
18957             Same deal about the token location, but here we can get it
18958             right by consuming the '>>' before issuing the diagnostic.  */
18959           cp_token *token = cp_lexer_consume_token (parser->lexer);
18960           error_at (token->location,
18961                     "spurious %<>>%>, use %<>%> to terminate "
18962                     "a template argument list");
18963         }
18964     }
18965   else
18966     cp_parser_skip_to_end_of_template_parameter_list (parser);
18967   /* The `>' token might be a greater-than operator again now.  */
18968   parser->greater_than_is_operator_p
18969     = saved_greater_than_is_operator_p;
18970   /* Restore the SAVED_SCOPE.  */
18971   parser->scope = saved_scope;
18972   parser->qualifying_scope = saved_qualifying_scope;
18973   parser->object_scope = saved_object_scope;
18974   cp_unevaluated_operand = saved_unevaluated_operand;
18975   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
18976
18977   return arguments;
18978 }
18979
18980 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18981    arguments, or the body of the function have not yet been parsed,
18982    parse them now.  */
18983
18984 static void
18985 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18986 {
18987   /* If this member is a template, get the underlying
18988      FUNCTION_DECL.  */
18989   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18990     member_function = DECL_TEMPLATE_RESULT (member_function);
18991
18992   /* There should not be any class definitions in progress at this
18993      point; the bodies of members are only parsed outside of all class
18994      definitions.  */
18995   gcc_assert (parser->num_classes_being_defined == 0);
18996   /* While we're parsing the member functions we might encounter more
18997      classes.  We want to handle them right away, but we don't want
18998      them getting mixed up with functions that are currently in the
18999      queue.  */
19000   parser->unparsed_functions_queues
19001     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19002
19003   /* Make sure that any template parameters are in scope.  */
19004   maybe_begin_member_template_processing (member_function);
19005
19006   /* If the body of the function has not yet been parsed, parse it
19007      now.  */
19008   if (DECL_PENDING_INLINE_P (member_function))
19009     {
19010       tree function_scope;
19011       cp_token_cache *tokens;
19012
19013       /* The function is no longer pending; we are processing it.  */
19014       tokens = DECL_PENDING_INLINE_INFO (member_function);
19015       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19016       DECL_PENDING_INLINE_P (member_function) = 0;
19017
19018       /* If this is a local class, enter the scope of the containing
19019          function.  */
19020       function_scope = current_function_decl;
19021       if (function_scope)
19022         push_function_context ();
19023
19024       /* Push the body of the function onto the lexer stack.  */
19025       cp_parser_push_lexer_for_tokens (parser, tokens);
19026
19027       /* Let the front end know that we going to be defining this
19028          function.  */
19029       start_preparsed_function (member_function, NULL_TREE,
19030                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19031
19032       /* Don't do access checking if it is a templated function.  */
19033       if (processing_template_decl)
19034         push_deferring_access_checks (dk_no_check);
19035
19036       /* Now, parse the body of the function.  */
19037       cp_parser_function_definition_after_declarator (parser,
19038                                                       /*inline_p=*/true);
19039
19040       if (processing_template_decl)
19041         pop_deferring_access_checks ();
19042
19043       /* Leave the scope of the containing function.  */
19044       if (function_scope)
19045         pop_function_context ();
19046       cp_parser_pop_lexer (parser);
19047     }
19048
19049   /* Remove any template parameters from the symbol table.  */
19050   maybe_end_member_template_processing ();
19051
19052   /* Restore the queue.  */
19053   parser->unparsed_functions_queues
19054     = TREE_CHAIN (parser->unparsed_functions_queues);
19055 }
19056
19057 /* If DECL contains any default args, remember it on the unparsed
19058    functions queue.  */
19059
19060 static void
19061 cp_parser_save_default_args (cp_parser* parser, tree decl)
19062 {
19063   tree probe;
19064
19065   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19066        probe;
19067        probe = TREE_CHAIN (probe))
19068     if (TREE_PURPOSE (probe))
19069       {
19070         TREE_PURPOSE (parser->unparsed_functions_queues)
19071           = tree_cons (current_class_type, decl,
19072                        TREE_PURPOSE (parser->unparsed_functions_queues));
19073         break;
19074       }
19075 }
19076
19077 /* FN is a FUNCTION_DECL which may contains a parameter with an
19078    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19079    assumes that the current scope is the scope in which the default
19080    argument should be processed.  */
19081
19082 static void
19083 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19084 {
19085   bool saved_local_variables_forbidden_p;
19086   tree parm, parmdecl;
19087
19088   /* While we're parsing the default args, we might (due to the
19089      statement expression extension) encounter more classes.  We want
19090      to handle them right away, but we don't want them getting mixed
19091      up with default args that are currently in the queue.  */
19092   parser->unparsed_functions_queues
19093     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19094
19095   /* Local variable names (and the `this' keyword) may not appear
19096      in a default argument.  */
19097   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19098   parser->local_variables_forbidden_p = true;
19099
19100   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19101          parmdecl = DECL_ARGUMENTS (fn);
19102        parm && parm != void_list_node;
19103        parm = TREE_CHAIN (parm),
19104          parmdecl = TREE_CHAIN (parmdecl))
19105     {
19106       cp_token_cache *tokens;
19107       tree default_arg = TREE_PURPOSE (parm);
19108       tree parsed_arg;
19109       VEC(tree,gc) *insts;
19110       tree copy;
19111       unsigned ix;
19112
19113       if (!default_arg)
19114         continue;
19115
19116       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19117         /* This can happen for a friend declaration for a function
19118            already declared with default arguments.  */
19119         continue;
19120
19121        /* Push the saved tokens for the default argument onto the parser's
19122           lexer stack.  */
19123       tokens = DEFARG_TOKENS (default_arg);
19124       cp_parser_push_lexer_for_tokens (parser, tokens);
19125
19126       start_lambda_scope (parmdecl);
19127
19128       /* Parse the assignment-expression.  */
19129       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19130       if (parsed_arg == error_mark_node)
19131         {
19132           cp_parser_pop_lexer (parser);
19133           continue;
19134         }
19135
19136       if (!processing_template_decl)
19137         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19138
19139       TREE_PURPOSE (parm) = parsed_arg;
19140
19141       /* Update any instantiations we've already created.  */
19142       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19143            VEC_iterate (tree, insts, ix, copy); ix++)
19144         TREE_PURPOSE (copy) = parsed_arg;
19145
19146       finish_lambda_scope ();
19147
19148       /* If the token stream has not been completely used up, then
19149          there was extra junk after the end of the default
19150          argument.  */
19151       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19152         cp_parser_error (parser, "expected %<,%>");
19153
19154       /* Revert to the main lexer.  */
19155       cp_parser_pop_lexer (parser);
19156     }
19157
19158   /* Make sure no default arg is missing.  */
19159   check_default_args (fn);
19160
19161   /* Restore the state of local_variables_forbidden_p.  */
19162   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19163
19164   /* Restore the queue.  */
19165   parser->unparsed_functions_queues
19166     = TREE_CHAIN (parser->unparsed_functions_queues);
19167 }
19168
19169 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19170    either a TYPE or an expression, depending on the form of the
19171    input.  The KEYWORD indicates which kind of expression we have
19172    encountered.  */
19173
19174 static tree
19175 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19176 {
19177   tree expr = NULL_TREE;
19178   const char *saved_message;
19179   char *tmp;
19180   bool saved_integral_constant_expression_p;
19181   bool saved_non_integral_constant_expression_p;
19182   bool pack_expansion_p = false;
19183
19184   /* Types cannot be defined in a `sizeof' expression.  Save away the
19185      old message.  */
19186   saved_message = parser->type_definition_forbidden_message;
19187   /* And create the new one.  */
19188   tmp = concat ("types may not be defined in %<",
19189                 IDENTIFIER_POINTER (ridpointers[keyword]),
19190                 "%> expressions", NULL);
19191   parser->type_definition_forbidden_message = tmp;
19192
19193   /* The restrictions on constant-expressions do not apply inside
19194      sizeof expressions.  */
19195   saved_integral_constant_expression_p
19196     = parser->integral_constant_expression_p;
19197   saved_non_integral_constant_expression_p
19198     = parser->non_integral_constant_expression_p;
19199   parser->integral_constant_expression_p = false;
19200
19201   /* If it's a `...', then we are computing the length of a parameter
19202      pack.  */
19203   if (keyword == RID_SIZEOF
19204       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19205     {
19206       /* Consume the `...'.  */
19207       cp_lexer_consume_token (parser->lexer);
19208       maybe_warn_variadic_templates ();
19209
19210       /* Note that this is an expansion.  */
19211       pack_expansion_p = true;
19212     }
19213
19214   /* Do not actually evaluate the expression.  */
19215   ++cp_unevaluated_operand;
19216   ++c_inhibit_evaluation_warnings;
19217   /* If it's a `(', then we might be looking at the type-id
19218      construction.  */
19219   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19220     {
19221       tree type;
19222       bool saved_in_type_id_in_expr_p;
19223
19224       /* We can't be sure yet whether we're looking at a type-id or an
19225          expression.  */
19226       cp_parser_parse_tentatively (parser);
19227       /* Consume the `('.  */
19228       cp_lexer_consume_token (parser->lexer);
19229       /* Parse the type-id.  */
19230       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19231       parser->in_type_id_in_expr_p = true;
19232       type = cp_parser_type_id (parser);
19233       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19234       /* Now, look for the trailing `)'.  */
19235       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19236       /* If all went well, then we're done.  */
19237       if (cp_parser_parse_definitely (parser))
19238         {
19239           cp_decl_specifier_seq decl_specs;
19240
19241           /* Build a trivial decl-specifier-seq.  */
19242           clear_decl_specs (&decl_specs);
19243           decl_specs.type = type;
19244
19245           /* Call grokdeclarator to figure out what type this is.  */
19246           expr = grokdeclarator (NULL,
19247                                  &decl_specs,
19248                                  TYPENAME,
19249                                  /*initialized=*/0,
19250                                  /*attrlist=*/NULL);
19251         }
19252     }
19253
19254   /* If the type-id production did not work out, then we must be
19255      looking at the unary-expression production.  */
19256   if (!expr)
19257     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19258                                        /*cast_p=*/false, NULL);
19259
19260   if (pack_expansion_p)
19261     /* Build a pack expansion. */
19262     expr = make_pack_expansion (expr);
19263
19264   /* Go back to evaluating expressions.  */
19265   --cp_unevaluated_operand;
19266   --c_inhibit_evaluation_warnings;
19267
19268   /* Free the message we created.  */
19269   free (tmp);
19270   /* And restore the old one.  */
19271   parser->type_definition_forbidden_message = saved_message;
19272   parser->integral_constant_expression_p
19273     = saved_integral_constant_expression_p;
19274   parser->non_integral_constant_expression_p
19275     = saved_non_integral_constant_expression_p;
19276
19277   return expr;
19278 }
19279
19280 /* If the current declaration has no declarator, return true.  */
19281
19282 static bool
19283 cp_parser_declares_only_class_p (cp_parser *parser)
19284 {
19285   /* If the next token is a `;' or a `,' then there is no
19286      declarator.  */
19287   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19288           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19289 }
19290
19291 /* Update the DECL_SPECS to reflect the storage class indicated by
19292    KEYWORD.  */
19293
19294 static void
19295 cp_parser_set_storage_class (cp_parser *parser,
19296                              cp_decl_specifier_seq *decl_specs,
19297                              enum rid keyword,
19298                              location_t location)
19299 {
19300   cp_storage_class storage_class;
19301
19302   if (parser->in_unbraced_linkage_specification_p)
19303     {
19304       error_at (location, "invalid use of %qD in linkage specification",
19305                 ridpointers[keyword]);
19306       return;
19307     }
19308   else if (decl_specs->storage_class != sc_none)
19309     {
19310       decl_specs->conflicting_specifiers_p = true;
19311       return;
19312     }
19313
19314   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19315       && decl_specs->specs[(int) ds_thread])
19316     {
19317       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19318       decl_specs->specs[(int) ds_thread] = 0;
19319     }
19320
19321   switch (keyword)
19322     {
19323     case RID_AUTO:
19324       storage_class = sc_auto;
19325       break;
19326     case RID_REGISTER:
19327       storage_class = sc_register;
19328       break;
19329     case RID_STATIC:
19330       storage_class = sc_static;
19331       break;
19332     case RID_EXTERN:
19333       storage_class = sc_extern;
19334       break;
19335     case RID_MUTABLE:
19336       storage_class = sc_mutable;
19337       break;
19338     default:
19339       gcc_unreachable ();
19340     }
19341   decl_specs->storage_class = storage_class;
19342
19343   /* A storage class specifier cannot be applied alongside a typedef 
19344      specifier. If there is a typedef specifier present then set 
19345      conflicting_specifiers_p which will trigger an error later
19346      on in grokdeclarator. */
19347   if (decl_specs->specs[(int)ds_typedef])
19348     decl_specs->conflicting_specifiers_p = true;
19349 }
19350
19351 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19352    is true, the type is a user-defined type; otherwise it is a
19353    built-in type specified by a keyword.  */
19354
19355 static void
19356 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19357                               tree type_spec,
19358                               location_t location,
19359                               bool user_defined_p)
19360 {
19361   decl_specs->any_specifiers_p = true;
19362
19363   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19364      (with, for example, in "typedef int wchar_t;") we remember that
19365      this is what happened.  In system headers, we ignore these
19366      declarations so that G++ can work with system headers that are not
19367      C++-safe.  */
19368   if (decl_specs->specs[(int) ds_typedef]
19369       && !user_defined_p
19370       && (type_spec == boolean_type_node
19371           || type_spec == char16_type_node
19372           || type_spec == char32_type_node
19373           || type_spec == wchar_type_node)
19374       && (decl_specs->type
19375           || decl_specs->specs[(int) ds_long]
19376           || decl_specs->specs[(int) ds_short]
19377           || decl_specs->specs[(int) ds_unsigned]
19378           || decl_specs->specs[(int) ds_signed]))
19379     {
19380       decl_specs->redefined_builtin_type = type_spec;
19381       if (!decl_specs->type)
19382         {
19383           decl_specs->type = type_spec;
19384           decl_specs->user_defined_type_p = false;
19385           decl_specs->type_location = location;
19386         }
19387     }
19388   else if (decl_specs->type)
19389     decl_specs->multiple_types_p = true;
19390   else
19391     {
19392       decl_specs->type = type_spec;
19393       decl_specs->user_defined_type_p = user_defined_p;
19394       decl_specs->redefined_builtin_type = NULL_TREE;
19395       decl_specs->type_location = location;
19396     }
19397 }
19398
19399 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19400    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19401
19402 static bool
19403 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19404 {
19405   return decl_specifiers->specs[(int) ds_friend] != 0;
19406 }
19407
19408 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19409    issue an error message indicating that TOKEN_DESC was expected.
19410
19411    Returns the token consumed, if the token had the appropriate type.
19412    Otherwise, returns NULL.  */
19413
19414 static cp_token *
19415 cp_parser_require (cp_parser* parser,
19416                    enum cpp_ttype type,
19417                    const char* token_desc)
19418 {
19419   if (cp_lexer_next_token_is (parser->lexer, type))
19420     return cp_lexer_consume_token (parser->lexer);
19421   else
19422     {
19423       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19424       if (!cp_parser_simulate_error (parser))
19425         {
19426           char *message = concat ("expected ", token_desc, NULL);
19427           cp_parser_error (parser, message);
19428           free (message);
19429         }
19430       return NULL;
19431     }
19432 }
19433
19434 /* An error message is produced if the next token is not '>'.
19435    All further tokens are skipped until the desired token is
19436    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19437
19438 static void
19439 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19440 {
19441   /* Current level of '< ... >'.  */
19442   unsigned level = 0;
19443   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19444   unsigned nesting_depth = 0;
19445
19446   /* Are we ready, yet?  If not, issue error message.  */
19447   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19448     return;
19449
19450   /* Skip tokens until the desired token is found.  */
19451   while (true)
19452     {
19453       /* Peek at the next token.  */
19454       switch (cp_lexer_peek_token (parser->lexer)->type)
19455         {
19456         case CPP_LESS:
19457           if (!nesting_depth)
19458             ++level;
19459           break;
19460
19461         case CPP_RSHIFT:
19462           if (cxx_dialect == cxx98)
19463             /* C++0x views the `>>' operator as two `>' tokens, but
19464                C++98 does not. */
19465             break;
19466           else if (!nesting_depth && level-- == 0)
19467             {
19468               /* We've hit a `>>' where the first `>' closes the
19469                  template argument list, and the second `>' is
19470                  spurious.  Just consume the `>>' and stop; we've
19471                  already produced at least one error.  */
19472               cp_lexer_consume_token (parser->lexer);
19473               return;
19474             }
19475           /* Fall through for C++0x, so we handle the second `>' in
19476              the `>>'.  */
19477
19478         case CPP_GREATER:
19479           if (!nesting_depth && level-- == 0)
19480             {
19481               /* We've reached the token we want, consume it and stop.  */
19482               cp_lexer_consume_token (parser->lexer);
19483               return;
19484             }
19485           break;
19486
19487         case CPP_OPEN_PAREN:
19488         case CPP_OPEN_SQUARE:
19489           ++nesting_depth;
19490           break;
19491
19492         case CPP_CLOSE_PAREN:
19493         case CPP_CLOSE_SQUARE:
19494           if (nesting_depth-- == 0)
19495             return;
19496           break;
19497
19498         case CPP_EOF:
19499         case CPP_PRAGMA_EOL:
19500         case CPP_SEMICOLON:
19501         case CPP_OPEN_BRACE:
19502         case CPP_CLOSE_BRACE:
19503           /* The '>' was probably forgotten, don't look further.  */
19504           return;
19505
19506         default:
19507           break;
19508         }
19509
19510       /* Consume this token.  */
19511       cp_lexer_consume_token (parser->lexer);
19512     }
19513 }
19514
19515 /* If the next token is the indicated keyword, consume it.  Otherwise,
19516    issue an error message indicating that TOKEN_DESC was expected.
19517
19518    Returns the token consumed, if the token had the appropriate type.
19519    Otherwise, returns NULL.  */
19520
19521 static cp_token *
19522 cp_parser_require_keyword (cp_parser* parser,
19523                            enum rid keyword,
19524                            const char* token_desc)
19525 {
19526   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19527
19528   if (token && token->keyword != keyword)
19529     {
19530       dyn_string_t error_msg;
19531
19532       /* Format the error message.  */
19533       error_msg = dyn_string_new (0);
19534       dyn_string_append_cstr (error_msg, "expected ");
19535       dyn_string_append_cstr (error_msg, token_desc);
19536       cp_parser_error (parser, error_msg->s);
19537       dyn_string_delete (error_msg);
19538       return NULL;
19539     }
19540
19541   return token;
19542 }
19543
19544 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19545    function-definition.  */
19546
19547 static bool
19548 cp_parser_token_starts_function_definition_p (cp_token* token)
19549 {
19550   return (/* An ordinary function-body begins with an `{'.  */
19551           token->type == CPP_OPEN_BRACE
19552           /* A ctor-initializer begins with a `:'.  */
19553           || token->type == CPP_COLON
19554           /* A function-try-block begins with `try'.  */
19555           || token->keyword == RID_TRY
19556           /* The named return value extension begins with `return'.  */
19557           || token->keyword == RID_RETURN);
19558 }
19559
19560 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19561    definition.  */
19562
19563 static bool
19564 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19565 {
19566   cp_token *token;
19567
19568   token = cp_lexer_peek_token (parser->lexer);
19569   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19570 }
19571
19572 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19573    C++0x) ending a template-argument.  */
19574
19575 static bool
19576 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19577 {
19578   cp_token *token;
19579
19580   token = cp_lexer_peek_token (parser->lexer);
19581   return (token->type == CPP_COMMA 
19582           || token->type == CPP_GREATER
19583           || token->type == CPP_ELLIPSIS
19584           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19585 }
19586
19587 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19588    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19589
19590 static bool
19591 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19592                                                      size_t n)
19593 {
19594   cp_token *token;
19595
19596   token = cp_lexer_peek_nth_token (parser->lexer, n);
19597   if (token->type == CPP_LESS)
19598     return true;
19599   /* Check for the sequence `<::' in the original code. It would be lexed as
19600      `[:', where `[' is a digraph, and there is no whitespace before
19601      `:'.  */
19602   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19603     {
19604       cp_token *token2;
19605       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19606       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19607         return true;
19608     }
19609   return false;
19610 }
19611
19612 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19613    or none_type otherwise.  */
19614
19615 static enum tag_types
19616 cp_parser_token_is_class_key (cp_token* token)
19617 {
19618   switch (token->keyword)
19619     {
19620     case RID_CLASS:
19621       return class_type;
19622     case RID_STRUCT:
19623       return record_type;
19624     case RID_UNION:
19625       return union_type;
19626
19627     default:
19628       return none_type;
19629     }
19630 }
19631
19632 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19633
19634 static void
19635 cp_parser_check_class_key (enum tag_types class_key, tree type)
19636 {
19637   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19638     permerror (input_location, "%qs tag used in naming %q#T",
19639             class_key == union_type ? "union"
19640              : class_key == record_type ? "struct" : "class",
19641              type);
19642 }
19643
19644 /* Issue an error message if DECL is redeclared with different
19645    access than its original declaration [class.access.spec/3].
19646    This applies to nested classes and nested class templates.
19647    [class.mem/1].  */
19648
19649 static void
19650 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19651 {
19652   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19653     return;
19654
19655   if ((TREE_PRIVATE (decl)
19656        != (current_access_specifier == access_private_node))
19657       || (TREE_PROTECTED (decl)
19658           != (current_access_specifier == access_protected_node)))
19659     error_at (location, "%qD redeclared with different access", decl);
19660 }
19661
19662 /* Look for the `template' keyword, as a syntactic disambiguator.
19663    Return TRUE iff it is present, in which case it will be
19664    consumed.  */
19665
19666 static bool
19667 cp_parser_optional_template_keyword (cp_parser *parser)
19668 {
19669   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19670     {
19671       /* The `template' keyword can only be used within templates;
19672          outside templates the parser can always figure out what is a
19673          template and what is not.  */
19674       if (!processing_template_decl)
19675         {
19676           cp_token *token = cp_lexer_peek_token (parser->lexer);
19677           error_at (token->location,
19678                     "%<template%> (as a disambiguator) is only allowed "
19679                     "within templates");
19680           /* If this part of the token stream is rescanned, the same
19681              error message would be generated.  So, we purge the token
19682              from the stream.  */
19683           cp_lexer_purge_token (parser->lexer);
19684           return false;
19685         }
19686       else
19687         {
19688           /* Consume the `template' keyword.  */
19689           cp_lexer_consume_token (parser->lexer);
19690           return true;
19691         }
19692     }
19693
19694   return false;
19695 }
19696
19697 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19698    set PARSER->SCOPE, and perform other related actions.  */
19699
19700 static void
19701 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19702 {
19703   int i;
19704   struct tree_check *check_value;
19705   deferred_access_check *chk;
19706   VEC (deferred_access_check,gc) *checks;
19707
19708   /* Get the stored value.  */
19709   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19710   /* Perform any access checks that were deferred.  */
19711   checks = check_value->checks;
19712   if (checks)
19713     {
19714       for (i = 0 ;
19715            VEC_iterate (deferred_access_check, checks, i, chk) ;
19716            ++i)
19717         {
19718           perform_or_defer_access_check (chk->binfo,
19719                                          chk->decl,
19720                                          chk->diag_decl);
19721         }
19722     }
19723   /* Set the scope from the stored value.  */
19724   parser->scope = check_value->value;
19725   parser->qualifying_scope = check_value->qualifying_scope;
19726   parser->object_scope = NULL_TREE;
19727 }
19728
19729 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19730    encounter the end of a block before what we were looking for.  */
19731
19732 static bool
19733 cp_parser_cache_group (cp_parser *parser,
19734                        enum cpp_ttype end,
19735                        unsigned depth)
19736 {
19737   while (true)
19738     {
19739       cp_token *token = cp_lexer_peek_token (parser->lexer);
19740
19741       /* Abort a parenthesized expression if we encounter a semicolon.  */
19742       if ((end == CPP_CLOSE_PAREN || depth == 0)
19743           && token->type == CPP_SEMICOLON)
19744         return true;
19745       /* If we've reached the end of the file, stop.  */
19746       if (token->type == CPP_EOF
19747           || (end != CPP_PRAGMA_EOL
19748               && token->type == CPP_PRAGMA_EOL))
19749         return true;
19750       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19751         /* We've hit the end of an enclosing block, so there's been some
19752            kind of syntax error.  */
19753         return true;
19754
19755       /* Consume the token.  */
19756       cp_lexer_consume_token (parser->lexer);
19757       /* See if it starts a new group.  */
19758       if (token->type == CPP_OPEN_BRACE)
19759         {
19760           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19761           /* In theory this should probably check end == '}', but
19762              cp_parser_save_member_function_body needs it to exit
19763              after either '}' or ')' when called with ')'.  */
19764           if (depth == 0)
19765             return false;
19766         }
19767       else if (token->type == CPP_OPEN_PAREN)
19768         {
19769           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19770           if (depth == 0 && end == CPP_CLOSE_PAREN)
19771             return false;
19772         }
19773       else if (token->type == CPP_PRAGMA)
19774         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19775       else if (token->type == end)
19776         return false;
19777     }
19778 }
19779
19780 /* Begin parsing tentatively.  We always save tokens while parsing
19781    tentatively so that if the tentative parsing fails we can restore the
19782    tokens.  */
19783
19784 static void
19785 cp_parser_parse_tentatively (cp_parser* parser)
19786 {
19787   /* Enter a new parsing context.  */
19788   parser->context = cp_parser_context_new (parser->context);
19789   /* Begin saving tokens.  */
19790   cp_lexer_save_tokens (parser->lexer);
19791   /* In order to avoid repetitive access control error messages,
19792      access checks are queued up until we are no longer parsing
19793      tentatively.  */
19794   push_deferring_access_checks (dk_deferred);
19795 }
19796
19797 /* Commit to the currently active tentative parse.  */
19798
19799 static void
19800 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19801 {
19802   cp_parser_context *context;
19803   cp_lexer *lexer;
19804
19805   /* Mark all of the levels as committed.  */
19806   lexer = parser->lexer;
19807   for (context = parser->context; context->next; context = context->next)
19808     {
19809       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19810         break;
19811       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19812       while (!cp_lexer_saving_tokens (lexer))
19813         lexer = lexer->next;
19814       cp_lexer_commit_tokens (lexer);
19815     }
19816 }
19817
19818 /* Abort the currently active tentative parse.  All consumed tokens
19819    will be rolled back, and no diagnostics will be issued.  */
19820
19821 static void
19822 cp_parser_abort_tentative_parse (cp_parser* parser)
19823 {
19824   cp_parser_simulate_error (parser);
19825   /* Now, pretend that we want to see if the construct was
19826      successfully parsed.  */
19827   cp_parser_parse_definitely (parser);
19828 }
19829
19830 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19831    token stream.  Otherwise, commit to the tokens we have consumed.
19832    Returns true if no error occurred; false otherwise.  */
19833
19834 static bool
19835 cp_parser_parse_definitely (cp_parser* parser)
19836 {
19837   bool error_occurred;
19838   cp_parser_context *context;
19839
19840   /* Remember whether or not an error occurred, since we are about to
19841      destroy that information.  */
19842   error_occurred = cp_parser_error_occurred (parser);
19843   /* Remove the topmost context from the stack.  */
19844   context = parser->context;
19845   parser->context = context->next;
19846   /* If no parse errors occurred, commit to the tentative parse.  */
19847   if (!error_occurred)
19848     {
19849       /* Commit to the tokens read tentatively, unless that was
19850          already done.  */
19851       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19852         cp_lexer_commit_tokens (parser->lexer);
19853
19854       pop_to_parent_deferring_access_checks ();
19855     }
19856   /* Otherwise, if errors occurred, roll back our state so that things
19857      are just as they were before we began the tentative parse.  */
19858   else
19859     {
19860       cp_lexer_rollback_tokens (parser->lexer);
19861       pop_deferring_access_checks ();
19862     }
19863   /* Add the context to the front of the free list.  */
19864   context->next = cp_parser_context_free_list;
19865   cp_parser_context_free_list = context;
19866
19867   return !error_occurred;
19868 }
19869
19870 /* Returns true if we are parsing tentatively and are not committed to
19871    this tentative parse.  */
19872
19873 static bool
19874 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19875 {
19876   return (cp_parser_parsing_tentatively (parser)
19877           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19878 }
19879
19880 /* Returns nonzero iff an error has occurred during the most recent
19881    tentative parse.  */
19882
19883 static bool
19884 cp_parser_error_occurred (cp_parser* parser)
19885 {
19886   return (cp_parser_parsing_tentatively (parser)
19887           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19888 }
19889
19890 /* Returns nonzero if GNU extensions are allowed.  */
19891
19892 static bool
19893 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19894 {
19895   return parser->allow_gnu_extensions_p;
19896 }
19897 \f
19898 /* Objective-C++ Productions */
19899
19900
19901 /* Parse an Objective-C expression, which feeds into a primary-expression
19902    above.
19903
19904    objc-expression:
19905      objc-message-expression
19906      objc-string-literal
19907      objc-encode-expression
19908      objc-protocol-expression
19909      objc-selector-expression
19910
19911   Returns a tree representation of the expression.  */
19912
19913 static tree
19914 cp_parser_objc_expression (cp_parser* parser)
19915 {
19916   /* Try to figure out what kind of declaration is present.  */
19917   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19918
19919   switch (kwd->type)
19920     {
19921     case CPP_OPEN_SQUARE:
19922       return cp_parser_objc_message_expression (parser);
19923
19924     case CPP_OBJC_STRING:
19925       kwd = cp_lexer_consume_token (parser->lexer);
19926       return objc_build_string_object (kwd->u.value);
19927
19928     case CPP_KEYWORD:
19929       switch (kwd->keyword)
19930         {
19931         case RID_AT_ENCODE:
19932           return cp_parser_objc_encode_expression (parser);
19933
19934         case RID_AT_PROTOCOL:
19935           return cp_parser_objc_protocol_expression (parser);
19936
19937         case RID_AT_SELECTOR:
19938           return cp_parser_objc_selector_expression (parser);
19939
19940         default:
19941           break;
19942         }
19943     default:
19944       error_at (kwd->location,
19945                 "misplaced %<@%D%> Objective-C++ construct",
19946                 kwd->u.value);
19947       cp_parser_skip_to_end_of_block_or_statement (parser);
19948     }
19949
19950   return error_mark_node;
19951 }
19952
19953 /* Parse an Objective-C message expression.
19954
19955    objc-message-expression:
19956      [ objc-message-receiver objc-message-args ]
19957
19958    Returns a representation of an Objective-C message.  */
19959
19960 static tree
19961 cp_parser_objc_message_expression (cp_parser* parser)
19962 {
19963   tree receiver, messageargs;
19964
19965   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19966   receiver = cp_parser_objc_message_receiver (parser);
19967   messageargs = cp_parser_objc_message_args (parser);
19968   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19969
19970   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19971 }
19972
19973 /* Parse an objc-message-receiver.
19974
19975    objc-message-receiver:
19976      expression
19977      simple-type-specifier
19978
19979   Returns a representation of the type or expression.  */
19980
19981 static tree
19982 cp_parser_objc_message_receiver (cp_parser* parser)
19983 {
19984   tree rcv;
19985
19986   /* An Objective-C message receiver may be either (1) a type
19987      or (2) an expression.  */
19988   cp_parser_parse_tentatively (parser);
19989   rcv = cp_parser_expression (parser, false, NULL);
19990
19991   if (cp_parser_parse_definitely (parser))
19992     return rcv;
19993
19994   rcv = cp_parser_simple_type_specifier (parser,
19995                                          /*decl_specs=*/NULL,
19996                                          CP_PARSER_FLAGS_NONE);
19997
19998   return objc_get_class_reference (rcv);
19999 }
20000
20001 /* Parse the arguments and selectors comprising an Objective-C message.
20002
20003    objc-message-args:
20004      objc-selector
20005      objc-selector-args
20006      objc-selector-args , objc-comma-args
20007
20008    objc-selector-args:
20009      objc-selector [opt] : assignment-expression
20010      objc-selector-args objc-selector [opt] : assignment-expression
20011
20012    objc-comma-args:
20013      assignment-expression
20014      objc-comma-args , assignment-expression
20015
20016    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20017    selector arguments and TREE_VALUE containing a list of comma
20018    arguments.  */
20019
20020 static tree
20021 cp_parser_objc_message_args (cp_parser* parser)
20022 {
20023   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20024   bool maybe_unary_selector_p = true;
20025   cp_token *token = cp_lexer_peek_token (parser->lexer);
20026
20027   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20028     {
20029       tree selector = NULL_TREE, arg;
20030
20031       if (token->type != CPP_COLON)
20032         selector = cp_parser_objc_selector (parser);
20033
20034       /* Detect if we have a unary selector.  */
20035       if (maybe_unary_selector_p
20036           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20037         return build_tree_list (selector, NULL_TREE);
20038
20039       maybe_unary_selector_p = false;
20040       cp_parser_require (parser, CPP_COLON, "%<:%>");
20041       arg = cp_parser_assignment_expression (parser, false, NULL);
20042
20043       sel_args
20044         = chainon (sel_args,
20045                    build_tree_list (selector, arg));
20046
20047       token = cp_lexer_peek_token (parser->lexer);
20048     }
20049
20050   /* Handle non-selector arguments, if any. */
20051   while (token->type == CPP_COMMA)
20052     {
20053       tree arg;
20054
20055       cp_lexer_consume_token (parser->lexer);
20056       arg = cp_parser_assignment_expression (parser, false, NULL);
20057
20058       addl_args
20059         = chainon (addl_args,
20060                    build_tree_list (NULL_TREE, arg));
20061
20062       token = cp_lexer_peek_token (parser->lexer);
20063     }
20064
20065   return build_tree_list (sel_args, addl_args);
20066 }
20067
20068 /* Parse an Objective-C encode expression.
20069
20070    objc-encode-expression:
20071      @encode objc-typename
20072
20073    Returns an encoded representation of the type argument.  */
20074
20075 static tree
20076 cp_parser_objc_encode_expression (cp_parser* parser)
20077 {
20078   tree type;
20079   cp_token *token;
20080
20081   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20082   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20083   token = cp_lexer_peek_token (parser->lexer);
20084   type = complete_type (cp_parser_type_id (parser));
20085   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20086
20087   if (!type)
20088     {
20089       error_at (token->location, 
20090                 "%<@encode%> must specify a type as an argument");
20091       return error_mark_node;
20092     }
20093
20094   return objc_build_encode_expr (type);
20095 }
20096
20097 /* Parse an Objective-C @defs expression.  */
20098
20099 static tree
20100 cp_parser_objc_defs_expression (cp_parser *parser)
20101 {
20102   tree name;
20103
20104   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20105   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20106   name = cp_parser_identifier (parser);
20107   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20108
20109   return objc_get_class_ivars (name);
20110 }
20111
20112 /* Parse an Objective-C protocol expression.
20113
20114   objc-protocol-expression:
20115     @protocol ( identifier )
20116
20117   Returns a representation of the protocol expression.  */
20118
20119 static tree
20120 cp_parser_objc_protocol_expression (cp_parser* parser)
20121 {
20122   tree proto;
20123
20124   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20125   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20126   proto = cp_parser_identifier (parser);
20127   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20128
20129   return objc_build_protocol_expr (proto);
20130 }
20131
20132 /* Parse an Objective-C selector expression.
20133
20134    objc-selector-expression:
20135      @selector ( objc-method-signature )
20136
20137    objc-method-signature:
20138      objc-selector
20139      objc-selector-seq
20140
20141    objc-selector-seq:
20142      objc-selector :
20143      objc-selector-seq objc-selector :
20144
20145   Returns a representation of the method selector.  */
20146
20147 static tree
20148 cp_parser_objc_selector_expression (cp_parser* parser)
20149 {
20150   tree sel_seq = NULL_TREE;
20151   bool maybe_unary_selector_p = true;
20152   cp_token *token;
20153   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20154
20155   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20156   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20157   token = cp_lexer_peek_token (parser->lexer);
20158
20159   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20160          || token->type == CPP_SCOPE)
20161     {
20162       tree selector = NULL_TREE;
20163
20164       if (token->type != CPP_COLON
20165           || token->type == CPP_SCOPE)
20166         selector = cp_parser_objc_selector (parser);
20167
20168       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20169           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20170         {
20171           /* Detect if we have a unary selector.  */
20172           if (maybe_unary_selector_p)
20173             {
20174               sel_seq = selector;
20175               goto finish_selector;
20176             }
20177           else
20178             {
20179               cp_parser_error (parser, "expected %<:%>");
20180             }
20181         }
20182       maybe_unary_selector_p = false;
20183       token = cp_lexer_consume_token (parser->lexer);
20184
20185       if (token->type == CPP_SCOPE)
20186         {
20187           sel_seq
20188             = chainon (sel_seq,
20189                        build_tree_list (selector, NULL_TREE));
20190           sel_seq
20191             = chainon (sel_seq,
20192                        build_tree_list (NULL_TREE, NULL_TREE));
20193         }
20194       else
20195         sel_seq
20196           = chainon (sel_seq,
20197                      build_tree_list (selector, NULL_TREE));
20198
20199       token = cp_lexer_peek_token (parser->lexer);
20200     }
20201
20202  finish_selector:
20203   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20204
20205   return objc_build_selector_expr (loc, sel_seq);
20206 }
20207
20208 /* Parse a list of identifiers.
20209
20210    objc-identifier-list:
20211      identifier
20212      objc-identifier-list , identifier
20213
20214    Returns a TREE_LIST of identifier nodes.  */
20215
20216 static tree
20217 cp_parser_objc_identifier_list (cp_parser* parser)
20218 {
20219   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20220   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20221
20222   while (sep->type == CPP_COMMA)
20223     {
20224       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20225       list = chainon (list,
20226                       build_tree_list (NULL_TREE,
20227                                        cp_parser_identifier (parser)));
20228       sep = cp_lexer_peek_token (parser->lexer);
20229     }
20230
20231   return list;
20232 }
20233
20234 /* Parse an Objective-C alias declaration.
20235
20236    objc-alias-declaration:
20237      @compatibility_alias identifier identifier ;
20238
20239    This function registers the alias mapping with the Objective-C front end.
20240    It returns nothing.  */
20241
20242 static void
20243 cp_parser_objc_alias_declaration (cp_parser* parser)
20244 {
20245   tree alias, orig;
20246
20247   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20248   alias = cp_parser_identifier (parser);
20249   orig = cp_parser_identifier (parser);
20250   objc_declare_alias (alias, orig);
20251   cp_parser_consume_semicolon_at_end_of_statement (parser);
20252 }
20253
20254 /* Parse an Objective-C class forward-declaration.
20255
20256    objc-class-declaration:
20257      @class objc-identifier-list ;
20258
20259    The function registers the forward declarations with the Objective-C
20260    front end.  It returns nothing.  */
20261
20262 static void
20263 cp_parser_objc_class_declaration (cp_parser* parser)
20264 {
20265   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20266   objc_declare_class (cp_parser_objc_identifier_list (parser));
20267   cp_parser_consume_semicolon_at_end_of_statement (parser);
20268 }
20269
20270 /* Parse a list of Objective-C protocol references.
20271
20272    objc-protocol-refs-opt:
20273      objc-protocol-refs [opt]
20274
20275    objc-protocol-refs:
20276      < objc-identifier-list >
20277
20278    Returns a TREE_LIST of identifiers, if any.  */
20279
20280 static tree
20281 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20282 {
20283   tree protorefs = NULL_TREE;
20284
20285   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20286     {
20287       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20288       protorefs = cp_parser_objc_identifier_list (parser);
20289       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20290     }
20291
20292   return protorefs;
20293 }
20294
20295 /* Parse a Objective-C visibility specification.  */
20296
20297 static void
20298 cp_parser_objc_visibility_spec (cp_parser* parser)
20299 {
20300   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20301
20302   switch (vis->keyword)
20303     {
20304     case RID_AT_PRIVATE:
20305       objc_set_visibility (2);
20306       break;
20307     case RID_AT_PROTECTED:
20308       objc_set_visibility (0);
20309       break;
20310     case RID_AT_PUBLIC:
20311       objc_set_visibility (1);
20312       break;
20313     default:
20314       return;
20315     }
20316
20317   /* Eat '@private'/'@protected'/'@public'.  */
20318   cp_lexer_consume_token (parser->lexer);
20319 }
20320
20321 /* Parse an Objective-C method type.  */
20322
20323 static void
20324 cp_parser_objc_method_type (cp_parser* parser)
20325 {
20326   objc_set_method_type
20327    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20328     ? PLUS_EXPR
20329     : MINUS_EXPR);
20330 }
20331
20332 /* Parse an Objective-C protocol qualifier.  */
20333
20334 static tree
20335 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20336 {
20337   tree quals = NULL_TREE, node;
20338   cp_token *token = cp_lexer_peek_token (parser->lexer);
20339
20340   node = token->u.value;
20341
20342   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20343          && (node == ridpointers [(int) RID_IN]
20344              || node == ridpointers [(int) RID_OUT]
20345              || node == ridpointers [(int) RID_INOUT]
20346              || node == ridpointers [(int) RID_BYCOPY]
20347              || node == ridpointers [(int) RID_BYREF]
20348              || node == ridpointers [(int) RID_ONEWAY]))
20349     {
20350       quals = tree_cons (NULL_TREE, node, quals);
20351       cp_lexer_consume_token (parser->lexer);
20352       token = cp_lexer_peek_token (parser->lexer);
20353       node = token->u.value;
20354     }
20355
20356   return quals;
20357 }
20358
20359 /* Parse an Objective-C typename.  */
20360
20361 static tree
20362 cp_parser_objc_typename (cp_parser* parser)
20363 {
20364   tree type_name = NULL_TREE;
20365
20366   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20367     {
20368       tree proto_quals, cp_type = NULL_TREE;
20369
20370       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20371       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20372
20373       /* An ObjC type name may consist of just protocol qualifiers, in which
20374          case the type shall default to 'id'.  */
20375       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20376         cp_type = cp_parser_type_id (parser);
20377
20378       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20379       type_name = build_tree_list (proto_quals, cp_type);
20380     }
20381
20382   return type_name;
20383 }
20384
20385 /* Check to see if TYPE refers to an Objective-C selector name.  */
20386
20387 static bool
20388 cp_parser_objc_selector_p (enum cpp_ttype type)
20389 {
20390   return (type == CPP_NAME || type == CPP_KEYWORD
20391           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20392           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20393           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20394           || type == CPP_XOR || type == CPP_XOR_EQ);
20395 }
20396
20397 /* Parse an Objective-C selector.  */
20398
20399 static tree
20400 cp_parser_objc_selector (cp_parser* parser)
20401 {
20402   cp_token *token = cp_lexer_consume_token (parser->lexer);
20403
20404   if (!cp_parser_objc_selector_p (token->type))
20405     {
20406       error_at (token->location, "invalid Objective-C++ selector name");
20407       return error_mark_node;
20408     }
20409
20410   /* C++ operator names are allowed to appear in ObjC selectors.  */
20411   switch (token->type)
20412     {
20413     case CPP_AND_AND: return get_identifier ("and");
20414     case CPP_AND_EQ: return get_identifier ("and_eq");
20415     case CPP_AND: return get_identifier ("bitand");
20416     case CPP_OR: return get_identifier ("bitor");
20417     case CPP_COMPL: return get_identifier ("compl");
20418     case CPP_NOT: return get_identifier ("not");
20419     case CPP_NOT_EQ: return get_identifier ("not_eq");
20420     case CPP_OR_OR: return get_identifier ("or");
20421     case CPP_OR_EQ: return get_identifier ("or_eq");
20422     case CPP_XOR: return get_identifier ("xor");
20423     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20424     default: return token->u.value;
20425     }
20426 }
20427
20428 /* Parse an Objective-C params list.  */
20429
20430 static tree
20431 cp_parser_objc_method_keyword_params (cp_parser* parser)
20432 {
20433   tree params = NULL_TREE;
20434   bool maybe_unary_selector_p = true;
20435   cp_token *token = cp_lexer_peek_token (parser->lexer);
20436
20437   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20438     {
20439       tree selector = NULL_TREE, type_name, identifier;
20440
20441       if (token->type != CPP_COLON)
20442         selector = cp_parser_objc_selector (parser);
20443
20444       /* Detect if we have a unary selector.  */
20445       if (maybe_unary_selector_p
20446           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20447         return selector;
20448
20449       maybe_unary_selector_p = false;
20450       cp_parser_require (parser, CPP_COLON, "%<:%>");
20451       type_name = cp_parser_objc_typename (parser);
20452       identifier = cp_parser_identifier (parser);
20453
20454       params
20455         = chainon (params,
20456                    objc_build_keyword_decl (selector,
20457                                             type_name,
20458                                             identifier));
20459
20460       token = cp_lexer_peek_token (parser->lexer);
20461     }
20462
20463   return params;
20464 }
20465
20466 /* Parse the non-keyword Objective-C params.  */
20467
20468 static tree
20469 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20470 {
20471   tree params = make_node (TREE_LIST);
20472   cp_token *token = cp_lexer_peek_token (parser->lexer);
20473   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20474
20475   while (token->type == CPP_COMMA)
20476     {
20477       cp_parameter_declarator *parmdecl;
20478       tree parm;
20479
20480       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20481       token = cp_lexer_peek_token (parser->lexer);
20482
20483       if (token->type == CPP_ELLIPSIS)
20484         {
20485           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20486           *ellipsisp = true;
20487           break;
20488         }
20489
20490       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20491       parm = grokdeclarator (parmdecl->declarator,
20492                              &parmdecl->decl_specifiers,
20493                              PARM, /*initialized=*/0,
20494                              /*attrlist=*/NULL);
20495
20496       chainon (params, build_tree_list (NULL_TREE, parm));
20497       token = cp_lexer_peek_token (parser->lexer);
20498     }
20499
20500   return params;
20501 }
20502
20503 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20504
20505 static void
20506 cp_parser_objc_interstitial_code (cp_parser* parser)
20507 {
20508   cp_token *token = cp_lexer_peek_token (parser->lexer);
20509
20510   /* If the next token is `extern' and the following token is a string
20511      literal, then we have a linkage specification.  */
20512   if (token->keyword == RID_EXTERN
20513       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20514     cp_parser_linkage_specification (parser);
20515   /* Handle #pragma, if any.  */
20516   else if (token->type == CPP_PRAGMA)
20517     cp_parser_pragma (parser, pragma_external);
20518   /* Allow stray semicolons.  */
20519   else if (token->type == CPP_SEMICOLON)
20520     cp_lexer_consume_token (parser->lexer);
20521   /* Finally, try to parse a block-declaration, or a function-definition.  */
20522   else
20523     cp_parser_block_declaration (parser, /*statement_p=*/false);
20524 }
20525
20526 /* Parse a method signature.  */
20527
20528 static tree
20529 cp_parser_objc_method_signature (cp_parser* parser)
20530 {
20531   tree rettype, kwdparms, optparms;
20532   bool ellipsis = false;
20533
20534   cp_parser_objc_method_type (parser);
20535   rettype = cp_parser_objc_typename (parser);
20536   kwdparms = cp_parser_objc_method_keyword_params (parser);
20537   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20538
20539   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20540 }
20541
20542 /* Pars an Objective-C method prototype list.  */
20543
20544 static void
20545 cp_parser_objc_method_prototype_list (cp_parser* parser)
20546 {
20547   cp_token *token = cp_lexer_peek_token (parser->lexer);
20548
20549   while (token->keyword != RID_AT_END)
20550     {
20551       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20552         {
20553           objc_add_method_declaration
20554            (cp_parser_objc_method_signature (parser));
20555           cp_parser_consume_semicolon_at_end_of_statement (parser);
20556         }
20557       else
20558         /* Allow for interspersed non-ObjC++ code.  */
20559         cp_parser_objc_interstitial_code (parser);
20560
20561       token = cp_lexer_peek_token (parser->lexer);
20562     }
20563
20564   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20565   objc_finish_interface ();
20566 }
20567
20568 /* Parse an Objective-C method definition list.  */
20569
20570 static void
20571 cp_parser_objc_method_definition_list (cp_parser* parser)
20572 {
20573   cp_token *token = cp_lexer_peek_token (parser->lexer);
20574
20575   while (token->keyword != RID_AT_END)
20576     {
20577       tree meth;
20578
20579       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20580         {
20581           push_deferring_access_checks (dk_deferred);
20582           objc_start_method_definition
20583            (cp_parser_objc_method_signature (parser));
20584
20585           /* For historical reasons, we accept an optional semicolon.  */
20586           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20587             cp_lexer_consume_token (parser->lexer);
20588
20589           perform_deferred_access_checks ();
20590           stop_deferring_access_checks ();
20591           meth = cp_parser_function_definition_after_declarator (parser,
20592                                                                  false);
20593           pop_deferring_access_checks ();
20594           objc_finish_method_definition (meth);
20595         }
20596       else
20597         /* Allow for interspersed non-ObjC++ code.  */
20598         cp_parser_objc_interstitial_code (parser);
20599
20600       token = cp_lexer_peek_token (parser->lexer);
20601     }
20602
20603   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20604   objc_finish_implementation ();
20605 }
20606
20607 /* Parse Objective-C ivars.  */
20608
20609 static void
20610 cp_parser_objc_class_ivars (cp_parser* parser)
20611 {
20612   cp_token *token = cp_lexer_peek_token (parser->lexer);
20613
20614   if (token->type != CPP_OPEN_BRACE)
20615     return;     /* No ivars specified.  */
20616
20617   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20618   token = cp_lexer_peek_token (parser->lexer);
20619
20620   while (token->type != CPP_CLOSE_BRACE)
20621     {
20622       cp_decl_specifier_seq declspecs;
20623       int decl_class_or_enum_p;
20624       tree prefix_attributes;
20625
20626       cp_parser_objc_visibility_spec (parser);
20627
20628       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20629         break;
20630
20631       cp_parser_decl_specifier_seq (parser,
20632                                     CP_PARSER_FLAGS_OPTIONAL,
20633                                     &declspecs,
20634                                     &decl_class_or_enum_p);
20635       prefix_attributes = declspecs.attributes;
20636       declspecs.attributes = NULL_TREE;
20637
20638       /* Keep going until we hit the `;' at the end of the
20639          declaration.  */
20640       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20641         {
20642           tree width = NULL_TREE, attributes, first_attribute, decl;
20643           cp_declarator *declarator = NULL;
20644           int ctor_dtor_or_conv_p;
20645
20646           /* Check for a (possibly unnamed) bitfield declaration.  */
20647           token = cp_lexer_peek_token (parser->lexer);
20648           if (token->type == CPP_COLON)
20649             goto eat_colon;
20650
20651           if (token->type == CPP_NAME
20652               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20653                   == CPP_COLON))
20654             {
20655               /* Get the name of the bitfield.  */
20656               declarator = make_id_declarator (NULL_TREE,
20657                                                cp_parser_identifier (parser),
20658                                                sfk_none);
20659
20660              eat_colon:
20661               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20662               /* Get the width of the bitfield.  */
20663               width
20664                 = cp_parser_constant_expression (parser,
20665                                                  /*allow_non_constant=*/false,
20666                                                  NULL);
20667             }
20668           else
20669             {
20670               /* Parse the declarator.  */
20671               declarator
20672                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20673                                         &ctor_dtor_or_conv_p,
20674                                         /*parenthesized_p=*/NULL,
20675                                         /*member_p=*/false);
20676             }
20677
20678           /* Look for attributes that apply to the ivar.  */
20679           attributes = cp_parser_attributes_opt (parser);
20680           /* Remember which attributes are prefix attributes and
20681              which are not.  */
20682           first_attribute = attributes;
20683           /* Combine the attributes.  */
20684           attributes = chainon (prefix_attributes, attributes);
20685
20686           if (width)
20687               /* Create the bitfield declaration.  */
20688               decl = grokbitfield (declarator, &declspecs,
20689                                    width,
20690                                    attributes);
20691           else
20692             decl = grokfield (declarator, &declspecs,
20693                               NULL_TREE, /*init_const_expr_p=*/false,
20694                               NULL_TREE, attributes);
20695
20696           /* Add the instance variable.  */
20697           objc_add_instance_variable (decl);
20698
20699           /* Reset PREFIX_ATTRIBUTES.  */
20700           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20701             attributes = TREE_CHAIN (attributes);
20702           if (attributes)
20703             TREE_CHAIN (attributes) = NULL_TREE;
20704
20705           token = cp_lexer_peek_token (parser->lexer);
20706
20707           if (token->type == CPP_COMMA)
20708             {
20709               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20710               continue;
20711             }
20712           break;
20713         }
20714
20715       cp_parser_consume_semicolon_at_end_of_statement (parser);
20716       token = cp_lexer_peek_token (parser->lexer);
20717     }
20718
20719   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20720   /* For historical reasons, we accept an optional semicolon.  */
20721   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20722     cp_lexer_consume_token (parser->lexer);
20723 }
20724
20725 /* Parse an Objective-C protocol declaration.  */
20726
20727 static void
20728 cp_parser_objc_protocol_declaration (cp_parser* parser)
20729 {
20730   tree proto, protorefs;
20731   cp_token *tok;
20732
20733   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20734   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20735     {
20736       tok = cp_lexer_peek_token (parser->lexer);
20737       error_at (tok->location, "identifier expected after %<@protocol%>");
20738       goto finish;
20739     }
20740
20741   /* See if we have a forward declaration or a definition.  */
20742   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20743
20744   /* Try a forward declaration first.  */
20745   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20746     {
20747       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20748      finish:
20749       cp_parser_consume_semicolon_at_end_of_statement (parser);
20750     }
20751
20752   /* Ok, we got a full-fledged definition (or at least should).  */
20753   else
20754     {
20755       proto = cp_parser_identifier (parser);
20756       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20757       objc_start_protocol (proto, protorefs);
20758       cp_parser_objc_method_prototype_list (parser);
20759     }
20760 }
20761
20762 /* Parse an Objective-C superclass or category.  */
20763
20764 static void
20765 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20766                                                           tree *categ)
20767 {
20768   cp_token *next = cp_lexer_peek_token (parser->lexer);
20769
20770   *super = *categ = NULL_TREE;
20771   if (next->type == CPP_COLON)
20772     {
20773       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20774       *super = cp_parser_identifier (parser);
20775     }
20776   else if (next->type == CPP_OPEN_PAREN)
20777     {
20778       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20779       *categ = cp_parser_identifier (parser);
20780       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20781     }
20782 }
20783
20784 /* Parse an Objective-C class interface.  */
20785
20786 static void
20787 cp_parser_objc_class_interface (cp_parser* parser)
20788 {
20789   tree name, super, categ, protos;
20790
20791   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20792   name = cp_parser_identifier (parser);
20793   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20794   protos = cp_parser_objc_protocol_refs_opt (parser);
20795
20796   /* We have either a class or a category on our hands.  */
20797   if (categ)
20798     objc_start_category_interface (name, categ, protos);
20799   else
20800     {
20801       objc_start_class_interface (name, super, protos);
20802       /* Handle instance variable declarations, if any.  */
20803       cp_parser_objc_class_ivars (parser);
20804       objc_continue_interface ();
20805     }
20806
20807   cp_parser_objc_method_prototype_list (parser);
20808 }
20809
20810 /* Parse an Objective-C class implementation.  */
20811
20812 static void
20813 cp_parser_objc_class_implementation (cp_parser* parser)
20814 {
20815   tree name, super, categ;
20816
20817   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20818   name = cp_parser_identifier (parser);
20819   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20820
20821   /* We have either a class or a category on our hands.  */
20822   if (categ)
20823     objc_start_category_implementation (name, categ);
20824   else
20825     {
20826       objc_start_class_implementation (name, super);
20827       /* Handle instance variable declarations, if any.  */
20828       cp_parser_objc_class_ivars (parser);
20829       objc_continue_implementation ();
20830     }
20831
20832   cp_parser_objc_method_definition_list (parser);
20833 }
20834
20835 /* Consume the @end token and finish off the implementation.  */
20836
20837 static void
20838 cp_parser_objc_end_implementation (cp_parser* parser)
20839 {
20840   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20841   objc_finish_implementation ();
20842 }
20843
20844 /* Parse an Objective-C declaration.  */
20845
20846 static void
20847 cp_parser_objc_declaration (cp_parser* parser)
20848 {
20849   /* Try to figure out what kind of declaration is present.  */
20850   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20851
20852   switch (kwd->keyword)
20853     {
20854     case RID_AT_ALIAS:
20855       cp_parser_objc_alias_declaration (parser);
20856       break;
20857     case RID_AT_CLASS:
20858       cp_parser_objc_class_declaration (parser);
20859       break;
20860     case RID_AT_PROTOCOL:
20861       cp_parser_objc_protocol_declaration (parser);
20862       break;
20863     case RID_AT_INTERFACE:
20864       cp_parser_objc_class_interface (parser);
20865       break;
20866     case RID_AT_IMPLEMENTATION:
20867       cp_parser_objc_class_implementation (parser);
20868       break;
20869     case RID_AT_END:
20870       cp_parser_objc_end_implementation (parser);
20871       break;
20872     default:
20873       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20874                 kwd->u.value);
20875       cp_parser_skip_to_end_of_block_or_statement (parser);
20876     }
20877 }
20878
20879 /* Parse an Objective-C try-catch-finally statement.
20880
20881    objc-try-catch-finally-stmt:
20882      @try compound-statement objc-catch-clause-seq [opt]
20883        objc-finally-clause [opt]
20884
20885    objc-catch-clause-seq:
20886      objc-catch-clause objc-catch-clause-seq [opt]
20887
20888    objc-catch-clause:
20889      @catch ( exception-declaration ) compound-statement
20890
20891    objc-finally-clause
20892      @finally compound-statement
20893
20894    Returns NULL_TREE.  */
20895
20896 static tree
20897 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20898   location_t location;
20899   tree stmt;
20900
20901   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20902   location = cp_lexer_peek_token (parser->lexer)->location;
20903   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20904      node, lest it get absorbed into the surrounding block.  */
20905   stmt = push_stmt_list ();
20906   cp_parser_compound_statement (parser, NULL, false);
20907   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20908
20909   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20910     {
20911       cp_parameter_declarator *parmdecl;
20912       tree parm;
20913
20914       cp_lexer_consume_token (parser->lexer);
20915       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20916       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20917       parm = grokdeclarator (parmdecl->declarator,
20918                              &parmdecl->decl_specifiers,
20919                              PARM, /*initialized=*/0,
20920                              /*attrlist=*/NULL);
20921       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20922       objc_begin_catch_clause (parm);
20923       cp_parser_compound_statement (parser, NULL, false);
20924       objc_finish_catch_clause ();
20925     }
20926
20927   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20928     {
20929       cp_lexer_consume_token (parser->lexer);
20930       location = cp_lexer_peek_token (parser->lexer)->location;
20931       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20932          node, lest it get absorbed into the surrounding block.  */
20933       stmt = push_stmt_list ();
20934       cp_parser_compound_statement (parser, NULL, false);
20935       objc_build_finally_clause (location, pop_stmt_list (stmt));
20936     }
20937
20938   return objc_finish_try_stmt ();
20939 }
20940
20941 /* Parse an Objective-C synchronized statement.
20942
20943    objc-synchronized-stmt:
20944      @synchronized ( expression ) compound-statement
20945
20946    Returns NULL_TREE.  */
20947
20948 static tree
20949 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20950   location_t location;
20951   tree lock, stmt;
20952
20953   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20954
20955   location = cp_lexer_peek_token (parser->lexer)->location;
20956   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20957   lock = cp_parser_expression (parser, false, NULL);
20958   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20959
20960   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20961      node, lest it get absorbed into the surrounding block.  */
20962   stmt = push_stmt_list ();
20963   cp_parser_compound_statement (parser, NULL, false);
20964
20965   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20966 }
20967
20968 /* Parse an Objective-C throw statement.
20969
20970    objc-throw-stmt:
20971      @throw assignment-expression [opt] ;
20972
20973    Returns a constructed '@throw' statement.  */
20974
20975 static tree
20976 cp_parser_objc_throw_statement (cp_parser *parser) {
20977   tree expr = NULL_TREE;
20978   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20979
20980   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20981
20982   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20983     expr = cp_parser_assignment_expression (parser, false, NULL);
20984
20985   cp_parser_consume_semicolon_at_end_of_statement (parser);
20986
20987   return objc_build_throw_stmt (loc, expr);
20988 }
20989
20990 /* Parse an Objective-C statement.  */
20991
20992 static tree
20993 cp_parser_objc_statement (cp_parser * parser) {
20994   /* Try to figure out what kind of declaration is present.  */
20995   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20996
20997   switch (kwd->keyword)
20998     {
20999     case RID_AT_TRY:
21000       return cp_parser_objc_try_catch_finally_statement (parser);
21001     case RID_AT_SYNCHRONIZED:
21002       return cp_parser_objc_synchronized_statement (parser);
21003     case RID_AT_THROW:
21004       return cp_parser_objc_throw_statement (parser);
21005     default:
21006       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21007                kwd->u.value);
21008       cp_parser_skip_to_end_of_block_or_statement (parser);
21009     }
21010
21011   return error_mark_node;
21012 }
21013 \f
21014 /* OpenMP 2.5 parsing routines.  */
21015
21016 /* Returns name of the next clause.
21017    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21018    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21019    returned and the token is consumed.  */
21020
21021 static pragma_omp_clause
21022 cp_parser_omp_clause_name (cp_parser *parser)
21023 {
21024   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21025
21026   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21027     result = PRAGMA_OMP_CLAUSE_IF;
21028   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21029     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21030   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21031     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21032   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21033     {
21034       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21035       const char *p = IDENTIFIER_POINTER (id);
21036
21037       switch (p[0])
21038         {
21039         case 'c':
21040           if (!strcmp ("collapse", p))
21041             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21042           else if (!strcmp ("copyin", p))
21043             result = PRAGMA_OMP_CLAUSE_COPYIN;
21044           else if (!strcmp ("copyprivate", p))
21045             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21046           break;
21047         case 'f':
21048           if (!strcmp ("firstprivate", p))
21049             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21050           break;
21051         case 'l':
21052           if (!strcmp ("lastprivate", p))
21053             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21054           break;
21055         case 'n':
21056           if (!strcmp ("nowait", p))
21057             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21058           else if (!strcmp ("num_threads", p))
21059             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21060           break;
21061         case 'o':
21062           if (!strcmp ("ordered", p))
21063             result = PRAGMA_OMP_CLAUSE_ORDERED;
21064           break;
21065         case 'r':
21066           if (!strcmp ("reduction", p))
21067             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21068           break;
21069         case 's':
21070           if (!strcmp ("schedule", p))
21071             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21072           else if (!strcmp ("shared", p))
21073             result = PRAGMA_OMP_CLAUSE_SHARED;
21074           break;
21075         case 'u':
21076           if (!strcmp ("untied", p))
21077             result = PRAGMA_OMP_CLAUSE_UNTIED;
21078           break;
21079         }
21080     }
21081
21082   if (result != PRAGMA_OMP_CLAUSE_NONE)
21083     cp_lexer_consume_token (parser->lexer);
21084
21085   return result;
21086 }
21087
21088 /* Validate that a clause of the given type does not already exist.  */
21089
21090 static void
21091 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21092                            const char *name, location_t location)
21093 {
21094   tree c;
21095
21096   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21097     if (OMP_CLAUSE_CODE (c) == code)
21098       {
21099         error_at (location, "too many %qs clauses", name);
21100         break;
21101       }
21102 }
21103
21104 /* OpenMP 2.5:
21105    variable-list:
21106      identifier
21107      variable-list , identifier
21108
21109    In addition, we match a closing parenthesis.  An opening parenthesis
21110    will have been consumed by the caller.
21111
21112    If KIND is nonzero, create the appropriate node and install the decl
21113    in OMP_CLAUSE_DECL and add the node to the head of the list.
21114
21115    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21116    return the list created.  */
21117
21118 static tree
21119 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21120                                 tree list)
21121 {
21122   cp_token *token;
21123   while (1)
21124     {
21125       tree name, decl;
21126
21127       token = cp_lexer_peek_token (parser->lexer);
21128       name = cp_parser_id_expression (parser, /*template_p=*/false,
21129                                       /*check_dependency_p=*/true,
21130                                       /*template_p=*/NULL,
21131                                       /*declarator_p=*/false,
21132                                       /*optional_p=*/false);
21133       if (name == error_mark_node)
21134         goto skip_comma;
21135
21136       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21137       if (decl == error_mark_node)
21138         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21139       else if (kind != 0)
21140         {
21141           tree u = build_omp_clause (token->location, kind);
21142           OMP_CLAUSE_DECL (u) = decl;
21143           OMP_CLAUSE_CHAIN (u) = list;
21144           list = u;
21145         }
21146       else
21147         list = tree_cons (decl, NULL_TREE, list);
21148
21149     get_comma:
21150       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21151         break;
21152       cp_lexer_consume_token (parser->lexer);
21153     }
21154
21155   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21156     {
21157       int ending;
21158
21159       /* Try to resync to an unnested comma.  Copied from
21160          cp_parser_parenthesized_expression_list.  */
21161     skip_comma:
21162       ending = cp_parser_skip_to_closing_parenthesis (parser,
21163                                                       /*recovering=*/true,
21164                                                       /*or_comma=*/true,
21165                                                       /*consume_paren=*/true);
21166       if (ending < 0)
21167         goto get_comma;
21168     }
21169
21170   return list;
21171 }
21172
21173 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21174    common case for omp clauses.  */
21175
21176 static tree
21177 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21178 {
21179   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21180     return cp_parser_omp_var_list_no_open (parser, kind, list);
21181   return list;
21182 }
21183
21184 /* OpenMP 3.0:
21185    collapse ( constant-expression ) */
21186
21187 static tree
21188 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21189 {
21190   tree c, num;
21191   location_t loc;
21192   HOST_WIDE_INT n;
21193
21194   loc = cp_lexer_peek_token (parser->lexer)->location;
21195   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21196     return list;
21197
21198   num = cp_parser_constant_expression (parser, false, NULL);
21199
21200   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21201     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21202                                            /*or_comma=*/false,
21203                                            /*consume_paren=*/true);
21204
21205   if (num == error_mark_node)
21206     return list;
21207   num = fold_non_dependent_expr (num);
21208   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21209       || !host_integerp (num, 0)
21210       || (n = tree_low_cst (num, 0)) <= 0
21211       || (int) n != n)
21212     {
21213       error_at (loc, "collapse argument needs positive constant integer expression");
21214       return list;
21215     }
21216
21217   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21218   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21219   OMP_CLAUSE_CHAIN (c) = list;
21220   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21221
21222   return c;
21223 }
21224
21225 /* OpenMP 2.5:
21226    default ( shared | none ) */
21227
21228 static tree
21229 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21230 {
21231   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21232   tree c;
21233
21234   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21235     return list;
21236   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21237     {
21238       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21239       const char *p = IDENTIFIER_POINTER (id);
21240
21241       switch (p[0])
21242         {
21243         case 'n':
21244           if (strcmp ("none", p) != 0)
21245             goto invalid_kind;
21246           kind = OMP_CLAUSE_DEFAULT_NONE;
21247           break;
21248
21249         case 's':
21250           if (strcmp ("shared", p) != 0)
21251             goto invalid_kind;
21252           kind = OMP_CLAUSE_DEFAULT_SHARED;
21253           break;
21254
21255         default:
21256           goto invalid_kind;
21257         }
21258
21259       cp_lexer_consume_token (parser->lexer);
21260     }
21261   else
21262     {
21263     invalid_kind:
21264       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21265     }
21266
21267   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21268     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21269                                            /*or_comma=*/false,
21270                                            /*consume_paren=*/true);
21271
21272   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21273     return list;
21274
21275   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21276   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21277   OMP_CLAUSE_CHAIN (c) = list;
21278   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21279
21280   return c;
21281 }
21282
21283 /* OpenMP 2.5:
21284    if ( expression ) */
21285
21286 static tree
21287 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21288 {
21289   tree t, c;
21290
21291   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21292     return list;
21293
21294   t = cp_parser_condition (parser);
21295
21296   if (t == error_mark_node
21297       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21298     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21299                                            /*or_comma=*/false,
21300                                            /*consume_paren=*/true);
21301
21302   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21303
21304   c = build_omp_clause (location, OMP_CLAUSE_IF);
21305   OMP_CLAUSE_IF_EXPR (c) = t;
21306   OMP_CLAUSE_CHAIN (c) = list;
21307
21308   return c;
21309 }
21310
21311 /* OpenMP 2.5:
21312    nowait */
21313
21314 static tree
21315 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21316                              tree list, location_t location)
21317 {
21318   tree c;
21319
21320   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21321
21322   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21323   OMP_CLAUSE_CHAIN (c) = list;
21324   return c;
21325 }
21326
21327 /* OpenMP 2.5:
21328    num_threads ( expression ) */
21329
21330 static tree
21331 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21332                                   location_t location)
21333 {
21334   tree t, c;
21335
21336   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21337     return list;
21338
21339   t = cp_parser_expression (parser, false, NULL);
21340
21341   if (t == error_mark_node
21342       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21343     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21344                                            /*or_comma=*/false,
21345                                            /*consume_paren=*/true);
21346
21347   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21348                              "num_threads", location);
21349
21350   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21351   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21352   OMP_CLAUSE_CHAIN (c) = list;
21353
21354   return c;
21355 }
21356
21357 /* OpenMP 2.5:
21358    ordered */
21359
21360 static tree
21361 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21362                               tree list, location_t location)
21363 {
21364   tree c;
21365
21366   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21367                              "ordered", location);
21368
21369   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21370   OMP_CLAUSE_CHAIN (c) = list;
21371   return c;
21372 }
21373
21374 /* OpenMP 2.5:
21375    reduction ( reduction-operator : variable-list )
21376
21377    reduction-operator:
21378      One of: + * - & ^ | && || */
21379
21380 static tree
21381 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21382 {
21383   enum tree_code code;
21384   tree nlist, c;
21385
21386   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21387     return list;
21388
21389   switch (cp_lexer_peek_token (parser->lexer)->type)
21390     {
21391     case CPP_PLUS:
21392       code = PLUS_EXPR;
21393       break;
21394     case CPP_MULT:
21395       code = MULT_EXPR;
21396       break;
21397     case CPP_MINUS:
21398       code = MINUS_EXPR;
21399       break;
21400     case CPP_AND:
21401       code = BIT_AND_EXPR;
21402       break;
21403     case CPP_XOR:
21404       code = BIT_XOR_EXPR;
21405       break;
21406     case CPP_OR:
21407       code = BIT_IOR_EXPR;
21408       break;
21409     case CPP_AND_AND:
21410       code = TRUTH_ANDIF_EXPR;
21411       break;
21412     case CPP_OR_OR:
21413       code = TRUTH_ORIF_EXPR;
21414       break;
21415     default:
21416       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21417                                "%<|%>, %<&&%>, or %<||%>");
21418     resync_fail:
21419       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21420                                              /*or_comma=*/false,
21421                                              /*consume_paren=*/true);
21422       return list;
21423     }
21424   cp_lexer_consume_token (parser->lexer);
21425
21426   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21427     goto resync_fail;
21428
21429   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21430   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21431     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21432
21433   return nlist;
21434 }
21435
21436 /* OpenMP 2.5:
21437    schedule ( schedule-kind )
21438    schedule ( schedule-kind , expression )
21439
21440    schedule-kind:
21441      static | dynamic | guided | runtime | auto  */
21442
21443 static tree
21444 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21445 {
21446   tree c, t;
21447
21448   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21449     return list;
21450
21451   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21452
21453   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21454     {
21455       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21456       const char *p = IDENTIFIER_POINTER (id);
21457
21458       switch (p[0])
21459         {
21460         case 'd':
21461           if (strcmp ("dynamic", p) != 0)
21462             goto invalid_kind;
21463           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21464           break;
21465
21466         case 'g':
21467           if (strcmp ("guided", p) != 0)
21468             goto invalid_kind;
21469           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21470           break;
21471
21472         case 'r':
21473           if (strcmp ("runtime", p) != 0)
21474             goto invalid_kind;
21475           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21476           break;
21477
21478         default:
21479           goto invalid_kind;
21480         }
21481     }
21482   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21483     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21484   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21485     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21486   else
21487     goto invalid_kind;
21488   cp_lexer_consume_token (parser->lexer);
21489
21490   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21491     {
21492       cp_token *token;
21493       cp_lexer_consume_token (parser->lexer);
21494
21495       token = cp_lexer_peek_token (parser->lexer);
21496       t = cp_parser_assignment_expression (parser, false, NULL);
21497
21498       if (t == error_mark_node)
21499         goto resync_fail;
21500       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21501         error_at (token->location, "schedule %<runtime%> does not take "
21502                   "a %<chunk_size%> parameter");
21503       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21504         error_at (token->location, "schedule %<auto%> does not take "
21505                   "a %<chunk_size%> parameter");
21506       else
21507         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21508
21509       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21510         goto resync_fail;
21511     }
21512   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21513     goto resync_fail;
21514
21515   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21516   OMP_CLAUSE_CHAIN (c) = list;
21517   return c;
21518
21519  invalid_kind:
21520   cp_parser_error (parser, "invalid schedule kind");
21521  resync_fail:
21522   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21523                                          /*or_comma=*/false,
21524                                          /*consume_paren=*/true);
21525   return list;
21526 }
21527
21528 /* OpenMP 3.0:
21529    untied */
21530
21531 static tree
21532 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21533                              tree list, location_t location)
21534 {
21535   tree c;
21536
21537   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21538
21539   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21540   OMP_CLAUSE_CHAIN (c) = list;
21541   return c;
21542 }
21543
21544 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21545    is a bitmask in MASK.  Return the list of clauses found; the result
21546    of clause default goes in *pdefault.  */
21547
21548 static tree
21549 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21550                            const char *where, cp_token *pragma_tok)
21551 {
21552   tree clauses = NULL;
21553   bool first = true;
21554   cp_token *token = NULL;
21555
21556   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21557     {
21558       pragma_omp_clause c_kind;
21559       const char *c_name;
21560       tree prev = clauses;
21561
21562       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21563         cp_lexer_consume_token (parser->lexer);
21564
21565       token = cp_lexer_peek_token (parser->lexer);
21566       c_kind = cp_parser_omp_clause_name (parser);
21567       first = false;
21568
21569       switch (c_kind)
21570         {
21571         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21572           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21573                                                    token->location);
21574           c_name = "collapse";
21575           break;
21576         case PRAGMA_OMP_CLAUSE_COPYIN:
21577           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21578           c_name = "copyin";
21579           break;
21580         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21581           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21582                                             clauses);
21583           c_name = "copyprivate";
21584           break;
21585         case PRAGMA_OMP_CLAUSE_DEFAULT:
21586           clauses = cp_parser_omp_clause_default (parser, clauses,
21587                                                   token->location);
21588           c_name = "default";
21589           break;
21590         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21591           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21592                                             clauses);
21593           c_name = "firstprivate";
21594           break;
21595         case PRAGMA_OMP_CLAUSE_IF:
21596           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21597           c_name = "if";
21598           break;
21599         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21600           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21601                                             clauses);
21602           c_name = "lastprivate";
21603           break;
21604         case PRAGMA_OMP_CLAUSE_NOWAIT:
21605           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21606           c_name = "nowait";
21607           break;
21608         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21609           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21610                                                       token->location);
21611           c_name = "num_threads";
21612           break;
21613         case PRAGMA_OMP_CLAUSE_ORDERED:
21614           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21615                                                   token->location);
21616           c_name = "ordered";
21617           break;
21618         case PRAGMA_OMP_CLAUSE_PRIVATE:
21619           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21620                                             clauses);
21621           c_name = "private";
21622           break;
21623         case PRAGMA_OMP_CLAUSE_REDUCTION:
21624           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21625           c_name = "reduction";
21626           break;
21627         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21628           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21629                                                    token->location);
21630           c_name = "schedule";
21631           break;
21632         case PRAGMA_OMP_CLAUSE_SHARED:
21633           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21634                                             clauses);
21635           c_name = "shared";
21636           break;
21637         case PRAGMA_OMP_CLAUSE_UNTIED:
21638           clauses = cp_parser_omp_clause_untied (parser, clauses,
21639                                                  token->location);
21640           c_name = "nowait";
21641           break;
21642         default:
21643           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21644           goto saw_error;
21645         }
21646
21647       if (((mask >> c_kind) & 1) == 0)
21648         {
21649           /* Remove the invalid clause(s) from the list to avoid
21650              confusing the rest of the compiler.  */
21651           clauses = prev;
21652           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21653         }
21654     }
21655  saw_error:
21656   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21657   return finish_omp_clauses (clauses);
21658 }
21659
21660 /* OpenMP 2.5:
21661    structured-block:
21662      statement
21663
21664    In practice, we're also interested in adding the statement to an
21665    outer node.  So it is convenient if we work around the fact that
21666    cp_parser_statement calls add_stmt.  */
21667
21668 static unsigned
21669 cp_parser_begin_omp_structured_block (cp_parser *parser)
21670 {
21671   unsigned save = parser->in_statement;
21672
21673   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21674      This preserves the "not within loop or switch" style error messages
21675      for nonsense cases like
21676         void foo() {
21677         #pragma omp single
21678           break;
21679         }
21680   */
21681   if (parser->in_statement)
21682     parser->in_statement = IN_OMP_BLOCK;
21683
21684   return save;
21685 }
21686
21687 static void
21688 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21689 {
21690   parser->in_statement = save;
21691 }
21692
21693 static tree
21694 cp_parser_omp_structured_block (cp_parser *parser)
21695 {
21696   tree stmt = begin_omp_structured_block ();
21697   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21698
21699   cp_parser_statement (parser, NULL_TREE, false, NULL);
21700
21701   cp_parser_end_omp_structured_block (parser, save);
21702   return finish_omp_structured_block (stmt);
21703 }
21704
21705 /* OpenMP 2.5:
21706    # pragma omp atomic new-line
21707      expression-stmt
21708
21709    expression-stmt:
21710      x binop= expr | x++ | ++x | x-- | --x
21711    binop:
21712      +, *, -, /, &, ^, |, <<, >>
21713
21714   where x is an lvalue expression with scalar type.  */
21715
21716 static void
21717 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21718 {
21719   tree lhs, rhs;
21720   enum tree_code code;
21721
21722   cp_parser_require_pragma_eol (parser, pragma_tok);
21723
21724   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21725                                     /*cast_p=*/false, NULL);
21726   switch (TREE_CODE (lhs))
21727     {
21728     case ERROR_MARK:
21729       goto saw_error;
21730
21731     case PREINCREMENT_EXPR:
21732     case POSTINCREMENT_EXPR:
21733       lhs = TREE_OPERAND (lhs, 0);
21734       code = PLUS_EXPR;
21735       rhs = integer_one_node;
21736       break;
21737
21738     case PREDECREMENT_EXPR:
21739     case POSTDECREMENT_EXPR:
21740       lhs = TREE_OPERAND (lhs, 0);
21741       code = MINUS_EXPR;
21742       rhs = integer_one_node;
21743       break;
21744
21745     default:
21746       switch (cp_lexer_peek_token (parser->lexer)->type)
21747         {
21748         case CPP_MULT_EQ:
21749           code = MULT_EXPR;
21750           break;
21751         case CPP_DIV_EQ:
21752           code = TRUNC_DIV_EXPR;
21753           break;
21754         case CPP_PLUS_EQ:
21755           code = PLUS_EXPR;
21756           break;
21757         case CPP_MINUS_EQ:
21758           code = MINUS_EXPR;
21759           break;
21760         case CPP_LSHIFT_EQ:
21761           code = LSHIFT_EXPR;
21762           break;
21763         case CPP_RSHIFT_EQ:
21764           code = RSHIFT_EXPR;
21765           break;
21766         case CPP_AND_EQ:
21767           code = BIT_AND_EXPR;
21768           break;
21769         case CPP_OR_EQ:
21770           code = BIT_IOR_EXPR;
21771           break;
21772         case CPP_XOR_EQ:
21773           code = BIT_XOR_EXPR;
21774           break;
21775         default:
21776           cp_parser_error (parser,
21777                            "invalid operator for %<#pragma omp atomic%>");
21778           goto saw_error;
21779         }
21780       cp_lexer_consume_token (parser->lexer);
21781
21782       rhs = cp_parser_expression (parser, false, NULL);
21783       if (rhs == error_mark_node)
21784         goto saw_error;
21785       break;
21786     }
21787   finish_omp_atomic (code, lhs, rhs);
21788   cp_parser_consume_semicolon_at_end_of_statement (parser);
21789   return;
21790
21791  saw_error:
21792   cp_parser_skip_to_end_of_block_or_statement (parser);
21793 }
21794
21795
21796 /* OpenMP 2.5:
21797    # pragma omp barrier new-line  */
21798
21799 static void
21800 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21801 {
21802   cp_parser_require_pragma_eol (parser, pragma_tok);
21803   finish_omp_barrier ();
21804 }
21805
21806 /* OpenMP 2.5:
21807    # pragma omp critical [(name)] new-line
21808      structured-block  */
21809
21810 static tree
21811 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21812 {
21813   tree stmt, name = NULL;
21814
21815   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21816     {
21817       cp_lexer_consume_token (parser->lexer);
21818
21819       name = cp_parser_identifier (parser);
21820
21821       if (name == error_mark_node
21822           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21823         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21824                                                /*or_comma=*/false,
21825                                                /*consume_paren=*/true);
21826       if (name == error_mark_node)
21827         name = NULL;
21828     }
21829   cp_parser_require_pragma_eol (parser, pragma_tok);
21830
21831   stmt = cp_parser_omp_structured_block (parser);
21832   return c_finish_omp_critical (input_location, stmt, name);
21833 }
21834
21835 /* OpenMP 2.5:
21836    # pragma omp flush flush-vars[opt] new-line
21837
21838    flush-vars:
21839      ( variable-list ) */
21840
21841 static void
21842 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21843 {
21844   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21845     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21846   cp_parser_require_pragma_eol (parser, pragma_tok);
21847
21848   finish_omp_flush ();
21849 }
21850
21851 /* Helper function, to parse omp for increment expression.  */
21852
21853 static tree
21854 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21855 {
21856   tree cond = cp_parser_binary_expression (parser, false, true,
21857                                            PREC_NOT_OPERATOR, NULL);
21858   bool overloaded_p;
21859
21860   if (cond == error_mark_node
21861       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21862     {
21863       cp_parser_skip_to_end_of_statement (parser);
21864       return error_mark_node;
21865     }
21866
21867   switch (TREE_CODE (cond))
21868     {
21869     case GT_EXPR:
21870     case GE_EXPR:
21871     case LT_EXPR:
21872     case LE_EXPR:
21873       break;
21874     default:
21875       return error_mark_node;
21876     }
21877
21878   /* If decl is an iterator, preserve LHS and RHS of the relational
21879      expr until finish_omp_for.  */
21880   if (decl
21881       && (type_dependent_expression_p (decl)
21882           || CLASS_TYPE_P (TREE_TYPE (decl))))
21883     return cond;
21884
21885   return build_x_binary_op (TREE_CODE (cond),
21886                             TREE_OPERAND (cond, 0), ERROR_MARK,
21887                             TREE_OPERAND (cond, 1), ERROR_MARK,
21888                             &overloaded_p, tf_warning_or_error);
21889 }
21890
21891 /* Helper function, to parse omp for increment expression.  */
21892
21893 static tree
21894 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21895 {
21896   cp_token *token = cp_lexer_peek_token (parser->lexer);
21897   enum tree_code op;
21898   tree lhs, rhs;
21899   cp_id_kind idk;
21900   bool decl_first;
21901
21902   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21903     {
21904       op = (token->type == CPP_PLUS_PLUS
21905             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21906       cp_lexer_consume_token (parser->lexer);
21907       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21908       if (lhs != decl)
21909         return error_mark_node;
21910       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21911     }
21912
21913   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21914   if (lhs != decl)
21915     return error_mark_node;
21916
21917   token = cp_lexer_peek_token (parser->lexer);
21918   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21919     {
21920       op = (token->type == CPP_PLUS_PLUS
21921             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21922       cp_lexer_consume_token (parser->lexer);
21923       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21924     }
21925
21926   op = cp_parser_assignment_operator_opt (parser);
21927   if (op == ERROR_MARK)
21928     return error_mark_node;
21929
21930   if (op != NOP_EXPR)
21931     {
21932       rhs = cp_parser_assignment_expression (parser, false, NULL);
21933       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21934       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21935     }
21936
21937   lhs = cp_parser_binary_expression (parser, false, false,
21938                                      PREC_ADDITIVE_EXPRESSION, NULL);
21939   token = cp_lexer_peek_token (parser->lexer);
21940   decl_first = lhs == decl;
21941   if (decl_first)
21942     lhs = NULL_TREE;
21943   if (token->type != CPP_PLUS
21944       && token->type != CPP_MINUS)
21945     return error_mark_node;
21946
21947   do
21948     {
21949       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21950       cp_lexer_consume_token (parser->lexer);
21951       rhs = cp_parser_binary_expression (parser, false, false,
21952                                          PREC_ADDITIVE_EXPRESSION, NULL);
21953       token = cp_lexer_peek_token (parser->lexer);
21954       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21955         {
21956           if (lhs == NULL_TREE)
21957             {
21958               if (op == PLUS_EXPR)
21959                 lhs = rhs;
21960               else
21961                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21962             }
21963           else
21964             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21965                                      NULL, tf_warning_or_error);
21966         }
21967     }
21968   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21969
21970   if (!decl_first)
21971     {
21972       if (rhs != decl || op == MINUS_EXPR)
21973         return error_mark_node;
21974       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21975     }
21976   else
21977     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21978
21979   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21980 }
21981
21982 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21983
21984 static tree
21985 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21986 {
21987   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21988   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21989   tree this_pre_body, cl;
21990   location_t loc_first;
21991   bool collapse_err = false;
21992   int i, collapse = 1, nbraces = 0;
21993
21994   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21995     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21996       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21997
21998   gcc_assert (collapse >= 1);
21999
22000   declv = make_tree_vec (collapse);
22001   initv = make_tree_vec (collapse);
22002   condv = make_tree_vec (collapse);
22003   incrv = make_tree_vec (collapse);
22004
22005   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22006
22007   for (i = 0; i < collapse; i++)
22008     {
22009       int bracecount = 0;
22010       bool add_private_clause = false;
22011       location_t loc;
22012
22013       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22014         {
22015           cp_parser_error (parser, "for statement expected");
22016           return NULL;
22017         }
22018       loc = cp_lexer_consume_token (parser->lexer)->location;
22019
22020       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22021         return NULL;
22022
22023       init = decl = real_decl = NULL;
22024       this_pre_body = push_stmt_list ();
22025       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22026         {
22027           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22028
22029              init-expr:
22030                        var = lb
22031                        integer-type var = lb
22032                        random-access-iterator-type var = lb
22033                        pointer-type var = lb
22034           */
22035           cp_decl_specifier_seq type_specifiers;
22036
22037           /* First, try to parse as an initialized declaration.  See
22038              cp_parser_condition, from whence the bulk of this is copied.  */
22039
22040           cp_parser_parse_tentatively (parser);
22041           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22042                                         &type_specifiers);
22043           if (cp_parser_parse_definitely (parser))
22044             {
22045               /* If parsing a type specifier seq succeeded, then this
22046                  MUST be a initialized declaration.  */
22047               tree asm_specification, attributes;
22048               cp_declarator *declarator;
22049
22050               declarator = cp_parser_declarator (parser,
22051                                                  CP_PARSER_DECLARATOR_NAMED,
22052                                                  /*ctor_dtor_or_conv_p=*/NULL,
22053                                                  /*parenthesized_p=*/NULL,
22054                                                  /*member_p=*/false);
22055               attributes = cp_parser_attributes_opt (parser);
22056               asm_specification = cp_parser_asm_specification_opt (parser);
22057
22058               if (declarator == cp_error_declarator) 
22059                 cp_parser_skip_to_end_of_statement (parser);
22060
22061               else 
22062                 {
22063                   tree pushed_scope, auto_node;
22064
22065                   decl = start_decl (declarator, &type_specifiers,
22066                                      SD_INITIALIZED, attributes,
22067                                      /*prefix_attributes=*/NULL_TREE,
22068                                      &pushed_scope);
22069
22070                   auto_node = type_uses_auto (TREE_TYPE (decl));
22071                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22072                     {
22073                       if (cp_lexer_next_token_is (parser->lexer, 
22074                                                   CPP_OPEN_PAREN))
22075                         error ("parenthesized initialization is not allowed in "
22076                                "OpenMP %<for%> loop");
22077                       else
22078                         /* Trigger an error.  */
22079                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22080
22081                       init = error_mark_node;
22082                       cp_parser_skip_to_end_of_statement (parser);
22083                     }
22084                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22085                            || type_dependent_expression_p (decl)
22086                            || auto_node)
22087                     {
22088                       bool is_direct_init, is_non_constant_init;
22089
22090                       init = cp_parser_initializer (parser,
22091                                                     &is_direct_init,
22092                                                     &is_non_constant_init);
22093
22094                       if (auto_node && describable_type (init))
22095                         {
22096                           TREE_TYPE (decl)
22097                             = do_auto_deduction (TREE_TYPE (decl), init,
22098                                                  auto_node);
22099
22100                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22101                               && !type_dependent_expression_p (decl))
22102                             goto non_class;
22103                         }
22104                       
22105                       cp_finish_decl (decl, init, !is_non_constant_init,
22106                                       asm_specification,
22107                                       LOOKUP_ONLYCONVERTING);
22108                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22109                         {
22110                           for_block
22111                             = tree_cons (NULL, this_pre_body, for_block);
22112                           init = NULL_TREE;
22113                         }
22114                       else
22115                         init = pop_stmt_list (this_pre_body);
22116                       this_pre_body = NULL_TREE;
22117                     }
22118                   else
22119                     {
22120                       /* Consume '='.  */
22121                       cp_lexer_consume_token (parser->lexer);
22122                       init = cp_parser_assignment_expression (parser, false, NULL);
22123
22124                     non_class:
22125                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22126                         init = error_mark_node;
22127                       else
22128                         cp_finish_decl (decl, NULL_TREE,
22129                                         /*init_const_expr_p=*/false,
22130                                         asm_specification,
22131                                         LOOKUP_ONLYCONVERTING);
22132                     }
22133
22134                   if (pushed_scope)
22135                     pop_scope (pushed_scope);
22136                 }
22137             }
22138           else 
22139             {
22140               cp_id_kind idk;
22141               /* If parsing a type specifier sequence failed, then
22142                  this MUST be a simple expression.  */
22143               cp_parser_parse_tentatively (parser);
22144               decl = cp_parser_primary_expression (parser, false, false,
22145                                                    false, &idk);
22146               if (!cp_parser_error_occurred (parser)
22147                   && decl
22148                   && DECL_P (decl)
22149                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22150                 {
22151                   tree rhs;
22152
22153                   cp_parser_parse_definitely (parser);
22154                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22155                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22156                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22157                                                          rhs,
22158                                                          tf_warning_or_error));
22159                   add_private_clause = true;
22160                 }
22161               else
22162                 {
22163                   decl = NULL;
22164                   cp_parser_abort_tentative_parse (parser);
22165                   init = cp_parser_expression (parser, false, NULL);
22166                   if (init)
22167                     {
22168                       if (TREE_CODE (init) == MODIFY_EXPR
22169                           || TREE_CODE (init) == MODOP_EXPR)
22170                         real_decl = TREE_OPERAND (init, 0);
22171                     }
22172                 }
22173             }
22174         }
22175       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22176       if (this_pre_body)
22177         {
22178           this_pre_body = pop_stmt_list (this_pre_body);
22179           if (pre_body)
22180             {
22181               tree t = pre_body;
22182               pre_body = push_stmt_list ();
22183               add_stmt (t);
22184               add_stmt (this_pre_body);
22185               pre_body = pop_stmt_list (pre_body);
22186             }
22187           else
22188             pre_body = this_pre_body;
22189         }
22190
22191       if (decl)
22192         real_decl = decl;
22193       if (par_clauses != NULL && real_decl != NULL_TREE)
22194         {
22195           tree *c;
22196           for (c = par_clauses; *c ; )
22197             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22198                 && OMP_CLAUSE_DECL (*c) == real_decl)
22199               {
22200                 error_at (loc, "iteration variable %qD"
22201                           " should not be firstprivate", real_decl);
22202                 *c = OMP_CLAUSE_CHAIN (*c);
22203               }
22204             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22205                      && OMP_CLAUSE_DECL (*c) == real_decl)
22206               {
22207                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22208                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22209                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22210                 OMP_CLAUSE_DECL (l) = real_decl;
22211                 OMP_CLAUSE_CHAIN (l) = clauses;
22212                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22213                 clauses = l;
22214                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22215                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22216                 add_private_clause = false;
22217               }
22218             else
22219               {
22220                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22221                     && OMP_CLAUSE_DECL (*c) == real_decl)
22222                   add_private_clause = false;
22223                 c = &OMP_CLAUSE_CHAIN (*c);
22224               }
22225         }
22226
22227       if (add_private_clause)
22228         {
22229           tree c;
22230           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22231             {
22232               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22233                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22234                   && OMP_CLAUSE_DECL (c) == decl)
22235                 break;
22236               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22237                        && OMP_CLAUSE_DECL (c) == decl)
22238                 error_at (loc, "iteration variable %qD "
22239                           "should not be firstprivate",
22240                           decl);
22241               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22242                        && OMP_CLAUSE_DECL (c) == decl)
22243                 error_at (loc, "iteration variable %qD should not be reduction",
22244                           decl);
22245             }
22246           if (c == NULL)
22247             {
22248               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22249               OMP_CLAUSE_DECL (c) = decl;
22250               c = finish_omp_clauses (c);
22251               if (c)
22252                 {
22253                   OMP_CLAUSE_CHAIN (c) = clauses;
22254                   clauses = c;
22255                 }
22256             }
22257         }
22258
22259       cond = NULL;
22260       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22261         cond = cp_parser_omp_for_cond (parser, decl);
22262       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22263
22264       incr = NULL;
22265       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22266         {
22267           /* If decl is an iterator, preserve the operator on decl
22268              until finish_omp_for.  */
22269           if (decl
22270               && (type_dependent_expression_p (decl)
22271                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22272             incr = cp_parser_omp_for_incr (parser, decl);
22273           else
22274             incr = cp_parser_expression (parser, false, NULL);
22275         }
22276
22277       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22278         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22279                                                /*or_comma=*/false,
22280                                                /*consume_paren=*/true);
22281
22282       TREE_VEC_ELT (declv, i) = decl;
22283       TREE_VEC_ELT (initv, i) = init;
22284       TREE_VEC_ELT (condv, i) = cond;
22285       TREE_VEC_ELT (incrv, i) = incr;
22286
22287       if (i == collapse - 1)
22288         break;
22289
22290       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22291          in between the collapsed for loops to be still considered perfectly
22292          nested.  Hopefully the final version clarifies this.
22293          For now handle (multiple) {'s and empty statements.  */
22294       cp_parser_parse_tentatively (parser);
22295       do
22296         {
22297           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22298             break;
22299           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22300             {
22301               cp_lexer_consume_token (parser->lexer);
22302               bracecount++;
22303             }
22304           else if (bracecount
22305                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22306             cp_lexer_consume_token (parser->lexer);
22307           else
22308             {
22309               loc = cp_lexer_peek_token (parser->lexer)->location;
22310               error_at (loc, "not enough collapsed for loops");
22311               collapse_err = true;
22312               cp_parser_abort_tentative_parse (parser);
22313               declv = NULL_TREE;
22314               break;
22315             }
22316         }
22317       while (1);
22318
22319       if (declv)
22320         {
22321           cp_parser_parse_definitely (parser);
22322           nbraces += bracecount;
22323         }
22324     }
22325
22326   /* Note that we saved the original contents of this flag when we entered
22327      the structured block, and so we don't need to re-save it here.  */
22328   parser->in_statement = IN_OMP_FOR;
22329
22330   /* Note that the grammar doesn't call for a structured block here,
22331      though the loop as a whole is a structured block.  */
22332   body = push_stmt_list ();
22333   cp_parser_statement (parser, NULL_TREE, false, NULL);
22334   body = pop_stmt_list (body);
22335
22336   if (declv == NULL_TREE)
22337     ret = NULL_TREE;
22338   else
22339     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22340                           pre_body, clauses);
22341
22342   while (nbraces)
22343     {
22344       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22345         {
22346           cp_lexer_consume_token (parser->lexer);
22347           nbraces--;
22348         }
22349       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22350         cp_lexer_consume_token (parser->lexer);
22351       else
22352         {
22353           if (!collapse_err)
22354             {
22355               error_at (cp_lexer_peek_token (parser->lexer)->location,
22356                         "collapsed loops not perfectly nested");
22357             }
22358           collapse_err = true;
22359           cp_parser_statement_seq_opt (parser, NULL);
22360           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22361         }
22362     }
22363
22364   while (for_block)
22365     {
22366       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22367       for_block = TREE_CHAIN (for_block);
22368     }
22369
22370   return ret;
22371 }
22372
22373 /* OpenMP 2.5:
22374    #pragma omp for for-clause[optseq] new-line
22375      for-loop  */
22376
22377 #define OMP_FOR_CLAUSE_MASK                             \
22378         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22379         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22380         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22381         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22382         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22383         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22384         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22385         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22386
22387 static tree
22388 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22389 {
22390   tree clauses, sb, ret;
22391   unsigned int save;
22392
22393   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22394                                        "#pragma omp for", pragma_tok);
22395
22396   sb = begin_omp_structured_block ();
22397   save = cp_parser_begin_omp_structured_block (parser);
22398
22399   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22400
22401   cp_parser_end_omp_structured_block (parser, save);
22402   add_stmt (finish_omp_structured_block (sb));
22403
22404   return ret;
22405 }
22406
22407 /* OpenMP 2.5:
22408    # pragma omp master new-line
22409      structured-block  */
22410
22411 static tree
22412 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22413 {
22414   cp_parser_require_pragma_eol (parser, pragma_tok);
22415   return c_finish_omp_master (input_location,
22416                               cp_parser_omp_structured_block (parser));
22417 }
22418
22419 /* OpenMP 2.5:
22420    # pragma omp ordered new-line
22421      structured-block  */
22422
22423 static tree
22424 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22425 {
22426   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22427   cp_parser_require_pragma_eol (parser, pragma_tok);
22428   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22429 }
22430
22431 /* OpenMP 2.5:
22432
22433    section-scope:
22434      { section-sequence }
22435
22436    section-sequence:
22437      section-directive[opt] structured-block
22438      section-sequence section-directive structured-block  */
22439
22440 static tree
22441 cp_parser_omp_sections_scope (cp_parser *parser)
22442 {
22443   tree stmt, substmt;
22444   bool error_suppress = false;
22445   cp_token *tok;
22446
22447   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22448     return NULL_TREE;
22449
22450   stmt = push_stmt_list ();
22451
22452   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22453     {
22454       unsigned save;
22455
22456       substmt = begin_omp_structured_block ();
22457       save = cp_parser_begin_omp_structured_block (parser);
22458
22459       while (1)
22460         {
22461           cp_parser_statement (parser, NULL_TREE, false, NULL);
22462
22463           tok = cp_lexer_peek_token (parser->lexer);
22464           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22465             break;
22466           if (tok->type == CPP_CLOSE_BRACE)
22467             break;
22468           if (tok->type == CPP_EOF)
22469             break;
22470         }
22471
22472       cp_parser_end_omp_structured_block (parser, save);
22473       substmt = finish_omp_structured_block (substmt);
22474       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22475       add_stmt (substmt);
22476     }
22477
22478   while (1)
22479     {
22480       tok = cp_lexer_peek_token (parser->lexer);
22481       if (tok->type == CPP_CLOSE_BRACE)
22482         break;
22483       if (tok->type == CPP_EOF)
22484         break;
22485
22486       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22487         {
22488           cp_lexer_consume_token (parser->lexer);
22489           cp_parser_require_pragma_eol (parser, tok);
22490           error_suppress = false;
22491         }
22492       else if (!error_suppress)
22493         {
22494           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22495           error_suppress = true;
22496         }
22497
22498       substmt = cp_parser_omp_structured_block (parser);
22499       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22500       add_stmt (substmt);
22501     }
22502   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22503
22504   substmt = pop_stmt_list (stmt);
22505
22506   stmt = make_node (OMP_SECTIONS);
22507   TREE_TYPE (stmt) = void_type_node;
22508   OMP_SECTIONS_BODY (stmt) = substmt;
22509
22510   add_stmt (stmt);
22511   return stmt;
22512 }
22513
22514 /* OpenMP 2.5:
22515    # pragma omp sections sections-clause[optseq] newline
22516      sections-scope  */
22517
22518 #define OMP_SECTIONS_CLAUSE_MASK                        \
22519         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22520         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22521         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22522         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22523         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22524
22525 static tree
22526 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22527 {
22528   tree clauses, ret;
22529
22530   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22531                                        "#pragma omp sections", pragma_tok);
22532
22533   ret = cp_parser_omp_sections_scope (parser);
22534   if (ret)
22535     OMP_SECTIONS_CLAUSES (ret) = clauses;
22536
22537   return ret;
22538 }
22539
22540 /* OpenMP 2.5:
22541    # pragma parallel parallel-clause new-line
22542    # pragma parallel for parallel-for-clause new-line
22543    # pragma parallel sections parallel-sections-clause new-line  */
22544
22545 #define OMP_PARALLEL_CLAUSE_MASK                        \
22546         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22547         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22548         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22549         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22550         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22551         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22552         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22553         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22554
22555 static tree
22556 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22557 {
22558   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22559   const char *p_name = "#pragma omp parallel";
22560   tree stmt, clauses, par_clause, ws_clause, block;
22561   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22562   unsigned int save;
22563   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22564
22565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22566     {
22567       cp_lexer_consume_token (parser->lexer);
22568       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22569       p_name = "#pragma omp parallel for";
22570       mask |= OMP_FOR_CLAUSE_MASK;
22571       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22572     }
22573   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22574     {
22575       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22576       const char *p = IDENTIFIER_POINTER (id);
22577       if (strcmp (p, "sections") == 0)
22578         {
22579           cp_lexer_consume_token (parser->lexer);
22580           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22581           p_name = "#pragma omp parallel sections";
22582           mask |= OMP_SECTIONS_CLAUSE_MASK;
22583           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22584         }
22585     }
22586
22587   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22588   block = begin_omp_parallel ();
22589   save = cp_parser_begin_omp_structured_block (parser);
22590
22591   switch (p_kind)
22592     {
22593     case PRAGMA_OMP_PARALLEL:
22594       cp_parser_statement (parser, NULL_TREE, false, NULL);
22595       par_clause = clauses;
22596       break;
22597
22598     case PRAGMA_OMP_PARALLEL_FOR:
22599       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22600       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22601       break;
22602
22603     case PRAGMA_OMP_PARALLEL_SECTIONS:
22604       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22605       stmt = cp_parser_omp_sections_scope (parser);
22606       if (stmt)
22607         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22608       break;
22609
22610     default:
22611       gcc_unreachable ();
22612     }
22613
22614   cp_parser_end_omp_structured_block (parser, save);
22615   stmt = finish_omp_parallel (par_clause, block);
22616   if (p_kind != PRAGMA_OMP_PARALLEL)
22617     OMP_PARALLEL_COMBINED (stmt) = 1;
22618   return stmt;
22619 }
22620
22621 /* OpenMP 2.5:
22622    # pragma omp single single-clause[optseq] new-line
22623      structured-block  */
22624
22625 #define OMP_SINGLE_CLAUSE_MASK                          \
22626         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22627         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22628         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22629         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22630
22631 static tree
22632 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22633 {
22634   tree stmt = make_node (OMP_SINGLE);
22635   TREE_TYPE (stmt) = void_type_node;
22636
22637   OMP_SINGLE_CLAUSES (stmt)
22638     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22639                                  "#pragma omp single", pragma_tok);
22640   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22641
22642   return add_stmt (stmt);
22643 }
22644
22645 /* OpenMP 3.0:
22646    # pragma omp task task-clause[optseq] new-line
22647      structured-block  */
22648
22649 #define OMP_TASK_CLAUSE_MASK                            \
22650         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22651         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22652         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22653         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22654         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22655         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22656
22657 static tree
22658 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22659 {
22660   tree clauses, block;
22661   unsigned int save;
22662
22663   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22664                                        "#pragma omp task", pragma_tok);
22665   block = begin_omp_task ();
22666   save = cp_parser_begin_omp_structured_block (parser);
22667   cp_parser_statement (parser, NULL_TREE, false, NULL);
22668   cp_parser_end_omp_structured_block (parser, save);
22669   return finish_omp_task (clauses, block);
22670 }
22671
22672 /* OpenMP 3.0:
22673    # pragma omp taskwait new-line  */
22674
22675 static void
22676 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22677 {
22678   cp_parser_require_pragma_eol (parser, pragma_tok);
22679   finish_omp_taskwait ();
22680 }
22681
22682 /* OpenMP 2.5:
22683    # pragma omp threadprivate (variable-list) */
22684
22685 static void
22686 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22687 {
22688   tree vars;
22689
22690   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22691   cp_parser_require_pragma_eol (parser, pragma_tok);
22692
22693   finish_omp_threadprivate (vars);
22694 }
22695
22696 /* Main entry point to OpenMP statement pragmas.  */
22697
22698 static void
22699 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22700 {
22701   tree stmt;
22702
22703   switch (pragma_tok->pragma_kind)
22704     {
22705     case PRAGMA_OMP_ATOMIC:
22706       cp_parser_omp_atomic (parser, pragma_tok);
22707       return;
22708     case PRAGMA_OMP_CRITICAL:
22709       stmt = cp_parser_omp_critical (parser, pragma_tok);
22710       break;
22711     case PRAGMA_OMP_FOR:
22712       stmt = cp_parser_omp_for (parser, pragma_tok);
22713       break;
22714     case PRAGMA_OMP_MASTER:
22715       stmt = cp_parser_omp_master (parser, pragma_tok);
22716       break;
22717     case PRAGMA_OMP_ORDERED:
22718       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22719       break;
22720     case PRAGMA_OMP_PARALLEL:
22721       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22722       break;
22723     case PRAGMA_OMP_SECTIONS:
22724       stmt = cp_parser_omp_sections (parser, pragma_tok);
22725       break;
22726     case PRAGMA_OMP_SINGLE:
22727       stmt = cp_parser_omp_single (parser, pragma_tok);
22728       break;
22729     case PRAGMA_OMP_TASK:
22730       stmt = cp_parser_omp_task (parser, pragma_tok);
22731       break;
22732     default:
22733       gcc_unreachable ();
22734     }
22735
22736   if (stmt)
22737     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22738 }
22739 \f
22740 /* The parser.  */
22741
22742 static GTY (()) cp_parser *the_parser;
22743
22744 \f
22745 /* Special handling for the first token or line in the file.  The first
22746    thing in the file might be #pragma GCC pch_preprocess, which loads a
22747    PCH file, which is a GC collection point.  So we need to handle this
22748    first pragma without benefit of an existing lexer structure.
22749
22750    Always returns one token to the caller in *FIRST_TOKEN.  This is
22751    either the true first token of the file, or the first token after
22752    the initial pragma.  */
22753
22754 static void
22755 cp_parser_initial_pragma (cp_token *first_token)
22756 {
22757   tree name = NULL;
22758
22759   cp_lexer_get_preprocessor_token (NULL, first_token);
22760   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22761     return;
22762
22763   cp_lexer_get_preprocessor_token (NULL, first_token);
22764   if (first_token->type == CPP_STRING)
22765     {
22766       name = first_token->u.value;
22767
22768       cp_lexer_get_preprocessor_token (NULL, first_token);
22769       if (first_token->type != CPP_PRAGMA_EOL)
22770         error_at (first_token->location,
22771                   "junk at end of %<#pragma GCC pch_preprocess%>");
22772     }
22773   else
22774     error_at (first_token->location, "expected string literal");
22775
22776   /* Skip to the end of the pragma.  */
22777   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22778     cp_lexer_get_preprocessor_token (NULL, first_token);
22779
22780   /* Now actually load the PCH file.  */
22781   if (name)
22782     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22783
22784   /* Read one more token to return to our caller.  We have to do this
22785      after reading the PCH file in, since its pointers have to be
22786      live.  */
22787   cp_lexer_get_preprocessor_token (NULL, first_token);
22788 }
22789
22790 /* Normal parsing of a pragma token.  Here we can (and must) use the
22791    regular lexer.  */
22792
22793 static bool
22794 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22795 {
22796   cp_token *pragma_tok;
22797   unsigned int id;
22798
22799   pragma_tok = cp_lexer_consume_token (parser->lexer);
22800   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22801   parser->lexer->in_pragma = true;
22802
22803   id = pragma_tok->pragma_kind;
22804   switch (id)
22805     {
22806     case PRAGMA_GCC_PCH_PREPROCESS:
22807       error_at (pragma_tok->location,
22808                 "%<#pragma GCC pch_preprocess%> must be first");
22809       break;
22810
22811     case PRAGMA_OMP_BARRIER:
22812       switch (context)
22813         {
22814         case pragma_compound:
22815           cp_parser_omp_barrier (parser, pragma_tok);
22816           return false;
22817         case pragma_stmt:
22818           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22819                     "used in compound statements");
22820           break;
22821         default:
22822           goto bad_stmt;
22823         }
22824       break;
22825
22826     case PRAGMA_OMP_FLUSH:
22827       switch (context)
22828         {
22829         case pragma_compound:
22830           cp_parser_omp_flush (parser, pragma_tok);
22831           return false;
22832         case pragma_stmt:
22833           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22834                     "used in compound statements");
22835           break;
22836         default:
22837           goto bad_stmt;
22838         }
22839       break;
22840
22841     case PRAGMA_OMP_TASKWAIT:
22842       switch (context)
22843         {
22844         case pragma_compound:
22845           cp_parser_omp_taskwait (parser, pragma_tok);
22846           return false;
22847         case pragma_stmt:
22848           error_at (pragma_tok->location,
22849                     "%<#pragma omp taskwait%> may only be "
22850                     "used in compound statements");
22851           break;
22852         default:
22853           goto bad_stmt;
22854         }
22855       break;
22856
22857     case PRAGMA_OMP_THREADPRIVATE:
22858       cp_parser_omp_threadprivate (parser, pragma_tok);
22859       return false;
22860
22861     case PRAGMA_OMP_ATOMIC:
22862     case PRAGMA_OMP_CRITICAL:
22863     case PRAGMA_OMP_FOR:
22864     case PRAGMA_OMP_MASTER:
22865     case PRAGMA_OMP_ORDERED:
22866     case PRAGMA_OMP_PARALLEL:
22867     case PRAGMA_OMP_SECTIONS:
22868     case PRAGMA_OMP_SINGLE:
22869     case PRAGMA_OMP_TASK:
22870       if (context == pragma_external)
22871         goto bad_stmt;
22872       cp_parser_omp_construct (parser, pragma_tok);
22873       return true;
22874
22875     case PRAGMA_OMP_SECTION:
22876       error_at (pragma_tok->location, 
22877                 "%<#pragma omp section%> may only be used in "
22878                 "%<#pragma omp sections%> construct");
22879       break;
22880
22881     default:
22882       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22883       c_invoke_pragma_handler (id);
22884       break;
22885
22886     bad_stmt:
22887       cp_parser_error (parser, "expected declaration specifiers");
22888       break;
22889     }
22890
22891   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22892   return false;
22893 }
22894
22895 /* The interface the pragma parsers have to the lexer.  */
22896
22897 enum cpp_ttype
22898 pragma_lex (tree *value)
22899 {
22900   cp_token *tok;
22901   enum cpp_ttype ret;
22902
22903   tok = cp_lexer_peek_token (the_parser->lexer);
22904
22905   ret = tok->type;
22906   *value = tok->u.value;
22907
22908   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22909     ret = CPP_EOF;
22910   else if (ret == CPP_STRING)
22911     *value = cp_parser_string_literal (the_parser, false, false);
22912   else
22913     {
22914       cp_lexer_consume_token (the_parser->lexer);
22915       if (ret == CPP_KEYWORD)
22916         ret = CPP_NAME;
22917     }
22918
22919   return ret;
22920 }
22921
22922 \f
22923 /* External interface.  */
22924
22925 /* Parse one entire translation unit.  */
22926
22927 void
22928 c_parse_file (void)
22929 {
22930   bool error_occurred;
22931   static bool already_called = false;
22932
22933   if (already_called)
22934     {
22935       sorry ("inter-module optimizations not implemented for C++");
22936       return;
22937     }
22938   already_called = true;
22939
22940   the_parser = cp_parser_new ();
22941   push_deferring_access_checks (flag_access_control
22942                                 ? dk_no_deferred : dk_no_check);
22943   error_occurred = cp_parser_translation_unit (the_parser);
22944   the_parser = NULL;
22945 }
22946
22947 #include "gt-cp-parser.h"