OSDN Git Service

2008-09-30 H.J. Lu <hongjiu.lu@intel.com>
[pf3gnuchains/gcc-fork.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 *, tree, cp_cv_quals, tree, 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                       tree parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification,
1019                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591   (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597   (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599   (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1601   (cp_token *);
1602 static tree cp_parser_new_expression
1603   (cp_parser *);
1604 static tree cp_parser_new_placement
1605   (cp_parser *);
1606 static tree cp_parser_new_type_id
1607   (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609   (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611   (cp_parser *);
1612 static tree cp_parser_new_initializer
1613   (cp_parser *);
1614 static tree cp_parser_delete_expression
1615   (cp_parser *);
1616 static tree cp_parser_cast_expression
1617   (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       /* Floating-point literals are only allowed in an integral
3148          constant expression if they are cast to an integral or
3149          enumeration type.  */
3150       if (TREE_CODE (token->u.value) == REAL_CST
3151           && parser->integral_constant_expression_p
3152           && pedantic)
3153         {
3154           /* CAST_P will be set even in invalid code like "int(2.7 +
3155              ...)".   Therefore, we have to check that the next token
3156              is sure to end the cast.  */
3157           if (cast_p)
3158             {
3159               cp_token *next_token;
3160
3161               next_token = cp_lexer_peek_token (parser->lexer);
3162               if (/* The comma at the end of an
3163                      enumerator-definition.  */
3164                   next_token->type != CPP_COMMA
3165                   /* The curly brace at the end of an enum-specifier.  */
3166                   && next_token->type != CPP_CLOSE_BRACE
3167                   /* The end of a statement.  */
3168                   && next_token->type != CPP_SEMICOLON
3169                   /* The end of the cast-expression.  */
3170                   && next_token->type != CPP_CLOSE_PAREN
3171                   /* The end of an array bound.  */
3172                   && next_token->type != CPP_CLOSE_SQUARE
3173                   /* The closing ">" in a template-argument-list.  */
3174                   && (next_token->type != CPP_GREATER
3175                       || parser->greater_than_is_operator_p)
3176                   /* C++0x only: A ">>" treated like two ">" tokens,
3177                      in a template-argument-list.  */
3178                   && (next_token->type != CPP_RSHIFT
3179                       || (cxx_dialect == cxx98)
3180                       || parser->greater_than_is_operator_p))
3181                 cast_p = false;
3182             }
3183
3184           /* If we are within a cast, then the constraint that the
3185              cast is to an integral or enumeration type will be
3186              checked at that point.  If we are not within a cast, then
3187              this code is invalid.  */
3188           if (!cast_p)
3189             cp_parser_non_integral_constant_expression
3190               (parser, "floating-point literal");
3191         }
3192       return token->u.value;
3193
3194     case CPP_STRING:
3195     case CPP_STRING16:
3196     case CPP_STRING32:
3197     case CPP_WSTRING:
3198       /* ??? Should wide strings be allowed when parser->translate_strings_p
3199          is false (i.e. in attributes)?  If not, we can kill the third
3200          argument to cp_parser_string_literal.  */
3201       return cp_parser_string_literal (parser,
3202                                        parser->translate_strings_p,
3203                                        true);
3204
3205     case CPP_OPEN_PAREN:
3206       {
3207         tree expr;
3208         bool saved_greater_than_is_operator_p;
3209
3210         /* Consume the `('.  */
3211         cp_lexer_consume_token (parser->lexer);
3212         /* Within a parenthesized expression, a `>' token is always
3213            the greater-than operator.  */
3214         saved_greater_than_is_operator_p
3215           = parser->greater_than_is_operator_p;
3216         parser->greater_than_is_operator_p = true;
3217         /* If we see `( { ' then we are looking at the beginning of
3218            a GNU statement-expression.  */
3219         if (cp_parser_allow_gnu_extensions_p (parser)
3220             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221           {
3222             /* Statement-expressions are not allowed by the standard.  */
3223             pedwarn (token->location, OPT_pedantic, 
3224                      "ISO C++ forbids braced-groups within expressions");
3225
3226             /* And they're not allowed outside of a function-body; you
3227                cannot, for example, write:
3228
3229                  int i = ({ int j = 3; j + 1; });
3230
3231                at class or namespace scope.  */
3232             if (!parser->in_function_body
3233                 || parser->in_template_argument_list_p)
3234               {
3235                 error ("%Hstatement-expressions are not allowed outside "
3236                        "functions nor in template-argument lists",
3237                        &token->location);
3238                 cp_parser_skip_to_end_of_block_or_statement (parser);
3239                 expr = error_mark_node;
3240               }
3241             else
3242               {
3243                 /* Start the statement-expression.  */
3244                 expr = begin_stmt_expr ();
3245                 /* Parse the compound-statement.  */
3246                 cp_parser_compound_statement (parser, expr, false);
3247                 /* Finish up.  */
3248                 expr = finish_stmt_expr (expr, false);
3249               }
3250           }
3251         else
3252           {
3253             /* Parse the parenthesized expression.  */
3254             expr = cp_parser_expression (parser, cast_p);
3255             /* Let the front end know that this expression was
3256                enclosed in parentheses. This matters in case, for
3257                example, the expression is of the form `A::B', since
3258                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259                not.  */
3260             finish_parenthesized_expr (expr);
3261           }
3262         /* The `>' token might be the end of a template-id or
3263            template-parameter-list now.  */
3264         parser->greater_than_is_operator_p
3265           = saved_greater_than_is_operator_p;
3266         /* Consume the `)'.  */
3267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268           cp_parser_skip_to_end_of_statement (parser);
3269
3270         return expr;
3271       }
3272
3273     case CPP_KEYWORD:
3274       switch (token->keyword)
3275         {
3276           /* These two are the boolean literals.  */
3277         case RID_TRUE:
3278           cp_lexer_consume_token (parser->lexer);
3279           return boolean_true_node;
3280         case RID_FALSE:
3281           cp_lexer_consume_token (parser->lexer);
3282           return boolean_false_node;
3283
3284           /* The `__null' literal.  */
3285         case RID_NULL:
3286           cp_lexer_consume_token (parser->lexer);
3287           return null_node;
3288
3289           /* Recognize the `this' keyword.  */
3290         case RID_THIS:
3291           cp_lexer_consume_token (parser->lexer);
3292           if (parser->local_variables_forbidden_p)
3293             {
3294               error ("%H%<this%> may not be used in this context",
3295                      &token->location);
3296               return error_mark_node;
3297             }
3298           /* Pointers cannot appear in constant-expressions.  */
3299           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300             return error_mark_node;
3301           return finish_this_expr ();
3302
3303           /* The `operator' keyword can be the beginning of an
3304              id-expression.  */
3305         case RID_OPERATOR:
3306           goto id_expression;
3307
3308         case RID_FUNCTION_NAME:
3309         case RID_PRETTY_FUNCTION_NAME:
3310         case RID_C99_FUNCTION_NAME:
3311           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312              __func__ are the names of variables -- but they are
3313              treated specially.  Therefore, they are handled here,
3314              rather than relying on the generic id-expression logic
3315              below.  Grammatically, these names are id-expressions.
3316
3317              Consume the token.  */
3318           token = cp_lexer_consume_token (parser->lexer);
3319           /* Look up the name.  */
3320           return finish_fname (token->u.value);
3321
3322         case RID_VA_ARG:
3323           {
3324             tree expression;
3325             tree type;
3326
3327             /* The `__builtin_va_arg' construct is used to handle
3328                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3329             cp_lexer_consume_token (parser->lexer);
3330             /* Look for the opening `('.  */
3331             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332             /* Now, parse the assignment-expression.  */
3333             expression = cp_parser_assignment_expression (parser,
3334                                                           /*cast_p=*/false);
3335             /* Look for the `,'.  */
3336             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337             /* Parse the type-id.  */
3338             type = cp_parser_type_id (parser);
3339             /* Look for the closing `)'.  */
3340             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341             /* Using `va_arg' in a constant-expression is not
3342                allowed.  */
3343             if (cp_parser_non_integral_constant_expression (parser,
3344                                                             "%<va_arg%>"))
3345               return error_mark_node;
3346             return build_x_va_arg (expression, type);
3347           }
3348
3349         case RID_OFFSETOF:
3350           return cp_parser_builtin_offsetof (parser);
3351
3352         case RID_HAS_NOTHROW_ASSIGN:
3353         case RID_HAS_NOTHROW_CONSTRUCTOR:
3354         case RID_HAS_NOTHROW_COPY:        
3355         case RID_HAS_TRIVIAL_ASSIGN:
3356         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357         case RID_HAS_TRIVIAL_COPY:        
3358         case RID_HAS_TRIVIAL_DESTRUCTOR:
3359         case RID_HAS_VIRTUAL_DESTRUCTOR:
3360         case RID_IS_ABSTRACT:
3361         case RID_IS_BASE_OF:
3362         case RID_IS_CLASS:
3363         case RID_IS_CONVERTIBLE_TO:
3364         case RID_IS_EMPTY:
3365         case RID_IS_ENUM:
3366         case RID_IS_POD:
3367         case RID_IS_POLYMORPHIC:
3368         case RID_IS_UNION:
3369           return cp_parser_trait_expr (parser, token->keyword);
3370
3371         /* Objective-C++ expressions.  */
3372         case RID_AT_ENCODE:
3373         case RID_AT_PROTOCOL:
3374         case RID_AT_SELECTOR:
3375           return cp_parser_objc_expression (parser);
3376
3377         default:
3378           cp_parser_error (parser, "expected primary-expression");
3379           return error_mark_node;
3380         }
3381
3382       /* An id-expression can start with either an identifier, a
3383          `::' as the beginning of a qualified-id, or the "operator"
3384          keyword.  */
3385     case CPP_NAME:
3386     case CPP_SCOPE:
3387     case CPP_TEMPLATE_ID:
3388     case CPP_NESTED_NAME_SPECIFIER:
3389       {
3390         tree id_expression;
3391         tree decl;
3392         const char *error_msg;
3393         bool template_p;
3394         bool done;
3395         cp_token *id_expr_token;
3396
3397       id_expression:
3398         /* Parse the id-expression.  */
3399         id_expression
3400           = cp_parser_id_expression (parser,
3401                                      /*template_keyword_p=*/false,
3402                                      /*check_dependency_p=*/true,
3403                                      &template_p,
3404                                      /*declarator_p=*/false,
3405                                      /*optional_p=*/false);
3406         if (id_expression == error_mark_node)
3407           return error_mark_node;
3408         id_expr_token = token;
3409         token = cp_lexer_peek_token (parser->lexer);
3410         done = (token->type != CPP_OPEN_SQUARE
3411                 && token->type != CPP_OPEN_PAREN
3412                 && token->type != CPP_DOT
3413                 && token->type != CPP_DEREF
3414                 && token->type != CPP_PLUS_PLUS
3415                 && token->type != CPP_MINUS_MINUS);
3416         /* If we have a template-id, then no further lookup is
3417            required.  If the template-id was for a template-class, we
3418            will sometimes have a TYPE_DECL at this point.  */
3419         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420                  || TREE_CODE (id_expression) == TYPE_DECL)
3421           decl = id_expression;
3422         /* Look up the name.  */
3423         else
3424           {
3425             tree ambiguous_decls;
3426
3427             decl = cp_parser_lookup_name (parser, id_expression,
3428                                           none_type,
3429                                           template_p,
3430                                           /*is_namespace=*/false,
3431                                           /*check_dependency=*/true,
3432                                           &ambiguous_decls,
3433                                           id_expr_token->location);
3434             /* If the lookup was ambiguous, an error will already have
3435                been issued.  */
3436             if (ambiguous_decls)
3437               return error_mark_node;
3438
3439             /* In Objective-C++, an instance variable (ivar) may be preferred
3440                to whatever cp_parser_lookup_name() found.  */
3441             decl = objc_lookup_ivar (decl, id_expression);
3442
3443             /* If name lookup gives us a SCOPE_REF, then the
3444                qualifying scope was dependent.  */
3445             if (TREE_CODE (decl) == SCOPE_REF)
3446               {
3447                 /* At this point, we do not know if DECL is a valid
3448                    integral constant expression.  We assume that it is
3449                    in fact such an expression, so that code like:
3450
3451                       template <int N> struct A {
3452                         int a[B<N>::i];
3453                       };
3454                      
3455                    is accepted.  At template-instantiation time, we
3456                    will check that B<N>::i is actually a constant.  */
3457                 return decl;
3458               }
3459             /* Check to see if DECL is a local variable in a context
3460                where that is forbidden.  */
3461             if (parser->local_variables_forbidden_p
3462                 && local_variable_p (decl))
3463               {
3464                 /* It might be that we only found DECL because we are
3465                    trying to be generous with pre-ISO scoping rules.
3466                    For example, consider:
3467
3468                      int i;
3469                      void g() {
3470                        for (int i = 0; i < 10; ++i) {}
3471                        extern void f(int j = i);
3472                      }
3473
3474                    Here, name look up will originally find the out
3475                    of scope `i'.  We need to issue a warning message,
3476                    but then use the global `i'.  */
3477                 decl = check_for_out_of_scope_variable (decl);
3478                 if (local_variable_p (decl))
3479                   {
3480                     error ("%Hlocal variable %qD may not appear in this context",
3481                            &id_expr_token->location, decl);
3482                     return error_mark_node;
3483                   }
3484               }
3485           }
3486
3487         decl = (finish_id_expression
3488                 (id_expression, decl, parser->scope,
3489                  idk,
3490                  parser->integral_constant_expression_p,
3491                  parser->allow_non_integral_constant_expression_p,
3492                  &parser->non_integral_constant_expression_p,
3493                  template_p, done, address_p,
3494                  template_arg_p,
3495                  &error_msg,
3496                  id_expr_token->location));
3497         if (error_msg)
3498           cp_parser_error (parser, error_msg);
3499         return decl;
3500       }
3501
3502       /* Anything else is an error.  */
3503     default:
3504       /* ...unless we have an Objective-C++ message or string literal,
3505          that is.  */
3506       if (c_dialect_objc ()
3507           && (token->type == CPP_OPEN_SQUARE
3508               || token->type == CPP_OBJC_STRING))
3509         return cp_parser_objc_expression (parser);
3510
3511       cp_parser_error (parser, "expected primary-expression");
3512       return error_mark_node;
3513     }
3514 }
3515
3516 /* Parse an id-expression.
3517
3518    id-expression:
3519      unqualified-id
3520      qualified-id
3521
3522    qualified-id:
3523      :: [opt] nested-name-specifier template [opt] unqualified-id
3524      :: identifier
3525      :: operator-function-id
3526      :: template-id
3527
3528    Return a representation of the unqualified portion of the
3529    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3530    a `::' or nested-name-specifier.
3531
3532    Often, if the id-expression was a qualified-id, the caller will
3533    want to make a SCOPE_REF to represent the qualified-id.  This
3534    function does not do this in order to avoid wastefully creating
3535    SCOPE_REFs when they are not required.
3536
3537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538    `template' keyword.
3539
3540    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541    uninstantiated templates.
3542
3543    If *TEMPLATE_P is non-NULL, it is set to true iff the
3544    `template' keyword is used to explicitly indicate that the entity
3545    named is a template.
3546
3547    If DECLARATOR_P is true, the id-expression is appearing as part of
3548    a declarator, rather than as part of an expression.  */
3549
3550 static tree
3551 cp_parser_id_expression (cp_parser *parser,
3552                          bool template_keyword_p,
3553                          bool check_dependency_p,
3554                          bool *template_p,
3555                          bool declarator_p,
3556                          bool optional_p)
3557 {
3558   bool global_scope_p;
3559   bool nested_name_specifier_p;
3560
3561   /* Assume the `template' keyword was not used.  */
3562   if (template_p)
3563     *template_p = template_keyword_p;
3564
3565   /* Look for the optional `::' operator.  */
3566   global_scope_p
3567     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3568        != NULL_TREE);
3569   /* Look for the optional nested-name-specifier.  */
3570   nested_name_specifier_p
3571     = (cp_parser_nested_name_specifier_opt (parser,
3572                                             /*typename_keyword_p=*/false,
3573                                             check_dependency_p,
3574                                             /*type_p=*/false,
3575                                             declarator_p)
3576        != NULL_TREE);
3577   /* If there is a nested-name-specifier, then we are looking at
3578      the first qualified-id production.  */
3579   if (nested_name_specifier_p)
3580     {
3581       tree saved_scope;
3582       tree saved_object_scope;
3583       tree saved_qualifying_scope;
3584       tree unqualified_id;
3585       bool is_template;
3586
3587       /* See if the next token is the `template' keyword.  */
3588       if (!template_p)
3589         template_p = &is_template;
3590       *template_p = cp_parser_optional_template_keyword (parser);
3591       /* Name lookup we do during the processing of the
3592          unqualified-id might obliterate SCOPE.  */
3593       saved_scope = parser->scope;
3594       saved_object_scope = parser->object_scope;
3595       saved_qualifying_scope = parser->qualifying_scope;
3596       /* Process the final unqualified-id.  */
3597       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598                                                  check_dependency_p,
3599                                                  declarator_p,
3600                                                  /*optional_p=*/false);
3601       /* Restore the SAVED_SCOPE for our caller.  */
3602       parser->scope = saved_scope;
3603       parser->object_scope = saved_object_scope;
3604       parser->qualifying_scope = saved_qualifying_scope;
3605
3606       return unqualified_id;
3607     }
3608   /* Otherwise, if we are in global scope, then we are looking at one
3609      of the other qualified-id productions.  */
3610   else if (global_scope_p)
3611     {
3612       cp_token *token;
3613       tree id;
3614
3615       /* Peek at the next token.  */
3616       token = cp_lexer_peek_token (parser->lexer);
3617
3618       /* If it's an identifier, and the next token is not a "<", then
3619          we can avoid the template-id case.  This is an optimization
3620          for this common case.  */
3621       if (token->type == CPP_NAME
3622           && !cp_parser_nth_token_starts_template_argument_list_p
3623                (parser, 2))
3624         return cp_parser_identifier (parser);
3625
3626       cp_parser_parse_tentatively (parser);
3627       /* Try a template-id.  */
3628       id = cp_parser_template_id (parser,
3629                                   /*template_keyword_p=*/false,
3630                                   /*check_dependency_p=*/true,
3631                                   declarator_p);
3632       /* If that worked, we're done.  */
3633       if (cp_parser_parse_definitely (parser))
3634         return id;
3635
3636       /* Peek at the next token.  (Changes in the token buffer may
3637          have invalidated the pointer obtained above.)  */
3638       token = cp_lexer_peek_token (parser->lexer);
3639
3640       switch (token->type)
3641         {
3642         case CPP_NAME:
3643           return cp_parser_identifier (parser);
3644
3645         case CPP_KEYWORD:
3646           if (token->keyword == RID_OPERATOR)
3647             return cp_parser_operator_function_id (parser);
3648           /* Fall through.  */
3649
3650         default:
3651           cp_parser_error (parser, "expected id-expression");
3652           return error_mark_node;
3653         }
3654     }
3655   else
3656     return cp_parser_unqualified_id (parser, template_keyword_p,
3657                                      /*check_dependency_p=*/true,
3658                                      declarator_p,
3659                                      optional_p);
3660 }
3661
3662 /* Parse an unqualified-id.
3663
3664    unqualified-id:
3665      identifier
3666      operator-function-id
3667      conversion-function-id
3668      ~ class-name
3669      template-id
3670
3671    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672    keyword, in a construct like `A::template ...'.
3673
3674    Returns a representation of unqualified-id.  For the `identifier'
3675    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3676    production a BIT_NOT_EXPR is returned; the operand of the
3677    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3678    other productions, see the documentation accompanying the
3679    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3680    names are looked up in uninstantiated templates.  If DECLARATOR_P
3681    is true, the unqualified-id is appearing as part of a declarator,
3682    rather than as part of an expression.  */
3683
3684 static tree
3685 cp_parser_unqualified_id (cp_parser* parser,
3686                           bool template_keyword_p,
3687                           bool check_dependency_p,
3688                           bool declarator_p,
3689                           bool optional_p)
3690 {
3691   cp_token *token;
3692
3693   /* Peek at the next token.  */
3694   token = cp_lexer_peek_token (parser->lexer);
3695
3696   switch (token->type)
3697     {
3698     case CPP_NAME:
3699       {
3700         tree id;
3701
3702         /* We don't know yet whether or not this will be a
3703            template-id.  */
3704         cp_parser_parse_tentatively (parser);
3705         /* Try a template-id.  */
3706         id = cp_parser_template_id (parser, template_keyword_p,
3707                                     check_dependency_p,
3708                                     declarator_p);
3709         /* If it worked, we're done.  */
3710         if (cp_parser_parse_definitely (parser))
3711           return id;
3712         /* Otherwise, it's an ordinary identifier.  */
3713         return cp_parser_identifier (parser);
3714       }
3715
3716     case CPP_TEMPLATE_ID:
3717       return cp_parser_template_id (parser, template_keyword_p,
3718                                     check_dependency_p,
3719                                     declarator_p);
3720
3721     case CPP_COMPL:
3722       {
3723         tree type_decl;
3724         tree qualifying_scope;
3725         tree object_scope;
3726         tree scope;
3727         bool done;
3728
3729         /* Consume the `~' token.  */
3730         cp_lexer_consume_token (parser->lexer);
3731         /* Parse the class-name.  The standard, as written, seems to
3732            say that:
3733
3734              template <typename T> struct S { ~S (); };
3735              template <typename T> S<T>::~S() {}
3736
3737            is invalid, since `~' must be followed by a class-name, but
3738            `S<T>' is dependent, and so not known to be a class.
3739            That's not right; we need to look in uninstantiated
3740            templates.  A further complication arises from:
3741
3742              template <typename T> void f(T t) {
3743                t.T::~T();
3744              }
3745
3746            Here, it is not possible to look up `T' in the scope of `T'
3747            itself.  We must look in both the current scope, and the
3748            scope of the containing complete expression.
3749
3750            Yet another issue is:
3751
3752              struct S {
3753                int S;
3754                ~S();
3755              };
3756
3757              S::~S() {}
3758
3759            The standard does not seem to say that the `S' in `~S'
3760            should refer to the type `S' and not the data member
3761            `S::S'.  */
3762
3763         /* DR 244 says that we look up the name after the "~" in the
3764            same scope as we looked up the qualifying name.  That idea
3765            isn't fully worked out; it's more complicated than that.  */
3766         scope = parser->scope;
3767         object_scope = parser->object_scope;
3768         qualifying_scope = parser->qualifying_scope;
3769
3770         /* Check for invalid scopes.  */
3771         if (scope == error_mark_node)
3772           {
3773             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774               cp_lexer_consume_token (parser->lexer);
3775             return error_mark_node;
3776           }
3777         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3778           {
3779             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780               error ("%Hscope %qT before %<~%> is not a class-name",
3781                      &token->location, scope);
3782             cp_parser_simulate_error (parser);
3783             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784               cp_lexer_consume_token (parser->lexer);
3785             return error_mark_node;
3786           }
3787         gcc_assert (!scope || TYPE_P (scope));
3788
3789         /* If the name is of the form "X::~X" it's OK.  */
3790         token = cp_lexer_peek_token (parser->lexer);
3791         if (scope
3792             && token->type == CPP_NAME
3793             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3794                 == CPP_OPEN_PAREN)
3795             && constructor_name_p (token->u.value, scope))
3796           {
3797             cp_lexer_consume_token (parser->lexer);
3798             return build_nt (BIT_NOT_EXPR, scope);
3799           }
3800
3801         /* If there was an explicit qualification (S::~T), first look
3802            in the scope given by the qualification (i.e., S).  */
3803         done = false;
3804         type_decl = NULL_TREE;
3805         if (scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             type_decl = cp_parser_class_name (parser,
3809                                               /*typename_keyword_p=*/false,
3810                                               /*template_keyword_p=*/false,
3811                                               none_type,
3812                                               /*check_dependency=*/false,
3813                                               /*class_head_p=*/false,
3814                                               declarator_p);
3815             if (cp_parser_parse_definitely (parser))
3816               done = true;
3817           }
3818         /* In "N::S::~S", look in "N" as well.  */
3819         if (!done && scope && qualifying_scope)
3820           {
3821             cp_parser_parse_tentatively (parser);
3822             parser->scope = qualifying_scope;
3823             parser->object_scope = NULL_TREE;
3824             parser->qualifying_scope = NULL_TREE;
3825             type_decl
3826               = cp_parser_class_name (parser,
3827                                       /*typename_keyword_p=*/false,
3828                                       /*template_keyword_p=*/false,
3829                                       none_type,
3830                                       /*check_dependency=*/false,
3831                                       /*class_head_p=*/false,
3832                                       declarator_p);
3833             if (cp_parser_parse_definitely (parser))
3834               done = true;
3835           }
3836         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3837         else if (!done && object_scope)
3838           {
3839             cp_parser_parse_tentatively (parser);
3840             parser->scope = object_scope;
3841             parser->object_scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             type_decl
3844               = cp_parser_class_name (parser,
3845                                       /*typename_keyword_p=*/false,
3846                                       /*template_keyword_p=*/false,
3847                                       none_type,
3848                                       /*check_dependency=*/false,
3849                                       /*class_head_p=*/false,
3850                                       declarator_p);
3851             if (cp_parser_parse_definitely (parser))
3852               done = true;
3853           }
3854         /* Look in the surrounding context.  */
3855         if (!done)
3856           {
3857             parser->scope = NULL_TREE;
3858             parser->object_scope = NULL_TREE;
3859             parser->qualifying_scope = NULL_TREE;
3860             type_decl
3861               = cp_parser_class_name (parser,
3862                                       /*typename_keyword_p=*/false,
3863                                       /*template_keyword_p=*/false,
3864                                       none_type,
3865                                       /*check_dependency=*/false,
3866                                       /*class_head_p=*/false,
3867                                       declarator_p);
3868           }
3869         /* If an error occurred, assume that the name of the
3870            destructor is the same as the name of the qualifying
3871            class.  That allows us to keep parsing after running
3872            into ill-formed destructor names.  */
3873         if (type_decl == error_mark_node && scope)
3874           return build_nt (BIT_NOT_EXPR, scope);
3875         else if (type_decl == error_mark_node)
3876           return error_mark_node;
3877
3878         /* Check that destructor name and scope match.  */
3879         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3880           {
3881             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882               error ("%Hdeclaration of %<~%T%> as member of %qT",
3883                      &token->location, type_decl, scope);
3884             cp_parser_simulate_error (parser);
3885             return error_mark_node;
3886           }
3887
3888         /* [class.dtor]
3889
3890            A typedef-name that names a class shall not be used as the
3891            identifier in the declarator for a destructor declaration.  */
3892         if (declarator_p
3893             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894             && !DECL_SELF_REFERENCE_P (type_decl)
3895             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896           error ("%Htypedef-name %qD used as destructor declarator",
3897                  &token->location, type_decl);
3898
3899         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3900       }
3901
3902     case CPP_KEYWORD:
3903       if (token->keyword == RID_OPERATOR)
3904         {
3905           tree id;
3906
3907           /* This could be a template-id, so we try that first.  */
3908           cp_parser_parse_tentatively (parser);
3909           /* Try a template-id.  */
3910           id = cp_parser_template_id (parser, template_keyword_p,
3911                                       /*check_dependency_p=*/true,
3912                                       declarator_p);
3913           /* If that worked, we're done.  */
3914           if (cp_parser_parse_definitely (parser))
3915             return id;
3916           /* We still don't know whether we're looking at an
3917              operator-function-id or a conversion-function-id.  */
3918           cp_parser_parse_tentatively (parser);
3919           /* Try an operator-function-id.  */
3920           id = cp_parser_operator_function_id (parser);
3921           /* If that didn't work, try a conversion-function-id.  */
3922           if (!cp_parser_parse_definitely (parser))
3923             id = cp_parser_conversion_function_id (parser);
3924
3925           return id;
3926         }
3927       /* Fall through.  */
3928
3929     default:
3930       if (optional_p)
3931         return NULL_TREE;
3932       cp_parser_error (parser, "expected unqualified-id");
3933       return error_mark_node;
3934     }
3935 }
3936
3937 /* Parse an (optional) nested-name-specifier.
3938
3939    nested-name-specifier: [C++98]
3940      class-or-namespace-name :: nested-name-specifier [opt]
3941      class-or-namespace-name :: template nested-name-specifier [opt]
3942
3943    nested-name-specifier: [C++0x]
3944      type-name ::
3945      namespace-name ::
3946      nested-name-specifier identifier ::
3947      nested-name-specifier template [opt] simple-template-id ::
3948
3949    PARSER->SCOPE should be set appropriately before this function is
3950    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3951    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3952    in name lookups.
3953
3954    Sets PARSER->SCOPE to the class (TYPE) or namespace
3955    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3956    it unchanged if there is no nested-name-specifier.  Returns the new
3957    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3958
3959    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3960    part of a declaration and/or decl-specifier.  */
3961
3962 static tree
3963 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3964                                      bool typename_keyword_p,
3965                                      bool check_dependency_p,
3966                                      bool type_p,
3967                                      bool is_declaration)
3968 {
3969   bool success = false;
3970   cp_token_position start = 0;
3971   cp_token *token;
3972
3973   /* Remember where the nested-name-specifier starts.  */
3974   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3975     {
3976       start = cp_lexer_token_position (parser->lexer, false);
3977       push_deferring_access_checks (dk_deferred);
3978     }
3979
3980   while (true)
3981     {
3982       tree new_scope;
3983       tree old_scope;
3984       tree saved_qualifying_scope;
3985       bool template_keyword_p;
3986
3987       /* Spot cases that cannot be the beginning of a
3988          nested-name-specifier.  */
3989       token = cp_lexer_peek_token (parser->lexer);
3990
3991       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3992          the already parsed nested-name-specifier.  */
3993       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3994         {
3995           /* Grab the nested-name-specifier and continue the loop.  */
3996           cp_parser_pre_parsed_nested_name_specifier (parser);
3997           /* If we originally encountered this nested-name-specifier
3998              with IS_DECLARATION set to false, we will not have
3999              resolved TYPENAME_TYPEs, so we must do so here.  */
4000           if (is_declaration
4001               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4002             {
4003               new_scope = resolve_typename_type (parser->scope,
4004                                                  /*only_current_p=*/false);
4005               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4006                 parser->scope = new_scope;
4007             }
4008           success = true;
4009           continue;
4010         }
4011
4012       /* Spot cases that cannot be the beginning of a
4013          nested-name-specifier.  On the second and subsequent times
4014          through the loop, we look for the `template' keyword.  */
4015       if (success && token->keyword == RID_TEMPLATE)
4016         ;
4017       /* A template-id can start a nested-name-specifier.  */
4018       else if (token->type == CPP_TEMPLATE_ID)
4019         ;
4020       else
4021         {
4022           /* If the next token is not an identifier, then it is
4023              definitely not a type-name or namespace-name.  */
4024           if (token->type != CPP_NAME)
4025             break;
4026           /* If the following token is neither a `<' (to begin a
4027              template-id), nor a `::', then we are not looking at a
4028              nested-name-specifier.  */
4029           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4030           if (token->type != CPP_SCOPE
4031               && !cp_parser_nth_token_starts_template_argument_list_p
4032                   (parser, 2))
4033             break;
4034         }
4035
4036       /* The nested-name-specifier is optional, so we parse
4037          tentatively.  */
4038       cp_parser_parse_tentatively (parser);
4039
4040       /* Look for the optional `template' keyword, if this isn't the
4041          first time through the loop.  */
4042       if (success)
4043         template_keyword_p = cp_parser_optional_template_keyword (parser);
4044       else
4045         template_keyword_p = false;
4046
4047       /* Save the old scope since the name lookup we are about to do
4048          might destroy it.  */
4049       old_scope = parser->scope;
4050       saved_qualifying_scope = parser->qualifying_scope;
4051       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4052          look up names in "X<T>::I" in order to determine that "Y" is
4053          a template.  So, if we have a typename at this point, we make
4054          an effort to look through it.  */
4055       if (is_declaration
4056           && !typename_keyword_p
4057           && parser->scope
4058           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059         parser->scope = resolve_typename_type (parser->scope,
4060                                                /*only_current_p=*/false);
4061       /* Parse the qualifying entity.  */
4062       new_scope
4063         = cp_parser_qualifying_entity (parser,
4064                                        typename_keyword_p,
4065                                        template_keyword_p,
4066                                        check_dependency_p,
4067                                        type_p,
4068                                        is_declaration);
4069       /* Look for the `::' token.  */
4070       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4071
4072       /* If we found what we wanted, we keep going; otherwise, we're
4073          done.  */
4074       if (!cp_parser_parse_definitely (parser))
4075         {
4076           bool error_p = false;
4077
4078           /* Restore the OLD_SCOPE since it was valid before the
4079              failed attempt at finding the last
4080              class-or-namespace-name.  */
4081           parser->scope = old_scope;
4082           parser->qualifying_scope = saved_qualifying_scope;
4083           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4084             break;
4085           /* If the next token is an identifier, and the one after
4086              that is a `::', then any valid interpretation would have
4087              found a class-or-namespace-name.  */
4088           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4089                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4090                      == CPP_SCOPE)
4091                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4092                      != CPP_COMPL))
4093             {
4094               token = cp_lexer_consume_token (parser->lexer);
4095               if (!error_p)
4096                 {
4097                   if (!token->ambiguous_p)
4098                     {
4099                       tree decl;
4100                       tree ambiguous_decls;
4101
4102                       decl = cp_parser_lookup_name (parser, token->u.value,
4103                                                     none_type,
4104                                                     /*is_template=*/false,
4105                                                     /*is_namespace=*/false,
4106                                                     /*check_dependency=*/true,
4107                                                     &ambiguous_decls,
4108                                                     token->location);
4109                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4110                         error ("%H%qD used without template parameters",
4111                                &token->location, decl);
4112                       else if (ambiguous_decls)
4113                         {
4114                           error ("%Hreference to %qD is ambiguous",
4115                                  &token->location, token->u.value);
4116                           print_candidates (ambiguous_decls);
4117                           decl = error_mark_node;
4118                         }
4119                       else
4120                         {
4121                           const char* msg = "is not a class or namespace";
4122                           if (cxx_dialect != cxx98)
4123                             msg = "is not a class, namespace, or enumeration";
4124                           cp_parser_name_lookup_error
4125                             (parser, token->u.value, decl, msg,
4126                              token->location);
4127                         }
4128                     }
4129                   parser->scope = error_mark_node;
4130                   error_p = true;
4131                   /* Treat this as a successful nested-name-specifier
4132                      due to:
4133
4134                      [basic.lookup.qual]
4135
4136                      If the name found is not a class-name (clause
4137                      _class_) or namespace-name (_namespace.def_), the
4138                      program is ill-formed.  */
4139                   success = true;
4140                 }
4141               cp_lexer_consume_token (parser->lexer);
4142             }
4143           break;
4144         }
4145       /* We've found one valid nested-name-specifier.  */
4146       success = true;
4147       /* Name lookup always gives us a DECL.  */
4148       if (TREE_CODE (new_scope) == TYPE_DECL)
4149         new_scope = TREE_TYPE (new_scope);
4150       /* Uses of "template" must be followed by actual templates.  */
4151       if (template_keyword_p
4152           && !(CLASS_TYPE_P (new_scope)
4153                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4154                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4155                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4156           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4157                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4158                    == TEMPLATE_ID_EXPR)))
4159         permerror (input_location, TYPE_P (new_scope)
4160                    ? "%qT is not a template"
4161                    : "%qD is not a template",
4162                    new_scope);
4163       /* If it is a class scope, try to complete it; we are about to
4164          be looking up names inside the class.  */
4165       if (TYPE_P (new_scope)
4166           /* Since checking types for dependency can be expensive,
4167              avoid doing it if the type is already complete.  */
4168           && !COMPLETE_TYPE_P (new_scope)
4169           /* Do not try to complete dependent types.  */
4170           && !dependent_type_p (new_scope))
4171         {
4172           new_scope = complete_type (new_scope);
4173           /* If it is a typedef to current class, use the current
4174              class instead, as the typedef won't have any names inside
4175              it yet.  */
4176           if (!COMPLETE_TYPE_P (new_scope)
4177               && currently_open_class (new_scope))
4178             new_scope = TYPE_MAIN_VARIANT (new_scope);
4179         }
4180       /* Make sure we look in the right scope the next time through
4181          the loop.  */
4182       parser->scope = new_scope;
4183     }
4184
4185   /* If parsing tentatively, replace the sequence of tokens that makes
4186      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4187      token.  That way, should we re-parse the token stream, we will
4188      not have to repeat the effort required to do the parse, nor will
4189      we issue duplicate error messages.  */
4190   if (success && start)
4191     {
4192       cp_token *token;
4193
4194       token = cp_lexer_token_at (parser->lexer, start);
4195       /* Reset the contents of the START token.  */
4196       token->type = CPP_NESTED_NAME_SPECIFIER;
4197       /* Retrieve any deferred checks.  Do not pop this access checks yet
4198          so the memory will not be reclaimed during token replacing below.  */
4199       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4200       token->u.tree_check_value->value = parser->scope;
4201       token->u.tree_check_value->checks = get_deferred_access_checks ();
4202       token->u.tree_check_value->qualifying_scope =
4203         parser->qualifying_scope;
4204       token->keyword = RID_MAX;
4205
4206       /* Purge all subsequent tokens.  */
4207       cp_lexer_purge_tokens_after (parser->lexer, start);
4208     }
4209
4210   if (start)
4211     pop_to_parent_deferring_access_checks ();
4212
4213   return success ? parser->scope : NULL_TREE;
4214 }
4215
4216 /* Parse a nested-name-specifier.  See
4217    cp_parser_nested_name_specifier_opt for details.  This function
4218    behaves identically, except that it will an issue an error if no
4219    nested-name-specifier is present.  */
4220
4221 static tree
4222 cp_parser_nested_name_specifier (cp_parser *parser,
4223                                  bool typename_keyword_p,
4224                                  bool check_dependency_p,
4225                                  bool type_p,
4226                                  bool is_declaration)
4227 {
4228   tree scope;
4229
4230   /* Look for the nested-name-specifier.  */
4231   scope = cp_parser_nested_name_specifier_opt (parser,
4232                                                typename_keyword_p,
4233                                                check_dependency_p,
4234                                                type_p,
4235                                                is_declaration);
4236   /* If it was not present, issue an error message.  */
4237   if (!scope)
4238     {
4239       cp_parser_error (parser, "expected nested-name-specifier");
4240       parser->scope = NULL_TREE;
4241     }
4242
4243   return scope;
4244 }
4245
4246 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4247    this is either a class-name or a namespace-name (which corresponds
4248    to the class-or-namespace-name production in the grammar). For
4249    C++0x, it can also be a type-name that refers to an enumeration
4250    type.
4251
4252    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4253    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4254    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4255    TYPE_P is TRUE iff the next name should be taken as a class-name,
4256    even the same name is declared to be another entity in the same
4257    scope.
4258
4259    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4260    specified by the class-or-namespace-name.  If neither is found the
4261    ERROR_MARK_NODE is returned.  */
4262
4263 static tree
4264 cp_parser_qualifying_entity (cp_parser *parser,
4265                              bool typename_keyword_p,
4266                              bool template_keyword_p,
4267                              bool check_dependency_p,
4268                              bool type_p,
4269                              bool is_declaration)
4270 {
4271   tree saved_scope;
4272   tree saved_qualifying_scope;
4273   tree saved_object_scope;
4274   tree scope;
4275   bool only_class_p;
4276   bool successful_parse_p;
4277
4278   /* Before we try to parse the class-name, we must save away the
4279      current PARSER->SCOPE since cp_parser_class_name will destroy
4280      it.  */
4281   saved_scope = parser->scope;
4282   saved_qualifying_scope = parser->qualifying_scope;
4283   saved_object_scope = parser->object_scope;
4284   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4285      there is no need to look for a namespace-name.  */
4286   only_class_p = template_keyword_p 
4287     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4288   if (!only_class_p)
4289     cp_parser_parse_tentatively (parser);
4290   scope = cp_parser_class_name (parser,
4291                                 typename_keyword_p,
4292                                 template_keyword_p,
4293                                 type_p ? class_type : none_type,
4294                                 check_dependency_p,
4295                                 /*class_head_p=*/false,
4296                                 is_declaration);
4297   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4298   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4299   if (!only_class_p 
4300       && cxx_dialect != cxx98
4301       && !successful_parse_p)
4302     {
4303       /* Restore the saved scope.  */
4304       parser->scope = saved_scope;
4305       parser->qualifying_scope = saved_qualifying_scope;
4306       parser->object_scope = saved_object_scope;
4307
4308       /* Parse tentatively.  */
4309       cp_parser_parse_tentatively (parser);
4310      
4311       /* Parse a typedef-name or enum-name.  */
4312       scope = cp_parser_nonclass_name (parser);
4313       successful_parse_p = cp_parser_parse_definitely (parser);
4314     }
4315   /* If that didn't work, try for a namespace-name.  */
4316   if (!only_class_p && !successful_parse_p)
4317     {
4318       /* Restore the saved scope.  */
4319       parser->scope = saved_scope;
4320       parser->qualifying_scope = saved_qualifying_scope;
4321       parser->object_scope = saved_object_scope;
4322       /* If we are not looking at an identifier followed by the scope
4323          resolution operator, then this is not part of a
4324          nested-name-specifier.  (Note that this function is only used
4325          to parse the components of a nested-name-specifier.)  */
4326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4327           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4328         return error_mark_node;
4329       scope = cp_parser_namespace_name (parser);
4330     }
4331
4332   return scope;
4333 }
4334
4335 /* Parse a postfix-expression.
4336
4337    postfix-expression:
4338      primary-expression
4339      postfix-expression [ expression ]
4340      postfix-expression ( expression-list [opt] )
4341      simple-type-specifier ( expression-list [opt] )
4342      typename :: [opt] nested-name-specifier identifier
4343        ( expression-list [opt] )
4344      typename :: [opt] nested-name-specifier template [opt] template-id
4345        ( expression-list [opt] )
4346      postfix-expression . template [opt] id-expression
4347      postfix-expression -> template [opt] id-expression
4348      postfix-expression . pseudo-destructor-name
4349      postfix-expression -> pseudo-destructor-name
4350      postfix-expression ++
4351      postfix-expression --
4352      dynamic_cast < type-id > ( expression )
4353      static_cast < type-id > ( expression )
4354      reinterpret_cast < type-id > ( expression )
4355      const_cast < type-id > ( expression )
4356      typeid ( expression )
4357      typeid ( type-id )
4358
4359    GNU Extension:
4360
4361    postfix-expression:
4362      ( type-id ) { initializer-list , [opt] }
4363
4364    This extension is a GNU version of the C99 compound-literal
4365    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4366    but they are essentially the same concept.)
4367
4368    If ADDRESS_P is true, the postfix expression is the operand of the
4369    `&' operator.  CAST_P is true if this expression is the target of a
4370    cast.
4371
4372    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4373    class member access expressions [expr.ref].
4374
4375    Returns a representation of the expression.  */
4376
4377 static tree
4378 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4379                               bool member_access_only_p)
4380 {
4381   cp_token *token;
4382   enum rid keyword;
4383   cp_id_kind idk = CP_ID_KIND_NONE;
4384   tree postfix_expression = NULL_TREE;
4385   bool is_member_access = false;
4386
4387   /* Peek at the next token.  */
4388   token = cp_lexer_peek_token (parser->lexer);
4389   /* Some of the productions are determined by keywords.  */
4390   keyword = token->keyword;
4391   switch (keyword)
4392     {
4393     case RID_DYNCAST:
4394     case RID_STATCAST:
4395     case RID_REINTCAST:
4396     case RID_CONSTCAST:
4397       {
4398         tree type;
4399         tree expression;
4400         const char *saved_message;
4401
4402         /* All of these can be handled in the same way from the point
4403            of view of parsing.  Begin by consuming the token
4404            identifying the cast.  */
4405         cp_lexer_consume_token (parser->lexer);
4406
4407         /* New types cannot be defined in the cast.  */
4408         saved_message = parser->type_definition_forbidden_message;
4409         parser->type_definition_forbidden_message
4410           = "types may not be defined in casts";
4411
4412         /* Look for the opening `<'.  */
4413         cp_parser_require (parser, CPP_LESS, "%<<%>");
4414         /* Parse the type to which we are casting.  */
4415         type = cp_parser_type_id (parser);
4416         /* Look for the closing `>'.  */
4417         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4418         /* Restore the old message.  */
4419         parser->type_definition_forbidden_message = saved_message;
4420
4421         /* And the expression which is being cast.  */
4422         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4423         expression = cp_parser_expression (parser, /*cast_p=*/true);
4424         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4425
4426         /* Only type conversions to integral or enumeration types
4427            can be used in constant-expressions.  */
4428         if (!cast_valid_in_integral_constant_expression_p (type)
4429             && (cp_parser_non_integral_constant_expression
4430                 (parser,
4431                  "a cast to a type other than an integral or "
4432                  "enumeration type")))
4433           return error_mark_node;
4434
4435         switch (keyword)
4436           {
4437           case RID_DYNCAST:
4438             postfix_expression
4439               = build_dynamic_cast (type, expression, tf_warning_or_error);
4440             break;
4441           case RID_STATCAST:
4442             postfix_expression
4443               = build_static_cast (type, expression, tf_warning_or_error);
4444             break;
4445           case RID_REINTCAST:
4446             postfix_expression
4447               = build_reinterpret_cast (type, expression, 
4448                                         tf_warning_or_error);
4449             break;
4450           case RID_CONSTCAST:
4451             postfix_expression
4452               = build_const_cast (type, expression, tf_warning_or_error);
4453             break;
4454           default:
4455             gcc_unreachable ();
4456           }
4457       }
4458       break;
4459
4460     case RID_TYPEID:
4461       {
4462         tree type;
4463         const char *saved_message;
4464         bool saved_in_type_id_in_expr_p;
4465
4466         /* Consume the `typeid' token.  */
4467         cp_lexer_consume_token (parser->lexer);
4468         /* Look for the `(' token.  */
4469         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470         /* Types cannot be defined in a `typeid' expression.  */
4471         saved_message = parser->type_definition_forbidden_message;
4472         parser->type_definition_forbidden_message
4473           = "types may not be defined in a %<typeid%> expression";
4474         /* We can't be sure yet whether we're looking at a type-id or an
4475            expression.  */
4476         cp_parser_parse_tentatively (parser);
4477         /* Try a type-id first.  */
4478         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4479         parser->in_type_id_in_expr_p = true;
4480         type = cp_parser_type_id (parser);
4481         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4482         /* Look for the `)' token.  Otherwise, we can't be sure that
4483            we're not looking at an expression: consider `typeid (int
4484            (3))', for example.  */
4485         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4486         /* If all went well, simply lookup the type-id.  */
4487         if (cp_parser_parse_definitely (parser))
4488           postfix_expression = get_typeid (type);
4489         /* Otherwise, fall back to the expression variant.  */
4490         else
4491           {
4492             tree expression;
4493
4494             /* Look for an expression.  */
4495             expression = cp_parser_expression (parser, /*cast_p=*/false);
4496             /* Compute its typeid.  */
4497             postfix_expression = build_typeid (expression);
4498             /* Look for the `)' token.  */
4499             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4500           }
4501         /* Restore the saved message.  */
4502         parser->type_definition_forbidden_message = saved_message;
4503         /* `typeid' may not appear in an integral constant expression.  */
4504         if (cp_parser_non_integral_constant_expression(parser,
4505                                                        "%<typeid%> operator"))
4506           return error_mark_node;
4507       }
4508       break;
4509
4510     case RID_TYPENAME:
4511       {
4512         tree type;
4513         /* The syntax permitted here is the same permitted for an
4514            elaborated-type-specifier.  */
4515         type = cp_parser_elaborated_type_specifier (parser,
4516                                                     /*is_friend=*/false,
4517                                                     /*is_declaration=*/false);
4518         postfix_expression = cp_parser_functional_cast (parser, type);
4519       }
4520       break;
4521
4522     default:
4523       {
4524         tree type;
4525
4526         /* If the next thing is a simple-type-specifier, we may be
4527            looking at a functional cast.  We could also be looking at
4528            an id-expression.  So, we try the functional cast, and if
4529            that doesn't work we fall back to the primary-expression.  */
4530         cp_parser_parse_tentatively (parser);
4531         /* Look for the simple-type-specifier.  */
4532         type = cp_parser_simple_type_specifier (parser,
4533                                                 /*decl_specs=*/NULL,
4534                                                 CP_PARSER_FLAGS_NONE);
4535         /* Parse the cast itself.  */
4536         if (!cp_parser_error_occurred (parser))
4537           postfix_expression
4538             = cp_parser_functional_cast (parser, type);
4539         /* If that worked, we're done.  */
4540         if (cp_parser_parse_definitely (parser))
4541           break;
4542
4543         /* If the functional-cast didn't work out, try a
4544            compound-literal.  */
4545         if (cp_parser_allow_gnu_extensions_p (parser)
4546             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4547           {
4548             VEC(constructor_elt,gc) *initializer_list = NULL;
4549             bool saved_in_type_id_in_expr_p;
4550
4551             cp_parser_parse_tentatively (parser);
4552             /* Consume the `('.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Parse the type.  */
4555             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4556             parser->in_type_id_in_expr_p = true;
4557             type = cp_parser_type_id (parser);
4558             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4559             /* Look for the `)'.  */
4560             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4561             /* Look for the `{'.  */
4562             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4563             /* If things aren't going well, there's no need to
4564                keep going.  */
4565             if (!cp_parser_error_occurred (parser))
4566               {
4567                 bool non_constant_p;
4568                 /* Parse the initializer-list.  */
4569                 initializer_list
4570                   = cp_parser_initializer_list (parser, &non_constant_p);
4571                 /* Allow a trailing `,'.  */
4572                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4573                   cp_lexer_consume_token (parser->lexer);
4574                 /* Look for the final `}'.  */
4575                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4576               }
4577             /* If that worked, we're definitely looking at a
4578                compound-literal expression.  */
4579             if (cp_parser_parse_definitely (parser))
4580               {
4581                 /* Warn the user that a compound literal is not
4582                    allowed in standard C++.  */
4583                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4584                 /* For simplicity, we disallow compound literals in
4585                    constant-expressions.  We could
4586                    allow compound literals of integer type, whose
4587                    initializer was a constant, in constant
4588                    expressions.  Permitting that usage, as a further
4589                    extension, would not change the meaning of any
4590                    currently accepted programs.  (Of course, as
4591                    compound literals are not part of ISO C++, the
4592                    standard has nothing to say.)  */
4593                 if (cp_parser_non_integral_constant_expression 
4594                     (parser, "non-constant compound literals"))
4595                   {
4596                     postfix_expression = error_mark_node;
4597                     break;
4598                   }
4599                 /* Form the representation of the compound-literal.  */
4600                 postfix_expression
4601                   = (finish_compound_literal
4602                      (type, build_constructor (init_list_type_node,
4603                                                initializer_list)));
4604                 break;
4605               }
4606           }
4607
4608         /* It must be a primary-expression.  */
4609         postfix_expression
4610           = cp_parser_primary_expression (parser, address_p, cast_p,
4611                                           /*template_arg_p=*/false,
4612                                           &idk);
4613       }
4614       break;
4615     }
4616
4617   /* Keep looping until the postfix-expression is complete.  */
4618   while (true)
4619     {
4620       if (idk == CP_ID_KIND_UNQUALIFIED
4621           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4622           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4623         /* It is not a Koenig lookup function call.  */
4624         postfix_expression
4625           = unqualified_name_lookup_error (postfix_expression);
4626
4627       /* Peek at the next token.  */
4628       token = cp_lexer_peek_token (parser->lexer);
4629
4630       switch (token->type)
4631         {
4632         case CPP_OPEN_SQUARE:
4633           postfix_expression
4634             = cp_parser_postfix_open_square_expression (parser,
4635                                                         postfix_expression,
4636                                                         false);
4637           idk = CP_ID_KIND_NONE;
4638           is_member_access = false;
4639           break;
4640
4641         case CPP_OPEN_PAREN:
4642           /* postfix-expression ( expression-list [opt] ) */
4643           {
4644             bool koenig_p;
4645             bool is_builtin_constant_p;
4646             bool saved_integral_constant_expression_p = false;
4647             bool saved_non_integral_constant_expression_p = false;
4648             tree args;
4649
4650             is_member_access = false;
4651
4652             is_builtin_constant_p
4653               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4654             if (is_builtin_constant_p)
4655               {
4656                 /* The whole point of __builtin_constant_p is to allow
4657                    non-constant expressions to appear as arguments.  */
4658                 saved_integral_constant_expression_p
4659                   = parser->integral_constant_expression_p;
4660                 saved_non_integral_constant_expression_p
4661                   = parser->non_integral_constant_expression_p;
4662                 parser->integral_constant_expression_p = false;
4663               }
4664             args = (cp_parser_parenthesized_expression_list
4665                     (parser, /*is_attribute_list=*/false,
4666                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4667                      /*non_constant_p=*/NULL));
4668             if (is_builtin_constant_p)
4669               {
4670                 parser->integral_constant_expression_p
4671                   = saved_integral_constant_expression_p;
4672                 parser->non_integral_constant_expression_p
4673                   = saved_non_integral_constant_expression_p;
4674               }
4675
4676             if (args == error_mark_node)
4677               {
4678                 postfix_expression = error_mark_node;
4679                 break;
4680               }
4681
4682             /* Function calls are not permitted in
4683                constant-expressions.  */
4684             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4685                 && cp_parser_non_integral_constant_expression (parser,
4686                                                                "a function call"))
4687               {
4688                 postfix_expression = error_mark_node;
4689                 break;
4690               }
4691
4692             koenig_p = false;
4693             if (idk == CP_ID_KIND_UNQUALIFIED)
4694               {
4695                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4696                   {
4697                     if (args)
4698                       {
4699                         koenig_p = true;
4700                         postfix_expression
4701                           = perform_koenig_lookup (postfix_expression, args);
4702                       }
4703                     else
4704                       postfix_expression
4705                         = unqualified_fn_lookup_error (postfix_expression);
4706                   }
4707                 /* We do not perform argument-dependent lookup if
4708                    normal lookup finds a non-function, in accordance
4709                    with the expected resolution of DR 218.  */
4710                 else if (args && is_overloaded_fn (postfix_expression))
4711                   {
4712                     tree fn = get_first_fn (postfix_expression);
4713
4714                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4715                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4716
4717                     /* Only do argument dependent lookup if regular
4718                        lookup does not find a set of member functions.
4719                        [basic.lookup.koenig]/2a  */
4720                     if (!DECL_FUNCTION_MEMBER_P (fn))
4721                       {
4722                         koenig_p = true;
4723                         postfix_expression
4724                           = perform_koenig_lookup (postfix_expression, args);
4725                       }
4726                   }
4727               }
4728
4729             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4730               {
4731                 tree instance = TREE_OPERAND (postfix_expression, 0);
4732                 tree fn = TREE_OPERAND (postfix_expression, 1);
4733
4734                 if (processing_template_decl
4735                     && (type_dependent_expression_p (instance)
4736                         || (!BASELINK_P (fn)
4737                             && TREE_CODE (fn) != FIELD_DECL)
4738                         || type_dependent_expression_p (fn)
4739                         || any_type_dependent_arguments_p (args)))
4740                   {
4741                     postfix_expression
4742                       = build_nt_call_list (postfix_expression, args);
4743                     break;
4744                   }
4745
4746                 if (BASELINK_P (fn))
4747                   postfix_expression
4748                     = (build_new_method_call
4749                        (instance, fn, args, NULL_TREE,
4750                         (idk == CP_ID_KIND_QUALIFIED
4751                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4752                         /*fn_p=*/NULL,
4753                         tf_warning_or_error));
4754                 else
4755                   postfix_expression
4756                     = finish_call_expr (postfix_expression, args,
4757                                         /*disallow_virtual=*/false,
4758                                         /*koenig_p=*/false,
4759                                         tf_warning_or_error);
4760               }
4761             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4762                      || TREE_CODE (postfix_expression) == MEMBER_REF
4763                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4764               postfix_expression = (build_offset_ref_call_from_tree
4765                                     (postfix_expression, args));
4766             else if (idk == CP_ID_KIND_QUALIFIED)
4767               /* A call to a static class member, or a namespace-scope
4768                  function.  */
4769               postfix_expression
4770                 = finish_call_expr (postfix_expression, args,
4771                                     /*disallow_virtual=*/true,
4772                                     koenig_p,
4773                                     tf_warning_or_error);
4774             else
4775               /* All other function calls.  */
4776               postfix_expression
4777                 = finish_call_expr (postfix_expression, args,
4778                                     /*disallow_virtual=*/false,
4779                                     koenig_p,
4780                                     tf_warning_or_error);
4781
4782             if (warn_disallowed_functions)
4783               warn_if_disallowed_function_p (postfix_expression);
4784
4785             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4786             idk = CP_ID_KIND_NONE;
4787           }
4788           break;
4789
4790         case CPP_DOT:
4791         case CPP_DEREF:
4792           /* postfix-expression . template [opt] id-expression
4793              postfix-expression . pseudo-destructor-name
4794              postfix-expression -> template [opt] id-expression
4795              postfix-expression -> pseudo-destructor-name */
4796
4797           /* Consume the `.' or `->' operator.  */
4798           cp_lexer_consume_token (parser->lexer);
4799
4800           postfix_expression
4801             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4802                                                       postfix_expression,
4803                                                       false, &idk,
4804                                                       token->location);
4805
4806           is_member_access = true;
4807           break;
4808
4809         case CPP_PLUS_PLUS:
4810           /* postfix-expression ++  */
4811           /* Consume the `++' token.  */
4812           cp_lexer_consume_token (parser->lexer);
4813           /* Generate a representation for the complete expression.  */
4814           postfix_expression
4815             = finish_increment_expr (postfix_expression,
4816                                      POSTINCREMENT_EXPR);
4817           /* Increments may not appear in constant-expressions.  */
4818           if (cp_parser_non_integral_constant_expression (parser,
4819                                                           "an increment"))
4820             postfix_expression = error_mark_node;
4821           idk = CP_ID_KIND_NONE;
4822           is_member_access = false;
4823           break;
4824
4825         case CPP_MINUS_MINUS:
4826           /* postfix-expression -- */
4827           /* Consume the `--' token.  */
4828           cp_lexer_consume_token (parser->lexer);
4829           /* Generate a representation for the complete expression.  */
4830           postfix_expression
4831             = finish_increment_expr (postfix_expression,
4832                                      POSTDECREMENT_EXPR);
4833           /* Decrements may not appear in constant-expressions.  */
4834           if (cp_parser_non_integral_constant_expression (parser,
4835                                                           "a decrement"))
4836             postfix_expression = error_mark_node;
4837           idk = CP_ID_KIND_NONE;
4838           is_member_access = false;
4839           break;
4840
4841         default:
4842           if (member_access_only_p)
4843             return is_member_access? postfix_expression : error_mark_node;
4844           else
4845             return postfix_expression;
4846         }
4847     }
4848
4849   /* We should never get here.  */
4850   gcc_unreachable ();
4851   return error_mark_node;
4852 }
4853
4854 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4855    by cp_parser_builtin_offsetof.  We're looking for
4856
4857      postfix-expression [ expression ]
4858
4859    FOR_OFFSETOF is set if we're being called in that context, which
4860    changes how we deal with integer constant expressions.  */
4861
4862 static tree
4863 cp_parser_postfix_open_square_expression (cp_parser *parser,
4864                                           tree postfix_expression,
4865                                           bool for_offsetof)
4866 {
4867   tree index;
4868
4869   /* Consume the `[' token.  */
4870   cp_lexer_consume_token (parser->lexer);
4871
4872   /* Parse the index expression.  */
4873   /* ??? For offsetof, there is a question of what to allow here.  If
4874      offsetof is not being used in an integral constant expression context,
4875      then we *could* get the right answer by computing the value at runtime.
4876      If we are in an integral constant expression context, then we might
4877      could accept any constant expression; hard to say without analysis.
4878      Rather than open the barn door too wide right away, allow only integer
4879      constant expressions here.  */
4880   if (for_offsetof)
4881     index = cp_parser_constant_expression (parser, false, NULL);
4882   else
4883     index = cp_parser_expression (parser, /*cast_p=*/false);
4884
4885   /* Look for the closing `]'.  */
4886   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4887
4888   /* Build the ARRAY_REF.  */
4889   postfix_expression = grok_array_decl (postfix_expression, index);
4890
4891   /* When not doing offsetof, array references are not permitted in
4892      constant-expressions.  */
4893   if (!for_offsetof
4894       && (cp_parser_non_integral_constant_expression
4895           (parser, "an array reference")))
4896     postfix_expression = error_mark_node;
4897
4898   return postfix_expression;
4899 }
4900
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902    by cp_parser_builtin_offsetof.  We're looking for
4903
4904      postfix-expression . template [opt] id-expression
4905      postfix-expression . pseudo-destructor-name
4906      postfix-expression -> template [opt] id-expression
4907      postfix-expression -> pseudo-destructor-name
4908
4909    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4910    limits what of the above we'll actually accept, but nevermind.
4911    TOKEN_TYPE is the "." or "->" token, which will already have been
4912    removed from the stream.  */
4913
4914 static tree
4915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4916                                         enum cpp_ttype token_type,
4917                                         tree postfix_expression,
4918                                         bool for_offsetof, cp_id_kind *idk,
4919                                         location_t location)
4920 {
4921   tree name;
4922   bool dependent_p;
4923   bool pseudo_destructor_p;
4924   tree scope = NULL_TREE;
4925
4926   /* If this is a `->' operator, dereference the pointer.  */
4927   if (token_type == CPP_DEREF)
4928     postfix_expression = build_x_arrow (postfix_expression);
4929   /* Check to see whether or not the expression is type-dependent.  */
4930   dependent_p = type_dependent_expression_p (postfix_expression);
4931   /* The identifier following the `->' or `.' is not qualified.  */
4932   parser->scope = NULL_TREE;
4933   parser->qualifying_scope = NULL_TREE;
4934   parser->object_scope = NULL_TREE;
4935   *idk = CP_ID_KIND_NONE;
4936   /* Enter the scope corresponding to the type of the object
4937      given by the POSTFIX_EXPRESSION.  */
4938   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4939     {
4940       scope = TREE_TYPE (postfix_expression);
4941       /* According to the standard, no expression should ever have
4942          reference type.  Unfortunately, we do not currently match
4943          the standard in this respect in that our internal representation
4944          of an expression may have reference type even when the standard
4945          says it does not.  Therefore, we have to manually obtain the
4946          underlying type here.  */
4947       scope = non_reference (scope);
4948       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4949       if (scope == unknown_type_node)
4950         {
4951           error ("%H%qE does not have class type", &location, postfix_expression);
4952           scope = NULL_TREE;
4953         }
4954       else
4955         scope = complete_type_or_else (scope, NULL_TREE);
4956       /* Let the name lookup machinery know that we are processing a
4957          class member access expression.  */
4958       parser->context->object_type = scope;
4959       /* If something went wrong, we want to be able to discern that case,
4960          as opposed to the case where there was no SCOPE due to the type
4961          of expression being dependent.  */
4962       if (!scope)
4963         scope = error_mark_node;
4964       /* If the SCOPE was erroneous, make the various semantic analysis
4965          functions exit quickly -- and without issuing additional error
4966          messages.  */
4967       if (scope == error_mark_node)
4968         postfix_expression = error_mark_node;
4969     }
4970
4971   /* Assume this expression is not a pseudo-destructor access.  */
4972   pseudo_destructor_p = false;
4973
4974   /* If the SCOPE is a scalar type, then, if this is a valid program,
4975      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4976      is type dependent, it can be pseudo-destructor-name or something else.
4977      Try to parse it as pseudo-destructor-name first.  */
4978   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4979     {
4980       tree s;
4981       tree type;
4982
4983       cp_parser_parse_tentatively (parser);
4984       /* Parse the pseudo-destructor-name.  */
4985       s = NULL_TREE;
4986       cp_parser_pseudo_destructor_name (parser, &s, &type);
4987       if (dependent_p
4988           && (cp_parser_error_occurred (parser)
4989               || TREE_CODE (type) != TYPE_DECL
4990               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4991         cp_parser_abort_tentative_parse (parser);
4992       else if (cp_parser_parse_definitely (parser))
4993         {
4994           pseudo_destructor_p = true;
4995           postfix_expression
4996             = finish_pseudo_destructor_expr (postfix_expression,
4997                                              s, TREE_TYPE (type));
4998         }
4999     }
5000
5001   if (!pseudo_destructor_p)
5002     {
5003       /* If the SCOPE is not a scalar type, we are looking at an
5004          ordinary class member access expression, rather than a
5005          pseudo-destructor-name.  */
5006       bool template_p;
5007       cp_token *token = cp_lexer_peek_token (parser->lexer);
5008       /* Parse the id-expression.  */
5009       name = (cp_parser_id_expression
5010               (parser,
5011                cp_parser_optional_template_keyword (parser),
5012                /*check_dependency_p=*/true,
5013                &template_p,
5014                /*declarator_p=*/false,
5015                /*optional_p=*/false));
5016       /* In general, build a SCOPE_REF if the member name is qualified.
5017          However, if the name was not dependent and has already been
5018          resolved; there is no need to build the SCOPE_REF.  For example;
5019
5020              struct X { void f(); };
5021              template <typename T> void f(T* t) { t->X::f(); }
5022
5023          Even though "t" is dependent, "X::f" is not and has been resolved
5024          to a BASELINK; there is no need to include scope information.  */
5025
5026       /* But we do need to remember that there was an explicit scope for
5027          virtual function calls.  */
5028       if (parser->scope)
5029         *idk = CP_ID_KIND_QUALIFIED;
5030
5031       /* If the name is a template-id that names a type, we will get a
5032          TYPE_DECL here.  That is invalid code.  */
5033       if (TREE_CODE (name) == TYPE_DECL)
5034         {
5035           error ("%Hinvalid use of %qD", &token->location, name);
5036           postfix_expression = error_mark_node;
5037         }
5038       else
5039         {
5040           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5041             {
5042               name = build_qualified_name (/*type=*/NULL_TREE,
5043                                            parser->scope,
5044                                            name,
5045                                            template_p);
5046               parser->scope = NULL_TREE;
5047               parser->qualifying_scope = NULL_TREE;
5048               parser->object_scope = NULL_TREE;
5049             }
5050           if (scope && name && BASELINK_P (name))
5051             adjust_result_of_qualified_name_lookup
5052               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5053           postfix_expression
5054             = finish_class_member_access_expr (postfix_expression, name,
5055                                                template_p, 
5056                                                tf_warning_or_error);
5057         }
5058     }
5059
5060   /* We no longer need to look up names in the scope of the object on
5061      the left-hand side of the `.' or `->' operator.  */
5062   parser->context->object_type = NULL_TREE;
5063
5064   /* Outside of offsetof, these operators may not appear in
5065      constant-expressions.  */
5066   if (!for_offsetof
5067       && (cp_parser_non_integral_constant_expression
5068           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5069     postfix_expression = error_mark_node;
5070
5071   return postfix_expression;
5072 }
5073
5074 /* Parse a parenthesized expression-list.
5075
5076    expression-list:
5077      assignment-expression
5078      expression-list, assignment-expression
5079
5080    attribute-list:
5081      expression-list
5082      identifier
5083      identifier, expression-list
5084
5085    CAST_P is true if this expression is the target of a cast.
5086
5087    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5088    argument pack.
5089
5090    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5091    representation of an assignment-expression.  Note that a TREE_LIST
5092    is returned even if there is only a single expression in the list.
5093    error_mark_node is returned if the ( and or ) are
5094    missing. NULL_TREE is returned on no expressions. The parentheses
5095    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5096    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5097    indicates whether or not all of the expressions in the list were
5098    constant.  */
5099
5100 static tree
5101 cp_parser_parenthesized_expression_list (cp_parser* parser,
5102                                          bool is_attribute_list,
5103                                          bool cast_p,
5104                                          bool allow_expansion_p,
5105                                          bool *non_constant_p)
5106 {
5107   tree expression_list = NULL_TREE;
5108   bool fold_expr_p = is_attribute_list;
5109   tree identifier = NULL_TREE;
5110   bool saved_greater_than_is_operator_p;
5111
5112   /* Assume all the expressions will be constant.  */
5113   if (non_constant_p)
5114     *non_constant_p = false;
5115
5116   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5117     return error_mark_node;
5118
5119   /* Within a parenthesized expression, a `>' token is always
5120      the greater-than operator.  */
5121   saved_greater_than_is_operator_p
5122     = parser->greater_than_is_operator_p;
5123   parser->greater_than_is_operator_p = true;
5124
5125   /* Consume expressions until there are no more.  */
5126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5127     while (true)
5128       {
5129         tree expr;
5130
5131         /* At the beginning of attribute lists, check to see if the
5132            next token is an identifier.  */
5133         if (is_attribute_list
5134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5135           {
5136             cp_token *token;
5137
5138             /* Consume the identifier.  */
5139             token = cp_lexer_consume_token (parser->lexer);
5140             /* Save the identifier.  */
5141             identifier = token->u.value;
5142           }
5143         else
5144           {
5145             bool expr_non_constant_p;
5146
5147             /* Parse the next assignment-expression.  */
5148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5149               {
5150                 /* A braced-init-list.  */
5151                 maybe_warn_cpp0x ("extended initializer lists");
5152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5153                 if (non_constant_p && expr_non_constant_p)
5154                   *non_constant_p = true;
5155               }
5156             else if (non_constant_p)
5157               {
5158                 expr = (cp_parser_constant_expression
5159                         (parser, /*allow_non_constant_p=*/true,
5160                          &expr_non_constant_p));
5161                 if (expr_non_constant_p)
5162                   *non_constant_p = true;
5163               }
5164             else
5165               expr = cp_parser_assignment_expression (parser, cast_p);
5166
5167             if (fold_expr_p)
5168               expr = fold_non_dependent_expr (expr);
5169
5170             /* If we have an ellipsis, then this is an expression
5171                expansion.  */
5172             if (allow_expansion_p
5173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5174               {
5175                 /* Consume the `...'.  */
5176                 cp_lexer_consume_token (parser->lexer);
5177
5178                 /* Build the argument pack.  */
5179                 expr = make_pack_expansion (expr);
5180               }
5181
5182              /* Add it to the list.  We add error_mark_node
5183                 expressions to the list, so that we can still tell if
5184                 the correct form for a parenthesized expression-list
5185                 is found. That gives better errors.  */
5186             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5187
5188             if (expr == error_mark_node)
5189               goto skip_comma;
5190           }
5191
5192         /* After the first item, attribute lists look the same as
5193            expression lists.  */
5194         is_attribute_list = false;
5195
5196       get_comma:;
5197         /* If the next token isn't a `,', then we are done.  */
5198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5199           break;
5200
5201         /* Otherwise, consume the `,' and keep going.  */
5202         cp_lexer_consume_token (parser->lexer);
5203       }
5204
5205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5206     {
5207       int ending;
5208
5209     skip_comma:;
5210       /* We try and resync to an unnested comma, as that will give the
5211          user better diagnostics.  */
5212       ending = cp_parser_skip_to_closing_parenthesis (parser,
5213                                                       /*recovering=*/true,
5214                                                       /*or_comma=*/true,
5215                                                       /*consume_paren=*/true);
5216       if (ending < 0)
5217         goto get_comma;
5218       if (!ending)
5219         {
5220           parser->greater_than_is_operator_p
5221             = saved_greater_than_is_operator_p;
5222           return error_mark_node;
5223         }
5224     }
5225
5226   parser->greater_than_is_operator_p
5227     = saved_greater_than_is_operator_p;
5228
5229   /* We built up the list in reverse order so we must reverse it now.  */
5230   expression_list = nreverse (expression_list);
5231   if (identifier)
5232     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5233
5234   return expression_list;
5235 }
5236
5237 /* Parse a pseudo-destructor-name.
5238
5239    pseudo-destructor-name:
5240      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5241      :: [opt] nested-name-specifier template template-id :: ~ type-name
5242      :: [opt] nested-name-specifier [opt] ~ type-name
5243
5244    If either of the first two productions is used, sets *SCOPE to the
5245    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5246    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5247    or ERROR_MARK_NODE if the parse fails.  */
5248
5249 static void
5250 cp_parser_pseudo_destructor_name (cp_parser* parser,
5251                                   tree* scope,
5252                                   tree* type)
5253 {
5254   bool nested_name_specifier_p;
5255
5256   /* Assume that things will not work out.  */
5257   *type = error_mark_node;
5258
5259   /* Look for the optional `::' operator.  */
5260   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5261   /* Look for the optional nested-name-specifier.  */
5262   nested_name_specifier_p
5263     = (cp_parser_nested_name_specifier_opt (parser,
5264                                             /*typename_keyword_p=*/false,
5265                                             /*check_dependency_p=*/true,
5266                                             /*type_p=*/false,
5267                                             /*is_declaration=*/true)
5268        != NULL_TREE);
5269   /* Now, if we saw a nested-name-specifier, we might be doing the
5270      second production.  */
5271   if (nested_name_specifier_p
5272       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5273     {
5274       /* Consume the `template' keyword.  */
5275       cp_lexer_consume_token (parser->lexer);
5276       /* Parse the template-id.  */
5277       cp_parser_template_id (parser,
5278                              /*template_keyword_p=*/true,
5279                              /*check_dependency_p=*/false,
5280                              /*is_declaration=*/true);
5281       /* Look for the `::' token.  */
5282       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5283     }
5284   /* If the next token is not a `~', then there might be some
5285      additional qualification.  */
5286   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5287     {
5288       /* At this point, we're looking for "type-name :: ~".  The type-name
5289          must not be a class-name, since this is a pseudo-destructor.  So,
5290          it must be either an enum-name, or a typedef-name -- both of which
5291          are just identifiers.  So, we peek ahead to check that the "::"
5292          and "~" tokens are present; if they are not, then we can avoid
5293          calling type_name.  */
5294       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5295           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5296           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5297         {
5298           cp_parser_error (parser, "non-scalar type");
5299           return;
5300         }
5301
5302       /* Look for the type-name.  */
5303       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5304       if (*scope == error_mark_node)
5305         return;
5306
5307       /* Look for the `::' token.  */
5308       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5309     }
5310   else
5311     *scope = NULL_TREE;
5312
5313   /* Look for the `~'.  */
5314   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5315   /* Look for the type-name again.  We are not responsible for
5316      checking that it matches the first type-name.  */
5317   *type = cp_parser_nonclass_name (parser);
5318 }
5319
5320 /* Parse a unary-expression.
5321
5322    unary-expression:
5323      postfix-expression
5324      ++ cast-expression
5325      -- cast-expression
5326      unary-operator cast-expression
5327      sizeof unary-expression
5328      sizeof ( type-id )
5329      new-expression
5330      delete-expression
5331
5332    GNU Extensions:
5333
5334    unary-expression:
5335      __extension__ cast-expression
5336      __alignof__ unary-expression
5337      __alignof__ ( type-id )
5338      __real__ cast-expression
5339      __imag__ cast-expression
5340      && identifier
5341
5342    ADDRESS_P is true iff the unary-expression is appearing as the
5343    operand of the `&' operator.   CAST_P is true if this expression is
5344    the target of a cast.
5345
5346    Returns a representation of the expression.  */
5347
5348 static tree
5349 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5350 {
5351   cp_token *token;
5352   enum tree_code unary_operator;
5353
5354   /* Peek at the next token.  */
5355   token = cp_lexer_peek_token (parser->lexer);
5356   /* Some keywords give away the kind of expression.  */
5357   if (token->type == CPP_KEYWORD)
5358     {
5359       enum rid keyword = token->keyword;
5360
5361       switch (keyword)
5362         {
5363         case RID_ALIGNOF:
5364         case RID_SIZEOF:
5365           {
5366             tree operand;
5367             enum tree_code op;
5368
5369             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5370             /* Consume the token.  */
5371             cp_lexer_consume_token (parser->lexer);
5372             /* Parse the operand.  */
5373             operand = cp_parser_sizeof_operand (parser, keyword);
5374
5375             if (TYPE_P (operand))
5376               return cxx_sizeof_or_alignof_type (operand, op, true);
5377             else
5378               return cxx_sizeof_or_alignof_expr (operand, op, true);
5379           }
5380
5381         case RID_NEW:
5382           return cp_parser_new_expression (parser);
5383
5384         case RID_DELETE:
5385           return cp_parser_delete_expression (parser);
5386
5387         case RID_EXTENSION:
5388           {
5389             /* The saved value of the PEDANTIC flag.  */
5390             int saved_pedantic;
5391             tree expr;
5392
5393             /* Save away the PEDANTIC flag.  */
5394             cp_parser_extension_opt (parser, &saved_pedantic);
5395             /* Parse the cast-expression.  */
5396             expr = cp_parser_simple_cast_expression (parser);
5397             /* Restore the PEDANTIC flag.  */
5398             pedantic = saved_pedantic;
5399
5400             return expr;
5401           }
5402
5403         case RID_REALPART:
5404         case RID_IMAGPART:
5405           {
5406             tree expression;
5407
5408             /* Consume the `__real__' or `__imag__' token.  */
5409             cp_lexer_consume_token (parser->lexer);
5410             /* Parse the cast-expression.  */
5411             expression = cp_parser_simple_cast_expression (parser);
5412             /* Create the complete representation.  */
5413             return build_x_unary_op ((keyword == RID_REALPART
5414                                       ? REALPART_EXPR : IMAGPART_EXPR),
5415                                      expression,
5416                                      tf_warning_or_error);
5417           }
5418           break;
5419
5420         default:
5421           break;
5422         }
5423     }
5424
5425   /* Look for the `:: new' and `:: delete', which also signal the
5426      beginning of a new-expression, or delete-expression,
5427      respectively.  If the next token is `::', then it might be one of
5428      these.  */
5429   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5430     {
5431       enum rid keyword;
5432
5433       /* See if the token after the `::' is one of the keywords in
5434          which we're interested.  */
5435       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5436       /* If it's `new', we have a new-expression.  */
5437       if (keyword == RID_NEW)
5438         return cp_parser_new_expression (parser);
5439       /* Similarly, for `delete'.  */
5440       else if (keyword == RID_DELETE)
5441         return cp_parser_delete_expression (parser);
5442     }
5443
5444   /* Look for a unary operator.  */
5445   unary_operator = cp_parser_unary_operator (token);
5446   /* The `++' and `--' operators can be handled similarly, even though
5447      they are not technically unary-operators in the grammar.  */
5448   if (unary_operator == ERROR_MARK)
5449     {
5450       if (token->type == CPP_PLUS_PLUS)
5451         unary_operator = PREINCREMENT_EXPR;
5452       else if (token->type == CPP_MINUS_MINUS)
5453         unary_operator = PREDECREMENT_EXPR;
5454       /* Handle the GNU address-of-label extension.  */
5455       else if (cp_parser_allow_gnu_extensions_p (parser)
5456                && token->type == CPP_AND_AND)
5457         {
5458           tree identifier;
5459           tree expression;
5460           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5461
5462           /* Consume the '&&' token.  */
5463           cp_lexer_consume_token (parser->lexer);
5464           /* Look for the identifier.  */
5465           identifier = cp_parser_identifier (parser);
5466           /* Create an expression representing the address.  */
5467           expression = finish_label_address_expr (identifier, loc);
5468           if (cp_parser_non_integral_constant_expression (parser,
5469                                                 "the address of a label"))
5470             expression = error_mark_node;
5471           return expression;
5472         }
5473     }
5474   if (unary_operator != ERROR_MARK)
5475     {
5476       tree cast_expression;
5477       tree expression = error_mark_node;
5478       const char *non_constant_p = NULL;
5479
5480       /* Consume the operator token.  */
5481       token = cp_lexer_consume_token (parser->lexer);
5482       /* Parse the cast-expression.  */
5483       cast_expression
5484         = cp_parser_cast_expression (parser,
5485                                      unary_operator == ADDR_EXPR,
5486                                      /*cast_p=*/false);
5487       /* Now, build an appropriate representation.  */
5488       switch (unary_operator)
5489         {
5490         case INDIRECT_REF:
5491           non_constant_p = "%<*%>";
5492           expression = build_x_indirect_ref (cast_expression, "unary *",
5493                                              tf_warning_or_error);
5494           break;
5495
5496         case ADDR_EXPR:
5497           non_constant_p = "%<&%>";
5498           /* Fall through.  */
5499         case BIT_NOT_EXPR:
5500           expression = build_x_unary_op (unary_operator, cast_expression,
5501                                          tf_warning_or_error);
5502           break;
5503
5504         case PREINCREMENT_EXPR:
5505         case PREDECREMENT_EXPR:
5506           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5507                             ? "%<++%>" : "%<--%>");
5508           /* Fall through.  */
5509         case UNARY_PLUS_EXPR:
5510         case NEGATE_EXPR:
5511         case TRUTH_NOT_EXPR:
5512           expression = finish_unary_op_expr (unary_operator, cast_expression);
5513           break;
5514
5515         default:
5516           gcc_unreachable ();
5517         }
5518
5519       if (non_constant_p
5520           && cp_parser_non_integral_constant_expression (parser,
5521                                                          non_constant_p))
5522         expression = error_mark_node;
5523
5524       return expression;
5525     }
5526
5527   return cp_parser_postfix_expression (parser, address_p, cast_p,
5528                                        /*member_access_only_p=*/false);
5529 }
5530
5531 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5532    unary-operator, the corresponding tree code is returned.  */
5533
5534 static enum tree_code
5535 cp_parser_unary_operator (cp_token* token)
5536 {
5537   switch (token->type)
5538     {
5539     case CPP_MULT:
5540       return INDIRECT_REF;
5541
5542     case CPP_AND:
5543       return ADDR_EXPR;
5544
5545     case CPP_PLUS:
5546       return UNARY_PLUS_EXPR;
5547
5548     case CPP_MINUS:
5549       return NEGATE_EXPR;
5550
5551     case CPP_NOT:
5552       return TRUTH_NOT_EXPR;
5553
5554     case CPP_COMPL:
5555       return BIT_NOT_EXPR;
5556
5557     default:
5558       return ERROR_MARK;
5559     }
5560 }
5561
5562 /* Parse a new-expression.
5563
5564    new-expression:
5565      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5566      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5567
5568    Returns a representation of the expression.  */
5569
5570 static tree
5571 cp_parser_new_expression (cp_parser* parser)
5572 {
5573   bool global_scope_p;
5574   tree placement;
5575   tree type;
5576   tree initializer;
5577   tree nelts;
5578
5579   /* Look for the optional `::' operator.  */
5580   global_scope_p
5581     = (cp_parser_global_scope_opt (parser,
5582                                    /*current_scope_valid_p=*/false)
5583        != NULL_TREE);
5584   /* Look for the `new' operator.  */
5585   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5586   /* There's no easy way to tell a new-placement from the
5587      `( type-id )' construct.  */
5588   cp_parser_parse_tentatively (parser);
5589   /* Look for a new-placement.  */
5590   placement = cp_parser_new_placement (parser);
5591   /* If that didn't work out, there's no new-placement.  */
5592   if (!cp_parser_parse_definitely (parser))
5593     placement = NULL_TREE;
5594
5595   /* If the next token is a `(', then we have a parenthesized
5596      type-id.  */
5597   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5598     {
5599       cp_token *token;
5600       /* Consume the `('.  */
5601       cp_lexer_consume_token (parser->lexer);
5602       /* Parse the type-id.  */
5603       type = cp_parser_type_id (parser);
5604       /* Look for the closing `)'.  */
5605       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5606       token = cp_lexer_peek_token (parser->lexer);
5607       /* There should not be a direct-new-declarator in this production,
5608          but GCC used to allowed this, so we check and emit a sensible error
5609          message for this case.  */
5610       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5611         {
5612           error ("%Harray bound forbidden after parenthesized type-id",
5613                  &token->location);
5614           inform (token->location, 
5615                   "try removing the parentheses around the type-id");
5616           cp_parser_direct_new_declarator (parser);
5617         }
5618       nelts = NULL_TREE;
5619     }
5620   /* Otherwise, there must be a new-type-id.  */
5621   else
5622     type = cp_parser_new_type_id (parser, &nelts);
5623
5624   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5625   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5626       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5627     initializer = cp_parser_new_initializer (parser);
5628   else
5629     initializer = NULL_TREE;
5630
5631   /* A new-expression may not appear in an integral constant
5632      expression.  */
5633   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5634     return error_mark_node;
5635
5636   /* Create a representation of the new-expression.  */
5637   return build_new (placement, type, nelts, initializer, global_scope_p,
5638                     tf_warning_or_error);
5639 }
5640
5641 /* Parse a new-placement.
5642
5643    new-placement:
5644      ( expression-list )
5645
5646    Returns the same representation as for an expression-list.  */
5647
5648 static tree
5649 cp_parser_new_placement (cp_parser* parser)
5650 {
5651   tree expression_list;
5652
5653   /* Parse the expression-list.  */
5654   expression_list = (cp_parser_parenthesized_expression_list
5655                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5656                       /*non_constant_p=*/NULL));
5657
5658   return expression_list;
5659 }
5660
5661 /* Parse a new-type-id.
5662
5663    new-type-id:
5664      type-specifier-seq new-declarator [opt]
5665
5666    Returns the TYPE allocated.  If the new-type-id indicates an array
5667    type, *NELTS is set to the number of elements in the last array
5668    bound; the TYPE will not include the last array bound.  */
5669
5670 static tree
5671 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5672 {
5673   cp_decl_specifier_seq type_specifier_seq;
5674   cp_declarator *new_declarator;
5675   cp_declarator *declarator;
5676   cp_declarator *outer_declarator;
5677   const char *saved_message;
5678   tree type;
5679
5680   /* The type-specifier sequence must not contain type definitions.
5681      (It cannot contain declarations of new types either, but if they
5682      are not definitions we will catch that because they are not
5683      complete.)  */
5684   saved_message = parser->type_definition_forbidden_message;
5685   parser->type_definition_forbidden_message
5686     = "types may not be defined in a new-type-id";
5687   /* Parse the type-specifier-seq.  */
5688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5689                                 &type_specifier_seq);
5690   /* Restore the old message.  */
5691   parser->type_definition_forbidden_message = saved_message;
5692   /* Parse the new-declarator.  */
5693   new_declarator = cp_parser_new_declarator_opt (parser);
5694
5695   /* Determine the number of elements in the last array dimension, if
5696      any.  */
5697   *nelts = NULL_TREE;
5698   /* Skip down to the last array dimension.  */
5699   declarator = new_declarator;
5700   outer_declarator = NULL;
5701   while (declarator && (declarator->kind == cdk_pointer
5702                         || declarator->kind == cdk_ptrmem))
5703     {
5704       outer_declarator = declarator;
5705       declarator = declarator->declarator;
5706     }
5707   while (declarator
5708          && declarator->kind == cdk_array
5709          && declarator->declarator
5710          && declarator->declarator->kind == cdk_array)
5711     {
5712       outer_declarator = declarator;
5713       declarator = declarator->declarator;
5714     }
5715
5716   if (declarator && declarator->kind == cdk_array)
5717     {
5718       *nelts = declarator->u.array.bounds;
5719       if (*nelts == error_mark_node)
5720         *nelts = integer_one_node;
5721
5722       if (outer_declarator)
5723         outer_declarator->declarator = declarator->declarator;
5724       else
5725         new_declarator = NULL;
5726     }
5727
5728   type = groktypename (&type_specifier_seq, new_declarator);
5729   return type;
5730 }
5731
5732 /* Parse an (optional) new-declarator.
5733
5734    new-declarator:
5735      ptr-operator new-declarator [opt]
5736      direct-new-declarator
5737
5738    Returns the declarator.  */
5739
5740 static cp_declarator *
5741 cp_parser_new_declarator_opt (cp_parser* parser)
5742 {
5743   enum tree_code code;
5744   tree type;
5745   cp_cv_quals cv_quals;
5746
5747   /* We don't know if there's a ptr-operator next, or not.  */
5748   cp_parser_parse_tentatively (parser);
5749   /* Look for a ptr-operator.  */
5750   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5751   /* If that worked, look for more new-declarators.  */
5752   if (cp_parser_parse_definitely (parser))
5753     {
5754       cp_declarator *declarator;
5755
5756       /* Parse another optional declarator.  */
5757       declarator = cp_parser_new_declarator_opt (parser);
5758
5759       return cp_parser_make_indirect_declarator
5760         (code, type, cv_quals, declarator);
5761     }
5762
5763   /* If the next token is a `[', there is a direct-new-declarator.  */
5764   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5765     return cp_parser_direct_new_declarator (parser);
5766
5767   return NULL;
5768 }
5769
5770 /* Parse a direct-new-declarator.
5771
5772    direct-new-declarator:
5773      [ expression ]
5774      direct-new-declarator [constant-expression]
5775
5776    */
5777
5778 static cp_declarator *
5779 cp_parser_direct_new_declarator (cp_parser* parser)
5780 {
5781   cp_declarator *declarator = NULL;
5782
5783   while (true)
5784     {
5785       tree expression;
5786
5787       /* Look for the opening `['.  */
5788       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5789       /* The first expression is not required to be constant.  */
5790       if (!declarator)
5791         {
5792           cp_token *token = cp_lexer_peek_token (parser->lexer);
5793           expression = cp_parser_expression (parser, /*cast_p=*/false);
5794           /* The standard requires that the expression have integral
5795              type.  DR 74 adds enumeration types.  We believe that the
5796              real intent is that these expressions be handled like the
5797              expression in a `switch' condition, which also allows
5798              classes with a single conversion to integral or
5799              enumeration type.  */
5800           if (!processing_template_decl)
5801             {
5802               expression
5803                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5804                                               expression,
5805                                               /*complain=*/true);
5806               if (!expression)
5807                 {
5808                   error ("%Hexpression in new-declarator must have integral "
5809                          "or enumeration type", &token->location);
5810                   expression = error_mark_node;
5811                 }
5812             }
5813         }
5814       /* But all the other expressions must be.  */
5815       else
5816         expression
5817           = cp_parser_constant_expression (parser,
5818                                            /*allow_non_constant=*/false,
5819                                            NULL);
5820       /* Look for the closing `]'.  */
5821       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5822
5823       /* Add this bound to the declarator.  */
5824       declarator = make_array_declarator (declarator, expression);
5825
5826       /* If the next token is not a `[', then there are no more
5827          bounds.  */
5828       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5829         break;
5830     }
5831
5832   return declarator;
5833 }
5834
5835 /* Parse a new-initializer.
5836
5837    new-initializer:
5838      ( expression-list [opt] )
5839      braced-init-list
5840
5841    Returns a representation of the expression-list.  If there is no
5842    expression-list, VOID_ZERO_NODE is returned.  */
5843
5844 static tree
5845 cp_parser_new_initializer (cp_parser* parser)
5846 {
5847   tree expression_list;
5848
5849   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5850     {
5851       bool expr_non_constant_p;
5852       maybe_warn_cpp0x ("extended initializer lists");
5853       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5854       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5855       expression_list = build_tree_list (NULL_TREE, expression_list);
5856     }
5857   else
5858     expression_list = (cp_parser_parenthesized_expression_list
5859                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5860                         /*non_constant_p=*/NULL));
5861   if (!expression_list)
5862     expression_list = void_zero_node;
5863
5864   return expression_list;
5865 }
5866
5867 /* Parse a delete-expression.
5868
5869    delete-expression:
5870      :: [opt] delete cast-expression
5871      :: [opt] delete [ ] cast-expression
5872
5873    Returns a representation of the expression.  */
5874
5875 static tree
5876 cp_parser_delete_expression (cp_parser* parser)
5877 {
5878   bool global_scope_p;
5879   bool array_p;
5880   tree expression;
5881
5882   /* Look for the optional `::' operator.  */
5883   global_scope_p
5884     = (cp_parser_global_scope_opt (parser,
5885                                    /*current_scope_valid_p=*/false)
5886        != NULL_TREE);
5887   /* Look for the `delete' keyword.  */
5888   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5889   /* See if the array syntax is in use.  */
5890   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5891     {
5892       /* Consume the `[' token.  */
5893       cp_lexer_consume_token (parser->lexer);
5894       /* Look for the `]' token.  */
5895       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5896       /* Remember that this is the `[]' construct.  */
5897       array_p = true;
5898     }
5899   else
5900     array_p = false;
5901
5902   /* Parse the cast-expression.  */
5903   expression = cp_parser_simple_cast_expression (parser);
5904
5905   /* A delete-expression may not appear in an integral constant
5906      expression.  */
5907   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5908     return error_mark_node;
5909
5910   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5911 }
5912
5913 /* Parse a cast-expression.
5914
5915    cast-expression:
5916      unary-expression
5917      ( type-id ) cast-expression
5918
5919    ADDRESS_P is true iff the unary-expression is appearing as the
5920    operand of the `&' operator.   CAST_P is true if this expression is
5921    the target of a cast.
5922
5923    Returns a representation of the expression.  */
5924
5925 static tree
5926 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5927 {
5928   /* If it's a `(', then we might be looking at a cast.  */
5929   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5930     {
5931       tree type = NULL_TREE;
5932       tree expr = NULL_TREE;
5933       bool compound_literal_p;
5934       const char *saved_message;
5935
5936       /* There's no way to know yet whether or not this is a cast.
5937          For example, `(int (3))' is a unary-expression, while `(int)
5938          3' is a cast.  So, we resort to parsing tentatively.  */
5939       cp_parser_parse_tentatively (parser);
5940       /* Types may not be defined in a cast.  */
5941       saved_message = parser->type_definition_forbidden_message;
5942       parser->type_definition_forbidden_message
5943         = "types may not be defined in casts";
5944       /* Consume the `('.  */
5945       cp_lexer_consume_token (parser->lexer);
5946       /* A very tricky bit is that `(struct S) { 3 }' is a
5947          compound-literal (which we permit in C++ as an extension).
5948          But, that construct is not a cast-expression -- it is a
5949          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5950          is legal; if the compound-literal were a cast-expression,
5951          you'd need an extra set of parentheses.)  But, if we parse
5952          the type-id, and it happens to be a class-specifier, then we
5953          will commit to the parse at that point, because we cannot
5954          undo the action that is done when creating a new class.  So,
5955          then we cannot back up and do a postfix-expression.
5956
5957          Therefore, we scan ahead to the closing `)', and check to see
5958          if the token after the `)' is a `{'.  If so, we are not
5959          looking at a cast-expression.
5960
5961          Save tokens so that we can put them back.  */
5962       cp_lexer_save_tokens (parser->lexer);
5963       /* Skip tokens until the next token is a closing parenthesis.
5964          If we find the closing `)', and the next token is a `{', then
5965          we are looking at a compound-literal.  */
5966       compound_literal_p
5967         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5968                                                   /*consume_paren=*/true)
5969            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5970       /* Roll back the tokens we skipped.  */
5971       cp_lexer_rollback_tokens (parser->lexer);
5972       /* If we were looking at a compound-literal, simulate an error
5973          so that the call to cp_parser_parse_definitely below will
5974          fail.  */
5975       if (compound_literal_p)
5976         cp_parser_simulate_error (parser);
5977       else
5978         {
5979           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5980           parser->in_type_id_in_expr_p = true;
5981           /* Look for the type-id.  */
5982           type = cp_parser_type_id (parser);
5983           /* Look for the closing `)'.  */
5984           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5985           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5986         }
5987
5988       /* Restore the saved message.  */
5989       parser->type_definition_forbidden_message = saved_message;
5990
5991       /* If ok so far, parse the dependent expression. We cannot be
5992          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5993          ctor of T, but looks like a cast to function returning T
5994          without a dependent expression.  */
5995       if (!cp_parser_error_occurred (parser))
5996         expr = cp_parser_cast_expression (parser,
5997                                           /*address_p=*/false,
5998                                           /*cast_p=*/true);
5999
6000       if (cp_parser_parse_definitely (parser))
6001         {
6002           /* Warn about old-style casts, if so requested.  */
6003           if (warn_old_style_cast
6004               && !in_system_header
6005               && !VOID_TYPE_P (type)
6006               && current_lang_name != lang_name_c)
6007             warning (OPT_Wold_style_cast, "use of old-style cast");
6008
6009           /* Only type conversions to integral or enumeration types
6010              can be used in constant-expressions.  */
6011           if (!cast_valid_in_integral_constant_expression_p (type)
6012               && (cp_parser_non_integral_constant_expression
6013                   (parser,
6014                    "a cast to a type other than an integral or "
6015                    "enumeration type")))
6016             return error_mark_node;
6017
6018           /* Perform the cast.  */
6019           expr = build_c_cast (type, expr);
6020           return expr;
6021         }
6022     }
6023
6024   /* If we get here, then it's not a cast, so it must be a
6025      unary-expression.  */
6026   return cp_parser_unary_expression (parser, address_p, cast_p);
6027 }
6028
6029 /* Parse a binary expression of the general form:
6030
6031    pm-expression:
6032      cast-expression
6033      pm-expression .* cast-expression
6034      pm-expression ->* cast-expression
6035
6036    multiplicative-expression:
6037      pm-expression
6038      multiplicative-expression * pm-expression
6039      multiplicative-expression / pm-expression
6040      multiplicative-expression % pm-expression
6041
6042    additive-expression:
6043      multiplicative-expression
6044      additive-expression + multiplicative-expression
6045      additive-expression - multiplicative-expression
6046
6047    shift-expression:
6048      additive-expression
6049      shift-expression << additive-expression
6050      shift-expression >> additive-expression
6051
6052    relational-expression:
6053      shift-expression
6054      relational-expression < shift-expression
6055      relational-expression > shift-expression
6056      relational-expression <= shift-expression
6057      relational-expression >= shift-expression
6058
6059   GNU Extension:
6060
6061    relational-expression:
6062      relational-expression <? shift-expression
6063      relational-expression >? shift-expression
6064
6065    equality-expression:
6066      relational-expression
6067      equality-expression == relational-expression
6068      equality-expression != relational-expression
6069
6070    and-expression:
6071      equality-expression
6072      and-expression & equality-expression
6073
6074    exclusive-or-expression:
6075      and-expression
6076      exclusive-or-expression ^ and-expression
6077
6078    inclusive-or-expression:
6079      exclusive-or-expression
6080      inclusive-or-expression | exclusive-or-expression
6081
6082    logical-and-expression:
6083      inclusive-or-expression
6084      logical-and-expression && inclusive-or-expression
6085
6086    logical-or-expression:
6087      logical-and-expression
6088      logical-or-expression || logical-and-expression
6089
6090    All these are implemented with a single function like:
6091
6092    binary-expression:
6093      simple-cast-expression
6094      binary-expression <token> binary-expression
6095
6096    CAST_P is true if this expression is the target of a cast.
6097
6098    The binops_by_token map is used to get the tree codes for each <token> type.
6099    binary-expressions are associated according to a precedence table.  */
6100
6101 #define TOKEN_PRECEDENCE(token)                              \
6102 (((token->type == CPP_GREATER                                \
6103    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6104   && !parser->greater_than_is_operator_p)                    \
6105  ? PREC_NOT_OPERATOR                                         \
6106  : binops_by_token[token->type].prec)
6107
6108 static tree
6109 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6110                              enum cp_parser_prec prec)
6111 {
6112   cp_parser_expression_stack stack;
6113   cp_parser_expression_stack_entry *sp = &stack[0];
6114   tree lhs, rhs;
6115   cp_token *token;
6116   enum tree_code tree_type, lhs_type, rhs_type;
6117   enum cp_parser_prec new_prec, lookahead_prec;
6118   bool overloaded_p;
6119
6120   /* Parse the first expression.  */
6121   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6122   lhs_type = ERROR_MARK;
6123
6124   for (;;)
6125     {
6126       /* Get an operator token.  */
6127       token = cp_lexer_peek_token (parser->lexer);
6128
6129       if (warn_cxx0x_compat
6130           && token->type == CPP_RSHIFT
6131           && !parser->greater_than_is_operator_p)
6132         {
6133           warning (OPT_Wc__0x_compat, 
6134                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6135                    &token->location);
6136           warning (OPT_Wc__0x_compat, 
6137                    "suggest parentheses around %<>>%> expression");
6138         }
6139
6140       new_prec = TOKEN_PRECEDENCE (token);
6141
6142       /* Popping an entry off the stack means we completed a subexpression:
6143          - either we found a token which is not an operator (`>' where it is not
6144            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6145            will happen repeatedly;
6146          - or, we found an operator which has lower priority.  This is the case
6147            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6148            parsing `3 * 4'.  */
6149       if (new_prec <= prec)
6150         {
6151           if (sp == stack)
6152             break;
6153           else
6154             goto pop;
6155         }
6156
6157      get_rhs:
6158       tree_type = binops_by_token[token->type].tree_type;
6159
6160       /* We used the operator token.  */
6161       cp_lexer_consume_token (parser->lexer);
6162
6163       /* Extract another operand.  It may be the RHS of this expression
6164          or the LHS of a new, higher priority expression.  */
6165       rhs = cp_parser_simple_cast_expression (parser);
6166       rhs_type = ERROR_MARK;
6167
6168       /* Get another operator token.  Look up its precedence to avoid
6169          building a useless (immediately popped) stack entry for common
6170          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6171       token = cp_lexer_peek_token (parser->lexer);
6172       lookahead_prec = TOKEN_PRECEDENCE (token);
6173       if (lookahead_prec > new_prec)
6174         {
6175           /* ... and prepare to parse the RHS of the new, higher priority
6176              expression.  Since precedence levels on the stack are
6177              monotonically increasing, we do not have to care about
6178              stack overflows.  */
6179           sp->prec = prec;
6180           sp->tree_type = tree_type;
6181           sp->lhs = lhs;
6182           sp->lhs_type = lhs_type;
6183           sp++;
6184           lhs = rhs;
6185           lhs_type = rhs_type;
6186           prec = new_prec;
6187           new_prec = lookahead_prec;
6188           goto get_rhs;
6189
6190          pop:
6191           /* If the stack is not empty, we have parsed into LHS the right side
6192              (`4' in the example above) of an expression we had suspended.
6193              We can use the information on the stack to recover the LHS (`3')
6194              from the stack together with the tree code (`MULT_EXPR'), and
6195              the precedence of the higher level subexpression
6196              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6197              which will be used to actually build the additive expression.  */
6198           --sp;
6199           prec = sp->prec;
6200           tree_type = sp->tree_type;
6201           rhs = lhs;
6202           rhs_type = lhs_type;
6203           lhs = sp->lhs;
6204           lhs_type = sp->lhs_type;
6205         }
6206
6207       overloaded_p = false;
6208       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6209                                &overloaded_p, tf_warning_or_error);
6210       lhs_type = tree_type;
6211
6212       /* If the binary operator required the use of an overloaded operator,
6213          then this expression cannot be an integral constant-expression.
6214          An overloaded operator can be used even if both operands are
6215          otherwise permissible in an integral constant-expression if at
6216          least one of the operands is of enumeration type.  */
6217
6218       if (overloaded_p
6219           && (cp_parser_non_integral_constant_expression
6220               (parser, "calls to overloaded operators")))
6221         return error_mark_node;
6222     }
6223
6224   return lhs;
6225 }
6226
6227
6228 /* Parse the `? expression : assignment-expression' part of a
6229    conditional-expression.  The LOGICAL_OR_EXPR is the
6230    logical-or-expression that started the conditional-expression.
6231    Returns a representation of the entire conditional-expression.
6232
6233    This routine is used by cp_parser_assignment_expression.
6234
6235      ? expression : assignment-expression
6236
6237    GNU Extensions:
6238
6239      ? : assignment-expression */
6240
6241 static tree
6242 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6243 {
6244   tree expr;
6245   tree assignment_expr;
6246
6247   /* Consume the `?' token.  */
6248   cp_lexer_consume_token (parser->lexer);
6249   if (cp_parser_allow_gnu_extensions_p (parser)
6250       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6251     /* Implicit true clause.  */
6252     expr = NULL_TREE;
6253   else
6254     /* Parse the expression.  */
6255     expr = cp_parser_expression (parser, /*cast_p=*/false);
6256
6257   /* The next token should be a `:'.  */
6258   cp_parser_require (parser, CPP_COLON, "%<:%>");
6259   /* Parse the assignment-expression.  */
6260   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6261
6262   /* Build the conditional-expression.  */
6263   return build_x_conditional_expr (logical_or_expr,
6264                                    expr,
6265                                    assignment_expr,
6266                                    tf_warning_or_error);
6267 }
6268
6269 /* Parse an assignment-expression.
6270
6271    assignment-expression:
6272      conditional-expression
6273      logical-or-expression assignment-operator assignment_expression
6274      throw-expression
6275
6276    CAST_P is true if this expression is the target of a cast.
6277
6278    Returns a representation for the expression.  */
6279
6280 static tree
6281 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6282 {
6283   tree expr;
6284
6285   /* If the next token is the `throw' keyword, then we're looking at
6286      a throw-expression.  */
6287   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6288     expr = cp_parser_throw_expression (parser);
6289   /* Otherwise, it must be that we are looking at a
6290      logical-or-expression.  */
6291   else
6292     {
6293       /* Parse the binary expressions (logical-or-expression).  */
6294       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6295       /* If the next token is a `?' then we're actually looking at a
6296          conditional-expression.  */
6297       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6298         return cp_parser_question_colon_clause (parser, expr);
6299       else
6300         {
6301           enum tree_code assignment_operator;
6302
6303           /* If it's an assignment-operator, we're using the second
6304              production.  */
6305           assignment_operator
6306             = cp_parser_assignment_operator_opt (parser);
6307           if (assignment_operator != ERROR_MARK)
6308             {
6309               bool non_constant_p;
6310
6311               /* Parse the right-hand side of the assignment.  */
6312               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6313
6314               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6315                 maybe_warn_cpp0x ("extended initializer lists");
6316
6317               /* An assignment may not appear in a
6318                  constant-expression.  */
6319               if (cp_parser_non_integral_constant_expression (parser,
6320                                                               "an assignment"))
6321                 return error_mark_node;
6322               /* Build the assignment expression.  */
6323               expr = build_x_modify_expr (expr,
6324                                           assignment_operator,
6325                                           rhs,
6326                                           tf_warning_or_error);
6327             }
6328         }
6329     }
6330
6331   return expr;
6332 }
6333
6334 /* Parse an (optional) assignment-operator.
6335
6336    assignment-operator: one of
6337      = *= /= %= += -= >>= <<= &= ^= |=
6338
6339    GNU Extension:
6340
6341    assignment-operator: one of
6342      <?= >?=
6343
6344    If the next token is an assignment operator, the corresponding tree
6345    code is returned, and the token is consumed.  For example, for
6346    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6347    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6348    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6349    operator, ERROR_MARK is returned.  */
6350
6351 static enum tree_code
6352 cp_parser_assignment_operator_opt (cp_parser* parser)
6353 {
6354   enum tree_code op;
6355   cp_token *token;
6356
6357   /* Peek at the next token.  */
6358   token = cp_lexer_peek_token (parser->lexer);
6359
6360   switch (token->type)
6361     {
6362     case CPP_EQ:
6363       op = NOP_EXPR;
6364       break;
6365
6366     case CPP_MULT_EQ:
6367       op = MULT_EXPR;
6368       break;
6369
6370     case CPP_DIV_EQ:
6371       op = TRUNC_DIV_EXPR;
6372       break;
6373
6374     case CPP_MOD_EQ:
6375       op = TRUNC_MOD_EXPR;
6376       break;
6377
6378     case CPP_PLUS_EQ:
6379       op = PLUS_EXPR;
6380       break;
6381
6382     case CPP_MINUS_EQ:
6383       op = MINUS_EXPR;
6384       break;
6385
6386     case CPP_RSHIFT_EQ:
6387       op = RSHIFT_EXPR;
6388       break;
6389
6390     case CPP_LSHIFT_EQ:
6391       op = LSHIFT_EXPR;
6392       break;
6393
6394     case CPP_AND_EQ:
6395       op = BIT_AND_EXPR;
6396       break;
6397
6398     case CPP_XOR_EQ:
6399       op = BIT_XOR_EXPR;
6400       break;
6401
6402     case CPP_OR_EQ:
6403       op = BIT_IOR_EXPR;
6404       break;
6405
6406     default:
6407       /* Nothing else is an assignment operator.  */
6408       op = ERROR_MARK;
6409     }
6410
6411   /* If it was an assignment operator, consume it.  */
6412   if (op != ERROR_MARK)
6413     cp_lexer_consume_token (parser->lexer);
6414
6415   return op;
6416 }
6417
6418 /* Parse an expression.
6419
6420    expression:
6421      assignment-expression
6422      expression , assignment-expression
6423
6424    CAST_P is true if this expression is the target of a cast.
6425
6426    Returns a representation of the expression.  */
6427
6428 static tree
6429 cp_parser_expression (cp_parser* parser, bool cast_p)
6430 {
6431   tree expression = NULL_TREE;
6432
6433   while (true)
6434     {
6435       tree assignment_expression;
6436
6437       /* Parse the next assignment-expression.  */
6438       assignment_expression
6439         = cp_parser_assignment_expression (parser, cast_p);
6440       /* If this is the first assignment-expression, we can just
6441          save it away.  */
6442       if (!expression)
6443         expression = assignment_expression;
6444       else
6445         expression = build_x_compound_expr (expression,
6446                                             assignment_expression,
6447                                             tf_warning_or_error);
6448       /* If the next token is not a comma, then we are done with the
6449          expression.  */
6450       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6451         break;
6452       /* Consume the `,'.  */
6453       cp_lexer_consume_token (parser->lexer);
6454       /* A comma operator cannot appear in a constant-expression.  */
6455       if (cp_parser_non_integral_constant_expression (parser,
6456                                                       "a comma operator"))
6457         expression = error_mark_node;
6458     }
6459
6460   return expression;
6461 }
6462
6463 /* Parse a constant-expression.
6464
6465    constant-expression:
6466      conditional-expression
6467
6468   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6469   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6470   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6471   is false, NON_CONSTANT_P should be NULL.  */
6472
6473 static tree
6474 cp_parser_constant_expression (cp_parser* parser,
6475                                bool allow_non_constant_p,
6476                                bool *non_constant_p)
6477 {
6478   bool saved_integral_constant_expression_p;
6479   bool saved_allow_non_integral_constant_expression_p;
6480   bool saved_non_integral_constant_expression_p;
6481   tree expression;
6482
6483   /* It might seem that we could simply parse the
6484      conditional-expression, and then check to see if it were
6485      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6486      one that the compiler can figure out is constant, possibly after
6487      doing some simplifications or optimizations.  The standard has a
6488      precise definition of constant-expression, and we must honor
6489      that, even though it is somewhat more restrictive.
6490
6491      For example:
6492
6493        int i[(2, 3)];
6494
6495      is not a legal declaration, because `(2, 3)' is not a
6496      constant-expression.  The `,' operator is forbidden in a
6497      constant-expression.  However, GCC's constant-folding machinery
6498      will fold this operation to an INTEGER_CST for `3'.  */
6499
6500   /* Save the old settings.  */
6501   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6502   saved_allow_non_integral_constant_expression_p
6503     = parser->allow_non_integral_constant_expression_p;
6504   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6505   /* We are now parsing a constant-expression.  */
6506   parser->integral_constant_expression_p = true;
6507   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6508   parser->non_integral_constant_expression_p = false;
6509   /* Although the grammar says "conditional-expression", we parse an
6510      "assignment-expression", which also permits "throw-expression"
6511      and the use of assignment operators.  In the case that
6512      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6513      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6514      actually essential that we look for an assignment-expression.
6515      For example, cp_parser_initializer_clauses uses this function to
6516      determine whether a particular assignment-expression is in fact
6517      constant.  */
6518   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6519   /* Restore the old settings.  */
6520   parser->integral_constant_expression_p
6521     = saved_integral_constant_expression_p;
6522   parser->allow_non_integral_constant_expression_p
6523     = saved_allow_non_integral_constant_expression_p;
6524   if (allow_non_constant_p)
6525     *non_constant_p = parser->non_integral_constant_expression_p;
6526   else if (parser->non_integral_constant_expression_p)
6527     expression = error_mark_node;
6528   parser->non_integral_constant_expression_p
6529     = saved_non_integral_constant_expression_p;
6530
6531   return expression;
6532 }
6533
6534 /* Parse __builtin_offsetof.
6535
6536    offsetof-expression:
6537      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6538
6539    offsetof-member-designator:
6540      id-expression
6541      | offsetof-member-designator "." id-expression
6542      | offsetof-member-designator "[" expression "]"  */
6543
6544 static tree
6545 cp_parser_builtin_offsetof (cp_parser *parser)
6546 {
6547   int save_ice_p, save_non_ice_p;
6548   tree type, expr;
6549   cp_id_kind dummy;
6550   cp_token *token;
6551
6552   /* We're about to accept non-integral-constant things, but will
6553      definitely yield an integral constant expression.  Save and
6554      restore these values around our local parsing.  */
6555   save_ice_p = parser->integral_constant_expression_p;
6556   save_non_ice_p = parser->non_integral_constant_expression_p;
6557
6558   /* Consume the "__builtin_offsetof" token.  */
6559   cp_lexer_consume_token (parser->lexer);
6560   /* Consume the opening `('.  */
6561   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6562   /* Parse the type-id.  */
6563   type = cp_parser_type_id (parser);
6564   /* Look for the `,'.  */
6565   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6566   token = cp_lexer_peek_token (parser->lexer);
6567
6568   /* Build the (type *)null that begins the traditional offsetof macro.  */
6569   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6570                             tf_warning_or_error);
6571
6572   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6573   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6574                                                  true, &dummy, token->location);
6575   while (true)
6576     {
6577       token = cp_lexer_peek_token (parser->lexer);
6578       switch (token->type)
6579         {
6580         case CPP_OPEN_SQUARE:
6581           /* offsetof-member-designator "[" expression "]" */
6582           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6583           break;
6584
6585         case CPP_DOT:
6586           /* offsetof-member-designator "." identifier */
6587           cp_lexer_consume_token (parser->lexer);
6588           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6589                                                          true, &dummy,
6590                                                          token->location);
6591           break;
6592
6593         case CPP_CLOSE_PAREN:
6594           /* Consume the ")" token.  */
6595           cp_lexer_consume_token (parser->lexer);
6596           goto success;
6597
6598         default:
6599           /* Error.  We know the following require will fail, but
6600              that gives the proper error message.  */
6601           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6602           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6603           expr = error_mark_node;
6604           goto failure;
6605         }
6606     }
6607
6608  success:
6609   /* If we're processing a template, we can't finish the semantics yet.
6610      Otherwise we can fold the entire expression now.  */
6611   if (processing_template_decl)
6612     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6613   else
6614     expr = finish_offsetof (expr);
6615
6616  failure:
6617   parser->integral_constant_expression_p = save_ice_p;
6618   parser->non_integral_constant_expression_p = save_non_ice_p;
6619
6620   return expr;
6621 }
6622
6623 /* Parse a trait expression.  */
6624
6625 static tree
6626 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6627 {
6628   cp_trait_kind kind;
6629   tree type1, type2 = NULL_TREE;
6630   bool binary = false;
6631   cp_decl_specifier_seq decl_specs;
6632
6633   switch (keyword)
6634     {
6635     case RID_HAS_NOTHROW_ASSIGN:
6636       kind = CPTK_HAS_NOTHROW_ASSIGN;
6637       break;
6638     case RID_HAS_NOTHROW_CONSTRUCTOR:
6639       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6640       break;
6641     case RID_HAS_NOTHROW_COPY:
6642       kind = CPTK_HAS_NOTHROW_COPY;
6643       break;
6644     case RID_HAS_TRIVIAL_ASSIGN:
6645       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6646       break;
6647     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6648       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6649       break;
6650     case RID_HAS_TRIVIAL_COPY:
6651       kind = CPTK_HAS_TRIVIAL_COPY;
6652       break;
6653     case RID_HAS_TRIVIAL_DESTRUCTOR:
6654       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6655       break;
6656     case RID_HAS_VIRTUAL_DESTRUCTOR:
6657       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6658       break;
6659     case RID_IS_ABSTRACT:
6660       kind = CPTK_IS_ABSTRACT;
6661       break;
6662     case RID_IS_BASE_OF:
6663       kind = CPTK_IS_BASE_OF;
6664       binary = true;
6665       break;
6666     case RID_IS_CLASS:
6667       kind = CPTK_IS_CLASS;
6668       break;
6669     case RID_IS_CONVERTIBLE_TO:
6670       kind = CPTK_IS_CONVERTIBLE_TO;
6671       binary = true;
6672       break;
6673     case RID_IS_EMPTY:
6674       kind = CPTK_IS_EMPTY;
6675       break;
6676     case RID_IS_ENUM:
6677       kind = CPTK_IS_ENUM;
6678       break;
6679     case RID_IS_POD:
6680       kind = CPTK_IS_POD;
6681       break;
6682     case RID_IS_POLYMORPHIC:
6683       kind = CPTK_IS_POLYMORPHIC;
6684       break;
6685     case RID_IS_UNION:
6686       kind = CPTK_IS_UNION;
6687       break;
6688     default:
6689       gcc_unreachable ();
6690     }
6691
6692   /* Consume the token.  */
6693   cp_lexer_consume_token (parser->lexer);
6694
6695   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6696
6697   type1 = cp_parser_type_id (parser);
6698
6699   if (type1 == error_mark_node)
6700     return error_mark_node;
6701
6702   /* Build a trivial decl-specifier-seq.  */
6703   clear_decl_specs (&decl_specs);
6704   decl_specs.type = type1;
6705
6706   /* Call grokdeclarator to figure out what type this is.  */
6707   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6708                           /*initialized=*/0, /*attrlist=*/NULL);
6709
6710   if (binary)
6711     {
6712       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6713  
6714       type2 = cp_parser_type_id (parser);
6715
6716       if (type2 == error_mark_node)
6717         return error_mark_node;
6718
6719       /* Build a trivial decl-specifier-seq.  */
6720       clear_decl_specs (&decl_specs);
6721       decl_specs.type = type2;
6722
6723       /* Call grokdeclarator to figure out what type this is.  */
6724       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6725                               /*initialized=*/0, /*attrlist=*/NULL);
6726     }
6727
6728   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6729
6730   /* Complete the trait expression, which may mean either processing
6731      the trait expr now or saving it for template instantiation.  */
6732   return finish_trait_expr (kind, type1, type2);
6733 }
6734
6735 /* Statements [gram.stmt.stmt]  */
6736
6737 /* Parse a statement.
6738
6739    statement:
6740      labeled-statement
6741      expression-statement
6742      compound-statement
6743      selection-statement
6744      iteration-statement
6745      jump-statement
6746      declaration-statement
6747      try-block
6748
6749   IN_COMPOUND is true when the statement is nested inside a
6750   cp_parser_compound_statement; this matters for certain pragmas.
6751
6752   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6753   is a (possibly labeled) if statement which is not enclosed in braces
6754   and has an else clause.  This is used to implement -Wparentheses.  */
6755
6756 static void
6757 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6758                      bool in_compound, bool *if_p)
6759 {
6760   tree statement;
6761   cp_token *token;
6762   location_t statement_location;
6763
6764  restart:
6765   if (if_p != NULL)
6766     *if_p = false;
6767   /* There is no statement yet.  */
6768   statement = NULL_TREE;
6769   /* Peek at the next token.  */
6770   token = cp_lexer_peek_token (parser->lexer);
6771   /* Remember the location of the first token in the statement.  */
6772   statement_location = token->location;
6773   /* If this is a keyword, then that will often determine what kind of
6774      statement we have.  */
6775   if (token->type == CPP_KEYWORD)
6776     {
6777       enum rid keyword = token->keyword;
6778
6779       switch (keyword)
6780         {
6781         case RID_CASE:
6782         case RID_DEFAULT:
6783           /* Looks like a labeled-statement with a case label.
6784              Parse the label, and then use tail recursion to parse
6785              the statement.  */
6786           cp_parser_label_for_labeled_statement (parser);
6787           goto restart;
6788
6789         case RID_IF:
6790         case RID_SWITCH:
6791           statement = cp_parser_selection_statement (parser, if_p);
6792           break;
6793
6794         case RID_WHILE:
6795         case RID_DO:
6796         case RID_FOR:
6797           statement = cp_parser_iteration_statement (parser);
6798           break;
6799
6800         case RID_BREAK:
6801         case RID_CONTINUE:
6802         case RID_RETURN:
6803         case RID_GOTO:
6804           statement = cp_parser_jump_statement (parser);
6805           break;
6806
6807           /* Objective-C++ exception-handling constructs.  */
6808         case RID_AT_TRY:
6809         case RID_AT_CATCH:
6810         case RID_AT_FINALLY:
6811         case RID_AT_SYNCHRONIZED:
6812         case RID_AT_THROW:
6813           statement = cp_parser_objc_statement (parser);
6814           break;
6815
6816         case RID_TRY:
6817           statement = cp_parser_try_block (parser);
6818           break;
6819
6820         case RID_NAMESPACE:
6821           /* This must be a namespace alias definition.  */
6822           cp_parser_declaration_statement (parser);
6823           return;
6824           
6825         default:
6826           /* It might be a keyword like `int' that can start a
6827              declaration-statement.  */
6828           break;
6829         }
6830     }
6831   else if (token->type == CPP_NAME)
6832     {
6833       /* If the next token is a `:', then we are looking at a
6834          labeled-statement.  */
6835       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6836       if (token->type == CPP_COLON)
6837         {
6838           /* Looks like a labeled-statement with an ordinary label.
6839              Parse the label, and then use tail recursion to parse
6840              the statement.  */
6841           cp_parser_label_for_labeled_statement (parser);
6842           goto restart;
6843         }
6844     }
6845   /* Anything that starts with a `{' must be a compound-statement.  */
6846   else if (token->type == CPP_OPEN_BRACE)
6847     statement = cp_parser_compound_statement (parser, NULL, false);
6848   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6849      a statement all its own.  */
6850   else if (token->type == CPP_PRAGMA)
6851     {
6852       /* Only certain OpenMP pragmas are attached to statements, and thus
6853          are considered statements themselves.  All others are not.  In
6854          the context of a compound, accept the pragma as a "statement" and
6855          return so that we can check for a close brace.  Otherwise we
6856          require a real statement and must go back and read one.  */
6857       if (in_compound)
6858         cp_parser_pragma (parser, pragma_compound);
6859       else if (!cp_parser_pragma (parser, pragma_stmt))
6860         goto restart;
6861       return;
6862     }
6863   else if (token->type == CPP_EOF)
6864     {
6865       cp_parser_error (parser, "expected statement");
6866       return;
6867     }
6868
6869   /* Everything else must be a declaration-statement or an
6870      expression-statement.  Try for the declaration-statement
6871      first, unless we are looking at a `;', in which case we know that
6872      we have an expression-statement.  */
6873   if (!statement)
6874     {
6875       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6876         {
6877           cp_parser_parse_tentatively (parser);
6878           /* Try to parse the declaration-statement.  */
6879           cp_parser_declaration_statement (parser);
6880           /* If that worked, we're done.  */
6881           if (cp_parser_parse_definitely (parser))
6882             return;
6883         }
6884       /* Look for an expression-statement instead.  */
6885       statement = cp_parser_expression_statement (parser, in_statement_expr);
6886     }
6887
6888   /* Set the line number for the statement.  */
6889   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6890     SET_EXPR_LOCATION (statement, statement_location);
6891 }
6892
6893 /* Parse the label for a labeled-statement, i.e.
6894
6895    identifier :
6896    case constant-expression :
6897    default :
6898
6899    GNU Extension:
6900    case constant-expression ... constant-expression : statement
6901
6902    When a label is parsed without errors, the label is added to the
6903    parse tree by the finish_* functions, so this function doesn't
6904    have to return the label.  */
6905
6906 static void
6907 cp_parser_label_for_labeled_statement (cp_parser* parser)
6908 {
6909   cp_token *token;
6910
6911   /* The next token should be an identifier.  */
6912   token = cp_lexer_peek_token (parser->lexer);
6913   if (token->type != CPP_NAME
6914       && token->type != CPP_KEYWORD)
6915     {
6916       cp_parser_error (parser, "expected labeled-statement");
6917       return;
6918     }
6919
6920   switch (token->keyword)
6921     {
6922     case RID_CASE:
6923       {
6924         tree expr, expr_hi;
6925         cp_token *ellipsis;
6926
6927         /* Consume the `case' token.  */
6928         cp_lexer_consume_token (parser->lexer);
6929         /* Parse the constant-expression.  */
6930         expr = cp_parser_constant_expression (parser,
6931                                               /*allow_non_constant_p=*/false,
6932                                               NULL);
6933
6934         ellipsis = cp_lexer_peek_token (parser->lexer);
6935         if (ellipsis->type == CPP_ELLIPSIS)
6936           {
6937             /* Consume the `...' token.  */
6938             cp_lexer_consume_token (parser->lexer);
6939             expr_hi =
6940               cp_parser_constant_expression (parser,
6941                                              /*allow_non_constant_p=*/false,
6942                                              NULL);
6943             /* We don't need to emit warnings here, as the common code
6944                will do this for us.  */
6945           }
6946         else
6947           expr_hi = NULL_TREE;
6948
6949         if (parser->in_switch_statement_p)
6950           finish_case_label (expr, expr_hi);
6951         else
6952           error ("%Hcase label %qE not within a switch statement",
6953                  &token->location, expr);
6954       }
6955       break;
6956
6957     case RID_DEFAULT:
6958       /* Consume the `default' token.  */
6959       cp_lexer_consume_token (parser->lexer);
6960
6961       if (parser->in_switch_statement_p)
6962         finish_case_label (NULL_TREE, NULL_TREE);
6963       else
6964         error ("%Hcase label not within a switch statement", &token->location);
6965       break;
6966
6967     default:
6968       /* Anything else must be an ordinary label.  */
6969       finish_label_stmt (cp_parser_identifier (parser));
6970       break;
6971     }
6972
6973   /* Require the `:' token.  */
6974   cp_parser_require (parser, CPP_COLON, "%<:%>");
6975 }
6976
6977 /* Parse an expression-statement.
6978
6979    expression-statement:
6980      expression [opt] ;
6981
6982    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6983    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6984    indicates whether this expression-statement is part of an
6985    expression statement.  */
6986
6987 static tree
6988 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6989 {
6990   tree statement = NULL_TREE;
6991
6992   /* If the next token is a ';', then there is no expression
6993      statement.  */
6994   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6995     statement = cp_parser_expression (parser, /*cast_p=*/false);
6996
6997   /* Consume the final `;'.  */
6998   cp_parser_consume_semicolon_at_end_of_statement (parser);
6999
7000   if (in_statement_expr
7001       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7002     /* This is the final expression statement of a statement
7003        expression.  */
7004     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7005   else if (statement)
7006     statement = finish_expr_stmt (statement);
7007   else
7008     finish_stmt ();
7009
7010   return statement;
7011 }
7012
7013 /* Parse a compound-statement.
7014
7015    compound-statement:
7016      { statement-seq [opt] }
7017
7018    GNU extension:
7019
7020    compound-statement:
7021      { label-declaration-seq [opt] statement-seq [opt] }
7022
7023    label-declaration-seq:
7024      label-declaration
7025      label-declaration-seq label-declaration
7026
7027    Returns a tree representing the statement.  */
7028
7029 static tree
7030 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7031                               bool in_try)
7032 {
7033   tree compound_stmt;
7034
7035   /* Consume the `{'.  */
7036   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7037     return error_mark_node;
7038   /* Begin the compound-statement.  */
7039   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7040   /* If the next keyword is `__label__' we have a label declaration.  */
7041   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7042     cp_parser_label_declaration (parser);
7043   /* Parse an (optional) statement-seq.  */
7044   cp_parser_statement_seq_opt (parser, in_statement_expr);
7045   /* Finish the compound-statement.  */
7046   finish_compound_stmt (compound_stmt);
7047   /* Consume the `}'.  */
7048   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7049
7050   return compound_stmt;
7051 }
7052
7053 /* Parse an (optional) statement-seq.
7054
7055    statement-seq:
7056      statement
7057      statement-seq [opt] statement  */
7058
7059 static void
7060 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7061 {
7062   /* Scan statements until there aren't any more.  */
7063   while (true)
7064     {
7065       cp_token *token = cp_lexer_peek_token (parser->lexer);
7066
7067       /* If we're looking at a `}', then we've run out of statements.  */
7068       if (token->type == CPP_CLOSE_BRACE
7069           || token->type == CPP_EOF
7070           || token->type == CPP_PRAGMA_EOL)
7071         break;
7072       
7073       /* If we are in a compound statement and find 'else' then
7074          something went wrong.  */
7075       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7076         {
7077           if (parser->in_statement & IN_IF_STMT) 
7078             break;
7079           else
7080             {
7081               token = cp_lexer_consume_token (parser->lexer);
7082               error ("%H%<else%> without a previous %<if%>", &token->location);
7083             }
7084         }
7085
7086       /* Parse the statement.  */
7087       cp_parser_statement (parser, in_statement_expr, true, NULL);
7088     }
7089 }
7090
7091 /* Parse a selection-statement.
7092
7093    selection-statement:
7094      if ( condition ) statement
7095      if ( condition ) statement else statement
7096      switch ( condition ) statement
7097
7098    Returns the new IF_STMT or SWITCH_STMT.
7099
7100    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7101    is a (possibly labeled) if statement which is not enclosed in
7102    braces and has an else clause.  This is used to implement
7103    -Wparentheses.  */
7104
7105 static tree
7106 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7107 {
7108   cp_token *token;
7109   enum rid keyword;
7110
7111   if (if_p != NULL)
7112     *if_p = false;
7113
7114   /* Peek at the next token.  */
7115   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7116
7117   /* See what kind of keyword it is.  */
7118   keyword = token->keyword;
7119   switch (keyword)
7120     {
7121     case RID_IF:
7122     case RID_SWITCH:
7123       {
7124         tree statement;
7125         tree condition;
7126
7127         /* Look for the `('.  */
7128         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7129           {
7130             cp_parser_skip_to_end_of_statement (parser);
7131             return error_mark_node;
7132           }
7133
7134         /* Begin the selection-statement.  */
7135         if (keyword == RID_IF)
7136           statement = begin_if_stmt ();
7137         else
7138           statement = begin_switch_stmt ();
7139
7140         /* Parse the condition.  */
7141         condition = cp_parser_condition (parser);
7142         /* Look for the `)'.  */
7143         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7144           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7145                                                  /*consume_paren=*/true);
7146
7147         if (keyword == RID_IF)
7148           {
7149             bool nested_if;
7150             unsigned char in_statement;
7151
7152             /* Add the condition.  */
7153             finish_if_stmt_cond (condition, statement);
7154
7155             /* Parse the then-clause.  */
7156             in_statement = parser->in_statement;
7157             parser->in_statement |= IN_IF_STMT;
7158             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7159               {
7160                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7161                 add_stmt (build_empty_stmt ());
7162                 cp_lexer_consume_token (parser->lexer);
7163                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7164                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7165                               "empty body in an %<if%> statement");
7166                 nested_if = false;
7167               }
7168             else
7169               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7170             parser->in_statement = in_statement;
7171
7172             finish_then_clause (statement);
7173
7174             /* If the next token is `else', parse the else-clause.  */
7175             if (cp_lexer_next_token_is_keyword (parser->lexer,
7176                                                 RID_ELSE))
7177               {
7178                 /* Consume the `else' keyword.  */
7179                 cp_lexer_consume_token (parser->lexer);
7180                 begin_else_clause (statement);
7181                 /* Parse the else-clause.  */
7182                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7183                   {
7184                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7185                                 OPT_Wempty_body, "suggest braces around "
7186                                 "empty body in an %<else%> statement");
7187                     add_stmt (build_empty_stmt ());
7188                     cp_lexer_consume_token (parser->lexer);
7189                   }
7190                 else
7191                   cp_parser_implicitly_scoped_statement (parser, NULL);
7192
7193                 finish_else_clause (statement);
7194
7195                 /* If we are currently parsing a then-clause, then
7196                    IF_P will not be NULL.  We set it to true to
7197                    indicate that this if statement has an else clause.
7198                    This may trigger the Wparentheses warning below
7199                    when we get back up to the parent if statement.  */
7200                 if (if_p != NULL)
7201                   *if_p = true;
7202               }
7203             else
7204               {
7205                 /* This if statement does not have an else clause.  If
7206                    NESTED_IF is true, then the then-clause is an if
7207                    statement which does have an else clause.  We warn
7208                    about the potential ambiguity.  */
7209                 if (nested_if)
7210                   warning (OPT_Wparentheses,
7211                            ("%Hsuggest explicit braces "
7212                             "to avoid ambiguous %<else%>"),
7213                            EXPR_LOCUS (statement));
7214               }
7215
7216             /* Now we're all done with the if-statement.  */
7217             finish_if_stmt (statement);
7218           }
7219         else
7220           {
7221             bool in_switch_statement_p;
7222             unsigned char in_statement;
7223
7224             /* Add the condition.  */
7225             finish_switch_cond (condition, statement);
7226
7227             /* Parse the body of the switch-statement.  */
7228             in_switch_statement_p = parser->in_switch_statement_p;
7229             in_statement = parser->in_statement;
7230             parser->in_switch_statement_p = true;
7231             parser->in_statement |= IN_SWITCH_STMT;
7232             cp_parser_implicitly_scoped_statement (parser, NULL);
7233             parser->in_switch_statement_p = in_switch_statement_p;
7234             parser->in_statement = in_statement;
7235
7236             /* Now we're all done with the switch-statement.  */
7237             finish_switch_stmt (statement);
7238           }
7239
7240         return statement;
7241       }
7242       break;
7243
7244     default:
7245       cp_parser_error (parser, "expected selection-statement");
7246       return error_mark_node;
7247     }
7248 }
7249
7250 /* Parse a condition.
7251
7252    condition:
7253      expression
7254      type-specifier-seq declarator = initializer-clause
7255      type-specifier-seq declarator braced-init-list
7256
7257    GNU Extension:
7258
7259    condition:
7260      type-specifier-seq declarator asm-specification [opt]
7261        attributes [opt] = assignment-expression
7262
7263    Returns the expression that should be tested.  */
7264
7265 static tree
7266 cp_parser_condition (cp_parser* parser)
7267 {
7268   cp_decl_specifier_seq type_specifiers;
7269   const char *saved_message;
7270
7271   /* Try the declaration first.  */
7272   cp_parser_parse_tentatively (parser);
7273   /* New types are not allowed in the type-specifier-seq for a
7274      condition.  */
7275   saved_message = parser->type_definition_forbidden_message;
7276   parser->type_definition_forbidden_message
7277     = "types may not be defined in conditions";
7278   /* Parse the type-specifier-seq.  */
7279   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7280                                 &type_specifiers);
7281   /* Restore the saved message.  */
7282   parser->type_definition_forbidden_message = saved_message;
7283   /* If all is well, we might be looking at a declaration.  */
7284   if (!cp_parser_error_occurred (parser))
7285     {
7286       tree decl;
7287       tree asm_specification;
7288       tree attributes;
7289       cp_declarator *declarator;
7290       tree initializer = NULL_TREE;
7291
7292       /* Parse the declarator.  */
7293       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7294                                          /*ctor_dtor_or_conv_p=*/NULL,
7295                                          /*parenthesized_p=*/NULL,
7296                                          /*member_p=*/false);
7297       /* Parse the attributes.  */
7298       attributes = cp_parser_attributes_opt (parser);
7299       /* Parse the asm-specification.  */
7300       asm_specification = cp_parser_asm_specification_opt (parser);
7301       /* If the next token is not an `=' or '{', then we might still be
7302          looking at an expression.  For example:
7303
7304            if (A(a).x)
7305
7306          looks like a decl-specifier-seq and a declarator -- but then
7307          there is no `=', so this is an expression.  */
7308       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7309           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7310         cp_parser_simulate_error (parser);
7311         
7312       /* If we did see an `=' or '{', then we are looking at a declaration
7313          for sure.  */
7314       if (cp_parser_parse_definitely (parser))
7315         {
7316           tree pushed_scope;
7317           bool non_constant_p;
7318           bool flags = LOOKUP_ONLYCONVERTING;
7319
7320           /* Create the declaration.  */
7321           decl = start_decl (declarator, &type_specifiers,
7322                              /*initialized_p=*/true,
7323                              attributes, /*prefix_attributes=*/NULL_TREE,
7324                              &pushed_scope);
7325
7326           /* Parse the initializer.  */
7327           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7328             {
7329               initializer = cp_parser_braced_list (parser, &non_constant_p);
7330               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7331               flags = 0;
7332             }
7333           else
7334             {
7335               /* Consume the `='.  */
7336               cp_lexer_consume_token (parser->lexer);
7337               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7338             }
7339           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7340             maybe_warn_cpp0x ("extended initializer lists");
7341
7342           if (!non_constant_p)
7343             initializer = fold_non_dependent_expr (initializer);
7344
7345           /* Process the initializer.  */
7346           cp_finish_decl (decl,
7347                           initializer, !non_constant_p,
7348                           asm_specification,
7349                           flags);
7350
7351           if (pushed_scope)
7352             pop_scope (pushed_scope);
7353
7354           return convert_from_reference (decl);
7355         }
7356     }
7357   /* If we didn't even get past the declarator successfully, we are
7358      definitely not looking at a declaration.  */
7359   else
7360     cp_parser_abort_tentative_parse (parser);
7361
7362   /* Otherwise, we are looking at an expression.  */
7363   return cp_parser_expression (parser, /*cast_p=*/false);
7364 }
7365
7366 /* We check for a ) immediately followed by ; with no whitespacing
7367    between.  This is used to issue a warning for:
7368
7369      while (...);
7370
7371    and:
7372
7373      for (...);
7374
7375    as the semicolon is probably extraneous.
7376
7377    On parse errors, the next token might not be a ), so do nothing in
7378    that case. */
7379
7380 static void
7381 check_empty_body (cp_parser* parser, const char* type)
7382 {
7383   cp_token *token;
7384   cp_token *close_paren;
7385   expanded_location close_loc;
7386   expanded_location semi_loc;
7387   
7388   close_paren = cp_lexer_peek_token (parser->lexer);
7389   if (close_paren->type != CPP_CLOSE_PAREN)
7390     return;
7391
7392   close_loc = expand_location (close_paren->location);
7393   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7394
7395   if (token->type != CPP_SEMICOLON
7396       || (token->flags & PREV_WHITE))
7397     return;
7398
7399   semi_loc =  expand_location (token->location);
7400   if (close_loc.line == semi_loc.line
7401       && close_loc.column+1 == semi_loc.column)
7402     warning (OPT_Wempty_body,
7403              "suggest a space before %<;%> or explicit braces around empty "
7404              "body in %<%s%> statement",
7405              type);
7406 }
7407
7408 /* Parse an iteration-statement.
7409
7410    iteration-statement:
7411      while ( condition ) statement
7412      do statement while ( expression ) ;
7413      for ( for-init-statement condition [opt] ; expression [opt] )
7414        statement
7415
7416    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7417
7418 static tree
7419 cp_parser_iteration_statement (cp_parser* parser)
7420 {
7421   cp_token *token;
7422   enum rid keyword;
7423   tree statement;
7424   unsigned char in_statement;
7425
7426   /* Peek at the next token.  */
7427   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7428   if (!token)
7429     return error_mark_node;
7430
7431   /* Remember whether or not we are already within an iteration
7432      statement.  */
7433   in_statement = parser->in_statement;
7434
7435   /* See what kind of keyword it is.  */
7436   keyword = token->keyword;
7437   switch (keyword)
7438     {
7439     case RID_WHILE:
7440       {
7441         tree condition;
7442
7443         /* Begin the while-statement.  */
7444         statement = begin_while_stmt ();
7445         /* Look for the `('.  */
7446         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7447         /* Parse the condition.  */
7448         condition = cp_parser_condition (parser);
7449         finish_while_stmt_cond (condition, statement);
7450         check_empty_body (parser, "while");
7451         /* Look for the `)'.  */
7452         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7453         /* Parse the dependent statement.  */
7454         parser->in_statement = IN_ITERATION_STMT;
7455         cp_parser_already_scoped_statement (parser);
7456         parser->in_statement = in_statement;
7457         /* We're done with the while-statement.  */
7458         finish_while_stmt (statement);
7459       }
7460       break;
7461
7462     case RID_DO:
7463       {
7464         tree expression;
7465
7466         /* Begin the do-statement.  */
7467         statement = begin_do_stmt ();
7468         /* Parse the body of the do-statement.  */
7469         parser->in_statement = IN_ITERATION_STMT;
7470         cp_parser_implicitly_scoped_statement (parser, NULL);
7471         parser->in_statement = in_statement;
7472         finish_do_body (statement);
7473         /* Look for the `while' keyword.  */
7474         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7475         /* Look for the `('.  */
7476         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7477         /* Parse the expression.  */
7478         expression = cp_parser_expression (parser, /*cast_p=*/false);
7479         /* We're done with the do-statement.  */
7480         finish_do_stmt (expression, statement);
7481         /* Look for the `)'.  */
7482         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7483         /* Look for the `;'.  */
7484         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7485       }
7486       break;
7487
7488     case RID_FOR:
7489       {
7490         tree condition = NULL_TREE;
7491         tree expression = NULL_TREE;
7492
7493         /* Begin the for-statement.  */
7494         statement = begin_for_stmt ();
7495         /* Look for the `('.  */
7496         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7497         /* Parse the initialization.  */
7498         cp_parser_for_init_statement (parser);
7499         finish_for_init_stmt (statement);
7500
7501         /* If there's a condition, process it.  */
7502         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7503           condition = cp_parser_condition (parser);
7504         finish_for_cond (condition, statement);
7505         /* Look for the `;'.  */
7506         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7507
7508         /* If there's an expression, process it.  */
7509         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7510           expression = cp_parser_expression (parser, /*cast_p=*/false);
7511         finish_for_expr (expression, statement);
7512         check_empty_body (parser, "for");
7513         /* Look for the `)'.  */
7514         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7515
7516         /* Parse the body of the for-statement.  */
7517         parser->in_statement = IN_ITERATION_STMT;
7518         cp_parser_already_scoped_statement (parser);
7519         parser->in_statement = in_statement;
7520
7521         /* We're done with the for-statement.  */
7522         finish_for_stmt (statement);
7523       }
7524       break;
7525
7526     default:
7527       cp_parser_error (parser, "expected iteration-statement");
7528       statement = error_mark_node;
7529       break;
7530     }
7531
7532   return statement;
7533 }
7534
7535 /* Parse a for-init-statement.
7536
7537    for-init-statement:
7538      expression-statement
7539      simple-declaration  */
7540
7541 static void
7542 cp_parser_for_init_statement (cp_parser* parser)
7543 {
7544   /* If the next token is a `;', then we have an empty
7545      expression-statement.  Grammatically, this is also a
7546      simple-declaration, but an invalid one, because it does not
7547      declare anything.  Therefore, if we did not handle this case
7548      specially, we would issue an error message about an invalid
7549      declaration.  */
7550   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7551     {
7552       /* We're going to speculatively look for a declaration, falling back
7553          to an expression, if necessary.  */
7554       cp_parser_parse_tentatively (parser);
7555       /* Parse the declaration.  */
7556       cp_parser_simple_declaration (parser,
7557                                     /*function_definition_allowed_p=*/false);
7558       /* If the tentative parse failed, then we shall need to look for an
7559          expression-statement.  */
7560       if (cp_parser_parse_definitely (parser))
7561         return;
7562     }
7563
7564   cp_parser_expression_statement (parser, false);
7565 }
7566
7567 /* Parse a jump-statement.
7568
7569    jump-statement:
7570      break ;
7571      continue ;
7572      return expression [opt] ;
7573      return braced-init-list ;
7574      goto identifier ;
7575
7576    GNU extension:
7577
7578    jump-statement:
7579      goto * expression ;
7580
7581    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7582
7583 static tree
7584 cp_parser_jump_statement (cp_parser* parser)
7585 {
7586   tree statement = error_mark_node;
7587   cp_token *token;
7588   enum rid keyword;
7589   unsigned char in_statement;
7590
7591   /* Peek at the next token.  */
7592   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7593   if (!token)
7594     return error_mark_node;
7595
7596   /* See what kind of keyword it is.  */
7597   keyword = token->keyword;
7598   switch (keyword)
7599     {
7600     case RID_BREAK:
7601       in_statement = parser->in_statement & ~IN_IF_STMT;      
7602       switch (in_statement)
7603         {
7604         case 0:
7605           error ("%Hbreak statement not within loop or switch", &token->location);
7606           break;
7607         default:
7608           gcc_assert ((in_statement & IN_SWITCH_STMT)
7609                       || in_statement == IN_ITERATION_STMT);
7610           statement = finish_break_stmt ();
7611           break;
7612         case IN_OMP_BLOCK:
7613           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7614           break;
7615         case IN_OMP_FOR:
7616           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7617           break;
7618         }
7619       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7620       break;
7621
7622     case RID_CONTINUE:
7623       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7624         {
7625         case 0:
7626           error ("%Hcontinue statement not within a loop", &token->location);
7627           break;
7628         case IN_ITERATION_STMT:
7629         case IN_OMP_FOR:
7630           statement = finish_continue_stmt ();
7631           break;
7632         case IN_OMP_BLOCK:
7633           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7634           break;
7635         default:
7636           gcc_unreachable ();
7637         }
7638       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7639       break;
7640
7641     case RID_RETURN:
7642       {
7643         tree expr;
7644         bool expr_non_constant_p;
7645
7646         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7647           {
7648             maybe_warn_cpp0x ("extended initializer lists");
7649             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7650           }
7651         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7652           expr = cp_parser_expression (parser, /*cast_p=*/false);
7653         else
7654           /* If the next token is a `;', then there is no
7655              expression.  */
7656           expr = NULL_TREE;
7657         /* Build the return-statement.  */
7658         statement = finish_return_stmt (expr);
7659         /* Look for the final `;'.  */
7660         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7661       }
7662       break;
7663
7664     case RID_GOTO:
7665       /* Create the goto-statement.  */
7666       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7667         {
7668           /* Issue a warning about this use of a GNU extension.  */
7669           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7670           /* Consume the '*' token.  */
7671           cp_lexer_consume_token (parser->lexer);
7672           /* Parse the dependent expression.  */
7673           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7674         }
7675       else
7676         finish_goto_stmt (cp_parser_identifier (parser));
7677       /* Look for the final `;'.  */
7678       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7679       break;
7680
7681     default:
7682       cp_parser_error (parser, "expected jump-statement");
7683       break;
7684     }
7685
7686   return statement;
7687 }
7688
7689 /* Parse a declaration-statement.
7690
7691    declaration-statement:
7692      block-declaration  */
7693
7694 static void
7695 cp_parser_declaration_statement (cp_parser* parser)
7696 {
7697   void *p;
7698
7699   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7700   p = obstack_alloc (&declarator_obstack, 0);
7701
7702  /* Parse the block-declaration.  */
7703   cp_parser_block_declaration (parser, /*statement_p=*/true);
7704
7705   /* Free any declarators allocated.  */
7706   obstack_free (&declarator_obstack, p);
7707
7708   /* Finish off the statement.  */
7709   finish_stmt ();
7710 }
7711
7712 /* Some dependent statements (like `if (cond) statement'), are
7713    implicitly in their own scope.  In other words, if the statement is
7714    a single statement (as opposed to a compound-statement), it is
7715    none-the-less treated as if it were enclosed in braces.  Any
7716    declarations appearing in the dependent statement are out of scope
7717    after control passes that point.  This function parses a statement,
7718    but ensures that is in its own scope, even if it is not a
7719    compound-statement.
7720
7721    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7722    is a (possibly labeled) if statement which is not enclosed in
7723    braces and has an else clause.  This is used to implement
7724    -Wparentheses.
7725
7726    Returns the new statement.  */
7727
7728 static tree
7729 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7730 {
7731   tree statement;
7732
7733   if (if_p != NULL)
7734     *if_p = false;
7735
7736   /* Mark if () ; with a special NOP_EXPR.  */
7737   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7738     {
7739       cp_lexer_consume_token (parser->lexer);
7740       statement = add_stmt (build_empty_stmt ());
7741     }
7742   /* if a compound is opened, we simply parse the statement directly.  */
7743   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7744     statement = cp_parser_compound_statement (parser, NULL, false);
7745   /* If the token is not a `{', then we must take special action.  */
7746   else
7747     {
7748       /* Create a compound-statement.  */
7749       statement = begin_compound_stmt (0);
7750       /* Parse the dependent-statement.  */
7751       cp_parser_statement (parser, NULL_TREE, false, if_p);
7752       /* Finish the dummy compound-statement.  */
7753       finish_compound_stmt (statement);
7754     }
7755
7756   /* Return the statement.  */
7757   return statement;
7758 }
7759
7760 /* For some dependent statements (like `while (cond) statement'), we
7761    have already created a scope.  Therefore, even if the dependent
7762    statement is a compound-statement, we do not want to create another
7763    scope.  */
7764
7765 static void
7766 cp_parser_already_scoped_statement (cp_parser* parser)
7767 {
7768   /* If the token is a `{', then we must take special action.  */
7769   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7770     cp_parser_statement (parser, NULL_TREE, false, NULL);
7771   else
7772     {
7773       /* Avoid calling cp_parser_compound_statement, so that we
7774          don't create a new scope.  Do everything else by hand.  */
7775       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7776       cp_parser_statement_seq_opt (parser, NULL_TREE);
7777       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7778     }
7779 }
7780
7781 /* Declarations [gram.dcl.dcl] */
7782
7783 /* Parse an optional declaration-sequence.
7784
7785    declaration-seq:
7786      declaration
7787      declaration-seq declaration  */
7788
7789 static void
7790 cp_parser_declaration_seq_opt (cp_parser* parser)
7791 {
7792   while (true)
7793     {
7794       cp_token *token;
7795
7796       token = cp_lexer_peek_token (parser->lexer);
7797
7798       if (token->type == CPP_CLOSE_BRACE
7799           || token->type == CPP_EOF
7800           || token->type == CPP_PRAGMA_EOL)
7801         break;
7802
7803       if (token->type == CPP_SEMICOLON)
7804         {
7805           /* A declaration consisting of a single semicolon is
7806              invalid.  Allow it unless we're being pedantic.  */
7807           cp_lexer_consume_token (parser->lexer);
7808           if (!in_system_header)
7809             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7810           continue;
7811         }
7812
7813       /* If we're entering or exiting a region that's implicitly
7814          extern "C", modify the lang context appropriately.  */
7815       if (!parser->implicit_extern_c && token->implicit_extern_c)
7816         {
7817           push_lang_context (lang_name_c);
7818           parser->implicit_extern_c = true;
7819         }
7820       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7821         {
7822           pop_lang_context ();
7823           parser->implicit_extern_c = false;
7824         }
7825
7826       if (token->type == CPP_PRAGMA)
7827         {
7828           /* A top-level declaration can consist solely of a #pragma.
7829              A nested declaration cannot, so this is done here and not
7830              in cp_parser_declaration.  (A #pragma at block scope is
7831              handled in cp_parser_statement.)  */
7832           cp_parser_pragma (parser, pragma_external);
7833           continue;
7834         }
7835
7836       /* Parse the declaration itself.  */
7837       cp_parser_declaration (parser);
7838     }
7839 }
7840
7841 /* Parse a declaration.
7842
7843    declaration:
7844      block-declaration
7845      function-definition
7846      template-declaration
7847      explicit-instantiation
7848      explicit-specialization
7849      linkage-specification
7850      namespace-definition
7851
7852    GNU extension:
7853
7854    declaration:
7855       __extension__ declaration */
7856
7857 static void
7858 cp_parser_declaration (cp_parser* parser)
7859 {
7860   cp_token token1;
7861   cp_token token2;
7862   int saved_pedantic;
7863   void *p;
7864
7865   /* Check for the `__extension__' keyword.  */
7866   if (cp_parser_extension_opt (parser, &saved_pedantic))
7867     {
7868       /* Parse the qualified declaration.  */
7869       cp_parser_declaration (parser);
7870       /* Restore the PEDANTIC flag.  */
7871       pedantic = saved_pedantic;
7872
7873       return;
7874     }
7875
7876   /* Try to figure out what kind of declaration is present.  */
7877   token1 = *cp_lexer_peek_token (parser->lexer);
7878
7879   if (token1.type != CPP_EOF)
7880     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7881   else
7882     {
7883       token2.type = CPP_EOF;
7884       token2.keyword = RID_MAX;
7885     }
7886
7887   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7888   p = obstack_alloc (&declarator_obstack, 0);
7889
7890   /* If the next token is `extern' and the following token is a string
7891      literal, then we have a linkage specification.  */
7892   if (token1.keyword == RID_EXTERN
7893       && cp_parser_is_string_literal (&token2))
7894     cp_parser_linkage_specification (parser);
7895   /* If the next token is `template', then we have either a template
7896      declaration, an explicit instantiation, or an explicit
7897      specialization.  */
7898   else if (token1.keyword == RID_TEMPLATE)
7899     {
7900       /* `template <>' indicates a template specialization.  */
7901       if (token2.type == CPP_LESS
7902           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7903         cp_parser_explicit_specialization (parser);
7904       /* `template <' indicates a template declaration.  */
7905       else if (token2.type == CPP_LESS)
7906         cp_parser_template_declaration (parser, /*member_p=*/false);
7907       /* Anything else must be an explicit instantiation.  */
7908       else
7909         cp_parser_explicit_instantiation (parser);
7910     }
7911   /* If the next token is `export', then we have a template
7912      declaration.  */
7913   else if (token1.keyword == RID_EXPORT)
7914     cp_parser_template_declaration (parser, /*member_p=*/false);
7915   /* If the next token is `extern', 'static' or 'inline' and the one
7916      after that is `template', we have a GNU extended explicit
7917      instantiation directive.  */
7918   else if (cp_parser_allow_gnu_extensions_p (parser)
7919            && (token1.keyword == RID_EXTERN
7920                || token1.keyword == RID_STATIC
7921                || token1.keyword == RID_INLINE)
7922            && token2.keyword == RID_TEMPLATE)
7923     cp_parser_explicit_instantiation (parser);
7924   /* If the next token is `namespace', check for a named or unnamed
7925      namespace definition.  */
7926   else if (token1.keyword == RID_NAMESPACE
7927            && (/* A named namespace definition.  */
7928                (token2.type == CPP_NAME
7929                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7930                     != CPP_EQ))
7931                /* An unnamed namespace definition.  */
7932                || token2.type == CPP_OPEN_BRACE
7933                || token2.keyword == RID_ATTRIBUTE))
7934     cp_parser_namespace_definition (parser);
7935   /* An inline (associated) namespace definition.  */
7936   else if (token1.keyword == RID_INLINE
7937            && token2.keyword == RID_NAMESPACE)
7938     cp_parser_namespace_definition (parser);
7939   /* Objective-C++ declaration/definition.  */
7940   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7941     cp_parser_objc_declaration (parser);
7942   /* We must have either a block declaration or a function
7943      definition.  */
7944   else
7945     /* Try to parse a block-declaration, or a function-definition.  */
7946     cp_parser_block_declaration (parser, /*statement_p=*/false);
7947
7948   /* Free any declarators allocated.  */
7949   obstack_free (&declarator_obstack, p);
7950 }
7951
7952 /* Parse a block-declaration.
7953
7954    block-declaration:
7955      simple-declaration
7956      asm-definition
7957      namespace-alias-definition
7958      using-declaration
7959      using-directive
7960
7961    GNU Extension:
7962
7963    block-declaration:
7964      __extension__ block-declaration
7965
7966    C++0x Extension:
7967
7968    block-declaration:
7969      static_assert-declaration
7970
7971    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7972    part of a declaration-statement.  */
7973
7974 static void
7975 cp_parser_block_declaration (cp_parser *parser,
7976                              bool      statement_p)
7977 {
7978   cp_token *token1;
7979   int saved_pedantic;
7980
7981   /* Check for the `__extension__' keyword.  */
7982   if (cp_parser_extension_opt (parser, &saved_pedantic))
7983     {
7984       /* Parse the qualified declaration.  */
7985       cp_parser_block_declaration (parser, statement_p);
7986       /* Restore the PEDANTIC flag.  */
7987       pedantic = saved_pedantic;
7988
7989       return;
7990     }
7991
7992   /* Peek at the next token to figure out which kind of declaration is
7993      present.  */
7994   token1 = cp_lexer_peek_token (parser->lexer);
7995
7996   /* If the next keyword is `asm', we have an asm-definition.  */
7997   if (token1->keyword == RID_ASM)
7998     {
7999       if (statement_p)
8000         cp_parser_commit_to_tentative_parse (parser);
8001       cp_parser_asm_definition (parser);
8002     }
8003   /* If the next keyword is `namespace', we have a
8004      namespace-alias-definition.  */
8005   else if (token1->keyword == RID_NAMESPACE)
8006     cp_parser_namespace_alias_definition (parser);
8007   /* If the next keyword is `using', we have either a
8008      using-declaration or a using-directive.  */
8009   else if (token1->keyword == RID_USING)
8010     {
8011       cp_token *token2;
8012
8013       if (statement_p)
8014         cp_parser_commit_to_tentative_parse (parser);
8015       /* If the token after `using' is `namespace', then we have a
8016          using-directive.  */
8017       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8018       if (token2->keyword == RID_NAMESPACE)
8019         cp_parser_using_directive (parser);
8020       /* Otherwise, it's a using-declaration.  */
8021       else
8022         cp_parser_using_declaration (parser,
8023                                      /*access_declaration_p=*/false);
8024     }
8025   /* If the next keyword is `__label__' we have a misplaced label
8026      declaration.  */
8027   else if (token1->keyword == RID_LABEL)
8028     {
8029       cp_lexer_consume_token (parser->lexer);
8030       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8031       cp_parser_skip_to_end_of_statement (parser);
8032       /* If the next token is now a `;', consume it.  */
8033       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8034         cp_lexer_consume_token (parser->lexer);
8035     }
8036   /* If the next token is `static_assert' we have a static assertion.  */
8037   else if (token1->keyword == RID_STATIC_ASSERT)
8038     cp_parser_static_assert (parser, /*member_p=*/false);
8039   /* Anything else must be a simple-declaration.  */
8040   else
8041     cp_parser_simple_declaration (parser, !statement_p);
8042 }
8043
8044 /* Parse a simple-declaration.
8045
8046    simple-declaration:
8047      decl-specifier-seq [opt] init-declarator-list [opt] ;
8048
8049    init-declarator-list:
8050      init-declarator
8051      init-declarator-list , init-declarator
8052
8053    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8054    function-definition as a simple-declaration.  */
8055
8056 static void
8057 cp_parser_simple_declaration (cp_parser* parser,
8058                               bool function_definition_allowed_p)
8059 {
8060   cp_decl_specifier_seq decl_specifiers;
8061   int declares_class_or_enum;
8062   bool saw_declarator;
8063
8064   /* Defer access checks until we know what is being declared; the
8065      checks for names appearing in the decl-specifier-seq should be
8066      done as if we were in the scope of the thing being declared.  */
8067   push_deferring_access_checks (dk_deferred);
8068
8069   /* Parse the decl-specifier-seq.  We have to keep track of whether
8070      or not the decl-specifier-seq declares a named class or
8071      enumeration type, since that is the only case in which the
8072      init-declarator-list is allowed to be empty.
8073
8074      [dcl.dcl]
8075
8076      In a simple-declaration, the optional init-declarator-list can be
8077      omitted only when declaring a class or enumeration, that is when
8078      the decl-specifier-seq contains either a class-specifier, an
8079      elaborated-type-specifier, or an enum-specifier.  */
8080   cp_parser_decl_specifier_seq (parser,
8081                                 CP_PARSER_FLAGS_OPTIONAL,
8082                                 &decl_specifiers,
8083                                 &declares_class_or_enum);
8084   /* We no longer need to defer access checks.  */
8085   stop_deferring_access_checks ();
8086
8087   /* In a block scope, a valid declaration must always have a
8088      decl-specifier-seq.  By not trying to parse declarators, we can
8089      resolve the declaration/expression ambiguity more quickly.  */
8090   if (!function_definition_allowed_p
8091       && !decl_specifiers.any_specifiers_p)
8092     {
8093       cp_parser_error (parser, "expected declaration");
8094       goto done;
8095     }
8096
8097   /* If the next two tokens are both identifiers, the code is
8098      erroneous. The usual cause of this situation is code like:
8099
8100        T t;
8101
8102      where "T" should name a type -- but does not.  */
8103   if (!decl_specifiers.type
8104       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8105     {
8106       /* If parsing tentatively, we should commit; we really are
8107          looking at a declaration.  */
8108       cp_parser_commit_to_tentative_parse (parser);
8109       /* Give up.  */
8110       goto done;
8111     }
8112
8113   /* If we have seen at least one decl-specifier, and the next token
8114      is not a parenthesis, then we must be looking at a declaration.
8115      (After "int (" we might be looking at a functional cast.)  */
8116   if (decl_specifiers.any_specifiers_p
8117       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8118       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8119     cp_parser_commit_to_tentative_parse (parser);
8120
8121   /* Keep going until we hit the `;' at the end of the simple
8122      declaration.  */
8123   saw_declarator = false;
8124   while (cp_lexer_next_token_is_not (parser->lexer,
8125                                      CPP_SEMICOLON))
8126     {
8127       cp_token *token;
8128       bool function_definition_p;
8129       tree decl;
8130
8131       if (saw_declarator)
8132         {
8133           /* If we are processing next declarator, coma is expected */
8134           token = cp_lexer_peek_token (parser->lexer);
8135           gcc_assert (token->type == CPP_COMMA);
8136           cp_lexer_consume_token (parser->lexer);
8137         }
8138       else
8139         saw_declarator = true;
8140
8141       /* Parse the init-declarator.  */
8142       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8143                                         /*checks=*/NULL,
8144                                         function_definition_allowed_p,
8145                                         /*member_p=*/false,
8146                                         declares_class_or_enum,
8147                                         &function_definition_p);
8148       /* If an error occurred while parsing tentatively, exit quickly.
8149          (That usually happens when in the body of a function; each
8150          statement is treated as a declaration-statement until proven
8151          otherwise.)  */
8152       if (cp_parser_error_occurred (parser))
8153         goto done;
8154       /* Handle function definitions specially.  */
8155       if (function_definition_p)
8156         {
8157           /* If the next token is a `,', then we are probably
8158              processing something like:
8159
8160                void f() {}, *p;
8161
8162              which is erroneous.  */
8163           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8164             {
8165               cp_token *token = cp_lexer_peek_token (parser->lexer);
8166               error ("%Hmixing declarations and function-definitions is forbidden",
8167                      &token->location);
8168             }
8169           /* Otherwise, we're done with the list of declarators.  */
8170           else
8171             {
8172               pop_deferring_access_checks ();
8173               return;
8174             }
8175         }
8176       /* The next token should be either a `,' or a `;'.  */
8177       token = cp_lexer_peek_token (parser->lexer);
8178       /* If it's a `,', there are more declarators to come.  */
8179       if (token->type == CPP_COMMA)
8180         /* will be consumed next time around */;
8181       /* If it's a `;', we are done.  */
8182       else if (token->type == CPP_SEMICOLON)
8183         break;
8184       /* Anything else is an error.  */
8185       else
8186         {
8187           /* If we have already issued an error message we don't need
8188              to issue another one.  */
8189           if (decl != error_mark_node
8190               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8191             cp_parser_error (parser, "expected %<,%> or %<;%>");
8192           /* Skip tokens until we reach the end of the statement.  */
8193           cp_parser_skip_to_end_of_statement (parser);
8194           /* If the next token is now a `;', consume it.  */
8195           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8196             cp_lexer_consume_token (parser->lexer);
8197           goto done;
8198         }
8199       /* After the first time around, a function-definition is not
8200          allowed -- even if it was OK at first.  For example:
8201
8202            int i, f() {}
8203
8204          is not valid.  */
8205       function_definition_allowed_p = false;
8206     }
8207
8208   /* Issue an error message if no declarators are present, and the
8209      decl-specifier-seq does not itself declare a class or
8210      enumeration.  */
8211   if (!saw_declarator)
8212     {
8213       if (cp_parser_declares_only_class_p (parser))
8214         shadow_tag (&decl_specifiers);
8215       /* Perform any deferred access checks.  */
8216       perform_deferred_access_checks ();
8217     }
8218
8219   /* Consume the `;'.  */
8220   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8221
8222  done:
8223   pop_deferring_access_checks ();
8224 }
8225
8226 /* Parse a decl-specifier-seq.
8227
8228    decl-specifier-seq:
8229      decl-specifier-seq [opt] decl-specifier
8230
8231    decl-specifier:
8232      storage-class-specifier
8233      type-specifier
8234      function-specifier
8235      friend
8236      typedef
8237
8238    GNU Extension:
8239
8240    decl-specifier:
8241      attributes
8242
8243    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8244
8245    The parser flags FLAGS is used to control type-specifier parsing.
8246
8247    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8248    flags:
8249
8250      1: one of the decl-specifiers is an elaborated-type-specifier
8251         (i.e., a type declaration)
8252      2: one of the decl-specifiers is an enum-specifier or a
8253         class-specifier (i.e., a type definition)
8254
8255    */
8256
8257 static void
8258 cp_parser_decl_specifier_seq (cp_parser* parser,
8259                               cp_parser_flags flags,
8260                               cp_decl_specifier_seq *decl_specs,
8261                               int* declares_class_or_enum)
8262 {
8263   bool constructor_possible_p = !parser->in_declarator_p;
8264   cp_token *start_token = NULL;
8265
8266   /* Clear DECL_SPECS.  */
8267   clear_decl_specs (decl_specs);
8268
8269   /* Assume no class or enumeration type is declared.  */
8270   *declares_class_or_enum = 0;
8271
8272   /* Keep reading specifiers until there are no more to read.  */
8273   while (true)
8274     {
8275       bool constructor_p;
8276       bool found_decl_spec;
8277       cp_token *token;
8278
8279       /* Peek at the next token.  */
8280       token = cp_lexer_peek_token (parser->lexer);
8281
8282       /* Save the first token of the decl spec list for error
8283          reporting.  */
8284       if (!start_token)
8285         start_token = token;
8286       /* Handle attributes.  */
8287       if (token->keyword == RID_ATTRIBUTE)
8288         {
8289           /* Parse the attributes.  */
8290           decl_specs->attributes
8291             = chainon (decl_specs->attributes,
8292                        cp_parser_attributes_opt (parser));
8293           continue;
8294         }
8295       /* Assume we will find a decl-specifier keyword.  */
8296       found_decl_spec = true;
8297       /* If the next token is an appropriate keyword, we can simply
8298          add it to the list.  */
8299       switch (token->keyword)
8300         {
8301           /* decl-specifier:
8302                friend  */
8303         case RID_FRIEND:
8304           if (!at_class_scope_p ())
8305             {
8306               error ("%H%<friend%> used outside of class", &token->location);
8307               cp_lexer_purge_token (parser->lexer);
8308             }
8309           else
8310             {
8311               ++decl_specs->specs[(int) ds_friend];
8312               /* Consume the token.  */
8313               cp_lexer_consume_token (parser->lexer);
8314             }
8315           break;
8316
8317           /* function-specifier:
8318                inline
8319                virtual
8320                explicit  */
8321         case RID_INLINE:
8322         case RID_VIRTUAL:
8323         case RID_EXPLICIT:
8324           cp_parser_function_specifier_opt (parser, decl_specs);
8325           break;
8326
8327           /* decl-specifier:
8328                typedef  */
8329         case RID_TYPEDEF:
8330           ++decl_specs->specs[(int) ds_typedef];
8331           /* Consume the token.  */
8332           cp_lexer_consume_token (parser->lexer);
8333           /* A constructor declarator cannot appear in a typedef.  */
8334           constructor_possible_p = false;
8335           /* The "typedef" keyword can only occur in a declaration; we
8336              may as well commit at this point.  */
8337           cp_parser_commit_to_tentative_parse (parser);
8338
8339           if (decl_specs->storage_class != sc_none)
8340             decl_specs->conflicting_specifiers_p = true;
8341           break;
8342
8343           /* storage-class-specifier:
8344                auto
8345                register
8346                static
8347                extern
8348                mutable
8349
8350              GNU Extension:
8351                thread  */
8352         case RID_AUTO:
8353           if (cxx_dialect == cxx98) 
8354             {
8355               /* Consume the token.  */
8356               cp_lexer_consume_token (parser->lexer);
8357
8358               /* Complain about `auto' as a storage specifier, if
8359                  we're complaining about C++0x compatibility.  */
8360               warning 
8361                 (OPT_Wc__0x_compat, 
8362                  "%H%<auto%> will change meaning in C++0x; please remove it",
8363                  &token->location);
8364
8365               /* Set the storage class anyway.  */
8366               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8367                                            token->location);
8368             }
8369           else
8370             /* C++0x auto type-specifier.  */
8371             found_decl_spec = false;
8372           break;
8373
8374         case RID_REGISTER:
8375         case RID_STATIC:
8376         case RID_EXTERN:
8377         case RID_MUTABLE:
8378           /* Consume the token.  */
8379           cp_lexer_consume_token (parser->lexer);
8380           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8381                                        token->location);
8382           break;
8383         case RID_THREAD:
8384           /* Consume the token.  */
8385           cp_lexer_consume_token (parser->lexer);
8386           ++decl_specs->specs[(int) ds_thread];
8387           break;
8388
8389         default:
8390           /* We did not yet find a decl-specifier yet.  */
8391           found_decl_spec = false;
8392           break;
8393         }
8394
8395       /* Constructors are a special case.  The `S' in `S()' is not a
8396          decl-specifier; it is the beginning of the declarator.  */
8397       constructor_p
8398         = (!found_decl_spec
8399            && constructor_possible_p
8400            && (cp_parser_constructor_declarator_p
8401                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8402
8403       /* If we don't have a DECL_SPEC yet, then we must be looking at
8404          a type-specifier.  */
8405       if (!found_decl_spec && !constructor_p)
8406         {
8407           int decl_spec_declares_class_or_enum;
8408           bool is_cv_qualifier;
8409           tree type_spec;
8410
8411           type_spec
8412             = cp_parser_type_specifier (parser, flags,
8413                                         decl_specs,
8414                                         /*is_declaration=*/true,
8415                                         &decl_spec_declares_class_or_enum,
8416                                         &is_cv_qualifier);
8417           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8418
8419           /* If this type-specifier referenced a user-defined type
8420              (a typedef, class-name, etc.), then we can't allow any
8421              more such type-specifiers henceforth.
8422
8423              [dcl.spec]
8424
8425              The longest sequence of decl-specifiers that could
8426              possibly be a type name is taken as the
8427              decl-specifier-seq of a declaration.  The sequence shall
8428              be self-consistent as described below.
8429
8430              [dcl.type]
8431
8432              As a general rule, at most one type-specifier is allowed
8433              in the complete decl-specifier-seq of a declaration.  The
8434              only exceptions are the following:
8435
8436              -- const or volatile can be combined with any other
8437                 type-specifier.
8438
8439              -- signed or unsigned can be combined with char, long,
8440                 short, or int.
8441
8442              -- ..
8443
8444              Example:
8445
8446                typedef char* Pc;
8447                void g (const int Pc);
8448
8449              Here, Pc is *not* part of the decl-specifier seq; it's
8450              the declarator.  Therefore, once we see a type-specifier
8451              (other than a cv-qualifier), we forbid any additional
8452              user-defined types.  We *do* still allow things like `int
8453              int' to be considered a decl-specifier-seq, and issue the
8454              error message later.  */
8455           if (type_spec && !is_cv_qualifier)
8456             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8457           /* A constructor declarator cannot follow a type-specifier.  */
8458           if (type_spec)
8459             {
8460               constructor_possible_p = false;
8461               found_decl_spec = true;
8462             }
8463         }
8464
8465       /* If we still do not have a DECL_SPEC, then there are no more
8466          decl-specifiers.  */
8467       if (!found_decl_spec)
8468         break;
8469
8470       decl_specs->any_specifiers_p = true;
8471       /* After we see one decl-specifier, further decl-specifiers are
8472          always optional.  */
8473       flags |= CP_PARSER_FLAGS_OPTIONAL;
8474     }
8475
8476   cp_parser_check_decl_spec (decl_specs, start_token->location);
8477
8478   /* Don't allow a friend specifier with a class definition.  */
8479   if (decl_specs->specs[(int) ds_friend] != 0
8480       && (*declares_class_or_enum & 2))
8481     error ("%Hclass definition may not be declared a friend",
8482             &start_token->location);
8483 }
8484
8485 /* Parse an (optional) storage-class-specifier.
8486
8487    storage-class-specifier:
8488      auto
8489      register
8490      static
8491      extern
8492      mutable
8493
8494    GNU Extension:
8495
8496    storage-class-specifier:
8497      thread
8498
8499    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8500
8501 static tree
8502 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8503 {
8504   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8505     {
8506     case RID_AUTO:
8507       if (cxx_dialect != cxx98)
8508         return NULL_TREE;
8509       /* Fall through for C++98.  */
8510
8511     case RID_REGISTER:
8512     case RID_STATIC:
8513     case RID_EXTERN:
8514     case RID_MUTABLE:
8515     case RID_THREAD:
8516       /* Consume the token.  */
8517       return cp_lexer_consume_token (parser->lexer)->u.value;
8518
8519     default:
8520       return NULL_TREE;
8521     }
8522 }
8523
8524 /* Parse an (optional) function-specifier.
8525
8526    function-specifier:
8527      inline
8528      virtual
8529      explicit
8530
8531    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8532    Updates DECL_SPECS, if it is non-NULL.  */
8533
8534 static tree
8535 cp_parser_function_specifier_opt (cp_parser* parser,
8536                                   cp_decl_specifier_seq *decl_specs)
8537 {
8538   cp_token *token = cp_lexer_peek_token (parser->lexer);
8539   switch (token->keyword)
8540     {
8541     case RID_INLINE:
8542       if (decl_specs)
8543         ++decl_specs->specs[(int) ds_inline];
8544       break;
8545
8546     case RID_VIRTUAL:
8547       /* 14.5.2.3 [temp.mem]
8548
8549          A member function template shall not be virtual.  */
8550       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8551         error ("%Htemplates may not be %<virtual%>", &token->location);
8552       else if (decl_specs)
8553         ++decl_specs->specs[(int) ds_virtual];
8554       break;
8555
8556     case RID_EXPLICIT:
8557       if (decl_specs)
8558         ++decl_specs->specs[(int) ds_explicit];
8559       break;
8560
8561     default:
8562       return NULL_TREE;
8563     }
8564
8565   /* Consume the token.  */
8566   return cp_lexer_consume_token (parser->lexer)->u.value;
8567 }
8568
8569 /* Parse a linkage-specification.
8570
8571    linkage-specification:
8572      extern string-literal { declaration-seq [opt] }
8573      extern string-literal declaration  */
8574
8575 static void
8576 cp_parser_linkage_specification (cp_parser* parser)
8577 {
8578   tree linkage;
8579
8580   /* Look for the `extern' keyword.  */
8581   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8582
8583   /* Look for the string-literal.  */
8584   linkage = cp_parser_string_literal (parser, false, false);
8585
8586   /* Transform the literal into an identifier.  If the literal is a
8587      wide-character string, or contains embedded NULs, then we can't
8588      handle it as the user wants.  */
8589   if (strlen (TREE_STRING_POINTER (linkage))
8590       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8591     {
8592       cp_parser_error (parser, "invalid linkage-specification");
8593       /* Assume C++ linkage.  */
8594       linkage = lang_name_cplusplus;
8595     }
8596   else
8597     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8598
8599   /* We're now using the new linkage.  */
8600   push_lang_context (linkage);
8601
8602   /* If the next token is a `{', then we're using the first
8603      production.  */
8604   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8605     {
8606       /* Consume the `{' token.  */
8607       cp_lexer_consume_token (parser->lexer);
8608       /* Parse the declarations.  */
8609       cp_parser_declaration_seq_opt (parser);
8610       /* Look for the closing `}'.  */
8611       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8612     }
8613   /* Otherwise, there's just one declaration.  */
8614   else
8615     {
8616       bool saved_in_unbraced_linkage_specification_p;
8617
8618       saved_in_unbraced_linkage_specification_p
8619         = parser->in_unbraced_linkage_specification_p;
8620       parser->in_unbraced_linkage_specification_p = true;
8621       cp_parser_declaration (parser);
8622       parser->in_unbraced_linkage_specification_p
8623         = saved_in_unbraced_linkage_specification_p;
8624     }
8625
8626   /* We're done with the linkage-specification.  */
8627   pop_lang_context ();
8628 }
8629
8630 /* Parse a static_assert-declaration.
8631
8632    static_assert-declaration:
8633      static_assert ( constant-expression , string-literal ) ; 
8634
8635    If MEMBER_P, this static_assert is a class member.  */
8636
8637 static void 
8638 cp_parser_static_assert(cp_parser *parser, bool member_p)
8639 {
8640   tree condition;
8641   tree message;
8642   cp_token *token;
8643   location_t saved_loc;
8644
8645   /* Peek at the `static_assert' token so we can keep track of exactly
8646      where the static assertion started.  */
8647   token = cp_lexer_peek_token (parser->lexer);
8648   saved_loc = token->location;
8649
8650   /* Look for the `static_assert' keyword.  */
8651   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8652                                   "%<static_assert%>"))
8653     return;
8654
8655   /*  We know we are in a static assertion; commit to any tentative
8656       parse.  */
8657   if (cp_parser_parsing_tentatively (parser))
8658     cp_parser_commit_to_tentative_parse (parser);
8659
8660   /* Parse the `(' starting the static assertion condition.  */
8661   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8662
8663   /* Parse the constant-expression.  */
8664   condition = 
8665     cp_parser_constant_expression (parser,
8666                                    /*allow_non_constant_p=*/false,
8667                                    /*non_constant_p=*/NULL);
8668
8669   /* Parse the separating `,'.  */
8670   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8671
8672   /* Parse the string-literal message.  */
8673   message = cp_parser_string_literal (parser, 
8674                                       /*translate=*/false,
8675                                       /*wide_ok=*/true);
8676
8677   /* A `)' completes the static assertion.  */
8678   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8679     cp_parser_skip_to_closing_parenthesis (parser, 
8680                                            /*recovering=*/true, 
8681                                            /*or_comma=*/false,
8682                                            /*consume_paren=*/true);
8683
8684   /* A semicolon terminates the declaration.  */
8685   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8686
8687   /* Complete the static assertion, which may mean either processing 
8688      the static assert now or saving it for template instantiation.  */
8689   finish_static_assert (condition, message, saved_loc, member_p);
8690 }
8691
8692 /* Parse a `decltype' type. Returns the type. 
8693
8694    simple-type-specifier:
8695      decltype ( expression )  */
8696
8697 static tree
8698 cp_parser_decltype (cp_parser *parser)
8699 {
8700   tree expr;
8701   bool id_expression_or_member_access_p = false;
8702   const char *saved_message;
8703   bool saved_integral_constant_expression_p;
8704   bool saved_non_integral_constant_expression_p;
8705   cp_token *id_expr_start_token;
8706
8707   /* Look for the `decltype' token.  */
8708   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8709     return error_mark_node;
8710
8711   /* Types cannot be defined in a `decltype' expression.  Save away the
8712      old message.  */
8713   saved_message = parser->type_definition_forbidden_message;
8714
8715   /* And create the new one.  */
8716   parser->type_definition_forbidden_message
8717     = "types may not be defined in %<decltype%> expressions";
8718
8719   /* The restrictions on constant-expressions do not apply inside
8720      decltype expressions.  */
8721   saved_integral_constant_expression_p
8722     = parser->integral_constant_expression_p;
8723   saved_non_integral_constant_expression_p
8724     = parser->non_integral_constant_expression_p;
8725   parser->integral_constant_expression_p = false;
8726
8727   /* Do not actually evaluate the expression.  */
8728   ++skip_evaluation;
8729
8730   /* Parse the opening `('.  */
8731   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8732     return error_mark_node;
8733   
8734   /* First, try parsing an id-expression.  */
8735   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8736   cp_parser_parse_tentatively (parser);
8737   expr = cp_parser_id_expression (parser,
8738                                   /*template_keyword_p=*/false,
8739                                   /*check_dependency_p=*/true,
8740                                   /*template_p=*/NULL,
8741                                   /*declarator_p=*/false,
8742                                   /*optional_p=*/false);
8743
8744   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8745     {
8746       bool non_integral_constant_expression_p = false;
8747       tree id_expression = expr;
8748       cp_id_kind idk;
8749       const char *error_msg;
8750
8751       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8752         /* Lookup the name we got back from the id-expression.  */
8753         expr = cp_parser_lookup_name (parser, expr,
8754                                       none_type,
8755                                       /*is_template=*/false,
8756                                       /*is_namespace=*/false,
8757                                       /*check_dependency=*/true,
8758                                       /*ambiguous_decls=*/NULL,
8759                                       id_expr_start_token->location);
8760
8761       if (expr
8762           && expr != error_mark_node
8763           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8764           && TREE_CODE (expr) != TYPE_DECL
8765           && (TREE_CODE (expr) != BIT_NOT_EXPR
8766               || !TYPE_P (TREE_OPERAND (expr, 0)))
8767           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8768         {
8769           /* Complete lookup of the id-expression.  */
8770           expr = (finish_id_expression
8771                   (id_expression, expr, parser->scope, &idk,
8772                    /*integral_constant_expression_p=*/false,
8773                    /*allow_non_integral_constant_expression_p=*/true,
8774                    &non_integral_constant_expression_p,
8775                    /*template_p=*/false,
8776                    /*done=*/true,
8777                    /*address_p=*/false,
8778                    /*template_arg_p=*/false,
8779                    &error_msg,
8780                    id_expr_start_token->location));
8781
8782           if (expr == error_mark_node)
8783             /* We found an id-expression, but it was something that we
8784                should not have found. This is an error, not something
8785                we can recover from, so note that we found an
8786                id-expression and we'll recover as gracefully as
8787                possible.  */
8788             id_expression_or_member_access_p = true;
8789         }
8790
8791       if (expr 
8792           && expr != error_mark_node
8793           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8794         /* We have an id-expression.  */
8795         id_expression_or_member_access_p = true;
8796     }
8797
8798   if (!id_expression_or_member_access_p)
8799     {
8800       /* Abort the id-expression parse.  */
8801       cp_parser_abort_tentative_parse (parser);
8802
8803       /* Parsing tentatively, again.  */
8804       cp_parser_parse_tentatively (parser);
8805
8806       /* Parse a class member access.  */
8807       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8808                                            /*cast_p=*/false,
8809                                            /*member_access_only_p=*/true);
8810
8811       if (expr 
8812           && expr != error_mark_node
8813           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8814         /* We have an id-expression.  */
8815         id_expression_or_member_access_p = true;
8816     }
8817
8818   if (id_expression_or_member_access_p)
8819     /* We have parsed the complete id-expression or member access.  */
8820     cp_parser_parse_definitely (parser);
8821   else
8822     {
8823       /* Abort our attempt to parse an id-expression or member access
8824          expression.  */
8825       cp_parser_abort_tentative_parse (parser);
8826
8827       /* Parse a full expression.  */
8828       expr = cp_parser_expression (parser, /*cast_p=*/false);
8829     }
8830
8831   /* Go back to evaluating expressions.  */
8832   --skip_evaluation;
8833
8834   /* Restore the old message and the integral constant expression
8835      flags.  */
8836   parser->type_definition_forbidden_message = saved_message;
8837   parser->integral_constant_expression_p
8838     = saved_integral_constant_expression_p;
8839   parser->non_integral_constant_expression_p
8840     = saved_non_integral_constant_expression_p;
8841
8842   if (expr == error_mark_node)
8843     {
8844       /* Skip everything up to the closing `)'.  */
8845       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8846                                              /*consume_paren=*/true);
8847       return error_mark_node;
8848     }
8849   
8850   /* Parse to the closing `)'.  */
8851   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8852     {
8853       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8854                                              /*consume_paren=*/true);
8855       return error_mark_node;
8856     }
8857
8858   return finish_decltype_type (expr, id_expression_or_member_access_p);
8859 }
8860
8861 /* Special member functions [gram.special] */
8862
8863 /* Parse a conversion-function-id.
8864
8865    conversion-function-id:
8866      operator conversion-type-id
8867
8868    Returns an IDENTIFIER_NODE representing the operator.  */
8869
8870 static tree
8871 cp_parser_conversion_function_id (cp_parser* parser)
8872 {
8873   tree type;
8874   tree saved_scope;
8875   tree saved_qualifying_scope;
8876   tree saved_object_scope;
8877   tree pushed_scope = NULL_TREE;
8878
8879   /* Look for the `operator' token.  */
8880   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8881     return error_mark_node;
8882   /* When we parse the conversion-type-id, the current scope will be
8883      reset.  However, we need that information in able to look up the
8884      conversion function later, so we save it here.  */
8885   saved_scope = parser->scope;
8886   saved_qualifying_scope = parser->qualifying_scope;
8887   saved_object_scope = parser->object_scope;
8888   /* We must enter the scope of the class so that the names of
8889      entities declared within the class are available in the
8890      conversion-type-id.  For example, consider:
8891
8892        struct S {
8893          typedef int I;
8894          operator I();
8895        };
8896
8897        S::operator I() { ... }
8898
8899      In order to see that `I' is a type-name in the definition, we
8900      must be in the scope of `S'.  */
8901   if (saved_scope)
8902     pushed_scope = push_scope (saved_scope);
8903   /* Parse the conversion-type-id.  */
8904   type = cp_parser_conversion_type_id (parser);
8905   /* Leave the scope of the class, if any.  */
8906   if (pushed_scope)
8907     pop_scope (pushed_scope);
8908   /* Restore the saved scope.  */
8909   parser->scope = saved_scope;
8910   parser->qualifying_scope = saved_qualifying_scope;
8911   parser->object_scope = saved_object_scope;
8912   /* If the TYPE is invalid, indicate failure.  */
8913   if (type == error_mark_node)
8914     return error_mark_node;
8915   return mangle_conv_op_name_for_type (type);
8916 }
8917
8918 /* Parse a conversion-type-id:
8919
8920    conversion-type-id:
8921      type-specifier-seq conversion-declarator [opt]
8922
8923    Returns the TYPE specified.  */
8924
8925 static tree
8926 cp_parser_conversion_type_id (cp_parser* parser)
8927 {
8928   tree attributes;
8929   cp_decl_specifier_seq type_specifiers;
8930   cp_declarator *declarator;
8931   tree type_specified;
8932
8933   /* Parse the attributes.  */
8934   attributes = cp_parser_attributes_opt (parser);
8935   /* Parse the type-specifiers.  */
8936   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8937                                 &type_specifiers);
8938   /* If that didn't work, stop.  */
8939   if (type_specifiers.type == error_mark_node)
8940     return error_mark_node;
8941   /* Parse the conversion-declarator.  */
8942   declarator = cp_parser_conversion_declarator_opt (parser);
8943
8944   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8945                                     /*initialized=*/0, &attributes);
8946   if (attributes)
8947     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8948   return type_specified;
8949 }
8950
8951 /* Parse an (optional) conversion-declarator.
8952
8953    conversion-declarator:
8954      ptr-operator conversion-declarator [opt]
8955
8956    */
8957
8958 static cp_declarator *
8959 cp_parser_conversion_declarator_opt (cp_parser* parser)
8960 {
8961   enum tree_code code;
8962   tree class_type;
8963   cp_cv_quals cv_quals;
8964
8965   /* We don't know if there's a ptr-operator next, or not.  */
8966   cp_parser_parse_tentatively (parser);
8967   /* Try the ptr-operator.  */
8968   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8969   /* If it worked, look for more conversion-declarators.  */
8970   if (cp_parser_parse_definitely (parser))
8971     {
8972       cp_declarator *declarator;
8973
8974       /* Parse another optional declarator.  */
8975       declarator = cp_parser_conversion_declarator_opt (parser);
8976
8977       return cp_parser_make_indirect_declarator
8978         (code, class_type, cv_quals, declarator);
8979    }
8980
8981   return NULL;
8982 }
8983
8984 /* Parse an (optional) ctor-initializer.
8985
8986    ctor-initializer:
8987      : mem-initializer-list
8988
8989    Returns TRUE iff the ctor-initializer was actually present.  */
8990
8991 static bool
8992 cp_parser_ctor_initializer_opt (cp_parser* parser)
8993 {
8994   /* If the next token is not a `:', then there is no
8995      ctor-initializer.  */
8996   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8997     {
8998       /* Do default initialization of any bases and members.  */
8999       if (DECL_CONSTRUCTOR_P (current_function_decl))
9000         finish_mem_initializers (NULL_TREE);
9001
9002       return false;
9003     }
9004
9005   /* Consume the `:' token.  */
9006   cp_lexer_consume_token (parser->lexer);
9007   /* And the mem-initializer-list.  */
9008   cp_parser_mem_initializer_list (parser);
9009
9010   return true;
9011 }
9012
9013 /* Parse a mem-initializer-list.
9014
9015    mem-initializer-list:
9016      mem-initializer ... [opt]
9017      mem-initializer ... [opt] , mem-initializer-list  */
9018
9019 static void
9020 cp_parser_mem_initializer_list (cp_parser* parser)
9021 {
9022   tree mem_initializer_list = NULL_TREE;
9023   cp_token *token = cp_lexer_peek_token (parser->lexer);
9024
9025   /* Let the semantic analysis code know that we are starting the
9026      mem-initializer-list.  */
9027   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9028     error ("%Honly constructors take base initializers",
9029            &token->location);
9030
9031   /* Loop through the list.  */
9032   while (true)
9033     {
9034       tree mem_initializer;
9035
9036       token = cp_lexer_peek_token (parser->lexer);
9037       /* Parse the mem-initializer.  */
9038       mem_initializer = cp_parser_mem_initializer (parser);
9039       /* If the next token is a `...', we're expanding member initializers. */
9040       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9041         {
9042           /* Consume the `...'. */
9043           cp_lexer_consume_token (parser->lexer);
9044
9045           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9046              can be expanded but members cannot. */
9047           if (mem_initializer != error_mark_node
9048               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9049             {
9050               error ("%Hcannot expand initializer for member %<%D%>",
9051                      &token->location, TREE_PURPOSE (mem_initializer));
9052               mem_initializer = error_mark_node;
9053             }
9054
9055           /* Construct the pack expansion type. */
9056           if (mem_initializer != error_mark_node)
9057             mem_initializer = make_pack_expansion (mem_initializer);
9058         }
9059       /* Add it to the list, unless it was erroneous.  */
9060       if (mem_initializer != error_mark_node)
9061         {
9062           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9063           mem_initializer_list = mem_initializer;
9064         }
9065       /* If the next token is not a `,', we're done.  */
9066       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9067         break;
9068       /* Consume the `,' token.  */
9069       cp_lexer_consume_token (parser->lexer);
9070     }
9071
9072   /* Perform semantic analysis.  */
9073   if (DECL_CONSTRUCTOR_P (current_function_decl))
9074     finish_mem_initializers (mem_initializer_list);
9075 }
9076
9077 /* Parse a mem-initializer.
9078
9079    mem-initializer:
9080      mem-initializer-id ( expression-list [opt] )
9081      mem-initializer-id braced-init-list
9082
9083    GNU extension:
9084
9085    mem-initializer:
9086      ( expression-list [opt] )
9087
9088    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9089    class) or FIELD_DECL (for a non-static data member) to initialize;
9090    the TREE_VALUE is the expression-list.  An empty initialization
9091    list is represented by void_list_node.  */
9092
9093 static tree
9094 cp_parser_mem_initializer (cp_parser* parser)
9095 {
9096   tree mem_initializer_id;
9097   tree expression_list;
9098   tree member;
9099   cp_token *token = cp_lexer_peek_token (parser->lexer);
9100
9101   /* Find out what is being initialized.  */
9102   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9103     {
9104       permerror (token->location,
9105                  "anachronistic old-style base class initializer");
9106       mem_initializer_id = NULL_TREE;
9107     }
9108   else
9109     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9110   member = expand_member_init (mem_initializer_id);
9111   if (member && !DECL_P (member))
9112     in_base_initializer = 1;
9113
9114   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9115     {
9116       bool expr_non_constant_p;
9117       maybe_warn_cpp0x ("extended initializer lists");
9118       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9119       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9120       expression_list = build_tree_list (NULL_TREE, expression_list);
9121     }
9122   else
9123     expression_list
9124       = cp_parser_parenthesized_expression_list (parser, false,
9125                                                  /*cast_p=*/false,
9126                                                  /*allow_expansion_p=*/true,
9127                                                  /*non_constant_p=*/NULL);
9128   if (expression_list == error_mark_node)
9129     return error_mark_node;
9130   if (!expression_list)
9131     expression_list = void_type_node;
9132
9133   in_base_initializer = 0;
9134
9135   return member ? build_tree_list (member, expression_list) : error_mark_node;
9136 }
9137
9138 /* Parse a mem-initializer-id.
9139
9140    mem-initializer-id:
9141      :: [opt] nested-name-specifier [opt] class-name
9142      identifier
9143
9144    Returns a TYPE indicating the class to be initializer for the first
9145    production.  Returns an IDENTIFIER_NODE indicating the data member
9146    to be initialized for the second production.  */
9147
9148 static tree
9149 cp_parser_mem_initializer_id (cp_parser* parser)
9150 {
9151   bool global_scope_p;
9152   bool nested_name_specifier_p;
9153   bool template_p = false;
9154   tree id;
9155
9156   cp_token *token = cp_lexer_peek_token (parser->lexer);
9157
9158   /* `typename' is not allowed in this context ([temp.res]).  */
9159   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9160     {
9161       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9162              "member initializer is implicitly a type)",
9163              &token->location);
9164       cp_lexer_consume_token (parser->lexer);
9165     }
9166   /* Look for the optional `::' operator.  */
9167   global_scope_p
9168     = (cp_parser_global_scope_opt (parser,
9169                                    /*current_scope_valid_p=*/false)
9170        != NULL_TREE);
9171   /* Look for the optional nested-name-specifier.  The simplest way to
9172      implement:
9173
9174        [temp.res]
9175
9176        The keyword `typename' is not permitted in a base-specifier or
9177        mem-initializer; in these contexts a qualified name that
9178        depends on a template-parameter is implicitly assumed to be a
9179        type name.
9180
9181      is to assume that we have seen the `typename' keyword at this
9182      point.  */
9183   nested_name_specifier_p
9184     = (cp_parser_nested_name_specifier_opt (parser,
9185                                             /*typename_keyword_p=*/true,
9186                                             /*check_dependency_p=*/true,
9187                                             /*type_p=*/true,
9188                                             /*is_declaration=*/true)
9189        != NULL_TREE);
9190   if (nested_name_specifier_p)
9191     template_p = cp_parser_optional_template_keyword (parser);
9192   /* If there is a `::' operator or a nested-name-specifier, then we
9193      are definitely looking for a class-name.  */
9194   if (global_scope_p || nested_name_specifier_p)
9195     return cp_parser_class_name (parser,
9196                                  /*typename_keyword_p=*/true,
9197                                  /*template_keyword_p=*/template_p,
9198                                  none_type,
9199                                  /*check_dependency_p=*/true,
9200                                  /*class_head_p=*/false,
9201                                  /*is_declaration=*/true);
9202   /* Otherwise, we could also be looking for an ordinary identifier.  */
9203   cp_parser_parse_tentatively (parser);
9204   /* Try a class-name.  */
9205   id = cp_parser_class_name (parser,
9206                              /*typename_keyword_p=*/true,
9207                              /*template_keyword_p=*/false,
9208                              none_type,
9209                              /*check_dependency_p=*/true,
9210                              /*class_head_p=*/false,
9211                              /*is_declaration=*/true);
9212   /* If we found one, we're done.  */
9213   if (cp_parser_parse_definitely (parser))
9214     return id;
9215   /* Otherwise, look for an ordinary identifier.  */
9216   return cp_parser_identifier (parser);
9217 }
9218
9219 /* Overloading [gram.over] */
9220
9221 /* Parse an operator-function-id.
9222
9223    operator-function-id:
9224      operator operator
9225
9226    Returns an IDENTIFIER_NODE for the operator which is a
9227    human-readable spelling of the identifier, e.g., `operator +'.  */
9228
9229 static tree
9230 cp_parser_operator_function_id (cp_parser* parser)
9231 {
9232   /* Look for the `operator' keyword.  */
9233   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9234     return error_mark_node;
9235   /* And then the name of the operator itself.  */
9236   return cp_parser_operator (parser);
9237 }
9238
9239 /* Parse an operator.
9240
9241    operator:
9242      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9243      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9244      || ++ -- , ->* -> () []
9245
9246    GNU Extensions:
9247
9248    operator:
9249      <? >? <?= >?=
9250
9251    Returns an IDENTIFIER_NODE for the operator which is a
9252    human-readable spelling of the identifier, e.g., `operator +'.  */
9253
9254 static tree
9255 cp_parser_operator (cp_parser* parser)
9256 {
9257   tree id = NULL_TREE;
9258   cp_token *token;
9259
9260   /* Peek at the next token.  */
9261   token = cp_lexer_peek_token (parser->lexer);
9262   /* Figure out which operator we have.  */
9263   switch (token->type)
9264     {
9265     case CPP_KEYWORD:
9266       {
9267         enum tree_code op;
9268
9269         /* The keyword should be either `new' or `delete'.  */
9270         if (token->keyword == RID_NEW)
9271           op = NEW_EXPR;
9272         else if (token->keyword == RID_DELETE)
9273           op = DELETE_EXPR;
9274         else
9275           break;
9276
9277         /* Consume the `new' or `delete' token.  */
9278         cp_lexer_consume_token (parser->lexer);
9279
9280         /* Peek at the next token.  */
9281         token = cp_lexer_peek_token (parser->lexer);
9282         /* If it's a `[' token then this is the array variant of the
9283            operator.  */
9284         if (token->type == CPP_OPEN_SQUARE)
9285           {
9286             /* Consume the `[' token.  */
9287             cp_lexer_consume_token (parser->lexer);
9288             /* Look for the `]' token.  */
9289             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9290             id = ansi_opname (op == NEW_EXPR
9291                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9292           }
9293         /* Otherwise, we have the non-array variant.  */
9294         else
9295           id = ansi_opname (op);
9296
9297         return id;
9298       }
9299
9300     case CPP_PLUS:
9301       id = ansi_opname (PLUS_EXPR);
9302       break;
9303
9304     case CPP_MINUS:
9305       id = ansi_opname (MINUS_EXPR);
9306       break;
9307
9308     case CPP_MULT:
9309       id = ansi_opname (MULT_EXPR);
9310       break;
9311
9312     case CPP_DIV:
9313       id = ansi_opname (TRUNC_DIV_EXPR);
9314       break;
9315
9316     case CPP_MOD:
9317       id = ansi_opname (TRUNC_MOD_EXPR);
9318       break;
9319
9320     case CPP_XOR:
9321       id = ansi_opname (BIT_XOR_EXPR);
9322       break;
9323
9324     case CPP_AND:
9325       id = ansi_opname (BIT_AND_EXPR);
9326       break;
9327
9328     case CPP_OR:
9329       id = ansi_opname (BIT_IOR_EXPR);
9330       break;
9331
9332     case CPP_COMPL:
9333       id = ansi_opname (BIT_NOT_EXPR);
9334       break;
9335
9336     case CPP_NOT:
9337       id = ansi_opname (TRUTH_NOT_EXPR);
9338       break;
9339
9340     case CPP_EQ:
9341       id = ansi_assopname (NOP_EXPR);
9342       break;
9343
9344     case CPP_LESS:
9345       id = ansi_opname (LT_EXPR);
9346       break;
9347
9348     case CPP_GREATER:
9349       id = ansi_opname (GT_EXPR);
9350       break;
9351
9352     case CPP_PLUS_EQ:
9353       id = ansi_assopname (PLUS_EXPR);
9354       break;
9355
9356     case CPP_MINUS_EQ:
9357       id = ansi_assopname (MINUS_EXPR);
9358       break;
9359
9360     case CPP_MULT_EQ:
9361       id = ansi_assopname (MULT_EXPR);
9362       break;
9363
9364     case CPP_DIV_EQ:
9365       id = ansi_assopname (TRUNC_DIV_EXPR);
9366       break;
9367
9368     case CPP_MOD_EQ:
9369       id = ansi_assopname (TRUNC_MOD_EXPR);
9370       break;
9371
9372     case CPP_XOR_EQ:
9373       id = ansi_assopname (BIT_XOR_EXPR);
9374       break;
9375
9376     case CPP_AND_EQ:
9377       id = ansi_assopname (BIT_AND_EXPR);
9378       break;
9379
9380     case CPP_OR_EQ:
9381       id = ansi_assopname (BIT_IOR_EXPR);
9382       break;
9383
9384     case CPP_LSHIFT:
9385       id = ansi_opname (LSHIFT_EXPR);
9386       break;
9387
9388     case CPP_RSHIFT:
9389       id = ansi_opname (RSHIFT_EXPR);
9390       break;
9391
9392     case CPP_LSHIFT_EQ:
9393       id = ansi_assopname (LSHIFT_EXPR);
9394       break;
9395
9396     case CPP_RSHIFT_EQ:
9397       id = ansi_assopname (RSHIFT_EXPR);
9398       break;
9399
9400     case CPP_EQ_EQ:
9401       id = ansi_opname (EQ_EXPR);
9402       break;
9403
9404     case CPP_NOT_EQ:
9405       id = ansi_opname (NE_EXPR);
9406       break;
9407
9408     case CPP_LESS_EQ:
9409       id = ansi_opname (LE_EXPR);
9410       break;
9411
9412     case CPP_GREATER_EQ:
9413       id = ansi_opname (GE_EXPR);
9414       break;
9415
9416     case CPP_AND_AND:
9417       id = ansi_opname (TRUTH_ANDIF_EXPR);
9418       break;
9419
9420     case CPP_OR_OR:
9421       id = ansi_opname (TRUTH_ORIF_EXPR);
9422       break;
9423
9424     case CPP_PLUS_PLUS:
9425       id = ansi_opname (POSTINCREMENT_EXPR);
9426       break;
9427
9428     case CPP_MINUS_MINUS:
9429       id = ansi_opname (PREDECREMENT_EXPR);
9430       break;
9431
9432     case CPP_COMMA:
9433       id = ansi_opname (COMPOUND_EXPR);
9434       break;
9435
9436     case CPP_DEREF_STAR:
9437       id = ansi_opname (MEMBER_REF);
9438       break;
9439
9440     case CPP_DEREF:
9441       id = ansi_opname (COMPONENT_REF);
9442       break;
9443
9444     case CPP_OPEN_PAREN:
9445       /* Consume the `('.  */
9446       cp_lexer_consume_token (parser->lexer);
9447       /* Look for the matching `)'.  */
9448       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9449       return ansi_opname (CALL_EXPR);
9450
9451     case CPP_OPEN_SQUARE:
9452       /* Consume the `['.  */
9453       cp_lexer_consume_token (parser->lexer);
9454       /* Look for the matching `]'.  */
9455       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9456       return ansi_opname (ARRAY_REF);
9457
9458     default:
9459       /* Anything else is an error.  */
9460       break;
9461     }
9462
9463   /* If we have selected an identifier, we need to consume the
9464      operator token.  */
9465   if (id)
9466     cp_lexer_consume_token (parser->lexer);
9467   /* Otherwise, no valid operator name was present.  */
9468   else
9469     {
9470       cp_parser_error (parser, "expected operator");
9471       id = error_mark_node;
9472     }
9473
9474   return id;
9475 }
9476
9477 /* Parse a template-declaration.
9478
9479    template-declaration:
9480      export [opt] template < template-parameter-list > declaration
9481
9482    If MEMBER_P is TRUE, this template-declaration occurs within a
9483    class-specifier.
9484
9485    The grammar rule given by the standard isn't correct.  What
9486    is really meant is:
9487
9488    template-declaration:
9489      export [opt] template-parameter-list-seq
9490        decl-specifier-seq [opt] init-declarator [opt] ;
9491      export [opt] template-parameter-list-seq
9492        function-definition
9493
9494    template-parameter-list-seq:
9495      template-parameter-list-seq [opt]
9496      template < template-parameter-list >  */
9497
9498 static void
9499 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9500 {
9501   /* Check for `export'.  */
9502   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9503     {
9504       /* Consume the `export' token.  */
9505       cp_lexer_consume_token (parser->lexer);
9506       /* Warn that we do not support `export'.  */
9507       warning (0, "keyword %<export%> not implemented, and will be ignored");
9508     }
9509
9510   cp_parser_template_declaration_after_export (parser, member_p);
9511 }
9512
9513 /* Parse a template-parameter-list.
9514
9515    template-parameter-list:
9516      template-parameter
9517      template-parameter-list , template-parameter
9518
9519    Returns a TREE_LIST.  Each node represents a template parameter.
9520    The nodes are connected via their TREE_CHAINs.  */
9521
9522 static tree
9523 cp_parser_template_parameter_list (cp_parser* parser)
9524 {
9525   tree parameter_list = NULL_TREE;
9526
9527   begin_template_parm_list ();
9528   while (true)
9529     {
9530       tree parameter;
9531       bool is_non_type;
9532       bool is_parameter_pack;
9533
9534       /* Parse the template-parameter.  */
9535       parameter = cp_parser_template_parameter (parser, 
9536                                                 &is_non_type,
9537                                                 &is_parameter_pack);
9538       /* Add it to the list.  */
9539       if (parameter != error_mark_node)
9540         parameter_list = process_template_parm (parameter_list,
9541                                                 parameter,
9542                                                 is_non_type,
9543                                                 is_parameter_pack);
9544       else
9545        {
9546          tree err_parm = build_tree_list (parameter, parameter);
9547          TREE_VALUE (err_parm) = error_mark_node;
9548          parameter_list = chainon (parameter_list, err_parm);
9549        }
9550
9551       /* If the next token is not a `,', we're done.  */
9552       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9553         break;
9554       /* Otherwise, consume the `,' token.  */
9555       cp_lexer_consume_token (parser->lexer);
9556     }
9557
9558   return end_template_parm_list (parameter_list);
9559 }
9560
9561 /* Parse a template-parameter.
9562
9563    template-parameter:
9564      type-parameter
9565      parameter-declaration
9566
9567    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9568    the parameter.  The TREE_PURPOSE is the default value, if any.
9569    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9570    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9571    set to true iff this parameter is a parameter pack. */
9572
9573 static tree
9574 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9575                               bool *is_parameter_pack)
9576 {
9577   cp_token *token;
9578   cp_parameter_declarator *parameter_declarator;
9579   cp_declarator *id_declarator;
9580   tree parm;
9581
9582   /* Assume it is a type parameter or a template parameter.  */
9583   *is_non_type = false;
9584   /* Assume it not a parameter pack. */
9585   *is_parameter_pack = false;
9586   /* Peek at the next token.  */
9587   token = cp_lexer_peek_token (parser->lexer);
9588   /* If it is `class' or `template', we have a type-parameter.  */
9589   if (token->keyword == RID_TEMPLATE)
9590     return cp_parser_type_parameter (parser, is_parameter_pack);
9591   /* If it is `class' or `typename' we do not know yet whether it is a
9592      type parameter or a non-type parameter.  Consider:
9593
9594        template <typename T, typename T::X X> ...
9595
9596      or:
9597
9598        template <class C, class D*> ...
9599
9600      Here, the first parameter is a type parameter, and the second is
9601      a non-type parameter.  We can tell by looking at the token after
9602      the identifier -- if it is a `,', `=', or `>' then we have a type
9603      parameter.  */
9604   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9605     {
9606       /* Peek at the token after `class' or `typename'.  */
9607       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9608       /* If it's an ellipsis, we have a template type parameter
9609          pack. */
9610       if (token->type == CPP_ELLIPSIS)
9611         return cp_parser_type_parameter (parser, is_parameter_pack);
9612       /* If it's an identifier, skip it.  */
9613       if (token->type == CPP_NAME)
9614         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9615       /* Now, see if the token looks like the end of a template
9616          parameter.  */
9617       if (token->type == CPP_COMMA
9618           || token->type == CPP_EQ
9619           || token->type == CPP_GREATER)
9620         return cp_parser_type_parameter (parser, is_parameter_pack);
9621     }
9622
9623   /* Otherwise, it is a non-type parameter.
9624
9625      [temp.param]
9626
9627      When parsing a default template-argument for a non-type
9628      template-parameter, the first non-nested `>' is taken as the end
9629      of the template parameter-list rather than a greater-than
9630      operator.  */
9631   *is_non_type = true;
9632   parameter_declarator
9633      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9634                                         /*parenthesized_p=*/NULL);
9635
9636   /* If the parameter declaration is marked as a parameter pack, set
9637      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9638      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9639      grokdeclarator. */
9640   if (parameter_declarator
9641       && parameter_declarator->declarator
9642       && parameter_declarator->declarator->parameter_pack_p)
9643     {
9644       *is_parameter_pack = true;
9645       parameter_declarator->declarator->parameter_pack_p = false;
9646     }
9647
9648   /* If the next token is an ellipsis, and we don't already have it
9649      marked as a parameter pack, then we have a parameter pack (that
9650      has no declarator).  */
9651   if (!*is_parameter_pack
9652       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9653       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9654     {
9655       /* Consume the `...'.  */
9656       cp_lexer_consume_token (parser->lexer);
9657       maybe_warn_variadic_templates ();
9658       
9659       *is_parameter_pack = true;
9660     }
9661   /* We might end up with a pack expansion as the type of the non-type
9662      template parameter, in which case this is a non-type template
9663      parameter pack.  */
9664   else if (parameter_declarator
9665            && parameter_declarator->decl_specifiers.type
9666            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9667     {
9668       *is_parameter_pack = true;
9669       parameter_declarator->decl_specifiers.type = 
9670         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9671     }
9672
9673   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9674     {
9675       /* Parameter packs cannot have default arguments.  However, a
9676          user may try to do so, so we'll parse them and give an
9677          appropriate diagnostic here.  */
9678
9679       /* Consume the `='.  */
9680       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9681       cp_lexer_consume_token (parser->lexer);
9682       
9683       /* Find the name of the parameter pack.  */     
9684       id_declarator = parameter_declarator->declarator;
9685       while (id_declarator && id_declarator->kind != cdk_id)
9686         id_declarator = id_declarator->declarator;
9687       
9688       if (id_declarator && id_declarator->kind == cdk_id)
9689         error ("%Htemplate parameter pack %qD cannot have a default argument",
9690                &start_token->location, id_declarator->u.id.unqualified_name);
9691       else
9692         error ("%Htemplate parameter pack cannot have a default argument",
9693                &start_token->location);
9694       
9695       /* Parse the default argument, but throw away the result.  */
9696       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9697     }
9698
9699   parm = grokdeclarator (parameter_declarator->declarator,
9700                          &parameter_declarator->decl_specifiers,
9701                          PARM, /*initialized=*/0,
9702                          /*attrlist=*/NULL);
9703   if (parm == error_mark_node)
9704     return error_mark_node;
9705
9706   return build_tree_list (parameter_declarator->default_argument, parm);
9707 }
9708
9709 /* Parse a type-parameter.
9710
9711    type-parameter:
9712      class identifier [opt]
9713      class identifier [opt] = type-id
9714      typename identifier [opt]
9715      typename identifier [opt] = type-id
9716      template < template-parameter-list > class identifier [opt]
9717      template < template-parameter-list > class identifier [opt]
9718        = id-expression
9719
9720    GNU Extension (variadic templates):
9721
9722    type-parameter:
9723      class ... identifier [opt]
9724      typename ... identifier [opt]
9725
9726    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9727    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9728    the declaration of the parameter.
9729
9730    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9731
9732 static tree
9733 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9734 {
9735   cp_token *token;
9736   tree parameter;
9737
9738   /* Look for a keyword to tell us what kind of parameter this is.  */
9739   token = cp_parser_require (parser, CPP_KEYWORD,
9740                              "%<class%>, %<typename%>, or %<template%>");
9741   if (!token)
9742     return error_mark_node;
9743
9744   switch (token->keyword)
9745     {
9746     case RID_CLASS:
9747     case RID_TYPENAME:
9748       {
9749         tree identifier;
9750         tree default_argument;
9751
9752         /* If the next token is an ellipsis, we have a template
9753            argument pack. */
9754         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9755           {
9756             /* Consume the `...' token. */
9757             cp_lexer_consume_token (parser->lexer);
9758             maybe_warn_variadic_templates ();
9759
9760             *is_parameter_pack = true;
9761           }
9762
9763         /* If the next token is an identifier, then it names the
9764            parameter.  */
9765         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9766           identifier = cp_parser_identifier (parser);
9767         else
9768           identifier = NULL_TREE;
9769
9770         /* Create the parameter.  */
9771         parameter = finish_template_type_parm (class_type_node, identifier);
9772
9773         /* If the next token is an `=', we have a default argument.  */
9774         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9775           {
9776             /* Consume the `=' token.  */
9777             cp_lexer_consume_token (parser->lexer);
9778             /* Parse the default-argument.  */
9779             push_deferring_access_checks (dk_no_deferred);
9780             default_argument = cp_parser_type_id (parser);
9781
9782             /* Template parameter packs cannot have default
9783                arguments. */
9784             if (*is_parameter_pack)
9785               {
9786                 if (identifier)
9787                   error ("%Htemplate parameter pack %qD cannot have a "
9788                          "default argument", &token->location, identifier);
9789                 else
9790                   error ("%Htemplate parameter packs cannot have "
9791                          "default arguments", &token->location);
9792                 default_argument = NULL_TREE;
9793               }
9794             pop_deferring_access_checks ();
9795           }
9796         else
9797           default_argument = NULL_TREE;
9798
9799         /* Create the combined representation of the parameter and the
9800            default argument.  */
9801         parameter = build_tree_list (default_argument, parameter);
9802       }
9803       break;
9804
9805     case RID_TEMPLATE:
9806       {
9807         tree parameter_list;
9808         tree identifier;
9809         tree default_argument;
9810
9811         /* Look for the `<'.  */
9812         cp_parser_require (parser, CPP_LESS, "%<<%>");
9813         /* Parse the template-parameter-list.  */
9814         parameter_list = cp_parser_template_parameter_list (parser);
9815         /* Look for the `>'.  */
9816         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9817         /* Look for the `class' keyword.  */
9818         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9819         /* If the next token is an ellipsis, we have a template
9820            argument pack. */
9821         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9822           {
9823             /* Consume the `...' token. */
9824             cp_lexer_consume_token (parser->lexer);
9825             maybe_warn_variadic_templates ();
9826
9827             *is_parameter_pack = true;
9828           }
9829         /* If the next token is an `=', then there is a
9830            default-argument.  If the next token is a `>', we are at
9831            the end of the parameter-list.  If the next token is a `,',
9832            then we are at the end of this parameter.  */
9833         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9834             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9835             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9836           {
9837             identifier = cp_parser_identifier (parser);
9838             /* Treat invalid names as if the parameter were nameless.  */
9839             if (identifier == error_mark_node)
9840               identifier = NULL_TREE;
9841           }
9842         else
9843           identifier = NULL_TREE;
9844
9845         /* Create the template parameter.  */
9846         parameter = finish_template_template_parm (class_type_node,
9847                                                    identifier);
9848
9849         /* If the next token is an `=', then there is a
9850            default-argument.  */
9851         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9852           {
9853             bool is_template;
9854
9855             /* Consume the `='.  */
9856             cp_lexer_consume_token (parser->lexer);
9857             /* Parse the id-expression.  */
9858             push_deferring_access_checks (dk_no_deferred);
9859             /* save token before parsing the id-expression, for error
9860                reporting */
9861             token = cp_lexer_peek_token (parser->lexer);
9862             default_argument
9863               = cp_parser_id_expression (parser,
9864                                          /*template_keyword_p=*/false,
9865                                          /*check_dependency_p=*/true,
9866                                          /*template_p=*/&is_template,
9867                                          /*declarator_p=*/false,
9868                                          /*optional_p=*/false);
9869             if (TREE_CODE (default_argument) == TYPE_DECL)
9870               /* If the id-expression was a template-id that refers to
9871                  a template-class, we already have the declaration here,
9872                  so no further lookup is needed.  */
9873                  ;
9874             else
9875               /* Look up the name.  */
9876               default_argument
9877                 = cp_parser_lookup_name (parser, default_argument,
9878                                          none_type,
9879                                          /*is_template=*/is_template,
9880                                          /*is_namespace=*/false,
9881                                          /*check_dependency=*/true,
9882                                          /*ambiguous_decls=*/NULL,
9883                                          token->location);
9884             /* See if the default argument is valid.  */
9885             default_argument
9886               = check_template_template_default_arg (default_argument);
9887
9888             /* Template parameter packs cannot have default
9889                arguments. */
9890             if (*is_parameter_pack)
9891               {
9892                 if (identifier)
9893                   error ("%Htemplate parameter pack %qD cannot "
9894                          "have a default argument",
9895                          &token->location, identifier);
9896                 else
9897                   error ("%Htemplate parameter packs cannot "
9898                          "have default arguments",
9899                          &token->location);
9900                 default_argument = NULL_TREE;
9901               }
9902             pop_deferring_access_checks ();
9903           }
9904         else
9905           default_argument = NULL_TREE;
9906
9907         /* Create the combined representation of the parameter and the
9908            default argument.  */
9909         parameter = build_tree_list (default_argument, parameter);
9910       }
9911       break;
9912
9913     default:
9914       gcc_unreachable ();
9915       break;
9916     }
9917
9918   return parameter;
9919 }
9920
9921 /* Parse a template-id.
9922
9923    template-id:
9924      template-name < template-argument-list [opt] >
9925
9926    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9927    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9928    returned.  Otherwise, if the template-name names a function, or set
9929    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9930    names a class, returns a TYPE_DECL for the specialization.
9931
9932    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9933    uninstantiated templates.  */
9934
9935 static tree
9936 cp_parser_template_id (cp_parser *parser,
9937                        bool template_keyword_p,
9938                        bool check_dependency_p,
9939                        bool is_declaration)
9940 {
9941   int i;
9942   tree templ;
9943   tree arguments;
9944   tree template_id;
9945   cp_token_position start_of_id = 0;
9946   deferred_access_check *chk;
9947   VEC (deferred_access_check,gc) *access_check;
9948   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9949   bool is_identifier;
9950
9951   /* If the next token corresponds to a template-id, there is no need
9952      to reparse it.  */
9953   next_token = cp_lexer_peek_token (parser->lexer);
9954   if (next_token->type == CPP_TEMPLATE_ID)
9955     {
9956       struct tree_check *check_value;
9957
9958       /* Get the stored value.  */
9959       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9960       /* Perform any access checks that were deferred.  */
9961       access_check = check_value->checks;
9962       if (access_check)
9963         {
9964           for (i = 0 ;
9965                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9966                ++i)
9967             {
9968               perform_or_defer_access_check (chk->binfo,
9969                                              chk->decl,
9970                                              chk->diag_decl);
9971             }
9972         }
9973       /* Return the stored value.  */
9974       return check_value->value;
9975     }
9976
9977   /* Avoid performing name lookup if there is no possibility of
9978      finding a template-id.  */
9979   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9980       || (next_token->type == CPP_NAME
9981           && !cp_parser_nth_token_starts_template_argument_list_p
9982                (parser, 2)))
9983     {
9984       cp_parser_error (parser, "expected template-id");
9985       return error_mark_node;
9986     }
9987
9988   /* Remember where the template-id starts.  */
9989   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9990     start_of_id = cp_lexer_token_position (parser->lexer, false);
9991
9992   push_deferring_access_checks (dk_deferred);
9993
9994   /* Parse the template-name.  */
9995   is_identifier = false;
9996   token = cp_lexer_peek_token (parser->lexer);
9997   templ = cp_parser_template_name (parser, template_keyword_p,
9998                                    check_dependency_p,
9999                                    is_declaration,
10000                                    &is_identifier);
10001   if (templ == error_mark_node || is_identifier)
10002     {
10003       pop_deferring_access_checks ();
10004       return templ;
10005     }
10006
10007   /* If we find the sequence `[:' after a template-name, it's probably
10008      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10009      parse correctly the argument list.  */
10010   next_token = cp_lexer_peek_token (parser->lexer);
10011   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10012   if (next_token->type == CPP_OPEN_SQUARE
10013       && next_token->flags & DIGRAPH
10014       && next_token_2->type == CPP_COLON
10015       && !(next_token_2->flags & PREV_WHITE))
10016     {
10017       cp_parser_parse_tentatively (parser);
10018       /* Change `:' into `::'.  */
10019       next_token_2->type = CPP_SCOPE;
10020       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10021          CPP_LESS.  */
10022       cp_lexer_consume_token (parser->lexer);
10023
10024       /* Parse the arguments.  */
10025       arguments = cp_parser_enclosed_template_argument_list (parser);
10026       if (!cp_parser_parse_definitely (parser))
10027         {
10028           /* If we couldn't parse an argument list, then we revert our changes
10029              and return simply an error. Maybe this is not a template-id
10030              after all.  */
10031           next_token_2->type = CPP_COLON;
10032           cp_parser_error (parser, "expected %<<%>");
10033           pop_deferring_access_checks ();
10034           return error_mark_node;
10035         }
10036       /* Otherwise, emit an error about the invalid digraph, but continue
10037          parsing because we got our argument list.  */
10038       if (permerror (next_token->location,
10039                      "%<<::%> cannot begin a template-argument list"))
10040         {
10041           static bool hint = false;
10042           inform (next_token->location,
10043                   "%<<:%> is an alternate spelling for %<[%>."
10044                   " Insert whitespace between %<<%> and %<::%>");
10045           if (!hint && !flag_permissive)
10046             {
10047               inform (next_token->location, "(if you use %<-fpermissive%>"
10048                       " G++ will accept your code)");
10049               hint = true;
10050             }
10051         }
10052     }
10053   else
10054     {
10055       /* Look for the `<' that starts the template-argument-list.  */
10056       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10057         {
10058           pop_deferring_access_checks ();
10059           return error_mark_node;
10060         }
10061       /* Parse the arguments.  */
10062       arguments = cp_parser_enclosed_template_argument_list (parser);
10063     }
10064
10065   /* Build a representation of the specialization.  */
10066   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10067     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10068   else if (DECL_CLASS_TEMPLATE_P (templ)
10069            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10070     {
10071       bool entering_scope;
10072       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10073          template (rather than some instantiation thereof) only if
10074          is not nested within some other construct.  For example, in
10075          "template <typename T> void f(T) { A<T>::", A<T> is just an
10076          instantiation of A.  */
10077       entering_scope = (template_parm_scope_p ()
10078                         && cp_lexer_next_token_is (parser->lexer,
10079                                                    CPP_SCOPE));
10080       template_id
10081         = finish_template_type (templ, arguments, entering_scope);
10082     }
10083   else
10084     {
10085       /* If it's not a class-template or a template-template, it should be
10086          a function-template.  */
10087       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10088                    || TREE_CODE (templ) == OVERLOAD
10089                    || BASELINK_P (templ)));
10090
10091       template_id = lookup_template_function (templ, arguments);
10092     }
10093
10094   /* If parsing tentatively, replace the sequence of tokens that makes
10095      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10096      should we re-parse the token stream, we will not have to repeat
10097      the effort required to do the parse, nor will we issue duplicate
10098      error messages about problems during instantiation of the
10099      template.  */
10100   if (start_of_id)
10101     {
10102       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10103
10104       /* Reset the contents of the START_OF_ID token.  */
10105       token->type = CPP_TEMPLATE_ID;
10106       /* Retrieve any deferred checks.  Do not pop this access checks yet
10107          so the memory will not be reclaimed during token replacing below.  */
10108       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10109       token->u.tree_check_value->value = template_id;
10110       token->u.tree_check_value->checks = get_deferred_access_checks ();
10111       token->keyword = RID_MAX;
10112
10113       /* Purge all subsequent tokens.  */
10114       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10115
10116       /* ??? Can we actually assume that, if template_id ==
10117          error_mark_node, we will have issued a diagnostic to the
10118          user, as opposed to simply marking the tentative parse as
10119          failed?  */
10120       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10121         error ("%Hparse error in template argument list",
10122                &token->location);
10123     }
10124
10125   pop_deferring_access_checks ();
10126   return template_id;
10127 }
10128
10129 /* Parse a template-name.
10130
10131    template-name:
10132      identifier
10133
10134    The standard should actually say:
10135
10136    template-name:
10137      identifier
10138      operator-function-id
10139
10140    A defect report has been filed about this issue.
10141
10142    A conversion-function-id cannot be a template name because they cannot
10143    be part of a template-id. In fact, looking at this code:
10144
10145    a.operator K<int>()
10146
10147    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10148    It is impossible to call a templated conversion-function-id with an
10149    explicit argument list, since the only allowed template parameter is
10150    the type to which it is converting.
10151
10152    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10153    `template' keyword, in a construction like:
10154
10155      T::template f<3>()
10156
10157    In that case `f' is taken to be a template-name, even though there
10158    is no way of knowing for sure.
10159
10160    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10161    name refers to a set of overloaded functions, at least one of which
10162    is a template, or an IDENTIFIER_NODE with the name of the template,
10163    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10164    names are looked up inside uninstantiated templates.  */
10165
10166 static tree
10167 cp_parser_template_name (cp_parser* parser,
10168                          bool template_keyword_p,
10169                          bool check_dependency_p,
10170                          bool is_declaration,
10171                          bool *is_identifier)
10172 {
10173   tree identifier;
10174   tree decl;
10175   tree fns;
10176   cp_token *token = cp_lexer_peek_token (parser->lexer);
10177
10178   /* If the next token is `operator', then we have either an
10179      operator-function-id or a conversion-function-id.  */
10180   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10181     {
10182       /* We don't know whether we're looking at an
10183          operator-function-id or a conversion-function-id.  */
10184       cp_parser_parse_tentatively (parser);
10185       /* Try an operator-function-id.  */
10186       identifier = cp_parser_operator_function_id (parser);
10187       /* If that didn't work, try a conversion-function-id.  */
10188       if (!cp_parser_parse_definitely (parser))
10189         {
10190           cp_parser_error (parser, "expected template-name");
10191           return error_mark_node;
10192         }
10193     }
10194   /* Look for the identifier.  */
10195   else
10196     identifier = cp_parser_identifier (parser);
10197
10198   /* If we didn't find an identifier, we don't have a template-id.  */
10199   if (identifier == error_mark_node)
10200     return error_mark_node;
10201
10202   /* If the name immediately followed the `template' keyword, then it
10203      is a template-name.  However, if the next token is not `<', then
10204      we do not treat it as a template-name, since it is not being used
10205      as part of a template-id.  This enables us to handle constructs
10206      like:
10207
10208        template <typename T> struct S { S(); };
10209        template <typename T> S<T>::S();
10210
10211      correctly.  We would treat `S' as a template -- if it were `S<T>'
10212      -- but we do not if there is no `<'.  */
10213
10214   if (processing_template_decl
10215       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10216     {
10217       /* In a declaration, in a dependent context, we pretend that the
10218          "template" keyword was present in order to improve error
10219          recovery.  For example, given:
10220
10221            template <typename T> void f(T::X<int>);
10222
10223          we want to treat "X<int>" as a template-id.  */
10224       if (is_declaration
10225           && !template_keyword_p
10226           && parser->scope && TYPE_P (parser->scope)
10227           && check_dependency_p
10228           && dependent_type_p (parser->scope)
10229           /* Do not do this for dtors (or ctors), since they never
10230              need the template keyword before their name.  */
10231           && !constructor_name_p (identifier, parser->scope))
10232         {
10233           cp_token_position start = 0;
10234
10235           /* Explain what went wrong.  */
10236           error ("%Hnon-template %qD used as template",
10237                  &token->location, identifier);
10238           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10239                   parser->scope, identifier);
10240           /* If parsing tentatively, find the location of the "<" token.  */
10241           if (cp_parser_simulate_error (parser))
10242             start = cp_lexer_token_position (parser->lexer, true);
10243           /* Parse the template arguments so that we can issue error
10244              messages about them.  */
10245           cp_lexer_consume_token (parser->lexer);
10246           cp_parser_enclosed_template_argument_list (parser);
10247           /* Skip tokens until we find a good place from which to
10248              continue parsing.  */
10249           cp_parser_skip_to_closing_parenthesis (parser,
10250                                                  /*recovering=*/true,
10251                                                  /*or_comma=*/true,
10252                                                  /*consume_paren=*/false);
10253           /* If parsing tentatively, permanently remove the
10254              template argument list.  That will prevent duplicate
10255              error messages from being issued about the missing
10256              "template" keyword.  */
10257           if (start)
10258             cp_lexer_purge_tokens_after (parser->lexer, start);
10259           if (is_identifier)
10260             *is_identifier = true;
10261           return identifier;
10262         }
10263
10264       /* If the "template" keyword is present, then there is generally
10265          no point in doing name-lookup, so we just return IDENTIFIER.
10266          But, if the qualifying scope is non-dependent then we can
10267          (and must) do name-lookup normally.  */
10268       if (template_keyword_p
10269           && (!parser->scope
10270               || (TYPE_P (parser->scope)
10271                   && dependent_type_p (parser->scope))))
10272         return identifier;
10273     }
10274
10275   /* Look up the name.  */
10276   decl = cp_parser_lookup_name (parser, identifier,
10277                                 none_type,
10278                                 /*is_template=*/false,
10279                                 /*is_namespace=*/false,
10280                                 check_dependency_p,
10281                                 /*ambiguous_decls=*/NULL,
10282                                 token->location);
10283   decl = maybe_get_template_decl_from_type_decl (decl);
10284
10285   /* If DECL is a template, then the name was a template-name.  */
10286   if (TREE_CODE (decl) == TEMPLATE_DECL)
10287     ;
10288   else
10289     {
10290       tree fn = NULL_TREE;
10291
10292       /* The standard does not explicitly indicate whether a name that
10293          names a set of overloaded declarations, some of which are
10294          templates, is a template-name.  However, such a name should
10295          be a template-name; otherwise, there is no way to form a
10296          template-id for the overloaded templates.  */
10297       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10298       if (TREE_CODE (fns) == OVERLOAD)
10299         for (fn = fns; fn; fn = OVL_NEXT (fn))
10300           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10301             break;
10302
10303       if (!fn)
10304         {
10305           /* The name does not name a template.  */
10306           cp_parser_error (parser, "expected template-name");
10307           return error_mark_node;
10308         }
10309     }
10310
10311   /* If DECL is dependent, and refers to a function, then just return
10312      its name; we will look it up again during template instantiation.  */
10313   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10314     {
10315       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10316       if (TYPE_P (scope) && dependent_type_p (scope))
10317         return identifier;
10318     }
10319
10320   return decl;
10321 }
10322
10323 /* Parse a template-argument-list.
10324
10325    template-argument-list:
10326      template-argument ... [opt]
10327      template-argument-list , template-argument ... [opt]
10328
10329    Returns a TREE_VEC containing the arguments.  */
10330
10331 static tree
10332 cp_parser_template_argument_list (cp_parser* parser)
10333 {
10334   tree fixed_args[10];
10335   unsigned n_args = 0;
10336   unsigned alloced = 10;
10337   tree *arg_ary = fixed_args;
10338   tree vec;
10339   bool saved_in_template_argument_list_p;
10340   bool saved_ice_p;
10341   bool saved_non_ice_p;
10342
10343   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10344   parser->in_template_argument_list_p = true;
10345   /* Even if the template-id appears in an integral
10346      constant-expression, the contents of the argument list do
10347      not.  */
10348   saved_ice_p = parser->integral_constant_expression_p;
10349   parser->integral_constant_expression_p = false;
10350   saved_non_ice_p = parser->non_integral_constant_expression_p;
10351   parser->non_integral_constant_expression_p = false;
10352   /* Parse the arguments.  */
10353   do
10354     {
10355       tree argument;
10356
10357       if (n_args)
10358         /* Consume the comma.  */
10359         cp_lexer_consume_token (parser->lexer);
10360
10361       /* Parse the template-argument.  */
10362       argument = cp_parser_template_argument (parser);
10363
10364       /* If the next token is an ellipsis, we're expanding a template
10365          argument pack. */
10366       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10367         {
10368           /* Consume the `...' token. */
10369           cp_lexer_consume_token (parser->lexer);
10370
10371           /* Make the argument into a TYPE_PACK_EXPANSION or
10372              EXPR_PACK_EXPANSION. */
10373           argument = make_pack_expansion (argument);
10374         }
10375
10376       if (n_args == alloced)
10377         {
10378           alloced *= 2;
10379
10380           if (arg_ary == fixed_args)
10381             {
10382               arg_ary = XNEWVEC (tree, alloced);
10383               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10384             }
10385           else
10386             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10387         }
10388       arg_ary[n_args++] = argument;
10389     }
10390   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10391
10392   vec = make_tree_vec (n_args);
10393
10394   while (n_args--)
10395     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10396
10397   if (arg_ary != fixed_args)
10398     free (arg_ary);
10399   parser->non_integral_constant_expression_p = saved_non_ice_p;
10400   parser->integral_constant_expression_p = saved_ice_p;
10401   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10402   return vec;
10403 }
10404
10405 /* Parse a template-argument.
10406
10407    template-argument:
10408      assignment-expression
10409      type-id
10410      id-expression
10411
10412    The representation is that of an assignment-expression, type-id, or
10413    id-expression -- except that the qualified id-expression is
10414    evaluated, so that the value returned is either a DECL or an
10415    OVERLOAD.
10416
10417    Although the standard says "assignment-expression", it forbids
10418    throw-expressions or assignments in the template argument.
10419    Therefore, we use "conditional-expression" instead.  */
10420
10421 static tree
10422 cp_parser_template_argument (cp_parser* parser)
10423 {
10424   tree argument;
10425   bool template_p;
10426   bool address_p;
10427   bool maybe_type_id = false;
10428   cp_token *token = NULL, *argument_start_token = NULL;
10429   cp_id_kind idk;
10430
10431   /* There's really no way to know what we're looking at, so we just
10432      try each alternative in order.
10433
10434        [temp.arg]
10435
10436        In a template-argument, an ambiguity between a type-id and an
10437        expression is resolved to a type-id, regardless of the form of
10438        the corresponding template-parameter.
10439
10440      Therefore, we try a type-id first.  */
10441   cp_parser_parse_tentatively (parser);
10442   argument = cp_parser_type_id (parser);
10443   /* If there was no error parsing the type-id but the next token is a
10444      '>>', our behavior depends on which dialect of C++ we're
10445      parsing. In C++98, we probably found a typo for '> >'. But there
10446      are type-id which are also valid expressions. For instance:
10447
10448      struct X { int operator >> (int); };
10449      template <int V> struct Foo {};
10450      Foo<X () >> 5> r;
10451
10452      Here 'X()' is a valid type-id of a function type, but the user just
10453      wanted to write the expression "X() >> 5". Thus, we remember that we
10454      found a valid type-id, but we still try to parse the argument as an
10455      expression to see what happens. 
10456
10457      In C++0x, the '>>' will be considered two separate '>'
10458      tokens.  */
10459   if (!cp_parser_error_occurred (parser)
10460       && cxx_dialect == cxx98
10461       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10462     {
10463       maybe_type_id = true;
10464       cp_parser_abort_tentative_parse (parser);
10465     }
10466   else
10467     {
10468       /* If the next token isn't a `,' or a `>', then this argument wasn't
10469       really finished. This means that the argument is not a valid
10470       type-id.  */
10471       if (!cp_parser_next_token_ends_template_argument_p (parser))
10472         cp_parser_error (parser, "expected template-argument");
10473       /* If that worked, we're done.  */
10474       if (cp_parser_parse_definitely (parser))
10475         return argument;
10476     }
10477   /* We're still not sure what the argument will be.  */
10478   cp_parser_parse_tentatively (parser);
10479   /* Try a template.  */
10480   argument_start_token = cp_lexer_peek_token (parser->lexer);
10481   argument = cp_parser_id_expression (parser,
10482                                       /*template_keyword_p=*/false,
10483                                       /*check_dependency_p=*/true,
10484                                       &template_p,
10485                                       /*declarator_p=*/false,
10486                                       /*optional_p=*/false);
10487   /* If the next token isn't a `,' or a `>', then this argument wasn't
10488      really finished.  */
10489   if (!cp_parser_next_token_ends_template_argument_p (parser))
10490     cp_parser_error (parser, "expected template-argument");
10491   if (!cp_parser_error_occurred (parser))
10492     {
10493       /* Figure out what is being referred to.  If the id-expression
10494          was for a class template specialization, then we will have a
10495          TYPE_DECL at this point.  There is no need to do name lookup
10496          at this point in that case.  */
10497       if (TREE_CODE (argument) != TYPE_DECL)
10498         argument = cp_parser_lookup_name (parser, argument,
10499                                           none_type,
10500                                           /*is_template=*/template_p,
10501                                           /*is_namespace=*/false,
10502                                           /*check_dependency=*/true,
10503                                           /*ambiguous_decls=*/NULL,
10504                                           argument_start_token->location);
10505       if (TREE_CODE (argument) != TEMPLATE_DECL
10506           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10507         cp_parser_error (parser, "expected template-name");
10508     }
10509   if (cp_parser_parse_definitely (parser))
10510     return argument;
10511   /* It must be a non-type argument.  There permitted cases are given
10512      in [temp.arg.nontype]:
10513
10514      -- an integral constant-expression of integral or enumeration
10515         type; or
10516
10517      -- the name of a non-type template-parameter; or
10518
10519      -- the name of an object or function with external linkage...
10520
10521      -- the address of an object or function with external linkage...
10522
10523      -- a pointer to member...  */
10524   /* Look for a non-type template parameter.  */
10525   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10526     {
10527       cp_parser_parse_tentatively (parser);
10528       argument = cp_parser_primary_expression (parser,
10529                                                /*address_p=*/false,
10530                                                /*cast_p=*/false,
10531                                                /*template_arg_p=*/true,
10532                                                &idk);
10533       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10534           || !cp_parser_next_token_ends_template_argument_p (parser))
10535         cp_parser_simulate_error (parser);
10536       if (cp_parser_parse_definitely (parser))
10537         return argument;
10538     }
10539
10540   /* If the next token is "&", the argument must be the address of an
10541      object or function with external linkage.  */
10542   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10543   if (address_p)
10544     cp_lexer_consume_token (parser->lexer);
10545   /* See if we might have an id-expression.  */
10546   token = cp_lexer_peek_token (parser->lexer);
10547   if (token->type == CPP_NAME
10548       || token->keyword == RID_OPERATOR
10549       || token->type == CPP_SCOPE
10550       || token->type == CPP_TEMPLATE_ID
10551       || token->type == CPP_NESTED_NAME_SPECIFIER)
10552     {
10553       cp_parser_parse_tentatively (parser);
10554       argument = cp_parser_primary_expression (parser,
10555                                                address_p,
10556                                                /*cast_p=*/false,
10557                                                /*template_arg_p=*/true,
10558                                                &idk);
10559       if (cp_parser_error_occurred (parser)
10560           || !cp_parser_next_token_ends_template_argument_p (parser))
10561         cp_parser_abort_tentative_parse (parser);
10562       else
10563         {
10564           if (TREE_CODE (argument) == INDIRECT_REF)
10565             {
10566               gcc_assert (REFERENCE_REF_P (argument));
10567               argument = TREE_OPERAND (argument, 0);
10568             }
10569
10570           if (TREE_CODE (argument) == VAR_DECL)
10571             {
10572               /* A variable without external linkage might still be a
10573                  valid constant-expression, so no error is issued here
10574                  if the external-linkage check fails.  */
10575               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10576                 cp_parser_simulate_error (parser);
10577             }
10578           else if (is_overloaded_fn (argument))
10579             /* All overloaded functions are allowed; if the external
10580                linkage test does not pass, an error will be issued
10581                later.  */
10582             ;
10583           else if (address_p
10584                    && (TREE_CODE (argument) == OFFSET_REF
10585                        || TREE_CODE (argument) == SCOPE_REF))
10586             /* A pointer-to-member.  */
10587             ;
10588           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10589             ;
10590           else
10591             cp_parser_simulate_error (parser);
10592
10593           if (cp_parser_parse_definitely (parser))
10594             {
10595               if (address_p)
10596                 argument = build_x_unary_op (ADDR_EXPR, argument,
10597                                              tf_warning_or_error);
10598               return argument;
10599             }
10600         }
10601     }
10602   /* If the argument started with "&", there are no other valid
10603      alternatives at this point.  */
10604   if (address_p)
10605     {
10606       cp_parser_error (parser, "invalid non-type template argument");
10607       return error_mark_node;
10608     }
10609
10610   /* If the argument wasn't successfully parsed as a type-id followed
10611      by '>>', the argument can only be a constant expression now.
10612      Otherwise, we try parsing the constant-expression tentatively,
10613      because the argument could really be a type-id.  */
10614   if (maybe_type_id)
10615     cp_parser_parse_tentatively (parser);
10616   argument = cp_parser_constant_expression (parser,
10617                                             /*allow_non_constant_p=*/false,
10618                                             /*non_constant_p=*/NULL);
10619   argument = fold_non_dependent_expr (argument);
10620   if (!maybe_type_id)
10621     return argument;
10622   if (!cp_parser_next_token_ends_template_argument_p (parser))
10623     cp_parser_error (parser, "expected template-argument");
10624   if (cp_parser_parse_definitely (parser))
10625     return argument;
10626   /* We did our best to parse the argument as a non type-id, but that
10627      was the only alternative that matched (albeit with a '>' after
10628      it). We can assume it's just a typo from the user, and a
10629      diagnostic will then be issued.  */
10630   return cp_parser_type_id (parser);
10631 }
10632
10633 /* Parse an explicit-instantiation.
10634
10635    explicit-instantiation:
10636      template declaration
10637
10638    Although the standard says `declaration', what it really means is:
10639
10640    explicit-instantiation:
10641      template decl-specifier-seq [opt] declarator [opt] ;
10642
10643    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10644    supposed to be allowed.  A defect report has been filed about this
10645    issue.
10646
10647    GNU Extension:
10648
10649    explicit-instantiation:
10650      storage-class-specifier template
10651        decl-specifier-seq [opt] declarator [opt] ;
10652      function-specifier template
10653        decl-specifier-seq [opt] declarator [opt] ;  */
10654
10655 static void
10656 cp_parser_explicit_instantiation (cp_parser* parser)
10657 {
10658   int declares_class_or_enum;
10659   cp_decl_specifier_seq decl_specifiers;
10660   tree extension_specifier = NULL_TREE;
10661   cp_token *token;
10662
10663   /* Look for an (optional) storage-class-specifier or
10664      function-specifier.  */
10665   if (cp_parser_allow_gnu_extensions_p (parser))
10666     {
10667       extension_specifier
10668         = cp_parser_storage_class_specifier_opt (parser);
10669       if (!extension_specifier)
10670         extension_specifier
10671           = cp_parser_function_specifier_opt (parser,
10672                                               /*decl_specs=*/NULL);
10673     }
10674
10675   /* Look for the `template' keyword.  */
10676   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10677   /* Let the front end know that we are processing an explicit
10678      instantiation.  */
10679   begin_explicit_instantiation ();
10680   /* [temp.explicit] says that we are supposed to ignore access
10681      control while processing explicit instantiation directives.  */
10682   push_deferring_access_checks (dk_no_check);
10683   /* Parse a decl-specifier-seq.  */
10684   token = cp_lexer_peek_token (parser->lexer);
10685   cp_parser_decl_specifier_seq (parser,
10686                                 CP_PARSER_FLAGS_OPTIONAL,
10687                                 &decl_specifiers,
10688                                 &declares_class_or_enum);
10689   /* If there was exactly one decl-specifier, and it declared a class,
10690      and there's no declarator, then we have an explicit type
10691      instantiation.  */
10692   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10693     {
10694       tree type;
10695
10696       type = check_tag_decl (&decl_specifiers);
10697       /* Turn access control back on for names used during
10698          template instantiation.  */
10699       pop_deferring_access_checks ();
10700       if (type)
10701         do_type_instantiation (type, extension_specifier,
10702                                /*complain=*/tf_error);
10703     }
10704   else
10705     {
10706       cp_declarator *declarator;
10707       tree decl;
10708
10709       /* Parse the declarator.  */
10710       declarator
10711         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10712                                 /*ctor_dtor_or_conv_p=*/NULL,
10713                                 /*parenthesized_p=*/NULL,
10714                                 /*member_p=*/false);
10715       if (declares_class_or_enum & 2)
10716         cp_parser_check_for_definition_in_return_type (declarator,
10717                                                        decl_specifiers.type,
10718                                                        decl_specifiers.type_location);
10719       if (declarator != cp_error_declarator)
10720         {
10721           decl = grokdeclarator (declarator, &decl_specifiers,
10722                                  NORMAL, 0, &decl_specifiers.attributes);
10723           /* Turn access control back on for names used during
10724              template instantiation.  */
10725           pop_deferring_access_checks ();
10726           /* Do the explicit instantiation.  */
10727           do_decl_instantiation (decl, extension_specifier);
10728         }
10729       else
10730         {
10731           pop_deferring_access_checks ();
10732           /* Skip the body of the explicit instantiation.  */
10733           cp_parser_skip_to_end_of_statement (parser);
10734         }
10735     }
10736   /* We're done with the instantiation.  */
10737   end_explicit_instantiation ();
10738
10739   cp_parser_consume_semicolon_at_end_of_statement (parser);
10740 }
10741
10742 /* Parse an explicit-specialization.
10743
10744    explicit-specialization:
10745      template < > declaration
10746
10747    Although the standard says `declaration', what it really means is:
10748
10749    explicit-specialization:
10750      template <> decl-specifier [opt] init-declarator [opt] ;
10751      template <> function-definition
10752      template <> explicit-specialization
10753      template <> template-declaration  */
10754
10755 static void
10756 cp_parser_explicit_specialization (cp_parser* parser)
10757 {
10758   bool need_lang_pop;
10759   cp_token *token = cp_lexer_peek_token (parser->lexer);
10760
10761   /* Look for the `template' keyword.  */
10762   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10763   /* Look for the `<'.  */
10764   cp_parser_require (parser, CPP_LESS, "%<<%>");
10765   /* Look for the `>'.  */
10766   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10767   /* We have processed another parameter list.  */
10768   ++parser->num_template_parameter_lists;
10769   /* [temp]
10770
10771      A template ... explicit specialization ... shall not have C
10772      linkage.  */
10773   if (current_lang_name == lang_name_c)
10774     {
10775       error ("%Htemplate specialization with C linkage", &token->location);
10776       /* Give it C++ linkage to avoid confusing other parts of the
10777          front end.  */
10778       push_lang_context (lang_name_cplusplus);
10779       need_lang_pop = true;
10780     }
10781   else
10782     need_lang_pop = false;
10783   /* Let the front end know that we are beginning a specialization.  */
10784   if (!begin_specialization ())
10785     {
10786       end_specialization ();
10787       cp_parser_skip_to_end_of_block_or_statement (parser);
10788       return;
10789     }
10790
10791   /* If the next keyword is `template', we need to figure out whether
10792      or not we're looking a template-declaration.  */
10793   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10794     {
10795       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10796           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10797         cp_parser_template_declaration_after_export (parser,
10798                                                      /*member_p=*/false);
10799       else
10800         cp_parser_explicit_specialization (parser);
10801     }
10802   else
10803     /* Parse the dependent declaration.  */
10804     cp_parser_single_declaration (parser,
10805                                   /*checks=*/NULL,
10806                                   /*member_p=*/false,
10807                                   /*explicit_specialization_p=*/true,
10808                                   /*friend_p=*/NULL);
10809   /* We're done with the specialization.  */
10810   end_specialization ();
10811   /* For the erroneous case of a template with C linkage, we pushed an
10812      implicit C++ linkage scope; exit that scope now.  */
10813   if (need_lang_pop)
10814     pop_lang_context ();
10815   /* We're done with this parameter list.  */
10816   --parser->num_template_parameter_lists;
10817 }
10818
10819 /* Parse a type-specifier.
10820
10821    type-specifier:
10822      simple-type-specifier
10823      class-specifier
10824      enum-specifier
10825      elaborated-type-specifier
10826      cv-qualifier
10827
10828    GNU Extension:
10829
10830    type-specifier:
10831      __complex__
10832
10833    Returns a representation of the type-specifier.  For a
10834    class-specifier, enum-specifier, or elaborated-type-specifier, a
10835    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10836
10837    The parser flags FLAGS is used to control type-specifier parsing.
10838
10839    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10840    in a decl-specifier-seq.
10841
10842    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10843    class-specifier, enum-specifier, or elaborated-type-specifier, then
10844    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10845    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10846    zero.
10847
10848    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10849    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10850    is set to FALSE.  */
10851
10852 static tree
10853 cp_parser_type_specifier (cp_parser* parser,
10854                           cp_parser_flags flags,
10855                           cp_decl_specifier_seq *decl_specs,
10856                           bool is_declaration,
10857                           int* declares_class_or_enum,
10858                           bool* is_cv_qualifier)
10859 {
10860   tree type_spec = NULL_TREE;
10861   cp_token *token;
10862   enum rid keyword;
10863   cp_decl_spec ds = ds_last;
10864
10865   /* Assume this type-specifier does not declare a new type.  */
10866   if (declares_class_or_enum)
10867     *declares_class_or_enum = 0;
10868   /* And that it does not specify a cv-qualifier.  */
10869   if (is_cv_qualifier)
10870     *is_cv_qualifier = false;
10871   /* Peek at the next token.  */
10872   token = cp_lexer_peek_token (parser->lexer);
10873
10874   /* If we're looking at a keyword, we can use that to guide the
10875      production we choose.  */
10876   keyword = token->keyword;
10877   switch (keyword)
10878     {
10879     case RID_ENUM:
10880       /* Look for the enum-specifier.  */
10881       type_spec = cp_parser_enum_specifier (parser);
10882       /* If that worked, we're done.  */
10883       if (type_spec)
10884         {
10885           if (declares_class_or_enum)
10886             *declares_class_or_enum = 2;
10887           if (decl_specs)
10888             cp_parser_set_decl_spec_type (decl_specs,
10889                                           type_spec,
10890                                           token->location,
10891                                           /*user_defined_p=*/true);
10892           return type_spec;
10893         }
10894       else
10895         goto elaborated_type_specifier;
10896
10897       /* Any of these indicate either a class-specifier, or an
10898          elaborated-type-specifier.  */
10899     case RID_CLASS:
10900     case RID_STRUCT:
10901     case RID_UNION:
10902       /* Parse tentatively so that we can back up if we don't find a
10903          class-specifier.  */
10904       cp_parser_parse_tentatively (parser);
10905       /* Look for the class-specifier.  */
10906       type_spec = cp_parser_class_specifier (parser);
10907       /* If that worked, we're done.  */
10908       if (cp_parser_parse_definitely (parser))
10909         {
10910           if (declares_class_or_enum)
10911             *declares_class_or_enum = 2;
10912           if (decl_specs)
10913             cp_parser_set_decl_spec_type (decl_specs,
10914                                           type_spec,
10915                                           token->location,
10916                                           /*user_defined_p=*/true);
10917           return type_spec;
10918         }
10919
10920       /* Fall through.  */
10921     elaborated_type_specifier:
10922       /* We're declaring (not defining) a class or enum.  */
10923       if (declares_class_or_enum)
10924         *declares_class_or_enum = 1;
10925
10926       /* Fall through.  */
10927     case RID_TYPENAME:
10928       /* Look for an elaborated-type-specifier.  */
10929       type_spec
10930         = (cp_parser_elaborated_type_specifier
10931            (parser,
10932             decl_specs && decl_specs->specs[(int) ds_friend],
10933             is_declaration));
10934       if (decl_specs)
10935         cp_parser_set_decl_spec_type (decl_specs,
10936                                       type_spec,
10937                                       token->location,
10938                                       /*user_defined_p=*/true);
10939       return type_spec;
10940
10941     case RID_CONST:
10942       ds = ds_const;
10943       if (is_cv_qualifier)
10944         *is_cv_qualifier = true;
10945       break;
10946
10947     case RID_VOLATILE:
10948       ds = ds_volatile;
10949       if (is_cv_qualifier)
10950         *is_cv_qualifier = true;
10951       break;
10952
10953     case RID_RESTRICT:
10954       ds = ds_restrict;
10955       if (is_cv_qualifier)
10956         *is_cv_qualifier = true;
10957       break;
10958
10959     case RID_COMPLEX:
10960       /* The `__complex__' keyword is a GNU extension.  */
10961       ds = ds_complex;
10962       break;
10963
10964     default:
10965       break;
10966     }
10967
10968   /* Handle simple keywords.  */
10969   if (ds != ds_last)
10970     {
10971       if (decl_specs)
10972         {
10973           ++decl_specs->specs[(int)ds];
10974           decl_specs->any_specifiers_p = true;
10975         }
10976       return cp_lexer_consume_token (parser->lexer)->u.value;
10977     }
10978
10979   /* If we do not already have a type-specifier, assume we are looking
10980      at a simple-type-specifier.  */
10981   type_spec = cp_parser_simple_type_specifier (parser,
10982                                                decl_specs,
10983                                                flags);
10984
10985   /* If we didn't find a type-specifier, and a type-specifier was not
10986      optional in this context, issue an error message.  */
10987   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10988     {
10989       cp_parser_error (parser, "expected type specifier");
10990       return error_mark_node;
10991     }
10992
10993   return type_spec;
10994 }
10995
10996 /* Parse a simple-type-specifier.
10997
10998    simple-type-specifier:
10999      :: [opt] nested-name-specifier [opt] type-name
11000      :: [opt] nested-name-specifier template template-id
11001      char
11002      wchar_t
11003      bool
11004      short
11005      int
11006      long
11007      signed
11008      unsigned
11009      float
11010      double
11011      void
11012
11013    C++0x Extension:
11014
11015    simple-type-specifier:
11016      auto
11017      decltype ( expression )   
11018      char16_t
11019      char32_t
11020
11021    GNU Extension:
11022
11023    simple-type-specifier:
11024      __typeof__ unary-expression
11025      __typeof__ ( type-id )
11026
11027    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11028    appropriately updated.  */
11029
11030 static tree
11031 cp_parser_simple_type_specifier (cp_parser* parser,
11032                                  cp_decl_specifier_seq *decl_specs,
11033                                  cp_parser_flags flags)
11034 {
11035   tree type = NULL_TREE;
11036   cp_token *token;
11037
11038   /* Peek at the next token.  */
11039   token = cp_lexer_peek_token (parser->lexer);
11040
11041   /* If we're looking at a keyword, things are easy.  */
11042   switch (token->keyword)
11043     {
11044     case RID_CHAR:
11045       if (decl_specs)
11046         decl_specs->explicit_char_p = true;
11047       type = char_type_node;
11048       break;
11049     case RID_CHAR16:
11050       type = char16_type_node;
11051       break;
11052     case RID_CHAR32:
11053       type = char32_type_node;
11054       break;
11055     case RID_WCHAR:
11056       type = wchar_type_node;
11057       break;
11058     case RID_BOOL:
11059       type = boolean_type_node;
11060       break;
11061     case RID_SHORT:
11062       if (decl_specs)
11063         ++decl_specs->specs[(int) ds_short];
11064       type = short_integer_type_node;
11065       break;
11066     case RID_INT:
11067       if (decl_specs)
11068         decl_specs->explicit_int_p = true;
11069       type = integer_type_node;
11070       break;
11071     case RID_LONG:
11072       if (decl_specs)
11073         ++decl_specs->specs[(int) ds_long];
11074       type = long_integer_type_node;
11075       break;
11076     case RID_SIGNED:
11077       if (decl_specs)
11078         ++decl_specs->specs[(int) ds_signed];
11079       type = integer_type_node;
11080       break;
11081     case RID_UNSIGNED:
11082       if (decl_specs)
11083         ++decl_specs->specs[(int) ds_unsigned];
11084       type = unsigned_type_node;
11085       break;
11086     case RID_FLOAT:
11087       type = float_type_node;
11088       break;
11089     case RID_DOUBLE:
11090       type = double_type_node;
11091       break;
11092     case RID_VOID:
11093       type = void_type_node;
11094       break;
11095       
11096     case RID_AUTO:
11097       maybe_warn_cpp0x ("C++0x auto");
11098       type = make_auto ();
11099       break;
11100
11101     case RID_DECLTYPE:
11102       /* Parse the `decltype' type.  */
11103       type = cp_parser_decltype (parser);
11104
11105       if (decl_specs)
11106         cp_parser_set_decl_spec_type (decl_specs, type,
11107                                       token->location,
11108                                       /*user_defined_p=*/true);
11109
11110       return type;
11111
11112     case RID_TYPEOF:
11113       /* Consume the `typeof' token.  */
11114       cp_lexer_consume_token (parser->lexer);
11115       /* Parse the operand to `typeof'.  */
11116       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11117       /* If it is not already a TYPE, take its type.  */
11118       if (!TYPE_P (type))
11119         type = finish_typeof (type);
11120
11121       if (decl_specs)
11122         cp_parser_set_decl_spec_type (decl_specs, type,
11123                                       token->location,
11124                                       /*user_defined_p=*/true);
11125
11126       return type;
11127
11128     default:
11129       break;
11130     }
11131
11132   /* If the type-specifier was for a built-in type, we're done.  */
11133   if (type)
11134     {
11135       tree id;
11136
11137       /* Record the type.  */
11138       if (decl_specs
11139           && (token->keyword != RID_SIGNED
11140               && token->keyword != RID_UNSIGNED
11141               && token->keyword != RID_SHORT
11142               && token->keyword != RID_LONG))
11143         cp_parser_set_decl_spec_type (decl_specs,
11144                                       type,
11145                                       token->location,
11146                                       /*user_defined=*/false);
11147       if (decl_specs)
11148         decl_specs->any_specifiers_p = true;
11149
11150       /* Consume the token.  */
11151       id = cp_lexer_consume_token (parser->lexer)->u.value;
11152
11153       /* There is no valid C++ program where a non-template type is
11154          followed by a "<".  That usually indicates that the user thought
11155          that the type was a template.  */
11156       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11157
11158       return TYPE_NAME (type);
11159     }
11160
11161   /* The type-specifier must be a user-defined type.  */
11162   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11163     {
11164       bool qualified_p;
11165       bool global_p;
11166
11167       /* Don't gobble tokens or issue error messages if this is an
11168          optional type-specifier.  */
11169       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11170         cp_parser_parse_tentatively (parser);
11171
11172       /* Look for the optional `::' operator.  */
11173       global_p
11174         = (cp_parser_global_scope_opt (parser,
11175                                        /*current_scope_valid_p=*/false)
11176            != NULL_TREE);
11177       /* Look for the nested-name specifier.  */
11178       qualified_p
11179         = (cp_parser_nested_name_specifier_opt (parser,
11180                                                 /*typename_keyword_p=*/false,
11181                                                 /*check_dependency_p=*/true,
11182                                                 /*type_p=*/false,
11183                                                 /*is_declaration=*/false)
11184            != NULL_TREE);
11185       token = cp_lexer_peek_token (parser->lexer);
11186       /* If we have seen a nested-name-specifier, and the next token
11187          is `template', then we are using the template-id production.  */
11188       if (parser->scope
11189           && cp_parser_optional_template_keyword (parser))
11190         {
11191           /* Look for the template-id.  */
11192           type = cp_parser_template_id (parser,
11193                                         /*template_keyword_p=*/true,
11194                                         /*check_dependency_p=*/true,
11195                                         /*is_declaration=*/false);
11196           /* If the template-id did not name a type, we are out of
11197              luck.  */
11198           if (TREE_CODE (type) != TYPE_DECL)
11199             {
11200               cp_parser_error (parser, "expected template-id for type");
11201               type = NULL_TREE;
11202             }
11203         }
11204       /* Otherwise, look for a type-name.  */
11205       else
11206         type = cp_parser_type_name (parser);
11207       /* Keep track of all name-lookups performed in class scopes.  */
11208       if (type
11209           && !global_p
11210           && !qualified_p
11211           && TREE_CODE (type) == TYPE_DECL
11212           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11213         maybe_note_name_used_in_class (DECL_NAME (type), type);
11214       /* If it didn't work out, we don't have a TYPE.  */
11215       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11216           && !cp_parser_parse_definitely (parser))
11217         type = NULL_TREE;
11218       if (type && decl_specs)
11219         cp_parser_set_decl_spec_type (decl_specs, type,
11220                                       token->location,
11221                                       /*user_defined=*/true);
11222     }
11223
11224   /* If we didn't get a type-name, issue an error message.  */
11225   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11226     {
11227       cp_parser_error (parser, "expected type-name");
11228       return error_mark_node;
11229     }
11230
11231   /* There is no valid C++ program where a non-template type is
11232      followed by a "<".  That usually indicates that the user thought
11233      that the type was a template.  */
11234   if (type && type != error_mark_node)
11235     {
11236       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11237          If it is, then the '<'...'>' enclose protocol names rather than
11238          template arguments, and so everything is fine.  */
11239       if (c_dialect_objc ()
11240           && (objc_is_id (type) || objc_is_class_name (type)))
11241         {
11242           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11243           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11244
11245           /* Clobber the "unqualified" type previously entered into
11246              DECL_SPECS with the new, improved protocol-qualified version.  */
11247           if (decl_specs)
11248             decl_specs->type = qual_type;
11249
11250           return qual_type;
11251         }
11252
11253       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11254                                                token->location);
11255     }
11256
11257   return type;
11258 }
11259
11260 /* Parse a type-name.
11261
11262    type-name:
11263      class-name
11264      enum-name
11265      typedef-name
11266
11267    enum-name:
11268      identifier
11269
11270    typedef-name:
11271      identifier
11272
11273    Returns a TYPE_DECL for the type.  */
11274
11275 static tree
11276 cp_parser_type_name (cp_parser* parser)
11277 {
11278   tree type_decl;
11279
11280   /* We can't know yet whether it is a class-name or not.  */
11281   cp_parser_parse_tentatively (parser);
11282   /* Try a class-name.  */
11283   type_decl = cp_parser_class_name (parser,
11284                                     /*typename_keyword_p=*/false,
11285                                     /*template_keyword_p=*/false,
11286                                     none_type,
11287                                     /*check_dependency_p=*/true,
11288                                     /*class_head_p=*/false,
11289                                     /*is_declaration=*/false);
11290   /* If it's not a class-name, keep looking.  */
11291   if (!cp_parser_parse_definitely (parser))
11292     {
11293       /* It must be a typedef-name or an enum-name.  */
11294       return cp_parser_nonclass_name (parser);
11295     }
11296
11297   return type_decl;
11298 }
11299
11300 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11301
11302    enum-name:
11303      identifier
11304
11305    typedef-name:
11306      identifier
11307
11308    Returns a TYPE_DECL for the type.  */
11309
11310 static tree
11311 cp_parser_nonclass_name (cp_parser* parser)
11312 {
11313   tree type_decl;
11314   tree identifier;
11315
11316   cp_token *token = cp_lexer_peek_token (parser->lexer);
11317   identifier = cp_parser_identifier (parser);
11318   if (identifier == error_mark_node)
11319     return error_mark_node;
11320
11321   /* Look up the type-name.  */
11322   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11323
11324   if (TREE_CODE (type_decl) != TYPE_DECL
11325       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11326     {
11327       /* See if this is an Objective-C type.  */
11328       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11329       tree type = objc_get_protocol_qualified_type (identifier, protos);
11330       if (type)
11331         type_decl = TYPE_NAME (type);
11332     }
11333   
11334   /* Issue an error if we did not find a type-name.  */
11335   if (TREE_CODE (type_decl) != TYPE_DECL)
11336     {
11337       if (!cp_parser_simulate_error (parser))
11338         cp_parser_name_lookup_error (parser, identifier, type_decl,
11339                                      "is not a type", token->location);
11340       return error_mark_node;
11341     }
11342   /* Remember that the name was used in the definition of the
11343      current class so that we can check later to see if the
11344      meaning would have been different after the class was
11345      entirely defined.  */
11346   else if (type_decl != error_mark_node
11347            && !parser->scope)
11348     maybe_note_name_used_in_class (identifier, type_decl);
11349   
11350   return type_decl;
11351 }
11352
11353 /* Parse an elaborated-type-specifier.  Note that the grammar given
11354    here incorporates the resolution to DR68.
11355
11356    elaborated-type-specifier:
11357      class-key :: [opt] nested-name-specifier [opt] identifier
11358      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11359      enum-key :: [opt] nested-name-specifier [opt] identifier
11360      typename :: [opt] nested-name-specifier identifier
11361      typename :: [opt] nested-name-specifier template [opt]
11362        template-id
11363
11364    GNU extension:
11365
11366    elaborated-type-specifier:
11367      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11368      class-key attributes :: [opt] nested-name-specifier [opt]
11369                template [opt] template-id
11370      enum attributes :: [opt] nested-name-specifier [opt] identifier
11371
11372    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11373    declared `friend'.  If IS_DECLARATION is TRUE, then this
11374    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11375    something is being declared.
11376
11377    Returns the TYPE specified.  */
11378
11379 static tree
11380 cp_parser_elaborated_type_specifier (cp_parser* parser,
11381                                      bool is_friend,
11382                                      bool is_declaration)
11383 {
11384   enum tag_types tag_type;
11385   tree identifier;
11386   tree type = NULL_TREE;
11387   tree attributes = NULL_TREE;
11388   cp_token *token = NULL;
11389
11390   /* See if we're looking at the `enum' keyword.  */
11391   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11392     {
11393       /* Consume the `enum' token.  */
11394       cp_lexer_consume_token (parser->lexer);
11395       /* Remember that it's an enumeration type.  */
11396       tag_type = enum_type;
11397       /* Parse the optional `struct' or `class' key (for C++0x scoped
11398          enums).  */
11399       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11400           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11401         {
11402           if (cxx_dialect == cxx98)
11403             maybe_warn_cpp0x ("scoped enums");
11404
11405           /* Consume the `struct' or `class'.  */
11406           cp_lexer_consume_token (parser->lexer);
11407         }
11408       /* Parse the attributes.  */
11409       attributes = cp_parser_attributes_opt (parser);
11410     }
11411   /* Or, it might be `typename'.  */
11412   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11413                                            RID_TYPENAME))
11414     {
11415       /* Consume the `typename' token.  */
11416       cp_lexer_consume_token (parser->lexer);
11417       /* Remember that it's a `typename' type.  */
11418       tag_type = typename_type;
11419       /* The `typename' keyword is only allowed in templates.  */
11420       if (!processing_template_decl)
11421         permerror (input_location, "using %<typename%> outside of template");
11422     }
11423   /* Otherwise it must be a class-key.  */
11424   else
11425     {
11426       tag_type = cp_parser_class_key (parser);
11427       if (tag_type == none_type)
11428         return error_mark_node;
11429       /* Parse the attributes.  */
11430       attributes = cp_parser_attributes_opt (parser);
11431     }
11432
11433   /* Look for the `::' operator.  */
11434   cp_parser_global_scope_opt (parser,
11435                               /*current_scope_valid_p=*/false);
11436   /* Look for the nested-name-specifier.  */
11437   if (tag_type == typename_type)
11438     {
11439       if (!cp_parser_nested_name_specifier (parser,
11440                                            /*typename_keyword_p=*/true,
11441                                            /*check_dependency_p=*/true,
11442                                            /*type_p=*/true,
11443                                             is_declaration))
11444         return error_mark_node;
11445     }
11446   else
11447     /* Even though `typename' is not present, the proposed resolution
11448        to Core Issue 180 says that in `class A<T>::B', `B' should be
11449        considered a type-name, even if `A<T>' is dependent.  */
11450     cp_parser_nested_name_specifier_opt (parser,
11451                                          /*typename_keyword_p=*/true,
11452                                          /*check_dependency_p=*/true,
11453                                          /*type_p=*/true,
11454                                          is_declaration);
11455  /* For everything but enumeration types, consider a template-id.
11456     For an enumeration type, consider only a plain identifier.  */
11457   if (tag_type != enum_type)
11458     {
11459       bool template_p = false;
11460       tree decl;
11461
11462       /* Allow the `template' keyword.  */
11463       template_p = cp_parser_optional_template_keyword (parser);
11464       /* If we didn't see `template', we don't know if there's a
11465          template-id or not.  */
11466       if (!template_p)
11467         cp_parser_parse_tentatively (parser);
11468       /* Parse the template-id.  */
11469       token = cp_lexer_peek_token (parser->lexer);
11470       decl = cp_parser_template_id (parser, template_p,
11471                                     /*check_dependency_p=*/true,
11472                                     is_declaration);
11473       /* If we didn't find a template-id, look for an ordinary
11474          identifier.  */
11475       if (!template_p && !cp_parser_parse_definitely (parser))
11476         ;
11477       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11478          in effect, then we must assume that, upon instantiation, the
11479          template will correspond to a class.  */
11480       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11481                && tag_type == typename_type)
11482         type = make_typename_type (parser->scope, decl,
11483                                    typename_type,
11484                                    /*complain=*/tf_error);
11485       else
11486         type = TREE_TYPE (decl);
11487     }
11488
11489   if (!type)
11490     {
11491       token = cp_lexer_peek_token (parser->lexer);
11492       identifier = cp_parser_identifier (parser);
11493
11494       if (identifier == error_mark_node)
11495         {
11496           parser->scope = NULL_TREE;
11497           return error_mark_node;
11498         }
11499
11500       /* For a `typename', we needn't call xref_tag.  */
11501       if (tag_type == typename_type
11502           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11503         return cp_parser_make_typename_type (parser, parser->scope,
11504                                              identifier,
11505                                              token->location);
11506       /* Look up a qualified name in the usual way.  */
11507       if (parser->scope)
11508         {
11509           tree decl;
11510           tree ambiguous_decls;
11511
11512           decl = cp_parser_lookup_name (parser, identifier,
11513                                         tag_type,
11514                                         /*is_template=*/false,
11515                                         /*is_namespace=*/false,
11516                                         /*check_dependency=*/true,
11517                                         &ambiguous_decls,
11518                                         token->location);
11519
11520           /* If the lookup was ambiguous, an error will already have been
11521              issued.  */
11522           if (ambiguous_decls)
11523             return error_mark_node;
11524
11525           /* If we are parsing friend declaration, DECL may be a
11526              TEMPLATE_DECL tree node here.  However, we need to check
11527              whether this TEMPLATE_DECL results in valid code.  Consider
11528              the following example:
11529
11530                namespace N {
11531                  template <class T> class C {};
11532                }
11533                class X {
11534                  template <class T> friend class N::C; // #1, valid code
11535                };
11536                template <class T> class Y {
11537                  friend class N::C;                    // #2, invalid code
11538                };
11539
11540              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11541              name lookup of `N::C'.  We see that friend declaration must
11542              be template for the code to be valid.  Note that
11543              processing_template_decl does not work here since it is
11544              always 1 for the above two cases.  */
11545
11546           decl = (cp_parser_maybe_treat_template_as_class
11547                   (decl, /*tag_name_p=*/is_friend
11548                          && parser->num_template_parameter_lists));
11549
11550           if (TREE_CODE (decl) != TYPE_DECL)
11551             {
11552               cp_parser_diagnose_invalid_type_name (parser,
11553                                                     parser->scope,
11554                                                     identifier,
11555                                                     token->location);
11556               return error_mark_node;
11557             }
11558
11559           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11560             {
11561               bool allow_template = (parser->num_template_parameter_lists
11562                                       || DECL_SELF_REFERENCE_P (decl));
11563               type = check_elaborated_type_specifier (tag_type, decl, 
11564                                                       allow_template);
11565
11566               if (type == error_mark_node)
11567                 return error_mark_node;
11568             }
11569
11570           /* Forward declarations of nested types, such as
11571
11572                class C1::C2;
11573                class C1::C2::C3;
11574
11575              are invalid unless all components preceding the final '::'
11576              are complete.  If all enclosing types are complete, these
11577              declarations become merely pointless.
11578
11579              Invalid forward declarations of nested types are errors
11580              caught elsewhere in parsing.  Those that are pointless arrive
11581              here.  */
11582
11583           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11584               && !is_friend && !processing_explicit_instantiation)
11585             warning (0, "declaration %qD does not declare anything", decl);
11586
11587           type = TREE_TYPE (decl);
11588         }
11589       else
11590         {
11591           /* An elaborated-type-specifier sometimes introduces a new type and
11592              sometimes names an existing type.  Normally, the rule is that it
11593              introduces a new type only if there is not an existing type of
11594              the same name already in scope.  For example, given:
11595
11596                struct S {};
11597                void f() { struct S s; }
11598
11599              the `struct S' in the body of `f' is the same `struct S' as in
11600              the global scope; the existing definition is used.  However, if
11601              there were no global declaration, this would introduce a new
11602              local class named `S'.
11603
11604              An exception to this rule applies to the following code:
11605
11606                namespace N { struct S; }
11607
11608              Here, the elaborated-type-specifier names a new type
11609              unconditionally; even if there is already an `S' in the
11610              containing scope this declaration names a new type.
11611              This exception only applies if the elaborated-type-specifier
11612              forms the complete declaration:
11613
11614                [class.name]
11615
11616                A declaration consisting solely of `class-key identifier ;' is
11617                either a redeclaration of the name in the current scope or a
11618                forward declaration of the identifier as a class name.  It
11619                introduces the name into the current scope.
11620
11621              We are in this situation precisely when the next token is a `;'.
11622
11623              An exception to the exception is that a `friend' declaration does
11624              *not* name a new type; i.e., given:
11625
11626                struct S { friend struct T; };
11627
11628              `T' is not a new type in the scope of `S'.
11629
11630              Also, `new struct S' or `sizeof (struct S)' never results in the
11631              definition of a new type; a new type can only be declared in a
11632              declaration context.  */
11633
11634           tag_scope ts;
11635           bool template_p;
11636
11637           if (is_friend)
11638             /* Friends have special name lookup rules.  */
11639             ts = ts_within_enclosing_non_class;
11640           else if (is_declaration
11641                    && cp_lexer_next_token_is (parser->lexer,
11642                                               CPP_SEMICOLON))
11643             /* This is a `class-key identifier ;' */
11644             ts = ts_current;
11645           else
11646             ts = ts_global;
11647
11648           template_p =
11649             (parser->num_template_parameter_lists
11650              && (cp_parser_next_token_starts_class_definition_p (parser)
11651                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11652           /* An unqualified name was used to reference this type, so
11653              there were no qualifying templates.  */
11654           if (!cp_parser_check_template_parameters (parser,
11655                                                     /*num_templates=*/0,
11656                                                     token->location))
11657             return error_mark_node;
11658           type = xref_tag (tag_type, identifier, ts, template_p);
11659         }
11660     }
11661
11662   if (type == error_mark_node)
11663     return error_mark_node;
11664
11665   /* Allow attributes on forward declarations of classes.  */
11666   if (attributes)
11667     {
11668       if (TREE_CODE (type) == TYPENAME_TYPE)
11669         warning (OPT_Wattributes,
11670                  "attributes ignored on uninstantiated type");
11671       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11672                && ! processing_explicit_instantiation)
11673         warning (OPT_Wattributes,
11674                  "attributes ignored on template instantiation");
11675       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11676         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11677       else
11678         warning (OPT_Wattributes,
11679                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11680     }
11681
11682   if (tag_type != enum_type)
11683     cp_parser_check_class_key (tag_type, type);
11684
11685   /* A "<" cannot follow an elaborated type specifier.  If that
11686      happens, the user was probably trying to form a template-id.  */
11687   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11688
11689   return type;
11690 }
11691
11692 /* Parse an enum-specifier.
11693
11694    enum-specifier:
11695      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11696
11697    enum-key:
11698      enum
11699      enum class   [C++0x]
11700      enum struct  [C++0x]
11701
11702    enum-base:   [C++0x]
11703      : type-specifier-seq
11704
11705    GNU Extensions:
11706      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11707        { enumerator-list [opt] }attributes[opt]
11708
11709    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11710    if the token stream isn't an enum-specifier after all.  */
11711
11712 static tree
11713 cp_parser_enum_specifier (cp_parser* parser)
11714 {
11715   tree identifier;
11716   tree type;
11717   tree attributes;
11718   bool scoped_enum_p = false;
11719   tree underlying_type = NULL_TREE;
11720
11721   /* Parse tentatively so that we can back up if we don't find a
11722      enum-specifier.  */
11723   cp_parser_parse_tentatively (parser);
11724
11725   /* Caller guarantees that the current token is 'enum', an identifier
11726      possibly follows, and the token after that is an opening brace.
11727      If we don't have an identifier, fabricate an anonymous name for
11728      the enumeration being defined.  */
11729   cp_lexer_consume_token (parser->lexer);
11730
11731   /* Parse the "class" or "struct", which indicates a scoped
11732      enumeration type in C++0x.  */
11733   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11734       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11735     {
11736       if (cxx_dialect == cxx98)
11737         maybe_warn_cpp0x ("scoped enums");
11738
11739       /* Consume the `struct' or `class' token.  */
11740       cp_lexer_consume_token (parser->lexer);
11741
11742       scoped_enum_p = true;
11743     }
11744       
11745   attributes = cp_parser_attributes_opt (parser);
11746
11747   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11748     identifier = cp_parser_identifier (parser);
11749   else
11750     identifier = make_anon_name ();
11751
11752   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11753   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11754     {
11755       cp_decl_specifier_seq type_specifiers;
11756
11757       if (cxx_dialect == cxx98)
11758         maybe_warn_cpp0x ("scoped enums");
11759
11760       /* Consume the `:'.  */
11761       cp_lexer_consume_token (parser->lexer);
11762
11763       /* Parse the type-specifier-seq.  */
11764       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11765                                     &type_specifiers);
11766       if (type_specifiers.type == error_mark_node)
11767         return error_mark_node;
11768      
11769       /* If that didn't work, stop.  */
11770       if (type_specifiers.type != error_mark_node)
11771         {
11772           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11773                                             /*initialized=*/0, NULL);
11774           if (underlying_type == error_mark_node)
11775             underlying_type = NULL_TREE;
11776         }
11777       else
11778         cp_parser_error (parser, "expected underlying type of enumeration");
11779     }
11780
11781   /* Look for the `{' but don't consume it yet.  */
11782   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11783     cp_parser_simulate_error (parser);
11784
11785   if (!cp_parser_parse_definitely (parser))
11786     return NULL_TREE;
11787
11788   /* Issue an error message if type-definitions are forbidden here.  */
11789   if (!cp_parser_check_type_definition (parser))
11790     type = error_mark_node;
11791   else
11792     /* Create the new type.  We do this before consuming the opening
11793        brace so the enum will be recorded as being on the line of its
11794        tag (or the 'enum' keyword, if there is no tag).  */
11795     type = start_enum (identifier, underlying_type, scoped_enum_p);
11796   
11797   /* Consume the opening brace.  */
11798   cp_lexer_consume_token (parser->lexer);
11799
11800   if (type == error_mark_node)
11801     {
11802       cp_parser_skip_to_end_of_block_or_statement (parser);
11803       return error_mark_node;
11804     }
11805
11806   /* If the next token is not '}', then there are some enumerators.  */
11807   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11808     cp_parser_enumerator_list (parser, type);
11809
11810   /* Consume the final '}'.  */
11811   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11812
11813   /* Look for trailing attributes to apply to this enumeration, and
11814      apply them if appropriate.  */
11815   if (cp_parser_allow_gnu_extensions_p (parser))
11816     {
11817       tree trailing_attr = cp_parser_attributes_opt (parser);
11818       cplus_decl_attributes (&type,
11819                              trailing_attr,
11820                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11821     }
11822
11823   /* Finish up the enumeration.  */
11824   finish_enum (type);
11825
11826   return type;
11827 }
11828
11829 /* Parse an enumerator-list.  The enumerators all have the indicated
11830    TYPE.
11831
11832    enumerator-list:
11833      enumerator-definition
11834      enumerator-list , enumerator-definition  */
11835
11836 static void
11837 cp_parser_enumerator_list (cp_parser* parser, tree type)
11838 {
11839   while (true)
11840     {
11841       /* Parse an enumerator-definition.  */
11842       cp_parser_enumerator_definition (parser, type);
11843
11844       /* If the next token is not a ',', we've reached the end of
11845          the list.  */
11846       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11847         break;
11848       /* Otherwise, consume the `,' and keep going.  */
11849       cp_lexer_consume_token (parser->lexer);
11850       /* If the next token is a `}', there is a trailing comma.  */
11851       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11852         {
11853           if (!in_system_header)
11854             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11855           break;
11856         }
11857     }
11858 }
11859
11860 /* Parse an enumerator-definition.  The enumerator has the indicated
11861    TYPE.
11862
11863    enumerator-definition:
11864      enumerator
11865      enumerator = constant-expression
11866
11867    enumerator:
11868      identifier  */
11869
11870 static void
11871 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11872 {
11873   tree identifier;
11874   tree value;
11875
11876   /* Look for the identifier.  */
11877   identifier = cp_parser_identifier (parser);
11878   if (identifier == error_mark_node)
11879     return;
11880
11881   /* If the next token is an '=', then there is an explicit value.  */
11882   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11883     {
11884       /* Consume the `=' token.  */
11885       cp_lexer_consume_token (parser->lexer);
11886       /* Parse the value.  */
11887       value = cp_parser_constant_expression (parser,
11888                                              /*allow_non_constant_p=*/false,
11889                                              NULL);
11890     }
11891   else
11892     value = NULL_TREE;
11893
11894   /* Create the enumerator.  */
11895   build_enumerator (identifier, value, type);
11896 }
11897
11898 /* Parse a namespace-name.
11899
11900    namespace-name:
11901      original-namespace-name
11902      namespace-alias
11903
11904    Returns the NAMESPACE_DECL for the namespace.  */
11905
11906 static tree
11907 cp_parser_namespace_name (cp_parser* parser)
11908 {
11909   tree identifier;
11910   tree namespace_decl;
11911
11912   cp_token *token = cp_lexer_peek_token (parser->lexer);
11913
11914   /* Get the name of the namespace.  */
11915   identifier = cp_parser_identifier (parser);
11916   if (identifier == error_mark_node)
11917     return error_mark_node;
11918
11919   /* Look up the identifier in the currently active scope.  Look only
11920      for namespaces, due to:
11921
11922        [basic.lookup.udir]
11923
11924        When looking up a namespace-name in a using-directive or alias
11925        definition, only namespace names are considered.
11926
11927      And:
11928
11929        [basic.lookup.qual]
11930
11931        During the lookup of a name preceding the :: scope resolution
11932        operator, object, function, and enumerator names are ignored.
11933
11934      (Note that cp_parser_qualifying_entity only calls this
11935      function if the token after the name is the scope resolution
11936      operator.)  */
11937   namespace_decl = cp_parser_lookup_name (parser, identifier,
11938                                           none_type,
11939                                           /*is_template=*/false,
11940                                           /*is_namespace=*/true,
11941                                           /*check_dependency=*/true,
11942                                           /*ambiguous_decls=*/NULL,
11943                                           token->location);
11944   /* If it's not a namespace, issue an error.  */
11945   if (namespace_decl == error_mark_node
11946       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11947     {
11948       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11949         error ("%H%qD is not a namespace-name", &token->location, identifier);
11950       cp_parser_error (parser, "expected namespace-name");
11951       namespace_decl = error_mark_node;
11952     }
11953
11954   return namespace_decl;
11955 }
11956
11957 /* Parse a namespace-definition.
11958
11959    namespace-definition:
11960      named-namespace-definition
11961      unnamed-namespace-definition
11962
11963    named-namespace-definition:
11964      original-namespace-definition
11965      extension-namespace-definition
11966
11967    original-namespace-definition:
11968      namespace identifier { namespace-body }
11969
11970    extension-namespace-definition:
11971      namespace original-namespace-name { namespace-body }
11972
11973    unnamed-namespace-definition:
11974      namespace { namespace-body } */
11975
11976 static void
11977 cp_parser_namespace_definition (cp_parser* parser)
11978 {
11979   tree identifier, attribs;
11980   bool has_visibility;
11981   bool is_inline;
11982
11983   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11984     {
11985       is_inline = true;
11986       cp_lexer_consume_token (parser->lexer);
11987     }
11988   else
11989     is_inline = false;
11990
11991   /* Look for the `namespace' keyword.  */
11992   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11993
11994   /* Get the name of the namespace.  We do not attempt to distinguish
11995      between an original-namespace-definition and an
11996      extension-namespace-definition at this point.  The semantic
11997      analysis routines are responsible for that.  */
11998   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11999     identifier = cp_parser_identifier (parser);
12000   else
12001     identifier = NULL_TREE;
12002
12003   /* Parse any specified attributes.  */
12004   attribs = cp_parser_attributes_opt (parser);
12005
12006   /* Look for the `{' to start the namespace.  */
12007   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12008   /* Start the namespace.  */
12009   push_namespace (identifier);
12010
12011   /* "inline namespace" is equivalent to a stub namespace definition
12012      followed by a strong using directive.  */
12013   if (is_inline)
12014     {
12015       tree name_space = current_namespace;
12016       /* Set up namespace association.  */
12017       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12018         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12019                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12020       /* Import the contents of the inline namespace.  */
12021       pop_namespace ();
12022       do_using_directive (name_space);
12023       push_namespace (identifier);
12024     }
12025
12026   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12027
12028   /* Parse the body of the namespace.  */
12029   cp_parser_namespace_body (parser);
12030
12031 #ifdef HANDLE_PRAGMA_VISIBILITY
12032   if (has_visibility)
12033     pop_visibility ();
12034 #endif
12035
12036   /* Finish the namespace.  */
12037   pop_namespace ();
12038   /* Look for the final `}'.  */
12039   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12040 }
12041
12042 /* Parse a namespace-body.
12043
12044    namespace-body:
12045      declaration-seq [opt]  */
12046
12047 static void
12048 cp_parser_namespace_body (cp_parser* parser)
12049 {
12050   cp_parser_declaration_seq_opt (parser);
12051 }
12052
12053 /* Parse a namespace-alias-definition.
12054
12055    namespace-alias-definition:
12056      namespace identifier = qualified-namespace-specifier ;  */
12057
12058 static void
12059 cp_parser_namespace_alias_definition (cp_parser* parser)
12060 {
12061   tree identifier;
12062   tree namespace_specifier;
12063
12064   cp_token *token = cp_lexer_peek_token (parser->lexer);
12065
12066   /* Look for the `namespace' keyword.  */
12067   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12068   /* Look for the identifier.  */
12069   identifier = cp_parser_identifier (parser);
12070   if (identifier == error_mark_node)
12071     return;
12072   /* Look for the `=' token.  */
12073   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12074       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12075     {
12076       error ("%H%<namespace%> definition is not allowed here", &token->location);
12077       /* Skip the definition.  */
12078       cp_lexer_consume_token (parser->lexer);
12079       if (cp_parser_skip_to_closing_brace (parser))
12080         cp_lexer_consume_token (parser->lexer);
12081       return;
12082     }
12083   cp_parser_require (parser, CPP_EQ, "%<=%>");
12084   /* Look for the qualified-namespace-specifier.  */
12085   namespace_specifier
12086     = cp_parser_qualified_namespace_specifier (parser);
12087   /* Look for the `;' token.  */
12088   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12089
12090   /* Register the alias in the symbol table.  */
12091   do_namespace_alias (identifier, namespace_specifier);
12092 }
12093
12094 /* Parse a qualified-namespace-specifier.
12095
12096    qualified-namespace-specifier:
12097      :: [opt] nested-name-specifier [opt] namespace-name
12098
12099    Returns a NAMESPACE_DECL corresponding to the specified
12100    namespace.  */
12101
12102 static tree
12103 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12104 {
12105   /* Look for the optional `::'.  */
12106   cp_parser_global_scope_opt (parser,
12107                               /*current_scope_valid_p=*/false);
12108
12109   /* Look for the optional nested-name-specifier.  */
12110   cp_parser_nested_name_specifier_opt (parser,
12111                                        /*typename_keyword_p=*/false,
12112                                        /*check_dependency_p=*/true,
12113                                        /*type_p=*/false,
12114                                        /*is_declaration=*/true);
12115
12116   return cp_parser_namespace_name (parser);
12117 }
12118
12119 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12120    access declaration.
12121
12122    using-declaration:
12123      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12124      using :: unqualified-id ;  
12125
12126    access-declaration:
12127      qualified-id ;  
12128
12129    */
12130
12131 static bool
12132 cp_parser_using_declaration (cp_parser* parser, 
12133                              bool access_declaration_p)
12134 {
12135   cp_token *token;
12136   bool typename_p = false;
12137   bool global_scope_p;
12138   tree decl;
12139   tree identifier;
12140   tree qscope;
12141
12142   if (access_declaration_p)
12143     cp_parser_parse_tentatively (parser);
12144   else
12145     {
12146       /* Look for the `using' keyword.  */
12147       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12148       
12149       /* Peek at the next token.  */
12150       token = cp_lexer_peek_token (parser->lexer);
12151       /* See if it's `typename'.  */
12152       if (token->keyword == RID_TYPENAME)
12153         {
12154           /* Remember that we've seen it.  */
12155           typename_p = true;
12156           /* Consume the `typename' token.  */
12157           cp_lexer_consume_token (parser->lexer);
12158         }
12159     }
12160
12161   /* Look for the optional global scope qualification.  */
12162   global_scope_p
12163     = (cp_parser_global_scope_opt (parser,
12164                                    /*current_scope_valid_p=*/false)
12165        != NULL_TREE);
12166
12167   /* If we saw `typename', or didn't see `::', then there must be a
12168      nested-name-specifier present.  */
12169   if (typename_p || !global_scope_p)
12170     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12171                                               /*check_dependency_p=*/true,
12172                                               /*type_p=*/false,
12173                                               /*is_declaration=*/true);
12174   /* Otherwise, we could be in either of the two productions.  In that
12175      case, treat the nested-name-specifier as optional.  */
12176   else
12177     qscope = cp_parser_nested_name_specifier_opt (parser,
12178                                                   /*typename_keyword_p=*/false,
12179                                                   /*check_dependency_p=*/true,
12180                                                   /*type_p=*/false,
12181                                                   /*is_declaration=*/true);
12182   if (!qscope)
12183     qscope = global_namespace;
12184
12185   if (access_declaration_p && cp_parser_error_occurred (parser))
12186     /* Something has already gone wrong; there's no need to parse
12187        further.  Since an error has occurred, the return value of
12188        cp_parser_parse_definitely will be false, as required.  */
12189     return cp_parser_parse_definitely (parser);
12190
12191   token = cp_lexer_peek_token (parser->lexer);
12192   /* Parse the unqualified-id.  */
12193   identifier = cp_parser_unqualified_id (parser,
12194                                          /*template_keyword_p=*/false,
12195                                          /*check_dependency_p=*/true,
12196                                          /*declarator_p=*/true,
12197                                          /*optional_p=*/false);
12198
12199   if (access_declaration_p)
12200     {
12201       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12202         cp_parser_simulate_error (parser);
12203       if (!cp_parser_parse_definitely (parser))
12204         return false;
12205     }
12206
12207   /* The function we call to handle a using-declaration is different
12208      depending on what scope we are in.  */
12209   if (qscope == error_mark_node || identifier == error_mark_node)
12210     ;
12211   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12212            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12213     /* [namespace.udecl]
12214
12215        A using declaration shall not name a template-id.  */
12216     error ("%Ha template-id may not appear in a using-declaration",
12217             &token->location);
12218   else
12219     {
12220       if (at_class_scope_p ())
12221         {
12222           /* Create the USING_DECL.  */
12223           decl = do_class_using_decl (parser->scope, identifier);
12224
12225           if (check_for_bare_parameter_packs (decl))
12226             return false;
12227           else
12228             /* Add it to the list of members in this class.  */
12229             finish_member_declaration (decl);
12230         }
12231       else
12232         {
12233           decl = cp_parser_lookup_name_simple (parser,
12234                                                identifier,
12235                                                token->location);
12236           if (decl == error_mark_node)
12237             cp_parser_name_lookup_error (parser, identifier,
12238                                          decl, NULL,
12239                                          token->location);
12240           else if (check_for_bare_parameter_packs (decl))
12241             return false;
12242           else if (!at_namespace_scope_p ())
12243             do_local_using_decl (decl, qscope, identifier);
12244           else
12245             do_toplevel_using_decl (decl, qscope, identifier);
12246         }
12247     }
12248
12249   /* Look for the final `;'.  */
12250   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12251   
12252   return true;
12253 }
12254
12255 /* Parse a using-directive.
12256
12257    using-directive:
12258      using namespace :: [opt] nested-name-specifier [opt]
12259        namespace-name ;  */
12260
12261 static void
12262 cp_parser_using_directive (cp_parser* parser)
12263 {
12264   tree namespace_decl;
12265   tree attribs;
12266
12267   /* Look for the `using' keyword.  */
12268   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12269   /* And the `namespace' keyword.  */
12270   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12271   /* Look for the optional `::' operator.  */
12272   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12273   /* And the optional nested-name-specifier.  */
12274   cp_parser_nested_name_specifier_opt (parser,
12275                                        /*typename_keyword_p=*/false,
12276                                        /*check_dependency_p=*/true,
12277                                        /*type_p=*/false,
12278                                        /*is_declaration=*/true);
12279   /* Get the namespace being used.  */
12280   namespace_decl = cp_parser_namespace_name (parser);
12281   /* And any specified attributes.  */
12282   attribs = cp_parser_attributes_opt (parser);
12283   /* Update the symbol table.  */
12284   parse_using_directive (namespace_decl, attribs);
12285   /* Look for the final `;'.  */
12286   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12287 }
12288
12289 /* Parse an asm-definition.
12290
12291    asm-definition:
12292      asm ( string-literal ) ;
12293
12294    GNU Extension:
12295
12296    asm-definition:
12297      asm volatile [opt] ( string-literal ) ;
12298      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12299      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12300                           : asm-operand-list [opt] ) ;
12301      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12302                           : asm-operand-list [opt]
12303                           : asm-operand-list [opt] ) ;  */
12304
12305 static void
12306 cp_parser_asm_definition (cp_parser* parser)
12307 {
12308   tree string;
12309   tree outputs = NULL_TREE;
12310   tree inputs = NULL_TREE;
12311   tree clobbers = NULL_TREE;
12312   tree asm_stmt;
12313   bool volatile_p = false;
12314   bool extended_p = false;
12315   bool invalid_inputs_p = false;
12316   bool invalid_outputs_p = false;
12317
12318   /* Look for the `asm' keyword.  */
12319   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12320   /* See if the next token is `volatile'.  */
12321   if (cp_parser_allow_gnu_extensions_p (parser)
12322       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12323     {
12324       /* Remember that we saw the `volatile' keyword.  */
12325       volatile_p = true;
12326       /* Consume the token.  */
12327       cp_lexer_consume_token (parser->lexer);
12328     }
12329   /* Look for the opening `('.  */
12330   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12331     return;
12332   /* Look for the string.  */
12333   string = cp_parser_string_literal (parser, false, false);
12334   if (string == error_mark_node)
12335     {
12336       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12337                                              /*consume_paren=*/true);
12338       return;
12339     }
12340
12341   /* If we're allowing GNU extensions, check for the extended assembly
12342      syntax.  Unfortunately, the `:' tokens need not be separated by
12343      a space in C, and so, for compatibility, we tolerate that here
12344      too.  Doing that means that we have to treat the `::' operator as
12345      two `:' tokens.  */
12346   if (cp_parser_allow_gnu_extensions_p (parser)
12347       && parser->in_function_body
12348       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12349           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12350     {
12351       bool inputs_p = false;
12352       bool clobbers_p = false;
12353
12354       /* The extended syntax was used.  */
12355       extended_p = true;
12356
12357       /* Look for outputs.  */
12358       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12359         {
12360           /* Consume the `:'.  */
12361           cp_lexer_consume_token (parser->lexer);
12362           /* Parse the output-operands.  */
12363           if (cp_lexer_next_token_is_not (parser->lexer,
12364                                           CPP_COLON)
12365               && cp_lexer_next_token_is_not (parser->lexer,
12366                                              CPP_SCOPE)
12367               && cp_lexer_next_token_is_not (parser->lexer,
12368                                              CPP_CLOSE_PAREN))
12369             outputs = cp_parser_asm_operand_list (parser);
12370
12371             if (outputs == error_mark_node)
12372               invalid_outputs_p = true;
12373         }
12374       /* If the next token is `::', there are no outputs, and the
12375          next token is the beginning of the inputs.  */
12376       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12377         /* The inputs are coming next.  */
12378         inputs_p = true;
12379
12380       /* Look for inputs.  */
12381       if (inputs_p
12382           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12383         {
12384           /* Consume the `:' or `::'.  */
12385           cp_lexer_consume_token (parser->lexer);
12386           /* Parse the output-operands.  */
12387           if (cp_lexer_next_token_is_not (parser->lexer,
12388                                           CPP_COLON)
12389               && cp_lexer_next_token_is_not (parser->lexer,
12390                                              CPP_CLOSE_PAREN))
12391             inputs = cp_parser_asm_operand_list (parser);
12392
12393             if (inputs == error_mark_node)
12394               invalid_inputs_p = true;
12395         }
12396       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12397         /* The clobbers are coming next.  */
12398         clobbers_p = true;
12399
12400       /* Look for clobbers.  */
12401       if (clobbers_p
12402           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12403         {
12404           /* Consume the `:' or `::'.  */
12405           cp_lexer_consume_token (parser->lexer);
12406           /* Parse the clobbers.  */
12407           if (cp_lexer_next_token_is_not (parser->lexer,
12408                                           CPP_CLOSE_PAREN))
12409             clobbers = cp_parser_asm_clobber_list (parser);
12410         }
12411     }
12412   /* Look for the closing `)'.  */
12413   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12414     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12415                                            /*consume_paren=*/true);
12416   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12417
12418   if (!invalid_inputs_p && !invalid_outputs_p)
12419     {
12420       /* Create the ASM_EXPR.  */
12421       if (parser->in_function_body)
12422         {
12423           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12424                                       inputs, clobbers);
12425           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12426           if (!extended_p)
12427             {
12428               tree temp = asm_stmt;
12429               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12430                 temp = TREE_OPERAND (temp, 0);
12431
12432               ASM_INPUT_P (temp) = 1;
12433             }
12434         }
12435       else
12436         cgraph_add_asm_node (string);
12437     }
12438 }
12439
12440 /* Declarators [gram.dcl.decl] */
12441
12442 /* Parse an init-declarator.
12443
12444    init-declarator:
12445      declarator initializer [opt]
12446
12447    GNU Extension:
12448
12449    init-declarator:
12450      declarator asm-specification [opt] attributes [opt] initializer [opt]
12451
12452    function-definition:
12453      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12454        function-body
12455      decl-specifier-seq [opt] declarator function-try-block
12456
12457    GNU Extension:
12458
12459    function-definition:
12460      __extension__ function-definition
12461
12462    The DECL_SPECIFIERS apply to this declarator.  Returns a
12463    representation of the entity declared.  If MEMBER_P is TRUE, then
12464    this declarator appears in a class scope.  The new DECL created by
12465    this declarator is returned.
12466
12467    The CHECKS are access checks that should be performed once we know
12468    what entity is being declared (and, therefore, what classes have
12469    befriended it).
12470
12471    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12472    for a function-definition here as well.  If the declarator is a
12473    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12474    be TRUE upon return.  By that point, the function-definition will
12475    have been completely parsed.
12476
12477    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12478    is FALSE.  */
12479
12480 static tree
12481 cp_parser_init_declarator (cp_parser* parser,
12482                            cp_decl_specifier_seq *decl_specifiers,
12483                            VEC (deferred_access_check,gc)* checks,
12484                            bool function_definition_allowed_p,
12485                            bool member_p,
12486                            int declares_class_or_enum,
12487                            bool* function_definition_p)
12488 {
12489   cp_token *token = NULL, *asm_spec_start_token = NULL,
12490            *attributes_start_token = NULL;
12491   cp_declarator *declarator;
12492   tree prefix_attributes;
12493   tree attributes;
12494   tree asm_specification;
12495   tree initializer;
12496   tree decl = NULL_TREE;
12497   tree scope;
12498   int is_initialized;
12499   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12500      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12501      "(...)".  */
12502   enum cpp_ttype initialization_kind;
12503   bool is_direct_init = false;
12504   bool is_non_constant_init;
12505   int ctor_dtor_or_conv_p;
12506   bool friend_p;
12507   tree pushed_scope = NULL;
12508
12509   /* Gather the attributes that were provided with the
12510      decl-specifiers.  */
12511   prefix_attributes = decl_specifiers->attributes;
12512
12513   /* Assume that this is not the declarator for a function
12514      definition.  */
12515   if (function_definition_p)
12516     *function_definition_p = false;
12517
12518   /* Defer access checks while parsing the declarator; we cannot know
12519      what names are accessible until we know what is being
12520      declared.  */
12521   resume_deferring_access_checks ();
12522
12523   /* Parse the declarator.  */
12524   token = cp_lexer_peek_token (parser->lexer);
12525   declarator
12526     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12527                             &ctor_dtor_or_conv_p,
12528                             /*parenthesized_p=*/NULL,
12529                             /*member_p=*/false);
12530   /* Gather up the deferred checks.  */
12531   stop_deferring_access_checks ();
12532
12533   /* If the DECLARATOR was erroneous, there's no need to go
12534      further.  */
12535   if (declarator == cp_error_declarator)
12536     return error_mark_node;
12537
12538   /* Check that the number of template-parameter-lists is OK.  */
12539   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12540                                                        token->location))
12541     return error_mark_node;
12542
12543   if (declares_class_or_enum & 2)
12544     cp_parser_check_for_definition_in_return_type (declarator,
12545                                                    decl_specifiers->type,
12546                                                    decl_specifiers->type_location);
12547
12548   /* Figure out what scope the entity declared by the DECLARATOR is
12549      located in.  `grokdeclarator' sometimes changes the scope, so
12550      we compute it now.  */
12551   scope = get_scope_of_declarator (declarator);
12552
12553   /* If we're allowing GNU extensions, look for an asm-specification
12554      and attributes.  */
12555   if (cp_parser_allow_gnu_extensions_p (parser))
12556     {
12557       /* Look for an asm-specification.  */
12558       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12559       asm_specification = cp_parser_asm_specification_opt (parser);
12560       /* And attributes.  */
12561       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12562       attributes = cp_parser_attributes_opt (parser);
12563     }
12564   else
12565     {
12566       asm_specification = NULL_TREE;
12567       attributes = NULL_TREE;
12568     }
12569
12570   /* Peek at the next token.  */
12571   token = cp_lexer_peek_token (parser->lexer);
12572   /* Check to see if the token indicates the start of a
12573      function-definition.  */
12574   if (function_declarator_p (declarator)
12575       && cp_parser_token_starts_function_definition_p (token))
12576     {
12577       if (!function_definition_allowed_p)
12578         {
12579           /* If a function-definition should not appear here, issue an
12580              error message.  */
12581           cp_parser_error (parser,
12582                            "a function-definition is not allowed here");
12583           return error_mark_node;
12584         }
12585       else
12586         {
12587           location_t func_brace_location
12588             = cp_lexer_peek_token (parser->lexer)->location;
12589
12590           /* Neither attributes nor an asm-specification are allowed
12591              on a function-definition.  */
12592           if (asm_specification)
12593             error ("%Han asm-specification is not allowed "
12594                    "on a function-definition",
12595                    &asm_spec_start_token->location);
12596           if (attributes)
12597             error ("%Hattributes are not allowed on a function-definition",
12598                    &attributes_start_token->location);
12599           /* This is a function-definition.  */
12600           *function_definition_p = true;
12601
12602           /* Parse the function definition.  */
12603           if (member_p)
12604             decl = cp_parser_save_member_function_body (parser,
12605                                                         decl_specifiers,
12606                                                         declarator,
12607                                                         prefix_attributes);
12608           else
12609             decl
12610               = (cp_parser_function_definition_from_specifiers_and_declarator
12611                  (parser, decl_specifiers, prefix_attributes, declarator));
12612
12613           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12614             {
12615               /* This is where the prologue starts...  */
12616               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12617                 = func_brace_location;
12618             }
12619
12620           return decl;
12621         }
12622     }
12623
12624   /* [dcl.dcl]
12625
12626      Only in function declarations for constructors, destructors, and
12627      type conversions can the decl-specifier-seq be omitted.
12628
12629      We explicitly postpone this check past the point where we handle
12630      function-definitions because we tolerate function-definitions
12631      that are missing their return types in some modes.  */
12632   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12633     {
12634       cp_parser_error (parser,
12635                        "expected constructor, destructor, or type conversion");
12636       return error_mark_node;
12637     }
12638
12639   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12640   if (token->type == CPP_EQ
12641       || token->type == CPP_OPEN_PAREN
12642       || token->type == CPP_OPEN_BRACE)
12643     {
12644       is_initialized = SD_INITIALIZED;
12645       initialization_kind = token->type;
12646
12647       if (token->type == CPP_EQ
12648           && function_declarator_p (declarator))
12649         {
12650           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12651           if (t2->keyword == RID_DEFAULT)
12652             is_initialized = SD_DEFAULTED;
12653           else if (t2->keyword == RID_DELETE)
12654             is_initialized = SD_DELETED;
12655         }
12656     }
12657   else
12658     {
12659       /* If the init-declarator isn't initialized and isn't followed by a
12660          `,' or `;', it's not a valid init-declarator.  */
12661       if (token->type != CPP_COMMA
12662           && token->type != CPP_SEMICOLON)
12663         {
12664           cp_parser_error (parser, "expected initializer");
12665           return error_mark_node;
12666         }
12667       is_initialized = SD_UNINITIALIZED;
12668       initialization_kind = CPP_EOF;
12669     }
12670
12671   /* Because start_decl has side-effects, we should only call it if we
12672      know we're going ahead.  By this point, we know that we cannot
12673      possibly be looking at any other construct.  */
12674   cp_parser_commit_to_tentative_parse (parser);
12675
12676   /* If the decl specifiers were bad, issue an error now that we're
12677      sure this was intended to be a declarator.  Then continue
12678      declaring the variable(s), as int, to try to cut down on further
12679      errors.  */
12680   if (decl_specifiers->any_specifiers_p
12681       && decl_specifiers->type == error_mark_node)
12682     {
12683       cp_parser_error (parser, "invalid type in declaration");
12684       decl_specifiers->type = integer_type_node;
12685     }
12686
12687   /* Check to see whether or not this declaration is a friend.  */
12688   friend_p = cp_parser_friend_p (decl_specifiers);
12689
12690   /* Enter the newly declared entry in the symbol table.  If we're
12691      processing a declaration in a class-specifier, we wait until
12692      after processing the initializer.  */
12693   if (!member_p)
12694     {
12695       if (parser->in_unbraced_linkage_specification_p)
12696         decl_specifiers->storage_class = sc_extern;
12697       decl = start_decl (declarator, decl_specifiers,
12698                          is_initialized, attributes, prefix_attributes,
12699                          &pushed_scope);
12700     }
12701   else if (scope)
12702     /* Enter the SCOPE.  That way unqualified names appearing in the
12703        initializer will be looked up in SCOPE.  */
12704     pushed_scope = push_scope (scope);
12705
12706   /* Perform deferred access control checks, now that we know in which
12707      SCOPE the declared entity resides.  */
12708   if (!member_p && decl)
12709     {
12710       tree saved_current_function_decl = NULL_TREE;
12711
12712       /* If the entity being declared is a function, pretend that we
12713          are in its scope.  If it is a `friend', it may have access to
12714          things that would not otherwise be accessible.  */
12715       if (TREE_CODE (decl) == FUNCTION_DECL)
12716         {
12717           saved_current_function_decl = current_function_decl;
12718           current_function_decl = decl;
12719         }
12720
12721       /* Perform access checks for template parameters.  */
12722       cp_parser_perform_template_parameter_access_checks (checks);
12723
12724       /* Perform the access control checks for the declarator and the
12725          decl-specifiers.  */
12726       perform_deferred_access_checks ();
12727
12728       /* Restore the saved value.  */
12729       if (TREE_CODE (decl) == FUNCTION_DECL)
12730         current_function_decl = saved_current_function_decl;
12731     }
12732
12733   /* Parse the initializer.  */
12734   initializer = NULL_TREE;
12735   is_direct_init = false;
12736   is_non_constant_init = true;
12737   if (is_initialized)
12738     {
12739       if (function_declarator_p (declarator))
12740         {
12741           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12742            if (initialization_kind == CPP_EQ)
12743              initializer = cp_parser_pure_specifier (parser);
12744            else
12745              {
12746                /* If the declaration was erroneous, we don't really
12747                   know what the user intended, so just silently
12748                   consume the initializer.  */
12749                if (decl != error_mark_node)
12750                  error ("%Hinitializer provided for function",
12751                         &initializer_start_token->location);
12752                cp_parser_skip_to_closing_parenthesis (parser,
12753                                                       /*recovering=*/true,
12754                                                       /*or_comma=*/false,
12755                                                       /*consume_paren=*/true);
12756              }
12757         }
12758       else
12759         initializer = cp_parser_initializer (parser,
12760                                              &is_direct_init,
12761                                              &is_non_constant_init);
12762     }
12763
12764   /* The old parser allows attributes to appear after a parenthesized
12765      initializer.  Mark Mitchell proposed removing this functionality
12766      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12767      attributes -- but ignores them.  */
12768   if (cp_parser_allow_gnu_extensions_p (parser)
12769       && initialization_kind == CPP_OPEN_PAREN)
12770     if (cp_parser_attributes_opt (parser))
12771       warning (OPT_Wattributes,
12772                "attributes after parenthesized initializer ignored");
12773
12774   /* For an in-class declaration, use `grokfield' to create the
12775      declaration.  */
12776   if (member_p)
12777     {
12778       if (pushed_scope)
12779         {
12780           pop_scope (pushed_scope);
12781           pushed_scope = false;
12782         }
12783       decl = grokfield (declarator, decl_specifiers,
12784                         initializer, !is_non_constant_init,
12785                         /*asmspec=*/NULL_TREE,
12786                         prefix_attributes);
12787       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12788         cp_parser_save_default_args (parser, decl);
12789     }
12790
12791   /* Finish processing the declaration.  But, skip friend
12792      declarations.  */
12793   if (!friend_p && decl && decl != error_mark_node)
12794     {
12795       cp_finish_decl (decl,
12796                       initializer, !is_non_constant_init,
12797                       asm_specification,
12798                       /* If the initializer is in parentheses, then this is
12799                          a direct-initialization, which means that an
12800                          `explicit' constructor is OK.  Otherwise, an
12801                          `explicit' constructor cannot be used.  */
12802                       ((is_direct_init || !is_initialized)
12803                        ? 0 : LOOKUP_ONLYCONVERTING));
12804     }
12805   else if ((cxx_dialect != cxx98) && friend_p
12806            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12807     /* Core issue #226 (C++0x only): A default template-argument
12808        shall not be specified in a friend class template
12809        declaration. */
12810     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12811                              /*is_partial=*/0, /*is_friend_decl=*/1);
12812
12813   if (!friend_p && pushed_scope)
12814     pop_scope (pushed_scope);
12815
12816   return decl;
12817 }
12818
12819 /* Parse a declarator.
12820
12821    declarator:
12822      direct-declarator
12823      ptr-operator declarator
12824
12825    abstract-declarator:
12826      ptr-operator abstract-declarator [opt]
12827      direct-abstract-declarator
12828
12829    GNU Extensions:
12830
12831    declarator:
12832      attributes [opt] direct-declarator
12833      attributes [opt] ptr-operator declarator
12834
12835    abstract-declarator:
12836      attributes [opt] ptr-operator abstract-declarator [opt]
12837      attributes [opt] direct-abstract-declarator
12838
12839    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12840    detect constructor, destructor or conversion operators. It is set
12841    to -1 if the declarator is a name, and +1 if it is a
12842    function. Otherwise it is set to zero. Usually you just want to
12843    test for >0, but internally the negative value is used.
12844
12845    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12846    a decl-specifier-seq unless it declares a constructor, destructor,
12847    or conversion.  It might seem that we could check this condition in
12848    semantic analysis, rather than parsing, but that makes it difficult
12849    to handle something like `f()'.  We want to notice that there are
12850    no decl-specifiers, and therefore realize that this is an
12851    expression, not a declaration.)
12852
12853    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12854    the declarator is a direct-declarator of the form "(...)".
12855
12856    MEMBER_P is true iff this declarator is a member-declarator.  */
12857
12858 static cp_declarator *
12859 cp_parser_declarator (cp_parser* parser,
12860                       cp_parser_declarator_kind dcl_kind,
12861                       int* ctor_dtor_or_conv_p,
12862                       bool* parenthesized_p,
12863                       bool member_p)
12864 {
12865   cp_token *token;
12866   cp_declarator *declarator;
12867   enum tree_code code;
12868   cp_cv_quals cv_quals;
12869   tree class_type;
12870   tree attributes = NULL_TREE;
12871
12872   /* Assume this is not a constructor, destructor, or type-conversion
12873      operator.  */
12874   if (ctor_dtor_or_conv_p)
12875     *ctor_dtor_or_conv_p = 0;
12876
12877   if (cp_parser_allow_gnu_extensions_p (parser))
12878     attributes = cp_parser_attributes_opt (parser);
12879
12880   /* Peek at the next token.  */
12881   token = cp_lexer_peek_token (parser->lexer);
12882
12883   /* Check for the ptr-operator production.  */
12884   cp_parser_parse_tentatively (parser);
12885   /* Parse the ptr-operator.  */
12886   code = cp_parser_ptr_operator (parser,
12887                                  &class_type,
12888                                  &cv_quals);
12889   /* If that worked, then we have a ptr-operator.  */
12890   if (cp_parser_parse_definitely (parser))
12891     {
12892       /* If a ptr-operator was found, then this declarator was not
12893          parenthesized.  */
12894       if (parenthesized_p)
12895         *parenthesized_p = true;
12896       /* The dependent declarator is optional if we are parsing an
12897          abstract-declarator.  */
12898       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12899         cp_parser_parse_tentatively (parser);
12900
12901       /* Parse the dependent declarator.  */
12902       declarator = cp_parser_declarator (parser, dcl_kind,
12903                                          /*ctor_dtor_or_conv_p=*/NULL,
12904                                          /*parenthesized_p=*/NULL,
12905                                          /*member_p=*/false);
12906
12907       /* If we are parsing an abstract-declarator, we must handle the
12908          case where the dependent declarator is absent.  */
12909       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12910           && !cp_parser_parse_definitely (parser))
12911         declarator = NULL;
12912
12913       declarator = cp_parser_make_indirect_declarator
12914         (code, class_type, cv_quals, declarator);
12915     }
12916   /* Everything else is a direct-declarator.  */
12917   else
12918     {
12919       if (parenthesized_p)
12920         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12921                                                    CPP_OPEN_PAREN);
12922       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12923                                                 ctor_dtor_or_conv_p,
12924                                                 member_p);
12925     }
12926
12927   if (attributes && declarator && declarator != cp_error_declarator)
12928     declarator->attributes = attributes;
12929
12930   return declarator;
12931 }
12932
12933 /* Parse a direct-declarator or direct-abstract-declarator.
12934
12935    direct-declarator:
12936      declarator-id
12937      direct-declarator ( parameter-declaration-clause )
12938        cv-qualifier-seq [opt]
12939        exception-specification [opt]
12940      direct-declarator [ constant-expression [opt] ]
12941      ( declarator )
12942
12943    direct-abstract-declarator:
12944      direct-abstract-declarator [opt]
12945        ( parameter-declaration-clause )
12946        cv-qualifier-seq [opt]
12947        exception-specification [opt]
12948      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12949      ( abstract-declarator )
12950
12951    Returns a representation of the declarator.  DCL_KIND is
12952    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12953    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12954    we are parsing a direct-declarator.  It is
12955    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12956    of ambiguity we prefer an abstract declarator, as per
12957    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12958    cp_parser_declarator.  */
12959
12960 static cp_declarator *
12961 cp_parser_direct_declarator (cp_parser* parser,
12962                              cp_parser_declarator_kind dcl_kind,
12963                              int* ctor_dtor_or_conv_p,
12964                              bool member_p)
12965 {
12966   cp_token *token;
12967   cp_declarator *declarator = NULL;
12968   tree scope = NULL_TREE;
12969   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12970   bool saved_in_declarator_p = parser->in_declarator_p;
12971   bool first = true;
12972   tree pushed_scope = NULL_TREE;
12973
12974   while (true)
12975     {
12976       /* Peek at the next token.  */
12977       token = cp_lexer_peek_token (parser->lexer);
12978       if (token->type == CPP_OPEN_PAREN)
12979         {
12980           /* This is either a parameter-declaration-clause, or a
12981              parenthesized declarator. When we know we are parsing a
12982              named declarator, it must be a parenthesized declarator
12983              if FIRST is true. For instance, `(int)' is a
12984              parameter-declaration-clause, with an omitted
12985              direct-abstract-declarator. But `((*))', is a
12986              parenthesized abstract declarator. Finally, when T is a
12987              template parameter `(T)' is a
12988              parameter-declaration-clause, and not a parenthesized
12989              named declarator.
12990
12991              We first try and parse a parameter-declaration-clause,
12992              and then try a nested declarator (if FIRST is true).
12993
12994              It is not an error for it not to be a
12995              parameter-declaration-clause, even when FIRST is
12996              false. Consider,
12997
12998                int i (int);
12999                int i (3);
13000
13001              The first is the declaration of a function while the
13002              second is the definition of a variable, including its
13003              initializer.
13004
13005              Having seen only the parenthesis, we cannot know which of
13006              these two alternatives should be selected.  Even more
13007              complex are examples like:
13008
13009                int i (int (a));
13010                int i (int (3));
13011
13012              The former is a function-declaration; the latter is a
13013              variable initialization.
13014
13015              Thus again, we try a parameter-declaration-clause, and if
13016              that fails, we back out and return.  */
13017
13018           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13019             {
13020               tree params;
13021               unsigned saved_num_template_parameter_lists;
13022               bool is_declarator = false;
13023               tree t;
13024
13025               /* In a member-declarator, the only valid interpretation
13026                  of a parenthesis is the start of a
13027                  parameter-declaration-clause.  (It is invalid to
13028                  initialize a static data member with a parenthesized
13029                  initializer; only the "=" form of initialization is
13030                  permitted.)  */
13031               if (!member_p)
13032                 cp_parser_parse_tentatively (parser);
13033
13034               /* Consume the `('.  */
13035               cp_lexer_consume_token (parser->lexer);
13036               if (first)
13037                 {
13038                   /* If this is going to be an abstract declarator, we're
13039                      in a declarator and we can't have default args.  */
13040                   parser->default_arg_ok_p = false;
13041                   parser->in_declarator_p = true;
13042                 }
13043
13044               /* Inside the function parameter list, surrounding
13045                  template-parameter-lists do not apply.  */
13046               saved_num_template_parameter_lists
13047                 = parser->num_template_parameter_lists;
13048               parser->num_template_parameter_lists = 0;
13049
13050               begin_scope (sk_function_parms, NULL_TREE);
13051
13052               /* Parse the parameter-declaration-clause.  */
13053               params = cp_parser_parameter_declaration_clause (parser);
13054
13055               parser->num_template_parameter_lists
13056                 = saved_num_template_parameter_lists;
13057
13058               /* If all went well, parse the cv-qualifier-seq and the
13059                  exception-specification.  */
13060               if (member_p || cp_parser_parse_definitely (parser))
13061                 {
13062                   cp_cv_quals cv_quals;
13063                   tree exception_specification;
13064                   tree late_return;
13065
13066                   is_declarator = true;
13067
13068                   if (ctor_dtor_or_conv_p)
13069                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13070                   first = false;
13071                   /* Consume the `)'.  */
13072                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13073
13074                   /* Parse the cv-qualifier-seq.  */
13075                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13076                   /* And the exception-specification.  */
13077                   exception_specification
13078                     = cp_parser_exception_specification_opt (parser);
13079
13080                   late_return
13081                     = cp_parser_late_return_type_opt (parser);
13082
13083                   /* Create the function-declarator.  */
13084                   declarator = make_call_declarator (declarator,
13085                                                      params,
13086                                                      cv_quals,
13087                                                      exception_specification,
13088                                                      late_return);
13089                   /* Any subsequent parameter lists are to do with
13090                      return type, so are not those of the declared
13091                      function.  */
13092                   parser->default_arg_ok_p = false;
13093                 }
13094
13095               /* Remove the function parms from scope.  */
13096               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13097                 pop_binding (DECL_NAME (t), t);
13098               leave_scope();
13099
13100               if (is_declarator)
13101                 /* Repeat the main loop.  */
13102                 continue;
13103             }
13104
13105           /* If this is the first, we can try a parenthesized
13106              declarator.  */
13107           if (first)
13108             {
13109               bool saved_in_type_id_in_expr_p;
13110
13111               parser->default_arg_ok_p = saved_default_arg_ok_p;
13112               parser->in_declarator_p = saved_in_declarator_p;
13113
13114               /* Consume the `('.  */
13115               cp_lexer_consume_token (parser->lexer);
13116               /* Parse the nested declarator.  */
13117               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13118               parser->in_type_id_in_expr_p = true;
13119               declarator
13120                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13121                                         /*parenthesized_p=*/NULL,
13122                                         member_p);
13123               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13124               first = false;
13125               /* Expect a `)'.  */
13126               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13127                 declarator = cp_error_declarator;
13128               if (declarator == cp_error_declarator)
13129                 break;
13130
13131               goto handle_declarator;
13132             }
13133           /* Otherwise, we must be done.  */
13134           else
13135             break;
13136         }
13137       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13138                && token->type == CPP_OPEN_SQUARE)
13139         {
13140           /* Parse an array-declarator.  */
13141           tree bounds;
13142
13143           if (ctor_dtor_or_conv_p)
13144             *ctor_dtor_or_conv_p = 0;
13145
13146           first = false;
13147           parser->default_arg_ok_p = false;
13148           parser->in_declarator_p = true;
13149           /* Consume the `['.  */
13150           cp_lexer_consume_token (parser->lexer);
13151           /* Peek at the next token.  */
13152           token = cp_lexer_peek_token (parser->lexer);
13153           /* If the next token is `]', then there is no
13154              constant-expression.  */
13155           if (token->type != CPP_CLOSE_SQUARE)
13156             {
13157               bool non_constant_p;
13158
13159               bounds
13160                 = cp_parser_constant_expression (parser,
13161                                                  /*allow_non_constant=*/true,
13162                                                  &non_constant_p);
13163               if (!non_constant_p)
13164                 bounds = fold_non_dependent_expr (bounds);
13165               /* Normally, the array bound must be an integral constant
13166                  expression.  However, as an extension, we allow VLAs
13167                  in function scopes.  */
13168               else if (!parser->in_function_body)
13169                 {
13170                   error ("%Harray bound is not an integer constant",
13171                          &token->location);
13172                   bounds = error_mark_node;
13173                 }
13174             }
13175           else
13176             bounds = NULL_TREE;
13177           /* Look for the closing `]'.  */
13178           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13179             {
13180               declarator = cp_error_declarator;
13181               break;
13182             }
13183
13184           declarator = make_array_declarator (declarator, bounds);
13185         }
13186       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13187         {
13188           tree qualifying_scope;
13189           tree unqualified_name;
13190           special_function_kind sfk;
13191           bool abstract_ok;
13192           bool pack_expansion_p = false;
13193           cp_token *declarator_id_start_token;
13194
13195           /* Parse a declarator-id */
13196           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13197           if (abstract_ok)
13198             {
13199               cp_parser_parse_tentatively (parser);
13200
13201               /* If we see an ellipsis, we should be looking at a
13202                  parameter pack. */
13203               if (token->type == CPP_ELLIPSIS)
13204                 {
13205                   /* Consume the `...' */
13206                   cp_lexer_consume_token (parser->lexer);
13207
13208                   pack_expansion_p = true;
13209                 }
13210             }
13211
13212           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13213           unqualified_name
13214             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13215           qualifying_scope = parser->scope;
13216           if (abstract_ok)
13217             {
13218               bool okay = false;
13219
13220               if (!unqualified_name && pack_expansion_p)
13221                 {
13222                   /* Check whether an error occurred. */
13223                   okay = !cp_parser_error_occurred (parser);
13224
13225                   /* We already consumed the ellipsis to mark a
13226                      parameter pack, but we have no way to report it,
13227                      so abort the tentative parse. We will be exiting
13228                      immediately anyway. */
13229                   cp_parser_abort_tentative_parse (parser);
13230                 }
13231               else
13232                 okay = cp_parser_parse_definitely (parser);
13233
13234               if (!okay)
13235                 unqualified_name = error_mark_node;
13236               else if (unqualified_name
13237                        && (qualifying_scope
13238                            || (TREE_CODE (unqualified_name)
13239                                != IDENTIFIER_NODE)))
13240                 {
13241                   cp_parser_error (parser, "expected unqualified-id");
13242                   unqualified_name = error_mark_node;
13243                 }
13244             }
13245
13246           if (!unqualified_name)
13247             return NULL;
13248           if (unqualified_name == error_mark_node)
13249             {
13250               declarator = cp_error_declarator;
13251               pack_expansion_p = false;
13252               declarator->parameter_pack_p = false;
13253               break;
13254             }
13255
13256           if (qualifying_scope && at_namespace_scope_p ()
13257               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13258             {
13259               /* In the declaration of a member of a template class
13260                  outside of the class itself, the SCOPE will sometimes
13261                  be a TYPENAME_TYPE.  For example, given:
13262
13263                  template <typename T>
13264                  int S<T>::R::i = 3;
13265
13266                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13267                  this context, we must resolve S<T>::R to an ordinary
13268                  type, rather than a typename type.
13269
13270                  The reason we normally avoid resolving TYPENAME_TYPEs
13271                  is that a specialization of `S' might render
13272                  `S<T>::R' not a type.  However, if `S' is
13273                  specialized, then this `i' will not be used, so there
13274                  is no harm in resolving the types here.  */
13275               tree type;
13276
13277               /* Resolve the TYPENAME_TYPE.  */
13278               type = resolve_typename_type (qualifying_scope,
13279                                             /*only_current_p=*/false);
13280               /* If that failed, the declarator is invalid.  */
13281               if (TREE_CODE (type) == TYPENAME_TYPE)
13282                 error ("%H%<%T::%E%> is not a type",
13283                        &declarator_id_start_token->location,
13284                        TYPE_CONTEXT (qualifying_scope),
13285                        TYPE_IDENTIFIER (qualifying_scope));
13286               qualifying_scope = type;
13287             }
13288
13289           sfk = sfk_none;
13290
13291           if (unqualified_name)
13292             {
13293               tree class_type;
13294
13295               if (qualifying_scope
13296                   && CLASS_TYPE_P (qualifying_scope))
13297                 class_type = qualifying_scope;
13298               else
13299                 class_type = current_class_type;
13300
13301               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13302                 {
13303                   tree name_type = TREE_TYPE (unqualified_name);
13304                   if (class_type && same_type_p (name_type, class_type))
13305                     {
13306                       if (qualifying_scope
13307                           && CLASSTYPE_USE_TEMPLATE (name_type))
13308                         {
13309                           error ("%Hinvalid use of constructor as a template",
13310                                  &declarator_id_start_token->location);
13311                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13312                                   "name the constructor in a qualified name",
13313                                   class_type,
13314                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13315                                   class_type, name_type);
13316                           declarator = cp_error_declarator;
13317                           break;
13318                         }
13319                       else
13320                         unqualified_name = constructor_name (class_type);
13321                     }
13322                   else
13323                     {
13324                       /* We do not attempt to print the declarator
13325                          here because we do not have enough
13326                          information about its original syntactic
13327                          form.  */
13328                       cp_parser_error (parser, "invalid declarator");
13329                       declarator = cp_error_declarator;
13330                       break;
13331                     }
13332                 }
13333
13334               if (class_type)
13335                 {
13336                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13337                     sfk = sfk_destructor;
13338                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13339                     sfk = sfk_conversion;
13340                   else if (/* There's no way to declare a constructor
13341                               for an anonymous type, even if the type
13342                               got a name for linkage purposes.  */
13343                            !TYPE_WAS_ANONYMOUS (class_type)
13344                            && constructor_name_p (unqualified_name,
13345                                                   class_type))
13346                     {
13347                       unqualified_name = constructor_name (class_type);
13348                       sfk = sfk_constructor;
13349                     }
13350
13351                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13352                     *ctor_dtor_or_conv_p = -1;
13353                 }
13354             }
13355           declarator = make_id_declarator (qualifying_scope,
13356                                            unqualified_name,
13357                                            sfk);
13358           declarator->id_loc = token->location;
13359           declarator->parameter_pack_p = pack_expansion_p;
13360
13361           if (pack_expansion_p)
13362             maybe_warn_variadic_templates ();
13363
13364         handle_declarator:;
13365           scope = get_scope_of_declarator (declarator);
13366           if (scope)
13367             /* Any names that appear after the declarator-id for a
13368                member are looked up in the containing scope.  */
13369             pushed_scope = push_scope (scope);
13370           parser->in_declarator_p = true;
13371           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13372               || (declarator && declarator->kind == cdk_id))
13373             /* Default args are only allowed on function
13374                declarations.  */
13375             parser->default_arg_ok_p = saved_default_arg_ok_p;
13376           else
13377             parser->default_arg_ok_p = false;
13378
13379           first = false;
13380         }
13381       /* We're done.  */
13382       else
13383         break;
13384     }
13385
13386   /* For an abstract declarator, we might wind up with nothing at this
13387      point.  That's an error; the declarator is not optional.  */
13388   if (!declarator)
13389     cp_parser_error (parser, "expected declarator");
13390
13391   /* If we entered a scope, we must exit it now.  */
13392   if (pushed_scope)
13393     pop_scope (pushed_scope);
13394
13395   parser->default_arg_ok_p = saved_default_arg_ok_p;
13396   parser->in_declarator_p = saved_in_declarator_p;
13397
13398   return declarator;
13399 }
13400
13401 /* Parse a ptr-operator.
13402
13403    ptr-operator:
13404      * cv-qualifier-seq [opt]
13405      &
13406      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13407
13408    GNU Extension:
13409
13410    ptr-operator:
13411      & cv-qualifier-seq [opt]
13412
13413    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13414    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13415    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13416    filled in with the TYPE containing the member.  *CV_QUALS is
13417    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13418    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13419    Note that the tree codes returned by this function have nothing
13420    to do with the types of trees that will be eventually be created
13421    to represent the pointer or reference type being parsed. They are
13422    just constants with suggestive names. */
13423 static enum tree_code
13424 cp_parser_ptr_operator (cp_parser* parser,
13425                         tree* type,
13426                         cp_cv_quals *cv_quals)
13427 {
13428   enum tree_code code = ERROR_MARK;
13429   cp_token *token;
13430
13431   /* Assume that it's not a pointer-to-member.  */
13432   *type = NULL_TREE;
13433   /* And that there are no cv-qualifiers.  */
13434   *cv_quals = TYPE_UNQUALIFIED;
13435
13436   /* Peek at the next token.  */
13437   token = cp_lexer_peek_token (parser->lexer);
13438
13439   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13440   if (token->type == CPP_MULT)
13441     code = INDIRECT_REF;
13442   else if (token->type == CPP_AND)
13443     code = ADDR_EXPR;
13444   else if ((cxx_dialect != cxx98) &&
13445            token->type == CPP_AND_AND) /* C++0x only */
13446     code = NON_LVALUE_EXPR;
13447
13448   if (code != ERROR_MARK)
13449     {
13450       /* Consume the `*', `&' or `&&'.  */
13451       cp_lexer_consume_token (parser->lexer);
13452
13453       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13454          `&', if we are allowing GNU extensions.  (The only qualifier
13455          that can legally appear after `&' is `restrict', but that is
13456          enforced during semantic analysis.  */
13457       if (code == INDIRECT_REF
13458           || cp_parser_allow_gnu_extensions_p (parser))
13459         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13460     }
13461   else
13462     {
13463       /* Try the pointer-to-member case.  */
13464       cp_parser_parse_tentatively (parser);
13465       /* Look for the optional `::' operator.  */
13466       cp_parser_global_scope_opt (parser,
13467                                   /*current_scope_valid_p=*/false);
13468       /* Look for the nested-name specifier.  */
13469       token = cp_lexer_peek_token (parser->lexer);
13470       cp_parser_nested_name_specifier (parser,
13471                                        /*typename_keyword_p=*/false,
13472                                        /*check_dependency_p=*/true,
13473                                        /*type_p=*/false,
13474                                        /*is_declaration=*/false);
13475       /* If we found it, and the next token is a `*', then we are
13476          indeed looking at a pointer-to-member operator.  */
13477       if (!cp_parser_error_occurred (parser)
13478           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13479         {
13480           /* Indicate that the `*' operator was used.  */
13481           code = INDIRECT_REF;
13482
13483           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13484             error ("%H%qD is a namespace", &token->location, parser->scope);
13485           else
13486             {
13487               /* The type of which the member is a member is given by the
13488                  current SCOPE.  */
13489               *type = parser->scope;
13490               /* The next name will not be qualified.  */
13491               parser->scope = NULL_TREE;
13492               parser->qualifying_scope = NULL_TREE;
13493               parser->object_scope = NULL_TREE;
13494               /* Look for the optional cv-qualifier-seq.  */
13495               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13496             }
13497         }
13498       /* If that didn't work we don't have a ptr-operator.  */
13499       if (!cp_parser_parse_definitely (parser))
13500         cp_parser_error (parser, "expected ptr-operator");
13501     }
13502
13503   return code;
13504 }
13505
13506 /* Parse an (optional) cv-qualifier-seq.
13507
13508    cv-qualifier-seq:
13509      cv-qualifier cv-qualifier-seq [opt]
13510
13511    cv-qualifier:
13512      const
13513      volatile
13514
13515    GNU Extension:
13516
13517    cv-qualifier:
13518      __restrict__
13519
13520    Returns a bitmask representing the cv-qualifiers.  */
13521
13522 static cp_cv_quals
13523 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13524 {
13525   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13526
13527   while (true)
13528     {
13529       cp_token *token;
13530       cp_cv_quals cv_qualifier;
13531
13532       /* Peek at the next token.  */
13533       token = cp_lexer_peek_token (parser->lexer);
13534       /* See if it's a cv-qualifier.  */
13535       switch (token->keyword)
13536         {
13537         case RID_CONST:
13538           cv_qualifier = TYPE_QUAL_CONST;
13539           break;
13540
13541         case RID_VOLATILE:
13542           cv_qualifier = TYPE_QUAL_VOLATILE;
13543           break;
13544
13545         case RID_RESTRICT:
13546           cv_qualifier = TYPE_QUAL_RESTRICT;
13547           break;
13548
13549         default:
13550           cv_qualifier = TYPE_UNQUALIFIED;
13551           break;
13552         }
13553
13554       if (!cv_qualifier)
13555         break;
13556
13557       if (cv_quals & cv_qualifier)
13558         {
13559           error ("%Hduplicate cv-qualifier", &token->location);
13560           cp_lexer_purge_token (parser->lexer);
13561         }
13562       else
13563         {
13564           cp_lexer_consume_token (parser->lexer);
13565           cv_quals |= cv_qualifier;
13566         }
13567     }
13568
13569   return cv_quals;
13570 }
13571
13572 /* Parse a late-specified return type, if any.  This is not a separate
13573    non-terminal, but part of a function declarator, which looks like
13574
13575    -> type-id
13576
13577    Returns the type indicated by the type-id.  */
13578
13579 static tree
13580 cp_parser_late_return_type_opt (cp_parser* parser)
13581 {
13582   cp_token *token;
13583
13584   /* Peek at the next token.  */
13585   token = cp_lexer_peek_token (parser->lexer);
13586   /* A late-specified return type is indicated by an initial '->'. */
13587   if (token->type != CPP_DEREF)
13588     return NULL_TREE;
13589
13590   /* Consume the ->.  */
13591   cp_lexer_consume_token (parser->lexer);
13592
13593   return cp_parser_type_id (parser);
13594 }
13595
13596 /* Parse a declarator-id.
13597
13598    declarator-id:
13599      id-expression
13600      :: [opt] nested-name-specifier [opt] type-name
13601
13602    In the `id-expression' case, the value returned is as for
13603    cp_parser_id_expression if the id-expression was an unqualified-id.
13604    If the id-expression was a qualified-id, then a SCOPE_REF is
13605    returned.  The first operand is the scope (either a NAMESPACE_DECL
13606    or TREE_TYPE), but the second is still just a representation of an
13607    unqualified-id.  */
13608
13609 static tree
13610 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13611 {
13612   tree id;
13613   /* The expression must be an id-expression.  Assume that qualified
13614      names are the names of types so that:
13615
13616        template <class T>
13617        int S<T>::R::i = 3;
13618
13619      will work; we must treat `S<T>::R' as the name of a type.
13620      Similarly, assume that qualified names are templates, where
13621      required, so that:
13622
13623        template <class T>
13624        int S<T>::R<T>::i = 3;
13625
13626      will work, too.  */
13627   id = cp_parser_id_expression (parser,
13628                                 /*template_keyword_p=*/false,
13629                                 /*check_dependency_p=*/false,
13630                                 /*template_p=*/NULL,
13631                                 /*declarator_p=*/true,
13632                                 optional_p);
13633   if (id && BASELINK_P (id))
13634     id = BASELINK_FUNCTIONS (id);
13635   return id;
13636 }
13637
13638 /* Parse a type-id.
13639
13640    type-id:
13641      type-specifier-seq abstract-declarator [opt]
13642
13643    Returns the TYPE specified.  */
13644
13645 static tree
13646 cp_parser_type_id (cp_parser* parser)
13647 {
13648   cp_decl_specifier_seq type_specifier_seq;
13649   cp_declarator *abstract_declarator;
13650
13651   /* Parse the type-specifier-seq.  */
13652   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13653                                 &type_specifier_seq);
13654   if (type_specifier_seq.type == error_mark_node)
13655     return error_mark_node;
13656
13657   /* There might or might not be an abstract declarator.  */
13658   cp_parser_parse_tentatively (parser);
13659   /* Look for the declarator.  */
13660   abstract_declarator
13661     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13662                             /*parenthesized_p=*/NULL,
13663                             /*member_p=*/false);
13664   /* Check to see if there really was a declarator.  */
13665   if (!cp_parser_parse_definitely (parser))
13666     abstract_declarator = NULL;
13667
13668   return groktypename (&type_specifier_seq, abstract_declarator);
13669 }
13670
13671 /* Parse a type-specifier-seq.
13672
13673    type-specifier-seq:
13674      type-specifier type-specifier-seq [opt]
13675
13676    GNU extension:
13677
13678    type-specifier-seq:
13679      attributes type-specifier-seq [opt]
13680
13681    If IS_CONDITION is true, we are at the start of a "condition",
13682    e.g., we've just seen "if (".
13683
13684    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13685
13686 static void
13687 cp_parser_type_specifier_seq (cp_parser* parser,
13688                               bool is_condition,
13689                               cp_decl_specifier_seq *type_specifier_seq)
13690 {
13691   bool seen_type_specifier = false;
13692   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13693   cp_token *start_token = NULL;
13694
13695   /* Clear the TYPE_SPECIFIER_SEQ.  */
13696   clear_decl_specs (type_specifier_seq);
13697
13698   /* Parse the type-specifiers and attributes.  */
13699   while (true)
13700     {
13701       tree type_specifier;
13702       bool is_cv_qualifier;
13703
13704       /* Check for attributes first.  */
13705       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13706         {
13707           type_specifier_seq->attributes =
13708             chainon (type_specifier_seq->attributes,
13709                      cp_parser_attributes_opt (parser));
13710           continue;
13711         }
13712
13713       /* record the token of the beginning of the type specifier seq,
13714          for error reporting purposes*/
13715      if (!start_token)
13716        start_token = cp_lexer_peek_token (parser->lexer);
13717
13718       /* Look for the type-specifier.  */
13719       type_specifier = cp_parser_type_specifier (parser,
13720                                                  flags,
13721                                                  type_specifier_seq,
13722                                                  /*is_declaration=*/false,
13723                                                  NULL,
13724                                                  &is_cv_qualifier);
13725       if (!type_specifier)
13726         {
13727           /* If the first type-specifier could not be found, this is not a
13728              type-specifier-seq at all.  */
13729           if (!seen_type_specifier)
13730             {
13731               cp_parser_error (parser, "expected type-specifier");
13732               type_specifier_seq->type = error_mark_node;
13733               return;
13734             }
13735           /* If subsequent type-specifiers could not be found, the
13736              type-specifier-seq is complete.  */
13737           break;
13738         }
13739
13740       seen_type_specifier = true;
13741       /* The standard says that a condition can be:
13742
13743             type-specifier-seq declarator = assignment-expression
13744
13745          However, given:
13746
13747            struct S {};
13748            if (int S = ...)
13749
13750          we should treat the "S" as a declarator, not as a
13751          type-specifier.  The standard doesn't say that explicitly for
13752          type-specifier-seq, but it does say that for
13753          decl-specifier-seq in an ordinary declaration.  Perhaps it
13754          would be clearer just to allow a decl-specifier-seq here, and
13755          then add a semantic restriction that if any decl-specifiers
13756          that are not type-specifiers appear, the program is invalid.  */
13757       if (is_condition && !is_cv_qualifier)
13758         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13759     }
13760
13761   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13762 }
13763
13764 /* Parse a parameter-declaration-clause.
13765
13766    parameter-declaration-clause:
13767      parameter-declaration-list [opt] ... [opt]
13768      parameter-declaration-list , ...
13769
13770    Returns a representation for the parameter declarations.  A return
13771    value of NULL indicates a parameter-declaration-clause consisting
13772    only of an ellipsis.  */
13773
13774 static tree
13775 cp_parser_parameter_declaration_clause (cp_parser* parser)
13776 {
13777   tree parameters;
13778   cp_token *token;
13779   bool ellipsis_p;
13780   bool is_error;
13781
13782   /* Peek at the next token.  */
13783   token = cp_lexer_peek_token (parser->lexer);
13784   /* Check for trivial parameter-declaration-clauses.  */
13785   if (token->type == CPP_ELLIPSIS)
13786     {
13787       /* Consume the `...' token.  */
13788       cp_lexer_consume_token (parser->lexer);
13789       return NULL_TREE;
13790     }
13791   else if (token->type == CPP_CLOSE_PAREN)
13792     /* There are no parameters.  */
13793     {
13794 #ifndef NO_IMPLICIT_EXTERN_C
13795       if (in_system_header && current_class_type == NULL
13796           && current_lang_name == lang_name_c)
13797         return NULL_TREE;
13798       else
13799 #endif
13800         return void_list_node;
13801     }
13802   /* Check for `(void)', too, which is a special case.  */
13803   else if (token->keyword == RID_VOID
13804            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13805                == CPP_CLOSE_PAREN))
13806     {
13807       /* Consume the `void' token.  */
13808       cp_lexer_consume_token (parser->lexer);
13809       /* There are no parameters.  */
13810       return void_list_node;
13811     }
13812
13813   /* Parse the parameter-declaration-list.  */
13814   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13815   /* If a parse error occurred while parsing the
13816      parameter-declaration-list, then the entire
13817      parameter-declaration-clause is erroneous.  */
13818   if (is_error)
13819     return NULL;
13820
13821   /* Peek at the next token.  */
13822   token = cp_lexer_peek_token (parser->lexer);
13823   /* If it's a `,', the clause should terminate with an ellipsis.  */
13824   if (token->type == CPP_COMMA)
13825     {
13826       /* Consume the `,'.  */
13827       cp_lexer_consume_token (parser->lexer);
13828       /* Expect an ellipsis.  */
13829       ellipsis_p
13830         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13831     }
13832   /* It might also be `...' if the optional trailing `,' was
13833      omitted.  */
13834   else if (token->type == CPP_ELLIPSIS)
13835     {
13836       /* Consume the `...' token.  */
13837       cp_lexer_consume_token (parser->lexer);
13838       /* And remember that we saw it.  */
13839       ellipsis_p = true;
13840     }
13841   else
13842     ellipsis_p = false;
13843
13844   /* Finish the parameter list.  */
13845   if (!ellipsis_p)
13846     parameters = chainon (parameters, void_list_node);
13847
13848   return parameters;
13849 }
13850
13851 /* Parse a parameter-declaration-list.
13852
13853    parameter-declaration-list:
13854      parameter-declaration
13855      parameter-declaration-list , parameter-declaration
13856
13857    Returns a representation of the parameter-declaration-list, as for
13858    cp_parser_parameter_declaration_clause.  However, the
13859    `void_list_node' is never appended to the list.  Upon return,
13860    *IS_ERROR will be true iff an error occurred.  */
13861
13862 static tree
13863 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13864 {
13865   tree parameters = NULL_TREE;
13866   tree *tail = &parameters; 
13867   bool saved_in_unbraced_linkage_specification_p;
13868
13869   /* Assume all will go well.  */
13870   *is_error = false;
13871   /* The special considerations that apply to a function within an
13872      unbraced linkage specifications do not apply to the parameters
13873      to the function.  */
13874   saved_in_unbraced_linkage_specification_p 
13875     = parser->in_unbraced_linkage_specification_p;
13876   parser->in_unbraced_linkage_specification_p = false;
13877
13878   /* Look for more parameters.  */
13879   while (true)
13880     {
13881       cp_parameter_declarator *parameter;
13882       tree decl = error_mark_node;
13883       bool parenthesized_p;
13884       /* Parse the parameter.  */
13885       parameter
13886         = cp_parser_parameter_declaration (parser,
13887                                            /*template_parm_p=*/false,
13888                                            &parenthesized_p);
13889
13890       /* We don't know yet if the enclosing context is deprecated, so wait
13891          and warn in grokparms if appropriate.  */
13892       deprecated_state = DEPRECATED_SUPPRESS;
13893
13894       if (parameter)
13895         decl = grokdeclarator (parameter->declarator,
13896                                &parameter->decl_specifiers,
13897                                PARM,
13898                                parameter->default_argument != NULL_TREE,
13899                                &parameter->decl_specifiers.attributes);
13900
13901       deprecated_state = DEPRECATED_NORMAL;
13902
13903       /* If a parse error occurred parsing the parameter declaration,
13904          then the entire parameter-declaration-list is erroneous.  */
13905       if (decl == error_mark_node)
13906         {
13907           *is_error = true;
13908           parameters = error_mark_node;
13909           break;
13910         }
13911
13912       if (parameter->decl_specifiers.attributes)
13913         cplus_decl_attributes (&decl,
13914                                parameter->decl_specifiers.attributes,
13915                                0);
13916       if (DECL_NAME (decl))
13917         decl = pushdecl (decl);
13918
13919       /* Add the new parameter to the list.  */
13920       *tail = build_tree_list (parameter->default_argument, decl);
13921       tail = &TREE_CHAIN (*tail);
13922
13923       /* Peek at the next token.  */
13924       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13925           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13926           /* These are for Objective-C++ */
13927           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13928           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13929         /* The parameter-declaration-list is complete.  */
13930         break;
13931       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13932         {
13933           cp_token *token;
13934
13935           /* Peek at the next token.  */
13936           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13937           /* If it's an ellipsis, then the list is complete.  */
13938           if (token->type == CPP_ELLIPSIS)
13939             break;
13940           /* Otherwise, there must be more parameters.  Consume the
13941              `,'.  */
13942           cp_lexer_consume_token (parser->lexer);
13943           /* When parsing something like:
13944
13945                 int i(float f, double d)
13946
13947              we can tell after seeing the declaration for "f" that we
13948              are not looking at an initialization of a variable "i",
13949              but rather at the declaration of a function "i".
13950
13951              Due to the fact that the parsing of template arguments
13952              (as specified to a template-id) requires backtracking we
13953              cannot use this technique when inside a template argument
13954              list.  */
13955           if (!parser->in_template_argument_list_p
13956               && !parser->in_type_id_in_expr_p
13957               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13958               /* However, a parameter-declaration of the form
13959                  "foat(f)" (which is a valid declaration of a
13960                  parameter "f") can also be interpreted as an
13961                  expression (the conversion of "f" to "float").  */
13962               && !parenthesized_p)
13963             cp_parser_commit_to_tentative_parse (parser);
13964         }
13965       else
13966         {
13967           cp_parser_error (parser, "expected %<,%> or %<...%>");
13968           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13969             cp_parser_skip_to_closing_parenthesis (parser,
13970                                                    /*recovering=*/true,
13971                                                    /*or_comma=*/false,
13972                                                    /*consume_paren=*/false);
13973           break;
13974         }
13975     }
13976
13977   parser->in_unbraced_linkage_specification_p
13978     = saved_in_unbraced_linkage_specification_p;
13979
13980   return parameters;
13981 }
13982
13983 /* Parse a parameter declaration.
13984
13985    parameter-declaration:
13986      decl-specifier-seq ... [opt] declarator
13987      decl-specifier-seq declarator = assignment-expression
13988      decl-specifier-seq ... [opt] abstract-declarator [opt]
13989      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13990
13991    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13992    declares a template parameter.  (In that case, a non-nested `>'
13993    token encountered during the parsing of the assignment-expression
13994    is not interpreted as a greater-than operator.)
13995
13996    Returns a representation of the parameter, or NULL if an error
13997    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13998    true iff the declarator is of the form "(p)".  */
13999
14000 static cp_parameter_declarator *
14001 cp_parser_parameter_declaration (cp_parser *parser,
14002                                  bool template_parm_p,
14003                                  bool *parenthesized_p)
14004 {
14005   int declares_class_or_enum;
14006   bool greater_than_is_operator_p;
14007   cp_decl_specifier_seq decl_specifiers;
14008   cp_declarator *declarator;
14009   tree default_argument;
14010   cp_token *token = NULL, *declarator_token_start = NULL;
14011   const char *saved_message;
14012
14013   /* In a template parameter, `>' is not an operator.
14014
14015      [temp.param]
14016
14017      When parsing a default template-argument for a non-type
14018      template-parameter, the first non-nested `>' is taken as the end
14019      of the template parameter-list rather than a greater-than
14020      operator.  */
14021   greater_than_is_operator_p = !template_parm_p;
14022
14023   /* Type definitions may not appear in parameter types.  */
14024   saved_message = parser->type_definition_forbidden_message;
14025   parser->type_definition_forbidden_message
14026     = "types may not be defined in parameter types";
14027
14028   /* Parse the declaration-specifiers.  */
14029   cp_parser_decl_specifier_seq (parser,
14030                                 CP_PARSER_FLAGS_NONE,
14031                                 &decl_specifiers,
14032                                 &declares_class_or_enum);
14033   /* If an error occurred, there's no reason to attempt to parse the
14034      rest of the declaration.  */
14035   if (cp_parser_error_occurred (parser))
14036     {
14037       parser->type_definition_forbidden_message = saved_message;
14038       return NULL;
14039     }
14040
14041   /* Peek at the next token.  */
14042   token = cp_lexer_peek_token (parser->lexer);
14043
14044   /* If the next token is a `)', `,', `=', `>', or `...', then there
14045      is no declarator. However, when variadic templates are enabled,
14046      there may be a declarator following `...'.  */
14047   if (token->type == CPP_CLOSE_PAREN
14048       || token->type == CPP_COMMA
14049       || token->type == CPP_EQ
14050       || token->type == CPP_GREATER)
14051     {
14052       declarator = NULL;
14053       if (parenthesized_p)
14054         *parenthesized_p = false;
14055     }
14056   /* Otherwise, there should be a declarator.  */
14057   else
14058     {
14059       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14060       parser->default_arg_ok_p = false;
14061
14062       /* After seeing a decl-specifier-seq, if the next token is not a
14063          "(", there is no possibility that the code is a valid
14064          expression.  Therefore, if parsing tentatively, we commit at
14065          this point.  */
14066       if (!parser->in_template_argument_list_p
14067           /* In an expression context, having seen:
14068
14069                (int((char ...
14070
14071              we cannot be sure whether we are looking at a
14072              function-type (taking a "char" as a parameter) or a cast
14073              of some object of type "char" to "int".  */
14074           && !parser->in_type_id_in_expr_p
14075           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14076           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14077         cp_parser_commit_to_tentative_parse (parser);
14078       /* Parse the declarator.  */
14079       declarator_token_start = token;
14080       declarator = cp_parser_declarator (parser,
14081                                          CP_PARSER_DECLARATOR_EITHER,
14082                                          /*ctor_dtor_or_conv_p=*/NULL,
14083                                          parenthesized_p,
14084                                          /*member_p=*/false);
14085       parser->default_arg_ok_p = saved_default_arg_ok_p;
14086       /* After the declarator, allow more attributes.  */
14087       decl_specifiers.attributes
14088         = chainon (decl_specifiers.attributes,
14089                    cp_parser_attributes_opt (parser));
14090     }
14091
14092   /* If the next token is an ellipsis, and we have not seen a
14093      declarator name, and the type of the declarator contains parameter
14094      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14095      a parameter pack expansion expression. Otherwise, leave the
14096      ellipsis for a C-style variadic function. */
14097   token = cp_lexer_peek_token (parser->lexer);
14098   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14099     {
14100       tree type = decl_specifiers.type;
14101
14102       if (type && DECL_P (type))
14103         type = TREE_TYPE (type);
14104
14105       if (type
14106           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14107           && declarator_can_be_parameter_pack (declarator)
14108           && (!declarator || !declarator->parameter_pack_p)
14109           && uses_parameter_packs (type))
14110         {
14111           /* Consume the `...'. */
14112           cp_lexer_consume_token (parser->lexer);
14113           maybe_warn_variadic_templates ();
14114           
14115           /* Build a pack expansion type */
14116           if (declarator)
14117             declarator->parameter_pack_p = true;
14118           else
14119             decl_specifiers.type = make_pack_expansion (type);
14120         }
14121     }
14122
14123   /* The restriction on defining new types applies only to the type
14124      of the parameter, not to the default argument.  */
14125   parser->type_definition_forbidden_message = saved_message;
14126
14127   /* If the next token is `=', then process a default argument.  */
14128   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14129     {
14130       /* Consume the `='.  */
14131       cp_lexer_consume_token (parser->lexer);
14132
14133       /* If we are defining a class, then the tokens that make up the
14134          default argument must be saved and processed later.  */
14135       if (!template_parm_p && at_class_scope_p ()
14136           && TYPE_BEING_DEFINED (current_class_type))
14137         {
14138           unsigned depth = 0;
14139           int maybe_template_id = 0;
14140           cp_token *first_token;
14141           cp_token *token;
14142
14143           /* Add tokens until we have processed the entire default
14144              argument.  We add the range [first_token, token).  */
14145           first_token = cp_lexer_peek_token (parser->lexer);
14146           while (true)
14147             {
14148               bool done = false;
14149
14150               /* Peek at the next token.  */
14151               token = cp_lexer_peek_token (parser->lexer);
14152               /* What we do depends on what token we have.  */
14153               switch (token->type)
14154                 {
14155                   /* In valid code, a default argument must be
14156                      immediately followed by a `,' `)', or `...'.  */
14157                 case CPP_COMMA:
14158                   if (depth == 0 && maybe_template_id)
14159                     {
14160                       /* If we've seen a '<', we might be in a
14161                          template-argument-list.  Until Core issue 325 is
14162                          resolved, we don't know how this situation ought
14163                          to be handled, so try to DTRT.  We check whether
14164                          what comes after the comma is a valid parameter
14165                          declaration list.  If it is, then the comma ends
14166                          the default argument; otherwise the default
14167                          argument continues.  */
14168                       bool error = false;
14169
14170                       /* Set ITALP so cp_parser_parameter_declaration_list
14171                          doesn't decide to commit to this parse.  */
14172                       bool saved_italp = parser->in_template_argument_list_p;
14173                       parser->in_template_argument_list_p = true;
14174
14175                       cp_parser_parse_tentatively (parser);
14176                       cp_lexer_consume_token (parser->lexer);
14177                       cp_parser_parameter_declaration_list (parser, &error);
14178                       if (!cp_parser_error_occurred (parser) && !error)
14179                         done = true;
14180                       cp_parser_abort_tentative_parse (parser);
14181
14182                       parser->in_template_argument_list_p = saved_italp;
14183                       break;
14184                     }
14185                 case CPP_CLOSE_PAREN:
14186                 case CPP_ELLIPSIS:
14187                   /* If we run into a non-nested `;', `}', or `]',
14188                      then the code is invalid -- but the default
14189                      argument is certainly over.  */
14190                 case CPP_SEMICOLON:
14191                 case CPP_CLOSE_BRACE:
14192                 case CPP_CLOSE_SQUARE:
14193                   if (depth == 0)
14194                     done = true;
14195                   /* Update DEPTH, if necessary.  */
14196                   else if (token->type == CPP_CLOSE_PAREN
14197                            || token->type == CPP_CLOSE_BRACE
14198                            || token->type == CPP_CLOSE_SQUARE)
14199                     --depth;
14200                   break;
14201
14202                 case CPP_OPEN_PAREN:
14203                 case CPP_OPEN_SQUARE:
14204                 case CPP_OPEN_BRACE:
14205                   ++depth;
14206                   break;
14207
14208                 case CPP_LESS:
14209                   if (depth == 0)
14210                     /* This might be the comparison operator, or it might
14211                        start a template argument list.  */
14212                     ++maybe_template_id;
14213                   break;
14214
14215                 case CPP_RSHIFT:
14216                   if (cxx_dialect == cxx98)
14217                     break;
14218                   /* Fall through for C++0x, which treats the `>>'
14219                      operator like two `>' tokens in certain
14220                      cases.  */
14221
14222                 case CPP_GREATER:
14223                   if (depth == 0)
14224                     {
14225                       /* This might be an operator, or it might close a
14226                          template argument list.  But if a previous '<'
14227                          started a template argument list, this will have
14228                          closed it, so we can't be in one anymore.  */
14229                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14230                       if (maybe_template_id < 0)
14231                         maybe_template_id = 0;
14232                     }
14233                   break;
14234
14235                   /* If we run out of tokens, issue an error message.  */
14236                 case CPP_EOF:
14237                 case CPP_PRAGMA_EOL:
14238                   error ("%Hfile ends in default argument", &token->location);
14239                   done = true;
14240                   break;
14241
14242                 case CPP_NAME:
14243                 case CPP_SCOPE:
14244                   /* In these cases, we should look for template-ids.
14245                      For example, if the default argument is
14246                      `X<int, double>()', we need to do name lookup to
14247                      figure out whether or not `X' is a template; if
14248                      so, the `,' does not end the default argument.
14249
14250                      That is not yet done.  */
14251                   break;
14252
14253                 default:
14254                   break;
14255                 }
14256
14257               /* If we've reached the end, stop.  */
14258               if (done)
14259                 break;
14260
14261               /* Add the token to the token block.  */
14262               token = cp_lexer_consume_token (parser->lexer);
14263             }
14264
14265           /* Create a DEFAULT_ARG to represent the unparsed default
14266              argument.  */
14267           default_argument = make_node (DEFAULT_ARG);
14268           DEFARG_TOKENS (default_argument)
14269             = cp_token_cache_new (first_token, token);
14270           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14271         }
14272       /* Outside of a class definition, we can just parse the
14273          assignment-expression.  */
14274       else
14275         {
14276           token = cp_lexer_peek_token (parser->lexer);
14277           default_argument 
14278             = cp_parser_default_argument (parser, template_parm_p);
14279         }
14280
14281       if (!parser->default_arg_ok_p)
14282         {
14283           if (flag_permissive)
14284             warning (0, "deprecated use of default argument for parameter of non-function");
14285           else
14286             {
14287               error ("%Hdefault arguments are only "
14288                      "permitted for function parameters",
14289                      &token->location);
14290               default_argument = NULL_TREE;
14291             }
14292         }
14293       else if ((declarator && declarator->parameter_pack_p)
14294                || (decl_specifiers.type
14295                    && PACK_EXPANSION_P (decl_specifiers.type)))
14296         {
14297           const char* kind = template_parm_p? "template " : "";
14298           
14299           /* Find the name of the parameter pack.  */     
14300           cp_declarator *id_declarator = declarator;
14301           while (id_declarator && id_declarator->kind != cdk_id)
14302             id_declarator = id_declarator->declarator;
14303           
14304           if (id_declarator && id_declarator->kind == cdk_id)
14305             error ("%H%sparameter pack %qD cannot have a default argument",
14306                    &declarator_token_start->location,
14307                    kind, id_declarator->u.id.unqualified_name);
14308           else
14309             error ("%H%sparameter pack cannot have a default argument",
14310                    &declarator_token_start->location, kind);
14311           
14312           default_argument = NULL_TREE;
14313         }
14314     }
14315   else
14316     default_argument = NULL_TREE;
14317
14318   return make_parameter_declarator (&decl_specifiers,
14319                                     declarator,
14320                                     default_argument);
14321 }
14322
14323 /* Parse a default argument and return it.
14324
14325    TEMPLATE_PARM_P is true if this is a default argument for a
14326    non-type template parameter.  */
14327 static tree
14328 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14329 {
14330   tree default_argument = NULL_TREE;
14331   bool saved_greater_than_is_operator_p;
14332   bool saved_local_variables_forbidden_p;
14333
14334   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14335      set correctly.  */
14336   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14337   parser->greater_than_is_operator_p = !template_parm_p;
14338   /* Local variable names (and the `this' keyword) may not
14339      appear in a default argument.  */
14340   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14341   parser->local_variables_forbidden_p = true;
14342   /* The default argument expression may cause implicitly
14343      defined member functions to be synthesized, which will
14344      result in garbage collection.  We must treat this
14345      situation as if we were within the body of function so as
14346      to avoid collecting live data on the stack.  */
14347   ++function_depth;
14348   /* Parse the assignment-expression.  */
14349   if (template_parm_p)
14350     push_deferring_access_checks (dk_no_deferred);
14351   default_argument
14352     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14353   if (template_parm_p)
14354     pop_deferring_access_checks ();
14355   /* Restore saved state.  */
14356   --function_depth;
14357   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14358   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14359
14360   return default_argument;
14361 }
14362
14363 /* Parse a function-body.
14364
14365    function-body:
14366      compound_statement  */
14367
14368 static void
14369 cp_parser_function_body (cp_parser *parser)
14370 {
14371   cp_parser_compound_statement (parser, NULL, false);
14372 }
14373
14374 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14375    true if a ctor-initializer was present.  */
14376
14377 static bool
14378 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14379 {
14380   tree body;
14381   bool ctor_initializer_p;
14382
14383   /* Begin the function body.  */
14384   body = begin_function_body ();
14385   /* Parse the optional ctor-initializer.  */
14386   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14387   /* Parse the function-body.  */
14388   cp_parser_function_body (parser);
14389   /* Finish the function body.  */
14390   finish_function_body (body);
14391
14392   return ctor_initializer_p;
14393 }
14394
14395 /* Parse an initializer.
14396
14397    initializer:
14398      = initializer-clause
14399      ( expression-list )
14400
14401    Returns an expression representing the initializer.  If no
14402    initializer is present, NULL_TREE is returned.
14403
14404    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14405    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14406    set to TRUE if there is no initializer present.  If there is an
14407    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14408    is set to true; otherwise it is set to false.  */
14409
14410 static tree
14411 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14412                        bool* non_constant_p)
14413 {
14414   cp_token *token;
14415   tree init;
14416
14417   /* Peek at the next token.  */
14418   token = cp_lexer_peek_token (parser->lexer);
14419
14420   /* Let our caller know whether or not this initializer was
14421      parenthesized.  */
14422   *is_direct_init = (token->type != CPP_EQ);
14423   /* Assume that the initializer is constant.  */
14424   *non_constant_p = false;
14425
14426   if (token->type == CPP_EQ)
14427     {
14428       /* Consume the `='.  */
14429       cp_lexer_consume_token (parser->lexer);
14430       /* Parse the initializer-clause.  */
14431       init = cp_parser_initializer_clause (parser, non_constant_p);
14432     }
14433   else if (token->type == CPP_OPEN_PAREN)
14434     init = cp_parser_parenthesized_expression_list (parser, false,
14435                                                     /*cast_p=*/false,
14436                                                     /*allow_expansion_p=*/true,
14437                                                     non_constant_p);
14438   else if (token->type == CPP_OPEN_BRACE)
14439     {
14440       maybe_warn_cpp0x ("extended initializer lists");
14441       init = cp_parser_braced_list (parser, non_constant_p);
14442       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14443     }
14444   else
14445     {
14446       /* Anything else is an error.  */
14447       cp_parser_error (parser, "expected initializer");
14448       init = error_mark_node;
14449     }
14450
14451   return init;
14452 }
14453
14454 /* Parse an initializer-clause.
14455
14456    initializer-clause:
14457      assignment-expression
14458      braced-init-list
14459
14460    Returns an expression representing the initializer.
14461
14462    If the `assignment-expression' production is used the value
14463    returned is simply a representation for the expression.
14464
14465    Otherwise, calls cp_parser_braced_list.  */
14466
14467 static tree
14468 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14469 {
14470   tree initializer;
14471
14472   /* Assume the expression is constant.  */
14473   *non_constant_p = false;
14474
14475   /* If it is not a `{', then we are looking at an
14476      assignment-expression.  */
14477   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14478     {
14479       initializer
14480         = cp_parser_constant_expression (parser,
14481                                         /*allow_non_constant_p=*/true,
14482                                         non_constant_p);
14483       if (!*non_constant_p)
14484         initializer = fold_non_dependent_expr (initializer);
14485     }
14486   else
14487     initializer = cp_parser_braced_list (parser, non_constant_p);
14488
14489   return initializer;
14490 }
14491
14492 /* Parse a brace-enclosed initializer list.
14493
14494    braced-init-list:
14495      { initializer-list , [opt] }
14496      { }
14497
14498    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14499    the elements of the initializer-list (or NULL, if the last
14500    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14501    NULL_TREE.  There is no way to detect whether or not the optional
14502    trailing `,' was provided.  NON_CONSTANT_P is as for
14503    cp_parser_initializer.  */     
14504
14505 static tree
14506 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14507 {
14508   tree initializer;
14509
14510   /* Consume the `{' token.  */
14511   cp_lexer_consume_token (parser->lexer);
14512   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14513   initializer = make_node (CONSTRUCTOR);
14514   /* If it's not a `}', then there is a non-trivial initializer.  */
14515   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14516     {
14517       /* Parse the initializer list.  */
14518       CONSTRUCTOR_ELTS (initializer)
14519         = cp_parser_initializer_list (parser, non_constant_p);
14520       /* A trailing `,' token is allowed.  */
14521       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14522         cp_lexer_consume_token (parser->lexer);
14523     }
14524   /* Now, there should be a trailing `}'.  */
14525   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14526   TREE_TYPE (initializer) = init_list_type_node;
14527   return initializer;
14528 }
14529
14530 /* Parse an initializer-list.
14531
14532    initializer-list:
14533      initializer-clause ... [opt]
14534      initializer-list , initializer-clause ... [opt]
14535
14536    GNU Extension:
14537
14538    initializer-list:
14539      identifier : initializer-clause
14540      initializer-list, identifier : initializer-clause
14541
14542    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14543    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14544    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14545    as for cp_parser_initializer.  */
14546
14547 static VEC(constructor_elt,gc) *
14548 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14549 {
14550   VEC(constructor_elt,gc) *v = NULL;
14551
14552   /* Assume all of the expressions are constant.  */
14553   *non_constant_p = false;
14554
14555   /* Parse the rest of the list.  */
14556   while (true)
14557     {
14558       cp_token *token;
14559       tree identifier;
14560       tree initializer;
14561       bool clause_non_constant_p;
14562
14563       /* If the next token is an identifier and the following one is a
14564          colon, we are looking at the GNU designated-initializer
14565          syntax.  */
14566       if (cp_parser_allow_gnu_extensions_p (parser)
14567           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14568           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14569         {
14570           /* Warn the user that they are using an extension.  */
14571           pedwarn (input_location, OPT_pedantic, 
14572                    "ISO C++ does not allow designated initializers");
14573           /* Consume the identifier.  */
14574           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14575           /* Consume the `:'.  */
14576           cp_lexer_consume_token (parser->lexer);
14577         }
14578       else
14579         identifier = NULL_TREE;
14580
14581       /* Parse the initializer.  */
14582       initializer = cp_parser_initializer_clause (parser,
14583                                                   &clause_non_constant_p);
14584       /* If any clause is non-constant, so is the entire initializer.  */
14585       if (clause_non_constant_p)
14586         *non_constant_p = true;
14587
14588       /* If we have an ellipsis, this is an initializer pack
14589          expansion.  */
14590       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14591         {
14592           /* Consume the `...'.  */
14593           cp_lexer_consume_token (parser->lexer);
14594
14595           /* Turn the initializer into an initializer expansion.  */
14596           initializer = make_pack_expansion (initializer);
14597         }
14598
14599       /* Add it to the vector.  */
14600       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14601
14602       /* If the next token is not a comma, we have reached the end of
14603          the list.  */
14604       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14605         break;
14606
14607       /* Peek at the next token.  */
14608       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14609       /* If the next token is a `}', then we're still done.  An
14610          initializer-clause can have a trailing `,' after the
14611          initializer-list and before the closing `}'.  */
14612       if (token->type == CPP_CLOSE_BRACE)
14613         break;
14614
14615       /* Consume the `,' token.  */
14616       cp_lexer_consume_token (parser->lexer);
14617     }
14618
14619   return v;
14620 }
14621
14622 /* Classes [gram.class] */
14623
14624 /* Parse a class-name.
14625
14626    class-name:
14627      identifier
14628      template-id
14629
14630    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14631    to indicate that names looked up in dependent types should be
14632    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14633    keyword has been used to indicate that the name that appears next
14634    is a template.  TAG_TYPE indicates the explicit tag given before
14635    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14636    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14637    is the class being defined in a class-head.
14638
14639    Returns the TYPE_DECL representing the class.  */
14640
14641 static tree
14642 cp_parser_class_name (cp_parser *parser,
14643                       bool typename_keyword_p,
14644                       bool template_keyword_p,
14645                       enum tag_types tag_type,
14646                       bool check_dependency_p,
14647                       bool class_head_p,
14648                       bool is_declaration)
14649 {
14650   tree decl;
14651   tree scope;
14652   bool typename_p;
14653   cp_token *token;
14654
14655   /* All class-names start with an identifier.  */
14656   token = cp_lexer_peek_token (parser->lexer);
14657   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14658     {
14659       cp_parser_error (parser, "expected class-name");
14660       return error_mark_node;
14661     }
14662
14663   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14664      to a template-id, so we save it here.  */
14665   scope = parser->scope;
14666   if (scope == error_mark_node)
14667     return error_mark_node;
14668
14669   /* Any name names a type if we're following the `typename' keyword
14670      in a qualified name where the enclosing scope is type-dependent.  */
14671   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14672                 && dependent_type_p (scope));
14673   /* Handle the common case (an identifier, but not a template-id)
14674      efficiently.  */
14675   if (token->type == CPP_NAME
14676       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14677     {
14678       cp_token *identifier_token;
14679       tree identifier;
14680       bool ambiguous_p;
14681
14682       /* Look for the identifier.  */
14683       identifier_token = cp_lexer_peek_token (parser->lexer);
14684       ambiguous_p = identifier_token->ambiguous_p;
14685       identifier = cp_parser_identifier (parser);
14686       /* If the next token isn't an identifier, we are certainly not
14687          looking at a class-name.  */
14688       if (identifier == error_mark_node)
14689         decl = error_mark_node;
14690       /* If we know this is a type-name, there's no need to look it
14691          up.  */
14692       else if (typename_p)
14693         decl = identifier;
14694       else
14695         {
14696           tree ambiguous_decls;
14697           /* If we already know that this lookup is ambiguous, then
14698              we've already issued an error message; there's no reason
14699              to check again.  */
14700           if (ambiguous_p)
14701             {
14702               cp_parser_simulate_error (parser);
14703               return error_mark_node;
14704             }
14705           /* If the next token is a `::', then the name must be a type
14706              name.
14707
14708              [basic.lookup.qual]
14709
14710              During the lookup for a name preceding the :: scope
14711              resolution operator, object, function, and enumerator
14712              names are ignored.  */
14713           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14714             tag_type = typename_type;
14715           /* Look up the name.  */
14716           decl = cp_parser_lookup_name (parser, identifier,
14717                                         tag_type,
14718                                         /*is_template=*/false,
14719                                         /*is_namespace=*/false,
14720                                         check_dependency_p,
14721                                         &ambiguous_decls,
14722                                         identifier_token->location);
14723           if (ambiguous_decls)
14724             {
14725               error ("%Hreference to %qD is ambiguous",
14726                      &identifier_token->location, identifier);
14727               print_candidates (ambiguous_decls);
14728               if (cp_parser_parsing_tentatively (parser))
14729                 {
14730                   identifier_token->ambiguous_p = true;
14731                   cp_parser_simulate_error (parser);
14732                 }
14733               return error_mark_node;
14734             }
14735         }
14736     }
14737   else
14738     {
14739       /* Try a template-id.  */
14740       decl = cp_parser_template_id (parser, template_keyword_p,
14741                                     check_dependency_p,
14742                                     is_declaration);
14743       if (decl == error_mark_node)
14744         return error_mark_node;
14745     }
14746
14747   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14748
14749   /* If this is a typename, create a TYPENAME_TYPE.  */
14750   if (typename_p && decl != error_mark_node)
14751     {
14752       decl = make_typename_type (scope, decl, typename_type,
14753                                  /*complain=*/tf_error);
14754       if (decl != error_mark_node)
14755         decl = TYPE_NAME (decl);
14756     }
14757
14758   /* Check to see that it is really the name of a class.  */
14759   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14760       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14761       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14762     /* Situations like this:
14763
14764          template <typename T> struct A {
14765            typename T::template X<int>::I i;
14766          };
14767
14768        are problematic.  Is `T::template X<int>' a class-name?  The
14769        standard does not seem to be definitive, but there is no other
14770        valid interpretation of the following `::'.  Therefore, those
14771        names are considered class-names.  */
14772     {
14773       decl = make_typename_type (scope, decl, tag_type, tf_error);
14774       if (decl != error_mark_node)
14775         decl = TYPE_NAME (decl);
14776     }
14777   else if (TREE_CODE (decl) != TYPE_DECL
14778            || TREE_TYPE (decl) == error_mark_node
14779            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14780     decl = error_mark_node;
14781
14782   if (decl == error_mark_node)
14783     cp_parser_error (parser, "expected class-name");
14784
14785   return decl;
14786 }
14787
14788 /* Parse a class-specifier.
14789
14790    class-specifier:
14791      class-head { member-specification [opt] }
14792
14793    Returns the TREE_TYPE representing the class.  */
14794
14795 static tree
14796 cp_parser_class_specifier (cp_parser* parser)
14797 {
14798   cp_token *token;
14799   tree type;
14800   tree attributes = NULL_TREE;
14801   int has_trailing_semicolon;
14802   bool nested_name_specifier_p;
14803   unsigned saved_num_template_parameter_lists;
14804   bool saved_in_function_body;
14805   tree old_scope = NULL_TREE;
14806   tree scope = NULL_TREE;
14807   tree bases;
14808
14809   push_deferring_access_checks (dk_no_deferred);
14810
14811   /* Parse the class-head.  */
14812   type = cp_parser_class_head (parser,
14813                                &nested_name_specifier_p,
14814                                &attributes,
14815                                &bases);
14816   /* If the class-head was a semantic disaster, skip the entire body
14817      of the class.  */
14818   if (!type)
14819     {
14820       cp_parser_skip_to_end_of_block_or_statement (parser);
14821       pop_deferring_access_checks ();
14822       return error_mark_node;
14823     }
14824
14825   /* Look for the `{'.  */
14826   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14827     {
14828       pop_deferring_access_checks ();
14829       return error_mark_node;
14830     }
14831
14832   /* Process the base classes. If they're invalid, skip the 
14833      entire class body.  */
14834   if (!xref_basetypes (type, bases))
14835     {
14836       /* Consuming the closing brace yields better error messages
14837          later on.  */
14838       if (cp_parser_skip_to_closing_brace (parser))
14839         cp_lexer_consume_token (parser->lexer);
14840       pop_deferring_access_checks ();
14841       return error_mark_node;
14842     }
14843
14844   /* Issue an error message if type-definitions are forbidden here.  */
14845   cp_parser_check_type_definition (parser);
14846   /* Remember that we are defining one more class.  */
14847   ++parser->num_classes_being_defined;
14848   /* Inside the class, surrounding template-parameter-lists do not
14849      apply.  */
14850   saved_num_template_parameter_lists
14851     = parser->num_template_parameter_lists;
14852   parser->num_template_parameter_lists = 0;
14853   /* We are not in a function body.  */
14854   saved_in_function_body = parser->in_function_body;
14855   parser->in_function_body = false;
14856
14857   /* Start the class.  */
14858   if (nested_name_specifier_p)
14859     {
14860       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14861       old_scope = push_inner_scope (scope);
14862     }
14863   type = begin_class_definition (type, attributes);
14864
14865   if (type == error_mark_node)
14866     /* If the type is erroneous, skip the entire body of the class.  */
14867     cp_parser_skip_to_closing_brace (parser);
14868   else
14869     /* Parse the member-specification.  */
14870     cp_parser_member_specification_opt (parser);
14871
14872   /* Look for the trailing `}'.  */
14873   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14874   /* We get better error messages by noticing a common problem: a
14875      missing trailing `;'.  */
14876   token = cp_lexer_peek_token (parser->lexer);
14877   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14878   /* Look for trailing attributes to apply to this class.  */
14879   if (cp_parser_allow_gnu_extensions_p (parser))
14880     attributes = cp_parser_attributes_opt (parser);
14881   if (type != error_mark_node)
14882     type = finish_struct (type, attributes);
14883   if (nested_name_specifier_p)
14884     pop_inner_scope (old_scope, scope);
14885   /* If this class is not itself within the scope of another class,
14886      then we need to parse the bodies of all of the queued function
14887      definitions.  Note that the queued functions defined in a class
14888      are not always processed immediately following the
14889      class-specifier for that class.  Consider:
14890
14891        struct A {
14892          struct B { void f() { sizeof (A); } };
14893        };
14894
14895      If `f' were processed before the processing of `A' were
14896      completed, there would be no way to compute the size of `A'.
14897      Note that the nesting we are interested in here is lexical --
14898      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14899      for:
14900
14901        struct A { struct B; };
14902        struct A::B { void f() { } };
14903
14904      there is no need to delay the parsing of `A::B::f'.  */
14905   if (--parser->num_classes_being_defined == 0)
14906     {
14907       tree queue_entry;
14908       tree fn;
14909       tree class_type = NULL_TREE;
14910       tree pushed_scope = NULL_TREE;
14911
14912       /* In a first pass, parse default arguments to the functions.
14913          Then, in a second pass, parse the bodies of the functions.
14914          This two-phased approach handles cases like:
14915
14916             struct S {
14917               void f() { g(); }
14918               void g(int i = 3);
14919             };
14920
14921          */
14922       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14923              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14924            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14925            TREE_PURPOSE (parser->unparsed_functions_queues)
14926              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14927         {
14928           fn = TREE_VALUE (queue_entry);
14929           /* If there are default arguments that have not yet been processed,
14930              take care of them now.  */
14931           if (class_type != TREE_PURPOSE (queue_entry))
14932             {
14933               if (pushed_scope)
14934                 pop_scope (pushed_scope);
14935               class_type = TREE_PURPOSE (queue_entry);
14936               pushed_scope = push_scope (class_type);
14937             }
14938           /* Make sure that any template parameters are in scope.  */
14939           maybe_begin_member_template_processing (fn);
14940           /* Parse the default argument expressions.  */
14941           cp_parser_late_parsing_default_args (parser, fn);
14942           /* Remove any template parameters from the symbol table.  */
14943           maybe_end_member_template_processing ();
14944         }
14945       if (pushed_scope)
14946         pop_scope (pushed_scope);
14947       /* Now parse the body of the functions.  */
14948       for (TREE_VALUE (parser->unparsed_functions_queues)
14949              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14950            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14951            TREE_VALUE (parser->unparsed_functions_queues)
14952              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14953         {
14954           /* Figure out which function we need to process.  */
14955           fn = TREE_VALUE (queue_entry);
14956           /* Parse the function.  */
14957           cp_parser_late_parsing_for_member (parser, fn);
14958         }
14959     }
14960
14961   /* Put back any saved access checks.  */
14962   pop_deferring_access_checks ();
14963
14964   /* Restore saved state.  */
14965   parser->in_function_body = saved_in_function_body;
14966   parser->num_template_parameter_lists
14967     = saved_num_template_parameter_lists;
14968
14969   return type;
14970 }
14971
14972 /* Parse a class-head.
14973
14974    class-head:
14975      class-key identifier [opt] base-clause [opt]
14976      class-key nested-name-specifier identifier base-clause [opt]
14977      class-key nested-name-specifier [opt] template-id
14978        base-clause [opt]
14979
14980    GNU Extensions:
14981      class-key attributes identifier [opt] base-clause [opt]
14982      class-key attributes nested-name-specifier identifier base-clause [opt]
14983      class-key attributes nested-name-specifier [opt] template-id
14984        base-clause [opt]
14985
14986    Upon return BASES is initialized to the list of base classes (or
14987    NULL, if there are none) in the same form returned by
14988    cp_parser_base_clause.
14989
14990    Returns the TYPE of the indicated class.  Sets
14991    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14992    involving a nested-name-specifier was used, and FALSE otherwise.
14993
14994    Returns error_mark_node if this is not a class-head.
14995
14996    Returns NULL_TREE if the class-head is syntactically valid, but
14997    semantically invalid in a way that means we should skip the entire
14998    body of the class.  */
14999
15000 static tree
15001 cp_parser_class_head (cp_parser* parser,
15002                       bool* nested_name_specifier_p,
15003                       tree *attributes_p,
15004                       tree *bases)
15005 {
15006   tree nested_name_specifier;
15007   enum tag_types class_key;
15008   tree id = NULL_TREE;
15009   tree type = NULL_TREE;
15010   tree attributes;
15011   bool template_id_p = false;
15012   bool qualified_p = false;
15013   bool invalid_nested_name_p = false;
15014   bool invalid_explicit_specialization_p = false;
15015   tree pushed_scope = NULL_TREE;
15016   unsigned num_templates;
15017   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15018   /* Assume no nested-name-specifier will be present.  */
15019   *nested_name_specifier_p = false;
15020   /* Assume no template parameter lists will be used in defining the
15021      type.  */
15022   num_templates = 0;
15023
15024   *bases = NULL_TREE;
15025
15026   /* Look for the class-key.  */
15027   class_key = cp_parser_class_key (parser);
15028   if (class_key == none_type)
15029     return error_mark_node;
15030
15031   /* Parse the attributes.  */
15032   attributes = cp_parser_attributes_opt (parser);
15033
15034   /* If the next token is `::', that is invalid -- but sometimes
15035      people do try to write:
15036
15037        struct ::S {};
15038
15039      Handle this gracefully by accepting the extra qualifier, and then
15040      issuing an error about it later if this really is a
15041      class-head.  If it turns out just to be an elaborated type
15042      specifier, remain silent.  */
15043   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15044     qualified_p = true;
15045
15046   push_deferring_access_checks (dk_no_check);
15047
15048   /* Determine the name of the class.  Begin by looking for an
15049      optional nested-name-specifier.  */
15050   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15051   nested_name_specifier
15052     = cp_parser_nested_name_specifier_opt (parser,
15053                                            /*typename_keyword_p=*/false,
15054                                            /*check_dependency_p=*/false,
15055                                            /*type_p=*/false,
15056                                            /*is_declaration=*/false);
15057   /* If there was a nested-name-specifier, then there *must* be an
15058      identifier.  */
15059   if (nested_name_specifier)
15060     {
15061       type_start_token = cp_lexer_peek_token (parser->lexer);
15062       /* Although the grammar says `identifier', it really means
15063          `class-name' or `template-name'.  You are only allowed to
15064          define a class that has already been declared with this
15065          syntax.
15066
15067          The proposed resolution for Core Issue 180 says that wherever
15068          you see `class T::X' you should treat `X' as a type-name.
15069
15070          It is OK to define an inaccessible class; for example:
15071
15072            class A { class B; };
15073            class A::B {};
15074
15075          We do not know if we will see a class-name, or a
15076          template-name.  We look for a class-name first, in case the
15077          class-name is a template-id; if we looked for the
15078          template-name first we would stop after the template-name.  */
15079       cp_parser_parse_tentatively (parser);
15080       type = cp_parser_class_name (parser,
15081                                    /*typename_keyword_p=*/false,
15082                                    /*template_keyword_p=*/false,
15083                                    class_type,
15084                                    /*check_dependency_p=*/false,
15085                                    /*class_head_p=*/true,
15086                                    /*is_declaration=*/false);
15087       /* If that didn't work, ignore the nested-name-specifier.  */
15088       if (!cp_parser_parse_definitely (parser))
15089         {
15090           invalid_nested_name_p = true;
15091           type_start_token = cp_lexer_peek_token (parser->lexer);
15092           id = cp_parser_identifier (parser);
15093           if (id == error_mark_node)
15094             id = NULL_TREE;
15095         }
15096       /* If we could not find a corresponding TYPE, treat this
15097          declaration like an unqualified declaration.  */
15098       if (type == error_mark_node)
15099         nested_name_specifier = NULL_TREE;
15100       /* Otherwise, count the number of templates used in TYPE and its
15101          containing scopes.  */
15102       else
15103         {
15104           tree scope;
15105
15106           for (scope = TREE_TYPE (type);
15107                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15108                scope = (TYPE_P (scope)
15109                         ? TYPE_CONTEXT (scope)
15110                         : DECL_CONTEXT (scope)))
15111             if (TYPE_P (scope)
15112                 && CLASS_TYPE_P (scope)
15113                 && CLASSTYPE_TEMPLATE_INFO (scope)
15114                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15115                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15116               ++num_templates;
15117         }
15118     }
15119   /* Otherwise, the identifier is optional.  */
15120   else
15121     {
15122       /* We don't know whether what comes next is a template-id,
15123          an identifier, or nothing at all.  */
15124       cp_parser_parse_tentatively (parser);
15125       /* Check for a template-id.  */
15126       type_start_token = cp_lexer_peek_token (parser->lexer);
15127       id = cp_parser_template_id (parser,
15128                                   /*template_keyword_p=*/false,
15129                                   /*check_dependency_p=*/true,
15130                                   /*is_declaration=*/true);
15131       /* If that didn't work, it could still be an identifier.  */
15132       if (!cp_parser_parse_definitely (parser))
15133         {
15134           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15135             {
15136               type_start_token = cp_lexer_peek_token (parser->lexer);
15137               id = cp_parser_identifier (parser);
15138             }
15139           else
15140             id = NULL_TREE;
15141         }
15142       else
15143         {
15144           template_id_p = true;
15145           ++num_templates;
15146         }
15147     }
15148
15149   pop_deferring_access_checks ();
15150
15151   if (id)
15152     cp_parser_check_for_invalid_template_id (parser, id,
15153                                              type_start_token->location);
15154
15155   /* If it's not a `:' or a `{' then we can't really be looking at a
15156      class-head, since a class-head only appears as part of a
15157      class-specifier.  We have to detect this situation before calling
15158      xref_tag, since that has irreversible side-effects.  */
15159   if (!cp_parser_next_token_starts_class_definition_p (parser))
15160     {
15161       cp_parser_error (parser, "expected %<{%> or %<:%>");
15162       return error_mark_node;
15163     }
15164
15165   /* At this point, we're going ahead with the class-specifier, even
15166      if some other problem occurs.  */
15167   cp_parser_commit_to_tentative_parse (parser);
15168   /* Issue the error about the overly-qualified name now.  */
15169   if (qualified_p)
15170     {
15171       cp_parser_error (parser,
15172                        "global qualification of class name is invalid");
15173       return error_mark_node;
15174     }
15175   else if (invalid_nested_name_p)
15176     {
15177       cp_parser_error (parser,
15178                        "qualified name does not name a class");
15179       return error_mark_node;
15180     }
15181   else if (nested_name_specifier)
15182     {
15183       tree scope;
15184
15185       /* Reject typedef-names in class heads.  */
15186       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15187         {
15188           error ("%Hinvalid class name in declaration of %qD",
15189                  &type_start_token->location, type);
15190           type = NULL_TREE;
15191           goto done;
15192         }
15193
15194       /* Figure out in what scope the declaration is being placed.  */
15195       scope = current_scope ();
15196       /* If that scope does not contain the scope in which the
15197          class was originally declared, the program is invalid.  */
15198       if (scope && !is_ancestor (scope, nested_name_specifier))
15199         {
15200           if (at_namespace_scope_p ())
15201             error ("%Hdeclaration of %qD in namespace %qD which does not "
15202                    "enclose %qD",
15203                    &type_start_token->location,
15204                    type, scope, nested_name_specifier);
15205           else
15206             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15207                    &type_start_token->location,
15208                    type, scope, nested_name_specifier);
15209           type = NULL_TREE;
15210           goto done;
15211         }
15212       /* [dcl.meaning]
15213
15214          A declarator-id shall not be qualified except for the
15215          definition of a ... nested class outside of its class
15216          ... [or] the definition or explicit instantiation of a
15217          class member of a namespace outside of its namespace.  */
15218       if (scope == nested_name_specifier)
15219         {
15220           permerror (input_location, "%Hextra qualification not allowed",
15221                      &nested_name_specifier_token_start->location);
15222           nested_name_specifier = NULL_TREE;
15223           num_templates = 0;
15224         }
15225     }
15226   /* An explicit-specialization must be preceded by "template <>".  If
15227      it is not, try to recover gracefully.  */
15228   if (at_namespace_scope_p ()
15229       && parser->num_template_parameter_lists == 0
15230       && template_id_p)
15231     {
15232       error ("%Han explicit specialization must be preceded by %<template <>%>",
15233              &type_start_token->location);
15234       invalid_explicit_specialization_p = true;
15235       /* Take the same action that would have been taken by
15236          cp_parser_explicit_specialization.  */
15237       ++parser->num_template_parameter_lists;
15238       begin_specialization ();
15239     }
15240   /* There must be no "return" statements between this point and the
15241      end of this function; set "type "to the correct return value and
15242      use "goto done;" to return.  */
15243   /* Make sure that the right number of template parameters were
15244      present.  */
15245   if (!cp_parser_check_template_parameters (parser, num_templates,
15246                                             type_start_token->location))
15247     {
15248       /* If something went wrong, there is no point in even trying to
15249          process the class-definition.  */
15250       type = NULL_TREE;
15251       goto done;
15252     }
15253
15254   /* Look up the type.  */
15255   if (template_id_p)
15256     {
15257       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15258           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15259               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15260         {
15261           error ("%Hfunction template %qD redeclared as a class template",
15262                  &type_start_token->location, id);
15263           type = error_mark_node;
15264         }
15265       else
15266         {
15267           type = TREE_TYPE (id);
15268           type = maybe_process_partial_specialization (type);
15269         }
15270       if (nested_name_specifier)
15271         pushed_scope = push_scope (nested_name_specifier);
15272     }
15273   else if (nested_name_specifier)
15274     {
15275       tree class_type;
15276
15277       /* Given:
15278
15279             template <typename T> struct S { struct T };
15280             template <typename T> struct S<T>::T { };
15281
15282          we will get a TYPENAME_TYPE when processing the definition of
15283          `S::T'.  We need to resolve it to the actual type before we
15284          try to define it.  */
15285       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15286         {
15287           class_type = resolve_typename_type (TREE_TYPE (type),
15288                                               /*only_current_p=*/false);
15289           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15290             type = TYPE_NAME (class_type);
15291           else
15292             {
15293               cp_parser_error (parser, "could not resolve typename type");
15294               type = error_mark_node;
15295             }
15296         }
15297
15298       if (maybe_process_partial_specialization (TREE_TYPE (type))
15299           == error_mark_node)
15300         {
15301           type = NULL_TREE;
15302           goto done;
15303         }
15304
15305       class_type = current_class_type;
15306       /* Enter the scope indicated by the nested-name-specifier.  */
15307       pushed_scope = push_scope (nested_name_specifier);
15308       /* Get the canonical version of this type.  */
15309       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15310       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15311           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15312         {
15313           type = push_template_decl (type);
15314           if (type == error_mark_node)
15315             {
15316               type = NULL_TREE;
15317               goto done;
15318             }
15319         }
15320
15321       type = TREE_TYPE (type);
15322       *nested_name_specifier_p = true;
15323     }
15324   else      /* The name is not a nested name.  */
15325     {
15326       /* If the class was unnamed, create a dummy name.  */
15327       if (!id)
15328         id = make_anon_name ();
15329       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15330                        parser->num_template_parameter_lists);
15331     }
15332
15333   /* Indicate whether this class was declared as a `class' or as a
15334      `struct'.  */
15335   if (TREE_CODE (type) == RECORD_TYPE)
15336     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15337   cp_parser_check_class_key (class_key, type);
15338
15339   /* If this type was already complete, and we see another definition,
15340      that's an error.  */
15341   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15342     {
15343       error ("%Hredefinition of %q#T",
15344              &type_start_token->location, type);
15345       error ("%Hprevious definition of %q+#T",
15346              &type_start_token->location, type);
15347       type = NULL_TREE;
15348       goto done;
15349     }
15350   else if (type == error_mark_node)
15351     type = NULL_TREE;
15352
15353   /* We will have entered the scope containing the class; the names of
15354      base classes should be looked up in that context.  For example:
15355
15356        struct A { struct B {}; struct C; };
15357        struct A::C : B {};
15358
15359      is valid.  */
15360
15361   /* Get the list of base-classes, if there is one.  */
15362   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15363     *bases = cp_parser_base_clause (parser);
15364
15365  done:
15366   /* Leave the scope given by the nested-name-specifier.  We will
15367      enter the class scope itself while processing the members.  */
15368   if (pushed_scope)
15369     pop_scope (pushed_scope);
15370
15371   if (invalid_explicit_specialization_p)
15372     {
15373       end_specialization ();
15374       --parser->num_template_parameter_lists;
15375     }
15376   *attributes_p = attributes;
15377   return type;
15378 }
15379
15380 /* Parse a class-key.
15381
15382    class-key:
15383      class
15384      struct
15385      union
15386
15387    Returns the kind of class-key specified, or none_type to indicate
15388    error.  */
15389
15390 static enum tag_types
15391 cp_parser_class_key (cp_parser* parser)
15392 {
15393   cp_token *token;
15394   enum tag_types tag_type;
15395
15396   /* Look for the class-key.  */
15397   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15398   if (!token)
15399     return none_type;
15400
15401   /* Check to see if the TOKEN is a class-key.  */
15402   tag_type = cp_parser_token_is_class_key (token);
15403   if (!tag_type)
15404     cp_parser_error (parser, "expected class-key");
15405   return tag_type;
15406 }
15407
15408 /* Parse an (optional) member-specification.
15409
15410    member-specification:
15411      member-declaration member-specification [opt]
15412      access-specifier : member-specification [opt]  */
15413
15414 static void
15415 cp_parser_member_specification_opt (cp_parser* parser)
15416 {
15417   while (true)
15418     {
15419       cp_token *token;
15420       enum rid keyword;
15421
15422       /* Peek at the next token.  */
15423       token = cp_lexer_peek_token (parser->lexer);
15424       /* If it's a `}', or EOF then we've seen all the members.  */
15425       if (token->type == CPP_CLOSE_BRACE
15426           || token->type == CPP_EOF
15427           || token->type == CPP_PRAGMA_EOL)
15428         break;
15429
15430       /* See if this token is a keyword.  */
15431       keyword = token->keyword;
15432       switch (keyword)
15433         {
15434         case RID_PUBLIC:
15435         case RID_PROTECTED:
15436         case RID_PRIVATE:
15437           /* Consume the access-specifier.  */
15438           cp_lexer_consume_token (parser->lexer);
15439           /* Remember which access-specifier is active.  */
15440           current_access_specifier = token->u.value;
15441           /* Look for the `:'.  */
15442           cp_parser_require (parser, CPP_COLON, "%<:%>");
15443           break;
15444
15445         default:
15446           /* Accept #pragmas at class scope.  */
15447           if (token->type == CPP_PRAGMA)
15448             {
15449               cp_parser_pragma (parser, pragma_external);
15450               break;
15451             }
15452
15453           /* Otherwise, the next construction must be a
15454              member-declaration.  */
15455           cp_parser_member_declaration (parser);
15456         }
15457     }
15458 }
15459
15460 /* Parse a member-declaration.
15461
15462    member-declaration:
15463      decl-specifier-seq [opt] member-declarator-list [opt] ;
15464      function-definition ; [opt]
15465      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15466      using-declaration
15467      template-declaration
15468
15469    member-declarator-list:
15470      member-declarator
15471      member-declarator-list , member-declarator
15472
15473    member-declarator:
15474      declarator pure-specifier [opt]
15475      declarator constant-initializer [opt]
15476      identifier [opt] : constant-expression
15477
15478    GNU Extensions:
15479
15480    member-declaration:
15481      __extension__ member-declaration
15482
15483    member-declarator:
15484      declarator attributes [opt] pure-specifier [opt]
15485      declarator attributes [opt] constant-initializer [opt]
15486      identifier [opt] attributes [opt] : constant-expression  
15487
15488    C++0x Extensions:
15489
15490    member-declaration:
15491      static_assert-declaration  */
15492
15493 static void
15494 cp_parser_member_declaration (cp_parser* parser)
15495 {
15496   cp_decl_specifier_seq decl_specifiers;
15497   tree prefix_attributes;
15498   tree decl;
15499   int declares_class_or_enum;
15500   bool friend_p;
15501   cp_token *token = NULL;
15502   cp_token *decl_spec_token_start = NULL;
15503   cp_token *initializer_token_start = NULL;
15504   int saved_pedantic;
15505
15506   /* Check for the `__extension__' keyword.  */
15507   if (cp_parser_extension_opt (parser, &saved_pedantic))
15508     {
15509       /* Recurse.  */
15510       cp_parser_member_declaration (parser);
15511       /* Restore the old value of the PEDANTIC flag.  */
15512       pedantic = saved_pedantic;
15513
15514       return;
15515     }
15516
15517   /* Check for a template-declaration.  */
15518   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15519     {
15520       /* An explicit specialization here is an error condition, and we
15521          expect the specialization handler to detect and report this.  */
15522       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15523           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15524         cp_parser_explicit_specialization (parser);
15525       else
15526         cp_parser_template_declaration (parser, /*member_p=*/true);
15527
15528       return;
15529     }
15530
15531   /* Check for a using-declaration.  */
15532   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15533     {
15534       /* Parse the using-declaration.  */
15535       cp_parser_using_declaration (parser,
15536                                    /*access_declaration_p=*/false);
15537       return;
15538     }
15539
15540   /* Check for @defs.  */
15541   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15542     {
15543       tree ivar, member;
15544       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15545       ivar = ivar_chains;
15546       while (ivar)
15547         {
15548           member = ivar;
15549           ivar = TREE_CHAIN (member);
15550           TREE_CHAIN (member) = NULL_TREE;
15551           finish_member_declaration (member);
15552         }
15553       return;
15554     }
15555
15556   /* If the next token is `static_assert' we have a static assertion.  */
15557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15558     {
15559       cp_parser_static_assert (parser, /*member_p=*/true);
15560       return;
15561     }
15562
15563   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15564     return;
15565
15566   /* Parse the decl-specifier-seq.  */
15567   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15568   cp_parser_decl_specifier_seq (parser,
15569                                 CP_PARSER_FLAGS_OPTIONAL,
15570                                 &decl_specifiers,
15571                                 &declares_class_or_enum);
15572   prefix_attributes = decl_specifiers.attributes;
15573   decl_specifiers.attributes = NULL_TREE;
15574   /* Check for an invalid type-name.  */
15575   if (!decl_specifiers.type
15576       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15577     return;
15578   /* If there is no declarator, then the decl-specifier-seq should
15579      specify a type.  */
15580   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15581     {
15582       /* If there was no decl-specifier-seq, and the next token is a
15583          `;', then we have something like:
15584
15585            struct S { ; };
15586
15587          [class.mem]
15588
15589          Each member-declaration shall declare at least one member
15590          name of the class.  */
15591       if (!decl_specifiers.any_specifiers_p)
15592         {
15593           cp_token *token = cp_lexer_peek_token (parser->lexer);
15594           if (!in_system_header_at (token->location))
15595             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15596         }
15597       else
15598         {
15599           tree type;
15600
15601           /* See if this declaration is a friend.  */
15602           friend_p = cp_parser_friend_p (&decl_specifiers);
15603           /* If there were decl-specifiers, check to see if there was
15604              a class-declaration.  */
15605           type = check_tag_decl (&decl_specifiers);
15606           /* Nested classes have already been added to the class, but
15607              a `friend' needs to be explicitly registered.  */
15608           if (friend_p)
15609             {
15610               /* If the `friend' keyword was present, the friend must
15611                  be introduced with a class-key.  */
15612                if (!declares_class_or_enum)
15613                  error ("%Ha class-key must be used when declaring a friend",
15614                         &decl_spec_token_start->location);
15615                /* In this case:
15616
15617                     template <typename T> struct A {
15618                       friend struct A<T>::B;
15619                     };
15620
15621                   A<T>::B will be represented by a TYPENAME_TYPE, and
15622                   therefore not recognized by check_tag_decl.  */
15623                if (!type
15624                    && decl_specifiers.type
15625                    && TYPE_P (decl_specifiers.type))
15626                  type = decl_specifiers.type;
15627                if (!type || !TYPE_P (type))
15628                  error ("%Hfriend declaration does not name a class or "
15629                         "function", &decl_spec_token_start->location);
15630                else
15631                  make_friend_class (current_class_type, type,
15632                                     /*complain=*/true);
15633             }
15634           /* If there is no TYPE, an error message will already have
15635              been issued.  */
15636           else if (!type || type == error_mark_node)
15637             ;
15638           /* An anonymous aggregate has to be handled specially; such
15639              a declaration really declares a data member (with a
15640              particular type), as opposed to a nested class.  */
15641           else if (ANON_AGGR_TYPE_P (type))
15642             {
15643               /* Remove constructors and such from TYPE, now that we
15644                  know it is an anonymous aggregate.  */
15645               fixup_anonymous_aggr (type);
15646               /* And make the corresponding data member.  */
15647               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15648               /* Add it to the class.  */
15649               finish_member_declaration (decl);
15650             }
15651           else
15652             cp_parser_check_access_in_redeclaration
15653                                               (TYPE_NAME (type),
15654                                                decl_spec_token_start->location);
15655         }
15656     }
15657   else
15658     {
15659       /* See if these declarations will be friends.  */
15660       friend_p = cp_parser_friend_p (&decl_specifiers);
15661
15662       /* Keep going until we hit the `;' at the end of the
15663          declaration.  */
15664       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15665         {
15666           tree attributes = NULL_TREE;
15667           tree first_attribute;
15668
15669           /* Peek at the next token.  */
15670           token = cp_lexer_peek_token (parser->lexer);
15671
15672           /* Check for a bitfield declaration.  */
15673           if (token->type == CPP_COLON
15674               || (token->type == CPP_NAME
15675                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15676                   == CPP_COLON))
15677             {
15678               tree identifier;
15679               tree width;
15680
15681               /* Get the name of the bitfield.  Note that we cannot just
15682                  check TOKEN here because it may have been invalidated by
15683                  the call to cp_lexer_peek_nth_token above.  */
15684               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15685                 identifier = cp_parser_identifier (parser);
15686               else
15687                 identifier = NULL_TREE;
15688
15689               /* Consume the `:' token.  */
15690               cp_lexer_consume_token (parser->lexer);
15691               /* Get the width of the bitfield.  */
15692               width
15693                 = cp_parser_constant_expression (parser,
15694                                                  /*allow_non_constant=*/false,
15695                                                  NULL);
15696
15697               /* Look for attributes that apply to the bitfield.  */
15698               attributes = cp_parser_attributes_opt (parser);
15699               /* Remember which attributes are prefix attributes and
15700                  which are not.  */
15701               first_attribute = attributes;
15702               /* Combine the attributes.  */
15703               attributes = chainon (prefix_attributes, attributes);
15704
15705               /* Create the bitfield declaration.  */
15706               decl = grokbitfield (identifier
15707                                    ? make_id_declarator (NULL_TREE,
15708                                                          identifier,
15709                                                          sfk_none)
15710                                    : NULL,
15711                                    &decl_specifiers,
15712                                    width,
15713                                    attributes);
15714             }
15715           else
15716             {
15717               cp_declarator *declarator;
15718               tree initializer;
15719               tree asm_specification;
15720               int ctor_dtor_or_conv_p;
15721
15722               /* Parse the declarator.  */
15723               declarator
15724                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15725                                         &ctor_dtor_or_conv_p,
15726                                         /*parenthesized_p=*/NULL,
15727                                         /*member_p=*/true);
15728
15729               /* If something went wrong parsing the declarator, make sure
15730                  that we at least consume some tokens.  */
15731               if (declarator == cp_error_declarator)
15732                 {
15733                   /* Skip to the end of the statement.  */
15734                   cp_parser_skip_to_end_of_statement (parser);
15735                   /* If the next token is not a semicolon, that is
15736                      probably because we just skipped over the body of
15737                      a function.  So, we consume a semicolon if
15738                      present, but do not issue an error message if it
15739                      is not present.  */
15740                   if (cp_lexer_next_token_is (parser->lexer,
15741                                               CPP_SEMICOLON))
15742                     cp_lexer_consume_token (parser->lexer);
15743                   return;
15744                 }
15745
15746               if (declares_class_or_enum & 2)
15747                 cp_parser_check_for_definition_in_return_type
15748                                             (declarator, decl_specifiers.type,
15749                                              decl_specifiers.type_location);
15750
15751               /* Look for an asm-specification.  */
15752               asm_specification = cp_parser_asm_specification_opt (parser);
15753               /* Look for attributes that apply to the declaration.  */
15754               attributes = cp_parser_attributes_opt (parser);
15755               /* Remember which attributes are prefix attributes and
15756                  which are not.  */
15757               first_attribute = attributes;
15758               /* Combine the attributes.  */
15759               attributes = chainon (prefix_attributes, attributes);
15760
15761               /* If it's an `=', then we have a constant-initializer or a
15762                  pure-specifier.  It is not correct to parse the
15763                  initializer before registering the member declaration
15764                  since the member declaration should be in scope while
15765                  its initializer is processed.  However, the rest of the
15766                  front end does not yet provide an interface that allows
15767                  us to handle this correctly.  */
15768               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15769                 {
15770                   /* In [class.mem]:
15771
15772                      A pure-specifier shall be used only in the declaration of
15773                      a virtual function.
15774
15775                      A member-declarator can contain a constant-initializer
15776                      only if it declares a static member of integral or
15777                      enumeration type.
15778
15779                      Therefore, if the DECLARATOR is for a function, we look
15780                      for a pure-specifier; otherwise, we look for a
15781                      constant-initializer.  When we call `grokfield', it will
15782                      perform more stringent semantics checks.  */
15783                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15784                   if (function_declarator_p (declarator))
15785                     initializer = cp_parser_pure_specifier (parser);
15786                   else
15787                     /* Parse the initializer.  */
15788                     initializer = cp_parser_constant_initializer (parser);
15789                 }
15790               /* Otherwise, there is no initializer.  */
15791               else
15792                 initializer = NULL_TREE;
15793
15794               /* See if we are probably looking at a function
15795                  definition.  We are certainly not looking at a
15796                  member-declarator.  Calling `grokfield' has
15797                  side-effects, so we must not do it unless we are sure
15798                  that we are looking at a member-declarator.  */
15799               if (cp_parser_token_starts_function_definition_p
15800                   (cp_lexer_peek_token (parser->lexer)))
15801                 {
15802                   /* The grammar does not allow a pure-specifier to be
15803                      used when a member function is defined.  (It is
15804                      possible that this fact is an oversight in the
15805                      standard, since a pure function may be defined
15806                      outside of the class-specifier.  */
15807                   if (initializer)
15808                     error ("%Hpure-specifier on function-definition",
15809                            &initializer_token_start->location);
15810                   decl = cp_parser_save_member_function_body (parser,
15811                                                               &decl_specifiers,
15812                                                               declarator,
15813                                                               attributes);
15814                   /* If the member was not a friend, declare it here.  */
15815                   if (!friend_p)
15816                     finish_member_declaration (decl);
15817                   /* Peek at the next token.  */
15818                   token = cp_lexer_peek_token (parser->lexer);
15819                   /* If the next token is a semicolon, consume it.  */
15820                   if (token->type == CPP_SEMICOLON)
15821                     cp_lexer_consume_token (parser->lexer);
15822                   return;
15823                 }
15824               else
15825                 if (declarator->kind == cdk_function)
15826                   declarator->id_loc = token->location;
15827                 /* Create the declaration.  */
15828                 decl = grokfield (declarator, &decl_specifiers,
15829                                   initializer, /*init_const_expr_p=*/true,
15830                                   asm_specification,
15831                                   attributes);
15832             }
15833
15834           /* Reset PREFIX_ATTRIBUTES.  */
15835           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15836             attributes = TREE_CHAIN (attributes);
15837           if (attributes)
15838             TREE_CHAIN (attributes) = NULL_TREE;
15839
15840           /* If there is any qualification still in effect, clear it
15841              now; we will be starting fresh with the next declarator.  */
15842           parser->scope = NULL_TREE;
15843           parser->qualifying_scope = NULL_TREE;
15844           parser->object_scope = NULL_TREE;
15845           /* If it's a `,', then there are more declarators.  */
15846           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15847             cp_lexer_consume_token (parser->lexer);
15848           /* If the next token isn't a `;', then we have a parse error.  */
15849           else if (cp_lexer_next_token_is_not (parser->lexer,
15850                                                CPP_SEMICOLON))
15851             {
15852               cp_parser_error (parser, "expected %<;%>");
15853               /* Skip tokens until we find a `;'.  */
15854               cp_parser_skip_to_end_of_statement (parser);
15855
15856               break;
15857             }
15858
15859           if (decl)
15860             {
15861               /* Add DECL to the list of members.  */
15862               if (!friend_p)
15863                 finish_member_declaration (decl);
15864
15865               if (TREE_CODE (decl) == FUNCTION_DECL)
15866                 cp_parser_save_default_args (parser, decl);
15867             }
15868         }
15869     }
15870
15871   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15872 }
15873
15874 /* Parse a pure-specifier.
15875
15876    pure-specifier:
15877      = 0
15878
15879    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15880    Otherwise, ERROR_MARK_NODE is returned.  */
15881
15882 static tree
15883 cp_parser_pure_specifier (cp_parser* parser)
15884 {
15885   cp_token *token;
15886
15887   /* Look for the `=' token.  */
15888   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15889     return error_mark_node;
15890   /* Look for the `0' token.  */
15891   token = cp_lexer_consume_token (parser->lexer);
15892
15893   /* Accept = default or = delete in c++0x mode.  */
15894   if (token->keyword == RID_DEFAULT
15895       || token->keyword == RID_DELETE)
15896     {
15897       maybe_warn_cpp0x ("defaulted and deleted functions");
15898       return token->u.value;
15899     }
15900
15901   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15902   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15903     {
15904       cp_parser_error (parser,
15905                        "invalid pure specifier (only %<= 0%> is allowed)");
15906       cp_parser_skip_to_end_of_statement (parser);
15907       return error_mark_node;
15908     }
15909   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15910     {
15911       error ("%Htemplates may not be %<virtual%>", &token->location);
15912       return error_mark_node;
15913     }
15914
15915   return integer_zero_node;
15916 }
15917
15918 /* Parse a constant-initializer.
15919
15920    constant-initializer:
15921      = constant-expression
15922
15923    Returns a representation of the constant-expression.  */
15924
15925 static tree
15926 cp_parser_constant_initializer (cp_parser* parser)
15927 {
15928   /* Look for the `=' token.  */
15929   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15930     return error_mark_node;
15931
15932   /* It is invalid to write:
15933
15934        struct S { static const int i = { 7 }; };
15935
15936      */
15937   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15938     {
15939       cp_parser_error (parser,
15940                        "a brace-enclosed initializer is not allowed here");
15941       /* Consume the opening brace.  */
15942       cp_lexer_consume_token (parser->lexer);
15943       /* Skip the initializer.  */
15944       cp_parser_skip_to_closing_brace (parser);
15945       /* Look for the trailing `}'.  */
15946       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15947
15948       return error_mark_node;
15949     }
15950
15951   return cp_parser_constant_expression (parser,
15952                                         /*allow_non_constant=*/false,
15953                                         NULL);
15954 }
15955
15956 /* Derived classes [gram.class.derived] */
15957
15958 /* Parse a base-clause.
15959
15960    base-clause:
15961      : base-specifier-list
15962
15963    base-specifier-list:
15964      base-specifier ... [opt]
15965      base-specifier-list , base-specifier ... [opt]
15966
15967    Returns a TREE_LIST representing the base-classes, in the order in
15968    which they were declared.  The representation of each node is as
15969    described by cp_parser_base_specifier.
15970
15971    In the case that no bases are specified, this function will return
15972    NULL_TREE, not ERROR_MARK_NODE.  */
15973
15974 static tree
15975 cp_parser_base_clause (cp_parser* parser)
15976 {
15977   tree bases = NULL_TREE;
15978
15979   /* Look for the `:' that begins the list.  */
15980   cp_parser_require (parser, CPP_COLON, "%<:%>");
15981
15982   /* Scan the base-specifier-list.  */
15983   while (true)
15984     {
15985       cp_token *token;
15986       tree base;
15987       bool pack_expansion_p = false;
15988
15989       /* Look for the base-specifier.  */
15990       base = cp_parser_base_specifier (parser);
15991       /* Look for the (optional) ellipsis. */
15992       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15993         {
15994           /* Consume the `...'. */
15995           cp_lexer_consume_token (parser->lexer);
15996
15997           pack_expansion_p = true;
15998         }
15999
16000       /* Add BASE to the front of the list.  */
16001       if (base != error_mark_node)
16002         {
16003           if (pack_expansion_p)
16004             /* Make this a pack expansion type. */
16005             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16006           
16007
16008           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16009             {
16010               TREE_CHAIN (base) = bases;
16011               bases = base;
16012             }
16013         }
16014       /* Peek at the next token.  */
16015       token = cp_lexer_peek_token (parser->lexer);
16016       /* If it's not a comma, then the list is complete.  */
16017       if (token->type != CPP_COMMA)
16018         break;
16019       /* Consume the `,'.  */
16020       cp_lexer_consume_token (parser->lexer);
16021     }
16022
16023   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16024      base class had a qualified name.  However, the next name that
16025      appears is certainly not qualified.  */
16026   parser->scope = NULL_TREE;
16027   parser->qualifying_scope = NULL_TREE;
16028   parser->object_scope = NULL_TREE;
16029
16030   return nreverse (bases);
16031 }
16032
16033 /* Parse a base-specifier.
16034
16035    base-specifier:
16036      :: [opt] nested-name-specifier [opt] class-name
16037      virtual access-specifier [opt] :: [opt] nested-name-specifier
16038        [opt] class-name
16039      access-specifier virtual [opt] :: [opt] nested-name-specifier
16040        [opt] class-name
16041
16042    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16043    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16044    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16045    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16046
16047 static tree
16048 cp_parser_base_specifier (cp_parser* parser)
16049 {
16050   cp_token *token;
16051   bool done = false;
16052   bool virtual_p = false;
16053   bool duplicate_virtual_error_issued_p = false;
16054   bool duplicate_access_error_issued_p = false;
16055   bool class_scope_p, template_p;
16056   tree access = access_default_node;
16057   tree type;
16058
16059   /* Process the optional `virtual' and `access-specifier'.  */
16060   while (!done)
16061     {
16062       /* Peek at the next token.  */
16063       token = cp_lexer_peek_token (parser->lexer);
16064       /* Process `virtual'.  */
16065       switch (token->keyword)
16066         {
16067         case RID_VIRTUAL:
16068           /* If `virtual' appears more than once, issue an error.  */
16069           if (virtual_p && !duplicate_virtual_error_issued_p)
16070             {
16071               cp_parser_error (parser,
16072                                "%<virtual%> specified more than once in base-specified");
16073               duplicate_virtual_error_issued_p = true;
16074             }
16075
16076           virtual_p = true;
16077
16078           /* Consume the `virtual' token.  */
16079           cp_lexer_consume_token (parser->lexer);
16080
16081           break;
16082
16083         case RID_PUBLIC:
16084         case RID_PROTECTED:
16085         case RID_PRIVATE:
16086           /* If more than one access specifier appears, issue an
16087              error.  */
16088           if (access != access_default_node
16089               && !duplicate_access_error_issued_p)
16090             {
16091               cp_parser_error (parser,
16092                                "more than one access specifier in base-specified");
16093               duplicate_access_error_issued_p = true;
16094             }
16095
16096           access = ridpointers[(int) token->keyword];
16097
16098           /* Consume the access-specifier.  */
16099           cp_lexer_consume_token (parser->lexer);
16100
16101           break;
16102
16103         default:
16104           done = true;
16105           break;
16106         }
16107     }
16108   /* It is not uncommon to see programs mechanically, erroneously, use
16109      the 'typename' keyword to denote (dependent) qualified types
16110      as base classes.  */
16111   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16112     {
16113       token = cp_lexer_peek_token (parser->lexer);
16114       if (!processing_template_decl)
16115         error ("%Hkeyword %<typename%> not allowed outside of templates",
16116                &token->location);
16117       else
16118         error ("%Hkeyword %<typename%> not allowed in this context "
16119                "(the base class is implicitly a type)",
16120                &token->location);
16121       cp_lexer_consume_token (parser->lexer);
16122     }
16123
16124   /* Look for the optional `::' operator.  */
16125   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16126   /* Look for the nested-name-specifier.  The simplest way to
16127      implement:
16128
16129        [temp.res]
16130
16131        The keyword `typename' is not permitted in a base-specifier or
16132        mem-initializer; in these contexts a qualified name that
16133        depends on a template-parameter is implicitly assumed to be a
16134        type name.
16135
16136      is to pretend that we have seen the `typename' keyword at this
16137      point.  */
16138   cp_parser_nested_name_specifier_opt (parser,
16139                                        /*typename_keyword_p=*/true,
16140                                        /*check_dependency_p=*/true,
16141                                        typename_type,
16142                                        /*is_declaration=*/true);
16143   /* If the base class is given by a qualified name, assume that names
16144      we see are type names or templates, as appropriate.  */
16145   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16146   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16147
16148   /* Finally, look for the class-name.  */
16149   type = cp_parser_class_name (parser,
16150                                class_scope_p,
16151                                template_p,
16152                                typename_type,
16153                                /*check_dependency_p=*/true,
16154                                /*class_head_p=*/false,
16155                                /*is_declaration=*/true);
16156
16157   if (type == error_mark_node)
16158     return error_mark_node;
16159
16160   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16161 }
16162
16163 /* Exception handling [gram.exception] */
16164
16165 /* Parse an (optional) exception-specification.
16166
16167    exception-specification:
16168      throw ( type-id-list [opt] )
16169
16170    Returns a TREE_LIST representing the exception-specification.  The
16171    TREE_VALUE of each node is a type.  */
16172
16173 static tree
16174 cp_parser_exception_specification_opt (cp_parser* parser)
16175 {
16176   cp_token *token;
16177   tree type_id_list;
16178
16179   /* Peek at the next token.  */
16180   token = cp_lexer_peek_token (parser->lexer);
16181   /* If it's not `throw', then there's no exception-specification.  */
16182   if (!cp_parser_is_keyword (token, RID_THROW))
16183     return NULL_TREE;
16184
16185   /* Consume the `throw'.  */
16186   cp_lexer_consume_token (parser->lexer);
16187
16188   /* Look for the `('.  */
16189   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16190
16191   /* Peek at the next token.  */
16192   token = cp_lexer_peek_token (parser->lexer);
16193   /* If it's not a `)', then there is a type-id-list.  */
16194   if (token->type != CPP_CLOSE_PAREN)
16195     {
16196       const char *saved_message;
16197
16198       /* Types may not be defined in an exception-specification.  */
16199       saved_message = parser->type_definition_forbidden_message;
16200       parser->type_definition_forbidden_message
16201         = "types may not be defined in an exception-specification";
16202       /* Parse the type-id-list.  */
16203       type_id_list = cp_parser_type_id_list (parser);
16204       /* Restore the saved message.  */
16205       parser->type_definition_forbidden_message = saved_message;
16206     }
16207   else
16208     type_id_list = empty_except_spec;
16209
16210   /* Look for the `)'.  */
16211   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16212
16213   return type_id_list;
16214 }
16215
16216 /* Parse an (optional) type-id-list.
16217
16218    type-id-list:
16219      type-id ... [opt]
16220      type-id-list , type-id ... [opt]
16221
16222    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16223    in the order that the types were presented.  */
16224
16225 static tree
16226 cp_parser_type_id_list (cp_parser* parser)
16227 {
16228   tree types = NULL_TREE;
16229
16230   while (true)
16231     {
16232       cp_token *token;
16233       tree type;
16234
16235       /* Get the next type-id.  */
16236       type = cp_parser_type_id (parser);
16237       /* Parse the optional ellipsis. */
16238       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16239         {
16240           /* Consume the `...'. */
16241           cp_lexer_consume_token (parser->lexer);
16242
16243           /* Turn the type into a pack expansion expression. */
16244           type = make_pack_expansion (type);
16245         }
16246       /* Add it to the list.  */
16247       types = add_exception_specifier (types, type, /*complain=*/1);
16248       /* Peek at the next token.  */
16249       token = cp_lexer_peek_token (parser->lexer);
16250       /* If it is not a `,', we are done.  */
16251       if (token->type != CPP_COMMA)
16252         break;
16253       /* Consume the `,'.  */
16254       cp_lexer_consume_token (parser->lexer);
16255     }
16256
16257   return nreverse (types);
16258 }
16259
16260 /* Parse a try-block.
16261
16262    try-block:
16263      try compound-statement handler-seq  */
16264
16265 static tree
16266 cp_parser_try_block (cp_parser* parser)
16267 {
16268   tree try_block;
16269
16270   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16271   try_block = begin_try_block ();
16272   cp_parser_compound_statement (parser, NULL, true);
16273   finish_try_block (try_block);
16274   cp_parser_handler_seq (parser);
16275   finish_handler_sequence (try_block);
16276
16277   return try_block;
16278 }
16279
16280 /* Parse a function-try-block.
16281
16282    function-try-block:
16283      try ctor-initializer [opt] function-body handler-seq  */
16284
16285 static bool
16286 cp_parser_function_try_block (cp_parser* parser)
16287 {
16288   tree compound_stmt;
16289   tree try_block;
16290   bool ctor_initializer_p;
16291
16292   /* Look for the `try' keyword.  */
16293   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16294     return false;
16295   /* Let the rest of the front end know where we are.  */
16296   try_block = begin_function_try_block (&compound_stmt);
16297   /* Parse the function-body.  */
16298   ctor_initializer_p
16299     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16300   /* We're done with the `try' part.  */
16301   finish_function_try_block (try_block);
16302   /* Parse the handlers.  */
16303   cp_parser_handler_seq (parser);
16304   /* We're done with the handlers.  */
16305   finish_function_handler_sequence (try_block, compound_stmt);
16306
16307   return ctor_initializer_p;
16308 }
16309
16310 /* Parse a handler-seq.
16311
16312    handler-seq:
16313      handler handler-seq [opt]  */
16314
16315 static void
16316 cp_parser_handler_seq (cp_parser* parser)
16317 {
16318   while (true)
16319     {
16320       cp_token *token;
16321
16322       /* Parse the handler.  */
16323       cp_parser_handler (parser);
16324       /* Peek at the next token.  */
16325       token = cp_lexer_peek_token (parser->lexer);
16326       /* If it's not `catch' then there are no more handlers.  */
16327       if (!cp_parser_is_keyword (token, RID_CATCH))
16328         break;
16329     }
16330 }
16331
16332 /* Parse a handler.
16333
16334    handler:
16335      catch ( exception-declaration ) compound-statement  */
16336
16337 static void
16338 cp_parser_handler (cp_parser* parser)
16339 {
16340   tree handler;
16341   tree declaration;
16342
16343   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16344   handler = begin_handler ();
16345   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16346   declaration = cp_parser_exception_declaration (parser);
16347   finish_handler_parms (declaration, handler);
16348   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16349   cp_parser_compound_statement (parser, NULL, false);
16350   finish_handler (handler);
16351 }
16352
16353 /* Parse an exception-declaration.
16354
16355    exception-declaration:
16356      type-specifier-seq declarator
16357      type-specifier-seq abstract-declarator
16358      type-specifier-seq
16359      ...
16360
16361    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16362    ellipsis variant is used.  */
16363
16364 static tree
16365 cp_parser_exception_declaration (cp_parser* parser)
16366 {
16367   cp_decl_specifier_seq type_specifiers;
16368   cp_declarator *declarator;
16369   const char *saved_message;
16370
16371   /* If it's an ellipsis, it's easy to handle.  */
16372   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16373     {
16374       /* Consume the `...' token.  */
16375       cp_lexer_consume_token (parser->lexer);
16376       return NULL_TREE;
16377     }
16378
16379   /* Types may not be defined in exception-declarations.  */
16380   saved_message = parser->type_definition_forbidden_message;
16381   parser->type_definition_forbidden_message
16382     = "types may not be defined in exception-declarations";
16383
16384   /* Parse the type-specifier-seq.  */
16385   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16386                                 &type_specifiers);
16387   /* If it's a `)', then there is no declarator.  */
16388   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16389     declarator = NULL;
16390   else
16391     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16392                                        /*ctor_dtor_or_conv_p=*/NULL,
16393                                        /*parenthesized_p=*/NULL,
16394                                        /*member_p=*/false);
16395
16396   /* Restore the saved message.  */
16397   parser->type_definition_forbidden_message = saved_message;
16398
16399   if (!type_specifiers.any_specifiers_p)
16400     return error_mark_node;
16401
16402   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16403 }
16404
16405 /* Parse a throw-expression.
16406
16407    throw-expression:
16408      throw assignment-expression [opt]
16409
16410    Returns a THROW_EXPR representing the throw-expression.  */
16411
16412 static tree
16413 cp_parser_throw_expression (cp_parser* parser)
16414 {
16415   tree expression;
16416   cp_token* token;
16417
16418   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16419   token = cp_lexer_peek_token (parser->lexer);
16420   /* Figure out whether or not there is an assignment-expression
16421      following the "throw" keyword.  */
16422   if (token->type == CPP_COMMA
16423       || token->type == CPP_SEMICOLON
16424       || token->type == CPP_CLOSE_PAREN
16425       || token->type == CPP_CLOSE_SQUARE
16426       || token->type == CPP_CLOSE_BRACE
16427       || token->type == CPP_COLON)
16428     expression = NULL_TREE;
16429   else
16430     expression = cp_parser_assignment_expression (parser,
16431                                                   /*cast_p=*/false);
16432
16433   return build_throw (expression);
16434 }
16435
16436 /* GNU Extensions */
16437
16438 /* Parse an (optional) asm-specification.
16439
16440    asm-specification:
16441      asm ( string-literal )
16442
16443    If the asm-specification is present, returns a STRING_CST
16444    corresponding to the string-literal.  Otherwise, returns
16445    NULL_TREE.  */
16446
16447 static tree
16448 cp_parser_asm_specification_opt (cp_parser* parser)
16449 {
16450   cp_token *token;
16451   tree asm_specification;
16452
16453   /* Peek at the next token.  */
16454   token = cp_lexer_peek_token (parser->lexer);
16455   /* If the next token isn't the `asm' keyword, then there's no
16456      asm-specification.  */
16457   if (!cp_parser_is_keyword (token, RID_ASM))
16458     return NULL_TREE;
16459
16460   /* Consume the `asm' token.  */
16461   cp_lexer_consume_token (parser->lexer);
16462   /* Look for the `('.  */
16463   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16464
16465   /* Look for the string-literal.  */
16466   asm_specification = cp_parser_string_literal (parser, false, false);
16467
16468   /* Look for the `)'.  */
16469   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16470
16471   return asm_specification;
16472 }
16473
16474 /* Parse an asm-operand-list.
16475
16476    asm-operand-list:
16477      asm-operand
16478      asm-operand-list , asm-operand
16479
16480    asm-operand:
16481      string-literal ( expression )
16482      [ string-literal ] string-literal ( expression )
16483
16484    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16485    each node is the expression.  The TREE_PURPOSE is itself a
16486    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16487    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16488    is a STRING_CST for the string literal before the parenthesis. Returns
16489    ERROR_MARK_NODE if any of the operands are invalid.  */
16490
16491 static tree
16492 cp_parser_asm_operand_list (cp_parser* parser)
16493 {
16494   tree asm_operands = NULL_TREE;
16495   bool invalid_operands = false;
16496
16497   while (true)
16498     {
16499       tree string_literal;
16500       tree expression;
16501       tree name;
16502
16503       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16504         {
16505           /* Consume the `[' token.  */
16506           cp_lexer_consume_token (parser->lexer);
16507           /* Read the operand name.  */
16508           name = cp_parser_identifier (parser);
16509           if (name != error_mark_node)
16510             name = build_string (IDENTIFIER_LENGTH (name),
16511                                  IDENTIFIER_POINTER (name));
16512           /* Look for the closing `]'.  */
16513           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16514         }
16515       else
16516         name = NULL_TREE;
16517       /* Look for the string-literal.  */
16518       string_literal = cp_parser_string_literal (parser, false, false);
16519
16520       /* Look for the `('.  */
16521       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16522       /* Parse the expression.  */
16523       expression = cp_parser_expression (parser, /*cast_p=*/false);
16524       /* Look for the `)'.  */
16525       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16526
16527       if (name == error_mark_node 
16528           || string_literal == error_mark_node 
16529           || expression == error_mark_node)
16530         invalid_operands = true;
16531
16532       /* Add this operand to the list.  */
16533       asm_operands = tree_cons (build_tree_list (name, string_literal),
16534                                 expression,
16535                                 asm_operands);
16536       /* If the next token is not a `,', there are no more
16537          operands.  */
16538       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16539         break;
16540       /* Consume the `,'.  */
16541       cp_lexer_consume_token (parser->lexer);
16542     }
16543
16544   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16545 }
16546
16547 /* Parse an asm-clobber-list.
16548
16549    asm-clobber-list:
16550      string-literal
16551      asm-clobber-list , string-literal
16552
16553    Returns a TREE_LIST, indicating the clobbers in the order that they
16554    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16555
16556 static tree
16557 cp_parser_asm_clobber_list (cp_parser* parser)
16558 {
16559   tree clobbers = NULL_TREE;
16560
16561   while (true)
16562     {
16563       tree string_literal;
16564
16565       /* Look for the string literal.  */
16566       string_literal = cp_parser_string_literal (parser, false, false);
16567       /* Add it to the list.  */
16568       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16569       /* If the next token is not a `,', then the list is
16570          complete.  */
16571       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16572         break;
16573       /* Consume the `,' token.  */
16574       cp_lexer_consume_token (parser->lexer);
16575     }
16576
16577   return clobbers;
16578 }
16579
16580 /* Parse an (optional) series of attributes.
16581
16582    attributes:
16583      attributes attribute
16584
16585    attribute:
16586      __attribute__ (( attribute-list [opt] ))
16587
16588    The return value is as for cp_parser_attribute_list.  */
16589
16590 static tree
16591 cp_parser_attributes_opt (cp_parser* parser)
16592 {
16593   tree attributes = NULL_TREE;
16594
16595   while (true)
16596     {
16597       cp_token *token;
16598       tree attribute_list;
16599
16600       /* Peek at the next token.  */
16601       token = cp_lexer_peek_token (parser->lexer);
16602       /* If it's not `__attribute__', then we're done.  */
16603       if (token->keyword != RID_ATTRIBUTE)
16604         break;
16605
16606       /* Consume the `__attribute__' keyword.  */
16607       cp_lexer_consume_token (parser->lexer);
16608       /* Look for the two `(' tokens.  */
16609       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16610       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16611
16612       /* Peek at the next token.  */
16613       token = cp_lexer_peek_token (parser->lexer);
16614       if (token->type != CPP_CLOSE_PAREN)
16615         /* Parse the attribute-list.  */
16616         attribute_list = cp_parser_attribute_list (parser);
16617       else
16618         /* If the next token is a `)', then there is no attribute
16619            list.  */
16620         attribute_list = NULL;
16621
16622       /* Look for the two `)' tokens.  */
16623       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16624       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16625
16626       /* Add these new attributes to the list.  */
16627       attributes = chainon (attributes, attribute_list);
16628     }
16629
16630   return attributes;
16631 }
16632
16633 /* Parse an attribute-list.
16634
16635    attribute-list:
16636      attribute
16637      attribute-list , attribute
16638
16639    attribute:
16640      identifier
16641      identifier ( identifier )
16642      identifier ( identifier , expression-list )
16643      identifier ( expression-list )
16644
16645    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16646    to an attribute.  The TREE_PURPOSE of each node is the identifier
16647    indicating which attribute is in use.  The TREE_VALUE represents
16648    the arguments, if any.  */
16649
16650 static tree
16651 cp_parser_attribute_list (cp_parser* parser)
16652 {
16653   tree attribute_list = NULL_TREE;
16654   bool save_translate_strings_p = parser->translate_strings_p;
16655
16656   parser->translate_strings_p = false;
16657   while (true)
16658     {
16659       cp_token *token;
16660       tree identifier;
16661       tree attribute;
16662
16663       /* Look for the identifier.  We also allow keywords here; for
16664          example `__attribute__ ((const))' is legal.  */
16665       token = cp_lexer_peek_token (parser->lexer);
16666       if (token->type == CPP_NAME
16667           || token->type == CPP_KEYWORD)
16668         {
16669           tree arguments = NULL_TREE;
16670
16671           /* Consume the token.  */
16672           token = cp_lexer_consume_token (parser->lexer);
16673
16674           /* Save away the identifier that indicates which attribute
16675              this is.  */
16676           identifier = token->u.value;
16677           attribute = build_tree_list (identifier, NULL_TREE);
16678
16679           /* Peek at the next token.  */
16680           token = cp_lexer_peek_token (parser->lexer);
16681           /* If it's an `(', then parse the attribute arguments.  */
16682           if (token->type == CPP_OPEN_PAREN)
16683             {
16684               arguments = cp_parser_parenthesized_expression_list
16685                           (parser, true, /*cast_p=*/false,
16686                            /*allow_expansion_p=*/false,
16687                            /*non_constant_p=*/NULL);
16688               /* Save the arguments away.  */
16689               TREE_VALUE (attribute) = arguments;
16690             }
16691
16692           if (arguments != error_mark_node)
16693             {
16694               /* Add this attribute to the list.  */
16695               TREE_CHAIN (attribute) = attribute_list;
16696               attribute_list = attribute;
16697             }
16698
16699           token = cp_lexer_peek_token (parser->lexer);
16700         }
16701       /* Now, look for more attributes.  If the next token isn't a
16702          `,', we're done.  */
16703       if (token->type != CPP_COMMA)
16704         break;
16705
16706       /* Consume the comma and keep going.  */
16707       cp_lexer_consume_token (parser->lexer);
16708     }
16709   parser->translate_strings_p = save_translate_strings_p;
16710
16711   /* We built up the list in reverse order.  */
16712   return nreverse (attribute_list);
16713 }
16714
16715 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16716    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16717    current value of the PEDANTIC flag, regardless of whether or not
16718    the `__extension__' keyword is present.  The caller is responsible
16719    for restoring the value of the PEDANTIC flag.  */
16720
16721 static bool
16722 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16723 {
16724   /* Save the old value of the PEDANTIC flag.  */
16725   *saved_pedantic = pedantic;
16726
16727   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16728     {
16729       /* Consume the `__extension__' token.  */
16730       cp_lexer_consume_token (parser->lexer);
16731       /* We're not being pedantic while the `__extension__' keyword is
16732          in effect.  */
16733       pedantic = 0;
16734
16735       return true;
16736     }
16737
16738   return false;
16739 }
16740
16741 /* Parse a label declaration.
16742
16743    label-declaration:
16744      __label__ label-declarator-seq ;
16745
16746    label-declarator-seq:
16747      identifier , label-declarator-seq
16748      identifier  */
16749
16750 static void
16751 cp_parser_label_declaration (cp_parser* parser)
16752 {
16753   /* Look for the `__label__' keyword.  */
16754   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16755
16756   while (true)
16757     {
16758       tree identifier;
16759
16760       /* Look for an identifier.  */
16761       identifier = cp_parser_identifier (parser);
16762       /* If we failed, stop.  */
16763       if (identifier == error_mark_node)
16764         break;
16765       /* Declare it as a label.  */
16766       finish_label_decl (identifier);
16767       /* If the next token is a `;', stop.  */
16768       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16769         break;
16770       /* Look for the `,' separating the label declarations.  */
16771       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16772     }
16773
16774   /* Look for the final `;'.  */
16775   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16776 }
16777
16778 /* Support Functions */
16779
16780 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16781    NAME should have one of the representations used for an
16782    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16783    is returned.  If PARSER->SCOPE is a dependent type, then a
16784    SCOPE_REF is returned.
16785
16786    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16787    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16788    was formed.  Abstractly, such entities should not be passed to this
16789    function, because they do not need to be looked up, but it is
16790    simpler to check for this special case here, rather than at the
16791    call-sites.
16792
16793    In cases not explicitly covered above, this function returns a
16794    DECL, OVERLOAD, or baselink representing the result of the lookup.
16795    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16796    is returned.
16797
16798    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16799    (e.g., "struct") that was used.  In that case bindings that do not
16800    refer to types are ignored.
16801
16802    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16803    ignored.
16804
16805    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16806    are ignored.
16807
16808    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16809    types.
16810
16811    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16812    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16813    NULL_TREE otherwise.  */
16814
16815 static tree
16816 cp_parser_lookup_name (cp_parser *parser, tree name,
16817                        enum tag_types tag_type,
16818                        bool is_template,
16819                        bool is_namespace,
16820                        bool check_dependency,
16821                        tree *ambiguous_decls,
16822                        location_t name_location)
16823 {
16824   int flags = 0;
16825   tree decl;
16826   tree object_type = parser->context->object_type;
16827
16828   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16829     flags |= LOOKUP_COMPLAIN;
16830
16831   /* Assume that the lookup will be unambiguous.  */
16832   if (ambiguous_decls)
16833     *ambiguous_decls = NULL_TREE;
16834
16835   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16836      no longer valid.  Note that if we are parsing tentatively, and
16837      the parse fails, OBJECT_TYPE will be automatically restored.  */
16838   parser->context->object_type = NULL_TREE;
16839
16840   if (name == error_mark_node)
16841     return error_mark_node;
16842
16843   /* A template-id has already been resolved; there is no lookup to
16844      do.  */
16845   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16846     return name;
16847   if (BASELINK_P (name))
16848     {
16849       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16850                   == TEMPLATE_ID_EXPR);
16851       return name;
16852     }
16853
16854   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16855      it should already have been checked to make sure that the name
16856      used matches the type being destroyed.  */
16857   if (TREE_CODE (name) == BIT_NOT_EXPR)
16858     {
16859       tree type;
16860
16861       /* Figure out to which type this destructor applies.  */
16862       if (parser->scope)
16863         type = parser->scope;
16864       else if (object_type)
16865         type = object_type;
16866       else
16867         type = current_class_type;
16868       /* If that's not a class type, there is no destructor.  */
16869       if (!type || !CLASS_TYPE_P (type))
16870         return error_mark_node;
16871       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16872         lazily_declare_fn (sfk_destructor, type);
16873       if (!CLASSTYPE_DESTRUCTORS (type))
16874           return error_mark_node;
16875       /* If it was a class type, return the destructor.  */
16876       return CLASSTYPE_DESTRUCTORS (type);
16877     }
16878
16879   /* By this point, the NAME should be an ordinary identifier.  If
16880      the id-expression was a qualified name, the qualifying scope is
16881      stored in PARSER->SCOPE at this point.  */
16882   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16883
16884   /* Perform the lookup.  */
16885   if (parser->scope)
16886     {
16887       bool dependent_p;
16888
16889       if (parser->scope == error_mark_node)
16890         return error_mark_node;
16891
16892       /* If the SCOPE is dependent, the lookup must be deferred until
16893          the template is instantiated -- unless we are explicitly
16894          looking up names in uninstantiated templates.  Even then, we
16895          cannot look up the name if the scope is not a class type; it
16896          might, for example, be a template type parameter.  */
16897       dependent_p = (TYPE_P (parser->scope)
16898                      && !(parser->in_declarator_p
16899                           && currently_open_class (parser->scope))
16900                      && dependent_type_p (parser->scope));
16901       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16902            && dependent_p)
16903         {
16904           if (tag_type)
16905             {
16906               tree type;
16907
16908               /* The resolution to Core Issue 180 says that `struct
16909                  A::B' should be considered a type-name, even if `A'
16910                  is dependent.  */
16911               type = make_typename_type (parser->scope, name, tag_type,
16912                                          /*complain=*/tf_error);
16913               decl = TYPE_NAME (type);
16914             }
16915           else if (is_template
16916                    && (cp_parser_next_token_ends_template_argument_p (parser)
16917                        || cp_lexer_next_token_is (parser->lexer,
16918                                                   CPP_CLOSE_PAREN)))
16919             decl = make_unbound_class_template (parser->scope,
16920                                                 name, NULL_TREE,
16921                                                 /*complain=*/tf_error);
16922           else
16923             decl = build_qualified_name (/*type=*/NULL_TREE,
16924                                          parser->scope, name,
16925                                          is_template);
16926         }
16927       else
16928         {
16929           tree pushed_scope = NULL_TREE;
16930
16931           /* If PARSER->SCOPE is a dependent type, then it must be a
16932              class type, and we must not be checking dependencies;
16933              otherwise, we would have processed this lookup above.  So
16934              that PARSER->SCOPE is not considered a dependent base by
16935              lookup_member, we must enter the scope here.  */
16936           if (dependent_p)
16937             pushed_scope = push_scope (parser->scope);
16938           /* If the PARSER->SCOPE is a template specialization, it
16939              may be instantiated during name lookup.  In that case,
16940              errors may be issued.  Even if we rollback the current
16941              tentative parse, those errors are valid.  */
16942           decl = lookup_qualified_name (parser->scope, name,
16943                                         tag_type != none_type,
16944                                         /*complain=*/true);
16945
16946           /* If we have a single function from a using decl, pull it out.  */
16947           if (decl
16948               && TREE_CODE (decl) == OVERLOAD
16949               && !really_overloaded_fn (decl))
16950             decl = OVL_FUNCTION (decl);
16951
16952           if (pushed_scope)
16953             pop_scope (pushed_scope);
16954         }
16955       parser->qualifying_scope = parser->scope;
16956       parser->object_scope = NULL_TREE;
16957     }
16958   else if (object_type)
16959     {
16960       tree object_decl = NULL_TREE;
16961       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16962          OBJECT_TYPE is not a class.  */
16963       if (CLASS_TYPE_P (object_type))
16964         /* If the OBJECT_TYPE is a template specialization, it may
16965            be instantiated during name lookup.  In that case, errors
16966            may be issued.  Even if we rollback the current tentative
16967            parse, those errors are valid.  */
16968         object_decl = lookup_member (object_type,
16969                                      name,
16970                                      /*protect=*/0,
16971                                      tag_type != none_type);
16972       /* Look it up in the enclosing context, too.  */
16973       decl = lookup_name_real (name, tag_type != none_type,
16974                                /*nonclass=*/0,
16975                                /*block_p=*/true, is_namespace, flags);
16976       parser->object_scope = object_type;
16977       parser->qualifying_scope = NULL_TREE;
16978       if (object_decl)
16979         decl = object_decl;
16980     }
16981   else
16982     {
16983       decl = lookup_name_real (name, tag_type != none_type,
16984                                /*nonclass=*/0,
16985                                /*block_p=*/true, is_namespace, flags);
16986       parser->qualifying_scope = NULL_TREE;
16987       parser->object_scope = NULL_TREE;
16988     }
16989
16990   /* If the lookup failed, let our caller know.  */
16991   if (!decl || decl == error_mark_node)
16992     return error_mark_node;
16993
16994   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16995   if (TREE_CODE (decl) == TREE_LIST)
16996     {
16997       if (ambiguous_decls)
16998         *ambiguous_decls = decl;
16999       /* The error message we have to print is too complicated for
17000          cp_parser_error, so we incorporate its actions directly.  */
17001       if (!cp_parser_simulate_error (parser))
17002         {
17003           error ("%Hreference to %qD is ambiguous",
17004                  &name_location, name);
17005           print_candidates (decl);
17006         }
17007       return error_mark_node;
17008     }
17009
17010   gcc_assert (DECL_P (decl)
17011               || TREE_CODE (decl) == OVERLOAD
17012               || TREE_CODE (decl) == SCOPE_REF
17013               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17014               || BASELINK_P (decl));
17015
17016   /* If we have resolved the name of a member declaration, check to
17017      see if the declaration is accessible.  When the name resolves to
17018      set of overloaded functions, accessibility is checked when
17019      overload resolution is done.
17020
17021      During an explicit instantiation, access is not checked at all,
17022      as per [temp.explicit].  */
17023   if (DECL_P (decl))
17024     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17025
17026   return decl;
17027 }
17028
17029 /* Like cp_parser_lookup_name, but for use in the typical case where
17030    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17031    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17032
17033 static tree
17034 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17035 {
17036   return cp_parser_lookup_name (parser, name,
17037                                 none_type,
17038                                 /*is_template=*/false,
17039                                 /*is_namespace=*/false,
17040                                 /*check_dependency=*/true,
17041                                 /*ambiguous_decls=*/NULL,
17042                                 location);
17043 }
17044
17045 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17046    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17047    true, the DECL indicates the class being defined in a class-head,
17048    or declared in an elaborated-type-specifier.
17049
17050    Otherwise, return DECL.  */
17051
17052 static tree
17053 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17054 {
17055   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17056      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17057
17058        struct A {
17059          template <typename T> struct B;
17060        };
17061
17062        template <typename T> struct A::B {};
17063
17064      Similarly, in an elaborated-type-specifier:
17065
17066        namespace N { struct X{}; }
17067
17068        struct A {
17069          template <typename T> friend struct N::X;
17070        };
17071
17072      However, if the DECL refers to a class type, and we are in
17073      the scope of the class, then the name lookup automatically
17074      finds the TYPE_DECL created by build_self_reference rather
17075      than a TEMPLATE_DECL.  For example, in:
17076
17077        template <class T> struct S {
17078          S s;
17079        };
17080
17081      there is no need to handle such case.  */
17082
17083   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17084     return DECL_TEMPLATE_RESULT (decl);
17085
17086   return decl;
17087 }
17088
17089 /* If too many, or too few, template-parameter lists apply to the
17090    declarator, issue an error message.  Returns TRUE if all went well,
17091    and FALSE otherwise.  */
17092
17093 static bool
17094 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17095                                                 cp_declarator *declarator,
17096                                                 location_t declarator_location)
17097 {
17098   unsigned num_templates;
17099
17100   /* We haven't seen any classes that involve template parameters yet.  */
17101   num_templates = 0;
17102
17103   switch (declarator->kind)
17104     {
17105     case cdk_id:
17106       if (declarator->u.id.qualifying_scope)
17107         {
17108           tree scope;
17109           tree member;
17110
17111           scope = declarator->u.id.qualifying_scope;
17112           member = declarator->u.id.unqualified_name;
17113
17114           while (scope && CLASS_TYPE_P (scope))
17115             {
17116               /* You're supposed to have one `template <...>'
17117                  for every template class, but you don't need one
17118                  for a full specialization.  For example:
17119
17120                  template <class T> struct S{};
17121                  template <> struct S<int> { void f(); };
17122                  void S<int>::f () {}
17123
17124                  is correct; there shouldn't be a `template <>' for
17125                  the definition of `S<int>::f'.  */
17126               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17127                 /* If SCOPE does not have template information of any
17128                    kind, then it is not a template, nor is it nested
17129                    within a template.  */
17130                 break;
17131               if (explicit_class_specialization_p (scope))
17132                 break;
17133               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17134                 ++num_templates;
17135
17136               scope = TYPE_CONTEXT (scope);
17137             }
17138         }
17139       else if (TREE_CODE (declarator->u.id.unqualified_name)
17140                == TEMPLATE_ID_EXPR)
17141         /* If the DECLARATOR has the form `X<y>' then it uses one
17142            additional level of template parameters.  */
17143         ++num_templates;
17144
17145       return cp_parser_check_template_parameters (parser,
17146                                                   num_templates,
17147                                                   declarator_location);
17148
17149     case cdk_function:
17150     case cdk_array:
17151     case cdk_pointer:
17152     case cdk_reference:
17153     case cdk_ptrmem:
17154       return (cp_parser_check_declarator_template_parameters
17155               (parser, declarator->declarator, declarator_location));
17156
17157     case cdk_error:
17158       return true;
17159
17160     default:
17161       gcc_unreachable ();
17162     }
17163   return false;
17164 }
17165
17166 /* NUM_TEMPLATES were used in the current declaration.  If that is
17167    invalid, return FALSE and issue an error messages.  Otherwise,
17168    return TRUE.  */
17169
17170 static bool
17171 cp_parser_check_template_parameters (cp_parser* parser,
17172                                      unsigned num_templates,
17173                                      location_t location)
17174 {
17175   /* If there are more template classes than parameter lists, we have
17176      something like:
17177
17178        template <class T> void S<T>::R<T>::f ();  */
17179   if (parser->num_template_parameter_lists < num_templates)
17180     {
17181       error ("%Htoo few template-parameter-lists", &location);
17182       return false;
17183     }
17184   /* If there are the same number of template classes and parameter
17185      lists, that's OK.  */
17186   if (parser->num_template_parameter_lists == num_templates)
17187     return true;
17188   /* If there are more, but only one more, then we are referring to a
17189      member template.  That's OK too.  */
17190   if (parser->num_template_parameter_lists == num_templates + 1)
17191       return true;
17192   /* Otherwise, there are too many template parameter lists.  We have
17193      something like:
17194
17195      template <class T> template <class U> void S::f();  */
17196   error ("%Htoo many template-parameter-lists", &location);
17197   return false;
17198 }
17199
17200 /* Parse an optional `::' token indicating that the following name is
17201    from the global namespace.  If so, PARSER->SCOPE is set to the
17202    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17203    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17204    Returns the new value of PARSER->SCOPE, if the `::' token is
17205    present, and NULL_TREE otherwise.  */
17206
17207 static tree
17208 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17209 {
17210   cp_token *token;
17211
17212   /* Peek at the next token.  */
17213   token = cp_lexer_peek_token (parser->lexer);
17214   /* If we're looking at a `::' token then we're starting from the
17215      global namespace, not our current location.  */
17216   if (token->type == CPP_SCOPE)
17217     {
17218       /* Consume the `::' token.  */
17219       cp_lexer_consume_token (parser->lexer);
17220       /* Set the SCOPE so that we know where to start the lookup.  */
17221       parser->scope = global_namespace;
17222       parser->qualifying_scope = global_namespace;
17223       parser->object_scope = NULL_TREE;
17224
17225       return parser->scope;
17226     }
17227   else if (!current_scope_valid_p)
17228     {
17229       parser->scope = NULL_TREE;
17230       parser->qualifying_scope = NULL_TREE;
17231       parser->object_scope = NULL_TREE;
17232     }
17233
17234   return NULL_TREE;
17235 }
17236
17237 /* Returns TRUE if the upcoming token sequence is the start of a
17238    constructor declarator.  If FRIEND_P is true, the declarator is
17239    preceded by the `friend' specifier.  */
17240
17241 static bool
17242 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17243 {
17244   bool constructor_p;
17245   tree type_decl = NULL_TREE;
17246   bool nested_name_p;
17247   cp_token *next_token;
17248
17249   /* The common case is that this is not a constructor declarator, so
17250      try to avoid doing lots of work if at all possible.  It's not
17251      valid declare a constructor at function scope.  */
17252   if (parser->in_function_body)
17253     return false;
17254   /* And only certain tokens can begin a constructor declarator.  */
17255   next_token = cp_lexer_peek_token (parser->lexer);
17256   if (next_token->type != CPP_NAME
17257       && next_token->type != CPP_SCOPE
17258       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17259       && next_token->type != CPP_TEMPLATE_ID)
17260     return false;
17261
17262   /* Parse tentatively; we are going to roll back all of the tokens
17263      consumed here.  */
17264   cp_parser_parse_tentatively (parser);
17265   /* Assume that we are looking at a constructor declarator.  */
17266   constructor_p = true;
17267
17268   /* Look for the optional `::' operator.  */
17269   cp_parser_global_scope_opt (parser,
17270                               /*current_scope_valid_p=*/false);
17271   /* Look for the nested-name-specifier.  */
17272   nested_name_p
17273     = (cp_parser_nested_name_specifier_opt (parser,
17274                                             /*typename_keyword_p=*/false,
17275                                             /*check_dependency_p=*/false,
17276                                             /*type_p=*/false,
17277                                             /*is_declaration=*/false)
17278        != NULL_TREE);
17279   /* Outside of a class-specifier, there must be a
17280      nested-name-specifier.  */
17281   if (!nested_name_p &&
17282       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17283        || friend_p))
17284     constructor_p = false;
17285   /* If we still think that this might be a constructor-declarator,
17286      look for a class-name.  */
17287   if (constructor_p)
17288     {
17289       /* If we have:
17290
17291            template <typename T> struct S { S(); };
17292            template <typename T> S<T>::S ();
17293
17294          we must recognize that the nested `S' names a class.
17295          Similarly, for:
17296
17297            template <typename T> S<T>::S<T> ();
17298
17299          we must recognize that the nested `S' names a template.  */
17300       type_decl = cp_parser_class_name (parser,
17301                                         /*typename_keyword_p=*/false,
17302                                         /*template_keyword_p=*/false,
17303                                         none_type,
17304                                         /*check_dependency_p=*/false,
17305                                         /*class_head_p=*/false,
17306                                         /*is_declaration=*/false);
17307       /* If there was no class-name, then this is not a constructor.  */
17308       constructor_p = !cp_parser_error_occurred (parser);
17309     }
17310
17311   /* If we're still considering a constructor, we have to see a `(',
17312      to begin the parameter-declaration-clause, followed by either a
17313      `)', an `...', or a decl-specifier.  We need to check for a
17314      type-specifier to avoid being fooled into thinking that:
17315
17316        S::S (f) (int);
17317
17318      is a constructor.  (It is actually a function named `f' that
17319      takes one parameter (of type `int') and returns a value of type
17320      `S::S'.  */
17321   if (constructor_p
17322       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17323     {
17324       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17325           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17326           /* A parameter declaration begins with a decl-specifier,
17327              which is either the "attribute" keyword, a storage class
17328              specifier, or (usually) a type-specifier.  */
17329           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17330         {
17331           tree type;
17332           tree pushed_scope = NULL_TREE;
17333           unsigned saved_num_template_parameter_lists;
17334
17335           /* Names appearing in the type-specifier should be looked up
17336              in the scope of the class.  */
17337           if (current_class_type)
17338             type = NULL_TREE;
17339           else
17340             {
17341               type = TREE_TYPE (type_decl);
17342               if (TREE_CODE (type) == TYPENAME_TYPE)
17343                 {
17344                   type = resolve_typename_type (type,
17345                                                 /*only_current_p=*/false);
17346                   if (TREE_CODE (type) == TYPENAME_TYPE)
17347                     {
17348                       cp_parser_abort_tentative_parse (parser);
17349                       return false;
17350                     }
17351                 }
17352               pushed_scope = push_scope (type);
17353             }
17354
17355           /* Inside the constructor parameter list, surrounding
17356              template-parameter-lists do not apply.  */
17357           saved_num_template_parameter_lists
17358             = parser->num_template_parameter_lists;
17359           parser->num_template_parameter_lists = 0;
17360
17361           /* Look for the type-specifier.  */
17362           cp_parser_type_specifier (parser,
17363                                     CP_PARSER_FLAGS_NONE,
17364                                     /*decl_specs=*/NULL,
17365                                     /*is_declarator=*/true,
17366                                     /*declares_class_or_enum=*/NULL,
17367                                     /*is_cv_qualifier=*/NULL);
17368
17369           parser->num_template_parameter_lists
17370             = saved_num_template_parameter_lists;
17371
17372           /* Leave the scope of the class.  */
17373           if (pushed_scope)
17374             pop_scope (pushed_scope);
17375
17376           constructor_p = !cp_parser_error_occurred (parser);
17377         }
17378     }
17379   else
17380     constructor_p = false;
17381   /* We did not really want to consume any tokens.  */
17382   cp_parser_abort_tentative_parse (parser);
17383
17384   return constructor_p;
17385 }
17386
17387 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17388    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17389    they must be performed once we are in the scope of the function.
17390
17391    Returns the function defined.  */
17392
17393 static tree
17394 cp_parser_function_definition_from_specifiers_and_declarator
17395   (cp_parser* parser,
17396    cp_decl_specifier_seq *decl_specifiers,
17397    tree attributes,
17398    const cp_declarator *declarator)
17399 {
17400   tree fn;
17401   bool success_p;
17402
17403   /* Begin the function-definition.  */
17404   success_p = start_function (decl_specifiers, declarator, attributes);
17405
17406   /* The things we're about to see are not directly qualified by any
17407      template headers we've seen thus far.  */
17408   reset_specialization ();
17409
17410   /* If there were names looked up in the decl-specifier-seq that we
17411      did not check, check them now.  We must wait until we are in the
17412      scope of the function to perform the checks, since the function
17413      might be a friend.  */
17414   perform_deferred_access_checks ();
17415
17416   if (!success_p)
17417     {
17418       /* Skip the entire function.  */
17419       cp_parser_skip_to_end_of_block_or_statement (parser);
17420       fn = error_mark_node;
17421     }
17422   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17423     {
17424       /* Seen already, skip it.  An error message has already been output.  */
17425       cp_parser_skip_to_end_of_block_or_statement (parser);
17426       fn = current_function_decl;
17427       current_function_decl = NULL_TREE;
17428       /* If this is a function from a class, pop the nested class.  */
17429       if (current_class_name)
17430         pop_nested_class ();
17431     }
17432   else
17433     fn = cp_parser_function_definition_after_declarator (parser,
17434                                                          /*inline_p=*/false);
17435
17436   return fn;
17437 }
17438
17439 /* Parse the part of a function-definition that follows the
17440    declarator.  INLINE_P is TRUE iff this function is an inline
17441    function defined with a class-specifier.
17442
17443    Returns the function defined.  */
17444
17445 static tree
17446 cp_parser_function_definition_after_declarator (cp_parser* parser,
17447                                                 bool inline_p)
17448 {
17449   tree fn;
17450   bool ctor_initializer_p = false;
17451   bool saved_in_unbraced_linkage_specification_p;
17452   bool saved_in_function_body;
17453   unsigned saved_num_template_parameter_lists;
17454   cp_token *token;
17455
17456   saved_in_function_body = parser->in_function_body;
17457   parser->in_function_body = true;
17458   /* If the next token is `return', then the code may be trying to
17459      make use of the "named return value" extension that G++ used to
17460      support.  */
17461   token = cp_lexer_peek_token (parser->lexer);
17462   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17463     {
17464       /* Consume the `return' keyword.  */
17465       cp_lexer_consume_token (parser->lexer);
17466       /* Look for the identifier that indicates what value is to be
17467          returned.  */
17468       cp_parser_identifier (parser);
17469       /* Issue an error message.  */
17470       error ("%Hnamed return values are no longer supported",
17471              &token->location);
17472       /* Skip tokens until we reach the start of the function body.  */
17473       while (true)
17474         {
17475           cp_token *token = cp_lexer_peek_token (parser->lexer);
17476           if (token->type == CPP_OPEN_BRACE
17477               || token->type == CPP_EOF
17478               || token->type == CPP_PRAGMA_EOL)
17479             break;
17480           cp_lexer_consume_token (parser->lexer);
17481         }
17482     }
17483   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17484      anything declared inside `f'.  */
17485   saved_in_unbraced_linkage_specification_p
17486     = parser->in_unbraced_linkage_specification_p;
17487   parser->in_unbraced_linkage_specification_p = false;
17488   /* Inside the function, surrounding template-parameter-lists do not
17489      apply.  */
17490   saved_num_template_parameter_lists
17491     = parser->num_template_parameter_lists;
17492   parser->num_template_parameter_lists = 0;
17493   /* If the next token is `try', then we are looking at a
17494      function-try-block.  */
17495   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17496     ctor_initializer_p = cp_parser_function_try_block (parser);
17497   /* A function-try-block includes the function-body, so we only do
17498      this next part if we're not processing a function-try-block.  */
17499   else
17500     ctor_initializer_p
17501       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17502
17503   /* Finish the function.  */
17504   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17505                         (inline_p ? 2 : 0));
17506   /* Generate code for it, if necessary.  */
17507   expand_or_defer_fn (fn);
17508   /* Restore the saved values.  */
17509   parser->in_unbraced_linkage_specification_p
17510     = saved_in_unbraced_linkage_specification_p;
17511   parser->num_template_parameter_lists
17512     = saved_num_template_parameter_lists;
17513   parser->in_function_body = saved_in_function_body;
17514
17515   return fn;
17516 }
17517
17518 /* Parse a template-declaration, assuming that the `export' (and
17519    `extern') keywords, if present, has already been scanned.  MEMBER_P
17520    is as for cp_parser_template_declaration.  */
17521
17522 static void
17523 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17524 {
17525   tree decl = NULL_TREE;
17526   VEC (deferred_access_check,gc) *checks;
17527   tree parameter_list;
17528   bool friend_p = false;
17529   bool need_lang_pop;
17530   cp_token *token;
17531
17532   /* Look for the `template' keyword.  */
17533   token = cp_lexer_peek_token (parser->lexer);
17534   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17535     return;
17536
17537   /* And the `<'.  */
17538   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17539     return;
17540   if (at_class_scope_p () && current_function_decl)
17541     {
17542       /* 14.5.2.2 [temp.mem]
17543
17544          A local class shall not have member templates.  */
17545       error ("%Hinvalid declaration of member template in local class",
17546              &token->location);
17547       cp_parser_skip_to_end_of_block_or_statement (parser);
17548       return;
17549     }
17550   /* [temp]
17551
17552      A template ... shall not have C linkage.  */
17553   if (current_lang_name == lang_name_c)
17554     {
17555       error ("%Htemplate with C linkage", &token->location);
17556       /* Give it C++ linkage to avoid confusing other parts of the
17557          front end.  */
17558       push_lang_context (lang_name_cplusplus);
17559       need_lang_pop = true;
17560     }
17561   else
17562     need_lang_pop = false;
17563
17564   /* We cannot perform access checks on the template parameter
17565      declarations until we know what is being declared, just as we
17566      cannot check the decl-specifier list.  */
17567   push_deferring_access_checks (dk_deferred);
17568
17569   /* If the next token is `>', then we have an invalid
17570      specialization.  Rather than complain about an invalid template
17571      parameter, issue an error message here.  */
17572   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17573     {
17574       cp_parser_error (parser, "invalid explicit specialization");
17575       begin_specialization ();
17576       parameter_list = NULL_TREE;
17577     }
17578   else
17579     /* Parse the template parameters.  */
17580     parameter_list = cp_parser_template_parameter_list (parser);
17581
17582   /* Get the deferred access checks from the parameter list.  These
17583      will be checked once we know what is being declared, as for a
17584      member template the checks must be performed in the scope of the
17585      class containing the member.  */
17586   checks = get_deferred_access_checks ();
17587
17588   /* Look for the `>'.  */
17589   cp_parser_skip_to_end_of_template_parameter_list (parser);
17590   /* We just processed one more parameter list.  */
17591   ++parser->num_template_parameter_lists;
17592   /* If the next token is `template', there are more template
17593      parameters.  */
17594   if (cp_lexer_next_token_is_keyword (parser->lexer,
17595                                       RID_TEMPLATE))
17596     cp_parser_template_declaration_after_export (parser, member_p);
17597   else
17598     {
17599       /* There are no access checks when parsing a template, as we do not
17600          know if a specialization will be a friend.  */
17601       push_deferring_access_checks (dk_no_check);
17602       token = cp_lexer_peek_token (parser->lexer);
17603       decl = cp_parser_single_declaration (parser,
17604                                            checks,
17605                                            member_p,
17606                                            /*explicit_specialization_p=*/false,
17607                                            &friend_p);
17608       pop_deferring_access_checks ();
17609
17610       /* If this is a member template declaration, let the front
17611          end know.  */
17612       if (member_p && !friend_p && decl)
17613         {
17614           if (TREE_CODE (decl) == TYPE_DECL)
17615             cp_parser_check_access_in_redeclaration (decl, token->location);
17616
17617           decl = finish_member_template_decl (decl);
17618         }
17619       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17620         make_friend_class (current_class_type, TREE_TYPE (decl),
17621                            /*complain=*/true);
17622     }
17623   /* We are done with the current parameter list.  */
17624   --parser->num_template_parameter_lists;
17625
17626   pop_deferring_access_checks ();
17627
17628   /* Finish up.  */
17629   finish_template_decl (parameter_list);
17630
17631   /* Register member declarations.  */
17632   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17633     finish_member_declaration (decl);
17634   /* For the erroneous case of a template with C linkage, we pushed an
17635      implicit C++ linkage scope; exit that scope now.  */
17636   if (need_lang_pop)
17637     pop_lang_context ();
17638   /* If DECL is a function template, we must return to parse it later.
17639      (Even though there is no definition, there might be default
17640      arguments that need handling.)  */
17641   if (member_p && decl
17642       && (TREE_CODE (decl) == FUNCTION_DECL
17643           || DECL_FUNCTION_TEMPLATE_P (decl)))
17644     TREE_VALUE (parser->unparsed_functions_queues)
17645       = tree_cons (NULL_TREE, decl,
17646                    TREE_VALUE (parser->unparsed_functions_queues));
17647 }
17648
17649 /* Perform the deferred access checks from a template-parameter-list.
17650    CHECKS is a TREE_LIST of access checks, as returned by
17651    get_deferred_access_checks.  */
17652
17653 static void
17654 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17655 {
17656   ++processing_template_parmlist;
17657   perform_access_checks (checks);
17658   --processing_template_parmlist;
17659 }
17660
17661 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17662    `function-definition' sequence.  MEMBER_P is true, this declaration
17663    appears in a class scope.
17664
17665    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17666    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17667
17668 static tree
17669 cp_parser_single_declaration (cp_parser* parser,
17670                               VEC (deferred_access_check,gc)* checks,
17671                               bool member_p,
17672                               bool explicit_specialization_p,
17673                               bool* friend_p)
17674 {
17675   int declares_class_or_enum;
17676   tree decl = NULL_TREE;
17677   cp_decl_specifier_seq decl_specifiers;
17678   bool function_definition_p = false;
17679   cp_token *decl_spec_token_start;
17680
17681   /* This function is only used when processing a template
17682      declaration.  */
17683   gcc_assert (innermost_scope_kind () == sk_template_parms
17684               || innermost_scope_kind () == sk_template_spec);
17685
17686   /* Defer access checks until we know what is being declared.  */
17687   push_deferring_access_checks (dk_deferred);
17688
17689   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17690      alternative.  */
17691   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17692   cp_parser_decl_specifier_seq (parser,
17693                                 CP_PARSER_FLAGS_OPTIONAL,
17694                                 &decl_specifiers,
17695                                 &declares_class_or_enum);
17696   if (friend_p)
17697     *friend_p = cp_parser_friend_p (&decl_specifiers);
17698
17699   /* There are no template typedefs.  */
17700   if (decl_specifiers.specs[(int) ds_typedef])
17701     {
17702       error ("%Htemplate declaration of %qs",
17703              &decl_spec_token_start->location, "typedef");
17704       decl = error_mark_node;
17705     }
17706
17707   /* Gather up the access checks that occurred the
17708      decl-specifier-seq.  */
17709   stop_deferring_access_checks ();
17710
17711   /* Check for the declaration of a template class.  */
17712   if (declares_class_or_enum)
17713     {
17714       if (cp_parser_declares_only_class_p (parser))
17715         {
17716           decl = shadow_tag (&decl_specifiers);
17717
17718           /* In this case:
17719
17720                struct C {
17721                  friend template <typename T> struct A<T>::B;
17722                };
17723
17724              A<T>::B will be represented by a TYPENAME_TYPE, and
17725              therefore not recognized by shadow_tag.  */
17726           if (friend_p && *friend_p
17727               && !decl
17728               && decl_specifiers.type
17729               && TYPE_P (decl_specifiers.type))
17730             decl = decl_specifiers.type;
17731
17732           if (decl && decl != error_mark_node)
17733             decl = TYPE_NAME (decl);
17734           else
17735             decl = error_mark_node;
17736
17737           /* Perform access checks for template parameters.  */
17738           cp_parser_perform_template_parameter_access_checks (checks);
17739         }
17740     }
17741   /* If it's not a template class, try for a template function.  If
17742      the next token is a `;', then this declaration does not declare
17743      anything.  But, if there were errors in the decl-specifiers, then
17744      the error might well have come from an attempted class-specifier.
17745      In that case, there's no need to warn about a missing declarator.  */
17746   if (!decl
17747       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17748           || decl_specifiers.type != error_mark_node))
17749     {
17750       decl = cp_parser_init_declarator (parser,
17751                                         &decl_specifiers,
17752                                         checks,
17753                                         /*function_definition_allowed_p=*/true,
17754                                         member_p,
17755                                         declares_class_or_enum,
17756                                         &function_definition_p);
17757
17758     /* 7.1.1-1 [dcl.stc]
17759
17760        A storage-class-specifier shall not be specified in an explicit
17761        specialization...  */
17762     if (decl
17763         && explicit_specialization_p
17764         && decl_specifiers.storage_class != sc_none)
17765       {
17766         error ("%Hexplicit template specialization cannot have a storage class",
17767                &decl_spec_token_start->location);
17768         decl = error_mark_node;
17769       }
17770     }
17771
17772   pop_deferring_access_checks ();
17773
17774   /* Clear any current qualification; whatever comes next is the start
17775      of something new.  */
17776   parser->scope = NULL_TREE;
17777   parser->qualifying_scope = NULL_TREE;
17778   parser->object_scope = NULL_TREE;
17779   /* Look for a trailing `;' after the declaration.  */
17780   if (!function_definition_p
17781       && (decl == error_mark_node
17782           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17783     cp_parser_skip_to_end_of_block_or_statement (parser);
17784
17785   return decl;
17786 }
17787
17788 /* Parse a cast-expression that is not the operand of a unary "&".  */
17789
17790 static tree
17791 cp_parser_simple_cast_expression (cp_parser *parser)
17792 {
17793   return cp_parser_cast_expression (parser, /*address_p=*/false,
17794                                     /*cast_p=*/false);
17795 }
17796
17797 /* Parse a functional cast to TYPE.  Returns an expression
17798    representing the cast.  */
17799
17800 static tree
17801 cp_parser_functional_cast (cp_parser* parser, tree type)
17802 {
17803   tree expression_list;
17804   tree cast;
17805   bool nonconst_p;
17806
17807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17808     {
17809       maybe_warn_cpp0x ("extended initializer lists");
17810       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17811       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17812       if (TREE_CODE (type) == TYPE_DECL)
17813         type = TREE_TYPE (type);
17814       return finish_compound_literal (type, expression_list);
17815     }
17816
17817   expression_list
17818     = cp_parser_parenthesized_expression_list (parser, false,
17819                                                /*cast_p=*/true,
17820                                                /*allow_expansion_p=*/true,
17821                                                /*non_constant_p=*/NULL);
17822
17823   cast = build_functional_cast (type, expression_list,
17824                                 tf_warning_or_error);
17825   /* [expr.const]/1: In an integral constant expression "only type
17826      conversions to integral or enumeration type can be used".  */
17827   if (TREE_CODE (type) == TYPE_DECL)
17828     type = TREE_TYPE (type);
17829   if (cast != error_mark_node
17830       && !cast_valid_in_integral_constant_expression_p (type)
17831       && (cp_parser_non_integral_constant_expression
17832           (parser, "a call to a constructor")))
17833     return error_mark_node;
17834   return cast;
17835 }
17836
17837 /* Save the tokens that make up the body of a member function defined
17838    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17839    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17840    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17841    for the member function.  */
17842
17843 static tree
17844 cp_parser_save_member_function_body (cp_parser* parser,
17845                                      cp_decl_specifier_seq *decl_specifiers,
17846                                      cp_declarator *declarator,
17847                                      tree attributes)
17848 {
17849   cp_token *first;
17850   cp_token *last;
17851   tree fn;
17852
17853   /* Create the function-declaration.  */
17854   fn = start_method (decl_specifiers, declarator, attributes);
17855   /* If something went badly wrong, bail out now.  */
17856   if (fn == error_mark_node)
17857     {
17858       /* If there's a function-body, skip it.  */
17859       if (cp_parser_token_starts_function_definition_p
17860           (cp_lexer_peek_token (parser->lexer)))
17861         cp_parser_skip_to_end_of_block_or_statement (parser);
17862       return error_mark_node;
17863     }
17864
17865   /* Remember it, if there default args to post process.  */
17866   cp_parser_save_default_args (parser, fn);
17867
17868   /* Save away the tokens that make up the body of the
17869      function.  */
17870   first = parser->lexer->next_token;
17871   /* We can have braced-init-list mem-initializers before the fn body.  */
17872   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17873     {
17874       cp_lexer_consume_token (parser->lexer);
17875       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17876              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17877         {
17878           /* cache_group will stop after an un-nested { } pair, too.  */
17879           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17880             break;
17881
17882           /* variadic mem-inits have ... after the ')'.  */
17883           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17884             cp_lexer_consume_token (parser->lexer);
17885         }
17886     }
17887   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17888   /* Handle function try blocks.  */
17889   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17890     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17891   last = parser->lexer->next_token;
17892
17893   /* Save away the inline definition; we will process it when the
17894      class is complete.  */
17895   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17896   DECL_PENDING_INLINE_P (fn) = 1;
17897
17898   /* We need to know that this was defined in the class, so that
17899      friend templates are handled correctly.  */
17900   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17901
17902   /* We're done with the inline definition.  */
17903   finish_method (fn);
17904
17905   /* Add FN to the queue of functions to be parsed later.  */
17906   TREE_VALUE (parser->unparsed_functions_queues)
17907     = tree_cons (NULL_TREE, fn,
17908                  TREE_VALUE (parser->unparsed_functions_queues));
17909
17910   return fn;
17911 }
17912
17913 /* Parse a template-argument-list, as well as the trailing ">" (but
17914    not the opening ">").  See cp_parser_template_argument_list for the
17915    return value.  */
17916
17917 static tree
17918 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17919 {
17920   tree arguments;
17921   tree saved_scope;
17922   tree saved_qualifying_scope;
17923   tree saved_object_scope;
17924   bool saved_greater_than_is_operator_p;
17925   bool saved_skip_evaluation;
17926
17927   /* [temp.names]
17928
17929      When parsing a template-id, the first non-nested `>' is taken as
17930      the end of the template-argument-list rather than a greater-than
17931      operator.  */
17932   saved_greater_than_is_operator_p
17933     = parser->greater_than_is_operator_p;
17934   parser->greater_than_is_operator_p = false;
17935   /* Parsing the argument list may modify SCOPE, so we save it
17936      here.  */
17937   saved_scope = parser->scope;
17938   saved_qualifying_scope = parser->qualifying_scope;
17939   saved_object_scope = parser->object_scope;
17940   /* We need to evaluate the template arguments, even though this
17941      template-id may be nested within a "sizeof".  */
17942   saved_skip_evaluation = skip_evaluation;
17943   skip_evaluation = false;
17944   /* Parse the template-argument-list itself.  */
17945   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17946       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17947     arguments = NULL_TREE;
17948   else
17949     arguments = cp_parser_template_argument_list (parser);
17950   /* Look for the `>' that ends the template-argument-list. If we find
17951      a '>>' instead, it's probably just a typo.  */
17952   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17953     {
17954       if (cxx_dialect != cxx98)
17955         {
17956           /* In C++0x, a `>>' in a template argument list or cast
17957              expression is considered to be two separate `>'
17958              tokens. So, change the current token to a `>', but don't
17959              consume it: it will be consumed later when the outer
17960              template argument list (or cast expression) is parsed.
17961              Note that this replacement of `>' for `>>' is necessary
17962              even if we are parsing tentatively: in the tentative
17963              case, after calling
17964              cp_parser_enclosed_template_argument_list we will always
17965              throw away all of the template arguments and the first
17966              closing `>', either because the template argument list
17967              was erroneous or because we are replacing those tokens
17968              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17969              not have been thrown away) is needed either to close an
17970              outer template argument list or to complete a new-style
17971              cast.  */
17972           cp_token *token = cp_lexer_peek_token (parser->lexer);
17973           token->type = CPP_GREATER;
17974         }
17975       else if (!saved_greater_than_is_operator_p)
17976         {
17977           /* If we're in a nested template argument list, the '>>' has
17978             to be a typo for '> >'. We emit the error message, but we
17979             continue parsing and we push a '>' as next token, so that
17980             the argument list will be parsed correctly.  Note that the
17981             global source location is still on the token before the
17982             '>>', so we need to say explicitly where we want it.  */
17983           cp_token *token = cp_lexer_peek_token (parser->lexer);
17984           error ("%H%<>>%> should be %<> >%> "
17985                  "within a nested template argument list",
17986                  &token->location);
17987
17988           token->type = CPP_GREATER;
17989         }
17990       else
17991         {
17992           /* If this is not a nested template argument list, the '>>'
17993             is a typo for '>'. Emit an error message and continue.
17994             Same deal about the token location, but here we can get it
17995             right by consuming the '>>' before issuing the diagnostic.  */
17996           cp_token *token = cp_lexer_consume_token (parser->lexer);
17997           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17998                  "a template argument list", &token->location);
17999         }
18000     }
18001   else
18002     cp_parser_skip_to_end_of_template_parameter_list (parser);
18003   /* The `>' token might be a greater-than operator again now.  */
18004   parser->greater_than_is_operator_p
18005     = saved_greater_than_is_operator_p;
18006   /* Restore the SAVED_SCOPE.  */
18007   parser->scope = saved_scope;
18008   parser->qualifying_scope = saved_qualifying_scope;
18009   parser->object_scope = saved_object_scope;
18010   skip_evaluation = saved_skip_evaluation;
18011
18012   return arguments;
18013 }
18014
18015 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18016    arguments, or the body of the function have not yet been parsed,
18017    parse them now.  */
18018
18019 static void
18020 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18021 {
18022   /* If this member is a template, get the underlying
18023      FUNCTION_DECL.  */
18024   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18025     member_function = DECL_TEMPLATE_RESULT (member_function);
18026
18027   /* There should not be any class definitions in progress at this
18028      point; the bodies of members are only parsed outside of all class
18029      definitions.  */
18030   gcc_assert (parser->num_classes_being_defined == 0);
18031   /* While we're parsing the member functions we might encounter more
18032      classes.  We want to handle them right away, but we don't want
18033      them getting mixed up with functions that are currently in the
18034      queue.  */
18035   parser->unparsed_functions_queues
18036     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18037
18038   /* Make sure that any template parameters are in scope.  */
18039   maybe_begin_member_template_processing (member_function);
18040
18041   /* If the body of the function has not yet been parsed, parse it
18042      now.  */
18043   if (DECL_PENDING_INLINE_P (member_function))
18044     {
18045       tree function_scope;
18046       cp_token_cache *tokens;
18047
18048       /* The function is no longer pending; we are processing it.  */
18049       tokens = DECL_PENDING_INLINE_INFO (member_function);
18050       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18051       DECL_PENDING_INLINE_P (member_function) = 0;
18052
18053       /* If this is a local class, enter the scope of the containing
18054          function.  */
18055       function_scope = current_function_decl;
18056       if (function_scope)
18057         push_function_context ();
18058
18059       /* Push the body of the function onto the lexer stack.  */
18060       cp_parser_push_lexer_for_tokens (parser, tokens);
18061
18062       /* Let the front end know that we going to be defining this
18063          function.  */
18064       start_preparsed_function (member_function, NULL_TREE,
18065                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18066
18067       /* Don't do access checking if it is a templated function.  */
18068       if (processing_template_decl)
18069         push_deferring_access_checks (dk_no_check);
18070
18071       /* Now, parse the body of the function.  */
18072       cp_parser_function_definition_after_declarator (parser,
18073                                                       /*inline_p=*/true);
18074
18075       if (processing_template_decl)
18076         pop_deferring_access_checks ();
18077
18078       /* Leave the scope of the containing function.  */
18079       if (function_scope)
18080         pop_function_context ();
18081       cp_parser_pop_lexer (parser);
18082     }
18083
18084   /* Remove any template parameters from the symbol table.  */
18085   maybe_end_member_template_processing ();
18086
18087   /* Restore the queue.  */
18088   parser->unparsed_functions_queues
18089     = TREE_CHAIN (parser->unparsed_functions_queues);
18090 }
18091
18092 /* If DECL contains any default args, remember it on the unparsed
18093    functions queue.  */
18094
18095 static void
18096 cp_parser_save_default_args (cp_parser* parser, tree decl)
18097 {
18098   tree probe;
18099
18100   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18101        probe;
18102        probe = TREE_CHAIN (probe))
18103     if (TREE_PURPOSE (probe))
18104       {
18105         TREE_PURPOSE (parser->unparsed_functions_queues)
18106           = tree_cons (current_class_type, decl,
18107                        TREE_PURPOSE (parser->unparsed_functions_queues));
18108         break;
18109       }
18110 }
18111
18112 /* FN is a FUNCTION_DECL which may contains a parameter with an
18113    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18114    assumes that the current scope is the scope in which the default
18115    argument should be processed.  */
18116
18117 static void
18118 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18119 {
18120   bool saved_local_variables_forbidden_p;
18121   tree parm;
18122
18123   /* While we're parsing the default args, we might (due to the
18124      statement expression extension) encounter more classes.  We want
18125      to handle them right away, but we don't want them getting mixed
18126      up with default args that are currently in the queue.  */
18127   parser->unparsed_functions_queues
18128     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18129
18130   /* Local variable names (and the `this' keyword) may not appear
18131      in a default argument.  */
18132   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18133   parser->local_variables_forbidden_p = true;
18134
18135   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18136        parm;
18137        parm = TREE_CHAIN (parm))
18138     {
18139       cp_token_cache *tokens;
18140       tree default_arg = TREE_PURPOSE (parm);
18141       tree parsed_arg;
18142       VEC(tree,gc) *insts;
18143       tree copy;
18144       unsigned ix;
18145
18146       if (!default_arg)
18147         continue;
18148
18149       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18150         /* This can happen for a friend declaration for a function
18151            already declared with default arguments.  */
18152         continue;
18153
18154        /* Push the saved tokens for the default argument onto the parser's
18155           lexer stack.  */
18156       tokens = DEFARG_TOKENS (default_arg);
18157       cp_parser_push_lexer_for_tokens (parser, tokens);
18158
18159       /* Parse the assignment-expression.  */
18160       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18161
18162       if (!processing_template_decl)
18163         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18164
18165       TREE_PURPOSE (parm) = parsed_arg;
18166
18167       /* Update any instantiations we've already created.  */
18168       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18169            VEC_iterate (tree, insts, ix, copy); ix++)
18170         TREE_PURPOSE (copy) = parsed_arg;
18171
18172       /* If the token stream has not been completely used up, then
18173          there was extra junk after the end of the default
18174          argument.  */
18175       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18176         cp_parser_error (parser, "expected %<,%>");
18177
18178       /* Revert to the main lexer.  */
18179       cp_parser_pop_lexer (parser);
18180     }
18181
18182   /* Make sure no default arg is missing.  */
18183   check_default_args (fn);
18184
18185   /* Restore the state of local_variables_forbidden_p.  */
18186   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18187
18188   /* Restore the queue.  */
18189   parser->unparsed_functions_queues
18190     = TREE_CHAIN (parser->unparsed_functions_queues);
18191 }
18192
18193 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18194    either a TYPE or an expression, depending on the form of the
18195    input.  The KEYWORD indicates which kind of expression we have
18196    encountered.  */
18197
18198 static tree
18199 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18200 {
18201   tree expr = NULL_TREE;
18202   const char *saved_message;
18203   char *tmp;
18204   bool saved_integral_constant_expression_p;
18205   bool saved_non_integral_constant_expression_p;
18206   bool pack_expansion_p = false;
18207
18208   /* Types cannot be defined in a `sizeof' expression.  Save away the
18209      old message.  */
18210   saved_message = parser->type_definition_forbidden_message;
18211   /* And create the new one.  */
18212   tmp = concat ("types may not be defined in %<",
18213                 IDENTIFIER_POINTER (ridpointers[keyword]),
18214                 "%> expressions", NULL);
18215   parser->type_definition_forbidden_message = tmp;
18216
18217   /* The restrictions on constant-expressions do not apply inside
18218      sizeof expressions.  */
18219   saved_integral_constant_expression_p
18220     = parser->integral_constant_expression_p;
18221   saved_non_integral_constant_expression_p
18222     = parser->non_integral_constant_expression_p;
18223   parser->integral_constant_expression_p = false;
18224
18225   /* If it's a `...', then we are computing the length of a parameter
18226      pack.  */
18227   if (keyword == RID_SIZEOF
18228       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18229     {
18230       /* Consume the `...'.  */
18231       cp_lexer_consume_token (parser->lexer);
18232       maybe_warn_variadic_templates ();
18233
18234       /* Note that this is an expansion.  */
18235       pack_expansion_p = true;
18236     }
18237
18238   /* Do not actually evaluate the expression.  */
18239   ++skip_evaluation;
18240   /* If it's a `(', then we might be looking at the type-id
18241      construction.  */
18242   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18243     {
18244       tree type;
18245       bool saved_in_type_id_in_expr_p;
18246
18247       /* We can't be sure yet whether we're looking at a type-id or an
18248          expression.  */
18249       cp_parser_parse_tentatively (parser);
18250       /* Consume the `('.  */
18251       cp_lexer_consume_token (parser->lexer);
18252       /* Parse the type-id.  */
18253       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18254       parser->in_type_id_in_expr_p = true;
18255       type = cp_parser_type_id (parser);
18256       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18257       /* Now, look for the trailing `)'.  */
18258       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18259       /* If all went well, then we're done.  */
18260       if (cp_parser_parse_definitely (parser))
18261         {
18262           cp_decl_specifier_seq decl_specs;
18263
18264           /* Build a trivial decl-specifier-seq.  */
18265           clear_decl_specs (&decl_specs);
18266           decl_specs.type = type;
18267
18268           /* Call grokdeclarator to figure out what type this is.  */
18269           expr = grokdeclarator (NULL,
18270                                  &decl_specs,
18271                                  TYPENAME,
18272                                  /*initialized=*/0,
18273                                  /*attrlist=*/NULL);
18274         }
18275     }
18276
18277   /* If the type-id production did not work out, then we must be
18278      looking at the unary-expression production.  */
18279   if (!expr)
18280     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18281                                        /*cast_p=*/false);
18282
18283   if (pack_expansion_p)
18284     /* Build a pack expansion. */
18285     expr = make_pack_expansion (expr);
18286
18287   /* Go back to evaluating expressions.  */
18288   --skip_evaluation;
18289
18290   /* Free the message we created.  */
18291   free (tmp);
18292   /* And restore the old one.  */
18293   parser->type_definition_forbidden_message = saved_message;
18294   parser->integral_constant_expression_p
18295     = saved_integral_constant_expression_p;
18296   parser->non_integral_constant_expression_p
18297     = saved_non_integral_constant_expression_p;
18298
18299   return expr;
18300 }
18301
18302 /* If the current declaration has no declarator, return true.  */
18303
18304 static bool
18305 cp_parser_declares_only_class_p (cp_parser *parser)
18306 {
18307   /* If the next token is a `;' or a `,' then there is no
18308      declarator.  */
18309   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18310           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18311 }
18312
18313 /* Update the DECL_SPECS to reflect the storage class indicated by
18314    KEYWORD.  */
18315
18316 static void
18317 cp_parser_set_storage_class (cp_parser *parser,
18318                              cp_decl_specifier_seq *decl_specs,
18319                              enum rid keyword,
18320                              location_t location)
18321 {
18322   cp_storage_class storage_class;
18323
18324   if (parser->in_unbraced_linkage_specification_p)
18325     {
18326       error ("%Hinvalid use of %qD in linkage specification",
18327              &location, ridpointers[keyword]);
18328       return;
18329     }
18330   else if (decl_specs->storage_class != sc_none)
18331     {
18332       decl_specs->conflicting_specifiers_p = true;
18333       return;
18334     }
18335
18336   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18337       && decl_specs->specs[(int) ds_thread])
18338     {
18339       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18340       decl_specs->specs[(int) ds_thread] = 0;
18341     }
18342
18343   switch (keyword)
18344     {
18345     case RID_AUTO:
18346       storage_class = sc_auto;
18347       break;
18348     case RID_REGISTER:
18349       storage_class = sc_register;
18350       break;
18351     case RID_STATIC:
18352       storage_class = sc_static;
18353       break;
18354     case RID_EXTERN:
18355       storage_class = sc_extern;
18356       break;
18357     case RID_MUTABLE:
18358       storage_class = sc_mutable;
18359       break;
18360     default:
18361       gcc_unreachable ();
18362     }
18363   decl_specs->storage_class = storage_class;
18364
18365   /* A storage class specifier cannot be applied alongside a typedef 
18366      specifier. If there is a typedef specifier present then set 
18367      conflicting_specifiers_p which will trigger an error later
18368      on in grokdeclarator. */
18369   if (decl_specs->specs[(int)ds_typedef])
18370     decl_specs->conflicting_specifiers_p = true;
18371 }
18372
18373 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18374    is true, the type is a user-defined type; otherwise it is a
18375    built-in type specified by a keyword.  */
18376
18377 static void
18378 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18379                               tree type_spec,
18380                               location_t location,
18381                               bool user_defined_p)
18382 {
18383   decl_specs->any_specifiers_p = true;
18384
18385   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18386      (with, for example, in "typedef int wchar_t;") we remember that
18387      this is what happened.  In system headers, we ignore these
18388      declarations so that G++ can work with system headers that are not
18389      C++-safe.  */
18390   if (decl_specs->specs[(int) ds_typedef]
18391       && !user_defined_p
18392       && (type_spec == boolean_type_node
18393           || type_spec == char16_type_node
18394           || type_spec == char32_type_node
18395           || type_spec == wchar_type_node)
18396       && (decl_specs->type
18397           || decl_specs->specs[(int) ds_long]
18398           || decl_specs->specs[(int) ds_short]
18399           || decl_specs->specs[(int) ds_unsigned]
18400           || decl_specs->specs[(int) ds_signed]))
18401     {
18402       decl_specs->redefined_builtin_type = type_spec;
18403       if (!decl_specs->type)
18404         {
18405           decl_specs->type = type_spec;
18406           decl_specs->user_defined_type_p = false;
18407           decl_specs->type_location = location;
18408         }
18409     }
18410   else if (decl_specs->type)
18411     decl_specs->multiple_types_p = true;
18412   else
18413     {
18414       decl_specs->type = type_spec;
18415       decl_specs->user_defined_type_p = user_defined_p;
18416       decl_specs->redefined_builtin_type = NULL_TREE;
18417       decl_specs->type_location = location;
18418     }
18419 }
18420
18421 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18422    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18423
18424 static bool
18425 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18426 {
18427   return decl_specifiers->specs[(int) ds_friend] != 0;
18428 }
18429
18430 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18431    issue an error message indicating that TOKEN_DESC was expected.
18432
18433    Returns the token consumed, if the token had the appropriate type.
18434    Otherwise, returns NULL.  */
18435
18436 static cp_token *
18437 cp_parser_require (cp_parser* parser,
18438                    enum cpp_ttype type,
18439                    const char* token_desc)
18440 {
18441   if (cp_lexer_next_token_is (parser->lexer, type))
18442     return cp_lexer_consume_token (parser->lexer);
18443   else
18444     {
18445       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18446       if (!cp_parser_simulate_error (parser))
18447         {
18448           char *message = concat ("expected ", token_desc, NULL);
18449           cp_parser_error (parser, message);
18450           free (message);
18451         }
18452       return NULL;
18453     }
18454 }
18455
18456 /* An error message is produced if the next token is not '>'.
18457    All further tokens are skipped until the desired token is
18458    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18459
18460 static void
18461 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18462 {
18463   /* Current level of '< ... >'.  */
18464   unsigned level = 0;
18465   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18466   unsigned nesting_depth = 0;
18467
18468   /* Are we ready, yet?  If not, issue error message.  */
18469   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18470     return;
18471
18472   /* Skip tokens until the desired token is found.  */
18473   while (true)
18474     {
18475       /* Peek at the next token.  */
18476       switch (cp_lexer_peek_token (parser->lexer)->type)
18477         {
18478         case CPP_LESS:
18479           if (!nesting_depth)
18480             ++level;
18481           break;
18482
18483         case CPP_RSHIFT:
18484           if (cxx_dialect == cxx98)
18485             /* C++0x views the `>>' operator as two `>' tokens, but
18486                C++98 does not. */
18487             break;
18488           else if (!nesting_depth && level-- == 0)
18489             {
18490               /* We've hit a `>>' where the first `>' closes the
18491                  template argument list, and the second `>' is
18492                  spurious.  Just consume the `>>' and stop; we've
18493                  already produced at least one error.  */
18494               cp_lexer_consume_token (parser->lexer);
18495               return;
18496             }
18497           /* Fall through for C++0x, so we handle the second `>' in
18498              the `>>'.  */
18499
18500         case CPP_GREATER:
18501           if (!nesting_depth && level-- == 0)
18502             {
18503               /* We've reached the token we want, consume it and stop.  */
18504               cp_lexer_consume_token (parser->lexer);
18505               return;
18506             }
18507           break;
18508
18509         case CPP_OPEN_PAREN:
18510         case CPP_OPEN_SQUARE:
18511           ++nesting_depth;
18512           break;
18513
18514         case CPP_CLOSE_PAREN:
18515         case CPP_CLOSE_SQUARE:
18516           if (nesting_depth-- == 0)
18517             return;
18518           break;
18519
18520         case CPP_EOF:
18521         case CPP_PRAGMA_EOL:
18522         case CPP_SEMICOLON:
18523         case CPP_OPEN_BRACE:
18524         case CPP_CLOSE_BRACE:
18525           /* The '>' was probably forgotten, don't look further.  */
18526           return;
18527
18528         default:
18529           break;
18530         }
18531
18532       /* Consume this token.  */
18533       cp_lexer_consume_token (parser->lexer);
18534     }
18535 }
18536
18537 /* If the next token is the indicated keyword, consume it.  Otherwise,
18538    issue an error message indicating that TOKEN_DESC was expected.
18539
18540    Returns the token consumed, if the token had the appropriate type.
18541    Otherwise, returns NULL.  */
18542
18543 static cp_token *
18544 cp_parser_require_keyword (cp_parser* parser,
18545                            enum rid keyword,
18546                            const char* token_desc)
18547 {
18548   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18549
18550   if (token && token->keyword != keyword)
18551     {
18552       dyn_string_t error_msg;
18553
18554       /* Format the error message.  */
18555       error_msg = dyn_string_new (0);
18556       dyn_string_append_cstr (error_msg, "expected ");
18557       dyn_string_append_cstr (error_msg, token_desc);
18558       cp_parser_error (parser, error_msg->s);
18559       dyn_string_delete (error_msg);
18560       return NULL;
18561     }
18562
18563   return token;
18564 }
18565
18566 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18567    function-definition.  */
18568
18569 static bool
18570 cp_parser_token_starts_function_definition_p (cp_token* token)
18571 {
18572   return (/* An ordinary function-body begins with an `{'.  */
18573           token->type == CPP_OPEN_BRACE
18574           /* A ctor-initializer begins with a `:'.  */
18575           || token->type == CPP_COLON
18576           /* A function-try-block begins with `try'.  */
18577           || token->keyword == RID_TRY
18578           /* The named return value extension begins with `return'.  */
18579           || token->keyword == RID_RETURN);
18580 }
18581
18582 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18583    definition.  */
18584
18585 static bool
18586 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18587 {
18588   cp_token *token;
18589
18590   token = cp_lexer_peek_token (parser->lexer);
18591   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18592 }
18593
18594 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18595    C++0x) ending a template-argument.  */
18596
18597 static bool
18598 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18599 {
18600   cp_token *token;
18601
18602   token = cp_lexer_peek_token (parser->lexer);
18603   return (token->type == CPP_COMMA 
18604           || token->type == CPP_GREATER
18605           || token->type == CPP_ELLIPSIS
18606           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18607 }
18608
18609 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18610    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18611
18612 static bool
18613 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18614                                                      size_t n)
18615 {
18616   cp_token *token;
18617
18618   token = cp_lexer_peek_nth_token (parser->lexer, n);
18619   if (token->type == CPP_LESS)
18620     return true;
18621   /* Check for the sequence `<::' in the original code. It would be lexed as
18622      `[:', where `[' is a digraph, and there is no whitespace before
18623      `:'.  */
18624   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18625     {
18626       cp_token *token2;
18627       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18628       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18629         return true;
18630     }
18631   return false;
18632 }
18633
18634 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18635    or none_type otherwise.  */
18636
18637 static enum tag_types
18638 cp_parser_token_is_class_key (cp_token* token)
18639 {
18640   switch (token->keyword)
18641     {
18642     case RID_CLASS:
18643       return class_type;
18644     case RID_STRUCT:
18645       return record_type;
18646     case RID_UNION:
18647       return union_type;
18648
18649     default:
18650       return none_type;
18651     }
18652 }
18653
18654 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18655
18656 static void
18657 cp_parser_check_class_key (enum tag_types class_key, tree type)
18658 {
18659   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18660     permerror (input_location, "%qs tag used in naming %q#T",
18661             class_key == union_type ? "union"
18662              : class_key == record_type ? "struct" : "class",
18663              type);
18664 }
18665
18666 /* Issue an error message if DECL is redeclared with different
18667    access than its original declaration [class.access.spec/3].
18668    This applies to nested classes and nested class templates.
18669    [class.mem/1].  */
18670
18671 static void
18672 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18673 {
18674   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18675     return;
18676
18677   if ((TREE_PRIVATE (decl)
18678        != (current_access_specifier == access_private_node))
18679       || (TREE_PROTECTED (decl)
18680           != (current_access_specifier == access_protected_node)))
18681     error ("%H%qD redeclared with different access", &location, decl);
18682 }
18683
18684 /* Look for the `template' keyword, as a syntactic disambiguator.
18685    Return TRUE iff it is present, in which case it will be
18686    consumed.  */
18687
18688 static bool
18689 cp_parser_optional_template_keyword (cp_parser *parser)
18690 {
18691   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18692     {
18693       /* The `template' keyword can only be used within templates;
18694          outside templates the parser can always figure out what is a
18695          template and what is not.  */
18696       if (!processing_template_decl)
18697         {
18698           cp_token *token = cp_lexer_peek_token (parser->lexer);
18699           error ("%H%<template%> (as a disambiguator) is only allowed "
18700                  "within templates", &token->location);
18701           /* If this part of the token stream is rescanned, the same
18702              error message would be generated.  So, we purge the token
18703              from the stream.  */
18704           cp_lexer_purge_token (parser->lexer);
18705           return false;
18706         }
18707       else
18708         {
18709           /* Consume the `template' keyword.  */
18710           cp_lexer_consume_token (parser->lexer);
18711           return true;
18712         }
18713     }
18714
18715   return false;
18716 }
18717
18718 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18719    set PARSER->SCOPE, and perform other related actions.  */
18720
18721 static void
18722 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18723 {
18724   int i;
18725   struct tree_check *check_value;
18726   deferred_access_check *chk;
18727   VEC (deferred_access_check,gc) *checks;
18728
18729   /* Get the stored value.  */
18730   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18731   /* Perform any access checks that were deferred.  */
18732   checks = check_value->checks;
18733   if (checks)
18734     {
18735       for (i = 0 ;
18736            VEC_iterate (deferred_access_check, checks, i, chk) ;
18737            ++i)
18738         {
18739           perform_or_defer_access_check (chk->binfo,
18740                                          chk->decl,
18741                                          chk->diag_decl);
18742         }
18743     }
18744   /* Set the scope from the stored value.  */
18745   parser->scope = check_value->value;
18746   parser->qualifying_scope = check_value->qualifying_scope;
18747   parser->object_scope = NULL_TREE;
18748 }
18749
18750 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18751    encounter the end of a block before what we were looking for.  */
18752
18753 static bool
18754 cp_parser_cache_group (cp_parser *parser,
18755                        enum cpp_ttype end,
18756                        unsigned depth)
18757 {
18758   while (true)
18759     {
18760       cp_token *token = cp_lexer_peek_token (parser->lexer);
18761
18762       /* Abort a parenthesized expression if we encounter a semicolon.  */
18763       if ((end == CPP_CLOSE_PAREN || depth == 0)
18764           && token->type == CPP_SEMICOLON)
18765         return true;
18766       /* If we've reached the end of the file, stop.  */
18767       if (token->type == CPP_EOF
18768           || (end != CPP_PRAGMA_EOL
18769               && token->type == CPP_PRAGMA_EOL))
18770         return true;
18771       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18772         /* We've hit the end of an enclosing block, so there's been some
18773            kind of syntax error.  */
18774         return true;
18775
18776       /* Consume the token.  */
18777       cp_lexer_consume_token (parser->lexer);
18778       /* See if it starts a new group.  */
18779       if (token->type == CPP_OPEN_BRACE)
18780         {
18781           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18782           /* In theory this should probably check end == '}', but
18783              cp_parser_save_member_function_body needs it to exit
18784              after either '}' or ')' when called with ')'.  */
18785           if (depth == 0)
18786             return false;
18787         }
18788       else if (token->type == CPP_OPEN_PAREN)
18789         {
18790           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18791           if (depth == 0 && end == CPP_CLOSE_PAREN)
18792             return false;
18793         }
18794       else if (token->type == CPP_PRAGMA)
18795         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18796       else if (token->type == end)
18797         return false;
18798     }
18799 }
18800
18801 /* Begin parsing tentatively.  We always save tokens while parsing
18802    tentatively so that if the tentative parsing fails we can restore the
18803    tokens.  */
18804
18805 static void
18806 cp_parser_parse_tentatively (cp_parser* parser)
18807 {
18808   /* Enter a new parsing context.  */
18809   parser->context = cp_parser_context_new (parser->context);
18810   /* Begin saving tokens.  */
18811   cp_lexer_save_tokens (parser->lexer);
18812   /* In order to avoid repetitive access control error messages,
18813      access checks are queued up until we are no longer parsing
18814      tentatively.  */
18815   push_deferring_access_checks (dk_deferred);
18816 }
18817
18818 /* Commit to the currently active tentative parse.  */
18819
18820 static void
18821 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18822 {
18823   cp_parser_context *context;
18824   cp_lexer *lexer;
18825
18826   /* Mark all of the levels as committed.  */
18827   lexer = parser->lexer;
18828   for (context = parser->context; context->next; context = context->next)
18829     {
18830       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18831         break;
18832       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18833       while (!cp_lexer_saving_tokens (lexer))
18834         lexer = lexer->next;
18835       cp_lexer_commit_tokens (lexer);
18836     }
18837 }
18838
18839 /* Abort the currently active tentative parse.  All consumed tokens
18840    will be rolled back, and no diagnostics will be issued.  */
18841
18842 static void
18843 cp_parser_abort_tentative_parse (cp_parser* parser)
18844 {
18845   cp_parser_simulate_error (parser);
18846   /* Now, pretend that we want to see if the construct was
18847      successfully parsed.  */
18848   cp_parser_parse_definitely (parser);
18849 }
18850
18851 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18852    token stream.  Otherwise, commit to the tokens we have consumed.
18853    Returns true if no error occurred; false otherwise.  */
18854
18855 static bool
18856 cp_parser_parse_definitely (cp_parser* parser)
18857 {
18858   bool error_occurred;
18859   cp_parser_context *context;
18860
18861   /* Remember whether or not an error occurred, since we are about to
18862      destroy that information.  */
18863   error_occurred = cp_parser_error_occurred (parser);
18864   /* Remove the topmost context from the stack.  */
18865   context = parser->context;
18866   parser->context = context->next;
18867   /* If no parse errors occurred, commit to the tentative parse.  */
18868   if (!error_occurred)
18869     {
18870       /* Commit to the tokens read tentatively, unless that was
18871          already done.  */
18872       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18873         cp_lexer_commit_tokens (parser->lexer);
18874
18875       pop_to_parent_deferring_access_checks ();
18876     }
18877   /* Otherwise, if errors occurred, roll back our state so that things
18878      are just as they were before we began the tentative parse.  */
18879   else
18880     {
18881       cp_lexer_rollback_tokens (parser->lexer);
18882       pop_deferring_access_checks ();
18883     }
18884   /* Add the context to the front of the free list.  */
18885   context->next = cp_parser_context_free_list;
18886   cp_parser_context_free_list = context;
18887
18888   return !error_occurred;
18889 }
18890
18891 /* Returns true if we are parsing tentatively and are not committed to
18892    this tentative parse.  */
18893
18894 static bool
18895 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18896 {
18897   return (cp_parser_parsing_tentatively (parser)
18898           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18899 }
18900
18901 /* Returns nonzero iff an error has occurred during the most recent
18902    tentative parse.  */
18903
18904 static bool
18905 cp_parser_error_occurred (cp_parser* parser)
18906 {
18907   return (cp_parser_parsing_tentatively (parser)
18908           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18909 }
18910
18911 /* Returns nonzero if GNU extensions are allowed.  */
18912
18913 static bool
18914 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18915 {
18916   return parser->allow_gnu_extensions_p;
18917 }
18918 \f
18919 /* Objective-C++ Productions */
18920
18921
18922 /* Parse an Objective-C expression, which feeds into a primary-expression
18923    above.
18924
18925    objc-expression:
18926      objc-message-expression
18927      objc-string-literal
18928      objc-encode-expression
18929      objc-protocol-expression
18930      objc-selector-expression
18931
18932   Returns a tree representation of the expression.  */
18933
18934 static tree
18935 cp_parser_objc_expression (cp_parser* parser)
18936 {
18937   /* Try to figure out what kind of declaration is present.  */
18938   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18939
18940   switch (kwd->type)
18941     {
18942     case CPP_OPEN_SQUARE:
18943       return cp_parser_objc_message_expression (parser);
18944
18945     case CPP_OBJC_STRING:
18946       kwd = cp_lexer_consume_token (parser->lexer);
18947       return objc_build_string_object (kwd->u.value);
18948
18949     case CPP_KEYWORD:
18950       switch (kwd->keyword)
18951         {
18952         case RID_AT_ENCODE:
18953           return cp_parser_objc_encode_expression (parser);
18954
18955         case RID_AT_PROTOCOL:
18956           return cp_parser_objc_protocol_expression (parser);
18957
18958         case RID_AT_SELECTOR:
18959           return cp_parser_objc_selector_expression (parser);
18960
18961         default:
18962           break;
18963         }
18964     default:
18965       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18966              &kwd->location, kwd->u.value);
18967       cp_parser_skip_to_end_of_block_or_statement (parser);
18968     }
18969
18970   return error_mark_node;
18971 }
18972
18973 /* Parse an Objective-C message expression.
18974
18975    objc-message-expression:
18976      [ objc-message-receiver objc-message-args ]
18977
18978    Returns a representation of an Objective-C message.  */
18979
18980 static tree
18981 cp_parser_objc_message_expression (cp_parser* parser)
18982 {
18983   tree receiver, messageargs;
18984
18985   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18986   receiver = cp_parser_objc_message_receiver (parser);
18987   messageargs = cp_parser_objc_message_args (parser);
18988   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18989
18990   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18991 }
18992
18993 /* Parse an objc-message-receiver.
18994
18995    objc-message-receiver:
18996      expression
18997      simple-type-specifier
18998
18999   Returns a representation of the type or expression.  */
19000
19001 static tree
19002 cp_parser_objc_message_receiver (cp_parser* parser)
19003 {
19004   tree rcv;
19005
19006   /* An Objective-C message receiver may be either (1) a type
19007      or (2) an expression.  */
19008   cp_parser_parse_tentatively (parser);
19009   rcv = cp_parser_expression (parser, false);
19010
19011   if (cp_parser_parse_definitely (parser))
19012     return rcv;
19013
19014   rcv = cp_parser_simple_type_specifier (parser,
19015                                          /*decl_specs=*/NULL,
19016                                          CP_PARSER_FLAGS_NONE);
19017
19018   return objc_get_class_reference (rcv);
19019 }
19020
19021 /* Parse the arguments and selectors comprising an Objective-C message.
19022
19023    objc-message-args:
19024      objc-selector
19025      objc-selector-args
19026      objc-selector-args , objc-comma-args
19027
19028    objc-selector-args:
19029      objc-selector [opt] : assignment-expression
19030      objc-selector-args objc-selector [opt] : assignment-expression
19031
19032    objc-comma-args:
19033      assignment-expression
19034      objc-comma-args , assignment-expression
19035
19036    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19037    selector arguments and TREE_VALUE containing a list of comma
19038    arguments.  */
19039
19040 static tree
19041 cp_parser_objc_message_args (cp_parser* parser)
19042 {
19043   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19044   bool maybe_unary_selector_p = true;
19045   cp_token *token = cp_lexer_peek_token (parser->lexer);
19046
19047   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19048     {
19049       tree selector = NULL_TREE, arg;
19050
19051       if (token->type != CPP_COLON)
19052         selector = cp_parser_objc_selector (parser);
19053
19054       /* Detect if we have a unary selector.  */
19055       if (maybe_unary_selector_p
19056           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19057         return build_tree_list (selector, NULL_TREE);
19058
19059       maybe_unary_selector_p = false;
19060       cp_parser_require (parser, CPP_COLON, "%<:%>");
19061       arg = cp_parser_assignment_expression (parser, false);
19062
19063       sel_args
19064         = chainon (sel_args,
19065                    build_tree_list (selector, arg));
19066
19067       token = cp_lexer_peek_token (parser->lexer);
19068     }
19069
19070   /* Handle non-selector arguments, if any. */
19071   while (token->type == CPP_COMMA)
19072     {
19073       tree arg;
19074
19075       cp_lexer_consume_token (parser->lexer);
19076       arg = cp_parser_assignment_expression (parser, false);
19077
19078       addl_args
19079         = chainon (addl_args,
19080                    build_tree_list (NULL_TREE, arg));
19081
19082       token = cp_lexer_peek_token (parser->lexer);
19083     }
19084
19085   return build_tree_list (sel_args, addl_args);
19086 }
19087
19088 /* Parse an Objective-C encode expression.
19089
19090    objc-encode-expression:
19091      @encode objc-typename
19092
19093    Returns an encoded representation of the type argument.  */
19094
19095 static tree
19096 cp_parser_objc_encode_expression (cp_parser* parser)
19097 {
19098   tree type;
19099   cp_token *token;
19100
19101   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19102   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19103   token = cp_lexer_peek_token (parser->lexer);
19104   type = complete_type (cp_parser_type_id (parser));
19105   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19106
19107   if (!type)
19108     {
19109       error ("%H%<@encode%> must specify a type as an argument",
19110              &token->location);
19111       return error_mark_node;
19112     }
19113
19114   return objc_build_encode_expr (type);
19115 }
19116
19117 /* Parse an Objective-C @defs expression.  */
19118
19119 static tree
19120 cp_parser_objc_defs_expression (cp_parser *parser)
19121 {
19122   tree name;
19123
19124   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19125   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19126   name = cp_parser_identifier (parser);
19127   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19128
19129   return objc_get_class_ivars (name);
19130 }
19131
19132 /* Parse an Objective-C protocol expression.
19133
19134   objc-protocol-expression:
19135     @protocol ( identifier )
19136
19137   Returns a representation of the protocol expression.  */
19138
19139 static tree
19140 cp_parser_objc_protocol_expression (cp_parser* parser)
19141 {
19142   tree proto;
19143
19144   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19145   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19146   proto = cp_parser_identifier (parser);
19147   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19148
19149   return objc_build_protocol_expr (proto);
19150 }
19151
19152 /* Parse an Objective-C selector expression.
19153
19154    objc-selector-expression:
19155      @selector ( objc-method-signature )
19156
19157    objc-method-signature:
19158      objc-selector
19159      objc-selector-seq
19160
19161    objc-selector-seq:
19162      objc-selector :
19163      objc-selector-seq objc-selector :
19164
19165   Returns a representation of the method selector.  */
19166
19167 static tree
19168 cp_parser_objc_selector_expression (cp_parser* parser)
19169 {
19170   tree sel_seq = NULL_TREE;
19171   bool maybe_unary_selector_p = true;
19172   cp_token *token;
19173
19174   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19175   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19176   token = cp_lexer_peek_token (parser->lexer);
19177
19178   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19179          || token->type == CPP_SCOPE)
19180     {
19181       tree selector = NULL_TREE;
19182
19183       if (token->type != CPP_COLON
19184           || token->type == CPP_SCOPE)
19185         selector = cp_parser_objc_selector (parser);
19186
19187       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19188           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19189         {
19190           /* Detect if we have a unary selector.  */
19191           if (maybe_unary_selector_p)
19192             {
19193               sel_seq = selector;
19194               goto finish_selector;
19195             }
19196           else
19197             {
19198               cp_parser_error (parser, "expected %<:%>");
19199             }
19200         }
19201       maybe_unary_selector_p = false;
19202       token = cp_lexer_consume_token (parser->lexer);
19203
19204       if (token->type == CPP_SCOPE)
19205         {
19206           sel_seq
19207             = chainon (sel_seq,
19208                        build_tree_list (selector, NULL_TREE));
19209           sel_seq
19210             = chainon (sel_seq,
19211                        build_tree_list (NULL_TREE, NULL_TREE));
19212         }
19213       else
19214         sel_seq
19215           = chainon (sel_seq,
19216                      build_tree_list (selector, NULL_TREE));
19217
19218       token = cp_lexer_peek_token (parser->lexer);
19219     }
19220
19221  finish_selector:
19222   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19223
19224   return objc_build_selector_expr (sel_seq);
19225 }
19226
19227 /* Parse a list of identifiers.
19228
19229    objc-identifier-list:
19230      identifier
19231      objc-identifier-list , identifier
19232
19233    Returns a TREE_LIST of identifier nodes.  */
19234
19235 static tree
19236 cp_parser_objc_identifier_list (cp_parser* parser)
19237 {
19238   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19239   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19240
19241   while (sep->type == CPP_COMMA)
19242     {
19243       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19244       list = chainon (list,
19245                       build_tree_list (NULL_TREE,
19246                                        cp_parser_identifier (parser)));
19247       sep = cp_lexer_peek_token (parser->lexer);
19248     }
19249
19250   return list;
19251 }
19252
19253 /* Parse an Objective-C alias declaration.
19254
19255    objc-alias-declaration:
19256      @compatibility_alias identifier identifier ;
19257
19258    This function registers the alias mapping with the Objective-C front end.
19259    It returns nothing.  */
19260
19261 static void
19262 cp_parser_objc_alias_declaration (cp_parser* parser)
19263 {
19264   tree alias, orig;
19265
19266   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19267   alias = cp_parser_identifier (parser);
19268   orig = cp_parser_identifier (parser);
19269   objc_declare_alias (alias, orig);
19270   cp_parser_consume_semicolon_at_end_of_statement (parser);
19271 }
19272
19273 /* Parse an Objective-C class forward-declaration.
19274
19275    objc-class-declaration:
19276      @class objc-identifier-list ;
19277
19278    The function registers the forward declarations with the Objective-C
19279    front end.  It returns nothing.  */
19280
19281 static void
19282 cp_parser_objc_class_declaration (cp_parser* parser)
19283 {
19284   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19285   objc_declare_class (cp_parser_objc_identifier_list (parser));
19286   cp_parser_consume_semicolon_at_end_of_statement (parser);
19287 }
19288
19289 /* Parse a list of Objective-C protocol references.
19290
19291    objc-protocol-refs-opt:
19292      objc-protocol-refs [opt]
19293
19294    objc-protocol-refs:
19295      < objc-identifier-list >
19296
19297    Returns a TREE_LIST of identifiers, if any.  */
19298
19299 static tree
19300 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19301 {
19302   tree protorefs = NULL_TREE;
19303
19304   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19305     {
19306       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19307       protorefs = cp_parser_objc_identifier_list (parser);
19308       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19309     }
19310
19311   return protorefs;
19312 }
19313
19314 /* Parse a Objective-C visibility specification.  */
19315
19316 static void
19317 cp_parser_objc_visibility_spec (cp_parser* parser)
19318 {
19319   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19320
19321   switch (vis->keyword)
19322     {
19323     case RID_AT_PRIVATE:
19324       objc_set_visibility (2);
19325       break;
19326     case RID_AT_PROTECTED:
19327       objc_set_visibility (0);
19328       break;
19329     case RID_AT_PUBLIC:
19330       objc_set_visibility (1);
19331       break;
19332     default:
19333       return;
19334     }
19335
19336   /* Eat '@private'/'@protected'/'@public'.  */
19337   cp_lexer_consume_token (parser->lexer);
19338 }
19339
19340 /* Parse an Objective-C method type.  */
19341
19342 static void
19343 cp_parser_objc_method_type (cp_parser* parser)
19344 {
19345   objc_set_method_type
19346    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19347     ? PLUS_EXPR
19348     : MINUS_EXPR);
19349 }
19350
19351 /* Parse an Objective-C protocol qualifier.  */
19352
19353 static tree
19354 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19355 {
19356   tree quals = NULL_TREE, node;
19357   cp_token *token = cp_lexer_peek_token (parser->lexer);
19358
19359   node = token->u.value;
19360
19361   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19362          && (node == ridpointers [(int) RID_IN]
19363              || node == ridpointers [(int) RID_OUT]
19364              || node == ridpointers [(int) RID_INOUT]
19365              || node == ridpointers [(int) RID_BYCOPY]
19366              || node == ridpointers [(int) RID_BYREF]
19367              || node == ridpointers [(int) RID_ONEWAY]))
19368     {
19369       quals = tree_cons (NULL_TREE, node, quals);
19370       cp_lexer_consume_token (parser->lexer);
19371       token = cp_lexer_peek_token (parser->lexer);
19372       node = token->u.value;
19373     }
19374
19375   return quals;
19376 }
19377
19378 /* Parse an Objective-C typename.  */
19379
19380 static tree
19381 cp_parser_objc_typename (cp_parser* parser)
19382 {
19383   tree type_name = NULL_TREE;
19384
19385   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19386     {
19387       tree proto_quals, cp_type = NULL_TREE;
19388
19389       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19390       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19391
19392       /* An ObjC type name may consist of just protocol qualifiers, in which
19393          case the type shall default to 'id'.  */
19394       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19395         cp_type = cp_parser_type_id (parser);
19396
19397       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19398       type_name = build_tree_list (proto_quals, cp_type);
19399     }
19400
19401   return type_name;
19402 }
19403
19404 /* Check to see if TYPE refers to an Objective-C selector name.  */
19405
19406 static bool
19407 cp_parser_objc_selector_p (enum cpp_ttype type)
19408 {
19409   return (type == CPP_NAME || type == CPP_KEYWORD
19410           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19411           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19412           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19413           || type == CPP_XOR || type == CPP_XOR_EQ);
19414 }
19415
19416 /* Parse an Objective-C selector.  */
19417
19418 static tree
19419 cp_parser_objc_selector (cp_parser* parser)
19420 {
19421   cp_token *token = cp_lexer_consume_token (parser->lexer);
19422
19423   if (!cp_parser_objc_selector_p (token->type))
19424     {
19425       error ("%Hinvalid Objective-C++ selector name", &token->location);
19426       return error_mark_node;
19427     }
19428
19429   /* C++ operator names are allowed to appear in ObjC selectors.  */
19430   switch (token->type)
19431     {
19432     case CPP_AND_AND: return get_identifier ("and");
19433     case CPP_AND_EQ: return get_identifier ("and_eq");
19434     case CPP_AND: return get_identifier ("bitand");
19435     case CPP_OR: return get_identifier ("bitor");
19436     case CPP_COMPL: return get_identifier ("compl");
19437     case CPP_NOT: return get_identifier ("not");
19438     case CPP_NOT_EQ: return get_identifier ("not_eq");
19439     case CPP_OR_OR: return get_identifier ("or");
19440     case CPP_OR_EQ: return get_identifier ("or_eq");
19441     case CPP_XOR: return get_identifier ("xor");
19442     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19443     default: return token->u.value;
19444     }
19445 }
19446
19447 /* Parse an Objective-C params list.  */
19448
19449 static tree
19450 cp_parser_objc_method_keyword_params (cp_parser* parser)
19451 {
19452   tree params = NULL_TREE;
19453   bool maybe_unary_selector_p = true;
19454   cp_token *token = cp_lexer_peek_token (parser->lexer);
19455
19456   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19457     {
19458       tree selector = NULL_TREE, type_name, identifier;
19459
19460       if (token->type != CPP_COLON)
19461         selector = cp_parser_objc_selector (parser);
19462
19463       /* Detect if we have a unary selector.  */
19464       if (maybe_unary_selector_p
19465           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19466         return selector;
19467
19468       maybe_unary_selector_p = false;
19469       cp_parser_require (parser, CPP_COLON, "%<:%>");
19470       type_name = cp_parser_objc_typename (parser);
19471       identifier = cp_parser_identifier (parser);
19472
19473       params
19474         = chainon (params,
19475                    objc_build_keyword_decl (selector,
19476                                             type_name,
19477                                             identifier));
19478
19479       token = cp_lexer_peek_token (parser->lexer);
19480     }
19481
19482   return params;
19483 }
19484
19485 /* Parse the non-keyword Objective-C params.  */
19486
19487 static tree
19488 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19489 {
19490   tree params = make_node (TREE_LIST);
19491   cp_token *token = cp_lexer_peek_token (parser->lexer);
19492   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19493
19494   while (token->type == CPP_COMMA)
19495     {
19496       cp_parameter_declarator *parmdecl;
19497       tree parm;
19498
19499       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19500       token = cp_lexer_peek_token (parser->lexer);
19501
19502       if (token->type == CPP_ELLIPSIS)
19503         {
19504           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19505           *ellipsisp = true;
19506           break;
19507         }
19508
19509       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19510       parm = grokdeclarator (parmdecl->declarator,
19511                              &parmdecl->decl_specifiers,
19512                              PARM, /*initialized=*/0,
19513                              /*attrlist=*/NULL);
19514
19515       chainon (params, build_tree_list (NULL_TREE, parm));
19516       token = cp_lexer_peek_token (parser->lexer);
19517     }
19518
19519   return params;
19520 }
19521
19522 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19523
19524 static void
19525 cp_parser_objc_interstitial_code (cp_parser* parser)
19526 {
19527   cp_token *token = cp_lexer_peek_token (parser->lexer);
19528
19529   /* If the next token is `extern' and the following token is a string
19530      literal, then we have a linkage specification.  */
19531   if (token->keyword == RID_EXTERN
19532       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19533     cp_parser_linkage_specification (parser);
19534   /* Handle #pragma, if any.  */
19535   else if (token->type == CPP_PRAGMA)
19536     cp_parser_pragma (parser, pragma_external);
19537   /* Allow stray semicolons.  */
19538   else if (token->type == CPP_SEMICOLON)
19539     cp_lexer_consume_token (parser->lexer);
19540   /* Finally, try to parse a block-declaration, or a function-definition.  */
19541   else
19542     cp_parser_block_declaration (parser, /*statement_p=*/false);
19543 }
19544
19545 /* Parse a method signature.  */
19546
19547 static tree
19548 cp_parser_objc_method_signature (cp_parser* parser)
19549 {
19550   tree rettype, kwdparms, optparms;
19551   bool ellipsis = false;
19552
19553   cp_parser_objc_method_type (parser);
19554   rettype = cp_parser_objc_typename (parser);
19555   kwdparms = cp_parser_objc_method_keyword_params (parser);
19556   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19557
19558   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19559 }
19560
19561 /* Pars an Objective-C method prototype list.  */
19562
19563 static void
19564 cp_parser_objc_method_prototype_list (cp_parser* parser)
19565 {
19566   cp_token *token = cp_lexer_peek_token (parser->lexer);
19567
19568   while (token->keyword != RID_AT_END)
19569     {
19570       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19571         {
19572           objc_add_method_declaration
19573            (cp_parser_objc_method_signature (parser));
19574           cp_parser_consume_semicolon_at_end_of_statement (parser);
19575         }
19576       else
19577         /* Allow for interspersed non-ObjC++ code.  */
19578         cp_parser_objc_interstitial_code (parser);
19579
19580       token = cp_lexer_peek_token (parser->lexer);
19581     }
19582
19583   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19584   objc_finish_interface ();
19585 }
19586
19587 /* Parse an Objective-C method definition list.  */
19588
19589 static void
19590 cp_parser_objc_method_definition_list (cp_parser* parser)
19591 {
19592   cp_token *token = cp_lexer_peek_token (parser->lexer);
19593
19594   while (token->keyword != RID_AT_END)
19595     {
19596       tree meth;
19597
19598       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19599         {
19600           push_deferring_access_checks (dk_deferred);
19601           objc_start_method_definition
19602            (cp_parser_objc_method_signature (parser));
19603
19604           /* For historical reasons, we accept an optional semicolon.  */
19605           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19606             cp_lexer_consume_token (parser->lexer);
19607
19608           perform_deferred_access_checks ();
19609           stop_deferring_access_checks ();
19610           meth = cp_parser_function_definition_after_declarator (parser,
19611                                                                  false);
19612           pop_deferring_access_checks ();
19613           objc_finish_method_definition (meth);
19614         }
19615       else
19616         /* Allow for interspersed non-ObjC++ code.  */
19617         cp_parser_objc_interstitial_code (parser);
19618
19619       token = cp_lexer_peek_token (parser->lexer);
19620     }
19621
19622   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19623   objc_finish_implementation ();
19624 }
19625
19626 /* Parse Objective-C ivars.  */
19627
19628 static void
19629 cp_parser_objc_class_ivars (cp_parser* parser)
19630 {
19631   cp_token *token = cp_lexer_peek_token (parser->lexer);
19632
19633   if (token->type != CPP_OPEN_BRACE)
19634     return;     /* No ivars specified.  */
19635
19636   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19637   token = cp_lexer_peek_token (parser->lexer);
19638
19639   while (token->type != CPP_CLOSE_BRACE)
19640     {
19641       cp_decl_specifier_seq declspecs;
19642       int decl_class_or_enum_p;
19643       tree prefix_attributes;
19644
19645       cp_parser_objc_visibility_spec (parser);
19646
19647       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19648         break;
19649
19650       cp_parser_decl_specifier_seq (parser,
19651                                     CP_PARSER_FLAGS_OPTIONAL,
19652                                     &declspecs,
19653                                     &decl_class_or_enum_p);
19654       prefix_attributes = declspecs.attributes;
19655       declspecs.attributes = NULL_TREE;
19656
19657       /* Keep going until we hit the `;' at the end of the
19658          declaration.  */
19659       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19660         {
19661           tree width = NULL_TREE, attributes, first_attribute, decl;
19662           cp_declarator *declarator = NULL;
19663           int ctor_dtor_or_conv_p;
19664
19665           /* Check for a (possibly unnamed) bitfield declaration.  */
19666           token = cp_lexer_peek_token (parser->lexer);
19667           if (token->type == CPP_COLON)
19668             goto eat_colon;
19669
19670           if (token->type == CPP_NAME
19671               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19672                   == CPP_COLON))
19673             {
19674               /* Get the name of the bitfield.  */
19675               declarator = make_id_declarator (NULL_TREE,
19676                                                cp_parser_identifier (parser),
19677                                                sfk_none);
19678
19679              eat_colon:
19680               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19681               /* Get the width of the bitfield.  */
19682               width
19683                 = cp_parser_constant_expression (parser,
19684                                                  /*allow_non_constant=*/false,
19685                                                  NULL);
19686             }
19687           else
19688             {
19689               /* Parse the declarator.  */
19690               declarator
19691                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19692                                         &ctor_dtor_or_conv_p,
19693                                         /*parenthesized_p=*/NULL,
19694                                         /*member_p=*/false);
19695             }
19696
19697           /* Look for attributes that apply to the ivar.  */
19698           attributes = cp_parser_attributes_opt (parser);
19699           /* Remember which attributes are prefix attributes and
19700              which are not.  */
19701           first_attribute = attributes;
19702           /* Combine the attributes.  */
19703           attributes = chainon (prefix_attributes, attributes);
19704
19705           if (width)
19706               /* Create the bitfield declaration.  */
19707               decl = grokbitfield (declarator, &declspecs,
19708                                    width,
19709                                    attributes);
19710           else
19711             decl = grokfield (declarator, &declspecs,
19712                               NULL_TREE, /*init_const_expr_p=*/false,
19713                               NULL_TREE, attributes);
19714
19715           /* Add the instance variable.  */
19716           objc_add_instance_variable (decl);
19717
19718           /* Reset PREFIX_ATTRIBUTES.  */
19719           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19720             attributes = TREE_CHAIN (attributes);
19721           if (attributes)
19722             TREE_CHAIN (attributes) = NULL_TREE;
19723
19724           token = cp_lexer_peek_token (parser->lexer);
19725
19726           if (token->type == CPP_COMMA)
19727             {
19728               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19729               continue;
19730             }
19731           break;
19732         }
19733
19734       cp_parser_consume_semicolon_at_end_of_statement (parser);
19735       token = cp_lexer_peek_token (parser->lexer);
19736     }
19737
19738   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19739   /* For historical reasons, we accept an optional semicolon.  */
19740   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19741     cp_lexer_consume_token (parser->lexer);
19742 }
19743
19744 /* Parse an Objective-C protocol declaration.  */
19745
19746 static void
19747 cp_parser_objc_protocol_declaration (cp_parser* parser)
19748 {
19749   tree proto, protorefs;
19750   cp_token *tok;
19751
19752   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19753   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19754     {
19755       tok = cp_lexer_peek_token (parser->lexer);
19756       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19757       goto finish;
19758     }
19759
19760   /* See if we have a forward declaration or a definition.  */
19761   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19762
19763   /* Try a forward declaration first.  */
19764   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19765     {
19766       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19767      finish:
19768       cp_parser_consume_semicolon_at_end_of_statement (parser);
19769     }
19770
19771   /* Ok, we got a full-fledged definition (or at least should).  */
19772   else
19773     {
19774       proto = cp_parser_identifier (parser);
19775       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19776       objc_start_protocol (proto, protorefs);
19777       cp_parser_objc_method_prototype_list (parser);
19778     }
19779 }
19780
19781 /* Parse an Objective-C superclass or category.  */
19782
19783 static void
19784 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19785                                                           tree *categ)
19786 {
19787   cp_token *next = cp_lexer_peek_token (parser->lexer);
19788
19789   *super = *categ = NULL_TREE;
19790   if (next->type == CPP_COLON)
19791     {
19792       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19793       *super = cp_parser_identifier (parser);
19794     }
19795   else if (next->type == CPP_OPEN_PAREN)
19796     {
19797       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19798       *categ = cp_parser_identifier (parser);
19799       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19800     }
19801 }
19802
19803 /* Parse an Objective-C class interface.  */
19804
19805 static void
19806 cp_parser_objc_class_interface (cp_parser* parser)
19807 {
19808   tree name, super, categ, protos;
19809
19810   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19811   name = cp_parser_identifier (parser);
19812   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19813   protos = cp_parser_objc_protocol_refs_opt (parser);
19814
19815   /* We have either a class or a category on our hands.  */
19816   if (categ)
19817     objc_start_category_interface (name, categ, protos);
19818   else
19819     {
19820       objc_start_class_interface (name, super, protos);
19821       /* Handle instance variable declarations, if any.  */
19822       cp_parser_objc_class_ivars (parser);
19823       objc_continue_interface ();
19824     }
19825
19826   cp_parser_objc_method_prototype_list (parser);
19827 }
19828
19829 /* Parse an Objective-C class implementation.  */
19830
19831 static void
19832 cp_parser_objc_class_implementation (cp_parser* parser)
19833 {
19834   tree name, super, categ;
19835
19836   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19837   name = cp_parser_identifier (parser);
19838   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19839
19840   /* We have either a class or a category on our hands.  */
19841   if (categ)
19842     objc_start_category_implementation (name, categ);
19843   else
19844     {
19845       objc_start_class_implementation (name, super);
19846       /* Handle instance variable declarations, if any.  */
19847       cp_parser_objc_class_ivars (parser);
19848       objc_continue_implementation ();
19849     }
19850
19851   cp_parser_objc_method_definition_list (parser);
19852 }
19853
19854 /* Consume the @end token and finish off the implementation.  */
19855
19856 static void
19857 cp_parser_objc_end_implementation (cp_parser* parser)
19858 {
19859   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19860   objc_finish_implementation ();
19861 }
19862
19863 /* Parse an Objective-C declaration.  */
19864
19865 static void
19866 cp_parser_objc_declaration (cp_parser* parser)
19867 {
19868   /* Try to figure out what kind of declaration is present.  */
19869   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19870
19871   switch (kwd->keyword)
19872     {
19873     case RID_AT_ALIAS:
19874       cp_parser_objc_alias_declaration (parser);
19875       break;
19876     case RID_AT_CLASS:
19877       cp_parser_objc_class_declaration (parser);
19878       break;
19879     case RID_AT_PROTOCOL:
19880       cp_parser_objc_protocol_declaration (parser);
19881       break;
19882     case RID_AT_INTERFACE:
19883       cp_parser_objc_class_interface (parser);
19884       break;
19885     case RID_AT_IMPLEMENTATION:
19886       cp_parser_objc_class_implementation (parser);
19887       break;
19888     case RID_AT_END:
19889       cp_parser_objc_end_implementation (parser);
19890       break;
19891     default:
19892       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19893              &kwd->location, kwd->u.value);
19894       cp_parser_skip_to_end_of_block_or_statement (parser);
19895     }
19896 }
19897
19898 /* Parse an Objective-C try-catch-finally statement.
19899
19900    objc-try-catch-finally-stmt:
19901      @try compound-statement objc-catch-clause-seq [opt]
19902        objc-finally-clause [opt]
19903
19904    objc-catch-clause-seq:
19905      objc-catch-clause objc-catch-clause-seq [opt]
19906
19907    objc-catch-clause:
19908      @catch ( exception-declaration ) compound-statement
19909
19910    objc-finally-clause
19911      @finally compound-statement
19912
19913    Returns NULL_TREE.  */
19914
19915 static tree
19916 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19917   location_t location;
19918   tree stmt;
19919
19920   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19921   location = cp_lexer_peek_token (parser->lexer)->location;
19922   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19923      node, lest it get absorbed into the surrounding block.  */
19924   stmt = push_stmt_list ();
19925   cp_parser_compound_statement (parser, NULL, false);
19926   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19927
19928   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19929     {
19930       cp_parameter_declarator *parmdecl;
19931       tree parm;
19932
19933       cp_lexer_consume_token (parser->lexer);
19934       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19935       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19936       parm = grokdeclarator (parmdecl->declarator,
19937                              &parmdecl->decl_specifiers,
19938                              PARM, /*initialized=*/0,
19939                              /*attrlist=*/NULL);
19940       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19941       objc_begin_catch_clause (parm);
19942       cp_parser_compound_statement (parser, NULL, false);
19943       objc_finish_catch_clause ();
19944     }
19945
19946   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19947     {
19948       cp_lexer_consume_token (parser->lexer);
19949       location = cp_lexer_peek_token (parser->lexer)->location;
19950       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19951          node, lest it get absorbed into the surrounding block.  */
19952       stmt = push_stmt_list ();
19953       cp_parser_compound_statement (parser, NULL, false);
19954       objc_build_finally_clause (location, pop_stmt_list (stmt));
19955     }
19956
19957   return objc_finish_try_stmt ();
19958 }
19959
19960 /* Parse an Objective-C synchronized statement.
19961
19962    objc-synchronized-stmt:
19963      @synchronized ( expression ) compound-statement
19964
19965    Returns NULL_TREE.  */
19966
19967 static tree
19968 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19969   location_t location;
19970   tree lock, stmt;
19971
19972   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19973
19974   location = cp_lexer_peek_token (parser->lexer)->location;
19975   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19976   lock = cp_parser_expression (parser, false);
19977   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19978
19979   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19980      node, lest it get absorbed into the surrounding block.  */
19981   stmt = push_stmt_list ();
19982   cp_parser_compound_statement (parser, NULL, false);
19983
19984   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19985 }
19986
19987 /* Parse an Objective-C throw statement.
19988
19989    objc-throw-stmt:
19990      @throw assignment-expression [opt] ;
19991
19992    Returns a constructed '@throw' statement.  */
19993
19994 static tree
19995 cp_parser_objc_throw_statement (cp_parser *parser) {
19996   tree expr = NULL_TREE;
19997
19998   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19999
20000   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20001     expr = cp_parser_assignment_expression (parser, false);
20002
20003   cp_parser_consume_semicolon_at_end_of_statement (parser);
20004
20005   return objc_build_throw_stmt (expr);
20006 }
20007
20008 /* Parse an Objective-C statement.  */
20009
20010 static tree
20011 cp_parser_objc_statement (cp_parser * parser) {
20012   /* Try to figure out what kind of declaration is present.  */
20013   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20014
20015   switch (kwd->keyword)
20016     {
20017     case RID_AT_TRY:
20018       return cp_parser_objc_try_catch_finally_statement (parser);
20019     case RID_AT_SYNCHRONIZED:
20020       return cp_parser_objc_synchronized_statement (parser);
20021     case RID_AT_THROW:
20022       return cp_parser_objc_throw_statement (parser);
20023     default:
20024       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20025              &kwd->location, kwd->u.value);
20026       cp_parser_skip_to_end_of_block_or_statement (parser);
20027     }
20028
20029   return error_mark_node;
20030 }
20031 \f
20032 /* OpenMP 2.5 parsing routines.  */
20033
20034 /* Returns name of the next clause.
20035    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20036    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20037    returned and the token is consumed.  */
20038
20039 static pragma_omp_clause
20040 cp_parser_omp_clause_name (cp_parser *parser)
20041 {
20042   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20043
20044   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20045     result = PRAGMA_OMP_CLAUSE_IF;
20046   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20047     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20048   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20049     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20050   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20051     {
20052       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20053       const char *p = IDENTIFIER_POINTER (id);
20054
20055       switch (p[0])
20056         {
20057         case 'c':
20058           if (!strcmp ("collapse", p))
20059             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20060           else if (!strcmp ("copyin", p))
20061             result = PRAGMA_OMP_CLAUSE_COPYIN;
20062           else if (!strcmp ("copyprivate", p))
20063             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20064           break;
20065         case 'f':
20066           if (!strcmp ("firstprivate", p))
20067             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20068           break;
20069         case 'l':
20070           if (!strcmp ("lastprivate", p))
20071             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20072           break;
20073         case 'n':
20074           if (!strcmp ("nowait", p))
20075             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20076           else if (!strcmp ("num_threads", p))
20077             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20078           break;
20079         case 'o':
20080           if (!strcmp ("ordered", p))
20081             result = PRAGMA_OMP_CLAUSE_ORDERED;
20082           break;
20083         case 'r':
20084           if (!strcmp ("reduction", p))
20085             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20086           break;
20087         case 's':
20088           if (!strcmp ("schedule", p))
20089             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20090           else if (!strcmp ("shared", p))
20091             result = PRAGMA_OMP_CLAUSE_SHARED;
20092           break;
20093         case 'u':
20094           if (!strcmp ("untied", p))
20095             result = PRAGMA_OMP_CLAUSE_UNTIED;
20096           break;
20097         }
20098     }
20099
20100   if (result != PRAGMA_OMP_CLAUSE_NONE)
20101     cp_lexer_consume_token (parser->lexer);
20102
20103   return result;
20104 }
20105
20106 /* Validate that a clause of the given type does not already exist.  */
20107
20108 static void
20109 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20110                            const char *name, location_t location)
20111 {
20112   tree c;
20113
20114   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20115     if (OMP_CLAUSE_CODE (c) == code)
20116       {
20117         error ("%Htoo many %qs clauses", &location, name);
20118         break;
20119       }
20120 }
20121
20122 /* OpenMP 2.5:
20123    variable-list:
20124      identifier
20125      variable-list , identifier
20126
20127    In addition, we match a closing parenthesis.  An opening parenthesis
20128    will have been consumed by the caller.
20129
20130    If KIND is nonzero, create the appropriate node and install the decl
20131    in OMP_CLAUSE_DECL and add the node to the head of the list.
20132
20133    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20134    return the list created.  */
20135
20136 static tree
20137 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20138                                 tree list)
20139 {
20140   cp_token *token;
20141   while (1)
20142     {
20143       tree name, decl;
20144
20145       token = cp_lexer_peek_token (parser->lexer);
20146       name = cp_parser_id_expression (parser, /*template_p=*/false,
20147                                       /*check_dependency_p=*/true,
20148                                       /*template_p=*/NULL,
20149                                       /*declarator_p=*/false,
20150                                       /*optional_p=*/false);
20151       if (name == error_mark_node)
20152         goto skip_comma;
20153
20154       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20155       if (decl == error_mark_node)
20156         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20157       else if (kind != 0)
20158         {
20159           tree u = build_omp_clause (kind);
20160           OMP_CLAUSE_DECL (u) = decl;
20161           OMP_CLAUSE_CHAIN (u) = list;
20162           list = u;
20163         }
20164       else
20165         list = tree_cons (decl, NULL_TREE, list);
20166
20167     get_comma:
20168       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20169         break;
20170       cp_lexer_consume_token (parser->lexer);
20171     }
20172
20173   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20174     {
20175       int ending;
20176
20177       /* Try to resync to an unnested comma.  Copied from
20178          cp_parser_parenthesized_expression_list.  */
20179     skip_comma:
20180       ending = cp_parser_skip_to_closing_parenthesis (parser,
20181                                                       /*recovering=*/true,
20182                                                       /*or_comma=*/true,
20183                                                       /*consume_paren=*/true);
20184       if (ending < 0)
20185         goto get_comma;
20186     }
20187
20188   return list;
20189 }
20190
20191 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20192    common case for omp clauses.  */
20193
20194 static tree
20195 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20196 {
20197   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20198     return cp_parser_omp_var_list_no_open (parser, kind, list);
20199   return list;
20200 }
20201
20202 /* OpenMP 3.0:
20203    collapse ( constant-expression ) */
20204
20205 static tree
20206 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20207 {
20208   tree c, num;
20209   location_t loc;
20210   HOST_WIDE_INT n;
20211
20212   loc = cp_lexer_peek_token (parser->lexer)->location;
20213   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20214     return list;
20215
20216   num = cp_parser_constant_expression (parser, false, NULL);
20217
20218   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20219     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20220                                            /*or_comma=*/false,
20221                                            /*consume_paren=*/true);
20222
20223   if (num == error_mark_node)
20224     return list;
20225   num = fold_non_dependent_expr (num);
20226   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20227       || !host_integerp (num, 0)
20228       || (n = tree_low_cst (num, 0)) <= 0
20229       || (int) n != n)
20230     {
20231       error ("%Hcollapse argument needs positive constant integer expression",
20232              &loc);
20233       return list;
20234     }
20235
20236   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20237   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20238   OMP_CLAUSE_CHAIN (c) = list;
20239   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20240
20241   return c;
20242 }
20243
20244 /* OpenMP 2.5:
20245    default ( shared | none ) */
20246
20247 static tree
20248 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20249 {
20250   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20251   tree c;
20252
20253   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20254     return list;
20255   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20256     {
20257       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20258       const char *p = IDENTIFIER_POINTER (id);
20259
20260       switch (p[0])
20261         {
20262         case 'n':
20263           if (strcmp ("none", p) != 0)
20264             goto invalid_kind;
20265           kind = OMP_CLAUSE_DEFAULT_NONE;
20266           break;
20267
20268         case 's':
20269           if (strcmp ("shared", p) != 0)
20270             goto invalid_kind;
20271           kind = OMP_CLAUSE_DEFAULT_SHARED;
20272           break;
20273
20274         default:
20275           goto invalid_kind;
20276         }
20277
20278       cp_lexer_consume_token (parser->lexer);
20279     }
20280   else
20281     {
20282     invalid_kind:
20283       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20284     }
20285
20286   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20287     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20288                                            /*or_comma=*/false,
20289                                            /*consume_paren=*/true);
20290
20291   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20292     return list;
20293
20294   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20295   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20296   OMP_CLAUSE_CHAIN (c) = list;
20297   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20298
20299   return c;
20300 }
20301
20302 /* OpenMP 2.5:
20303    if ( expression ) */
20304
20305 static tree
20306 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20307 {
20308   tree t, c;
20309
20310   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20311     return list;
20312
20313   t = cp_parser_condition (parser);
20314
20315   if (t == error_mark_node
20316       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20317     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20318                                            /*or_comma=*/false,
20319                                            /*consume_paren=*/true);
20320
20321   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20322
20323   c = build_omp_clause (OMP_CLAUSE_IF);
20324   OMP_CLAUSE_IF_EXPR (c) = t;
20325   OMP_CLAUSE_CHAIN (c) = list;
20326
20327   return c;
20328 }
20329
20330 /* OpenMP 2.5:
20331    nowait */
20332
20333 static tree
20334 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20335                              tree list, location_t location)
20336 {
20337   tree c;
20338
20339   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20340
20341   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20342   OMP_CLAUSE_CHAIN (c) = list;
20343   return c;
20344 }
20345
20346 /* OpenMP 2.5:
20347    num_threads ( expression ) */
20348
20349 static tree
20350 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20351                                   location_t location)
20352 {
20353   tree t, c;
20354
20355   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20356     return list;
20357
20358   t = cp_parser_expression (parser, false);
20359
20360   if (t == error_mark_node
20361       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20362     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20363                                            /*or_comma=*/false,
20364                                            /*consume_paren=*/true);
20365
20366   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20367                              "num_threads", location);
20368
20369   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20370   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20371   OMP_CLAUSE_CHAIN (c) = list;
20372
20373   return c;
20374 }
20375
20376 /* OpenMP 2.5:
20377    ordered */
20378
20379 static tree
20380 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20381                               tree list, location_t location)
20382 {
20383   tree c;
20384
20385   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20386                              "ordered", location);
20387
20388   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20389   OMP_CLAUSE_CHAIN (c) = list;
20390   return c;
20391 }
20392
20393 /* OpenMP 2.5:
20394    reduction ( reduction-operator : variable-list )
20395
20396    reduction-operator:
20397      One of: + * - & ^ | && || */
20398
20399 static tree
20400 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20401 {
20402   enum tree_code code;
20403   tree nlist, c;
20404
20405   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20406     return list;
20407
20408   switch (cp_lexer_peek_token (parser->lexer)->type)
20409     {
20410     case CPP_PLUS:
20411       code = PLUS_EXPR;
20412       break;
20413     case CPP_MULT:
20414       code = MULT_EXPR;
20415       break;
20416     case CPP_MINUS:
20417       code = MINUS_EXPR;
20418       break;
20419     case CPP_AND:
20420       code = BIT_AND_EXPR;
20421       break;
20422     case CPP_XOR:
20423       code = BIT_XOR_EXPR;
20424       break;
20425     case CPP_OR:
20426       code = BIT_IOR_EXPR;
20427       break;
20428     case CPP_AND_AND:
20429       code = TRUTH_ANDIF_EXPR;
20430       break;
20431     case CPP_OR_OR:
20432       code = TRUTH_ORIF_EXPR;
20433       break;
20434     default:
20435       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20436                                "%<|%>, %<&&%>, or %<||%>");
20437     resync_fail:
20438       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20439                                              /*or_comma=*/false,
20440                                              /*consume_paren=*/true);
20441       return list;
20442     }
20443   cp_lexer_consume_token (parser->lexer);
20444
20445   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20446     goto resync_fail;
20447
20448   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20449   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20450     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20451
20452   return nlist;
20453 }
20454
20455 /* OpenMP 2.5:
20456    schedule ( schedule-kind )
20457    schedule ( schedule-kind , expression )
20458
20459    schedule-kind:
20460      static | dynamic | guided | runtime | auto  */
20461
20462 static tree
20463 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20464 {
20465   tree c, t;
20466
20467   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20468     return list;
20469
20470   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20471
20472   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20473     {
20474       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20475       const char *p = IDENTIFIER_POINTER (id);
20476
20477       switch (p[0])
20478         {
20479         case 'd':
20480           if (strcmp ("dynamic", p) != 0)
20481             goto invalid_kind;
20482           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20483           break;
20484
20485         case 'g':
20486           if (strcmp ("guided", p) != 0)
20487             goto invalid_kind;
20488           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20489           break;
20490
20491         case 'r':
20492           if (strcmp ("runtime", p) != 0)
20493             goto invalid_kind;
20494           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20495           break;
20496
20497         default:
20498           goto invalid_kind;
20499         }
20500     }
20501   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20502     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20503   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20504     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20505   else
20506     goto invalid_kind;
20507   cp_lexer_consume_token (parser->lexer);
20508
20509   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20510     {
20511       cp_token *token;
20512       cp_lexer_consume_token (parser->lexer);
20513
20514       token = cp_lexer_peek_token (parser->lexer);
20515       t = cp_parser_assignment_expression (parser, false);
20516
20517       if (t == error_mark_node)
20518         goto resync_fail;
20519       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20520         error ("%Hschedule %<runtime%> does not take "
20521                "a %<chunk_size%> parameter", &token->location);
20522       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20523         error ("%Hschedule %<auto%> does not take "
20524                "a %<chunk_size%> parameter", &token->location);
20525       else
20526         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20527
20528       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20529         goto resync_fail;
20530     }
20531   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20532     goto resync_fail;
20533
20534   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20535   OMP_CLAUSE_CHAIN (c) = list;
20536   return c;
20537
20538  invalid_kind:
20539   cp_parser_error (parser, "invalid schedule kind");
20540  resync_fail:
20541   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20542                                          /*or_comma=*/false,
20543                                          /*consume_paren=*/true);
20544   return list;
20545 }
20546
20547 /* OpenMP 3.0:
20548    untied */
20549
20550 static tree
20551 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20552                              tree list, location_t location)
20553 {
20554   tree c;
20555
20556   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20557
20558   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20559   OMP_CLAUSE_CHAIN (c) = list;
20560   return c;
20561 }
20562
20563 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20564    is a bitmask in MASK.  Return the list of clauses found; the result
20565    of clause default goes in *pdefault.  */
20566
20567 static tree
20568 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20569                            const char *where, cp_token *pragma_tok)
20570 {
20571   tree clauses = NULL;
20572   bool first = true;
20573   cp_token *token = NULL;
20574
20575   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20576     {
20577       pragma_omp_clause c_kind;
20578       const char *c_name;
20579       tree prev = clauses;
20580
20581       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20582         cp_lexer_consume_token (parser->lexer);
20583
20584       token = cp_lexer_peek_token (parser->lexer);
20585       c_kind = cp_parser_omp_clause_name (parser);
20586       first = false;
20587
20588       switch (c_kind)
20589         {
20590         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20591           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20592                                                    token->location);
20593           c_name = "collapse";
20594           break;
20595         case PRAGMA_OMP_CLAUSE_COPYIN:
20596           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20597           c_name = "copyin";
20598           break;
20599         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20600           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20601                                             clauses);
20602           c_name = "copyprivate";
20603           break;
20604         case PRAGMA_OMP_CLAUSE_DEFAULT:
20605           clauses = cp_parser_omp_clause_default (parser, clauses,
20606                                                   token->location);
20607           c_name = "default";
20608           break;
20609         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20610           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20611                                             clauses);
20612           c_name = "firstprivate";
20613           break;
20614         case PRAGMA_OMP_CLAUSE_IF:
20615           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20616           c_name = "if";
20617           break;
20618         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20619           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20620                                             clauses);
20621           c_name = "lastprivate";
20622           break;
20623         case PRAGMA_OMP_CLAUSE_NOWAIT:
20624           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20625           c_name = "nowait";
20626           break;
20627         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20628           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20629                                                       token->location);
20630           c_name = "num_threads";
20631           break;
20632         case PRAGMA_OMP_CLAUSE_ORDERED:
20633           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20634                                                   token->location);
20635           c_name = "ordered";
20636           break;
20637         case PRAGMA_OMP_CLAUSE_PRIVATE:
20638           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20639                                             clauses);
20640           c_name = "private";
20641           break;
20642         case PRAGMA_OMP_CLAUSE_REDUCTION:
20643           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20644           c_name = "reduction";
20645           break;
20646         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20647           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20648                                                    token->location);
20649           c_name = "schedule";
20650           break;
20651         case PRAGMA_OMP_CLAUSE_SHARED:
20652           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20653                                             clauses);
20654           c_name = "shared";
20655           break;
20656         case PRAGMA_OMP_CLAUSE_UNTIED:
20657           clauses = cp_parser_omp_clause_untied (parser, clauses,
20658                                                  token->location);
20659           c_name = "nowait";
20660           break;
20661         default:
20662           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20663           goto saw_error;
20664         }
20665
20666       if (((mask >> c_kind) & 1) == 0)
20667         {
20668           /* Remove the invalid clause(s) from the list to avoid
20669              confusing the rest of the compiler.  */
20670           clauses = prev;
20671           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20672         }
20673     }
20674  saw_error:
20675   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20676   return finish_omp_clauses (clauses);
20677 }
20678
20679 /* OpenMP 2.5:
20680    structured-block:
20681      statement
20682
20683    In practice, we're also interested in adding the statement to an
20684    outer node.  So it is convenient if we work around the fact that
20685    cp_parser_statement calls add_stmt.  */
20686
20687 static unsigned
20688 cp_parser_begin_omp_structured_block (cp_parser *parser)
20689 {
20690   unsigned save = parser->in_statement;
20691
20692   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20693      This preserves the "not within loop or switch" style error messages
20694      for nonsense cases like
20695         void foo() {
20696         #pragma omp single
20697           break;
20698         }
20699   */
20700   if (parser->in_statement)
20701     parser->in_statement = IN_OMP_BLOCK;
20702
20703   return save;
20704 }
20705
20706 static void
20707 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20708 {
20709   parser->in_statement = save;
20710 }
20711
20712 static tree
20713 cp_parser_omp_structured_block (cp_parser *parser)
20714 {
20715   tree stmt = begin_omp_structured_block ();
20716   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20717
20718   cp_parser_statement (parser, NULL_TREE, false, NULL);
20719
20720   cp_parser_end_omp_structured_block (parser, save);
20721   return finish_omp_structured_block (stmt);
20722 }
20723
20724 /* OpenMP 2.5:
20725    # pragma omp atomic new-line
20726      expression-stmt
20727
20728    expression-stmt:
20729      x binop= expr | x++ | ++x | x-- | --x
20730    binop:
20731      +, *, -, /, &, ^, |, <<, >>
20732
20733   where x is an lvalue expression with scalar type.  */
20734
20735 static void
20736 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20737 {
20738   tree lhs, rhs;
20739   enum tree_code code;
20740
20741   cp_parser_require_pragma_eol (parser, pragma_tok);
20742
20743   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20744                                     /*cast_p=*/false);
20745   switch (TREE_CODE (lhs))
20746     {
20747     case ERROR_MARK:
20748       goto saw_error;
20749
20750     case PREINCREMENT_EXPR:
20751     case POSTINCREMENT_EXPR:
20752       lhs = TREE_OPERAND (lhs, 0);
20753       code = PLUS_EXPR;
20754       rhs = integer_one_node;
20755       break;
20756
20757     case PREDECREMENT_EXPR:
20758     case POSTDECREMENT_EXPR:
20759       lhs = TREE_OPERAND (lhs, 0);
20760       code = MINUS_EXPR;
20761       rhs = integer_one_node;
20762       break;
20763
20764     default:
20765       switch (cp_lexer_peek_token (parser->lexer)->type)
20766         {
20767         case CPP_MULT_EQ:
20768           code = MULT_EXPR;
20769           break;
20770         case CPP_DIV_EQ:
20771           code = TRUNC_DIV_EXPR;
20772           break;
20773         case CPP_PLUS_EQ:
20774           code = PLUS_EXPR;
20775           break;
20776         case CPP_MINUS_EQ:
20777           code = MINUS_EXPR;
20778           break;
20779         case CPP_LSHIFT_EQ:
20780           code = LSHIFT_EXPR;
20781           break;
20782         case CPP_RSHIFT_EQ:
20783           code = RSHIFT_EXPR;
20784           break;
20785         case CPP_AND_EQ:
20786           code = BIT_AND_EXPR;
20787           break;
20788         case CPP_OR_EQ:
20789           code = BIT_IOR_EXPR;
20790           break;
20791         case CPP_XOR_EQ:
20792           code = BIT_XOR_EXPR;
20793           break;
20794         default:
20795           cp_parser_error (parser,
20796                            "invalid operator for %<#pragma omp atomic%>");
20797           goto saw_error;
20798         }
20799       cp_lexer_consume_token (parser->lexer);
20800
20801       rhs = cp_parser_expression (parser, false);
20802       if (rhs == error_mark_node)
20803         goto saw_error;
20804       break;
20805     }
20806   finish_omp_atomic (code, lhs, rhs);
20807   cp_parser_consume_semicolon_at_end_of_statement (parser);
20808   return;
20809
20810  saw_error:
20811   cp_parser_skip_to_end_of_block_or_statement (parser);
20812 }
20813
20814
20815 /* OpenMP 2.5:
20816    # pragma omp barrier new-line  */
20817
20818 static void
20819 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20820 {
20821   cp_parser_require_pragma_eol (parser, pragma_tok);
20822   finish_omp_barrier ();
20823 }
20824
20825 /* OpenMP 2.5:
20826    # pragma omp critical [(name)] new-line
20827      structured-block  */
20828
20829 static tree
20830 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20831 {
20832   tree stmt, name = NULL;
20833
20834   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20835     {
20836       cp_lexer_consume_token (parser->lexer);
20837
20838       name = cp_parser_identifier (parser);
20839
20840       if (name == error_mark_node
20841           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20842         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20843                                                /*or_comma=*/false,
20844                                                /*consume_paren=*/true);
20845       if (name == error_mark_node)
20846         name = NULL;
20847     }
20848   cp_parser_require_pragma_eol (parser, pragma_tok);
20849
20850   stmt = cp_parser_omp_structured_block (parser);
20851   return c_finish_omp_critical (stmt, name);
20852 }
20853
20854 /* OpenMP 2.5:
20855    # pragma omp flush flush-vars[opt] new-line
20856
20857    flush-vars:
20858      ( variable-list ) */
20859
20860 static void
20861 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20862 {
20863   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20864     (void) cp_parser_omp_var_list (parser, 0, NULL);
20865   cp_parser_require_pragma_eol (parser, pragma_tok);
20866
20867   finish_omp_flush ();
20868 }
20869
20870 /* Helper function, to parse omp for increment expression.  */
20871
20872 static tree
20873 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20874 {
20875   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20876   enum tree_code op;
20877   cp_token *token;
20878
20879   if (lhs != decl)
20880     {
20881       cp_parser_skip_to_end_of_statement (parser);
20882       return error_mark_node;
20883     }
20884
20885   token = cp_lexer_peek_token (parser->lexer);
20886   op = binops_by_token [token->type].tree_type;
20887   switch (op)
20888     {
20889     case LT_EXPR:
20890     case LE_EXPR:
20891     case GT_EXPR:
20892     case GE_EXPR:
20893       break;
20894     default:
20895       cp_parser_skip_to_end_of_statement (parser);
20896       return error_mark_node;
20897     }
20898
20899   cp_lexer_consume_token (parser->lexer);
20900   rhs = cp_parser_binary_expression (parser, false,
20901                                      PREC_RELATIONAL_EXPRESSION);
20902   if (rhs == error_mark_node
20903       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20904     {
20905       cp_parser_skip_to_end_of_statement (parser);
20906       return error_mark_node;
20907     }
20908
20909   return build2 (op, boolean_type_node, lhs, rhs);
20910 }
20911
20912 /* Helper function, to parse omp for increment expression.  */
20913
20914 static tree
20915 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20916 {
20917   cp_token *token = cp_lexer_peek_token (parser->lexer);
20918   enum tree_code op;
20919   tree lhs, rhs;
20920   cp_id_kind idk;
20921   bool decl_first;
20922
20923   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20924     {
20925       op = (token->type == CPP_PLUS_PLUS
20926             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20927       cp_lexer_consume_token (parser->lexer);
20928       lhs = cp_parser_cast_expression (parser, false, false);
20929       if (lhs != decl)
20930         return error_mark_node;
20931       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20932     }
20933
20934   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20935   if (lhs != decl)
20936     return error_mark_node;
20937
20938   token = cp_lexer_peek_token (parser->lexer);
20939   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20940     {
20941       op = (token->type == CPP_PLUS_PLUS
20942             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20943       cp_lexer_consume_token (parser->lexer);
20944       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20945     }
20946
20947   op = cp_parser_assignment_operator_opt (parser);
20948   if (op == ERROR_MARK)
20949     return error_mark_node;
20950
20951   if (op != NOP_EXPR)
20952     {
20953       rhs = cp_parser_assignment_expression (parser, false);
20954       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20955       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20956     }
20957
20958   lhs = cp_parser_binary_expression (parser, false,
20959                                      PREC_ADDITIVE_EXPRESSION);
20960   token = cp_lexer_peek_token (parser->lexer);
20961   decl_first = lhs == decl;
20962   if (decl_first)
20963     lhs = NULL_TREE;
20964   if (token->type != CPP_PLUS
20965       && token->type != CPP_MINUS)
20966     return error_mark_node;
20967
20968   do
20969     {
20970       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20971       cp_lexer_consume_token (parser->lexer);
20972       rhs = cp_parser_binary_expression (parser, false,
20973                                          PREC_ADDITIVE_EXPRESSION);
20974       token = cp_lexer_peek_token (parser->lexer);
20975       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20976         {
20977           if (lhs == NULL_TREE)
20978             {
20979               if (op == PLUS_EXPR)
20980                 lhs = rhs;
20981               else
20982                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20983             }
20984           else
20985             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20986                                      NULL, tf_warning_or_error);
20987         }
20988     }
20989   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20990
20991   if (!decl_first)
20992     {
20993       if (rhs != decl || op == MINUS_EXPR)
20994         return error_mark_node;
20995       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20996     }
20997   else
20998     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20999
21000   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21001 }
21002
21003 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21004
21005 static tree
21006 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21007 {
21008   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21009   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21010   tree this_pre_body, cl;
21011   location_t loc_first;
21012   bool collapse_err = false;
21013   int i, collapse = 1, nbraces = 0;
21014
21015   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21016     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21017       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21018
21019   gcc_assert (collapse >= 1);
21020
21021   declv = make_tree_vec (collapse);
21022   initv = make_tree_vec (collapse);
21023   condv = make_tree_vec (collapse);
21024   incrv = make_tree_vec (collapse);
21025
21026   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21027
21028   for (i = 0; i < collapse; i++)
21029     {
21030       int bracecount = 0;
21031       bool add_private_clause = false;
21032       location_t loc;
21033
21034       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21035         {
21036           cp_parser_error (parser, "for statement expected");
21037           return NULL;
21038         }
21039       loc = cp_lexer_consume_token (parser->lexer)->location;
21040
21041       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21042         return NULL;
21043
21044       init = decl = real_decl = NULL;
21045       this_pre_body = push_stmt_list ();
21046       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21047         {
21048           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21049
21050              init-expr:
21051                        var = lb
21052                        integer-type var = lb
21053                        random-access-iterator-type var = lb
21054                        pointer-type var = lb
21055           */
21056           cp_decl_specifier_seq type_specifiers;
21057
21058           /* First, try to parse as an initialized declaration.  See
21059              cp_parser_condition, from whence the bulk of this is copied.  */
21060
21061           cp_parser_parse_tentatively (parser);
21062           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21063                                         &type_specifiers);
21064           if (cp_parser_parse_definitely (parser))
21065             {
21066               /* If parsing a type specifier seq succeeded, then this
21067                  MUST be a initialized declaration.  */
21068               tree asm_specification, attributes;
21069               cp_declarator *declarator;
21070
21071               declarator = cp_parser_declarator (parser,
21072                                                  CP_PARSER_DECLARATOR_NAMED,
21073                                                  /*ctor_dtor_or_conv_p=*/NULL,
21074                                                  /*parenthesized_p=*/NULL,
21075                                                  /*member_p=*/false);
21076               attributes = cp_parser_attributes_opt (parser);
21077               asm_specification = cp_parser_asm_specification_opt (parser);
21078
21079               if (declarator == cp_error_declarator) 
21080                 cp_parser_skip_to_end_of_statement (parser);
21081
21082               else 
21083                 {
21084                   tree pushed_scope;
21085
21086                   decl = start_decl (declarator, &type_specifiers,
21087                                      /*initialized_p=*/false, attributes,
21088                                      /*prefix_attributes=*/NULL_TREE,
21089                                      &pushed_scope);
21090
21091                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21092                     {
21093                       if (cp_lexer_next_token_is (parser->lexer, 
21094                                                   CPP_OPEN_PAREN))
21095                         error ("parenthesized initialization is not allowed in "
21096                                "OpenMP %<for%> loop");
21097                       else
21098                         /* Trigger an error.  */
21099                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21100
21101                       init = error_mark_node;
21102                       cp_parser_skip_to_end_of_statement (parser);
21103                     }
21104                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21105                            || type_dependent_expression_p (decl))
21106                     {
21107                       bool is_direct_init, is_non_constant_init;
21108
21109                       init = cp_parser_initializer (parser,
21110                                                     &is_direct_init,
21111                                                     &is_non_constant_init);
21112
21113                       cp_finish_decl (decl, init, !is_non_constant_init,
21114                                       asm_specification,
21115                                       LOOKUP_ONLYCONVERTING);
21116                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21117                         {
21118                           for_block
21119                             = tree_cons (NULL, this_pre_body, for_block);
21120                           init = NULL_TREE;
21121                         }
21122                       else
21123                         init = pop_stmt_list (this_pre_body);
21124                       this_pre_body = NULL_TREE;
21125                     }
21126                   else
21127                     {
21128                       /* Consume '='.  */
21129                       cp_lexer_consume_token (parser->lexer);
21130                       init = cp_parser_assignment_expression (parser, false);
21131
21132                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21133                         init = error_mark_node;
21134                       else
21135                         cp_finish_decl (decl, NULL_TREE,
21136                                         /*init_const_expr_p=*/false,
21137                                         asm_specification,
21138                                         LOOKUP_ONLYCONVERTING);
21139                     }
21140
21141                   if (pushed_scope)
21142                     pop_scope (pushed_scope);
21143                 }
21144             }
21145           else 
21146             {
21147               cp_id_kind idk;
21148               /* If parsing a type specifier sequence failed, then
21149                  this MUST be a simple expression.  */
21150               cp_parser_parse_tentatively (parser);
21151               decl = cp_parser_primary_expression (parser, false, false,
21152                                                    false, &idk);
21153               if (!cp_parser_error_occurred (parser)
21154                   && decl
21155                   && DECL_P (decl)
21156                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21157                 {
21158                   tree rhs;
21159
21160                   cp_parser_parse_definitely (parser);
21161                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21162                   rhs = cp_parser_assignment_expression (parser, false);
21163                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21164                                                          rhs,
21165                                                          tf_warning_or_error));
21166                   add_private_clause = true;
21167                 }
21168               else
21169                 {
21170                   decl = NULL;
21171                   cp_parser_abort_tentative_parse (parser);
21172                   init = cp_parser_expression (parser, false);
21173                   if (init)
21174                     {
21175                       if (TREE_CODE (init) == MODIFY_EXPR
21176                           || TREE_CODE (init) == MODOP_EXPR)
21177                         real_decl = TREE_OPERAND (init, 0);
21178                     }
21179                 }
21180             }
21181         }
21182       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21183       if (this_pre_body)
21184         {
21185           this_pre_body = pop_stmt_list (this_pre_body);
21186           if (pre_body)
21187             {
21188               tree t = pre_body;
21189               pre_body = push_stmt_list ();
21190               add_stmt (t);
21191               add_stmt (this_pre_body);
21192               pre_body = pop_stmt_list (pre_body);
21193             }
21194           else
21195             pre_body = this_pre_body;
21196         }
21197
21198       if (decl)
21199         real_decl = decl;
21200       if (par_clauses != NULL && real_decl != NULL_TREE)
21201         {
21202           tree *c;
21203           for (c = par_clauses; *c ; )
21204             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21205                 && OMP_CLAUSE_DECL (*c) == real_decl)
21206               {
21207                 error ("%Hiteration variable %qD should not be firstprivate",
21208                        &loc, real_decl);
21209                 *c = OMP_CLAUSE_CHAIN (*c);
21210               }
21211             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21212                      && OMP_CLAUSE_DECL (*c) == real_decl)
21213               {
21214                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21215                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21216                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21217                 OMP_CLAUSE_DECL (l) = real_decl;
21218                 OMP_CLAUSE_CHAIN (l) = clauses;
21219                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21220                 clauses = l;
21221                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21222                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21223                 add_private_clause = false;
21224               }
21225             else
21226               {
21227                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21228                     && OMP_CLAUSE_DECL (*c) == real_decl)
21229                   add_private_clause = false;
21230                 c = &OMP_CLAUSE_CHAIN (*c);
21231               }
21232         }
21233
21234       if (add_private_clause)
21235         {
21236           tree c;
21237           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21238             {
21239               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21240                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21241                   && OMP_CLAUSE_DECL (c) == decl)
21242                 break;
21243               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21244                        && OMP_CLAUSE_DECL (c) == decl)
21245                 error ("%Hiteration variable %qD should not be firstprivate",
21246                        &loc, decl);
21247               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21248                        && OMP_CLAUSE_DECL (c) == decl)
21249                 error ("%Hiteration variable %qD should not be reduction",
21250                        &loc, decl);
21251             }
21252           if (c == NULL)
21253             {
21254               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21255               OMP_CLAUSE_DECL (c) = decl;
21256               c = finish_omp_clauses (c);
21257               if (c)
21258                 {
21259                   OMP_CLAUSE_CHAIN (c) = clauses;
21260                   clauses = c;
21261                 }
21262             }
21263         }
21264
21265       cond = NULL;
21266       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21267         {
21268           /* If decl is an iterator, preserve LHS and RHS of the relational
21269              expr until finish_omp_for.  */
21270           if (decl
21271               && (type_dependent_expression_p (decl)
21272                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21273             cond = cp_parser_omp_for_cond (parser, decl);
21274           else
21275             cond = cp_parser_condition (parser);
21276         }
21277       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21278
21279       incr = NULL;
21280       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21281         {
21282           /* If decl is an iterator, preserve the operator on decl
21283              until finish_omp_for.  */
21284           if (decl
21285               && (type_dependent_expression_p (decl)
21286                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21287             incr = cp_parser_omp_for_incr (parser, decl);
21288           else
21289             incr = cp_parser_expression (parser, false);
21290         }
21291
21292       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21293         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21294                                                /*or_comma=*/false,
21295                                                /*consume_paren=*/true);
21296
21297       TREE_VEC_ELT (declv, i) = decl;
21298       TREE_VEC_ELT (initv, i) = init;
21299       TREE_VEC_ELT (condv, i) = cond;
21300       TREE_VEC_ELT (incrv, i) = incr;
21301
21302       if (i == collapse - 1)
21303         break;
21304
21305       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21306          in between the collapsed for loops to be still considered perfectly
21307          nested.  Hopefully the final version clarifies this.
21308          For now handle (multiple) {'s and empty statements.  */
21309       cp_parser_parse_tentatively (parser);
21310       do
21311         {
21312           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21313             break;
21314           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21315             {
21316               cp_lexer_consume_token (parser->lexer);
21317               bracecount++;
21318             }
21319           else if (bracecount
21320                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21321             cp_lexer_consume_token (parser->lexer);
21322           else
21323             {
21324               loc = cp_lexer_peek_token (parser->lexer)->location;
21325               error ("%Hnot enough collapsed for loops", &loc);
21326               collapse_err = true;
21327               cp_parser_abort_tentative_parse (parser);
21328               declv = NULL_TREE;
21329               break;
21330             }
21331         }
21332       while (1);
21333
21334       if (declv)
21335         {
21336           cp_parser_parse_definitely (parser);
21337           nbraces += bracecount;
21338         }
21339     }
21340
21341   /* Note that we saved the original contents of this flag when we entered
21342      the structured block, and so we don't need to re-save it here.  */
21343   parser->in_statement = IN_OMP_FOR;
21344
21345   /* Note that the grammar doesn't call for a structured block here,
21346      though the loop as a whole is a structured block.  */
21347   body = push_stmt_list ();
21348   cp_parser_statement (parser, NULL_TREE, false, NULL);
21349   body = pop_stmt_list (body);
21350
21351   if (declv == NULL_TREE)
21352     ret = NULL_TREE;
21353   else
21354     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21355                           pre_body, clauses);
21356
21357   while (nbraces)
21358     {
21359       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21360         {
21361           cp_lexer_consume_token (parser->lexer);
21362           nbraces--;
21363         }
21364       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21365         cp_lexer_consume_token (parser->lexer);
21366       else
21367         {
21368           if (!collapse_err)
21369             {
21370               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21371               error ("%Hcollapsed loops not perfectly nested", &loc);
21372             }
21373           collapse_err = true;
21374           cp_parser_statement_seq_opt (parser, NULL);
21375           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21376         }
21377     }
21378
21379   while (for_block)
21380     {
21381       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21382       for_block = TREE_CHAIN (for_block);
21383     }
21384
21385   return ret;
21386 }
21387
21388 /* OpenMP 2.5:
21389    #pragma omp for for-clause[optseq] new-line
21390      for-loop  */
21391
21392 #define OMP_FOR_CLAUSE_MASK                             \
21393         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21394         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21395         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21396         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21397         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21398         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21399         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21400         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21401
21402 static tree
21403 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21404 {
21405   tree clauses, sb, ret;
21406   unsigned int save;
21407
21408   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21409                                        "#pragma omp for", pragma_tok);
21410
21411   sb = begin_omp_structured_block ();
21412   save = cp_parser_begin_omp_structured_block (parser);
21413
21414   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21415
21416   cp_parser_end_omp_structured_block (parser, save);
21417   add_stmt (finish_omp_structured_block (sb));
21418
21419   return ret;
21420 }
21421
21422 /* OpenMP 2.5:
21423    # pragma omp master new-line
21424      structured-block  */
21425
21426 static tree
21427 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21428 {
21429   cp_parser_require_pragma_eol (parser, pragma_tok);
21430   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21431 }
21432
21433 /* OpenMP 2.5:
21434    # pragma omp ordered new-line
21435      structured-block  */
21436
21437 static tree
21438 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21439 {
21440   cp_parser_require_pragma_eol (parser, pragma_tok);
21441   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21442 }
21443
21444 /* OpenMP 2.5:
21445
21446    section-scope:
21447      { section-sequence }
21448
21449    section-sequence:
21450      section-directive[opt] structured-block
21451      section-sequence section-directive structured-block  */
21452
21453 static tree
21454 cp_parser_omp_sections_scope (cp_parser *parser)
21455 {
21456   tree stmt, substmt;
21457   bool error_suppress = false;
21458   cp_token *tok;
21459
21460   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21461     return NULL_TREE;
21462
21463   stmt = push_stmt_list ();
21464
21465   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21466     {
21467       unsigned save;
21468
21469       substmt = begin_omp_structured_block ();
21470       save = cp_parser_begin_omp_structured_block (parser);
21471
21472       while (1)
21473         {
21474           cp_parser_statement (parser, NULL_TREE, false, NULL);
21475
21476           tok = cp_lexer_peek_token (parser->lexer);
21477           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21478             break;
21479           if (tok->type == CPP_CLOSE_BRACE)
21480             break;
21481           if (tok->type == CPP_EOF)
21482             break;
21483         }
21484
21485       cp_parser_end_omp_structured_block (parser, save);
21486       substmt = finish_omp_structured_block (substmt);
21487       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21488       add_stmt (substmt);
21489     }
21490
21491   while (1)
21492     {
21493       tok = cp_lexer_peek_token (parser->lexer);
21494       if (tok->type == CPP_CLOSE_BRACE)
21495         break;
21496       if (tok->type == CPP_EOF)
21497         break;
21498
21499       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21500         {
21501           cp_lexer_consume_token (parser->lexer);
21502           cp_parser_require_pragma_eol (parser, tok);
21503           error_suppress = false;
21504         }
21505       else if (!error_suppress)
21506         {
21507           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21508           error_suppress = true;
21509         }
21510
21511       substmt = cp_parser_omp_structured_block (parser);
21512       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21513       add_stmt (substmt);
21514     }
21515   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21516
21517   substmt = pop_stmt_list (stmt);
21518
21519   stmt = make_node (OMP_SECTIONS);
21520   TREE_TYPE (stmt) = void_type_node;
21521   OMP_SECTIONS_BODY (stmt) = substmt;
21522
21523   add_stmt (stmt);
21524   return stmt;
21525 }
21526
21527 /* OpenMP 2.5:
21528    # pragma omp sections sections-clause[optseq] newline
21529      sections-scope  */
21530
21531 #define OMP_SECTIONS_CLAUSE_MASK                        \
21532         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21533         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21534         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21535         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21536         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21537
21538 static tree
21539 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21540 {
21541   tree clauses, ret;
21542
21543   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21544                                        "#pragma omp sections", pragma_tok);
21545
21546   ret = cp_parser_omp_sections_scope (parser);
21547   if (ret)
21548     OMP_SECTIONS_CLAUSES (ret) = clauses;
21549
21550   return ret;
21551 }
21552
21553 /* OpenMP 2.5:
21554    # pragma parallel parallel-clause new-line
21555    # pragma parallel for parallel-for-clause new-line
21556    # pragma parallel sections parallel-sections-clause new-line  */
21557
21558 #define OMP_PARALLEL_CLAUSE_MASK                        \
21559         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21560         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21561         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21562         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21563         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21564         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21565         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21566         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21567
21568 static tree
21569 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21570 {
21571   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21572   const char *p_name = "#pragma omp parallel";
21573   tree stmt, clauses, par_clause, ws_clause, block;
21574   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21575   unsigned int save;
21576
21577   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21578     {
21579       cp_lexer_consume_token (parser->lexer);
21580       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21581       p_name = "#pragma omp parallel for";
21582       mask |= OMP_FOR_CLAUSE_MASK;
21583       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21584     }
21585   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21586     {
21587       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21588       const char *p = IDENTIFIER_POINTER (id);
21589       if (strcmp (p, "sections") == 0)
21590         {
21591           cp_lexer_consume_token (parser->lexer);
21592           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21593           p_name = "#pragma omp parallel sections";
21594           mask |= OMP_SECTIONS_CLAUSE_MASK;
21595           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21596         }
21597     }
21598
21599   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21600   block = begin_omp_parallel ();
21601   save = cp_parser_begin_omp_structured_block (parser);
21602
21603   switch (p_kind)
21604     {
21605     case PRAGMA_OMP_PARALLEL:
21606       cp_parser_statement (parser, NULL_TREE, false, NULL);
21607       par_clause = clauses;
21608       break;
21609
21610     case PRAGMA_OMP_PARALLEL_FOR:
21611       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21612       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21613       break;
21614
21615     case PRAGMA_OMP_PARALLEL_SECTIONS:
21616       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21617       stmt = cp_parser_omp_sections_scope (parser);
21618       if (stmt)
21619         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21620       break;
21621
21622     default:
21623       gcc_unreachable ();
21624     }
21625
21626   cp_parser_end_omp_structured_block (parser, save);
21627   stmt = finish_omp_parallel (par_clause, block);
21628   if (p_kind != PRAGMA_OMP_PARALLEL)
21629     OMP_PARALLEL_COMBINED (stmt) = 1;
21630   return stmt;
21631 }
21632
21633 /* OpenMP 2.5:
21634    # pragma omp single single-clause[optseq] new-line
21635      structured-block  */
21636
21637 #define OMP_SINGLE_CLAUSE_MASK                          \
21638         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21639         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21640         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21641         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21642
21643 static tree
21644 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21645 {
21646   tree stmt = make_node (OMP_SINGLE);
21647   TREE_TYPE (stmt) = void_type_node;
21648
21649   OMP_SINGLE_CLAUSES (stmt)
21650     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21651                                  "#pragma omp single", pragma_tok);
21652   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21653
21654   return add_stmt (stmt);
21655 }
21656
21657 /* OpenMP 3.0:
21658    # pragma omp task task-clause[optseq] new-line
21659      structured-block  */
21660
21661 #define OMP_TASK_CLAUSE_MASK                            \
21662         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21663         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21664         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21665         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21666         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21667         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21668
21669 static tree
21670 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21671 {
21672   tree clauses, block;
21673   unsigned int save;
21674
21675   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21676                                        "#pragma omp task", pragma_tok);
21677   block = begin_omp_task ();
21678   save = cp_parser_begin_omp_structured_block (parser);
21679   cp_parser_statement (parser, NULL_TREE, false, NULL);
21680   cp_parser_end_omp_structured_block (parser, save);
21681   return finish_omp_task (clauses, block);
21682 }
21683
21684 /* OpenMP 3.0:
21685    # pragma omp taskwait new-line  */
21686
21687 static void
21688 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21689 {
21690   cp_parser_require_pragma_eol (parser, pragma_tok);
21691   finish_omp_taskwait ();
21692 }
21693
21694 /* OpenMP 2.5:
21695    # pragma omp threadprivate (variable-list) */
21696
21697 static void
21698 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21699 {
21700   tree vars;
21701
21702   vars = cp_parser_omp_var_list (parser, 0, NULL);
21703   cp_parser_require_pragma_eol (parser, pragma_tok);
21704
21705   finish_omp_threadprivate (vars);
21706 }
21707
21708 /* Main entry point to OpenMP statement pragmas.  */
21709
21710 static void
21711 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21712 {
21713   tree stmt;
21714
21715   switch (pragma_tok->pragma_kind)
21716     {
21717     case PRAGMA_OMP_ATOMIC:
21718       cp_parser_omp_atomic (parser, pragma_tok);
21719       return;
21720     case PRAGMA_OMP_CRITICAL:
21721       stmt = cp_parser_omp_critical (parser, pragma_tok);
21722       break;
21723     case PRAGMA_OMP_FOR:
21724       stmt = cp_parser_omp_for (parser, pragma_tok);
21725       break;
21726     case PRAGMA_OMP_MASTER:
21727       stmt = cp_parser_omp_master (parser, pragma_tok);
21728       break;
21729     case PRAGMA_OMP_ORDERED:
21730       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21731       break;
21732     case PRAGMA_OMP_PARALLEL:
21733       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21734       break;
21735     case PRAGMA_OMP_SECTIONS:
21736       stmt = cp_parser_omp_sections (parser, pragma_tok);
21737       break;
21738     case PRAGMA_OMP_SINGLE:
21739       stmt = cp_parser_omp_single (parser, pragma_tok);
21740       break;
21741     case PRAGMA_OMP_TASK:
21742       stmt = cp_parser_omp_task (parser, pragma_tok);
21743       break;
21744     default:
21745       gcc_unreachable ();
21746     }
21747
21748   if (stmt)
21749     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21750 }
21751 \f
21752 /* The parser.  */
21753
21754 static GTY (()) cp_parser *the_parser;
21755
21756 \f
21757 /* Special handling for the first token or line in the file.  The first
21758    thing in the file might be #pragma GCC pch_preprocess, which loads a
21759    PCH file, which is a GC collection point.  So we need to handle this
21760    first pragma without benefit of an existing lexer structure.
21761
21762    Always returns one token to the caller in *FIRST_TOKEN.  This is
21763    either the true first token of the file, or the first token after
21764    the initial pragma.  */
21765
21766 static void
21767 cp_parser_initial_pragma (cp_token *first_token)
21768 {
21769   tree name = NULL;
21770
21771   cp_lexer_get_preprocessor_token (NULL, first_token);
21772   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21773     return;
21774
21775   cp_lexer_get_preprocessor_token (NULL, first_token);
21776   if (first_token->type == CPP_STRING)
21777     {
21778       name = first_token->u.value;
21779
21780       cp_lexer_get_preprocessor_token (NULL, first_token);
21781       if (first_token->type != CPP_PRAGMA_EOL)
21782         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21783                &first_token->location);
21784     }
21785   else
21786     error ("%Hexpected string literal", &first_token->location);
21787
21788   /* Skip to the end of the pragma.  */
21789   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21790     cp_lexer_get_preprocessor_token (NULL, first_token);
21791
21792   /* Now actually load the PCH file.  */
21793   if (name)
21794     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21795
21796   /* Read one more token to return to our caller.  We have to do this
21797      after reading the PCH file in, since its pointers have to be
21798      live.  */
21799   cp_lexer_get_preprocessor_token (NULL, first_token);
21800 }
21801
21802 /* Normal parsing of a pragma token.  Here we can (and must) use the
21803    regular lexer.  */
21804
21805 static bool
21806 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21807 {
21808   cp_token *pragma_tok;
21809   unsigned int id;
21810
21811   pragma_tok = cp_lexer_consume_token (parser->lexer);
21812   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21813   parser->lexer->in_pragma = true;
21814
21815   id = pragma_tok->pragma_kind;
21816   switch (id)
21817     {
21818     case PRAGMA_GCC_PCH_PREPROCESS:
21819       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21820              &pragma_tok->location);
21821       break;
21822
21823     case PRAGMA_OMP_BARRIER:
21824       switch (context)
21825         {
21826         case pragma_compound:
21827           cp_parser_omp_barrier (parser, pragma_tok);
21828           return false;
21829         case pragma_stmt:
21830           error ("%H%<#pragma omp barrier%> may only be "
21831                  "used in compound statements", &pragma_tok->location);
21832           break;
21833         default:
21834           goto bad_stmt;
21835         }
21836       break;
21837
21838     case PRAGMA_OMP_FLUSH:
21839       switch (context)
21840         {
21841         case pragma_compound:
21842           cp_parser_omp_flush (parser, pragma_tok);
21843           return false;
21844         case pragma_stmt:
21845           error ("%H%<#pragma omp flush%> may only be "
21846                  "used in compound statements", &pragma_tok->location);
21847           break;
21848         default:
21849           goto bad_stmt;
21850         }
21851       break;
21852
21853     case PRAGMA_OMP_TASKWAIT:
21854       switch (context)
21855         {
21856         case pragma_compound:
21857           cp_parser_omp_taskwait (parser, pragma_tok);
21858           return false;
21859         case pragma_stmt:
21860           error ("%H%<#pragma omp taskwait%> may only be "
21861                  "used in compound statements",
21862                  &pragma_tok->location);
21863           break;
21864         default:
21865           goto bad_stmt;
21866         }
21867       break;
21868
21869     case PRAGMA_OMP_THREADPRIVATE:
21870       cp_parser_omp_threadprivate (parser, pragma_tok);
21871       return false;
21872
21873     case PRAGMA_OMP_ATOMIC:
21874     case PRAGMA_OMP_CRITICAL:
21875     case PRAGMA_OMP_FOR:
21876     case PRAGMA_OMP_MASTER:
21877     case PRAGMA_OMP_ORDERED:
21878     case PRAGMA_OMP_PARALLEL:
21879     case PRAGMA_OMP_SECTIONS:
21880     case PRAGMA_OMP_SINGLE:
21881     case PRAGMA_OMP_TASK:
21882       if (context == pragma_external)
21883         goto bad_stmt;
21884       cp_parser_omp_construct (parser, pragma_tok);
21885       return true;
21886
21887     case PRAGMA_OMP_SECTION:
21888       error ("%H%<#pragma omp section%> may only be used in "
21889              "%<#pragma omp sections%> construct", &pragma_tok->location);
21890       break;
21891
21892     default:
21893       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21894       c_invoke_pragma_handler (id);
21895       break;
21896
21897     bad_stmt:
21898       cp_parser_error (parser, "expected declaration specifiers");
21899       break;
21900     }
21901
21902   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21903   return false;
21904 }
21905
21906 /* The interface the pragma parsers have to the lexer.  */
21907
21908 enum cpp_ttype
21909 pragma_lex (tree *value)
21910 {
21911   cp_token *tok;
21912   enum cpp_ttype ret;
21913
21914   tok = cp_lexer_peek_token (the_parser->lexer);
21915
21916   ret = tok->type;
21917   *value = tok->u.value;
21918
21919   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21920     ret = CPP_EOF;
21921   else if (ret == CPP_STRING)
21922     *value = cp_parser_string_literal (the_parser, false, false);
21923   else
21924     {
21925       cp_lexer_consume_token (the_parser->lexer);
21926       if (ret == CPP_KEYWORD)
21927         ret = CPP_NAME;
21928     }
21929
21930   return ret;
21931 }
21932
21933 \f
21934 /* External interface.  */
21935
21936 /* Parse one entire translation unit.  */
21937
21938 void
21939 c_parse_file (void)
21940 {
21941   bool error_occurred;
21942   static bool already_called = false;
21943
21944   if (already_called)
21945     {
21946       sorry ("inter-module optimizations not implemented for C++");
21947       return;
21948     }
21949   already_called = true;
21950
21951   the_parser = cp_parser_new ();
21952   push_deferring_access_checks (flag_access_control
21953                                 ? dk_no_deferred : dk_no_check);
21954   error_occurred = cp_parser_translation_unit (the_parser);
21955   the_parser = NULL;
21956 }
21957
21958 #include "gt-cp-parser.h"