diagnostic.c (pedwarn_at): Rename as pedwarn.
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  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
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
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 cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
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   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       cp_parameter_declarator *parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification)
1019 {
1020   cp_declarator *declarator;
1021
1022   declarator = make_declarator (cdk_function);
1023   declarator->declarator = target;
1024   declarator->u.function.parameters = parms;
1025   declarator->u.function.qualifiers = cv_qualifiers;
1026   declarator->u.function.exception_specification = exception_specification;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
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 cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_class_or_namespace_name
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_declarator_id
1730   (cp_parser *, bool);
1731 static tree cp_parser_type_id
1732   (cp_parser *);
1733 static void cp_parser_type_specifier_seq
1734   (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1736   (cp_parser *);
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738   (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740   (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument 
1742   (cp_parser *, bool);
1743 static void cp_parser_function_body
1744   (cp_parser *);
1745 static tree cp_parser_initializer
1746   (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748   (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750   (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752   (cp_parser *, bool *);
1753
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1755   (cp_parser *);
1756
1757 /* Classes [gram.class] */
1758
1759 static tree cp_parser_class_name
1760   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1762   (cp_parser *);
1763 static tree cp_parser_class_head
1764   (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1766   (cp_parser *);
1767 static void cp_parser_member_specification_opt
1768   (cp_parser *);
1769 static void cp_parser_member_declaration
1770   (cp_parser *);
1771 static tree cp_parser_pure_specifier
1772   (cp_parser *);
1773 static tree cp_parser_constant_initializer
1774   (cp_parser *);
1775
1776 /* Derived classes [gram.class.derived] */
1777
1778 static tree cp_parser_base_clause
1779   (cp_parser *);
1780 static tree cp_parser_base_specifier
1781   (cp_parser *);
1782
1783 /* Special member functions [gram.special] */
1784
1785 static tree cp_parser_conversion_function_id
1786   (cp_parser *);
1787 static tree cp_parser_conversion_type_id
1788   (cp_parser *);
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1790   (cp_parser *);
1791 static bool cp_parser_ctor_initializer_opt
1792   (cp_parser *);
1793 static void cp_parser_mem_initializer_list
1794   (cp_parser *);
1795 static tree cp_parser_mem_initializer
1796   (cp_parser *);
1797 static tree cp_parser_mem_initializer_id
1798   (cp_parser *);
1799
1800 /* Overloading [gram.over] */
1801
1802 static tree cp_parser_operator_function_id
1803   (cp_parser *);
1804 static tree cp_parser_operator
1805   (cp_parser *);
1806
1807 /* Templates [gram.temp] */
1808
1809 static void cp_parser_template_declaration
1810   (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1812   (cp_parser *);
1813 static tree cp_parser_template_parameter
1814   (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816   (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818   (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820   (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1822   (cp_parser *);
1823 static tree cp_parser_template_argument
1824   (cp_parser *);
1825 static void cp_parser_explicit_instantiation
1826   (cp_parser *);
1827 static void cp_parser_explicit_specialization
1828   (cp_parser *);
1829
1830 /* Exception handling [gram.exception] */
1831
1832 static tree cp_parser_try_block
1833   (cp_parser *);
1834 static bool cp_parser_function_try_block
1835   (cp_parser *);
1836 static void cp_parser_handler_seq
1837   (cp_parser *);
1838 static void cp_parser_handler
1839   (cp_parser *);
1840 static tree cp_parser_exception_declaration
1841   (cp_parser *);
1842 static tree cp_parser_throw_expression
1843   (cp_parser *);
1844 static tree cp_parser_exception_specification_opt
1845   (cp_parser *);
1846 static tree cp_parser_type_id_list
1847   (cp_parser *);
1848
1849 /* GNU Extensions */
1850
1851 static tree cp_parser_asm_specification_opt
1852   (cp_parser *);
1853 static tree cp_parser_asm_operand_list
1854   (cp_parser *);
1855 static tree cp_parser_asm_clobber_list
1856   (cp_parser *);
1857 static tree cp_parser_attributes_opt
1858   (cp_parser *);
1859 static tree cp_parser_attribute_list
1860   (cp_parser *);
1861 static bool cp_parser_extension_opt
1862   (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1864   (cp_parser *);
1865
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868   (cp_parser *, enum pragma_context);
1869
1870 /* Objective-C++ Productions */
1871
1872 static tree cp_parser_objc_message_receiver
1873   (cp_parser *);
1874 static tree cp_parser_objc_message_args
1875   (cp_parser *);
1876 static tree cp_parser_objc_message_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_encode_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_defs_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_protocol_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_selector_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_expression
1887   (cp_parser *);
1888 static bool cp_parser_objc_selector_p
1889   (enum cpp_ttype);
1890 static tree cp_parser_objc_selector
1891   (cp_parser *);
1892 static tree cp_parser_objc_protocol_refs_opt
1893   (cp_parser *);
1894 static void cp_parser_objc_declaration
1895   (cp_parser *);
1896 static tree cp_parser_objc_statement
1897   (cp_parser *);
1898
1899 /* Utility Routines */
1900
1901 static tree cp_parser_lookup_name
1902   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904   (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1906   (tree, bool);
1907 static bool cp_parser_check_declarator_template_parameters
1908   (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910   (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1912   (cp_parser *);
1913 static tree cp_parser_global_scope_opt
1914   (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916   (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920   (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922   (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924   (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928   (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1932   (cp_parser *);
1933 static void cp_parser_save_default_args
1934   (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936   (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938   (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940   (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942   (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1944   (cp_parser *);
1945 static void cp_parser_set_storage_class
1946   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948   (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950   (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952   (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954   (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1956   (cp_token *);
1957 static bool cp_parser_next_token_starts_class_definition_p
1958   (cp_parser *);
1959 static bool cp_parser_next_token_ends_template_argument_p
1960   (cp_parser *);
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962   (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1964   (cp_token *);
1965 static void cp_parser_check_class_key
1966   (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968   (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1970   (cp_parser *);
1971 static void cp_parser_pre_parsed_nested_name_specifier
1972   (cp_parser *);
1973 static bool cp_parser_cache_group
1974   (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1976   (cp_parser *);
1977 static void cp_parser_commit_to_tentative_parse
1978   (cp_parser *);
1979 static void cp_parser_abort_tentative_parse
1980   (cp_parser *);
1981 static bool cp_parser_parse_definitely
1982   (cp_parser *);
1983 static inline bool cp_parser_parsing_tentatively
1984   (cp_parser *);
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1986   (cp_parser *);
1987 static void cp_parser_error
1988   (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990   (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1992   (cp_parser *);
1993 static bool cp_parser_check_type_definition
1994   (cp_parser *);
1995 static void cp_parser_check_for_definition_in_return_type
1996   (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998   (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000   (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002   (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2004   (cp_parser *);
2005 static int cp_parser_skip_to_closing_parenthesis
2006   (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2008   (cp_parser *);
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2010   (cp_parser *);
2011 static void cp_parser_skip_to_end_of_block_or_statement
2012   (cp_parser *);
2013 static bool cp_parser_skip_to_closing_brace
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2016   (cp_parser *);
2017 static void cp_parser_skip_to_pragma_eol
2018   (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2020   (cp_parser *);
2021 static bool cp_parser_allow_gnu_extensions_p
2022   (cp_parser *);
2023 static bool cp_parser_is_string_literal
2024   (cp_token *);
2025 static bool cp_parser_is_keyword
2026   (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028   (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2031
2032 /* Returns nonzero if we are parsing tentatively.  */
2033
2034 static inline bool
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2036 {
2037   return parser->context->next != NULL;
2038 }
2039
2040 /* Returns nonzero if TOKEN is a string literal.  */
2041
2042 static bool
2043 cp_parser_is_string_literal (cp_token* token)
2044 {
2045   return (token->type == CPP_STRING ||
2046           token->type == CPP_STRING16 ||
2047           token->type == CPP_STRING32 ||
2048           token->type == CPP_WSTRING);
2049 }
2050
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2052
2053 static bool
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2055 {
2056   return token->keyword == keyword;
2057 }
2058
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060       FILE:LINE: MESSAGE before TOKEN
2061    where TOKEN is the next token in the input stream.  MESSAGE
2062    (specified by the caller) is usually of the form "expected
2063    OTHER-TOKEN".  */
2064
2065 static void
2066 cp_parser_error (cp_parser* parser, const char* message)
2067 {
2068   if (!cp_parser_simulate_error (parser))
2069     {
2070       cp_token *token = cp_lexer_peek_token (parser->lexer);
2071       /* This diagnostic makes more sense if it is tagged to the line
2072          of the token we just peeked at.  */
2073       cp_lexer_set_source_position_from_token (token);
2074
2075       if (token->type == CPP_PRAGMA)
2076         {
2077           error ("%H%<#pragma%> is not allowed here", &token->location);
2078           cp_parser_skip_to_pragma_eol (parser, token);
2079           return;
2080         }
2081
2082       c_parse_error (message,
2083                      /* Because c_parser_error does not understand
2084                         CPP_KEYWORD, keywords are treated like
2085                         identifiers.  */
2086                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2087                      token->u.value);
2088     }
2089 }
2090
2091 /* Issue an error about name-lookup failing.  NAME is the
2092    IDENTIFIER_NODE DECL is the result of
2093    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2094    the thing that we hoped to find.  */
2095
2096 static void
2097 cp_parser_name_lookup_error (cp_parser* parser,
2098                              tree name,
2099                              tree decl,
2100                              const char* desired,
2101                              location_t location)
2102 {
2103   /* If name lookup completely failed, tell the user that NAME was not
2104      declared.  */
2105   if (decl == error_mark_node)
2106     {
2107       if (parser->scope && parser->scope != global_namespace)
2108         error ("%H%<%E::%E%> has not been declared",
2109                &location, parser->scope, name);
2110       else if (parser->scope == global_namespace)
2111         error ("%H%<::%E%> has not been declared", &location, name);
2112       else if (parser->object_scope
2113                && !CLASS_TYPE_P (parser->object_scope))
2114         error ("%Hrequest for member %qE in non-class type %qT",
2115                &location, name, parser->object_scope);
2116       else if (parser->object_scope)
2117         error ("%H%<%T::%E%> has not been declared",
2118                &location, parser->object_scope, name);
2119       else
2120         error ("%H%qE has not been declared", &location, name);
2121     }
2122   else if (parser->scope && parser->scope != global_namespace)
2123     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124   else if (parser->scope == global_namespace)
2125     error ("%H%<::%E%> %s", &location, name, desired);
2126   else
2127     error ("%H%qE %s", &location, name, desired);
2128 }
2129
2130 /* If we are parsing tentatively, remember that an error has occurred
2131    during this tentative parse.  Returns true if the error was
2132    simulated; false if a message should be issued by the caller.  */
2133
2134 static bool
2135 cp_parser_simulate_error (cp_parser* parser)
2136 {
2137   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2138     {
2139       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2140       return true;
2141     }
2142   return false;
2143 }
2144
2145 /* Check for repeated decl-specifiers.  */
2146
2147 static void
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149                            location_t location)
2150 {
2151   cp_decl_spec ds;
2152
2153   for (ds = ds_first; ds != ds_last; ++ds)
2154     {
2155       unsigned count = decl_specs->specs[(int)ds];
2156       if (count < 2)
2157         continue;
2158       /* The "long" specifier is a special case because of "long long".  */
2159       if (ds == ds_long)
2160         {
2161           if (count > 2)
2162             error ("%H%<long long long%> is too long for GCC", &location);
2163           else if (pedantic && !in_system_header && warn_long_long
2164                    && cxx_dialect == cxx98)
2165             pedwarn (location, OPT_Wlong_long, 
2166                      "ISO C++ 1998 does not support %<long long%>");
2167         }
2168       else if (count > 1)
2169         {
2170           static const char *const decl_spec_names[] = {
2171             "signed",
2172             "unsigned",
2173             "short",
2174             "long",
2175             "const",
2176             "volatile",
2177             "restrict",
2178             "inline",
2179             "virtual",
2180             "explicit",
2181             "friend",
2182             "typedef",
2183             "__complex",
2184             "__thread"
2185           };
2186           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2187         }
2188     }
2189 }
2190
2191 /* This function is called when a type is defined.  If type
2192    definitions are forbidden at this point, an error message is
2193    issued.  */
2194
2195 static bool
2196 cp_parser_check_type_definition (cp_parser* parser)
2197 {
2198   /* If types are forbidden here, issue a message.  */
2199   if (parser->type_definition_forbidden_message)
2200     {
2201       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202          in the message need to be interpreted.  */
2203       error (parser->type_definition_forbidden_message);
2204       return false;
2205     }
2206   return true;
2207 }
2208
2209 /* This function is called when the DECLARATOR is processed.  The TYPE
2210    was a type defined in the decl-specifiers.  If it is invalid to
2211    define a type in the decl-specifiers for DECLARATOR, an error is
2212    issued. TYPE_LOCATION is the location of TYPE and is used
2213    for error reporting.  */
2214
2215 static void
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217                                                tree type, location_t type_location)
2218 {
2219   /* [dcl.fct] forbids type definitions in return types.
2220      Unfortunately, it's not easy to know whether or not we are
2221      processing a return type until after the fact.  */
2222   while (declarator
2223          && (declarator->kind == cdk_pointer
2224              || declarator->kind == cdk_reference
2225              || declarator->kind == cdk_ptrmem))
2226     declarator = declarator->declarator;
2227   if (declarator
2228       && declarator->kind == cdk_function)
2229     {
2230       error ("%Hnew types may not be defined in a return type", &type_location);
2231       inform (type_location, 
2232               "(perhaps a semicolon is missing after the definition of %qT)",
2233               type);
2234     }
2235 }
2236
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238    "<" in any valid C++ program.  If the next token is indeed "<",
2239    issue a message warning the user about what appears to be an
2240    invalid attempt to form a template-id. LOCATION is the location
2241    of the type-specifier (TYPE) */
2242
2243 static void
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245                                          tree type, location_t location)
2246 {
2247   cp_token_position start = 0;
2248
2249   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2250     {
2251       if (TYPE_P (type))
2252         error ("%H%qT is not a template", &location, type);
2253       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254         error ("%H%qE is not a template", &location, type);
2255       else
2256         error ("%Hinvalid template-id", &location);
2257       /* Remember the location of the invalid "<".  */
2258       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259         start = cp_lexer_token_position (parser->lexer, true);
2260       /* Consume the "<".  */
2261       cp_lexer_consume_token (parser->lexer);
2262       /* Parse the template arguments.  */
2263       cp_parser_enclosed_template_argument_list (parser);
2264       /* Permanently remove the invalid template arguments so that
2265          this error message is not issued again.  */
2266       if (start)
2267         cp_lexer_purge_tokens_after (parser->lexer, start);
2268     }
2269 }
2270
2271 /* If parsing an integral constant-expression, issue an error message
2272    about the fact that THING appeared and return true.  Otherwise,
2273    return false.  In either case, set
2274    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2275
2276 static bool
2277 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2278                                             const char *thing)
2279 {
2280   parser->non_integral_constant_expression_p = true;
2281   if (parser->integral_constant_expression_p)
2282     {
2283       if (!parser->allow_non_integral_constant_expression_p)
2284         {
2285           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286              in the message need to be interpreted.  */
2287           char *message = concat (thing,
2288                                   " cannot appear in a constant-expression",
2289                                   NULL);
2290           error (message);
2291           free (message);
2292           return true;
2293         }
2294     }
2295   return false;
2296 }
2297
2298 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2299    qualifying scope (or NULL, if none) for ID.  This function commits
2300    to the current active tentative parse, if any.  (Otherwise, the
2301    problematic construct might be encountered again later, resulting
2302    in duplicate error messages.) LOCATION is the location of ID.  */
2303
2304 static void
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306                                       tree scope, tree id,
2307                                       location_t location)
2308 {
2309   tree decl, old_scope;
2310   /* Try to lookup the identifier.  */
2311   old_scope = parser->scope;
2312   parser->scope = scope;
2313   decl = cp_parser_lookup_name_simple (parser, id, location);
2314   parser->scope = old_scope;
2315   /* If the lookup found a template-name, it means that the user forgot
2316   to specify an argument list. Emit a useful error message.  */
2317   if (TREE_CODE (decl) == TEMPLATE_DECL)
2318     error ("%Hinvalid use of template-name %qE without an argument list",
2319            &location, decl);
2320   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322   else if (TREE_CODE (decl) == TYPE_DECL)
2323     /* Something like 'unsigned A a;'  */
2324     error ("%Hinvalid combination of multiple type-specifiers",
2325            &location);
2326   else if (!parser->scope)
2327     {
2328       /* Issue an error message.  */
2329       error ("%H%qE does not name a type", &location, id);
2330       /* If we're in a template class, it's possible that the user was
2331          referring to a type from a base class.  For example:
2332
2333            template <typename T> struct A { typedef T X; };
2334            template <typename T> struct B : public A<T> { X x; };
2335
2336          The user should have said "typename A<T>::X".  */
2337       if (processing_template_decl && current_class_type
2338           && TYPE_BINFO (current_class_type))
2339         {
2340           tree b;
2341
2342           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2343                b;
2344                b = TREE_CHAIN (b))
2345             {
2346               tree base_type = BINFO_TYPE (b);
2347               if (CLASS_TYPE_P (base_type)
2348                   && dependent_type_p (base_type))
2349                 {
2350                   tree field;
2351                   /* Go from a particular instantiation of the
2352                      template (which will have an empty TYPE_FIELDs),
2353                      to the main version.  */
2354                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355                   for (field = TYPE_FIELDS (base_type);
2356                        field;
2357                        field = TREE_CHAIN (field))
2358                     if (TREE_CODE (field) == TYPE_DECL
2359                         && DECL_NAME (field) == id)
2360                       {
2361                         inform (location, 
2362                                 "(perhaps %<typename %T::%E%> was intended)",
2363                                 BINFO_TYPE (b), id);
2364                         break;
2365                       }
2366                   if (field)
2367                     break;
2368                 }
2369             }
2370         }
2371     }
2372   /* Here we diagnose qualified-ids where the scope is actually correct,
2373      but the identifier does not resolve to a valid type name.  */
2374   else if (parser->scope != error_mark_node)
2375     {
2376       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377         error ("%H%qE in namespace %qE does not name a type",
2378                &location, id, parser->scope);
2379       else if (TYPE_P (parser->scope))
2380         error ("%H%qE in class %qT does not name a type",
2381                &location, id, parser->scope);
2382       else
2383         gcc_unreachable ();
2384     }
2385   cp_parser_commit_to_tentative_parse (parser);
2386 }
2387
2388 /* Check for a common situation where a type-name should be present,
2389    but is not, and issue a sensible error message.  Returns true if an
2390    invalid type-name was detected.
2391
2392    The situation handled by this function are variable declarations of the
2393    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394    Usually, `ID' should name a type, but if we got here it means that it
2395    does not. We try to emit the best possible error message depending on
2396    how exactly the id-expression looks like.  */
2397
2398 static bool
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2400 {
2401   tree id;
2402   cp_token *token = cp_lexer_peek_token (parser->lexer);
2403
2404   cp_parser_parse_tentatively (parser);
2405   id = cp_parser_id_expression (parser,
2406                                 /*template_keyword_p=*/false,
2407                                 /*check_dependency_p=*/true,
2408                                 /*template_p=*/NULL,
2409                                 /*declarator_p=*/true,
2410                                 /*optional_p=*/false);
2411   /* After the id-expression, there should be a plain identifier,
2412      otherwise this is not a simple variable declaration. Also, if
2413      the scope is dependent, we cannot do much.  */
2414   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415       || (parser->scope && TYPE_P (parser->scope)
2416           && dependent_type_p (parser->scope))
2417       || TREE_CODE (id) == TYPE_DECL)
2418     {
2419       cp_parser_abort_tentative_parse (parser);
2420       return false;
2421     }
2422   if (!cp_parser_parse_definitely (parser))
2423     return false;
2424
2425   /* Emit a diagnostic for the invalid type.  */
2426   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427                                         id, token->location);
2428   /* Skip to the end of the declaration; there's no point in
2429      trying to process it.  */
2430   cp_parser_skip_to_end_of_block_or_statement (parser);
2431   return true;
2432 }
2433
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2436    are doing error recovery. Returns -1 if OR_COMMA is true and we
2437    found an unnested comma.  */
2438
2439 static int
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2441                                        bool recovering,
2442                                        bool or_comma,
2443                                        bool consume_paren)
2444 {
2445   unsigned paren_depth = 0;
2446   unsigned brace_depth = 0;
2447
2448   if (recovering && !or_comma
2449       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2450     return 0;
2451
2452   while (true)
2453     {
2454       cp_token * token = cp_lexer_peek_token (parser->lexer);
2455
2456       switch (token->type)
2457         {
2458         case CPP_EOF:
2459         case CPP_PRAGMA_EOL:
2460           /* If we've run out of tokens, then there is no closing `)'.  */
2461           return 0;
2462
2463         case CPP_SEMICOLON:
2464           /* This matches the processing in skip_to_end_of_statement.  */
2465           if (!brace_depth)
2466             return 0;
2467           break;
2468
2469         case CPP_OPEN_BRACE:
2470           ++brace_depth;
2471           break;
2472         case CPP_CLOSE_BRACE:
2473           if (!brace_depth--)
2474             return 0;
2475           break;
2476
2477         case CPP_COMMA:
2478           if (recovering && or_comma && !brace_depth && !paren_depth)
2479             return -1;
2480           break;
2481
2482         case CPP_OPEN_PAREN:
2483           if (!brace_depth)
2484             ++paren_depth;
2485           break;
2486
2487         case CPP_CLOSE_PAREN:
2488           if (!brace_depth && !paren_depth--)
2489             {
2490               if (consume_paren)
2491                 cp_lexer_consume_token (parser->lexer);
2492               return 1;
2493             }
2494           break;
2495
2496         default:
2497           break;
2498         }
2499
2500       /* Consume the token.  */
2501       cp_lexer_consume_token (parser->lexer);
2502     }
2503 }
2504
2505 /* Consume tokens until we reach the end of the current statement.
2506    Normally, that will be just before consuming a `;'.  However, if a
2507    non-nested `}' comes first, then we stop before consuming that.  */
2508
2509 static void
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2511 {
2512   unsigned nesting_depth = 0;
2513
2514   while (true)
2515     {
2516       cp_token *token = cp_lexer_peek_token (parser->lexer);
2517
2518       switch (token->type)
2519         {
2520         case CPP_EOF:
2521         case CPP_PRAGMA_EOL:
2522           /* If we've run out of tokens, stop.  */
2523           return;
2524
2525         case CPP_SEMICOLON:
2526           /* If the next token is a `;', we have reached the end of the
2527              statement.  */
2528           if (!nesting_depth)
2529             return;
2530           break;
2531
2532         case CPP_CLOSE_BRACE:
2533           /* If this is a non-nested '}', stop before consuming it.
2534              That way, when confronted with something like:
2535
2536                { 3 + }
2537
2538              we stop before consuming the closing '}', even though we
2539              have not yet reached a `;'.  */
2540           if (nesting_depth == 0)
2541             return;
2542
2543           /* If it is the closing '}' for a block that we have
2544              scanned, stop -- but only after consuming the token.
2545              That way given:
2546
2547                 void f g () { ... }
2548                 typedef int I;
2549
2550              we will stop after the body of the erroneously declared
2551              function, but before consuming the following `typedef'
2552              declaration.  */
2553           if (--nesting_depth == 0)
2554             {
2555               cp_lexer_consume_token (parser->lexer);
2556               return;
2557             }
2558
2559         case CPP_OPEN_BRACE:
2560           ++nesting_depth;
2561           break;
2562
2563         default:
2564           break;
2565         }
2566
2567       /* Consume the token.  */
2568       cp_lexer_consume_token (parser->lexer);
2569     }
2570 }
2571
2572 /* This function is called at the end of a statement or declaration.
2573    If the next token is a semicolon, it is consumed; otherwise, error
2574    recovery is attempted.  */
2575
2576 static void
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578 {
2579   /* Look for the trailing `;'.  */
2580   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2581     {
2582       /* If there is additional (erroneous) input, skip to the end of
2583          the statement.  */
2584       cp_parser_skip_to_end_of_statement (parser);
2585       /* If the next token is now a `;', consume it.  */
2586       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587         cp_lexer_consume_token (parser->lexer);
2588     }
2589 }
2590
2591 /* Skip tokens until we have consumed an entire block, or until we
2592    have consumed a non-nested `;'.  */
2593
2594 static void
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2596 {
2597   int nesting_depth = 0;
2598
2599   while (nesting_depth >= 0)
2600     {
2601       cp_token *token = cp_lexer_peek_token (parser->lexer);
2602
2603       switch (token->type)
2604         {
2605         case CPP_EOF:
2606         case CPP_PRAGMA_EOL:
2607           /* If we've run out of tokens, stop.  */
2608           return;
2609
2610         case CPP_SEMICOLON:
2611           /* Stop if this is an unnested ';'. */
2612           if (!nesting_depth)
2613             nesting_depth = -1;
2614           break;
2615
2616         case CPP_CLOSE_BRACE:
2617           /* Stop if this is an unnested '}', or closes the outermost
2618              nesting level.  */
2619           nesting_depth--;
2620           if (!nesting_depth)
2621             nesting_depth = -1;
2622           break;
2623
2624         case CPP_OPEN_BRACE:
2625           /* Nest. */
2626           nesting_depth++;
2627           break;
2628
2629         default:
2630           break;
2631         }
2632
2633       /* Consume the token.  */
2634       cp_lexer_consume_token (parser->lexer);
2635     }
2636 }
2637
2638 /* Skip tokens until a non-nested closing curly brace is the next
2639    token, or there are no more tokens. Return true in the first case,
2640    false otherwise.  */
2641
2642 static bool
2643 cp_parser_skip_to_closing_brace (cp_parser *parser)
2644 {
2645   unsigned nesting_depth = 0;
2646
2647   while (true)
2648     {
2649       cp_token *token = cp_lexer_peek_token (parser->lexer);
2650
2651       switch (token->type)
2652         {
2653         case CPP_EOF:
2654         case CPP_PRAGMA_EOL:
2655           /* If we've run out of tokens, stop.  */
2656           return false;
2657
2658         case CPP_CLOSE_BRACE:
2659           /* If the next token is a non-nested `}', then we have reached
2660              the end of the current block.  */
2661           if (nesting_depth-- == 0)
2662             return true;
2663           break;
2664
2665         case CPP_OPEN_BRACE:
2666           /* If it the next token is a `{', then we are entering a new
2667              block.  Consume the entire block.  */
2668           ++nesting_depth;
2669           break;
2670
2671         default:
2672           break;
2673         }
2674
2675       /* Consume the token.  */
2676       cp_lexer_consume_token (parser->lexer);
2677     }
2678 }
2679
2680 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2681    parameter is the PRAGMA token, allowing us to purge the entire pragma
2682    sequence.  */
2683
2684 static void
2685 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2686 {
2687   cp_token *token;
2688
2689   parser->lexer->in_pragma = false;
2690
2691   do
2692     token = cp_lexer_consume_token (parser->lexer);
2693   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2694
2695   /* Ensure that the pragma is not parsed again.  */
2696   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2697 }
2698
2699 /* Require pragma end of line, resyncing with it as necessary.  The
2700    arguments are as for cp_parser_skip_to_pragma_eol.  */
2701
2702 static void
2703 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2704 {
2705   parser->lexer->in_pragma = false;
2706   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2707     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2708 }
2709
2710 /* This is a simple wrapper around make_typename_type. When the id is
2711    an unresolved identifier node, we can provide a superior diagnostic
2712    using cp_parser_diagnose_invalid_type_name.  */
2713
2714 static tree
2715 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2716                               tree id, location_t id_location)
2717 {
2718   tree result;
2719   if (TREE_CODE (id) == IDENTIFIER_NODE)
2720     {
2721       result = make_typename_type (scope, id, typename_type,
2722                                    /*complain=*/tf_none);
2723       if (result == error_mark_node)
2724         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2725       return result;
2726     }
2727   return make_typename_type (scope, id, typename_type, tf_error);
2728 }
2729
2730 /* This is a wrapper around the
2731    make_{pointer,ptrmem,reference}_declarator functions that decides
2732    which one to call based on the CODE and CLASS_TYPE arguments. The
2733    CODE argument should be one of the values returned by
2734    cp_parser_ptr_operator. */
2735 static cp_declarator *
2736 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2737                                     cp_cv_quals cv_qualifiers,
2738                                     cp_declarator *target)
2739 {
2740   if (code == ERROR_MARK)
2741     return cp_error_declarator;
2742
2743   if (code == INDIRECT_REF)
2744     if (class_type == NULL_TREE)
2745       return make_pointer_declarator (cv_qualifiers, target);
2746     else
2747       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2748   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2749     return make_reference_declarator (cv_qualifiers, target, false);
2750   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2751     return make_reference_declarator (cv_qualifiers, target, true);
2752   gcc_unreachable ();
2753 }
2754
2755 /* Create a new C++ parser.  */
2756
2757 static cp_parser *
2758 cp_parser_new (void)
2759 {
2760   cp_parser *parser;
2761   cp_lexer *lexer;
2762   unsigned i;
2763
2764   /* cp_lexer_new_main is called before calling ggc_alloc because
2765      cp_lexer_new_main might load a PCH file.  */
2766   lexer = cp_lexer_new_main ();
2767
2768   /* Initialize the binops_by_token so that we can get the tree
2769      directly from the token.  */
2770   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2771     binops_by_token[binops[i].token_type] = binops[i];
2772
2773   parser = GGC_CNEW (cp_parser);
2774   parser->lexer = lexer;
2775   parser->context = cp_parser_context_new (NULL);
2776
2777   /* For now, we always accept GNU extensions.  */
2778   parser->allow_gnu_extensions_p = 1;
2779
2780   /* The `>' token is a greater-than operator, not the end of a
2781      template-id.  */
2782   parser->greater_than_is_operator_p = true;
2783
2784   parser->default_arg_ok_p = true;
2785
2786   /* We are not parsing a constant-expression.  */
2787   parser->integral_constant_expression_p = false;
2788   parser->allow_non_integral_constant_expression_p = false;
2789   parser->non_integral_constant_expression_p = false;
2790
2791   /* Local variable names are not forbidden.  */
2792   parser->local_variables_forbidden_p = false;
2793
2794   /* We are not processing an `extern "C"' declaration.  */
2795   parser->in_unbraced_linkage_specification_p = false;
2796
2797   /* We are not processing a declarator.  */
2798   parser->in_declarator_p = false;
2799
2800   /* We are not processing a template-argument-list.  */
2801   parser->in_template_argument_list_p = false;
2802
2803   /* We are not in an iteration statement.  */
2804   parser->in_statement = 0;
2805
2806   /* We are not in a switch statement.  */
2807   parser->in_switch_statement_p = false;
2808
2809   /* We are not parsing a type-id inside an expression.  */
2810   parser->in_type_id_in_expr_p = false;
2811
2812   /* Declarations aren't implicitly extern "C".  */
2813   parser->implicit_extern_c = false;
2814
2815   /* String literals should be translated to the execution character set.  */
2816   parser->translate_strings_p = true;
2817
2818   /* We are not parsing a function body.  */
2819   parser->in_function_body = false;
2820
2821   /* The unparsed function queue is empty.  */
2822   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2823
2824   /* There are no classes being defined.  */
2825   parser->num_classes_being_defined = 0;
2826
2827   /* No template parameters apply.  */
2828   parser->num_template_parameter_lists = 0;
2829
2830   return parser;
2831 }
2832
2833 /* Create a cp_lexer structure which will emit the tokens in CACHE
2834    and push it onto the parser's lexer stack.  This is used for delayed
2835    parsing of in-class method bodies and default arguments, and should
2836    not be confused with tentative parsing.  */
2837 static void
2838 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2839 {
2840   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2841   lexer->next = parser->lexer;
2842   parser->lexer = lexer;
2843
2844   /* Move the current source position to that of the first token in the
2845      new lexer.  */
2846   cp_lexer_set_source_position_from_token (lexer->next_token);
2847 }
2848
2849 /* Pop the top lexer off the parser stack.  This is never used for the
2850    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2851 static void
2852 cp_parser_pop_lexer (cp_parser *parser)
2853 {
2854   cp_lexer *lexer = parser->lexer;
2855   parser->lexer = lexer->next;
2856   cp_lexer_destroy (lexer);
2857
2858   /* Put the current source position back where it was before this
2859      lexer was pushed.  */
2860   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2861 }
2862
2863 /* Lexical conventions [gram.lex]  */
2864
2865 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2866    identifier.  */
2867
2868 static tree
2869 cp_parser_identifier (cp_parser* parser)
2870 {
2871   cp_token *token;
2872
2873   /* Look for the identifier.  */
2874   token = cp_parser_require (parser, CPP_NAME, "identifier");
2875   /* Return the value.  */
2876   return token ? token->u.value : error_mark_node;
2877 }
2878
2879 /* Parse a sequence of adjacent string constants.  Returns a
2880    TREE_STRING representing the combined, nul-terminated string
2881    constant.  If TRANSLATE is true, translate the string to the
2882    execution character set.  If WIDE_OK is true, a wide string is
2883    invalid here.
2884
2885    C++98 [lex.string] says that if a narrow string literal token is
2886    adjacent to a wide string literal token, the behavior is undefined.
2887    However, C99 6.4.5p4 says that this results in a wide string literal.
2888    We follow C99 here, for consistency with the C front end.
2889
2890    This code is largely lifted from lex_string() in c-lex.c.
2891
2892    FUTURE: ObjC++ will need to handle @-strings here.  */
2893 static tree
2894 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2895 {
2896   tree value;
2897   size_t count;
2898   struct obstack str_ob;
2899   cpp_string str, istr, *strs;
2900   cp_token *tok;
2901   enum cpp_ttype type;
2902
2903   tok = cp_lexer_peek_token (parser->lexer);
2904   if (!cp_parser_is_string_literal (tok))
2905     {
2906       cp_parser_error (parser, "expected string-literal");
2907       return error_mark_node;
2908     }
2909
2910   type = tok->type;
2911
2912   /* Try to avoid the overhead of creating and destroying an obstack
2913      for the common case of just one string.  */
2914   if (!cp_parser_is_string_literal
2915       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2916     {
2917       cp_lexer_consume_token (parser->lexer);
2918
2919       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2920       str.len = TREE_STRING_LENGTH (tok->u.value);
2921       count = 1;
2922
2923       strs = &str;
2924     }
2925   else
2926     {
2927       gcc_obstack_init (&str_ob);
2928       count = 0;
2929
2930       do
2931         {
2932           cp_lexer_consume_token (parser->lexer);
2933           count++;
2934           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2935           str.len = TREE_STRING_LENGTH (tok->u.value);
2936
2937           if (type != tok->type)
2938             {
2939               if (type == CPP_STRING)
2940                 type = tok->type;
2941               else if (tok->type != CPP_STRING)
2942                 error ("%Hunsupported non-standard concatenation "
2943                        "of string literals", &tok->location);
2944             }
2945
2946           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2947
2948           tok = cp_lexer_peek_token (parser->lexer);
2949         }
2950       while (cp_parser_is_string_literal (tok));
2951
2952       strs = (cpp_string *) obstack_finish (&str_ob);
2953     }
2954
2955   if (type != CPP_STRING && !wide_ok)
2956     {
2957       cp_parser_error (parser, "a wide string is invalid in this context");
2958       type = CPP_STRING;
2959     }
2960
2961   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2962       (parse_in, strs, count, &istr, type))
2963     {
2964       value = build_string (istr.len, (const char *)istr.text);
2965       free (CONST_CAST (unsigned char *, istr.text));
2966
2967       switch (type)
2968         {
2969         default:
2970         case CPP_STRING:
2971           TREE_TYPE (value) = char_array_type_node;
2972           break;
2973         case CPP_STRING16:
2974           TREE_TYPE (value) = char16_array_type_node;
2975           break;
2976         case CPP_STRING32:
2977           TREE_TYPE (value) = char32_array_type_node;
2978           break;
2979         case CPP_WSTRING:
2980           TREE_TYPE (value) = wchar_array_type_node;
2981           break;
2982         }
2983
2984       value = fix_string_type (value);
2985     }
2986   else
2987     /* cpp_interpret_string has issued an error.  */
2988     value = error_mark_node;
2989
2990   if (count > 1)
2991     obstack_free (&str_ob, 0);
2992
2993   return value;
2994 }
2995
2996
2997 /* Basic concepts [gram.basic]  */
2998
2999 /* Parse a translation-unit.
3000
3001    translation-unit:
3002      declaration-seq [opt]
3003
3004    Returns TRUE if all went well.  */
3005
3006 static bool
3007 cp_parser_translation_unit (cp_parser* parser)
3008 {
3009   /* The address of the first non-permanent object on the declarator
3010      obstack.  */
3011   static void *declarator_obstack_base;
3012
3013   bool success;
3014
3015   /* Create the declarator obstack, if necessary.  */
3016   if (!cp_error_declarator)
3017     {
3018       gcc_obstack_init (&declarator_obstack);
3019       /* Create the error declarator.  */
3020       cp_error_declarator = make_declarator (cdk_error);
3021       /* Create the empty parameter list.  */
3022       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3023       /* Remember where the base of the declarator obstack lies.  */
3024       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3025     }
3026
3027   cp_parser_declaration_seq_opt (parser);
3028
3029   /* If there are no tokens left then all went well.  */
3030   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3031     {
3032       /* Get rid of the token array; we don't need it any more.  */
3033       cp_lexer_destroy (parser->lexer);
3034       parser->lexer = NULL;
3035
3036       /* This file might have been a context that's implicitly extern
3037          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3038       if (parser->implicit_extern_c)
3039         {
3040           pop_lang_context ();
3041           parser->implicit_extern_c = false;
3042         }
3043
3044       /* Finish up.  */
3045       finish_translation_unit ();
3046
3047       success = true;
3048     }
3049   else
3050     {
3051       cp_parser_error (parser, "expected declaration");
3052       success = false;
3053     }
3054
3055   /* Make sure the declarator obstack was fully cleaned up.  */
3056   gcc_assert (obstack_next_free (&declarator_obstack)
3057               == declarator_obstack_base);
3058
3059   /* All went well.  */
3060   return success;
3061 }
3062
3063 /* Expressions [gram.expr] */
3064
3065 /* Parse a primary-expression.
3066
3067    primary-expression:
3068      literal
3069      this
3070      ( expression )
3071      id-expression
3072
3073    GNU Extensions:
3074
3075    primary-expression:
3076      ( compound-statement )
3077      __builtin_va_arg ( assignment-expression , type-id )
3078      __builtin_offsetof ( type-id , offsetof-expression )
3079
3080    C++ Extensions:
3081      __has_nothrow_assign ( type-id )   
3082      __has_nothrow_constructor ( type-id )
3083      __has_nothrow_copy ( type-id )
3084      __has_trivial_assign ( type-id )   
3085      __has_trivial_constructor ( type-id )
3086      __has_trivial_copy ( type-id )
3087      __has_trivial_destructor ( type-id )
3088      __has_virtual_destructor ( type-id )     
3089      __is_abstract ( type-id )
3090      __is_base_of ( type-id , type-id )
3091      __is_class ( type-id )
3092      __is_convertible_to ( type-id , type-id )     
3093      __is_empty ( type-id )
3094      __is_enum ( type-id )
3095      __is_pod ( type-id )
3096      __is_polymorphic ( type-id )
3097      __is_union ( type-id )
3098
3099    Objective-C++ Extension:
3100
3101    primary-expression:
3102      objc-expression
3103
3104    literal:
3105      __null
3106
3107    ADDRESS_P is true iff this expression was immediately preceded by
3108    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3109    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3110    true iff this expression is a template argument.
3111
3112    Returns a representation of the expression.  Upon return, *IDK
3113    indicates what kind of id-expression (if any) was present.  */
3114
3115 static tree
3116 cp_parser_primary_expression (cp_parser *parser,
3117                               bool address_p,
3118                               bool cast_p,
3119                               bool template_arg_p,
3120                               cp_id_kind *idk)
3121 {
3122   cp_token *token = NULL;
3123
3124   /* Assume the primary expression is not an id-expression.  */
3125   *idk = CP_ID_KIND_NONE;
3126
3127   /* Peek at the next token.  */
3128   token = cp_lexer_peek_token (parser->lexer);
3129   switch (token->type)
3130     {
3131       /* literal:
3132            integer-literal
3133            character-literal
3134            floating-literal
3135            string-literal
3136            boolean-literal  */
3137     case CPP_CHAR:
3138     case CPP_CHAR16:
3139     case CPP_CHAR32:
3140     case CPP_WCHAR:
3141     case CPP_NUMBER:
3142       token = cp_lexer_consume_token (parser->lexer);
3143       /* Floating-point literals are only allowed in an integral
3144          constant expression if they are cast to an integral or
3145          enumeration type.  */
3146       if (TREE_CODE (token->u.value) == REAL_CST
3147           && parser->integral_constant_expression_p
3148           && pedantic)
3149         {
3150           /* CAST_P will be set even in invalid code like "int(2.7 +
3151              ...)".   Therefore, we have to check that the next token
3152              is sure to end the cast.  */
3153           if (cast_p)
3154             {
3155               cp_token *next_token;
3156
3157               next_token = cp_lexer_peek_token (parser->lexer);
3158               if (/* The comma at the end of an
3159                      enumerator-definition.  */
3160                   next_token->type != CPP_COMMA
3161                   /* The curly brace at the end of an enum-specifier.  */
3162                   && next_token->type != CPP_CLOSE_BRACE
3163                   /* The end of a statement.  */
3164                   && next_token->type != CPP_SEMICOLON
3165                   /* The end of the cast-expression.  */
3166                   && next_token->type != CPP_CLOSE_PAREN
3167                   /* The end of an array bound.  */
3168                   && next_token->type != CPP_CLOSE_SQUARE
3169                   /* The closing ">" in a template-argument-list.  */
3170                   && (next_token->type != CPP_GREATER
3171                       || parser->greater_than_is_operator_p)
3172                   /* C++0x only: A ">>" treated like two ">" tokens,
3173                      in a template-argument-list.  */
3174                   && (next_token->type != CPP_RSHIFT
3175                       || (cxx_dialect == cxx98)
3176                       || parser->greater_than_is_operator_p))
3177                 cast_p = false;
3178             }
3179
3180           /* If we are within a cast, then the constraint that the
3181              cast is to an integral or enumeration type will be
3182              checked at that point.  If we are not within a cast, then
3183              this code is invalid.  */
3184           if (!cast_p)
3185             cp_parser_non_integral_constant_expression
3186               (parser, "floating-point literal");
3187         }
3188       return token->u.value;
3189
3190     case CPP_STRING:
3191     case CPP_STRING16:
3192     case CPP_STRING32:
3193     case CPP_WSTRING:
3194       /* ??? Should wide strings be allowed when parser->translate_strings_p
3195          is false (i.e. in attributes)?  If not, we can kill the third
3196          argument to cp_parser_string_literal.  */
3197       return cp_parser_string_literal (parser,
3198                                        parser->translate_strings_p,
3199                                        true);
3200
3201     case CPP_OPEN_PAREN:
3202       {
3203         tree expr;
3204         bool saved_greater_than_is_operator_p;
3205
3206         /* Consume the `('.  */
3207         cp_lexer_consume_token (parser->lexer);
3208         /* Within a parenthesized expression, a `>' token is always
3209            the greater-than operator.  */
3210         saved_greater_than_is_operator_p
3211           = parser->greater_than_is_operator_p;
3212         parser->greater_than_is_operator_p = true;
3213         /* If we see `( { ' then we are looking at the beginning of
3214            a GNU statement-expression.  */
3215         if (cp_parser_allow_gnu_extensions_p (parser)
3216             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3217           {
3218             /* Statement-expressions are not allowed by the standard.  */
3219             pedwarn (token->location, OPT_pedantic, 
3220                      "ISO C++ forbids braced-groups within expressions");
3221
3222             /* And they're not allowed outside of a function-body; you
3223                cannot, for example, write:
3224
3225                  int i = ({ int j = 3; j + 1; });
3226
3227                at class or namespace scope.  */
3228             if (!parser->in_function_body
3229                 || parser->in_template_argument_list_p)
3230               {
3231                 error ("%Hstatement-expressions are not allowed outside "
3232                        "functions nor in template-argument lists",
3233                        &token->location);
3234                 cp_parser_skip_to_end_of_block_or_statement (parser);
3235                 expr = error_mark_node;
3236               }
3237             else
3238               {
3239                 /* Start the statement-expression.  */
3240                 expr = begin_stmt_expr ();
3241                 /* Parse the compound-statement.  */
3242                 cp_parser_compound_statement (parser, expr, false);
3243                 /* Finish up.  */
3244                 expr = finish_stmt_expr (expr, false);
3245               }
3246           }
3247         else
3248           {
3249             /* Parse the parenthesized expression.  */
3250             expr = cp_parser_expression (parser, cast_p);
3251             /* Let the front end know that this expression was
3252                enclosed in parentheses. This matters in case, for
3253                example, the expression is of the form `A::B', since
3254                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3255                not.  */
3256             finish_parenthesized_expr (expr);
3257           }
3258         /* The `>' token might be the end of a template-id or
3259            template-parameter-list now.  */
3260         parser->greater_than_is_operator_p
3261           = saved_greater_than_is_operator_p;
3262         /* Consume the `)'.  */
3263         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3264           cp_parser_skip_to_end_of_statement (parser);
3265
3266         return expr;
3267       }
3268
3269     case CPP_KEYWORD:
3270       switch (token->keyword)
3271         {
3272           /* These two are the boolean literals.  */
3273         case RID_TRUE:
3274           cp_lexer_consume_token (parser->lexer);
3275           return boolean_true_node;
3276         case RID_FALSE:
3277           cp_lexer_consume_token (parser->lexer);
3278           return boolean_false_node;
3279
3280           /* The `__null' literal.  */
3281         case RID_NULL:
3282           cp_lexer_consume_token (parser->lexer);
3283           return null_node;
3284
3285           /* Recognize the `this' keyword.  */
3286         case RID_THIS:
3287           cp_lexer_consume_token (parser->lexer);
3288           if (parser->local_variables_forbidden_p)
3289             {
3290               error ("%H%<this%> may not be used in this context",
3291                      &token->location);
3292               return error_mark_node;
3293             }
3294           /* Pointers cannot appear in constant-expressions.  */
3295           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3296             return error_mark_node;
3297           return finish_this_expr ();
3298
3299           /* The `operator' keyword can be the beginning of an
3300              id-expression.  */
3301         case RID_OPERATOR:
3302           goto id_expression;
3303
3304         case RID_FUNCTION_NAME:
3305         case RID_PRETTY_FUNCTION_NAME:
3306         case RID_C99_FUNCTION_NAME:
3307           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3308              __func__ are the names of variables -- but they are
3309              treated specially.  Therefore, they are handled here,
3310              rather than relying on the generic id-expression logic
3311              below.  Grammatically, these names are id-expressions.
3312
3313              Consume the token.  */
3314           token = cp_lexer_consume_token (parser->lexer);
3315           /* Look up the name.  */
3316           return finish_fname (token->u.value);
3317
3318         case RID_VA_ARG:
3319           {
3320             tree expression;
3321             tree type;
3322
3323             /* The `__builtin_va_arg' construct is used to handle
3324                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3325             cp_lexer_consume_token (parser->lexer);
3326             /* Look for the opening `('.  */
3327             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3328             /* Now, parse the assignment-expression.  */
3329             expression = cp_parser_assignment_expression (parser,
3330                                                           /*cast_p=*/false);
3331             /* Look for the `,'.  */
3332             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3333             /* Parse the type-id.  */
3334             type = cp_parser_type_id (parser);
3335             /* Look for the closing `)'.  */
3336             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3337             /* Using `va_arg' in a constant-expression is not
3338                allowed.  */
3339             if (cp_parser_non_integral_constant_expression (parser,
3340                                                             "%<va_arg%>"))
3341               return error_mark_node;
3342             return build_x_va_arg (expression, type);
3343           }
3344
3345         case RID_OFFSETOF:
3346           return cp_parser_builtin_offsetof (parser);
3347
3348         case RID_HAS_NOTHROW_ASSIGN:
3349         case RID_HAS_NOTHROW_CONSTRUCTOR:
3350         case RID_HAS_NOTHROW_COPY:        
3351         case RID_HAS_TRIVIAL_ASSIGN:
3352         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3353         case RID_HAS_TRIVIAL_COPY:        
3354         case RID_HAS_TRIVIAL_DESTRUCTOR:
3355         case RID_HAS_VIRTUAL_DESTRUCTOR:
3356         case RID_IS_ABSTRACT:
3357         case RID_IS_BASE_OF:
3358         case RID_IS_CLASS:
3359         case RID_IS_CONVERTIBLE_TO:
3360         case RID_IS_EMPTY:
3361         case RID_IS_ENUM:
3362         case RID_IS_POD:
3363         case RID_IS_POLYMORPHIC:
3364         case RID_IS_UNION:
3365           return cp_parser_trait_expr (parser, token->keyword);
3366
3367         /* Objective-C++ expressions.  */
3368         case RID_AT_ENCODE:
3369         case RID_AT_PROTOCOL:
3370         case RID_AT_SELECTOR:
3371           return cp_parser_objc_expression (parser);
3372
3373         default:
3374           cp_parser_error (parser, "expected primary-expression");
3375           return error_mark_node;
3376         }
3377
3378       /* An id-expression can start with either an identifier, a
3379          `::' as the beginning of a qualified-id, or the "operator"
3380          keyword.  */
3381     case CPP_NAME:
3382     case CPP_SCOPE:
3383     case CPP_TEMPLATE_ID:
3384     case CPP_NESTED_NAME_SPECIFIER:
3385       {
3386         tree id_expression;
3387         tree decl;
3388         const char *error_msg;
3389         bool template_p;
3390         bool done;
3391         cp_token *id_expr_token;
3392
3393       id_expression:
3394         /* Parse the id-expression.  */
3395         id_expression
3396           = cp_parser_id_expression (parser,
3397                                      /*template_keyword_p=*/false,
3398                                      /*check_dependency_p=*/true,
3399                                      &template_p,
3400                                      /*declarator_p=*/false,
3401                                      /*optional_p=*/false);
3402         if (id_expression == error_mark_node)
3403           return error_mark_node;
3404         id_expr_token = token;
3405         token = cp_lexer_peek_token (parser->lexer);
3406         done = (token->type != CPP_OPEN_SQUARE
3407                 && token->type != CPP_OPEN_PAREN
3408                 && token->type != CPP_DOT
3409                 && token->type != CPP_DEREF
3410                 && token->type != CPP_PLUS_PLUS
3411                 && token->type != CPP_MINUS_MINUS);
3412         /* If we have a template-id, then no further lookup is
3413            required.  If the template-id was for a template-class, we
3414            will sometimes have a TYPE_DECL at this point.  */
3415         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3416                  || TREE_CODE (id_expression) == TYPE_DECL)
3417           decl = id_expression;
3418         /* Look up the name.  */
3419         else
3420           {
3421             tree ambiguous_decls;
3422
3423             decl = cp_parser_lookup_name (parser, id_expression,
3424                                           none_type,
3425                                           template_p,
3426                                           /*is_namespace=*/false,
3427                                           /*check_dependency=*/true,
3428                                           &ambiguous_decls,
3429                                           id_expr_token->location);
3430             /* If the lookup was ambiguous, an error will already have
3431                been issued.  */
3432             if (ambiguous_decls)
3433               return error_mark_node;
3434
3435             /* In Objective-C++, an instance variable (ivar) may be preferred
3436                to whatever cp_parser_lookup_name() found.  */
3437             decl = objc_lookup_ivar (decl, id_expression);
3438
3439             /* If name lookup gives us a SCOPE_REF, then the
3440                qualifying scope was dependent.  */
3441             if (TREE_CODE (decl) == SCOPE_REF)
3442               {
3443                 /* At this point, we do not know if DECL is a valid
3444                    integral constant expression.  We assume that it is
3445                    in fact such an expression, so that code like:
3446
3447                       template <int N> struct A {
3448                         int a[B<N>::i];
3449                       };
3450                      
3451                    is accepted.  At template-instantiation time, we
3452                    will check that B<N>::i is actually a constant.  */
3453                 return decl;
3454               }
3455             /* Check to see if DECL is a local variable in a context
3456                where that is forbidden.  */
3457             if (parser->local_variables_forbidden_p
3458                 && local_variable_p (decl))
3459               {
3460                 /* It might be that we only found DECL because we are
3461                    trying to be generous with pre-ISO scoping rules.
3462                    For example, consider:
3463
3464                      int i;
3465                      void g() {
3466                        for (int i = 0; i < 10; ++i) {}
3467                        extern void f(int j = i);
3468                      }
3469
3470                    Here, name look up will originally find the out
3471                    of scope `i'.  We need to issue a warning message,
3472                    but then use the global `i'.  */
3473                 decl = check_for_out_of_scope_variable (decl);
3474                 if (local_variable_p (decl))
3475                   {
3476                     error ("%Hlocal variable %qD may not appear in this context",
3477                            &id_expr_token->location, decl);
3478                     return error_mark_node;
3479                   }
3480               }
3481           }
3482
3483         decl = (finish_id_expression
3484                 (id_expression, decl, parser->scope,
3485                  idk,
3486                  parser->integral_constant_expression_p,
3487                  parser->allow_non_integral_constant_expression_p,
3488                  &parser->non_integral_constant_expression_p,
3489                  template_p, done, address_p,
3490                  template_arg_p,
3491                  &error_msg,
3492                  id_expr_token->location));
3493         if (error_msg)
3494           cp_parser_error (parser, error_msg);
3495         return decl;
3496       }
3497
3498       /* Anything else is an error.  */
3499     default:
3500       /* ...unless we have an Objective-C++ message or string literal,
3501          that is.  */
3502       if (c_dialect_objc ()
3503           && (token->type == CPP_OPEN_SQUARE
3504               || token->type == CPP_OBJC_STRING))
3505         return cp_parser_objc_expression (parser);
3506
3507       cp_parser_error (parser, "expected primary-expression");
3508       return error_mark_node;
3509     }
3510 }
3511
3512 /* Parse an id-expression.
3513
3514    id-expression:
3515      unqualified-id
3516      qualified-id
3517
3518    qualified-id:
3519      :: [opt] nested-name-specifier template [opt] unqualified-id
3520      :: identifier
3521      :: operator-function-id
3522      :: template-id
3523
3524    Return a representation of the unqualified portion of the
3525    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3526    a `::' or nested-name-specifier.
3527
3528    Often, if the id-expression was a qualified-id, the caller will
3529    want to make a SCOPE_REF to represent the qualified-id.  This
3530    function does not do this in order to avoid wastefully creating
3531    SCOPE_REFs when they are not required.
3532
3533    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3534    `template' keyword.
3535
3536    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3537    uninstantiated templates.
3538
3539    If *TEMPLATE_P is non-NULL, it is set to true iff the
3540    `template' keyword is used to explicitly indicate that the entity
3541    named is a template.
3542
3543    If DECLARATOR_P is true, the id-expression is appearing as part of
3544    a declarator, rather than as part of an expression.  */
3545
3546 static tree
3547 cp_parser_id_expression (cp_parser *parser,
3548                          bool template_keyword_p,
3549                          bool check_dependency_p,
3550                          bool *template_p,
3551                          bool declarator_p,
3552                          bool optional_p)
3553 {
3554   bool global_scope_p;
3555   bool nested_name_specifier_p;
3556
3557   /* Assume the `template' keyword was not used.  */
3558   if (template_p)
3559     *template_p = template_keyword_p;
3560
3561   /* Look for the optional `::' operator.  */
3562   global_scope_p
3563     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3564        != NULL_TREE);
3565   /* Look for the optional nested-name-specifier.  */
3566   nested_name_specifier_p
3567     = (cp_parser_nested_name_specifier_opt (parser,
3568                                             /*typename_keyword_p=*/false,
3569                                             check_dependency_p,
3570                                             /*type_p=*/false,
3571                                             declarator_p)
3572        != NULL_TREE);
3573   /* If there is a nested-name-specifier, then we are looking at
3574      the first qualified-id production.  */
3575   if (nested_name_specifier_p)
3576     {
3577       tree saved_scope;
3578       tree saved_object_scope;
3579       tree saved_qualifying_scope;
3580       tree unqualified_id;
3581       bool is_template;
3582
3583       /* See if the next token is the `template' keyword.  */
3584       if (!template_p)
3585         template_p = &is_template;
3586       *template_p = cp_parser_optional_template_keyword (parser);
3587       /* Name lookup we do during the processing of the
3588          unqualified-id might obliterate SCOPE.  */
3589       saved_scope = parser->scope;
3590       saved_object_scope = parser->object_scope;
3591       saved_qualifying_scope = parser->qualifying_scope;
3592       /* Process the final unqualified-id.  */
3593       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3594                                                  check_dependency_p,
3595                                                  declarator_p,
3596                                                  /*optional_p=*/false);
3597       /* Restore the SAVED_SCOPE for our caller.  */
3598       parser->scope = saved_scope;
3599       parser->object_scope = saved_object_scope;
3600       parser->qualifying_scope = saved_qualifying_scope;
3601
3602       return unqualified_id;
3603     }
3604   /* Otherwise, if we are in global scope, then we are looking at one
3605      of the other qualified-id productions.  */
3606   else if (global_scope_p)
3607     {
3608       cp_token *token;
3609       tree id;
3610
3611       /* Peek at the next token.  */
3612       token = cp_lexer_peek_token (parser->lexer);
3613
3614       /* If it's an identifier, and the next token is not a "<", then
3615          we can avoid the template-id case.  This is an optimization
3616          for this common case.  */
3617       if (token->type == CPP_NAME
3618           && !cp_parser_nth_token_starts_template_argument_list_p
3619                (parser, 2))
3620         return cp_parser_identifier (parser);
3621
3622       cp_parser_parse_tentatively (parser);
3623       /* Try a template-id.  */
3624       id = cp_parser_template_id (parser,
3625                                   /*template_keyword_p=*/false,
3626                                   /*check_dependency_p=*/true,
3627                                   declarator_p);
3628       /* If that worked, we're done.  */
3629       if (cp_parser_parse_definitely (parser))
3630         return id;
3631
3632       /* Peek at the next token.  (Changes in the token buffer may
3633          have invalidated the pointer obtained above.)  */
3634       token = cp_lexer_peek_token (parser->lexer);
3635
3636       switch (token->type)
3637         {
3638         case CPP_NAME:
3639           return cp_parser_identifier (parser);
3640
3641         case CPP_KEYWORD:
3642           if (token->keyword == RID_OPERATOR)
3643             return cp_parser_operator_function_id (parser);
3644           /* Fall through.  */
3645
3646         default:
3647           cp_parser_error (parser, "expected id-expression");
3648           return error_mark_node;
3649         }
3650     }
3651   else
3652     return cp_parser_unqualified_id (parser, template_keyword_p,
3653                                      /*check_dependency_p=*/true,
3654                                      declarator_p,
3655                                      optional_p);
3656 }
3657
3658 /* Parse an unqualified-id.
3659
3660    unqualified-id:
3661      identifier
3662      operator-function-id
3663      conversion-function-id
3664      ~ class-name
3665      template-id
3666
3667    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3668    keyword, in a construct like `A::template ...'.
3669
3670    Returns a representation of unqualified-id.  For the `identifier'
3671    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3672    production a BIT_NOT_EXPR is returned; the operand of the
3673    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3674    other productions, see the documentation accompanying the
3675    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3676    names are looked up in uninstantiated templates.  If DECLARATOR_P
3677    is true, the unqualified-id is appearing as part of a declarator,
3678    rather than as part of an expression.  */
3679
3680 static tree
3681 cp_parser_unqualified_id (cp_parser* parser,
3682                           bool template_keyword_p,
3683                           bool check_dependency_p,
3684                           bool declarator_p,
3685                           bool optional_p)
3686 {
3687   cp_token *token;
3688
3689   /* Peek at the next token.  */
3690   token = cp_lexer_peek_token (parser->lexer);
3691
3692   switch (token->type)
3693     {
3694     case CPP_NAME:
3695       {
3696         tree id;
3697
3698         /* We don't know yet whether or not this will be a
3699            template-id.  */
3700         cp_parser_parse_tentatively (parser);
3701         /* Try a template-id.  */
3702         id = cp_parser_template_id (parser, template_keyword_p,
3703                                     check_dependency_p,
3704                                     declarator_p);
3705         /* If it worked, we're done.  */
3706         if (cp_parser_parse_definitely (parser))
3707           return id;
3708         /* Otherwise, it's an ordinary identifier.  */
3709         return cp_parser_identifier (parser);
3710       }
3711
3712     case CPP_TEMPLATE_ID:
3713       return cp_parser_template_id (parser, template_keyword_p,
3714                                     check_dependency_p,
3715                                     declarator_p);
3716
3717     case CPP_COMPL:
3718       {
3719         tree type_decl;
3720         tree qualifying_scope;
3721         tree object_scope;
3722         tree scope;
3723         bool done;
3724
3725         /* Consume the `~' token.  */
3726         cp_lexer_consume_token (parser->lexer);
3727         /* Parse the class-name.  The standard, as written, seems to
3728            say that:
3729
3730              template <typename T> struct S { ~S (); };
3731              template <typename T> S<T>::~S() {}
3732
3733            is invalid, since `~' must be followed by a class-name, but
3734            `S<T>' is dependent, and so not known to be a class.
3735            That's not right; we need to look in uninstantiated
3736            templates.  A further complication arises from:
3737
3738              template <typename T> void f(T t) {
3739                t.T::~T();
3740              }
3741
3742            Here, it is not possible to look up `T' in the scope of `T'
3743            itself.  We must look in both the current scope, and the
3744            scope of the containing complete expression.
3745
3746            Yet another issue is:
3747
3748              struct S {
3749                int S;
3750                ~S();
3751              };
3752
3753              S::~S() {}
3754
3755            The standard does not seem to say that the `S' in `~S'
3756            should refer to the type `S' and not the data member
3757            `S::S'.  */
3758
3759         /* DR 244 says that we look up the name after the "~" in the
3760            same scope as we looked up the qualifying name.  That idea
3761            isn't fully worked out; it's more complicated than that.  */
3762         scope = parser->scope;
3763         object_scope = parser->object_scope;
3764         qualifying_scope = parser->qualifying_scope;
3765
3766         /* Check for invalid scopes.  */
3767         if (scope == error_mark_node)
3768           {
3769             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3770               cp_lexer_consume_token (parser->lexer);
3771             return error_mark_node;
3772           }
3773         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3774           {
3775             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3776               error ("%Hscope %qT before %<~%> is not a class-name",
3777                      &token->location, scope);
3778             cp_parser_simulate_error (parser);
3779             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3780               cp_lexer_consume_token (parser->lexer);
3781             return error_mark_node;
3782           }
3783         gcc_assert (!scope || TYPE_P (scope));
3784
3785         /* If the name is of the form "X::~X" it's OK.  */
3786         token = cp_lexer_peek_token (parser->lexer);
3787         if (scope
3788             && token->type == CPP_NAME
3789             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3790                 == CPP_OPEN_PAREN)
3791             && constructor_name_p (token->u.value, scope))
3792           {
3793             cp_lexer_consume_token (parser->lexer);
3794             return build_nt (BIT_NOT_EXPR, scope);
3795           }
3796
3797         /* If there was an explicit qualification (S::~T), first look
3798            in the scope given by the qualification (i.e., S).  */
3799         done = false;
3800         type_decl = NULL_TREE;
3801         if (scope)
3802           {
3803             cp_parser_parse_tentatively (parser);
3804             type_decl = cp_parser_class_name (parser,
3805                                               /*typename_keyword_p=*/false,
3806                                               /*template_keyword_p=*/false,
3807                                               none_type,
3808                                               /*check_dependency=*/false,
3809                                               /*class_head_p=*/false,
3810                                               declarator_p);
3811             if (cp_parser_parse_definitely (parser))
3812               done = true;
3813           }
3814         /* In "N::S::~S", look in "N" as well.  */
3815         if (!done && scope && qualifying_scope)
3816           {
3817             cp_parser_parse_tentatively (parser);
3818             parser->scope = qualifying_scope;
3819             parser->object_scope = NULL_TREE;
3820             parser->qualifying_scope = NULL_TREE;
3821             type_decl
3822               = cp_parser_class_name (parser,
3823                                       /*typename_keyword_p=*/false,
3824                                       /*template_keyword_p=*/false,
3825                                       none_type,
3826                                       /*check_dependency=*/false,
3827                                       /*class_head_p=*/false,
3828                                       declarator_p);
3829             if (cp_parser_parse_definitely (parser))
3830               done = true;
3831           }
3832         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3833         else if (!done && object_scope)
3834           {
3835             cp_parser_parse_tentatively (parser);
3836             parser->scope = object_scope;
3837             parser->object_scope = NULL_TREE;
3838             parser->qualifying_scope = NULL_TREE;
3839             type_decl
3840               = cp_parser_class_name (parser,
3841                                       /*typename_keyword_p=*/false,
3842                                       /*template_keyword_p=*/false,
3843                                       none_type,
3844                                       /*check_dependency=*/false,
3845                                       /*class_head_p=*/false,
3846                                       declarator_p);
3847             if (cp_parser_parse_definitely (parser))
3848               done = true;
3849           }
3850         /* Look in the surrounding context.  */
3851         if (!done)
3852           {
3853             parser->scope = NULL_TREE;
3854             parser->object_scope = NULL_TREE;
3855             parser->qualifying_scope = NULL_TREE;
3856             type_decl
3857               = cp_parser_class_name (parser,
3858                                       /*typename_keyword_p=*/false,
3859                                       /*template_keyword_p=*/false,
3860                                       none_type,
3861                                       /*check_dependency=*/false,
3862                                       /*class_head_p=*/false,
3863                                       declarator_p);
3864           }
3865         /* If an error occurred, assume that the name of the
3866            destructor is the same as the name of the qualifying
3867            class.  That allows us to keep parsing after running
3868            into ill-formed destructor names.  */
3869         if (type_decl == error_mark_node && scope)
3870           return build_nt (BIT_NOT_EXPR, scope);
3871         else if (type_decl == error_mark_node)
3872           return error_mark_node;
3873
3874         /* Check that destructor name and scope match.  */
3875         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3876           {
3877             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878               error ("%Hdeclaration of %<~%T%> as member of %qT",
3879                      &token->location, type_decl, scope);
3880             cp_parser_simulate_error (parser);
3881             return error_mark_node;
3882           }
3883
3884         /* [class.dtor]
3885
3886            A typedef-name that names a class shall not be used as the
3887            identifier in the declarator for a destructor declaration.  */
3888         if (declarator_p
3889             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3890             && !DECL_SELF_REFERENCE_P (type_decl)
3891             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3892           error ("%Htypedef-name %qD used as destructor declarator",
3893                  &token->location, type_decl);
3894
3895         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3896       }
3897
3898     case CPP_KEYWORD:
3899       if (token->keyword == RID_OPERATOR)
3900         {
3901           tree id;
3902
3903           /* This could be a template-id, so we try that first.  */
3904           cp_parser_parse_tentatively (parser);
3905           /* Try a template-id.  */
3906           id = cp_parser_template_id (parser, template_keyword_p,
3907                                       /*check_dependency_p=*/true,
3908                                       declarator_p);
3909           /* If that worked, we're done.  */
3910           if (cp_parser_parse_definitely (parser))
3911             return id;
3912           /* We still don't know whether we're looking at an
3913              operator-function-id or a conversion-function-id.  */
3914           cp_parser_parse_tentatively (parser);
3915           /* Try an operator-function-id.  */
3916           id = cp_parser_operator_function_id (parser);
3917           /* If that didn't work, try a conversion-function-id.  */
3918           if (!cp_parser_parse_definitely (parser))
3919             id = cp_parser_conversion_function_id (parser);
3920
3921           return id;
3922         }
3923       /* Fall through.  */
3924
3925     default:
3926       if (optional_p)
3927         return NULL_TREE;
3928       cp_parser_error (parser, "expected unqualified-id");
3929       return error_mark_node;
3930     }
3931 }
3932
3933 /* Parse an (optional) nested-name-specifier.
3934
3935    nested-name-specifier:
3936      class-or-namespace-name :: nested-name-specifier [opt]
3937      class-or-namespace-name :: template nested-name-specifier [opt]
3938
3939    PARSER->SCOPE should be set appropriately before this function is
3940    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3941    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3942    in name lookups.
3943
3944    Sets PARSER->SCOPE to the class (TYPE) or namespace
3945    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3946    it unchanged if there is no nested-name-specifier.  Returns the new
3947    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3948
3949    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3950    part of a declaration and/or decl-specifier.  */
3951
3952 static tree
3953 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3954                                      bool typename_keyword_p,
3955                                      bool check_dependency_p,
3956                                      bool type_p,
3957                                      bool is_declaration)
3958 {
3959   bool success = false;
3960   cp_token_position start = 0;
3961   cp_token *token;
3962
3963   /* Remember where the nested-name-specifier starts.  */
3964   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3965     {
3966       start = cp_lexer_token_position (parser->lexer, false);
3967       push_deferring_access_checks (dk_deferred);
3968     }
3969
3970   while (true)
3971     {
3972       tree new_scope;
3973       tree old_scope;
3974       tree saved_qualifying_scope;
3975       bool template_keyword_p;
3976
3977       /* Spot cases that cannot be the beginning of a
3978          nested-name-specifier.  */
3979       token = cp_lexer_peek_token (parser->lexer);
3980
3981       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3982          the already parsed nested-name-specifier.  */
3983       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3984         {
3985           /* Grab the nested-name-specifier and continue the loop.  */
3986           cp_parser_pre_parsed_nested_name_specifier (parser);
3987           /* If we originally encountered this nested-name-specifier
3988              with IS_DECLARATION set to false, we will not have
3989              resolved TYPENAME_TYPEs, so we must do so here.  */
3990           if (is_declaration
3991               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3992             {
3993               new_scope = resolve_typename_type (parser->scope,
3994                                                  /*only_current_p=*/false);
3995               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3996                 parser->scope = new_scope;
3997             }
3998           success = true;
3999           continue;
4000         }
4001
4002       /* Spot cases that cannot be the beginning of a
4003          nested-name-specifier.  On the second and subsequent times
4004          through the loop, we look for the `template' keyword.  */
4005       if (success && token->keyword == RID_TEMPLATE)
4006         ;
4007       /* A template-id can start a nested-name-specifier.  */
4008       else if (token->type == CPP_TEMPLATE_ID)
4009         ;
4010       else
4011         {
4012           /* If the next token is not an identifier, then it is
4013              definitely not a class-or-namespace-name.  */
4014           if (token->type != CPP_NAME)
4015             break;
4016           /* If the following token is neither a `<' (to begin a
4017              template-id), nor a `::', then we are not looking at a
4018              nested-name-specifier.  */
4019           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4020           if (token->type != CPP_SCOPE
4021               && !cp_parser_nth_token_starts_template_argument_list_p
4022                   (parser, 2))
4023             break;
4024         }
4025
4026       /* The nested-name-specifier is optional, so we parse
4027          tentatively.  */
4028       cp_parser_parse_tentatively (parser);
4029
4030       /* Look for the optional `template' keyword, if this isn't the
4031          first time through the loop.  */
4032       if (success)
4033         template_keyword_p = cp_parser_optional_template_keyword (parser);
4034       else
4035         template_keyword_p = false;
4036
4037       /* Save the old scope since the name lookup we are about to do
4038          might destroy it.  */
4039       old_scope = parser->scope;
4040       saved_qualifying_scope = parser->qualifying_scope;
4041       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4042          look up names in "X<T>::I" in order to determine that "Y" is
4043          a template.  So, if we have a typename at this point, we make
4044          an effort to look through it.  */
4045       if (is_declaration
4046           && !typename_keyword_p
4047           && parser->scope
4048           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4049         parser->scope = resolve_typename_type (parser->scope,
4050                                                /*only_current_p=*/false);
4051       /* Parse the qualifying entity.  */
4052       new_scope
4053         = cp_parser_class_or_namespace_name (parser,
4054                                              typename_keyword_p,
4055                                              template_keyword_p,
4056                                              check_dependency_p,
4057                                              type_p,
4058                                              is_declaration);
4059       /* Look for the `::' token.  */
4060       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4061
4062       /* If we found what we wanted, we keep going; otherwise, we're
4063          done.  */
4064       if (!cp_parser_parse_definitely (parser))
4065         {
4066           bool error_p = false;
4067
4068           /* Restore the OLD_SCOPE since it was valid before the
4069              failed attempt at finding the last
4070              class-or-namespace-name.  */
4071           parser->scope = old_scope;
4072           parser->qualifying_scope = saved_qualifying_scope;
4073           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4074             break;
4075           /* If the next token is an identifier, and the one after
4076              that is a `::', then any valid interpretation would have
4077              found a class-or-namespace-name.  */
4078           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4079                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4080                      == CPP_SCOPE)
4081                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4082                      != CPP_COMPL))
4083             {
4084               token = cp_lexer_consume_token (parser->lexer);
4085               if (!error_p)
4086                 {
4087                   if (!token->ambiguous_p)
4088                     {
4089                       tree decl;
4090                       tree ambiguous_decls;
4091
4092                       decl = cp_parser_lookup_name (parser, token->u.value,
4093                                                     none_type,
4094                                                     /*is_template=*/false,
4095                                                     /*is_namespace=*/false,
4096                                                     /*check_dependency=*/true,
4097                                                     &ambiguous_decls,
4098                                                     token->location);
4099                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4100                         error ("%H%qD used without template parameters",
4101                                &token->location, decl);
4102                       else if (ambiguous_decls)
4103                         {
4104                           error ("%Hreference to %qD is ambiguous",
4105                                  &token->location, token->u.value);
4106                           print_candidates (ambiguous_decls);
4107                           decl = error_mark_node;
4108                         }
4109                       else
4110                         cp_parser_name_lookup_error
4111                           (parser, token->u.value, decl,
4112                            "is not a class or namespace",
4113                            token->location);
4114                     }
4115                   parser->scope = error_mark_node;
4116                   error_p = true;
4117                   /* Treat this as a successful nested-name-specifier
4118                      due to:
4119
4120                      [basic.lookup.qual]
4121
4122                      If the name found is not a class-name (clause
4123                      _class_) or namespace-name (_namespace.def_), the
4124                      program is ill-formed.  */
4125                   success = true;
4126                 }
4127               cp_lexer_consume_token (parser->lexer);
4128             }
4129           break;
4130         }
4131       /* We've found one valid nested-name-specifier.  */
4132       success = true;
4133       /* Name lookup always gives us a DECL.  */
4134       if (TREE_CODE (new_scope) == TYPE_DECL)
4135         new_scope = TREE_TYPE (new_scope);
4136       /* Uses of "template" must be followed by actual templates.  */
4137       if (template_keyword_p
4138           && !(CLASS_TYPE_P (new_scope)
4139                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4140                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4141                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4142           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4143                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4144                    == TEMPLATE_ID_EXPR)))
4145         permerror (input_location, TYPE_P (new_scope)
4146                    ? "%qT is not a template"
4147                    : "%qD is not a template",
4148                    new_scope);
4149       /* If it is a class scope, try to complete it; we are about to
4150          be looking up names inside the class.  */
4151       if (TYPE_P (new_scope)
4152           /* Since checking types for dependency can be expensive,
4153              avoid doing it if the type is already complete.  */
4154           && !COMPLETE_TYPE_P (new_scope)
4155           /* Do not try to complete dependent types.  */
4156           && !dependent_type_p (new_scope))
4157         {
4158           new_scope = complete_type (new_scope);
4159           /* If it is a typedef to current class, use the current
4160              class instead, as the typedef won't have any names inside
4161              it yet.  */
4162           if (!COMPLETE_TYPE_P (new_scope)
4163               && currently_open_class (new_scope))
4164             new_scope = TYPE_MAIN_VARIANT (new_scope);
4165         }
4166       /* Make sure we look in the right scope the next time through
4167          the loop.  */
4168       parser->scope = new_scope;
4169     }
4170
4171   /* If parsing tentatively, replace the sequence of tokens that makes
4172      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4173      token.  That way, should we re-parse the token stream, we will
4174      not have to repeat the effort required to do the parse, nor will
4175      we issue duplicate error messages.  */
4176   if (success && start)
4177     {
4178       cp_token *token;
4179
4180       token = cp_lexer_token_at (parser->lexer, start);
4181       /* Reset the contents of the START token.  */
4182       token->type = CPP_NESTED_NAME_SPECIFIER;
4183       /* Retrieve any deferred checks.  Do not pop this access checks yet
4184          so the memory will not be reclaimed during token replacing below.  */
4185       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4186       token->u.tree_check_value->value = parser->scope;
4187       token->u.tree_check_value->checks = get_deferred_access_checks ();
4188       token->u.tree_check_value->qualifying_scope =
4189         parser->qualifying_scope;
4190       token->keyword = RID_MAX;
4191
4192       /* Purge all subsequent tokens.  */
4193       cp_lexer_purge_tokens_after (parser->lexer, start);
4194     }
4195
4196   if (start)
4197     pop_to_parent_deferring_access_checks ();
4198
4199   return success ? parser->scope : NULL_TREE;
4200 }
4201
4202 /* Parse a nested-name-specifier.  See
4203    cp_parser_nested_name_specifier_opt for details.  This function
4204    behaves identically, except that it will an issue an error if no
4205    nested-name-specifier is present.  */
4206
4207 static tree
4208 cp_parser_nested_name_specifier (cp_parser *parser,
4209                                  bool typename_keyword_p,
4210                                  bool check_dependency_p,
4211                                  bool type_p,
4212                                  bool is_declaration)
4213 {
4214   tree scope;
4215
4216   /* Look for the nested-name-specifier.  */
4217   scope = cp_parser_nested_name_specifier_opt (parser,
4218                                                typename_keyword_p,
4219                                                check_dependency_p,
4220                                                type_p,
4221                                                is_declaration);
4222   /* If it was not present, issue an error message.  */
4223   if (!scope)
4224     {
4225       cp_parser_error (parser, "expected nested-name-specifier");
4226       parser->scope = NULL_TREE;
4227     }
4228
4229   return scope;
4230 }
4231
4232 /* Parse a class-or-namespace-name.
4233
4234    class-or-namespace-name:
4235      class-name
4236      namespace-name
4237
4238    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4239    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4240    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4241    TYPE_P is TRUE iff the next name should be taken as a class-name,
4242    even the same name is declared to be another entity in the same
4243    scope.
4244
4245    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4246    specified by the class-or-namespace-name.  If neither is found the
4247    ERROR_MARK_NODE is returned.  */
4248
4249 static tree
4250 cp_parser_class_or_namespace_name (cp_parser *parser,
4251                                    bool typename_keyword_p,
4252                                    bool template_keyword_p,
4253                                    bool check_dependency_p,
4254                                    bool type_p,
4255                                    bool is_declaration)
4256 {
4257   tree saved_scope;
4258   tree saved_qualifying_scope;
4259   tree saved_object_scope;
4260   tree scope;
4261   bool only_class_p;
4262
4263   /* Before we try to parse the class-name, we must save away the
4264      current PARSER->SCOPE since cp_parser_class_name will destroy
4265      it.  */
4266   saved_scope = parser->scope;
4267   saved_qualifying_scope = parser->qualifying_scope;
4268   saved_object_scope = parser->object_scope;
4269   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4270      there is no need to look for a namespace-name.  */
4271   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4272   if (!only_class_p)
4273     cp_parser_parse_tentatively (parser);
4274   scope = cp_parser_class_name (parser,
4275                                 typename_keyword_p,
4276                                 template_keyword_p,
4277                                 type_p ? class_type : none_type,
4278                                 check_dependency_p,
4279                                 /*class_head_p=*/false,
4280                                 is_declaration);
4281   /* If that didn't work, try for a namespace-name.  */
4282   if (!only_class_p && !cp_parser_parse_definitely (parser))
4283     {
4284       /* Restore the saved scope.  */
4285       parser->scope = saved_scope;
4286       parser->qualifying_scope = saved_qualifying_scope;
4287       parser->object_scope = saved_object_scope;
4288       /* If we are not looking at an identifier followed by the scope
4289          resolution operator, then this is not part of a
4290          nested-name-specifier.  (Note that this function is only used
4291          to parse the components of a nested-name-specifier.)  */
4292       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4293           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4294         return error_mark_node;
4295       scope = cp_parser_namespace_name (parser);
4296     }
4297
4298   return scope;
4299 }
4300
4301 /* Parse a postfix-expression.
4302
4303    postfix-expression:
4304      primary-expression
4305      postfix-expression [ expression ]
4306      postfix-expression ( expression-list [opt] )
4307      simple-type-specifier ( expression-list [opt] )
4308      typename :: [opt] nested-name-specifier identifier
4309        ( expression-list [opt] )
4310      typename :: [opt] nested-name-specifier template [opt] template-id
4311        ( expression-list [opt] )
4312      postfix-expression . template [opt] id-expression
4313      postfix-expression -> template [opt] id-expression
4314      postfix-expression . pseudo-destructor-name
4315      postfix-expression -> pseudo-destructor-name
4316      postfix-expression ++
4317      postfix-expression --
4318      dynamic_cast < type-id > ( expression )
4319      static_cast < type-id > ( expression )
4320      reinterpret_cast < type-id > ( expression )
4321      const_cast < type-id > ( expression )
4322      typeid ( expression )
4323      typeid ( type-id )
4324
4325    GNU Extension:
4326
4327    postfix-expression:
4328      ( type-id ) { initializer-list , [opt] }
4329
4330    This extension is a GNU version of the C99 compound-literal
4331    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4332    but they are essentially the same concept.)
4333
4334    If ADDRESS_P is true, the postfix expression is the operand of the
4335    `&' operator.  CAST_P is true if this expression is the target of a
4336    cast.
4337
4338    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4339    class member access expressions [expr.ref].
4340
4341    Returns a representation of the expression.  */
4342
4343 static tree
4344 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4345                               bool member_access_only_p)
4346 {
4347   cp_token *token;
4348   enum rid keyword;
4349   cp_id_kind idk = CP_ID_KIND_NONE;
4350   tree postfix_expression = NULL_TREE;
4351   bool is_member_access = false;
4352
4353   /* Peek at the next token.  */
4354   token = cp_lexer_peek_token (parser->lexer);
4355   /* Some of the productions are determined by keywords.  */
4356   keyword = token->keyword;
4357   switch (keyword)
4358     {
4359     case RID_DYNCAST:
4360     case RID_STATCAST:
4361     case RID_REINTCAST:
4362     case RID_CONSTCAST:
4363       {
4364         tree type;
4365         tree expression;
4366         const char *saved_message;
4367
4368         /* All of these can be handled in the same way from the point
4369            of view of parsing.  Begin by consuming the token
4370            identifying the cast.  */
4371         cp_lexer_consume_token (parser->lexer);
4372
4373         /* New types cannot be defined in the cast.  */
4374         saved_message = parser->type_definition_forbidden_message;
4375         parser->type_definition_forbidden_message
4376           = "types may not be defined in casts";
4377
4378         /* Look for the opening `<'.  */
4379         cp_parser_require (parser, CPP_LESS, "%<<%>");
4380         /* Parse the type to which we are casting.  */
4381         type = cp_parser_type_id (parser);
4382         /* Look for the closing `>'.  */
4383         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4384         /* Restore the old message.  */
4385         parser->type_definition_forbidden_message = saved_message;
4386
4387         /* And the expression which is being cast.  */
4388         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4389         expression = cp_parser_expression (parser, /*cast_p=*/true);
4390         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4391
4392         /* Only type conversions to integral or enumeration types
4393            can be used in constant-expressions.  */
4394         if (!cast_valid_in_integral_constant_expression_p (type)
4395             && (cp_parser_non_integral_constant_expression
4396                 (parser,
4397                  "a cast to a type other than an integral or "
4398                  "enumeration type")))
4399           return error_mark_node;
4400
4401         switch (keyword)
4402           {
4403           case RID_DYNCAST:
4404             postfix_expression
4405               = build_dynamic_cast (type, expression, tf_warning_or_error);
4406             break;
4407           case RID_STATCAST:
4408             postfix_expression
4409               = build_static_cast (type, expression, tf_warning_or_error);
4410             break;
4411           case RID_REINTCAST:
4412             postfix_expression
4413               = build_reinterpret_cast (type, expression, 
4414                                         tf_warning_or_error);
4415             break;
4416           case RID_CONSTCAST:
4417             postfix_expression
4418               = build_const_cast (type, expression, tf_warning_or_error);
4419             break;
4420           default:
4421             gcc_unreachable ();
4422           }
4423       }
4424       break;
4425
4426     case RID_TYPEID:
4427       {
4428         tree type;
4429         const char *saved_message;
4430         bool saved_in_type_id_in_expr_p;
4431
4432         /* Consume the `typeid' token.  */
4433         cp_lexer_consume_token (parser->lexer);
4434         /* Look for the `(' token.  */
4435         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4436         /* Types cannot be defined in a `typeid' expression.  */
4437         saved_message = parser->type_definition_forbidden_message;
4438         parser->type_definition_forbidden_message
4439           = "types may not be defined in a %<typeid%> expression";
4440         /* We can't be sure yet whether we're looking at a type-id or an
4441            expression.  */
4442         cp_parser_parse_tentatively (parser);
4443         /* Try a type-id first.  */
4444         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4445         parser->in_type_id_in_expr_p = true;
4446         type = cp_parser_type_id (parser);
4447         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4448         /* Look for the `)' token.  Otherwise, we can't be sure that
4449            we're not looking at an expression: consider `typeid (int
4450            (3))', for example.  */
4451         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4452         /* If all went well, simply lookup the type-id.  */
4453         if (cp_parser_parse_definitely (parser))
4454           postfix_expression = get_typeid (type);
4455         /* Otherwise, fall back to the expression variant.  */
4456         else
4457           {
4458             tree expression;
4459
4460             /* Look for an expression.  */
4461             expression = cp_parser_expression (parser, /*cast_p=*/false);
4462             /* Compute its typeid.  */
4463             postfix_expression = build_typeid (expression);
4464             /* Look for the `)' token.  */
4465             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4466           }
4467         /* Restore the saved message.  */
4468         parser->type_definition_forbidden_message = saved_message;
4469         /* `typeid' may not appear in an integral constant expression.  */
4470         if (cp_parser_non_integral_constant_expression(parser,
4471                                                        "%<typeid%> operator"))
4472           return error_mark_node;
4473       }
4474       break;
4475
4476     case RID_TYPENAME:
4477       {
4478         tree type;
4479         /* The syntax permitted here is the same permitted for an
4480            elaborated-type-specifier.  */
4481         type = cp_parser_elaborated_type_specifier (parser,
4482                                                     /*is_friend=*/false,
4483                                                     /*is_declaration=*/false);
4484         postfix_expression = cp_parser_functional_cast (parser, type);
4485       }
4486       break;
4487
4488     default:
4489       {
4490         tree type;
4491
4492         /* If the next thing is a simple-type-specifier, we may be
4493            looking at a functional cast.  We could also be looking at
4494            an id-expression.  So, we try the functional cast, and if
4495            that doesn't work we fall back to the primary-expression.  */
4496         cp_parser_parse_tentatively (parser);
4497         /* Look for the simple-type-specifier.  */
4498         type = cp_parser_simple_type_specifier (parser,
4499                                                 /*decl_specs=*/NULL,
4500                                                 CP_PARSER_FLAGS_NONE);
4501         /* Parse the cast itself.  */
4502         if (!cp_parser_error_occurred (parser))
4503           postfix_expression
4504             = cp_parser_functional_cast (parser, type);
4505         /* If that worked, we're done.  */
4506         if (cp_parser_parse_definitely (parser))
4507           break;
4508
4509         /* If the functional-cast didn't work out, try a
4510            compound-literal.  */
4511         if (cp_parser_allow_gnu_extensions_p (parser)
4512             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4513           {
4514             VEC(constructor_elt,gc) *initializer_list = NULL;
4515             bool saved_in_type_id_in_expr_p;
4516
4517             cp_parser_parse_tentatively (parser);
4518             /* Consume the `('.  */
4519             cp_lexer_consume_token (parser->lexer);
4520             /* Parse the type.  */
4521             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4522             parser->in_type_id_in_expr_p = true;
4523             type = cp_parser_type_id (parser);
4524             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4525             /* Look for the `)'.  */
4526             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4527             /* Look for the `{'.  */
4528             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4529             /* If things aren't going well, there's no need to
4530                keep going.  */
4531             if (!cp_parser_error_occurred (parser))
4532               {
4533                 bool non_constant_p;
4534                 /* Parse the initializer-list.  */
4535                 initializer_list
4536                   = cp_parser_initializer_list (parser, &non_constant_p);
4537                 /* Allow a trailing `,'.  */
4538                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4539                   cp_lexer_consume_token (parser->lexer);
4540                 /* Look for the final `}'.  */
4541                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4542               }
4543             /* If that worked, we're definitely looking at a
4544                compound-literal expression.  */
4545             if (cp_parser_parse_definitely (parser))
4546               {
4547                 /* Warn the user that a compound literal is not
4548                    allowed in standard C++.  */
4549                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4550                 /* For simplicity, we disallow compound literals in
4551                    constant-expressions.  We could
4552                    allow compound literals of integer type, whose
4553                    initializer was a constant, in constant
4554                    expressions.  Permitting that usage, as a further
4555                    extension, would not change the meaning of any
4556                    currently accepted programs.  (Of course, as
4557                    compound literals are not part of ISO C++, the
4558                    standard has nothing to say.)  */
4559                 if (cp_parser_non_integral_constant_expression 
4560                     (parser, "non-constant compound literals"))
4561                   {
4562                     postfix_expression = error_mark_node;
4563                     break;
4564                   }
4565                 /* Form the representation of the compound-literal.  */
4566                 postfix_expression
4567                   = (finish_compound_literal
4568                      (type, build_constructor (init_list_type_node,
4569                                                initializer_list)));
4570                 break;
4571               }
4572           }
4573
4574         /* It must be a primary-expression.  */
4575         postfix_expression
4576           = cp_parser_primary_expression (parser, address_p, cast_p,
4577                                           /*template_arg_p=*/false,
4578                                           &idk);
4579       }
4580       break;
4581     }
4582
4583   /* Keep looping until the postfix-expression is complete.  */
4584   while (true)
4585     {
4586       if (idk == CP_ID_KIND_UNQUALIFIED
4587           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4588           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4589         /* It is not a Koenig lookup function call.  */
4590         postfix_expression
4591           = unqualified_name_lookup_error (postfix_expression);
4592
4593       /* Peek at the next token.  */
4594       token = cp_lexer_peek_token (parser->lexer);
4595
4596       switch (token->type)
4597         {
4598         case CPP_OPEN_SQUARE:
4599           postfix_expression
4600             = cp_parser_postfix_open_square_expression (parser,
4601                                                         postfix_expression,
4602                                                         false);
4603           idk = CP_ID_KIND_NONE;
4604           is_member_access = false;
4605           break;
4606
4607         case CPP_OPEN_PAREN:
4608           /* postfix-expression ( expression-list [opt] ) */
4609           {
4610             bool koenig_p;
4611             bool is_builtin_constant_p;
4612             bool saved_integral_constant_expression_p = false;
4613             bool saved_non_integral_constant_expression_p = false;
4614             tree args;
4615
4616             is_member_access = false;
4617
4618             is_builtin_constant_p
4619               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4620             if (is_builtin_constant_p)
4621               {
4622                 /* The whole point of __builtin_constant_p is to allow
4623                    non-constant expressions to appear as arguments.  */
4624                 saved_integral_constant_expression_p
4625                   = parser->integral_constant_expression_p;
4626                 saved_non_integral_constant_expression_p
4627                   = parser->non_integral_constant_expression_p;
4628                 parser->integral_constant_expression_p = false;
4629               }
4630             args = (cp_parser_parenthesized_expression_list
4631                     (parser, /*is_attribute_list=*/false,
4632                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4633                      /*non_constant_p=*/NULL));
4634             if (is_builtin_constant_p)
4635               {
4636                 parser->integral_constant_expression_p
4637                   = saved_integral_constant_expression_p;
4638                 parser->non_integral_constant_expression_p
4639                   = saved_non_integral_constant_expression_p;
4640               }
4641
4642             if (args == error_mark_node)
4643               {
4644                 postfix_expression = error_mark_node;
4645                 break;
4646               }
4647
4648             /* Function calls are not permitted in
4649                constant-expressions.  */
4650             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4651                 && cp_parser_non_integral_constant_expression (parser,
4652                                                                "a function call"))
4653               {
4654                 postfix_expression = error_mark_node;
4655                 break;
4656               }
4657
4658             koenig_p = false;
4659             if (idk == CP_ID_KIND_UNQUALIFIED)
4660               {
4661                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4662                   {
4663                     if (args)
4664                       {
4665                         koenig_p = true;
4666                         postfix_expression
4667                           = perform_koenig_lookup (postfix_expression, args);
4668                       }
4669                     else
4670                       postfix_expression
4671                         = unqualified_fn_lookup_error (postfix_expression);
4672                   }
4673                 /* We do not perform argument-dependent lookup if
4674                    normal lookup finds a non-function, in accordance
4675                    with the expected resolution of DR 218.  */
4676                 else if (args && is_overloaded_fn (postfix_expression))
4677                   {
4678                     tree fn = get_first_fn (postfix_expression);
4679
4680                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4681                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4682
4683                     /* Only do argument dependent lookup if regular
4684                        lookup does not find a set of member functions.
4685                        [basic.lookup.koenig]/2a  */
4686                     if (!DECL_FUNCTION_MEMBER_P (fn))
4687                       {
4688                         koenig_p = true;
4689                         postfix_expression
4690                           = perform_koenig_lookup (postfix_expression, args);
4691                       }
4692                   }
4693               }
4694
4695             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4696               {
4697                 tree instance = TREE_OPERAND (postfix_expression, 0);
4698                 tree fn = TREE_OPERAND (postfix_expression, 1);
4699
4700                 if (processing_template_decl
4701                     && (type_dependent_expression_p (instance)
4702                         || (!BASELINK_P (fn)
4703                             && TREE_CODE (fn) != FIELD_DECL)
4704                         || type_dependent_expression_p (fn)
4705                         || any_type_dependent_arguments_p (args)))
4706                   {
4707                     postfix_expression
4708                       = build_nt_call_list (postfix_expression, args);
4709                     break;
4710                   }
4711
4712                 if (BASELINK_P (fn))
4713                   postfix_expression
4714                     = (build_new_method_call
4715                        (instance, fn, args, NULL_TREE,
4716                         (idk == CP_ID_KIND_QUALIFIED
4717                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4718                         /*fn_p=*/NULL,
4719                         tf_warning_or_error));
4720                 else
4721                   postfix_expression
4722                     = finish_call_expr (postfix_expression, args,
4723                                         /*disallow_virtual=*/false,
4724                                         /*koenig_p=*/false,
4725                                         tf_warning_or_error);
4726               }
4727             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4728                      || TREE_CODE (postfix_expression) == MEMBER_REF
4729                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4730               postfix_expression = (build_offset_ref_call_from_tree
4731                                     (postfix_expression, args));
4732             else if (idk == CP_ID_KIND_QUALIFIED)
4733               /* A call to a static class member, or a namespace-scope
4734                  function.  */
4735               postfix_expression
4736                 = finish_call_expr (postfix_expression, args,
4737                                     /*disallow_virtual=*/true,
4738                                     koenig_p,
4739                                     tf_warning_or_error);
4740             else
4741               /* All other function calls.  */
4742               postfix_expression
4743                 = finish_call_expr (postfix_expression, args,
4744                                     /*disallow_virtual=*/false,
4745                                     koenig_p,
4746                                     tf_warning_or_error);
4747
4748             if (warn_disallowed_functions)
4749               warn_if_disallowed_function_p (postfix_expression);
4750
4751             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4752             idk = CP_ID_KIND_NONE;
4753           }
4754           break;
4755
4756         case CPP_DOT:
4757         case CPP_DEREF:
4758           /* postfix-expression . template [opt] id-expression
4759              postfix-expression . pseudo-destructor-name
4760              postfix-expression -> template [opt] id-expression
4761              postfix-expression -> pseudo-destructor-name */
4762
4763           /* Consume the `.' or `->' operator.  */
4764           cp_lexer_consume_token (parser->lexer);
4765
4766           postfix_expression
4767             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4768                                                       postfix_expression,
4769                                                       false, &idk,
4770                                                       token->location);
4771
4772           is_member_access = true;
4773           break;
4774
4775         case CPP_PLUS_PLUS:
4776           /* postfix-expression ++  */
4777           /* Consume the `++' token.  */
4778           cp_lexer_consume_token (parser->lexer);
4779           /* Generate a representation for the complete expression.  */
4780           postfix_expression
4781             = finish_increment_expr (postfix_expression,
4782                                      POSTINCREMENT_EXPR);
4783           /* Increments may not appear in constant-expressions.  */
4784           if (cp_parser_non_integral_constant_expression (parser,
4785                                                           "an increment"))
4786             postfix_expression = error_mark_node;
4787           idk = CP_ID_KIND_NONE;
4788           is_member_access = false;
4789           break;
4790
4791         case CPP_MINUS_MINUS:
4792           /* postfix-expression -- */
4793           /* Consume the `--' token.  */
4794           cp_lexer_consume_token (parser->lexer);
4795           /* Generate a representation for the complete expression.  */
4796           postfix_expression
4797             = finish_increment_expr (postfix_expression,
4798                                      POSTDECREMENT_EXPR);
4799           /* Decrements may not appear in constant-expressions.  */
4800           if (cp_parser_non_integral_constant_expression (parser,
4801                                                           "a decrement"))
4802             postfix_expression = error_mark_node;
4803           idk = CP_ID_KIND_NONE;
4804           is_member_access = false;
4805           break;
4806
4807         default:
4808           if (member_access_only_p)
4809             return is_member_access? postfix_expression : error_mark_node;
4810           else
4811             return postfix_expression;
4812         }
4813     }
4814
4815   /* We should never get here.  */
4816   gcc_unreachable ();
4817   return error_mark_node;
4818 }
4819
4820 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4821    by cp_parser_builtin_offsetof.  We're looking for
4822
4823      postfix-expression [ expression ]
4824
4825    FOR_OFFSETOF is set if we're being called in that context, which
4826    changes how we deal with integer constant expressions.  */
4827
4828 static tree
4829 cp_parser_postfix_open_square_expression (cp_parser *parser,
4830                                           tree postfix_expression,
4831                                           bool for_offsetof)
4832 {
4833   tree index;
4834
4835   /* Consume the `[' token.  */
4836   cp_lexer_consume_token (parser->lexer);
4837
4838   /* Parse the index expression.  */
4839   /* ??? For offsetof, there is a question of what to allow here.  If
4840      offsetof is not being used in an integral constant expression context,
4841      then we *could* get the right answer by computing the value at runtime.
4842      If we are in an integral constant expression context, then we might
4843      could accept any constant expression; hard to say without analysis.
4844      Rather than open the barn door too wide right away, allow only integer
4845      constant expressions here.  */
4846   if (for_offsetof)
4847     index = cp_parser_constant_expression (parser, false, NULL);
4848   else
4849     index = cp_parser_expression (parser, /*cast_p=*/false);
4850
4851   /* Look for the closing `]'.  */
4852   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4853
4854   /* Build the ARRAY_REF.  */
4855   postfix_expression = grok_array_decl (postfix_expression, index);
4856
4857   /* When not doing offsetof, array references are not permitted in
4858      constant-expressions.  */
4859   if (!for_offsetof
4860       && (cp_parser_non_integral_constant_expression
4861           (parser, "an array reference")))
4862     postfix_expression = error_mark_node;
4863
4864   return postfix_expression;
4865 }
4866
4867 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4868    by cp_parser_builtin_offsetof.  We're looking for
4869
4870      postfix-expression . template [opt] id-expression
4871      postfix-expression . pseudo-destructor-name
4872      postfix-expression -> template [opt] id-expression
4873      postfix-expression -> pseudo-destructor-name
4874
4875    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4876    limits what of the above we'll actually accept, but nevermind.
4877    TOKEN_TYPE is the "." or "->" token, which will already have been
4878    removed from the stream.  */
4879
4880 static tree
4881 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4882                                         enum cpp_ttype token_type,
4883                                         tree postfix_expression,
4884                                         bool for_offsetof, cp_id_kind *idk,
4885                                         location_t location)
4886 {
4887   tree name;
4888   bool dependent_p;
4889   bool pseudo_destructor_p;
4890   tree scope = NULL_TREE;
4891
4892   /* If this is a `->' operator, dereference the pointer.  */
4893   if (token_type == CPP_DEREF)
4894     postfix_expression = build_x_arrow (postfix_expression);
4895   /* Check to see whether or not the expression is type-dependent.  */
4896   dependent_p = type_dependent_expression_p (postfix_expression);
4897   /* The identifier following the `->' or `.' is not qualified.  */
4898   parser->scope = NULL_TREE;
4899   parser->qualifying_scope = NULL_TREE;
4900   parser->object_scope = NULL_TREE;
4901   *idk = CP_ID_KIND_NONE;
4902   /* Enter the scope corresponding to the type of the object
4903      given by the POSTFIX_EXPRESSION.  */
4904   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4905     {
4906       scope = TREE_TYPE (postfix_expression);
4907       /* According to the standard, no expression should ever have
4908          reference type.  Unfortunately, we do not currently match
4909          the standard in this respect in that our internal representation
4910          of an expression may have reference type even when the standard
4911          says it does not.  Therefore, we have to manually obtain the
4912          underlying type here.  */
4913       scope = non_reference (scope);
4914       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4915       if (scope == unknown_type_node)
4916         {
4917           error ("%H%qE does not have class type", &location, postfix_expression);
4918           scope = NULL_TREE;
4919         }
4920       else
4921         scope = complete_type_or_else (scope, NULL_TREE);
4922       /* Let the name lookup machinery know that we are processing a
4923          class member access expression.  */
4924       parser->context->object_type = scope;
4925       /* If something went wrong, we want to be able to discern that case,
4926          as opposed to the case where there was no SCOPE due to the type
4927          of expression being dependent.  */
4928       if (!scope)
4929         scope = error_mark_node;
4930       /* If the SCOPE was erroneous, make the various semantic analysis
4931          functions exit quickly -- and without issuing additional error
4932          messages.  */
4933       if (scope == error_mark_node)
4934         postfix_expression = error_mark_node;
4935     }
4936
4937   /* Assume this expression is not a pseudo-destructor access.  */
4938   pseudo_destructor_p = false;
4939
4940   /* If the SCOPE is a scalar type, then, if this is a valid program,
4941      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4942      is type dependent, it can be pseudo-destructor-name or something else.
4943      Try to parse it as pseudo-destructor-name first.  */
4944   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4945     {
4946       tree s;
4947       tree type;
4948
4949       cp_parser_parse_tentatively (parser);
4950       /* Parse the pseudo-destructor-name.  */
4951       s = NULL_TREE;
4952       cp_parser_pseudo_destructor_name (parser, &s, &type);
4953       if (dependent_p
4954           && (cp_parser_error_occurred (parser)
4955               || TREE_CODE (type) != TYPE_DECL
4956               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4957         cp_parser_abort_tentative_parse (parser);
4958       else if (cp_parser_parse_definitely (parser))
4959         {
4960           pseudo_destructor_p = true;
4961           postfix_expression
4962             = finish_pseudo_destructor_expr (postfix_expression,
4963                                              s, TREE_TYPE (type));
4964         }
4965     }
4966
4967   if (!pseudo_destructor_p)
4968     {
4969       /* If the SCOPE is not a scalar type, we are looking at an
4970          ordinary class member access expression, rather than a
4971          pseudo-destructor-name.  */
4972       bool template_p;
4973       cp_token *token = cp_lexer_peek_token (parser->lexer);
4974       /* Parse the id-expression.  */
4975       name = (cp_parser_id_expression
4976               (parser,
4977                cp_parser_optional_template_keyword (parser),
4978                /*check_dependency_p=*/true,
4979                &template_p,
4980                /*declarator_p=*/false,
4981                /*optional_p=*/false));
4982       /* In general, build a SCOPE_REF if the member name is qualified.
4983          However, if the name was not dependent and has already been
4984          resolved; there is no need to build the SCOPE_REF.  For example;
4985
4986              struct X { void f(); };
4987              template <typename T> void f(T* t) { t->X::f(); }
4988
4989          Even though "t" is dependent, "X::f" is not and has been resolved
4990          to a BASELINK; there is no need to include scope information.  */
4991
4992       /* But we do need to remember that there was an explicit scope for
4993          virtual function calls.  */
4994       if (parser->scope)
4995         *idk = CP_ID_KIND_QUALIFIED;
4996
4997       /* If the name is a template-id that names a type, we will get a
4998          TYPE_DECL here.  That is invalid code.  */
4999       if (TREE_CODE (name) == TYPE_DECL)
5000         {
5001           error ("%Hinvalid use of %qD", &token->location, name);
5002           postfix_expression = error_mark_node;
5003         }
5004       else
5005         {
5006           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5007             {
5008               name = build_qualified_name (/*type=*/NULL_TREE,
5009                                            parser->scope,
5010                                            name,
5011                                            template_p);
5012               parser->scope = NULL_TREE;
5013               parser->qualifying_scope = NULL_TREE;
5014               parser->object_scope = NULL_TREE;
5015             }
5016           if (scope && name && BASELINK_P (name))
5017             adjust_result_of_qualified_name_lookup
5018               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5019           postfix_expression
5020             = finish_class_member_access_expr (postfix_expression, name,
5021                                                template_p, 
5022                                                tf_warning_or_error);
5023         }
5024     }
5025
5026   /* We no longer need to look up names in the scope of the object on
5027      the left-hand side of the `.' or `->' operator.  */
5028   parser->context->object_type = NULL_TREE;
5029
5030   /* Outside of offsetof, these operators may not appear in
5031      constant-expressions.  */
5032   if (!for_offsetof
5033       && (cp_parser_non_integral_constant_expression
5034           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5035     postfix_expression = error_mark_node;
5036
5037   return postfix_expression;
5038 }
5039
5040 /* Parse a parenthesized expression-list.
5041
5042    expression-list:
5043      assignment-expression
5044      expression-list, assignment-expression
5045
5046    attribute-list:
5047      expression-list
5048      identifier
5049      identifier, expression-list
5050
5051    CAST_P is true if this expression is the target of a cast.
5052
5053    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5054    argument pack.
5055
5056    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5057    representation of an assignment-expression.  Note that a TREE_LIST
5058    is returned even if there is only a single expression in the list.
5059    error_mark_node is returned if the ( and or ) are
5060    missing. NULL_TREE is returned on no expressions. The parentheses
5061    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5062    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5063    indicates whether or not all of the expressions in the list were
5064    constant.  */
5065
5066 static tree
5067 cp_parser_parenthesized_expression_list (cp_parser* parser,
5068                                          bool is_attribute_list,
5069                                          bool cast_p,
5070                                          bool allow_expansion_p,
5071                                          bool *non_constant_p)
5072 {
5073   tree expression_list = NULL_TREE;
5074   bool fold_expr_p = is_attribute_list;
5075   tree identifier = NULL_TREE;
5076   bool saved_greater_than_is_operator_p;
5077
5078   /* Assume all the expressions will be constant.  */
5079   if (non_constant_p)
5080     *non_constant_p = false;
5081
5082   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5083     return error_mark_node;
5084
5085   /* Within a parenthesized expression, a `>' token is always
5086      the greater-than operator.  */
5087   saved_greater_than_is_operator_p
5088     = parser->greater_than_is_operator_p;
5089   parser->greater_than_is_operator_p = true;
5090
5091   /* Consume expressions until there are no more.  */
5092   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5093     while (true)
5094       {
5095         tree expr;
5096
5097         /* At the beginning of attribute lists, check to see if the
5098            next token is an identifier.  */
5099         if (is_attribute_list
5100             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5101           {
5102             cp_token *token;
5103
5104             /* Consume the identifier.  */
5105             token = cp_lexer_consume_token (parser->lexer);
5106             /* Save the identifier.  */
5107             identifier = token->u.value;
5108           }
5109         else
5110           {
5111             bool expr_non_constant_p;
5112
5113             /* Parse the next assignment-expression.  */
5114             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5115               {
5116                 /* A braced-init-list.  */
5117                 maybe_warn_cpp0x ("extended initializer lists");
5118                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5119                 if (non_constant_p && expr_non_constant_p)
5120                   *non_constant_p = true;
5121               }
5122             else if (non_constant_p)
5123               {
5124                 expr = (cp_parser_constant_expression
5125                         (parser, /*allow_non_constant_p=*/true,
5126                          &expr_non_constant_p));
5127                 if (expr_non_constant_p)
5128                   *non_constant_p = true;
5129               }
5130             else
5131               expr = cp_parser_assignment_expression (parser, cast_p);
5132
5133             if (fold_expr_p)
5134               expr = fold_non_dependent_expr (expr);
5135
5136             /* If we have an ellipsis, then this is an expression
5137                expansion.  */
5138             if (allow_expansion_p
5139                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5140               {
5141                 /* Consume the `...'.  */
5142                 cp_lexer_consume_token (parser->lexer);
5143
5144                 /* Build the argument pack.  */
5145                 expr = make_pack_expansion (expr);
5146               }
5147
5148              /* Add it to the list.  We add error_mark_node
5149                 expressions to the list, so that we can still tell if
5150                 the correct form for a parenthesized expression-list
5151                 is found. That gives better errors.  */
5152             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5153
5154             if (expr == error_mark_node)
5155               goto skip_comma;
5156           }
5157
5158         /* After the first item, attribute lists look the same as
5159            expression lists.  */
5160         is_attribute_list = false;
5161
5162       get_comma:;
5163         /* If the next token isn't a `,', then we are done.  */
5164         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5165           break;
5166
5167         /* Otherwise, consume the `,' and keep going.  */
5168         cp_lexer_consume_token (parser->lexer);
5169       }
5170
5171   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5172     {
5173       int ending;
5174
5175     skip_comma:;
5176       /* We try and resync to an unnested comma, as that will give the
5177          user better diagnostics.  */
5178       ending = cp_parser_skip_to_closing_parenthesis (parser,
5179                                                       /*recovering=*/true,
5180                                                       /*or_comma=*/true,
5181                                                       /*consume_paren=*/true);
5182       if (ending < 0)
5183         goto get_comma;
5184       if (!ending)
5185         {
5186           parser->greater_than_is_operator_p
5187             = saved_greater_than_is_operator_p;
5188           return error_mark_node;
5189         }
5190     }
5191
5192   parser->greater_than_is_operator_p
5193     = saved_greater_than_is_operator_p;
5194
5195   /* We built up the list in reverse order so we must reverse it now.  */
5196   expression_list = nreverse (expression_list);
5197   if (identifier)
5198     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5199
5200   return expression_list;
5201 }
5202
5203 /* Parse a pseudo-destructor-name.
5204
5205    pseudo-destructor-name:
5206      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5207      :: [opt] nested-name-specifier template template-id :: ~ type-name
5208      :: [opt] nested-name-specifier [opt] ~ type-name
5209
5210    If either of the first two productions is used, sets *SCOPE to the
5211    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5212    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5213    or ERROR_MARK_NODE if the parse fails.  */
5214
5215 static void
5216 cp_parser_pseudo_destructor_name (cp_parser* parser,
5217                                   tree* scope,
5218                                   tree* type)
5219 {
5220   bool nested_name_specifier_p;
5221
5222   /* Assume that things will not work out.  */
5223   *type = error_mark_node;
5224
5225   /* Look for the optional `::' operator.  */
5226   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5227   /* Look for the optional nested-name-specifier.  */
5228   nested_name_specifier_p
5229     = (cp_parser_nested_name_specifier_opt (parser,
5230                                             /*typename_keyword_p=*/false,
5231                                             /*check_dependency_p=*/true,
5232                                             /*type_p=*/false,
5233                                             /*is_declaration=*/true)
5234        != NULL_TREE);
5235   /* Now, if we saw a nested-name-specifier, we might be doing the
5236      second production.  */
5237   if (nested_name_specifier_p
5238       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5239     {
5240       /* Consume the `template' keyword.  */
5241       cp_lexer_consume_token (parser->lexer);
5242       /* Parse the template-id.  */
5243       cp_parser_template_id (parser,
5244                              /*template_keyword_p=*/true,
5245                              /*check_dependency_p=*/false,
5246                              /*is_declaration=*/true);
5247       /* Look for the `::' token.  */
5248       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5249     }
5250   /* If the next token is not a `~', then there might be some
5251      additional qualification.  */
5252   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5253     {
5254       /* At this point, we're looking for "type-name :: ~".  The type-name
5255          must not be a class-name, since this is a pseudo-destructor.  So,
5256          it must be either an enum-name, or a typedef-name -- both of which
5257          are just identifiers.  So, we peek ahead to check that the "::"
5258          and "~" tokens are present; if they are not, then we can avoid
5259          calling type_name.  */
5260       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5261           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5262           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5263         {
5264           cp_parser_error (parser, "non-scalar type");
5265           return;
5266         }
5267
5268       /* Look for the type-name.  */
5269       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5270       if (*scope == error_mark_node)
5271         return;
5272
5273       /* Look for the `::' token.  */
5274       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5275     }
5276   else
5277     *scope = NULL_TREE;
5278
5279   /* Look for the `~'.  */
5280   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5281   /* Look for the type-name again.  We are not responsible for
5282      checking that it matches the first type-name.  */
5283   *type = cp_parser_nonclass_name (parser);
5284 }
5285
5286 /* Parse a unary-expression.
5287
5288    unary-expression:
5289      postfix-expression
5290      ++ cast-expression
5291      -- cast-expression
5292      unary-operator cast-expression
5293      sizeof unary-expression
5294      sizeof ( type-id )
5295      new-expression
5296      delete-expression
5297
5298    GNU Extensions:
5299
5300    unary-expression:
5301      __extension__ cast-expression
5302      __alignof__ unary-expression
5303      __alignof__ ( type-id )
5304      __real__ cast-expression
5305      __imag__ cast-expression
5306      && identifier
5307
5308    ADDRESS_P is true iff the unary-expression is appearing as the
5309    operand of the `&' operator.   CAST_P is true if this expression is
5310    the target of a cast.
5311
5312    Returns a representation of the expression.  */
5313
5314 static tree
5315 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5316 {
5317   cp_token *token;
5318   enum tree_code unary_operator;
5319
5320   /* Peek at the next token.  */
5321   token = cp_lexer_peek_token (parser->lexer);
5322   /* Some keywords give away the kind of expression.  */
5323   if (token->type == CPP_KEYWORD)
5324     {
5325       enum rid keyword = token->keyword;
5326
5327       switch (keyword)
5328         {
5329         case RID_ALIGNOF:
5330         case RID_SIZEOF:
5331           {
5332             tree operand;
5333             enum tree_code op;
5334
5335             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5336             /* Consume the token.  */
5337             cp_lexer_consume_token (parser->lexer);
5338             /* Parse the operand.  */
5339             operand = cp_parser_sizeof_operand (parser, keyword);
5340
5341             if (TYPE_P (operand))
5342               return cxx_sizeof_or_alignof_type (operand, op, true);
5343             else
5344               return cxx_sizeof_or_alignof_expr (operand, op, true);
5345           }
5346
5347         case RID_NEW:
5348           return cp_parser_new_expression (parser);
5349
5350         case RID_DELETE:
5351           return cp_parser_delete_expression (parser);
5352
5353         case RID_EXTENSION:
5354           {
5355             /* The saved value of the PEDANTIC flag.  */
5356             int saved_pedantic;
5357             tree expr;
5358
5359             /* Save away the PEDANTIC flag.  */
5360             cp_parser_extension_opt (parser, &saved_pedantic);
5361             /* Parse the cast-expression.  */
5362             expr = cp_parser_simple_cast_expression (parser);
5363             /* Restore the PEDANTIC flag.  */
5364             pedantic = saved_pedantic;
5365
5366             return expr;
5367           }
5368
5369         case RID_REALPART:
5370         case RID_IMAGPART:
5371           {
5372             tree expression;
5373
5374             /* Consume the `__real__' or `__imag__' token.  */
5375             cp_lexer_consume_token (parser->lexer);
5376             /* Parse the cast-expression.  */
5377             expression = cp_parser_simple_cast_expression (parser);
5378             /* Create the complete representation.  */
5379             return build_x_unary_op ((keyword == RID_REALPART
5380                                       ? REALPART_EXPR : IMAGPART_EXPR),
5381                                      expression,
5382                                      tf_warning_or_error);
5383           }
5384           break;
5385
5386         default:
5387           break;
5388         }
5389     }
5390
5391   /* Look for the `:: new' and `:: delete', which also signal the
5392      beginning of a new-expression, or delete-expression,
5393      respectively.  If the next token is `::', then it might be one of
5394      these.  */
5395   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5396     {
5397       enum rid keyword;
5398
5399       /* See if the token after the `::' is one of the keywords in
5400          which we're interested.  */
5401       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5402       /* If it's `new', we have a new-expression.  */
5403       if (keyword == RID_NEW)
5404         return cp_parser_new_expression (parser);
5405       /* Similarly, for `delete'.  */
5406       else if (keyword == RID_DELETE)
5407         return cp_parser_delete_expression (parser);
5408     }
5409
5410   /* Look for a unary operator.  */
5411   unary_operator = cp_parser_unary_operator (token);
5412   /* The `++' and `--' operators can be handled similarly, even though
5413      they are not technically unary-operators in the grammar.  */
5414   if (unary_operator == ERROR_MARK)
5415     {
5416       if (token->type == CPP_PLUS_PLUS)
5417         unary_operator = PREINCREMENT_EXPR;
5418       else if (token->type == CPP_MINUS_MINUS)
5419         unary_operator = PREDECREMENT_EXPR;
5420       /* Handle the GNU address-of-label extension.  */
5421       else if (cp_parser_allow_gnu_extensions_p (parser)
5422                && token->type == CPP_AND_AND)
5423         {
5424           tree identifier;
5425           tree expression;
5426
5427           /* Consume the '&&' token.  */
5428           cp_lexer_consume_token (parser->lexer);
5429           /* Look for the identifier.  */
5430           identifier = cp_parser_identifier (parser);
5431           /* Create an expression representing the address.  */
5432           expression = finish_label_address_expr (identifier);
5433           if (cp_parser_non_integral_constant_expression (parser,
5434                                                 "the address of a label"))
5435             expression = error_mark_node;
5436           return expression;
5437         }
5438     }
5439   if (unary_operator != ERROR_MARK)
5440     {
5441       tree cast_expression;
5442       tree expression = error_mark_node;
5443       const char *non_constant_p = NULL;
5444
5445       /* Consume the operator token.  */
5446       token = cp_lexer_consume_token (parser->lexer);
5447       /* Parse the cast-expression.  */
5448       cast_expression
5449         = cp_parser_cast_expression (parser,
5450                                      unary_operator == ADDR_EXPR,
5451                                      /*cast_p=*/false);
5452       /* Now, build an appropriate representation.  */
5453       switch (unary_operator)
5454         {
5455         case INDIRECT_REF:
5456           non_constant_p = "%<*%>";
5457           expression = build_x_indirect_ref (cast_expression, "unary *",
5458                                              tf_warning_or_error);
5459           break;
5460
5461         case ADDR_EXPR:
5462           non_constant_p = "%<&%>";
5463           /* Fall through.  */
5464         case BIT_NOT_EXPR:
5465           expression = build_x_unary_op (unary_operator, cast_expression,
5466                                          tf_warning_or_error);
5467           break;
5468
5469         case PREINCREMENT_EXPR:
5470         case PREDECREMENT_EXPR:
5471           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5472                             ? "%<++%>" : "%<--%>");
5473           /* Fall through.  */
5474         case UNARY_PLUS_EXPR:
5475         case NEGATE_EXPR:
5476         case TRUTH_NOT_EXPR:
5477           expression = finish_unary_op_expr (unary_operator, cast_expression);
5478           break;
5479
5480         default:
5481           gcc_unreachable ();
5482         }
5483
5484       if (non_constant_p
5485           && cp_parser_non_integral_constant_expression (parser,
5486                                                          non_constant_p))
5487         expression = error_mark_node;
5488
5489       return expression;
5490     }
5491
5492   return cp_parser_postfix_expression (parser, address_p, cast_p,
5493                                        /*member_access_only_p=*/false);
5494 }
5495
5496 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5497    unary-operator, the corresponding tree code is returned.  */
5498
5499 static enum tree_code
5500 cp_parser_unary_operator (cp_token* token)
5501 {
5502   switch (token->type)
5503     {
5504     case CPP_MULT:
5505       return INDIRECT_REF;
5506
5507     case CPP_AND:
5508       return ADDR_EXPR;
5509
5510     case CPP_PLUS:
5511       return UNARY_PLUS_EXPR;
5512
5513     case CPP_MINUS:
5514       return NEGATE_EXPR;
5515
5516     case CPP_NOT:
5517       return TRUTH_NOT_EXPR;
5518
5519     case CPP_COMPL:
5520       return BIT_NOT_EXPR;
5521
5522     default:
5523       return ERROR_MARK;
5524     }
5525 }
5526
5527 /* Parse a new-expression.
5528
5529    new-expression:
5530      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5531      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5532
5533    Returns a representation of the expression.  */
5534
5535 static tree
5536 cp_parser_new_expression (cp_parser* parser)
5537 {
5538   bool global_scope_p;
5539   tree placement;
5540   tree type;
5541   tree initializer;
5542   tree nelts;
5543
5544   /* Look for the optional `::' operator.  */
5545   global_scope_p
5546     = (cp_parser_global_scope_opt (parser,
5547                                    /*current_scope_valid_p=*/false)
5548        != NULL_TREE);
5549   /* Look for the `new' operator.  */
5550   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5551   /* There's no easy way to tell a new-placement from the
5552      `( type-id )' construct.  */
5553   cp_parser_parse_tentatively (parser);
5554   /* Look for a new-placement.  */
5555   placement = cp_parser_new_placement (parser);
5556   /* If that didn't work out, there's no new-placement.  */
5557   if (!cp_parser_parse_definitely (parser))
5558     placement = NULL_TREE;
5559
5560   /* If the next token is a `(', then we have a parenthesized
5561      type-id.  */
5562   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5563     {
5564       cp_token *token;
5565       /* Consume the `('.  */
5566       cp_lexer_consume_token (parser->lexer);
5567       /* Parse the type-id.  */
5568       type = cp_parser_type_id (parser);
5569       /* Look for the closing `)'.  */
5570       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5571       token = cp_lexer_peek_token (parser->lexer);
5572       /* There should not be a direct-new-declarator in this production,
5573          but GCC used to allowed this, so we check and emit a sensible error
5574          message for this case.  */
5575       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5576         {
5577           error ("%Harray bound forbidden after parenthesized type-id",
5578                  &token->location);
5579           inform (token->location, 
5580                   "try removing the parentheses around the type-id");
5581           cp_parser_direct_new_declarator (parser);
5582         }
5583       nelts = NULL_TREE;
5584     }
5585   /* Otherwise, there must be a new-type-id.  */
5586   else
5587     type = cp_parser_new_type_id (parser, &nelts);
5588
5589   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5590   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5591       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5592     initializer = cp_parser_new_initializer (parser);
5593   else
5594     initializer = NULL_TREE;
5595
5596   /* A new-expression may not appear in an integral constant
5597      expression.  */
5598   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5599     return error_mark_node;
5600
5601   /* Create a representation of the new-expression.  */
5602   return build_new (placement, type, nelts, initializer, global_scope_p,
5603                     tf_warning_or_error);
5604 }
5605
5606 /* Parse a new-placement.
5607
5608    new-placement:
5609      ( expression-list )
5610
5611    Returns the same representation as for an expression-list.  */
5612
5613 static tree
5614 cp_parser_new_placement (cp_parser* parser)
5615 {
5616   tree expression_list;
5617
5618   /* Parse the expression-list.  */
5619   expression_list = (cp_parser_parenthesized_expression_list
5620                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5621                       /*non_constant_p=*/NULL));
5622
5623   return expression_list;
5624 }
5625
5626 /* Parse a new-type-id.
5627
5628    new-type-id:
5629      type-specifier-seq new-declarator [opt]
5630
5631    Returns the TYPE allocated.  If the new-type-id indicates an array
5632    type, *NELTS is set to the number of elements in the last array
5633    bound; the TYPE will not include the last array bound.  */
5634
5635 static tree
5636 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5637 {
5638   cp_decl_specifier_seq type_specifier_seq;
5639   cp_declarator *new_declarator;
5640   cp_declarator *declarator;
5641   cp_declarator *outer_declarator;
5642   const char *saved_message;
5643   tree type;
5644
5645   /* The type-specifier sequence must not contain type definitions.
5646      (It cannot contain declarations of new types either, but if they
5647      are not definitions we will catch that because they are not
5648      complete.)  */
5649   saved_message = parser->type_definition_forbidden_message;
5650   parser->type_definition_forbidden_message
5651     = "types may not be defined in a new-type-id";
5652   /* Parse the type-specifier-seq.  */
5653   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5654                                 &type_specifier_seq);
5655   /* Restore the old message.  */
5656   parser->type_definition_forbidden_message = saved_message;
5657   /* Parse the new-declarator.  */
5658   new_declarator = cp_parser_new_declarator_opt (parser);
5659
5660   /* Determine the number of elements in the last array dimension, if
5661      any.  */
5662   *nelts = NULL_TREE;
5663   /* Skip down to the last array dimension.  */
5664   declarator = new_declarator;
5665   outer_declarator = NULL;
5666   while (declarator && (declarator->kind == cdk_pointer
5667                         || declarator->kind == cdk_ptrmem))
5668     {
5669       outer_declarator = declarator;
5670       declarator = declarator->declarator;
5671     }
5672   while (declarator
5673          && declarator->kind == cdk_array
5674          && declarator->declarator
5675          && declarator->declarator->kind == cdk_array)
5676     {
5677       outer_declarator = declarator;
5678       declarator = declarator->declarator;
5679     }
5680
5681   if (declarator && declarator->kind == cdk_array)
5682     {
5683       *nelts = declarator->u.array.bounds;
5684       if (*nelts == error_mark_node)
5685         *nelts = integer_one_node;
5686
5687       if (outer_declarator)
5688         outer_declarator->declarator = declarator->declarator;
5689       else
5690         new_declarator = NULL;
5691     }
5692
5693   type = groktypename (&type_specifier_seq, new_declarator);
5694   return type;
5695 }
5696
5697 /* Parse an (optional) new-declarator.
5698
5699    new-declarator:
5700      ptr-operator new-declarator [opt]
5701      direct-new-declarator
5702
5703    Returns the declarator.  */
5704
5705 static cp_declarator *
5706 cp_parser_new_declarator_opt (cp_parser* parser)
5707 {
5708   enum tree_code code;
5709   tree type;
5710   cp_cv_quals cv_quals;
5711
5712   /* We don't know if there's a ptr-operator next, or not.  */
5713   cp_parser_parse_tentatively (parser);
5714   /* Look for a ptr-operator.  */
5715   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5716   /* If that worked, look for more new-declarators.  */
5717   if (cp_parser_parse_definitely (parser))
5718     {
5719       cp_declarator *declarator;
5720
5721       /* Parse another optional declarator.  */
5722       declarator = cp_parser_new_declarator_opt (parser);
5723
5724       return cp_parser_make_indirect_declarator
5725         (code, type, cv_quals, declarator);
5726     }
5727
5728   /* If the next token is a `[', there is a direct-new-declarator.  */
5729   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5730     return cp_parser_direct_new_declarator (parser);
5731
5732   return NULL;
5733 }
5734
5735 /* Parse a direct-new-declarator.
5736
5737    direct-new-declarator:
5738      [ expression ]
5739      direct-new-declarator [constant-expression]
5740
5741    */
5742
5743 static cp_declarator *
5744 cp_parser_direct_new_declarator (cp_parser* parser)
5745 {
5746   cp_declarator *declarator = NULL;
5747
5748   while (true)
5749     {
5750       tree expression;
5751
5752       /* Look for the opening `['.  */
5753       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5754       /* The first expression is not required to be constant.  */
5755       if (!declarator)
5756         {
5757           cp_token *token = cp_lexer_peek_token (parser->lexer);
5758           expression = cp_parser_expression (parser, /*cast_p=*/false);
5759           /* The standard requires that the expression have integral
5760              type.  DR 74 adds enumeration types.  We believe that the
5761              real intent is that these expressions be handled like the
5762              expression in a `switch' condition, which also allows
5763              classes with a single conversion to integral or
5764              enumeration type.  */
5765           if (!processing_template_decl)
5766             {
5767               expression
5768                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5769                                               expression,
5770                                               /*complain=*/true);
5771               if (!expression)
5772                 {
5773                   error ("%Hexpression in new-declarator must have integral "
5774                          "or enumeration type", &token->location);
5775                   expression = error_mark_node;
5776                 }
5777             }
5778         }
5779       /* But all the other expressions must be.  */
5780       else
5781         expression
5782           = cp_parser_constant_expression (parser,
5783                                            /*allow_non_constant=*/false,
5784                                            NULL);
5785       /* Look for the closing `]'.  */
5786       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5787
5788       /* Add this bound to the declarator.  */
5789       declarator = make_array_declarator (declarator, expression);
5790
5791       /* If the next token is not a `[', then there are no more
5792          bounds.  */
5793       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5794         break;
5795     }
5796
5797   return declarator;
5798 }
5799
5800 /* Parse a new-initializer.
5801
5802    new-initializer:
5803      ( expression-list [opt] )
5804      braced-init-list
5805
5806    Returns a representation of the expression-list.  If there is no
5807    expression-list, VOID_ZERO_NODE is returned.  */
5808
5809 static tree
5810 cp_parser_new_initializer (cp_parser* parser)
5811 {
5812   tree expression_list;
5813
5814   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5815     {
5816       bool expr_non_constant_p;
5817       maybe_warn_cpp0x ("extended initializer lists");
5818       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5819       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5820       expression_list = build_tree_list (NULL_TREE, expression_list);
5821     }
5822   else
5823     expression_list = (cp_parser_parenthesized_expression_list
5824                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5825                         /*non_constant_p=*/NULL));
5826   if (!expression_list)
5827     expression_list = void_zero_node;
5828
5829   return expression_list;
5830 }
5831
5832 /* Parse a delete-expression.
5833
5834    delete-expression:
5835      :: [opt] delete cast-expression
5836      :: [opt] delete [ ] cast-expression
5837
5838    Returns a representation of the expression.  */
5839
5840 static tree
5841 cp_parser_delete_expression (cp_parser* parser)
5842 {
5843   bool global_scope_p;
5844   bool array_p;
5845   tree expression;
5846
5847   /* Look for the optional `::' operator.  */
5848   global_scope_p
5849     = (cp_parser_global_scope_opt (parser,
5850                                    /*current_scope_valid_p=*/false)
5851        != NULL_TREE);
5852   /* Look for the `delete' keyword.  */
5853   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5854   /* See if the array syntax is in use.  */
5855   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5856     {
5857       /* Consume the `[' token.  */
5858       cp_lexer_consume_token (parser->lexer);
5859       /* Look for the `]' token.  */
5860       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5861       /* Remember that this is the `[]' construct.  */
5862       array_p = true;
5863     }
5864   else
5865     array_p = false;
5866
5867   /* Parse the cast-expression.  */
5868   expression = cp_parser_simple_cast_expression (parser);
5869
5870   /* A delete-expression may not appear in an integral constant
5871      expression.  */
5872   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5873     return error_mark_node;
5874
5875   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5876 }
5877
5878 /* Parse a cast-expression.
5879
5880    cast-expression:
5881      unary-expression
5882      ( type-id ) cast-expression
5883
5884    ADDRESS_P is true iff the unary-expression is appearing as the
5885    operand of the `&' operator.   CAST_P is true if this expression is
5886    the target of a cast.
5887
5888    Returns a representation of the expression.  */
5889
5890 static tree
5891 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5892 {
5893   /* If it's a `(', then we might be looking at a cast.  */
5894   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5895     {
5896       tree type = NULL_TREE;
5897       tree expr = NULL_TREE;
5898       bool compound_literal_p;
5899       const char *saved_message;
5900
5901       /* There's no way to know yet whether or not this is a cast.
5902          For example, `(int (3))' is a unary-expression, while `(int)
5903          3' is a cast.  So, we resort to parsing tentatively.  */
5904       cp_parser_parse_tentatively (parser);
5905       /* Types may not be defined in a cast.  */
5906       saved_message = parser->type_definition_forbidden_message;
5907       parser->type_definition_forbidden_message
5908         = "types may not be defined in casts";
5909       /* Consume the `('.  */
5910       cp_lexer_consume_token (parser->lexer);
5911       /* A very tricky bit is that `(struct S) { 3 }' is a
5912          compound-literal (which we permit in C++ as an extension).
5913          But, that construct is not a cast-expression -- it is a
5914          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5915          is legal; if the compound-literal were a cast-expression,
5916          you'd need an extra set of parentheses.)  But, if we parse
5917          the type-id, and it happens to be a class-specifier, then we
5918          will commit to the parse at that point, because we cannot
5919          undo the action that is done when creating a new class.  So,
5920          then we cannot back up and do a postfix-expression.
5921
5922          Therefore, we scan ahead to the closing `)', and check to see
5923          if the token after the `)' is a `{'.  If so, we are not
5924          looking at a cast-expression.
5925
5926          Save tokens so that we can put them back.  */
5927       cp_lexer_save_tokens (parser->lexer);
5928       /* Skip tokens until the next token is a closing parenthesis.
5929          If we find the closing `)', and the next token is a `{', then
5930          we are looking at a compound-literal.  */
5931       compound_literal_p
5932         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5933                                                   /*consume_paren=*/true)
5934            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5935       /* Roll back the tokens we skipped.  */
5936       cp_lexer_rollback_tokens (parser->lexer);
5937       /* If we were looking at a compound-literal, simulate an error
5938          so that the call to cp_parser_parse_definitely below will
5939          fail.  */
5940       if (compound_literal_p)
5941         cp_parser_simulate_error (parser);
5942       else
5943         {
5944           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5945           parser->in_type_id_in_expr_p = true;
5946           /* Look for the type-id.  */
5947           type = cp_parser_type_id (parser);
5948           /* Look for the closing `)'.  */
5949           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5950           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5951         }
5952
5953       /* Restore the saved message.  */
5954       parser->type_definition_forbidden_message = saved_message;
5955
5956       /* If ok so far, parse the dependent expression. We cannot be
5957          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5958          ctor of T, but looks like a cast to function returning T
5959          without a dependent expression.  */
5960       if (!cp_parser_error_occurred (parser))
5961         expr = cp_parser_cast_expression (parser,
5962                                           /*address_p=*/false,
5963                                           /*cast_p=*/true);
5964
5965       if (cp_parser_parse_definitely (parser))
5966         {
5967           /* Warn about old-style casts, if so requested.  */
5968           if (warn_old_style_cast
5969               && !in_system_header
5970               && !VOID_TYPE_P (type)
5971               && current_lang_name != lang_name_c)
5972             warning (OPT_Wold_style_cast, "use of old-style cast");
5973
5974           /* Only type conversions to integral or enumeration types
5975              can be used in constant-expressions.  */
5976           if (!cast_valid_in_integral_constant_expression_p (type)
5977               && (cp_parser_non_integral_constant_expression
5978                   (parser,
5979                    "a cast to a type other than an integral or "
5980                    "enumeration type")))
5981             return error_mark_node;
5982
5983           /* Perform the cast.  */
5984           expr = build_c_cast (type, expr);
5985           return expr;
5986         }
5987     }
5988
5989   /* If we get here, then it's not a cast, so it must be a
5990      unary-expression.  */
5991   return cp_parser_unary_expression (parser, address_p, cast_p);
5992 }
5993
5994 /* Parse a binary expression of the general form:
5995
5996    pm-expression:
5997      cast-expression
5998      pm-expression .* cast-expression
5999      pm-expression ->* cast-expression
6000
6001    multiplicative-expression:
6002      pm-expression
6003      multiplicative-expression * pm-expression
6004      multiplicative-expression / pm-expression
6005      multiplicative-expression % pm-expression
6006
6007    additive-expression:
6008      multiplicative-expression
6009      additive-expression + multiplicative-expression
6010      additive-expression - multiplicative-expression
6011
6012    shift-expression:
6013      additive-expression
6014      shift-expression << additive-expression
6015      shift-expression >> additive-expression
6016
6017    relational-expression:
6018      shift-expression
6019      relational-expression < shift-expression
6020      relational-expression > shift-expression
6021      relational-expression <= shift-expression
6022      relational-expression >= shift-expression
6023
6024   GNU Extension:
6025
6026    relational-expression:
6027      relational-expression <? shift-expression
6028      relational-expression >? shift-expression
6029
6030    equality-expression:
6031      relational-expression
6032      equality-expression == relational-expression
6033      equality-expression != relational-expression
6034
6035    and-expression:
6036      equality-expression
6037      and-expression & equality-expression
6038
6039    exclusive-or-expression:
6040      and-expression
6041      exclusive-or-expression ^ and-expression
6042
6043    inclusive-or-expression:
6044      exclusive-or-expression
6045      inclusive-or-expression | exclusive-or-expression
6046
6047    logical-and-expression:
6048      inclusive-or-expression
6049      logical-and-expression && inclusive-or-expression
6050
6051    logical-or-expression:
6052      logical-and-expression
6053      logical-or-expression || logical-and-expression
6054
6055    All these are implemented with a single function like:
6056
6057    binary-expression:
6058      simple-cast-expression
6059      binary-expression <token> binary-expression
6060
6061    CAST_P is true if this expression is the target of a cast.
6062
6063    The binops_by_token map is used to get the tree codes for each <token> type.
6064    binary-expressions are associated according to a precedence table.  */
6065
6066 #define TOKEN_PRECEDENCE(token)                              \
6067 (((token->type == CPP_GREATER                                \
6068    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6069   && !parser->greater_than_is_operator_p)                    \
6070  ? PREC_NOT_OPERATOR                                         \
6071  : binops_by_token[token->type].prec)
6072
6073 static tree
6074 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6075                              enum cp_parser_prec prec)
6076 {
6077   cp_parser_expression_stack stack;
6078   cp_parser_expression_stack_entry *sp = &stack[0];
6079   tree lhs, rhs;
6080   cp_token *token;
6081   enum tree_code tree_type, lhs_type, rhs_type;
6082   enum cp_parser_prec new_prec, lookahead_prec;
6083   bool overloaded_p;
6084
6085   /* Parse the first expression.  */
6086   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6087   lhs_type = ERROR_MARK;
6088
6089   for (;;)
6090     {
6091       /* Get an operator token.  */
6092       token = cp_lexer_peek_token (parser->lexer);
6093
6094       if (warn_cxx0x_compat
6095           && token->type == CPP_RSHIFT
6096           && !parser->greater_than_is_operator_p)
6097         {
6098           warning (OPT_Wc__0x_compat, 
6099                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6100                    &token->location);
6101           warning (OPT_Wc__0x_compat, 
6102                    "suggest parentheses around %<>>%> expression");
6103         }
6104
6105       new_prec = TOKEN_PRECEDENCE (token);
6106
6107       /* Popping an entry off the stack means we completed a subexpression:
6108          - either we found a token which is not an operator (`>' where it is not
6109            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6110            will happen repeatedly;
6111          - or, we found an operator which has lower priority.  This is the case
6112            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6113            parsing `3 * 4'.  */
6114       if (new_prec <= prec)
6115         {
6116           if (sp == stack)
6117             break;
6118           else
6119             goto pop;
6120         }
6121
6122      get_rhs:
6123       tree_type = binops_by_token[token->type].tree_type;
6124
6125       /* We used the operator token.  */
6126       cp_lexer_consume_token (parser->lexer);
6127
6128       /* Extract another operand.  It may be the RHS of this expression
6129          or the LHS of a new, higher priority expression.  */
6130       rhs = cp_parser_simple_cast_expression (parser);
6131       rhs_type = ERROR_MARK;
6132
6133       /* Get another operator token.  Look up its precedence to avoid
6134          building a useless (immediately popped) stack entry for common
6135          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6136       token = cp_lexer_peek_token (parser->lexer);
6137       lookahead_prec = TOKEN_PRECEDENCE (token);
6138       if (lookahead_prec > new_prec)
6139         {
6140           /* ... and prepare to parse the RHS of the new, higher priority
6141              expression.  Since precedence levels on the stack are
6142              monotonically increasing, we do not have to care about
6143              stack overflows.  */
6144           sp->prec = prec;
6145           sp->tree_type = tree_type;
6146           sp->lhs = lhs;
6147           sp->lhs_type = lhs_type;
6148           sp++;
6149           lhs = rhs;
6150           lhs_type = rhs_type;
6151           prec = new_prec;
6152           new_prec = lookahead_prec;
6153           goto get_rhs;
6154
6155          pop:
6156           /* If the stack is not empty, we have parsed into LHS the right side
6157              (`4' in the example above) of an expression we had suspended.
6158              We can use the information on the stack to recover the LHS (`3')
6159              from the stack together with the tree code (`MULT_EXPR'), and
6160              the precedence of the higher level subexpression
6161              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6162              which will be used to actually build the additive expression.  */
6163           --sp;
6164           prec = sp->prec;
6165           tree_type = sp->tree_type;
6166           rhs = lhs;
6167           rhs_type = lhs_type;
6168           lhs = sp->lhs;
6169           lhs_type = sp->lhs_type;
6170         }
6171
6172       overloaded_p = false;
6173       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6174                                &overloaded_p, tf_warning_or_error);
6175       lhs_type = tree_type;
6176
6177       /* If the binary operator required the use of an overloaded operator,
6178          then this expression cannot be an integral constant-expression.
6179          An overloaded operator can be used even if both operands are
6180          otherwise permissible in an integral constant-expression if at
6181          least one of the operands is of enumeration type.  */
6182
6183       if (overloaded_p
6184           && (cp_parser_non_integral_constant_expression
6185               (parser, "calls to overloaded operators")))
6186         return error_mark_node;
6187     }
6188
6189   return lhs;
6190 }
6191
6192
6193 /* Parse the `? expression : assignment-expression' part of a
6194    conditional-expression.  The LOGICAL_OR_EXPR is the
6195    logical-or-expression that started the conditional-expression.
6196    Returns a representation of the entire conditional-expression.
6197
6198    This routine is used by cp_parser_assignment_expression.
6199
6200      ? expression : assignment-expression
6201
6202    GNU Extensions:
6203
6204      ? : assignment-expression */
6205
6206 static tree
6207 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6208 {
6209   tree expr;
6210   tree assignment_expr;
6211
6212   /* Consume the `?' token.  */
6213   cp_lexer_consume_token (parser->lexer);
6214   if (cp_parser_allow_gnu_extensions_p (parser)
6215       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6216     /* Implicit true clause.  */
6217     expr = NULL_TREE;
6218   else
6219     /* Parse the expression.  */
6220     expr = cp_parser_expression (parser, /*cast_p=*/false);
6221
6222   /* The next token should be a `:'.  */
6223   cp_parser_require (parser, CPP_COLON, "%<:%>");
6224   /* Parse the assignment-expression.  */
6225   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6226
6227   /* Build the conditional-expression.  */
6228   return build_x_conditional_expr (logical_or_expr,
6229                                    expr,
6230                                    assignment_expr,
6231                                    tf_warning_or_error);
6232 }
6233
6234 /* Parse an assignment-expression.
6235
6236    assignment-expression:
6237      conditional-expression
6238      logical-or-expression assignment-operator assignment_expression
6239      throw-expression
6240
6241    CAST_P is true if this expression is the target of a cast.
6242
6243    Returns a representation for the expression.  */
6244
6245 static tree
6246 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6247 {
6248   tree expr;
6249
6250   /* If the next token is the `throw' keyword, then we're looking at
6251      a throw-expression.  */
6252   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6253     expr = cp_parser_throw_expression (parser);
6254   /* Otherwise, it must be that we are looking at a
6255      logical-or-expression.  */
6256   else
6257     {
6258       /* Parse the binary expressions (logical-or-expression).  */
6259       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6260       /* If the next token is a `?' then we're actually looking at a
6261          conditional-expression.  */
6262       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6263         return cp_parser_question_colon_clause (parser, expr);
6264       else
6265         {
6266           enum tree_code assignment_operator;
6267
6268           /* If it's an assignment-operator, we're using the second
6269              production.  */
6270           assignment_operator
6271             = cp_parser_assignment_operator_opt (parser);
6272           if (assignment_operator != ERROR_MARK)
6273             {
6274               bool non_constant_p;
6275
6276               /* Parse the right-hand side of the assignment.  */
6277               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6278
6279               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6280                 maybe_warn_cpp0x ("extended initializer lists");
6281
6282               /* An assignment may not appear in a
6283                  constant-expression.  */
6284               if (cp_parser_non_integral_constant_expression (parser,
6285                                                               "an assignment"))
6286                 return error_mark_node;
6287               /* Build the assignment expression.  */
6288               expr = build_x_modify_expr (expr,
6289                                           assignment_operator,
6290                                           rhs,
6291                                           tf_warning_or_error);
6292             }
6293         }
6294     }
6295
6296   return expr;
6297 }
6298
6299 /* Parse an (optional) assignment-operator.
6300
6301    assignment-operator: one of
6302      = *= /= %= += -= >>= <<= &= ^= |=
6303
6304    GNU Extension:
6305
6306    assignment-operator: one of
6307      <?= >?=
6308
6309    If the next token is an assignment operator, the corresponding tree
6310    code is returned, and the token is consumed.  For example, for
6311    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6312    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6313    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6314    operator, ERROR_MARK is returned.  */
6315
6316 static enum tree_code
6317 cp_parser_assignment_operator_opt (cp_parser* parser)
6318 {
6319   enum tree_code op;
6320   cp_token *token;
6321
6322   /* Peek at the next token.  */
6323   token = cp_lexer_peek_token (parser->lexer);
6324
6325   switch (token->type)
6326     {
6327     case CPP_EQ:
6328       op = NOP_EXPR;
6329       break;
6330
6331     case CPP_MULT_EQ:
6332       op = MULT_EXPR;
6333       break;
6334
6335     case CPP_DIV_EQ:
6336       op = TRUNC_DIV_EXPR;
6337       break;
6338
6339     case CPP_MOD_EQ:
6340       op = TRUNC_MOD_EXPR;
6341       break;
6342
6343     case CPP_PLUS_EQ:
6344       op = PLUS_EXPR;
6345       break;
6346
6347     case CPP_MINUS_EQ:
6348       op = MINUS_EXPR;
6349       break;
6350
6351     case CPP_RSHIFT_EQ:
6352       op = RSHIFT_EXPR;
6353       break;
6354
6355     case CPP_LSHIFT_EQ:
6356       op = LSHIFT_EXPR;
6357       break;
6358
6359     case CPP_AND_EQ:
6360       op = BIT_AND_EXPR;
6361       break;
6362
6363     case CPP_XOR_EQ:
6364       op = BIT_XOR_EXPR;
6365       break;
6366
6367     case CPP_OR_EQ:
6368       op = BIT_IOR_EXPR;
6369       break;
6370
6371     default:
6372       /* Nothing else is an assignment operator.  */
6373       op = ERROR_MARK;
6374     }
6375
6376   /* If it was an assignment operator, consume it.  */
6377   if (op != ERROR_MARK)
6378     cp_lexer_consume_token (parser->lexer);
6379
6380   return op;
6381 }
6382
6383 /* Parse an expression.
6384
6385    expression:
6386      assignment-expression
6387      expression , assignment-expression
6388
6389    CAST_P is true if this expression is the target of a cast.
6390
6391    Returns a representation of the expression.  */
6392
6393 static tree
6394 cp_parser_expression (cp_parser* parser, bool cast_p)
6395 {
6396   tree expression = NULL_TREE;
6397
6398   while (true)
6399     {
6400       tree assignment_expression;
6401
6402       /* Parse the next assignment-expression.  */
6403       assignment_expression
6404         = cp_parser_assignment_expression (parser, cast_p);
6405       /* If this is the first assignment-expression, we can just
6406          save it away.  */
6407       if (!expression)
6408         expression = assignment_expression;
6409       else
6410         expression = build_x_compound_expr (expression,
6411                                             assignment_expression,
6412                                             tf_warning_or_error);
6413       /* If the next token is not a comma, then we are done with the
6414          expression.  */
6415       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6416         break;
6417       /* Consume the `,'.  */
6418       cp_lexer_consume_token (parser->lexer);
6419       /* A comma operator cannot appear in a constant-expression.  */
6420       if (cp_parser_non_integral_constant_expression (parser,
6421                                                       "a comma operator"))
6422         expression = error_mark_node;
6423     }
6424
6425   return expression;
6426 }
6427
6428 /* Parse a constant-expression.
6429
6430    constant-expression:
6431      conditional-expression
6432
6433   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6434   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6435   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6436   is false, NON_CONSTANT_P should be NULL.  */
6437
6438 static tree
6439 cp_parser_constant_expression (cp_parser* parser,
6440                                bool allow_non_constant_p,
6441                                bool *non_constant_p)
6442 {
6443   bool saved_integral_constant_expression_p;
6444   bool saved_allow_non_integral_constant_expression_p;
6445   bool saved_non_integral_constant_expression_p;
6446   tree expression;
6447
6448   /* It might seem that we could simply parse the
6449      conditional-expression, and then check to see if it were
6450      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6451      one that the compiler can figure out is constant, possibly after
6452      doing some simplifications or optimizations.  The standard has a
6453      precise definition of constant-expression, and we must honor
6454      that, even though it is somewhat more restrictive.
6455
6456      For example:
6457
6458        int i[(2, 3)];
6459
6460      is not a legal declaration, because `(2, 3)' is not a
6461      constant-expression.  The `,' operator is forbidden in a
6462      constant-expression.  However, GCC's constant-folding machinery
6463      will fold this operation to an INTEGER_CST for `3'.  */
6464
6465   /* Save the old settings.  */
6466   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6467   saved_allow_non_integral_constant_expression_p
6468     = parser->allow_non_integral_constant_expression_p;
6469   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6470   /* We are now parsing a constant-expression.  */
6471   parser->integral_constant_expression_p = true;
6472   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6473   parser->non_integral_constant_expression_p = false;
6474   /* Although the grammar says "conditional-expression", we parse an
6475      "assignment-expression", which also permits "throw-expression"
6476      and the use of assignment operators.  In the case that
6477      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6478      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6479      actually essential that we look for an assignment-expression.
6480      For example, cp_parser_initializer_clauses uses this function to
6481      determine whether a particular assignment-expression is in fact
6482      constant.  */
6483   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6484   /* Restore the old settings.  */
6485   parser->integral_constant_expression_p
6486     = saved_integral_constant_expression_p;
6487   parser->allow_non_integral_constant_expression_p
6488     = saved_allow_non_integral_constant_expression_p;
6489   if (allow_non_constant_p)
6490     *non_constant_p = parser->non_integral_constant_expression_p;
6491   else if (parser->non_integral_constant_expression_p)
6492     expression = error_mark_node;
6493   parser->non_integral_constant_expression_p
6494     = saved_non_integral_constant_expression_p;
6495
6496   return expression;
6497 }
6498
6499 /* Parse __builtin_offsetof.
6500
6501    offsetof-expression:
6502      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6503
6504    offsetof-member-designator:
6505      id-expression
6506      | offsetof-member-designator "." id-expression
6507      | offsetof-member-designator "[" expression "]"  */
6508
6509 static tree
6510 cp_parser_builtin_offsetof (cp_parser *parser)
6511 {
6512   int save_ice_p, save_non_ice_p;
6513   tree type, expr;
6514   cp_id_kind dummy;
6515   cp_token *token;
6516
6517   /* We're about to accept non-integral-constant things, but will
6518      definitely yield an integral constant expression.  Save and
6519      restore these values around our local parsing.  */
6520   save_ice_p = parser->integral_constant_expression_p;
6521   save_non_ice_p = parser->non_integral_constant_expression_p;
6522
6523   /* Consume the "__builtin_offsetof" token.  */
6524   cp_lexer_consume_token (parser->lexer);
6525   /* Consume the opening `('.  */
6526   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6527   /* Parse the type-id.  */
6528   type = cp_parser_type_id (parser);
6529   /* Look for the `,'.  */
6530   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6531   token = cp_lexer_peek_token (parser->lexer);
6532
6533   /* Build the (type *)null that begins the traditional offsetof macro.  */
6534   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6535                             tf_warning_or_error);
6536
6537   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6538   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6539                                                  true, &dummy, token->location);
6540   while (true)
6541     {
6542       token = cp_lexer_peek_token (parser->lexer);
6543       switch (token->type)
6544         {
6545         case CPP_OPEN_SQUARE:
6546           /* offsetof-member-designator "[" expression "]" */
6547           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6548           break;
6549
6550         case CPP_DOT:
6551           /* offsetof-member-designator "." identifier */
6552           cp_lexer_consume_token (parser->lexer);
6553           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6554                                                          true, &dummy,
6555                                                          token->location);
6556           break;
6557
6558         case CPP_CLOSE_PAREN:
6559           /* Consume the ")" token.  */
6560           cp_lexer_consume_token (parser->lexer);
6561           goto success;
6562
6563         default:
6564           /* Error.  We know the following require will fail, but
6565              that gives the proper error message.  */
6566           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6567           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6568           expr = error_mark_node;
6569           goto failure;
6570         }
6571     }
6572
6573  success:
6574   /* If we're processing a template, we can't finish the semantics yet.
6575      Otherwise we can fold the entire expression now.  */
6576   if (processing_template_decl)
6577     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6578   else
6579     expr = finish_offsetof (expr);
6580
6581  failure:
6582   parser->integral_constant_expression_p = save_ice_p;
6583   parser->non_integral_constant_expression_p = save_non_ice_p;
6584
6585   return expr;
6586 }
6587
6588 /* Parse a trait expression.  */
6589
6590 static tree
6591 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6592 {
6593   cp_trait_kind kind;
6594   tree type1, type2 = NULL_TREE;
6595   bool binary = false;
6596   cp_decl_specifier_seq decl_specs;
6597
6598   switch (keyword)
6599     {
6600     case RID_HAS_NOTHROW_ASSIGN:
6601       kind = CPTK_HAS_NOTHROW_ASSIGN;
6602       break;
6603     case RID_HAS_NOTHROW_CONSTRUCTOR:
6604       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6605       break;
6606     case RID_HAS_NOTHROW_COPY:
6607       kind = CPTK_HAS_NOTHROW_COPY;
6608       break;
6609     case RID_HAS_TRIVIAL_ASSIGN:
6610       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6611       break;
6612     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6613       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6614       break;
6615     case RID_HAS_TRIVIAL_COPY:
6616       kind = CPTK_HAS_TRIVIAL_COPY;
6617       break;
6618     case RID_HAS_TRIVIAL_DESTRUCTOR:
6619       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6620       break;
6621     case RID_HAS_VIRTUAL_DESTRUCTOR:
6622       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6623       break;
6624     case RID_IS_ABSTRACT:
6625       kind = CPTK_IS_ABSTRACT;
6626       break;
6627     case RID_IS_BASE_OF:
6628       kind = CPTK_IS_BASE_OF;
6629       binary = true;
6630       break;
6631     case RID_IS_CLASS:
6632       kind = CPTK_IS_CLASS;
6633       break;
6634     case RID_IS_CONVERTIBLE_TO:
6635       kind = CPTK_IS_CONVERTIBLE_TO;
6636       binary = true;
6637       break;
6638     case RID_IS_EMPTY:
6639       kind = CPTK_IS_EMPTY;
6640       break;
6641     case RID_IS_ENUM:
6642       kind = CPTK_IS_ENUM;
6643       break;
6644     case RID_IS_POD:
6645       kind = CPTK_IS_POD;
6646       break;
6647     case RID_IS_POLYMORPHIC:
6648       kind = CPTK_IS_POLYMORPHIC;
6649       break;
6650     case RID_IS_UNION:
6651       kind = CPTK_IS_UNION;
6652       break;
6653     default:
6654       gcc_unreachable ();
6655     }
6656
6657   /* Consume the token.  */
6658   cp_lexer_consume_token (parser->lexer);
6659
6660   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6661
6662   type1 = cp_parser_type_id (parser);
6663
6664   if (type1 == error_mark_node)
6665     return error_mark_node;
6666
6667   /* Build a trivial decl-specifier-seq.  */
6668   clear_decl_specs (&decl_specs);
6669   decl_specs.type = type1;
6670
6671   /* Call grokdeclarator to figure out what type this is.  */
6672   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6673                           /*initialized=*/0, /*attrlist=*/NULL);
6674
6675   if (binary)
6676     {
6677       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6678  
6679       type2 = cp_parser_type_id (parser);
6680
6681       if (type2 == error_mark_node)
6682         return error_mark_node;
6683
6684       /* Build a trivial decl-specifier-seq.  */
6685       clear_decl_specs (&decl_specs);
6686       decl_specs.type = type2;
6687
6688       /* Call grokdeclarator to figure out what type this is.  */
6689       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6690                               /*initialized=*/0, /*attrlist=*/NULL);
6691     }
6692
6693   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6694
6695   /* Complete the trait expression, which may mean either processing
6696      the trait expr now or saving it for template instantiation.  */
6697   return finish_trait_expr (kind, type1, type2);
6698 }
6699
6700 /* Statements [gram.stmt.stmt]  */
6701
6702 /* Parse a statement.
6703
6704    statement:
6705      labeled-statement
6706      expression-statement
6707      compound-statement
6708      selection-statement
6709      iteration-statement
6710      jump-statement
6711      declaration-statement
6712      try-block
6713
6714   IN_COMPOUND is true when the statement is nested inside a
6715   cp_parser_compound_statement; this matters for certain pragmas.
6716
6717   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6718   is a (possibly labeled) if statement which is not enclosed in braces
6719   and has an else clause.  This is used to implement -Wparentheses.  */
6720
6721 static void
6722 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6723                      bool in_compound, bool *if_p)
6724 {
6725   tree statement;
6726   cp_token *token;
6727   location_t statement_location;
6728
6729  restart:
6730   if (if_p != NULL)
6731     *if_p = false;
6732   /* There is no statement yet.  */
6733   statement = NULL_TREE;
6734   /* Peek at the next token.  */
6735   token = cp_lexer_peek_token (parser->lexer);
6736   /* Remember the location of the first token in the statement.  */
6737   statement_location = token->location;
6738   /* If this is a keyword, then that will often determine what kind of
6739      statement we have.  */
6740   if (token->type == CPP_KEYWORD)
6741     {
6742       enum rid keyword = token->keyword;
6743
6744       switch (keyword)
6745         {
6746         case RID_CASE:
6747         case RID_DEFAULT:
6748           /* Looks like a labeled-statement with a case label.
6749              Parse the label, and then use tail recursion to parse
6750              the statement.  */
6751           cp_parser_label_for_labeled_statement (parser);
6752           goto restart;
6753
6754         case RID_IF:
6755         case RID_SWITCH:
6756           statement = cp_parser_selection_statement (parser, if_p);
6757           break;
6758
6759         case RID_WHILE:
6760         case RID_DO:
6761         case RID_FOR:
6762           statement = cp_parser_iteration_statement (parser);
6763           break;
6764
6765         case RID_BREAK:
6766         case RID_CONTINUE:
6767         case RID_RETURN:
6768         case RID_GOTO:
6769           statement = cp_parser_jump_statement (parser);
6770           break;
6771
6772           /* Objective-C++ exception-handling constructs.  */
6773         case RID_AT_TRY:
6774         case RID_AT_CATCH:
6775         case RID_AT_FINALLY:
6776         case RID_AT_SYNCHRONIZED:
6777         case RID_AT_THROW:
6778           statement = cp_parser_objc_statement (parser);
6779           break;
6780
6781         case RID_TRY:
6782           statement = cp_parser_try_block (parser);
6783           break;
6784
6785         case RID_NAMESPACE:
6786           /* This must be a namespace alias definition.  */
6787           cp_parser_declaration_statement (parser);
6788           return;
6789           
6790         default:
6791           /* It might be a keyword like `int' that can start a
6792              declaration-statement.  */
6793           break;
6794         }
6795     }
6796   else if (token->type == CPP_NAME)
6797     {
6798       /* If the next token is a `:', then we are looking at a
6799          labeled-statement.  */
6800       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6801       if (token->type == CPP_COLON)
6802         {
6803           /* Looks like a labeled-statement with an ordinary label.
6804              Parse the label, and then use tail recursion to parse
6805              the statement.  */
6806           cp_parser_label_for_labeled_statement (parser);
6807           goto restart;
6808         }
6809     }
6810   /* Anything that starts with a `{' must be a compound-statement.  */
6811   else if (token->type == CPP_OPEN_BRACE)
6812     statement = cp_parser_compound_statement (parser, NULL, false);
6813   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6814      a statement all its own.  */
6815   else if (token->type == CPP_PRAGMA)
6816     {
6817       /* Only certain OpenMP pragmas are attached to statements, and thus
6818          are considered statements themselves.  All others are not.  In
6819          the context of a compound, accept the pragma as a "statement" and
6820          return so that we can check for a close brace.  Otherwise we
6821          require a real statement and must go back and read one.  */
6822       if (in_compound)
6823         cp_parser_pragma (parser, pragma_compound);
6824       else if (!cp_parser_pragma (parser, pragma_stmt))
6825         goto restart;
6826       return;
6827     }
6828   else if (token->type == CPP_EOF)
6829     {
6830       cp_parser_error (parser, "expected statement");
6831       return;
6832     }
6833
6834   /* Everything else must be a declaration-statement or an
6835      expression-statement.  Try for the declaration-statement
6836      first, unless we are looking at a `;', in which case we know that
6837      we have an expression-statement.  */
6838   if (!statement)
6839     {
6840       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6841         {
6842           cp_parser_parse_tentatively (parser);
6843           /* Try to parse the declaration-statement.  */
6844           cp_parser_declaration_statement (parser);
6845           /* If that worked, we're done.  */
6846           if (cp_parser_parse_definitely (parser))
6847             return;
6848         }
6849       /* Look for an expression-statement instead.  */
6850       statement = cp_parser_expression_statement (parser, in_statement_expr);
6851     }
6852
6853   /* Set the line number for the statement.  */
6854   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6855     SET_EXPR_LOCATION (statement, statement_location);
6856 }
6857
6858 /* Parse the label for a labeled-statement, i.e.
6859
6860    identifier :
6861    case constant-expression :
6862    default :
6863
6864    GNU Extension:
6865    case constant-expression ... constant-expression : statement
6866
6867    When a label is parsed without errors, the label is added to the
6868    parse tree by the finish_* functions, so this function doesn't
6869    have to return the label.  */
6870
6871 static void
6872 cp_parser_label_for_labeled_statement (cp_parser* parser)
6873 {
6874   cp_token *token;
6875
6876   /* The next token should be an identifier.  */
6877   token = cp_lexer_peek_token (parser->lexer);
6878   if (token->type != CPP_NAME
6879       && token->type != CPP_KEYWORD)
6880     {
6881       cp_parser_error (parser, "expected labeled-statement");
6882       return;
6883     }
6884
6885   switch (token->keyword)
6886     {
6887     case RID_CASE:
6888       {
6889         tree expr, expr_hi;
6890         cp_token *ellipsis;
6891
6892         /* Consume the `case' token.  */
6893         cp_lexer_consume_token (parser->lexer);
6894         /* Parse the constant-expression.  */
6895         expr = cp_parser_constant_expression (parser,
6896                                               /*allow_non_constant_p=*/false,
6897                                               NULL);
6898
6899         ellipsis = cp_lexer_peek_token (parser->lexer);
6900         if (ellipsis->type == CPP_ELLIPSIS)
6901           {
6902             /* Consume the `...' token.  */
6903             cp_lexer_consume_token (parser->lexer);
6904             expr_hi =
6905               cp_parser_constant_expression (parser,
6906                                              /*allow_non_constant_p=*/false,
6907                                              NULL);
6908             /* We don't need to emit warnings here, as the common code
6909                will do this for us.  */
6910           }
6911         else
6912           expr_hi = NULL_TREE;
6913
6914         if (parser->in_switch_statement_p)
6915           finish_case_label (expr, expr_hi);
6916         else
6917           error ("%Hcase label %qE not within a switch statement",
6918                  &token->location, expr);
6919       }
6920       break;
6921
6922     case RID_DEFAULT:
6923       /* Consume the `default' token.  */
6924       cp_lexer_consume_token (parser->lexer);
6925
6926       if (parser->in_switch_statement_p)
6927         finish_case_label (NULL_TREE, NULL_TREE);
6928       else
6929         error ("%Hcase label not within a switch statement", &token->location);
6930       break;
6931
6932     default:
6933       /* Anything else must be an ordinary label.  */
6934       finish_label_stmt (cp_parser_identifier (parser));
6935       break;
6936     }
6937
6938   /* Require the `:' token.  */
6939   cp_parser_require (parser, CPP_COLON, "%<:%>");
6940 }
6941
6942 /* Parse an expression-statement.
6943
6944    expression-statement:
6945      expression [opt] ;
6946
6947    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6948    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6949    indicates whether this expression-statement is part of an
6950    expression statement.  */
6951
6952 static tree
6953 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6954 {
6955   tree statement = NULL_TREE;
6956
6957   /* If the next token is a ';', then there is no expression
6958      statement.  */
6959   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6960     statement = cp_parser_expression (parser, /*cast_p=*/false);
6961
6962   /* Consume the final `;'.  */
6963   cp_parser_consume_semicolon_at_end_of_statement (parser);
6964
6965   if (in_statement_expr
6966       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6967     /* This is the final expression statement of a statement
6968        expression.  */
6969     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6970   else if (statement)
6971     statement = finish_expr_stmt (statement);
6972   else
6973     finish_stmt ();
6974
6975   return statement;
6976 }
6977
6978 /* Parse a compound-statement.
6979
6980    compound-statement:
6981      { statement-seq [opt] }
6982
6983    GNU extension:
6984
6985    compound-statement:
6986      { label-declaration-seq [opt] statement-seq [opt] }
6987
6988    label-declaration-seq:
6989      label-declaration
6990      label-declaration-seq label-declaration
6991
6992    Returns a tree representing the statement.  */
6993
6994 static tree
6995 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6996                               bool in_try)
6997 {
6998   tree compound_stmt;
6999
7000   /* Consume the `{'.  */
7001   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7002     return error_mark_node;
7003   /* Begin the compound-statement.  */
7004   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7005   /* If the next keyword is `__label__' we have a label declaration.  */
7006   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7007     cp_parser_label_declaration (parser);
7008   /* Parse an (optional) statement-seq.  */
7009   cp_parser_statement_seq_opt (parser, in_statement_expr);
7010   /* Finish the compound-statement.  */
7011   finish_compound_stmt (compound_stmt);
7012   /* Consume the `}'.  */
7013   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7014
7015   return compound_stmt;
7016 }
7017
7018 /* Parse an (optional) statement-seq.
7019
7020    statement-seq:
7021      statement
7022      statement-seq [opt] statement  */
7023
7024 static void
7025 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7026 {
7027   /* Scan statements until there aren't any more.  */
7028   while (true)
7029     {
7030       cp_token *token = cp_lexer_peek_token (parser->lexer);
7031
7032       /* If we're looking at a `}', then we've run out of statements.  */
7033       if (token->type == CPP_CLOSE_BRACE
7034           || token->type == CPP_EOF
7035           || token->type == CPP_PRAGMA_EOL)
7036         break;
7037       
7038       /* If we are in a compound statement and find 'else' then
7039          something went wrong.  */
7040       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7041         {
7042           if (parser->in_statement & IN_IF_STMT) 
7043             break;
7044           else
7045             {
7046               token = cp_lexer_consume_token (parser->lexer);
7047               error ("%H%<else%> without a previous %<if%>", &token->location);
7048             }
7049         }
7050
7051       /* Parse the statement.  */
7052       cp_parser_statement (parser, in_statement_expr, true, NULL);
7053     }
7054 }
7055
7056 /* Parse a selection-statement.
7057
7058    selection-statement:
7059      if ( condition ) statement
7060      if ( condition ) statement else statement
7061      switch ( condition ) statement
7062
7063    Returns the new IF_STMT or SWITCH_STMT.
7064
7065    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7066    is a (possibly labeled) if statement which is not enclosed in
7067    braces and has an else clause.  This is used to implement
7068    -Wparentheses.  */
7069
7070 static tree
7071 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7072 {
7073   cp_token *token;
7074   enum rid keyword;
7075
7076   if (if_p != NULL)
7077     *if_p = false;
7078
7079   /* Peek at the next token.  */
7080   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7081
7082   /* See what kind of keyword it is.  */
7083   keyword = token->keyword;
7084   switch (keyword)
7085     {
7086     case RID_IF:
7087     case RID_SWITCH:
7088       {
7089         tree statement;
7090         tree condition;
7091
7092         /* Look for the `('.  */
7093         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7094           {
7095             cp_parser_skip_to_end_of_statement (parser);
7096             return error_mark_node;
7097           }
7098
7099         /* Begin the selection-statement.  */
7100         if (keyword == RID_IF)
7101           statement = begin_if_stmt ();
7102         else
7103           statement = begin_switch_stmt ();
7104
7105         /* Parse the condition.  */
7106         condition = cp_parser_condition (parser);
7107         /* Look for the `)'.  */
7108         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7109           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7110                                                  /*consume_paren=*/true);
7111
7112         if (keyword == RID_IF)
7113           {
7114             bool nested_if;
7115             unsigned char in_statement;
7116
7117             /* Add the condition.  */
7118             finish_if_stmt_cond (condition, statement);
7119
7120             /* Parse the then-clause.  */
7121             in_statement = parser->in_statement;
7122             parser->in_statement |= IN_IF_STMT;
7123             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7124             parser->in_statement = in_statement;
7125
7126             finish_then_clause (statement);
7127
7128             /* If the next token is `else', parse the else-clause.  */
7129             if (cp_lexer_next_token_is_keyword (parser->lexer,
7130                                                 RID_ELSE))
7131               {
7132                 /* Consume the `else' keyword.  */
7133                 cp_lexer_consume_token (parser->lexer);
7134                 begin_else_clause (statement);
7135                 /* Parse the else-clause.  */
7136                 cp_parser_implicitly_scoped_statement (parser, NULL);
7137                 finish_else_clause (statement);
7138
7139                 /* If we are currently parsing a then-clause, then
7140                    IF_P will not be NULL.  We set it to true to
7141                    indicate that this if statement has an else clause.
7142                    This may trigger the Wparentheses warning below
7143                    when we get back up to the parent if statement.  */
7144                 if (if_p != NULL)
7145                   *if_p = true;
7146               }
7147             else
7148               {
7149                 /* This if statement does not have an else clause.  If
7150                    NESTED_IF is true, then the then-clause is an if
7151                    statement which does have an else clause.  We warn
7152                    about the potential ambiguity.  */
7153                 if (nested_if)
7154                   warning (OPT_Wparentheses,
7155                            ("%Hsuggest explicit braces "
7156                             "to avoid ambiguous %<else%>"),
7157                            EXPR_LOCUS (statement));
7158               }
7159
7160             /* Now we're all done with the if-statement.  */
7161             finish_if_stmt (statement);
7162           }
7163         else
7164           {
7165             bool in_switch_statement_p;
7166             unsigned char in_statement;
7167
7168             /* Add the condition.  */
7169             finish_switch_cond (condition, statement);
7170
7171             /* Parse the body of the switch-statement.  */
7172             in_switch_statement_p = parser->in_switch_statement_p;
7173             in_statement = parser->in_statement;
7174             parser->in_switch_statement_p = true;
7175             parser->in_statement |= IN_SWITCH_STMT;
7176             cp_parser_implicitly_scoped_statement (parser, NULL);
7177             parser->in_switch_statement_p = in_switch_statement_p;
7178             parser->in_statement = in_statement;
7179
7180             /* Now we're all done with the switch-statement.  */
7181             finish_switch_stmt (statement);
7182           }
7183
7184         return statement;
7185       }
7186       break;
7187
7188     default:
7189       cp_parser_error (parser, "expected selection-statement");
7190       return error_mark_node;
7191     }
7192 }
7193
7194 /* Parse a condition.
7195
7196    condition:
7197      expression
7198      type-specifier-seq declarator = initializer-clause
7199      type-specifier-seq declarator braced-init-list
7200
7201    GNU Extension:
7202
7203    condition:
7204      type-specifier-seq declarator asm-specification [opt]
7205        attributes [opt] = assignment-expression
7206
7207    Returns the expression that should be tested.  */
7208
7209 static tree
7210 cp_parser_condition (cp_parser* parser)
7211 {
7212   cp_decl_specifier_seq type_specifiers;
7213   const char *saved_message;
7214
7215   /* Try the declaration first.  */
7216   cp_parser_parse_tentatively (parser);
7217   /* New types are not allowed in the type-specifier-seq for a
7218      condition.  */
7219   saved_message = parser->type_definition_forbidden_message;
7220   parser->type_definition_forbidden_message
7221     = "types may not be defined in conditions";
7222   /* Parse the type-specifier-seq.  */
7223   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7224                                 &type_specifiers);
7225   /* Restore the saved message.  */
7226   parser->type_definition_forbidden_message = saved_message;
7227   /* If all is well, we might be looking at a declaration.  */
7228   if (!cp_parser_error_occurred (parser))
7229     {
7230       tree decl;
7231       tree asm_specification;
7232       tree attributes;
7233       cp_declarator *declarator;
7234       tree initializer = NULL_TREE;
7235
7236       /* Parse the declarator.  */
7237       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7238                                          /*ctor_dtor_or_conv_p=*/NULL,
7239                                          /*parenthesized_p=*/NULL,
7240                                          /*member_p=*/false);
7241       /* Parse the attributes.  */
7242       attributes = cp_parser_attributes_opt (parser);
7243       /* Parse the asm-specification.  */
7244       asm_specification = cp_parser_asm_specification_opt (parser);
7245       /* If the next token is not an `=' or '{', then we might still be
7246          looking at an expression.  For example:
7247
7248            if (A(a).x)
7249
7250          looks like a decl-specifier-seq and a declarator -- but then
7251          there is no `=', so this is an expression.  */
7252       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7253           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7254         cp_parser_simulate_error (parser);
7255         
7256       /* If we did see an `=' or '{', then we are looking at a declaration
7257          for sure.  */
7258       if (cp_parser_parse_definitely (parser))
7259         {
7260           tree pushed_scope;
7261           bool non_constant_p;
7262           bool flags = LOOKUP_ONLYCONVERTING;
7263
7264           /* Create the declaration.  */
7265           decl = start_decl (declarator, &type_specifiers,
7266                              /*initialized_p=*/true,
7267                              attributes, /*prefix_attributes=*/NULL_TREE,
7268                              &pushed_scope);
7269
7270           /* Parse the initializer.  */
7271           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7272             {
7273               initializer = cp_parser_braced_list (parser, &non_constant_p);
7274               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7275               flags = 0;
7276             }
7277           else
7278             {
7279               /* Consume the `='.  */
7280               cp_lexer_consume_token (parser->lexer);
7281               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7282             }
7283           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7284             maybe_warn_cpp0x ("extended initializer lists");
7285
7286           if (!non_constant_p)
7287             initializer = fold_non_dependent_expr (initializer);
7288
7289           /* Process the initializer.  */
7290           cp_finish_decl (decl,
7291                           initializer, !non_constant_p,
7292                           asm_specification,
7293                           flags);
7294
7295           if (pushed_scope)
7296             pop_scope (pushed_scope);
7297
7298           return convert_from_reference (decl);
7299         }
7300     }
7301   /* If we didn't even get past the declarator successfully, we are
7302      definitely not looking at a declaration.  */
7303   else
7304     cp_parser_abort_tentative_parse (parser);
7305
7306   /* Otherwise, we are looking at an expression.  */
7307   return cp_parser_expression (parser, /*cast_p=*/false);
7308 }
7309
7310 /* We check for a ) immediately followed by ; with no whitespacing
7311    between.  This is used to issue a warning for:
7312
7313      while (...);
7314
7315    and:
7316
7317      for (...);
7318
7319    as the semicolon is probably extraneous.
7320
7321    On parse errors, the next token might not be a ), so do nothing in
7322    that case. */
7323
7324 static void
7325 check_empty_body (cp_parser* parser, const char* type)
7326 {
7327   cp_token *token;
7328   cp_token *close_paren;
7329   expanded_location close_loc;
7330   expanded_location semi_loc;
7331   
7332   close_paren = cp_lexer_peek_token (parser->lexer);
7333   if (close_paren->type != CPP_CLOSE_PAREN)
7334     return;
7335
7336   close_loc = expand_location (close_paren->location);
7337   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7338
7339   if (token->type != CPP_SEMICOLON
7340       || (token->flags & PREV_WHITE))
7341     return;
7342
7343   semi_loc =  expand_location (token->location);
7344   if (close_loc.line == semi_loc.line
7345       && close_loc.column+1 == semi_loc.column)
7346     warning (OPT_Wempty_body,
7347              "suggest a space before %<;%> or explicit braces around empty "
7348              "body in %<%s%> statement",
7349              type);
7350 }
7351
7352 /* Parse an iteration-statement.
7353
7354    iteration-statement:
7355      while ( condition ) statement
7356      do statement while ( expression ) ;
7357      for ( for-init-statement condition [opt] ; expression [opt] )
7358        statement
7359
7360    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7361
7362 static tree
7363 cp_parser_iteration_statement (cp_parser* parser)
7364 {
7365   cp_token *token;
7366   enum rid keyword;
7367   tree statement;
7368   unsigned char in_statement;
7369
7370   /* Peek at the next token.  */
7371   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7372   if (!token)
7373     return error_mark_node;
7374
7375   /* Remember whether or not we are already within an iteration
7376      statement.  */
7377   in_statement = parser->in_statement;
7378
7379   /* See what kind of keyword it is.  */
7380   keyword = token->keyword;
7381   switch (keyword)
7382     {
7383     case RID_WHILE:
7384       {
7385         tree condition;
7386
7387         /* Begin the while-statement.  */
7388         statement = begin_while_stmt ();
7389         /* Look for the `('.  */
7390         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7391         /* Parse the condition.  */
7392         condition = cp_parser_condition (parser);
7393         finish_while_stmt_cond (condition, statement);
7394         check_empty_body (parser, "while");
7395         /* Look for the `)'.  */
7396         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7397         /* Parse the dependent statement.  */
7398         parser->in_statement = IN_ITERATION_STMT;
7399         cp_parser_already_scoped_statement (parser);
7400         parser->in_statement = in_statement;
7401         /* We're done with the while-statement.  */
7402         finish_while_stmt (statement);
7403       }
7404       break;
7405
7406     case RID_DO:
7407       {
7408         tree expression;
7409
7410         /* Begin the do-statement.  */
7411         statement = begin_do_stmt ();
7412         /* Parse the body of the do-statement.  */
7413         parser->in_statement = IN_ITERATION_STMT;
7414         cp_parser_implicitly_scoped_statement (parser, NULL);
7415         parser->in_statement = in_statement;
7416         finish_do_body (statement);
7417         /* Look for the `while' keyword.  */
7418         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7419         /* Look for the `('.  */
7420         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7421         /* Parse the expression.  */
7422         expression = cp_parser_expression (parser, /*cast_p=*/false);
7423         /* We're done with the do-statement.  */
7424         finish_do_stmt (expression, statement);
7425         /* Look for the `)'.  */
7426         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7427         /* Look for the `;'.  */
7428         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7429       }
7430       break;
7431
7432     case RID_FOR:
7433       {
7434         tree condition = NULL_TREE;
7435         tree expression = NULL_TREE;
7436
7437         /* Begin the for-statement.  */
7438         statement = begin_for_stmt ();
7439         /* Look for the `('.  */
7440         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7441         /* Parse the initialization.  */
7442         cp_parser_for_init_statement (parser);
7443         finish_for_init_stmt (statement);
7444
7445         /* If there's a condition, process it.  */
7446         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7447           condition = cp_parser_condition (parser);
7448         finish_for_cond (condition, statement);
7449         /* Look for the `;'.  */
7450         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7451
7452         /* If there's an expression, process it.  */
7453         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7454           expression = cp_parser_expression (parser, /*cast_p=*/false);
7455         finish_for_expr (expression, statement);
7456         check_empty_body (parser, "for");
7457         /* Look for the `)'.  */
7458         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7459
7460         /* Parse the body of the for-statement.  */
7461         parser->in_statement = IN_ITERATION_STMT;
7462         cp_parser_already_scoped_statement (parser);
7463         parser->in_statement = in_statement;
7464
7465         /* We're done with the for-statement.  */
7466         finish_for_stmt (statement);
7467       }
7468       break;
7469
7470     default:
7471       cp_parser_error (parser, "expected iteration-statement");
7472       statement = error_mark_node;
7473       break;
7474     }
7475
7476   return statement;
7477 }
7478
7479 /* Parse a for-init-statement.
7480
7481    for-init-statement:
7482      expression-statement
7483      simple-declaration  */
7484
7485 static void
7486 cp_parser_for_init_statement (cp_parser* parser)
7487 {
7488   /* If the next token is a `;', then we have an empty
7489      expression-statement.  Grammatically, this is also a
7490      simple-declaration, but an invalid one, because it does not
7491      declare anything.  Therefore, if we did not handle this case
7492      specially, we would issue an error message about an invalid
7493      declaration.  */
7494   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7495     {
7496       /* We're going to speculatively look for a declaration, falling back
7497          to an expression, if necessary.  */
7498       cp_parser_parse_tentatively (parser);
7499       /* Parse the declaration.  */
7500       cp_parser_simple_declaration (parser,
7501                                     /*function_definition_allowed_p=*/false);
7502       /* If the tentative parse failed, then we shall need to look for an
7503          expression-statement.  */
7504       if (cp_parser_parse_definitely (parser))
7505         return;
7506     }
7507
7508   cp_parser_expression_statement (parser, false);
7509 }
7510
7511 /* Parse a jump-statement.
7512
7513    jump-statement:
7514      break ;
7515      continue ;
7516      return expression [opt] ;
7517      return braced-init-list ;
7518      goto identifier ;
7519
7520    GNU extension:
7521
7522    jump-statement:
7523      goto * expression ;
7524
7525    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7526
7527 static tree
7528 cp_parser_jump_statement (cp_parser* parser)
7529 {
7530   tree statement = error_mark_node;
7531   cp_token *token;
7532   enum rid keyword;
7533   unsigned char in_statement;
7534
7535   /* Peek at the next token.  */
7536   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7537   if (!token)
7538     return error_mark_node;
7539
7540   /* See what kind of keyword it is.  */
7541   keyword = token->keyword;
7542   switch (keyword)
7543     {
7544     case RID_BREAK:
7545       in_statement = parser->in_statement & ~IN_IF_STMT;      
7546       switch (in_statement)
7547         {
7548         case 0:
7549           error ("%Hbreak statement not within loop or switch", &token->location);
7550           break;
7551         default:
7552           gcc_assert ((in_statement & IN_SWITCH_STMT)
7553                       || in_statement == IN_ITERATION_STMT);
7554           statement = finish_break_stmt ();
7555           break;
7556         case IN_OMP_BLOCK:
7557           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7558           break;
7559         case IN_OMP_FOR:
7560           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7561           break;
7562         }
7563       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7564       break;
7565
7566     case RID_CONTINUE:
7567       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7568         {
7569         case 0:
7570           error ("%Hcontinue statement not within a loop", &token->location);
7571           break;
7572         case IN_ITERATION_STMT:
7573         case IN_OMP_FOR:
7574           statement = finish_continue_stmt ();
7575           break;
7576         case IN_OMP_BLOCK:
7577           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7578           break;
7579         default:
7580           gcc_unreachable ();
7581         }
7582       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7583       break;
7584
7585     case RID_RETURN:
7586       {
7587         tree expr;
7588         bool expr_non_constant_p;
7589
7590         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7591           {
7592             maybe_warn_cpp0x ("extended initializer lists");
7593             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7594           }
7595         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7596           expr = cp_parser_expression (parser, /*cast_p=*/false);
7597         else
7598           /* If the next token is a `;', then there is no
7599              expression.  */
7600           expr = NULL_TREE;
7601         /* Build the return-statement.  */
7602         statement = finish_return_stmt (expr);
7603         /* Look for the final `;'.  */
7604         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7605       }
7606       break;
7607
7608     case RID_GOTO:
7609       /* Create the goto-statement.  */
7610       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7611         {
7612           /* Issue a warning about this use of a GNU extension.  */
7613           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7614           /* Consume the '*' token.  */
7615           cp_lexer_consume_token (parser->lexer);
7616           /* Parse the dependent expression.  */
7617           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7618         }
7619       else
7620         finish_goto_stmt (cp_parser_identifier (parser));
7621       /* Look for the final `;'.  */
7622       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7623       break;
7624
7625     default:
7626       cp_parser_error (parser, "expected jump-statement");
7627       break;
7628     }
7629
7630   return statement;
7631 }
7632
7633 /* Parse a declaration-statement.
7634
7635    declaration-statement:
7636      block-declaration  */
7637
7638 static void
7639 cp_parser_declaration_statement (cp_parser* parser)
7640 {
7641   void *p;
7642
7643   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7644   p = obstack_alloc (&declarator_obstack, 0);
7645
7646  /* Parse the block-declaration.  */
7647   cp_parser_block_declaration (parser, /*statement_p=*/true);
7648
7649   /* Free any declarators allocated.  */
7650   obstack_free (&declarator_obstack, p);
7651
7652   /* Finish off the statement.  */
7653   finish_stmt ();
7654 }
7655
7656 /* Some dependent statements (like `if (cond) statement'), are
7657    implicitly in their own scope.  In other words, if the statement is
7658    a single statement (as opposed to a compound-statement), it is
7659    none-the-less treated as if it were enclosed in braces.  Any
7660    declarations appearing in the dependent statement are out of scope
7661    after control passes that point.  This function parses a statement,
7662    but ensures that is in its own scope, even if it is not a
7663    compound-statement.
7664
7665    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7666    is a (possibly labeled) if statement which is not enclosed in
7667    braces and has an else clause.  This is used to implement
7668    -Wparentheses.
7669
7670    Returns the new statement.  */
7671
7672 static tree
7673 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7674 {
7675   tree statement;
7676
7677   if (if_p != NULL)
7678     *if_p = false;
7679
7680   /* Mark if () ; with a special NOP_EXPR.  */
7681   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7682     {
7683       cp_lexer_consume_token (parser->lexer);
7684       statement = add_stmt (build_empty_stmt ());
7685     }
7686   /* if a compound is opened, we simply parse the statement directly.  */
7687   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7688     statement = cp_parser_compound_statement (parser, NULL, false);
7689   /* If the token is not a `{', then we must take special action.  */
7690   else
7691     {
7692       /* Create a compound-statement.  */
7693       statement = begin_compound_stmt (0);
7694       /* Parse the dependent-statement.  */
7695       cp_parser_statement (parser, NULL_TREE, false, if_p);
7696       /* Finish the dummy compound-statement.  */
7697       finish_compound_stmt (statement);
7698     }
7699
7700   /* Return the statement.  */
7701   return statement;
7702 }
7703
7704 /* For some dependent statements (like `while (cond) statement'), we
7705    have already created a scope.  Therefore, even if the dependent
7706    statement is a compound-statement, we do not want to create another
7707    scope.  */
7708
7709 static void
7710 cp_parser_already_scoped_statement (cp_parser* parser)
7711 {
7712   /* If the token is a `{', then we must take special action.  */
7713   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7714     cp_parser_statement (parser, NULL_TREE, false, NULL);
7715   else
7716     {
7717       /* Avoid calling cp_parser_compound_statement, so that we
7718          don't create a new scope.  Do everything else by hand.  */
7719       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7720       cp_parser_statement_seq_opt (parser, NULL_TREE);
7721       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7722     }
7723 }
7724
7725 /* Declarations [gram.dcl.dcl] */
7726
7727 /* Parse an optional declaration-sequence.
7728
7729    declaration-seq:
7730      declaration
7731      declaration-seq declaration  */
7732
7733 static void
7734 cp_parser_declaration_seq_opt (cp_parser* parser)
7735 {
7736   while (true)
7737     {
7738       cp_token *token;
7739
7740       token = cp_lexer_peek_token (parser->lexer);
7741
7742       if (token->type == CPP_CLOSE_BRACE
7743           || token->type == CPP_EOF
7744           || token->type == CPP_PRAGMA_EOL)
7745         break;
7746
7747       if (token->type == CPP_SEMICOLON)
7748         {
7749           /* A declaration consisting of a single semicolon is
7750              invalid.  Allow it unless we're being pedantic.  */
7751           cp_lexer_consume_token (parser->lexer);
7752           if (!in_system_header)
7753             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7754           continue;
7755         }
7756
7757       /* If we're entering or exiting a region that's implicitly
7758          extern "C", modify the lang context appropriately.  */
7759       if (!parser->implicit_extern_c && token->implicit_extern_c)
7760         {
7761           push_lang_context (lang_name_c);
7762           parser->implicit_extern_c = true;
7763         }
7764       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7765         {
7766           pop_lang_context ();
7767           parser->implicit_extern_c = false;
7768         }
7769
7770       if (token->type == CPP_PRAGMA)
7771         {
7772           /* A top-level declaration can consist solely of a #pragma.
7773              A nested declaration cannot, so this is done here and not
7774              in cp_parser_declaration.  (A #pragma at block scope is
7775              handled in cp_parser_statement.)  */
7776           cp_parser_pragma (parser, pragma_external);
7777           continue;
7778         }
7779
7780       /* Parse the declaration itself.  */
7781       cp_parser_declaration (parser);
7782     }
7783 }
7784
7785 /* Parse a declaration.
7786
7787    declaration:
7788      block-declaration
7789      function-definition
7790      template-declaration
7791      explicit-instantiation
7792      explicit-specialization
7793      linkage-specification
7794      namespace-definition
7795
7796    GNU extension:
7797
7798    declaration:
7799       __extension__ declaration */
7800
7801 static void
7802 cp_parser_declaration (cp_parser* parser)
7803 {
7804   cp_token token1;
7805   cp_token token2;
7806   int saved_pedantic;
7807   void *p;
7808
7809   /* Check for the `__extension__' keyword.  */
7810   if (cp_parser_extension_opt (parser, &saved_pedantic))
7811     {
7812       /* Parse the qualified declaration.  */
7813       cp_parser_declaration (parser);
7814       /* Restore the PEDANTIC flag.  */
7815       pedantic = saved_pedantic;
7816
7817       return;
7818     }
7819
7820   /* Try to figure out what kind of declaration is present.  */
7821   token1 = *cp_lexer_peek_token (parser->lexer);
7822
7823   if (token1.type != CPP_EOF)
7824     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7825   else
7826     {
7827       token2.type = CPP_EOF;
7828       token2.keyword = RID_MAX;
7829     }
7830
7831   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7832   p = obstack_alloc (&declarator_obstack, 0);
7833
7834   /* If the next token is `extern' and the following token is a string
7835      literal, then we have a linkage specification.  */
7836   if (token1.keyword == RID_EXTERN
7837       && cp_parser_is_string_literal (&token2))
7838     cp_parser_linkage_specification (parser);
7839   /* If the next token is `template', then we have either a template
7840      declaration, an explicit instantiation, or an explicit
7841      specialization.  */
7842   else if (token1.keyword == RID_TEMPLATE)
7843     {
7844       /* `template <>' indicates a template specialization.  */
7845       if (token2.type == CPP_LESS
7846           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7847         cp_parser_explicit_specialization (parser);
7848       /* `template <' indicates a template declaration.  */
7849       else if (token2.type == CPP_LESS)
7850         cp_parser_template_declaration (parser, /*member_p=*/false);
7851       /* Anything else must be an explicit instantiation.  */
7852       else
7853         cp_parser_explicit_instantiation (parser);
7854     }
7855   /* If the next token is `export', then we have a template
7856      declaration.  */
7857   else if (token1.keyword == RID_EXPORT)
7858     cp_parser_template_declaration (parser, /*member_p=*/false);
7859   /* If the next token is `extern', 'static' or 'inline' and the one
7860      after that is `template', we have a GNU extended explicit
7861      instantiation directive.  */
7862   else if (cp_parser_allow_gnu_extensions_p (parser)
7863            && (token1.keyword == RID_EXTERN
7864                || token1.keyword == RID_STATIC
7865                || token1.keyword == RID_INLINE)
7866            && token2.keyword == RID_TEMPLATE)
7867     cp_parser_explicit_instantiation (parser);
7868   /* If the next token is `namespace', check for a named or unnamed
7869      namespace definition.  */
7870   else if (token1.keyword == RID_NAMESPACE
7871            && (/* A named namespace definition.  */
7872                (token2.type == CPP_NAME
7873                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7874                     != CPP_EQ))
7875                /* An unnamed namespace definition.  */
7876                || token2.type == CPP_OPEN_BRACE
7877                || token2.keyword == RID_ATTRIBUTE))
7878     cp_parser_namespace_definition (parser);
7879   /* An inline (associated) namespace definition.  */
7880   else if (token1.keyword == RID_INLINE
7881            && token2.keyword == RID_NAMESPACE)
7882     cp_parser_namespace_definition (parser);
7883   /* Objective-C++ declaration/definition.  */
7884   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7885     cp_parser_objc_declaration (parser);
7886   /* We must have either a block declaration or a function
7887      definition.  */
7888   else
7889     /* Try to parse a block-declaration, or a function-definition.  */
7890     cp_parser_block_declaration (parser, /*statement_p=*/false);
7891
7892   /* Free any declarators allocated.  */
7893   obstack_free (&declarator_obstack, p);
7894 }
7895
7896 /* Parse a block-declaration.
7897
7898    block-declaration:
7899      simple-declaration
7900      asm-definition
7901      namespace-alias-definition
7902      using-declaration
7903      using-directive
7904
7905    GNU Extension:
7906
7907    block-declaration:
7908      __extension__ block-declaration
7909
7910    C++0x Extension:
7911
7912    block-declaration:
7913      static_assert-declaration
7914
7915    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7916    part of a declaration-statement.  */
7917
7918 static void
7919 cp_parser_block_declaration (cp_parser *parser,
7920                              bool      statement_p)
7921 {
7922   cp_token *token1;
7923   int saved_pedantic;
7924
7925   /* Check for the `__extension__' keyword.  */
7926   if (cp_parser_extension_opt (parser, &saved_pedantic))
7927     {
7928       /* Parse the qualified declaration.  */
7929       cp_parser_block_declaration (parser, statement_p);
7930       /* Restore the PEDANTIC flag.  */
7931       pedantic = saved_pedantic;
7932
7933       return;
7934     }
7935
7936   /* Peek at the next token to figure out which kind of declaration is
7937      present.  */
7938   token1 = cp_lexer_peek_token (parser->lexer);
7939
7940   /* If the next keyword is `asm', we have an asm-definition.  */
7941   if (token1->keyword == RID_ASM)
7942     {
7943       if (statement_p)
7944         cp_parser_commit_to_tentative_parse (parser);
7945       cp_parser_asm_definition (parser);
7946     }
7947   /* If the next keyword is `namespace', we have a
7948      namespace-alias-definition.  */
7949   else if (token1->keyword == RID_NAMESPACE)
7950     cp_parser_namespace_alias_definition (parser);
7951   /* If the next keyword is `using', we have either a
7952      using-declaration or a using-directive.  */
7953   else if (token1->keyword == RID_USING)
7954     {
7955       cp_token *token2;
7956
7957       if (statement_p)
7958         cp_parser_commit_to_tentative_parse (parser);
7959       /* If the token after `using' is `namespace', then we have a
7960          using-directive.  */
7961       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7962       if (token2->keyword == RID_NAMESPACE)
7963         cp_parser_using_directive (parser);
7964       /* Otherwise, it's a using-declaration.  */
7965       else
7966         cp_parser_using_declaration (parser,
7967                                      /*access_declaration_p=*/false);
7968     }
7969   /* If the next keyword is `__label__' we have a misplaced label
7970      declaration.  */
7971   else if (token1->keyword == RID_LABEL)
7972     {
7973       cp_lexer_consume_token (parser->lexer);
7974       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7975       cp_parser_skip_to_end_of_statement (parser);
7976       /* If the next token is now a `;', consume it.  */
7977       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7978         cp_lexer_consume_token (parser->lexer);
7979     }
7980   /* If the next token is `static_assert' we have a static assertion.  */
7981   else if (token1->keyword == RID_STATIC_ASSERT)
7982     cp_parser_static_assert (parser, /*member_p=*/false);
7983   /* Anything else must be a simple-declaration.  */
7984   else
7985     cp_parser_simple_declaration (parser, !statement_p);
7986 }
7987
7988 /* Parse a simple-declaration.
7989
7990    simple-declaration:
7991      decl-specifier-seq [opt] init-declarator-list [opt] ;
7992
7993    init-declarator-list:
7994      init-declarator
7995      init-declarator-list , init-declarator
7996
7997    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7998    function-definition as a simple-declaration.  */
7999
8000 static void
8001 cp_parser_simple_declaration (cp_parser* parser,
8002                               bool function_definition_allowed_p)
8003 {
8004   cp_decl_specifier_seq decl_specifiers;
8005   int declares_class_or_enum;
8006   bool saw_declarator;
8007
8008   /* Defer access checks until we know what is being declared; the
8009      checks for names appearing in the decl-specifier-seq should be
8010      done as if we were in the scope of the thing being declared.  */
8011   push_deferring_access_checks (dk_deferred);
8012
8013   /* Parse the decl-specifier-seq.  We have to keep track of whether
8014      or not the decl-specifier-seq declares a named class or
8015      enumeration type, since that is the only case in which the
8016      init-declarator-list is allowed to be empty.
8017
8018      [dcl.dcl]
8019
8020      In a simple-declaration, the optional init-declarator-list can be
8021      omitted only when declaring a class or enumeration, that is when
8022      the decl-specifier-seq contains either a class-specifier, an
8023      elaborated-type-specifier, or an enum-specifier.  */
8024   cp_parser_decl_specifier_seq (parser,
8025                                 CP_PARSER_FLAGS_OPTIONAL,
8026                                 &decl_specifiers,
8027                                 &declares_class_or_enum);
8028   /* We no longer need to defer access checks.  */
8029   stop_deferring_access_checks ();
8030
8031   /* In a block scope, a valid declaration must always have a
8032      decl-specifier-seq.  By not trying to parse declarators, we can
8033      resolve the declaration/expression ambiguity more quickly.  */
8034   if (!function_definition_allowed_p
8035       && !decl_specifiers.any_specifiers_p)
8036     {
8037       cp_parser_error (parser, "expected declaration");
8038       goto done;
8039     }
8040
8041   /* If the next two tokens are both identifiers, the code is
8042      erroneous. The usual cause of this situation is code like:
8043
8044        T t;
8045
8046      where "T" should name a type -- but does not.  */
8047   if (!decl_specifiers.type
8048       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8049     {
8050       /* If parsing tentatively, we should commit; we really are
8051          looking at a declaration.  */
8052       cp_parser_commit_to_tentative_parse (parser);
8053       /* Give up.  */
8054       goto done;
8055     }
8056
8057   /* If we have seen at least one decl-specifier, and the next token
8058      is not a parenthesis, then we must be looking at a declaration.
8059      (After "int (" we might be looking at a functional cast.)  */
8060   if (decl_specifiers.any_specifiers_p
8061       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8062       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8063     cp_parser_commit_to_tentative_parse (parser);
8064
8065   /* Keep going until we hit the `;' at the end of the simple
8066      declaration.  */
8067   saw_declarator = false;
8068   while (cp_lexer_next_token_is_not (parser->lexer,
8069                                      CPP_SEMICOLON))
8070     {
8071       cp_token *token;
8072       bool function_definition_p;
8073       tree decl;
8074
8075       if (saw_declarator)
8076         {
8077           /* If we are processing next declarator, coma is expected */
8078           token = cp_lexer_peek_token (parser->lexer);
8079           gcc_assert (token->type == CPP_COMMA);
8080           cp_lexer_consume_token (parser->lexer);
8081         }
8082       else
8083         saw_declarator = true;
8084
8085       /* Parse the init-declarator.  */
8086       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8087                                         /*checks=*/NULL,
8088                                         function_definition_allowed_p,
8089                                         /*member_p=*/false,
8090                                         declares_class_or_enum,
8091                                         &function_definition_p);
8092       /* If an error occurred while parsing tentatively, exit quickly.
8093          (That usually happens when in the body of a function; each
8094          statement is treated as a declaration-statement until proven
8095          otherwise.)  */
8096       if (cp_parser_error_occurred (parser))
8097         goto done;
8098       /* Handle function definitions specially.  */
8099       if (function_definition_p)
8100         {
8101           /* If the next token is a `,', then we are probably
8102              processing something like:
8103
8104                void f() {}, *p;
8105
8106              which is erroneous.  */
8107           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8108             {
8109               cp_token *token = cp_lexer_peek_token (parser->lexer);
8110               error ("%Hmixing declarations and function-definitions is forbidden",
8111                      &token->location);
8112             }
8113           /* Otherwise, we're done with the list of declarators.  */
8114           else
8115             {
8116               pop_deferring_access_checks ();
8117               return;
8118             }
8119         }
8120       /* The next token should be either a `,' or a `;'.  */
8121       token = cp_lexer_peek_token (parser->lexer);
8122       /* If it's a `,', there are more declarators to come.  */
8123       if (token->type == CPP_COMMA)
8124         /* will be consumed next time around */;
8125       /* If it's a `;', we are done.  */
8126       else if (token->type == CPP_SEMICOLON)
8127         break;
8128       /* Anything else is an error.  */
8129       else
8130         {
8131           /* If we have already issued an error message we don't need
8132              to issue another one.  */
8133           if (decl != error_mark_node
8134               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8135             cp_parser_error (parser, "expected %<,%> or %<;%>");
8136           /* Skip tokens until we reach the end of the statement.  */
8137           cp_parser_skip_to_end_of_statement (parser);
8138           /* If the next token is now a `;', consume it.  */
8139           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8140             cp_lexer_consume_token (parser->lexer);
8141           goto done;
8142         }
8143       /* After the first time around, a function-definition is not
8144          allowed -- even if it was OK at first.  For example:
8145
8146            int i, f() {}
8147
8148          is not valid.  */
8149       function_definition_allowed_p = false;
8150     }
8151
8152   /* Issue an error message if no declarators are present, and the
8153      decl-specifier-seq does not itself declare a class or
8154      enumeration.  */
8155   if (!saw_declarator)
8156     {
8157       if (cp_parser_declares_only_class_p (parser))
8158         shadow_tag (&decl_specifiers);
8159       /* Perform any deferred access checks.  */
8160       perform_deferred_access_checks ();
8161     }
8162
8163   /* Consume the `;'.  */
8164   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8165
8166  done:
8167   pop_deferring_access_checks ();
8168 }
8169
8170 /* Parse a decl-specifier-seq.
8171
8172    decl-specifier-seq:
8173      decl-specifier-seq [opt] decl-specifier
8174
8175    decl-specifier:
8176      storage-class-specifier
8177      type-specifier
8178      function-specifier
8179      friend
8180      typedef
8181
8182    GNU Extension:
8183
8184    decl-specifier:
8185      attributes
8186
8187    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8188
8189    The parser flags FLAGS is used to control type-specifier parsing.
8190
8191    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8192    flags:
8193
8194      1: one of the decl-specifiers is an elaborated-type-specifier
8195         (i.e., a type declaration)
8196      2: one of the decl-specifiers is an enum-specifier or a
8197         class-specifier (i.e., a type definition)
8198
8199    */
8200
8201 static void
8202 cp_parser_decl_specifier_seq (cp_parser* parser,
8203                               cp_parser_flags flags,
8204                               cp_decl_specifier_seq *decl_specs,
8205                               int* declares_class_or_enum)
8206 {
8207   bool constructor_possible_p = !parser->in_declarator_p;
8208   cp_token *start_token = NULL;
8209
8210   /* Clear DECL_SPECS.  */
8211   clear_decl_specs (decl_specs);
8212
8213   /* Assume no class or enumeration type is declared.  */
8214   *declares_class_or_enum = 0;
8215
8216   /* Keep reading specifiers until there are no more to read.  */
8217   while (true)
8218     {
8219       bool constructor_p;
8220       bool found_decl_spec;
8221       cp_token *token;
8222
8223       /* Peek at the next token.  */
8224       token = cp_lexer_peek_token (parser->lexer);
8225
8226       /* Save the first token of the decl spec list for error
8227          reporting.  */
8228       if (!start_token)
8229         start_token = token;
8230       /* Handle attributes.  */
8231       if (token->keyword == RID_ATTRIBUTE)
8232         {
8233           /* Parse the attributes.  */
8234           decl_specs->attributes
8235             = chainon (decl_specs->attributes,
8236                        cp_parser_attributes_opt (parser));
8237           continue;
8238         }
8239       /* Assume we will find a decl-specifier keyword.  */
8240       found_decl_spec = true;
8241       /* If the next token is an appropriate keyword, we can simply
8242          add it to the list.  */
8243       switch (token->keyword)
8244         {
8245           /* decl-specifier:
8246                friend  */
8247         case RID_FRIEND:
8248           if (!at_class_scope_p ())
8249             {
8250               error ("%H%<friend%> used outside of class", &token->location);
8251               cp_lexer_purge_token (parser->lexer);
8252             }
8253           else
8254             {
8255               ++decl_specs->specs[(int) ds_friend];
8256               /* Consume the token.  */
8257               cp_lexer_consume_token (parser->lexer);
8258             }
8259           break;
8260
8261           /* function-specifier:
8262                inline
8263                virtual
8264                explicit  */
8265         case RID_INLINE:
8266         case RID_VIRTUAL:
8267         case RID_EXPLICIT:
8268           cp_parser_function_specifier_opt (parser, decl_specs);
8269           break;
8270
8271           /* decl-specifier:
8272                typedef  */
8273         case RID_TYPEDEF:
8274           ++decl_specs->specs[(int) ds_typedef];
8275           /* Consume the token.  */
8276           cp_lexer_consume_token (parser->lexer);
8277           /* A constructor declarator cannot appear in a typedef.  */
8278           constructor_possible_p = false;
8279           /* The "typedef" keyword can only occur in a declaration; we
8280              may as well commit at this point.  */
8281           cp_parser_commit_to_tentative_parse (parser);
8282
8283           if (decl_specs->storage_class != sc_none)
8284             decl_specs->conflicting_specifiers_p = true;
8285           break;
8286
8287           /* storage-class-specifier:
8288                auto
8289                register
8290                static
8291                extern
8292                mutable
8293
8294              GNU Extension:
8295                thread  */
8296         case RID_AUTO:
8297           /* Consume the token.  */
8298           cp_lexer_consume_token (parser->lexer);
8299
8300           if (cxx_dialect == cxx98) 
8301             {
8302               /* Complain about `auto' as a storage specifier, if
8303                  we're complaining about C++0x compatibility.  */
8304               warning 
8305                 (OPT_Wc__0x_compat, 
8306                  "%H%<auto%> will change meaning in C++0x; please remove it",
8307                  &token->location);
8308
8309               /* Set the storage class anyway.  */
8310               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8311                                            token->location);
8312             }
8313           else 
8314             /* We do not yet support the use of `auto' as a
8315                type-specifier.  */
8316             error ("%HC++0x %<auto%> specifier not supported", &token->location);
8317           break;
8318
8319         case RID_REGISTER:
8320         case RID_STATIC:
8321         case RID_EXTERN:
8322         case RID_MUTABLE:
8323           /* Consume the token.  */
8324           cp_lexer_consume_token (parser->lexer);
8325           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8326                                        token->location);
8327           break;
8328         case RID_THREAD:
8329           /* Consume the token.  */
8330           cp_lexer_consume_token (parser->lexer);
8331           ++decl_specs->specs[(int) ds_thread];
8332           break;
8333
8334         default:
8335           /* We did not yet find a decl-specifier yet.  */
8336           found_decl_spec = false;
8337           break;
8338         }
8339
8340       /* Constructors are a special case.  The `S' in `S()' is not a
8341          decl-specifier; it is the beginning of the declarator.  */
8342       constructor_p
8343         = (!found_decl_spec
8344            && constructor_possible_p
8345            && (cp_parser_constructor_declarator_p
8346                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8347
8348       /* If we don't have a DECL_SPEC yet, then we must be looking at
8349          a type-specifier.  */
8350       if (!found_decl_spec && !constructor_p)
8351         {
8352           int decl_spec_declares_class_or_enum;
8353           bool is_cv_qualifier;
8354           tree type_spec;
8355
8356           type_spec
8357             = cp_parser_type_specifier (parser, flags,
8358                                         decl_specs,
8359                                         /*is_declaration=*/true,
8360                                         &decl_spec_declares_class_or_enum,
8361                                         &is_cv_qualifier);
8362           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8363
8364           /* If this type-specifier referenced a user-defined type
8365              (a typedef, class-name, etc.), then we can't allow any
8366              more such type-specifiers henceforth.
8367
8368              [dcl.spec]
8369
8370              The longest sequence of decl-specifiers that could
8371              possibly be a type name is taken as the
8372              decl-specifier-seq of a declaration.  The sequence shall
8373              be self-consistent as described below.
8374
8375              [dcl.type]
8376
8377              As a general rule, at most one type-specifier is allowed
8378              in the complete decl-specifier-seq of a declaration.  The
8379              only exceptions are the following:
8380
8381              -- const or volatile can be combined with any other
8382                 type-specifier.
8383
8384              -- signed or unsigned can be combined with char, long,
8385                 short, or int.
8386
8387              -- ..
8388
8389              Example:
8390
8391                typedef char* Pc;
8392                void g (const int Pc);
8393
8394              Here, Pc is *not* part of the decl-specifier seq; it's
8395              the declarator.  Therefore, once we see a type-specifier
8396              (other than a cv-qualifier), we forbid any additional
8397              user-defined types.  We *do* still allow things like `int
8398              int' to be considered a decl-specifier-seq, and issue the
8399              error message later.  */
8400           if (type_spec && !is_cv_qualifier)
8401             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8402           /* A constructor declarator cannot follow a type-specifier.  */
8403           if (type_spec)
8404             {
8405               constructor_possible_p = false;
8406               found_decl_spec = true;
8407             }
8408         }
8409
8410       /* If we still do not have a DECL_SPEC, then there are no more
8411          decl-specifiers.  */
8412       if (!found_decl_spec)
8413         break;
8414
8415       decl_specs->any_specifiers_p = true;
8416       /* After we see one decl-specifier, further decl-specifiers are
8417          always optional.  */
8418       flags |= CP_PARSER_FLAGS_OPTIONAL;
8419     }
8420
8421   cp_parser_check_decl_spec (decl_specs, start_token->location);
8422
8423   /* Don't allow a friend specifier with a class definition.  */
8424   if (decl_specs->specs[(int) ds_friend] != 0
8425       && (*declares_class_or_enum & 2))
8426     error ("%Hclass definition may not be declared a friend",
8427             &start_token->location);
8428 }
8429
8430 /* Parse an (optional) storage-class-specifier.
8431
8432    storage-class-specifier:
8433      auto
8434      register
8435      static
8436      extern
8437      mutable
8438
8439    GNU Extension:
8440
8441    storage-class-specifier:
8442      thread
8443
8444    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8445
8446 static tree
8447 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8448 {
8449   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8450     {
8451     case RID_AUTO:
8452       if (cxx_dialect != cxx98)
8453         return NULL_TREE;
8454       /* Fall through for C++98.  */
8455
8456     case RID_REGISTER:
8457     case RID_STATIC:
8458     case RID_EXTERN:
8459     case RID_MUTABLE:
8460     case RID_THREAD:
8461       /* Consume the token.  */
8462       return cp_lexer_consume_token (parser->lexer)->u.value;
8463
8464     default:
8465       return NULL_TREE;
8466     }
8467 }
8468
8469 /* Parse an (optional) function-specifier.
8470
8471    function-specifier:
8472      inline
8473      virtual
8474      explicit
8475
8476    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8477    Updates DECL_SPECS, if it is non-NULL.  */
8478
8479 static tree
8480 cp_parser_function_specifier_opt (cp_parser* parser,
8481                                   cp_decl_specifier_seq *decl_specs)
8482 {
8483   cp_token *token = cp_lexer_peek_token (parser->lexer);
8484   switch (token->keyword)
8485     {
8486     case RID_INLINE:
8487       if (decl_specs)
8488         ++decl_specs->specs[(int) ds_inline];
8489       break;
8490
8491     case RID_VIRTUAL:
8492       /* 14.5.2.3 [temp.mem]
8493
8494          A member function template shall not be virtual.  */
8495       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8496         error ("%Htemplates may not be %<virtual%>", &token->location);
8497       else if (decl_specs)
8498         ++decl_specs->specs[(int) ds_virtual];
8499       break;
8500
8501     case RID_EXPLICIT:
8502       if (decl_specs)
8503         ++decl_specs->specs[(int) ds_explicit];
8504       break;
8505
8506     default:
8507       return NULL_TREE;
8508     }
8509
8510   /* Consume the token.  */
8511   return cp_lexer_consume_token (parser->lexer)->u.value;
8512 }
8513
8514 /* Parse a linkage-specification.
8515
8516    linkage-specification:
8517      extern string-literal { declaration-seq [opt] }
8518      extern string-literal declaration  */
8519
8520 static void
8521 cp_parser_linkage_specification (cp_parser* parser)
8522 {
8523   tree linkage;
8524
8525   /* Look for the `extern' keyword.  */
8526   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8527
8528   /* Look for the string-literal.  */
8529   linkage = cp_parser_string_literal (parser, false, false);
8530
8531   /* Transform the literal into an identifier.  If the literal is a
8532      wide-character string, or contains embedded NULs, then we can't
8533      handle it as the user wants.  */
8534   if (strlen (TREE_STRING_POINTER (linkage))
8535       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8536     {
8537       cp_parser_error (parser, "invalid linkage-specification");
8538       /* Assume C++ linkage.  */
8539       linkage = lang_name_cplusplus;
8540     }
8541   else
8542     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8543
8544   /* We're now using the new linkage.  */
8545   push_lang_context (linkage);
8546
8547   /* If the next token is a `{', then we're using the first
8548      production.  */
8549   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8550     {
8551       /* Consume the `{' token.  */
8552       cp_lexer_consume_token (parser->lexer);
8553       /* Parse the declarations.  */
8554       cp_parser_declaration_seq_opt (parser);
8555       /* Look for the closing `}'.  */
8556       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8557     }
8558   /* Otherwise, there's just one declaration.  */
8559   else
8560     {
8561       bool saved_in_unbraced_linkage_specification_p;
8562
8563       saved_in_unbraced_linkage_specification_p
8564         = parser->in_unbraced_linkage_specification_p;
8565       parser->in_unbraced_linkage_specification_p = true;
8566       cp_parser_declaration (parser);
8567       parser->in_unbraced_linkage_specification_p
8568         = saved_in_unbraced_linkage_specification_p;
8569     }
8570
8571   /* We're done with the linkage-specification.  */
8572   pop_lang_context ();
8573 }
8574
8575 /* Parse a static_assert-declaration.
8576
8577    static_assert-declaration:
8578      static_assert ( constant-expression , string-literal ) ; 
8579
8580    If MEMBER_P, this static_assert is a class member.  */
8581
8582 static void 
8583 cp_parser_static_assert(cp_parser *parser, bool member_p)
8584 {
8585   tree condition;
8586   tree message;
8587   cp_token *token;
8588   location_t saved_loc;
8589
8590   /* Peek at the `static_assert' token so we can keep track of exactly
8591      where the static assertion started.  */
8592   token = cp_lexer_peek_token (parser->lexer);
8593   saved_loc = token->location;
8594
8595   /* Look for the `static_assert' keyword.  */
8596   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8597                                   "%<static_assert%>"))
8598     return;
8599
8600   /*  We know we are in a static assertion; commit to any tentative
8601       parse.  */
8602   if (cp_parser_parsing_tentatively (parser))
8603     cp_parser_commit_to_tentative_parse (parser);
8604
8605   /* Parse the `(' starting the static assertion condition.  */
8606   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8607
8608   /* Parse the constant-expression.  */
8609   condition = 
8610     cp_parser_constant_expression (parser,
8611                                    /*allow_non_constant_p=*/false,
8612                                    /*non_constant_p=*/NULL);
8613
8614   /* Parse the separating `,'.  */
8615   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8616
8617   /* Parse the string-literal message.  */
8618   message = cp_parser_string_literal (parser, 
8619                                       /*translate=*/false,
8620                                       /*wide_ok=*/true);
8621
8622   /* A `)' completes the static assertion.  */
8623   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8624     cp_parser_skip_to_closing_parenthesis (parser, 
8625                                            /*recovering=*/true, 
8626                                            /*or_comma=*/false,
8627                                            /*consume_paren=*/true);
8628
8629   /* A semicolon terminates the declaration.  */
8630   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8631
8632   /* Complete the static assertion, which may mean either processing 
8633      the static assert now or saving it for template instantiation.  */
8634   finish_static_assert (condition, message, saved_loc, member_p);
8635 }
8636
8637 /* Parse a `decltype' type. Returns the type. 
8638
8639    simple-type-specifier:
8640      decltype ( expression )  */
8641
8642 static tree
8643 cp_parser_decltype (cp_parser *parser)
8644 {
8645   tree expr;
8646   bool id_expression_or_member_access_p = false;
8647   const char *saved_message;
8648   bool saved_integral_constant_expression_p;
8649   bool saved_non_integral_constant_expression_p;
8650   cp_token *id_expr_start_token;
8651
8652   /* Look for the `decltype' token.  */
8653   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8654     return error_mark_node;
8655
8656   /* Types cannot be defined in a `decltype' expression.  Save away the
8657      old message.  */
8658   saved_message = parser->type_definition_forbidden_message;
8659
8660   /* And create the new one.  */
8661   parser->type_definition_forbidden_message
8662     = "types may not be defined in %<decltype%> expressions";
8663
8664   /* The restrictions on constant-expressions do not apply inside
8665      decltype expressions.  */
8666   saved_integral_constant_expression_p
8667     = parser->integral_constant_expression_p;
8668   saved_non_integral_constant_expression_p
8669     = parser->non_integral_constant_expression_p;
8670   parser->integral_constant_expression_p = false;
8671
8672   /* Do not actually evaluate the expression.  */
8673   ++skip_evaluation;
8674
8675   /* Parse the opening `('.  */
8676   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8677     return error_mark_node;
8678   
8679   /* First, try parsing an id-expression.  */
8680   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8681   cp_parser_parse_tentatively (parser);
8682   expr = cp_parser_id_expression (parser,
8683                                   /*template_keyword_p=*/false,
8684                                   /*check_dependency_p=*/true,
8685                                   /*template_p=*/NULL,
8686                                   /*declarator_p=*/false,
8687                                   /*optional_p=*/false);
8688
8689   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8690     {
8691       bool non_integral_constant_expression_p = false;
8692       tree id_expression = expr;
8693       cp_id_kind idk;
8694       const char *error_msg;
8695
8696       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8697         /* Lookup the name we got back from the id-expression.  */
8698         expr = cp_parser_lookup_name (parser, expr,
8699                                       none_type,
8700                                       /*is_template=*/false,
8701                                       /*is_namespace=*/false,
8702                                       /*check_dependency=*/true,
8703                                       /*ambiguous_decls=*/NULL,
8704                                       id_expr_start_token->location);
8705
8706       if (expr
8707           && expr != error_mark_node
8708           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8709           && TREE_CODE (expr) != TYPE_DECL
8710           && (TREE_CODE (expr) != BIT_NOT_EXPR
8711               || !TYPE_P (TREE_OPERAND (expr, 0)))
8712           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8713         {
8714           /* Complete lookup of the id-expression.  */
8715           expr = (finish_id_expression
8716                   (id_expression, expr, parser->scope, &idk,
8717                    /*integral_constant_expression_p=*/false,
8718                    /*allow_non_integral_constant_expression_p=*/true,
8719                    &non_integral_constant_expression_p,
8720                    /*template_p=*/false,
8721                    /*done=*/true,
8722                    /*address_p=*/false,
8723                    /*template_arg_p=*/false,
8724                    &error_msg,
8725                    id_expr_start_token->location));
8726
8727           if (expr == error_mark_node)
8728             /* We found an id-expression, but it was something that we
8729                should not have found. This is an error, not something
8730                we can recover from, so note that we found an
8731                id-expression and we'll recover as gracefully as
8732                possible.  */
8733             id_expression_or_member_access_p = true;
8734         }
8735
8736       if (expr 
8737           && expr != error_mark_node
8738           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8739         /* We have an id-expression.  */
8740         id_expression_or_member_access_p = true;
8741     }
8742
8743   if (!id_expression_or_member_access_p)
8744     {
8745       /* Abort the id-expression parse.  */
8746       cp_parser_abort_tentative_parse (parser);
8747
8748       /* Parsing tentatively, again.  */
8749       cp_parser_parse_tentatively (parser);
8750
8751       /* Parse a class member access.  */
8752       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8753                                            /*cast_p=*/false,
8754                                            /*member_access_only_p=*/true);
8755
8756       if (expr 
8757           && expr != error_mark_node
8758           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8759         /* We have an id-expression.  */
8760         id_expression_or_member_access_p = true;
8761     }
8762
8763   if (id_expression_or_member_access_p)
8764     /* We have parsed the complete id-expression or member access.  */
8765     cp_parser_parse_definitely (parser);
8766   else
8767     {
8768       /* Abort our attempt to parse an id-expression or member access
8769          expression.  */
8770       cp_parser_abort_tentative_parse (parser);
8771
8772       /* Parse a full expression.  */
8773       expr = cp_parser_expression (parser, /*cast_p=*/false);
8774     }
8775
8776   /* Go back to evaluating expressions.  */
8777   --skip_evaluation;
8778
8779   /* Restore the old message and the integral constant expression
8780      flags.  */
8781   parser->type_definition_forbidden_message = saved_message;
8782   parser->integral_constant_expression_p
8783     = saved_integral_constant_expression_p;
8784   parser->non_integral_constant_expression_p
8785     = saved_non_integral_constant_expression_p;
8786
8787   if (expr == error_mark_node)
8788     {
8789       /* Skip everything up to the closing `)'.  */
8790       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8791                                              /*consume_paren=*/true);
8792       return error_mark_node;
8793     }
8794   
8795   /* Parse to the closing `)'.  */
8796   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8797     {
8798       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8799                                              /*consume_paren=*/true);
8800       return error_mark_node;
8801     }
8802
8803   return finish_decltype_type (expr, id_expression_or_member_access_p);
8804 }
8805
8806 /* Special member functions [gram.special] */
8807
8808 /* Parse a conversion-function-id.
8809
8810    conversion-function-id:
8811      operator conversion-type-id
8812
8813    Returns an IDENTIFIER_NODE representing the operator.  */
8814
8815 static tree
8816 cp_parser_conversion_function_id (cp_parser* parser)
8817 {
8818   tree type;
8819   tree saved_scope;
8820   tree saved_qualifying_scope;
8821   tree saved_object_scope;
8822   tree pushed_scope = NULL_TREE;
8823
8824   /* Look for the `operator' token.  */
8825   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8826     return error_mark_node;
8827   /* When we parse the conversion-type-id, the current scope will be
8828      reset.  However, we need that information in able to look up the
8829      conversion function later, so we save it here.  */
8830   saved_scope = parser->scope;
8831   saved_qualifying_scope = parser->qualifying_scope;
8832   saved_object_scope = parser->object_scope;
8833   /* We must enter the scope of the class so that the names of
8834      entities declared within the class are available in the
8835      conversion-type-id.  For example, consider:
8836
8837        struct S {
8838          typedef int I;
8839          operator I();
8840        };
8841
8842        S::operator I() { ... }
8843
8844      In order to see that `I' is a type-name in the definition, we
8845      must be in the scope of `S'.  */
8846   if (saved_scope)
8847     pushed_scope = push_scope (saved_scope);
8848   /* Parse the conversion-type-id.  */
8849   type = cp_parser_conversion_type_id (parser);
8850   /* Leave the scope of the class, if any.  */
8851   if (pushed_scope)
8852     pop_scope (pushed_scope);
8853   /* Restore the saved scope.  */
8854   parser->scope = saved_scope;
8855   parser->qualifying_scope = saved_qualifying_scope;
8856   parser->object_scope = saved_object_scope;
8857   /* If the TYPE is invalid, indicate failure.  */
8858   if (type == error_mark_node)
8859     return error_mark_node;
8860   return mangle_conv_op_name_for_type (type);
8861 }
8862
8863 /* Parse a conversion-type-id:
8864
8865    conversion-type-id:
8866      type-specifier-seq conversion-declarator [opt]
8867
8868    Returns the TYPE specified.  */
8869
8870 static tree
8871 cp_parser_conversion_type_id (cp_parser* parser)
8872 {
8873   tree attributes;
8874   cp_decl_specifier_seq type_specifiers;
8875   cp_declarator *declarator;
8876   tree type_specified;
8877
8878   /* Parse the attributes.  */
8879   attributes = cp_parser_attributes_opt (parser);
8880   /* Parse the type-specifiers.  */
8881   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8882                                 &type_specifiers);
8883   /* If that didn't work, stop.  */
8884   if (type_specifiers.type == error_mark_node)
8885     return error_mark_node;
8886   /* Parse the conversion-declarator.  */
8887   declarator = cp_parser_conversion_declarator_opt (parser);
8888
8889   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8890                                     /*initialized=*/0, &attributes);
8891   if (attributes)
8892     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8893   return type_specified;
8894 }
8895
8896 /* Parse an (optional) conversion-declarator.
8897
8898    conversion-declarator:
8899      ptr-operator conversion-declarator [opt]
8900
8901    */
8902
8903 static cp_declarator *
8904 cp_parser_conversion_declarator_opt (cp_parser* parser)
8905 {
8906   enum tree_code code;
8907   tree class_type;
8908   cp_cv_quals cv_quals;
8909
8910   /* We don't know if there's a ptr-operator next, or not.  */
8911   cp_parser_parse_tentatively (parser);
8912   /* Try the ptr-operator.  */
8913   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8914   /* If it worked, look for more conversion-declarators.  */
8915   if (cp_parser_parse_definitely (parser))
8916     {
8917       cp_declarator *declarator;
8918
8919       /* Parse another optional declarator.  */
8920       declarator = cp_parser_conversion_declarator_opt (parser);
8921
8922       return cp_parser_make_indirect_declarator
8923         (code, class_type, cv_quals, declarator);
8924    }
8925
8926   return NULL;
8927 }
8928
8929 /* Parse an (optional) ctor-initializer.
8930
8931    ctor-initializer:
8932      : mem-initializer-list
8933
8934    Returns TRUE iff the ctor-initializer was actually present.  */
8935
8936 static bool
8937 cp_parser_ctor_initializer_opt (cp_parser* parser)
8938 {
8939   /* If the next token is not a `:', then there is no
8940      ctor-initializer.  */
8941   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8942     {
8943       /* Do default initialization of any bases and members.  */
8944       if (DECL_CONSTRUCTOR_P (current_function_decl))
8945         finish_mem_initializers (NULL_TREE);
8946
8947       return false;
8948     }
8949
8950   /* Consume the `:' token.  */
8951   cp_lexer_consume_token (parser->lexer);
8952   /* And the mem-initializer-list.  */
8953   cp_parser_mem_initializer_list (parser);
8954
8955   return true;
8956 }
8957
8958 /* Parse a mem-initializer-list.
8959
8960    mem-initializer-list:
8961      mem-initializer ... [opt]
8962      mem-initializer ... [opt] , mem-initializer-list  */
8963
8964 static void
8965 cp_parser_mem_initializer_list (cp_parser* parser)
8966 {
8967   tree mem_initializer_list = NULL_TREE;
8968   cp_token *token = cp_lexer_peek_token (parser->lexer);
8969
8970   /* Let the semantic analysis code know that we are starting the
8971      mem-initializer-list.  */
8972   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8973     error ("%Honly constructors take base initializers",
8974            &token->location);
8975
8976   /* Loop through the list.  */
8977   while (true)
8978     {
8979       tree mem_initializer;
8980
8981       token = cp_lexer_peek_token (parser->lexer);
8982       /* Parse the mem-initializer.  */
8983       mem_initializer = cp_parser_mem_initializer (parser);
8984       /* If the next token is a `...', we're expanding member initializers. */
8985       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8986         {
8987           /* Consume the `...'. */
8988           cp_lexer_consume_token (parser->lexer);
8989
8990           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8991              can be expanded but members cannot. */
8992           if (mem_initializer != error_mark_node
8993               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8994             {
8995               error ("%Hcannot expand initializer for member %<%D%>",
8996                      &token->location, TREE_PURPOSE (mem_initializer));
8997               mem_initializer = error_mark_node;
8998             }
8999
9000           /* Construct the pack expansion type. */
9001           if (mem_initializer != error_mark_node)
9002             mem_initializer = make_pack_expansion (mem_initializer);
9003         }
9004       /* Add it to the list, unless it was erroneous.  */
9005       if (mem_initializer != error_mark_node)
9006         {
9007           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9008           mem_initializer_list = mem_initializer;
9009         }
9010       /* If the next token is not a `,', we're done.  */
9011       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9012         break;
9013       /* Consume the `,' token.  */
9014       cp_lexer_consume_token (parser->lexer);
9015     }
9016
9017   /* Perform semantic analysis.  */
9018   if (DECL_CONSTRUCTOR_P (current_function_decl))
9019     finish_mem_initializers (mem_initializer_list);
9020 }
9021
9022 /* Parse a mem-initializer.
9023
9024    mem-initializer:
9025      mem-initializer-id ( expression-list [opt] )
9026      mem-initializer-id braced-init-list
9027
9028    GNU extension:
9029
9030    mem-initializer:
9031      ( expression-list [opt] )
9032
9033    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9034    class) or FIELD_DECL (for a non-static data member) to initialize;
9035    the TREE_VALUE is the expression-list.  An empty initialization
9036    list is represented by void_list_node.  */
9037
9038 static tree
9039 cp_parser_mem_initializer (cp_parser* parser)
9040 {
9041   tree mem_initializer_id;
9042   tree expression_list;
9043   tree member;
9044   cp_token *token = cp_lexer_peek_token (parser->lexer);
9045
9046   /* Find out what is being initialized.  */
9047   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9048     {
9049       permerror (token->location,
9050                  "anachronistic old-style base class initializer");
9051       mem_initializer_id = NULL_TREE;
9052     }
9053   else
9054     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9055   member = expand_member_init (mem_initializer_id);
9056   if (member && !DECL_P (member))
9057     in_base_initializer = 1;
9058
9059   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9060     {
9061       bool expr_non_constant_p;
9062       maybe_warn_cpp0x ("extended initializer lists");
9063       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9064       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9065       expression_list = build_tree_list (NULL_TREE, expression_list);
9066     }
9067   else
9068     expression_list
9069       = cp_parser_parenthesized_expression_list (parser, false,
9070                                                  /*cast_p=*/false,
9071                                                  /*allow_expansion_p=*/true,
9072                                                  /*non_constant_p=*/NULL);
9073   if (expression_list == error_mark_node)
9074     return error_mark_node;
9075   if (!expression_list)
9076     expression_list = void_type_node;
9077
9078   in_base_initializer = 0;
9079
9080   return member ? build_tree_list (member, expression_list) : error_mark_node;
9081 }
9082
9083 /* Parse a mem-initializer-id.
9084
9085    mem-initializer-id:
9086      :: [opt] nested-name-specifier [opt] class-name
9087      identifier
9088
9089    Returns a TYPE indicating the class to be initializer for the first
9090    production.  Returns an IDENTIFIER_NODE indicating the data member
9091    to be initialized for the second production.  */
9092
9093 static tree
9094 cp_parser_mem_initializer_id (cp_parser* parser)
9095 {
9096   bool global_scope_p;
9097   bool nested_name_specifier_p;
9098   bool template_p = false;
9099   tree id;
9100
9101   cp_token *token = cp_lexer_peek_token (parser->lexer);
9102
9103   /* `typename' is not allowed in this context ([temp.res]).  */
9104   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9105     {
9106       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9107              "member initializer is implicitly a type)",
9108              &token->location);
9109       cp_lexer_consume_token (parser->lexer);
9110     }
9111   /* Look for the optional `::' operator.  */
9112   global_scope_p
9113     = (cp_parser_global_scope_opt (parser,
9114                                    /*current_scope_valid_p=*/false)
9115        != NULL_TREE);
9116   /* Look for the optional nested-name-specifier.  The simplest way to
9117      implement:
9118
9119        [temp.res]
9120
9121        The keyword `typename' is not permitted in a base-specifier or
9122        mem-initializer; in these contexts a qualified name that
9123        depends on a template-parameter is implicitly assumed to be a
9124        type name.
9125
9126      is to assume that we have seen the `typename' keyword at this
9127      point.  */
9128   nested_name_specifier_p
9129     = (cp_parser_nested_name_specifier_opt (parser,
9130                                             /*typename_keyword_p=*/true,
9131                                             /*check_dependency_p=*/true,
9132                                             /*type_p=*/true,
9133                                             /*is_declaration=*/true)
9134        != NULL_TREE);
9135   if (nested_name_specifier_p)
9136     template_p = cp_parser_optional_template_keyword (parser);
9137   /* If there is a `::' operator or a nested-name-specifier, then we
9138      are definitely looking for a class-name.  */
9139   if (global_scope_p || nested_name_specifier_p)
9140     return cp_parser_class_name (parser,
9141                                  /*typename_keyword_p=*/true,
9142                                  /*template_keyword_p=*/template_p,
9143                                  none_type,
9144                                  /*check_dependency_p=*/true,
9145                                  /*class_head_p=*/false,
9146                                  /*is_declaration=*/true);
9147   /* Otherwise, we could also be looking for an ordinary identifier.  */
9148   cp_parser_parse_tentatively (parser);
9149   /* Try a class-name.  */
9150   id = cp_parser_class_name (parser,
9151                              /*typename_keyword_p=*/true,
9152                              /*template_keyword_p=*/false,
9153                              none_type,
9154                              /*check_dependency_p=*/true,
9155                              /*class_head_p=*/false,
9156                              /*is_declaration=*/true);
9157   /* If we found one, we're done.  */
9158   if (cp_parser_parse_definitely (parser))
9159     return id;
9160   /* Otherwise, look for an ordinary identifier.  */
9161   return cp_parser_identifier (parser);
9162 }
9163
9164 /* Overloading [gram.over] */
9165
9166 /* Parse an operator-function-id.
9167
9168    operator-function-id:
9169      operator operator
9170
9171    Returns an IDENTIFIER_NODE for the operator which is a
9172    human-readable spelling of the identifier, e.g., `operator +'.  */
9173
9174 static tree
9175 cp_parser_operator_function_id (cp_parser* parser)
9176 {
9177   /* Look for the `operator' keyword.  */
9178   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9179     return error_mark_node;
9180   /* And then the name of the operator itself.  */
9181   return cp_parser_operator (parser);
9182 }
9183
9184 /* Parse an operator.
9185
9186    operator:
9187      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9188      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9189      || ++ -- , ->* -> () []
9190
9191    GNU Extensions:
9192
9193    operator:
9194      <? >? <?= >?=
9195
9196    Returns an IDENTIFIER_NODE for the operator which is a
9197    human-readable spelling of the identifier, e.g., `operator +'.  */
9198
9199 static tree
9200 cp_parser_operator (cp_parser* parser)
9201 {
9202   tree id = NULL_TREE;
9203   cp_token *token;
9204
9205   /* Peek at the next token.  */
9206   token = cp_lexer_peek_token (parser->lexer);
9207   /* Figure out which operator we have.  */
9208   switch (token->type)
9209     {
9210     case CPP_KEYWORD:
9211       {
9212         enum tree_code op;
9213
9214         /* The keyword should be either `new' or `delete'.  */
9215         if (token->keyword == RID_NEW)
9216           op = NEW_EXPR;
9217         else if (token->keyword == RID_DELETE)
9218           op = DELETE_EXPR;
9219         else
9220           break;
9221
9222         /* Consume the `new' or `delete' token.  */
9223         cp_lexer_consume_token (parser->lexer);
9224
9225         /* Peek at the next token.  */
9226         token = cp_lexer_peek_token (parser->lexer);
9227         /* If it's a `[' token then this is the array variant of the
9228            operator.  */
9229         if (token->type == CPP_OPEN_SQUARE)
9230           {
9231             /* Consume the `[' token.  */
9232             cp_lexer_consume_token (parser->lexer);
9233             /* Look for the `]' token.  */
9234             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9235             id = ansi_opname (op == NEW_EXPR
9236                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9237           }
9238         /* Otherwise, we have the non-array variant.  */
9239         else
9240           id = ansi_opname (op);
9241
9242         return id;
9243       }
9244
9245     case CPP_PLUS:
9246       id = ansi_opname (PLUS_EXPR);
9247       break;
9248
9249     case CPP_MINUS:
9250       id = ansi_opname (MINUS_EXPR);
9251       break;
9252
9253     case CPP_MULT:
9254       id = ansi_opname (MULT_EXPR);
9255       break;
9256
9257     case CPP_DIV:
9258       id = ansi_opname (TRUNC_DIV_EXPR);
9259       break;
9260
9261     case CPP_MOD:
9262       id = ansi_opname (TRUNC_MOD_EXPR);
9263       break;
9264
9265     case CPP_XOR:
9266       id = ansi_opname (BIT_XOR_EXPR);
9267       break;
9268
9269     case CPP_AND:
9270       id = ansi_opname (BIT_AND_EXPR);
9271       break;
9272
9273     case CPP_OR:
9274       id = ansi_opname (BIT_IOR_EXPR);
9275       break;
9276
9277     case CPP_COMPL:
9278       id = ansi_opname (BIT_NOT_EXPR);
9279       break;
9280
9281     case CPP_NOT:
9282       id = ansi_opname (TRUTH_NOT_EXPR);
9283       break;
9284
9285     case CPP_EQ:
9286       id = ansi_assopname (NOP_EXPR);
9287       break;
9288
9289     case CPP_LESS:
9290       id = ansi_opname (LT_EXPR);
9291       break;
9292
9293     case CPP_GREATER:
9294       id = ansi_opname (GT_EXPR);
9295       break;
9296
9297     case CPP_PLUS_EQ:
9298       id = ansi_assopname (PLUS_EXPR);
9299       break;
9300
9301     case CPP_MINUS_EQ:
9302       id = ansi_assopname (MINUS_EXPR);
9303       break;
9304
9305     case CPP_MULT_EQ:
9306       id = ansi_assopname (MULT_EXPR);
9307       break;
9308
9309     case CPP_DIV_EQ:
9310       id = ansi_assopname (TRUNC_DIV_EXPR);
9311       break;
9312
9313     case CPP_MOD_EQ:
9314       id = ansi_assopname (TRUNC_MOD_EXPR);
9315       break;
9316
9317     case CPP_XOR_EQ:
9318       id = ansi_assopname (BIT_XOR_EXPR);
9319       break;
9320
9321     case CPP_AND_EQ:
9322       id = ansi_assopname (BIT_AND_EXPR);
9323       break;
9324
9325     case CPP_OR_EQ:
9326       id = ansi_assopname (BIT_IOR_EXPR);
9327       break;
9328
9329     case CPP_LSHIFT:
9330       id = ansi_opname (LSHIFT_EXPR);
9331       break;
9332
9333     case CPP_RSHIFT:
9334       id = ansi_opname (RSHIFT_EXPR);
9335       break;
9336
9337     case CPP_LSHIFT_EQ:
9338       id = ansi_assopname (LSHIFT_EXPR);
9339       break;
9340
9341     case CPP_RSHIFT_EQ:
9342       id = ansi_assopname (RSHIFT_EXPR);
9343       break;
9344
9345     case CPP_EQ_EQ:
9346       id = ansi_opname (EQ_EXPR);
9347       break;
9348
9349     case CPP_NOT_EQ:
9350       id = ansi_opname (NE_EXPR);
9351       break;
9352
9353     case CPP_LESS_EQ:
9354       id = ansi_opname (LE_EXPR);
9355       break;
9356
9357     case CPP_GREATER_EQ:
9358       id = ansi_opname (GE_EXPR);
9359       break;
9360
9361     case CPP_AND_AND:
9362       id = ansi_opname (TRUTH_ANDIF_EXPR);
9363       break;
9364
9365     case CPP_OR_OR:
9366       id = ansi_opname (TRUTH_ORIF_EXPR);
9367       break;
9368
9369     case CPP_PLUS_PLUS:
9370       id = ansi_opname (POSTINCREMENT_EXPR);
9371       break;
9372
9373     case CPP_MINUS_MINUS:
9374       id = ansi_opname (PREDECREMENT_EXPR);
9375       break;
9376
9377     case CPP_COMMA:
9378       id = ansi_opname (COMPOUND_EXPR);
9379       break;
9380
9381     case CPP_DEREF_STAR:
9382       id = ansi_opname (MEMBER_REF);
9383       break;
9384
9385     case CPP_DEREF:
9386       id = ansi_opname (COMPONENT_REF);
9387       break;
9388
9389     case CPP_OPEN_PAREN:
9390       /* Consume the `('.  */
9391       cp_lexer_consume_token (parser->lexer);
9392       /* Look for the matching `)'.  */
9393       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9394       return ansi_opname (CALL_EXPR);
9395
9396     case CPP_OPEN_SQUARE:
9397       /* Consume the `['.  */
9398       cp_lexer_consume_token (parser->lexer);
9399       /* Look for the matching `]'.  */
9400       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9401       return ansi_opname (ARRAY_REF);
9402
9403     default:
9404       /* Anything else is an error.  */
9405       break;
9406     }
9407
9408   /* If we have selected an identifier, we need to consume the
9409      operator token.  */
9410   if (id)
9411     cp_lexer_consume_token (parser->lexer);
9412   /* Otherwise, no valid operator name was present.  */
9413   else
9414     {
9415       cp_parser_error (parser, "expected operator");
9416       id = error_mark_node;
9417     }
9418
9419   return id;
9420 }
9421
9422 /* Parse a template-declaration.
9423
9424    template-declaration:
9425      export [opt] template < template-parameter-list > declaration
9426
9427    If MEMBER_P is TRUE, this template-declaration occurs within a
9428    class-specifier.
9429
9430    The grammar rule given by the standard isn't correct.  What
9431    is really meant is:
9432
9433    template-declaration:
9434      export [opt] template-parameter-list-seq
9435        decl-specifier-seq [opt] init-declarator [opt] ;
9436      export [opt] template-parameter-list-seq
9437        function-definition
9438
9439    template-parameter-list-seq:
9440      template-parameter-list-seq [opt]
9441      template < template-parameter-list >  */
9442
9443 static void
9444 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9445 {
9446   /* Check for `export'.  */
9447   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9448     {
9449       /* Consume the `export' token.  */
9450       cp_lexer_consume_token (parser->lexer);
9451       /* Warn that we do not support `export'.  */
9452       warning (0, "keyword %<export%> not implemented, and will be ignored");
9453     }
9454
9455   cp_parser_template_declaration_after_export (parser, member_p);
9456 }
9457
9458 /* Parse a template-parameter-list.
9459
9460    template-parameter-list:
9461      template-parameter
9462      template-parameter-list , template-parameter
9463
9464    Returns a TREE_LIST.  Each node represents a template parameter.
9465    The nodes are connected via their TREE_CHAINs.  */
9466
9467 static tree
9468 cp_parser_template_parameter_list (cp_parser* parser)
9469 {
9470   tree parameter_list = NULL_TREE;
9471
9472   begin_template_parm_list ();
9473   while (true)
9474     {
9475       tree parameter;
9476       bool is_non_type;
9477       bool is_parameter_pack;
9478
9479       /* Parse the template-parameter.  */
9480       parameter = cp_parser_template_parameter (parser, 
9481                                                 &is_non_type,
9482                                                 &is_parameter_pack);
9483       /* Add it to the list.  */
9484       if (parameter != error_mark_node)
9485         parameter_list = process_template_parm (parameter_list,
9486                                                 parameter,
9487                                                 is_non_type,
9488                                                 is_parameter_pack);
9489       else
9490        {
9491          tree err_parm = build_tree_list (parameter, parameter);
9492          TREE_VALUE (err_parm) = error_mark_node;
9493          parameter_list = chainon (parameter_list, err_parm);
9494        }
9495
9496       /* If the next token is not a `,', we're done.  */
9497       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9498         break;
9499       /* Otherwise, consume the `,' token.  */
9500       cp_lexer_consume_token (parser->lexer);
9501     }
9502
9503   return end_template_parm_list (parameter_list);
9504 }
9505
9506 /* Parse a template-parameter.
9507
9508    template-parameter:
9509      type-parameter
9510      parameter-declaration
9511
9512    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9513    the parameter.  The TREE_PURPOSE is the default value, if any.
9514    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9515    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9516    set to true iff this parameter is a parameter pack. */
9517
9518 static tree
9519 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9520                               bool *is_parameter_pack)
9521 {
9522   cp_token *token;
9523   cp_parameter_declarator *parameter_declarator;
9524   cp_declarator *id_declarator;
9525   tree parm;
9526
9527   /* Assume it is a type parameter or a template parameter.  */
9528   *is_non_type = false;
9529   /* Assume it not a parameter pack. */
9530   *is_parameter_pack = false;
9531   /* Peek at the next token.  */
9532   token = cp_lexer_peek_token (parser->lexer);
9533   /* If it is `class' or `template', we have a type-parameter.  */
9534   if (token->keyword == RID_TEMPLATE)
9535     return cp_parser_type_parameter (parser, is_parameter_pack);
9536   /* If it is `class' or `typename' we do not know yet whether it is a
9537      type parameter or a non-type parameter.  Consider:
9538
9539        template <typename T, typename T::X X> ...
9540
9541      or:
9542
9543        template <class C, class D*> ...
9544
9545      Here, the first parameter is a type parameter, and the second is
9546      a non-type parameter.  We can tell by looking at the token after
9547      the identifier -- if it is a `,', `=', or `>' then we have a type
9548      parameter.  */
9549   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9550     {
9551       /* Peek at the token after `class' or `typename'.  */
9552       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9553       /* If it's an ellipsis, we have a template type parameter
9554          pack. */
9555       if (token->type == CPP_ELLIPSIS)
9556         return cp_parser_type_parameter (parser, is_parameter_pack);
9557       /* If it's an identifier, skip it.  */
9558       if (token->type == CPP_NAME)
9559         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9560       /* Now, see if the token looks like the end of a template
9561          parameter.  */
9562       if (token->type == CPP_COMMA
9563           || token->type == CPP_EQ
9564           || token->type == CPP_GREATER)
9565         return cp_parser_type_parameter (parser, is_parameter_pack);
9566     }
9567
9568   /* Otherwise, it is a non-type parameter.
9569
9570      [temp.param]
9571
9572      When parsing a default template-argument for a non-type
9573      template-parameter, the first non-nested `>' is taken as the end
9574      of the template parameter-list rather than a greater-than
9575      operator.  */
9576   *is_non_type = true;
9577   parameter_declarator
9578      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9579                                         /*parenthesized_p=*/NULL);
9580
9581   /* If the parameter declaration is marked as a parameter pack, set
9582      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9583      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9584      grokdeclarator. */
9585   if (parameter_declarator
9586       && parameter_declarator->declarator
9587       && parameter_declarator->declarator->parameter_pack_p)
9588     {
9589       *is_parameter_pack = true;
9590       parameter_declarator->declarator->parameter_pack_p = false;
9591     }
9592
9593   /* If the next token is an ellipsis, and we don't already have it
9594      marked as a parameter pack, then we have a parameter pack (that
9595      has no declarator).  */
9596   if (!*is_parameter_pack
9597       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9598       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9599     {
9600       /* Consume the `...'.  */
9601       cp_lexer_consume_token (parser->lexer);
9602       maybe_warn_variadic_templates ();
9603       
9604       *is_parameter_pack = true;
9605     }
9606   /* We might end up with a pack expansion as the type of the non-type
9607      template parameter, in which case this is a non-type template
9608      parameter pack.  */
9609   else if (parameter_declarator
9610            && parameter_declarator->decl_specifiers.type
9611            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9612     {
9613       *is_parameter_pack = true;
9614       parameter_declarator->decl_specifiers.type = 
9615         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9616     }
9617
9618   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9619     {
9620       /* Parameter packs cannot have default arguments.  However, a
9621          user may try to do so, so we'll parse them and give an
9622          appropriate diagnostic here.  */
9623
9624       /* Consume the `='.  */
9625       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9626       cp_lexer_consume_token (parser->lexer);
9627       
9628       /* Find the name of the parameter pack.  */     
9629       id_declarator = parameter_declarator->declarator;
9630       while (id_declarator && id_declarator->kind != cdk_id)
9631         id_declarator = id_declarator->declarator;
9632       
9633       if (id_declarator && id_declarator->kind == cdk_id)
9634         error ("%Htemplate parameter pack %qD cannot have a default argument",
9635                &start_token->location, id_declarator->u.id.unqualified_name);
9636       else
9637         error ("%Htemplate parameter pack cannot have a default argument",
9638                &start_token->location);
9639       
9640       /* Parse the default argument, but throw away the result.  */
9641       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9642     }
9643
9644   parm = grokdeclarator (parameter_declarator->declarator,
9645                          &parameter_declarator->decl_specifiers,
9646                          PARM, /*initialized=*/0,
9647                          /*attrlist=*/NULL);
9648   if (parm == error_mark_node)
9649     return error_mark_node;
9650
9651   return build_tree_list (parameter_declarator->default_argument, parm);
9652 }
9653
9654 /* Parse a type-parameter.
9655
9656    type-parameter:
9657      class identifier [opt]
9658      class identifier [opt] = type-id
9659      typename identifier [opt]
9660      typename identifier [opt] = type-id
9661      template < template-parameter-list > class identifier [opt]
9662      template < template-parameter-list > class identifier [opt]
9663        = id-expression
9664
9665    GNU Extension (variadic templates):
9666
9667    type-parameter:
9668      class ... identifier [opt]
9669      typename ... identifier [opt]
9670
9671    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9672    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9673    the declaration of the parameter.
9674
9675    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9676
9677 static tree
9678 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9679 {
9680   cp_token *token;
9681   tree parameter;
9682
9683   /* Look for a keyword to tell us what kind of parameter this is.  */
9684   token = cp_parser_require (parser, CPP_KEYWORD,
9685                              "%<class%>, %<typename%>, or %<template%>");
9686   if (!token)
9687     return error_mark_node;
9688
9689   switch (token->keyword)
9690     {
9691     case RID_CLASS:
9692     case RID_TYPENAME:
9693       {
9694         tree identifier;
9695         tree default_argument;
9696
9697         /* If the next token is an ellipsis, we have a template
9698            argument pack. */
9699         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9700           {
9701             /* Consume the `...' token. */
9702             cp_lexer_consume_token (parser->lexer);
9703             maybe_warn_variadic_templates ();
9704
9705             *is_parameter_pack = true;
9706           }
9707
9708         /* If the next token is an identifier, then it names the
9709            parameter.  */
9710         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9711           identifier = cp_parser_identifier (parser);
9712         else
9713           identifier = NULL_TREE;
9714
9715         /* Create the parameter.  */
9716         parameter = finish_template_type_parm (class_type_node, identifier);
9717
9718         /* If the next token is an `=', we have a default argument.  */
9719         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9720           {
9721             /* Consume the `=' token.  */
9722             cp_lexer_consume_token (parser->lexer);
9723             /* Parse the default-argument.  */
9724             push_deferring_access_checks (dk_no_deferred);
9725             default_argument = cp_parser_type_id (parser);
9726
9727             /* Template parameter packs cannot have default
9728                arguments. */
9729             if (*is_parameter_pack)
9730               {
9731                 if (identifier)
9732                   error ("%Htemplate parameter pack %qD cannot have a "
9733                          "default argument", &token->location, identifier);
9734                 else
9735                   error ("%Htemplate parameter packs cannot have "
9736                          "default arguments", &token->location);
9737                 default_argument = NULL_TREE;
9738               }
9739             pop_deferring_access_checks ();
9740           }
9741         else
9742           default_argument = NULL_TREE;
9743
9744         /* Create the combined representation of the parameter and the
9745            default argument.  */
9746         parameter = build_tree_list (default_argument, parameter);
9747       }
9748       break;
9749
9750     case RID_TEMPLATE:
9751       {
9752         tree parameter_list;
9753         tree identifier;
9754         tree default_argument;
9755
9756         /* Look for the `<'.  */
9757         cp_parser_require (parser, CPP_LESS, "%<<%>");
9758         /* Parse the template-parameter-list.  */
9759         parameter_list = cp_parser_template_parameter_list (parser);
9760         /* Look for the `>'.  */
9761         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9762         /* Look for the `class' keyword.  */
9763         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9764         /* If the next token is an ellipsis, we have a template
9765            argument pack. */
9766         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9767           {
9768             /* Consume the `...' token. */
9769             cp_lexer_consume_token (parser->lexer);
9770             maybe_warn_variadic_templates ();
9771
9772             *is_parameter_pack = true;
9773           }
9774         /* If the next token is an `=', then there is a
9775            default-argument.  If the next token is a `>', we are at
9776            the end of the parameter-list.  If the next token is a `,',
9777            then we are at the end of this parameter.  */
9778         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9779             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9780             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9781           {
9782             identifier = cp_parser_identifier (parser);
9783             /* Treat invalid names as if the parameter were nameless.  */
9784             if (identifier == error_mark_node)
9785               identifier = NULL_TREE;
9786           }
9787         else
9788           identifier = NULL_TREE;
9789
9790         /* Create the template parameter.  */
9791         parameter = finish_template_template_parm (class_type_node,
9792                                                    identifier);
9793
9794         /* If the next token is an `=', then there is a
9795            default-argument.  */
9796         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9797           {
9798             bool is_template;
9799
9800             /* Consume the `='.  */
9801             cp_lexer_consume_token (parser->lexer);
9802             /* Parse the id-expression.  */
9803             push_deferring_access_checks (dk_no_deferred);
9804             /* save token before parsing the id-expression, for error
9805                reporting */
9806             token = cp_lexer_peek_token (parser->lexer);
9807             default_argument
9808               = cp_parser_id_expression (parser,
9809                                          /*template_keyword_p=*/false,
9810                                          /*check_dependency_p=*/true,
9811                                          /*template_p=*/&is_template,
9812                                          /*declarator_p=*/false,
9813                                          /*optional_p=*/false);
9814             if (TREE_CODE (default_argument) == TYPE_DECL)
9815               /* If the id-expression was a template-id that refers to
9816                  a template-class, we already have the declaration here,
9817                  so no further lookup is needed.  */
9818                  ;
9819             else
9820               /* Look up the name.  */
9821               default_argument
9822                 = cp_parser_lookup_name (parser, default_argument,
9823                                          none_type,
9824                                          /*is_template=*/is_template,
9825                                          /*is_namespace=*/false,
9826                                          /*check_dependency=*/true,
9827                                          /*ambiguous_decls=*/NULL,
9828                                          token->location);
9829             /* See if the default argument is valid.  */
9830             default_argument
9831               = check_template_template_default_arg (default_argument);
9832
9833             /* Template parameter packs cannot have default
9834                arguments. */
9835             if (*is_parameter_pack)
9836               {
9837                 if (identifier)
9838                   error ("%Htemplate parameter pack %qD cannot "
9839                          "have a default argument",
9840                          &token->location, identifier);
9841                 else
9842                   error ("%Htemplate parameter packs cannot "
9843                          "have default arguments",
9844                          &token->location);
9845                 default_argument = NULL_TREE;
9846               }
9847             pop_deferring_access_checks ();
9848           }
9849         else
9850           default_argument = NULL_TREE;
9851
9852         /* Create the combined representation of the parameter and the
9853            default argument.  */
9854         parameter = build_tree_list (default_argument, parameter);
9855       }
9856       break;
9857
9858     default:
9859       gcc_unreachable ();
9860       break;
9861     }
9862
9863   return parameter;
9864 }
9865
9866 /* Parse a template-id.
9867
9868    template-id:
9869      template-name < template-argument-list [opt] >
9870
9871    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9872    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9873    returned.  Otherwise, if the template-name names a function, or set
9874    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9875    names a class, returns a TYPE_DECL for the specialization.
9876
9877    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9878    uninstantiated templates.  */
9879
9880 static tree
9881 cp_parser_template_id (cp_parser *parser,
9882                        bool template_keyword_p,
9883                        bool check_dependency_p,
9884                        bool is_declaration)
9885 {
9886   int i;
9887   tree templ;
9888   tree arguments;
9889   tree template_id;
9890   cp_token_position start_of_id = 0;
9891   deferred_access_check *chk;
9892   VEC (deferred_access_check,gc) *access_check;
9893   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9894   bool is_identifier;
9895
9896   /* If the next token corresponds to a template-id, there is no need
9897      to reparse it.  */
9898   next_token = cp_lexer_peek_token (parser->lexer);
9899   if (next_token->type == CPP_TEMPLATE_ID)
9900     {
9901       struct tree_check *check_value;
9902
9903       /* Get the stored value.  */
9904       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9905       /* Perform any access checks that were deferred.  */
9906       access_check = check_value->checks;
9907       if (access_check)
9908         {
9909           for (i = 0 ;
9910                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9911                ++i)
9912             {
9913               perform_or_defer_access_check (chk->binfo,
9914                                              chk->decl,
9915                                              chk->diag_decl);
9916             }
9917         }
9918       /* Return the stored value.  */
9919       return check_value->value;
9920     }
9921
9922   /* Avoid performing name lookup if there is no possibility of
9923      finding a template-id.  */
9924   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9925       || (next_token->type == CPP_NAME
9926           && !cp_parser_nth_token_starts_template_argument_list_p
9927                (parser, 2)))
9928     {
9929       cp_parser_error (parser, "expected template-id");
9930       return error_mark_node;
9931     }
9932
9933   /* Remember where the template-id starts.  */
9934   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9935     start_of_id = cp_lexer_token_position (parser->lexer, false);
9936
9937   push_deferring_access_checks (dk_deferred);
9938
9939   /* Parse the template-name.  */
9940   is_identifier = false;
9941   token = cp_lexer_peek_token (parser->lexer);
9942   templ = cp_parser_template_name (parser, template_keyword_p,
9943                                    check_dependency_p,
9944                                    is_declaration,
9945                                    &is_identifier);
9946   if (templ == error_mark_node || is_identifier)
9947     {
9948       pop_deferring_access_checks ();
9949       return templ;
9950     }
9951
9952   /* If we find the sequence `[:' after a template-name, it's probably
9953      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9954      parse correctly the argument list.  */
9955   next_token = cp_lexer_peek_token (parser->lexer);
9956   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9957   if (next_token->type == CPP_OPEN_SQUARE
9958       && next_token->flags & DIGRAPH
9959       && next_token_2->type == CPP_COLON
9960       && !(next_token_2->flags & PREV_WHITE))
9961     {
9962       cp_parser_parse_tentatively (parser);
9963       /* Change `:' into `::'.  */
9964       next_token_2->type = CPP_SCOPE;
9965       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9966          CPP_LESS.  */
9967       cp_lexer_consume_token (parser->lexer);
9968
9969       /* Parse the arguments.  */
9970       arguments = cp_parser_enclosed_template_argument_list (parser);
9971       if (!cp_parser_parse_definitely (parser))
9972         {
9973           /* If we couldn't parse an argument list, then we revert our changes
9974              and return simply an error. Maybe this is not a template-id
9975              after all.  */
9976           next_token_2->type = CPP_COLON;
9977           cp_parser_error (parser, "expected %<<%>");
9978           pop_deferring_access_checks ();
9979           return error_mark_node;
9980         }
9981       /* Otherwise, emit an error about the invalid digraph, but continue
9982          parsing because we got our argument list.  */
9983       if (permerror (next_token->location,
9984                      "%<<::%> cannot begin a template-argument list"))
9985         {
9986           static bool hint = false;
9987           inform (next_token->location,
9988                   "%<<:%> is an alternate spelling for %<[%>."
9989                   " Insert whitespace between %<<%> and %<::%>");
9990           if (!hint && !flag_permissive)
9991             {
9992               inform (next_token->location, "(if you use %<-fpermissive%>"
9993                       " G++ will accept your code)");
9994               hint = true;
9995             }
9996         }
9997     }
9998   else
9999     {
10000       /* Look for the `<' that starts the template-argument-list.  */
10001       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10002         {
10003           pop_deferring_access_checks ();
10004           return error_mark_node;
10005         }
10006       /* Parse the arguments.  */
10007       arguments = cp_parser_enclosed_template_argument_list (parser);
10008     }
10009
10010   /* Build a representation of the specialization.  */
10011   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10012     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10013   else if (DECL_CLASS_TEMPLATE_P (templ)
10014            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10015     {
10016       bool entering_scope;
10017       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10018          template (rather than some instantiation thereof) only if
10019          is not nested within some other construct.  For example, in
10020          "template <typename T> void f(T) { A<T>::", A<T> is just an
10021          instantiation of A.  */
10022       entering_scope = (template_parm_scope_p ()
10023                         && cp_lexer_next_token_is (parser->lexer,
10024                                                    CPP_SCOPE));
10025       template_id
10026         = finish_template_type (templ, arguments, entering_scope);
10027     }
10028   else
10029     {
10030       /* If it's not a class-template or a template-template, it should be
10031          a function-template.  */
10032       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10033                    || TREE_CODE (templ) == OVERLOAD
10034                    || BASELINK_P (templ)));
10035
10036       template_id = lookup_template_function (templ, arguments);
10037     }
10038
10039   /* If parsing tentatively, replace the sequence of tokens that makes
10040      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10041      should we re-parse the token stream, we will not have to repeat
10042      the effort required to do the parse, nor will we issue duplicate
10043      error messages about problems during instantiation of the
10044      template.  */
10045   if (start_of_id)
10046     {
10047       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10048
10049       /* Reset the contents of the START_OF_ID token.  */
10050       token->type = CPP_TEMPLATE_ID;
10051       /* Retrieve any deferred checks.  Do not pop this access checks yet
10052          so the memory will not be reclaimed during token replacing below.  */
10053       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10054       token->u.tree_check_value->value = template_id;
10055       token->u.tree_check_value->checks = get_deferred_access_checks ();
10056       token->keyword = RID_MAX;
10057
10058       /* Purge all subsequent tokens.  */
10059       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10060
10061       /* ??? Can we actually assume that, if template_id ==
10062          error_mark_node, we will have issued a diagnostic to the
10063          user, as opposed to simply marking the tentative parse as
10064          failed?  */
10065       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10066         error ("%Hparse error in template argument list",
10067                &token->location);
10068     }
10069
10070   pop_deferring_access_checks ();
10071   return template_id;
10072 }
10073
10074 /* Parse a template-name.
10075
10076    template-name:
10077      identifier
10078
10079    The standard should actually say:
10080
10081    template-name:
10082      identifier
10083      operator-function-id
10084
10085    A defect report has been filed about this issue.
10086
10087    A conversion-function-id cannot be a template name because they cannot
10088    be part of a template-id. In fact, looking at this code:
10089
10090    a.operator K<int>()
10091
10092    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10093    It is impossible to call a templated conversion-function-id with an
10094    explicit argument list, since the only allowed template parameter is
10095    the type to which it is converting.
10096
10097    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10098    `template' keyword, in a construction like:
10099
10100      T::template f<3>()
10101
10102    In that case `f' is taken to be a template-name, even though there
10103    is no way of knowing for sure.
10104
10105    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10106    name refers to a set of overloaded functions, at least one of which
10107    is a template, or an IDENTIFIER_NODE with the name of the template,
10108    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10109    names are looked up inside uninstantiated templates.  */
10110
10111 static tree
10112 cp_parser_template_name (cp_parser* parser,
10113                          bool template_keyword_p,
10114                          bool check_dependency_p,
10115                          bool is_declaration,
10116                          bool *is_identifier)
10117 {
10118   tree identifier;
10119   tree decl;
10120   tree fns;
10121   cp_token *token = cp_lexer_peek_token (parser->lexer);
10122
10123   /* If the next token is `operator', then we have either an
10124      operator-function-id or a conversion-function-id.  */
10125   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10126     {
10127       /* We don't know whether we're looking at an
10128          operator-function-id or a conversion-function-id.  */
10129       cp_parser_parse_tentatively (parser);
10130       /* Try an operator-function-id.  */
10131       identifier = cp_parser_operator_function_id (parser);
10132       /* If that didn't work, try a conversion-function-id.  */
10133       if (!cp_parser_parse_definitely (parser))
10134         {
10135           cp_parser_error (parser, "expected template-name");
10136           return error_mark_node;
10137         }
10138     }
10139   /* Look for the identifier.  */
10140   else
10141     identifier = cp_parser_identifier (parser);
10142
10143   /* If we didn't find an identifier, we don't have a template-id.  */
10144   if (identifier == error_mark_node)
10145     return error_mark_node;
10146
10147   /* If the name immediately followed the `template' keyword, then it
10148      is a template-name.  However, if the next token is not `<', then
10149      we do not treat it as a template-name, since it is not being used
10150      as part of a template-id.  This enables us to handle constructs
10151      like:
10152
10153        template <typename T> struct S { S(); };
10154        template <typename T> S<T>::S();
10155
10156      correctly.  We would treat `S' as a template -- if it were `S<T>'
10157      -- but we do not if there is no `<'.  */
10158
10159   if (processing_template_decl
10160       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10161     {
10162       /* In a declaration, in a dependent context, we pretend that the
10163          "template" keyword was present in order to improve error
10164          recovery.  For example, given:
10165
10166            template <typename T> void f(T::X<int>);
10167
10168          we want to treat "X<int>" as a template-id.  */
10169       if (is_declaration
10170           && !template_keyword_p
10171           && parser->scope && TYPE_P (parser->scope)
10172           && check_dependency_p
10173           && dependent_type_p (parser->scope)
10174           /* Do not do this for dtors (or ctors), since they never
10175              need the template keyword before their name.  */
10176           && !constructor_name_p (identifier, parser->scope))
10177         {
10178           cp_token_position start = 0;
10179
10180           /* Explain what went wrong.  */
10181           error ("%Hnon-template %qD used as template",
10182                  &token->location, identifier);
10183           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10184                   parser->scope, identifier);
10185           /* If parsing tentatively, find the location of the "<" token.  */
10186           if (cp_parser_simulate_error (parser))
10187             start = cp_lexer_token_position (parser->lexer, true);
10188           /* Parse the template arguments so that we can issue error
10189              messages about them.  */
10190           cp_lexer_consume_token (parser->lexer);
10191           cp_parser_enclosed_template_argument_list (parser);
10192           /* Skip tokens until we find a good place from which to
10193              continue parsing.  */
10194           cp_parser_skip_to_closing_parenthesis (parser,
10195                                                  /*recovering=*/true,
10196                                                  /*or_comma=*/true,
10197                                                  /*consume_paren=*/false);
10198           /* If parsing tentatively, permanently remove the
10199              template argument list.  That will prevent duplicate
10200              error messages from being issued about the missing
10201              "template" keyword.  */
10202           if (start)
10203             cp_lexer_purge_tokens_after (parser->lexer, start);
10204           if (is_identifier)
10205             *is_identifier = true;
10206           return identifier;
10207         }
10208
10209       /* If the "template" keyword is present, then there is generally
10210          no point in doing name-lookup, so we just return IDENTIFIER.
10211          But, if the qualifying scope is non-dependent then we can
10212          (and must) do name-lookup normally.  */
10213       if (template_keyword_p
10214           && (!parser->scope
10215               || (TYPE_P (parser->scope)
10216                   && dependent_type_p (parser->scope))))
10217         return identifier;
10218     }
10219
10220   /* Look up the name.  */
10221   decl = cp_parser_lookup_name (parser, identifier,
10222                                 none_type,
10223                                 /*is_template=*/false,
10224                                 /*is_namespace=*/false,
10225                                 check_dependency_p,
10226                                 /*ambiguous_decls=*/NULL,
10227                                 token->location);
10228   decl = maybe_get_template_decl_from_type_decl (decl);
10229
10230   /* If DECL is a template, then the name was a template-name.  */
10231   if (TREE_CODE (decl) == TEMPLATE_DECL)
10232     ;
10233   else
10234     {
10235       tree fn = NULL_TREE;
10236
10237       /* The standard does not explicitly indicate whether a name that
10238          names a set of overloaded declarations, some of which are
10239          templates, is a template-name.  However, such a name should
10240          be a template-name; otherwise, there is no way to form a
10241          template-id for the overloaded templates.  */
10242       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10243       if (TREE_CODE (fns) == OVERLOAD)
10244         for (fn = fns; fn; fn = OVL_NEXT (fn))
10245           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10246             break;
10247
10248       if (!fn)
10249         {
10250           /* The name does not name a template.  */
10251           cp_parser_error (parser, "expected template-name");
10252           return error_mark_node;
10253         }
10254     }
10255
10256   /* If DECL is dependent, and refers to a function, then just return
10257      its name; we will look it up again during template instantiation.  */
10258   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10259     {
10260       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10261       if (TYPE_P (scope) && dependent_type_p (scope))
10262         return identifier;
10263     }
10264
10265   return decl;
10266 }
10267
10268 /* Parse a template-argument-list.
10269
10270    template-argument-list:
10271      template-argument ... [opt]
10272      template-argument-list , template-argument ... [opt]
10273
10274    Returns a TREE_VEC containing the arguments.  */
10275
10276 static tree
10277 cp_parser_template_argument_list (cp_parser* parser)
10278 {
10279   tree fixed_args[10];
10280   unsigned n_args = 0;
10281   unsigned alloced = 10;
10282   tree *arg_ary = fixed_args;
10283   tree vec;
10284   bool saved_in_template_argument_list_p;
10285   bool saved_ice_p;
10286   bool saved_non_ice_p;
10287
10288   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10289   parser->in_template_argument_list_p = true;
10290   /* Even if the template-id appears in an integral
10291      constant-expression, the contents of the argument list do
10292      not.  */
10293   saved_ice_p = parser->integral_constant_expression_p;
10294   parser->integral_constant_expression_p = false;
10295   saved_non_ice_p = parser->non_integral_constant_expression_p;
10296   parser->non_integral_constant_expression_p = false;
10297   /* Parse the arguments.  */
10298   do
10299     {
10300       tree argument;
10301
10302       if (n_args)
10303         /* Consume the comma.  */
10304         cp_lexer_consume_token (parser->lexer);
10305
10306       /* Parse the template-argument.  */
10307       argument = cp_parser_template_argument (parser);
10308
10309       /* If the next token is an ellipsis, we're expanding a template
10310          argument pack. */
10311       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10312         {
10313           /* Consume the `...' token. */
10314           cp_lexer_consume_token (parser->lexer);
10315
10316           /* Make the argument into a TYPE_PACK_EXPANSION or
10317              EXPR_PACK_EXPANSION. */
10318           argument = make_pack_expansion (argument);
10319         }
10320
10321       if (n_args == alloced)
10322         {
10323           alloced *= 2;
10324
10325           if (arg_ary == fixed_args)
10326             {
10327               arg_ary = XNEWVEC (tree, alloced);
10328               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10329             }
10330           else
10331             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10332         }
10333       arg_ary[n_args++] = argument;
10334     }
10335   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10336
10337   vec = make_tree_vec (n_args);
10338
10339   while (n_args--)
10340     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10341
10342   if (arg_ary != fixed_args)
10343     free (arg_ary);
10344   parser->non_integral_constant_expression_p = saved_non_ice_p;
10345   parser->integral_constant_expression_p = saved_ice_p;
10346   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10347   return vec;
10348 }
10349
10350 /* Parse a template-argument.
10351
10352    template-argument:
10353      assignment-expression
10354      type-id
10355      id-expression
10356
10357    The representation is that of an assignment-expression, type-id, or
10358    id-expression -- except that the qualified id-expression is
10359    evaluated, so that the value returned is either a DECL or an
10360    OVERLOAD.
10361
10362    Although the standard says "assignment-expression", it forbids
10363    throw-expressions or assignments in the template argument.
10364    Therefore, we use "conditional-expression" instead.  */
10365
10366 static tree
10367 cp_parser_template_argument (cp_parser* parser)
10368 {
10369   tree argument;
10370   bool template_p;
10371   bool address_p;
10372   bool maybe_type_id = false;
10373   cp_token *token = NULL, *argument_start_token = NULL;
10374   cp_id_kind idk;
10375
10376   /* There's really no way to know what we're looking at, so we just
10377      try each alternative in order.
10378
10379        [temp.arg]
10380
10381        In a template-argument, an ambiguity between a type-id and an
10382        expression is resolved to a type-id, regardless of the form of
10383        the corresponding template-parameter.
10384
10385      Therefore, we try a type-id first.  */
10386   cp_parser_parse_tentatively (parser);
10387   argument = cp_parser_type_id (parser);
10388   /* If there was no error parsing the type-id but the next token is a
10389      '>>', our behavior depends on which dialect of C++ we're
10390      parsing. In C++98, we probably found a typo for '> >'. But there
10391      are type-id which are also valid expressions. For instance:
10392
10393      struct X { int operator >> (int); };
10394      template <int V> struct Foo {};
10395      Foo<X () >> 5> r;
10396
10397      Here 'X()' is a valid type-id of a function type, but the user just
10398      wanted to write the expression "X() >> 5". Thus, we remember that we
10399      found a valid type-id, but we still try to parse the argument as an
10400      expression to see what happens. 
10401
10402      In C++0x, the '>>' will be considered two separate '>'
10403      tokens.  */
10404   if (!cp_parser_error_occurred (parser)
10405       && cxx_dialect == cxx98
10406       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10407     {
10408       maybe_type_id = true;
10409       cp_parser_abort_tentative_parse (parser);
10410     }
10411   else
10412     {
10413       /* If the next token isn't a `,' or a `>', then this argument wasn't
10414       really finished. This means that the argument is not a valid
10415       type-id.  */
10416       if (!cp_parser_next_token_ends_template_argument_p (parser))
10417         cp_parser_error (parser, "expected template-argument");
10418       /* If that worked, we're done.  */
10419       if (cp_parser_parse_definitely (parser))
10420         return argument;
10421     }
10422   /* We're still not sure what the argument will be.  */
10423   cp_parser_parse_tentatively (parser);
10424   /* Try a template.  */
10425   argument_start_token = cp_lexer_peek_token (parser->lexer);
10426   argument = cp_parser_id_expression (parser,
10427                                       /*template_keyword_p=*/false,
10428                                       /*check_dependency_p=*/true,
10429                                       &template_p,
10430                                       /*declarator_p=*/false,
10431                                       /*optional_p=*/false);
10432   /* If the next token isn't a `,' or a `>', then this argument wasn't
10433      really finished.  */
10434   if (!cp_parser_next_token_ends_template_argument_p (parser))
10435     cp_parser_error (parser, "expected template-argument");
10436   if (!cp_parser_error_occurred (parser))
10437     {
10438       /* Figure out what is being referred to.  If the id-expression
10439          was for a class template specialization, then we will have a
10440          TYPE_DECL at this point.  There is no need to do name lookup
10441          at this point in that case.  */
10442       if (TREE_CODE (argument) != TYPE_DECL)
10443         argument = cp_parser_lookup_name (parser, argument,
10444                                           none_type,
10445                                           /*is_template=*/template_p,
10446                                           /*is_namespace=*/false,
10447                                           /*check_dependency=*/true,
10448                                           /*ambiguous_decls=*/NULL,
10449                                           argument_start_token->location);
10450       if (TREE_CODE (argument) != TEMPLATE_DECL
10451           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10452         cp_parser_error (parser, "expected template-name");
10453     }
10454   if (cp_parser_parse_definitely (parser))
10455     return argument;
10456   /* It must be a non-type argument.  There permitted cases are given
10457      in [temp.arg.nontype]:
10458
10459      -- an integral constant-expression of integral or enumeration
10460         type; or
10461
10462      -- the name of a non-type template-parameter; or
10463
10464      -- the name of an object or function with external linkage...
10465
10466      -- the address of an object or function with external linkage...
10467
10468      -- a pointer to member...  */
10469   /* Look for a non-type template parameter.  */
10470   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10471     {
10472       cp_parser_parse_tentatively (parser);
10473       argument = cp_parser_primary_expression (parser,
10474                                                /*address_p=*/false,
10475                                                /*cast_p=*/false,
10476                                                /*template_arg_p=*/true,
10477                                                &idk);
10478       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10479           || !cp_parser_next_token_ends_template_argument_p (parser))
10480         cp_parser_simulate_error (parser);
10481       if (cp_parser_parse_definitely (parser))
10482         return argument;
10483     }
10484
10485   /* If the next token is "&", the argument must be the address of an
10486      object or function with external linkage.  */
10487   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10488   if (address_p)
10489     cp_lexer_consume_token (parser->lexer);
10490   /* See if we might have an id-expression.  */
10491   token = cp_lexer_peek_token (parser->lexer);
10492   if (token->type == CPP_NAME
10493       || token->keyword == RID_OPERATOR
10494       || token->type == CPP_SCOPE
10495       || token->type == CPP_TEMPLATE_ID
10496       || token->type == CPP_NESTED_NAME_SPECIFIER)
10497     {
10498       cp_parser_parse_tentatively (parser);
10499       argument = cp_parser_primary_expression (parser,
10500                                                address_p,
10501                                                /*cast_p=*/false,
10502                                                /*template_arg_p=*/true,
10503                                                &idk);
10504       if (cp_parser_error_occurred (parser)
10505           || !cp_parser_next_token_ends_template_argument_p (parser))
10506         cp_parser_abort_tentative_parse (parser);
10507       else
10508         {
10509           if (TREE_CODE (argument) == INDIRECT_REF)
10510             {
10511               gcc_assert (REFERENCE_REF_P (argument));
10512               argument = TREE_OPERAND (argument, 0);
10513             }
10514
10515           if (TREE_CODE (argument) == VAR_DECL)
10516             {
10517               /* A variable without external linkage might still be a
10518                  valid constant-expression, so no error is issued here
10519                  if the external-linkage check fails.  */
10520               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10521                 cp_parser_simulate_error (parser);
10522             }
10523           else if (is_overloaded_fn (argument))
10524             /* All overloaded functions are allowed; if the external
10525                linkage test does not pass, an error will be issued
10526                later.  */
10527             ;
10528           else if (address_p
10529                    && (TREE_CODE (argument) == OFFSET_REF
10530                        || TREE_CODE (argument) == SCOPE_REF))
10531             /* A pointer-to-member.  */
10532             ;
10533           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10534             ;
10535           else
10536             cp_parser_simulate_error (parser);
10537
10538           if (cp_parser_parse_definitely (parser))
10539             {
10540               if (address_p)
10541                 argument = build_x_unary_op (ADDR_EXPR, argument,
10542                                              tf_warning_or_error);
10543               return argument;
10544             }
10545         }
10546     }
10547   /* If the argument started with "&", there are no other valid
10548      alternatives at this point.  */
10549   if (address_p)
10550     {
10551       cp_parser_error (parser, "invalid non-type template argument");
10552       return error_mark_node;
10553     }
10554
10555   /* If the argument wasn't successfully parsed as a type-id followed
10556      by '>>', the argument can only be a constant expression now.
10557      Otherwise, we try parsing the constant-expression tentatively,
10558      because the argument could really be a type-id.  */
10559   if (maybe_type_id)
10560     cp_parser_parse_tentatively (parser);
10561   argument = cp_parser_constant_expression (parser,
10562                                             /*allow_non_constant_p=*/false,
10563                                             /*non_constant_p=*/NULL);
10564   argument = fold_non_dependent_expr (argument);
10565   if (!maybe_type_id)
10566     return argument;
10567   if (!cp_parser_next_token_ends_template_argument_p (parser))
10568     cp_parser_error (parser, "expected template-argument");
10569   if (cp_parser_parse_definitely (parser))
10570     return argument;
10571   /* We did our best to parse the argument as a non type-id, but that
10572      was the only alternative that matched (albeit with a '>' after
10573      it). We can assume it's just a typo from the user, and a
10574      diagnostic will then be issued.  */
10575   return cp_parser_type_id (parser);
10576 }
10577
10578 /* Parse an explicit-instantiation.
10579
10580    explicit-instantiation:
10581      template declaration
10582
10583    Although the standard says `declaration', what it really means is:
10584
10585    explicit-instantiation:
10586      template decl-specifier-seq [opt] declarator [opt] ;
10587
10588    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10589    supposed to be allowed.  A defect report has been filed about this
10590    issue.
10591
10592    GNU Extension:
10593
10594    explicit-instantiation:
10595      storage-class-specifier template
10596        decl-specifier-seq [opt] declarator [opt] ;
10597      function-specifier template
10598        decl-specifier-seq [opt] declarator [opt] ;  */
10599
10600 static void
10601 cp_parser_explicit_instantiation (cp_parser* parser)
10602 {
10603   int declares_class_or_enum;
10604   cp_decl_specifier_seq decl_specifiers;
10605   tree extension_specifier = NULL_TREE;
10606   cp_token *token;
10607
10608   /* Look for an (optional) storage-class-specifier or
10609      function-specifier.  */
10610   if (cp_parser_allow_gnu_extensions_p (parser))
10611     {
10612       extension_specifier
10613         = cp_parser_storage_class_specifier_opt (parser);
10614       if (!extension_specifier)
10615         extension_specifier
10616           = cp_parser_function_specifier_opt (parser,
10617                                               /*decl_specs=*/NULL);
10618     }
10619
10620   /* Look for the `template' keyword.  */
10621   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10622   /* Let the front end know that we are processing an explicit
10623      instantiation.  */
10624   begin_explicit_instantiation ();
10625   /* [temp.explicit] says that we are supposed to ignore access
10626      control while processing explicit instantiation directives.  */
10627   push_deferring_access_checks (dk_no_check);
10628   /* Parse a decl-specifier-seq.  */
10629   token = cp_lexer_peek_token (parser->lexer);
10630   cp_parser_decl_specifier_seq (parser,
10631                                 CP_PARSER_FLAGS_OPTIONAL,
10632                                 &decl_specifiers,
10633                                 &declares_class_or_enum);
10634   /* If there was exactly one decl-specifier, and it declared a class,
10635      and there's no declarator, then we have an explicit type
10636      instantiation.  */
10637   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10638     {
10639       tree type;
10640
10641       type = check_tag_decl (&decl_specifiers);
10642       /* Turn access control back on for names used during
10643          template instantiation.  */
10644       pop_deferring_access_checks ();
10645       if (type)
10646         do_type_instantiation (type, extension_specifier,
10647                                /*complain=*/tf_error);
10648     }
10649   else
10650     {
10651       cp_declarator *declarator;
10652       tree decl;
10653
10654       /* Parse the declarator.  */
10655       declarator
10656         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10657                                 /*ctor_dtor_or_conv_p=*/NULL,
10658                                 /*parenthesized_p=*/NULL,
10659                                 /*member_p=*/false);
10660       if (declares_class_or_enum & 2)
10661         cp_parser_check_for_definition_in_return_type (declarator,
10662                                                        decl_specifiers.type,
10663                                                        decl_specifiers.type_location);
10664       if (declarator != cp_error_declarator)
10665         {
10666           decl = grokdeclarator (declarator, &decl_specifiers,
10667                                  NORMAL, 0, &decl_specifiers.attributes);
10668           /* Turn access control back on for names used during
10669              template instantiation.  */
10670           pop_deferring_access_checks ();
10671           /* Do the explicit instantiation.  */
10672           do_decl_instantiation (decl, extension_specifier);
10673         }
10674       else
10675         {
10676           pop_deferring_access_checks ();
10677           /* Skip the body of the explicit instantiation.  */
10678           cp_parser_skip_to_end_of_statement (parser);
10679         }
10680     }
10681   /* We're done with the instantiation.  */
10682   end_explicit_instantiation ();
10683
10684   cp_parser_consume_semicolon_at_end_of_statement (parser);
10685 }
10686
10687 /* Parse an explicit-specialization.
10688
10689    explicit-specialization:
10690      template < > declaration
10691
10692    Although the standard says `declaration', what it really means is:
10693
10694    explicit-specialization:
10695      template <> decl-specifier [opt] init-declarator [opt] ;
10696      template <> function-definition
10697      template <> explicit-specialization
10698      template <> template-declaration  */
10699
10700 static void
10701 cp_parser_explicit_specialization (cp_parser* parser)
10702 {
10703   bool need_lang_pop;
10704   cp_token *token = cp_lexer_peek_token (parser->lexer);
10705
10706   /* Look for the `template' keyword.  */
10707   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10708   /* Look for the `<'.  */
10709   cp_parser_require (parser, CPP_LESS, "%<<%>");
10710   /* Look for the `>'.  */
10711   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10712   /* We have processed another parameter list.  */
10713   ++parser->num_template_parameter_lists;
10714   /* [temp]
10715
10716      A template ... explicit specialization ... shall not have C
10717      linkage.  */
10718   if (current_lang_name == lang_name_c)
10719     {
10720       error ("%Htemplate specialization with C linkage", &token->location);
10721       /* Give it C++ linkage to avoid confusing other parts of the
10722          front end.  */
10723       push_lang_context (lang_name_cplusplus);
10724       need_lang_pop = true;
10725     }
10726   else
10727     need_lang_pop = false;
10728   /* Let the front end know that we are beginning a specialization.  */
10729   if (!begin_specialization ())
10730     {
10731       end_specialization ();
10732       cp_parser_skip_to_end_of_block_or_statement (parser);
10733       return;
10734     }
10735
10736   /* If the next keyword is `template', we need to figure out whether
10737      or not we're looking a template-declaration.  */
10738   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10739     {
10740       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10741           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10742         cp_parser_template_declaration_after_export (parser,
10743                                                      /*member_p=*/false);
10744       else
10745         cp_parser_explicit_specialization (parser);
10746     }
10747   else
10748     /* Parse the dependent declaration.  */
10749     cp_parser_single_declaration (parser,
10750                                   /*checks=*/NULL,
10751                                   /*member_p=*/false,
10752                                   /*explicit_specialization_p=*/true,
10753                                   /*friend_p=*/NULL);
10754   /* We're done with the specialization.  */
10755   end_specialization ();
10756   /* For the erroneous case of a template with C linkage, we pushed an
10757      implicit C++ linkage scope; exit that scope now.  */
10758   if (need_lang_pop)
10759     pop_lang_context ();
10760   /* We're done with this parameter list.  */
10761   --parser->num_template_parameter_lists;
10762 }
10763
10764 /* Parse a type-specifier.
10765
10766    type-specifier:
10767      simple-type-specifier
10768      class-specifier
10769      enum-specifier
10770      elaborated-type-specifier
10771      cv-qualifier
10772
10773    GNU Extension:
10774
10775    type-specifier:
10776      __complex__
10777
10778    Returns a representation of the type-specifier.  For a
10779    class-specifier, enum-specifier, or elaborated-type-specifier, a
10780    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10781
10782    The parser flags FLAGS is used to control type-specifier parsing.
10783
10784    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10785    in a decl-specifier-seq.
10786
10787    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10788    class-specifier, enum-specifier, or elaborated-type-specifier, then
10789    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10790    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10791    zero.
10792
10793    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10794    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10795    is set to FALSE.  */
10796
10797 static tree
10798 cp_parser_type_specifier (cp_parser* parser,
10799                           cp_parser_flags flags,
10800                           cp_decl_specifier_seq *decl_specs,
10801                           bool is_declaration,
10802                           int* declares_class_or_enum,
10803                           bool* is_cv_qualifier)
10804 {
10805   tree type_spec = NULL_TREE;
10806   cp_token *token;
10807   enum rid keyword;
10808   cp_decl_spec ds = ds_last;
10809
10810   /* Assume this type-specifier does not declare a new type.  */
10811   if (declares_class_or_enum)
10812     *declares_class_or_enum = 0;
10813   /* And that it does not specify a cv-qualifier.  */
10814   if (is_cv_qualifier)
10815     *is_cv_qualifier = false;
10816   /* Peek at the next token.  */
10817   token = cp_lexer_peek_token (parser->lexer);
10818
10819   /* If we're looking at a keyword, we can use that to guide the
10820      production we choose.  */
10821   keyword = token->keyword;
10822   switch (keyword)
10823     {
10824     case RID_ENUM:
10825       /* Look for the enum-specifier.  */
10826       type_spec = cp_parser_enum_specifier (parser);
10827       /* If that worked, we're done.  */
10828       if (type_spec)
10829         {
10830           if (declares_class_or_enum)
10831             *declares_class_or_enum = 2;
10832           if (decl_specs)
10833             cp_parser_set_decl_spec_type (decl_specs,
10834                                           type_spec,
10835                                           token->location,
10836                                           /*user_defined_p=*/true);
10837           return type_spec;
10838         }
10839       else
10840         goto elaborated_type_specifier;
10841
10842       /* Any of these indicate either a class-specifier, or an
10843          elaborated-type-specifier.  */
10844     case RID_CLASS:
10845     case RID_STRUCT:
10846     case RID_UNION:
10847       /* Parse tentatively so that we can back up if we don't find a
10848          class-specifier.  */
10849       cp_parser_parse_tentatively (parser);
10850       /* Look for the class-specifier.  */
10851       type_spec = cp_parser_class_specifier (parser);
10852       /* If that worked, we're done.  */
10853       if (cp_parser_parse_definitely (parser))
10854         {
10855           if (declares_class_or_enum)
10856             *declares_class_or_enum = 2;
10857           if (decl_specs)
10858             cp_parser_set_decl_spec_type (decl_specs,
10859                                           type_spec,
10860                                           token->location,
10861                                           /*user_defined_p=*/true);
10862           return type_spec;
10863         }
10864
10865       /* Fall through.  */
10866     elaborated_type_specifier:
10867       /* We're declaring (not defining) a class or enum.  */
10868       if (declares_class_or_enum)
10869         *declares_class_or_enum = 1;
10870
10871       /* Fall through.  */
10872     case RID_TYPENAME:
10873       /* Look for an elaborated-type-specifier.  */
10874       type_spec
10875         = (cp_parser_elaborated_type_specifier
10876            (parser,
10877             decl_specs && decl_specs->specs[(int) ds_friend],
10878             is_declaration));
10879       if (decl_specs)
10880         cp_parser_set_decl_spec_type (decl_specs,
10881                                       type_spec,
10882                                       token->location,
10883                                       /*user_defined_p=*/true);
10884       return type_spec;
10885
10886     case RID_CONST:
10887       ds = ds_const;
10888       if (is_cv_qualifier)
10889         *is_cv_qualifier = true;
10890       break;
10891
10892     case RID_VOLATILE:
10893       ds = ds_volatile;
10894       if (is_cv_qualifier)
10895         *is_cv_qualifier = true;
10896       break;
10897
10898     case RID_RESTRICT:
10899       ds = ds_restrict;
10900       if (is_cv_qualifier)
10901         *is_cv_qualifier = true;
10902       break;
10903
10904     case RID_COMPLEX:
10905       /* The `__complex__' keyword is a GNU extension.  */
10906       ds = ds_complex;
10907       break;
10908
10909     default:
10910       break;
10911     }
10912
10913   /* Handle simple keywords.  */
10914   if (ds != ds_last)
10915     {
10916       if (decl_specs)
10917         {
10918           ++decl_specs->specs[(int)ds];
10919           decl_specs->any_specifiers_p = true;
10920         }
10921       return cp_lexer_consume_token (parser->lexer)->u.value;
10922     }
10923
10924   /* If we do not already have a type-specifier, assume we are looking
10925      at a simple-type-specifier.  */
10926   type_spec = cp_parser_simple_type_specifier (parser,
10927                                                decl_specs,
10928                                                flags);
10929
10930   /* If we didn't find a type-specifier, and a type-specifier was not
10931      optional in this context, issue an error message.  */
10932   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10933     {
10934       cp_parser_error (parser, "expected type specifier");
10935       return error_mark_node;
10936     }
10937
10938   return type_spec;
10939 }
10940
10941 /* Parse a simple-type-specifier.
10942
10943    simple-type-specifier:
10944      :: [opt] nested-name-specifier [opt] type-name
10945      :: [opt] nested-name-specifier template template-id
10946      char
10947      wchar_t
10948      bool
10949      short
10950      int
10951      long
10952      signed
10953      unsigned
10954      float
10955      double
10956      void
10957
10958    C++0x Extension:
10959
10960    simple-type-specifier:
10961      auto
10962      decltype ( expression )   
10963      char16_t
10964      char32_t
10965
10966    GNU Extension:
10967
10968    simple-type-specifier:
10969      __typeof__ unary-expression
10970      __typeof__ ( type-id )
10971
10972    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10973    appropriately updated.  */
10974
10975 static tree
10976 cp_parser_simple_type_specifier (cp_parser* parser,
10977                                  cp_decl_specifier_seq *decl_specs,
10978                                  cp_parser_flags flags)
10979 {
10980   tree type = NULL_TREE;
10981   cp_token *token;
10982
10983   /* Peek at the next token.  */
10984   token = cp_lexer_peek_token (parser->lexer);
10985
10986   /* If we're looking at a keyword, things are easy.  */
10987   switch (token->keyword)
10988     {
10989     case RID_CHAR:
10990       if (decl_specs)
10991         decl_specs->explicit_char_p = true;
10992       type = char_type_node;
10993       break;
10994     case RID_CHAR16:
10995       type = char16_type_node;
10996       break;
10997     case RID_CHAR32:
10998       type = char32_type_node;
10999       break;
11000     case RID_WCHAR:
11001       type = wchar_type_node;
11002       break;
11003     case RID_BOOL:
11004       type = boolean_type_node;
11005       break;
11006     case RID_SHORT:
11007       if (decl_specs)
11008         ++decl_specs->specs[(int) ds_short];
11009       type = short_integer_type_node;
11010       break;
11011     case RID_INT:
11012       if (decl_specs)
11013         decl_specs->explicit_int_p = true;
11014       type = integer_type_node;
11015       break;
11016     case RID_LONG:
11017       if (decl_specs)
11018         ++decl_specs->specs[(int) ds_long];
11019       type = long_integer_type_node;
11020       break;
11021     case RID_SIGNED:
11022       if (decl_specs)
11023         ++decl_specs->specs[(int) ds_signed];
11024       type = integer_type_node;
11025       break;
11026     case RID_UNSIGNED:
11027       if (decl_specs)
11028         ++decl_specs->specs[(int) ds_unsigned];
11029       type = unsigned_type_node;
11030       break;
11031     case RID_FLOAT:
11032       type = float_type_node;
11033       break;
11034     case RID_DOUBLE:
11035       type = double_type_node;
11036       break;
11037     case RID_VOID:
11038       type = void_type_node;
11039       break;
11040       
11041     case RID_AUTO:
11042       if (cxx_dialect != cxx98)
11043         {
11044           /* Consume the token.  */
11045           cp_lexer_consume_token (parser->lexer);
11046           /* We do not yet support the use of `auto' as a
11047              type-specifier.  */
11048           error ("%HC++0x %<auto%> specifier not supported", &token->location);
11049         }
11050       break;
11051
11052     case RID_DECLTYPE:
11053       /* Parse the `decltype' type.  */
11054       type = cp_parser_decltype (parser);
11055
11056       if (decl_specs)
11057         cp_parser_set_decl_spec_type (decl_specs, type,
11058                                       token->location,
11059                                       /*user_defined_p=*/true);
11060
11061       return type;
11062
11063     case RID_TYPEOF:
11064       /* Consume the `typeof' token.  */
11065       cp_lexer_consume_token (parser->lexer);
11066       /* Parse the operand to `typeof'.  */
11067       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11068       /* If it is not already a TYPE, take its type.  */
11069       if (!TYPE_P (type))
11070         type = finish_typeof (type);
11071
11072       if (decl_specs)
11073         cp_parser_set_decl_spec_type (decl_specs, type,
11074                                       token->location,
11075                                       /*user_defined_p=*/true);
11076
11077       return type;
11078
11079     default:
11080       break;
11081     }
11082
11083   /* If the type-specifier was for a built-in type, we're done.  */
11084   if (type)
11085     {
11086       tree id;
11087
11088       /* Record the type.  */
11089       if (decl_specs
11090           && (token->keyword != RID_SIGNED
11091               && token->keyword != RID_UNSIGNED
11092               && token->keyword != RID_SHORT
11093               && token->keyword != RID_LONG))
11094         cp_parser_set_decl_spec_type (decl_specs,
11095                                       type,
11096                                       token->location,
11097                                       /*user_defined=*/false);
11098       if (decl_specs)
11099         decl_specs->any_specifiers_p = true;
11100
11101       /* Consume the token.  */
11102       id = cp_lexer_consume_token (parser->lexer)->u.value;
11103
11104       /* There is no valid C++ program where a non-template type is
11105          followed by a "<".  That usually indicates that the user thought
11106          that the type was a template.  */
11107       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11108
11109       return TYPE_NAME (type);
11110     }
11111
11112   /* The type-specifier must be a user-defined type.  */
11113   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11114     {
11115       bool qualified_p;
11116       bool global_p;
11117
11118       /* Don't gobble tokens or issue error messages if this is an
11119          optional type-specifier.  */
11120       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11121         cp_parser_parse_tentatively (parser);
11122
11123       /* Look for the optional `::' operator.  */
11124       global_p
11125         = (cp_parser_global_scope_opt (parser,
11126                                        /*current_scope_valid_p=*/false)
11127            != NULL_TREE);
11128       /* Look for the nested-name specifier.  */
11129       qualified_p
11130         = (cp_parser_nested_name_specifier_opt (parser,
11131                                                 /*typename_keyword_p=*/false,
11132                                                 /*check_dependency_p=*/true,
11133                                                 /*type_p=*/false,
11134                                                 /*is_declaration=*/false)
11135            != NULL_TREE);
11136       token = cp_lexer_peek_token (parser->lexer);
11137       /* If we have seen a nested-name-specifier, and the next token
11138          is `template', then we are using the template-id production.  */
11139       if (parser->scope
11140           && cp_parser_optional_template_keyword (parser))
11141         {
11142           /* Look for the template-id.  */
11143           type = cp_parser_template_id (parser,
11144                                         /*template_keyword_p=*/true,
11145                                         /*check_dependency_p=*/true,
11146                                         /*is_declaration=*/false);
11147           /* If the template-id did not name a type, we are out of
11148              luck.  */
11149           if (TREE_CODE (type) != TYPE_DECL)
11150             {
11151               cp_parser_error (parser, "expected template-id for type");
11152               type = NULL_TREE;
11153             }
11154         }
11155       /* Otherwise, look for a type-name.  */
11156       else
11157         type = cp_parser_type_name (parser);
11158       /* Keep track of all name-lookups performed in class scopes.  */
11159       if (type
11160           && !global_p
11161           && !qualified_p
11162           && TREE_CODE (type) == TYPE_DECL
11163           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11164         maybe_note_name_used_in_class (DECL_NAME (type), type);
11165       /* If it didn't work out, we don't have a TYPE.  */
11166       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11167           && !cp_parser_parse_definitely (parser))
11168         type = NULL_TREE;
11169       if (type && decl_specs)
11170         cp_parser_set_decl_spec_type (decl_specs, type,
11171                                       token->location,
11172                                       /*user_defined=*/true);
11173     }
11174
11175   /* If we didn't get a type-name, issue an error message.  */
11176   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11177     {
11178       cp_parser_error (parser, "expected type-name");
11179       return error_mark_node;
11180     }
11181
11182   /* There is no valid C++ program where a non-template type is
11183      followed by a "<".  That usually indicates that the user thought
11184      that the type was a template.  */
11185   if (type && type != error_mark_node)
11186     {
11187       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11188          If it is, then the '<'...'>' enclose protocol names rather than
11189          template arguments, and so everything is fine.  */
11190       if (c_dialect_objc ()
11191           && (objc_is_id (type) || objc_is_class_name (type)))
11192         {
11193           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11194           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11195
11196           /* Clobber the "unqualified" type previously entered into
11197              DECL_SPECS with the new, improved protocol-qualified version.  */
11198           if (decl_specs)
11199             decl_specs->type = qual_type;
11200
11201           return qual_type;
11202         }
11203
11204       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11205                                                token->location);
11206     }
11207
11208   return type;
11209 }
11210
11211 /* Parse a type-name.
11212
11213    type-name:
11214      class-name
11215      enum-name
11216      typedef-name
11217
11218    enum-name:
11219      identifier
11220
11221    typedef-name:
11222      identifier
11223
11224    Returns a TYPE_DECL for the type.  */
11225
11226 static tree
11227 cp_parser_type_name (cp_parser* parser)
11228 {
11229   tree type_decl;
11230
11231   /* We can't know yet whether it is a class-name or not.  */
11232   cp_parser_parse_tentatively (parser);
11233   /* Try a class-name.  */
11234   type_decl = cp_parser_class_name (parser,
11235                                     /*typename_keyword_p=*/false,
11236                                     /*template_keyword_p=*/false,
11237                                     none_type,
11238                                     /*check_dependency_p=*/true,
11239                                     /*class_head_p=*/false,
11240                                     /*is_declaration=*/false);
11241   /* If it's not a class-name, keep looking.  */
11242   if (!cp_parser_parse_definitely (parser))
11243     {
11244       /* It must be a typedef-name or an enum-name.  */
11245       return cp_parser_nonclass_name (parser);
11246     }
11247
11248   return type_decl;
11249 }
11250
11251 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11252
11253    enum-name:
11254      identifier
11255
11256    typedef-name:
11257      identifier
11258
11259    Returns a TYPE_DECL for the type.  */
11260
11261 static tree
11262 cp_parser_nonclass_name (cp_parser* parser)
11263 {
11264   tree type_decl;
11265   tree identifier;
11266
11267   cp_token *token = cp_lexer_peek_token (parser->lexer);
11268   identifier = cp_parser_identifier (parser);
11269   if (identifier == error_mark_node)
11270     return error_mark_node;
11271
11272   /* Look up the type-name.  */
11273   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11274
11275   if (TREE_CODE (type_decl) != TYPE_DECL
11276       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11277     {
11278       /* See if this is an Objective-C type.  */
11279       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11280       tree type = objc_get_protocol_qualified_type (identifier, protos);
11281       if (type)
11282         type_decl = TYPE_NAME (type);
11283     }
11284   
11285   /* Issue an error if we did not find a type-name.  */
11286   if (TREE_CODE (type_decl) != TYPE_DECL)
11287     {
11288       if (!cp_parser_simulate_error (parser))
11289         cp_parser_name_lookup_error (parser, identifier, type_decl,
11290                                      "is not a type", token->location);
11291       return error_mark_node;
11292     }
11293   /* Remember that the name was used in the definition of the
11294      current class so that we can check later to see if the
11295      meaning would have been different after the class was
11296      entirely defined.  */
11297   else if (type_decl != error_mark_node
11298            && !parser->scope)
11299     maybe_note_name_used_in_class (identifier, type_decl);
11300   
11301   return type_decl;
11302 }
11303
11304 /* Parse an elaborated-type-specifier.  Note that the grammar given
11305    here incorporates the resolution to DR68.
11306
11307    elaborated-type-specifier:
11308      class-key :: [opt] nested-name-specifier [opt] identifier
11309      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11310      enum :: [opt] nested-name-specifier [opt] identifier
11311      typename :: [opt] nested-name-specifier identifier
11312      typename :: [opt] nested-name-specifier template [opt]
11313        template-id
11314
11315    GNU extension:
11316
11317    elaborated-type-specifier:
11318      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11319      class-key attributes :: [opt] nested-name-specifier [opt]
11320                template [opt] template-id
11321      enum attributes :: [opt] nested-name-specifier [opt] identifier
11322
11323    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11324    declared `friend'.  If IS_DECLARATION is TRUE, then this
11325    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11326    something is being declared.
11327
11328    Returns the TYPE specified.  */
11329
11330 static tree
11331 cp_parser_elaborated_type_specifier (cp_parser* parser,
11332                                      bool is_friend,
11333                                      bool is_declaration)
11334 {
11335   enum tag_types tag_type;
11336   tree identifier;
11337   tree type = NULL_TREE;
11338   tree attributes = NULL_TREE;
11339   cp_token *token = NULL;
11340
11341   /* See if we're looking at the `enum' keyword.  */
11342   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11343     {
11344       /* Consume the `enum' token.  */
11345       cp_lexer_consume_token (parser->lexer);
11346       /* Remember that it's an enumeration type.  */
11347       tag_type = enum_type;
11348       /* Parse the attributes.  */
11349       attributes = cp_parser_attributes_opt (parser);
11350     }
11351   /* Or, it might be `typename'.  */
11352   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11353                                            RID_TYPENAME))
11354     {
11355       /* Consume the `typename' token.  */
11356       cp_lexer_consume_token (parser->lexer);
11357       /* Remember that it's a `typename' type.  */
11358       tag_type = typename_type;
11359       /* The `typename' keyword is only allowed in templates.  */
11360       if (!processing_template_decl)
11361         permerror (input_location, "using %<typename%> outside of template");
11362     }
11363   /* Otherwise it must be a class-key.  */
11364   else
11365     {
11366       tag_type = cp_parser_class_key (parser);
11367       if (tag_type == none_type)
11368         return error_mark_node;
11369       /* Parse the attributes.  */
11370       attributes = cp_parser_attributes_opt (parser);
11371     }
11372
11373   /* Look for the `::' operator.  */
11374   cp_parser_global_scope_opt (parser,
11375                               /*current_scope_valid_p=*/false);
11376   /* Look for the nested-name-specifier.  */
11377   if (tag_type == typename_type)
11378     {
11379       if (!cp_parser_nested_name_specifier (parser,
11380                                            /*typename_keyword_p=*/true,
11381                                            /*check_dependency_p=*/true,
11382                                            /*type_p=*/true,
11383                                             is_declaration))
11384         return error_mark_node;
11385     }
11386   else
11387     /* Even though `typename' is not present, the proposed resolution
11388        to Core Issue 180 says that in `class A<T>::B', `B' should be
11389        considered a type-name, even if `A<T>' is dependent.  */
11390     cp_parser_nested_name_specifier_opt (parser,
11391                                          /*typename_keyword_p=*/true,
11392                                          /*check_dependency_p=*/true,
11393                                          /*type_p=*/true,
11394                                          is_declaration);
11395  /* For everything but enumeration types, consider a template-id.
11396     For an enumeration type, consider only a plain identifier.  */
11397   if (tag_type != enum_type)
11398     {
11399       bool template_p = false;
11400       tree decl;
11401
11402       /* Allow the `template' keyword.  */
11403       template_p = cp_parser_optional_template_keyword (parser);
11404       /* If we didn't see `template', we don't know if there's a
11405          template-id or not.  */
11406       if (!template_p)
11407         cp_parser_parse_tentatively (parser);
11408       /* Parse the template-id.  */
11409       token = cp_lexer_peek_token (parser->lexer);
11410       decl = cp_parser_template_id (parser, template_p,
11411                                     /*check_dependency_p=*/true,
11412                                     is_declaration);
11413       /* If we didn't find a template-id, look for an ordinary
11414          identifier.  */
11415       if (!template_p && !cp_parser_parse_definitely (parser))
11416         ;
11417       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11418          in effect, then we must assume that, upon instantiation, the
11419          template will correspond to a class.  */
11420       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11421                && tag_type == typename_type)
11422         type = make_typename_type (parser->scope, decl,
11423                                    typename_type,
11424                                    /*complain=*/tf_error);
11425       else
11426         type = TREE_TYPE (decl);
11427     }
11428
11429   if (!type)
11430     {
11431       token = cp_lexer_peek_token (parser->lexer);
11432       identifier = cp_parser_identifier (parser);
11433
11434       if (identifier == error_mark_node)
11435         {
11436           parser->scope = NULL_TREE;
11437           return error_mark_node;
11438         }
11439
11440       /* For a `typename', we needn't call xref_tag.  */
11441       if (tag_type == typename_type
11442           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11443         return cp_parser_make_typename_type (parser, parser->scope,
11444                                              identifier,
11445                                              token->location);
11446       /* Look up a qualified name in the usual way.  */
11447       if (parser->scope)
11448         {
11449           tree decl;
11450           tree ambiguous_decls;
11451
11452           decl = cp_parser_lookup_name (parser, identifier,
11453                                         tag_type,
11454                                         /*is_template=*/false,
11455                                         /*is_namespace=*/false,
11456                                         /*check_dependency=*/true,
11457                                         &ambiguous_decls,
11458                                         token->location);
11459
11460           /* If the lookup was ambiguous, an error will already have been
11461              issued.  */
11462           if (ambiguous_decls)
11463             return error_mark_node;
11464
11465           /* If we are parsing friend declaration, DECL may be a
11466              TEMPLATE_DECL tree node here.  However, we need to check
11467              whether this TEMPLATE_DECL results in valid code.  Consider
11468              the following example:
11469
11470                namespace N {
11471                  template <class T> class C {};
11472                }
11473                class X {
11474                  template <class T> friend class N::C; // #1, valid code
11475                };
11476                template <class T> class Y {
11477                  friend class N::C;                    // #2, invalid code
11478                };
11479
11480              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11481              name lookup of `N::C'.  We see that friend declaration must
11482              be template for the code to be valid.  Note that
11483              processing_template_decl does not work here since it is
11484              always 1 for the above two cases.  */
11485
11486           decl = (cp_parser_maybe_treat_template_as_class
11487                   (decl, /*tag_name_p=*/is_friend
11488                          && parser->num_template_parameter_lists));
11489
11490           if (TREE_CODE (decl) != TYPE_DECL)
11491             {
11492               cp_parser_diagnose_invalid_type_name (parser,
11493                                                     parser->scope,
11494                                                     identifier,
11495                                                     token->location);
11496               return error_mark_node;
11497             }
11498
11499           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11500             {
11501               bool allow_template = (parser->num_template_parameter_lists
11502                                       || DECL_SELF_REFERENCE_P (decl));
11503               type = check_elaborated_type_specifier (tag_type, decl, 
11504                                                       allow_template);
11505
11506               if (type == error_mark_node)
11507                 return error_mark_node;
11508             }
11509
11510           /* Forward declarations of nested types, such as
11511
11512                class C1::C2;
11513                class C1::C2::C3;
11514
11515              are invalid unless all components preceding the final '::'
11516              are complete.  If all enclosing types are complete, these
11517              declarations become merely pointless.
11518
11519              Invalid forward declarations of nested types are errors
11520              caught elsewhere in parsing.  Those that are pointless arrive
11521              here.  */
11522
11523           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11524               && !is_friend && !processing_explicit_instantiation)
11525             warning (0, "declaration %qD does not declare anything", decl);
11526
11527           type = TREE_TYPE (decl);
11528         }
11529       else
11530         {
11531           /* An elaborated-type-specifier sometimes introduces a new type and
11532              sometimes names an existing type.  Normally, the rule is that it
11533              introduces a new type only if there is not an existing type of
11534              the same name already in scope.  For example, given:
11535
11536                struct S {};
11537                void f() { struct S s; }
11538
11539              the `struct S' in the body of `f' is the same `struct S' as in
11540              the global scope; the existing definition is used.  However, if
11541              there were no global declaration, this would introduce a new
11542              local class named `S'.
11543
11544              An exception to this rule applies to the following code:
11545
11546                namespace N { struct S; }
11547
11548              Here, the elaborated-type-specifier names a new type
11549              unconditionally; even if there is already an `S' in the
11550              containing scope this declaration names a new type.
11551              This exception only applies if the elaborated-type-specifier
11552              forms the complete declaration:
11553
11554                [class.name]
11555
11556                A declaration consisting solely of `class-key identifier ;' is
11557                either a redeclaration of the name in the current scope or a
11558                forward declaration of the identifier as a class name.  It
11559                introduces the name into the current scope.
11560
11561              We are in this situation precisely when the next token is a `;'.
11562
11563              An exception to the exception is that a `friend' declaration does
11564              *not* name a new type; i.e., given:
11565
11566                struct S { friend struct T; };
11567
11568              `T' is not a new type in the scope of `S'.
11569
11570              Also, `new struct S' or `sizeof (struct S)' never results in the
11571              definition of a new type; a new type can only be declared in a
11572              declaration context.  */
11573
11574           tag_scope ts;
11575           bool template_p;
11576
11577           if (is_friend)
11578             /* Friends have special name lookup rules.  */
11579             ts = ts_within_enclosing_non_class;
11580           else if (is_declaration
11581                    && cp_lexer_next_token_is (parser->lexer,
11582                                               CPP_SEMICOLON))
11583             /* This is a `class-key identifier ;' */
11584             ts = ts_current;
11585           else
11586             ts = ts_global;
11587
11588           template_p =
11589             (parser->num_template_parameter_lists
11590              && (cp_parser_next_token_starts_class_definition_p (parser)
11591                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11592           /* An unqualified name was used to reference this type, so
11593              there were no qualifying templates.  */
11594           if (!cp_parser_check_template_parameters (parser,
11595                                                     /*num_templates=*/0,
11596                                                     token->location))
11597             return error_mark_node;
11598           type = xref_tag (tag_type, identifier, ts, template_p);
11599         }
11600     }
11601
11602   if (type == error_mark_node)
11603     return error_mark_node;
11604
11605   /* Allow attributes on forward declarations of classes.  */
11606   if (attributes)
11607     {
11608       if (TREE_CODE (type) == TYPENAME_TYPE)
11609         warning (OPT_Wattributes,
11610                  "attributes ignored on uninstantiated type");
11611       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11612                && ! processing_explicit_instantiation)
11613         warning (OPT_Wattributes,
11614                  "attributes ignored on template instantiation");
11615       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11616         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11617       else
11618         warning (OPT_Wattributes,
11619                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11620     }
11621
11622   if (tag_type != enum_type)
11623     cp_parser_check_class_key (tag_type, type);
11624
11625   /* A "<" cannot follow an elaborated type specifier.  If that
11626      happens, the user was probably trying to form a template-id.  */
11627   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11628
11629   return type;
11630 }
11631
11632 /* Parse an enum-specifier.
11633
11634    enum-specifier:
11635      enum identifier [opt] { enumerator-list [opt] }
11636
11637    GNU Extensions:
11638      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11639        attributes[opt]
11640
11641    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11642    if the token stream isn't an enum-specifier after all.  */
11643
11644 static tree
11645 cp_parser_enum_specifier (cp_parser* parser)
11646 {
11647   tree identifier;
11648   tree type;
11649   tree attributes;
11650
11651   /* Parse tentatively so that we can back up if we don't find a
11652      enum-specifier.  */
11653   cp_parser_parse_tentatively (parser);
11654
11655   /* Caller guarantees that the current token is 'enum', an identifier
11656      possibly follows, and the token after that is an opening brace.
11657      If we don't have an identifier, fabricate an anonymous name for
11658      the enumeration being defined.  */
11659   cp_lexer_consume_token (parser->lexer);
11660
11661   attributes = cp_parser_attributes_opt (parser);
11662
11663   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11664     identifier = cp_parser_identifier (parser);
11665   else
11666     identifier = make_anon_name ();
11667
11668   /* Look for the `{' but don't consume it yet.  */
11669   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11670     cp_parser_simulate_error (parser);
11671
11672   if (!cp_parser_parse_definitely (parser))
11673     return NULL_TREE;
11674
11675   /* Issue an error message if type-definitions are forbidden here.  */
11676   if (!cp_parser_check_type_definition (parser))
11677     type = error_mark_node;
11678   else
11679     /* Create the new type.  We do this before consuming the opening
11680        brace so the enum will be recorded as being on the line of its
11681        tag (or the 'enum' keyword, if there is no tag).  */
11682     type = start_enum (identifier);
11683   
11684   /* Consume the opening brace.  */
11685   cp_lexer_consume_token (parser->lexer);
11686
11687   if (type == error_mark_node)
11688     {
11689       cp_parser_skip_to_end_of_block_or_statement (parser);
11690       return error_mark_node;
11691     }
11692
11693   /* If the next token is not '}', then there are some enumerators.  */
11694   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11695     cp_parser_enumerator_list (parser, type);
11696
11697   /* Consume the final '}'.  */
11698   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11699
11700   /* Look for trailing attributes to apply to this enumeration, and
11701      apply them if appropriate.  */
11702   if (cp_parser_allow_gnu_extensions_p (parser))
11703     {
11704       tree trailing_attr = cp_parser_attributes_opt (parser);
11705       cplus_decl_attributes (&type,
11706                              trailing_attr,
11707                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11708     }
11709
11710   /* Finish up the enumeration.  */
11711   finish_enum (type);
11712
11713   return type;
11714 }
11715
11716 /* Parse an enumerator-list.  The enumerators all have the indicated
11717    TYPE.
11718
11719    enumerator-list:
11720      enumerator-definition
11721      enumerator-list , enumerator-definition  */
11722
11723 static void
11724 cp_parser_enumerator_list (cp_parser* parser, tree type)
11725 {
11726   while (true)
11727     {
11728       /* Parse an enumerator-definition.  */
11729       cp_parser_enumerator_definition (parser, type);
11730
11731       /* If the next token is not a ',', we've reached the end of
11732          the list.  */
11733       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11734         break;
11735       /* Otherwise, consume the `,' and keep going.  */
11736       cp_lexer_consume_token (parser->lexer);
11737       /* If the next token is a `}', there is a trailing comma.  */
11738       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11739         {
11740           if (!in_system_header)
11741             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11742           break;
11743         }
11744     }
11745 }
11746
11747 /* Parse an enumerator-definition.  The enumerator has the indicated
11748    TYPE.
11749
11750    enumerator-definition:
11751      enumerator
11752      enumerator = constant-expression
11753
11754    enumerator:
11755      identifier  */
11756
11757 static void
11758 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11759 {
11760   tree identifier;
11761   tree value;
11762
11763   /* Look for the identifier.  */
11764   identifier = cp_parser_identifier (parser);
11765   if (identifier == error_mark_node)
11766     return;
11767
11768   /* If the next token is an '=', then there is an explicit value.  */
11769   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11770     {
11771       /* Consume the `=' token.  */
11772       cp_lexer_consume_token (parser->lexer);
11773       /* Parse the value.  */
11774       value = cp_parser_constant_expression (parser,
11775                                              /*allow_non_constant_p=*/false,
11776                                              NULL);
11777     }
11778   else
11779     value = NULL_TREE;
11780
11781   /* Create the enumerator.  */
11782   build_enumerator (identifier, value, type);
11783 }
11784
11785 /* Parse a namespace-name.
11786
11787    namespace-name:
11788      original-namespace-name
11789      namespace-alias
11790
11791    Returns the NAMESPACE_DECL for the namespace.  */
11792
11793 static tree
11794 cp_parser_namespace_name (cp_parser* parser)
11795 {
11796   tree identifier;
11797   tree namespace_decl;
11798
11799   cp_token *token = cp_lexer_peek_token (parser->lexer);
11800
11801   /* Get the name of the namespace.  */
11802   identifier = cp_parser_identifier (parser);
11803   if (identifier == error_mark_node)
11804     return error_mark_node;
11805
11806   /* Look up the identifier in the currently active scope.  Look only
11807      for namespaces, due to:
11808
11809        [basic.lookup.udir]
11810
11811        When looking up a namespace-name in a using-directive or alias
11812        definition, only namespace names are considered.
11813
11814      And:
11815
11816        [basic.lookup.qual]
11817
11818        During the lookup of a name preceding the :: scope resolution
11819        operator, object, function, and enumerator names are ignored.
11820
11821      (Note that cp_parser_class_or_namespace_name only calls this
11822      function if the token after the name is the scope resolution
11823      operator.)  */
11824   namespace_decl = cp_parser_lookup_name (parser, identifier,
11825                                           none_type,
11826                                           /*is_template=*/false,
11827                                           /*is_namespace=*/true,
11828                                           /*check_dependency=*/true,
11829                                           /*ambiguous_decls=*/NULL,
11830                                           token->location);
11831   /* If it's not a namespace, issue an error.  */
11832   if (namespace_decl == error_mark_node
11833       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11834     {
11835       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11836         error ("%H%qD is not a namespace-name", &token->location, identifier);
11837       cp_parser_error (parser, "expected namespace-name");
11838       namespace_decl = error_mark_node;
11839     }
11840
11841   return namespace_decl;
11842 }
11843
11844 /* Parse a namespace-definition.
11845
11846    namespace-definition:
11847      named-namespace-definition
11848      unnamed-namespace-definition
11849
11850    named-namespace-definition:
11851      original-namespace-definition
11852      extension-namespace-definition
11853
11854    original-namespace-definition:
11855      namespace identifier { namespace-body }
11856
11857    extension-namespace-definition:
11858      namespace original-namespace-name { namespace-body }
11859
11860    unnamed-namespace-definition:
11861      namespace { namespace-body } */
11862
11863 static void
11864 cp_parser_namespace_definition (cp_parser* parser)
11865 {
11866   tree identifier, attribs;
11867   bool has_visibility;
11868   bool is_inline;
11869
11870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11871     {
11872       is_inline = true;
11873       cp_lexer_consume_token (parser->lexer);
11874     }
11875   else
11876     is_inline = false;
11877
11878   /* Look for the `namespace' keyword.  */
11879   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11880
11881   /* Get the name of the namespace.  We do not attempt to distinguish
11882      between an original-namespace-definition and an
11883      extension-namespace-definition at this point.  The semantic
11884      analysis routines are responsible for that.  */
11885   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11886     identifier = cp_parser_identifier (parser);
11887   else
11888     identifier = NULL_TREE;
11889
11890   /* Parse any specified attributes.  */
11891   attribs = cp_parser_attributes_opt (parser);
11892
11893   /* Look for the `{' to start the namespace.  */
11894   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11895   /* Start the namespace.  */
11896   push_namespace (identifier);
11897
11898   /* "inline namespace" is equivalent to a stub namespace definition
11899      followed by a strong using directive.  */
11900   if (is_inline)
11901     {
11902       tree name_space = current_namespace;
11903       /* Set up namespace association.  */
11904       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11905         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11906                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11907       /* Import the contents of the inline namespace.  */
11908       pop_namespace ();
11909       do_using_directive (name_space);
11910       push_namespace (identifier);
11911     }
11912
11913   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11914
11915   /* Parse the body of the namespace.  */
11916   cp_parser_namespace_body (parser);
11917
11918 #ifdef HANDLE_PRAGMA_VISIBILITY
11919   if (has_visibility)
11920     pop_visibility ();
11921 #endif
11922
11923   /* Finish the namespace.  */
11924   pop_namespace ();
11925   /* Look for the final `}'.  */
11926   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11927 }
11928
11929 /* Parse a namespace-body.
11930
11931    namespace-body:
11932      declaration-seq [opt]  */
11933
11934 static void
11935 cp_parser_namespace_body (cp_parser* parser)
11936 {
11937   cp_parser_declaration_seq_opt (parser);
11938 }
11939
11940 /* Parse a namespace-alias-definition.
11941
11942    namespace-alias-definition:
11943      namespace identifier = qualified-namespace-specifier ;  */
11944
11945 static void
11946 cp_parser_namespace_alias_definition (cp_parser* parser)
11947 {
11948   tree identifier;
11949   tree namespace_specifier;
11950
11951   cp_token *token = cp_lexer_peek_token (parser->lexer);
11952
11953   /* Look for the `namespace' keyword.  */
11954   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11955   /* Look for the identifier.  */
11956   identifier = cp_parser_identifier (parser);
11957   if (identifier == error_mark_node)
11958     return;
11959   /* Look for the `=' token.  */
11960   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11961       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11962     {
11963       error ("%H%<namespace%> definition is not allowed here", &token->location);
11964       /* Skip the definition.  */
11965       cp_lexer_consume_token (parser->lexer);
11966       if (cp_parser_skip_to_closing_brace (parser))
11967         cp_lexer_consume_token (parser->lexer);
11968       return;
11969     }
11970   cp_parser_require (parser, CPP_EQ, "%<=%>");
11971   /* Look for the qualified-namespace-specifier.  */
11972   namespace_specifier
11973     = cp_parser_qualified_namespace_specifier (parser);
11974   /* Look for the `;' token.  */
11975   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11976
11977   /* Register the alias in the symbol table.  */
11978   do_namespace_alias (identifier, namespace_specifier);
11979 }
11980
11981 /* Parse a qualified-namespace-specifier.
11982
11983    qualified-namespace-specifier:
11984      :: [opt] nested-name-specifier [opt] namespace-name
11985
11986    Returns a NAMESPACE_DECL corresponding to the specified
11987    namespace.  */
11988
11989 static tree
11990 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11991 {
11992   /* Look for the optional `::'.  */
11993   cp_parser_global_scope_opt (parser,
11994                               /*current_scope_valid_p=*/false);
11995
11996   /* Look for the optional nested-name-specifier.  */
11997   cp_parser_nested_name_specifier_opt (parser,
11998                                        /*typename_keyword_p=*/false,
11999                                        /*check_dependency_p=*/true,
12000                                        /*type_p=*/false,
12001                                        /*is_declaration=*/true);
12002
12003   return cp_parser_namespace_name (parser);
12004 }
12005
12006 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12007    access declaration.
12008
12009    using-declaration:
12010      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12011      using :: unqualified-id ;  
12012
12013    access-declaration:
12014      qualified-id ;  
12015
12016    */
12017
12018 static bool
12019 cp_parser_using_declaration (cp_parser* parser, 
12020                              bool access_declaration_p)
12021 {
12022   cp_token *token;
12023   bool typename_p = false;
12024   bool global_scope_p;
12025   tree decl;
12026   tree identifier;
12027   tree qscope;
12028
12029   if (access_declaration_p)
12030     cp_parser_parse_tentatively (parser);
12031   else
12032     {
12033       /* Look for the `using' keyword.  */
12034       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12035       
12036       /* Peek at the next token.  */
12037       token = cp_lexer_peek_token (parser->lexer);
12038       /* See if it's `typename'.  */
12039       if (token->keyword == RID_TYPENAME)
12040         {
12041           /* Remember that we've seen it.  */
12042           typename_p = true;
12043           /* Consume the `typename' token.  */
12044           cp_lexer_consume_token (parser->lexer);
12045         }
12046     }
12047
12048   /* Look for the optional global scope qualification.  */
12049   global_scope_p
12050     = (cp_parser_global_scope_opt (parser,
12051                                    /*current_scope_valid_p=*/false)
12052        != NULL_TREE);
12053
12054   /* If we saw `typename', or didn't see `::', then there must be a
12055      nested-name-specifier present.  */
12056   if (typename_p || !global_scope_p)
12057     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12058                                               /*check_dependency_p=*/true,
12059                                               /*type_p=*/false,
12060                                               /*is_declaration=*/true);
12061   /* Otherwise, we could be in either of the two productions.  In that
12062      case, treat the nested-name-specifier as optional.  */
12063   else
12064     qscope = cp_parser_nested_name_specifier_opt (parser,
12065                                                   /*typename_keyword_p=*/false,
12066                                                   /*check_dependency_p=*/true,
12067                                                   /*type_p=*/false,
12068                                                   /*is_declaration=*/true);
12069   if (!qscope)
12070     qscope = global_namespace;
12071
12072   if (access_declaration_p && cp_parser_error_occurred (parser))
12073     /* Something has already gone wrong; there's no need to parse
12074        further.  Since an error has occurred, the return value of
12075        cp_parser_parse_definitely will be false, as required.  */
12076     return cp_parser_parse_definitely (parser);
12077
12078   token = cp_lexer_peek_token (parser->lexer);
12079   /* Parse the unqualified-id.  */
12080   identifier = cp_parser_unqualified_id (parser,
12081                                          /*template_keyword_p=*/false,
12082                                          /*check_dependency_p=*/true,
12083                                          /*declarator_p=*/true,
12084                                          /*optional_p=*/false);
12085
12086   if (access_declaration_p)
12087     {
12088       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12089         cp_parser_simulate_error (parser);
12090       if (!cp_parser_parse_definitely (parser))
12091         return false;
12092     }
12093
12094   /* The function we call to handle a using-declaration is different
12095      depending on what scope we are in.  */
12096   if (qscope == error_mark_node || identifier == error_mark_node)
12097     ;
12098   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12099            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12100     /* [namespace.udecl]
12101
12102        A using declaration shall not name a template-id.  */
12103     error ("%Ha template-id may not appear in a using-declaration",
12104             &token->location);
12105   else
12106     {
12107       if (at_class_scope_p ())
12108         {
12109           /* Create the USING_DECL.  */
12110           decl = do_class_using_decl (parser->scope, identifier);
12111
12112           if (check_for_bare_parameter_packs (decl))
12113             return false;
12114           else
12115             /* Add it to the list of members in this class.  */
12116             finish_member_declaration (decl);
12117         }
12118       else
12119         {
12120           decl = cp_parser_lookup_name_simple (parser,
12121                                                identifier,
12122                                                token->location);
12123           if (decl == error_mark_node)
12124             cp_parser_name_lookup_error (parser, identifier,
12125                                          decl, NULL,
12126                                          token->location);
12127           else if (check_for_bare_parameter_packs (decl))
12128             return false;
12129           else if (!at_namespace_scope_p ())
12130             do_local_using_decl (decl, qscope, identifier);
12131           else
12132             do_toplevel_using_decl (decl, qscope, identifier);
12133         }
12134     }
12135
12136   /* Look for the final `;'.  */
12137   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12138   
12139   return true;
12140 }
12141
12142 /* Parse a using-directive.
12143
12144    using-directive:
12145      using namespace :: [opt] nested-name-specifier [opt]
12146        namespace-name ;  */
12147
12148 static void
12149 cp_parser_using_directive (cp_parser* parser)
12150 {
12151   tree namespace_decl;
12152   tree attribs;
12153
12154   /* Look for the `using' keyword.  */
12155   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12156   /* And the `namespace' keyword.  */
12157   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12158   /* Look for the optional `::' operator.  */
12159   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12160   /* And the optional nested-name-specifier.  */
12161   cp_parser_nested_name_specifier_opt (parser,
12162                                        /*typename_keyword_p=*/false,
12163                                        /*check_dependency_p=*/true,
12164                                        /*type_p=*/false,
12165                                        /*is_declaration=*/true);
12166   /* Get the namespace being used.  */
12167   namespace_decl = cp_parser_namespace_name (parser);
12168   /* And any specified attributes.  */
12169   attribs = cp_parser_attributes_opt (parser);
12170   /* Update the symbol table.  */
12171   parse_using_directive (namespace_decl, attribs);
12172   /* Look for the final `;'.  */
12173   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12174 }
12175
12176 /* Parse an asm-definition.
12177
12178    asm-definition:
12179      asm ( string-literal ) ;
12180
12181    GNU Extension:
12182
12183    asm-definition:
12184      asm volatile [opt] ( string-literal ) ;
12185      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12186      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12187                           : asm-operand-list [opt] ) ;
12188      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12189                           : asm-operand-list [opt]
12190                           : asm-operand-list [opt] ) ;  */
12191
12192 static void
12193 cp_parser_asm_definition (cp_parser* parser)
12194 {
12195   tree string;
12196   tree outputs = NULL_TREE;
12197   tree inputs = NULL_TREE;
12198   tree clobbers = NULL_TREE;
12199   tree asm_stmt;
12200   bool volatile_p = false;
12201   bool extended_p = false;
12202   bool invalid_inputs_p = false;
12203   bool invalid_outputs_p = false;
12204
12205   /* Look for the `asm' keyword.  */
12206   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12207   /* See if the next token is `volatile'.  */
12208   if (cp_parser_allow_gnu_extensions_p (parser)
12209       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12210     {
12211       /* Remember that we saw the `volatile' keyword.  */
12212       volatile_p = true;
12213       /* Consume the token.  */
12214       cp_lexer_consume_token (parser->lexer);
12215     }
12216   /* Look for the opening `('.  */
12217   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12218     return;
12219   /* Look for the string.  */
12220   string = cp_parser_string_literal (parser, false, false);
12221   if (string == error_mark_node)
12222     {
12223       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12224                                              /*consume_paren=*/true);
12225       return;
12226     }
12227
12228   /* If we're allowing GNU extensions, check for the extended assembly
12229      syntax.  Unfortunately, the `:' tokens need not be separated by
12230      a space in C, and so, for compatibility, we tolerate that here
12231      too.  Doing that means that we have to treat the `::' operator as
12232      two `:' tokens.  */
12233   if (cp_parser_allow_gnu_extensions_p (parser)
12234       && parser->in_function_body
12235       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12236           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12237     {
12238       bool inputs_p = false;
12239       bool clobbers_p = false;
12240
12241       /* The extended syntax was used.  */
12242       extended_p = true;
12243
12244       /* Look for outputs.  */
12245       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12246         {
12247           /* Consume the `:'.  */
12248           cp_lexer_consume_token (parser->lexer);
12249           /* Parse the output-operands.  */
12250           if (cp_lexer_next_token_is_not (parser->lexer,
12251                                           CPP_COLON)
12252               && cp_lexer_next_token_is_not (parser->lexer,
12253                                              CPP_SCOPE)
12254               && cp_lexer_next_token_is_not (parser->lexer,
12255                                              CPP_CLOSE_PAREN))
12256             outputs = cp_parser_asm_operand_list (parser);
12257
12258             if (outputs == error_mark_node)
12259               invalid_outputs_p = true;
12260         }
12261       /* If the next token is `::', there are no outputs, and the
12262          next token is the beginning of the inputs.  */
12263       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12264         /* The inputs are coming next.  */
12265         inputs_p = true;
12266
12267       /* Look for inputs.  */
12268       if (inputs_p
12269           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12270         {
12271           /* Consume the `:' or `::'.  */
12272           cp_lexer_consume_token (parser->lexer);
12273           /* Parse the output-operands.  */
12274           if (cp_lexer_next_token_is_not (parser->lexer,
12275                                           CPP_COLON)
12276               && cp_lexer_next_token_is_not (parser->lexer,
12277                                              CPP_CLOSE_PAREN))
12278             inputs = cp_parser_asm_operand_list (parser);
12279
12280             if (inputs == error_mark_node)
12281               invalid_inputs_p = true;
12282         }
12283       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12284         /* The clobbers are coming next.  */
12285         clobbers_p = true;
12286
12287       /* Look for clobbers.  */
12288       if (clobbers_p
12289           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12290         {
12291           /* Consume the `:' or `::'.  */
12292           cp_lexer_consume_token (parser->lexer);
12293           /* Parse the clobbers.  */
12294           if (cp_lexer_next_token_is_not (parser->lexer,
12295                                           CPP_CLOSE_PAREN))
12296             clobbers = cp_parser_asm_clobber_list (parser);
12297         }
12298     }
12299   /* Look for the closing `)'.  */
12300   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12301     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12302                                            /*consume_paren=*/true);
12303   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12304
12305   if (!invalid_inputs_p && !invalid_outputs_p)
12306     {
12307       /* Create the ASM_EXPR.  */
12308       if (parser->in_function_body)
12309         {
12310           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12311                                       inputs, clobbers);
12312           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12313           if (!extended_p)
12314             {
12315               tree temp = asm_stmt;
12316               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12317                 temp = TREE_OPERAND (temp, 0);
12318
12319               ASM_INPUT_P (temp) = 1;
12320             }
12321         }
12322       else
12323         cgraph_add_asm_node (string);
12324     }
12325 }
12326
12327 /* Declarators [gram.dcl.decl] */
12328
12329 /* Parse an init-declarator.
12330
12331    init-declarator:
12332      declarator initializer [opt]
12333
12334    GNU Extension:
12335
12336    init-declarator:
12337      declarator asm-specification [opt] attributes [opt] initializer [opt]
12338
12339    function-definition:
12340      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12341        function-body
12342      decl-specifier-seq [opt] declarator function-try-block
12343
12344    GNU Extension:
12345
12346    function-definition:
12347      __extension__ function-definition
12348
12349    The DECL_SPECIFIERS apply to this declarator.  Returns a
12350    representation of the entity declared.  If MEMBER_P is TRUE, then
12351    this declarator appears in a class scope.  The new DECL created by
12352    this declarator is returned.
12353
12354    The CHECKS are access checks that should be performed once we know
12355    what entity is being declared (and, therefore, what classes have
12356    befriended it).
12357
12358    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12359    for a function-definition here as well.  If the declarator is a
12360    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12361    be TRUE upon return.  By that point, the function-definition will
12362    have been completely parsed.
12363
12364    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12365    is FALSE.  */
12366
12367 static tree
12368 cp_parser_init_declarator (cp_parser* parser,
12369                            cp_decl_specifier_seq *decl_specifiers,
12370                            VEC (deferred_access_check,gc)* checks,
12371                            bool function_definition_allowed_p,
12372                            bool member_p,
12373                            int declares_class_or_enum,
12374                            bool* function_definition_p)
12375 {
12376   cp_token *token = NULL, *asm_spec_start_token = NULL,
12377            *attributes_start_token = NULL;
12378   cp_declarator *declarator;
12379   tree prefix_attributes;
12380   tree attributes;
12381   tree asm_specification;
12382   tree initializer;
12383   tree decl = NULL_TREE;
12384   tree scope;
12385   int is_initialized;
12386   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12387      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12388      "(...)".  */
12389   enum cpp_ttype initialization_kind;
12390   bool is_direct_init = false;
12391   bool is_non_constant_init;
12392   int ctor_dtor_or_conv_p;
12393   bool friend_p;
12394   tree pushed_scope = NULL;
12395
12396   /* Gather the attributes that were provided with the
12397      decl-specifiers.  */
12398   prefix_attributes = decl_specifiers->attributes;
12399
12400   /* Assume that this is not the declarator for a function
12401      definition.  */
12402   if (function_definition_p)
12403     *function_definition_p = false;
12404
12405   /* Defer access checks while parsing the declarator; we cannot know
12406      what names are accessible until we know what is being
12407      declared.  */
12408   resume_deferring_access_checks ();
12409
12410   /* Parse the declarator.  */
12411   token = cp_lexer_peek_token (parser->lexer);
12412   declarator
12413     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12414                             &ctor_dtor_or_conv_p,
12415                             /*parenthesized_p=*/NULL,
12416                             /*member_p=*/false);
12417   /* Gather up the deferred checks.  */
12418   stop_deferring_access_checks ();
12419
12420   /* If the DECLARATOR was erroneous, there's no need to go
12421      further.  */
12422   if (declarator == cp_error_declarator)
12423     return error_mark_node;
12424
12425   /* Check that the number of template-parameter-lists is OK.  */
12426   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12427                                                        token->location))
12428     return error_mark_node;
12429
12430   if (declares_class_or_enum & 2)
12431     cp_parser_check_for_definition_in_return_type (declarator,
12432                                                    decl_specifiers->type,
12433                                                    decl_specifiers->type_location);
12434
12435   /* Figure out what scope the entity declared by the DECLARATOR is
12436      located in.  `grokdeclarator' sometimes changes the scope, so
12437      we compute it now.  */
12438   scope = get_scope_of_declarator (declarator);
12439
12440   /* If we're allowing GNU extensions, look for an asm-specification
12441      and attributes.  */
12442   if (cp_parser_allow_gnu_extensions_p (parser))
12443     {
12444       /* Look for an asm-specification.  */
12445       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12446       asm_specification = cp_parser_asm_specification_opt (parser);
12447       /* And attributes.  */
12448       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12449       attributes = cp_parser_attributes_opt (parser);
12450     }
12451   else
12452     {
12453       asm_specification = NULL_TREE;
12454       attributes = NULL_TREE;
12455     }
12456
12457   /* Peek at the next token.  */
12458   token = cp_lexer_peek_token (parser->lexer);
12459   /* Check to see if the token indicates the start of a
12460      function-definition.  */
12461   if (function_declarator_p (declarator)
12462       && cp_parser_token_starts_function_definition_p (token))
12463     {
12464       if (!function_definition_allowed_p)
12465         {
12466           /* If a function-definition should not appear here, issue an
12467              error message.  */
12468           cp_parser_error (parser,
12469                            "a function-definition is not allowed here");
12470           return error_mark_node;
12471         }
12472       else
12473         {
12474           /* Neither attributes nor an asm-specification are allowed
12475              on a function-definition.  */
12476           if (asm_specification)
12477             error ("%Han asm-specification is not allowed "
12478                    "on a function-definition",
12479                    &asm_spec_start_token->location);
12480           if (attributes)
12481             error ("%Hattributes are not allowed on a function-definition",
12482                    &attributes_start_token->location);
12483           /* This is a function-definition.  */
12484           *function_definition_p = true;
12485
12486           /* Parse the function definition.  */
12487           if (member_p)
12488             decl = cp_parser_save_member_function_body (parser,
12489                                                         decl_specifiers,
12490                                                         declarator,
12491                                                         prefix_attributes);
12492           else
12493             decl
12494               = (cp_parser_function_definition_from_specifiers_and_declarator
12495                  (parser, decl_specifiers, prefix_attributes, declarator));
12496
12497           return decl;
12498         }
12499     }
12500
12501   /* [dcl.dcl]
12502
12503      Only in function declarations for constructors, destructors, and
12504      type conversions can the decl-specifier-seq be omitted.
12505
12506      We explicitly postpone this check past the point where we handle
12507      function-definitions because we tolerate function-definitions
12508      that are missing their return types in some modes.  */
12509   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12510     {
12511       cp_parser_error (parser,
12512                        "expected constructor, destructor, or type conversion");
12513       return error_mark_node;
12514     }
12515
12516   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12517   if (token->type == CPP_EQ
12518       || token->type == CPP_OPEN_PAREN
12519       || token->type == CPP_OPEN_BRACE)
12520     {
12521       is_initialized = 1;
12522       initialization_kind = token->type;
12523
12524       if (token->type == CPP_EQ
12525           && function_declarator_p (declarator))
12526         {
12527           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12528           if (t2->keyword == RID_DEFAULT)
12529             is_initialized = 2;
12530           else if (t2->keyword == RID_DELETE)
12531             is_initialized = 3;
12532         }
12533     }
12534   else
12535     {
12536       /* If the init-declarator isn't initialized and isn't followed by a
12537          `,' or `;', it's not a valid init-declarator.  */
12538       if (token->type != CPP_COMMA
12539           && token->type != CPP_SEMICOLON)
12540         {
12541           cp_parser_error (parser, "expected initializer");
12542           return error_mark_node;
12543         }
12544       is_initialized = 0;
12545       initialization_kind = CPP_EOF;
12546     }
12547
12548   /* Because start_decl has side-effects, we should only call it if we
12549      know we're going ahead.  By this point, we know that we cannot
12550      possibly be looking at any other construct.  */
12551   cp_parser_commit_to_tentative_parse (parser);
12552
12553   /* If the decl specifiers were bad, issue an error now that we're
12554      sure this was intended to be a declarator.  Then continue
12555      declaring the variable(s), as int, to try to cut down on further
12556      errors.  */
12557   if (decl_specifiers->any_specifiers_p
12558       && decl_specifiers->type == error_mark_node)
12559     {
12560       cp_parser_error (parser, "invalid type in declaration");
12561       decl_specifiers->type = integer_type_node;
12562     }
12563
12564   /* Check to see whether or not this declaration is a friend.  */
12565   friend_p = cp_parser_friend_p (decl_specifiers);
12566
12567   /* Enter the newly declared entry in the symbol table.  If we're
12568      processing a declaration in a class-specifier, we wait until
12569      after processing the initializer.  */
12570   if (!member_p)
12571     {
12572       if (parser->in_unbraced_linkage_specification_p)
12573         decl_specifiers->storage_class = sc_extern;
12574       decl = start_decl (declarator, decl_specifiers,
12575                          is_initialized, attributes, prefix_attributes,
12576                          &pushed_scope);
12577     }
12578   else if (scope)
12579     /* Enter the SCOPE.  That way unqualified names appearing in the
12580        initializer will be looked up in SCOPE.  */
12581     pushed_scope = push_scope (scope);
12582
12583   /* Perform deferred access control checks, now that we know in which
12584      SCOPE the declared entity resides.  */
12585   if (!member_p && decl)
12586     {
12587       tree saved_current_function_decl = NULL_TREE;
12588
12589       /* If the entity being declared is a function, pretend that we
12590          are in its scope.  If it is a `friend', it may have access to
12591          things that would not otherwise be accessible.  */
12592       if (TREE_CODE (decl) == FUNCTION_DECL)
12593         {
12594           saved_current_function_decl = current_function_decl;
12595           current_function_decl = decl;
12596         }
12597
12598       /* Perform access checks for template parameters.  */
12599       cp_parser_perform_template_parameter_access_checks (checks);
12600
12601       /* Perform the access control checks for the declarator and the
12602          decl-specifiers.  */
12603       perform_deferred_access_checks ();
12604
12605       /* Restore the saved value.  */
12606       if (TREE_CODE (decl) == FUNCTION_DECL)
12607         current_function_decl = saved_current_function_decl;
12608     }
12609
12610   /* Parse the initializer.  */
12611   initializer = NULL_TREE;
12612   is_direct_init = false;
12613   is_non_constant_init = true;
12614   if (is_initialized)
12615     {
12616       if (function_declarator_p (declarator))
12617         {
12618           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12619            if (initialization_kind == CPP_EQ)
12620              initializer = cp_parser_pure_specifier (parser);
12621            else
12622              {
12623                /* If the declaration was erroneous, we don't really
12624                   know what the user intended, so just silently
12625                   consume the initializer.  */
12626                if (decl != error_mark_node)
12627                  error ("%Hinitializer provided for function",
12628                         &initializer_start_token->location);
12629                cp_parser_skip_to_closing_parenthesis (parser,
12630                                                       /*recovering=*/true,
12631                                                       /*or_comma=*/false,
12632                                                       /*consume_paren=*/true);
12633              }
12634         }
12635       else
12636         initializer = cp_parser_initializer (parser,
12637                                              &is_direct_init,
12638                                              &is_non_constant_init);
12639     }
12640
12641   /* The old parser allows attributes to appear after a parenthesized
12642      initializer.  Mark Mitchell proposed removing this functionality
12643      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12644      attributes -- but ignores them.  */
12645   if (cp_parser_allow_gnu_extensions_p (parser)
12646       && initialization_kind == CPP_OPEN_PAREN)
12647     if (cp_parser_attributes_opt (parser))
12648       warning (OPT_Wattributes,
12649                "attributes after parenthesized initializer ignored");
12650
12651   /* For an in-class declaration, use `grokfield' to create the
12652      declaration.  */
12653   if (member_p)
12654     {
12655       if (pushed_scope)
12656         {
12657           pop_scope (pushed_scope);
12658           pushed_scope = false;
12659         }
12660       decl = grokfield (declarator, decl_specifiers,
12661                         initializer, !is_non_constant_init,
12662                         /*asmspec=*/NULL_TREE,
12663                         prefix_attributes);
12664       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12665         cp_parser_save_default_args (parser, decl);
12666     }
12667
12668   /* Finish processing the declaration.  But, skip friend
12669      declarations.  */
12670   if (!friend_p && decl && decl != error_mark_node)
12671     {
12672       cp_finish_decl (decl,
12673                       initializer, !is_non_constant_init,
12674                       asm_specification,
12675                       /* If the initializer is in parentheses, then this is
12676                          a direct-initialization, which means that an
12677                          `explicit' constructor is OK.  Otherwise, an
12678                          `explicit' constructor cannot be used.  */
12679                       ((is_direct_init || !is_initialized)
12680                        ? 0 : LOOKUP_ONLYCONVERTING));
12681     }
12682   else if ((cxx_dialect != cxx98) && friend_p
12683            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12684     /* Core issue #226 (C++0x only): A default template-argument
12685        shall not be specified in a friend class template
12686        declaration. */
12687     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12688                              /*is_partial=*/0, /*is_friend_decl=*/1);
12689
12690   if (!friend_p && pushed_scope)
12691     pop_scope (pushed_scope);
12692
12693   return decl;
12694 }
12695
12696 /* Parse a declarator.
12697
12698    declarator:
12699      direct-declarator
12700      ptr-operator declarator
12701
12702    abstract-declarator:
12703      ptr-operator abstract-declarator [opt]
12704      direct-abstract-declarator
12705
12706    GNU Extensions:
12707
12708    declarator:
12709      attributes [opt] direct-declarator
12710      attributes [opt] ptr-operator declarator
12711
12712    abstract-declarator:
12713      attributes [opt] ptr-operator abstract-declarator [opt]
12714      attributes [opt] direct-abstract-declarator
12715
12716    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12717    detect constructor, destructor or conversion operators. It is set
12718    to -1 if the declarator is a name, and +1 if it is a
12719    function. Otherwise it is set to zero. Usually you just want to
12720    test for >0, but internally the negative value is used.
12721
12722    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12723    a decl-specifier-seq unless it declares a constructor, destructor,
12724    or conversion.  It might seem that we could check this condition in
12725    semantic analysis, rather than parsing, but that makes it difficult
12726    to handle something like `f()'.  We want to notice that there are
12727    no decl-specifiers, and therefore realize that this is an
12728    expression, not a declaration.)
12729
12730    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12731    the declarator is a direct-declarator of the form "(...)".
12732
12733    MEMBER_P is true iff this declarator is a member-declarator.  */
12734
12735 static cp_declarator *
12736 cp_parser_declarator (cp_parser* parser,
12737                       cp_parser_declarator_kind dcl_kind,
12738                       int* ctor_dtor_or_conv_p,
12739                       bool* parenthesized_p,
12740                       bool member_p)
12741 {
12742   cp_token *token;
12743   cp_declarator *declarator;
12744   enum tree_code code;
12745   cp_cv_quals cv_quals;
12746   tree class_type;
12747   tree attributes = NULL_TREE;
12748
12749   /* Assume this is not a constructor, destructor, or type-conversion
12750      operator.  */
12751   if (ctor_dtor_or_conv_p)
12752     *ctor_dtor_or_conv_p = 0;
12753
12754   if (cp_parser_allow_gnu_extensions_p (parser))
12755     attributes = cp_parser_attributes_opt (parser);
12756
12757   /* Peek at the next token.  */
12758   token = cp_lexer_peek_token (parser->lexer);
12759
12760   /* Check for the ptr-operator production.  */
12761   cp_parser_parse_tentatively (parser);
12762   /* Parse the ptr-operator.  */
12763   code = cp_parser_ptr_operator (parser,
12764                                  &class_type,
12765                                  &cv_quals);
12766   /* If that worked, then we have a ptr-operator.  */
12767   if (cp_parser_parse_definitely (parser))
12768     {
12769       /* If a ptr-operator was found, then this declarator was not
12770          parenthesized.  */
12771       if (parenthesized_p)
12772         *parenthesized_p = true;
12773       /* The dependent declarator is optional if we are parsing an
12774          abstract-declarator.  */
12775       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12776         cp_parser_parse_tentatively (parser);
12777
12778       /* Parse the dependent declarator.  */
12779       declarator = cp_parser_declarator (parser, dcl_kind,
12780                                          /*ctor_dtor_or_conv_p=*/NULL,
12781                                          /*parenthesized_p=*/NULL,
12782                                          /*member_p=*/false);
12783
12784       /* If we are parsing an abstract-declarator, we must handle the
12785          case where the dependent declarator is absent.  */
12786       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12787           && !cp_parser_parse_definitely (parser))
12788         declarator = NULL;
12789
12790       declarator = cp_parser_make_indirect_declarator
12791         (code, class_type, cv_quals, declarator);
12792     }
12793   /* Everything else is a direct-declarator.  */
12794   else
12795     {
12796       if (parenthesized_p)
12797         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12798                                                    CPP_OPEN_PAREN);
12799       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12800                                                 ctor_dtor_or_conv_p,
12801                                                 member_p);
12802     }
12803
12804   if (attributes && declarator && declarator != cp_error_declarator)
12805     declarator->attributes = attributes;
12806
12807   return declarator;
12808 }
12809
12810 /* Parse a direct-declarator or direct-abstract-declarator.
12811
12812    direct-declarator:
12813      declarator-id
12814      direct-declarator ( parameter-declaration-clause )
12815        cv-qualifier-seq [opt]
12816        exception-specification [opt]
12817      direct-declarator [ constant-expression [opt] ]
12818      ( declarator )
12819
12820    direct-abstract-declarator:
12821      direct-abstract-declarator [opt]
12822        ( parameter-declaration-clause )
12823        cv-qualifier-seq [opt]
12824        exception-specification [opt]
12825      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12826      ( abstract-declarator )
12827
12828    Returns a representation of the declarator.  DCL_KIND is
12829    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12830    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12831    we are parsing a direct-declarator.  It is
12832    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12833    of ambiguity we prefer an abstract declarator, as per
12834    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12835    cp_parser_declarator.  */
12836
12837 static cp_declarator *
12838 cp_parser_direct_declarator (cp_parser* parser,
12839                              cp_parser_declarator_kind dcl_kind,
12840                              int* ctor_dtor_or_conv_p,
12841                              bool member_p)
12842 {
12843   cp_token *token;
12844   cp_declarator *declarator = NULL;
12845   tree scope = NULL_TREE;
12846   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12847   bool saved_in_declarator_p = parser->in_declarator_p;
12848   bool first = true;
12849   tree pushed_scope = NULL_TREE;
12850
12851   while (true)
12852     {
12853       /* Peek at the next token.  */
12854       token = cp_lexer_peek_token (parser->lexer);
12855       if (token->type == CPP_OPEN_PAREN)
12856         {
12857           /* This is either a parameter-declaration-clause, or a
12858              parenthesized declarator. When we know we are parsing a
12859              named declarator, it must be a parenthesized declarator
12860              if FIRST is true. For instance, `(int)' is a
12861              parameter-declaration-clause, with an omitted
12862              direct-abstract-declarator. But `((*))', is a
12863              parenthesized abstract declarator. Finally, when T is a
12864              template parameter `(T)' is a
12865              parameter-declaration-clause, and not a parenthesized
12866              named declarator.
12867
12868              We first try and parse a parameter-declaration-clause,
12869              and then try a nested declarator (if FIRST is true).
12870
12871              It is not an error for it not to be a
12872              parameter-declaration-clause, even when FIRST is
12873              false. Consider,
12874
12875                int i (int);
12876                int i (3);
12877
12878              The first is the declaration of a function while the
12879              second is the definition of a variable, including its
12880              initializer.
12881
12882              Having seen only the parenthesis, we cannot know which of
12883              these two alternatives should be selected.  Even more
12884              complex are examples like:
12885
12886                int i (int (a));
12887                int i (int (3));
12888
12889              The former is a function-declaration; the latter is a
12890              variable initialization.
12891
12892              Thus again, we try a parameter-declaration-clause, and if
12893              that fails, we back out and return.  */
12894
12895           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12896             {
12897               cp_parameter_declarator *params;
12898               unsigned saved_num_template_parameter_lists;
12899
12900               /* In a member-declarator, the only valid interpretation
12901                  of a parenthesis is the start of a
12902                  parameter-declaration-clause.  (It is invalid to
12903                  initialize a static data member with a parenthesized
12904                  initializer; only the "=" form of initialization is
12905                  permitted.)  */
12906               if (!member_p)
12907                 cp_parser_parse_tentatively (parser);
12908
12909               /* Consume the `('.  */
12910               cp_lexer_consume_token (parser->lexer);
12911               if (first)
12912                 {
12913                   /* If this is going to be an abstract declarator, we're
12914                      in a declarator and we can't have default args.  */
12915                   parser->default_arg_ok_p = false;
12916                   parser->in_declarator_p = true;
12917                 }
12918
12919               /* Inside the function parameter list, surrounding
12920                  template-parameter-lists do not apply.  */
12921               saved_num_template_parameter_lists
12922                 = parser->num_template_parameter_lists;
12923               parser->num_template_parameter_lists = 0;
12924
12925               /* Parse the parameter-declaration-clause.  */
12926               params = cp_parser_parameter_declaration_clause (parser);
12927
12928               parser->num_template_parameter_lists
12929                 = saved_num_template_parameter_lists;
12930
12931               /* If all went well, parse the cv-qualifier-seq and the
12932                  exception-specification.  */
12933               if (member_p || cp_parser_parse_definitely (parser))
12934                 {
12935                   cp_cv_quals cv_quals;
12936                   tree exception_specification;
12937
12938                   if (ctor_dtor_or_conv_p)
12939                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12940                   first = false;
12941                   /* Consume the `)'.  */
12942                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12943
12944                   /* Parse the cv-qualifier-seq.  */
12945                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12946                   /* And the exception-specification.  */
12947                   exception_specification
12948                     = cp_parser_exception_specification_opt (parser);
12949
12950                   /* Create the function-declarator.  */
12951                   declarator = make_call_declarator (declarator,
12952                                                      params,
12953                                                      cv_quals,
12954                                                      exception_specification);
12955                   /* Any subsequent parameter lists are to do with
12956                      return type, so are not those of the declared
12957                      function.  */
12958                   parser->default_arg_ok_p = false;
12959
12960                   /* Repeat the main loop.  */
12961                   continue;
12962                 }
12963             }
12964
12965           /* If this is the first, we can try a parenthesized
12966              declarator.  */
12967           if (first)
12968             {
12969               bool saved_in_type_id_in_expr_p;
12970
12971               parser->default_arg_ok_p = saved_default_arg_ok_p;
12972               parser->in_declarator_p = saved_in_declarator_p;
12973
12974               /* Consume the `('.  */
12975               cp_lexer_consume_token (parser->lexer);
12976               /* Parse the nested declarator.  */
12977               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12978               parser->in_type_id_in_expr_p = true;
12979               declarator
12980                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12981                                         /*parenthesized_p=*/NULL,
12982                                         member_p);
12983               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12984               first = false;
12985               /* Expect a `)'.  */
12986               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12987                 declarator = cp_error_declarator;
12988               if (declarator == cp_error_declarator)
12989                 break;
12990
12991               goto handle_declarator;
12992             }
12993           /* Otherwise, we must be done.  */
12994           else
12995             break;
12996         }
12997       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12998                && token->type == CPP_OPEN_SQUARE)
12999         {
13000           /* Parse an array-declarator.  */
13001           tree bounds;
13002
13003           if (ctor_dtor_or_conv_p)
13004             *ctor_dtor_or_conv_p = 0;
13005
13006           first = false;
13007           parser->default_arg_ok_p = false;
13008           parser->in_declarator_p = true;
13009           /* Consume the `['.  */
13010           cp_lexer_consume_token (parser->lexer);
13011           /* Peek at the next token.  */
13012           token = cp_lexer_peek_token (parser->lexer);
13013           /* If the next token is `]', then there is no
13014              constant-expression.  */
13015           if (token->type != CPP_CLOSE_SQUARE)
13016             {
13017               bool non_constant_p;
13018
13019               bounds
13020                 = cp_parser_constant_expression (parser,
13021                                                  /*allow_non_constant=*/true,
13022                                                  &non_constant_p);
13023               if (!non_constant_p)
13024                 bounds = fold_non_dependent_expr (bounds);
13025               /* Normally, the array bound must be an integral constant
13026                  expression.  However, as an extension, we allow VLAs
13027                  in function scopes.  */
13028               else if (!parser->in_function_body)
13029                 {
13030                   error ("%Harray bound is not an integer constant",
13031                          &token->location);
13032                   bounds = error_mark_node;
13033                 }
13034             }
13035           else
13036             bounds = NULL_TREE;
13037           /* Look for the closing `]'.  */
13038           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13039             {
13040               declarator = cp_error_declarator;
13041               break;
13042             }
13043
13044           declarator = make_array_declarator (declarator, bounds);
13045         }
13046       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13047         {
13048           tree qualifying_scope;
13049           tree unqualified_name;
13050           special_function_kind sfk;
13051           bool abstract_ok;
13052           bool pack_expansion_p = false;
13053           cp_token *declarator_id_start_token;
13054
13055           /* Parse a declarator-id */
13056           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13057           if (abstract_ok)
13058             {
13059               cp_parser_parse_tentatively (parser);
13060
13061               /* If we see an ellipsis, we should be looking at a
13062                  parameter pack. */
13063               if (token->type == CPP_ELLIPSIS)
13064                 {
13065                   /* Consume the `...' */
13066                   cp_lexer_consume_token (parser->lexer);
13067
13068                   pack_expansion_p = true;
13069                 }
13070             }
13071
13072           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13073           unqualified_name
13074             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13075           qualifying_scope = parser->scope;
13076           if (abstract_ok)
13077             {
13078               bool okay = false;
13079
13080               if (!unqualified_name && pack_expansion_p)
13081                 {
13082                   /* Check whether an error occurred. */
13083                   okay = !cp_parser_error_occurred (parser);
13084
13085                   /* We already consumed the ellipsis to mark a
13086                      parameter pack, but we have no way to report it,
13087                      so abort the tentative parse. We will be exiting
13088                      immediately anyway. */
13089                   cp_parser_abort_tentative_parse (parser);
13090                 }
13091               else
13092                 okay = cp_parser_parse_definitely (parser);
13093
13094               if (!okay)
13095                 unqualified_name = error_mark_node;
13096               else if (unqualified_name
13097                        && (qualifying_scope
13098                            || (TREE_CODE (unqualified_name)
13099                                != IDENTIFIER_NODE)))
13100                 {
13101                   cp_parser_error (parser, "expected unqualified-id");
13102                   unqualified_name = error_mark_node;
13103                 }
13104             }
13105
13106           if (!unqualified_name)
13107             return NULL;
13108           if (unqualified_name == error_mark_node)
13109             {
13110               declarator = cp_error_declarator;
13111               pack_expansion_p = false;
13112               declarator->parameter_pack_p = false;
13113               break;
13114             }
13115
13116           if (qualifying_scope && at_namespace_scope_p ()
13117               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13118             {
13119               /* In the declaration of a member of a template class
13120                  outside of the class itself, the SCOPE will sometimes
13121                  be a TYPENAME_TYPE.  For example, given:
13122
13123                  template <typename T>
13124                  int S<T>::R::i = 3;
13125
13126                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13127                  this context, we must resolve S<T>::R to an ordinary
13128                  type, rather than a typename type.
13129
13130                  The reason we normally avoid resolving TYPENAME_TYPEs
13131                  is that a specialization of `S' might render
13132                  `S<T>::R' not a type.  However, if `S' is
13133                  specialized, then this `i' will not be used, so there
13134                  is no harm in resolving the types here.  */
13135               tree type;
13136
13137               /* Resolve the TYPENAME_TYPE.  */
13138               type = resolve_typename_type (qualifying_scope,
13139                                             /*only_current_p=*/false);
13140               /* If that failed, the declarator is invalid.  */
13141               if (TREE_CODE (type) == TYPENAME_TYPE)
13142                 error ("%H%<%T::%E%> is not a type",
13143                        &declarator_id_start_token->location,
13144                        TYPE_CONTEXT (qualifying_scope),
13145                        TYPE_IDENTIFIER (qualifying_scope));
13146               qualifying_scope = type;
13147             }
13148
13149           sfk = sfk_none;
13150
13151           if (unqualified_name)
13152             {
13153               tree class_type;
13154
13155               if (qualifying_scope
13156                   && CLASS_TYPE_P (qualifying_scope))
13157                 class_type = qualifying_scope;
13158               else
13159                 class_type = current_class_type;
13160
13161               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13162                 {
13163                   tree name_type = TREE_TYPE (unqualified_name);
13164                   if (class_type && same_type_p (name_type, class_type))
13165                     {
13166                       if (qualifying_scope
13167                           && CLASSTYPE_USE_TEMPLATE (name_type))
13168                         {
13169                           error ("%Hinvalid use of constructor as a template",
13170                                  &declarator_id_start_token->location);
13171                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13172                                   "name the constructor in a qualified name",
13173                                   class_type,
13174                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13175                                   class_type, name_type);
13176                           declarator = cp_error_declarator;
13177                           break;
13178                         }
13179                       else
13180                         unqualified_name = constructor_name (class_type);
13181                     }
13182                   else
13183                     {
13184                       /* We do not attempt to print the declarator
13185                          here because we do not have enough
13186                          information about its original syntactic
13187                          form.  */
13188                       cp_parser_error (parser, "invalid declarator");
13189                       declarator = cp_error_declarator;
13190                       break;
13191                     }
13192                 }
13193
13194               if (class_type)
13195                 {
13196                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13197                     sfk = sfk_destructor;
13198                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13199                     sfk = sfk_conversion;
13200                   else if (/* There's no way to declare a constructor
13201                               for an anonymous type, even if the type
13202                               got a name for linkage purposes.  */
13203                            !TYPE_WAS_ANONYMOUS (class_type)
13204                            && constructor_name_p (unqualified_name,
13205                                                   class_type))
13206                     {
13207                       unqualified_name = constructor_name (class_type);
13208                       sfk = sfk_constructor;
13209                     }
13210
13211                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13212                     *ctor_dtor_or_conv_p = -1;
13213                 }
13214             }
13215           declarator = make_id_declarator (qualifying_scope,
13216                                            unqualified_name,
13217                                            sfk);
13218           declarator->id_loc = token->location;
13219           declarator->parameter_pack_p = pack_expansion_p;
13220
13221           if (pack_expansion_p)
13222             maybe_warn_variadic_templates ();
13223
13224         handle_declarator:;
13225           scope = get_scope_of_declarator (declarator);
13226           if (scope)
13227             /* Any names that appear after the declarator-id for a
13228                member are looked up in the containing scope.  */
13229             pushed_scope = push_scope (scope);
13230           parser->in_declarator_p = true;
13231           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13232               || (declarator && declarator->kind == cdk_id))
13233             /* Default args are only allowed on function
13234                declarations.  */
13235             parser->default_arg_ok_p = saved_default_arg_ok_p;
13236           else
13237             parser->default_arg_ok_p = false;
13238
13239           first = false;
13240         }
13241       /* We're done.  */
13242       else
13243         break;
13244     }
13245
13246   /* For an abstract declarator, we might wind up with nothing at this
13247      point.  That's an error; the declarator is not optional.  */
13248   if (!declarator)
13249     cp_parser_error (parser, "expected declarator");
13250
13251   /* If we entered a scope, we must exit it now.  */
13252   if (pushed_scope)
13253     pop_scope (pushed_scope);
13254
13255   parser->default_arg_ok_p = saved_default_arg_ok_p;
13256   parser->in_declarator_p = saved_in_declarator_p;
13257
13258   return declarator;
13259 }
13260
13261 /* Parse a ptr-operator.
13262
13263    ptr-operator:
13264      * cv-qualifier-seq [opt]
13265      &
13266      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13267
13268    GNU Extension:
13269
13270    ptr-operator:
13271      & cv-qualifier-seq [opt]
13272
13273    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13274    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13275    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13276    filled in with the TYPE containing the member.  *CV_QUALS is
13277    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13278    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13279    Note that the tree codes returned by this function have nothing
13280    to do with the types of trees that will be eventually be created
13281    to represent the pointer or reference type being parsed. They are
13282    just constants with suggestive names. */
13283 static enum tree_code
13284 cp_parser_ptr_operator (cp_parser* parser,
13285                         tree* type,
13286                         cp_cv_quals *cv_quals)
13287 {
13288   enum tree_code code = ERROR_MARK;
13289   cp_token *token;
13290
13291   /* Assume that it's not a pointer-to-member.  */
13292   *type = NULL_TREE;
13293   /* And that there are no cv-qualifiers.  */
13294   *cv_quals = TYPE_UNQUALIFIED;
13295
13296   /* Peek at the next token.  */
13297   token = cp_lexer_peek_token (parser->lexer);
13298
13299   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13300   if (token->type == CPP_MULT)
13301     code = INDIRECT_REF;
13302   else if (token->type == CPP_AND)
13303     code = ADDR_EXPR;
13304   else if ((cxx_dialect != cxx98) &&
13305            token->type == CPP_AND_AND) /* C++0x only */
13306     code = NON_LVALUE_EXPR;
13307
13308   if (code != ERROR_MARK)
13309     {
13310       /* Consume the `*', `&' or `&&'.  */
13311       cp_lexer_consume_token (parser->lexer);
13312
13313       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13314          `&', if we are allowing GNU extensions.  (The only qualifier
13315          that can legally appear after `&' is `restrict', but that is
13316          enforced during semantic analysis.  */
13317       if (code == INDIRECT_REF
13318           || cp_parser_allow_gnu_extensions_p (parser))
13319         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13320     }
13321   else
13322     {
13323       /* Try the pointer-to-member case.  */
13324       cp_parser_parse_tentatively (parser);
13325       /* Look for the optional `::' operator.  */
13326       cp_parser_global_scope_opt (parser,
13327                                   /*current_scope_valid_p=*/false);
13328       /* Look for the nested-name specifier.  */
13329       token = cp_lexer_peek_token (parser->lexer);
13330       cp_parser_nested_name_specifier (parser,
13331                                        /*typename_keyword_p=*/false,
13332                                        /*check_dependency_p=*/true,
13333                                        /*type_p=*/false,
13334                                        /*is_declaration=*/false);
13335       /* If we found it, and the next token is a `*', then we are
13336          indeed looking at a pointer-to-member operator.  */
13337       if (!cp_parser_error_occurred (parser)
13338           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13339         {
13340           /* Indicate that the `*' operator was used.  */
13341           code = INDIRECT_REF;
13342
13343           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13344             error ("%H%qD is a namespace", &token->location, parser->scope);
13345           else
13346             {
13347               /* The type of which the member is a member is given by the
13348                  current SCOPE.  */
13349               *type = parser->scope;
13350               /* The next name will not be qualified.  */
13351               parser->scope = NULL_TREE;
13352               parser->qualifying_scope = NULL_TREE;
13353               parser->object_scope = NULL_TREE;
13354               /* Look for the optional cv-qualifier-seq.  */
13355               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13356             }
13357         }
13358       /* If that didn't work we don't have a ptr-operator.  */
13359       if (!cp_parser_parse_definitely (parser))
13360         cp_parser_error (parser, "expected ptr-operator");
13361     }
13362
13363   return code;
13364 }
13365
13366 /* Parse an (optional) cv-qualifier-seq.
13367
13368    cv-qualifier-seq:
13369      cv-qualifier cv-qualifier-seq [opt]
13370
13371    cv-qualifier:
13372      const
13373      volatile
13374
13375    GNU Extension:
13376
13377    cv-qualifier:
13378      __restrict__
13379
13380    Returns a bitmask representing the cv-qualifiers.  */
13381
13382 static cp_cv_quals
13383 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13384 {
13385   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13386
13387   while (true)
13388     {
13389       cp_token *token;
13390       cp_cv_quals cv_qualifier;
13391
13392       /* Peek at the next token.  */
13393       token = cp_lexer_peek_token (parser->lexer);
13394       /* See if it's a cv-qualifier.  */
13395       switch (token->keyword)
13396         {
13397         case RID_CONST:
13398           cv_qualifier = TYPE_QUAL_CONST;
13399           break;
13400
13401         case RID_VOLATILE:
13402           cv_qualifier = TYPE_QUAL_VOLATILE;
13403           break;
13404
13405         case RID_RESTRICT:
13406           cv_qualifier = TYPE_QUAL_RESTRICT;
13407           break;
13408
13409         default:
13410           cv_qualifier = TYPE_UNQUALIFIED;
13411           break;
13412         }
13413
13414       if (!cv_qualifier)
13415         break;
13416
13417       if (cv_quals & cv_qualifier)
13418         {
13419           error ("%Hduplicate cv-qualifier", &token->location);
13420           cp_lexer_purge_token (parser->lexer);
13421         }
13422       else
13423         {
13424           cp_lexer_consume_token (parser->lexer);
13425           cv_quals |= cv_qualifier;
13426         }
13427     }
13428
13429   return cv_quals;
13430 }
13431
13432 /* Parse a declarator-id.
13433
13434    declarator-id:
13435      id-expression
13436      :: [opt] nested-name-specifier [opt] type-name
13437
13438    In the `id-expression' case, the value returned is as for
13439    cp_parser_id_expression if the id-expression was an unqualified-id.
13440    If the id-expression was a qualified-id, then a SCOPE_REF is
13441    returned.  The first operand is the scope (either a NAMESPACE_DECL
13442    or TREE_TYPE), but the second is still just a representation of an
13443    unqualified-id.  */
13444
13445 static tree
13446 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13447 {
13448   tree id;
13449   /* The expression must be an id-expression.  Assume that qualified
13450      names are the names of types so that:
13451
13452        template <class T>
13453        int S<T>::R::i = 3;
13454
13455      will work; we must treat `S<T>::R' as the name of a type.
13456      Similarly, assume that qualified names are templates, where
13457      required, so that:
13458
13459        template <class T>
13460        int S<T>::R<T>::i = 3;
13461
13462      will work, too.  */
13463   id = cp_parser_id_expression (parser,
13464                                 /*template_keyword_p=*/false,
13465                                 /*check_dependency_p=*/false,
13466                                 /*template_p=*/NULL,
13467                                 /*declarator_p=*/true,
13468                                 optional_p);
13469   if (id && BASELINK_P (id))
13470     id = BASELINK_FUNCTIONS (id);
13471   return id;
13472 }
13473
13474 /* Parse a type-id.
13475
13476    type-id:
13477      type-specifier-seq abstract-declarator [opt]
13478
13479    Returns the TYPE specified.  */
13480
13481 static tree
13482 cp_parser_type_id (cp_parser* parser)
13483 {
13484   cp_decl_specifier_seq type_specifier_seq;
13485   cp_declarator *abstract_declarator;
13486
13487   /* Parse the type-specifier-seq.  */
13488   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13489                                 &type_specifier_seq);
13490   if (type_specifier_seq.type == error_mark_node)
13491     return error_mark_node;
13492
13493   /* There might or might not be an abstract declarator.  */
13494   cp_parser_parse_tentatively (parser);
13495   /* Look for the declarator.  */
13496   abstract_declarator
13497     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13498                             /*parenthesized_p=*/NULL,
13499                             /*member_p=*/false);
13500   /* Check to see if there really was a declarator.  */
13501   if (!cp_parser_parse_definitely (parser))
13502     abstract_declarator = NULL;
13503
13504   return groktypename (&type_specifier_seq, abstract_declarator);
13505 }
13506
13507 /* Parse a type-specifier-seq.
13508
13509    type-specifier-seq:
13510      type-specifier type-specifier-seq [opt]
13511
13512    GNU extension:
13513
13514    type-specifier-seq:
13515      attributes type-specifier-seq [opt]
13516
13517    If IS_CONDITION is true, we are at the start of a "condition",
13518    e.g., we've just seen "if (".
13519
13520    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13521
13522 static void
13523 cp_parser_type_specifier_seq (cp_parser* parser,
13524                               bool is_condition,
13525                               cp_decl_specifier_seq *type_specifier_seq)
13526 {
13527   bool seen_type_specifier = false;
13528   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13529   cp_token *start_token = NULL;
13530
13531   /* Clear the TYPE_SPECIFIER_SEQ.  */
13532   clear_decl_specs (type_specifier_seq);
13533
13534   /* Parse the type-specifiers and attributes.  */
13535   while (true)
13536     {
13537       tree type_specifier;
13538       bool is_cv_qualifier;
13539
13540       /* Check for attributes first.  */
13541       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13542         {
13543           type_specifier_seq->attributes =
13544             chainon (type_specifier_seq->attributes,
13545                      cp_parser_attributes_opt (parser));
13546           continue;
13547         }
13548
13549       /* record the token of the beginning of the type specifier seq,
13550          for error reporting purposes*/
13551      if (!start_token)
13552        start_token = cp_lexer_peek_token (parser->lexer);
13553
13554       /* Look for the type-specifier.  */
13555       type_specifier = cp_parser_type_specifier (parser,
13556                                                  flags,
13557                                                  type_specifier_seq,
13558                                                  /*is_declaration=*/false,
13559                                                  NULL,
13560                                                  &is_cv_qualifier);
13561       if (!type_specifier)
13562         {
13563           /* If the first type-specifier could not be found, this is not a
13564              type-specifier-seq at all.  */
13565           if (!seen_type_specifier)
13566             {
13567               cp_parser_error (parser, "expected type-specifier");
13568               type_specifier_seq->type = error_mark_node;
13569               return;
13570             }
13571           /* If subsequent type-specifiers could not be found, the
13572              type-specifier-seq is complete.  */
13573           break;
13574         }
13575
13576       seen_type_specifier = true;
13577       /* The standard says that a condition can be:
13578
13579             type-specifier-seq declarator = assignment-expression
13580
13581          However, given:
13582
13583            struct S {};
13584            if (int S = ...)
13585
13586          we should treat the "S" as a declarator, not as a
13587          type-specifier.  The standard doesn't say that explicitly for
13588          type-specifier-seq, but it does say that for
13589          decl-specifier-seq in an ordinary declaration.  Perhaps it
13590          would be clearer just to allow a decl-specifier-seq here, and
13591          then add a semantic restriction that if any decl-specifiers
13592          that are not type-specifiers appear, the program is invalid.  */
13593       if (is_condition && !is_cv_qualifier)
13594         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13595     }
13596
13597   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13598 }
13599
13600 /* Parse a parameter-declaration-clause.
13601
13602    parameter-declaration-clause:
13603      parameter-declaration-list [opt] ... [opt]
13604      parameter-declaration-list , ...
13605
13606    Returns a representation for the parameter declarations.  A return
13607    value of NULL indicates a parameter-declaration-clause consisting
13608    only of an ellipsis.  */
13609
13610 static cp_parameter_declarator *
13611 cp_parser_parameter_declaration_clause (cp_parser* parser)
13612 {
13613   cp_parameter_declarator *parameters;
13614   cp_token *token;
13615   bool ellipsis_p;
13616   bool is_error;
13617
13618   /* Peek at the next token.  */
13619   token = cp_lexer_peek_token (parser->lexer);
13620   /* Check for trivial parameter-declaration-clauses.  */
13621   if (token->type == CPP_ELLIPSIS)
13622     {
13623       /* Consume the `...' token.  */
13624       cp_lexer_consume_token (parser->lexer);
13625       return NULL;
13626     }
13627   else if (token->type == CPP_CLOSE_PAREN)
13628     /* There are no parameters.  */
13629     {
13630 #ifndef NO_IMPLICIT_EXTERN_C
13631       if (in_system_header && current_class_type == NULL
13632           && current_lang_name == lang_name_c)
13633         return NULL;
13634       else
13635 #endif
13636         return no_parameters;
13637     }
13638   /* Check for `(void)', too, which is a special case.  */
13639   else if (token->keyword == RID_VOID
13640            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13641                == CPP_CLOSE_PAREN))
13642     {
13643       /* Consume the `void' token.  */
13644       cp_lexer_consume_token (parser->lexer);
13645       /* There are no parameters.  */
13646       return no_parameters;
13647     }
13648
13649   /* Parse the parameter-declaration-list.  */
13650   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13651   /* If a parse error occurred while parsing the
13652      parameter-declaration-list, then the entire
13653      parameter-declaration-clause is erroneous.  */
13654   if (is_error)
13655     return NULL;
13656
13657   /* Peek at the next token.  */
13658   token = cp_lexer_peek_token (parser->lexer);
13659   /* If it's a `,', the clause should terminate with an ellipsis.  */
13660   if (token->type == CPP_COMMA)
13661     {
13662       /* Consume the `,'.  */
13663       cp_lexer_consume_token (parser->lexer);
13664       /* Expect an ellipsis.  */
13665       ellipsis_p
13666         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13667     }
13668   /* It might also be `...' if the optional trailing `,' was
13669      omitted.  */
13670   else if (token->type == CPP_ELLIPSIS)
13671     {
13672       /* Consume the `...' token.  */
13673       cp_lexer_consume_token (parser->lexer);
13674       /* And remember that we saw it.  */
13675       ellipsis_p = true;
13676     }
13677   else
13678     ellipsis_p = false;
13679
13680   /* Finish the parameter list.  */
13681   if (parameters && ellipsis_p)
13682     parameters->ellipsis_p = true;
13683
13684   return parameters;
13685 }
13686
13687 /* Parse a parameter-declaration-list.
13688
13689    parameter-declaration-list:
13690      parameter-declaration
13691      parameter-declaration-list , parameter-declaration
13692
13693    Returns a representation of the parameter-declaration-list, as for
13694    cp_parser_parameter_declaration_clause.  However, the
13695    `void_list_node' is never appended to the list.  Upon return,
13696    *IS_ERROR will be true iff an error occurred.  */
13697
13698 static cp_parameter_declarator *
13699 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13700 {
13701   cp_parameter_declarator *parameters = NULL;
13702   cp_parameter_declarator **tail = &parameters;
13703   bool saved_in_unbraced_linkage_specification_p;
13704
13705   /* Assume all will go well.  */
13706   *is_error = false;
13707   /* The special considerations that apply to a function within an
13708      unbraced linkage specifications do not apply to the parameters
13709      to the function.  */
13710   saved_in_unbraced_linkage_specification_p 
13711     = parser->in_unbraced_linkage_specification_p;
13712   parser->in_unbraced_linkage_specification_p = false;
13713
13714   /* Look for more parameters.  */
13715   while (true)
13716     {
13717       cp_parameter_declarator *parameter;
13718       bool parenthesized_p;
13719       /* Parse the parameter.  */
13720       parameter
13721         = cp_parser_parameter_declaration (parser,
13722                                            /*template_parm_p=*/false,
13723                                            &parenthesized_p);
13724
13725       /* If a parse error occurred parsing the parameter declaration,
13726          then the entire parameter-declaration-list is erroneous.  */
13727       if (!parameter)
13728         {
13729           *is_error = true;
13730           parameters = NULL;
13731           break;
13732         }
13733       /* Add the new parameter to the list.  */
13734       *tail = parameter;
13735       tail = &parameter->next;
13736
13737       /* Peek at the next token.  */
13738       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13739           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13740           /* These are for Objective-C++ */
13741           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13742           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13743         /* The parameter-declaration-list is complete.  */
13744         break;
13745       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13746         {
13747           cp_token *token;
13748
13749           /* Peek at the next token.  */
13750           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13751           /* If it's an ellipsis, then the list is complete.  */
13752           if (token->type == CPP_ELLIPSIS)
13753             break;
13754           /* Otherwise, there must be more parameters.  Consume the
13755              `,'.  */
13756           cp_lexer_consume_token (parser->lexer);
13757           /* When parsing something like:
13758
13759                 int i(float f, double d)
13760
13761              we can tell after seeing the declaration for "f" that we
13762              are not looking at an initialization of a variable "i",
13763              but rather at the declaration of a function "i".
13764
13765              Due to the fact that the parsing of template arguments
13766              (as specified to a template-id) requires backtracking we
13767              cannot use this technique when inside a template argument
13768              list.  */
13769           if (!parser->in_template_argument_list_p
13770               && !parser->in_type_id_in_expr_p
13771               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13772               /* However, a parameter-declaration of the form
13773                  "foat(f)" (which is a valid declaration of a
13774                  parameter "f") can also be interpreted as an
13775                  expression (the conversion of "f" to "float").  */
13776               && !parenthesized_p)
13777             cp_parser_commit_to_tentative_parse (parser);
13778         }
13779       else
13780         {
13781           cp_parser_error (parser, "expected %<,%> or %<...%>");
13782           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13783             cp_parser_skip_to_closing_parenthesis (parser,
13784                                                    /*recovering=*/true,
13785                                                    /*or_comma=*/false,
13786                                                    /*consume_paren=*/false);
13787           break;
13788         }
13789     }
13790
13791   parser->in_unbraced_linkage_specification_p
13792     = saved_in_unbraced_linkage_specification_p;
13793
13794   return parameters;
13795 }
13796
13797 /* Parse a parameter declaration.
13798
13799    parameter-declaration:
13800      decl-specifier-seq ... [opt] declarator
13801      decl-specifier-seq declarator = assignment-expression
13802      decl-specifier-seq ... [opt] abstract-declarator [opt]
13803      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13804
13805    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13806    declares a template parameter.  (In that case, a non-nested `>'
13807    token encountered during the parsing of the assignment-expression
13808    is not interpreted as a greater-than operator.)
13809
13810    Returns a representation of the parameter, or NULL if an error
13811    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13812    true iff the declarator is of the form "(p)".  */
13813
13814 static cp_parameter_declarator *
13815 cp_parser_parameter_declaration (cp_parser *parser,
13816                                  bool template_parm_p,
13817                                  bool *parenthesized_p)
13818 {
13819   int declares_class_or_enum;
13820   bool greater_than_is_operator_p;
13821   cp_decl_specifier_seq decl_specifiers;
13822   cp_declarator *declarator;
13823   tree default_argument;
13824   cp_token *token = NULL, *declarator_token_start = NULL;
13825   const char *saved_message;
13826
13827   /* In a template parameter, `>' is not an operator.
13828
13829      [temp.param]
13830
13831      When parsing a default template-argument for a non-type
13832      template-parameter, the first non-nested `>' is taken as the end
13833      of the template parameter-list rather than a greater-than
13834      operator.  */
13835   greater_than_is_operator_p = !template_parm_p;
13836
13837   /* Type definitions may not appear in parameter types.  */
13838   saved_message = parser->type_definition_forbidden_message;
13839   parser->type_definition_forbidden_message
13840     = "types may not be defined in parameter types";
13841
13842   /* Parse the declaration-specifiers.  */
13843   cp_parser_decl_specifier_seq (parser,
13844                                 CP_PARSER_FLAGS_NONE,
13845                                 &decl_specifiers,
13846                                 &declares_class_or_enum);
13847   /* If an error occurred, there's no reason to attempt to parse the
13848      rest of the declaration.  */
13849   if (cp_parser_error_occurred (parser))
13850     {
13851       parser->type_definition_forbidden_message = saved_message;
13852       return NULL;
13853     }
13854
13855   /* Peek at the next token.  */
13856   token = cp_lexer_peek_token (parser->lexer);
13857
13858   /* If the next token is a `)', `,', `=', `>', or `...', then there
13859      is no declarator. However, when variadic templates are enabled,
13860      there may be a declarator following `...'.  */
13861   if (token->type == CPP_CLOSE_PAREN
13862       || token->type == CPP_COMMA
13863       || token->type == CPP_EQ
13864       || token->type == CPP_GREATER)
13865     {
13866       declarator = NULL;
13867       if (parenthesized_p)
13868         *parenthesized_p = false;
13869     }
13870   /* Otherwise, there should be a declarator.  */
13871   else
13872     {
13873       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13874       parser->default_arg_ok_p = false;
13875
13876       /* After seeing a decl-specifier-seq, if the next token is not a
13877          "(", there is no possibility that the code is a valid
13878          expression.  Therefore, if parsing tentatively, we commit at
13879          this point.  */
13880       if (!parser->in_template_argument_list_p
13881           /* In an expression context, having seen:
13882
13883                (int((char ...
13884
13885              we cannot be sure whether we are looking at a
13886              function-type (taking a "char" as a parameter) or a cast
13887              of some object of type "char" to "int".  */
13888           && !parser->in_type_id_in_expr_p
13889           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13890           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13891         cp_parser_commit_to_tentative_parse (parser);
13892       /* Parse the declarator.  */
13893       declarator_token_start = token;
13894       declarator = cp_parser_declarator (parser,
13895                                          CP_PARSER_DECLARATOR_EITHER,
13896                                          /*ctor_dtor_or_conv_p=*/NULL,
13897                                          parenthesized_p,
13898                                          /*member_p=*/false);
13899       parser->default_arg_ok_p = saved_default_arg_ok_p;
13900       /* After the declarator, allow more attributes.  */
13901       decl_specifiers.attributes
13902         = chainon (decl_specifiers.attributes,
13903                    cp_parser_attributes_opt (parser));
13904     }
13905
13906   /* If the next token is an ellipsis, and we have not seen a
13907      declarator name, and the type of the declarator contains parameter
13908      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13909      a parameter pack expansion expression. Otherwise, leave the
13910      ellipsis for a C-style variadic function. */
13911   token = cp_lexer_peek_token (parser->lexer);
13912   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13913     {
13914       tree type = decl_specifiers.type;
13915
13916       if (type && DECL_P (type))
13917         type = TREE_TYPE (type);
13918
13919       if (type
13920           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13921           && declarator_can_be_parameter_pack (declarator)
13922           && (!declarator || !declarator->parameter_pack_p)
13923           && uses_parameter_packs (type))
13924         {
13925           /* Consume the `...'. */
13926           cp_lexer_consume_token (parser->lexer);
13927           maybe_warn_variadic_templates ();
13928           
13929           /* Build a pack expansion type */
13930           if (declarator)
13931             declarator->parameter_pack_p = true;
13932           else
13933             decl_specifiers.type = make_pack_expansion (type);
13934         }
13935     }
13936
13937   /* The restriction on defining new types applies only to the type
13938      of the parameter, not to the default argument.  */
13939   parser->type_definition_forbidden_message = saved_message;
13940
13941   /* If the next token is `=', then process a default argument.  */
13942   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13943     {
13944       /* Consume the `='.  */
13945       cp_lexer_consume_token (parser->lexer);
13946
13947       /* If we are defining a class, then the tokens that make up the
13948          default argument must be saved and processed later.  */
13949       if (!template_parm_p && at_class_scope_p ()
13950           && TYPE_BEING_DEFINED (current_class_type))
13951         {
13952           unsigned depth = 0;
13953           int maybe_template_id = 0;
13954           cp_token *first_token;
13955           cp_token *token;
13956
13957           /* Add tokens until we have processed the entire default
13958              argument.  We add the range [first_token, token).  */
13959           first_token = cp_lexer_peek_token (parser->lexer);
13960           while (true)
13961             {
13962               bool done = false;
13963
13964               /* Peek at the next token.  */
13965               token = cp_lexer_peek_token (parser->lexer);
13966               /* What we do depends on what token we have.  */
13967               switch (token->type)
13968                 {
13969                   /* In valid code, a default argument must be
13970                      immediately followed by a `,' `)', or `...'.  */
13971                 case CPP_COMMA:
13972                   if (depth == 0 && maybe_template_id)
13973                     {
13974                       /* If we've seen a '<', we might be in a
13975                          template-argument-list.  Until Core issue 325 is
13976                          resolved, we don't know how this situation ought
13977                          to be handled, so try to DTRT.  We check whether
13978                          what comes after the comma is a valid parameter
13979                          declaration list.  If it is, then the comma ends
13980                          the default argument; otherwise the default
13981                          argument continues.  */
13982                       bool error = false;
13983
13984                       /* Set ITALP so cp_parser_parameter_declaration_list
13985                          doesn't decide to commit to this parse.  */
13986                       bool saved_italp = parser->in_template_argument_list_p;
13987                       parser->in_template_argument_list_p = true;
13988
13989                       cp_parser_parse_tentatively (parser);
13990                       cp_lexer_consume_token (parser->lexer);
13991                       cp_parser_parameter_declaration_list (parser, &error);
13992                       if (!cp_parser_error_occurred (parser) && !error)
13993                         done = true;
13994                       cp_parser_abort_tentative_parse (parser);
13995
13996                       parser->in_template_argument_list_p = saved_italp;
13997                       break;
13998                     }
13999                 case CPP_CLOSE_PAREN:
14000                 case CPP_ELLIPSIS:
14001                   /* If we run into a non-nested `;', `}', or `]',
14002                      then the code is invalid -- but the default
14003                      argument is certainly over.  */
14004                 case CPP_SEMICOLON:
14005                 case CPP_CLOSE_BRACE:
14006                 case CPP_CLOSE_SQUARE:
14007                   if (depth == 0)
14008                     done = true;
14009                   /* Update DEPTH, if necessary.  */
14010                   else if (token->type == CPP_CLOSE_PAREN
14011                            || token->type == CPP_CLOSE_BRACE
14012                            || token->type == CPP_CLOSE_SQUARE)
14013                     --depth;
14014                   break;
14015
14016                 case CPP_OPEN_PAREN:
14017                 case CPP_OPEN_SQUARE:
14018                 case CPP_OPEN_BRACE:
14019                   ++depth;
14020                   break;
14021
14022                 case CPP_LESS:
14023                   if (depth == 0)
14024                     /* This might be the comparison operator, or it might
14025                        start a template argument list.  */
14026                     ++maybe_template_id;
14027                   break;
14028
14029                 case CPP_RSHIFT:
14030                   if (cxx_dialect == cxx98)
14031                     break;
14032                   /* Fall through for C++0x, which treats the `>>'
14033                      operator like two `>' tokens in certain
14034                      cases.  */
14035
14036                 case CPP_GREATER:
14037                   if (depth == 0)
14038                     {
14039                       /* This might be an operator, or it might close a
14040                          template argument list.  But if a previous '<'
14041                          started a template argument list, this will have
14042                          closed it, so we can't be in one anymore.  */
14043                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14044                       if (maybe_template_id < 0)
14045                         maybe_template_id = 0;
14046                     }
14047                   break;
14048
14049                   /* If we run out of tokens, issue an error message.  */
14050                 case CPP_EOF:
14051                 case CPP_PRAGMA_EOL:
14052                   error ("%Hfile ends in default argument", &token->location);
14053                   done = true;
14054                   break;
14055
14056                 case CPP_NAME:
14057                 case CPP_SCOPE:
14058                   /* In these cases, we should look for template-ids.
14059                      For example, if the default argument is
14060                      `X<int, double>()', we need to do name lookup to
14061                      figure out whether or not `X' is a template; if
14062                      so, the `,' does not end the default argument.
14063
14064                      That is not yet done.  */
14065                   break;
14066
14067                 default:
14068                   break;
14069                 }
14070
14071               /* If we've reached the end, stop.  */
14072               if (done)
14073                 break;
14074
14075               /* Add the token to the token block.  */
14076               token = cp_lexer_consume_token (parser->lexer);
14077             }
14078
14079           /* Create a DEFAULT_ARG to represent the unparsed default
14080              argument.  */
14081           default_argument = make_node (DEFAULT_ARG);
14082           DEFARG_TOKENS (default_argument)
14083             = cp_token_cache_new (first_token, token);
14084           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14085         }
14086       /* Outside of a class definition, we can just parse the
14087          assignment-expression.  */
14088       else
14089         {
14090           token = cp_lexer_peek_token (parser->lexer);
14091           default_argument 
14092             = cp_parser_default_argument (parser, template_parm_p);
14093         }
14094
14095       if (!parser->default_arg_ok_p)
14096         {
14097           if (flag_permissive)
14098             warning (0, "deprecated use of default argument for parameter of non-function");
14099           else
14100             {
14101               error ("%Hdefault arguments are only "
14102                      "permitted for function parameters",
14103                      &token->location);
14104               default_argument = NULL_TREE;
14105             }
14106         }
14107       else if ((declarator && declarator->parameter_pack_p)
14108                || (decl_specifiers.type
14109                    && PACK_EXPANSION_P (decl_specifiers.type)))
14110         {
14111           const char* kind = template_parm_p? "template " : "";
14112           
14113           /* Find the name of the parameter pack.  */     
14114           cp_declarator *id_declarator = declarator;
14115           while (id_declarator && id_declarator->kind != cdk_id)
14116             id_declarator = id_declarator->declarator;
14117           
14118           if (id_declarator && id_declarator->kind == cdk_id)
14119             error ("%H%sparameter pack %qD cannot have a default argument",
14120                    &declarator_token_start->location,
14121                    kind, id_declarator->u.id.unqualified_name);
14122           else
14123             error ("%H%sparameter pack cannot have a default argument",
14124                    &declarator_token_start->location, kind);
14125           
14126           default_argument = NULL_TREE;
14127         }
14128     }
14129   else
14130     default_argument = NULL_TREE;
14131
14132   return make_parameter_declarator (&decl_specifiers,
14133                                     declarator,
14134                                     default_argument);
14135 }
14136
14137 /* Parse a default argument and return it.
14138
14139    TEMPLATE_PARM_P is true if this is a default argument for a
14140    non-type template parameter.  */
14141 static tree
14142 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14143 {
14144   tree default_argument = NULL_TREE;
14145   bool saved_greater_than_is_operator_p;
14146   bool saved_local_variables_forbidden_p;
14147
14148   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14149      set correctly.  */
14150   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14151   parser->greater_than_is_operator_p = !template_parm_p;
14152   /* Local variable names (and the `this' keyword) may not
14153      appear in a default argument.  */
14154   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14155   parser->local_variables_forbidden_p = true;
14156   /* The default argument expression may cause implicitly
14157      defined member functions to be synthesized, which will
14158      result in garbage collection.  We must treat this
14159      situation as if we were within the body of function so as
14160      to avoid collecting live data on the stack.  */
14161   ++function_depth;
14162   /* Parse the assignment-expression.  */
14163   if (template_parm_p)
14164     push_deferring_access_checks (dk_no_deferred);
14165   default_argument
14166     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14167   if (template_parm_p)
14168     pop_deferring_access_checks ();
14169   /* Restore saved state.  */
14170   --function_depth;
14171   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14172   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14173
14174   return default_argument;
14175 }
14176
14177 /* Parse a function-body.
14178
14179    function-body:
14180      compound_statement  */
14181
14182 static void
14183 cp_parser_function_body (cp_parser *parser)
14184 {
14185   cp_parser_compound_statement (parser, NULL, false);
14186 }
14187
14188 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14189    true if a ctor-initializer was present.  */
14190
14191 static bool
14192 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14193 {
14194   tree body;
14195   bool ctor_initializer_p;
14196
14197   /* Begin the function body.  */
14198   body = begin_function_body ();
14199   /* Parse the optional ctor-initializer.  */
14200   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14201   /* Parse the function-body.  */
14202   cp_parser_function_body (parser);
14203   /* Finish the function body.  */
14204   finish_function_body (body);
14205
14206   return ctor_initializer_p;
14207 }
14208
14209 /* Parse an initializer.
14210
14211    initializer:
14212      = initializer-clause
14213      ( expression-list )
14214
14215    Returns an expression representing the initializer.  If no
14216    initializer is present, NULL_TREE is returned.
14217
14218    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14219    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14220    set to TRUE if there is no initializer present.  If there is an
14221    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14222    is set to true; otherwise it is set to false.  */
14223
14224 static tree
14225 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14226                        bool* non_constant_p)
14227 {
14228   cp_token *token;
14229   tree init;
14230
14231   /* Peek at the next token.  */
14232   token = cp_lexer_peek_token (parser->lexer);
14233
14234   /* Let our caller know whether or not this initializer was
14235      parenthesized.  */
14236   *is_direct_init = (token->type != CPP_EQ);
14237   /* Assume that the initializer is constant.  */
14238   *non_constant_p = false;
14239
14240   if (token->type == CPP_EQ)
14241     {
14242       /* Consume the `='.  */
14243       cp_lexer_consume_token (parser->lexer);
14244       /* Parse the initializer-clause.  */
14245       init = cp_parser_initializer_clause (parser, non_constant_p);
14246     }
14247   else if (token->type == CPP_OPEN_PAREN)
14248     init = cp_parser_parenthesized_expression_list (parser, false,
14249                                                     /*cast_p=*/false,
14250                                                     /*allow_expansion_p=*/true,
14251                                                     non_constant_p);
14252   else if (token->type == CPP_OPEN_BRACE)
14253     {
14254       maybe_warn_cpp0x ("extended initializer lists");
14255       init = cp_parser_braced_list (parser, non_constant_p);
14256       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14257     }
14258   else
14259     {
14260       /* Anything else is an error.  */
14261       cp_parser_error (parser, "expected initializer");
14262       init = error_mark_node;
14263     }
14264
14265   return init;
14266 }
14267
14268 /* Parse an initializer-clause.
14269
14270    initializer-clause:
14271      assignment-expression
14272      braced-init-list
14273
14274    Returns an expression representing the initializer.
14275
14276    If the `assignment-expression' production is used the value
14277    returned is simply a representation for the expression.
14278
14279    Otherwise, calls cp_parser_braced_list.  */
14280
14281 static tree
14282 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14283 {
14284   tree initializer;
14285
14286   /* Assume the expression is constant.  */
14287   *non_constant_p = false;
14288
14289   /* If it is not a `{', then we are looking at an
14290      assignment-expression.  */
14291   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14292     {
14293       initializer
14294         = cp_parser_constant_expression (parser,
14295                                         /*allow_non_constant_p=*/true,
14296                                         non_constant_p);
14297       if (!*non_constant_p)
14298         initializer = fold_non_dependent_expr (initializer);
14299     }
14300   else
14301     initializer = cp_parser_braced_list (parser, non_constant_p);
14302
14303   return initializer;
14304 }
14305
14306 /* Parse a brace-enclosed initializer list.
14307
14308    braced-init-list:
14309      { initializer-list , [opt] }
14310      { }
14311
14312    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14313    the elements of the initializer-list (or NULL, if the last
14314    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14315    NULL_TREE.  There is no way to detect whether or not the optional
14316    trailing `,' was provided.  NON_CONSTANT_P is as for
14317    cp_parser_initializer.  */     
14318
14319 static tree
14320 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14321 {
14322   tree initializer;
14323
14324   /* Consume the `{' token.  */
14325   cp_lexer_consume_token (parser->lexer);
14326   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14327   initializer = make_node (CONSTRUCTOR);
14328   /* If it's not a `}', then there is a non-trivial initializer.  */
14329   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14330     {
14331       /* Parse the initializer list.  */
14332       CONSTRUCTOR_ELTS (initializer)
14333         = cp_parser_initializer_list (parser, non_constant_p);
14334       /* A trailing `,' token is allowed.  */
14335       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14336         cp_lexer_consume_token (parser->lexer);
14337     }
14338   /* Now, there should be a trailing `}'.  */
14339   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14340   TREE_TYPE (initializer) = init_list_type_node;
14341   return initializer;
14342 }
14343
14344 /* Parse an initializer-list.
14345
14346    initializer-list:
14347      initializer-clause ... [opt]
14348      initializer-list , initializer-clause ... [opt]
14349
14350    GNU Extension:
14351
14352    initializer-list:
14353      identifier : initializer-clause
14354      initializer-list, identifier : initializer-clause
14355
14356    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14357    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14358    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14359    as for cp_parser_initializer.  */
14360
14361 static VEC(constructor_elt,gc) *
14362 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14363 {
14364   VEC(constructor_elt,gc) *v = NULL;
14365
14366   /* Assume all of the expressions are constant.  */
14367   *non_constant_p = false;
14368
14369   /* Parse the rest of the list.  */
14370   while (true)
14371     {
14372       cp_token *token;
14373       tree identifier;
14374       tree initializer;
14375       bool clause_non_constant_p;
14376
14377       /* If the next token is an identifier and the following one is a
14378          colon, we are looking at the GNU designated-initializer
14379          syntax.  */
14380       if (cp_parser_allow_gnu_extensions_p (parser)
14381           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14382           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14383         {
14384           /* Warn the user that they are using an extension.  */
14385           pedwarn (input_location, OPT_pedantic, 
14386                    "ISO C++ does not allow designated initializers");
14387           /* Consume the identifier.  */
14388           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14389           /* Consume the `:'.  */
14390           cp_lexer_consume_token (parser->lexer);
14391         }
14392       else
14393         identifier = NULL_TREE;
14394
14395       /* Parse the initializer.  */
14396       initializer = cp_parser_initializer_clause (parser,
14397                                                   &clause_non_constant_p);
14398       /* If any clause is non-constant, so is the entire initializer.  */
14399       if (clause_non_constant_p)
14400         *non_constant_p = true;
14401
14402       /* If we have an ellipsis, this is an initializer pack
14403          expansion.  */
14404       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14405         {
14406           /* Consume the `...'.  */
14407           cp_lexer_consume_token (parser->lexer);
14408
14409           /* Turn the initializer into an initializer expansion.  */
14410           initializer = make_pack_expansion (initializer);
14411         }
14412
14413       /* Add it to the vector.  */
14414       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14415
14416       /* If the next token is not a comma, we have reached the end of
14417          the list.  */
14418       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14419         break;
14420
14421       /* Peek at the next token.  */
14422       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14423       /* If the next token is a `}', then we're still done.  An
14424          initializer-clause can have a trailing `,' after the
14425          initializer-list and before the closing `}'.  */
14426       if (token->type == CPP_CLOSE_BRACE)
14427         break;
14428
14429       /* Consume the `,' token.  */
14430       cp_lexer_consume_token (parser->lexer);
14431     }
14432
14433   return v;
14434 }
14435
14436 /* Classes [gram.class] */
14437
14438 /* Parse a class-name.
14439
14440    class-name:
14441      identifier
14442      template-id
14443
14444    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14445    to indicate that names looked up in dependent types should be
14446    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14447    keyword has been used to indicate that the name that appears next
14448    is a template.  TAG_TYPE indicates the explicit tag given before
14449    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14450    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14451    is the class being defined in a class-head.
14452
14453    Returns the TYPE_DECL representing the class.  */
14454
14455 static tree
14456 cp_parser_class_name (cp_parser *parser,
14457                       bool typename_keyword_p,
14458                       bool template_keyword_p,
14459                       enum tag_types tag_type,
14460                       bool check_dependency_p,
14461                       bool class_head_p,
14462                       bool is_declaration)
14463 {
14464   tree decl;
14465   tree scope;
14466   bool typename_p;
14467   cp_token *token;
14468
14469   /* All class-names start with an identifier.  */
14470   token = cp_lexer_peek_token (parser->lexer);
14471   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14472     {
14473       cp_parser_error (parser, "expected class-name");
14474       return error_mark_node;
14475     }
14476
14477   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14478      to a template-id, so we save it here.  */
14479   scope = parser->scope;
14480   if (scope == error_mark_node)
14481     return error_mark_node;
14482
14483   /* Any name names a type if we're following the `typename' keyword
14484      in a qualified name where the enclosing scope is type-dependent.  */
14485   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14486                 && dependent_type_p (scope));
14487   /* Handle the common case (an identifier, but not a template-id)
14488      efficiently.  */
14489   if (token->type == CPP_NAME
14490       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14491     {
14492       cp_token *identifier_token;
14493       tree identifier;
14494       bool ambiguous_p;
14495
14496       /* Look for the identifier.  */
14497       identifier_token = cp_lexer_peek_token (parser->lexer);
14498       ambiguous_p = identifier_token->ambiguous_p;
14499       identifier = cp_parser_identifier (parser);
14500       /* If the next token isn't an identifier, we are certainly not
14501          looking at a class-name.  */
14502       if (identifier == error_mark_node)
14503         decl = error_mark_node;
14504       /* If we know this is a type-name, there's no need to look it
14505          up.  */
14506       else if (typename_p)
14507         decl = identifier;
14508       else
14509         {
14510           tree ambiguous_decls;
14511           /* If we already know that this lookup is ambiguous, then
14512              we've already issued an error message; there's no reason
14513              to check again.  */
14514           if (ambiguous_p)
14515             {
14516               cp_parser_simulate_error (parser);
14517               return error_mark_node;
14518             }
14519           /* If the next token is a `::', then the name must be a type
14520              name.
14521
14522              [basic.lookup.qual]
14523
14524              During the lookup for a name preceding the :: scope
14525              resolution operator, object, function, and enumerator
14526              names are ignored.  */
14527           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14528             tag_type = typename_type;
14529           /* Look up the name.  */
14530           decl = cp_parser_lookup_name (parser, identifier,
14531                                         tag_type,
14532                                         /*is_template=*/false,
14533                                         /*is_namespace=*/false,
14534                                         check_dependency_p,
14535                                         &ambiguous_decls,
14536                                         identifier_token->location);
14537           if (ambiguous_decls)
14538             {
14539               error ("%Hreference to %qD is ambiguous",
14540                      &identifier_token->location, identifier);
14541               print_candidates (ambiguous_decls);
14542               if (cp_parser_parsing_tentatively (parser))
14543                 {
14544                   identifier_token->ambiguous_p = true;
14545                   cp_parser_simulate_error (parser);
14546                 }
14547               return error_mark_node;
14548             }
14549         }
14550     }
14551   else
14552     {
14553       /* Try a template-id.  */
14554       decl = cp_parser_template_id (parser, template_keyword_p,
14555                                     check_dependency_p,
14556                                     is_declaration);
14557       if (decl == error_mark_node)
14558         return error_mark_node;
14559     }
14560
14561   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14562
14563   /* If this is a typename, create a TYPENAME_TYPE.  */
14564   if (typename_p && decl != error_mark_node)
14565     {
14566       decl = make_typename_type (scope, decl, typename_type,
14567                                  /*complain=*/tf_error);
14568       if (decl != error_mark_node)
14569         decl = TYPE_NAME (decl);
14570     }
14571
14572   /* Check to see that it is really the name of a class.  */
14573   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14574       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14575       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14576     /* Situations like this:
14577
14578          template <typename T> struct A {
14579            typename T::template X<int>::I i;
14580          };
14581
14582        are problematic.  Is `T::template X<int>' a class-name?  The
14583        standard does not seem to be definitive, but there is no other
14584        valid interpretation of the following `::'.  Therefore, those
14585        names are considered class-names.  */
14586     {
14587       decl = make_typename_type (scope, decl, tag_type, tf_error);
14588       if (decl != error_mark_node)
14589         decl = TYPE_NAME (decl);
14590     }
14591   else if (TREE_CODE (decl) != TYPE_DECL
14592            || TREE_TYPE (decl) == error_mark_node
14593            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14594     decl = error_mark_node;
14595
14596   if (decl == error_mark_node)
14597     cp_parser_error (parser, "expected class-name");
14598
14599   return decl;
14600 }
14601
14602 /* Parse a class-specifier.
14603
14604    class-specifier:
14605      class-head { member-specification [opt] }
14606
14607    Returns the TREE_TYPE representing the class.  */
14608
14609 static tree
14610 cp_parser_class_specifier (cp_parser* parser)
14611 {
14612   cp_token *token;
14613   tree type;
14614   tree attributes = NULL_TREE;
14615   int has_trailing_semicolon;
14616   bool nested_name_specifier_p;
14617   unsigned saved_num_template_parameter_lists;
14618   bool saved_in_function_body;
14619   tree old_scope = NULL_TREE;
14620   tree scope = NULL_TREE;
14621   tree bases;
14622
14623   push_deferring_access_checks (dk_no_deferred);
14624
14625   /* Parse the class-head.  */
14626   type = cp_parser_class_head (parser,
14627                                &nested_name_specifier_p,
14628                                &attributes,
14629                                &bases);
14630   /* If the class-head was a semantic disaster, skip the entire body
14631      of the class.  */
14632   if (!type)
14633     {
14634       cp_parser_skip_to_end_of_block_or_statement (parser);
14635       pop_deferring_access_checks ();
14636       return error_mark_node;
14637     }
14638
14639   /* Look for the `{'.  */
14640   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14641     {
14642       pop_deferring_access_checks ();
14643       return error_mark_node;
14644     }
14645
14646   /* Process the base classes. If they're invalid, skip the 
14647      entire class body.  */
14648   if (!xref_basetypes (type, bases))
14649     {
14650       /* Consuming the closing brace yields better error messages
14651          later on.  */
14652       if (cp_parser_skip_to_closing_brace (parser))
14653         cp_lexer_consume_token (parser->lexer);
14654       pop_deferring_access_checks ();
14655       return error_mark_node;
14656     }
14657
14658   /* Issue an error message if type-definitions are forbidden here.  */
14659   cp_parser_check_type_definition (parser);
14660   /* Remember that we are defining one more class.  */
14661   ++parser->num_classes_being_defined;
14662   /* Inside the class, surrounding template-parameter-lists do not
14663      apply.  */
14664   saved_num_template_parameter_lists
14665     = parser->num_template_parameter_lists;
14666   parser->num_template_parameter_lists = 0;
14667   /* We are not in a function body.  */
14668   saved_in_function_body = parser->in_function_body;
14669   parser->in_function_body = false;
14670
14671   /* Start the class.  */
14672   if (nested_name_specifier_p)
14673     {
14674       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14675       old_scope = push_inner_scope (scope);
14676     }
14677   type = begin_class_definition (type, attributes);
14678
14679   if (type == error_mark_node)
14680     /* If the type is erroneous, skip the entire body of the class.  */
14681     cp_parser_skip_to_closing_brace (parser);
14682   else
14683     /* Parse the member-specification.  */
14684     cp_parser_member_specification_opt (parser);
14685
14686   /* Look for the trailing `}'.  */
14687   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14688   /* We get better error messages by noticing a common problem: a
14689      missing trailing `;'.  */
14690   token = cp_lexer_peek_token (parser->lexer);
14691   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14692   /* Look for trailing attributes to apply to this class.  */
14693   if (cp_parser_allow_gnu_extensions_p (parser))
14694     attributes = cp_parser_attributes_opt (parser);
14695   if (type != error_mark_node)
14696     type = finish_struct (type, attributes);
14697   if (nested_name_specifier_p)
14698     pop_inner_scope (old_scope, scope);
14699   /* If this class is not itself within the scope of another class,
14700      then we need to parse the bodies of all of the queued function
14701      definitions.  Note that the queued functions defined in a class
14702      are not always processed immediately following the
14703      class-specifier for that class.  Consider:
14704
14705        struct A {
14706          struct B { void f() { sizeof (A); } };
14707        };
14708
14709      If `f' were processed before the processing of `A' were
14710      completed, there would be no way to compute the size of `A'.
14711      Note that the nesting we are interested in here is lexical --
14712      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14713      for:
14714
14715        struct A { struct B; };
14716        struct A::B { void f() { } };
14717
14718      there is no need to delay the parsing of `A::B::f'.  */
14719   if (--parser->num_classes_being_defined == 0)
14720     {
14721       tree queue_entry;
14722       tree fn;
14723       tree class_type = NULL_TREE;
14724       tree pushed_scope = NULL_TREE;
14725
14726       /* In a first pass, parse default arguments to the functions.
14727          Then, in a second pass, parse the bodies of the functions.
14728          This two-phased approach handles cases like:
14729
14730             struct S {
14731               void f() { g(); }
14732               void g(int i = 3);
14733             };
14734
14735          */
14736       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14737              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14738            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14739            TREE_PURPOSE (parser->unparsed_functions_queues)
14740              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14741         {
14742           fn = TREE_VALUE (queue_entry);
14743           /* If there are default arguments that have not yet been processed,
14744              take care of them now.  */
14745           if (class_type != TREE_PURPOSE (queue_entry))
14746             {
14747               if (pushed_scope)
14748                 pop_scope (pushed_scope);
14749               class_type = TREE_PURPOSE (queue_entry);
14750               pushed_scope = push_scope (class_type);
14751             }
14752           /* Make sure that any template parameters are in scope.  */
14753           maybe_begin_member_template_processing (fn);
14754           /* Parse the default argument expressions.  */
14755           cp_parser_late_parsing_default_args (parser, fn);
14756           /* Remove any template parameters from the symbol table.  */
14757           maybe_end_member_template_processing ();
14758         }
14759       if (pushed_scope)
14760         pop_scope (pushed_scope);
14761       /* Now parse the body of the functions.  */
14762       for (TREE_VALUE (parser->unparsed_functions_queues)
14763              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14764            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14765            TREE_VALUE (parser->unparsed_functions_queues)
14766              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14767         {
14768           /* Figure out which function we need to process.  */
14769           fn = TREE_VALUE (queue_entry);
14770           /* Parse the function.  */
14771           cp_parser_late_parsing_for_member (parser, fn);
14772         }
14773     }
14774
14775   /* Put back any saved access checks.  */
14776   pop_deferring_access_checks ();
14777
14778   /* Restore saved state.  */
14779   parser->in_function_body = saved_in_function_body;
14780   parser->num_template_parameter_lists
14781     = saved_num_template_parameter_lists;
14782
14783   return type;
14784 }
14785
14786 /* Parse a class-head.
14787
14788    class-head:
14789      class-key identifier [opt] base-clause [opt]
14790      class-key nested-name-specifier identifier base-clause [opt]
14791      class-key nested-name-specifier [opt] template-id
14792        base-clause [opt]
14793
14794    GNU Extensions:
14795      class-key attributes identifier [opt] base-clause [opt]
14796      class-key attributes nested-name-specifier identifier base-clause [opt]
14797      class-key attributes nested-name-specifier [opt] template-id
14798        base-clause [opt]
14799
14800    Upon return BASES is initialized to the list of base classes (or
14801    NULL, if there are none) in the same form returned by
14802    cp_parser_base_clause.
14803
14804    Returns the TYPE of the indicated class.  Sets
14805    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14806    involving a nested-name-specifier was used, and FALSE otherwise.
14807
14808    Returns error_mark_node if this is not a class-head.
14809
14810    Returns NULL_TREE if the class-head is syntactically valid, but
14811    semantically invalid in a way that means we should skip the entire
14812    body of the class.  */
14813
14814 static tree
14815 cp_parser_class_head (cp_parser* parser,
14816                       bool* nested_name_specifier_p,
14817                       tree *attributes_p,
14818                       tree *bases)
14819 {
14820   tree nested_name_specifier;
14821   enum tag_types class_key;
14822   tree id = NULL_TREE;
14823   tree type = NULL_TREE;
14824   tree attributes;
14825   bool template_id_p = false;
14826   bool qualified_p = false;
14827   bool invalid_nested_name_p = false;
14828   bool invalid_explicit_specialization_p = false;
14829   tree pushed_scope = NULL_TREE;
14830   unsigned num_templates;
14831   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14832   /* Assume no nested-name-specifier will be present.  */
14833   *nested_name_specifier_p = false;
14834   /* Assume no template parameter lists will be used in defining the
14835      type.  */
14836   num_templates = 0;
14837
14838   *bases = NULL_TREE;
14839
14840   /* Look for the class-key.  */
14841   class_key = cp_parser_class_key (parser);
14842   if (class_key == none_type)
14843     return error_mark_node;
14844
14845   /* Parse the attributes.  */
14846   attributes = cp_parser_attributes_opt (parser);
14847
14848   /* If the next token is `::', that is invalid -- but sometimes
14849      people do try to write:
14850
14851        struct ::S {};
14852
14853      Handle this gracefully by accepting the extra qualifier, and then
14854      issuing an error about it later if this really is a
14855      class-head.  If it turns out just to be an elaborated type
14856      specifier, remain silent.  */
14857   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14858     qualified_p = true;
14859
14860   push_deferring_access_checks (dk_no_check);
14861
14862   /* Determine the name of the class.  Begin by looking for an
14863      optional nested-name-specifier.  */
14864   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14865   nested_name_specifier
14866     = cp_parser_nested_name_specifier_opt (parser,
14867                                            /*typename_keyword_p=*/false,
14868                                            /*check_dependency_p=*/false,
14869                                            /*type_p=*/false,
14870                                            /*is_declaration=*/false);
14871   /* If there was a nested-name-specifier, then there *must* be an
14872      identifier.  */
14873   if (nested_name_specifier)
14874     {
14875       type_start_token = cp_lexer_peek_token (parser->lexer);
14876       /* Although the grammar says `identifier', it really means
14877          `class-name' or `template-name'.  You are only allowed to
14878          define a class that has already been declared with this
14879          syntax.
14880
14881          The proposed resolution for Core Issue 180 says that wherever
14882          you see `class T::X' you should treat `X' as a type-name.
14883
14884          It is OK to define an inaccessible class; for example:
14885
14886            class A { class B; };
14887            class A::B {};
14888
14889          We do not know if we will see a class-name, or a
14890          template-name.  We look for a class-name first, in case the
14891          class-name is a template-id; if we looked for the
14892          template-name first we would stop after the template-name.  */
14893       cp_parser_parse_tentatively (parser);
14894       type = cp_parser_class_name (parser,
14895                                    /*typename_keyword_p=*/false,
14896                                    /*template_keyword_p=*/false,
14897                                    class_type,
14898                                    /*check_dependency_p=*/false,
14899                                    /*class_head_p=*/true,
14900                                    /*is_declaration=*/false);
14901       /* If that didn't work, ignore the nested-name-specifier.  */
14902       if (!cp_parser_parse_definitely (parser))
14903         {
14904           invalid_nested_name_p = true;
14905           type_start_token = cp_lexer_peek_token (parser->lexer);
14906           id = cp_parser_identifier (parser);
14907           if (id == error_mark_node)
14908             id = NULL_TREE;
14909         }
14910       /* If we could not find a corresponding TYPE, treat this
14911          declaration like an unqualified declaration.  */
14912       if (type == error_mark_node)
14913         nested_name_specifier = NULL_TREE;
14914       /* Otherwise, count the number of templates used in TYPE and its
14915          containing scopes.  */
14916       else
14917         {
14918           tree scope;
14919
14920           for (scope = TREE_TYPE (type);
14921                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14922                scope = (TYPE_P (scope)
14923                         ? TYPE_CONTEXT (scope)
14924                         : DECL_CONTEXT (scope)))
14925             if (TYPE_P (scope)
14926                 && CLASS_TYPE_P (scope)
14927                 && CLASSTYPE_TEMPLATE_INFO (scope)
14928                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14929                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14930               ++num_templates;
14931         }
14932     }
14933   /* Otherwise, the identifier is optional.  */
14934   else
14935     {
14936       /* We don't know whether what comes next is a template-id,
14937          an identifier, or nothing at all.  */
14938       cp_parser_parse_tentatively (parser);
14939       /* Check for a template-id.  */
14940       type_start_token = cp_lexer_peek_token (parser->lexer);
14941       id = cp_parser_template_id (parser,
14942                                   /*template_keyword_p=*/false,
14943                                   /*check_dependency_p=*/true,
14944                                   /*is_declaration=*/true);
14945       /* If that didn't work, it could still be an identifier.  */
14946       if (!cp_parser_parse_definitely (parser))
14947         {
14948           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14949             {
14950               type_start_token = cp_lexer_peek_token (parser->lexer);
14951               id = cp_parser_identifier (parser);
14952             }
14953           else
14954             id = NULL_TREE;
14955         }
14956       else
14957         {
14958           template_id_p = true;
14959           ++num_templates;
14960         }
14961     }
14962
14963   pop_deferring_access_checks ();
14964
14965   if (id)
14966     cp_parser_check_for_invalid_template_id (parser, id,
14967                                              type_start_token->location);
14968
14969   /* If it's not a `:' or a `{' then we can't really be looking at a
14970      class-head, since a class-head only appears as part of a
14971      class-specifier.  We have to detect this situation before calling
14972      xref_tag, since that has irreversible side-effects.  */
14973   if (!cp_parser_next_token_starts_class_definition_p (parser))
14974     {
14975       cp_parser_error (parser, "expected %<{%> or %<:%>");
14976       return error_mark_node;
14977     }
14978
14979   /* At this point, we're going ahead with the class-specifier, even
14980      if some other problem occurs.  */
14981   cp_parser_commit_to_tentative_parse (parser);
14982   /* Issue the error about the overly-qualified name now.  */
14983   if (qualified_p)
14984     {
14985       cp_parser_error (parser,
14986                        "global qualification of class name is invalid");
14987       return error_mark_node;
14988     }
14989   else if (invalid_nested_name_p)
14990     {
14991       cp_parser_error (parser,
14992                        "qualified name does not name a class");
14993       return error_mark_node;
14994     }
14995   else if (nested_name_specifier)
14996     {
14997       tree scope;
14998
14999       /* Reject typedef-names in class heads.  */
15000       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15001         {
15002           error ("%Hinvalid class name in declaration of %qD",
15003                  &type_start_token->location, type);
15004           type = NULL_TREE;
15005           goto done;
15006         }
15007
15008       /* Figure out in what scope the declaration is being placed.  */
15009       scope = current_scope ();
15010       /* If that scope does not contain the scope in which the
15011          class was originally declared, the program is invalid.  */
15012       if (scope && !is_ancestor (scope, nested_name_specifier))
15013         {
15014           if (at_namespace_scope_p ())
15015             error ("%Hdeclaration of %qD in namespace %qD which does not "
15016                    "enclose %qD",
15017                    &type_start_token->location,
15018                    type, scope, nested_name_specifier);
15019           else
15020             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15021                    &type_start_token->location,
15022                    type, scope, nested_name_specifier);
15023           type = NULL_TREE;
15024           goto done;
15025         }
15026       /* [dcl.meaning]
15027
15028          A declarator-id shall not be qualified except for the
15029          definition of a ... nested class outside of its class
15030          ... [or] the definition or explicit instantiation of a
15031          class member of a namespace outside of its namespace.  */
15032       if (scope == nested_name_specifier)
15033         {
15034           permerror (input_location, "%Hextra qualification not allowed",
15035                      &nested_name_specifier_token_start->location);
15036           nested_name_specifier = NULL_TREE;
15037           num_templates = 0;
15038         }
15039     }
15040   /* An explicit-specialization must be preceded by "template <>".  If
15041      it is not, try to recover gracefully.  */
15042   if (at_namespace_scope_p ()
15043       && parser->num_template_parameter_lists == 0
15044       && template_id_p)
15045     {
15046       error ("%Han explicit specialization must be preceded by %<template <>%>",
15047              &type_start_token->location);
15048       invalid_explicit_specialization_p = true;
15049       /* Take the same action that would have been taken by
15050          cp_parser_explicit_specialization.  */
15051       ++parser->num_template_parameter_lists;
15052       begin_specialization ();
15053     }
15054   /* There must be no "return" statements between this point and the
15055      end of this function; set "type "to the correct return value and
15056      use "goto done;" to return.  */
15057   /* Make sure that the right number of template parameters were
15058      present.  */
15059   if (!cp_parser_check_template_parameters (parser, num_templates,
15060                                             type_start_token->location))
15061     {
15062       /* If something went wrong, there is no point in even trying to
15063          process the class-definition.  */
15064       type = NULL_TREE;
15065       goto done;
15066     }
15067
15068   /* Look up the type.  */
15069   if (template_id_p)
15070     {
15071       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15072           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15073               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15074         {
15075           error ("%Hfunction template %qD redeclared as a class template",
15076                  &type_start_token->location, id);
15077           type = error_mark_node;
15078         }
15079       else
15080         {
15081           type = TREE_TYPE (id);
15082           type = maybe_process_partial_specialization (type);
15083         }
15084       if (nested_name_specifier)
15085         pushed_scope = push_scope (nested_name_specifier);
15086     }
15087   else if (nested_name_specifier)
15088     {
15089       tree class_type;
15090
15091       /* Given:
15092
15093             template <typename T> struct S { struct T };
15094             template <typename T> struct S<T>::T { };
15095
15096          we will get a TYPENAME_TYPE when processing the definition of
15097          `S::T'.  We need to resolve it to the actual type before we
15098          try to define it.  */
15099       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15100         {
15101           class_type = resolve_typename_type (TREE_TYPE (type),
15102                                               /*only_current_p=*/false);
15103           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15104             type = TYPE_NAME (class_type);
15105           else
15106             {
15107               cp_parser_error (parser, "could not resolve typename type");
15108               type = error_mark_node;
15109             }
15110         }
15111
15112       if (maybe_process_partial_specialization (TREE_TYPE (type))
15113           == error_mark_node)
15114         {
15115           type = NULL_TREE;
15116           goto done;
15117         }
15118
15119       class_type = current_class_type;
15120       /* Enter the scope indicated by the nested-name-specifier.  */
15121       pushed_scope = push_scope (nested_name_specifier);
15122       /* Get the canonical version of this type.  */
15123       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15124       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15125           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15126         {
15127           type = push_template_decl (type);
15128           if (type == error_mark_node)
15129             {
15130               type = NULL_TREE;
15131               goto done;
15132             }
15133         }
15134
15135       type = TREE_TYPE (type);
15136       *nested_name_specifier_p = true;
15137     }
15138   else      /* The name is not a nested name.  */
15139     {
15140       /* If the class was unnamed, create a dummy name.  */
15141       if (!id)
15142         id = make_anon_name ();
15143       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15144                        parser->num_template_parameter_lists);
15145     }
15146
15147   /* Indicate whether this class was declared as a `class' or as a
15148      `struct'.  */
15149   if (TREE_CODE (type) == RECORD_TYPE)
15150     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15151   cp_parser_check_class_key (class_key, type);
15152
15153   /* If this type was already complete, and we see another definition,
15154      that's an error.  */
15155   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15156     {
15157       error ("%Hredefinition of %q#T",
15158              &type_start_token->location, type);
15159       error ("%Hprevious definition of %q+#T",
15160              &type_start_token->location, type);
15161       type = NULL_TREE;
15162       goto done;
15163     }
15164   else if (type == error_mark_node)
15165     type = NULL_TREE;
15166
15167   /* We will have entered the scope containing the class; the names of
15168      base classes should be looked up in that context.  For example:
15169
15170        struct A { struct B {}; struct C; };
15171        struct A::C : B {};
15172
15173      is valid.  */
15174
15175   /* Get the list of base-classes, if there is one.  */
15176   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15177     *bases = cp_parser_base_clause (parser);
15178
15179  done:
15180   /* Leave the scope given by the nested-name-specifier.  We will
15181      enter the class scope itself while processing the members.  */
15182   if (pushed_scope)
15183     pop_scope (pushed_scope);
15184
15185   if (invalid_explicit_specialization_p)
15186     {
15187       end_specialization ();
15188       --parser->num_template_parameter_lists;
15189     }
15190   *attributes_p = attributes;
15191   return type;
15192 }
15193
15194 /* Parse a class-key.
15195
15196    class-key:
15197      class
15198      struct
15199      union
15200
15201    Returns the kind of class-key specified, or none_type to indicate
15202    error.  */
15203
15204 static enum tag_types
15205 cp_parser_class_key (cp_parser* parser)
15206 {
15207   cp_token *token;
15208   enum tag_types tag_type;
15209
15210   /* Look for the class-key.  */
15211   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15212   if (!token)
15213     return none_type;
15214
15215   /* Check to see if the TOKEN is a class-key.  */
15216   tag_type = cp_parser_token_is_class_key (token);
15217   if (!tag_type)
15218     cp_parser_error (parser, "expected class-key");
15219   return tag_type;
15220 }
15221
15222 /* Parse an (optional) member-specification.
15223
15224    member-specification:
15225      member-declaration member-specification [opt]
15226      access-specifier : member-specification [opt]  */
15227
15228 static void
15229 cp_parser_member_specification_opt (cp_parser* parser)
15230 {
15231   while (true)
15232     {
15233       cp_token *token;
15234       enum rid keyword;
15235
15236       /* Peek at the next token.  */
15237       token = cp_lexer_peek_token (parser->lexer);
15238       /* If it's a `}', or EOF then we've seen all the members.  */
15239       if (token->type == CPP_CLOSE_BRACE
15240           || token->type == CPP_EOF
15241           || token->type == CPP_PRAGMA_EOL)
15242         break;
15243
15244       /* See if this token is a keyword.  */
15245       keyword = token->keyword;
15246       switch (keyword)
15247         {
15248         case RID_PUBLIC:
15249         case RID_PROTECTED:
15250         case RID_PRIVATE:
15251           /* Consume the access-specifier.  */
15252           cp_lexer_consume_token (parser->lexer);
15253           /* Remember which access-specifier is active.  */
15254           current_access_specifier = token->u.value;
15255           /* Look for the `:'.  */
15256           cp_parser_require (parser, CPP_COLON, "%<:%>");
15257           break;
15258
15259         default:
15260           /* Accept #pragmas at class scope.  */
15261           if (token->type == CPP_PRAGMA)
15262             {
15263               cp_parser_pragma (parser, pragma_external);
15264               break;
15265             }
15266
15267           /* Otherwise, the next construction must be a
15268              member-declaration.  */
15269           cp_parser_member_declaration (parser);
15270         }
15271     }
15272 }
15273
15274 /* Parse a member-declaration.
15275
15276    member-declaration:
15277      decl-specifier-seq [opt] member-declarator-list [opt] ;
15278      function-definition ; [opt]
15279      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15280      using-declaration
15281      template-declaration
15282
15283    member-declarator-list:
15284      member-declarator
15285      member-declarator-list , member-declarator
15286
15287    member-declarator:
15288      declarator pure-specifier [opt]
15289      declarator constant-initializer [opt]
15290      identifier [opt] : constant-expression
15291
15292    GNU Extensions:
15293
15294    member-declaration:
15295      __extension__ member-declaration
15296
15297    member-declarator:
15298      declarator attributes [opt] pure-specifier [opt]
15299      declarator attributes [opt] constant-initializer [opt]
15300      identifier [opt] attributes [opt] : constant-expression  
15301
15302    C++0x Extensions:
15303
15304    member-declaration:
15305      static_assert-declaration  */
15306
15307 static void
15308 cp_parser_member_declaration (cp_parser* parser)
15309 {
15310   cp_decl_specifier_seq decl_specifiers;
15311   tree prefix_attributes;
15312   tree decl;
15313   int declares_class_or_enum;
15314   bool friend_p;
15315   cp_token *token = NULL;
15316   cp_token *decl_spec_token_start = NULL;
15317   cp_token *initializer_token_start = NULL;
15318   int saved_pedantic;
15319
15320   /* Check for the `__extension__' keyword.  */
15321   if (cp_parser_extension_opt (parser, &saved_pedantic))
15322     {
15323       /* Recurse.  */
15324       cp_parser_member_declaration (parser);
15325       /* Restore the old value of the PEDANTIC flag.  */
15326       pedantic = saved_pedantic;
15327
15328       return;
15329     }
15330
15331   /* Check for a template-declaration.  */
15332   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15333     {
15334       /* An explicit specialization here is an error condition, and we
15335          expect the specialization handler to detect and report this.  */
15336       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15337           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15338         cp_parser_explicit_specialization (parser);
15339       else
15340         cp_parser_template_declaration (parser, /*member_p=*/true);
15341
15342       return;
15343     }
15344
15345   /* Check for a using-declaration.  */
15346   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15347     {
15348       /* Parse the using-declaration.  */
15349       cp_parser_using_declaration (parser,
15350                                    /*access_declaration_p=*/false);
15351       return;
15352     }
15353
15354   /* Check for @defs.  */
15355   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15356     {
15357       tree ivar, member;
15358       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15359       ivar = ivar_chains;
15360       while (ivar)
15361         {
15362           member = ivar;
15363           ivar = TREE_CHAIN (member);
15364           TREE_CHAIN (member) = NULL_TREE;
15365           finish_member_declaration (member);
15366         }
15367       return;
15368     }
15369
15370   /* If the next token is `static_assert' we have a static assertion.  */
15371   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15372     {
15373       cp_parser_static_assert (parser, /*member_p=*/true);
15374       return;
15375     }
15376
15377   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15378     return;
15379
15380   /* Parse the decl-specifier-seq.  */
15381   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15382   cp_parser_decl_specifier_seq (parser,
15383                                 CP_PARSER_FLAGS_OPTIONAL,
15384                                 &decl_specifiers,
15385                                 &declares_class_or_enum);
15386   prefix_attributes = decl_specifiers.attributes;
15387   decl_specifiers.attributes = NULL_TREE;
15388   /* Check for an invalid type-name.  */
15389   if (!decl_specifiers.type
15390       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15391     return;
15392   /* If there is no declarator, then the decl-specifier-seq should
15393      specify a type.  */
15394   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15395     {
15396       /* If there was no decl-specifier-seq, and the next token is a
15397          `;', then we have something like:
15398
15399            struct S { ; };
15400
15401          [class.mem]
15402
15403          Each member-declaration shall declare at least one member
15404          name of the class.  */
15405       if (!decl_specifiers.any_specifiers_p)
15406         {
15407           cp_token *token = cp_lexer_peek_token (parser->lexer);
15408           if (!in_system_header_at (token->location))
15409             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15410         }
15411       else
15412         {
15413           tree type;
15414
15415           /* See if this declaration is a friend.  */
15416           friend_p = cp_parser_friend_p (&decl_specifiers);
15417           /* If there were decl-specifiers, check to see if there was
15418              a class-declaration.  */
15419           type = check_tag_decl (&decl_specifiers);
15420           /* Nested classes have already been added to the class, but
15421              a `friend' needs to be explicitly registered.  */
15422           if (friend_p)
15423             {
15424               /* If the `friend' keyword was present, the friend must
15425                  be introduced with a class-key.  */
15426                if (!declares_class_or_enum)
15427                  error ("%Ha class-key must be used when declaring a friend",
15428                         &decl_spec_token_start->location);
15429                /* In this case:
15430
15431                     template <typename T> struct A {
15432                       friend struct A<T>::B;
15433                     };
15434
15435                   A<T>::B will be represented by a TYPENAME_TYPE, and
15436                   therefore not recognized by check_tag_decl.  */
15437                if (!type
15438                    && decl_specifiers.type
15439                    && TYPE_P (decl_specifiers.type))
15440                  type = decl_specifiers.type;
15441                if (!type || !TYPE_P (type))
15442                  error ("%Hfriend declaration does not name a class or "
15443                         "function", &decl_spec_token_start->location);
15444                else
15445                  make_friend_class (current_class_type, type,
15446                                     /*complain=*/true);
15447             }
15448           /* If there is no TYPE, an error message will already have
15449              been issued.  */
15450           else if (!type || type == error_mark_node)
15451             ;
15452           /* An anonymous aggregate has to be handled specially; such
15453              a declaration really declares a data member (with a
15454              particular type), as opposed to a nested class.  */
15455           else if (ANON_AGGR_TYPE_P (type))
15456             {
15457               /* Remove constructors and such from TYPE, now that we
15458                  know it is an anonymous aggregate.  */
15459               fixup_anonymous_aggr (type);
15460               /* And make the corresponding data member.  */
15461               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15462               /* Add it to the class.  */
15463               finish_member_declaration (decl);
15464             }
15465           else
15466             cp_parser_check_access_in_redeclaration
15467                                               (TYPE_NAME (type),
15468                                                decl_spec_token_start->location);
15469         }
15470     }
15471   else
15472     {
15473       /* See if these declarations will be friends.  */
15474       friend_p = cp_parser_friend_p (&decl_specifiers);
15475
15476       /* Keep going until we hit the `;' at the end of the
15477          declaration.  */
15478       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15479         {
15480           tree attributes = NULL_TREE;
15481           tree first_attribute;
15482
15483           /* Peek at the next token.  */
15484           token = cp_lexer_peek_token (parser->lexer);
15485
15486           /* Check for a bitfield declaration.  */
15487           if (token->type == CPP_COLON
15488               || (token->type == CPP_NAME
15489                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15490                   == CPP_COLON))
15491             {
15492               tree identifier;
15493               tree width;
15494
15495               /* Get the name of the bitfield.  Note that we cannot just
15496                  check TOKEN here because it may have been invalidated by
15497                  the call to cp_lexer_peek_nth_token above.  */
15498               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15499                 identifier = cp_parser_identifier (parser);
15500               else
15501                 identifier = NULL_TREE;
15502
15503               /* Consume the `:' token.  */
15504               cp_lexer_consume_token (parser->lexer);
15505               /* Get the width of the bitfield.  */
15506               width
15507                 = cp_parser_constant_expression (parser,
15508                                                  /*allow_non_constant=*/false,
15509                                                  NULL);
15510
15511               /* Look for attributes that apply to the bitfield.  */
15512               attributes = cp_parser_attributes_opt (parser);
15513               /* Remember which attributes are prefix attributes and
15514                  which are not.  */
15515               first_attribute = attributes;
15516               /* Combine the attributes.  */
15517               attributes = chainon (prefix_attributes, attributes);
15518
15519               /* Create the bitfield declaration.  */
15520               decl = grokbitfield (identifier
15521                                    ? make_id_declarator (NULL_TREE,
15522                                                          identifier,
15523                                                          sfk_none)
15524                                    : NULL,
15525                                    &decl_specifiers,
15526                                    width,
15527                                    attributes);
15528             }
15529           else
15530             {
15531               cp_declarator *declarator;
15532               tree initializer;
15533               tree asm_specification;
15534               int ctor_dtor_or_conv_p;
15535
15536               /* Parse the declarator.  */
15537               declarator
15538                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15539                                         &ctor_dtor_or_conv_p,
15540                                         /*parenthesized_p=*/NULL,
15541                                         /*member_p=*/true);
15542
15543               /* If something went wrong parsing the declarator, make sure
15544                  that we at least consume some tokens.  */
15545               if (declarator == cp_error_declarator)
15546                 {
15547                   /* Skip to the end of the statement.  */
15548                   cp_parser_skip_to_end_of_statement (parser);
15549                   /* If the next token is not a semicolon, that is
15550                      probably because we just skipped over the body of
15551                      a function.  So, we consume a semicolon if
15552                      present, but do not issue an error message if it
15553                      is not present.  */
15554                   if (cp_lexer_next_token_is (parser->lexer,
15555                                               CPP_SEMICOLON))
15556                     cp_lexer_consume_token (parser->lexer);
15557                   return;
15558                 }
15559
15560               if (declares_class_or_enum & 2)
15561                 cp_parser_check_for_definition_in_return_type
15562                                             (declarator, decl_specifiers.type,
15563                                              decl_specifiers.type_location);
15564
15565               /* Look for an asm-specification.  */
15566               asm_specification = cp_parser_asm_specification_opt (parser);
15567               /* Look for attributes that apply to the declaration.  */
15568               attributes = cp_parser_attributes_opt (parser);
15569               /* Remember which attributes are prefix attributes and
15570                  which are not.  */
15571               first_attribute = attributes;
15572               /* Combine the attributes.  */
15573               attributes = chainon (prefix_attributes, attributes);
15574
15575               /* If it's an `=', then we have a constant-initializer or a
15576                  pure-specifier.  It is not correct to parse the
15577                  initializer before registering the member declaration
15578                  since the member declaration should be in scope while
15579                  its initializer is processed.  However, the rest of the
15580                  front end does not yet provide an interface that allows
15581                  us to handle this correctly.  */
15582               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15583                 {
15584                   /* In [class.mem]:
15585
15586                      A pure-specifier shall be used only in the declaration of
15587                      a virtual function.
15588
15589                      A member-declarator can contain a constant-initializer
15590                      only if it declares a static member of integral or
15591                      enumeration type.
15592
15593                      Therefore, if the DECLARATOR is for a function, we look
15594                      for a pure-specifier; otherwise, we look for a
15595                      constant-initializer.  When we call `grokfield', it will
15596                      perform more stringent semantics checks.  */
15597                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15598                   if (function_declarator_p (declarator))
15599                     initializer = cp_parser_pure_specifier (parser);
15600                   else
15601                     /* Parse the initializer.  */
15602                     initializer = cp_parser_constant_initializer (parser);
15603                 }
15604               /* Otherwise, there is no initializer.  */
15605               else
15606                 initializer = NULL_TREE;
15607
15608               /* See if we are probably looking at a function
15609                  definition.  We are certainly not looking at a
15610                  member-declarator.  Calling `grokfield' has
15611                  side-effects, so we must not do it unless we are sure
15612                  that we are looking at a member-declarator.  */
15613               if (cp_parser_token_starts_function_definition_p
15614                   (cp_lexer_peek_token (parser->lexer)))
15615                 {
15616                   /* The grammar does not allow a pure-specifier to be
15617                      used when a member function is defined.  (It is
15618                      possible that this fact is an oversight in the
15619                      standard, since a pure function may be defined
15620                      outside of the class-specifier.  */
15621                   if (initializer)
15622                     error ("%Hpure-specifier on function-definition",
15623                            &initializer_token_start->location);
15624                   decl = cp_parser_save_member_function_body (parser,
15625                                                               &decl_specifiers,
15626                                                               declarator,
15627                                                               attributes);
15628                   /* If the member was not a friend, declare it here.  */
15629                   if (!friend_p)
15630                     finish_member_declaration (decl);
15631                   /* Peek at the next token.  */
15632                   token = cp_lexer_peek_token (parser->lexer);
15633                   /* If the next token is a semicolon, consume it.  */
15634                   if (token->type == CPP_SEMICOLON)
15635                     cp_lexer_consume_token (parser->lexer);
15636                   return;
15637                 }
15638               else
15639                 /* Create the declaration.  */
15640                 decl = grokfield (declarator, &decl_specifiers,
15641                                   initializer, /*init_const_expr_p=*/true,
15642                                   asm_specification,
15643                                   attributes);
15644             }
15645
15646           /* Reset PREFIX_ATTRIBUTES.  */
15647           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15648             attributes = TREE_CHAIN (attributes);
15649           if (attributes)
15650             TREE_CHAIN (attributes) = NULL_TREE;
15651
15652           /* If there is any qualification still in effect, clear it
15653              now; we will be starting fresh with the next declarator.  */
15654           parser->scope = NULL_TREE;
15655           parser->qualifying_scope = NULL_TREE;
15656           parser->object_scope = NULL_TREE;
15657           /* If it's a `,', then there are more declarators.  */
15658           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15659             cp_lexer_consume_token (parser->lexer);
15660           /* If the next token isn't a `;', then we have a parse error.  */
15661           else if (cp_lexer_next_token_is_not (parser->lexer,
15662                                                CPP_SEMICOLON))
15663             {
15664               cp_parser_error (parser, "expected %<;%>");
15665               /* Skip tokens until we find a `;'.  */
15666               cp_parser_skip_to_end_of_statement (parser);
15667
15668               break;
15669             }
15670
15671           if (decl)
15672             {
15673               /* Add DECL to the list of members.  */
15674               if (!friend_p)
15675                 finish_member_declaration (decl);
15676
15677               if (TREE_CODE (decl) == FUNCTION_DECL)
15678                 cp_parser_save_default_args (parser, decl);
15679             }
15680         }
15681     }
15682
15683   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15684 }
15685
15686 /* Parse a pure-specifier.
15687
15688    pure-specifier:
15689      = 0
15690
15691    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15692    Otherwise, ERROR_MARK_NODE is returned.  */
15693
15694 static tree
15695 cp_parser_pure_specifier (cp_parser* parser)
15696 {
15697   cp_token *token;
15698
15699   /* Look for the `=' token.  */
15700   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15701     return error_mark_node;
15702   /* Look for the `0' token.  */
15703   token = cp_lexer_consume_token (parser->lexer);
15704
15705   /* Accept = default or = delete in c++0x mode.  */
15706   if (token->keyword == RID_DEFAULT
15707       || token->keyword == RID_DELETE)
15708     {
15709       maybe_warn_cpp0x ("defaulted and deleted functions");
15710       return token->u.value;
15711     }
15712
15713   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15714   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15715     {
15716       cp_parser_error (parser,
15717                        "invalid pure specifier (only %<= 0%> is allowed)");
15718       cp_parser_skip_to_end_of_statement (parser);
15719       return error_mark_node;
15720     }
15721   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15722     {
15723       error ("%Htemplates may not be %<virtual%>", &token->location);
15724       return error_mark_node;
15725     }
15726
15727   return integer_zero_node;
15728 }
15729
15730 /* Parse a constant-initializer.
15731
15732    constant-initializer:
15733      = constant-expression
15734
15735    Returns a representation of the constant-expression.  */
15736
15737 static tree
15738 cp_parser_constant_initializer (cp_parser* parser)
15739 {
15740   /* Look for the `=' token.  */
15741   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15742     return error_mark_node;
15743
15744   /* It is invalid to write:
15745
15746        struct S { static const int i = { 7 }; };
15747
15748      */
15749   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15750     {
15751       cp_parser_error (parser,
15752                        "a brace-enclosed initializer is not allowed here");
15753       /* Consume the opening brace.  */
15754       cp_lexer_consume_token (parser->lexer);
15755       /* Skip the initializer.  */
15756       cp_parser_skip_to_closing_brace (parser);
15757       /* Look for the trailing `}'.  */
15758       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15759
15760       return error_mark_node;
15761     }
15762
15763   return cp_parser_constant_expression (parser,
15764                                         /*allow_non_constant=*/false,
15765                                         NULL);
15766 }
15767
15768 /* Derived classes [gram.class.derived] */
15769
15770 /* Parse a base-clause.
15771
15772    base-clause:
15773      : base-specifier-list
15774
15775    base-specifier-list:
15776      base-specifier ... [opt]
15777      base-specifier-list , base-specifier ... [opt]
15778
15779    Returns a TREE_LIST representing the base-classes, in the order in
15780    which they were declared.  The representation of each node is as
15781    described by cp_parser_base_specifier.
15782
15783    In the case that no bases are specified, this function will return
15784    NULL_TREE, not ERROR_MARK_NODE.  */
15785
15786 static tree
15787 cp_parser_base_clause (cp_parser* parser)
15788 {
15789   tree bases = NULL_TREE;
15790
15791   /* Look for the `:' that begins the list.  */
15792   cp_parser_require (parser, CPP_COLON, "%<:%>");
15793
15794   /* Scan the base-specifier-list.  */
15795   while (true)
15796     {
15797       cp_token *token;
15798       tree base;
15799       bool pack_expansion_p = false;
15800
15801       /* Look for the base-specifier.  */
15802       base = cp_parser_base_specifier (parser);
15803       /* Look for the (optional) ellipsis. */
15804       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15805         {
15806           /* Consume the `...'. */
15807           cp_lexer_consume_token (parser->lexer);
15808
15809           pack_expansion_p = true;
15810         }
15811
15812       /* Add BASE to the front of the list.  */
15813       if (base != error_mark_node)
15814         {
15815           if (pack_expansion_p)
15816             /* Make this a pack expansion type. */
15817             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15818           
15819
15820           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15821             {
15822               TREE_CHAIN (base) = bases;
15823               bases = base;
15824             }
15825         }
15826       /* Peek at the next token.  */
15827       token = cp_lexer_peek_token (parser->lexer);
15828       /* If it's not a comma, then the list is complete.  */
15829       if (token->type != CPP_COMMA)
15830         break;
15831       /* Consume the `,'.  */
15832       cp_lexer_consume_token (parser->lexer);
15833     }
15834
15835   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15836      base class had a qualified name.  However, the next name that
15837      appears is certainly not qualified.  */
15838   parser->scope = NULL_TREE;
15839   parser->qualifying_scope = NULL_TREE;
15840   parser->object_scope = NULL_TREE;
15841
15842   return nreverse (bases);
15843 }
15844
15845 /* Parse a base-specifier.
15846
15847    base-specifier:
15848      :: [opt] nested-name-specifier [opt] class-name
15849      virtual access-specifier [opt] :: [opt] nested-name-specifier
15850        [opt] class-name
15851      access-specifier virtual [opt] :: [opt] nested-name-specifier
15852        [opt] class-name
15853
15854    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15855    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15856    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15857    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15858
15859 static tree
15860 cp_parser_base_specifier (cp_parser* parser)
15861 {
15862   cp_token *token;
15863   bool done = false;
15864   bool virtual_p = false;
15865   bool duplicate_virtual_error_issued_p = false;
15866   bool duplicate_access_error_issued_p = false;
15867   bool class_scope_p, template_p;
15868   tree access = access_default_node;
15869   tree type;
15870
15871   /* Process the optional `virtual' and `access-specifier'.  */
15872   while (!done)
15873     {
15874       /* Peek at the next token.  */
15875       token = cp_lexer_peek_token (parser->lexer);
15876       /* Process `virtual'.  */
15877       switch (token->keyword)
15878         {
15879         case RID_VIRTUAL:
15880           /* If `virtual' appears more than once, issue an error.  */
15881           if (virtual_p && !duplicate_virtual_error_issued_p)
15882             {
15883               cp_parser_error (parser,
15884                                "%<virtual%> specified more than once in base-specified");
15885               duplicate_virtual_error_issued_p = true;
15886             }
15887
15888           virtual_p = true;
15889
15890           /* Consume the `virtual' token.  */
15891           cp_lexer_consume_token (parser->lexer);
15892
15893           break;
15894
15895         case RID_PUBLIC:
15896         case RID_PROTECTED:
15897         case RID_PRIVATE:
15898           /* If more than one access specifier appears, issue an
15899              error.  */
15900           if (access != access_default_node
15901               && !duplicate_access_error_issued_p)
15902             {
15903               cp_parser_error (parser,
15904                                "more than one access specifier in base-specified");
15905               duplicate_access_error_issued_p = true;
15906             }
15907
15908           access = ridpointers[(int) token->keyword];
15909
15910           /* Consume the access-specifier.  */
15911           cp_lexer_consume_token (parser->lexer);
15912
15913           break;
15914
15915         default:
15916           done = true;
15917           break;
15918         }
15919     }
15920   /* It is not uncommon to see programs mechanically, erroneously, use
15921      the 'typename' keyword to denote (dependent) qualified types
15922      as base classes.  */
15923   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15924     {
15925       token = cp_lexer_peek_token (parser->lexer);
15926       if (!processing_template_decl)
15927         error ("%Hkeyword %<typename%> not allowed outside of templates",
15928                &token->location);
15929       else
15930         error ("%Hkeyword %<typename%> not allowed in this context "
15931                "(the base class is implicitly a type)",
15932                &token->location);
15933       cp_lexer_consume_token (parser->lexer);
15934     }
15935
15936   /* Look for the optional `::' operator.  */
15937   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15938   /* Look for the nested-name-specifier.  The simplest way to
15939      implement:
15940
15941        [temp.res]
15942
15943        The keyword `typename' is not permitted in a base-specifier or
15944        mem-initializer; in these contexts a qualified name that
15945        depends on a template-parameter is implicitly assumed to be a
15946        type name.
15947
15948      is to pretend that we have seen the `typename' keyword at this
15949      point.  */
15950   cp_parser_nested_name_specifier_opt (parser,
15951                                        /*typename_keyword_p=*/true,
15952                                        /*check_dependency_p=*/true,
15953                                        typename_type,
15954                                        /*is_declaration=*/true);
15955   /* If the base class is given by a qualified name, assume that names
15956      we see are type names or templates, as appropriate.  */
15957   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15958   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15959
15960   /* Finally, look for the class-name.  */
15961   type = cp_parser_class_name (parser,
15962                                class_scope_p,
15963                                template_p,
15964                                typename_type,
15965                                /*check_dependency_p=*/true,
15966                                /*class_head_p=*/false,
15967                                /*is_declaration=*/true);
15968
15969   if (type == error_mark_node)
15970     return error_mark_node;
15971
15972   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15973 }
15974
15975 /* Exception handling [gram.exception] */
15976
15977 /* Parse an (optional) exception-specification.
15978
15979    exception-specification:
15980      throw ( type-id-list [opt] )
15981
15982    Returns a TREE_LIST representing the exception-specification.  The
15983    TREE_VALUE of each node is a type.  */
15984
15985 static tree
15986 cp_parser_exception_specification_opt (cp_parser* parser)
15987 {
15988   cp_token *token;
15989   tree type_id_list;
15990
15991   /* Peek at the next token.  */
15992   token = cp_lexer_peek_token (parser->lexer);
15993   /* If it's not `throw', then there's no exception-specification.  */
15994   if (!cp_parser_is_keyword (token, RID_THROW))
15995     return NULL_TREE;
15996
15997   /* Consume the `throw'.  */
15998   cp_lexer_consume_token (parser->lexer);
15999
16000   /* Look for the `('.  */
16001   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16002
16003   /* Peek at the next token.  */
16004   token = cp_lexer_peek_token (parser->lexer);
16005   /* If it's not a `)', then there is a type-id-list.  */
16006   if (token->type != CPP_CLOSE_PAREN)
16007     {
16008       const char *saved_message;
16009
16010       /* Types may not be defined in an exception-specification.  */
16011       saved_message = parser->type_definition_forbidden_message;
16012       parser->type_definition_forbidden_message
16013         = "types may not be defined in an exception-specification";
16014       /* Parse the type-id-list.  */
16015       type_id_list = cp_parser_type_id_list (parser);
16016       /* Restore the saved message.  */
16017       parser->type_definition_forbidden_message = saved_message;
16018     }
16019   else
16020     type_id_list = empty_except_spec;
16021
16022   /* Look for the `)'.  */
16023   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16024
16025   return type_id_list;
16026 }
16027
16028 /* Parse an (optional) type-id-list.
16029
16030    type-id-list:
16031      type-id ... [opt]
16032      type-id-list , type-id ... [opt]
16033
16034    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16035    in the order that the types were presented.  */
16036
16037 static tree
16038 cp_parser_type_id_list (cp_parser* parser)
16039 {
16040   tree types = NULL_TREE;
16041
16042   while (true)
16043     {
16044       cp_token *token;
16045       tree type;
16046
16047       /* Get the next type-id.  */
16048       type = cp_parser_type_id (parser);
16049       /* Parse the optional ellipsis. */
16050       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16051         {
16052           /* Consume the `...'. */
16053           cp_lexer_consume_token (parser->lexer);
16054
16055           /* Turn the type into a pack expansion expression. */
16056           type = make_pack_expansion (type);
16057         }
16058       /* Add it to the list.  */
16059       types = add_exception_specifier (types, type, /*complain=*/1);
16060       /* Peek at the next token.  */
16061       token = cp_lexer_peek_token (parser->lexer);
16062       /* If it is not a `,', we are done.  */
16063       if (token->type != CPP_COMMA)
16064         break;
16065       /* Consume the `,'.  */
16066       cp_lexer_consume_token (parser->lexer);
16067     }
16068
16069   return nreverse (types);
16070 }
16071
16072 /* Parse a try-block.
16073
16074    try-block:
16075      try compound-statement handler-seq  */
16076
16077 static tree
16078 cp_parser_try_block (cp_parser* parser)
16079 {
16080   tree try_block;
16081
16082   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16083   try_block = begin_try_block ();
16084   cp_parser_compound_statement (parser, NULL, true);
16085   finish_try_block (try_block);
16086   cp_parser_handler_seq (parser);
16087   finish_handler_sequence (try_block);
16088
16089   return try_block;
16090 }
16091
16092 /* Parse a function-try-block.
16093
16094    function-try-block:
16095      try ctor-initializer [opt] function-body handler-seq  */
16096
16097 static bool
16098 cp_parser_function_try_block (cp_parser* parser)
16099 {
16100   tree compound_stmt;
16101   tree try_block;
16102   bool ctor_initializer_p;
16103
16104   /* Look for the `try' keyword.  */
16105   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16106     return false;
16107   /* Let the rest of the front end know where we are.  */
16108   try_block = begin_function_try_block (&compound_stmt);
16109   /* Parse the function-body.  */
16110   ctor_initializer_p
16111     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16112   /* We're done with the `try' part.  */
16113   finish_function_try_block (try_block);
16114   /* Parse the handlers.  */
16115   cp_parser_handler_seq (parser);
16116   /* We're done with the handlers.  */
16117   finish_function_handler_sequence (try_block, compound_stmt);
16118
16119   return ctor_initializer_p;
16120 }
16121
16122 /* Parse a handler-seq.
16123
16124    handler-seq:
16125      handler handler-seq [opt]  */
16126
16127 static void
16128 cp_parser_handler_seq (cp_parser* parser)
16129 {
16130   while (true)
16131     {
16132       cp_token *token;
16133
16134       /* Parse the handler.  */
16135       cp_parser_handler (parser);
16136       /* Peek at the next token.  */
16137       token = cp_lexer_peek_token (parser->lexer);
16138       /* If it's not `catch' then there are no more handlers.  */
16139       if (!cp_parser_is_keyword (token, RID_CATCH))
16140         break;
16141     }
16142 }
16143
16144 /* Parse a handler.
16145
16146    handler:
16147      catch ( exception-declaration ) compound-statement  */
16148
16149 static void
16150 cp_parser_handler (cp_parser* parser)
16151 {
16152   tree handler;
16153   tree declaration;
16154
16155   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16156   handler = begin_handler ();
16157   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16158   declaration = cp_parser_exception_declaration (parser);
16159   finish_handler_parms (declaration, handler);
16160   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16161   cp_parser_compound_statement (parser, NULL, false);
16162   finish_handler (handler);
16163 }
16164
16165 /* Parse an exception-declaration.
16166
16167    exception-declaration:
16168      type-specifier-seq declarator
16169      type-specifier-seq abstract-declarator
16170      type-specifier-seq
16171      ...
16172
16173    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16174    ellipsis variant is used.  */
16175
16176 static tree
16177 cp_parser_exception_declaration (cp_parser* parser)
16178 {
16179   cp_decl_specifier_seq type_specifiers;
16180   cp_declarator *declarator;
16181   const char *saved_message;
16182
16183   /* If it's an ellipsis, it's easy to handle.  */
16184   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16185     {
16186       /* Consume the `...' token.  */
16187       cp_lexer_consume_token (parser->lexer);
16188       return NULL_TREE;
16189     }
16190
16191   /* Types may not be defined in exception-declarations.  */
16192   saved_message = parser->type_definition_forbidden_message;
16193   parser->type_definition_forbidden_message
16194     = "types may not be defined in exception-declarations";
16195
16196   /* Parse the type-specifier-seq.  */
16197   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16198                                 &type_specifiers);
16199   /* If it's a `)', then there is no declarator.  */
16200   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16201     declarator = NULL;
16202   else
16203     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16204                                        /*ctor_dtor_or_conv_p=*/NULL,
16205                                        /*parenthesized_p=*/NULL,
16206                                        /*member_p=*/false);
16207
16208   /* Restore the saved message.  */
16209   parser->type_definition_forbidden_message = saved_message;
16210
16211   if (!type_specifiers.any_specifiers_p)
16212     return error_mark_node;
16213
16214   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16215 }
16216
16217 /* Parse a throw-expression.
16218
16219    throw-expression:
16220      throw assignment-expression [opt]
16221
16222    Returns a THROW_EXPR representing the throw-expression.  */
16223
16224 static tree
16225 cp_parser_throw_expression (cp_parser* parser)
16226 {
16227   tree expression;
16228   cp_token* token;
16229
16230   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16231   token = cp_lexer_peek_token (parser->lexer);
16232   /* Figure out whether or not there is an assignment-expression
16233      following the "throw" keyword.  */
16234   if (token->type == CPP_COMMA
16235       || token->type == CPP_SEMICOLON
16236       || token->type == CPP_CLOSE_PAREN
16237       || token->type == CPP_CLOSE_SQUARE
16238       || token->type == CPP_CLOSE_BRACE
16239       || token->type == CPP_COLON)
16240     expression = NULL_TREE;
16241   else
16242     expression = cp_parser_assignment_expression (parser,
16243                                                   /*cast_p=*/false);
16244
16245   return build_throw (expression);
16246 }
16247
16248 /* GNU Extensions */
16249
16250 /* Parse an (optional) asm-specification.
16251
16252    asm-specification:
16253      asm ( string-literal )
16254
16255    If the asm-specification is present, returns a STRING_CST
16256    corresponding to the string-literal.  Otherwise, returns
16257    NULL_TREE.  */
16258
16259 static tree
16260 cp_parser_asm_specification_opt (cp_parser* parser)
16261 {
16262   cp_token *token;
16263   tree asm_specification;
16264
16265   /* Peek at the next token.  */
16266   token = cp_lexer_peek_token (parser->lexer);
16267   /* If the next token isn't the `asm' keyword, then there's no
16268      asm-specification.  */
16269   if (!cp_parser_is_keyword (token, RID_ASM))
16270     return NULL_TREE;
16271
16272   /* Consume the `asm' token.  */
16273   cp_lexer_consume_token (parser->lexer);
16274   /* Look for the `('.  */
16275   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16276
16277   /* Look for the string-literal.  */
16278   asm_specification = cp_parser_string_literal (parser, false, false);
16279
16280   /* Look for the `)'.  */
16281   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16282
16283   return asm_specification;
16284 }
16285
16286 /* Parse an asm-operand-list.
16287
16288    asm-operand-list:
16289      asm-operand
16290      asm-operand-list , asm-operand
16291
16292    asm-operand:
16293      string-literal ( expression )
16294      [ string-literal ] string-literal ( expression )
16295
16296    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16297    each node is the expression.  The TREE_PURPOSE is itself a
16298    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16299    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16300    is a STRING_CST for the string literal before the parenthesis. Returns
16301    ERROR_MARK_NODE if any of the operands are invalid.  */
16302
16303 static tree
16304 cp_parser_asm_operand_list (cp_parser* parser)
16305 {
16306   tree asm_operands = NULL_TREE;
16307   bool invalid_operands = false;
16308
16309   while (true)
16310     {
16311       tree string_literal;
16312       tree expression;
16313       tree name;
16314
16315       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16316         {
16317           /* Consume the `[' token.  */
16318           cp_lexer_consume_token (parser->lexer);
16319           /* Read the operand name.  */
16320           name = cp_parser_identifier (parser);
16321           if (name != error_mark_node)
16322             name = build_string (IDENTIFIER_LENGTH (name),
16323                                  IDENTIFIER_POINTER (name));
16324           /* Look for the closing `]'.  */
16325           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16326         }
16327       else
16328         name = NULL_TREE;
16329       /* Look for the string-literal.  */
16330       string_literal = cp_parser_string_literal (parser, false, false);
16331
16332       /* Look for the `('.  */
16333       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16334       /* Parse the expression.  */
16335       expression = cp_parser_expression (parser, /*cast_p=*/false);
16336       /* Look for the `)'.  */
16337       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16338
16339       if (name == error_mark_node 
16340           || string_literal == error_mark_node 
16341           || expression == error_mark_node)
16342         invalid_operands = true;
16343
16344       /* Add this operand to the list.  */
16345       asm_operands = tree_cons (build_tree_list (name, string_literal),
16346                                 expression,
16347                                 asm_operands);
16348       /* If the next token is not a `,', there are no more
16349          operands.  */
16350       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16351         break;
16352       /* Consume the `,'.  */
16353       cp_lexer_consume_token (parser->lexer);
16354     }
16355
16356   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16357 }
16358
16359 /* Parse an asm-clobber-list.
16360
16361    asm-clobber-list:
16362      string-literal
16363      asm-clobber-list , string-literal
16364
16365    Returns a TREE_LIST, indicating the clobbers in the order that they
16366    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16367
16368 static tree
16369 cp_parser_asm_clobber_list (cp_parser* parser)
16370 {
16371   tree clobbers = NULL_TREE;
16372
16373   while (true)
16374     {
16375       tree string_literal;
16376
16377       /* Look for the string literal.  */
16378       string_literal = cp_parser_string_literal (parser, false, false);
16379       /* Add it to the list.  */
16380       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16381       /* If the next token is not a `,', then the list is
16382          complete.  */
16383       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16384         break;
16385       /* Consume the `,' token.  */
16386       cp_lexer_consume_token (parser->lexer);
16387     }
16388
16389   return clobbers;
16390 }
16391
16392 /* Parse an (optional) series of attributes.
16393
16394    attributes:
16395      attributes attribute
16396
16397    attribute:
16398      __attribute__ (( attribute-list [opt] ))
16399
16400    The return value is as for cp_parser_attribute_list.  */
16401
16402 static tree
16403 cp_parser_attributes_opt (cp_parser* parser)
16404 {
16405   tree attributes = NULL_TREE;
16406
16407   while (true)
16408     {
16409       cp_token *token;
16410       tree attribute_list;
16411
16412       /* Peek at the next token.  */
16413       token = cp_lexer_peek_token (parser->lexer);
16414       /* If it's not `__attribute__', then we're done.  */
16415       if (token->keyword != RID_ATTRIBUTE)
16416         break;
16417
16418       /* Consume the `__attribute__' keyword.  */
16419       cp_lexer_consume_token (parser->lexer);
16420       /* Look for the two `(' tokens.  */
16421       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16422       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16423
16424       /* Peek at the next token.  */
16425       token = cp_lexer_peek_token (parser->lexer);
16426       if (token->type != CPP_CLOSE_PAREN)
16427         /* Parse the attribute-list.  */
16428         attribute_list = cp_parser_attribute_list (parser);
16429       else
16430         /* If the next token is a `)', then there is no attribute
16431            list.  */
16432         attribute_list = NULL;
16433
16434       /* Look for the two `)' tokens.  */
16435       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16436       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16437
16438       /* Add these new attributes to the list.  */
16439       attributes = chainon (attributes, attribute_list);
16440     }
16441
16442   return attributes;
16443 }
16444
16445 /* Parse an attribute-list.
16446
16447    attribute-list:
16448      attribute
16449      attribute-list , attribute
16450
16451    attribute:
16452      identifier
16453      identifier ( identifier )
16454      identifier ( identifier , expression-list )
16455      identifier ( expression-list )
16456
16457    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16458    to an attribute.  The TREE_PURPOSE of each node is the identifier
16459    indicating which attribute is in use.  The TREE_VALUE represents
16460    the arguments, if any.  */
16461
16462 static tree
16463 cp_parser_attribute_list (cp_parser* parser)
16464 {
16465   tree attribute_list = NULL_TREE;
16466   bool save_translate_strings_p = parser->translate_strings_p;
16467
16468   parser->translate_strings_p = false;
16469   while (true)
16470     {
16471       cp_token *token;
16472       tree identifier;
16473       tree attribute;
16474
16475       /* Look for the identifier.  We also allow keywords here; for
16476          example `__attribute__ ((const))' is legal.  */
16477       token = cp_lexer_peek_token (parser->lexer);
16478       if (token->type == CPP_NAME
16479           || token->type == CPP_KEYWORD)
16480         {
16481           tree arguments = NULL_TREE;
16482
16483           /* Consume the token.  */
16484           token = cp_lexer_consume_token (parser->lexer);
16485
16486           /* Save away the identifier that indicates which attribute
16487              this is.  */
16488           identifier = token->u.value;
16489           attribute = build_tree_list (identifier, NULL_TREE);
16490
16491           /* Peek at the next token.  */
16492           token = cp_lexer_peek_token (parser->lexer);
16493           /* If it's an `(', then parse the attribute arguments.  */
16494           if (token->type == CPP_OPEN_PAREN)
16495             {
16496               arguments = cp_parser_parenthesized_expression_list
16497                           (parser, true, /*cast_p=*/false,
16498                            /*allow_expansion_p=*/false,
16499                            /*non_constant_p=*/NULL);
16500               /* Save the arguments away.  */
16501               TREE_VALUE (attribute) = arguments;
16502             }
16503
16504           if (arguments != error_mark_node)
16505             {
16506               /* Add this attribute to the list.  */
16507               TREE_CHAIN (attribute) = attribute_list;
16508               attribute_list = attribute;
16509             }
16510
16511           token = cp_lexer_peek_token (parser->lexer);
16512         }
16513       /* Now, look for more attributes.  If the next token isn't a
16514          `,', we're done.  */
16515       if (token->type != CPP_COMMA)
16516         break;
16517
16518       /* Consume the comma and keep going.  */
16519       cp_lexer_consume_token (parser->lexer);
16520     }
16521   parser->translate_strings_p = save_translate_strings_p;
16522
16523   /* We built up the list in reverse order.  */
16524   return nreverse (attribute_list);
16525 }
16526
16527 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16528    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16529    current value of the PEDANTIC flag, regardless of whether or not
16530    the `__extension__' keyword is present.  The caller is responsible
16531    for restoring the value of the PEDANTIC flag.  */
16532
16533 static bool
16534 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16535 {
16536   /* Save the old value of the PEDANTIC flag.  */
16537   *saved_pedantic = pedantic;
16538
16539   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16540     {
16541       /* Consume the `__extension__' token.  */
16542       cp_lexer_consume_token (parser->lexer);
16543       /* We're not being pedantic while the `__extension__' keyword is
16544          in effect.  */
16545       pedantic = 0;
16546
16547       return true;
16548     }
16549
16550   return false;
16551 }
16552
16553 /* Parse a label declaration.
16554
16555    label-declaration:
16556      __label__ label-declarator-seq ;
16557
16558    label-declarator-seq:
16559      identifier , label-declarator-seq
16560      identifier  */
16561
16562 static void
16563 cp_parser_label_declaration (cp_parser* parser)
16564 {
16565   /* Look for the `__label__' keyword.  */
16566   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16567
16568   while (true)
16569     {
16570       tree identifier;
16571
16572       /* Look for an identifier.  */
16573       identifier = cp_parser_identifier (parser);
16574       /* If we failed, stop.  */
16575       if (identifier == error_mark_node)
16576         break;
16577       /* Declare it as a label.  */
16578       finish_label_decl (identifier);
16579       /* If the next token is a `;', stop.  */
16580       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16581         break;
16582       /* Look for the `,' separating the label declarations.  */
16583       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16584     }
16585
16586   /* Look for the final `;'.  */
16587   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16588 }
16589
16590 /* Support Functions */
16591
16592 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16593    NAME should have one of the representations used for an
16594    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16595    is returned.  If PARSER->SCOPE is a dependent type, then a
16596    SCOPE_REF is returned.
16597
16598    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16599    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16600    was formed.  Abstractly, such entities should not be passed to this
16601    function, because they do not need to be looked up, but it is
16602    simpler to check for this special case here, rather than at the
16603    call-sites.
16604
16605    In cases not explicitly covered above, this function returns a
16606    DECL, OVERLOAD, or baselink representing the result of the lookup.
16607    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16608    is returned.
16609
16610    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16611    (e.g., "struct") that was used.  In that case bindings that do not
16612    refer to types are ignored.
16613
16614    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16615    ignored.
16616
16617    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16618    are ignored.
16619
16620    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16621    types.
16622
16623    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16624    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16625    NULL_TREE otherwise.  */
16626
16627 static tree
16628 cp_parser_lookup_name (cp_parser *parser, tree name,
16629                        enum tag_types tag_type,
16630                        bool is_template,
16631                        bool is_namespace,
16632                        bool check_dependency,
16633                        tree *ambiguous_decls,
16634                        location_t name_location)
16635 {
16636   int flags = 0;
16637   tree decl;
16638   tree object_type = parser->context->object_type;
16639
16640   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16641     flags |= LOOKUP_COMPLAIN;
16642
16643   /* Assume that the lookup will be unambiguous.  */
16644   if (ambiguous_decls)
16645     *ambiguous_decls = NULL_TREE;
16646
16647   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16648      no longer valid.  Note that if we are parsing tentatively, and
16649      the parse fails, OBJECT_TYPE will be automatically restored.  */
16650   parser->context->object_type = NULL_TREE;
16651
16652   if (name == error_mark_node)
16653     return error_mark_node;
16654
16655   /* A template-id has already been resolved; there is no lookup to
16656      do.  */
16657   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16658     return name;
16659   if (BASELINK_P (name))
16660     {
16661       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16662                   == TEMPLATE_ID_EXPR);
16663       return name;
16664     }
16665
16666   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16667      it should already have been checked to make sure that the name
16668      used matches the type being destroyed.  */
16669   if (TREE_CODE (name) == BIT_NOT_EXPR)
16670     {
16671       tree type;
16672
16673       /* Figure out to which type this destructor applies.  */
16674       if (parser->scope)
16675         type = parser->scope;
16676       else if (object_type)
16677         type = object_type;
16678       else
16679         type = current_class_type;
16680       /* If that's not a class type, there is no destructor.  */
16681       if (!type || !CLASS_TYPE_P (type))
16682         return error_mark_node;
16683       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16684         lazily_declare_fn (sfk_destructor, type);
16685       if (!CLASSTYPE_DESTRUCTORS (type))
16686           return error_mark_node;
16687       /* If it was a class type, return the destructor.  */
16688       return CLASSTYPE_DESTRUCTORS (type);
16689     }
16690
16691   /* By this point, the NAME should be an ordinary identifier.  If
16692      the id-expression was a qualified name, the qualifying scope is
16693      stored in PARSER->SCOPE at this point.  */
16694   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16695
16696   /* Perform the lookup.  */
16697   if (parser->scope)
16698     {
16699       bool dependent_p;
16700
16701       if (parser->scope == error_mark_node)
16702         return error_mark_node;
16703
16704       /* If the SCOPE is dependent, the lookup must be deferred until
16705          the template is instantiated -- unless we are explicitly
16706          looking up names in uninstantiated templates.  Even then, we
16707          cannot look up the name if the scope is not a class type; it
16708          might, for example, be a template type parameter.  */
16709       dependent_p = (TYPE_P (parser->scope)
16710                      && !(parser->in_declarator_p
16711                           && currently_open_class (parser->scope))
16712                      && dependent_type_p (parser->scope));
16713       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16714            && dependent_p)
16715         {
16716           if (tag_type)
16717             {
16718               tree type;
16719
16720               /* The resolution to Core Issue 180 says that `struct
16721                  A::B' should be considered a type-name, even if `A'
16722                  is dependent.  */
16723               type = make_typename_type (parser->scope, name, tag_type,
16724                                          /*complain=*/tf_error);
16725               decl = TYPE_NAME (type);
16726             }
16727           else if (is_template
16728                    && (cp_parser_next_token_ends_template_argument_p (parser)
16729                        || cp_lexer_next_token_is (parser->lexer,
16730                                                   CPP_CLOSE_PAREN)))
16731             decl = make_unbound_class_template (parser->scope,
16732                                                 name, NULL_TREE,
16733                                                 /*complain=*/tf_error);
16734           else
16735             decl = build_qualified_name (/*type=*/NULL_TREE,
16736                                          parser->scope, name,
16737                                          is_template);
16738         }
16739       else
16740         {
16741           tree pushed_scope = NULL_TREE;
16742
16743           /* If PARSER->SCOPE is a dependent type, then it must be a
16744              class type, and we must not be checking dependencies;
16745              otherwise, we would have processed this lookup above.  So
16746              that PARSER->SCOPE is not considered a dependent base by
16747              lookup_member, we must enter the scope here.  */
16748           if (dependent_p)
16749             pushed_scope = push_scope (parser->scope);
16750           /* If the PARSER->SCOPE is a template specialization, it
16751              may be instantiated during name lookup.  In that case,
16752              errors may be issued.  Even if we rollback the current
16753              tentative parse, those errors are valid.  */
16754           decl = lookup_qualified_name (parser->scope, name,
16755                                         tag_type != none_type,
16756                                         /*complain=*/true);
16757
16758           /* If we have a single function from a using decl, pull it out.  */
16759           if (decl
16760               && TREE_CODE (decl) == OVERLOAD
16761               && !really_overloaded_fn (decl))
16762             decl = OVL_FUNCTION (decl);
16763
16764           if (pushed_scope)
16765             pop_scope (pushed_scope);
16766         }
16767       parser->qualifying_scope = parser->scope;
16768       parser->object_scope = NULL_TREE;
16769     }
16770   else if (object_type)
16771     {
16772       tree object_decl = NULL_TREE;
16773       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16774          OBJECT_TYPE is not a class.  */
16775       if (CLASS_TYPE_P (object_type))
16776         /* If the OBJECT_TYPE is a template specialization, it may
16777            be instantiated during name lookup.  In that case, errors
16778            may be issued.  Even if we rollback the current tentative
16779            parse, those errors are valid.  */
16780         object_decl = lookup_member (object_type,
16781                                      name,
16782                                      /*protect=*/0,
16783                                      tag_type != none_type);
16784       /* Look it up in the enclosing context, too.  */
16785       decl = lookup_name_real (name, tag_type != none_type,
16786                                /*nonclass=*/0,
16787                                /*block_p=*/true, is_namespace, flags);
16788       parser->object_scope = object_type;
16789       parser->qualifying_scope = NULL_TREE;
16790       if (object_decl)
16791         decl = object_decl;
16792     }
16793   else
16794     {
16795       decl = lookup_name_real (name, tag_type != none_type,
16796                                /*nonclass=*/0,
16797                                /*block_p=*/true, is_namespace, flags);
16798       parser->qualifying_scope = NULL_TREE;
16799       parser->object_scope = NULL_TREE;
16800     }
16801
16802   /* If the lookup failed, let our caller know.  */
16803   if (!decl || decl == error_mark_node)
16804     return error_mark_node;
16805
16806   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16807   if (TREE_CODE (decl) == TREE_LIST)
16808     {
16809       if (ambiguous_decls)
16810         *ambiguous_decls = decl;
16811       /* The error message we have to print is too complicated for
16812          cp_parser_error, so we incorporate its actions directly.  */
16813       if (!cp_parser_simulate_error (parser))
16814         {
16815           error ("%Hreference to %qD is ambiguous",
16816                  &name_location, name);
16817           print_candidates (decl);
16818         }
16819       return error_mark_node;
16820     }
16821
16822   gcc_assert (DECL_P (decl)
16823               || TREE_CODE (decl) == OVERLOAD
16824               || TREE_CODE (decl) == SCOPE_REF
16825               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16826               || BASELINK_P (decl));
16827
16828   /* If we have resolved the name of a member declaration, check to
16829      see if the declaration is accessible.  When the name resolves to
16830      set of overloaded functions, accessibility is checked when
16831      overload resolution is done.
16832
16833      During an explicit instantiation, access is not checked at all,
16834      as per [temp.explicit].  */
16835   if (DECL_P (decl))
16836     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16837
16838   return decl;
16839 }
16840
16841 /* Like cp_parser_lookup_name, but for use in the typical case where
16842    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16843    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16844
16845 static tree
16846 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16847 {
16848   return cp_parser_lookup_name (parser, name,
16849                                 none_type,
16850                                 /*is_template=*/false,
16851                                 /*is_namespace=*/false,
16852                                 /*check_dependency=*/true,
16853                                 /*ambiguous_decls=*/NULL,
16854                                 location);
16855 }
16856
16857 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16858    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16859    true, the DECL indicates the class being defined in a class-head,
16860    or declared in an elaborated-type-specifier.
16861
16862    Otherwise, return DECL.  */
16863
16864 static tree
16865 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16866 {
16867   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16868      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16869
16870        struct A {
16871          template <typename T> struct B;
16872        };
16873
16874        template <typename T> struct A::B {};
16875
16876      Similarly, in an elaborated-type-specifier:
16877
16878        namespace N { struct X{}; }
16879
16880        struct A {
16881          template <typename T> friend struct N::X;
16882        };
16883
16884      However, if the DECL refers to a class type, and we are in
16885      the scope of the class, then the name lookup automatically
16886      finds the TYPE_DECL created by build_self_reference rather
16887      than a TEMPLATE_DECL.  For example, in:
16888
16889        template <class T> struct S {
16890          S s;
16891        };
16892
16893      there is no need to handle such case.  */
16894
16895   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16896     return DECL_TEMPLATE_RESULT (decl);
16897
16898   return decl;
16899 }
16900
16901 /* If too many, or too few, template-parameter lists apply to the
16902    declarator, issue an error message.  Returns TRUE if all went well,
16903    and FALSE otherwise.  */
16904
16905 static bool
16906 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16907                                                 cp_declarator *declarator,
16908                                                 location_t declarator_location)
16909 {
16910   unsigned num_templates;
16911
16912   /* We haven't seen any classes that involve template parameters yet.  */
16913   num_templates = 0;
16914
16915   switch (declarator->kind)
16916     {
16917     case cdk_id:
16918       if (declarator->u.id.qualifying_scope)
16919         {
16920           tree scope;
16921           tree member;
16922
16923           scope = declarator->u.id.qualifying_scope;
16924           member = declarator->u.id.unqualified_name;
16925
16926           while (scope && CLASS_TYPE_P (scope))
16927             {
16928               /* You're supposed to have one `template <...>'
16929                  for every template class, but you don't need one
16930                  for a full specialization.  For example:
16931
16932                  template <class T> struct S{};
16933                  template <> struct S<int> { void f(); };
16934                  void S<int>::f () {}
16935
16936                  is correct; there shouldn't be a `template <>' for
16937                  the definition of `S<int>::f'.  */
16938               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16939                 /* If SCOPE does not have template information of any
16940                    kind, then it is not a template, nor is it nested
16941                    within a template.  */
16942                 break;
16943               if (explicit_class_specialization_p (scope))
16944                 break;
16945               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16946                 ++num_templates;
16947
16948               scope = TYPE_CONTEXT (scope);
16949             }
16950         }
16951       else if (TREE_CODE (declarator->u.id.unqualified_name)
16952                == TEMPLATE_ID_EXPR)
16953         /* If the DECLARATOR has the form `X<y>' then it uses one
16954            additional level of template parameters.  */
16955         ++num_templates;
16956
16957       return cp_parser_check_template_parameters (parser,
16958                                                   num_templates,
16959                                                   declarator_location);
16960
16961     case cdk_function:
16962     case cdk_array:
16963     case cdk_pointer:
16964     case cdk_reference:
16965     case cdk_ptrmem:
16966       return (cp_parser_check_declarator_template_parameters
16967               (parser, declarator->declarator, declarator_location));
16968
16969     case cdk_error:
16970       return true;
16971
16972     default:
16973       gcc_unreachable ();
16974     }
16975   return false;
16976 }
16977
16978 /* NUM_TEMPLATES were used in the current declaration.  If that is
16979    invalid, return FALSE and issue an error messages.  Otherwise,
16980    return TRUE.  */
16981
16982 static bool
16983 cp_parser_check_template_parameters (cp_parser* parser,
16984                                      unsigned num_templates,
16985                                      location_t location)
16986 {
16987   /* If there are more template classes than parameter lists, we have
16988      something like:
16989
16990        template <class T> void S<T>::R<T>::f ();  */
16991   if (parser->num_template_parameter_lists < num_templates)
16992     {
16993       error ("%Htoo few template-parameter-lists", &location);
16994       return false;
16995     }
16996   /* If there are the same number of template classes and parameter
16997      lists, that's OK.  */
16998   if (parser->num_template_parameter_lists == num_templates)
16999     return true;
17000   /* If there are more, but only one more, then we are referring to a
17001      member template.  That's OK too.  */
17002   if (parser->num_template_parameter_lists == num_templates + 1)
17003       return true;
17004   /* Otherwise, there are too many template parameter lists.  We have
17005      something like:
17006
17007      template <class T> template <class U> void S::f();  */
17008   error ("%Htoo many template-parameter-lists", &location);
17009   return false;
17010 }
17011
17012 /* Parse an optional `::' token indicating that the following name is
17013    from the global namespace.  If so, PARSER->SCOPE is set to the
17014    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17015    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17016    Returns the new value of PARSER->SCOPE, if the `::' token is
17017    present, and NULL_TREE otherwise.  */
17018
17019 static tree
17020 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17021 {
17022   cp_token *token;
17023
17024   /* Peek at the next token.  */
17025   token = cp_lexer_peek_token (parser->lexer);
17026   /* If we're looking at a `::' token then we're starting from the
17027      global namespace, not our current location.  */
17028   if (token->type == CPP_SCOPE)
17029     {
17030       /* Consume the `::' token.  */
17031       cp_lexer_consume_token (parser->lexer);
17032       /* Set the SCOPE so that we know where to start the lookup.  */
17033       parser->scope = global_namespace;
17034       parser->qualifying_scope = global_namespace;
17035       parser->object_scope = NULL_TREE;
17036
17037       return parser->scope;
17038     }
17039   else if (!current_scope_valid_p)
17040     {
17041       parser->scope = NULL_TREE;
17042       parser->qualifying_scope = NULL_TREE;
17043       parser->object_scope = NULL_TREE;
17044     }
17045
17046   return NULL_TREE;
17047 }
17048
17049 /* Returns TRUE if the upcoming token sequence is the start of a
17050    constructor declarator.  If FRIEND_P is true, the declarator is
17051    preceded by the `friend' specifier.  */
17052
17053 static bool
17054 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17055 {
17056   bool constructor_p;
17057   tree type_decl = NULL_TREE;
17058   bool nested_name_p;
17059   cp_token *next_token;
17060
17061   /* The common case is that this is not a constructor declarator, so
17062      try to avoid doing lots of work if at all possible.  It's not
17063      valid declare a constructor at function scope.  */
17064   if (parser->in_function_body)
17065     return false;
17066   /* And only certain tokens can begin a constructor declarator.  */
17067   next_token = cp_lexer_peek_token (parser->lexer);
17068   if (next_token->type != CPP_NAME
17069       && next_token->type != CPP_SCOPE
17070       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17071       && next_token->type != CPP_TEMPLATE_ID)
17072     return false;
17073
17074   /* Parse tentatively; we are going to roll back all of the tokens
17075      consumed here.  */
17076   cp_parser_parse_tentatively (parser);
17077   /* Assume that we are looking at a constructor declarator.  */
17078   constructor_p = true;
17079
17080   /* Look for the optional `::' operator.  */
17081   cp_parser_global_scope_opt (parser,
17082                               /*current_scope_valid_p=*/false);
17083   /* Look for the nested-name-specifier.  */
17084   nested_name_p
17085     = (cp_parser_nested_name_specifier_opt (parser,
17086                                             /*typename_keyword_p=*/false,
17087                                             /*check_dependency_p=*/false,
17088                                             /*type_p=*/false,
17089                                             /*is_declaration=*/false)
17090        != NULL_TREE);
17091   /* Outside of a class-specifier, there must be a
17092      nested-name-specifier.  */
17093   if (!nested_name_p &&
17094       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17095        || friend_p))
17096     constructor_p = false;
17097   /* If we still think that this might be a constructor-declarator,
17098      look for a class-name.  */
17099   if (constructor_p)
17100     {
17101       /* If we have:
17102
17103            template <typename T> struct S { S(); };
17104            template <typename T> S<T>::S ();
17105
17106          we must recognize that the nested `S' names a class.
17107          Similarly, for:
17108
17109            template <typename T> S<T>::S<T> ();
17110
17111          we must recognize that the nested `S' names a template.  */
17112       type_decl = cp_parser_class_name (parser,
17113                                         /*typename_keyword_p=*/false,
17114                                         /*template_keyword_p=*/false,
17115                                         none_type,
17116                                         /*check_dependency_p=*/false,
17117                                         /*class_head_p=*/false,
17118                                         /*is_declaration=*/false);
17119       /* If there was no class-name, then this is not a constructor.  */
17120       constructor_p = !cp_parser_error_occurred (parser);
17121     }
17122
17123   /* If we're still considering a constructor, we have to see a `(',
17124      to begin the parameter-declaration-clause, followed by either a
17125      `)', an `...', or a decl-specifier.  We need to check for a
17126      type-specifier to avoid being fooled into thinking that:
17127
17128        S::S (f) (int);
17129
17130      is a constructor.  (It is actually a function named `f' that
17131      takes one parameter (of type `int') and returns a value of type
17132      `S::S'.  */
17133   if (constructor_p
17134       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17135     {
17136       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17137           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17138           /* A parameter declaration begins with a decl-specifier,
17139              which is either the "attribute" keyword, a storage class
17140              specifier, or (usually) a type-specifier.  */
17141           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17142         {
17143           tree type;
17144           tree pushed_scope = NULL_TREE;
17145           unsigned saved_num_template_parameter_lists;
17146
17147           /* Names appearing in the type-specifier should be looked up
17148              in the scope of the class.  */
17149           if (current_class_type)
17150             type = NULL_TREE;
17151           else
17152             {
17153               type = TREE_TYPE (type_decl);
17154               if (TREE_CODE (type) == TYPENAME_TYPE)
17155                 {
17156                   type = resolve_typename_type (type,
17157                                                 /*only_current_p=*/false);
17158                   if (TREE_CODE (type) == TYPENAME_TYPE)
17159                     {
17160                       cp_parser_abort_tentative_parse (parser);
17161                       return false;
17162                     }
17163                 }
17164               pushed_scope = push_scope (type);
17165             }
17166
17167           /* Inside the constructor parameter list, surrounding
17168              template-parameter-lists do not apply.  */
17169           saved_num_template_parameter_lists
17170             = parser->num_template_parameter_lists;
17171           parser->num_template_parameter_lists = 0;
17172
17173           /* Look for the type-specifier.  */
17174           cp_parser_type_specifier (parser,
17175                                     CP_PARSER_FLAGS_NONE,
17176                                     /*decl_specs=*/NULL,
17177                                     /*is_declarator=*/true,
17178                                     /*declares_class_or_enum=*/NULL,
17179                                     /*is_cv_qualifier=*/NULL);
17180
17181           parser->num_template_parameter_lists
17182             = saved_num_template_parameter_lists;
17183
17184           /* Leave the scope of the class.  */
17185           if (pushed_scope)
17186             pop_scope (pushed_scope);
17187
17188           constructor_p = !cp_parser_error_occurred (parser);
17189         }
17190     }
17191   else
17192     constructor_p = false;
17193   /* We did not really want to consume any tokens.  */
17194   cp_parser_abort_tentative_parse (parser);
17195
17196   return constructor_p;
17197 }
17198
17199 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17200    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17201    they must be performed once we are in the scope of the function.
17202
17203    Returns the function defined.  */
17204
17205 static tree
17206 cp_parser_function_definition_from_specifiers_and_declarator
17207   (cp_parser* parser,
17208    cp_decl_specifier_seq *decl_specifiers,
17209    tree attributes,
17210    const cp_declarator *declarator)
17211 {
17212   tree fn;
17213   bool success_p;
17214
17215   /* Begin the function-definition.  */
17216   success_p = start_function (decl_specifiers, declarator, attributes);
17217
17218   /* The things we're about to see are not directly qualified by any
17219      template headers we've seen thus far.  */
17220   reset_specialization ();
17221
17222   /* If there were names looked up in the decl-specifier-seq that we
17223      did not check, check them now.  We must wait until we are in the
17224      scope of the function to perform the checks, since the function
17225      might be a friend.  */
17226   perform_deferred_access_checks ();
17227
17228   if (!success_p)
17229     {
17230       /* Skip the entire function.  */
17231       cp_parser_skip_to_end_of_block_or_statement (parser);
17232       fn = error_mark_node;
17233     }
17234   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17235     {
17236       /* Seen already, skip it.  An error message has already been output.  */
17237       cp_parser_skip_to_end_of_block_or_statement (parser);
17238       fn = current_function_decl;
17239       current_function_decl = NULL_TREE;
17240       /* If this is a function from a class, pop the nested class.  */
17241       if (current_class_name)
17242         pop_nested_class ();
17243     }
17244   else
17245     fn = cp_parser_function_definition_after_declarator (parser,
17246                                                          /*inline_p=*/false);
17247
17248   return fn;
17249 }
17250
17251 /* Parse the part of a function-definition that follows the
17252    declarator.  INLINE_P is TRUE iff this function is an inline
17253    function defined with a class-specifier.
17254
17255    Returns the function defined.  */
17256
17257 static tree
17258 cp_parser_function_definition_after_declarator (cp_parser* parser,
17259                                                 bool inline_p)
17260 {
17261   tree fn;
17262   bool ctor_initializer_p = false;
17263   bool saved_in_unbraced_linkage_specification_p;
17264   bool saved_in_function_body;
17265   unsigned saved_num_template_parameter_lists;
17266   cp_token *token;
17267
17268   saved_in_function_body = parser->in_function_body;
17269   parser->in_function_body = true;
17270   /* If the next token is `return', then the code may be trying to
17271      make use of the "named return value" extension that G++ used to
17272      support.  */
17273   token = cp_lexer_peek_token (parser->lexer);
17274   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17275     {
17276       /* Consume the `return' keyword.  */
17277       cp_lexer_consume_token (parser->lexer);
17278       /* Look for the identifier that indicates what value is to be
17279          returned.  */
17280       cp_parser_identifier (parser);
17281       /* Issue an error message.  */
17282       error ("%Hnamed return values are no longer supported",
17283              &token->location);
17284       /* Skip tokens until we reach the start of the function body.  */
17285       while (true)
17286         {
17287           cp_token *token = cp_lexer_peek_token (parser->lexer);
17288           if (token->type == CPP_OPEN_BRACE
17289               || token->type == CPP_EOF
17290               || token->type == CPP_PRAGMA_EOL)
17291             break;
17292           cp_lexer_consume_token (parser->lexer);
17293         }
17294     }
17295   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17296      anything declared inside `f'.  */
17297   saved_in_unbraced_linkage_specification_p
17298     = parser->in_unbraced_linkage_specification_p;
17299   parser->in_unbraced_linkage_specification_p = false;
17300   /* Inside the function, surrounding template-parameter-lists do not
17301      apply.  */
17302   saved_num_template_parameter_lists
17303     = parser->num_template_parameter_lists;
17304   parser->num_template_parameter_lists = 0;
17305   /* If the next token is `try', then we are looking at a
17306      function-try-block.  */
17307   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17308     ctor_initializer_p = cp_parser_function_try_block (parser);
17309   /* A function-try-block includes the function-body, so we only do
17310      this next part if we're not processing a function-try-block.  */
17311   else
17312     ctor_initializer_p
17313       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17314
17315   /* Finish the function.  */
17316   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17317                         (inline_p ? 2 : 0));
17318   /* Generate code for it, if necessary.  */
17319   expand_or_defer_fn (fn);
17320   /* Restore the saved values.  */
17321   parser->in_unbraced_linkage_specification_p
17322     = saved_in_unbraced_linkage_specification_p;
17323   parser->num_template_parameter_lists
17324     = saved_num_template_parameter_lists;
17325   parser->in_function_body = saved_in_function_body;
17326
17327   return fn;
17328 }
17329
17330 /* Parse a template-declaration, assuming that the `export' (and
17331    `extern') keywords, if present, has already been scanned.  MEMBER_P
17332    is as for cp_parser_template_declaration.  */
17333
17334 static void
17335 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17336 {
17337   tree decl = NULL_TREE;
17338   VEC (deferred_access_check,gc) *checks;
17339   tree parameter_list;
17340   bool friend_p = false;
17341   bool need_lang_pop;
17342   cp_token *token;
17343
17344   /* Look for the `template' keyword.  */
17345   token = cp_lexer_peek_token (parser->lexer);
17346   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17347     return;
17348
17349   /* And the `<'.  */
17350   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17351     return;
17352   if (at_class_scope_p () && current_function_decl)
17353     {
17354       /* 14.5.2.2 [temp.mem]
17355
17356          A local class shall not have member templates.  */
17357       error ("%Hinvalid declaration of member template in local class",
17358              &token->location);
17359       cp_parser_skip_to_end_of_block_or_statement (parser);
17360       return;
17361     }
17362   /* [temp]
17363
17364      A template ... shall not have C linkage.  */
17365   if (current_lang_name == lang_name_c)
17366     {
17367       error ("%Htemplate with C linkage", &token->location);
17368       /* Give it C++ linkage to avoid confusing other parts of the
17369          front end.  */
17370       push_lang_context (lang_name_cplusplus);
17371       need_lang_pop = true;
17372     }
17373   else
17374     need_lang_pop = false;
17375
17376   /* We cannot perform access checks on the template parameter
17377      declarations until we know what is being declared, just as we
17378      cannot check the decl-specifier list.  */
17379   push_deferring_access_checks (dk_deferred);
17380
17381   /* If the next token is `>', then we have an invalid
17382      specialization.  Rather than complain about an invalid template
17383      parameter, issue an error message here.  */
17384   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17385     {
17386       cp_parser_error (parser, "invalid explicit specialization");
17387       begin_specialization ();
17388       parameter_list = NULL_TREE;
17389     }
17390   else
17391     /* Parse the template parameters.  */
17392     parameter_list = cp_parser_template_parameter_list (parser);
17393
17394   /* Get the deferred access checks from the parameter list.  These
17395      will be checked once we know what is being declared, as for a
17396      member template the checks must be performed in the scope of the
17397      class containing the member.  */
17398   checks = get_deferred_access_checks ();
17399
17400   /* Look for the `>'.  */
17401   cp_parser_skip_to_end_of_template_parameter_list (parser);
17402   /* We just processed one more parameter list.  */
17403   ++parser->num_template_parameter_lists;
17404   /* If the next token is `template', there are more template
17405      parameters.  */
17406   if (cp_lexer_next_token_is_keyword (parser->lexer,
17407                                       RID_TEMPLATE))
17408     cp_parser_template_declaration_after_export (parser, member_p);
17409   else
17410     {
17411       /* There are no access checks when parsing a template, as we do not
17412          know if a specialization will be a friend.  */
17413       push_deferring_access_checks (dk_no_check);
17414       token = cp_lexer_peek_token (parser->lexer);
17415       decl = cp_parser_single_declaration (parser,
17416                                            checks,
17417                                            member_p,
17418                                            /*explicit_specialization_p=*/false,
17419                                            &friend_p);
17420       pop_deferring_access_checks ();
17421
17422       /* If this is a member template declaration, let the front
17423          end know.  */
17424       if (member_p && !friend_p && decl)
17425         {
17426           if (TREE_CODE (decl) == TYPE_DECL)
17427             cp_parser_check_access_in_redeclaration (decl, token->location);
17428
17429           decl = finish_member_template_decl (decl);
17430         }
17431       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17432         make_friend_class (current_class_type, TREE_TYPE (decl),
17433                            /*complain=*/true);
17434     }
17435   /* We are done with the current parameter list.  */
17436   --parser->num_template_parameter_lists;
17437
17438   pop_deferring_access_checks ();
17439
17440   /* Finish up.  */
17441   finish_template_decl (parameter_list);
17442
17443   /* Register member declarations.  */
17444   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17445     finish_member_declaration (decl);
17446   /* For the erroneous case of a template with C linkage, we pushed an
17447      implicit C++ linkage scope; exit that scope now.  */
17448   if (need_lang_pop)
17449     pop_lang_context ();
17450   /* If DECL is a function template, we must return to parse it later.
17451      (Even though there is no definition, there might be default
17452      arguments that need handling.)  */
17453   if (member_p && decl
17454       && (TREE_CODE (decl) == FUNCTION_DECL
17455           || DECL_FUNCTION_TEMPLATE_P (decl)))
17456     TREE_VALUE (parser->unparsed_functions_queues)
17457       = tree_cons (NULL_TREE, decl,
17458                    TREE_VALUE (parser->unparsed_functions_queues));
17459 }
17460
17461 /* Perform the deferred access checks from a template-parameter-list.
17462    CHECKS is a TREE_LIST of access checks, as returned by
17463    get_deferred_access_checks.  */
17464
17465 static void
17466 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17467 {
17468   ++processing_template_parmlist;
17469   perform_access_checks (checks);
17470   --processing_template_parmlist;
17471 }
17472
17473 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17474    `function-definition' sequence.  MEMBER_P is true, this declaration
17475    appears in a class scope.
17476
17477    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17478    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17479
17480 static tree
17481 cp_parser_single_declaration (cp_parser* parser,
17482                               VEC (deferred_access_check,gc)* checks,
17483                               bool member_p,
17484                               bool explicit_specialization_p,
17485                               bool* friend_p)
17486 {
17487   int declares_class_or_enum;
17488   tree decl = NULL_TREE;
17489   cp_decl_specifier_seq decl_specifiers;
17490   bool function_definition_p = false;
17491   cp_token *decl_spec_token_start;
17492
17493   /* This function is only used when processing a template
17494      declaration.  */
17495   gcc_assert (innermost_scope_kind () == sk_template_parms
17496               || innermost_scope_kind () == sk_template_spec);
17497
17498   /* Defer access checks until we know what is being declared.  */
17499   push_deferring_access_checks (dk_deferred);
17500
17501   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17502      alternative.  */
17503   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17504   cp_parser_decl_specifier_seq (parser,
17505                                 CP_PARSER_FLAGS_OPTIONAL,
17506                                 &decl_specifiers,
17507                                 &declares_class_or_enum);
17508   if (friend_p)
17509     *friend_p = cp_parser_friend_p (&decl_specifiers);
17510
17511   /* There are no template typedefs.  */
17512   if (decl_specifiers.specs[(int) ds_typedef])
17513     {
17514       error ("%Htemplate declaration of %qs",
17515              &decl_spec_token_start->location, "typedef");
17516       decl = error_mark_node;
17517     }
17518
17519   /* Gather up the access checks that occurred the
17520      decl-specifier-seq.  */
17521   stop_deferring_access_checks ();
17522
17523   /* Check for the declaration of a template class.  */
17524   if (declares_class_or_enum)
17525     {
17526       if (cp_parser_declares_only_class_p (parser))
17527         {
17528           decl = shadow_tag (&decl_specifiers);
17529
17530           /* In this case:
17531
17532                struct C {
17533                  friend template <typename T> struct A<T>::B;
17534                };
17535
17536              A<T>::B will be represented by a TYPENAME_TYPE, and
17537              therefore not recognized by shadow_tag.  */
17538           if (friend_p && *friend_p
17539               && !decl
17540               && decl_specifiers.type
17541               && TYPE_P (decl_specifiers.type))
17542             decl = decl_specifiers.type;
17543
17544           if (decl && decl != error_mark_node)
17545             decl = TYPE_NAME (decl);
17546           else
17547             decl = error_mark_node;
17548
17549           /* Perform access checks for template parameters.  */
17550           cp_parser_perform_template_parameter_access_checks (checks);
17551         }
17552     }
17553   /* If it's not a template class, try for a template function.  If
17554      the next token is a `;', then this declaration does not declare
17555      anything.  But, if there were errors in the decl-specifiers, then
17556      the error might well have come from an attempted class-specifier.
17557      In that case, there's no need to warn about a missing declarator.  */
17558   if (!decl
17559       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17560           || decl_specifiers.type != error_mark_node))
17561     {
17562       decl = cp_parser_init_declarator (parser,
17563                                         &decl_specifiers,
17564                                         checks,
17565                                         /*function_definition_allowed_p=*/true,
17566                                         member_p,
17567                                         declares_class_or_enum,
17568                                         &function_definition_p);
17569
17570     /* 7.1.1-1 [dcl.stc]
17571
17572        A storage-class-specifier shall not be specified in an explicit
17573        specialization...  */
17574     if (decl
17575         && explicit_specialization_p
17576         && decl_specifiers.storage_class != sc_none)
17577       {
17578         error ("%Hexplicit template specialization cannot have a storage class",
17579                &decl_spec_token_start->location);
17580         decl = error_mark_node;
17581       }
17582     }
17583
17584   pop_deferring_access_checks ();
17585
17586   /* Clear any current qualification; whatever comes next is the start
17587      of something new.  */
17588   parser->scope = NULL_TREE;
17589   parser->qualifying_scope = NULL_TREE;
17590   parser->object_scope = NULL_TREE;
17591   /* Look for a trailing `;' after the declaration.  */
17592   if (!function_definition_p
17593       && (decl == error_mark_node
17594           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17595     cp_parser_skip_to_end_of_block_or_statement (parser);
17596
17597   return decl;
17598 }
17599
17600 /* Parse a cast-expression that is not the operand of a unary "&".  */
17601
17602 static tree
17603 cp_parser_simple_cast_expression (cp_parser *parser)
17604 {
17605   return cp_parser_cast_expression (parser, /*address_p=*/false,
17606                                     /*cast_p=*/false);
17607 }
17608
17609 /* Parse a functional cast to TYPE.  Returns an expression
17610    representing the cast.  */
17611
17612 static tree
17613 cp_parser_functional_cast (cp_parser* parser, tree type)
17614 {
17615   tree expression_list;
17616   tree cast;
17617   bool nonconst_p;
17618
17619   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17620     {
17621       maybe_warn_cpp0x ("extended initializer lists");
17622       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17623       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17624       if (TREE_CODE (type) == TYPE_DECL)
17625         type = TREE_TYPE (type);
17626       return finish_compound_literal (type, expression_list);
17627     }
17628
17629   expression_list
17630     = cp_parser_parenthesized_expression_list (parser, false,
17631                                                /*cast_p=*/true,
17632                                                /*allow_expansion_p=*/true,
17633                                                /*non_constant_p=*/NULL);
17634
17635   cast = build_functional_cast (type, expression_list,
17636                                 tf_warning_or_error);
17637   /* [expr.const]/1: In an integral constant expression "only type
17638      conversions to integral or enumeration type can be used".  */
17639   if (TREE_CODE (type) == TYPE_DECL)
17640     type = TREE_TYPE (type);
17641   if (cast != error_mark_node
17642       && !cast_valid_in_integral_constant_expression_p (type)
17643       && (cp_parser_non_integral_constant_expression
17644           (parser, "a call to a constructor")))
17645     return error_mark_node;
17646   return cast;
17647 }
17648
17649 /* Save the tokens that make up the body of a member function defined
17650    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17651    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17652    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17653    for the member function.  */
17654
17655 static tree
17656 cp_parser_save_member_function_body (cp_parser* parser,
17657                                      cp_decl_specifier_seq *decl_specifiers,
17658                                      cp_declarator *declarator,
17659                                      tree attributes)
17660 {
17661   cp_token *first;
17662   cp_token *last;
17663   tree fn;
17664
17665   /* Create the function-declaration.  */
17666   fn = start_method (decl_specifiers, declarator, attributes);
17667   /* If something went badly wrong, bail out now.  */
17668   if (fn == error_mark_node)
17669     {
17670       /* If there's a function-body, skip it.  */
17671       if (cp_parser_token_starts_function_definition_p
17672           (cp_lexer_peek_token (parser->lexer)))
17673         cp_parser_skip_to_end_of_block_or_statement (parser);
17674       return error_mark_node;
17675     }
17676
17677   /* Remember it, if there default args to post process.  */
17678   cp_parser_save_default_args (parser, fn);
17679
17680   /* Save away the tokens that make up the body of the
17681      function.  */
17682   first = parser->lexer->next_token;
17683   /* We can have braced-init-list mem-initializers before the fn body.  */
17684   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17685     {
17686       cp_lexer_consume_token (parser->lexer);
17687       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17688              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17689         {
17690           /* cache_group will stop after an un-nested { } pair, too.  */
17691           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17692             break;
17693
17694           /* variadic mem-inits have ... after the ')'.  */
17695           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17696             cp_lexer_consume_token (parser->lexer);
17697         }
17698     }
17699   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17700   /* Handle function try blocks.  */
17701   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17702     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17703   last = parser->lexer->next_token;
17704
17705   /* Save away the inline definition; we will process it when the
17706      class is complete.  */
17707   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17708   DECL_PENDING_INLINE_P (fn) = 1;
17709
17710   /* We need to know that this was defined in the class, so that
17711      friend templates are handled correctly.  */
17712   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17713
17714   /* We're done with the inline definition.  */
17715   finish_method (fn);
17716
17717   /* Add FN to the queue of functions to be parsed later.  */
17718   TREE_VALUE (parser->unparsed_functions_queues)
17719     = tree_cons (NULL_TREE, fn,
17720                  TREE_VALUE (parser->unparsed_functions_queues));
17721
17722   return fn;
17723 }
17724
17725 /* Parse a template-argument-list, as well as the trailing ">" (but
17726    not the opening ">").  See cp_parser_template_argument_list for the
17727    return value.  */
17728
17729 static tree
17730 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17731 {
17732   tree arguments;
17733   tree saved_scope;
17734   tree saved_qualifying_scope;
17735   tree saved_object_scope;
17736   bool saved_greater_than_is_operator_p;
17737   bool saved_skip_evaluation;
17738
17739   /* [temp.names]
17740
17741      When parsing a template-id, the first non-nested `>' is taken as
17742      the end of the template-argument-list rather than a greater-than
17743      operator.  */
17744   saved_greater_than_is_operator_p
17745     = parser->greater_than_is_operator_p;
17746   parser->greater_than_is_operator_p = false;
17747   /* Parsing the argument list may modify SCOPE, so we save it
17748      here.  */
17749   saved_scope = parser->scope;
17750   saved_qualifying_scope = parser->qualifying_scope;
17751   saved_object_scope = parser->object_scope;
17752   /* We need to evaluate the template arguments, even though this
17753      template-id may be nested within a "sizeof".  */
17754   saved_skip_evaluation = skip_evaluation;
17755   skip_evaluation = false;
17756   /* Parse the template-argument-list itself.  */
17757   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17758       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17759     arguments = NULL_TREE;
17760   else
17761     arguments = cp_parser_template_argument_list (parser);
17762   /* Look for the `>' that ends the template-argument-list. If we find
17763      a '>>' instead, it's probably just a typo.  */
17764   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17765     {
17766       if (cxx_dialect != cxx98)
17767         {
17768           /* In C++0x, a `>>' in a template argument list or cast
17769              expression is considered to be two separate `>'
17770              tokens. So, change the current token to a `>', but don't
17771              consume it: it will be consumed later when the outer
17772              template argument list (or cast expression) is parsed.
17773              Note that this replacement of `>' for `>>' is necessary
17774              even if we are parsing tentatively: in the tentative
17775              case, after calling
17776              cp_parser_enclosed_template_argument_list we will always
17777              throw away all of the template arguments and the first
17778              closing `>', either because the template argument list
17779              was erroneous or because we are replacing those tokens
17780              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17781              not have been thrown away) is needed either to close an
17782              outer template argument list or to complete a new-style
17783              cast.  */
17784           cp_token *token = cp_lexer_peek_token (parser->lexer);
17785           token->type = CPP_GREATER;
17786         }
17787       else if (!saved_greater_than_is_operator_p)
17788         {
17789           /* If we're in a nested template argument list, the '>>' has
17790             to be a typo for '> >'. We emit the error message, but we
17791             continue parsing and we push a '>' as next token, so that
17792             the argument list will be parsed correctly.  Note that the
17793             global source location is still on the token before the
17794             '>>', so we need to say explicitly where we want it.  */
17795           cp_token *token = cp_lexer_peek_token (parser->lexer);
17796           error ("%H%<>>%> should be %<> >%> "
17797                  "within a nested template argument list",
17798                  &token->location);
17799
17800           token->type = CPP_GREATER;
17801         }
17802       else
17803         {
17804           /* If this is not a nested template argument list, the '>>'
17805             is a typo for '>'. Emit an error message and continue.
17806             Same deal about the token location, but here we can get it
17807             right by consuming the '>>' before issuing the diagnostic.  */
17808           cp_token *token = cp_lexer_consume_token (parser->lexer);
17809           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17810                  "a template argument list", &token->location);
17811         }
17812     }
17813   else
17814     cp_parser_skip_to_end_of_template_parameter_list (parser);
17815   /* The `>' token might be a greater-than operator again now.  */
17816   parser->greater_than_is_operator_p
17817     = saved_greater_than_is_operator_p;
17818   /* Restore the SAVED_SCOPE.  */
17819   parser->scope = saved_scope;
17820   parser->qualifying_scope = saved_qualifying_scope;
17821   parser->object_scope = saved_object_scope;
17822   skip_evaluation = saved_skip_evaluation;
17823
17824   return arguments;
17825 }
17826
17827 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17828    arguments, or the body of the function have not yet been parsed,
17829    parse them now.  */
17830
17831 static void
17832 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17833 {
17834   /* If this member is a template, get the underlying
17835      FUNCTION_DECL.  */
17836   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17837     member_function = DECL_TEMPLATE_RESULT (member_function);
17838
17839   /* There should not be any class definitions in progress at this
17840      point; the bodies of members are only parsed outside of all class
17841      definitions.  */
17842   gcc_assert (parser->num_classes_being_defined == 0);
17843   /* While we're parsing the member functions we might encounter more
17844      classes.  We want to handle them right away, but we don't want
17845      them getting mixed up with functions that are currently in the
17846      queue.  */
17847   parser->unparsed_functions_queues
17848     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17849
17850   /* Make sure that any template parameters are in scope.  */
17851   maybe_begin_member_template_processing (member_function);
17852
17853   /* If the body of the function has not yet been parsed, parse it
17854      now.  */
17855   if (DECL_PENDING_INLINE_P (member_function))
17856     {
17857       tree function_scope;
17858       cp_token_cache *tokens;
17859
17860       /* The function is no longer pending; we are processing it.  */
17861       tokens = DECL_PENDING_INLINE_INFO (member_function);
17862       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17863       DECL_PENDING_INLINE_P (member_function) = 0;
17864
17865       /* If this is a local class, enter the scope of the containing
17866          function.  */
17867       function_scope = current_function_decl;
17868       if (function_scope)
17869         push_function_context ();
17870
17871       /* Push the body of the function onto the lexer stack.  */
17872       cp_parser_push_lexer_for_tokens (parser, tokens);
17873
17874       /* Let the front end know that we going to be defining this
17875          function.  */
17876       start_preparsed_function (member_function, NULL_TREE,
17877                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17878
17879       /* Don't do access checking if it is a templated function.  */
17880       if (processing_template_decl)
17881         push_deferring_access_checks (dk_no_check);
17882
17883       /* Now, parse the body of the function.  */
17884       cp_parser_function_definition_after_declarator (parser,
17885                                                       /*inline_p=*/true);
17886
17887       if (processing_template_decl)
17888         pop_deferring_access_checks ();
17889
17890       /* Leave the scope of the containing function.  */
17891       if (function_scope)
17892         pop_function_context ();
17893       cp_parser_pop_lexer (parser);
17894     }
17895
17896   /* Remove any template parameters from the symbol table.  */
17897   maybe_end_member_template_processing ();
17898
17899   /* Restore the queue.  */
17900   parser->unparsed_functions_queues
17901     = TREE_CHAIN (parser->unparsed_functions_queues);
17902 }
17903
17904 /* If DECL contains any default args, remember it on the unparsed
17905    functions queue.  */
17906
17907 static void
17908 cp_parser_save_default_args (cp_parser* parser, tree decl)
17909 {
17910   tree probe;
17911
17912   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17913        probe;
17914        probe = TREE_CHAIN (probe))
17915     if (TREE_PURPOSE (probe))
17916       {
17917         TREE_PURPOSE (parser->unparsed_functions_queues)
17918           = tree_cons (current_class_type, decl,
17919                        TREE_PURPOSE (parser->unparsed_functions_queues));
17920         break;
17921       }
17922 }
17923
17924 /* FN is a FUNCTION_DECL which may contains a parameter with an
17925    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17926    assumes that the current scope is the scope in which the default
17927    argument should be processed.  */
17928
17929 static void
17930 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17931 {
17932   bool saved_local_variables_forbidden_p;
17933   tree parm;
17934
17935   /* While we're parsing the default args, we might (due to the
17936      statement expression extension) encounter more classes.  We want
17937      to handle them right away, but we don't want them getting mixed
17938      up with default args that are currently in the queue.  */
17939   parser->unparsed_functions_queues
17940     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17941
17942   /* Local variable names (and the `this' keyword) may not appear
17943      in a default argument.  */
17944   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17945   parser->local_variables_forbidden_p = true;
17946
17947   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17948        parm;
17949        parm = TREE_CHAIN (parm))
17950     {
17951       cp_token_cache *tokens;
17952       tree default_arg = TREE_PURPOSE (parm);
17953       tree parsed_arg;
17954       VEC(tree,gc) *insts;
17955       tree copy;
17956       unsigned ix;
17957
17958       if (!default_arg)
17959         continue;
17960
17961       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17962         /* This can happen for a friend declaration for a function
17963            already declared with default arguments.  */
17964         continue;
17965
17966        /* Push the saved tokens for the default argument onto the parser's
17967           lexer stack.  */
17968       tokens = DEFARG_TOKENS (default_arg);
17969       cp_parser_push_lexer_for_tokens (parser, tokens);
17970
17971       /* Parse the assignment-expression.  */
17972       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17973
17974       if (!processing_template_decl)
17975         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17976
17977       TREE_PURPOSE (parm) = parsed_arg;
17978
17979       /* Update any instantiations we've already created.  */
17980       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17981            VEC_iterate (tree, insts, ix, copy); ix++)
17982         TREE_PURPOSE (copy) = parsed_arg;
17983
17984       /* If the token stream has not been completely used up, then
17985          there was extra junk after the end of the default
17986          argument.  */
17987       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17988         cp_parser_error (parser, "expected %<,%>");
17989
17990       /* Revert to the main lexer.  */
17991       cp_parser_pop_lexer (parser);
17992     }
17993
17994   /* Make sure no default arg is missing.  */
17995   check_default_args (fn);
17996
17997   /* Restore the state of local_variables_forbidden_p.  */
17998   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17999
18000   /* Restore the queue.  */
18001   parser->unparsed_functions_queues
18002     = TREE_CHAIN (parser->unparsed_functions_queues);
18003 }
18004
18005 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18006    either a TYPE or an expression, depending on the form of the
18007    input.  The KEYWORD indicates which kind of expression we have
18008    encountered.  */
18009
18010 static tree
18011 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18012 {
18013   tree expr = NULL_TREE;
18014   const char *saved_message;
18015   char *tmp;
18016   bool saved_integral_constant_expression_p;
18017   bool saved_non_integral_constant_expression_p;
18018   bool pack_expansion_p = false;
18019
18020   /* Types cannot be defined in a `sizeof' expression.  Save away the
18021      old message.  */
18022   saved_message = parser->type_definition_forbidden_message;
18023   /* And create the new one.  */
18024   tmp = concat ("types may not be defined in %<",
18025                 IDENTIFIER_POINTER (ridpointers[keyword]),
18026                 "%> expressions", NULL);
18027   parser->type_definition_forbidden_message = tmp;
18028
18029   /* The restrictions on constant-expressions do not apply inside
18030      sizeof expressions.  */
18031   saved_integral_constant_expression_p
18032     = parser->integral_constant_expression_p;
18033   saved_non_integral_constant_expression_p
18034     = parser->non_integral_constant_expression_p;
18035   parser->integral_constant_expression_p = false;
18036
18037   /* If it's a `...', then we are computing the length of a parameter
18038      pack.  */
18039   if (keyword == RID_SIZEOF
18040       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18041     {
18042       /* Consume the `...'.  */
18043       cp_lexer_consume_token (parser->lexer);
18044       maybe_warn_variadic_templates ();
18045
18046       /* Note that this is an expansion.  */
18047       pack_expansion_p = true;
18048     }
18049
18050   /* Do not actually evaluate the expression.  */
18051   ++skip_evaluation;
18052   /* If it's a `(', then we might be looking at the type-id
18053      construction.  */
18054   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18055     {
18056       tree type;
18057       bool saved_in_type_id_in_expr_p;
18058
18059       /* We can't be sure yet whether we're looking at a type-id or an
18060          expression.  */
18061       cp_parser_parse_tentatively (parser);
18062       /* Consume the `('.  */
18063       cp_lexer_consume_token (parser->lexer);
18064       /* Parse the type-id.  */
18065       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18066       parser->in_type_id_in_expr_p = true;
18067       type = cp_parser_type_id (parser);
18068       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18069       /* Now, look for the trailing `)'.  */
18070       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18071       /* If all went well, then we're done.  */
18072       if (cp_parser_parse_definitely (parser))
18073         {
18074           cp_decl_specifier_seq decl_specs;
18075
18076           /* Build a trivial decl-specifier-seq.  */
18077           clear_decl_specs (&decl_specs);
18078           decl_specs.type = type;
18079
18080           /* Call grokdeclarator to figure out what type this is.  */
18081           expr = grokdeclarator (NULL,
18082                                  &decl_specs,
18083                                  TYPENAME,
18084                                  /*initialized=*/0,
18085                                  /*attrlist=*/NULL);
18086         }
18087     }
18088
18089   /* If the type-id production did not work out, then we must be
18090      looking at the unary-expression production.  */
18091   if (!expr)
18092     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18093                                        /*cast_p=*/false);
18094
18095   if (pack_expansion_p)
18096     /* Build a pack expansion. */
18097     expr = make_pack_expansion (expr);
18098
18099   /* Go back to evaluating expressions.  */
18100   --skip_evaluation;
18101
18102   /* Free the message we created.  */
18103   free (tmp);
18104   /* And restore the old one.  */
18105   parser->type_definition_forbidden_message = saved_message;
18106   parser->integral_constant_expression_p
18107     = saved_integral_constant_expression_p;
18108   parser->non_integral_constant_expression_p
18109     = saved_non_integral_constant_expression_p;
18110
18111   return expr;
18112 }
18113
18114 /* If the current declaration has no declarator, return true.  */
18115
18116 static bool
18117 cp_parser_declares_only_class_p (cp_parser *parser)
18118 {
18119   /* If the next token is a `;' or a `,' then there is no
18120      declarator.  */
18121   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18122           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18123 }
18124
18125 /* Update the DECL_SPECS to reflect the storage class indicated by
18126    KEYWORD.  */
18127
18128 static void
18129 cp_parser_set_storage_class (cp_parser *parser,
18130                              cp_decl_specifier_seq *decl_specs,
18131                              enum rid keyword,
18132                              location_t location)
18133 {
18134   cp_storage_class storage_class;
18135
18136   if (parser->in_unbraced_linkage_specification_p)
18137     {
18138       error ("%Hinvalid use of %qD in linkage specification",
18139              &location, ridpointers[keyword]);
18140       return;
18141     }
18142   else if (decl_specs->storage_class != sc_none)
18143     {
18144       decl_specs->conflicting_specifiers_p = true;
18145       return;
18146     }
18147
18148   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18149       && decl_specs->specs[(int) ds_thread])
18150     {
18151       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18152       decl_specs->specs[(int) ds_thread] = 0;
18153     }
18154
18155   switch (keyword)
18156     {
18157     case RID_AUTO:
18158       storage_class = sc_auto;
18159       break;
18160     case RID_REGISTER:
18161       storage_class = sc_register;
18162       break;
18163     case RID_STATIC:
18164       storage_class = sc_static;
18165       break;
18166     case RID_EXTERN:
18167       storage_class = sc_extern;
18168       break;
18169     case RID_MUTABLE:
18170       storage_class = sc_mutable;
18171       break;
18172     default:
18173       gcc_unreachable ();
18174     }
18175   decl_specs->storage_class = storage_class;
18176
18177   /* A storage class specifier cannot be applied alongside a typedef 
18178      specifier. If there is a typedef specifier present then set 
18179      conflicting_specifiers_p which will trigger an error later
18180      on in grokdeclarator. */
18181   if (decl_specs->specs[(int)ds_typedef])
18182     decl_specs->conflicting_specifiers_p = true;
18183 }
18184
18185 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18186    is true, the type is a user-defined type; otherwise it is a
18187    built-in type specified by a keyword.  */
18188
18189 static void
18190 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18191                               tree type_spec,
18192                               location_t location,
18193                               bool user_defined_p)
18194 {
18195   decl_specs->any_specifiers_p = true;
18196
18197   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18198      (with, for example, in "typedef int wchar_t;") we remember that
18199      this is what happened.  In system headers, we ignore these
18200      declarations so that G++ can work with system headers that are not
18201      C++-safe.  */
18202   if (decl_specs->specs[(int) ds_typedef]
18203       && !user_defined_p
18204       && (type_spec == boolean_type_node
18205           || type_spec == char16_type_node
18206           || type_spec == char32_type_node
18207           || type_spec == wchar_type_node)
18208       && (decl_specs->type
18209           || decl_specs->specs[(int) ds_long]
18210           || decl_specs->specs[(int) ds_short]
18211           || decl_specs->specs[(int) ds_unsigned]
18212           || decl_specs->specs[(int) ds_signed]))
18213     {
18214       decl_specs->redefined_builtin_type = type_spec;
18215       if (!decl_specs->type)
18216         {
18217           decl_specs->type = type_spec;
18218           decl_specs->user_defined_type_p = false;
18219           decl_specs->type_location = location;
18220         }
18221     }
18222   else if (decl_specs->type)
18223     decl_specs->multiple_types_p = true;
18224   else
18225     {
18226       decl_specs->type = type_spec;
18227       decl_specs->user_defined_type_p = user_defined_p;
18228       decl_specs->redefined_builtin_type = NULL_TREE;
18229       decl_specs->type_location = location;
18230     }
18231 }
18232
18233 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18234    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18235
18236 static bool
18237 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18238 {
18239   return decl_specifiers->specs[(int) ds_friend] != 0;
18240 }
18241
18242 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18243    issue an error message indicating that TOKEN_DESC was expected.
18244
18245    Returns the token consumed, if the token had the appropriate type.
18246    Otherwise, returns NULL.  */
18247
18248 static cp_token *
18249 cp_parser_require (cp_parser* parser,
18250                    enum cpp_ttype type,
18251                    const char* token_desc)
18252 {
18253   if (cp_lexer_next_token_is (parser->lexer, type))
18254     return cp_lexer_consume_token (parser->lexer);
18255   else
18256     {
18257       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18258       if (!cp_parser_simulate_error (parser))
18259         {
18260           char *message = concat ("expected ", token_desc, NULL);
18261           cp_parser_error (parser, message);
18262           free (message);
18263         }
18264       return NULL;
18265     }
18266 }
18267
18268 /* An error message is produced if the next token is not '>'.
18269    All further tokens are skipped until the desired token is
18270    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18271
18272 static void
18273 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18274 {
18275   /* Current level of '< ... >'.  */
18276   unsigned level = 0;
18277   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18278   unsigned nesting_depth = 0;
18279
18280   /* Are we ready, yet?  If not, issue error message.  */
18281   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18282     return;
18283
18284   /* Skip tokens until the desired token is found.  */
18285   while (true)
18286     {
18287       /* Peek at the next token.  */
18288       switch (cp_lexer_peek_token (parser->lexer)->type)
18289         {
18290         case CPP_LESS:
18291           if (!nesting_depth)
18292             ++level;
18293           break;
18294
18295         case CPP_RSHIFT:
18296           if (cxx_dialect == cxx98)
18297             /* C++0x views the `>>' operator as two `>' tokens, but
18298                C++98 does not. */
18299             break;
18300           else if (!nesting_depth && level-- == 0)
18301             {
18302               /* We've hit a `>>' where the first `>' closes the
18303                  template argument list, and the second `>' is
18304                  spurious.  Just consume the `>>' and stop; we've
18305                  already produced at least one error.  */
18306               cp_lexer_consume_token (parser->lexer);
18307               return;
18308             }
18309           /* Fall through for C++0x, so we handle the second `>' in
18310              the `>>'.  */
18311
18312         case CPP_GREATER:
18313           if (!nesting_depth && level-- == 0)
18314             {
18315               /* We've reached the token we want, consume it and stop.  */
18316               cp_lexer_consume_token (parser->lexer);
18317               return;
18318             }
18319           break;
18320
18321         case CPP_OPEN_PAREN:
18322         case CPP_OPEN_SQUARE:
18323           ++nesting_depth;
18324           break;
18325
18326         case CPP_CLOSE_PAREN:
18327         case CPP_CLOSE_SQUARE:
18328           if (nesting_depth-- == 0)
18329             return;
18330           break;
18331
18332         case CPP_EOF:
18333         case CPP_PRAGMA_EOL:
18334         case CPP_SEMICOLON:
18335         case CPP_OPEN_BRACE:
18336         case CPP_CLOSE_BRACE:
18337           /* The '>' was probably forgotten, don't look further.  */
18338           return;
18339
18340         default:
18341           break;
18342         }
18343
18344       /* Consume this token.  */
18345       cp_lexer_consume_token (parser->lexer);
18346     }
18347 }
18348
18349 /* If the next token is the indicated keyword, consume it.  Otherwise,
18350    issue an error message indicating that TOKEN_DESC was expected.
18351
18352    Returns the token consumed, if the token had the appropriate type.
18353    Otherwise, returns NULL.  */
18354
18355 static cp_token *
18356 cp_parser_require_keyword (cp_parser* parser,
18357                            enum rid keyword,
18358                            const char* token_desc)
18359 {
18360   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18361
18362   if (token && token->keyword != keyword)
18363     {
18364       dyn_string_t error_msg;
18365
18366       /* Format the error message.  */
18367       error_msg = dyn_string_new (0);
18368       dyn_string_append_cstr (error_msg, "expected ");
18369       dyn_string_append_cstr (error_msg, token_desc);
18370       cp_parser_error (parser, error_msg->s);
18371       dyn_string_delete (error_msg);
18372       return NULL;
18373     }
18374
18375   return token;
18376 }
18377
18378 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18379    function-definition.  */
18380
18381 static bool
18382 cp_parser_token_starts_function_definition_p (cp_token* token)
18383 {
18384   return (/* An ordinary function-body begins with an `{'.  */
18385           token->type == CPP_OPEN_BRACE
18386           /* A ctor-initializer begins with a `:'.  */
18387           || token->type == CPP_COLON
18388           /* A function-try-block begins with `try'.  */
18389           || token->keyword == RID_TRY
18390           /* The named return value extension begins with `return'.  */
18391           || token->keyword == RID_RETURN);
18392 }
18393
18394 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18395    definition.  */
18396
18397 static bool
18398 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18399 {
18400   cp_token *token;
18401
18402   token = cp_lexer_peek_token (parser->lexer);
18403   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18404 }
18405
18406 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18407    C++0x) ending a template-argument.  */
18408
18409 static bool
18410 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18411 {
18412   cp_token *token;
18413
18414   token = cp_lexer_peek_token (parser->lexer);
18415   return (token->type == CPP_COMMA 
18416           || token->type == CPP_GREATER
18417           || token->type == CPP_ELLIPSIS
18418           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18419 }
18420
18421 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18422    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18423
18424 static bool
18425 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18426                                                      size_t n)
18427 {
18428   cp_token *token;
18429
18430   token = cp_lexer_peek_nth_token (parser->lexer, n);
18431   if (token->type == CPP_LESS)
18432     return true;
18433   /* Check for the sequence `<::' in the original code. It would be lexed as
18434      `[:', where `[' is a digraph, and there is no whitespace before
18435      `:'.  */
18436   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18437     {
18438       cp_token *token2;
18439       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18440       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18441         return true;
18442     }
18443   return false;
18444 }
18445
18446 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18447    or none_type otherwise.  */
18448
18449 static enum tag_types
18450 cp_parser_token_is_class_key (cp_token* token)
18451 {
18452   switch (token->keyword)
18453     {
18454     case RID_CLASS:
18455       return class_type;
18456     case RID_STRUCT:
18457       return record_type;
18458     case RID_UNION:
18459       return union_type;
18460
18461     default:
18462       return none_type;
18463     }
18464 }
18465
18466 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18467
18468 static void
18469 cp_parser_check_class_key (enum tag_types class_key, tree type)
18470 {
18471   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18472     permerror (input_location, "%qs tag used in naming %q#T",
18473             class_key == union_type ? "union"
18474              : class_key == record_type ? "struct" : "class",
18475              type);
18476 }
18477
18478 /* Issue an error message if DECL is redeclared with different
18479    access than its original declaration [class.access.spec/3].
18480    This applies to nested classes and nested class templates.
18481    [class.mem/1].  */
18482
18483 static void
18484 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18485 {
18486   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18487     return;
18488
18489   if ((TREE_PRIVATE (decl)
18490        != (current_access_specifier == access_private_node))
18491       || (TREE_PROTECTED (decl)
18492           != (current_access_specifier == access_protected_node)))
18493     error ("%H%qD redeclared with different access", &location, decl);
18494 }
18495
18496 /* Look for the `template' keyword, as a syntactic disambiguator.
18497    Return TRUE iff it is present, in which case it will be
18498    consumed.  */
18499
18500 static bool
18501 cp_parser_optional_template_keyword (cp_parser *parser)
18502 {
18503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18504     {
18505       /* The `template' keyword can only be used within templates;
18506          outside templates the parser can always figure out what is a
18507          template and what is not.  */
18508       if (!processing_template_decl)
18509         {
18510           cp_token *token = cp_lexer_peek_token (parser->lexer);
18511           error ("%H%<template%> (as a disambiguator) is only allowed "
18512                  "within templates", &token->location);
18513           /* If this part of the token stream is rescanned, the same
18514              error message would be generated.  So, we purge the token
18515              from the stream.  */
18516           cp_lexer_purge_token (parser->lexer);
18517           return false;
18518         }
18519       else
18520         {
18521           /* Consume the `template' keyword.  */
18522           cp_lexer_consume_token (parser->lexer);
18523           return true;
18524         }
18525     }
18526
18527   return false;
18528 }
18529
18530 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18531    set PARSER->SCOPE, and perform other related actions.  */
18532
18533 static void
18534 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18535 {
18536   int i;
18537   struct tree_check *check_value;
18538   deferred_access_check *chk;
18539   VEC (deferred_access_check,gc) *checks;
18540
18541   /* Get the stored value.  */
18542   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18543   /* Perform any access checks that were deferred.  */
18544   checks = check_value->checks;
18545   if (checks)
18546     {
18547       for (i = 0 ;
18548            VEC_iterate (deferred_access_check, checks, i, chk) ;
18549            ++i)
18550         {
18551           perform_or_defer_access_check (chk->binfo,
18552                                          chk->decl,
18553                                          chk->diag_decl);
18554         }
18555     }
18556   /* Set the scope from the stored value.  */
18557   parser->scope = check_value->value;
18558   parser->qualifying_scope = check_value->qualifying_scope;
18559   parser->object_scope = NULL_TREE;
18560 }
18561
18562 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18563    encounter the end of a block before what we were looking for.  */
18564
18565 static bool
18566 cp_parser_cache_group (cp_parser *parser,
18567                        enum cpp_ttype end,
18568                        unsigned depth)
18569 {
18570   while (true)
18571     {
18572       cp_token *token = cp_lexer_peek_token (parser->lexer);
18573
18574       /* Abort a parenthesized expression if we encounter a semicolon.  */
18575       if ((end == CPP_CLOSE_PAREN || depth == 0)
18576           && token->type == CPP_SEMICOLON)
18577         return true;
18578       /* If we've reached the end of the file, stop.  */
18579       if (token->type == CPP_EOF
18580           || (end != CPP_PRAGMA_EOL
18581               && token->type == CPP_PRAGMA_EOL))
18582         return true;
18583       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18584         /* We've hit the end of an enclosing block, so there's been some
18585            kind of syntax error.  */
18586         return true;
18587
18588       /* Consume the token.  */
18589       cp_lexer_consume_token (parser->lexer);
18590       /* See if it starts a new group.  */
18591       if (token->type == CPP_OPEN_BRACE)
18592         {
18593           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18594           /* In theory this should probably check end == '}', but
18595              cp_parser_save_member_function_body needs it to exit
18596              after either '}' or ')' when called with ')'.  */
18597           if (depth == 0)
18598             return false;
18599         }
18600       else if (token->type == CPP_OPEN_PAREN)
18601         {
18602           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18603           if (depth == 0 && end == CPP_CLOSE_PAREN)
18604             return false;
18605         }
18606       else if (token->type == CPP_PRAGMA)
18607         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18608       else if (token->type == end)
18609         return false;
18610     }
18611 }
18612
18613 /* Begin parsing tentatively.  We always save tokens while parsing
18614    tentatively so that if the tentative parsing fails we can restore the
18615    tokens.  */
18616
18617 static void
18618 cp_parser_parse_tentatively (cp_parser* parser)
18619 {
18620   /* Enter a new parsing context.  */
18621   parser->context = cp_parser_context_new (parser->context);
18622   /* Begin saving tokens.  */
18623   cp_lexer_save_tokens (parser->lexer);
18624   /* In order to avoid repetitive access control error messages,
18625      access checks are queued up until we are no longer parsing
18626      tentatively.  */
18627   push_deferring_access_checks (dk_deferred);
18628 }
18629
18630 /* Commit to the currently active tentative parse.  */
18631
18632 static void
18633 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18634 {
18635   cp_parser_context *context;
18636   cp_lexer *lexer;
18637
18638   /* Mark all of the levels as committed.  */
18639   lexer = parser->lexer;
18640   for (context = parser->context; context->next; context = context->next)
18641     {
18642       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18643         break;
18644       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18645       while (!cp_lexer_saving_tokens (lexer))
18646         lexer = lexer->next;
18647       cp_lexer_commit_tokens (lexer);
18648     }
18649 }
18650
18651 /* Abort the currently active tentative parse.  All consumed tokens
18652    will be rolled back, and no diagnostics will be issued.  */
18653
18654 static void
18655 cp_parser_abort_tentative_parse (cp_parser* parser)
18656 {
18657   cp_parser_simulate_error (parser);
18658   /* Now, pretend that we want to see if the construct was
18659      successfully parsed.  */
18660   cp_parser_parse_definitely (parser);
18661 }
18662
18663 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18664    token stream.  Otherwise, commit to the tokens we have consumed.
18665    Returns true if no error occurred; false otherwise.  */
18666
18667 static bool
18668 cp_parser_parse_definitely (cp_parser* parser)
18669 {
18670   bool error_occurred;
18671   cp_parser_context *context;
18672
18673   /* Remember whether or not an error occurred, since we are about to
18674      destroy that information.  */
18675   error_occurred = cp_parser_error_occurred (parser);
18676   /* Remove the topmost context from the stack.  */
18677   context = parser->context;
18678   parser->context = context->next;
18679   /* If no parse errors occurred, commit to the tentative parse.  */
18680   if (!error_occurred)
18681     {
18682       /* Commit to the tokens read tentatively, unless that was
18683          already done.  */
18684       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18685         cp_lexer_commit_tokens (parser->lexer);
18686
18687       pop_to_parent_deferring_access_checks ();
18688     }
18689   /* Otherwise, if errors occurred, roll back our state so that things
18690      are just as they were before we began the tentative parse.  */
18691   else
18692     {
18693       cp_lexer_rollback_tokens (parser->lexer);
18694       pop_deferring_access_checks ();
18695     }
18696   /* Add the context to the front of the free list.  */
18697   context->next = cp_parser_context_free_list;
18698   cp_parser_context_free_list = context;
18699
18700   return !error_occurred;
18701 }
18702
18703 /* Returns true if we are parsing tentatively and are not committed to
18704    this tentative parse.  */
18705
18706 static bool
18707 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18708 {
18709   return (cp_parser_parsing_tentatively (parser)
18710           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18711 }
18712
18713 /* Returns nonzero iff an error has occurred during the most recent
18714    tentative parse.  */
18715
18716 static bool
18717 cp_parser_error_occurred (cp_parser* parser)
18718 {
18719   return (cp_parser_parsing_tentatively (parser)
18720           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18721 }
18722
18723 /* Returns nonzero if GNU extensions are allowed.  */
18724
18725 static bool
18726 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18727 {
18728   return parser->allow_gnu_extensions_p;
18729 }
18730 \f
18731 /* Objective-C++ Productions */
18732
18733
18734 /* Parse an Objective-C expression, which feeds into a primary-expression
18735    above.
18736
18737    objc-expression:
18738      objc-message-expression
18739      objc-string-literal
18740      objc-encode-expression
18741      objc-protocol-expression
18742      objc-selector-expression
18743
18744   Returns a tree representation of the expression.  */
18745
18746 static tree
18747 cp_parser_objc_expression (cp_parser* parser)
18748 {
18749   /* Try to figure out what kind of declaration is present.  */
18750   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18751
18752   switch (kwd->type)
18753     {
18754     case CPP_OPEN_SQUARE:
18755       return cp_parser_objc_message_expression (parser);
18756
18757     case CPP_OBJC_STRING:
18758       kwd = cp_lexer_consume_token (parser->lexer);
18759       return objc_build_string_object (kwd->u.value);
18760
18761     case CPP_KEYWORD:
18762       switch (kwd->keyword)
18763         {
18764         case RID_AT_ENCODE:
18765           return cp_parser_objc_encode_expression (parser);
18766
18767         case RID_AT_PROTOCOL:
18768           return cp_parser_objc_protocol_expression (parser);
18769
18770         case RID_AT_SELECTOR:
18771           return cp_parser_objc_selector_expression (parser);
18772
18773         default:
18774           break;
18775         }
18776     default:
18777       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18778              &kwd->location, kwd->u.value);
18779       cp_parser_skip_to_end_of_block_or_statement (parser);
18780     }
18781
18782   return error_mark_node;
18783 }
18784
18785 /* Parse an Objective-C message expression.
18786
18787    objc-message-expression:
18788      [ objc-message-receiver objc-message-args ]
18789
18790    Returns a representation of an Objective-C message.  */
18791
18792 static tree
18793 cp_parser_objc_message_expression (cp_parser* parser)
18794 {
18795   tree receiver, messageargs;
18796
18797   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18798   receiver = cp_parser_objc_message_receiver (parser);
18799   messageargs = cp_parser_objc_message_args (parser);
18800   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18801
18802   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18803 }
18804
18805 /* Parse an objc-message-receiver.
18806
18807    objc-message-receiver:
18808      expression
18809      simple-type-specifier
18810
18811   Returns a representation of the type or expression.  */
18812
18813 static tree
18814 cp_parser_objc_message_receiver (cp_parser* parser)
18815 {
18816   tree rcv;
18817
18818   /* An Objective-C message receiver may be either (1) a type
18819      or (2) an expression.  */
18820   cp_parser_parse_tentatively (parser);
18821   rcv = cp_parser_expression (parser, false);
18822
18823   if (cp_parser_parse_definitely (parser))
18824     return rcv;
18825
18826   rcv = cp_parser_simple_type_specifier (parser,
18827                                          /*decl_specs=*/NULL,
18828                                          CP_PARSER_FLAGS_NONE);
18829
18830   return objc_get_class_reference (rcv);
18831 }
18832
18833 /* Parse the arguments and selectors comprising an Objective-C message.
18834
18835    objc-message-args:
18836      objc-selector
18837      objc-selector-args
18838      objc-selector-args , objc-comma-args
18839
18840    objc-selector-args:
18841      objc-selector [opt] : assignment-expression
18842      objc-selector-args objc-selector [opt] : assignment-expression
18843
18844    objc-comma-args:
18845      assignment-expression
18846      objc-comma-args , assignment-expression
18847
18848    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18849    selector arguments and TREE_VALUE containing a list of comma
18850    arguments.  */
18851
18852 static tree
18853 cp_parser_objc_message_args (cp_parser* parser)
18854 {
18855   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18856   bool maybe_unary_selector_p = true;
18857   cp_token *token = cp_lexer_peek_token (parser->lexer);
18858
18859   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18860     {
18861       tree selector = NULL_TREE, arg;
18862
18863       if (token->type != CPP_COLON)
18864         selector = cp_parser_objc_selector (parser);
18865
18866       /* Detect if we have a unary selector.  */
18867       if (maybe_unary_selector_p
18868           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18869         return build_tree_list (selector, NULL_TREE);
18870
18871       maybe_unary_selector_p = false;
18872       cp_parser_require (parser, CPP_COLON, "%<:%>");
18873       arg = cp_parser_assignment_expression (parser, false);
18874
18875       sel_args
18876         = chainon (sel_args,
18877                    build_tree_list (selector, arg));
18878
18879       token = cp_lexer_peek_token (parser->lexer);
18880     }
18881
18882   /* Handle non-selector arguments, if any. */
18883   while (token->type == CPP_COMMA)
18884     {
18885       tree arg;
18886
18887       cp_lexer_consume_token (parser->lexer);
18888       arg = cp_parser_assignment_expression (parser, false);
18889
18890       addl_args
18891         = chainon (addl_args,
18892                    build_tree_list (NULL_TREE, arg));
18893
18894       token = cp_lexer_peek_token (parser->lexer);
18895     }
18896
18897   return build_tree_list (sel_args, addl_args);
18898 }
18899
18900 /* Parse an Objective-C encode expression.
18901
18902    objc-encode-expression:
18903      @encode objc-typename
18904
18905    Returns an encoded representation of the type argument.  */
18906
18907 static tree
18908 cp_parser_objc_encode_expression (cp_parser* parser)
18909 {
18910   tree type;
18911   cp_token *token;
18912
18913   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18914   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18915   token = cp_lexer_peek_token (parser->lexer);
18916   type = complete_type (cp_parser_type_id (parser));
18917   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18918
18919   if (!type)
18920     {
18921       error ("%H%<@encode%> must specify a type as an argument",
18922              &token->location);
18923       return error_mark_node;
18924     }
18925
18926   return objc_build_encode_expr (type);
18927 }
18928
18929 /* Parse an Objective-C @defs expression.  */
18930
18931 static tree
18932 cp_parser_objc_defs_expression (cp_parser *parser)
18933 {
18934   tree name;
18935
18936   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18937   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18938   name = cp_parser_identifier (parser);
18939   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18940
18941   return objc_get_class_ivars (name);
18942 }
18943
18944 /* Parse an Objective-C protocol expression.
18945
18946   objc-protocol-expression:
18947     @protocol ( identifier )
18948
18949   Returns a representation of the protocol expression.  */
18950
18951 static tree
18952 cp_parser_objc_protocol_expression (cp_parser* parser)
18953 {
18954   tree proto;
18955
18956   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18957   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18958   proto = cp_parser_identifier (parser);
18959   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18960
18961   return objc_build_protocol_expr (proto);
18962 }
18963
18964 /* Parse an Objective-C selector expression.
18965
18966    objc-selector-expression:
18967      @selector ( objc-method-signature )
18968
18969    objc-method-signature:
18970      objc-selector
18971      objc-selector-seq
18972
18973    objc-selector-seq:
18974      objc-selector :
18975      objc-selector-seq objc-selector :
18976
18977   Returns a representation of the method selector.  */
18978
18979 static tree
18980 cp_parser_objc_selector_expression (cp_parser* parser)
18981 {
18982   tree sel_seq = NULL_TREE;
18983   bool maybe_unary_selector_p = true;
18984   cp_token *token;
18985
18986   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18987   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18988   token = cp_lexer_peek_token (parser->lexer);
18989
18990   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18991          || token->type == CPP_SCOPE)
18992     {
18993       tree selector = NULL_TREE;
18994
18995       if (token->type != CPP_COLON
18996           || token->type == CPP_SCOPE)
18997         selector = cp_parser_objc_selector (parser);
18998
18999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19000           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19001         {
19002           /* Detect if we have a unary selector.  */
19003           if (maybe_unary_selector_p)
19004             {
19005               sel_seq = selector;
19006               goto finish_selector;
19007             }
19008           else
19009             {
19010               cp_parser_error (parser, "expected %<:%>");
19011             }
19012         }
19013       maybe_unary_selector_p = false;
19014       token = cp_lexer_consume_token (parser->lexer);
19015
19016       if (token->type == CPP_SCOPE)
19017         {
19018           sel_seq
19019             = chainon (sel_seq,
19020                        build_tree_list (selector, NULL_TREE));
19021           sel_seq
19022             = chainon (sel_seq,
19023                        build_tree_list (NULL_TREE, NULL_TREE));
19024         }
19025       else
19026         sel_seq
19027           = chainon (sel_seq,
19028                      build_tree_list (selector, NULL_TREE));
19029
19030       token = cp_lexer_peek_token (parser->lexer);
19031     }
19032
19033  finish_selector:
19034   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19035
19036   return objc_build_selector_expr (sel_seq);
19037 }
19038
19039 /* Parse a list of identifiers.
19040
19041    objc-identifier-list:
19042      identifier
19043      objc-identifier-list , identifier
19044
19045    Returns a TREE_LIST of identifier nodes.  */
19046
19047 static tree
19048 cp_parser_objc_identifier_list (cp_parser* parser)
19049 {
19050   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19051   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19052
19053   while (sep->type == CPP_COMMA)
19054     {
19055       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19056       list = chainon (list,
19057                       build_tree_list (NULL_TREE,
19058                                        cp_parser_identifier (parser)));
19059       sep = cp_lexer_peek_token (parser->lexer);
19060     }
19061
19062   return list;
19063 }
19064
19065 /* Parse an Objective-C alias declaration.
19066
19067    objc-alias-declaration:
19068      @compatibility_alias identifier identifier ;
19069
19070    This function registers the alias mapping with the Objective-C front end.
19071    It returns nothing.  */
19072
19073 static void
19074 cp_parser_objc_alias_declaration (cp_parser* parser)
19075 {
19076   tree alias, orig;
19077
19078   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19079   alias = cp_parser_identifier (parser);
19080   orig = cp_parser_identifier (parser);
19081   objc_declare_alias (alias, orig);
19082   cp_parser_consume_semicolon_at_end_of_statement (parser);
19083 }
19084
19085 /* Parse an Objective-C class forward-declaration.
19086
19087    objc-class-declaration:
19088      @class objc-identifier-list ;
19089
19090    The function registers the forward declarations with the Objective-C
19091    front end.  It returns nothing.  */
19092
19093 static void
19094 cp_parser_objc_class_declaration (cp_parser* parser)
19095 {
19096   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19097   objc_declare_class (cp_parser_objc_identifier_list (parser));
19098   cp_parser_consume_semicolon_at_end_of_statement (parser);
19099 }
19100
19101 /* Parse a list of Objective-C protocol references.
19102
19103    objc-protocol-refs-opt:
19104      objc-protocol-refs [opt]
19105
19106    objc-protocol-refs:
19107      < objc-identifier-list >
19108
19109    Returns a TREE_LIST of identifiers, if any.  */
19110
19111 static tree
19112 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19113 {
19114   tree protorefs = NULL_TREE;
19115
19116   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19117     {
19118       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19119       protorefs = cp_parser_objc_identifier_list (parser);
19120       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19121     }
19122
19123   return protorefs;
19124 }
19125
19126 /* Parse a Objective-C visibility specification.  */
19127
19128 static void
19129 cp_parser_objc_visibility_spec (cp_parser* parser)
19130 {
19131   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19132
19133   switch (vis->keyword)
19134     {
19135     case RID_AT_PRIVATE:
19136       objc_set_visibility (2);
19137       break;
19138     case RID_AT_PROTECTED:
19139       objc_set_visibility (0);
19140       break;
19141     case RID_AT_PUBLIC:
19142       objc_set_visibility (1);
19143       break;
19144     default:
19145       return;
19146     }
19147
19148   /* Eat '@private'/'@protected'/'@public'.  */
19149   cp_lexer_consume_token (parser->lexer);
19150 }
19151
19152 /* Parse an Objective-C method type.  */
19153
19154 static void
19155 cp_parser_objc_method_type (cp_parser* parser)
19156 {
19157   objc_set_method_type
19158    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19159     ? PLUS_EXPR
19160     : MINUS_EXPR);
19161 }
19162
19163 /* Parse an Objective-C protocol qualifier.  */
19164
19165 static tree
19166 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19167 {
19168   tree quals = NULL_TREE, node;
19169   cp_token *token = cp_lexer_peek_token (parser->lexer);
19170
19171   node = token->u.value;
19172
19173   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19174          && (node == ridpointers [(int) RID_IN]
19175              || node == ridpointers [(int) RID_OUT]
19176              || node == ridpointers [(int) RID_INOUT]
19177              || node == ridpointers [(int) RID_BYCOPY]
19178              || node == ridpointers [(int) RID_BYREF]
19179              || node == ridpointers [(int) RID_ONEWAY]))
19180     {
19181       quals = tree_cons (NULL_TREE, node, quals);
19182       cp_lexer_consume_token (parser->lexer);
19183       token = cp_lexer_peek_token (parser->lexer);
19184       node = token->u.value;
19185     }
19186
19187   return quals;
19188 }
19189
19190 /* Parse an Objective-C typename.  */
19191
19192 static tree
19193 cp_parser_objc_typename (cp_parser* parser)
19194 {
19195   tree type_name = NULL_TREE;
19196
19197   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19198     {
19199       tree proto_quals, cp_type = NULL_TREE;
19200
19201       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19202       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19203
19204       /* An ObjC type name may consist of just protocol qualifiers, in which
19205          case the type shall default to 'id'.  */
19206       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19207         cp_type = cp_parser_type_id (parser);
19208
19209       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19210       type_name = build_tree_list (proto_quals, cp_type);
19211     }
19212
19213   return type_name;
19214 }
19215
19216 /* Check to see if TYPE refers to an Objective-C selector name.  */
19217
19218 static bool
19219 cp_parser_objc_selector_p (enum cpp_ttype type)
19220 {
19221   return (type == CPP_NAME || type == CPP_KEYWORD
19222           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19223           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19224           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19225           || type == CPP_XOR || type == CPP_XOR_EQ);
19226 }
19227
19228 /* Parse an Objective-C selector.  */
19229
19230 static tree
19231 cp_parser_objc_selector (cp_parser* parser)
19232 {
19233   cp_token *token = cp_lexer_consume_token (parser->lexer);
19234
19235   if (!cp_parser_objc_selector_p (token->type))
19236     {
19237       error ("%Hinvalid Objective-C++ selector name", &token->location);
19238       return error_mark_node;
19239     }
19240
19241   /* C++ operator names are allowed to appear in ObjC selectors.  */
19242   switch (token->type)
19243     {
19244     case CPP_AND_AND: return get_identifier ("and");
19245     case CPP_AND_EQ: return get_identifier ("and_eq");
19246     case CPP_AND: return get_identifier ("bitand");
19247     case CPP_OR: return get_identifier ("bitor");
19248     case CPP_COMPL: return get_identifier ("compl");
19249     case CPP_NOT: return get_identifier ("not");
19250     case CPP_NOT_EQ: return get_identifier ("not_eq");
19251     case CPP_OR_OR: return get_identifier ("or");
19252     case CPP_OR_EQ: return get_identifier ("or_eq");
19253     case CPP_XOR: return get_identifier ("xor");
19254     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19255     default: return token->u.value;
19256     }
19257 }
19258
19259 /* Parse an Objective-C params list.  */
19260
19261 static tree
19262 cp_parser_objc_method_keyword_params (cp_parser* parser)
19263 {
19264   tree params = NULL_TREE;
19265   bool maybe_unary_selector_p = true;
19266   cp_token *token = cp_lexer_peek_token (parser->lexer);
19267
19268   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19269     {
19270       tree selector = NULL_TREE, type_name, identifier;
19271
19272       if (token->type != CPP_COLON)
19273         selector = cp_parser_objc_selector (parser);
19274
19275       /* Detect if we have a unary selector.  */
19276       if (maybe_unary_selector_p
19277           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19278         return selector;
19279
19280       maybe_unary_selector_p = false;
19281       cp_parser_require (parser, CPP_COLON, "%<:%>");
19282       type_name = cp_parser_objc_typename (parser);
19283       identifier = cp_parser_identifier (parser);
19284
19285       params
19286         = chainon (params,
19287                    objc_build_keyword_decl (selector,
19288                                             type_name,
19289                                             identifier));
19290
19291       token = cp_lexer_peek_token (parser->lexer);
19292     }
19293
19294   return params;
19295 }
19296
19297 /* Parse the non-keyword Objective-C params.  */
19298
19299 static tree
19300 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19301 {
19302   tree params = make_node (TREE_LIST);
19303   cp_token *token = cp_lexer_peek_token (parser->lexer);
19304   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19305
19306   while (token->type == CPP_COMMA)
19307     {
19308       cp_parameter_declarator *parmdecl;
19309       tree parm;
19310
19311       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19312       token = cp_lexer_peek_token (parser->lexer);
19313
19314       if (token->type == CPP_ELLIPSIS)
19315         {
19316           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19317           *ellipsisp = true;
19318           break;
19319         }
19320
19321       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19322       parm = grokdeclarator (parmdecl->declarator,
19323                              &parmdecl->decl_specifiers,
19324                              PARM, /*initialized=*/0,
19325                              /*attrlist=*/NULL);
19326
19327       chainon (params, build_tree_list (NULL_TREE, parm));
19328       token = cp_lexer_peek_token (parser->lexer);
19329     }
19330
19331   return params;
19332 }
19333
19334 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19335
19336 static void
19337 cp_parser_objc_interstitial_code (cp_parser* parser)
19338 {
19339   cp_token *token = cp_lexer_peek_token (parser->lexer);
19340
19341   /* If the next token is `extern' and the following token is a string
19342      literal, then we have a linkage specification.  */
19343   if (token->keyword == RID_EXTERN
19344       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19345     cp_parser_linkage_specification (parser);
19346   /* Handle #pragma, if any.  */
19347   else if (token->type == CPP_PRAGMA)
19348     cp_parser_pragma (parser, pragma_external);
19349   /* Allow stray semicolons.  */
19350   else if (token->type == CPP_SEMICOLON)
19351     cp_lexer_consume_token (parser->lexer);
19352   /* Finally, try to parse a block-declaration, or a function-definition.  */
19353   else
19354     cp_parser_block_declaration (parser, /*statement_p=*/false);
19355 }
19356
19357 /* Parse a method signature.  */
19358
19359 static tree
19360 cp_parser_objc_method_signature (cp_parser* parser)
19361 {
19362   tree rettype, kwdparms, optparms;
19363   bool ellipsis = false;
19364
19365   cp_parser_objc_method_type (parser);
19366   rettype = cp_parser_objc_typename (parser);
19367   kwdparms = cp_parser_objc_method_keyword_params (parser);
19368   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19369
19370   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19371 }
19372
19373 /* Pars an Objective-C method prototype list.  */
19374
19375 static void
19376 cp_parser_objc_method_prototype_list (cp_parser* parser)
19377 {
19378   cp_token *token = cp_lexer_peek_token (parser->lexer);
19379
19380   while (token->keyword != RID_AT_END)
19381     {
19382       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19383         {
19384           objc_add_method_declaration
19385            (cp_parser_objc_method_signature (parser));
19386           cp_parser_consume_semicolon_at_end_of_statement (parser);
19387         }
19388       else
19389         /* Allow for interspersed non-ObjC++ code.  */
19390         cp_parser_objc_interstitial_code (parser);
19391
19392       token = cp_lexer_peek_token (parser->lexer);
19393     }
19394
19395   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19396   objc_finish_interface ();
19397 }
19398
19399 /* Parse an Objective-C method definition list.  */
19400
19401 static void
19402 cp_parser_objc_method_definition_list (cp_parser* parser)
19403 {
19404   cp_token *token = cp_lexer_peek_token (parser->lexer);
19405
19406   while (token->keyword != RID_AT_END)
19407     {
19408       tree meth;
19409
19410       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19411         {
19412           push_deferring_access_checks (dk_deferred);
19413           objc_start_method_definition
19414            (cp_parser_objc_method_signature (parser));
19415
19416           /* For historical reasons, we accept an optional semicolon.  */
19417           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19418             cp_lexer_consume_token (parser->lexer);
19419
19420           perform_deferred_access_checks ();
19421           stop_deferring_access_checks ();
19422           meth = cp_parser_function_definition_after_declarator (parser,
19423                                                                  false);
19424           pop_deferring_access_checks ();
19425           objc_finish_method_definition (meth);
19426         }
19427       else
19428         /* Allow for interspersed non-ObjC++ code.  */
19429         cp_parser_objc_interstitial_code (parser);
19430
19431       token = cp_lexer_peek_token (parser->lexer);
19432     }
19433
19434   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19435   objc_finish_implementation ();
19436 }
19437
19438 /* Parse Objective-C ivars.  */
19439
19440 static void
19441 cp_parser_objc_class_ivars (cp_parser* parser)
19442 {
19443   cp_token *token = cp_lexer_peek_token (parser->lexer);
19444
19445   if (token->type != CPP_OPEN_BRACE)
19446     return;     /* No ivars specified.  */
19447
19448   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19449   token = cp_lexer_peek_token (parser->lexer);
19450
19451   while (token->type != CPP_CLOSE_BRACE)
19452     {
19453       cp_decl_specifier_seq declspecs;
19454       int decl_class_or_enum_p;
19455       tree prefix_attributes;
19456
19457       cp_parser_objc_visibility_spec (parser);
19458
19459       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19460         break;
19461
19462       cp_parser_decl_specifier_seq (parser,
19463                                     CP_PARSER_FLAGS_OPTIONAL,
19464                                     &declspecs,
19465                                     &decl_class_or_enum_p);
19466       prefix_attributes = declspecs.attributes;
19467       declspecs.attributes = NULL_TREE;
19468
19469       /* Keep going until we hit the `;' at the end of the
19470          declaration.  */
19471       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19472         {
19473           tree width = NULL_TREE, attributes, first_attribute, decl;
19474           cp_declarator *declarator = NULL;
19475           int ctor_dtor_or_conv_p;
19476
19477           /* Check for a (possibly unnamed) bitfield declaration.  */
19478           token = cp_lexer_peek_token (parser->lexer);
19479           if (token->type == CPP_COLON)
19480             goto eat_colon;
19481
19482           if (token->type == CPP_NAME
19483               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19484                   == CPP_COLON))
19485             {
19486               /* Get the name of the bitfield.  */
19487               declarator = make_id_declarator (NULL_TREE,
19488                                                cp_parser_identifier (parser),
19489                                                sfk_none);
19490
19491              eat_colon:
19492               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19493               /* Get the width of the bitfield.  */
19494               width
19495                 = cp_parser_constant_expression (parser,
19496                                                  /*allow_non_constant=*/false,
19497                                                  NULL);
19498             }
19499           else
19500             {
19501               /* Parse the declarator.  */
19502               declarator
19503                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19504                                         &ctor_dtor_or_conv_p,
19505                                         /*parenthesized_p=*/NULL,
19506                                         /*member_p=*/false);
19507             }
19508
19509           /* Look for attributes that apply to the ivar.  */
19510           attributes = cp_parser_attributes_opt (parser);
19511           /* Remember which attributes are prefix attributes and
19512              which are not.  */
19513           first_attribute = attributes;
19514           /* Combine the attributes.  */
19515           attributes = chainon (prefix_attributes, attributes);
19516
19517           if (width)
19518               /* Create the bitfield declaration.  */
19519               decl = grokbitfield (declarator, &declspecs,
19520                                    width,
19521                                    attributes);
19522           else
19523             decl = grokfield (declarator, &declspecs,
19524                               NULL_TREE, /*init_const_expr_p=*/false,
19525                               NULL_TREE, attributes);
19526
19527           /* Add the instance variable.  */
19528           objc_add_instance_variable (decl);
19529
19530           /* Reset PREFIX_ATTRIBUTES.  */
19531           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19532             attributes = TREE_CHAIN (attributes);
19533           if (attributes)
19534             TREE_CHAIN (attributes) = NULL_TREE;
19535
19536           token = cp_lexer_peek_token (parser->lexer);
19537
19538           if (token->type == CPP_COMMA)
19539             {
19540               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19541               continue;
19542             }
19543           break;
19544         }
19545
19546       cp_parser_consume_semicolon_at_end_of_statement (parser);
19547       token = cp_lexer_peek_token (parser->lexer);
19548     }
19549
19550   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19551   /* For historical reasons, we accept an optional semicolon.  */
19552   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19553     cp_lexer_consume_token (parser->lexer);
19554 }
19555
19556 /* Parse an Objective-C protocol declaration.  */
19557
19558 static void
19559 cp_parser_objc_protocol_declaration (cp_parser* parser)
19560 {
19561   tree proto, protorefs;
19562   cp_token *tok;
19563
19564   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19565   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19566     {
19567       tok = cp_lexer_peek_token (parser->lexer);
19568       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19569       goto finish;
19570     }
19571
19572   /* See if we have a forward declaration or a definition.  */
19573   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19574
19575   /* Try a forward declaration first.  */
19576   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19577     {
19578       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19579      finish:
19580       cp_parser_consume_semicolon_at_end_of_statement (parser);
19581     }
19582
19583   /* Ok, we got a full-fledged definition (or at least should).  */
19584   else
19585     {
19586       proto = cp_parser_identifier (parser);
19587       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19588       objc_start_protocol (proto, protorefs);
19589       cp_parser_objc_method_prototype_list (parser);
19590     }
19591 }
19592
19593 /* Parse an Objective-C superclass or category.  */
19594
19595 static void
19596 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19597                                                           tree *categ)
19598 {
19599   cp_token *next = cp_lexer_peek_token (parser->lexer);
19600
19601   *super = *categ = NULL_TREE;
19602   if (next->type == CPP_COLON)
19603     {
19604       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19605       *super = cp_parser_identifier (parser);
19606     }
19607   else if (next->type == CPP_OPEN_PAREN)
19608     {
19609       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19610       *categ = cp_parser_identifier (parser);
19611       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19612     }
19613 }
19614
19615 /* Parse an Objective-C class interface.  */
19616
19617 static void
19618 cp_parser_objc_class_interface (cp_parser* parser)
19619 {
19620   tree name, super, categ, protos;
19621
19622   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19623   name = cp_parser_identifier (parser);
19624   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19625   protos = cp_parser_objc_protocol_refs_opt (parser);
19626
19627   /* We have either a class or a category on our hands.  */
19628   if (categ)
19629     objc_start_category_interface (name, categ, protos);
19630   else
19631     {
19632       objc_start_class_interface (name, super, protos);
19633       /* Handle instance variable declarations, if any.  */
19634       cp_parser_objc_class_ivars (parser);
19635       objc_continue_interface ();
19636     }
19637
19638   cp_parser_objc_method_prototype_list (parser);
19639 }
19640
19641 /* Parse an Objective-C class implementation.  */
19642
19643 static void
19644 cp_parser_objc_class_implementation (cp_parser* parser)
19645 {
19646   tree name, super, categ;
19647
19648   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19649   name = cp_parser_identifier (parser);
19650   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19651
19652   /* We have either a class or a category on our hands.  */
19653   if (categ)
19654     objc_start_category_implementation (name, categ);
19655   else
19656     {
19657       objc_start_class_implementation (name, super);
19658       /* Handle instance variable declarations, if any.  */
19659       cp_parser_objc_class_ivars (parser);
19660       objc_continue_implementation ();
19661     }
19662
19663   cp_parser_objc_method_definition_list (parser);
19664 }
19665
19666 /* Consume the @end token and finish off the implementation.  */
19667
19668 static void
19669 cp_parser_objc_end_implementation (cp_parser* parser)
19670 {
19671   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19672   objc_finish_implementation ();
19673 }
19674
19675 /* Parse an Objective-C declaration.  */
19676
19677 static void
19678 cp_parser_objc_declaration (cp_parser* parser)
19679 {
19680   /* Try to figure out what kind of declaration is present.  */
19681   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19682
19683   switch (kwd->keyword)
19684     {
19685     case RID_AT_ALIAS:
19686       cp_parser_objc_alias_declaration (parser);
19687       break;
19688     case RID_AT_CLASS:
19689       cp_parser_objc_class_declaration (parser);
19690       break;
19691     case RID_AT_PROTOCOL:
19692       cp_parser_objc_protocol_declaration (parser);
19693       break;
19694     case RID_AT_INTERFACE:
19695       cp_parser_objc_class_interface (parser);
19696       break;
19697     case RID_AT_IMPLEMENTATION:
19698       cp_parser_objc_class_implementation (parser);
19699       break;
19700     case RID_AT_END:
19701       cp_parser_objc_end_implementation (parser);
19702       break;
19703     default:
19704       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19705              &kwd->location, kwd->u.value);
19706       cp_parser_skip_to_end_of_block_or_statement (parser);
19707     }
19708 }
19709
19710 /* Parse an Objective-C try-catch-finally statement.
19711
19712    objc-try-catch-finally-stmt:
19713      @try compound-statement objc-catch-clause-seq [opt]
19714        objc-finally-clause [opt]
19715
19716    objc-catch-clause-seq:
19717      objc-catch-clause objc-catch-clause-seq [opt]
19718
19719    objc-catch-clause:
19720      @catch ( exception-declaration ) compound-statement
19721
19722    objc-finally-clause
19723      @finally compound-statement
19724
19725    Returns NULL_TREE.  */
19726
19727 static tree
19728 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19729   location_t location;
19730   tree stmt;
19731
19732   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19733   location = cp_lexer_peek_token (parser->lexer)->location;
19734   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19735      node, lest it get absorbed into the surrounding block.  */
19736   stmt = push_stmt_list ();
19737   cp_parser_compound_statement (parser, NULL, false);
19738   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19739
19740   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19741     {
19742       cp_parameter_declarator *parmdecl;
19743       tree parm;
19744
19745       cp_lexer_consume_token (parser->lexer);
19746       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19747       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19748       parm = grokdeclarator (parmdecl->declarator,
19749                              &parmdecl->decl_specifiers,
19750                              PARM, /*initialized=*/0,
19751                              /*attrlist=*/NULL);
19752       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19753       objc_begin_catch_clause (parm);
19754       cp_parser_compound_statement (parser, NULL, false);
19755       objc_finish_catch_clause ();
19756     }
19757
19758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19759     {
19760       cp_lexer_consume_token (parser->lexer);
19761       location = cp_lexer_peek_token (parser->lexer)->location;
19762       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19763          node, lest it get absorbed into the surrounding block.  */
19764       stmt = push_stmt_list ();
19765       cp_parser_compound_statement (parser, NULL, false);
19766       objc_build_finally_clause (location, pop_stmt_list (stmt));
19767     }
19768
19769   return objc_finish_try_stmt ();
19770 }
19771
19772 /* Parse an Objective-C synchronized statement.
19773
19774    objc-synchronized-stmt:
19775      @synchronized ( expression ) compound-statement
19776
19777    Returns NULL_TREE.  */
19778
19779 static tree
19780 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19781   location_t location;
19782   tree lock, stmt;
19783
19784   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19785
19786   location = cp_lexer_peek_token (parser->lexer)->location;
19787   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19788   lock = cp_parser_expression (parser, false);
19789   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19790
19791   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19792      node, lest it get absorbed into the surrounding block.  */
19793   stmt = push_stmt_list ();
19794   cp_parser_compound_statement (parser, NULL, false);
19795
19796   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19797 }
19798
19799 /* Parse an Objective-C throw statement.
19800
19801    objc-throw-stmt:
19802      @throw assignment-expression [opt] ;
19803
19804    Returns a constructed '@throw' statement.  */
19805
19806 static tree
19807 cp_parser_objc_throw_statement (cp_parser *parser) {
19808   tree expr = NULL_TREE;
19809
19810   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19811
19812   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19813     expr = cp_parser_assignment_expression (parser, false);
19814
19815   cp_parser_consume_semicolon_at_end_of_statement (parser);
19816
19817   return objc_build_throw_stmt (expr);
19818 }
19819
19820 /* Parse an Objective-C statement.  */
19821
19822 static tree
19823 cp_parser_objc_statement (cp_parser * parser) {
19824   /* Try to figure out what kind of declaration is present.  */
19825   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19826
19827   switch (kwd->keyword)
19828     {
19829     case RID_AT_TRY:
19830       return cp_parser_objc_try_catch_finally_statement (parser);
19831     case RID_AT_SYNCHRONIZED:
19832       return cp_parser_objc_synchronized_statement (parser);
19833     case RID_AT_THROW:
19834       return cp_parser_objc_throw_statement (parser);
19835     default:
19836       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19837              &kwd->location, kwd->u.value);
19838       cp_parser_skip_to_end_of_block_or_statement (parser);
19839     }
19840
19841   return error_mark_node;
19842 }
19843 \f
19844 /* OpenMP 2.5 parsing routines.  */
19845
19846 /* Returns name of the next clause.
19847    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19848    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19849    returned and the token is consumed.  */
19850
19851 static pragma_omp_clause
19852 cp_parser_omp_clause_name (cp_parser *parser)
19853 {
19854   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19855
19856   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19857     result = PRAGMA_OMP_CLAUSE_IF;
19858   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19859     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19860   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19861     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19862   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19863     {
19864       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19865       const char *p = IDENTIFIER_POINTER (id);
19866
19867       switch (p[0])
19868         {
19869         case 'c':
19870           if (!strcmp ("collapse", p))
19871             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19872           else if (!strcmp ("copyin", p))
19873             result = PRAGMA_OMP_CLAUSE_COPYIN;
19874           else if (!strcmp ("copyprivate", p))
19875             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19876           break;
19877         case 'f':
19878           if (!strcmp ("firstprivate", p))
19879             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19880           break;
19881         case 'l':
19882           if (!strcmp ("lastprivate", p))
19883             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19884           break;
19885         case 'n':
19886           if (!strcmp ("nowait", p))
19887             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19888           else if (!strcmp ("num_threads", p))
19889             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19890           break;
19891         case 'o':
19892           if (!strcmp ("ordered", p))
19893             result = PRAGMA_OMP_CLAUSE_ORDERED;
19894           break;
19895         case 'r':
19896           if (!strcmp ("reduction", p))
19897             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19898           break;
19899         case 's':
19900           if (!strcmp ("schedule", p))
19901             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19902           else if (!strcmp ("shared", p))
19903             result = PRAGMA_OMP_CLAUSE_SHARED;
19904           break;
19905         case 'u':
19906           if (!strcmp ("untied", p))
19907             result = PRAGMA_OMP_CLAUSE_UNTIED;
19908           break;
19909         }
19910     }
19911
19912   if (result != PRAGMA_OMP_CLAUSE_NONE)
19913     cp_lexer_consume_token (parser->lexer);
19914
19915   return result;
19916 }
19917
19918 /* Validate that a clause of the given type does not already exist.  */
19919
19920 static void
19921 check_no_duplicate_clause (tree clauses, enum tree_code code,
19922                            const char *name, location_t location)
19923 {
19924   tree c;
19925
19926   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19927     if (OMP_CLAUSE_CODE (c) == code)
19928       {
19929         error ("%Htoo many %qs clauses", &location, name);
19930         break;
19931       }
19932 }
19933
19934 /* OpenMP 2.5:
19935    variable-list:
19936      identifier
19937      variable-list , identifier
19938
19939    In addition, we match a closing parenthesis.  An opening parenthesis
19940    will have been consumed by the caller.
19941
19942    If KIND is nonzero, create the appropriate node and install the decl
19943    in OMP_CLAUSE_DECL and add the node to the head of the list.
19944
19945    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19946    return the list created.  */
19947
19948 static tree
19949 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19950                                 tree list)
19951 {
19952   cp_token *token;
19953   while (1)
19954     {
19955       tree name, decl;
19956
19957       token = cp_lexer_peek_token (parser->lexer);
19958       name = cp_parser_id_expression (parser, /*template_p=*/false,
19959                                       /*check_dependency_p=*/true,
19960                                       /*template_p=*/NULL,
19961                                       /*declarator_p=*/false,
19962                                       /*optional_p=*/false);
19963       if (name == error_mark_node)
19964         goto skip_comma;
19965
19966       decl = cp_parser_lookup_name_simple (parser, name, token->location);
19967       if (decl == error_mark_node)
19968         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19969       else if (kind != 0)
19970         {
19971           tree u = build_omp_clause (kind);
19972           OMP_CLAUSE_DECL (u) = decl;
19973           OMP_CLAUSE_CHAIN (u) = list;
19974           list = u;
19975         }
19976       else
19977         list = tree_cons (decl, NULL_TREE, list);
19978
19979     get_comma:
19980       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19981         break;
19982       cp_lexer_consume_token (parser->lexer);
19983     }
19984
19985   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19986     {
19987       int ending;
19988
19989       /* Try to resync to an unnested comma.  Copied from
19990          cp_parser_parenthesized_expression_list.  */
19991     skip_comma:
19992       ending = cp_parser_skip_to_closing_parenthesis (parser,
19993                                                       /*recovering=*/true,
19994                                                       /*or_comma=*/true,
19995                                                       /*consume_paren=*/true);
19996       if (ending < 0)
19997         goto get_comma;
19998     }
19999
20000   return list;
20001 }
20002
20003 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20004    common case for omp clauses.  */
20005
20006 static tree
20007 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20008 {
20009   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20010     return cp_parser_omp_var_list_no_open (parser, kind, list);
20011   return list;
20012 }
20013
20014 /* OpenMP 3.0:
20015    collapse ( constant-expression ) */
20016
20017 static tree
20018 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20019 {
20020   tree c, num;
20021   location_t loc;
20022   HOST_WIDE_INT n;
20023
20024   loc = cp_lexer_peek_token (parser->lexer)->location;
20025   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20026     return list;
20027
20028   num = cp_parser_constant_expression (parser, false, NULL);
20029
20030   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20031     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20032                                            /*or_comma=*/false,
20033                                            /*consume_paren=*/true);
20034
20035   if (num == error_mark_node)
20036     return list;
20037   num = fold_non_dependent_expr (num);
20038   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20039       || !host_integerp (num, 0)
20040       || (n = tree_low_cst (num, 0)) <= 0
20041       || (int) n != n)
20042     {
20043       error ("%Hcollapse argument needs positive constant integer expression",
20044              &loc);
20045       return list;
20046     }
20047
20048   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20049   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20050   OMP_CLAUSE_CHAIN (c) = list;
20051   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20052
20053   return c;
20054 }
20055
20056 /* OpenMP 2.5:
20057    default ( shared | none ) */
20058
20059 static tree
20060 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20061 {
20062   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20063   tree c;
20064
20065   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20066     return list;
20067   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20068     {
20069       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20070       const char *p = IDENTIFIER_POINTER (id);
20071
20072       switch (p[0])
20073         {
20074         case 'n':
20075           if (strcmp ("none", p) != 0)
20076             goto invalid_kind;
20077           kind = OMP_CLAUSE_DEFAULT_NONE;
20078           break;
20079
20080         case 's':
20081           if (strcmp ("shared", p) != 0)
20082             goto invalid_kind;
20083           kind = OMP_CLAUSE_DEFAULT_SHARED;
20084           break;
20085
20086         default:
20087           goto invalid_kind;
20088         }
20089
20090       cp_lexer_consume_token (parser->lexer);
20091     }
20092   else
20093     {
20094     invalid_kind:
20095       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20096     }
20097
20098   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20099     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20100                                            /*or_comma=*/false,
20101                                            /*consume_paren=*/true);
20102
20103   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20104     return list;
20105
20106   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20107   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20108   OMP_CLAUSE_CHAIN (c) = list;
20109   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20110
20111   return c;
20112 }
20113
20114 /* OpenMP 2.5:
20115    if ( expression ) */
20116
20117 static tree
20118 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20119 {
20120   tree t, c;
20121
20122   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20123     return list;
20124
20125   t = cp_parser_condition (parser);
20126
20127   if (t == error_mark_node
20128       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20129     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20130                                            /*or_comma=*/false,
20131                                            /*consume_paren=*/true);
20132
20133   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20134
20135   c = build_omp_clause (OMP_CLAUSE_IF);
20136   OMP_CLAUSE_IF_EXPR (c) = t;
20137   OMP_CLAUSE_CHAIN (c) = list;
20138
20139   return c;
20140 }
20141
20142 /* OpenMP 2.5:
20143    nowait */
20144
20145 static tree
20146 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20147                              tree list, location_t location)
20148 {
20149   tree c;
20150
20151   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20152
20153   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20154   OMP_CLAUSE_CHAIN (c) = list;
20155   return c;
20156 }
20157
20158 /* OpenMP 2.5:
20159    num_threads ( expression ) */
20160
20161 static tree
20162 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20163                                   location_t location)
20164 {
20165   tree t, c;
20166
20167   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20168     return list;
20169
20170   t = cp_parser_expression (parser, false);
20171
20172   if (t == error_mark_node
20173       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20174     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20175                                            /*or_comma=*/false,
20176                                            /*consume_paren=*/true);
20177
20178   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20179                              "num_threads", location);
20180
20181   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20182   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20183   OMP_CLAUSE_CHAIN (c) = list;
20184
20185   return c;
20186 }
20187
20188 /* OpenMP 2.5:
20189    ordered */
20190
20191 static tree
20192 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20193                               tree list, location_t location)
20194 {
20195   tree c;
20196
20197   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20198                              "ordered", location);
20199
20200   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20201   OMP_CLAUSE_CHAIN (c) = list;
20202   return c;
20203 }
20204
20205 /* OpenMP 2.5:
20206    reduction ( reduction-operator : variable-list )
20207
20208    reduction-operator:
20209      One of: + * - & ^ | && || */
20210
20211 static tree
20212 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20213 {
20214   enum tree_code code;
20215   tree nlist, c;
20216
20217   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20218     return list;
20219
20220   switch (cp_lexer_peek_token (parser->lexer)->type)
20221     {
20222     case CPP_PLUS:
20223       code = PLUS_EXPR;
20224       break;
20225     case CPP_MULT:
20226       code = MULT_EXPR;
20227       break;
20228     case CPP_MINUS:
20229       code = MINUS_EXPR;
20230       break;
20231     case CPP_AND:
20232       code = BIT_AND_EXPR;
20233       break;
20234     case CPP_XOR:
20235       code = BIT_XOR_EXPR;
20236       break;
20237     case CPP_OR:
20238       code = BIT_IOR_EXPR;
20239       break;
20240     case CPP_AND_AND:
20241       code = TRUTH_ANDIF_EXPR;
20242       break;
20243     case CPP_OR_OR:
20244       code = TRUTH_ORIF_EXPR;
20245       break;
20246     default:
20247       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20248                                "%<|%>, %<&&%>, or %<||%>");
20249     resync_fail:
20250       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20251                                              /*or_comma=*/false,
20252                                              /*consume_paren=*/true);
20253       return list;
20254     }
20255   cp_lexer_consume_token (parser->lexer);
20256
20257   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20258     goto resync_fail;
20259
20260   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20261   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20262     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20263
20264   return nlist;
20265 }
20266
20267 /* OpenMP 2.5:
20268    schedule ( schedule-kind )
20269    schedule ( schedule-kind , expression )
20270
20271    schedule-kind:
20272      static | dynamic | guided | runtime | auto  */
20273
20274 static tree
20275 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20276 {
20277   tree c, t;
20278
20279   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20280     return list;
20281
20282   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20283
20284   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20285     {
20286       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20287       const char *p = IDENTIFIER_POINTER (id);
20288
20289       switch (p[0])
20290         {
20291         case 'd':
20292           if (strcmp ("dynamic", p) != 0)
20293             goto invalid_kind;
20294           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20295           break;
20296
20297         case 'g':
20298           if (strcmp ("guided", p) != 0)
20299             goto invalid_kind;
20300           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20301           break;
20302
20303         case 'r':
20304           if (strcmp ("runtime", p) != 0)
20305             goto invalid_kind;
20306           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20307           break;
20308
20309         default:
20310           goto invalid_kind;
20311         }
20312     }
20313   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20314     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20315   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20316     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20317   else
20318     goto invalid_kind;
20319   cp_lexer_consume_token (parser->lexer);
20320
20321   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20322     {
20323       cp_token *token;
20324       cp_lexer_consume_token (parser->lexer);
20325
20326       token = cp_lexer_peek_token (parser->lexer);
20327       t = cp_parser_assignment_expression (parser, false);
20328
20329       if (t == error_mark_node)
20330         goto resync_fail;
20331       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20332         error ("%Hschedule %<runtime%> does not take "
20333                "a %<chunk_size%> parameter", &token->location);
20334       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20335         error ("%Hschedule %<auto%> does not take "
20336                "a %<chunk_size%> parameter", &token->location);
20337       else
20338         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20339
20340       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20341         goto resync_fail;
20342     }
20343   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20344     goto resync_fail;
20345
20346   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20347   OMP_CLAUSE_CHAIN (c) = list;
20348   return c;
20349
20350  invalid_kind:
20351   cp_parser_error (parser, "invalid schedule kind");
20352  resync_fail:
20353   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20354                                          /*or_comma=*/false,
20355                                          /*consume_paren=*/true);
20356   return list;
20357 }
20358
20359 /* OpenMP 3.0:
20360    untied */
20361
20362 static tree
20363 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20364                              tree list, location_t location)
20365 {
20366   tree c;
20367
20368   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20369
20370   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20371   OMP_CLAUSE_CHAIN (c) = list;
20372   return c;
20373 }
20374
20375 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20376    is a bitmask in MASK.  Return the list of clauses found; the result
20377    of clause default goes in *pdefault.  */
20378
20379 static tree
20380 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20381                            const char *where, cp_token *pragma_tok)
20382 {
20383   tree clauses = NULL;
20384   bool first = true;
20385   cp_token *token = NULL;
20386
20387   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20388     {
20389       pragma_omp_clause c_kind;
20390       const char *c_name;
20391       tree prev = clauses;
20392
20393       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20394         cp_lexer_consume_token (parser->lexer);
20395
20396       token = cp_lexer_peek_token (parser->lexer);
20397       c_kind = cp_parser_omp_clause_name (parser);
20398       first = false;
20399
20400       switch (c_kind)
20401         {
20402         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20403           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20404                                                    token->location);
20405           c_name = "collapse";
20406           break;
20407         case PRAGMA_OMP_CLAUSE_COPYIN:
20408           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20409           c_name = "copyin";
20410           break;
20411         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20412           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20413                                             clauses);
20414           c_name = "copyprivate";
20415           break;
20416         case PRAGMA_OMP_CLAUSE_DEFAULT:
20417           clauses = cp_parser_omp_clause_default (parser, clauses,
20418                                                   token->location);
20419           c_name = "default";
20420           break;
20421         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20422           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20423                                             clauses);
20424           c_name = "firstprivate";
20425           break;
20426         case PRAGMA_OMP_CLAUSE_IF:
20427           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20428           c_name = "if";
20429           break;
20430         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20431           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20432                                             clauses);
20433           c_name = "lastprivate";
20434           break;
20435         case PRAGMA_OMP_CLAUSE_NOWAIT:
20436           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20437           c_name = "nowait";
20438           break;
20439         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20440           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20441                                                       token->location);
20442           c_name = "num_threads";
20443           break;
20444         case PRAGMA_OMP_CLAUSE_ORDERED:
20445           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20446                                                   token->location);
20447           c_name = "ordered";
20448           break;
20449         case PRAGMA_OMP_CLAUSE_PRIVATE:
20450           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20451                                             clauses);
20452           c_name = "private";
20453           break;
20454         case PRAGMA_OMP_CLAUSE_REDUCTION:
20455           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20456           c_name = "reduction";
20457           break;
20458         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20459           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20460                                                    token->location);
20461           c_name = "schedule";
20462           break;
20463         case PRAGMA_OMP_CLAUSE_SHARED:
20464           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20465                                             clauses);
20466           c_name = "shared";
20467           break;
20468         case PRAGMA_OMP_CLAUSE_UNTIED:
20469           clauses = cp_parser_omp_clause_untied (parser, clauses,
20470                                                  token->location);
20471           c_name = "nowait";
20472           break;
20473         default:
20474           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20475           goto saw_error;
20476         }
20477
20478       if (((mask >> c_kind) & 1) == 0)
20479         {
20480           /* Remove the invalid clause(s) from the list to avoid
20481              confusing the rest of the compiler.  */
20482           clauses = prev;
20483           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20484         }
20485     }
20486  saw_error:
20487   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20488   return finish_omp_clauses (clauses);
20489 }
20490
20491 /* OpenMP 2.5:
20492    structured-block:
20493      statement
20494
20495    In practice, we're also interested in adding the statement to an
20496    outer node.  So it is convenient if we work around the fact that
20497    cp_parser_statement calls add_stmt.  */
20498
20499 static unsigned
20500 cp_parser_begin_omp_structured_block (cp_parser *parser)
20501 {
20502   unsigned save = parser->in_statement;
20503
20504   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20505      This preserves the "not within loop or switch" style error messages
20506      for nonsense cases like
20507         void foo() {
20508         #pragma omp single
20509           break;
20510         }
20511   */
20512   if (parser->in_statement)
20513     parser->in_statement = IN_OMP_BLOCK;
20514
20515   return save;
20516 }
20517
20518 static void
20519 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20520 {
20521   parser->in_statement = save;
20522 }
20523
20524 static tree
20525 cp_parser_omp_structured_block (cp_parser *parser)
20526 {
20527   tree stmt = begin_omp_structured_block ();
20528   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20529
20530   cp_parser_statement (parser, NULL_TREE, false, NULL);
20531
20532   cp_parser_end_omp_structured_block (parser, save);
20533   return finish_omp_structured_block (stmt);
20534 }
20535
20536 /* OpenMP 2.5:
20537    # pragma omp atomic new-line
20538      expression-stmt
20539
20540    expression-stmt:
20541      x binop= expr | x++ | ++x | x-- | --x
20542    binop:
20543      +, *, -, /, &, ^, |, <<, >>
20544
20545   where x is an lvalue expression with scalar type.  */
20546
20547 static void
20548 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20549 {
20550   tree lhs, rhs;
20551   enum tree_code code;
20552
20553   cp_parser_require_pragma_eol (parser, pragma_tok);
20554
20555   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20556                                     /*cast_p=*/false);
20557   switch (TREE_CODE (lhs))
20558     {
20559     case ERROR_MARK:
20560       goto saw_error;
20561
20562     case PREINCREMENT_EXPR:
20563     case POSTINCREMENT_EXPR:
20564       lhs = TREE_OPERAND (lhs, 0);
20565       code = PLUS_EXPR;
20566       rhs = integer_one_node;
20567       break;
20568
20569     case PREDECREMENT_EXPR:
20570     case POSTDECREMENT_EXPR:
20571       lhs = TREE_OPERAND (lhs, 0);
20572       code = MINUS_EXPR;
20573       rhs = integer_one_node;
20574       break;
20575
20576     default:
20577       switch (cp_lexer_peek_token (parser->lexer)->type)
20578         {
20579         case CPP_MULT_EQ:
20580           code = MULT_EXPR;
20581           break;
20582         case CPP_DIV_EQ:
20583           code = TRUNC_DIV_EXPR;
20584           break;
20585         case CPP_PLUS_EQ:
20586           code = PLUS_EXPR;
20587           break;
20588         case CPP_MINUS_EQ:
20589           code = MINUS_EXPR;
20590           break;
20591         case CPP_LSHIFT_EQ:
20592           code = LSHIFT_EXPR;
20593           break;
20594         case CPP_RSHIFT_EQ:
20595           code = RSHIFT_EXPR;
20596           break;
20597         case CPP_AND_EQ:
20598           code = BIT_AND_EXPR;
20599           break;
20600         case CPP_OR_EQ:
20601           code = BIT_IOR_EXPR;
20602           break;
20603         case CPP_XOR_EQ:
20604           code = BIT_XOR_EXPR;
20605           break;
20606         default:
20607           cp_parser_error (parser,
20608                            "invalid operator for %<#pragma omp atomic%>");
20609           goto saw_error;
20610         }
20611       cp_lexer_consume_token (parser->lexer);
20612
20613       rhs = cp_parser_expression (parser, false);
20614       if (rhs == error_mark_node)
20615         goto saw_error;
20616       break;
20617     }
20618   finish_omp_atomic (code, lhs, rhs);
20619   cp_parser_consume_semicolon_at_end_of_statement (parser);
20620   return;
20621
20622  saw_error:
20623   cp_parser_skip_to_end_of_block_or_statement (parser);
20624 }
20625
20626
20627 /* OpenMP 2.5:
20628    # pragma omp barrier new-line  */
20629
20630 static void
20631 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20632 {
20633   cp_parser_require_pragma_eol (parser, pragma_tok);
20634   finish_omp_barrier ();
20635 }
20636
20637 /* OpenMP 2.5:
20638    # pragma omp critical [(name)] new-line
20639      structured-block  */
20640
20641 static tree
20642 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20643 {
20644   tree stmt, name = NULL;
20645
20646   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20647     {
20648       cp_lexer_consume_token (parser->lexer);
20649
20650       name = cp_parser_identifier (parser);
20651
20652       if (name == error_mark_node
20653           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20654         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20655                                                /*or_comma=*/false,
20656                                                /*consume_paren=*/true);
20657       if (name == error_mark_node)
20658         name = NULL;
20659     }
20660   cp_parser_require_pragma_eol (parser, pragma_tok);
20661
20662   stmt = cp_parser_omp_structured_block (parser);
20663   return c_finish_omp_critical (stmt, name);
20664 }
20665
20666 /* OpenMP 2.5:
20667    # pragma omp flush flush-vars[opt] new-line
20668
20669    flush-vars:
20670      ( variable-list ) */
20671
20672 static void
20673 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20674 {
20675   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20676     (void) cp_parser_omp_var_list (parser, 0, NULL);
20677   cp_parser_require_pragma_eol (parser, pragma_tok);
20678
20679   finish_omp_flush ();
20680 }
20681
20682 /* Helper function, to parse omp for increment expression.  */
20683
20684 static tree
20685 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20686 {
20687   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20688   enum tree_code op;
20689   cp_token *token;
20690
20691   if (lhs != decl)
20692     {
20693       cp_parser_skip_to_end_of_statement (parser);
20694       return error_mark_node;
20695     }
20696
20697   token = cp_lexer_peek_token (parser->lexer);
20698   op = binops_by_token [token->type].tree_type;
20699   switch (op)
20700     {
20701     case LT_EXPR:
20702     case LE_EXPR:
20703     case GT_EXPR:
20704     case GE_EXPR:
20705       break;
20706     default:
20707       cp_parser_skip_to_end_of_statement (parser);
20708       return error_mark_node;
20709     }
20710
20711   cp_lexer_consume_token (parser->lexer);
20712   rhs = cp_parser_binary_expression (parser, false,
20713                                      PREC_RELATIONAL_EXPRESSION);
20714   if (rhs == error_mark_node
20715       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20716     {
20717       cp_parser_skip_to_end_of_statement (parser);
20718       return error_mark_node;
20719     }
20720
20721   return build2 (op, boolean_type_node, lhs, rhs);
20722 }
20723
20724 /* Helper function, to parse omp for increment expression.  */
20725
20726 static tree
20727 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20728 {
20729   cp_token *token = cp_lexer_peek_token (parser->lexer);
20730   enum tree_code op;
20731   tree lhs, rhs;
20732   cp_id_kind idk;
20733   bool decl_first;
20734
20735   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20736     {
20737       op = (token->type == CPP_PLUS_PLUS
20738             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20739       cp_lexer_consume_token (parser->lexer);
20740       lhs = cp_parser_cast_expression (parser, false, false);
20741       if (lhs != decl)
20742         return error_mark_node;
20743       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20744     }
20745
20746   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20747   if (lhs != decl)
20748     return error_mark_node;
20749
20750   token = cp_lexer_peek_token (parser->lexer);
20751   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20752     {
20753       op = (token->type == CPP_PLUS_PLUS
20754             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20755       cp_lexer_consume_token (parser->lexer);
20756       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20757     }
20758
20759   op = cp_parser_assignment_operator_opt (parser);
20760   if (op == ERROR_MARK)
20761     return error_mark_node;
20762
20763   if (op != NOP_EXPR)
20764     {
20765       rhs = cp_parser_assignment_expression (parser, false);
20766       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20767       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20768     }
20769
20770   lhs = cp_parser_binary_expression (parser, false,
20771                                      PREC_ADDITIVE_EXPRESSION);
20772   token = cp_lexer_peek_token (parser->lexer);
20773   decl_first = lhs == decl;
20774   if (decl_first)
20775     lhs = NULL_TREE;
20776   if (token->type != CPP_PLUS
20777       && token->type != CPP_MINUS)
20778     return error_mark_node;
20779
20780   do
20781     {
20782       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20783       cp_lexer_consume_token (parser->lexer);
20784       rhs = cp_parser_binary_expression (parser, false,
20785                                          PREC_ADDITIVE_EXPRESSION);
20786       token = cp_lexer_peek_token (parser->lexer);
20787       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20788         {
20789           if (lhs == NULL_TREE)
20790             {
20791               if (op == PLUS_EXPR)
20792                 lhs = rhs;
20793               else
20794                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20795             }
20796           else
20797             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20798                                      NULL, tf_warning_or_error);
20799         }
20800     }
20801   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20802
20803   if (!decl_first)
20804     {
20805       if (rhs != decl || op == MINUS_EXPR)
20806         return error_mark_node;
20807       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20808     }
20809   else
20810     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20811
20812   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20813 }
20814
20815 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20816
20817 static tree
20818 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20819 {
20820   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20821   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20822   tree this_pre_body, cl;
20823   location_t loc_first;
20824   bool collapse_err = false;
20825   int i, collapse = 1, nbraces = 0;
20826
20827   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20828     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20829       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20830
20831   gcc_assert (collapse >= 1);
20832
20833   declv = make_tree_vec (collapse);
20834   initv = make_tree_vec (collapse);
20835   condv = make_tree_vec (collapse);
20836   incrv = make_tree_vec (collapse);
20837
20838   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20839
20840   for (i = 0; i < collapse; i++)
20841     {
20842       int bracecount = 0;
20843       bool add_private_clause = false;
20844       location_t loc;
20845
20846       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20847         {
20848           cp_parser_error (parser, "for statement expected");
20849           return NULL;
20850         }
20851       loc = cp_lexer_consume_token (parser->lexer)->location;
20852
20853       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20854         return NULL;
20855
20856       init = decl = real_decl = NULL;
20857       this_pre_body = push_stmt_list ();
20858       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20859         {
20860           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20861
20862              init-expr:
20863                        var = lb
20864                        integer-type var = lb
20865                        random-access-iterator-type var = lb
20866                        pointer-type var = lb
20867           */
20868           cp_decl_specifier_seq type_specifiers;
20869
20870           /* First, try to parse as an initialized declaration.  See
20871              cp_parser_condition, from whence the bulk of this is copied.  */
20872
20873           cp_parser_parse_tentatively (parser);
20874           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20875                                         &type_specifiers);
20876           if (cp_parser_parse_definitely (parser))
20877             {
20878               /* If parsing a type specifier seq succeeded, then this
20879                  MUST be a initialized declaration.  */
20880               tree asm_specification, attributes;
20881               cp_declarator *declarator;
20882
20883               declarator = cp_parser_declarator (parser,
20884                                                  CP_PARSER_DECLARATOR_NAMED,
20885                                                  /*ctor_dtor_or_conv_p=*/NULL,
20886                                                  /*parenthesized_p=*/NULL,
20887                                                  /*member_p=*/false);
20888               attributes = cp_parser_attributes_opt (parser);
20889               asm_specification = cp_parser_asm_specification_opt (parser);
20890
20891               if (declarator == cp_error_declarator) 
20892                 cp_parser_skip_to_end_of_statement (parser);
20893
20894               else 
20895                 {
20896                   tree pushed_scope;
20897
20898                   decl = start_decl (declarator, &type_specifiers,
20899                                      /*initialized_p=*/false, attributes,
20900                                      /*prefix_attributes=*/NULL_TREE,
20901                                      &pushed_scope);
20902
20903                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20904                     {
20905                       if (cp_lexer_next_token_is (parser->lexer, 
20906                                                   CPP_OPEN_PAREN))
20907                         error ("parenthesized initialization is not allowed in "
20908                                "OpenMP %<for%> loop");
20909                       else
20910                         /* Trigger an error.  */
20911                         cp_parser_require (parser, CPP_EQ, "%<=%>");
20912
20913                       init = error_mark_node;
20914                       cp_parser_skip_to_end_of_statement (parser);
20915                     }
20916                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
20917                            || type_dependent_expression_p (decl))
20918                     {
20919                       bool is_direct_init, is_non_constant_init;
20920
20921                       init = cp_parser_initializer (parser,
20922                                                     &is_direct_init,
20923                                                     &is_non_constant_init);
20924
20925                       cp_finish_decl (decl, init, !is_non_constant_init,
20926                                       asm_specification,
20927                                       LOOKUP_ONLYCONVERTING);
20928                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
20929                         {
20930                           for_block
20931                             = tree_cons (NULL, this_pre_body, for_block);
20932                           init = NULL_TREE;
20933                         }
20934                       else
20935                         init = pop_stmt_list (this_pre_body);
20936                       this_pre_body = NULL_TREE;
20937                     }
20938                   else
20939                     {
20940                       /* Consume '='.  */
20941                       cp_lexer_consume_token (parser->lexer);
20942                       init = cp_parser_assignment_expression (parser, false);
20943
20944                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20945                         init = error_mark_node;
20946                       else
20947                         cp_finish_decl (decl, NULL_TREE,
20948                                         /*init_const_expr_p=*/false,
20949                                         asm_specification,
20950                                         LOOKUP_ONLYCONVERTING);
20951                     }
20952
20953                   if (pushed_scope)
20954                     pop_scope (pushed_scope);
20955                 }
20956             }
20957           else 
20958             {
20959               cp_id_kind idk;
20960               /* If parsing a type specifier sequence failed, then
20961                  this MUST be a simple expression.  */
20962               cp_parser_parse_tentatively (parser);
20963               decl = cp_parser_primary_expression (parser, false, false,
20964                                                    false, &idk);
20965               if (!cp_parser_error_occurred (parser)
20966                   && decl
20967                   && DECL_P (decl)
20968                   && CLASS_TYPE_P (TREE_TYPE (decl)))
20969                 {
20970                   tree rhs;
20971
20972                   cp_parser_parse_definitely (parser);
20973                   cp_parser_require (parser, CPP_EQ, "%<=%>");
20974                   rhs = cp_parser_assignment_expression (parser, false);
20975                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20976                                                          rhs,
20977                                                          tf_warning_or_error));
20978                   add_private_clause = true;
20979                 }
20980               else
20981                 {
20982                   decl = NULL;
20983                   cp_parser_abort_tentative_parse (parser);
20984                   init = cp_parser_expression (parser, false);
20985                   if (init)
20986                     {
20987                       if (TREE_CODE (init) == MODIFY_EXPR
20988                           || TREE_CODE (init) == MODOP_EXPR)
20989                         real_decl = TREE_OPERAND (init, 0);
20990                     }
20991                 }
20992             }
20993         }
20994       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20995       if (this_pre_body)
20996         {
20997           this_pre_body = pop_stmt_list (this_pre_body);
20998           if (pre_body)
20999             {
21000               tree t = pre_body;
21001               pre_body = push_stmt_list ();
21002               add_stmt (t);
21003               add_stmt (this_pre_body);
21004               pre_body = pop_stmt_list (pre_body);
21005             }
21006           else
21007             pre_body = this_pre_body;
21008         }
21009
21010       if (decl)
21011         real_decl = decl;
21012       if (par_clauses != NULL && real_decl != NULL_TREE)
21013         {
21014           tree *c;
21015           for (c = par_clauses; *c ; )
21016             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21017                 && OMP_CLAUSE_DECL (*c) == real_decl)
21018               {
21019                 error ("%Hiteration variable %qD should not be firstprivate",
21020                        &loc, real_decl);
21021                 *c = OMP_CLAUSE_CHAIN (*c);
21022               }
21023             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21024                      && OMP_CLAUSE_DECL (*c) == real_decl)
21025               {
21026                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21027                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21028                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21029                 OMP_CLAUSE_DECL (l) = real_decl;
21030                 OMP_CLAUSE_CHAIN (l) = clauses;
21031                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21032                 clauses = l;
21033                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21034                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21035                 add_private_clause = false;
21036               }
21037             else
21038               {
21039                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21040                     && OMP_CLAUSE_DECL (*c) == real_decl)
21041                   add_private_clause = false;
21042                 c = &OMP_CLAUSE_CHAIN (*c);
21043               }
21044         }
21045
21046       if (add_private_clause)
21047         {
21048           tree c;
21049           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21050             {
21051               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21052                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21053                   && OMP_CLAUSE_DECL (c) == decl)
21054                 break;
21055               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21056                        && OMP_CLAUSE_DECL (c) == decl)
21057                 error ("%Hiteration variable %qD should not be firstprivate",
21058                        &loc, decl);
21059               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21060                        && OMP_CLAUSE_DECL (c) == decl)
21061                 error ("%Hiteration variable %qD should not be reduction",
21062                        &loc, decl);
21063             }
21064           if (c == NULL)
21065             {
21066               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21067               OMP_CLAUSE_DECL (c) = decl;
21068               c = finish_omp_clauses (c);
21069               if (c)
21070                 {
21071                   OMP_CLAUSE_CHAIN (c) = clauses;
21072                   clauses = c;
21073                 }
21074             }
21075         }
21076
21077       cond = NULL;
21078       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21079         {
21080           /* If decl is an iterator, preserve LHS and RHS of the relational
21081              expr until finish_omp_for.  */
21082           if (decl
21083               && (type_dependent_expression_p (decl)
21084                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21085             cond = cp_parser_omp_for_cond (parser, decl);
21086           else
21087             cond = cp_parser_condition (parser);
21088         }
21089       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21090
21091       incr = NULL;
21092       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21093         {
21094           /* If decl is an iterator, preserve the operator on decl
21095              until finish_omp_for.  */
21096           if (decl
21097               && (type_dependent_expression_p (decl)
21098                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21099             incr = cp_parser_omp_for_incr (parser, decl);
21100           else
21101             incr = cp_parser_expression (parser, false);
21102         }
21103
21104       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21105         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21106                                                /*or_comma=*/false,
21107                                                /*consume_paren=*/true);
21108
21109       TREE_VEC_ELT (declv, i) = decl;
21110       TREE_VEC_ELT (initv, i) = init;
21111       TREE_VEC_ELT (condv, i) = cond;
21112       TREE_VEC_ELT (incrv, i) = incr;
21113
21114       if (i == collapse - 1)
21115         break;
21116
21117       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21118          in between the collapsed for loops to be still considered perfectly
21119          nested.  Hopefully the final version clarifies this.
21120          For now handle (multiple) {'s and empty statements.  */
21121       cp_parser_parse_tentatively (parser);
21122       do
21123         {
21124           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21125             break;
21126           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21127             {
21128               cp_lexer_consume_token (parser->lexer);
21129               bracecount++;
21130             }
21131           else if (bracecount
21132                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21133             cp_lexer_consume_token (parser->lexer);
21134           else
21135             {
21136               loc = cp_lexer_peek_token (parser->lexer)->location;
21137               error ("%Hnot enough collapsed for loops", &loc);
21138               collapse_err = true;
21139               cp_parser_abort_tentative_parse (parser);
21140               declv = NULL_TREE;
21141               break;
21142             }
21143         }
21144       while (1);
21145
21146       if (declv)
21147         {
21148           cp_parser_parse_definitely (parser);
21149           nbraces += bracecount;
21150         }
21151     }
21152
21153   /* Note that we saved the original contents of this flag when we entered
21154      the structured block, and so we don't need to re-save it here.  */
21155   parser->in_statement = IN_OMP_FOR;
21156
21157   /* Note that the grammar doesn't call for a structured block here,
21158      though the loop as a whole is a structured block.  */
21159   body = push_stmt_list ();
21160   cp_parser_statement (parser, NULL_TREE, false, NULL);
21161   body = pop_stmt_list (body);
21162
21163   if (declv == NULL_TREE)
21164     ret = NULL_TREE;
21165   else
21166     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21167                           pre_body, clauses);
21168
21169   while (nbraces)
21170     {
21171       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21172         {
21173           cp_lexer_consume_token (parser->lexer);
21174           nbraces--;
21175         }
21176       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21177         cp_lexer_consume_token (parser->lexer);
21178       else
21179         {
21180           if (!collapse_err)
21181             {
21182               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21183               error ("%Hcollapsed loops not perfectly nested", &loc);
21184             }
21185           collapse_err = true;
21186           cp_parser_statement_seq_opt (parser, NULL);
21187           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21188         }
21189     }
21190
21191   while (for_block)
21192     {
21193       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21194       for_block = TREE_CHAIN (for_block);
21195     }
21196
21197   return ret;
21198 }
21199
21200 /* OpenMP 2.5:
21201    #pragma omp for for-clause[optseq] new-line
21202      for-loop  */
21203
21204 #define OMP_FOR_CLAUSE_MASK                             \
21205         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21206         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21207         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21208         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21209         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21210         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21211         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21212         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21213
21214 static tree
21215 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21216 {
21217   tree clauses, sb, ret;
21218   unsigned int save;
21219
21220   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21221                                        "#pragma omp for", pragma_tok);
21222
21223   sb = begin_omp_structured_block ();
21224   save = cp_parser_begin_omp_structured_block (parser);
21225
21226   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21227
21228   cp_parser_end_omp_structured_block (parser, save);
21229   add_stmt (finish_omp_structured_block (sb));
21230
21231   return ret;
21232 }
21233
21234 /* OpenMP 2.5:
21235    # pragma omp master new-line
21236      structured-block  */
21237
21238 static tree
21239 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21240 {
21241   cp_parser_require_pragma_eol (parser, pragma_tok);
21242   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21243 }
21244
21245 /* OpenMP 2.5:
21246    # pragma omp ordered new-line
21247      structured-block  */
21248
21249 static tree
21250 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21251 {
21252   cp_parser_require_pragma_eol (parser, pragma_tok);
21253   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21254 }
21255
21256 /* OpenMP 2.5:
21257
21258    section-scope:
21259      { section-sequence }
21260
21261    section-sequence:
21262      section-directive[opt] structured-block
21263      section-sequence section-directive structured-block  */
21264
21265 static tree
21266 cp_parser_omp_sections_scope (cp_parser *parser)
21267 {
21268   tree stmt, substmt;
21269   bool error_suppress = false;
21270   cp_token *tok;
21271
21272   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21273     return NULL_TREE;
21274
21275   stmt = push_stmt_list ();
21276
21277   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21278     {
21279       unsigned save;
21280
21281       substmt = begin_omp_structured_block ();
21282       save = cp_parser_begin_omp_structured_block (parser);
21283
21284       while (1)
21285         {
21286           cp_parser_statement (parser, NULL_TREE, false, NULL);
21287
21288           tok = cp_lexer_peek_token (parser->lexer);
21289           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21290             break;
21291           if (tok->type == CPP_CLOSE_BRACE)
21292             break;
21293           if (tok->type == CPP_EOF)
21294             break;
21295         }
21296
21297       cp_parser_end_omp_structured_block (parser, save);
21298       substmt = finish_omp_structured_block (substmt);
21299       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21300       add_stmt (substmt);
21301     }
21302
21303   while (1)
21304     {
21305       tok = cp_lexer_peek_token (parser->lexer);
21306       if (tok->type == CPP_CLOSE_BRACE)
21307         break;
21308       if (tok->type == CPP_EOF)
21309         break;
21310
21311       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21312         {
21313           cp_lexer_consume_token (parser->lexer);
21314           cp_parser_require_pragma_eol (parser, tok);
21315           error_suppress = false;
21316         }
21317       else if (!error_suppress)
21318         {
21319           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21320           error_suppress = true;
21321         }
21322
21323       substmt = cp_parser_omp_structured_block (parser);
21324       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21325       add_stmt (substmt);
21326     }
21327   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21328
21329   substmt = pop_stmt_list (stmt);
21330
21331   stmt = make_node (OMP_SECTIONS);
21332   TREE_TYPE (stmt) = void_type_node;
21333   OMP_SECTIONS_BODY (stmt) = substmt;
21334
21335   add_stmt (stmt);
21336   return stmt;
21337 }
21338
21339 /* OpenMP 2.5:
21340    # pragma omp sections sections-clause[optseq] newline
21341      sections-scope  */
21342
21343 #define OMP_SECTIONS_CLAUSE_MASK                        \
21344         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21345         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21346         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21347         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21348         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21349
21350 static tree
21351 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21352 {
21353   tree clauses, ret;
21354
21355   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21356                                        "#pragma omp sections", pragma_tok);
21357
21358   ret = cp_parser_omp_sections_scope (parser);
21359   if (ret)
21360     OMP_SECTIONS_CLAUSES (ret) = clauses;
21361
21362   return ret;
21363 }
21364
21365 /* OpenMP 2.5:
21366    # pragma parallel parallel-clause new-line
21367    # pragma parallel for parallel-for-clause new-line
21368    # pragma parallel sections parallel-sections-clause new-line  */
21369
21370 #define OMP_PARALLEL_CLAUSE_MASK                        \
21371         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21372         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21373         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21374         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21375         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21376         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21377         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21378         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21379
21380 static tree
21381 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21382 {
21383   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21384   const char *p_name = "#pragma omp parallel";
21385   tree stmt, clauses, par_clause, ws_clause, block;
21386   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21387   unsigned int save;
21388
21389   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21390     {
21391       cp_lexer_consume_token (parser->lexer);
21392       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21393       p_name = "#pragma omp parallel for";
21394       mask |= OMP_FOR_CLAUSE_MASK;
21395       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21396     }
21397   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21398     {
21399       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21400       const char *p = IDENTIFIER_POINTER (id);
21401       if (strcmp (p, "sections") == 0)
21402         {
21403           cp_lexer_consume_token (parser->lexer);
21404           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21405           p_name = "#pragma omp parallel sections";
21406           mask |= OMP_SECTIONS_CLAUSE_MASK;
21407           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21408         }
21409     }
21410
21411   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21412   block = begin_omp_parallel ();
21413   save = cp_parser_begin_omp_structured_block (parser);
21414
21415   switch (p_kind)
21416     {
21417     case PRAGMA_OMP_PARALLEL:
21418       cp_parser_statement (parser, NULL_TREE, false, NULL);
21419       par_clause = clauses;
21420       break;
21421
21422     case PRAGMA_OMP_PARALLEL_FOR:
21423       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21424       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21425       break;
21426
21427     case PRAGMA_OMP_PARALLEL_SECTIONS:
21428       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21429       stmt = cp_parser_omp_sections_scope (parser);
21430       if (stmt)
21431         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21432       break;
21433
21434     default:
21435       gcc_unreachable ();
21436     }
21437
21438   cp_parser_end_omp_structured_block (parser, save);
21439   stmt = finish_omp_parallel (par_clause, block);
21440   if (p_kind != PRAGMA_OMP_PARALLEL)
21441     OMP_PARALLEL_COMBINED (stmt) = 1;
21442   return stmt;
21443 }
21444
21445 /* OpenMP 2.5:
21446    # pragma omp single single-clause[optseq] new-line
21447      structured-block  */
21448
21449 #define OMP_SINGLE_CLAUSE_MASK                          \
21450         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21451         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21452         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21453         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21454
21455 static tree
21456 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21457 {
21458   tree stmt = make_node (OMP_SINGLE);
21459   TREE_TYPE (stmt) = void_type_node;
21460
21461   OMP_SINGLE_CLAUSES (stmt)
21462     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21463                                  "#pragma omp single", pragma_tok);
21464   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21465
21466   return add_stmt (stmt);
21467 }
21468
21469 /* OpenMP 3.0:
21470    # pragma omp task task-clause[optseq] new-line
21471      structured-block  */
21472
21473 #define OMP_TASK_CLAUSE_MASK                            \
21474         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21475         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21476         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21477         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21478         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21479         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21480
21481 static tree
21482 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21483 {
21484   tree clauses, block;
21485   unsigned int save;
21486
21487   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21488                                        "#pragma omp task", pragma_tok);
21489   block = begin_omp_task ();
21490   save = cp_parser_begin_omp_structured_block (parser);
21491   cp_parser_statement (parser, NULL_TREE, false, NULL);
21492   cp_parser_end_omp_structured_block (parser, save);
21493   return finish_omp_task (clauses, block);
21494 }
21495
21496 /* OpenMP 3.0:
21497    # pragma omp taskwait new-line  */
21498
21499 static void
21500 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21501 {
21502   cp_parser_require_pragma_eol (parser, pragma_tok);
21503   finish_omp_taskwait ();
21504 }
21505
21506 /* OpenMP 2.5:
21507    # pragma omp threadprivate (variable-list) */
21508
21509 static void
21510 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21511 {
21512   tree vars;
21513
21514   vars = cp_parser_omp_var_list (parser, 0, NULL);
21515   cp_parser_require_pragma_eol (parser, pragma_tok);
21516
21517   finish_omp_threadprivate (vars);
21518 }
21519
21520 /* Main entry point to OpenMP statement pragmas.  */
21521
21522 static void
21523 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21524 {
21525   tree stmt;
21526
21527   switch (pragma_tok->pragma_kind)
21528     {
21529     case PRAGMA_OMP_ATOMIC:
21530       cp_parser_omp_atomic (parser, pragma_tok);
21531       return;
21532     case PRAGMA_OMP_CRITICAL:
21533       stmt = cp_parser_omp_critical (parser, pragma_tok);
21534       break;
21535     case PRAGMA_OMP_FOR:
21536       stmt = cp_parser_omp_for (parser, pragma_tok);
21537       break;
21538     case PRAGMA_OMP_MASTER:
21539       stmt = cp_parser_omp_master (parser, pragma_tok);
21540       break;
21541     case PRAGMA_OMP_ORDERED:
21542       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21543       break;
21544     case PRAGMA_OMP_PARALLEL:
21545       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21546       break;
21547     case PRAGMA_OMP_SECTIONS:
21548       stmt = cp_parser_omp_sections (parser, pragma_tok);
21549       break;
21550     case PRAGMA_OMP_SINGLE:
21551       stmt = cp_parser_omp_single (parser, pragma_tok);
21552       break;
21553     case PRAGMA_OMP_TASK:
21554       stmt = cp_parser_omp_task (parser, pragma_tok);
21555       break;
21556     default:
21557       gcc_unreachable ();
21558     }
21559
21560   if (stmt)
21561     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21562 }
21563 \f
21564 /* The parser.  */
21565
21566 static GTY (()) cp_parser *the_parser;
21567
21568 \f
21569 /* Special handling for the first token or line in the file.  The first
21570    thing in the file might be #pragma GCC pch_preprocess, which loads a
21571    PCH file, which is a GC collection point.  So we need to handle this
21572    first pragma without benefit of an existing lexer structure.
21573
21574    Always returns one token to the caller in *FIRST_TOKEN.  This is
21575    either the true first token of the file, or the first token after
21576    the initial pragma.  */
21577
21578 static void
21579 cp_parser_initial_pragma (cp_token *first_token)
21580 {
21581   tree name = NULL;
21582
21583   cp_lexer_get_preprocessor_token (NULL, first_token);
21584   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21585     return;
21586
21587   cp_lexer_get_preprocessor_token (NULL, first_token);
21588   if (first_token->type == CPP_STRING)
21589     {
21590       name = first_token->u.value;
21591
21592       cp_lexer_get_preprocessor_token (NULL, first_token);
21593       if (first_token->type != CPP_PRAGMA_EOL)
21594         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21595                &first_token->location);
21596     }
21597   else
21598     error ("%Hexpected string literal", &first_token->location);
21599
21600   /* Skip to the end of the pragma.  */
21601   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21602     cp_lexer_get_preprocessor_token (NULL, first_token);
21603
21604   /* Now actually load the PCH file.  */
21605   if (name)
21606     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21607
21608   /* Read one more token to return to our caller.  We have to do this
21609      after reading the PCH file in, since its pointers have to be
21610      live.  */
21611   cp_lexer_get_preprocessor_token (NULL, first_token);
21612 }
21613
21614 /* Normal parsing of a pragma token.  Here we can (and must) use the
21615    regular lexer.  */
21616
21617 static bool
21618 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21619 {
21620   cp_token *pragma_tok;
21621   unsigned int id;
21622
21623   pragma_tok = cp_lexer_consume_token (parser->lexer);
21624   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21625   parser->lexer->in_pragma = true;
21626
21627   id = pragma_tok->pragma_kind;
21628   switch (id)
21629     {
21630     case PRAGMA_GCC_PCH_PREPROCESS:
21631       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21632              &pragma_tok->location);
21633       break;
21634
21635     case PRAGMA_OMP_BARRIER:
21636       switch (context)
21637         {
21638         case pragma_compound:
21639           cp_parser_omp_barrier (parser, pragma_tok);
21640           return false;
21641         case pragma_stmt:
21642           error ("%H%<#pragma omp barrier%> may only be "
21643                  "used in compound statements", &pragma_tok->location);
21644           break;
21645         default:
21646           goto bad_stmt;
21647         }
21648       break;
21649
21650     case PRAGMA_OMP_FLUSH:
21651       switch (context)
21652         {
21653         case pragma_compound:
21654           cp_parser_omp_flush (parser, pragma_tok);
21655           return false;
21656         case pragma_stmt:
21657           error ("%H%<#pragma omp flush%> may only be "
21658                  "used in compound statements", &pragma_tok->location);
21659           break;
21660         default:
21661           goto bad_stmt;
21662         }
21663       break;
21664
21665     case PRAGMA_OMP_TASKWAIT:
21666       switch (context)
21667         {
21668         case pragma_compound:
21669           cp_parser_omp_taskwait (parser, pragma_tok);
21670           return false;
21671         case pragma_stmt:
21672           error ("%H%<#pragma omp taskwait%> may only be "
21673                  "used in compound statements",
21674                  &pragma_tok->location);
21675           break;
21676         default:
21677           goto bad_stmt;
21678         }
21679       break;
21680
21681     case PRAGMA_OMP_THREADPRIVATE:
21682       cp_parser_omp_threadprivate (parser, pragma_tok);
21683       return false;
21684
21685     case PRAGMA_OMP_ATOMIC:
21686     case PRAGMA_OMP_CRITICAL:
21687     case PRAGMA_OMP_FOR:
21688     case PRAGMA_OMP_MASTER:
21689     case PRAGMA_OMP_ORDERED:
21690     case PRAGMA_OMP_PARALLEL:
21691     case PRAGMA_OMP_SECTIONS:
21692     case PRAGMA_OMP_SINGLE:
21693     case PRAGMA_OMP_TASK:
21694       if (context == pragma_external)
21695         goto bad_stmt;
21696       cp_parser_omp_construct (parser, pragma_tok);
21697       return true;
21698
21699     case PRAGMA_OMP_SECTION:
21700       error ("%H%<#pragma omp section%> may only be used in "
21701              "%<#pragma omp sections%> construct", &pragma_tok->location);
21702       break;
21703
21704     default:
21705       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21706       c_invoke_pragma_handler (id);
21707       break;
21708
21709     bad_stmt:
21710       cp_parser_error (parser, "expected declaration specifiers");
21711       break;
21712     }
21713
21714   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21715   return false;
21716 }
21717
21718 /* The interface the pragma parsers have to the lexer.  */
21719
21720 enum cpp_ttype
21721 pragma_lex (tree *value)
21722 {
21723   cp_token *tok;
21724   enum cpp_ttype ret;
21725
21726   tok = cp_lexer_peek_token (the_parser->lexer);
21727
21728   ret = tok->type;
21729   *value = tok->u.value;
21730
21731   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21732     ret = CPP_EOF;
21733   else if (ret == CPP_STRING)
21734     *value = cp_parser_string_literal (the_parser, false, false);
21735   else
21736     {
21737       cp_lexer_consume_token (the_parser->lexer);
21738       if (ret == CPP_KEYWORD)
21739         ret = CPP_NAME;
21740     }
21741
21742   return ret;
21743 }
21744
21745 \f
21746 /* External interface.  */
21747
21748 /* Parse one entire translation unit.  */
21749
21750 void
21751 c_parse_file (void)
21752 {
21753   bool error_occurred;
21754   static bool already_called = false;
21755
21756   if (already_called)
21757     {
21758       sorry ("inter-module optimizations not implemented for C++");
21759       return;
21760     }
21761   already_called = true;
21762
21763   the_parser = cp_parser_new ();
21764   push_deferring_access_checks (flag_access_control
21765                                 ? dk_no_deferred : dk_no_check);
21766   error_occurred = cp_parser_translation_unit (the_parser);
21767   the_parser = NULL;
21768 }
21769
21770 #include "gt-cp-parser.h"