OSDN Git Service

Implement C++0x 'auto' semantics.
[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 *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       cp_parameter_declarator *parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification)
1019 {
1020   cp_declarator *declarator;
1021
1022   declarator = make_declarator (cdk_function);
1023   declarator->declarator = target;
1024   declarator->u.function.parameters = parms;
1025   declarator->u.function.qualifiers = cv_qualifiers;
1026   declarator->u.function.exception_specification = exception_specification;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_qualifying_entity
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_declarator_id
1730   (cp_parser *, bool);
1731 static tree cp_parser_type_id
1732   (cp_parser *);
1733 static void cp_parser_type_specifier_seq
1734   (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1736   (cp_parser *);
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738   (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740   (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument 
1742   (cp_parser *, bool);
1743 static void cp_parser_function_body
1744   (cp_parser *);
1745 static tree cp_parser_initializer
1746   (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748   (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750   (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752   (cp_parser *, bool *);
1753
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1755   (cp_parser *);
1756
1757 /* Classes [gram.class] */
1758
1759 static tree cp_parser_class_name
1760   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1762   (cp_parser *);
1763 static tree cp_parser_class_head
1764   (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1766   (cp_parser *);
1767 static void cp_parser_member_specification_opt
1768   (cp_parser *);
1769 static void cp_parser_member_declaration
1770   (cp_parser *);
1771 static tree cp_parser_pure_specifier
1772   (cp_parser *);
1773 static tree cp_parser_constant_initializer
1774   (cp_parser *);
1775
1776 /* Derived classes [gram.class.derived] */
1777
1778 static tree cp_parser_base_clause
1779   (cp_parser *);
1780 static tree cp_parser_base_specifier
1781   (cp_parser *);
1782
1783 /* Special member functions [gram.special] */
1784
1785 static tree cp_parser_conversion_function_id
1786   (cp_parser *);
1787 static tree cp_parser_conversion_type_id
1788   (cp_parser *);
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1790   (cp_parser *);
1791 static bool cp_parser_ctor_initializer_opt
1792   (cp_parser *);
1793 static void cp_parser_mem_initializer_list
1794   (cp_parser *);
1795 static tree cp_parser_mem_initializer
1796   (cp_parser *);
1797 static tree cp_parser_mem_initializer_id
1798   (cp_parser *);
1799
1800 /* Overloading [gram.over] */
1801
1802 static tree cp_parser_operator_function_id
1803   (cp_parser *);
1804 static tree cp_parser_operator
1805   (cp_parser *);
1806
1807 /* Templates [gram.temp] */
1808
1809 static void cp_parser_template_declaration
1810   (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1812   (cp_parser *);
1813 static tree cp_parser_template_parameter
1814   (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816   (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818   (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820   (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1822   (cp_parser *);
1823 static tree cp_parser_template_argument
1824   (cp_parser *);
1825 static void cp_parser_explicit_instantiation
1826   (cp_parser *);
1827 static void cp_parser_explicit_specialization
1828   (cp_parser *);
1829
1830 /* Exception handling [gram.exception] */
1831
1832 static tree cp_parser_try_block
1833   (cp_parser *);
1834 static bool cp_parser_function_try_block
1835   (cp_parser *);
1836 static void cp_parser_handler_seq
1837   (cp_parser *);
1838 static void cp_parser_handler
1839   (cp_parser *);
1840 static tree cp_parser_exception_declaration
1841   (cp_parser *);
1842 static tree cp_parser_throw_expression
1843   (cp_parser *);
1844 static tree cp_parser_exception_specification_opt
1845   (cp_parser *);
1846 static tree cp_parser_type_id_list
1847   (cp_parser *);
1848
1849 /* GNU Extensions */
1850
1851 static tree cp_parser_asm_specification_opt
1852   (cp_parser *);
1853 static tree cp_parser_asm_operand_list
1854   (cp_parser *);
1855 static tree cp_parser_asm_clobber_list
1856   (cp_parser *);
1857 static tree cp_parser_attributes_opt
1858   (cp_parser *);
1859 static tree cp_parser_attribute_list
1860   (cp_parser *);
1861 static bool cp_parser_extension_opt
1862   (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1864   (cp_parser *);
1865
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868   (cp_parser *, enum pragma_context);
1869
1870 /* Objective-C++ Productions */
1871
1872 static tree cp_parser_objc_message_receiver
1873   (cp_parser *);
1874 static tree cp_parser_objc_message_args
1875   (cp_parser *);
1876 static tree cp_parser_objc_message_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_encode_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_defs_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_protocol_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_selector_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_expression
1887   (cp_parser *);
1888 static bool cp_parser_objc_selector_p
1889   (enum cpp_ttype);
1890 static tree cp_parser_objc_selector
1891   (cp_parser *);
1892 static tree cp_parser_objc_protocol_refs_opt
1893   (cp_parser *);
1894 static void cp_parser_objc_declaration
1895   (cp_parser *);
1896 static tree cp_parser_objc_statement
1897   (cp_parser *);
1898
1899 /* Utility Routines */
1900
1901 static tree cp_parser_lookup_name
1902   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904   (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1906   (tree, bool);
1907 static bool cp_parser_check_declarator_template_parameters
1908   (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910   (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1912   (cp_parser *);
1913 static tree cp_parser_global_scope_opt
1914   (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916   (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920   (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922   (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924   (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928   (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1932   (cp_parser *);
1933 static void cp_parser_save_default_args
1934   (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936   (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938   (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940   (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942   (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1944   (cp_parser *);
1945 static void cp_parser_set_storage_class
1946   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948   (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950   (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952   (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954   (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1956   (cp_token *);
1957 static bool cp_parser_next_token_starts_class_definition_p
1958   (cp_parser *);
1959 static bool cp_parser_next_token_ends_template_argument_p
1960   (cp_parser *);
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962   (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1964   (cp_token *);
1965 static void cp_parser_check_class_key
1966   (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968   (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1970   (cp_parser *);
1971 static void cp_parser_pre_parsed_nested_name_specifier
1972   (cp_parser *);
1973 static bool cp_parser_cache_group
1974   (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1976   (cp_parser *);
1977 static void cp_parser_commit_to_tentative_parse
1978   (cp_parser *);
1979 static void cp_parser_abort_tentative_parse
1980   (cp_parser *);
1981 static bool cp_parser_parse_definitely
1982   (cp_parser *);
1983 static inline bool cp_parser_parsing_tentatively
1984   (cp_parser *);
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1986   (cp_parser *);
1987 static void cp_parser_error
1988   (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990   (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1992   (cp_parser *);
1993 static bool cp_parser_check_type_definition
1994   (cp_parser *);
1995 static void cp_parser_check_for_definition_in_return_type
1996   (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998   (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000   (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002   (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2004   (cp_parser *);
2005 static int cp_parser_skip_to_closing_parenthesis
2006   (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2008   (cp_parser *);
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2010   (cp_parser *);
2011 static void cp_parser_skip_to_end_of_block_or_statement
2012   (cp_parser *);
2013 static bool cp_parser_skip_to_closing_brace
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2016   (cp_parser *);
2017 static void cp_parser_skip_to_pragma_eol
2018   (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2020   (cp_parser *);
2021 static bool cp_parser_allow_gnu_extensions_p
2022   (cp_parser *);
2023 static bool cp_parser_is_string_literal
2024   (cp_token *);
2025 static bool cp_parser_is_keyword
2026   (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028   (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2031
2032 /* Returns nonzero if we are parsing tentatively.  */
2033
2034 static inline bool
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2036 {
2037   return parser->context->next != NULL;
2038 }
2039
2040 /* Returns nonzero if TOKEN is a string literal.  */
2041
2042 static bool
2043 cp_parser_is_string_literal (cp_token* token)
2044 {
2045   return (token->type == CPP_STRING ||
2046           token->type == CPP_STRING16 ||
2047           token->type == CPP_STRING32 ||
2048           token->type == CPP_WSTRING);
2049 }
2050
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2052
2053 static bool
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2055 {
2056   return token->keyword == keyword;
2057 }
2058
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060       FILE:LINE: MESSAGE before TOKEN
2061    where TOKEN is the next token in the input stream.  MESSAGE
2062    (specified by the caller) is usually of the form "expected
2063    OTHER-TOKEN".  */
2064
2065 static void
2066 cp_parser_error (cp_parser* parser, const char* message)
2067 {
2068   if (!cp_parser_simulate_error (parser))
2069     {
2070       cp_token *token = cp_lexer_peek_token (parser->lexer);
2071       /* This diagnostic makes more sense if it is tagged to the line
2072          of the token we just peeked at.  */
2073       cp_lexer_set_source_position_from_token (token);
2074
2075       if (token->type == CPP_PRAGMA)
2076         {
2077           error ("%H%<#pragma%> is not allowed here", &token->location);
2078           cp_parser_skip_to_pragma_eol (parser, token);
2079           return;
2080         }
2081
2082       c_parse_error (message,
2083                      /* Because c_parser_error does not understand
2084                         CPP_KEYWORD, keywords are treated like
2085                         identifiers.  */
2086                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2087                      token->u.value);
2088     }
2089 }
2090
2091 /* Issue an error about name-lookup failing.  NAME is the
2092    IDENTIFIER_NODE DECL is the result of
2093    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2094    the thing that we hoped to find.  */
2095
2096 static void
2097 cp_parser_name_lookup_error (cp_parser* parser,
2098                              tree name,
2099                              tree decl,
2100                              const char* desired,
2101                              location_t location)
2102 {
2103   /* If name lookup completely failed, tell the user that NAME was not
2104      declared.  */
2105   if (decl == error_mark_node)
2106     {
2107       if (parser->scope && parser->scope != global_namespace)
2108         error ("%H%<%E::%E%> has not been declared",
2109                &location, parser->scope, name);
2110       else if (parser->scope == global_namespace)
2111         error ("%H%<::%E%> has not been declared", &location, name);
2112       else if (parser->object_scope
2113                && !CLASS_TYPE_P (parser->object_scope))
2114         error ("%Hrequest for member %qE in non-class type %qT",
2115                &location, name, parser->object_scope);
2116       else if (parser->object_scope)
2117         error ("%H%<%T::%E%> has not been declared",
2118                &location, parser->object_scope, name);
2119       else
2120         error ("%H%qE has not been declared", &location, name);
2121     }
2122   else if (parser->scope && parser->scope != global_namespace)
2123     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124   else if (parser->scope == global_namespace)
2125     error ("%H%<::%E%> %s", &location, name, desired);
2126   else
2127     error ("%H%qE %s", &location, name, desired);
2128 }
2129
2130 /* If we are parsing tentatively, remember that an error has occurred
2131    during this tentative parse.  Returns true if the error was
2132    simulated; false if a message should be issued by the caller.  */
2133
2134 static bool
2135 cp_parser_simulate_error (cp_parser* parser)
2136 {
2137   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2138     {
2139       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2140       return true;
2141     }
2142   return false;
2143 }
2144
2145 /* Check for repeated decl-specifiers.  */
2146
2147 static void
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149                            location_t location)
2150 {
2151   cp_decl_spec ds;
2152
2153   for (ds = ds_first; ds != ds_last; ++ds)
2154     {
2155       unsigned count = decl_specs->specs[(int)ds];
2156       if (count < 2)
2157         continue;
2158       /* The "long" specifier is a special case because of "long long".  */
2159       if (ds == ds_long)
2160         {
2161           if (count > 2)
2162             error ("%H%<long long long%> is too long for GCC", &location);
2163           else if (pedantic && !in_system_header && warn_long_long
2164                    && cxx_dialect == cxx98)
2165             pedwarn (location, OPT_Wlong_long, 
2166                      "ISO C++ 1998 does not support %<long long%>");
2167         }
2168       else if (count > 1)
2169         {
2170           static const char *const decl_spec_names[] = {
2171             "signed",
2172             "unsigned",
2173             "short",
2174             "long",
2175             "const",
2176             "volatile",
2177             "restrict",
2178             "inline",
2179             "virtual",
2180             "explicit",
2181             "friend",
2182             "typedef",
2183             "__complex",
2184             "__thread"
2185           };
2186           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2187         }
2188     }
2189 }
2190
2191 /* This function is called when a type is defined.  If type
2192    definitions are forbidden at this point, an error message is
2193    issued.  */
2194
2195 static bool
2196 cp_parser_check_type_definition (cp_parser* parser)
2197 {
2198   /* If types are forbidden here, issue a message.  */
2199   if (parser->type_definition_forbidden_message)
2200     {
2201       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202          in the message need to be interpreted.  */
2203       error (parser->type_definition_forbidden_message);
2204       return false;
2205     }
2206   return true;
2207 }
2208
2209 /* This function is called when the DECLARATOR is processed.  The TYPE
2210    was a type defined in the decl-specifiers.  If it is invalid to
2211    define a type in the decl-specifiers for DECLARATOR, an error is
2212    issued. TYPE_LOCATION is the location of TYPE and is used
2213    for error reporting.  */
2214
2215 static void
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217                                                tree type, location_t type_location)
2218 {
2219   /* [dcl.fct] forbids type definitions in return types.
2220      Unfortunately, it's not easy to know whether or not we are
2221      processing a return type until after the fact.  */
2222   while (declarator
2223          && (declarator->kind == cdk_pointer
2224              || declarator->kind == cdk_reference
2225              || declarator->kind == cdk_ptrmem))
2226     declarator = declarator->declarator;
2227   if (declarator
2228       && declarator->kind == cdk_function)
2229     {
2230       error ("%Hnew types may not be defined in a return type", &type_location);
2231       inform (type_location, 
2232               "(perhaps a semicolon is missing after the definition of %qT)",
2233               type);
2234     }
2235 }
2236
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238    "<" in any valid C++ program.  If the next token is indeed "<",
2239    issue a message warning the user about what appears to be an
2240    invalid attempt to form a template-id. LOCATION is the location
2241    of the type-specifier (TYPE) */
2242
2243 static void
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245                                          tree type, location_t location)
2246 {
2247   cp_token_position start = 0;
2248
2249   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2250     {
2251       if (TYPE_P (type))
2252         error ("%H%qT is not a template", &location, type);
2253       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254         error ("%H%qE is not a template", &location, type);
2255       else
2256         error ("%Hinvalid template-id", &location);
2257       /* Remember the location of the invalid "<".  */
2258       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259         start = cp_lexer_token_position (parser->lexer, true);
2260       /* Consume the "<".  */
2261       cp_lexer_consume_token (parser->lexer);
2262       /* Parse the template arguments.  */
2263       cp_parser_enclosed_template_argument_list (parser);
2264       /* Permanently remove the invalid template arguments so that
2265          this error message is not issued again.  */
2266       if (start)
2267         cp_lexer_purge_tokens_after (parser->lexer, start);
2268     }
2269 }
2270
2271 /* If parsing an integral constant-expression, issue an error message
2272    about the fact that THING appeared and return true.  Otherwise,
2273    return false.  In either case, set
2274    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2275
2276 static bool
2277 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2278                                             const char *thing)
2279 {
2280   parser->non_integral_constant_expression_p = true;
2281   if (parser->integral_constant_expression_p)
2282     {
2283       if (!parser->allow_non_integral_constant_expression_p)
2284         {
2285           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286              in the message need to be interpreted.  */
2287           char *message = concat (thing,
2288                                   " cannot appear in a constant-expression",
2289                                   NULL);
2290           error (message);
2291           free (message);
2292           return true;
2293         }
2294     }
2295   return false;
2296 }
2297
2298 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2299    qualifying scope (or NULL, if none) for ID.  This function commits
2300    to the current active tentative parse, if any.  (Otherwise, the
2301    problematic construct might be encountered again later, resulting
2302    in duplicate error messages.) LOCATION is the location of ID.  */
2303
2304 static void
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306                                       tree scope, tree id,
2307                                       location_t location)
2308 {
2309   tree decl, old_scope;
2310   /* Try to lookup the identifier.  */
2311   old_scope = parser->scope;
2312   parser->scope = scope;
2313   decl = cp_parser_lookup_name_simple (parser, id, location);
2314   parser->scope = old_scope;
2315   /* If the lookup found a template-name, it means that the user forgot
2316   to specify an argument list. Emit a useful error message.  */
2317   if (TREE_CODE (decl) == TEMPLATE_DECL)
2318     error ("%Hinvalid use of template-name %qE without an argument list",
2319            &location, decl);
2320   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322   else if (TREE_CODE (decl) == TYPE_DECL)
2323     /* Something like 'unsigned A a;'  */
2324     error ("%Hinvalid combination of multiple type-specifiers",
2325            &location);
2326   else if (!parser->scope)
2327     {
2328       /* Issue an error message.  */
2329       error ("%H%qE does not name a type", &location, id);
2330       /* If we're in a template class, it's possible that the user was
2331          referring to a type from a base class.  For example:
2332
2333            template <typename T> struct A { typedef T X; };
2334            template <typename T> struct B : public A<T> { X x; };
2335
2336          The user should have said "typename A<T>::X".  */
2337       if (processing_template_decl && current_class_type
2338           && TYPE_BINFO (current_class_type))
2339         {
2340           tree b;
2341
2342           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2343                b;
2344                b = TREE_CHAIN (b))
2345             {
2346               tree base_type = BINFO_TYPE (b);
2347               if (CLASS_TYPE_P (base_type)
2348                   && dependent_type_p (base_type))
2349                 {
2350                   tree field;
2351                   /* Go from a particular instantiation of the
2352                      template (which will have an empty TYPE_FIELDs),
2353                      to the main version.  */
2354                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355                   for (field = TYPE_FIELDS (base_type);
2356                        field;
2357                        field = TREE_CHAIN (field))
2358                     if (TREE_CODE (field) == TYPE_DECL
2359                         && DECL_NAME (field) == id)
2360                       {
2361                         inform (location, 
2362                                 "(perhaps %<typename %T::%E%> was intended)",
2363                                 BINFO_TYPE (b), id);
2364                         break;
2365                       }
2366                   if (field)
2367                     break;
2368                 }
2369             }
2370         }
2371     }
2372   /* Here we diagnose qualified-ids where the scope is actually correct,
2373      but the identifier does not resolve to a valid type name.  */
2374   else if (parser->scope != error_mark_node)
2375     {
2376       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377         error ("%H%qE in namespace %qE does not name a type",
2378                &location, id, parser->scope);
2379       else if (TYPE_P (parser->scope))
2380         error ("%H%qE in class %qT does not name a type",
2381                &location, id, parser->scope);
2382       else
2383         gcc_unreachable ();
2384     }
2385   cp_parser_commit_to_tentative_parse (parser);
2386 }
2387
2388 /* Check for a common situation where a type-name should be present,
2389    but is not, and issue a sensible error message.  Returns true if an
2390    invalid type-name was detected.
2391
2392    The situation handled by this function are variable declarations of the
2393    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394    Usually, `ID' should name a type, but if we got here it means that it
2395    does not. We try to emit the best possible error message depending on
2396    how exactly the id-expression looks like.  */
2397
2398 static bool
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2400 {
2401   tree id;
2402   cp_token *token = cp_lexer_peek_token (parser->lexer);
2403
2404   cp_parser_parse_tentatively (parser);
2405   id = cp_parser_id_expression (parser,
2406                                 /*template_keyword_p=*/false,
2407                                 /*check_dependency_p=*/true,
2408                                 /*template_p=*/NULL,
2409                                 /*declarator_p=*/true,
2410                                 /*optional_p=*/false);
2411   /* After the id-expression, there should be a plain identifier,
2412      otherwise this is not a simple variable declaration. Also, if
2413      the scope is dependent, we cannot do much.  */
2414   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415       || (parser->scope && TYPE_P (parser->scope)
2416           && dependent_type_p (parser->scope))
2417       || TREE_CODE (id) == TYPE_DECL)
2418     {
2419       cp_parser_abort_tentative_parse (parser);
2420       return false;
2421     }
2422   if (!cp_parser_parse_definitely (parser))
2423     return false;
2424
2425   /* Emit a diagnostic for the invalid type.  */
2426   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427                                         id, token->location);
2428   /* Skip to the end of the declaration; there's no point in
2429      trying to process it.  */
2430   cp_parser_skip_to_end_of_block_or_statement (parser);
2431   return true;
2432 }
2433
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2436    are doing error recovery. Returns -1 if OR_COMMA is true and we
2437    found an unnested comma.  */
2438
2439 static int
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2441                                        bool recovering,
2442                                        bool or_comma,
2443                                        bool consume_paren)
2444 {
2445   unsigned paren_depth = 0;
2446   unsigned brace_depth = 0;
2447
2448   if (recovering && !or_comma
2449       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2450     return 0;
2451
2452   while (true)
2453     {
2454       cp_token * token = cp_lexer_peek_token (parser->lexer);
2455
2456       switch (token->type)
2457         {
2458         case CPP_EOF:
2459         case CPP_PRAGMA_EOL:
2460           /* If we've run out of tokens, then there is no closing `)'.  */
2461           return 0;
2462
2463         case CPP_SEMICOLON:
2464           /* This matches the processing in skip_to_end_of_statement.  */
2465           if (!brace_depth)
2466             return 0;
2467           break;
2468
2469         case CPP_OPEN_BRACE:
2470           ++brace_depth;
2471           break;
2472         case CPP_CLOSE_BRACE:
2473           if (!brace_depth--)
2474             return 0;
2475           break;
2476
2477         case CPP_COMMA:
2478           if (recovering && or_comma && !brace_depth && !paren_depth)
2479             return -1;
2480           break;
2481
2482         case CPP_OPEN_PAREN:
2483           if (!brace_depth)
2484             ++paren_depth;
2485           break;
2486
2487         case CPP_CLOSE_PAREN:
2488           if (!brace_depth && !paren_depth--)
2489             {
2490               if (consume_paren)
2491                 cp_lexer_consume_token (parser->lexer);
2492               return 1;
2493             }
2494           break;
2495
2496         default:
2497           break;
2498         }
2499
2500       /* Consume the token.  */
2501       cp_lexer_consume_token (parser->lexer);
2502     }
2503 }
2504
2505 /* Consume tokens until we reach the end of the current statement.
2506    Normally, that will be just before consuming a `;'.  However, if a
2507    non-nested `}' comes first, then we stop before consuming that.  */
2508
2509 static void
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2511 {
2512   unsigned nesting_depth = 0;
2513
2514   while (true)
2515     {
2516       cp_token *token = cp_lexer_peek_token (parser->lexer);
2517
2518       switch (token->type)
2519         {
2520         case CPP_EOF:
2521         case CPP_PRAGMA_EOL:
2522           /* If we've run out of tokens, stop.  */
2523           return;
2524
2525         case CPP_SEMICOLON:
2526           /* If the next token is a `;', we have reached the end of the
2527              statement.  */
2528           if (!nesting_depth)
2529             return;
2530           break;
2531
2532         case CPP_CLOSE_BRACE:
2533           /* If this is a non-nested '}', stop before consuming it.
2534              That way, when confronted with something like:
2535
2536                { 3 + }
2537
2538              we stop before consuming the closing '}', even though we
2539              have not yet reached a `;'.  */
2540           if (nesting_depth == 0)
2541             return;
2542
2543           /* If it is the closing '}' for a block that we have
2544              scanned, stop -- but only after consuming the token.
2545              That way given:
2546
2547                 void f g () { ... }
2548                 typedef int I;
2549
2550              we will stop after the body of the erroneously declared
2551              function, but before consuming the following `typedef'
2552              declaration.  */
2553           if (--nesting_depth == 0)
2554             {
2555               cp_lexer_consume_token (parser->lexer);
2556               return;
2557             }
2558
2559         case CPP_OPEN_BRACE:
2560           ++nesting_depth;
2561           break;
2562
2563         default:
2564           break;
2565         }
2566
2567       /* Consume the token.  */
2568       cp_lexer_consume_token (parser->lexer);
2569     }
2570 }
2571
2572 /* This function is called at the end of a statement or declaration.
2573    If the next token is a semicolon, it is consumed; otherwise, error
2574    recovery is attempted.  */
2575
2576 static void
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578 {
2579   /* Look for the trailing `;'.  */
2580   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2581     {
2582       /* If there is additional (erroneous) input, skip to the end of
2583          the statement.  */
2584       cp_parser_skip_to_end_of_statement (parser);
2585       /* If the next token is now a `;', consume it.  */
2586       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587         cp_lexer_consume_token (parser->lexer);
2588     }
2589 }
2590
2591 /* Skip tokens until we have consumed an entire block, or until we
2592    have consumed a non-nested `;'.  */
2593
2594 static void
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2596 {
2597   int nesting_depth = 0;
2598
2599   while (nesting_depth >= 0)
2600     {
2601       cp_token *token = cp_lexer_peek_token (parser->lexer);
2602
2603       switch (token->type)
2604         {
2605         case CPP_EOF:
2606         case CPP_PRAGMA_EOL:
2607           /* If we've run out of tokens, stop.  */
2608           return;
2609
2610         case CPP_SEMICOLON:
2611           /* Stop if this is an unnested ';'. */
2612           if (!nesting_depth)
2613             nesting_depth = -1;
2614           break;
2615
2616         case CPP_CLOSE_BRACE:
2617           /* Stop if this is an unnested '}', or closes the outermost
2618              nesting level.  */
2619           nesting_depth--;
2620           if (!nesting_depth)
2621             nesting_depth = -1;
2622           break;
2623
2624         case CPP_OPEN_BRACE:
2625           /* Nest. */
2626           nesting_depth++;
2627           break;
2628
2629         default:
2630           break;
2631         }
2632
2633       /* Consume the token.  */
2634       cp_lexer_consume_token (parser->lexer);
2635     }
2636 }
2637
2638 /* Skip tokens until a non-nested closing curly brace is the next
2639    token, or there are no more tokens. Return true in the first case,
2640    false otherwise.  */
2641
2642 static bool
2643 cp_parser_skip_to_closing_brace (cp_parser *parser)
2644 {
2645   unsigned nesting_depth = 0;
2646
2647   while (true)
2648     {
2649       cp_token *token = cp_lexer_peek_token (parser->lexer);
2650
2651       switch (token->type)
2652         {
2653         case CPP_EOF:
2654         case CPP_PRAGMA_EOL:
2655           /* If we've run out of tokens, stop.  */
2656           return false;
2657
2658         case CPP_CLOSE_BRACE:
2659           /* If the next token is a non-nested `}', then we have reached
2660              the end of the current block.  */
2661           if (nesting_depth-- == 0)
2662             return true;
2663           break;
2664
2665         case CPP_OPEN_BRACE:
2666           /* If it the next token is a `{', then we are entering a new
2667              block.  Consume the entire block.  */
2668           ++nesting_depth;
2669           break;
2670
2671         default:
2672           break;
2673         }
2674
2675       /* Consume the token.  */
2676       cp_lexer_consume_token (parser->lexer);
2677     }
2678 }
2679
2680 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2681    parameter is the PRAGMA token, allowing us to purge the entire pragma
2682    sequence.  */
2683
2684 static void
2685 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2686 {
2687   cp_token *token;
2688
2689   parser->lexer->in_pragma = false;
2690
2691   do
2692     token = cp_lexer_consume_token (parser->lexer);
2693   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2694
2695   /* Ensure that the pragma is not parsed again.  */
2696   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2697 }
2698
2699 /* Require pragma end of line, resyncing with it as necessary.  The
2700    arguments are as for cp_parser_skip_to_pragma_eol.  */
2701
2702 static void
2703 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2704 {
2705   parser->lexer->in_pragma = false;
2706   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2707     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2708 }
2709
2710 /* This is a simple wrapper around make_typename_type. When the id is
2711    an unresolved identifier node, we can provide a superior diagnostic
2712    using cp_parser_diagnose_invalid_type_name.  */
2713
2714 static tree
2715 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2716                               tree id, location_t id_location)
2717 {
2718   tree result;
2719   if (TREE_CODE (id) == IDENTIFIER_NODE)
2720     {
2721       result = make_typename_type (scope, id, typename_type,
2722                                    /*complain=*/tf_none);
2723       if (result == error_mark_node)
2724         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2725       return result;
2726     }
2727   return make_typename_type (scope, id, typename_type, tf_error);
2728 }
2729
2730 /* This is a wrapper around the
2731    make_{pointer,ptrmem,reference}_declarator functions that decides
2732    which one to call based on the CODE and CLASS_TYPE arguments. The
2733    CODE argument should be one of the values returned by
2734    cp_parser_ptr_operator. */
2735 static cp_declarator *
2736 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2737                                     cp_cv_quals cv_qualifiers,
2738                                     cp_declarator *target)
2739 {
2740   if (code == ERROR_MARK)
2741     return cp_error_declarator;
2742
2743   if (code == INDIRECT_REF)
2744     if (class_type == NULL_TREE)
2745       return make_pointer_declarator (cv_qualifiers, target);
2746     else
2747       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2748   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2749     return make_reference_declarator (cv_qualifiers, target, false);
2750   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2751     return make_reference_declarator (cv_qualifiers, target, true);
2752   gcc_unreachable ();
2753 }
2754
2755 /* Create a new C++ parser.  */
2756
2757 static cp_parser *
2758 cp_parser_new (void)
2759 {
2760   cp_parser *parser;
2761   cp_lexer *lexer;
2762   unsigned i;
2763
2764   /* cp_lexer_new_main is called before calling ggc_alloc because
2765      cp_lexer_new_main might load a PCH file.  */
2766   lexer = cp_lexer_new_main ();
2767
2768   /* Initialize the binops_by_token so that we can get the tree
2769      directly from the token.  */
2770   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2771     binops_by_token[binops[i].token_type] = binops[i];
2772
2773   parser = GGC_CNEW (cp_parser);
2774   parser->lexer = lexer;
2775   parser->context = cp_parser_context_new (NULL);
2776
2777   /* For now, we always accept GNU extensions.  */
2778   parser->allow_gnu_extensions_p = 1;
2779
2780   /* The `>' token is a greater-than operator, not the end of a
2781      template-id.  */
2782   parser->greater_than_is_operator_p = true;
2783
2784   parser->default_arg_ok_p = true;
2785
2786   /* We are not parsing a constant-expression.  */
2787   parser->integral_constant_expression_p = false;
2788   parser->allow_non_integral_constant_expression_p = false;
2789   parser->non_integral_constant_expression_p = false;
2790
2791   /* Local variable names are not forbidden.  */
2792   parser->local_variables_forbidden_p = false;
2793
2794   /* We are not processing an `extern "C"' declaration.  */
2795   parser->in_unbraced_linkage_specification_p = false;
2796
2797   /* We are not processing a declarator.  */
2798   parser->in_declarator_p = false;
2799
2800   /* We are not processing a template-argument-list.  */
2801   parser->in_template_argument_list_p = false;
2802
2803   /* We are not in an iteration statement.  */
2804   parser->in_statement = 0;
2805
2806   /* We are not in a switch statement.  */
2807   parser->in_switch_statement_p = false;
2808
2809   /* We are not parsing a type-id inside an expression.  */
2810   parser->in_type_id_in_expr_p = false;
2811
2812   /* Declarations aren't implicitly extern "C".  */
2813   parser->implicit_extern_c = false;
2814
2815   /* String literals should be translated to the execution character set.  */
2816   parser->translate_strings_p = true;
2817
2818   /* We are not parsing a function body.  */
2819   parser->in_function_body = false;
2820
2821   /* The unparsed function queue is empty.  */
2822   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2823
2824   /* There are no classes being defined.  */
2825   parser->num_classes_being_defined = 0;
2826
2827   /* No template parameters apply.  */
2828   parser->num_template_parameter_lists = 0;
2829
2830   return parser;
2831 }
2832
2833 /* Create a cp_lexer structure which will emit the tokens in CACHE
2834    and push it onto the parser's lexer stack.  This is used for delayed
2835    parsing of in-class method bodies and default arguments, and should
2836    not be confused with tentative parsing.  */
2837 static void
2838 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2839 {
2840   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2841   lexer->next = parser->lexer;
2842   parser->lexer = lexer;
2843
2844   /* Move the current source position to that of the first token in the
2845      new lexer.  */
2846   cp_lexer_set_source_position_from_token (lexer->next_token);
2847 }
2848
2849 /* Pop the top lexer off the parser stack.  This is never used for the
2850    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2851 static void
2852 cp_parser_pop_lexer (cp_parser *parser)
2853 {
2854   cp_lexer *lexer = parser->lexer;
2855   parser->lexer = lexer->next;
2856   cp_lexer_destroy (lexer);
2857
2858   /* Put the current source position back where it was before this
2859      lexer was pushed.  */
2860   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2861 }
2862
2863 /* Lexical conventions [gram.lex]  */
2864
2865 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2866    identifier.  */
2867
2868 static tree
2869 cp_parser_identifier (cp_parser* parser)
2870 {
2871   cp_token *token;
2872
2873   /* Look for the identifier.  */
2874   token = cp_parser_require (parser, CPP_NAME, "identifier");
2875   /* Return the value.  */
2876   return token ? token->u.value : error_mark_node;
2877 }
2878
2879 /* Parse a sequence of adjacent string constants.  Returns a
2880    TREE_STRING representing the combined, nul-terminated string
2881    constant.  If TRANSLATE is true, translate the string to the
2882    execution character set.  If WIDE_OK is true, a wide string is
2883    invalid here.
2884
2885    C++98 [lex.string] says that if a narrow string literal token is
2886    adjacent to a wide string literal token, the behavior is undefined.
2887    However, C99 6.4.5p4 says that this results in a wide string literal.
2888    We follow C99 here, for consistency with the C front end.
2889
2890    This code is largely lifted from lex_string() in c-lex.c.
2891
2892    FUTURE: ObjC++ will need to handle @-strings here.  */
2893 static tree
2894 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2895 {
2896   tree value;
2897   size_t count;
2898   struct obstack str_ob;
2899   cpp_string str, istr, *strs;
2900   cp_token *tok;
2901   enum cpp_ttype type;
2902
2903   tok = cp_lexer_peek_token (parser->lexer);
2904   if (!cp_parser_is_string_literal (tok))
2905     {
2906       cp_parser_error (parser, "expected string-literal");
2907       return error_mark_node;
2908     }
2909
2910   type = tok->type;
2911
2912   /* Try to avoid the overhead of creating and destroying an obstack
2913      for the common case of just one string.  */
2914   if (!cp_parser_is_string_literal
2915       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2916     {
2917       cp_lexer_consume_token (parser->lexer);
2918
2919       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2920       str.len = TREE_STRING_LENGTH (tok->u.value);
2921       count = 1;
2922
2923       strs = &str;
2924     }
2925   else
2926     {
2927       gcc_obstack_init (&str_ob);
2928       count = 0;
2929
2930       do
2931         {
2932           cp_lexer_consume_token (parser->lexer);
2933           count++;
2934           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2935           str.len = TREE_STRING_LENGTH (tok->u.value);
2936
2937           if (type != tok->type)
2938             {
2939               if (type == CPP_STRING)
2940                 type = tok->type;
2941               else if (tok->type != CPP_STRING)
2942                 error ("%Hunsupported non-standard concatenation "
2943                        "of string literals", &tok->location);
2944             }
2945
2946           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2947
2948           tok = cp_lexer_peek_token (parser->lexer);
2949         }
2950       while (cp_parser_is_string_literal (tok));
2951
2952       strs = (cpp_string *) obstack_finish (&str_ob);
2953     }
2954
2955   if (type != CPP_STRING && !wide_ok)
2956     {
2957       cp_parser_error (parser, "a wide string is invalid in this context");
2958       type = CPP_STRING;
2959     }
2960
2961   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2962       (parse_in, strs, count, &istr, type))
2963     {
2964       value = build_string (istr.len, (const char *)istr.text);
2965       free (CONST_CAST (unsigned char *, istr.text));
2966
2967       switch (type)
2968         {
2969         default:
2970         case CPP_STRING:
2971           TREE_TYPE (value) = char_array_type_node;
2972           break;
2973         case CPP_STRING16:
2974           TREE_TYPE (value) = char16_array_type_node;
2975           break;
2976         case CPP_STRING32:
2977           TREE_TYPE (value) = char32_array_type_node;
2978           break;
2979         case CPP_WSTRING:
2980           TREE_TYPE (value) = wchar_array_type_node;
2981           break;
2982         }
2983
2984       value = fix_string_type (value);
2985     }
2986   else
2987     /* cpp_interpret_string has issued an error.  */
2988     value = error_mark_node;
2989
2990   if (count > 1)
2991     obstack_free (&str_ob, 0);
2992
2993   return value;
2994 }
2995
2996
2997 /* Basic concepts [gram.basic]  */
2998
2999 /* Parse a translation-unit.
3000
3001    translation-unit:
3002      declaration-seq [opt]
3003
3004    Returns TRUE if all went well.  */
3005
3006 static bool
3007 cp_parser_translation_unit (cp_parser* parser)
3008 {
3009   /* The address of the first non-permanent object on the declarator
3010      obstack.  */
3011   static void *declarator_obstack_base;
3012
3013   bool success;
3014
3015   /* Create the declarator obstack, if necessary.  */
3016   if (!cp_error_declarator)
3017     {
3018       gcc_obstack_init (&declarator_obstack);
3019       /* Create the error declarator.  */
3020       cp_error_declarator = make_declarator (cdk_error);
3021       /* Create the empty parameter list.  */
3022       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3023       /* Remember where the base of the declarator obstack lies.  */
3024       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3025     }
3026
3027   cp_parser_declaration_seq_opt (parser);
3028
3029   /* If there are no tokens left then all went well.  */
3030   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3031     {
3032       /* Get rid of the token array; we don't need it any more.  */
3033       cp_lexer_destroy (parser->lexer);
3034       parser->lexer = NULL;
3035
3036       /* This file might have been a context that's implicitly extern
3037          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3038       if (parser->implicit_extern_c)
3039         {
3040           pop_lang_context ();
3041           parser->implicit_extern_c = false;
3042         }
3043
3044       /* Finish up.  */
3045       finish_translation_unit ();
3046
3047       success = true;
3048     }
3049   else
3050     {
3051       cp_parser_error (parser, "expected declaration");
3052       success = false;
3053     }
3054
3055   /* Make sure the declarator obstack was fully cleaned up.  */
3056   gcc_assert (obstack_next_free (&declarator_obstack)
3057               == declarator_obstack_base);
3058
3059   /* All went well.  */
3060   return success;
3061 }
3062
3063 /* Expressions [gram.expr] */
3064
3065 /* Parse a primary-expression.
3066
3067    primary-expression:
3068      literal
3069      this
3070      ( expression )
3071      id-expression
3072
3073    GNU Extensions:
3074
3075    primary-expression:
3076      ( compound-statement )
3077      __builtin_va_arg ( assignment-expression , type-id )
3078      __builtin_offsetof ( type-id , offsetof-expression )
3079
3080    C++ Extensions:
3081      __has_nothrow_assign ( type-id )   
3082      __has_nothrow_constructor ( type-id )
3083      __has_nothrow_copy ( type-id )
3084      __has_trivial_assign ( type-id )   
3085      __has_trivial_constructor ( type-id )
3086      __has_trivial_copy ( type-id )
3087      __has_trivial_destructor ( type-id )
3088      __has_virtual_destructor ( type-id )     
3089      __is_abstract ( type-id )
3090      __is_base_of ( type-id , type-id )
3091      __is_class ( type-id )
3092      __is_convertible_to ( type-id , type-id )     
3093      __is_empty ( type-id )
3094      __is_enum ( type-id )
3095      __is_pod ( type-id )
3096      __is_polymorphic ( type-id )
3097      __is_union ( type-id )
3098
3099    Objective-C++ Extension:
3100
3101    primary-expression:
3102      objc-expression
3103
3104    literal:
3105      __null
3106
3107    ADDRESS_P is true iff this expression was immediately preceded by
3108    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3109    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3110    true iff this expression is a template argument.
3111
3112    Returns a representation of the expression.  Upon return, *IDK
3113    indicates what kind of id-expression (if any) was present.  */
3114
3115 static tree
3116 cp_parser_primary_expression (cp_parser *parser,
3117                               bool address_p,
3118                               bool cast_p,
3119                               bool template_arg_p,
3120                               cp_id_kind *idk)
3121 {
3122   cp_token *token = NULL;
3123
3124   /* Assume the primary expression is not an id-expression.  */
3125   *idk = CP_ID_KIND_NONE;
3126
3127   /* Peek at the next token.  */
3128   token = cp_lexer_peek_token (parser->lexer);
3129   switch (token->type)
3130     {
3131       /* literal:
3132            integer-literal
3133            character-literal
3134            floating-literal
3135            string-literal
3136            boolean-literal  */
3137     case CPP_CHAR:
3138     case CPP_CHAR16:
3139     case CPP_CHAR32:
3140     case CPP_WCHAR:
3141     case CPP_NUMBER:
3142       token = cp_lexer_consume_token (parser->lexer);
3143       /* Floating-point literals are only allowed in an integral
3144          constant expression if they are cast to an integral or
3145          enumeration type.  */
3146       if (TREE_CODE (token->u.value) == REAL_CST
3147           && parser->integral_constant_expression_p
3148           && pedantic)
3149         {
3150           /* CAST_P will be set even in invalid code like "int(2.7 +
3151              ...)".   Therefore, we have to check that the next token
3152              is sure to end the cast.  */
3153           if (cast_p)
3154             {
3155               cp_token *next_token;
3156
3157               next_token = cp_lexer_peek_token (parser->lexer);
3158               if (/* The comma at the end of an
3159                      enumerator-definition.  */
3160                   next_token->type != CPP_COMMA
3161                   /* The curly brace at the end of an enum-specifier.  */
3162                   && next_token->type != CPP_CLOSE_BRACE
3163                   /* The end of a statement.  */
3164                   && next_token->type != CPP_SEMICOLON
3165                   /* The end of the cast-expression.  */
3166                   && next_token->type != CPP_CLOSE_PAREN
3167                   /* The end of an array bound.  */
3168                   && next_token->type != CPP_CLOSE_SQUARE
3169                   /* The closing ">" in a template-argument-list.  */
3170                   && (next_token->type != CPP_GREATER
3171                       || parser->greater_than_is_operator_p)
3172                   /* C++0x only: A ">>" treated like two ">" tokens,
3173                      in a template-argument-list.  */
3174                   && (next_token->type != CPP_RSHIFT
3175                       || (cxx_dialect == cxx98)
3176                       || parser->greater_than_is_operator_p))
3177                 cast_p = false;
3178             }
3179
3180           /* If we are within a cast, then the constraint that the
3181              cast is to an integral or enumeration type will be
3182              checked at that point.  If we are not within a cast, then
3183              this code is invalid.  */
3184           if (!cast_p)
3185             cp_parser_non_integral_constant_expression
3186               (parser, "floating-point literal");
3187         }
3188       return token->u.value;
3189
3190     case CPP_STRING:
3191     case CPP_STRING16:
3192     case CPP_STRING32:
3193     case CPP_WSTRING:
3194       /* ??? Should wide strings be allowed when parser->translate_strings_p
3195          is false (i.e. in attributes)?  If not, we can kill the third
3196          argument to cp_parser_string_literal.  */
3197       return cp_parser_string_literal (parser,
3198                                        parser->translate_strings_p,
3199                                        true);
3200
3201     case CPP_OPEN_PAREN:
3202       {
3203         tree expr;
3204         bool saved_greater_than_is_operator_p;
3205
3206         /* Consume the `('.  */
3207         cp_lexer_consume_token (parser->lexer);
3208         /* Within a parenthesized expression, a `>' token is always
3209            the greater-than operator.  */
3210         saved_greater_than_is_operator_p
3211           = parser->greater_than_is_operator_p;
3212         parser->greater_than_is_operator_p = true;
3213         /* If we see `( { ' then we are looking at the beginning of
3214            a GNU statement-expression.  */
3215         if (cp_parser_allow_gnu_extensions_p (parser)
3216             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3217           {
3218             /* Statement-expressions are not allowed by the standard.  */
3219             pedwarn (token->location, OPT_pedantic, 
3220                      "ISO C++ forbids braced-groups within expressions");
3221
3222             /* And they're not allowed outside of a function-body; you
3223                cannot, for example, write:
3224
3225                  int i = ({ int j = 3; j + 1; });
3226
3227                at class or namespace scope.  */
3228             if (!parser->in_function_body
3229                 || parser->in_template_argument_list_p)
3230               {
3231                 error ("%Hstatement-expressions are not allowed outside "
3232                        "functions nor in template-argument lists",
3233                        &token->location);
3234                 cp_parser_skip_to_end_of_block_or_statement (parser);
3235                 expr = error_mark_node;
3236               }
3237             else
3238               {
3239                 /* Start the statement-expression.  */
3240                 expr = begin_stmt_expr ();
3241                 /* Parse the compound-statement.  */
3242                 cp_parser_compound_statement (parser, expr, false);
3243                 /* Finish up.  */
3244                 expr = finish_stmt_expr (expr, false);
3245               }
3246           }
3247         else
3248           {
3249             /* Parse the parenthesized expression.  */
3250             expr = cp_parser_expression (parser, cast_p);
3251             /* Let the front end know that this expression was
3252                enclosed in parentheses. This matters in case, for
3253                example, the expression is of the form `A::B', since
3254                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3255                not.  */
3256             finish_parenthesized_expr (expr);
3257           }
3258         /* The `>' token might be the end of a template-id or
3259            template-parameter-list now.  */
3260         parser->greater_than_is_operator_p
3261           = saved_greater_than_is_operator_p;
3262         /* Consume the `)'.  */
3263         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3264           cp_parser_skip_to_end_of_statement (parser);
3265
3266         return expr;
3267       }
3268
3269     case CPP_KEYWORD:
3270       switch (token->keyword)
3271         {
3272           /* These two are the boolean literals.  */
3273         case RID_TRUE:
3274           cp_lexer_consume_token (parser->lexer);
3275           return boolean_true_node;
3276         case RID_FALSE:
3277           cp_lexer_consume_token (parser->lexer);
3278           return boolean_false_node;
3279
3280           /* The `__null' literal.  */
3281         case RID_NULL:
3282           cp_lexer_consume_token (parser->lexer);
3283           return null_node;
3284
3285           /* Recognize the `this' keyword.  */
3286         case RID_THIS:
3287           cp_lexer_consume_token (parser->lexer);
3288           if (parser->local_variables_forbidden_p)
3289             {
3290               error ("%H%<this%> may not be used in this context",
3291                      &token->location);
3292               return error_mark_node;
3293             }
3294           /* Pointers cannot appear in constant-expressions.  */
3295           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3296             return error_mark_node;
3297           return finish_this_expr ();
3298
3299           /* The `operator' keyword can be the beginning of an
3300              id-expression.  */
3301         case RID_OPERATOR:
3302           goto id_expression;
3303
3304         case RID_FUNCTION_NAME:
3305         case RID_PRETTY_FUNCTION_NAME:
3306         case RID_C99_FUNCTION_NAME:
3307           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3308              __func__ are the names of variables -- but they are
3309              treated specially.  Therefore, they are handled here,
3310              rather than relying on the generic id-expression logic
3311              below.  Grammatically, these names are id-expressions.
3312
3313              Consume the token.  */
3314           token = cp_lexer_consume_token (parser->lexer);
3315           /* Look up the name.  */
3316           return finish_fname (token->u.value);
3317
3318         case RID_VA_ARG:
3319           {
3320             tree expression;
3321             tree type;
3322
3323             /* The `__builtin_va_arg' construct is used to handle
3324                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3325             cp_lexer_consume_token (parser->lexer);
3326             /* Look for the opening `('.  */
3327             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3328             /* Now, parse the assignment-expression.  */
3329             expression = cp_parser_assignment_expression (parser,
3330                                                           /*cast_p=*/false);
3331             /* Look for the `,'.  */
3332             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3333             /* Parse the type-id.  */
3334             type = cp_parser_type_id (parser);
3335             /* Look for the closing `)'.  */
3336             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3337             /* Using `va_arg' in a constant-expression is not
3338                allowed.  */
3339             if (cp_parser_non_integral_constant_expression (parser,
3340                                                             "%<va_arg%>"))
3341               return error_mark_node;
3342             return build_x_va_arg (expression, type);
3343           }
3344
3345         case RID_OFFSETOF:
3346           return cp_parser_builtin_offsetof (parser);
3347
3348         case RID_HAS_NOTHROW_ASSIGN:
3349         case RID_HAS_NOTHROW_CONSTRUCTOR:
3350         case RID_HAS_NOTHROW_COPY:        
3351         case RID_HAS_TRIVIAL_ASSIGN:
3352         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3353         case RID_HAS_TRIVIAL_COPY:        
3354         case RID_HAS_TRIVIAL_DESTRUCTOR:
3355         case RID_HAS_VIRTUAL_DESTRUCTOR:
3356         case RID_IS_ABSTRACT:
3357         case RID_IS_BASE_OF:
3358         case RID_IS_CLASS:
3359         case RID_IS_CONVERTIBLE_TO:
3360         case RID_IS_EMPTY:
3361         case RID_IS_ENUM:
3362         case RID_IS_POD:
3363         case RID_IS_POLYMORPHIC:
3364         case RID_IS_UNION:
3365           return cp_parser_trait_expr (parser, token->keyword);
3366
3367         /* Objective-C++ expressions.  */
3368         case RID_AT_ENCODE:
3369         case RID_AT_PROTOCOL:
3370         case RID_AT_SELECTOR:
3371           return cp_parser_objc_expression (parser);
3372
3373         default:
3374           cp_parser_error (parser, "expected primary-expression");
3375           return error_mark_node;
3376         }
3377
3378       /* An id-expression can start with either an identifier, a
3379          `::' as the beginning of a qualified-id, or the "operator"
3380          keyword.  */
3381     case CPP_NAME:
3382     case CPP_SCOPE:
3383     case CPP_TEMPLATE_ID:
3384     case CPP_NESTED_NAME_SPECIFIER:
3385       {
3386         tree id_expression;
3387         tree decl;
3388         const char *error_msg;
3389         bool template_p;
3390         bool done;
3391         cp_token *id_expr_token;
3392
3393       id_expression:
3394         /* Parse the id-expression.  */
3395         id_expression
3396           = cp_parser_id_expression (parser,
3397                                      /*template_keyword_p=*/false,
3398                                      /*check_dependency_p=*/true,
3399                                      &template_p,
3400                                      /*declarator_p=*/false,
3401                                      /*optional_p=*/false);
3402         if (id_expression == error_mark_node)
3403           return error_mark_node;
3404         id_expr_token = token;
3405         token = cp_lexer_peek_token (parser->lexer);
3406         done = (token->type != CPP_OPEN_SQUARE
3407                 && token->type != CPP_OPEN_PAREN
3408                 && token->type != CPP_DOT
3409                 && token->type != CPP_DEREF
3410                 && token->type != CPP_PLUS_PLUS
3411                 && token->type != CPP_MINUS_MINUS);
3412         /* If we have a template-id, then no further lookup is
3413            required.  If the template-id was for a template-class, we
3414            will sometimes have a TYPE_DECL at this point.  */
3415         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3416                  || TREE_CODE (id_expression) == TYPE_DECL)
3417           decl = id_expression;
3418         /* Look up the name.  */
3419         else
3420           {
3421             tree ambiguous_decls;
3422
3423             decl = cp_parser_lookup_name (parser, id_expression,
3424                                           none_type,
3425                                           template_p,
3426                                           /*is_namespace=*/false,
3427                                           /*check_dependency=*/true,
3428                                           &ambiguous_decls,
3429                                           id_expr_token->location);
3430             /* If the lookup was ambiguous, an error will already have
3431                been issued.  */
3432             if (ambiguous_decls)
3433               return error_mark_node;
3434
3435             /* In Objective-C++, an instance variable (ivar) may be preferred
3436                to whatever cp_parser_lookup_name() found.  */
3437             decl = objc_lookup_ivar (decl, id_expression);
3438
3439             /* If name lookup gives us a SCOPE_REF, then the
3440                qualifying scope was dependent.  */
3441             if (TREE_CODE (decl) == SCOPE_REF)
3442               {
3443                 /* At this point, we do not know if DECL is a valid
3444                    integral constant expression.  We assume that it is
3445                    in fact such an expression, so that code like:
3446
3447                       template <int N> struct A {
3448                         int a[B<N>::i];
3449                       };
3450                      
3451                    is accepted.  At template-instantiation time, we
3452                    will check that B<N>::i is actually a constant.  */
3453                 return decl;
3454               }
3455             /* Check to see if DECL is a local variable in a context
3456                where that is forbidden.  */
3457             if (parser->local_variables_forbidden_p
3458                 && local_variable_p (decl))
3459               {
3460                 /* It might be that we only found DECL because we are
3461                    trying to be generous with pre-ISO scoping rules.
3462                    For example, consider:
3463
3464                      int i;
3465                      void g() {
3466                        for (int i = 0; i < 10; ++i) {}
3467                        extern void f(int j = i);
3468                      }
3469
3470                    Here, name look up will originally find the out
3471                    of scope `i'.  We need to issue a warning message,
3472                    but then use the global `i'.  */
3473                 decl = check_for_out_of_scope_variable (decl);
3474                 if (local_variable_p (decl))
3475                   {
3476                     error ("%Hlocal variable %qD may not appear in this context",
3477                            &id_expr_token->location, decl);
3478                     return error_mark_node;
3479                   }
3480               }
3481           }
3482
3483         decl = (finish_id_expression
3484                 (id_expression, decl, parser->scope,
3485                  idk,
3486                  parser->integral_constant_expression_p,
3487                  parser->allow_non_integral_constant_expression_p,
3488                  &parser->non_integral_constant_expression_p,
3489                  template_p, done, address_p,
3490                  template_arg_p,
3491                  &error_msg,
3492                  id_expr_token->location));
3493         if (error_msg)
3494           cp_parser_error (parser, error_msg);
3495         return decl;
3496       }
3497
3498       /* Anything else is an error.  */
3499     default:
3500       /* ...unless we have an Objective-C++ message or string literal,
3501          that is.  */
3502       if (c_dialect_objc ()
3503           && (token->type == CPP_OPEN_SQUARE
3504               || token->type == CPP_OBJC_STRING))
3505         return cp_parser_objc_expression (parser);
3506
3507       cp_parser_error (parser, "expected primary-expression");
3508       return error_mark_node;
3509     }
3510 }
3511
3512 /* Parse an id-expression.
3513
3514    id-expression:
3515      unqualified-id
3516      qualified-id
3517
3518    qualified-id:
3519      :: [opt] nested-name-specifier template [opt] unqualified-id
3520      :: identifier
3521      :: operator-function-id
3522      :: template-id
3523
3524    Return a representation of the unqualified portion of the
3525    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3526    a `::' or nested-name-specifier.
3527
3528    Often, if the id-expression was a qualified-id, the caller will
3529    want to make a SCOPE_REF to represent the qualified-id.  This
3530    function does not do this in order to avoid wastefully creating
3531    SCOPE_REFs when they are not required.
3532
3533    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3534    `template' keyword.
3535
3536    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3537    uninstantiated templates.
3538
3539    If *TEMPLATE_P is non-NULL, it is set to true iff the
3540    `template' keyword is used to explicitly indicate that the entity
3541    named is a template.
3542
3543    If DECLARATOR_P is true, the id-expression is appearing as part of
3544    a declarator, rather than as part of an expression.  */
3545
3546 static tree
3547 cp_parser_id_expression (cp_parser *parser,
3548                          bool template_keyword_p,
3549                          bool check_dependency_p,
3550                          bool *template_p,
3551                          bool declarator_p,
3552                          bool optional_p)
3553 {
3554   bool global_scope_p;
3555   bool nested_name_specifier_p;
3556
3557   /* Assume the `template' keyword was not used.  */
3558   if (template_p)
3559     *template_p = template_keyword_p;
3560
3561   /* Look for the optional `::' operator.  */
3562   global_scope_p
3563     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3564        != NULL_TREE);
3565   /* Look for the optional nested-name-specifier.  */
3566   nested_name_specifier_p
3567     = (cp_parser_nested_name_specifier_opt (parser,
3568                                             /*typename_keyword_p=*/false,
3569                                             check_dependency_p,
3570                                             /*type_p=*/false,
3571                                             declarator_p)
3572        != NULL_TREE);
3573   /* If there is a nested-name-specifier, then we are looking at
3574      the first qualified-id production.  */
3575   if (nested_name_specifier_p)
3576     {
3577       tree saved_scope;
3578       tree saved_object_scope;
3579       tree saved_qualifying_scope;
3580       tree unqualified_id;
3581       bool is_template;
3582
3583       /* See if the next token is the `template' keyword.  */
3584       if (!template_p)
3585         template_p = &is_template;
3586       *template_p = cp_parser_optional_template_keyword (parser);
3587       /* Name lookup we do during the processing of the
3588          unqualified-id might obliterate SCOPE.  */
3589       saved_scope = parser->scope;
3590       saved_object_scope = parser->object_scope;
3591       saved_qualifying_scope = parser->qualifying_scope;
3592       /* Process the final unqualified-id.  */
3593       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3594                                                  check_dependency_p,
3595                                                  declarator_p,
3596                                                  /*optional_p=*/false);
3597       /* Restore the SAVED_SCOPE for our caller.  */
3598       parser->scope = saved_scope;
3599       parser->object_scope = saved_object_scope;
3600       parser->qualifying_scope = saved_qualifying_scope;
3601
3602       return unqualified_id;
3603     }
3604   /* Otherwise, if we are in global scope, then we are looking at one
3605      of the other qualified-id productions.  */
3606   else if (global_scope_p)
3607     {
3608       cp_token *token;
3609       tree id;
3610
3611       /* Peek at the next token.  */
3612       token = cp_lexer_peek_token (parser->lexer);
3613
3614       /* If it's an identifier, and the next token is not a "<", then
3615          we can avoid the template-id case.  This is an optimization
3616          for this common case.  */
3617       if (token->type == CPP_NAME
3618           && !cp_parser_nth_token_starts_template_argument_list_p
3619                (parser, 2))
3620         return cp_parser_identifier (parser);
3621
3622       cp_parser_parse_tentatively (parser);
3623       /* Try a template-id.  */
3624       id = cp_parser_template_id (parser,
3625                                   /*template_keyword_p=*/false,
3626                                   /*check_dependency_p=*/true,
3627                                   declarator_p);
3628       /* If that worked, we're done.  */
3629       if (cp_parser_parse_definitely (parser))
3630         return id;
3631
3632       /* Peek at the next token.  (Changes in the token buffer may
3633          have invalidated the pointer obtained above.)  */
3634       token = cp_lexer_peek_token (parser->lexer);
3635
3636       switch (token->type)
3637         {
3638         case CPP_NAME:
3639           return cp_parser_identifier (parser);
3640
3641         case CPP_KEYWORD:
3642           if (token->keyword == RID_OPERATOR)
3643             return cp_parser_operator_function_id (parser);
3644           /* Fall through.  */
3645
3646         default:
3647           cp_parser_error (parser, "expected id-expression");
3648           return error_mark_node;
3649         }
3650     }
3651   else
3652     return cp_parser_unqualified_id (parser, template_keyword_p,
3653                                      /*check_dependency_p=*/true,
3654                                      declarator_p,
3655                                      optional_p);
3656 }
3657
3658 /* Parse an unqualified-id.
3659
3660    unqualified-id:
3661      identifier
3662      operator-function-id
3663      conversion-function-id
3664      ~ class-name
3665      template-id
3666
3667    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3668    keyword, in a construct like `A::template ...'.
3669
3670    Returns a representation of unqualified-id.  For the `identifier'
3671    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3672    production a BIT_NOT_EXPR is returned; the operand of the
3673    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3674    other productions, see the documentation accompanying the
3675    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3676    names are looked up in uninstantiated templates.  If DECLARATOR_P
3677    is true, the unqualified-id is appearing as part of a declarator,
3678    rather than as part of an expression.  */
3679
3680 static tree
3681 cp_parser_unqualified_id (cp_parser* parser,
3682                           bool template_keyword_p,
3683                           bool check_dependency_p,
3684                           bool declarator_p,
3685                           bool optional_p)
3686 {
3687   cp_token *token;
3688
3689   /* Peek at the next token.  */
3690   token = cp_lexer_peek_token (parser->lexer);
3691
3692   switch (token->type)
3693     {
3694     case CPP_NAME:
3695       {
3696         tree id;
3697
3698         /* We don't know yet whether or not this will be a
3699            template-id.  */
3700         cp_parser_parse_tentatively (parser);
3701         /* Try a template-id.  */
3702         id = cp_parser_template_id (parser, template_keyword_p,
3703                                     check_dependency_p,
3704                                     declarator_p);
3705         /* If it worked, we're done.  */
3706         if (cp_parser_parse_definitely (parser))
3707           return id;
3708         /* Otherwise, it's an ordinary identifier.  */
3709         return cp_parser_identifier (parser);
3710       }
3711
3712     case CPP_TEMPLATE_ID:
3713       return cp_parser_template_id (parser, template_keyword_p,
3714                                     check_dependency_p,
3715                                     declarator_p);
3716
3717     case CPP_COMPL:
3718       {
3719         tree type_decl;
3720         tree qualifying_scope;
3721         tree object_scope;
3722         tree scope;
3723         bool done;
3724
3725         /* Consume the `~' token.  */
3726         cp_lexer_consume_token (parser->lexer);
3727         /* Parse the class-name.  The standard, as written, seems to
3728            say that:
3729
3730              template <typename T> struct S { ~S (); };
3731              template <typename T> S<T>::~S() {}
3732
3733            is invalid, since `~' must be followed by a class-name, but
3734            `S<T>' is dependent, and so not known to be a class.
3735            That's not right; we need to look in uninstantiated
3736            templates.  A further complication arises from:
3737
3738              template <typename T> void f(T t) {
3739                t.T::~T();
3740              }
3741
3742            Here, it is not possible to look up `T' in the scope of `T'
3743            itself.  We must look in both the current scope, and the
3744            scope of the containing complete expression.
3745
3746            Yet another issue is:
3747
3748              struct S {
3749                int S;
3750                ~S();
3751              };
3752
3753              S::~S() {}
3754
3755            The standard does not seem to say that the `S' in `~S'
3756            should refer to the type `S' and not the data member
3757            `S::S'.  */
3758
3759         /* DR 244 says that we look up the name after the "~" in the
3760            same scope as we looked up the qualifying name.  That idea
3761            isn't fully worked out; it's more complicated than that.  */
3762         scope = parser->scope;
3763         object_scope = parser->object_scope;
3764         qualifying_scope = parser->qualifying_scope;
3765
3766         /* Check for invalid scopes.  */
3767         if (scope == error_mark_node)
3768           {
3769             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3770               cp_lexer_consume_token (parser->lexer);
3771             return error_mark_node;
3772           }
3773         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3774           {
3775             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3776               error ("%Hscope %qT before %<~%> is not a class-name",
3777                      &token->location, scope);
3778             cp_parser_simulate_error (parser);
3779             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3780               cp_lexer_consume_token (parser->lexer);
3781             return error_mark_node;
3782           }
3783         gcc_assert (!scope || TYPE_P (scope));
3784
3785         /* If the name is of the form "X::~X" it's OK.  */
3786         token = cp_lexer_peek_token (parser->lexer);
3787         if (scope
3788             && token->type == CPP_NAME
3789             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3790                 == CPP_OPEN_PAREN)
3791             && constructor_name_p (token->u.value, scope))
3792           {
3793             cp_lexer_consume_token (parser->lexer);
3794             return build_nt (BIT_NOT_EXPR, scope);
3795           }
3796
3797         /* If there was an explicit qualification (S::~T), first look
3798            in the scope given by the qualification (i.e., S).  */
3799         done = false;
3800         type_decl = NULL_TREE;
3801         if (scope)
3802           {
3803             cp_parser_parse_tentatively (parser);
3804             type_decl = cp_parser_class_name (parser,
3805                                               /*typename_keyword_p=*/false,
3806                                               /*template_keyword_p=*/false,
3807                                               none_type,
3808                                               /*check_dependency=*/false,
3809                                               /*class_head_p=*/false,
3810                                               declarator_p);
3811             if (cp_parser_parse_definitely (parser))
3812               done = true;
3813           }
3814         /* In "N::S::~S", look in "N" as well.  */
3815         if (!done && scope && qualifying_scope)
3816           {
3817             cp_parser_parse_tentatively (parser);
3818             parser->scope = qualifying_scope;
3819             parser->object_scope = NULL_TREE;
3820             parser->qualifying_scope = NULL_TREE;
3821             type_decl
3822               = cp_parser_class_name (parser,
3823                                       /*typename_keyword_p=*/false,
3824                                       /*template_keyword_p=*/false,
3825                                       none_type,
3826                                       /*check_dependency=*/false,
3827                                       /*class_head_p=*/false,
3828                                       declarator_p);
3829             if (cp_parser_parse_definitely (parser))
3830               done = true;
3831           }
3832         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3833         else if (!done && object_scope)
3834           {
3835             cp_parser_parse_tentatively (parser);
3836             parser->scope = object_scope;
3837             parser->object_scope = NULL_TREE;
3838             parser->qualifying_scope = NULL_TREE;
3839             type_decl
3840               = cp_parser_class_name (parser,
3841                                       /*typename_keyword_p=*/false,
3842                                       /*template_keyword_p=*/false,
3843                                       none_type,
3844                                       /*check_dependency=*/false,
3845                                       /*class_head_p=*/false,
3846                                       declarator_p);
3847             if (cp_parser_parse_definitely (parser))
3848               done = true;
3849           }
3850         /* Look in the surrounding context.  */
3851         if (!done)
3852           {
3853             parser->scope = NULL_TREE;
3854             parser->object_scope = NULL_TREE;
3855             parser->qualifying_scope = NULL_TREE;
3856             type_decl
3857               = cp_parser_class_name (parser,
3858                                       /*typename_keyword_p=*/false,
3859                                       /*template_keyword_p=*/false,
3860                                       none_type,
3861                                       /*check_dependency=*/false,
3862                                       /*class_head_p=*/false,
3863                                       declarator_p);
3864           }
3865         /* If an error occurred, assume that the name of the
3866            destructor is the same as the name of the qualifying
3867            class.  That allows us to keep parsing after running
3868            into ill-formed destructor names.  */
3869         if (type_decl == error_mark_node && scope)
3870           return build_nt (BIT_NOT_EXPR, scope);
3871         else if (type_decl == error_mark_node)
3872           return error_mark_node;
3873
3874         /* Check that destructor name and scope match.  */
3875         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3876           {
3877             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3878               error ("%Hdeclaration of %<~%T%> as member of %qT",
3879                      &token->location, type_decl, scope);
3880             cp_parser_simulate_error (parser);
3881             return error_mark_node;
3882           }
3883
3884         /* [class.dtor]
3885
3886            A typedef-name that names a class shall not be used as the
3887            identifier in the declarator for a destructor declaration.  */
3888         if (declarator_p
3889             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3890             && !DECL_SELF_REFERENCE_P (type_decl)
3891             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3892           error ("%Htypedef-name %qD used as destructor declarator",
3893                  &token->location, type_decl);
3894
3895         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3896       }
3897
3898     case CPP_KEYWORD:
3899       if (token->keyword == RID_OPERATOR)
3900         {
3901           tree id;
3902
3903           /* This could be a template-id, so we try that first.  */
3904           cp_parser_parse_tentatively (parser);
3905           /* Try a template-id.  */
3906           id = cp_parser_template_id (parser, template_keyword_p,
3907                                       /*check_dependency_p=*/true,
3908                                       declarator_p);
3909           /* If that worked, we're done.  */
3910           if (cp_parser_parse_definitely (parser))
3911             return id;
3912           /* We still don't know whether we're looking at an
3913              operator-function-id or a conversion-function-id.  */
3914           cp_parser_parse_tentatively (parser);
3915           /* Try an operator-function-id.  */
3916           id = cp_parser_operator_function_id (parser);
3917           /* If that didn't work, try a conversion-function-id.  */
3918           if (!cp_parser_parse_definitely (parser))
3919             id = cp_parser_conversion_function_id (parser);
3920
3921           return id;
3922         }
3923       /* Fall through.  */
3924
3925     default:
3926       if (optional_p)
3927         return NULL_TREE;
3928       cp_parser_error (parser, "expected unqualified-id");
3929       return error_mark_node;
3930     }
3931 }
3932
3933 /* Parse an (optional) nested-name-specifier.
3934
3935    nested-name-specifier: [C++98]
3936      class-or-namespace-name :: nested-name-specifier [opt]
3937      class-or-namespace-name :: template nested-name-specifier [opt]
3938
3939    nested-name-specifier: [C++0x]
3940      type-name ::
3941      namespace-name ::
3942      nested-name-specifier identifier ::
3943      nested-name-specifier template [opt] simple-template-id ::
3944
3945    PARSER->SCOPE should be set appropriately before this function is
3946    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3947    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3948    in name lookups.
3949
3950    Sets PARSER->SCOPE to the class (TYPE) or namespace
3951    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3952    it unchanged if there is no nested-name-specifier.  Returns the new
3953    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3954
3955    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3956    part of a declaration and/or decl-specifier.  */
3957
3958 static tree
3959 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3960                                      bool typename_keyword_p,
3961                                      bool check_dependency_p,
3962                                      bool type_p,
3963                                      bool is_declaration)
3964 {
3965   bool success = false;
3966   cp_token_position start = 0;
3967   cp_token *token;
3968
3969   /* Remember where the nested-name-specifier starts.  */
3970   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3971     {
3972       start = cp_lexer_token_position (parser->lexer, false);
3973       push_deferring_access_checks (dk_deferred);
3974     }
3975
3976   while (true)
3977     {
3978       tree new_scope;
3979       tree old_scope;
3980       tree saved_qualifying_scope;
3981       bool template_keyword_p;
3982
3983       /* Spot cases that cannot be the beginning of a
3984          nested-name-specifier.  */
3985       token = cp_lexer_peek_token (parser->lexer);
3986
3987       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3988          the already parsed nested-name-specifier.  */
3989       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3990         {
3991           /* Grab the nested-name-specifier and continue the loop.  */
3992           cp_parser_pre_parsed_nested_name_specifier (parser);
3993           /* If we originally encountered this nested-name-specifier
3994              with IS_DECLARATION set to false, we will not have
3995              resolved TYPENAME_TYPEs, so we must do so here.  */
3996           if (is_declaration
3997               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3998             {
3999               new_scope = resolve_typename_type (parser->scope,
4000                                                  /*only_current_p=*/false);
4001               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4002                 parser->scope = new_scope;
4003             }
4004           success = true;
4005           continue;
4006         }
4007
4008       /* Spot cases that cannot be the beginning of a
4009          nested-name-specifier.  On the second and subsequent times
4010          through the loop, we look for the `template' keyword.  */
4011       if (success && token->keyword == RID_TEMPLATE)
4012         ;
4013       /* A template-id can start a nested-name-specifier.  */
4014       else if (token->type == CPP_TEMPLATE_ID)
4015         ;
4016       else
4017         {
4018           /* If the next token is not an identifier, then it is
4019              definitely not a type-name or namespace-name.  */
4020           if (token->type != CPP_NAME)
4021             break;
4022           /* If the following token is neither a `<' (to begin a
4023              template-id), nor a `::', then we are not looking at a
4024              nested-name-specifier.  */
4025           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4026           if (token->type != CPP_SCOPE
4027               && !cp_parser_nth_token_starts_template_argument_list_p
4028                   (parser, 2))
4029             break;
4030         }
4031
4032       /* The nested-name-specifier is optional, so we parse
4033          tentatively.  */
4034       cp_parser_parse_tentatively (parser);
4035
4036       /* Look for the optional `template' keyword, if this isn't the
4037          first time through the loop.  */
4038       if (success)
4039         template_keyword_p = cp_parser_optional_template_keyword (parser);
4040       else
4041         template_keyword_p = false;
4042
4043       /* Save the old scope since the name lookup we are about to do
4044          might destroy it.  */
4045       old_scope = parser->scope;
4046       saved_qualifying_scope = parser->qualifying_scope;
4047       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4048          look up names in "X<T>::I" in order to determine that "Y" is
4049          a template.  So, if we have a typename at this point, we make
4050          an effort to look through it.  */
4051       if (is_declaration
4052           && !typename_keyword_p
4053           && parser->scope
4054           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4055         parser->scope = resolve_typename_type (parser->scope,
4056                                                /*only_current_p=*/false);
4057       /* Parse the qualifying entity.  */
4058       new_scope
4059         = cp_parser_qualifying_entity (parser,
4060                                        typename_keyword_p,
4061                                        template_keyword_p,
4062                                        check_dependency_p,
4063                                        type_p,
4064                                        is_declaration);
4065       /* Look for the `::' token.  */
4066       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4067
4068       /* If we found what we wanted, we keep going; otherwise, we're
4069          done.  */
4070       if (!cp_parser_parse_definitely (parser))
4071         {
4072           bool error_p = false;
4073
4074           /* Restore the OLD_SCOPE since it was valid before the
4075              failed attempt at finding the last
4076              class-or-namespace-name.  */
4077           parser->scope = old_scope;
4078           parser->qualifying_scope = saved_qualifying_scope;
4079           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4080             break;
4081           /* If the next token is an identifier, and the one after
4082              that is a `::', then any valid interpretation would have
4083              found a class-or-namespace-name.  */
4084           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4085                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4086                      == CPP_SCOPE)
4087                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4088                      != CPP_COMPL))
4089             {
4090               token = cp_lexer_consume_token (parser->lexer);
4091               if (!error_p)
4092                 {
4093                   if (!token->ambiguous_p)
4094                     {
4095                       tree decl;
4096                       tree ambiguous_decls;
4097
4098                       decl = cp_parser_lookup_name (parser, token->u.value,
4099                                                     none_type,
4100                                                     /*is_template=*/false,
4101                                                     /*is_namespace=*/false,
4102                                                     /*check_dependency=*/true,
4103                                                     &ambiguous_decls,
4104                                                     token->location);
4105                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4106                         error ("%H%qD used without template parameters",
4107                                &token->location, decl);
4108                       else if (ambiguous_decls)
4109                         {
4110                           error ("%Hreference to %qD is ambiguous",
4111                                  &token->location, token->u.value);
4112                           print_candidates (ambiguous_decls);
4113                           decl = error_mark_node;
4114                         }
4115                       else
4116                         {
4117                           const char* msg = "is not a class or namespace";
4118                           if (cxx_dialect != cxx98)
4119                             msg = "is not a class, namespace, or enumeration";
4120                           cp_parser_name_lookup_error
4121                             (parser, token->u.value, decl, msg,
4122                              token->location);
4123                         }
4124                     }
4125                   parser->scope = error_mark_node;
4126                   error_p = true;
4127                   /* Treat this as a successful nested-name-specifier
4128                      due to:
4129
4130                      [basic.lookup.qual]
4131
4132                      If the name found is not a class-name (clause
4133                      _class_) or namespace-name (_namespace.def_), the
4134                      program is ill-formed.  */
4135                   success = true;
4136                 }
4137               cp_lexer_consume_token (parser->lexer);
4138             }
4139           break;
4140         }
4141       /* We've found one valid nested-name-specifier.  */
4142       success = true;
4143       /* Name lookup always gives us a DECL.  */
4144       if (TREE_CODE (new_scope) == TYPE_DECL)
4145         new_scope = TREE_TYPE (new_scope);
4146       /* Uses of "template" must be followed by actual templates.  */
4147       if (template_keyword_p
4148           && !(CLASS_TYPE_P (new_scope)
4149                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4150                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4151                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4152           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4153                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4154                    == TEMPLATE_ID_EXPR)))
4155         permerror (input_location, TYPE_P (new_scope)
4156                    ? "%qT is not a template"
4157                    : "%qD is not a template",
4158                    new_scope);
4159       /* If it is a class scope, try to complete it; we are about to
4160          be looking up names inside the class.  */
4161       if (TYPE_P (new_scope)
4162           /* Since checking types for dependency can be expensive,
4163              avoid doing it if the type is already complete.  */
4164           && !COMPLETE_TYPE_P (new_scope)
4165           /* Do not try to complete dependent types.  */
4166           && !dependent_type_p (new_scope))
4167         {
4168           new_scope = complete_type (new_scope);
4169           /* If it is a typedef to current class, use the current
4170              class instead, as the typedef won't have any names inside
4171              it yet.  */
4172           if (!COMPLETE_TYPE_P (new_scope)
4173               && currently_open_class (new_scope))
4174             new_scope = TYPE_MAIN_VARIANT (new_scope);
4175         }
4176       /* Make sure we look in the right scope the next time through
4177          the loop.  */
4178       parser->scope = new_scope;
4179     }
4180
4181   /* If parsing tentatively, replace the sequence of tokens that makes
4182      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4183      token.  That way, should we re-parse the token stream, we will
4184      not have to repeat the effort required to do the parse, nor will
4185      we issue duplicate error messages.  */
4186   if (success && start)
4187     {
4188       cp_token *token;
4189
4190       token = cp_lexer_token_at (parser->lexer, start);
4191       /* Reset the contents of the START token.  */
4192       token->type = CPP_NESTED_NAME_SPECIFIER;
4193       /* Retrieve any deferred checks.  Do not pop this access checks yet
4194          so the memory will not be reclaimed during token replacing below.  */
4195       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4196       token->u.tree_check_value->value = parser->scope;
4197       token->u.tree_check_value->checks = get_deferred_access_checks ();
4198       token->u.tree_check_value->qualifying_scope =
4199         parser->qualifying_scope;
4200       token->keyword = RID_MAX;
4201
4202       /* Purge all subsequent tokens.  */
4203       cp_lexer_purge_tokens_after (parser->lexer, start);
4204     }
4205
4206   if (start)
4207     pop_to_parent_deferring_access_checks ();
4208
4209   return success ? parser->scope : NULL_TREE;
4210 }
4211
4212 /* Parse a nested-name-specifier.  See
4213    cp_parser_nested_name_specifier_opt for details.  This function
4214    behaves identically, except that it will an issue an error if no
4215    nested-name-specifier is present.  */
4216
4217 static tree
4218 cp_parser_nested_name_specifier (cp_parser *parser,
4219                                  bool typename_keyword_p,
4220                                  bool check_dependency_p,
4221                                  bool type_p,
4222                                  bool is_declaration)
4223 {
4224   tree scope;
4225
4226   /* Look for the nested-name-specifier.  */
4227   scope = cp_parser_nested_name_specifier_opt (parser,
4228                                                typename_keyword_p,
4229                                                check_dependency_p,
4230                                                type_p,
4231                                                is_declaration);
4232   /* If it was not present, issue an error message.  */
4233   if (!scope)
4234     {
4235       cp_parser_error (parser, "expected nested-name-specifier");
4236       parser->scope = NULL_TREE;
4237     }
4238
4239   return scope;
4240 }
4241
4242 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4243    this is either a class-name or a namespace-name (which corresponds
4244    to the class-or-namespace-name production in the grammar). For
4245    C++0x, it can also be a type-name that refers to an enumeration
4246    type.
4247
4248    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4249    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4250    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4251    TYPE_P is TRUE iff the next name should be taken as a class-name,
4252    even the same name is declared to be another entity in the same
4253    scope.
4254
4255    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4256    specified by the class-or-namespace-name.  If neither is found the
4257    ERROR_MARK_NODE is returned.  */
4258
4259 static tree
4260 cp_parser_qualifying_entity (cp_parser *parser,
4261                              bool typename_keyword_p,
4262                              bool template_keyword_p,
4263                              bool check_dependency_p,
4264                              bool type_p,
4265                              bool is_declaration)
4266 {
4267   tree saved_scope;
4268   tree saved_qualifying_scope;
4269   tree saved_object_scope;
4270   tree scope;
4271   bool only_class_p;
4272   bool successful_parse_p;
4273
4274   /* Before we try to parse the class-name, we must save away the
4275      current PARSER->SCOPE since cp_parser_class_name will destroy
4276      it.  */
4277   saved_scope = parser->scope;
4278   saved_qualifying_scope = parser->qualifying_scope;
4279   saved_object_scope = parser->object_scope;
4280   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4281      there is no need to look for a namespace-name.  */
4282   only_class_p = template_keyword_p 
4283     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4284   if (!only_class_p)
4285     cp_parser_parse_tentatively (parser);
4286   scope = cp_parser_class_name (parser,
4287                                 typename_keyword_p,
4288                                 template_keyword_p,
4289                                 type_p ? class_type : none_type,
4290                                 check_dependency_p,
4291                                 /*class_head_p=*/false,
4292                                 is_declaration);
4293   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4294   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4295   if (!only_class_p 
4296       && cxx_dialect != cxx98
4297       && !successful_parse_p)
4298     {
4299       /* Restore the saved scope.  */
4300       parser->scope = saved_scope;
4301       parser->qualifying_scope = saved_qualifying_scope;
4302       parser->object_scope = saved_object_scope;
4303
4304       /* Parse tentatively.  */
4305       cp_parser_parse_tentatively (parser);
4306      
4307       /* Parse a typedef-name or enum-name.  */
4308       scope = cp_parser_nonclass_name (parser);
4309       successful_parse_p = cp_parser_parse_definitely (parser);
4310     }
4311   /* If that didn't work, try for a namespace-name.  */
4312   if (!only_class_p && !successful_parse_p)
4313     {
4314       /* Restore the saved scope.  */
4315       parser->scope = saved_scope;
4316       parser->qualifying_scope = saved_qualifying_scope;
4317       parser->object_scope = saved_object_scope;
4318       /* If we are not looking at an identifier followed by the scope
4319          resolution operator, then this is not part of a
4320          nested-name-specifier.  (Note that this function is only used
4321          to parse the components of a nested-name-specifier.)  */
4322       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4323           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4324         return error_mark_node;
4325       scope = cp_parser_namespace_name (parser);
4326     }
4327
4328   return scope;
4329 }
4330
4331 /* Parse a postfix-expression.
4332
4333    postfix-expression:
4334      primary-expression
4335      postfix-expression [ expression ]
4336      postfix-expression ( expression-list [opt] )
4337      simple-type-specifier ( expression-list [opt] )
4338      typename :: [opt] nested-name-specifier identifier
4339        ( expression-list [opt] )
4340      typename :: [opt] nested-name-specifier template [opt] template-id
4341        ( expression-list [opt] )
4342      postfix-expression . template [opt] id-expression
4343      postfix-expression -> template [opt] id-expression
4344      postfix-expression . pseudo-destructor-name
4345      postfix-expression -> pseudo-destructor-name
4346      postfix-expression ++
4347      postfix-expression --
4348      dynamic_cast < type-id > ( expression )
4349      static_cast < type-id > ( expression )
4350      reinterpret_cast < type-id > ( expression )
4351      const_cast < type-id > ( expression )
4352      typeid ( expression )
4353      typeid ( type-id )
4354
4355    GNU Extension:
4356
4357    postfix-expression:
4358      ( type-id ) { initializer-list , [opt] }
4359
4360    This extension is a GNU version of the C99 compound-literal
4361    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4362    but they are essentially the same concept.)
4363
4364    If ADDRESS_P is true, the postfix expression is the operand of the
4365    `&' operator.  CAST_P is true if this expression is the target of a
4366    cast.
4367
4368    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4369    class member access expressions [expr.ref].
4370
4371    Returns a representation of the expression.  */
4372
4373 static tree
4374 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4375                               bool member_access_only_p)
4376 {
4377   cp_token *token;
4378   enum rid keyword;
4379   cp_id_kind idk = CP_ID_KIND_NONE;
4380   tree postfix_expression = NULL_TREE;
4381   bool is_member_access = false;
4382
4383   /* Peek at the next token.  */
4384   token = cp_lexer_peek_token (parser->lexer);
4385   /* Some of the productions are determined by keywords.  */
4386   keyword = token->keyword;
4387   switch (keyword)
4388     {
4389     case RID_DYNCAST:
4390     case RID_STATCAST:
4391     case RID_REINTCAST:
4392     case RID_CONSTCAST:
4393       {
4394         tree type;
4395         tree expression;
4396         const char *saved_message;
4397
4398         /* All of these can be handled in the same way from the point
4399            of view of parsing.  Begin by consuming the token
4400            identifying the cast.  */
4401         cp_lexer_consume_token (parser->lexer);
4402
4403         /* New types cannot be defined in the cast.  */
4404         saved_message = parser->type_definition_forbidden_message;
4405         parser->type_definition_forbidden_message
4406           = "types may not be defined in casts";
4407
4408         /* Look for the opening `<'.  */
4409         cp_parser_require (parser, CPP_LESS, "%<<%>");
4410         /* Parse the type to which we are casting.  */
4411         type = cp_parser_type_id (parser);
4412         /* Look for the closing `>'.  */
4413         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4414         /* Restore the old message.  */
4415         parser->type_definition_forbidden_message = saved_message;
4416
4417         /* And the expression which is being cast.  */
4418         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4419         expression = cp_parser_expression (parser, /*cast_p=*/true);
4420         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4421
4422         /* Only type conversions to integral or enumeration types
4423            can be used in constant-expressions.  */
4424         if (!cast_valid_in_integral_constant_expression_p (type)
4425             && (cp_parser_non_integral_constant_expression
4426                 (parser,
4427                  "a cast to a type other than an integral or "
4428                  "enumeration type")))
4429           return error_mark_node;
4430
4431         switch (keyword)
4432           {
4433           case RID_DYNCAST:
4434             postfix_expression
4435               = build_dynamic_cast (type, expression, tf_warning_or_error);
4436             break;
4437           case RID_STATCAST:
4438             postfix_expression
4439               = build_static_cast (type, expression, tf_warning_or_error);
4440             break;
4441           case RID_REINTCAST:
4442             postfix_expression
4443               = build_reinterpret_cast (type, expression, 
4444                                         tf_warning_or_error);
4445             break;
4446           case RID_CONSTCAST:
4447             postfix_expression
4448               = build_const_cast (type, expression, tf_warning_or_error);
4449             break;
4450           default:
4451             gcc_unreachable ();
4452           }
4453       }
4454       break;
4455
4456     case RID_TYPEID:
4457       {
4458         tree type;
4459         const char *saved_message;
4460         bool saved_in_type_id_in_expr_p;
4461
4462         /* Consume the `typeid' token.  */
4463         cp_lexer_consume_token (parser->lexer);
4464         /* Look for the `(' token.  */
4465         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4466         /* Types cannot be defined in a `typeid' expression.  */
4467         saved_message = parser->type_definition_forbidden_message;
4468         parser->type_definition_forbidden_message
4469           = "types may not be defined in a %<typeid%> expression";
4470         /* We can't be sure yet whether we're looking at a type-id or an
4471            expression.  */
4472         cp_parser_parse_tentatively (parser);
4473         /* Try a type-id first.  */
4474         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4475         parser->in_type_id_in_expr_p = true;
4476         type = cp_parser_type_id (parser);
4477         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4478         /* Look for the `)' token.  Otherwise, we can't be sure that
4479            we're not looking at an expression: consider `typeid (int
4480            (3))', for example.  */
4481         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4482         /* If all went well, simply lookup the type-id.  */
4483         if (cp_parser_parse_definitely (parser))
4484           postfix_expression = get_typeid (type);
4485         /* Otherwise, fall back to the expression variant.  */
4486         else
4487           {
4488             tree expression;
4489
4490             /* Look for an expression.  */
4491             expression = cp_parser_expression (parser, /*cast_p=*/false);
4492             /* Compute its typeid.  */
4493             postfix_expression = build_typeid (expression);
4494             /* Look for the `)' token.  */
4495             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4496           }
4497         /* Restore the saved message.  */
4498         parser->type_definition_forbidden_message = saved_message;
4499         /* `typeid' may not appear in an integral constant expression.  */
4500         if (cp_parser_non_integral_constant_expression(parser,
4501                                                        "%<typeid%> operator"))
4502           return error_mark_node;
4503       }
4504       break;
4505
4506     case RID_TYPENAME:
4507       {
4508         tree type;
4509         /* The syntax permitted here is the same permitted for an
4510            elaborated-type-specifier.  */
4511         type = cp_parser_elaborated_type_specifier (parser,
4512                                                     /*is_friend=*/false,
4513                                                     /*is_declaration=*/false);
4514         postfix_expression = cp_parser_functional_cast (parser, type);
4515       }
4516       break;
4517
4518     default:
4519       {
4520         tree type;
4521
4522         /* If the next thing is a simple-type-specifier, we may be
4523            looking at a functional cast.  We could also be looking at
4524            an id-expression.  So, we try the functional cast, and if
4525            that doesn't work we fall back to the primary-expression.  */
4526         cp_parser_parse_tentatively (parser);
4527         /* Look for the simple-type-specifier.  */
4528         type = cp_parser_simple_type_specifier (parser,
4529                                                 /*decl_specs=*/NULL,
4530                                                 CP_PARSER_FLAGS_NONE);
4531         /* Parse the cast itself.  */
4532         if (!cp_parser_error_occurred (parser))
4533           postfix_expression
4534             = cp_parser_functional_cast (parser, type);
4535         /* If that worked, we're done.  */
4536         if (cp_parser_parse_definitely (parser))
4537           break;
4538
4539         /* If the functional-cast didn't work out, try a
4540            compound-literal.  */
4541         if (cp_parser_allow_gnu_extensions_p (parser)
4542             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4543           {
4544             VEC(constructor_elt,gc) *initializer_list = NULL;
4545             bool saved_in_type_id_in_expr_p;
4546
4547             cp_parser_parse_tentatively (parser);
4548             /* Consume the `('.  */
4549             cp_lexer_consume_token (parser->lexer);
4550             /* Parse the type.  */
4551             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4552             parser->in_type_id_in_expr_p = true;
4553             type = cp_parser_type_id (parser);
4554             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4555             /* Look for the `)'.  */
4556             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4557             /* Look for the `{'.  */
4558             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4559             /* If things aren't going well, there's no need to
4560                keep going.  */
4561             if (!cp_parser_error_occurred (parser))
4562               {
4563                 bool non_constant_p;
4564                 /* Parse the initializer-list.  */
4565                 initializer_list
4566                   = cp_parser_initializer_list (parser, &non_constant_p);
4567                 /* Allow a trailing `,'.  */
4568                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4569                   cp_lexer_consume_token (parser->lexer);
4570                 /* Look for the final `}'.  */
4571                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4572               }
4573             /* If that worked, we're definitely looking at a
4574                compound-literal expression.  */
4575             if (cp_parser_parse_definitely (parser))
4576               {
4577                 /* Warn the user that a compound literal is not
4578                    allowed in standard C++.  */
4579                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4580                 /* For simplicity, we disallow compound literals in
4581                    constant-expressions.  We could
4582                    allow compound literals of integer type, whose
4583                    initializer was a constant, in constant
4584                    expressions.  Permitting that usage, as a further
4585                    extension, would not change the meaning of any
4586                    currently accepted programs.  (Of course, as
4587                    compound literals are not part of ISO C++, the
4588                    standard has nothing to say.)  */
4589                 if (cp_parser_non_integral_constant_expression 
4590                     (parser, "non-constant compound literals"))
4591                   {
4592                     postfix_expression = error_mark_node;
4593                     break;
4594                   }
4595                 /* Form the representation of the compound-literal.  */
4596                 postfix_expression
4597                   = (finish_compound_literal
4598                      (type, build_constructor (init_list_type_node,
4599                                                initializer_list)));
4600                 break;
4601               }
4602           }
4603
4604         /* It must be a primary-expression.  */
4605         postfix_expression
4606           = cp_parser_primary_expression (parser, address_p, cast_p,
4607                                           /*template_arg_p=*/false,
4608                                           &idk);
4609       }
4610       break;
4611     }
4612
4613   /* Keep looping until the postfix-expression is complete.  */
4614   while (true)
4615     {
4616       if (idk == CP_ID_KIND_UNQUALIFIED
4617           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4618           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4619         /* It is not a Koenig lookup function call.  */
4620         postfix_expression
4621           = unqualified_name_lookup_error (postfix_expression);
4622
4623       /* Peek at the next token.  */
4624       token = cp_lexer_peek_token (parser->lexer);
4625
4626       switch (token->type)
4627         {
4628         case CPP_OPEN_SQUARE:
4629           postfix_expression
4630             = cp_parser_postfix_open_square_expression (parser,
4631                                                         postfix_expression,
4632                                                         false);
4633           idk = CP_ID_KIND_NONE;
4634           is_member_access = false;
4635           break;
4636
4637         case CPP_OPEN_PAREN:
4638           /* postfix-expression ( expression-list [opt] ) */
4639           {
4640             bool koenig_p;
4641             bool is_builtin_constant_p;
4642             bool saved_integral_constant_expression_p = false;
4643             bool saved_non_integral_constant_expression_p = false;
4644             tree args;
4645
4646             is_member_access = false;
4647
4648             is_builtin_constant_p
4649               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4650             if (is_builtin_constant_p)
4651               {
4652                 /* The whole point of __builtin_constant_p is to allow
4653                    non-constant expressions to appear as arguments.  */
4654                 saved_integral_constant_expression_p
4655                   = parser->integral_constant_expression_p;
4656                 saved_non_integral_constant_expression_p
4657                   = parser->non_integral_constant_expression_p;
4658                 parser->integral_constant_expression_p = false;
4659               }
4660             args = (cp_parser_parenthesized_expression_list
4661                     (parser, /*is_attribute_list=*/false,
4662                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4663                      /*non_constant_p=*/NULL));
4664             if (is_builtin_constant_p)
4665               {
4666                 parser->integral_constant_expression_p
4667                   = saved_integral_constant_expression_p;
4668                 parser->non_integral_constant_expression_p
4669                   = saved_non_integral_constant_expression_p;
4670               }
4671
4672             if (args == error_mark_node)
4673               {
4674                 postfix_expression = error_mark_node;
4675                 break;
4676               }
4677
4678             /* Function calls are not permitted in
4679                constant-expressions.  */
4680             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4681                 && cp_parser_non_integral_constant_expression (parser,
4682                                                                "a function call"))
4683               {
4684                 postfix_expression = error_mark_node;
4685                 break;
4686               }
4687
4688             koenig_p = false;
4689             if (idk == CP_ID_KIND_UNQUALIFIED)
4690               {
4691                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4692                   {
4693                     if (args)
4694                       {
4695                         koenig_p = true;
4696                         postfix_expression
4697                           = perform_koenig_lookup (postfix_expression, args);
4698                       }
4699                     else
4700                       postfix_expression
4701                         = unqualified_fn_lookup_error (postfix_expression);
4702                   }
4703                 /* We do not perform argument-dependent lookup if
4704                    normal lookup finds a non-function, in accordance
4705                    with the expected resolution of DR 218.  */
4706                 else if (args && is_overloaded_fn (postfix_expression))
4707                   {
4708                     tree fn = get_first_fn (postfix_expression);
4709
4710                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4711                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4712
4713                     /* Only do argument dependent lookup if regular
4714                        lookup does not find a set of member functions.
4715                        [basic.lookup.koenig]/2a  */
4716                     if (!DECL_FUNCTION_MEMBER_P (fn))
4717                       {
4718                         koenig_p = true;
4719                         postfix_expression
4720                           = perform_koenig_lookup (postfix_expression, args);
4721                       }
4722                   }
4723               }
4724
4725             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4726               {
4727                 tree instance = TREE_OPERAND (postfix_expression, 0);
4728                 tree fn = TREE_OPERAND (postfix_expression, 1);
4729
4730                 if (processing_template_decl
4731                     && (type_dependent_expression_p (instance)
4732                         || (!BASELINK_P (fn)
4733                             && TREE_CODE (fn) != FIELD_DECL)
4734                         || type_dependent_expression_p (fn)
4735                         || any_type_dependent_arguments_p (args)))
4736                   {
4737                     postfix_expression
4738                       = build_nt_call_list (postfix_expression, args);
4739                     break;
4740                   }
4741
4742                 if (BASELINK_P (fn))
4743                   postfix_expression
4744                     = (build_new_method_call
4745                        (instance, fn, args, NULL_TREE,
4746                         (idk == CP_ID_KIND_QUALIFIED
4747                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4748                         /*fn_p=*/NULL,
4749                         tf_warning_or_error));
4750                 else
4751                   postfix_expression
4752                     = finish_call_expr (postfix_expression, args,
4753                                         /*disallow_virtual=*/false,
4754                                         /*koenig_p=*/false,
4755                                         tf_warning_or_error);
4756               }
4757             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4758                      || TREE_CODE (postfix_expression) == MEMBER_REF
4759                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4760               postfix_expression = (build_offset_ref_call_from_tree
4761                                     (postfix_expression, args));
4762             else if (idk == CP_ID_KIND_QUALIFIED)
4763               /* A call to a static class member, or a namespace-scope
4764                  function.  */
4765               postfix_expression
4766                 = finish_call_expr (postfix_expression, args,
4767                                     /*disallow_virtual=*/true,
4768                                     koenig_p,
4769                                     tf_warning_or_error);
4770             else
4771               /* All other function calls.  */
4772               postfix_expression
4773                 = finish_call_expr (postfix_expression, args,
4774                                     /*disallow_virtual=*/false,
4775                                     koenig_p,
4776                                     tf_warning_or_error);
4777
4778             if (warn_disallowed_functions)
4779               warn_if_disallowed_function_p (postfix_expression);
4780
4781             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4782             idk = CP_ID_KIND_NONE;
4783           }
4784           break;
4785
4786         case CPP_DOT:
4787         case CPP_DEREF:
4788           /* postfix-expression . template [opt] id-expression
4789              postfix-expression . pseudo-destructor-name
4790              postfix-expression -> template [opt] id-expression
4791              postfix-expression -> pseudo-destructor-name */
4792
4793           /* Consume the `.' or `->' operator.  */
4794           cp_lexer_consume_token (parser->lexer);
4795
4796           postfix_expression
4797             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4798                                                       postfix_expression,
4799                                                       false, &idk,
4800                                                       token->location);
4801
4802           is_member_access = true;
4803           break;
4804
4805         case CPP_PLUS_PLUS:
4806           /* postfix-expression ++  */
4807           /* Consume the `++' token.  */
4808           cp_lexer_consume_token (parser->lexer);
4809           /* Generate a representation for the complete expression.  */
4810           postfix_expression
4811             = finish_increment_expr (postfix_expression,
4812                                      POSTINCREMENT_EXPR);
4813           /* Increments may not appear in constant-expressions.  */
4814           if (cp_parser_non_integral_constant_expression (parser,
4815                                                           "an increment"))
4816             postfix_expression = error_mark_node;
4817           idk = CP_ID_KIND_NONE;
4818           is_member_access = false;
4819           break;
4820
4821         case CPP_MINUS_MINUS:
4822           /* postfix-expression -- */
4823           /* Consume the `--' token.  */
4824           cp_lexer_consume_token (parser->lexer);
4825           /* Generate a representation for the complete expression.  */
4826           postfix_expression
4827             = finish_increment_expr (postfix_expression,
4828                                      POSTDECREMENT_EXPR);
4829           /* Decrements may not appear in constant-expressions.  */
4830           if (cp_parser_non_integral_constant_expression (parser,
4831                                                           "a decrement"))
4832             postfix_expression = error_mark_node;
4833           idk = CP_ID_KIND_NONE;
4834           is_member_access = false;
4835           break;
4836
4837         default:
4838           if (member_access_only_p)
4839             return is_member_access? postfix_expression : error_mark_node;
4840           else
4841             return postfix_expression;
4842         }
4843     }
4844
4845   /* We should never get here.  */
4846   gcc_unreachable ();
4847   return error_mark_node;
4848 }
4849
4850 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4851    by cp_parser_builtin_offsetof.  We're looking for
4852
4853      postfix-expression [ expression ]
4854
4855    FOR_OFFSETOF is set if we're being called in that context, which
4856    changes how we deal with integer constant expressions.  */
4857
4858 static tree
4859 cp_parser_postfix_open_square_expression (cp_parser *parser,
4860                                           tree postfix_expression,
4861                                           bool for_offsetof)
4862 {
4863   tree index;
4864
4865   /* Consume the `[' token.  */
4866   cp_lexer_consume_token (parser->lexer);
4867
4868   /* Parse the index expression.  */
4869   /* ??? For offsetof, there is a question of what to allow here.  If
4870      offsetof is not being used in an integral constant expression context,
4871      then we *could* get the right answer by computing the value at runtime.
4872      If we are in an integral constant expression context, then we might
4873      could accept any constant expression; hard to say without analysis.
4874      Rather than open the barn door too wide right away, allow only integer
4875      constant expressions here.  */
4876   if (for_offsetof)
4877     index = cp_parser_constant_expression (parser, false, NULL);
4878   else
4879     index = cp_parser_expression (parser, /*cast_p=*/false);
4880
4881   /* Look for the closing `]'.  */
4882   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4883
4884   /* Build the ARRAY_REF.  */
4885   postfix_expression = grok_array_decl (postfix_expression, index);
4886
4887   /* When not doing offsetof, array references are not permitted in
4888      constant-expressions.  */
4889   if (!for_offsetof
4890       && (cp_parser_non_integral_constant_expression
4891           (parser, "an array reference")))
4892     postfix_expression = error_mark_node;
4893
4894   return postfix_expression;
4895 }
4896
4897 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4898    by cp_parser_builtin_offsetof.  We're looking for
4899
4900      postfix-expression . template [opt] id-expression
4901      postfix-expression . pseudo-destructor-name
4902      postfix-expression -> template [opt] id-expression
4903      postfix-expression -> pseudo-destructor-name
4904
4905    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4906    limits what of the above we'll actually accept, but nevermind.
4907    TOKEN_TYPE is the "." or "->" token, which will already have been
4908    removed from the stream.  */
4909
4910 static tree
4911 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4912                                         enum cpp_ttype token_type,
4913                                         tree postfix_expression,
4914                                         bool for_offsetof, cp_id_kind *idk,
4915                                         location_t location)
4916 {
4917   tree name;
4918   bool dependent_p;
4919   bool pseudo_destructor_p;
4920   tree scope = NULL_TREE;
4921
4922   /* If this is a `->' operator, dereference the pointer.  */
4923   if (token_type == CPP_DEREF)
4924     postfix_expression = build_x_arrow (postfix_expression);
4925   /* Check to see whether or not the expression is type-dependent.  */
4926   dependent_p = type_dependent_expression_p (postfix_expression);
4927   /* The identifier following the `->' or `.' is not qualified.  */
4928   parser->scope = NULL_TREE;
4929   parser->qualifying_scope = NULL_TREE;
4930   parser->object_scope = NULL_TREE;
4931   *idk = CP_ID_KIND_NONE;
4932   /* Enter the scope corresponding to the type of the object
4933      given by the POSTFIX_EXPRESSION.  */
4934   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4935     {
4936       scope = TREE_TYPE (postfix_expression);
4937       /* According to the standard, no expression should ever have
4938          reference type.  Unfortunately, we do not currently match
4939          the standard in this respect in that our internal representation
4940          of an expression may have reference type even when the standard
4941          says it does not.  Therefore, we have to manually obtain the
4942          underlying type here.  */
4943       scope = non_reference (scope);
4944       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4945       if (scope == unknown_type_node)
4946         {
4947           error ("%H%qE does not have class type", &location, postfix_expression);
4948           scope = NULL_TREE;
4949         }
4950       else
4951         scope = complete_type_or_else (scope, NULL_TREE);
4952       /* Let the name lookup machinery know that we are processing a
4953          class member access expression.  */
4954       parser->context->object_type = scope;
4955       /* If something went wrong, we want to be able to discern that case,
4956          as opposed to the case where there was no SCOPE due to the type
4957          of expression being dependent.  */
4958       if (!scope)
4959         scope = error_mark_node;
4960       /* If the SCOPE was erroneous, make the various semantic analysis
4961          functions exit quickly -- and without issuing additional error
4962          messages.  */
4963       if (scope == error_mark_node)
4964         postfix_expression = error_mark_node;
4965     }
4966
4967   /* Assume this expression is not a pseudo-destructor access.  */
4968   pseudo_destructor_p = false;
4969
4970   /* If the SCOPE is a scalar type, then, if this is a valid program,
4971      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4972      is type dependent, it can be pseudo-destructor-name or something else.
4973      Try to parse it as pseudo-destructor-name first.  */
4974   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4975     {
4976       tree s;
4977       tree type;
4978
4979       cp_parser_parse_tentatively (parser);
4980       /* Parse the pseudo-destructor-name.  */
4981       s = NULL_TREE;
4982       cp_parser_pseudo_destructor_name (parser, &s, &type);
4983       if (dependent_p
4984           && (cp_parser_error_occurred (parser)
4985               || TREE_CODE (type) != TYPE_DECL
4986               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4987         cp_parser_abort_tentative_parse (parser);
4988       else if (cp_parser_parse_definitely (parser))
4989         {
4990           pseudo_destructor_p = true;
4991           postfix_expression
4992             = finish_pseudo_destructor_expr (postfix_expression,
4993                                              s, TREE_TYPE (type));
4994         }
4995     }
4996
4997   if (!pseudo_destructor_p)
4998     {
4999       /* If the SCOPE is not a scalar type, we are looking at an
5000          ordinary class member access expression, rather than a
5001          pseudo-destructor-name.  */
5002       bool template_p;
5003       cp_token *token = cp_lexer_peek_token (parser->lexer);
5004       /* Parse the id-expression.  */
5005       name = (cp_parser_id_expression
5006               (parser,
5007                cp_parser_optional_template_keyword (parser),
5008                /*check_dependency_p=*/true,
5009                &template_p,
5010                /*declarator_p=*/false,
5011                /*optional_p=*/false));
5012       /* In general, build a SCOPE_REF if the member name is qualified.
5013          However, if the name was not dependent and has already been
5014          resolved; there is no need to build the SCOPE_REF.  For example;
5015
5016              struct X { void f(); };
5017              template <typename T> void f(T* t) { t->X::f(); }
5018
5019          Even though "t" is dependent, "X::f" is not and has been resolved
5020          to a BASELINK; there is no need to include scope information.  */
5021
5022       /* But we do need to remember that there was an explicit scope for
5023          virtual function calls.  */
5024       if (parser->scope)
5025         *idk = CP_ID_KIND_QUALIFIED;
5026
5027       /* If the name is a template-id that names a type, we will get a
5028          TYPE_DECL here.  That is invalid code.  */
5029       if (TREE_CODE (name) == TYPE_DECL)
5030         {
5031           error ("%Hinvalid use of %qD", &token->location, name);
5032           postfix_expression = error_mark_node;
5033         }
5034       else
5035         {
5036           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5037             {
5038               name = build_qualified_name (/*type=*/NULL_TREE,
5039                                            parser->scope,
5040                                            name,
5041                                            template_p);
5042               parser->scope = NULL_TREE;
5043               parser->qualifying_scope = NULL_TREE;
5044               parser->object_scope = NULL_TREE;
5045             }
5046           if (scope && name && BASELINK_P (name))
5047             adjust_result_of_qualified_name_lookup
5048               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5049           postfix_expression
5050             = finish_class_member_access_expr (postfix_expression, name,
5051                                                template_p, 
5052                                                tf_warning_or_error);
5053         }
5054     }
5055
5056   /* We no longer need to look up names in the scope of the object on
5057      the left-hand side of the `.' or `->' operator.  */
5058   parser->context->object_type = NULL_TREE;
5059
5060   /* Outside of offsetof, these operators may not appear in
5061      constant-expressions.  */
5062   if (!for_offsetof
5063       && (cp_parser_non_integral_constant_expression
5064           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5065     postfix_expression = error_mark_node;
5066
5067   return postfix_expression;
5068 }
5069
5070 /* Parse a parenthesized expression-list.
5071
5072    expression-list:
5073      assignment-expression
5074      expression-list, assignment-expression
5075
5076    attribute-list:
5077      expression-list
5078      identifier
5079      identifier, expression-list
5080
5081    CAST_P is true if this expression is the target of a cast.
5082
5083    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5084    argument pack.
5085
5086    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5087    representation of an assignment-expression.  Note that a TREE_LIST
5088    is returned even if there is only a single expression in the list.
5089    error_mark_node is returned if the ( and or ) are
5090    missing. NULL_TREE is returned on no expressions. The parentheses
5091    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5092    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5093    indicates whether or not all of the expressions in the list were
5094    constant.  */
5095
5096 static tree
5097 cp_parser_parenthesized_expression_list (cp_parser* parser,
5098                                          bool is_attribute_list,
5099                                          bool cast_p,
5100                                          bool allow_expansion_p,
5101                                          bool *non_constant_p)
5102 {
5103   tree expression_list = NULL_TREE;
5104   bool fold_expr_p = is_attribute_list;
5105   tree identifier = NULL_TREE;
5106   bool saved_greater_than_is_operator_p;
5107
5108   /* Assume all the expressions will be constant.  */
5109   if (non_constant_p)
5110     *non_constant_p = false;
5111
5112   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5113     return error_mark_node;
5114
5115   /* Within a parenthesized expression, a `>' token is always
5116      the greater-than operator.  */
5117   saved_greater_than_is_operator_p
5118     = parser->greater_than_is_operator_p;
5119   parser->greater_than_is_operator_p = true;
5120
5121   /* Consume expressions until there are no more.  */
5122   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5123     while (true)
5124       {
5125         tree expr;
5126
5127         /* At the beginning of attribute lists, check to see if the
5128            next token is an identifier.  */
5129         if (is_attribute_list
5130             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5131           {
5132             cp_token *token;
5133
5134             /* Consume the identifier.  */
5135             token = cp_lexer_consume_token (parser->lexer);
5136             /* Save the identifier.  */
5137             identifier = token->u.value;
5138           }
5139         else
5140           {
5141             bool expr_non_constant_p;
5142
5143             /* Parse the next assignment-expression.  */
5144             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5145               {
5146                 /* A braced-init-list.  */
5147                 maybe_warn_cpp0x ("extended initializer lists");
5148                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5149                 if (non_constant_p && expr_non_constant_p)
5150                   *non_constant_p = true;
5151               }
5152             else if (non_constant_p)
5153               {
5154                 expr = (cp_parser_constant_expression
5155                         (parser, /*allow_non_constant_p=*/true,
5156                          &expr_non_constant_p));
5157                 if (expr_non_constant_p)
5158                   *non_constant_p = true;
5159               }
5160             else
5161               expr = cp_parser_assignment_expression (parser, cast_p);
5162
5163             if (fold_expr_p)
5164               expr = fold_non_dependent_expr (expr);
5165
5166             /* If we have an ellipsis, then this is an expression
5167                expansion.  */
5168             if (allow_expansion_p
5169                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5170               {
5171                 /* Consume the `...'.  */
5172                 cp_lexer_consume_token (parser->lexer);
5173
5174                 /* Build the argument pack.  */
5175                 expr = make_pack_expansion (expr);
5176               }
5177
5178              /* Add it to the list.  We add error_mark_node
5179                 expressions to the list, so that we can still tell if
5180                 the correct form for a parenthesized expression-list
5181                 is found. That gives better errors.  */
5182             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5183
5184             if (expr == error_mark_node)
5185               goto skip_comma;
5186           }
5187
5188         /* After the first item, attribute lists look the same as
5189            expression lists.  */
5190         is_attribute_list = false;
5191
5192       get_comma:;
5193         /* If the next token isn't a `,', then we are done.  */
5194         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5195           break;
5196
5197         /* Otherwise, consume the `,' and keep going.  */
5198         cp_lexer_consume_token (parser->lexer);
5199       }
5200
5201   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5202     {
5203       int ending;
5204
5205     skip_comma:;
5206       /* We try and resync to an unnested comma, as that will give the
5207          user better diagnostics.  */
5208       ending = cp_parser_skip_to_closing_parenthesis (parser,
5209                                                       /*recovering=*/true,
5210                                                       /*or_comma=*/true,
5211                                                       /*consume_paren=*/true);
5212       if (ending < 0)
5213         goto get_comma;
5214       if (!ending)
5215         {
5216           parser->greater_than_is_operator_p
5217             = saved_greater_than_is_operator_p;
5218           return error_mark_node;
5219         }
5220     }
5221
5222   parser->greater_than_is_operator_p
5223     = saved_greater_than_is_operator_p;
5224
5225   /* We built up the list in reverse order so we must reverse it now.  */
5226   expression_list = nreverse (expression_list);
5227   if (identifier)
5228     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5229
5230   return expression_list;
5231 }
5232
5233 /* Parse a pseudo-destructor-name.
5234
5235    pseudo-destructor-name:
5236      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5237      :: [opt] nested-name-specifier template template-id :: ~ type-name
5238      :: [opt] nested-name-specifier [opt] ~ type-name
5239
5240    If either of the first two productions is used, sets *SCOPE to the
5241    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5242    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5243    or ERROR_MARK_NODE if the parse fails.  */
5244
5245 static void
5246 cp_parser_pseudo_destructor_name (cp_parser* parser,
5247                                   tree* scope,
5248                                   tree* type)
5249 {
5250   bool nested_name_specifier_p;
5251
5252   /* Assume that things will not work out.  */
5253   *type = error_mark_node;
5254
5255   /* Look for the optional `::' operator.  */
5256   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5257   /* Look for the optional nested-name-specifier.  */
5258   nested_name_specifier_p
5259     = (cp_parser_nested_name_specifier_opt (parser,
5260                                             /*typename_keyword_p=*/false,
5261                                             /*check_dependency_p=*/true,
5262                                             /*type_p=*/false,
5263                                             /*is_declaration=*/true)
5264        != NULL_TREE);
5265   /* Now, if we saw a nested-name-specifier, we might be doing the
5266      second production.  */
5267   if (nested_name_specifier_p
5268       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5269     {
5270       /* Consume the `template' keyword.  */
5271       cp_lexer_consume_token (parser->lexer);
5272       /* Parse the template-id.  */
5273       cp_parser_template_id (parser,
5274                              /*template_keyword_p=*/true,
5275                              /*check_dependency_p=*/false,
5276                              /*is_declaration=*/true);
5277       /* Look for the `::' token.  */
5278       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5279     }
5280   /* If the next token is not a `~', then there might be some
5281      additional qualification.  */
5282   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5283     {
5284       /* At this point, we're looking for "type-name :: ~".  The type-name
5285          must not be a class-name, since this is a pseudo-destructor.  So,
5286          it must be either an enum-name, or a typedef-name -- both of which
5287          are just identifiers.  So, we peek ahead to check that the "::"
5288          and "~" tokens are present; if they are not, then we can avoid
5289          calling type_name.  */
5290       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5291           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5292           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5293         {
5294           cp_parser_error (parser, "non-scalar type");
5295           return;
5296         }
5297
5298       /* Look for the type-name.  */
5299       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5300       if (*scope == error_mark_node)
5301         return;
5302
5303       /* Look for the `::' token.  */
5304       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5305     }
5306   else
5307     *scope = NULL_TREE;
5308
5309   /* Look for the `~'.  */
5310   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5311   /* Look for the type-name again.  We are not responsible for
5312      checking that it matches the first type-name.  */
5313   *type = cp_parser_nonclass_name (parser);
5314 }
5315
5316 /* Parse a unary-expression.
5317
5318    unary-expression:
5319      postfix-expression
5320      ++ cast-expression
5321      -- cast-expression
5322      unary-operator cast-expression
5323      sizeof unary-expression
5324      sizeof ( type-id )
5325      new-expression
5326      delete-expression
5327
5328    GNU Extensions:
5329
5330    unary-expression:
5331      __extension__ cast-expression
5332      __alignof__ unary-expression
5333      __alignof__ ( type-id )
5334      __real__ cast-expression
5335      __imag__ cast-expression
5336      && identifier
5337
5338    ADDRESS_P is true iff the unary-expression is appearing as the
5339    operand of the `&' operator.   CAST_P is true if this expression is
5340    the target of a cast.
5341
5342    Returns a representation of the expression.  */
5343
5344 static tree
5345 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5346 {
5347   cp_token *token;
5348   enum tree_code unary_operator;
5349
5350   /* Peek at the next token.  */
5351   token = cp_lexer_peek_token (parser->lexer);
5352   /* Some keywords give away the kind of expression.  */
5353   if (token->type == CPP_KEYWORD)
5354     {
5355       enum rid keyword = token->keyword;
5356
5357       switch (keyword)
5358         {
5359         case RID_ALIGNOF:
5360         case RID_SIZEOF:
5361           {
5362             tree operand;
5363             enum tree_code op;
5364
5365             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5366             /* Consume the token.  */
5367             cp_lexer_consume_token (parser->lexer);
5368             /* Parse the operand.  */
5369             operand = cp_parser_sizeof_operand (parser, keyword);
5370
5371             if (TYPE_P (operand))
5372               return cxx_sizeof_or_alignof_type (operand, op, true);
5373             else
5374               return cxx_sizeof_or_alignof_expr (operand, op, true);
5375           }
5376
5377         case RID_NEW:
5378           return cp_parser_new_expression (parser);
5379
5380         case RID_DELETE:
5381           return cp_parser_delete_expression (parser);
5382
5383         case RID_EXTENSION:
5384           {
5385             /* The saved value of the PEDANTIC flag.  */
5386             int saved_pedantic;
5387             tree expr;
5388
5389             /* Save away the PEDANTIC flag.  */
5390             cp_parser_extension_opt (parser, &saved_pedantic);
5391             /* Parse the cast-expression.  */
5392             expr = cp_parser_simple_cast_expression (parser);
5393             /* Restore the PEDANTIC flag.  */
5394             pedantic = saved_pedantic;
5395
5396             return expr;
5397           }
5398
5399         case RID_REALPART:
5400         case RID_IMAGPART:
5401           {
5402             tree expression;
5403
5404             /* Consume the `__real__' or `__imag__' token.  */
5405             cp_lexer_consume_token (parser->lexer);
5406             /* Parse the cast-expression.  */
5407             expression = cp_parser_simple_cast_expression (parser);
5408             /* Create the complete representation.  */
5409             return build_x_unary_op ((keyword == RID_REALPART
5410                                       ? REALPART_EXPR : IMAGPART_EXPR),
5411                                      expression,
5412                                      tf_warning_or_error);
5413           }
5414           break;
5415
5416         default:
5417           break;
5418         }
5419     }
5420
5421   /* Look for the `:: new' and `:: delete', which also signal the
5422      beginning of a new-expression, or delete-expression,
5423      respectively.  If the next token is `::', then it might be one of
5424      these.  */
5425   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5426     {
5427       enum rid keyword;
5428
5429       /* See if the token after the `::' is one of the keywords in
5430          which we're interested.  */
5431       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5432       /* If it's `new', we have a new-expression.  */
5433       if (keyword == RID_NEW)
5434         return cp_parser_new_expression (parser);
5435       /* Similarly, for `delete'.  */
5436       else if (keyword == RID_DELETE)
5437         return cp_parser_delete_expression (parser);
5438     }
5439
5440   /* Look for a unary operator.  */
5441   unary_operator = cp_parser_unary_operator (token);
5442   /* The `++' and `--' operators can be handled similarly, even though
5443      they are not technically unary-operators in the grammar.  */
5444   if (unary_operator == ERROR_MARK)
5445     {
5446       if (token->type == CPP_PLUS_PLUS)
5447         unary_operator = PREINCREMENT_EXPR;
5448       else if (token->type == CPP_MINUS_MINUS)
5449         unary_operator = PREDECREMENT_EXPR;
5450       /* Handle the GNU address-of-label extension.  */
5451       else if (cp_parser_allow_gnu_extensions_p (parser)
5452                && token->type == CPP_AND_AND)
5453         {
5454           tree identifier;
5455           tree expression;
5456
5457           /* Consume the '&&' token.  */
5458           cp_lexer_consume_token (parser->lexer);
5459           /* Look for the identifier.  */
5460           identifier = cp_parser_identifier (parser);
5461           /* Create an expression representing the address.  */
5462           expression = finish_label_address_expr (identifier);
5463           if (cp_parser_non_integral_constant_expression (parser,
5464                                                 "the address of a label"))
5465             expression = error_mark_node;
5466           return expression;
5467         }
5468     }
5469   if (unary_operator != ERROR_MARK)
5470     {
5471       tree cast_expression;
5472       tree expression = error_mark_node;
5473       const char *non_constant_p = NULL;
5474
5475       /* Consume the operator token.  */
5476       token = cp_lexer_consume_token (parser->lexer);
5477       /* Parse the cast-expression.  */
5478       cast_expression
5479         = cp_parser_cast_expression (parser,
5480                                      unary_operator == ADDR_EXPR,
5481                                      /*cast_p=*/false);
5482       /* Now, build an appropriate representation.  */
5483       switch (unary_operator)
5484         {
5485         case INDIRECT_REF:
5486           non_constant_p = "%<*%>";
5487           expression = build_x_indirect_ref (cast_expression, "unary *",
5488                                              tf_warning_or_error);
5489           break;
5490
5491         case ADDR_EXPR:
5492           non_constant_p = "%<&%>";
5493           /* Fall through.  */
5494         case BIT_NOT_EXPR:
5495           expression = build_x_unary_op (unary_operator, cast_expression,
5496                                          tf_warning_or_error);
5497           break;
5498
5499         case PREINCREMENT_EXPR:
5500         case PREDECREMENT_EXPR:
5501           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5502                             ? "%<++%>" : "%<--%>");
5503           /* Fall through.  */
5504         case UNARY_PLUS_EXPR:
5505         case NEGATE_EXPR:
5506         case TRUTH_NOT_EXPR:
5507           expression = finish_unary_op_expr (unary_operator, cast_expression);
5508           break;
5509
5510         default:
5511           gcc_unreachable ();
5512         }
5513
5514       if (non_constant_p
5515           && cp_parser_non_integral_constant_expression (parser,
5516                                                          non_constant_p))
5517         expression = error_mark_node;
5518
5519       return expression;
5520     }
5521
5522   return cp_parser_postfix_expression (parser, address_p, cast_p,
5523                                        /*member_access_only_p=*/false);
5524 }
5525
5526 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5527    unary-operator, the corresponding tree code is returned.  */
5528
5529 static enum tree_code
5530 cp_parser_unary_operator (cp_token* token)
5531 {
5532   switch (token->type)
5533     {
5534     case CPP_MULT:
5535       return INDIRECT_REF;
5536
5537     case CPP_AND:
5538       return ADDR_EXPR;
5539
5540     case CPP_PLUS:
5541       return UNARY_PLUS_EXPR;
5542
5543     case CPP_MINUS:
5544       return NEGATE_EXPR;
5545
5546     case CPP_NOT:
5547       return TRUTH_NOT_EXPR;
5548
5549     case CPP_COMPL:
5550       return BIT_NOT_EXPR;
5551
5552     default:
5553       return ERROR_MARK;
5554     }
5555 }
5556
5557 /* Parse a new-expression.
5558
5559    new-expression:
5560      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5561      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5562
5563    Returns a representation of the expression.  */
5564
5565 static tree
5566 cp_parser_new_expression (cp_parser* parser)
5567 {
5568   bool global_scope_p;
5569   tree placement;
5570   tree type;
5571   tree initializer;
5572   tree nelts;
5573
5574   /* Look for the optional `::' operator.  */
5575   global_scope_p
5576     = (cp_parser_global_scope_opt (parser,
5577                                    /*current_scope_valid_p=*/false)
5578        != NULL_TREE);
5579   /* Look for the `new' operator.  */
5580   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5581   /* There's no easy way to tell a new-placement from the
5582      `( type-id )' construct.  */
5583   cp_parser_parse_tentatively (parser);
5584   /* Look for a new-placement.  */
5585   placement = cp_parser_new_placement (parser);
5586   /* If that didn't work out, there's no new-placement.  */
5587   if (!cp_parser_parse_definitely (parser))
5588     placement = NULL_TREE;
5589
5590   /* If the next token is a `(', then we have a parenthesized
5591      type-id.  */
5592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5593     {
5594       cp_token *token;
5595       /* Consume the `('.  */
5596       cp_lexer_consume_token (parser->lexer);
5597       /* Parse the type-id.  */
5598       type = cp_parser_type_id (parser);
5599       /* Look for the closing `)'.  */
5600       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5601       token = cp_lexer_peek_token (parser->lexer);
5602       /* There should not be a direct-new-declarator in this production,
5603          but GCC used to allowed this, so we check and emit a sensible error
5604          message for this case.  */
5605       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5606         {
5607           error ("%Harray bound forbidden after parenthesized type-id",
5608                  &token->location);
5609           inform (token->location, 
5610                   "try removing the parentheses around the type-id");
5611           cp_parser_direct_new_declarator (parser);
5612         }
5613       nelts = NULL_TREE;
5614     }
5615   /* Otherwise, there must be a new-type-id.  */
5616   else
5617     type = cp_parser_new_type_id (parser, &nelts);
5618
5619   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5621       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5622     initializer = cp_parser_new_initializer (parser);
5623   else
5624     initializer = NULL_TREE;
5625
5626   /* A new-expression may not appear in an integral constant
5627      expression.  */
5628   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5629     return error_mark_node;
5630
5631   /* Create a representation of the new-expression.  */
5632   return build_new (placement, type, nelts, initializer, global_scope_p,
5633                     tf_warning_or_error);
5634 }
5635
5636 /* Parse a new-placement.
5637
5638    new-placement:
5639      ( expression-list )
5640
5641    Returns the same representation as for an expression-list.  */
5642
5643 static tree
5644 cp_parser_new_placement (cp_parser* parser)
5645 {
5646   tree expression_list;
5647
5648   /* Parse the expression-list.  */
5649   expression_list = (cp_parser_parenthesized_expression_list
5650                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5651                       /*non_constant_p=*/NULL));
5652
5653   return expression_list;
5654 }
5655
5656 /* Parse a new-type-id.
5657
5658    new-type-id:
5659      type-specifier-seq new-declarator [opt]
5660
5661    Returns the TYPE allocated.  If the new-type-id indicates an array
5662    type, *NELTS is set to the number of elements in the last array
5663    bound; the TYPE will not include the last array bound.  */
5664
5665 static tree
5666 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5667 {
5668   cp_decl_specifier_seq type_specifier_seq;
5669   cp_declarator *new_declarator;
5670   cp_declarator *declarator;
5671   cp_declarator *outer_declarator;
5672   const char *saved_message;
5673   tree type;
5674
5675   /* The type-specifier sequence must not contain type definitions.
5676      (It cannot contain declarations of new types either, but if they
5677      are not definitions we will catch that because they are not
5678      complete.)  */
5679   saved_message = parser->type_definition_forbidden_message;
5680   parser->type_definition_forbidden_message
5681     = "types may not be defined in a new-type-id";
5682   /* Parse the type-specifier-seq.  */
5683   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5684                                 &type_specifier_seq);
5685   /* Restore the old message.  */
5686   parser->type_definition_forbidden_message = saved_message;
5687   /* Parse the new-declarator.  */
5688   new_declarator = cp_parser_new_declarator_opt (parser);
5689
5690   /* Determine the number of elements in the last array dimension, if
5691      any.  */
5692   *nelts = NULL_TREE;
5693   /* Skip down to the last array dimension.  */
5694   declarator = new_declarator;
5695   outer_declarator = NULL;
5696   while (declarator && (declarator->kind == cdk_pointer
5697                         || declarator->kind == cdk_ptrmem))
5698     {
5699       outer_declarator = declarator;
5700       declarator = declarator->declarator;
5701     }
5702   while (declarator
5703          && declarator->kind == cdk_array
5704          && declarator->declarator
5705          && declarator->declarator->kind == cdk_array)
5706     {
5707       outer_declarator = declarator;
5708       declarator = declarator->declarator;
5709     }
5710
5711   if (declarator && declarator->kind == cdk_array)
5712     {
5713       *nelts = declarator->u.array.bounds;
5714       if (*nelts == error_mark_node)
5715         *nelts = integer_one_node;
5716
5717       if (outer_declarator)
5718         outer_declarator->declarator = declarator->declarator;
5719       else
5720         new_declarator = NULL;
5721     }
5722
5723   type = groktypename (&type_specifier_seq, new_declarator);
5724   return type;
5725 }
5726
5727 /* Parse an (optional) new-declarator.
5728
5729    new-declarator:
5730      ptr-operator new-declarator [opt]
5731      direct-new-declarator
5732
5733    Returns the declarator.  */
5734
5735 static cp_declarator *
5736 cp_parser_new_declarator_opt (cp_parser* parser)
5737 {
5738   enum tree_code code;
5739   tree type;
5740   cp_cv_quals cv_quals;
5741
5742   /* We don't know if there's a ptr-operator next, or not.  */
5743   cp_parser_parse_tentatively (parser);
5744   /* Look for a ptr-operator.  */
5745   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5746   /* If that worked, look for more new-declarators.  */
5747   if (cp_parser_parse_definitely (parser))
5748     {
5749       cp_declarator *declarator;
5750
5751       /* Parse another optional declarator.  */
5752       declarator = cp_parser_new_declarator_opt (parser);
5753
5754       return cp_parser_make_indirect_declarator
5755         (code, type, cv_quals, declarator);
5756     }
5757
5758   /* If the next token is a `[', there is a direct-new-declarator.  */
5759   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5760     return cp_parser_direct_new_declarator (parser);
5761
5762   return NULL;
5763 }
5764
5765 /* Parse a direct-new-declarator.
5766
5767    direct-new-declarator:
5768      [ expression ]
5769      direct-new-declarator [constant-expression]
5770
5771    */
5772
5773 static cp_declarator *
5774 cp_parser_direct_new_declarator (cp_parser* parser)
5775 {
5776   cp_declarator *declarator = NULL;
5777
5778   while (true)
5779     {
5780       tree expression;
5781
5782       /* Look for the opening `['.  */
5783       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5784       /* The first expression is not required to be constant.  */
5785       if (!declarator)
5786         {
5787           cp_token *token = cp_lexer_peek_token (parser->lexer);
5788           expression = cp_parser_expression (parser, /*cast_p=*/false);
5789           /* The standard requires that the expression have integral
5790              type.  DR 74 adds enumeration types.  We believe that the
5791              real intent is that these expressions be handled like the
5792              expression in a `switch' condition, which also allows
5793              classes with a single conversion to integral or
5794              enumeration type.  */
5795           if (!processing_template_decl)
5796             {
5797               expression
5798                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5799                                               expression,
5800                                               /*complain=*/true);
5801               if (!expression)
5802                 {
5803                   error ("%Hexpression in new-declarator must have integral "
5804                          "or enumeration type", &token->location);
5805                   expression = error_mark_node;
5806                 }
5807             }
5808         }
5809       /* But all the other expressions must be.  */
5810       else
5811         expression
5812           = cp_parser_constant_expression (parser,
5813                                            /*allow_non_constant=*/false,
5814                                            NULL);
5815       /* Look for the closing `]'.  */
5816       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5817
5818       /* Add this bound to the declarator.  */
5819       declarator = make_array_declarator (declarator, expression);
5820
5821       /* If the next token is not a `[', then there are no more
5822          bounds.  */
5823       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5824         break;
5825     }
5826
5827   return declarator;
5828 }
5829
5830 /* Parse a new-initializer.
5831
5832    new-initializer:
5833      ( expression-list [opt] )
5834      braced-init-list
5835
5836    Returns a representation of the expression-list.  If there is no
5837    expression-list, VOID_ZERO_NODE is returned.  */
5838
5839 static tree
5840 cp_parser_new_initializer (cp_parser* parser)
5841 {
5842   tree expression_list;
5843
5844   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5845     {
5846       bool expr_non_constant_p;
5847       maybe_warn_cpp0x ("extended initializer lists");
5848       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5849       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5850       expression_list = build_tree_list (NULL_TREE, expression_list);
5851     }
5852   else
5853     expression_list = (cp_parser_parenthesized_expression_list
5854                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5855                         /*non_constant_p=*/NULL));
5856   if (!expression_list)
5857     expression_list = void_zero_node;
5858
5859   return expression_list;
5860 }
5861
5862 /* Parse a delete-expression.
5863
5864    delete-expression:
5865      :: [opt] delete cast-expression
5866      :: [opt] delete [ ] cast-expression
5867
5868    Returns a representation of the expression.  */
5869
5870 static tree
5871 cp_parser_delete_expression (cp_parser* parser)
5872 {
5873   bool global_scope_p;
5874   bool array_p;
5875   tree expression;
5876
5877   /* Look for the optional `::' operator.  */
5878   global_scope_p
5879     = (cp_parser_global_scope_opt (parser,
5880                                    /*current_scope_valid_p=*/false)
5881        != NULL_TREE);
5882   /* Look for the `delete' keyword.  */
5883   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5884   /* See if the array syntax is in use.  */
5885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5886     {
5887       /* Consume the `[' token.  */
5888       cp_lexer_consume_token (parser->lexer);
5889       /* Look for the `]' token.  */
5890       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5891       /* Remember that this is the `[]' construct.  */
5892       array_p = true;
5893     }
5894   else
5895     array_p = false;
5896
5897   /* Parse the cast-expression.  */
5898   expression = cp_parser_simple_cast_expression (parser);
5899
5900   /* A delete-expression may not appear in an integral constant
5901      expression.  */
5902   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5903     return error_mark_node;
5904
5905   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5906 }
5907
5908 /* Parse a cast-expression.
5909
5910    cast-expression:
5911      unary-expression
5912      ( type-id ) cast-expression
5913
5914    ADDRESS_P is true iff the unary-expression is appearing as the
5915    operand of the `&' operator.   CAST_P is true if this expression is
5916    the target of a cast.
5917
5918    Returns a representation of the expression.  */
5919
5920 static tree
5921 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5922 {
5923   /* If it's a `(', then we might be looking at a cast.  */
5924   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5925     {
5926       tree type = NULL_TREE;
5927       tree expr = NULL_TREE;
5928       bool compound_literal_p;
5929       const char *saved_message;
5930
5931       /* There's no way to know yet whether or not this is a cast.
5932          For example, `(int (3))' is a unary-expression, while `(int)
5933          3' is a cast.  So, we resort to parsing tentatively.  */
5934       cp_parser_parse_tentatively (parser);
5935       /* Types may not be defined in a cast.  */
5936       saved_message = parser->type_definition_forbidden_message;
5937       parser->type_definition_forbidden_message
5938         = "types may not be defined in casts";
5939       /* Consume the `('.  */
5940       cp_lexer_consume_token (parser->lexer);
5941       /* A very tricky bit is that `(struct S) { 3 }' is a
5942          compound-literal (which we permit in C++ as an extension).
5943          But, that construct is not a cast-expression -- it is a
5944          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5945          is legal; if the compound-literal were a cast-expression,
5946          you'd need an extra set of parentheses.)  But, if we parse
5947          the type-id, and it happens to be a class-specifier, then we
5948          will commit to the parse at that point, because we cannot
5949          undo the action that is done when creating a new class.  So,
5950          then we cannot back up and do a postfix-expression.
5951
5952          Therefore, we scan ahead to the closing `)', and check to see
5953          if the token after the `)' is a `{'.  If so, we are not
5954          looking at a cast-expression.
5955
5956          Save tokens so that we can put them back.  */
5957       cp_lexer_save_tokens (parser->lexer);
5958       /* Skip tokens until the next token is a closing parenthesis.
5959          If we find the closing `)', and the next token is a `{', then
5960          we are looking at a compound-literal.  */
5961       compound_literal_p
5962         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5963                                                   /*consume_paren=*/true)
5964            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5965       /* Roll back the tokens we skipped.  */
5966       cp_lexer_rollback_tokens (parser->lexer);
5967       /* If we were looking at a compound-literal, simulate an error
5968          so that the call to cp_parser_parse_definitely below will
5969          fail.  */
5970       if (compound_literal_p)
5971         cp_parser_simulate_error (parser);
5972       else
5973         {
5974           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5975           parser->in_type_id_in_expr_p = true;
5976           /* Look for the type-id.  */
5977           type = cp_parser_type_id (parser);
5978           /* Look for the closing `)'.  */
5979           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5980           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5981         }
5982
5983       /* Restore the saved message.  */
5984       parser->type_definition_forbidden_message = saved_message;
5985
5986       /* If ok so far, parse the dependent expression. We cannot be
5987          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5988          ctor of T, but looks like a cast to function returning T
5989          without a dependent expression.  */
5990       if (!cp_parser_error_occurred (parser))
5991         expr = cp_parser_cast_expression (parser,
5992                                           /*address_p=*/false,
5993                                           /*cast_p=*/true);
5994
5995       if (cp_parser_parse_definitely (parser))
5996         {
5997           /* Warn about old-style casts, if so requested.  */
5998           if (warn_old_style_cast
5999               && !in_system_header
6000               && !VOID_TYPE_P (type)
6001               && current_lang_name != lang_name_c)
6002             warning (OPT_Wold_style_cast, "use of old-style cast");
6003
6004           /* Only type conversions to integral or enumeration types
6005              can be used in constant-expressions.  */
6006           if (!cast_valid_in_integral_constant_expression_p (type)
6007               && (cp_parser_non_integral_constant_expression
6008                   (parser,
6009                    "a cast to a type other than an integral or "
6010                    "enumeration type")))
6011             return error_mark_node;
6012
6013           /* Perform the cast.  */
6014           expr = build_c_cast (type, expr);
6015           return expr;
6016         }
6017     }
6018
6019   /* If we get here, then it's not a cast, so it must be a
6020      unary-expression.  */
6021   return cp_parser_unary_expression (parser, address_p, cast_p);
6022 }
6023
6024 /* Parse a binary expression of the general form:
6025
6026    pm-expression:
6027      cast-expression
6028      pm-expression .* cast-expression
6029      pm-expression ->* cast-expression
6030
6031    multiplicative-expression:
6032      pm-expression
6033      multiplicative-expression * pm-expression
6034      multiplicative-expression / pm-expression
6035      multiplicative-expression % pm-expression
6036
6037    additive-expression:
6038      multiplicative-expression
6039      additive-expression + multiplicative-expression
6040      additive-expression - multiplicative-expression
6041
6042    shift-expression:
6043      additive-expression
6044      shift-expression << additive-expression
6045      shift-expression >> additive-expression
6046
6047    relational-expression:
6048      shift-expression
6049      relational-expression < shift-expression
6050      relational-expression > shift-expression
6051      relational-expression <= shift-expression
6052      relational-expression >= shift-expression
6053
6054   GNU Extension:
6055
6056    relational-expression:
6057      relational-expression <? shift-expression
6058      relational-expression >? shift-expression
6059
6060    equality-expression:
6061      relational-expression
6062      equality-expression == relational-expression
6063      equality-expression != relational-expression
6064
6065    and-expression:
6066      equality-expression
6067      and-expression & equality-expression
6068
6069    exclusive-or-expression:
6070      and-expression
6071      exclusive-or-expression ^ and-expression
6072
6073    inclusive-or-expression:
6074      exclusive-or-expression
6075      inclusive-or-expression | exclusive-or-expression
6076
6077    logical-and-expression:
6078      inclusive-or-expression
6079      logical-and-expression && inclusive-or-expression
6080
6081    logical-or-expression:
6082      logical-and-expression
6083      logical-or-expression || logical-and-expression
6084
6085    All these are implemented with a single function like:
6086
6087    binary-expression:
6088      simple-cast-expression
6089      binary-expression <token> binary-expression
6090
6091    CAST_P is true if this expression is the target of a cast.
6092
6093    The binops_by_token map is used to get the tree codes for each <token> type.
6094    binary-expressions are associated according to a precedence table.  */
6095
6096 #define TOKEN_PRECEDENCE(token)                              \
6097 (((token->type == CPP_GREATER                                \
6098    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6099   && !parser->greater_than_is_operator_p)                    \
6100  ? PREC_NOT_OPERATOR                                         \
6101  : binops_by_token[token->type].prec)
6102
6103 static tree
6104 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6105                              enum cp_parser_prec prec)
6106 {
6107   cp_parser_expression_stack stack;
6108   cp_parser_expression_stack_entry *sp = &stack[0];
6109   tree lhs, rhs;
6110   cp_token *token;
6111   enum tree_code tree_type, lhs_type, rhs_type;
6112   enum cp_parser_prec new_prec, lookahead_prec;
6113   bool overloaded_p;
6114
6115   /* Parse the first expression.  */
6116   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6117   lhs_type = ERROR_MARK;
6118
6119   for (;;)
6120     {
6121       /* Get an operator token.  */
6122       token = cp_lexer_peek_token (parser->lexer);
6123
6124       if (warn_cxx0x_compat
6125           && token->type == CPP_RSHIFT
6126           && !parser->greater_than_is_operator_p)
6127         {
6128           warning (OPT_Wc__0x_compat, 
6129                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6130                    &token->location);
6131           warning (OPT_Wc__0x_compat, 
6132                    "suggest parentheses around %<>>%> expression");
6133         }
6134
6135       new_prec = TOKEN_PRECEDENCE (token);
6136
6137       /* Popping an entry off the stack means we completed a subexpression:
6138          - either we found a token which is not an operator (`>' where it is not
6139            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6140            will happen repeatedly;
6141          - or, we found an operator which has lower priority.  This is the case
6142            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6143            parsing `3 * 4'.  */
6144       if (new_prec <= prec)
6145         {
6146           if (sp == stack)
6147             break;
6148           else
6149             goto pop;
6150         }
6151
6152      get_rhs:
6153       tree_type = binops_by_token[token->type].tree_type;
6154
6155       /* We used the operator token.  */
6156       cp_lexer_consume_token (parser->lexer);
6157
6158       /* Extract another operand.  It may be the RHS of this expression
6159          or the LHS of a new, higher priority expression.  */
6160       rhs = cp_parser_simple_cast_expression (parser);
6161       rhs_type = ERROR_MARK;
6162
6163       /* Get another operator token.  Look up its precedence to avoid
6164          building a useless (immediately popped) stack entry for common
6165          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6166       token = cp_lexer_peek_token (parser->lexer);
6167       lookahead_prec = TOKEN_PRECEDENCE (token);
6168       if (lookahead_prec > new_prec)
6169         {
6170           /* ... and prepare to parse the RHS of the new, higher priority
6171              expression.  Since precedence levels on the stack are
6172              monotonically increasing, we do not have to care about
6173              stack overflows.  */
6174           sp->prec = prec;
6175           sp->tree_type = tree_type;
6176           sp->lhs = lhs;
6177           sp->lhs_type = lhs_type;
6178           sp++;
6179           lhs = rhs;
6180           lhs_type = rhs_type;
6181           prec = new_prec;
6182           new_prec = lookahead_prec;
6183           goto get_rhs;
6184
6185          pop:
6186           /* If the stack is not empty, we have parsed into LHS the right side
6187              (`4' in the example above) of an expression we had suspended.
6188              We can use the information on the stack to recover the LHS (`3')
6189              from the stack together with the tree code (`MULT_EXPR'), and
6190              the precedence of the higher level subexpression
6191              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6192              which will be used to actually build the additive expression.  */
6193           --sp;
6194           prec = sp->prec;
6195           tree_type = sp->tree_type;
6196           rhs = lhs;
6197           rhs_type = lhs_type;
6198           lhs = sp->lhs;
6199           lhs_type = sp->lhs_type;
6200         }
6201
6202       overloaded_p = false;
6203       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6204                                &overloaded_p, tf_warning_or_error);
6205       lhs_type = tree_type;
6206
6207       /* If the binary operator required the use of an overloaded operator,
6208          then this expression cannot be an integral constant-expression.
6209          An overloaded operator can be used even if both operands are
6210          otherwise permissible in an integral constant-expression if at
6211          least one of the operands is of enumeration type.  */
6212
6213       if (overloaded_p
6214           && (cp_parser_non_integral_constant_expression
6215               (parser, "calls to overloaded operators")))
6216         return error_mark_node;
6217     }
6218
6219   return lhs;
6220 }
6221
6222
6223 /* Parse the `? expression : assignment-expression' part of a
6224    conditional-expression.  The LOGICAL_OR_EXPR is the
6225    logical-or-expression that started the conditional-expression.
6226    Returns a representation of the entire conditional-expression.
6227
6228    This routine is used by cp_parser_assignment_expression.
6229
6230      ? expression : assignment-expression
6231
6232    GNU Extensions:
6233
6234      ? : assignment-expression */
6235
6236 static tree
6237 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6238 {
6239   tree expr;
6240   tree assignment_expr;
6241
6242   /* Consume the `?' token.  */
6243   cp_lexer_consume_token (parser->lexer);
6244   if (cp_parser_allow_gnu_extensions_p (parser)
6245       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6246     /* Implicit true clause.  */
6247     expr = NULL_TREE;
6248   else
6249     /* Parse the expression.  */
6250     expr = cp_parser_expression (parser, /*cast_p=*/false);
6251
6252   /* The next token should be a `:'.  */
6253   cp_parser_require (parser, CPP_COLON, "%<:%>");
6254   /* Parse the assignment-expression.  */
6255   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6256
6257   /* Build the conditional-expression.  */
6258   return build_x_conditional_expr (logical_or_expr,
6259                                    expr,
6260                                    assignment_expr,
6261                                    tf_warning_or_error);
6262 }
6263
6264 /* Parse an assignment-expression.
6265
6266    assignment-expression:
6267      conditional-expression
6268      logical-or-expression assignment-operator assignment_expression
6269      throw-expression
6270
6271    CAST_P is true if this expression is the target of a cast.
6272
6273    Returns a representation for the expression.  */
6274
6275 static tree
6276 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6277 {
6278   tree expr;
6279
6280   /* If the next token is the `throw' keyword, then we're looking at
6281      a throw-expression.  */
6282   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6283     expr = cp_parser_throw_expression (parser);
6284   /* Otherwise, it must be that we are looking at a
6285      logical-or-expression.  */
6286   else
6287     {
6288       /* Parse the binary expressions (logical-or-expression).  */
6289       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6290       /* If the next token is a `?' then we're actually looking at a
6291          conditional-expression.  */
6292       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6293         return cp_parser_question_colon_clause (parser, expr);
6294       else
6295         {
6296           enum tree_code assignment_operator;
6297
6298           /* If it's an assignment-operator, we're using the second
6299              production.  */
6300           assignment_operator
6301             = cp_parser_assignment_operator_opt (parser);
6302           if (assignment_operator != ERROR_MARK)
6303             {
6304               bool non_constant_p;
6305
6306               /* Parse the right-hand side of the assignment.  */
6307               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6308
6309               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6310                 maybe_warn_cpp0x ("extended initializer lists");
6311
6312               /* An assignment may not appear in a
6313                  constant-expression.  */
6314               if (cp_parser_non_integral_constant_expression (parser,
6315                                                               "an assignment"))
6316                 return error_mark_node;
6317               /* Build the assignment expression.  */
6318               expr = build_x_modify_expr (expr,
6319                                           assignment_operator,
6320                                           rhs,
6321                                           tf_warning_or_error);
6322             }
6323         }
6324     }
6325
6326   return expr;
6327 }
6328
6329 /* Parse an (optional) assignment-operator.
6330
6331    assignment-operator: one of
6332      = *= /= %= += -= >>= <<= &= ^= |=
6333
6334    GNU Extension:
6335
6336    assignment-operator: one of
6337      <?= >?=
6338
6339    If the next token is an assignment operator, the corresponding tree
6340    code is returned, and the token is consumed.  For example, for
6341    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6342    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6343    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6344    operator, ERROR_MARK is returned.  */
6345
6346 static enum tree_code
6347 cp_parser_assignment_operator_opt (cp_parser* parser)
6348 {
6349   enum tree_code op;
6350   cp_token *token;
6351
6352   /* Peek at the next token.  */
6353   token = cp_lexer_peek_token (parser->lexer);
6354
6355   switch (token->type)
6356     {
6357     case CPP_EQ:
6358       op = NOP_EXPR;
6359       break;
6360
6361     case CPP_MULT_EQ:
6362       op = MULT_EXPR;
6363       break;
6364
6365     case CPP_DIV_EQ:
6366       op = TRUNC_DIV_EXPR;
6367       break;
6368
6369     case CPP_MOD_EQ:
6370       op = TRUNC_MOD_EXPR;
6371       break;
6372
6373     case CPP_PLUS_EQ:
6374       op = PLUS_EXPR;
6375       break;
6376
6377     case CPP_MINUS_EQ:
6378       op = MINUS_EXPR;
6379       break;
6380
6381     case CPP_RSHIFT_EQ:
6382       op = RSHIFT_EXPR;
6383       break;
6384
6385     case CPP_LSHIFT_EQ:
6386       op = LSHIFT_EXPR;
6387       break;
6388
6389     case CPP_AND_EQ:
6390       op = BIT_AND_EXPR;
6391       break;
6392
6393     case CPP_XOR_EQ:
6394       op = BIT_XOR_EXPR;
6395       break;
6396
6397     case CPP_OR_EQ:
6398       op = BIT_IOR_EXPR;
6399       break;
6400
6401     default:
6402       /* Nothing else is an assignment operator.  */
6403       op = ERROR_MARK;
6404     }
6405
6406   /* If it was an assignment operator, consume it.  */
6407   if (op != ERROR_MARK)
6408     cp_lexer_consume_token (parser->lexer);
6409
6410   return op;
6411 }
6412
6413 /* Parse an expression.
6414
6415    expression:
6416      assignment-expression
6417      expression , assignment-expression
6418
6419    CAST_P is true if this expression is the target of a cast.
6420
6421    Returns a representation of the expression.  */
6422
6423 static tree
6424 cp_parser_expression (cp_parser* parser, bool cast_p)
6425 {
6426   tree expression = NULL_TREE;
6427
6428   while (true)
6429     {
6430       tree assignment_expression;
6431
6432       /* Parse the next assignment-expression.  */
6433       assignment_expression
6434         = cp_parser_assignment_expression (parser, cast_p);
6435       /* If this is the first assignment-expression, we can just
6436          save it away.  */
6437       if (!expression)
6438         expression = assignment_expression;
6439       else
6440         expression = build_x_compound_expr (expression,
6441                                             assignment_expression,
6442                                             tf_warning_or_error);
6443       /* If the next token is not a comma, then we are done with the
6444          expression.  */
6445       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6446         break;
6447       /* Consume the `,'.  */
6448       cp_lexer_consume_token (parser->lexer);
6449       /* A comma operator cannot appear in a constant-expression.  */
6450       if (cp_parser_non_integral_constant_expression (parser,
6451                                                       "a comma operator"))
6452         expression = error_mark_node;
6453     }
6454
6455   return expression;
6456 }
6457
6458 /* Parse a constant-expression.
6459
6460    constant-expression:
6461      conditional-expression
6462
6463   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6464   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6465   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6466   is false, NON_CONSTANT_P should be NULL.  */
6467
6468 static tree
6469 cp_parser_constant_expression (cp_parser* parser,
6470                                bool allow_non_constant_p,
6471                                bool *non_constant_p)
6472 {
6473   bool saved_integral_constant_expression_p;
6474   bool saved_allow_non_integral_constant_expression_p;
6475   bool saved_non_integral_constant_expression_p;
6476   tree expression;
6477
6478   /* It might seem that we could simply parse the
6479      conditional-expression, and then check to see if it were
6480      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6481      one that the compiler can figure out is constant, possibly after
6482      doing some simplifications or optimizations.  The standard has a
6483      precise definition of constant-expression, and we must honor
6484      that, even though it is somewhat more restrictive.
6485
6486      For example:
6487
6488        int i[(2, 3)];
6489
6490      is not a legal declaration, because `(2, 3)' is not a
6491      constant-expression.  The `,' operator is forbidden in a
6492      constant-expression.  However, GCC's constant-folding machinery
6493      will fold this operation to an INTEGER_CST for `3'.  */
6494
6495   /* Save the old settings.  */
6496   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6497   saved_allow_non_integral_constant_expression_p
6498     = parser->allow_non_integral_constant_expression_p;
6499   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6500   /* We are now parsing a constant-expression.  */
6501   parser->integral_constant_expression_p = true;
6502   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6503   parser->non_integral_constant_expression_p = false;
6504   /* Although the grammar says "conditional-expression", we parse an
6505      "assignment-expression", which also permits "throw-expression"
6506      and the use of assignment operators.  In the case that
6507      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6508      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6509      actually essential that we look for an assignment-expression.
6510      For example, cp_parser_initializer_clauses uses this function to
6511      determine whether a particular assignment-expression is in fact
6512      constant.  */
6513   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6514   /* Restore the old settings.  */
6515   parser->integral_constant_expression_p
6516     = saved_integral_constant_expression_p;
6517   parser->allow_non_integral_constant_expression_p
6518     = saved_allow_non_integral_constant_expression_p;
6519   if (allow_non_constant_p)
6520     *non_constant_p = parser->non_integral_constant_expression_p;
6521   else if (parser->non_integral_constant_expression_p)
6522     expression = error_mark_node;
6523   parser->non_integral_constant_expression_p
6524     = saved_non_integral_constant_expression_p;
6525
6526   return expression;
6527 }
6528
6529 /* Parse __builtin_offsetof.
6530
6531    offsetof-expression:
6532      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6533
6534    offsetof-member-designator:
6535      id-expression
6536      | offsetof-member-designator "." id-expression
6537      | offsetof-member-designator "[" expression "]"  */
6538
6539 static tree
6540 cp_parser_builtin_offsetof (cp_parser *parser)
6541 {
6542   int save_ice_p, save_non_ice_p;
6543   tree type, expr;
6544   cp_id_kind dummy;
6545   cp_token *token;
6546
6547   /* We're about to accept non-integral-constant things, but will
6548      definitely yield an integral constant expression.  Save and
6549      restore these values around our local parsing.  */
6550   save_ice_p = parser->integral_constant_expression_p;
6551   save_non_ice_p = parser->non_integral_constant_expression_p;
6552
6553   /* Consume the "__builtin_offsetof" token.  */
6554   cp_lexer_consume_token (parser->lexer);
6555   /* Consume the opening `('.  */
6556   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6557   /* Parse the type-id.  */
6558   type = cp_parser_type_id (parser);
6559   /* Look for the `,'.  */
6560   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6561   token = cp_lexer_peek_token (parser->lexer);
6562
6563   /* Build the (type *)null that begins the traditional offsetof macro.  */
6564   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6565                             tf_warning_or_error);
6566
6567   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6568   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6569                                                  true, &dummy, token->location);
6570   while (true)
6571     {
6572       token = cp_lexer_peek_token (parser->lexer);
6573       switch (token->type)
6574         {
6575         case CPP_OPEN_SQUARE:
6576           /* offsetof-member-designator "[" expression "]" */
6577           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6578           break;
6579
6580         case CPP_DOT:
6581           /* offsetof-member-designator "." identifier */
6582           cp_lexer_consume_token (parser->lexer);
6583           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6584                                                          true, &dummy,
6585                                                          token->location);
6586           break;
6587
6588         case CPP_CLOSE_PAREN:
6589           /* Consume the ")" token.  */
6590           cp_lexer_consume_token (parser->lexer);
6591           goto success;
6592
6593         default:
6594           /* Error.  We know the following require will fail, but
6595              that gives the proper error message.  */
6596           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6597           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6598           expr = error_mark_node;
6599           goto failure;
6600         }
6601     }
6602
6603  success:
6604   /* If we're processing a template, we can't finish the semantics yet.
6605      Otherwise we can fold the entire expression now.  */
6606   if (processing_template_decl)
6607     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6608   else
6609     expr = finish_offsetof (expr);
6610
6611  failure:
6612   parser->integral_constant_expression_p = save_ice_p;
6613   parser->non_integral_constant_expression_p = save_non_ice_p;
6614
6615   return expr;
6616 }
6617
6618 /* Parse a trait expression.  */
6619
6620 static tree
6621 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6622 {
6623   cp_trait_kind kind;
6624   tree type1, type2 = NULL_TREE;
6625   bool binary = false;
6626   cp_decl_specifier_seq decl_specs;
6627
6628   switch (keyword)
6629     {
6630     case RID_HAS_NOTHROW_ASSIGN:
6631       kind = CPTK_HAS_NOTHROW_ASSIGN;
6632       break;
6633     case RID_HAS_NOTHROW_CONSTRUCTOR:
6634       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6635       break;
6636     case RID_HAS_NOTHROW_COPY:
6637       kind = CPTK_HAS_NOTHROW_COPY;
6638       break;
6639     case RID_HAS_TRIVIAL_ASSIGN:
6640       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6641       break;
6642     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6643       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6644       break;
6645     case RID_HAS_TRIVIAL_COPY:
6646       kind = CPTK_HAS_TRIVIAL_COPY;
6647       break;
6648     case RID_HAS_TRIVIAL_DESTRUCTOR:
6649       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6650       break;
6651     case RID_HAS_VIRTUAL_DESTRUCTOR:
6652       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6653       break;
6654     case RID_IS_ABSTRACT:
6655       kind = CPTK_IS_ABSTRACT;
6656       break;
6657     case RID_IS_BASE_OF:
6658       kind = CPTK_IS_BASE_OF;
6659       binary = true;
6660       break;
6661     case RID_IS_CLASS:
6662       kind = CPTK_IS_CLASS;
6663       break;
6664     case RID_IS_CONVERTIBLE_TO:
6665       kind = CPTK_IS_CONVERTIBLE_TO;
6666       binary = true;
6667       break;
6668     case RID_IS_EMPTY:
6669       kind = CPTK_IS_EMPTY;
6670       break;
6671     case RID_IS_ENUM:
6672       kind = CPTK_IS_ENUM;
6673       break;
6674     case RID_IS_POD:
6675       kind = CPTK_IS_POD;
6676       break;
6677     case RID_IS_POLYMORPHIC:
6678       kind = CPTK_IS_POLYMORPHIC;
6679       break;
6680     case RID_IS_UNION:
6681       kind = CPTK_IS_UNION;
6682       break;
6683     default:
6684       gcc_unreachable ();
6685     }
6686
6687   /* Consume the token.  */
6688   cp_lexer_consume_token (parser->lexer);
6689
6690   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6691
6692   type1 = cp_parser_type_id (parser);
6693
6694   if (type1 == error_mark_node)
6695     return error_mark_node;
6696
6697   /* Build a trivial decl-specifier-seq.  */
6698   clear_decl_specs (&decl_specs);
6699   decl_specs.type = type1;
6700
6701   /* Call grokdeclarator to figure out what type this is.  */
6702   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6703                           /*initialized=*/0, /*attrlist=*/NULL);
6704
6705   if (binary)
6706     {
6707       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6708  
6709       type2 = cp_parser_type_id (parser);
6710
6711       if (type2 == error_mark_node)
6712         return error_mark_node;
6713
6714       /* Build a trivial decl-specifier-seq.  */
6715       clear_decl_specs (&decl_specs);
6716       decl_specs.type = type2;
6717
6718       /* Call grokdeclarator to figure out what type this is.  */
6719       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6720                               /*initialized=*/0, /*attrlist=*/NULL);
6721     }
6722
6723   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6724
6725   /* Complete the trait expression, which may mean either processing
6726      the trait expr now or saving it for template instantiation.  */
6727   return finish_trait_expr (kind, type1, type2);
6728 }
6729
6730 /* Statements [gram.stmt.stmt]  */
6731
6732 /* Parse a statement.
6733
6734    statement:
6735      labeled-statement
6736      expression-statement
6737      compound-statement
6738      selection-statement
6739      iteration-statement
6740      jump-statement
6741      declaration-statement
6742      try-block
6743
6744   IN_COMPOUND is true when the statement is nested inside a
6745   cp_parser_compound_statement; this matters for certain pragmas.
6746
6747   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6748   is a (possibly labeled) if statement which is not enclosed in braces
6749   and has an else clause.  This is used to implement -Wparentheses.  */
6750
6751 static void
6752 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6753                      bool in_compound, bool *if_p)
6754 {
6755   tree statement;
6756   cp_token *token;
6757   location_t statement_location;
6758
6759  restart:
6760   if (if_p != NULL)
6761     *if_p = false;
6762   /* There is no statement yet.  */
6763   statement = NULL_TREE;
6764   /* Peek at the next token.  */
6765   token = cp_lexer_peek_token (parser->lexer);
6766   /* Remember the location of the first token in the statement.  */
6767   statement_location = token->location;
6768   /* If this is a keyword, then that will often determine what kind of
6769      statement we have.  */
6770   if (token->type == CPP_KEYWORD)
6771     {
6772       enum rid keyword = token->keyword;
6773
6774       switch (keyword)
6775         {
6776         case RID_CASE:
6777         case RID_DEFAULT:
6778           /* Looks like a labeled-statement with a case label.
6779              Parse the label, and then use tail recursion to parse
6780              the statement.  */
6781           cp_parser_label_for_labeled_statement (parser);
6782           goto restart;
6783
6784         case RID_IF:
6785         case RID_SWITCH:
6786           statement = cp_parser_selection_statement (parser, if_p);
6787           break;
6788
6789         case RID_WHILE:
6790         case RID_DO:
6791         case RID_FOR:
6792           statement = cp_parser_iteration_statement (parser);
6793           break;
6794
6795         case RID_BREAK:
6796         case RID_CONTINUE:
6797         case RID_RETURN:
6798         case RID_GOTO:
6799           statement = cp_parser_jump_statement (parser);
6800           break;
6801
6802           /* Objective-C++ exception-handling constructs.  */
6803         case RID_AT_TRY:
6804         case RID_AT_CATCH:
6805         case RID_AT_FINALLY:
6806         case RID_AT_SYNCHRONIZED:
6807         case RID_AT_THROW:
6808           statement = cp_parser_objc_statement (parser);
6809           break;
6810
6811         case RID_TRY:
6812           statement = cp_parser_try_block (parser);
6813           break;
6814
6815         case RID_NAMESPACE:
6816           /* This must be a namespace alias definition.  */
6817           cp_parser_declaration_statement (parser);
6818           return;
6819           
6820         default:
6821           /* It might be a keyword like `int' that can start a
6822              declaration-statement.  */
6823           break;
6824         }
6825     }
6826   else if (token->type == CPP_NAME)
6827     {
6828       /* If the next token is a `:', then we are looking at a
6829          labeled-statement.  */
6830       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6831       if (token->type == CPP_COLON)
6832         {
6833           /* Looks like a labeled-statement with an ordinary label.
6834              Parse the label, and then use tail recursion to parse
6835              the statement.  */
6836           cp_parser_label_for_labeled_statement (parser);
6837           goto restart;
6838         }
6839     }
6840   /* Anything that starts with a `{' must be a compound-statement.  */
6841   else if (token->type == CPP_OPEN_BRACE)
6842     statement = cp_parser_compound_statement (parser, NULL, false);
6843   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6844      a statement all its own.  */
6845   else if (token->type == CPP_PRAGMA)
6846     {
6847       /* Only certain OpenMP pragmas are attached to statements, and thus
6848          are considered statements themselves.  All others are not.  In
6849          the context of a compound, accept the pragma as a "statement" and
6850          return so that we can check for a close brace.  Otherwise we
6851          require a real statement and must go back and read one.  */
6852       if (in_compound)
6853         cp_parser_pragma (parser, pragma_compound);
6854       else if (!cp_parser_pragma (parser, pragma_stmt))
6855         goto restart;
6856       return;
6857     }
6858   else if (token->type == CPP_EOF)
6859     {
6860       cp_parser_error (parser, "expected statement");
6861       return;
6862     }
6863
6864   /* Everything else must be a declaration-statement or an
6865      expression-statement.  Try for the declaration-statement
6866      first, unless we are looking at a `;', in which case we know that
6867      we have an expression-statement.  */
6868   if (!statement)
6869     {
6870       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6871         {
6872           cp_parser_parse_tentatively (parser);
6873           /* Try to parse the declaration-statement.  */
6874           cp_parser_declaration_statement (parser);
6875           /* If that worked, we're done.  */
6876           if (cp_parser_parse_definitely (parser))
6877             return;
6878         }
6879       /* Look for an expression-statement instead.  */
6880       statement = cp_parser_expression_statement (parser, in_statement_expr);
6881     }
6882
6883   /* Set the line number for the statement.  */
6884   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6885     SET_EXPR_LOCATION (statement, statement_location);
6886 }
6887
6888 /* Parse the label for a labeled-statement, i.e.
6889
6890    identifier :
6891    case constant-expression :
6892    default :
6893
6894    GNU Extension:
6895    case constant-expression ... constant-expression : statement
6896
6897    When a label is parsed without errors, the label is added to the
6898    parse tree by the finish_* functions, so this function doesn't
6899    have to return the label.  */
6900
6901 static void
6902 cp_parser_label_for_labeled_statement (cp_parser* parser)
6903 {
6904   cp_token *token;
6905
6906   /* The next token should be an identifier.  */
6907   token = cp_lexer_peek_token (parser->lexer);
6908   if (token->type != CPP_NAME
6909       && token->type != CPP_KEYWORD)
6910     {
6911       cp_parser_error (parser, "expected labeled-statement");
6912       return;
6913     }
6914
6915   switch (token->keyword)
6916     {
6917     case RID_CASE:
6918       {
6919         tree expr, expr_hi;
6920         cp_token *ellipsis;
6921
6922         /* Consume the `case' token.  */
6923         cp_lexer_consume_token (parser->lexer);
6924         /* Parse the constant-expression.  */
6925         expr = cp_parser_constant_expression (parser,
6926                                               /*allow_non_constant_p=*/false,
6927                                               NULL);
6928
6929         ellipsis = cp_lexer_peek_token (parser->lexer);
6930         if (ellipsis->type == CPP_ELLIPSIS)
6931           {
6932             /* Consume the `...' token.  */
6933             cp_lexer_consume_token (parser->lexer);
6934             expr_hi =
6935               cp_parser_constant_expression (parser,
6936                                              /*allow_non_constant_p=*/false,
6937                                              NULL);
6938             /* We don't need to emit warnings here, as the common code
6939                will do this for us.  */
6940           }
6941         else
6942           expr_hi = NULL_TREE;
6943
6944         if (parser->in_switch_statement_p)
6945           finish_case_label (expr, expr_hi);
6946         else
6947           error ("%Hcase label %qE not within a switch statement",
6948                  &token->location, expr);
6949       }
6950       break;
6951
6952     case RID_DEFAULT:
6953       /* Consume the `default' token.  */
6954       cp_lexer_consume_token (parser->lexer);
6955
6956       if (parser->in_switch_statement_p)
6957         finish_case_label (NULL_TREE, NULL_TREE);
6958       else
6959         error ("%Hcase label not within a switch statement", &token->location);
6960       break;
6961
6962     default:
6963       /* Anything else must be an ordinary label.  */
6964       finish_label_stmt (cp_parser_identifier (parser));
6965       break;
6966     }
6967
6968   /* Require the `:' token.  */
6969   cp_parser_require (parser, CPP_COLON, "%<:%>");
6970 }
6971
6972 /* Parse an expression-statement.
6973
6974    expression-statement:
6975      expression [opt] ;
6976
6977    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6978    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6979    indicates whether this expression-statement is part of an
6980    expression statement.  */
6981
6982 static tree
6983 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6984 {
6985   tree statement = NULL_TREE;
6986
6987   /* If the next token is a ';', then there is no expression
6988      statement.  */
6989   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6990     statement = cp_parser_expression (parser, /*cast_p=*/false);
6991
6992   /* Consume the final `;'.  */
6993   cp_parser_consume_semicolon_at_end_of_statement (parser);
6994
6995   if (in_statement_expr
6996       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6997     /* This is the final expression statement of a statement
6998        expression.  */
6999     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7000   else if (statement)
7001     statement = finish_expr_stmt (statement);
7002   else
7003     finish_stmt ();
7004
7005   return statement;
7006 }
7007
7008 /* Parse a compound-statement.
7009
7010    compound-statement:
7011      { statement-seq [opt] }
7012
7013    GNU extension:
7014
7015    compound-statement:
7016      { label-declaration-seq [opt] statement-seq [opt] }
7017
7018    label-declaration-seq:
7019      label-declaration
7020      label-declaration-seq label-declaration
7021
7022    Returns a tree representing the statement.  */
7023
7024 static tree
7025 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7026                               bool in_try)
7027 {
7028   tree compound_stmt;
7029
7030   /* Consume the `{'.  */
7031   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7032     return error_mark_node;
7033   /* Begin the compound-statement.  */
7034   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7035   /* If the next keyword is `__label__' we have a label declaration.  */
7036   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7037     cp_parser_label_declaration (parser);
7038   /* Parse an (optional) statement-seq.  */
7039   cp_parser_statement_seq_opt (parser, in_statement_expr);
7040   /* Finish the compound-statement.  */
7041   finish_compound_stmt (compound_stmt);
7042   /* Consume the `}'.  */
7043   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7044
7045   return compound_stmt;
7046 }
7047
7048 /* Parse an (optional) statement-seq.
7049
7050    statement-seq:
7051      statement
7052      statement-seq [opt] statement  */
7053
7054 static void
7055 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7056 {
7057   /* Scan statements until there aren't any more.  */
7058   while (true)
7059     {
7060       cp_token *token = cp_lexer_peek_token (parser->lexer);
7061
7062       /* If we're looking at a `}', then we've run out of statements.  */
7063       if (token->type == CPP_CLOSE_BRACE
7064           || token->type == CPP_EOF
7065           || token->type == CPP_PRAGMA_EOL)
7066         break;
7067       
7068       /* If we are in a compound statement and find 'else' then
7069          something went wrong.  */
7070       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7071         {
7072           if (parser->in_statement & IN_IF_STMT) 
7073             break;
7074           else
7075             {
7076               token = cp_lexer_consume_token (parser->lexer);
7077               error ("%H%<else%> without a previous %<if%>", &token->location);
7078             }
7079         }
7080
7081       /* Parse the statement.  */
7082       cp_parser_statement (parser, in_statement_expr, true, NULL);
7083     }
7084 }
7085
7086 /* Parse a selection-statement.
7087
7088    selection-statement:
7089      if ( condition ) statement
7090      if ( condition ) statement else statement
7091      switch ( condition ) statement
7092
7093    Returns the new IF_STMT or SWITCH_STMT.
7094
7095    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7096    is a (possibly labeled) if statement which is not enclosed in
7097    braces and has an else clause.  This is used to implement
7098    -Wparentheses.  */
7099
7100 static tree
7101 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7102 {
7103   cp_token *token;
7104   enum rid keyword;
7105
7106   if (if_p != NULL)
7107     *if_p = false;
7108
7109   /* Peek at the next token.  */
7110   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7111
7112   /* See what kind of keyword it is.  */
7113   keyword = token->keyword;
7114   switch (keyword)
7115     {
7116     case RID_IF:
7117     case RID_SWITCH:
7118       {
7119         tree statement;
7120         tree condition;
7121
7122         /* Look for the `('.  */
7123         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7124           {
7125             cp_parser_skip_to_end_of_statement (parser);
7126             return error_mark_node;
7127           }
7128
7129         /* Begin the selection-statement.  */
7130         if (keyword == RID_IF)
7131           statement = begin_if_stmt ();
7132         else
7133           statement = begin_switch_stmt ();
7134
7135         /* Parse the condition.  */
7136         condition = cp_parser_condition (parser);
7137         /* Look for the `)'.  */
7138         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7139           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7140                                                  /*consume_paren=*/true);
7141
7142         if (keyword == RID_IF)
7143           {
7144             bool nested_if;
7145             unsigned char in_statement;
7146
7147             /* Add the condition.  */
7148             finish_if_stmt_cond (condition, statement);
7149
7150             /* Parse the then-clause.  */
7151             in_statement = parser->in_statement;
7152             parser->in_statement |= IN_IF_STMT;
7153             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7154             parser->in_statement = in_statement;
7155
7156             finish_then_clause (statement);
7157
7158             /* If the next token is `else', parse the else-clause.  */
7159             if (cp_lexer_next_token_is_keyword (parser->lexer,
7160                                                 RID_ELSE))
7161               {
7162                 /* Consume the `else' keyword.  */
7163                 cp_lexer_consume_token (parser->lexer);
7164                 begin_else_clause (statement);
7165                 /* Parse the else-clause.  */
7166                 cp_parser_implicitly_scoped_statement (parser, NULL);
7167                 finish_else_clause (statement);
7168
7169                 /* If we are currently parsing a then-clause, then
7170                    IF_P will not be NULL.  We set it to true to
7171                    indicate that this if statement has an else clause.
7172                    This may trigger the Wparentheses warning below
7173                    when we get back up to the parent if statement.  */
7174                 if (if_p != NULL)
7175                   *if_p = true;
7176               }
7177             else
7178               {
7179                 /* This if statement does not have an else clause.  If
7180                    NESTED_IF is true, then the then-clause is an if
7181                    statement which does have an else clause.  We warn
7182                    about the potential ambiguity.  */
7183                 if (nested_if)
7184                   warning (OPT_Wparentheses,
7185                            ("%Hsuggest explicit braces "
7186                             "to avoid ambiguous %<else%>"),
7187                            EXPR_LOCUS (statement));
7188               }
7189
7190             /* Now we're all done with the if-statement.  */
7191             finish_if_stmt (statement);
7192           }
7193         else
7194           {
7195             bool in_switch_statement_p;
7196             unsigned char in_statement;
7197
7198             /* Add the condition.  */
7199             finish_switch_cond (condition, statement);
7200
7201             /* Parse the body of the switch-statement.  */
7202             in_switch_statement_p = parser->in_switch_statement_p;
7203             in_statement = parser->in_statement;
7204             parser->in_switch_statement_p = true;
7205             parser->in_statement |= IN_SWITCH_STMT;
7206             cp_parser_implicitly_scoped_statement (parser, NULL);
7207             parser->in_switch_statement_p = in_switch_statement_p;
7208             parser->in_statement = in_statement;
7209
7210             /* Now we're all done with the switch-statement.  */
7211             finish_switch_stmt (statement);
7212           }
7213
7214         return statement;
7215       }
7216       break;
7217
7218     default:
7219       cp_parser_error (parser, "expected selection-statement");
7220       return error_mark_node;
7221     }
7222 }
7223
7224 /* Parse a condition.
7225
7226    condition:
7227      expression
7228      type-specifier-seq declarator = initializer-clause
7229      type-specifier-seq declarator braced-init-list
7230
7231    GNU Extension:
7232
7233    condition:
7234      type-specifier-seq declarator asm-specification [opt]
7235        attributes [opt] = assignment-expression
7236
7237    Returns the expression that should be tested.  */
7238
7239 static tree
7240 cp_parser_condition (cp_parser* parser)
7241 {
7242   cp_decl_specifier_seq type_specifiers;
7243   const char *saved_message;
7244
7245   /* Try the declaration first.  */
7246   cp_parser_parse_tentatively (parser);
7247   /* New types are not allowed in the type-specifier-seq for a
7248      condition.  */
7249   saved_message = parser->type_definition_forbidden_message;
7250   parser->type_definition_forbidden_message
7251     = "types may not be defined in conditions";
7252   /* Parse the type-specifier-seq.  */
7253   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7254                                 &type_specifiers);
7255   /* Restore the saved message.  */
7256   parser->type_definition_forbidden_message = saved_message;
7257   /* If all is well, we might be looking at a declaration.  */
7258   if (!cp_parser_error_occurred (parser))
7259     {
7260       tree decl;
7261       tree asm_specification;
7262       tree attributes;
7263       cp_declarator *declarator;
7264       tree initializer = NULL_TREE;
7265
7266       /* Parse the declarator.  */
7267       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7268                                          /*ctor_dtor_or_conv_p=*/NULL,
7269                                          /*parenthesized_p=*/NULL,
7270                                          /*member_p=*/false);
7271       /* Parse the attributes.  */
7272       attributes = cp_parser_attributes_opt (parser);
7273       /* Parse the asm-specification.  */
7274       asm_specification = cp_parser_asm_specification_opt (parser);
7275       /* If the next token is not an `=' or '{', then we might still be
7276          looking at an expression.  For example:
7277
7278            if (A(a).x)
7279
7280          looks like a decl-specifier-seq and a declarator -- but then
7281          there is no `=', so this is an expression.  */
7282       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7283           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7284         cp_parser_simulate_error (parser);
7285         
7286       /* If we did see an `=' or '{', then we are looking at a declaration
7287          for sure.  */
7288       if (cp_parser_parse_definitely (parser))
7289         {
7290           tree pushed_scope;
7291           bool non_constant_p;
7292           bool flags = LOOKUP_ONLYCONVERTING;
7293
7294           /* Create the declaration.  */
7295           decl = start_decl (declarator, &type_specifiers,
7296                              /*initialized_p=*/true,
7297                              attributes, /*prefix_attributes=*/NULL_TREE,
7298                              &pushed_scope);
7299
7300           /* Parse the initializer.  */
7301           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7302             {
7303               initializer = cp_parser_braced_list (parser, &non_constant_p);
7304               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7305               flags = 0;
7306             }
7307           else
7308             {
7309               /* Consume the `='.  */
7310               cp_lexer_consume_token (parser->lexer);
7311               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7312             }
7313           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7314             maybe_warn_cpp0x ("extended initializer lists");
7315
7316           if (!non_constant_p)
7317             initializer = fold_non_dependent_expr (initializer);
7318
7319           /* Process the initializer.  */
7320           cp_finish_decl (decl,
7321                           initializer, !non_constant_p,
7322                           asm_specification,
7323                           flags);
7324
7325           if (pushed_scope)
7326             pop_scope (pushed_scope);
7327
7328           return convert_from_reference (decl);
7329         }
7330     }
7331   /* If we didn't even get past the declarator successfully, we are
7332      definitely not looking at a declaration.  */
7333   else
7334     cp_parser_abort_tentative_parse (parser);
7335
7336   /* Otherwise, we are looking at an expression.  */
7337   return cp_parser_expression (parser, /*cast_p=*/false);
7338 }
7339
7340 /* We check for a ) immediately followed by ; with no whitespacing
7341    between.  This is used to issue a warning for:
7342
7343      while (...);
7344
7345    and:
7346
7347      for (...);
7348
7349    as the semicolon is probably extraneous.
7350
7351    On parse errors, the next token might not be a ), so do nothing in
7352    that case. */
7353
7354 static void
7355 check_empty_body (cp_parser* parser, const char* type)
7356 {
7357   cp_token *token;
7358   cp_token *close_paren;
7359   expanded_location close_loc;
7360   expanded_location semi_loc;
7361   
7362   close_paren = cp_lexer_peek_token (parser->lexer);
7363   if (close_paren->type != CPP_CLOSE_PAREN)
7364     return;
7365
7366   close_loc = expand_location (close_paren->location);
7367   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7368
7369   if (token->type != CPP_SEMICOLON
7370       || (token->flags & PREV_WHITE))
7371     return;
7372
7373   semi_loc =  expand_location (token->location);
7374   if (close_loc.line == semi_loc.line
7375       && close_loc.column+1 == semi_loc.column)
7376     warning (OPT_Wempty_body,
7377              "suggest a space before %<;%> or explicit braces around empty "
7378              "body in %<%s%> statement",
7379              type);
7380 }
7381
7382 /* Parse an iteration-statement.
7383
7384    iteration-statement:
7385      while ( condition ) statement
7386      do statement while ( expression ) ;
7387      for ( for-init-statement condition [opt] ; expression [opt] )
7388        statement
7389
7390    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7391
7392 static tree
7393 cp_parser_iteration_statement (cp_parser* parser)
7394 {
7395   cp_token *token;
7396   enum rid keyword;
7397   tree statement;
7398   unsigned char in_statement;
7399
7400   /* Peek at the next token.  */
7401   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7402   if (!token)
7403     return error_mark_node;
7404
7405   /* Remember whether or not we are already within an iteration
7406      statement.  */
7407   in_statement = parser->in_statement;
7408
7409   /* See what kind of keyword it is.  */
7410   keyword = token->keyword;
7411   switch (keyword)
7412     {
7413     case RID_WHILE:
7414       {
7415         tree condition;
7416
7417         /* Begin the while-statement.  */
7418         statement = begin_while_stmt ();
7419         /* Look for the `('.  */
7420         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7421         /* Parse the condition.  */
7422         condition = cp_parser_condition (parser);
7423         finish_while_stmt_cond (condition, statement);
7424         check_empty_body (parser, "while");
7425         /* Look for the `)'.  */
7426         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7427         /* Parse the dependent statement.  */
7428         parser->in_statement = IN_ITERATION_STMT;
7429         cp_parser_already_scoped_statement (parser);
7430         parser->in_statement = in_statement;
7431         /* We're done with the while-statement.  */
7432         finish_while_stmt (statement);
7433       }
7434       break;
7435
7436     case RID_DO:
7437       {
7438         tree expression;
7439
7440         /* Begin the do-statement.  */
7441         statement = begin_do_stmt ();
7442         /* Parse the body of the do-statement.  */
7443         parser->in_statement = IN_ITERATION_STMT;
7444         cp_parser_implicitly_scoped_statement (parser, NULL);
7445         parser->in_statement = in_statement;
7446         finish_do_body (statement);
7447         /* Look for the `while' keyword.  */
7448         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7449         /* Look for the `('.  */
7450         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7451         /* Parse the expression.  */
7452         expression = cp_parser_expression (parser, /*cast_p=*/false);
7453         /* We're done with the do-statement.  */
7454         finish_do_stmt (expression, statement);
7455         /* Look for the `)'.  */
7456         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7457         /* Look for the `;'.  */
7458         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7459       }
7460       break;
7461
7462     case RID_FOR:
7463       {
7464         tree condition = NULL_TREE;
7465         tree expression = NULL_TREE;
7466
7467         /* Begin the for-statement.  */
7468         statement = begin_for_stmt ();
7469         /* Look for the `('.  */
7470         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7471         /* Parse the initialization.  */
7472         cp_parser_for_init_statement (parser);
7473         finish_for_init_stmt (statement);
7474
7475         /* If there's a condition, process it.  */
7476         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7477           condition = cp_parser_condition (parser);
7478         finish_for_cond (condition, statement);
7479         /* Look for the `;'.  */
7480         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7481
7482         /* If there's an expression, process it.  */
7483         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7484           expression = cp_parser_expression (parser, /*cast_p=*/false);
7485         finish_for_expr (expression, statement);
7486         check_empty_body (parser, "for");
7487         /* Look for the `)'.  */
7488         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7489
7490         /* Parse the body of the for-statement.  */
7491         parser->in_statement = IN_ITERATION_STMT;
7492         cp_parser_already_scoped_statement (parser);
7493         parser->in_statement = in_statement;
7494
7495         /* We're done with the for-statement.  */
7496         finish_for_stmt (statement);
7497       }
7498       break;
7499
7500     default:
7501       cp_parser_error (parser, "expected iteration-statement");
7502       statement = error_mark_node;
7503       break;
7504     }
7505
7506   return statement;
7507 }
7508
7509 /* Parse a for-init-statement.
7510
7511    for-init-statement:
7512      expression-statement
7513      simple-declaration  */
7514
7515 static void
7516 cp_parser_for_init_statement (cp_parser* parser)
7517 {
7518   /* If the next token is a `;', then we have an empty
7519      expression-statement.  Grammatically, this is also a
7520      simple-declaration, but an invalid one, because it does not
7521      declare anything.  Therefore, if we did not handle this case
7522      specially, we would issue an error message about an invalid
7523      declaration.  */
7524   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7525     {
7526       /* We're going to speculatively look for a declaration, falling back
7527          to an expression, if necessary.  */
7528       cp_parser_parse_tentatively (parser);
7529       /* Parse the declaration.  */
7530       cp_parser_simple_declaration (parser,
7531                                     /*function_definition_allowed_p=*/false);
7532       /* If the tentative parse failed, then we shall need to look for an
7533          expression-statement.  */
7534       if (cp_parser_parse_definitely (parser))
7535         return;
7536     }
7537
7538   cp_parser_expression_statement (parser, false);
7539 }
7540
7541 /* Parse a jump-statement.
7542
7543    jump-statement:
7544      break ;
7545      continue ;
7546      return expression [opt] ;
7547      return braced-init-list ;
7548      goto identifier ;
7549
7550    GNU extension:
7551
7552    jump-statement:
7553      goto * expression ;
7554
7555    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7556
7557 static tree
7558 cp_parser_jump_statement (cp_parser* parser)
7559 {
7560   tree statement = error_mark_node;
7561   cp_token *token;
7562   enum rid keyword;
7563   unsigned char in_statement;
7564
7565   /* Peek at the next token.  */
7566   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7567   if (!token)
7568     return error_mark_node;
7569
7570   /* See what kind of keyword it is.  */
7571   keyword = token->keyword;
7572   switch (keyword)
7573     {
7574     case RID_BREAK:
7575       in_statement = parser->in_statement & ~IN_IF_STMT;      
7576       switch (in_statement)
7577         {
7578         case 0:
7579           error ("%Hbreak statement not within loop or switch", &token->location);
7580           break;
7581         default:
7582           gcc_assert ((in_statement & IN_SWITCH_STMT)
7583                       || in_statement == IN_ITERATION_STMT);
7584           statement = finish_break_stmt ();
7585           break;
7586         case IN_OMP_BLOCK:
7587           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7588           break;
7589         case IN_OMP_FOR:
7590           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7591           break;
7592         }
7593       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7594       break;
7595
7596     case RID_CONTINUE:
7597       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7598         {
7599         case 0:
7600           error ("%Hcontinue statement not within a loop", &token->location);
7601           break;
7602         case IN_ITERATION_STMT:
7603         case IN_OMP_FOR:
7604           statement = finish_continue_stmt ();
7605           break;
7606         case IN_OMP_BLOCK:
7607           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7608           break;
7609         default:
7610           gcc_unreachable ();
7611         }
7612       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7613       break;
7614
7615     case RID_RETURN:
7616       {
7617         tree expr;
7618         bool expr_non_constant_p;
7619
7620         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7621           {
7622             maybe_warn_cpp0x ("extended initializer lists");
7623             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7624           }
7625         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7626           expr = cp_parser_expression (parser, /*cast_p=*/false);
7627         else
7628           /* If the next token is a `;', then there is no
7629              expression.  */
7630           expr = NULL_TREE;
7631         /* Build the return-statement.  */
7632         statement = finish_return_stmt (expr);
7633         /* Look for the final `;'.  */
7634         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7635       }
7636       break;
7637
7638     case RID_GOTO:
7639       /* Create the goto-statement.  */
7640       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7641         {
7642           /* Issue a warning about this use of a GNU extension.  */
7643           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7644           /* Consume the '*' token.  */
7645           cp_lexer_consume_token (parser->lexer);
7646           /* Parse the dependent expression.  */
7647           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7648         }
7649       else
7650         finish_goto_stmt (cp_parser_identifier (parser));
7651       /* Look for the final `;'.  */
7652       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7653       break;
7654
7655     default:
7656       cp_parser_error (parser, "expected jump-statement");
7657       break;
7658     }
7659
7660   return statement;
7661 }
7662
7663 /* Parse a declaration-statement.
7664
7665    declaration-statement:
7666      block-declaration  */
7667
7668 static void
7669 cp_parser_declaration_statement (cp_parser* parser)
7670 {
7671   void *p;
7672
7673   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7674   p = obstack_alloc (&declarator_obstack, 0);
7675
7676  /* Parse the block-declaration.  */
7677   cp_parser_block_declaration (parser, /*statement_p=*/true);
7678
7679   /* Free any declarators allocated.  */
7680   obstack_free (&declarator_obstack, p);
7681
7682   /* Finish off the statement.  */
7683   finish_stmt ();
7684 }
7685
7686 /* Some dependent statements (like `if (cond) statement'), are
7687    implicitly in their own scope.  In other words, if the statement is
7688    a single statement (as opposed to a compound-statement), it is
7689    none-the-less treated as if it were enclosed in braces.  Any
7690    declarations appearing in the dependent statement are out of scope
7691    after control passes that point.  This function parses a statement,
7692    but ensures that is in its own scope, even if it is not a
7693    compound-statement.
7694
7695    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7696    is a (possibly labeled) if statement which is not enclosed in
7697    braces and has an else clause.  This is used to implement
7698    -Wparentheses.
7699
7700    Returns the new statement.  */
7701
7702 static tree
7703 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7704 {
7705   tree statement;
7706
7707   if (if_p != NULL)
7708     *if_p = false;
7709
7710   /* Mark if () ; with a special NOP_EXPR.  */
7711   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7712     {
7713       cp_lexer_consume_token (parser->lexer);
7714       statement = add_stmt (build_empty_stmt ());
7715     }
7716   /* if a compound is opened, we simply parse the statement directly.  */
7717   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7718     statement = cp_parser_compound_statement (parser, NULL, false);
7719   /* If the token is not a `{', then we must take special action.  */
7720   else
7721     {
7722       /* Create a compound-statement.  */
7723       statement = begin_compound_stmt (0);
7724       /* Parse the dependent-statement.  */
7725       cp_parser_statement (parser, NULL_TREE, false, if_p);
7726       /* Finish the dummy compound-statement.  */
7727       finish_compound_stmt (statement);
7728     }
7729
7730   /* Return the statement.  */
7731   return statement;
7732 }
7733
7734 /* For some dependent statements (like `while (cond) statement'), we
7735    have already created a scope.  Therefore, even if the dependent
7736    statement is a compound-statement, we do not want to create another
7737    scope.  */
7738
7739 static void
7740 cp_parser_already_scoped_statement (cp_parser* parser)
7741 {
7742   /* If the token is a `{', then we must take special action.  */
7743   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7744     cp_parser_statement (parser, NULL_TREE, false, NULL);
7745   else
7746     {
7747       /* Avoid calling cp_parser_compound_statement, so that we
7748          don't create a new scope.  Do everything else by hand.  */
7749       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7750       cp_parser_statement_seq_opt (parser, NULL_TREE);
7751       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7752     }
7753 }
7754
7755 /* Declarations [gram.dcl.dcl] */
7756
7757 /* Parse an optional declaration-sequence.
7758
7759    declaration-seq:
7760      declaration
7761      declaration-seq declaration  */
7762
7763 static void
7764 cp_parser_declaration_seq_opt (cp_parser* parser)
7765 {
7766   while (true)
7767     {
7768       cp_token *token;
7769
7770       token = cp_lexer_peek_token (parser->lexer);
7771
7772       if (token->type == CPP_CLOSE_BRACE
7773           || token->type == CPP_EOF
7774           || token->type == CPP_PRAGMA_EOL)
7775         break;
7776
7777       if (token->type == CPP_SEMICOLON)
7778         {
7779           /* A declaration consisting of a single semicolon is
7780              invalid.  Allow it unless we're being pedantic.  */
7781           cp_lexer_consume_token (parser->lexer);
7782           if (!in_system_header)
7783             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7784           continue;
7785         }
7786
7787       /* If we're entering or exiting a region that's implicitly
7788          extern "C", modify the lang context appropriately.  */
7789       if (!parser->implicit_extern_c && token->implicit_extern_c)
7790         {
7791           push_lang_context (lang_name_c);
7792           parser->implicit_extern_c = true;
7793         }
7794       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7795         {
7796           pop_lang_context ();
7797           parser->implicit_extern_c = false;
7798         }
7799
7800       if (token->type == CPP_PRAGMA)
7801         {
7802           /* A top-level declaration can consist solely of a #pragma.
7803              A nested declaration cannot, so this is done here and not
7804              in cp_parser_declaration.  (A #pragma at block scope is
7805              handled in cp_parser_statement.)  */
7806           cp_parser_pragma (parser, pragma_external);
7807           continue;
7808         }
7809
7810       /* Parse the declaration itself.  */
7811       cp_parser_declaration (parser);
7812     }
7813 }
7814
7815 /* Parse a declaration.
7816
7817    declaration:
7818      block-declaration
7819      function-definition
7820      template-declaration
7821      explicit-instantiation
7822      explicit-specialization
7823      linkage-specification
7824      namespace-definition
7825
7826    GNU extension:
7827
7828    declaration:
7829       __extension__ declaration */
7830
7831 static void
7832 cp_parser_declaration (cp_parser* parser)
7833 {
7834   cp_token token1;
7835   cp_token token2;
7836   int saved_pedantic;
7837   void *p;
7838
7839   /* Check for the `__extension__' keyword.  */
7840   if (cp_parser_extension_opt (parser, &saved_pedantic))
7841     {
7842       /* Parse the qualified declaration.  */
7843       cp_parser_declaration (parser);
7844       /* Restore the PEDANTIC flag.  */
7845       pedantic = saved_pedantic;
7846
7847       return;
7848     }
7849
7850   /* Try to figure out what kind of declaration is present.  */
7851   token1 = *cp_lexer_peek_token (parser->lexer);
7852
7853   if (token1.type != CPP_EOF)
7854     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7855   else
7856     {
7857       token2.type = CPP_EOF;
7858       token2.keyword = RID_MAX;
7859     }
7860
7861   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7862   p = obstack_alloc (&declarator_obstack, 0);
7863
7864   /* If the next token is `extern' and the following token is a string
7865      literal, then we have a linkage specification.  */
7866   if (token1.keyword == RID_EXTERN
7867       && cp_parser_is_string_literal (&token2))
7868     cp_parser_linkage_specification (parser);
7869   /* If the next token is `template', then we have either a template
7870      declaration, an explicit instantiation, or an explicit
7871      specialization.  */
7872   else if (token1.keyword == RID_TEMPLATE)
7873     {
7874       /* `template <>' indicates a template specialization.  */
7875       if (token2.type == CPP_LESS
7876           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7877         cp_parser_explicit_specialization (parser);
7878       /* `template <' indicates a template declaration.  */
7879       else if (token2.type == CPP_LESS)
7880         cp_parser_template_declaration (parser, /*member_p=*/false);
7881       /* Anything else must be an explicit instantiation.  */
7882       else
7883         cp_parser_explicit_instantiation (parser);
7884     }
7885   /* If the next token is `export', then we have a template
7886      declaration.  */
7887   else if (token1.keyword == RID_EXPORT)
7888     cp_parser_template_declaration (parser, /*member_p=*/false);
7889   /* If the next token is `extern', 'static' or 'inline' and the one
7890      after that is `template', we have a GNU extended explicit
7891      instantiation directive.  */
7892   else if (cp_parser_allow_gnu_extensions_p (parser)
7893            && (token1.keyword == RID_EXTERN
7894                || token1.keyword == RID_STATIC
7895                || token1.keyword == RID_INLINE)
7896            && token2.keyword == RID_TEMPLATE)
7897     cp_parser_explicit_instantiation (parser);
7898   /* If the next token is `namespace', check for a named or unnamed
7899      namespace definition.  */
7900   else if (token1.keyword == RID_NAMESPACE
7901            && (/* A named namespace definition.  */
7902                (token2.type == CPP_NAME
7903                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7904                     != CPP_EQ))
7905                /* An unnamed namespace definition.  */
7906                || token2.type == CPP_OPEN_BRACE
7907                || token2.keyword == RID_ATTRIBUTE))
7908     cp_parser_namespace_definition (parser);
7909   /* An inline (associated) namespace definition.  */
7910   else if (token1.keyword == RID_INLINE
7911            && token2.keyword == RID_NAMESPACE)
7912     cp_parser_namespace_definition (parser);
7913   /* Objective-C++ declaration/definition.  */
7914   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7915     cp_parser_objc_declaration (parser);
7916   /* We must have either a block declaration or a function
7917      definition.  */
7918   else
7919     /* Try to parse a block-declaration, or a function-definition.  */
7920     cp_parser_block_declaration (parser, /*statement_p=*/false);
7921
7922   /* Free any declarators allocated.  */
7923   obstack_free (&declarator_obstack, p);
7924 }
7925
7926 /* Parse a block-declaration.
7927
7928    block-declaration:
7929      simple-declaration
7930      asm-definition
7931      namespace-alias-definition
7932      using-declaration
7933      using-directive
7934
7935    GNU Extension:
7936
7937    block-declaration:
7938      __extension__ block-declaration
7939
7940    C++0x Extension:
7941
7942    block-declaration:
7943      static_assert-declaration
7944
7945    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7946    part of a declaration-statement.  */
7947
7948 static void
7949 cp_parser_block_declaration (cp_parser *parser,
7950                              bool      statement_p)
7951 {
7952   cp_token *token1;
7953   int saved_pedantic;
7954
7955   /* Check for the `__extension__' keyword.  */
7956   if (cp_parser_extension_opt (parser, &saved_pedantic))
7957     {
7958       /* Parse the qualified declaration.  */
7959       cp_parser_block_declaration (parser, statement_p);
7960       /* Restore the PEDANTIC flag.  */
7961       pedantic = saved_pedantic;
7962
7963       return;
7964     }
7965
7966   /* Peek at the next token to figure out which kind of declaration is
7967      present.  */
7968   token1 = cp_lexer_peek_token (parser->lexer);
7969
7970   /* If the next keyword is `asm', we have an asm-definition.  */
7971   if (token1->keyword == RID_ASM)
7972     {
7973       if (statement_p)
7974         cp_parser_commit_to_tentative_parse (parser);
7975       cp_parser_asm_definition (parser);
7976     }
7977   /* If the next keyword is `namespace', we have a
7978      namespace-alias-definition.  */
7979   else if (token1->keyword == RID_NAMESPACE)
7980     cp_parser_namespace_alias_definition (parser);
7981   /* If the next keyword is `using', we have either a
7982      using-declaration or a using-directive.  */
7983   else if (token1->keyword == RID_USING)
7984     {
7985       cp_token *token2;
7986
7987       if (statement_p)
7988         cp_parser_commit_to_tentative_parse (parser);
7989       /* If the token after `using' is `namespace', then we have a
7990          using-directive.  */
7991       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7992       if (token2->keyword == RID_NAMESPACE)
7993         cp_parser_using_directive (parser);
7994       /* Otherwise, it's a using-declaration.  */
7995       else
7996         cp_parser_using_declaration (parser,
7997                                      /*access_declaration_p=*/false);
7998     }
7999   /* If the next keyword is `__label__' we have a misplaced label
8000      declaration.  */
8001   else if (token1->keyword == RID_LABEL)
8002     {
8003       cp_lexer_consume_token (parser->lexer);
8004       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8005       cp_parser_skip_to_end_of_statement (parser);
8006       /* If the next token is now a `;', consume it.  */
8007       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8008         cp_lexer_consume_token (parser->lexer);
8009     }
8010   /* If the next token is `static_assert' we have a static assertion.  */
8011   else if (token1->keyword == RID_STATIC_ASSERT)
8012     cp_parser_static_assert (parser, /*member_p=*/false);
8013   /* Anything else must be a simple-declaration.  */
8014   else
8015     cp_parser_simple_declaration (parser, !statement_p);
8016 }
8017
8018 /* Parse a simple-declaration.
8019
8020    simple-declaration:
8021      decl-specifier-seq [opt] init-declarator-list [opt] ;
8022
8023    init-declarator-list:
8024      init-declarator
8025      init-declarator-list , init-declarator
8026
8027    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8028    function-definition as a simple-declaration.  */
8029
8030 static void
8031 cp_parser_simple_declaration (cp_parser* parser,
8032                               bool function_definition_allowed_p)
8033 {
8034   cp_decl_specifier_seq decl_specifiers;
8035   int declares_class_or_enum;
8036   bool saw_declarator;
8037
8038   /* Defer access checks until we know what is being declared; the
8039      checks for names appearing in the decl-specifier-seq should be
8040      done as if we were in the scope of the thing being declared.  */
8041   push_deferring_access_checks (dk_deferred);
8042
8043   /* Parse the decl-specifier-seq.  We have to keep track of whether
8044      or not the decl-specifier-seq declares a named class or
8045      enumeration type, since that is the only case in which the
8046      init-declarator-list is allowed to be empty.
8047
8048      [dcl.dcl]
8049
8050      In a simple-declaration, the optional init-declarator-list can be
8051      omitted only when declaring a class or enumeration, that is when
8052      the decl-specifier-seq contains either a class-specifier, an
8053      elaborated-type-specifier, or an enum-specifier.  */
8054   cp_parser_decl_specifier_seq (parser,
8055                                 CP_PARSER_FLAGS_OPTIONAL,
8056                                 &decl_specifiers,
8057                                 &declares_class_or_enum);
8058   /* We no longer need to defer access checks.  */
8059   stop_deferring_access_checks ();
8060
8061   /* In a block scope, a valid declaration must always have a
8062      decl-specifier-seq.  By not trying to parse declarators, we can
8063      resolve the declaration/expression ambiguity more quickly.  */
8064   if (!function_definition_allowed_p
8065       && !decl_specifiers.any_specifiers_p)
8066     {
8067       cp_parser_error (parser, "expected declaration");
8068       goto done;
8069     }
8070
8071   /* If the next two tokens are both identifiers, the code is
8072      erroneous. The usual cause of this situation is code like:
8073
8074        T t;
8075
8076      where "T" should name a type -- but does not.  */
8077   if (!decl_specifiers.type
8078       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8079     {
8080       /* If parsing tentatively, we should commit; we really are
8081          looking at a declaration.  */
8082       cp_parser_commit_to_tentative_parse (parser);
8083       /* Give up.  */
8084       goto done;
8085     }
8086
8087   /* If we have seen at least one decl-specifier, and the next token
8088      is not a parenthesis, then we must be looking at a declaration.
8089      (After "int (" we might be looking at a functional cast.)  */
8090   if (decl_specifiers.any_specifiers_p
8091       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8092       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8093     cp_parser_commit_to_tentative_parse (parser);
8094
8095   /* Keep going until we hit the `;' at the end of the simple
8096      declaration.  */
8097   saw_declarator = false;
8098   while (cp_lexer_next_token_is_not (parser->lexer,
8099                                      CPP_SEMICOLON))
8100     {
8101       cp_token *token;
8102       bool function_definition_p;
8103       tree decl;
8104
8105       if (saw_declarator)
8106         {
8107           /* If we are processing next declarator, coma is expected */
8108           token = cp_lexer_peek_token (parser->lexer);
8109           gcc_assert (token->type == CPP_COMMA);
8110           cp_lexer_consume_token (parser->lexer);
8111         }
8112       else
8113         saw_declarator = true;
8114
8115       /* Parse the init-declarator.  */
8116       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8117                                         /*checks=*/NULL,
8118                                         function_definition_allowed_p,
8119                                         /*member_p=*/false,
8120                                         declares_class_or_enum,
8121                                         &function_definition_p);
8122       /* If an error occurred while parsing tentatively, exit quickly.
8123          (That usually happens when in the body of a function; each
8124          statement is treated as a declaration-statement until proven
8125          otherwise.)  */
8126       if (cp_parser_error_occurred (parser))
8127         goto done;
8128       /* Handle function definitions specially.  */
8129       if (function_definition_p)
8130         {
8131           /* If the next token is a `,', then we are probably
8132              processing something like:
8133
8134                void f() {}, *p;
8135
8136              which is erroneous.  */
8137           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8138             {
8139               cp_token *token = cp_lexer_peek_token (parser->lexer);
8140               error ("%Hmixing declarations and function-definitions is forbidden",
8141                      &token->location);
8142             }
8143           /* Otherwise, we're done with the list of declarators.  */
8144           else
8145             {
8146               pop_deferring_access_checks ();
8147               return;
8148             }
8149         }
8150       /* The next token should be either a `,' or a `;'.  */
8151       token = cp_lexer_peek_token (parser->lexer);
8152       /* If it's a `,', there are more declarators to come.  */
8153       if (token->type == CPP_COMMA)
8154         /* will be consumed next time around */;
8155       /* If it's a `;', we are done.  */
8156       else if (token->type == CPP_SEMICOLON)
8157         break;
8158       /* Anything else is an error.  */
8159       else
8160         {
8161           /* If we have already issued an error message we don't need
8162              to issue another one.  */
8163           if (decl != error_mark_node
8164               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8165             cp_parser_error (parser, "expected %<,%> or %<;%>");
8166           /* Skip tokens until we reach the end of the statement.  */
8167           cp_parser_skip_to_end_of_statement (parser);
8168           /* If the next token is now a `;', consume it.  */
8169           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8170             cp_lexer_consume_token (parser->lexer);
8171           goto done;
8172         }
8173       /* After the first time around, a function-definition is not
8174          allowed -- even if it was OK at first.  For example:
8175
8176            int i, f() {}
8177
8178          is not valid.  */
8179       function_definition_allowed_p = false;
8180     }
8181
8182   /* Issue an error message if no declarators are present, and the
8183      decl-specifier-seq does not itself declare a class or
8184      enumeration.  */
8185   if (!saw_declarator)
8186     {
8187       if (cp_parser_declares_only_class_p (parser))
8188         shadow_tag (&decl_specifiers);
8189       /* Perform any deferred access checks.  */
8190       perform_deferred_access_checks ();
8191     }
8192
8193   /* Consume the `;'.  */
8194   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8195
8196  done:
8197   pop_deferring_access_checks ();
8198 }
8199
8200 /* Parse a decl-specifier-seq.
8201
8202    decl-specifier-seq:
8203      decl-specifier-seq [opt] decl-specifier
8204
8205    decl-specifier:
8206      storage-class-specifier
8207      type-specifier
8208      function-specifier
8209      friend
8210      typedef
8211
8212    GNU Extension:
8213
8214    decl-specifier:
8215      attributes
8216
8217    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8218
8219    The parser flags FLAGS is used to control type-specifier parsing.
8220
8221    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8222    flags:
8223
8224      1: one of the decl-specifiers is an elaborated-type-specifier
8225         (i.e., a type declaration)
8226      2: one of the decl-specifiers is an enum-specifier or a
8227         class-specifier (i.e., a type definition)
8228
8229    */
8230
8231 static void
8232 cp_parser_decl_specifier_seq (cp_parser* parser,
8233                               cp_parser_flags flags,
8234                               cp_decl_specifier_seq *decl_specs,
8235                               int* declares_class_or_enum)
8236 {
8237   bool constructor_possible_p = !parser->in_declarator_p;
8238   cp_token *start_token = NULL;
8239
8240   /* Clear DECL_SPECS.  */
8241   clear_decl_specs (decl_specs);
8242
8243   /* Assume no class or enumeration type is declared.  */
8244   *declares_class_or_enum = 0;
8245
8246   /* Keep reading specifiers until there are no more to read.  */
8247   while (true)
8248     {
8249       bool constructor_p;
8250       bool found_decl_spec;
8251       cp_token *token;
8252
8253       /* Peek at the next token.  */
8254       token = cp_lexer_peek_token (parser->lexer);
8255
8256       /* Save the first token of the decl spec list for error
8257          reporting.  */
8258       if (!start_token)
8259         start_token = token;
8260       /* Handle attributes.  */
8261       if (token->keyword == RID_ATTRIBUTE)
8262         {
8263           /* Parse the attributes.  */
8264           decl_specs->attributes
8265             = chainon (decl_specs->attributes,
8266                        cp_parser_attributes_opt (parser));
8267           continue;
8268         }
8269       /* Assume we will find a decl-specifier keyword.  */
8270       found_decl_spec = true;
8271       /* If the next token is an appropriate keyword, we can simply
8272          add it to the list.  */
8273       switch (token->keyword)
8274         {
8275           /* decl-specifier:
8276                friend  */
8277         case RID_FRIEND:
8278           if (!at_class_scope_p ())
8279             {
8280               error ("%H%<friend%> used outside of class", &token->location);
8281               cp_lexer_purge_token (parser->lexer);
8282             }
8283           else
8284             {
8285               ++decl_specs->specs[(int) ds_friend];
8286               /* Consume the token.  */
8287               cp_lexer_consume_token (parser->lexer);
8288             }
8289           break;
8290
8291           /* function-specifier:
8292                inline
8293                virtual
8294                explicit  */
8295         case RID_INLINE:
8296         case RID_VIRTUAL:
8297         case RID_EXPLICIT:
8298           cp_parser_function_specifier_opt (parser, decl_specs);
8299           break;
8300
8301           /* decl-specifier:
8302                typedef  */
8303         case RID_TYPEDEF:
8304           ++decl_specs->specs[(int) ds_typedef];
8305           /* Consume the token.  */
8306           cp_lexer_consume_token (parser->lexer);
8307           /* A constructor declarator cannot appear in a typedef.  */
8308           constructor_possible_p = false;
8309           /* The "typedef" keyword can only occur in a declaration; we
8310              may as well commit at this point.  */
8311           cp_parser_commit_to_tentative_parse (parser);
8312
8313           if (decl_specs->storage_class != sc_none)
8314             decl_specs->conflicting_specifiers_p = true;
8315           break;
8316
8317           /* storage-class-specifier:
8318                auto
8319                register
8320                static
8321                extern
8322                mutable
8323
8324              GNU Extension:
8325                thread  */
8326         case RID_AUTO:
8327           if (cxx_dialect == cxx98) 
8328             {
8329               /* Consume the token.  */
8330               cp_lexer_consume_token (parser->lexer);
8331
8332               /* Complain about `auto' as a storage specifier, if
8333                  we're complaining about C++0x compatibility.  */
8334               warning 
8335                 (OPT_Wc__0x_compat, 
8336                  "%H%<auto%> will change meaning in C++0x; please remove it",
8337                  &token->location);
8338
8339               /* Set the storage class anyway.  */
8340               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8341                                            token->location);
8342             }
8343           else
8344             /* C++0x auto type-specifier.  */
8345             found_decl_spec = false;
8346           break;
8347
8348         case RID_REGISTER:
8349         case RID_STATIC:
8350         case RID_EXTERN:
8351         case RID_MUTABLE:
8352           /* Consume the token.  */
8353           cp_lexer_consume_token (parser->lexer);
8354           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8355                                        token->location);
8356           break;
8357         case RID_THREAD:
8358           /* Consume the token.  */
8359           cp_lexer_consume_token (parser->lexer);
8360           ++decl_specs->specs[(int) ds_thread];
8361           break;
8362
8363         default:
8364           /* We did not yet find a decl-specifier yet.  */
8365           found_decl_spec = false;
8366           break;
8367         }
8368
8369       /* Constructors are a special case.  The `S' in `S()' is not a
8370          decl-specifier; it is the beginning of the declarator.  */
8371       constructor_p
8372         = (!found_decl_spec
8373            && constructor_possible_p
8374            && (cp_parser_constructor_declarator_p
8375                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8376
8377       /* If we don't have a DECL_SPEC yet, then we must be looking at
8378          a type-specifier.  */
8379       if (!found_decl_spec && !constructor_p)
8380         {
8381           int decl_spec_declares_class_or_enum;
8382           bool is_cv_qualifier;
8383           tree type_spec;
8384
8385           type_spec
8386             = cp_parser_type_specifier (parser, flags,
8387                                         decl_specs,
8388                                         /*is_declaration=*/true,
8389                                         &decl_spec_declares_class_or_enum,
8390                                         &is_cv_qualifier);
8391           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8392
8393           /* If this type-specifier referenced a user-defined type
8394              (a typedef, class-name, etc.), then we can't allow any
8395              more such type-specifiers henceforth.
8396
8397              [dcl.spec]
8398
8399              The longest sequence of decl-specifiers that could
8400              possibly be a type name is taken as the
8401              decl-specifier-seq of a declaration.  The sequence shall
8402              be self-consistent as described below.
8403
8404              [dcl.type]
8405
8406              As a general rule, at most one type-specifier is allowed
8407              in the complete decl-specifier-seq of a declaration.  The
8408              only exceptions are the following:
8409
8410              -- const or volatile can be combined with any other
8411                 type-specifier.
8412
8413              -- signed or unsigned can be combined with char, long,
8414                 short, or int.
8415
8416              -- ..
8417
8418              Example:
8419
8420                typedef char* Pc;
8421                void g (const int Pc);
8422
8423              Here, Pc is *not* part of the decl-specifier seq; it's
8424              the declarator.  Therefore, once we see a type-specifier
8425              (other than a cv-qualifier), we forbid any additional
8426              user-defined types.  We *do* still allow things like `int
8427              int' to be considered a decl-specifier-seq, and issue the
8428              error message later.  */
8429           if (type_spec && !is_cv_qualifier)
8430             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8431           /* A constructor declarator cannot follow a type-specifier.  */
8432           if (type_spec)
8433             {
8434               constructor_possible_p = false;
8435               found_decl_spec = true;
8436             }
8437         }
8438
8439       /* If we still do not have a DECL_SPEC, then there are no more
8440          decl-specifiers.  */
8441       if (!found_decl_spec)
8442         break;
8443
8444       decl_specs->any_specifiers_p = true;
8445       /* After we see one decl-specifier, further decl-specifiers are
8446          always optional.  */
8447       flags |= CP_PARSER_FLAGS_OPTIONAL;
8448     }
8449
8450   cp_parser_check_decl_spec (decl_specs, start_token->location);
8451
8452   /* Don't allow a friend specifier with a class definition.  */
8453   if (decl_specs->specs[(int) ds_friend] != 0
8454       && (*declares_class_or_enum & 2))
8455     error ("%Hclass definition may not be declared a friend",
8456             &start_token->location);
8457 }
8458
8459 /* Parse an (optional) storage-class-specifier.
8460
8461    storage-class-specifier:
8462      auto
8463      register
8464      static
8465      extern
8466      mutable
8467
8468    GNU Extension:
8469
8470    storage-class-specifier:
8471      thread
8472
8473    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8474
8475 static tree
8476 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8477 {
8478   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8479     {
8480     case RID_AUTO:
8481       if (cxx_dialect != cxx98)
8482         return NULL_TREE;
8483       /* Fall through for C++98.  */
8484
8485     case RID_REGISTER:
8486     case RID_STATIC:
8487     case RID_EXTERN:
8488     case RID_MUTABLE:
8489     case RID_THREAD:
8490       /* Consume the token.  */
8491       return cp_lexer_consume_token (parser->lexer)->u.value;
8492
8493     default:
8494       return NULL_TREE;
8495     }
8496 }
8497
8498 /* Parse an (optional) function-specifier.
8499
8500    function-specifier:
8501      inline
8502      virtual
8503      explicit
8504
8505    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8506    Updates DECL_SPECS, if it is non-NULL.  */
8507
8508 static tree
8509 cp_parser_function_specifier_opt (cp_parser* parser,
8510                                   cp_decl_specifier_seq *decl_specs)
8511 {
8512   cp_token *token = cp_lexer_peek_token (parser->lexer);
8513   switch (token->keyword)
8514     {
8515     case RID_INLINE:
8516       if (decl_specs)
8517         ++decl_specs->specs[(int) ds_inline];
8518       break;
8519
8520     case RID_VIRTUAL:
8521       /* 14.5.2.3 [temp.mem]
8522
8523          A member function template shall not be virtual.  */
8524       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8525         error ("%Htemplates may not be %<virtual%>", &token->location);
8526       else if (decl_specs)
8527         ++decl_specs->specs[(int) ds_virtual];
8528       break;
8529
8530     case RID_EXPLICIT:
8531       if (decl_specs)
8532         ++decl_specs->specs[(int) ds_explicit];
8533       break;
8534
8535     default:
8536       return NULL_TREE;
8537     }
8538
8539   /* Consume the token.  */
8540   return cp_lexer_consume_token (parser->lexer)->u.value;
8541 }
8542
8543 /* Parse a linkage-specification.
8544
8545    linkage-specification:
8546      extern string-literal { declaration-seq [opt] }
8547      extern string-literal declaration  */
8548
8549 static void
8550 cp_parser_linkage_specification (cp_parser* parser)
8551 {
8552   tree linkage;
8553
8554   /* Look for the `extern' keyword.  */
8555   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8556
8557   /* Look for the string-literal.  */
8558   linkage = cp_parser_string_literal (parser, false, false);
8559
8560   /* Transform the literal into an identifier.  If the literal is a
8561      wide-character string, or contains embedded NULs, then we can't
8562      handle it as the user wants.  */
8563   if (strlen (TREE_STRING_POINTER (linkage))
8564       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8565     {
8566       cp_parser_error (parser, "invalid linkage-specification");
8567       /* Assume C++ linkage.  */
8568       linkage = lang_name_cplusplus;
8569     }
8570   else
8571     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8572
8573   /* We're now using the new linkage.  */
8574   push_lang_context (linkage);
8575
8576   /* If the next token is a `{', then we're using the first
8577      production.  */
8578   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8579     {
8580       /* Consume the `{' token.  */
8581       cp_lexer_consume_token (parser->lexer);
8582       /* Parse the declarations.  */
8583       cp_parser_declaration_seq_opt (parser);
8584       /* Look for the closing `}'.  */
8585       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8586     }
8587   /* Otherwise, there's just one declaration.  */
8588   else
8589     {
8590       bool saved_in_unbraced_linkage_specification_p;
8591
8592       saved_in_unbraced_linkage_specification_p
8593         = parser->in_unbraced_linkage_specification_p;
8594       parser->in_unbraced_linkage_specification_p = true;
8595       cp_parser_declaration (parser);
8596       parser->in_unbraced_linkage_specification_p
8597         = saved_in_unbraced_linkage_specification_p;
8598     }
8599
8600   /* We're done with the linkage-specification.  */
8601   pop_lang_context ();
8602 }
8603
8604 /* Parse a static_assert-declaration.
8605
8606    static_assert-declaration:
8607      static_assert ( constant-expression , string-literal ) ; 
8608
8609    If MEMBER_P, this static_assert is a class member.  */
8610
8611 static void 
8612 cp_parser_static_assert(cp_parser *parser, bool member_p)
8613 {
8614   tree condition;
8615   tree message;
8616   cp_token *token;
8617   location_t saved_loc;
8618
8619   /* Peek at the `static_assert' token so we can keep track of exactly
8620      where the static assertion started.  */
8621   token = cp_lexer_peek_token (parser->lexer);
8622   saved_loc = token->location;
8623
8624   /* Look for the `static_assert' keyword.  */
8625   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8626                                   "%<static_assert%>"))
8627     return;
8628
8629   /*  We know we are in a static assertion; commit to any tentative
8630       parse.  */
8631   if (cp_parser_parsing_tentatively (parser))
8632     cp_parser_commit_to_tentative_parse (parser);
8633
8634   /* Parse the `(' starting the static assertion condition.  */
8635   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8636
8637   /* Parse the constant-expression.  */
8638   condition = 
8639     cp_parser_constant_expression (parser,
8640                                    /*allow_non_constant_p=*/false,
8641                                    /*non_constant_p=*/NULL);
8642
8643   /* Parse the separating `,'.  */
8644   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8645
8646   /* Parse the string-literal message.  */
8647   message = cp_parser_string_literal (parser, 
8648                                       /*translate=*/false,
8649                                       /*wide_ok=*/true);
8650
8651   /* A `)' completes the static assertion.  */
8652   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8653     cp_parser_skip_to_closing_parenthesis (parser, 
8654                                            /*recovering=*/true, 
8655                                            /*or_comma=*/false,
8656                                            /*consume_paren=*/true);
8657
8658   /* A semicolon terminates the declaration.  */
8659   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8660
8661   /* Complete the static assertion, which may mean either processing 
8662      the static assert now or saving it for template instantiation.  */
8663   finish_static_assert (condition, message, saved_loc, member_p);
8664 }
8665
8666 /* Parse a `decltype' type. Returns the type. 
8667
8668    simple-type-specifier:
8669      decltype ( expression )  */
8670
8671 static tree
8672 cp_parser_decltype (cp_parser *parser)
8673 {
8674   tree expr;
8675   bool id_expression_or_member_access_p = false;
8676   const char *saved_message;
8677   bool saved_integral_constant_expression_p;
8678   bool saved_non_integral_constant_expression_p;
8679   cp_token *id_expr_start_token;
8680
8681   /* Look for the `decltype' token.  */
8682   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8683     return error_mark_node;
8684
8685   /* Types cannot be defined in a `decltype' expression.  Save away the
8686      old message.  */
8687   saved_message = parser->type_definition_forbidden_message;
8688
8689   /* And create the new one.  */
8690   parser->type_definition_forbidden_message
8691     = "types may not be defined in %<decltype%> expressions";
8692
8693   /* The restrictions on constant-expressions do not apply inside
8694      decltype expressions.  */
8695   saved_integral_constant_expression_p
8696     = parser->integral_constant_expression_p;
8697   saved_non_integral_constant_expression_p
8698     = parser->non_integral_constant_expression_p;
8699   parser->integral_constant_expression_p = false;
8700
8701   /* Do not actually evaluate the expression.  */
8702   ++skip_evaluation;
8703
8704   /* Parse the opening `('.  */
8705   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8706     return error_mark_node;
8707   
8708   /* First, try parsing an id-expression.  */
8709   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8710   cp_parser_parse_tentatively (parser);
8711   expr = cp_parser_id_expression (parser,
8712                                   /*template_keyword_p=*/false,
8713                                   /*check_dependency_p=*/true,
8714                                   /*template_p=*/NULL,
8715                                   /*declarator_p=*/false,
8716                                   /*optional_p=*/false);
8717
8718   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8719     {
8720       bool non_integral_constant_expression_p = false;
8721       tree id_expression = expr;
8722       cp_id_kind idk;
8723       const char *error_msg;
8724
8725       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8726         /* Lookup the name we got back from the id-expression.  */
8727         expr = cp_parser_lookup_name (parser, expr,
8728                                       none_type,
8729                                       /*is_template=*/false,
8730                                       /*is_namespace=*/false,
8731                                       /*check_dependency=*/true,
8732                                       /*ambiguous_decls=*/NULL,
8733                                       id_expr_start_token->location);
8734
8735       if (expr
8736           && expr != error_mark_node
8737           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8738           && TREE_CODE (expr) != TYPE_DECL
8739           && (TREE_CODE (expr) != BIT_NOT_EXPR
8740               || !TYPE_P (TREE_OPERAND (expr, 0)))
8741           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8742         {
8743           /* Complete lookup of the id-expression.  */
8744           expr = (finish_id_expression
8745                   (id_expression, expr, parser->scope, &idk,
8746                    /*integral_constant_expression_p=*/false,
8747                    /*allow_non_integral_constant_expression_p=*/true,
8748                    &non_integral_constant_expression_p,
8749                    /*template_p=*/false,
8750                    /*done=*/true,
8751                    /*address_p=*/false,
8752                    /*template_arg_p=*/false,
8753                    &error_msg,
8754                    id_expr_start_token->location));
8755
8756           if (expr == error_mark_node)
8757             /* We found an id-expression, but it was something that we
8758                should not have found. This is an error, not something
8759                we can recover from, so note that we found an
8760                id-expression and we'll recover as gracefully as
8761                possible.  */
8762             id_expression_or_member_access_p = true;
8763         }
8764
8765       if (expr 
8766           && expr != error_mark_node
8767           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8768         /* We have an id-expression.  */
8769         id_expression_or_member_access_p = true;
8770     }
8771
8772   if (!id_expression_or_member_access_p)
8773     {
8774       /* Abort the id-expression parse.  */
8775       cp_parser_abort_tentative_parse (parser);
8776
8777       /* Parsing tentatively, again.  */
8778       cp_parser_parse_tentatively (parser);
8779
8780       /* Parse a class member access.  */
8781       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8782                                            /*cast_p=*/false,
8783                                            /*member_access_only_p=*/true);
8784
8785       if (expr 
8786           && expr != error_mark_node
8787           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8788         /* We have an id-expression.  */
8789         id_expression_or_member_access_p = true;
8790     }
8791
8792   if (id_expression_or_member_access_p)
8793     /* We have parsed the complete id-expression or member access.  */
8794     cp_parser_parse_definitely (parser);
8795   else
8796     {
8797       /* Abort our attempt to parse an id-expression or member access
8798          expression.  */
8799       cp_parser_abort_tentative_parse (parser);
8800
8801       /* Parse a full expression.  */
8802       expr = cp_parser_expression (parser, /*cast_p=*/false);
8803     }
8804
8805   /* Go back to evaluating expressions.  */
8806   --skip_evaluation;
8807
8808   /* Restore the old message and the integral constant expression
8809      flags.  */
8810   parser->type_definition_forbidden_message = saved_message;
8811   parser->integral_constant_expression_p
8812     = saved_integral_constant_expression_p;
8813   parser->non_integral_constant_expression_p
8814     = saved_non_integral_constant_expression_p;
8815
8816   if (expr == error_mark_node)
8817     {
8818       /* Skip everything up to the closing `)'.  */
8819       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8820                                              /*consume_paren=*/true);
8821       return error_mark_node;
8822     }
8823   
8824   /* Parse to the closing `)'.  */
8825   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8826     {
8827       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8828                                              /*consume_paren=*/true);
8829       return error_mark_node;
8830     }
8831
8832   return finish_decltype_type (expr, id_expression_or_member_access_p);
8833 }
8834
8835 /* Special member functions [gram.special] */
8836
8837 /* Parse a conversion-function-id.
8838
8839    conversion-function-id:
8840      operator conversion-type-id
8841
8842    Returns an IDENTIFIER_NODE representing the operator.  */
8843
8844 static tree
8845 cp_parser_conversion_function_id (cp_parser* parser)
8846 {
8847   tree type;
8848   tree saved_scope;
8849   tree saved_qualifying_scope;
8850   tree saved_object_scope;
8851   tree pushed_scope = NULL_TREE;
8852
8853   /* Look for the `operator' token.  */
8854   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8855     return error_mark_node;
8856   /* When we parse the conversion-type-id, the current scope will be
8857      reset.  However, we need that information in able to look up the
8858      conversion function later, so we save it here.  */
8859   saved_scope = parser->scope;
8860   saved_qualifying_scope = parser->qualifying_scope;
8861   saved_object_scope = parser->object_scope;
8862   /* We must enter the scope of the class so that the names of
8863      entities declared within the class are available in the
8864      conversion-type-id.  For example, consider:
8865
8866        struct S {
8867          typedef int I;
8868          operator I();
8869        };
8870
8871        S::operator I() { ... }
8872
8873      In order to see that `I' is a type-name in the definition, we
8874      must be in the scope of `S'.  */
8875   if (saved_scope)
8876     pushed_scope = push_scope (saved_scope);
8877   /* Parse the conversion-type-id.  */
8878   type = cp_parser_conversion_type_id (parser);
8879   /* Leave the scope of the class, if any.  */
8880   if (pushed_scope)
8881     pop_scope (pushed_scope);
8882   /* Restore the saved scope.  */
8883   parser->scope = saved_scope;
8884   parser->qualifying_scope = saved_qualifying_scope;
8885   parser->object_scope = saved_object_scope;
8886   /* If the TYPE is invalid, indicate failure.  */
8887   if (type == error_mark_node)
8888     return error_mark_node;
8889   return mangle_conv_op_name_for_type (type);
8890 }
8891
8892 /* Parse a conversion-type-id:
8893
8894    conversion-type-id:
8895      type-specifier-seq conversion-declarator [opt]
8896
8897    Returns the TYPE specified.  */
8898
8899 static tree
8900 cp_parser_conversion_type_id (cp_parser* parser)
8901 {
8902   tree attributes;
8903   cp_decl_specifier_seq type_specifiers;
8904   cp_declarator *declarator;
8905   tree type_specified;
8906
8907   /* Parse the attributes.  */
8908   attributes = cp_parser_attributes_opt (parser);
8909   /* Parse the type-specifiers.  */
8910   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8911                                 &type_specifiers);
8912   /* If that didn't work, stop.  */
8913   if (type_specifiers.type == error_mark_node)
8914     return error_mark_node;
8915   /* Parse the conversion-declarator.  */
8916   declarator = cp_parser_conversion_declarator_opt (parser);
8917
8918   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8919                                     /*initialized=*/0, &attributes);
8920   if (attributes)
8921     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8922   return type_specified;
8923 }
8924
8925 /* Parse an (optional) conversion-declarator.
8926
8927    conversion-declarator:
8928      ptr-operator conversion-declarator [opt]
8929
8930    */
8931
8932 static cp_declarator *
8933 cp_parser_conversion_declarator_opt (cp_parser* parser)
8934 {
8935   enum tree_code code;
8936   tree class_type;
8937   cp_cv_quals cv_quals;
8938
8939   /* We don't know if there's a ptr-operator next, or not.  */
8940   cp_parser_parse_tentatively (parser);
8941   /* Try the ptr-operator.  */
8942   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8943   /* If it worked, look for more conversion-declarators.  */
8944   if (cp_parser_parse_definitely (parser))
8945     {
8946       cp_declarator *declarator;
8947
8948       /* Parse another optional declarator.  */
8949       declarator = cp_parser_conversion_declarator_opt (parser);
8950
8951       return cp_parser_make_indirect_declarator
8952         (code, class_type, cv_quals, declarator);
8953    }
8954
8955   return NULL;
8956 }
8957
8958 /* Parse an (optional) ctor-initializer.
8959
8960    ctor-initializer:
8961      : mem-initializer-list
8962
8963    Returns TRUE iff the ctor-initializer was actually present.  */
8964
8965 static bool
8966 cp_parser_ctor_initializer_opt (cp_parser* parser)
8967 {
8968   /* If the next token is not a `:', then there is no
8969      ctor-initializer.  */
8970   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8971     {
8972       /* Do default initialization of any bases and members.  */
8973       if (DECL_CONSTRUCTOR_P (current_function_decl))
8974         finish_mem_initializers (NULL_TREE);
8975
8976       return false;
8977     }
8978
8979   /* Consume the `:' token.  */
8980   cp_lexer_consume_token (parser->lexer);
8981   /* And the mem-initializer-list.  */
8982   cp_parser_mem_initializer_list (parser);
8983
8984   return true;
8985 }
8986
8987 /* Parse a mem-initializer-list.
8988
8989    mem-initializer-list:
8990      mem-initializer ... [opt]
8991      mem-initializer ... [opt] , mem-initializer-list  */
8992
8993 static void
8994 cp_parser_mem_initializer_list (cp_parser* parser)
8995 {
8996   tree mem_initializer_list = NULL_TREE;
8997   cp_token *token = cp_lexer_peek_token (parser->lexer);
8998
8999   /* Let the semantic analysis code know that we are starting the
9000      mem-initializer-list.  */
9001   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9002     error ("%Honly constructors take base initializers",
9003            &token->location);
9004
9005   /* Loop through the list.  */
9006   while (true)
9007     {
9008       tree mem_initializer;
9009
9010       token = cp_lexer_peek_token (parser->lexer);
9011       /* Parse the mem-initializer.  */
9012       mem_initializer = cp_parser_mem_initializer (parser);
9013       /* If the next token is a `...', we're expanding member initializers. */
9014       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9015         {
9016           /* Consume the `...'. */
9017           cp_lexer_consume_token (parser->lexer);
9018
9019           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9020              can be expanded but members cannot. */
9021           if (mem_initializer != error_mark_node
9022               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9023             {
9024               error ("%Hcannot expand initializer for member %<%D%>",
9025                      &token->location, TREE_PURPOSE (mem_initializer));
9026               mem_initializer = error_mark_node;
9027             }
9028
9029           /* Construct the pack expansion type. */
9030           if (mem_initializer != error_mark_node)
9031             mem_initializer = make_pack_expansion (mem_initializer);
9032         }
9033       /* Add it to the list, unless it was erroneous.  */
9034       if (mem_initializer != error_mark_node)
9035         {
9036           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9037           mem_initializer_list = mem_initializer;
9038         }
9039       /* If the next token is not a `,', we're done.  */
9040       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9041         break;
9042       /* Consume the `,' token.  */
9043       cp_lexer_consume_token (parser->lexer);
9044     }
9045
9046   /* Perform semantic analysis.  */
9047   if (DECL_CONSTRUCTOR_P (current_function_decl))
9048     finish_mem_initializers (mem_initializer_list);
9049 }
9050
9051 /* Parse a mem-initializer.
9052
9053    mem-initializer:
9054      mem-initializer-id ( expression-list [opt] )
9055      mem-initializer-id braced-init-list
9056
9057    GNU extension:
9058
9059    mem-initializer:
9060      ( expression-list [opt] )
9061
9062    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9063    class) or FIELD_DECL (for a non-static data member) to initialize;
9064    the TREE_VALUE is the expression-list.  An empty initialization
9065    list is represented by void_list_node.  */
9066
9067 static tree
9068 cp_parser_mem_initializer (cp_parser* parser)
9069 {
9070   tree mem_initializer_id;
9071   tree expression_list;
9072   tree member;
9073   cp_token *token = cp_lexer_peek_token (parser->lexer);
9074
9075   /* Find out what is being initialized.  */
9076   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9077     {
9078       permerror (token->location,
9079                  "anachronistic old-style base class initializer");
9080       mem_initializer_id = NULL_TREE;
9081     }
9082   else
9083     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9084   member = expand_member_init (mem_initializer_id);
9085   if (member && !DECL_P (member))
9086     in_base_initializer = 1;
9087
9088   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9089     {
9090       bool expr_non_constant_p;
9091       maybe_warn_cpp0x ("extended initializer lists");
9092       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9093       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9094       expression_list = build_tree_list (NULL_TREE, expression_list);
9095     }
9096   else
9097     expression_list
9098       = cp_parser_parenthesized_expression_list (parser, false,
9099                                                  /*cast_p=*/false,
9100                                                  /*allow_expansion_p=*/true,
9101                                                  /*non_constant_p=*/NULL);
9102   if (expression_list == error_mark_node)
9103     return error_mark_node;
9104   if (!expression_list)
9105     expression_list = void_type_node;
9106
9107   in_base_initializer = 0;
9108
9109   return member ? build_tree_list (member, expression_list) : error_mark_node;
9110 }
9111
9112 /* Parse a mem-initializer-id.
9113
9114    mem-initializer-id:
9115      :: [opt] nested-name-specifier [opt] class-name
9116      identifier
9117
9118    Returns a TYPE indicating the class to be initializer for the first
9119    production.  Returns an IDENTIFIER_NODE indicating the data member
9120    to be initialized for the second production.  */
9121
9122 static tree
9123 cp_parser_mem_initializer_id (cp_parser* parser)
9124 {
9125   bool global_scope_p;
9126   bool nested_name_specifier_p;
9127   bool template_p = false;
9128   tree id;
9129
9130   cp_token *token = cp_lexer_peek_token (parser->lexer);
9131
9132   /* `typename' is not allowed in this context ([temp.res]).  */
9133   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9134     {
9135       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9136              "member initializer is implicitly a type)",
9137              &token->location);
9138       cp_lexer_consume_token (parser->lexer);
9139     }
9140   /* Look for the optional `::' operator.  */
9141   global_scope_p
9142     = (cp_parser_global_scope_opt (parser,
9143                                    /*current_scope_valid_p=*/false)
9144        != NULL_TREE);
9145   /* Look for the optional nested-name-specifier.  The simplest way to
9146      implement:
9147
9148        [temp.res]
9149
9150        The keyword `typename' is not permitted in a base-specifier or
9151        mem-initializer; in these contexts a qualified name that
9152        depends on a template-parameter is implicitly assumed to be a
9153        type name.
9154
9155      is to assume that we have seen the `typename' keyword at this
9156      point.  */
9157   nested_name_specifier_p
9158     = (cp_parser_nested_name_specifier_opt (parser,
9159                                             /*typename_keyword_p=*/true,
9160                                             /*check_dependency_p=*/true,
9161                                             /*type_p=*/true,
9162                                             /*is_declaration=*/true)
9163        != NULL_TREE);
9164   if (nested_name_specifier_p)
9165     template_p = cp_parser_optional_template_keyword (parser);
9166   /* If there is a `::' operator or a nested-name-specifier, then we
9167      are definitely looking for a class-name.  */
9168   if (global_scope_p || nested_name_specifier_p)
9169     return cp_parser_class_name (parser,
9170                                  /*typename_keyword_p=*/true,
9171                                  /*template_keyword_p=*/template_p,
9172                                  none_type,
9173                                  /*check_dependency_p=*/true,
9174                                  /*class_head_p=*/false,
9175                                  /*is_declaration=*/true);
9176   /* Otherwise, we could also be looking for an ordinary identifier.  */
9177   cp_parser_parse_tentatively (parser);
9178   /* Try a class-name.  */
9179   id = cp_parser_class_name (parser,
9180                              /*typename_keyword_p=*/true,
9181                              /*template_keyword_p=*/false,
9182                              none_type,
9183                              /*check_dependency_p=*/true,
9184                              /*class_head_p=*/false,
9185                              /*is_declaration=*/true);
9186   /* If we found one, we're done.  */
9187   if (cp_parser_parse_definitely (parser))
9188     return id;
9189   /* Otherwise, look for an ordinary identifier.  */
9190   return cp_parser_identifier (parser);
9191 }
9192
9193 /* Overloading [gram.over] */
9194
9195 /* Parse an operator-function-id.
9196
9197    operator-function-id:
9198      operator operator
9199
9200    Returns an IDENTIFIER_NODE for the operator which is a
9201    human-readable spelling of the identifier, e.g., `operator +'.  */
9202
9203 static tree
9204 cp_parser_operator_function_id (cp_parser* parser)
9205 {
9206   /* Look for the `operator' keyword.  */
9207   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9208     return error_mark_node;
9209   /* And then the name of the operator itself.  */
9210   return cp_parser_operator (parser);
9211 }
9212
9213 /* Parse an operator.
9214
9215    operator:
9216      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9217      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9218      || ++ -- , ->* -> () []
9219
9220    GNU Extensions:
9221
9222    operator:
9223      <? >? <?= >?=
9224
9225    Returns an IDENTIFIER_NODE for the operator which is a
9226    human-readable spelling of the identifier, e.g., `operator +'.  */
9227
9228 static tree
9229 cp_parser_operator (cp_parser* parser)
9230 {
9231   tree id = NULL_TREE;
9232   cp_token *token;
9233
9234   /* Peek at the next token.  */
9235   token = cp_lexer_peek_token (parser->lexer);
9236   /* Figure out which operator we have.  */
9237   switch (token->type)
9238     {
9239     case CPP_KEYWORD:
9240       {
9241         enum tree_code op;
9242
9243         /* The keyword should be either `new' or `delete'.  */
9244         if (token->keyword == RID_NEW)
9245           op = NEW_EXPR;
9246         else if (token->keyword == RID_DELETE)
9247           op = DELETE_EXPR;
9248         else
9249           break;
9250
9251         /* Consume the `new' or `delete' token.  */
9252         cp_lexer_consume_token (parser->lexer);
9253
9254         /* Peek at the next token.  */
9255         token = cp_lexer_peek_token (parser->lexer);
9256         /* If it's a `[' token then this is the array variant of the
9257            operator.  */
9258         if (token->type == CPP_OPEN_SQUARE)
9259           {
9260             /* Consume the `[' token.  */
9261             cp_lexer_consume_token (parser->lexer);
9262             /* Look for the `]' token.  */
9263             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9264             id = ansi_opname (op == NEW_EXPR
9265                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9266           }
9267         /* Otherwise, we have the non-array variant.  */
9268         else
9269           id = ansi_opname (op);
9270
9271         return id;
9272       }
9273
9274     case CPP_PLUS:
9275       id = ansi_opname (PLUS_EXPR);
9276       break;
9277
9278     case CPP_MINUS:
9279       id = ansi_opname (MINUS_EXPR);
9280       break;
9281
9282     case CPP_MULT:
9283       id = ansi_opname (MULT_EXPR);
9284       break;
9285
9286     case CPP_DIV:
9287       id = ansi_opname (TRUNC_DIV_EXPR);
9288       break;
9289
9290     case CPP_MOD:
9291       id = ansi_opname (TRUNC_MOD_EXPR);
9292       break;
9293
9294     case CPP_XOR:
9295       id = ansi_opname (BIT_XOR_EXPR);
9296       break;
9297
9298     case CPP_AND:
9299       id = ansi_opname (BIT_AND_EXPR);
9300       break;
9301
9302     case CPP_OR:
9303       id = ansi_opname (BIT_IOR_EXPR);
9304       break;
9305
9306     case CPP_COMPL:
9307       id = ansi_opname (BIT_NOT_EXPR);
9308       break;
9309
9310     case CPP_NOT:
9311       id = ansi_opname (TRUTH_NOT_EXPR);
9312       break;
9313
9314     case CPP_EQ:
9315       id = ansi_assopname (NOP_EXPR);
9316       break;
9317
9318     case CPP_LESS:
9319       id = ansi_opname (LT_EXPR);
9320       break;
9321
9322     case CPP_GREATER:
9323       id = ansi_opname (GT_EXPR);
9324       break;
9325
9326     case CPP_PLUS_EQ:
9327       id = ansi_assopname (PLUS_EXPR);
9328       break;
9329
9330     case CPP_MINUS_EQ:
9331       id = ansi_assopname (MINUS_EXPR);
9332       break;
9333
9334     case CPP_MULT_EQ:
9335       id = ansi_assopname (MULT_EXPR);
9336       break;
9337
9338     case CPP_DIV_EQ:
9339       id = ansi_assopname (TRUNC_DIV_EXPR);
9340       break;
9341
9342     case CPP_MOD_EQ:
9343       id = ansi_assopname (TRUNC_MOD_EXPR);
9344       break;
9345
9346     case CPP_XOR_EQ:
9347       id = ansi_assopname (BIT_XOR_EXPR);
9348       break;
9349
9350     case CPP_AND_EQ:
9351       id = ansi_assopname (BIT_AND_EXPR);
9352       break;
9353
9354     case CPP_OR_EQ:
9355       id = ansi_assopname (BIT_IOR_EXPR);
9356       break;
9357
9358     case CPP_LSHIFT:
9359       id = ansi_opname (LSHIFT_EXPR);
9360       break;
9361
9362     case CPP_RSHIFT:
9363       id = ansi_opname (RSHIFT_EXPR);
9364       break;
9365
9366     case CPP_LSHIFT_EQ:
9367       id = ansi_assopname (LSHIFT_EXPR);
9368       break;
9369
9370     case CPP_RSHIFT_EQ:
9371       id = ansi_assopname (RSHIFT_EXPR);
9372       break;
9373
9374     case CPP_EQ_EQ:
9375       id = ansi_opname (EQ_EXPR);
9376       break;
9377
9378     case CPP_NOT_EQ:
9379       id = ansi_opname (NE_EXPR);
9380       break;
9381
9382     case CPP_LESS_EQ:
9383       id = ansi_opname (LE_EXPR);
9384       break;
9385
9386     case CPP_GREATER_EQ:
9387       id = ansi_opname (GE_EXPR);
9388       break;
9389
9390     case CPP_AND_AND:
9391       id = ansi_opname (TRUTH_ANDIF_EXPR);
9392       break;
9393
9394     case CPP_OR_OR:
9395       id = ansi_opname (TRUTH_ORIF_EXPR);
9396       break;
9397
9398     case CPP_PLUS_PLUS:
9399       id = ansi_opname (POSTINCREMENT_EXPR);
9400       break;
9401
9402     case CPP_MINUS_MINUS:
9403       id = ansi_opname (PREDECREMENT_EXPR);
9404       break;
9405
9406     case CPP_COMMA:
9407       id = ansi_opname (COMPOUND_EXPR);
9408       break;
9409
9410     case CPP_DEREF_STAR:
9411       id = ansi_opname (MEMBER_REF);
9412       break;
9413
9414     case CPP_DEREF:
9415       id = ansi_opname (COMPONENT_REF);
9416       break;
9417
9418     case CPP_OPEN_PAREN:
9419       /* Consume the `('.  */
9420       cp_lexer_consume_token (parser->lexer);
9421       /* Look for the matching `)'.  */
9422       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9423       return ansi_opname (CALL_EXPR);
9424
9425     case CPP_OPEN_SQUARE:
9426       /* Consume the `['.  */
9427       cp_lexer_consume_token (parser->lexer);
9428       /* Look for the matching `]'.  */
9429       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9430       return ansi_opname (ARRAY_REF);
9431
9432     default:
9433       /* Anything else is an error.  */
9434       break;
9435     }
9436
9437   /* If we have selected an identifier, we need to consume the
9438      operator token.  */
9439   if (id)
9440     cp_lexer_consume_token (parser->lexer);
9441   /* Otherwise, no valid operator name was present.  */
9442   else
9443     {
9444       cp_parser_error (parser, "expected operator");
9445       id = error_mark_node;
9446     }
9447
9448   return id;
9449 }
9450
9451 /* Parse a template-declaration.
9452
9453    template-declaration:
9454      export [opt] template < template-parameter-list > declaration
9455
9456    If MEMBER_P is TRUE, this template-declaration occurs within a
9457    class-specifier.
9458
9459    The grammar rule given by the standard isn't correct.  What
9460    is really meant is:
9461
9462    template-declaration:
9463      export [opt] template-parameter-list-seq
9464        decl-specifier-seq [opt] init-declarator [opt] ;
9465      export [opt] template-parameter-list-seq
9466        function-definition
9467
9468    template-parameter-list-seq:
9469      template-parameter-list-seq [opt]
9470      template < template-parameter-list >  */
9471
9472 static void
9473 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9474 {
9475   /* Check for `export'.  */
9476   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9477     {
9478       /* Consume the `export' token.  */
9479       cp_lexer_consume_token (parser->lexer);
9480       /* Warn that we do not support `export'.  */
9481       warning (0, "keyword %<export%> not implemented, and will be ignored");
9482     }
9483
9484   cp_parser_template_declaration_after_export (parser, member_p);
9485 }
9486
9487 /* Parse a template-parameter-list.
9488
9489    template-parameter-list:
9490      template-parameter
9491      template-parameter-list , template-parameter
9492
9493    Returns a TREE_LIST.  Each node represents a template parameter.
9494    The nodes are connected via their TREE_CHAINs.  */
9495
9496 static tree
9497 cp_parser_template_parameter_list (cp_parser* parser)
9498 {
9499   tree parameter_list = NULL_TREE;
9500
9501   begin_template_parm_list ();
9502   while (true)
9503     {
9504       tree parameter;
9505       bool is_non_type;
9506       bool is_parameter_pack;
9507
9508       /* Parse the template-parameter.  */
9509       parameter = cp_parser_template_parameter (parser, 
9510                                                 &is_non_type,
9511                                                 &is_parameter_pack);
9512       /* Add it to the list.  */
9513       if (parameter != error_mark_node)
9514         parameter_list = process_template_parm (parameter_list,
9515                                                 parameter,
9516                                                 is_non_type,
9517                                                 is_parameter_pack);
9518       else
9519        {
9520          tree err_parm = build_tree_list (parameter, parameter);
9521          TREE_VALUE (err_parm) = error_mark_node;
9522          parameter_list = chainon (parameter_list, err_parm);
9523        }
9524
9525       /* If the next token is not a `,', we're done.  */
9526       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9527         break;
9528       /* Otherwise, consume the `,' token.  */
9529       cp_lexer_consume_token (parser->lexer);
9530     }
9531
9532   return end_template_parm_list (parameter_list);
9533 }
9534
9535 /* Parse a template-parameter.
9536
9537    template-parameter:
9538      type-parameter
9539      parameter-declaration
9540
9541    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9542    the parameter.  The TREE_PURPOSE is the default value, if any.
9543    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9544    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9545    set to true iff this parameter is a parameter pack. */
9546
9547 static tree
9548 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9549                               bool *is_parameter_pack)
9550 {
9551   cp_token *token;
9552   cp_parameter_declarator *parameter_declarator;
9553   cp_declarator *id_declarator;
9554   tree parm;
9555
9556   /* Assume it is a type parameter or a template parameter.  */
9557   *is_non_type = false;
9558   /* Assume it not a parameter pack. */
9559   *is_parameter_pack = false;
9560   /* Peek at the next token.  */
9561   token = cp_lexer_peek_token (parser->lexer);
9562   /* If it is `class' or `template', we have a type-parameter.  */
9563   if (token->keyword == RID_TEMPLATE)
9564     return cp_parser_type_parameter (parser, is_parameter_pack);
9565   /* If it is `class' or `typename' we do not know yet whether it is a
9566      type parameter or a non-type parameter.  Consider:
9567
9568        template <typename T, typename T::X X> ...
9569
9570      or:
9571
9572        template <class C, class D*> ...
9573
9574      Here, the first parameter is a type parameter, and the second is
9575      a non-type parameter.  We can tell by looking at the token after
9576      the identifier -- if it is a `,', `=', or `>' then we have a type
9577      parameter.  */
9578   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9579     {
9580       /* Peek at the token after `class' or `typename'.  */
9581       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9582       /* If it's an ellipsis, we have a template type parameter
9583          pack. */
9584       if (token->type == CPP_ELLIPSIS)
9585         return cp_parser_type_parameter (parser, is_parameter_pack);
9586       /* If it's an identifier, skip it.  */
9587       if (token->type == CPP_NAME)
9588         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9589       /* Now, see if the token looks like the end of a template
9590          parameter.  */
9591       if (token->type == CPP_COMMA
9592           || token->type == CPP_EQ
9593           || token->type == CPP_GREATER)
9594         return cp_parser_type_parameter (parser, is_parameter_pack);
9595     }
9596
9597   /* Otherwise, it is a non-type parameter.
9598
9599      [temp.param]
9600
9601      When parsing a default template-argument for a non-type
9602      template-parameter, the first non-nested `>' is taken as the end
9603      of the template parameter-list rather than a greater-than
9604      operator.  */
9605   *is_non_type = true;
9606   parameter_declarator
9607      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9608                                         /*parenthesized_p=*/NULL);
9609
9610   /* If the parameter declaration is marked as a parameter pack, set
9611      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9612      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9613      grokdeclarator. */
9614   if (parameter_declarator
9615       && parameter_declarator->declarator
9616       && parameter_declarator->declarator->parameter_pack_p)
9617     {
9618       *is_parameter_pack = true;
9619       parameter_declarator->declarator->parameter_pack_p = false;
9620     }
9621
9622   /* If the next token is an ellipsis, and we don't already have it
9623      marked as a parameter pack, then we have a parameter pack (that
9624      has no declarator).  */
9625   if (!*is_parameter_pack
9626       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9627       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9628     {
9629       /* Consume the `...'.  */
9630       cp_lexer_consume_token (parser->lexer);
9631       maybe_warn_variadic_templates ();
9632       
9633       *is_parameter_pack = true;
9634     }
9635   /* We might end up with a pack expansion as the type of the non-type
9636      template parameter, in which case this is a non-type template
9637      parameter pack.  */
9638   else if (parameter_declarator
9639            && parameter_declarator->decl_specifiers.type
9640            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9641     {
9642       *is_parameter_pack = true;
9643       parameter_declarator->decl_specifiers.type = 
9644         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9645     }
9646
9647   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9648     {
9649       /* Parameter packs cannot have default arguments.  However, a
9650          user may try to do so, so we'll parse them and give an
9651          appropriate diagnostic here.  */
9652
9653       /* Consume the `='.  */
9654       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9655       cp_lexer_consume_token (parser->lexer);
9656       
9657       /* Find the name of the parameter pack.  */     
9658       id_declarator = parameter_declarator->declarator;
9659       while (id_declarator && id_declarator->kind != cdk_id)
9660         id_declarator = id_declarator->declarator;
9661       
9662       if (id_declarator && id_declarator->kind == cdk_id)
9663         error ("%Htemplate parameter pack %qD cannot have a default argument",
9664                &start_token->location, id_declarator->u.id.unqualified_name);
9665       else
9666         error ("%Htemplate parameter pack cannot have a default argument",
9667                &start_token->location);
9668       
9669       /* Parse the default argument, but throw away the result.  */
9670       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9671     }
9672
9673   parm = grokdeclarator (parameter_declarator->declarator,
9674                          &parameter_declarator->decl_specifiers,
9675                          PARM, /*initialized=*/0,
9676                          /*attrlist=*/NULL);
9677   if (parm == error_mark_node)
9678     return error_mark_node;
9679
9680   return build_tree_list (parameter_declarator->default_argument, parm);
9681 }
9682
9683 /* Parse a type-parameter.
9684
9685    type-parameter:
9686      class identifier [opt]
9687      class identifier [opt] = type-id
9688      typename identifier [opt]
9689      typename identifier [opt] = type-id
9690      template < template-parameter-list > class identifier [opt]
9691      template < template-parameter-list > class identifier [opt]
9692        = id-expression
9693
9694    GNU Extension (variadic templates):
9695
9696    type-parameter:
9697      class ... identifier [opt]
9698      typename ... identifier [opt]
9699
9700    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9701    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9702    the declaration of the parameter.
9703
9704    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9705
9706 static tree
9707 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9708 {
9709   cp_token *token;
9710   tree parameter;
9711
9712   /* Look for a keyword to tell us what kind of parameter this is.  */
9713   token = cp_parser_require (parser, CPP_KEYWORD,
9714                              "%<class%>, %<typename%>, or %<template%>");
9715   if (!token)
9716     return error_mark_node;
9717
9718   switch (token->keyword)
9719     {
9720     case RID_CLASS:
9721     case RID_TYPENAME:
9722       {
9723         tree identifier;
9724         tree default_argument;
9725
9726         /* If the next token is an ellipsis, we have a template
9727            argument pack. */
9728         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9729           {
9730             /* Consume the `...' token. */
9731             cp_lexer_consume_token (parser->lexer);
9732             maybe_warn_variadic_templates ();
9733
9734             *is_parameter_pack = true;
9735           }
9736
9737         /* If the next token is an identifier, then it names the
9738            parameter.  */
9739         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9740           identifier = cp_parser_identifier (parser);
9741         else
9742           identifier = NULL_TREE;
9743
9744         /* Create the parameter.  */
9745         parameter = finish_template_type_parm (class_type_node, identifier);
9746
9747         /* If the next token is an `=', we have a default argument.  */
9748         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9749           {
9750             /* Consume the `=' token.  */
9751             cp_lexer_consume_token (parser->lexer);
9752             /* Parse the default-argument.  */
9753             push_deferring_access_checks (dk_no_deferred);
9754             default_argument = cp_parser_type_id (parser);
9755
9756             /* Template parameter packs cannot have default
9757                arguments. */
9758             if (*is_parameter_pack)
9759               {
9760                 if (identifier)
9761                   error ("%Htemplate parameter pack %qD cannot have a "
9762                          "default argument", &token->location, identifier);
9763                 else
9764                   error ("%Htemplate parameter packs cannot have "
9765                          "default arguments", &token->location);
9766                 default_argument = NULL_TREE;
9767               }
9768             pop_deferring_access_checks ();
9769           }
9770         else
9771           default_argument = NULL_TREE;
9772
9773         /* Create the combined representation of the parameter and the
9774            default argument.  */
9775         parameter = build_tree_list (default_argument, parameter);
9776       }
9777       break;
9778
9779     case RID_TEMPLATE:
9780       {
9781         tree parameter_list;
9782         tree identifier;
9783         tree default_argument;
9784
9785         /* Look for the `<'.  */
9786         cp_parser_require (parser, CPP_LESS, "%<<%>");
9787         /* Parse the template-parameter-list.  */
9788         parameter_list = cp_parser_template_parameter_list (parser);
9789         /* Look for the `>'.  */
9790         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9791         /* Look for the `class' keyword.  */
9792         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9793         /* If the next token is an ellipsis, we have a template
9794            argument pack. */
9795         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9796           {
9797             /* Consume the `...' token. */
9798             cp_lexer_consume_token (parser->lexer);
9799             maybe_warn_variadic_templates ();
9800
9801             *is_parameter_pack = true;
9802           }
9803         /* If the next token is an `=', then there is a
9804            default-argument.  If the next token is a `>', we are at
9805            the end of the parameter-list.  If the next token is a `,',
9806            then we are at the end of this parameter.  */
9807         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9808             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9809             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9810           {
9811             identifier = cp_parser_identifier (parser);
9812             /* Treat invalid names as if the parameter were nameless.  */
9813             if (identifier == error_mark_node)
9814               identifier = NULL_TREE;
9815           }
9816         else
9817           identifier = NULL_TREE;
9818
9819         /* Create the template parameter.  */
9820         parameter = finish_template_template_parm (class_type_node,
9821                                                    identifier);
9822
9823         /* If the next token is an `=', then there is a
9824            default-argument.  */
9825         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9826           {
9827             bool is_template;
9828
9829             /* Consume the `='.  */
9830             cp_lexer_consume_token (parser->lexer);
9831             /* Parse the id-expression.  */
9832             push_deferring_access_checks (dk_no_deferred);
9833             /* save token before parsing the id-expression, for error
9834                reporting */
9835             token = cp_lexer_peek_token (parser->lexer);
9836             default_argument
9837               = cp_parser_id_expression (parser,
9838                                          /*template_keyword_p=*/false,
9839                                          /*check_dependency_p=*/true,
9840                                          /*template_p=*/&is_template,
9841                                          /*declarator_p=*/false,
9842                                          /*optional_p=*/false);
9843             if (TREE_CODE (default_argument) == TYPE_DECL)
9844               /* If the id-expression was a template-id that refers to
9845                  a template-class, we already have the declaration here,
9846                  so no further lookup is needed.  */
9847                  ;
9848             else
9849               /* Look up the name.  */
9850               default_argument
9851                 = cp_parser_lookup_name (parser, default_argument,
9852                                          none_type,
9853                                          /*is_template=*/is_template,
9854                                          /*is_namespace=*/false,
9855                                          /*check_dependency=*/true,
9856                                          /*ambiguous_decls=*/NULL,
9857                                          token->location);
9858             /* See if the default argument is valid.  */
9859             default_argument
9860               = check_template_template_default_arg (default_argument);
9861
9862             /* Template parameter packs cannot have default
9863                arguments. */
9864             if (*is_parameter_pack)
9865               {
9866                 if (identifier)
9867                   error ("%Htemplate parameter pack %qD cannot "
9868                          "have a default argument",
9869                          &token->location, identifier);
9870                 else
9871                   error ("%Htemplate parameter packs cannot "
9872                          "have default arguments",
9873                          &token->location);
9874                 default_argument = NULL_TREE;
9875               }
9876             pop_deferring_access_checks ();
9877           }
9878         else
9879           default_argument = NULL_TREE;
9880
9881         /* Create the combined representation of the parameter and the
9882            default argument.  */
9883         parameter = build_tree_list (default_argument, parameter);
9884       }
9885       break;
9886
9887     default:
9888       gcc_unreachable ();
9889       break;
9890     }
9891
9892   return parameter;
9893 }
9894
9895 /* Parse a template-id.
9896
9897    template-id:
9898      template-name < template-argument-list [opt] >
9899
9900    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9901    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9902    returned.  Otherwise, if the template-name names a function, or set
9903    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9904    names a class, returns a TYPE_DECL for the specialization.
9905
9906    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9907    uninstantiated templates.  */
9908
9909 static tree
9910 cp_parser_template_id (cp_parser *parser,
9911                        bool template_keyword_p,
9912                        bool check_dependency_p,
9913                        bool is_declaration)
9914 {
9915   int i;
9916   tree templ;
9917   tree arguments;
9918   tree template_id;
9919   cp_token_position start_of_id = 0;
9920   deferred_access_check *chk;
9921   VEC (deferred_access_check,gc) *access_check;
9922   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9923   bool is_identifier;
9924
9925   /* If the next token corresponds to a template-id, there is no need
9926      to reparse it.  */
9927   next_token = cp_lexer_peek_token (parser->lexer);
9928   if (next_token->type == CPP_TEMPLATE_ID)
9929     {
9930       struct tree_check *check_value;
9931
9932       /* Get the stored value.  */
9933       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9934       /* Perform any access checks that were deferred.  */
9935       access_check = check_value->checks;
9936       if (access_check)
9937         {
9938           for (i = 0 ;
9939                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9940                ++i)
9941             {
9942               perform_or_defer_access_check (chk->binfo,
9943                                              chk->decl,
9944                                              chk->diag_decl);
9945             }
9946         }
9947       /* Return the stored value.  */
9948       return check_value->value;
9949     }
9950
9951   /* Avoid performing name lookup if there is no possibility of
9952      finding a template-id.  */
9953   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9954       || (next_token->type == CPP_NAME
9955           && !cp_parser_nth_token_starts_template_argument_list_p
9956                (parser, 2)))
9957     {
9958       cp_parser_error (parser, "expected template-id");
9959       return error_mark_node;
9960     }
9961
9962   /* Remember where the template-id starts.  */
9963   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9964     start_of_id = cp_lexer_token_position (parser->lexer, false);
9965
9966   push_deferring_access_checks (dk_deferred);
9967
9968   /* Parse the template-name.  */
9969   is_identifier = false;
9970   token = cp_lexer_peek_token (parser->lexer);
9971   templ = cp_parser_template_name (parser, template_keyword_p,
9972                                    check_dependency_p,
9973                                    is_declaration,
9974                                    &is_identifier);
9975   if (templ == error_mark_node || is_identifier)
9976     {
9977       pop_deferring_access_checks ();
9978       return templ;
9979     }
9980
9981   /* If we find the sequence `[:' after a template-name, it's probably
9982      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9983      parse correctly the argument list.  */
9984   next_token = cp_lexer_peek_token (parser->lexer);
9985   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9986   if (next_token->type == CPP_OPEN_SQUARE
9987       && next_token->flags & DIGRAPH
9988       && next_token_2->type == CPP_COLON
9989       && !(next_token_2->flags & PREV_WHITE))
9990     {
9991       cp_parser_parse_tentatively (parser);
9992       /* Change `:' into `::'.  */
9993       next_token_2->type = CPP_SCOPE;
9994       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9995          CPP_LESS.  */
9996       cp_lexer_consume_token (parser->lexer);
9997
9998       /* Parse the arguments.  */
9999       arguments = cp_parser_enclosed_template_argument_list (parser);
10000       if (!cp_parser_parse_definitely (parser))
10001         {
10002           /* If we couldn't parse an argument list, then we revert our changes
10003              and return simply an error. Maybe this is not a template-id
10004              after all.  */
10005           next_token_2->type = CPP_COLON;
10006           cp_parser_error (parser, "expected %<<%>");
10007           pop_deferring_access_checks ();
10008           return error_mark_node;
10009         }
10010       /* Otherwise, emit an error about the invalid digraph, but continue
10011          parsing because we got our argument list.  */
10012       if (permerror (next_token->location,
10013                      "%<<::%> cannot begin a template-argument list"))
10014         {
10015           static bool hint = false;
10016           inform (next_token->location,
10017                   "%<<:%> is an alternate spelling for %<[%>."
10018                   " Insert whitespace between %<<%> and %<::%>");
10019           if (!hint && !flag_permissive)
10020             {
10021               inform (next_token->location, "(if you use %<-fpermissive%>"
10022                       " G++ will accept your code)");
10023               hint = true;
10024             }
10025         }
10026     }
10027   else
10028     {
10029       /* Look for the `<' that starts the template-argument-list.  */
10030       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10031         {
10032           pop_deferring_access_checks ();
10033           return error_mark_node;
10034         }
10035       /* Parse the arguments.  */
10036       arguments = cp_parser_enclosed_template_argument_list (parser);
10037     }
10038
10039   /* Build a representation of the specialization.  */
10040   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10041     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10042   else if (DECL_CLASS_TEMPLATE_P (templ)
10043            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10044     {
10045       bool entering_scope;
10046       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10047          template (rather than some instantiation thereof) only if
10048          is not nested within some other construct.  For example, in
10049          "template <typename T> void f(T) { A<T>::", A<T> is just an
10050          instantiation of A.  */
10051       entering_scope = (template_parm_scope_p ()
10052                         && cp_lexer_next_token_is (parser->lexer,
10053                                                    CPP_SCOPE));
10054       template_id
10055         = finish_template_type (templ, arguments, entering_scope);
10056     }
10057   else
10058     {
10059       /* If it's not a class-template or a template-template, it should be
10060          a function-template.  */
10061       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10062                    || TREE_CODE (templ) == OVERLOAD
10063                    || BASELINK_P (templ)));
10064
10065       template_id = lookup_template_function (templ, arguments);
10066     }
10067
10068   /* If parsing tentatively, replace the sequence of tokens that makes
10069      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10070      should we re-parse the token stream, we will not have to repeat
10071      the effort required to do the parse, nor will we issue duplicate
10072      error messages about problems during instantiation of the
10073      template.  */
10074   if (start_of_id)
10075     {
10076       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10077
10078       /* Reset the contents of the START_OF_ID token.  */
10079       token->type = CPP_TEMPLATE_ID;
10080       /* Retrieve any deferred checks.  Do not pop this access checks yet
10081          so the memory will not be reclaimed during token replacing below.  */
10082       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10083       token->u.tree_check_value->value = template_id;
10084       token->u.tree_check_value->checks = get_deferred_access_checks ();
10085       token->keyword = RID_MAX;
10086
10087       /* Purge all subsequent tokens.  */
10088       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10089
10090       /* ??? Can we actually assume that, if template_id ==
10091          error_mark_node, we will have issued a diagnostic to the
10092          user, as opposed to simply marking the tentative parse as
10093          failed?  */
10094       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10095         error ("%Hparse error in template argument list",
10096                &token->location);
10097     }
10098
10099   pop_deferring_access_checks ();
10100   return template_id;
10101 }
10102
10103 /* Parse a template-name.
10104
10105    template-name:
10106      identifier
10107
10108    The standard should actually say:
10109
10110    template-name:
10111      identifier
10112      operator-function-id
10113
10114    A defect report has been filed about this issue.
10115
10116    A conversion-function-id cannot be a template name because they cannot
10117    be part of a template-id. In fact, looking at this code:
10118
10119    a.operator K<int>()
10120
10121    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10122    It is impossible to call a templated conversion-function-id with an
10123    explicit argument list, since the only allowed template parameter is
10124    the type to which it is converting.
10125
10126    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10127    `template' keyword, in a construction like:
10128
10129      T::template f<3>()
10130
10131    In that case `f' is taken to be a template-name, even though there
10132    is no way of knowing for sure.
10133
10134    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10135    name refers to a set of overloaded functions, at least one of which
10136    is a template, or an IDENTIFIER_NODE with the name of the template,
10137    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10138    names are looked up inside uninstantiated templates.  */
10139
10140 static tree
10141 cp_parser_template_name (cp_parser* parser,
10142                          bool template_keyword_p,
10143                          bool check_dependency_p,
10144                          bool is_declaration,
10145                          bool *is_identifier)
10146 {
10147   tree identifier;
10148   tree decl;
10149   tree fns;
10150   cp_token *token = cp_lexer_peek_token (parser->lexer);
10151
10152   /* If the next token is `operator', then we have either an
10153      operator-function-id or a conversion-function-id.  */
10154   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10155     {
10156       /* We don't know whether we're looking at an
10157          operator-function-id or a conversion-function-id.  */
10158       cp_parser_parse_tentatively (parser);
10159       /* Try an operator-function-id.  */
10160       identifier = cp_parser_operator_function_id (parser);
10161       /* If that didn't work, try a conversion-function-id.  */
10162       if (!cp_parser_parse_definitely (parser))
10163         {
10164           cp_parser_error (parser, "expected template-name");
10165           return error_mark_node;
10166         }
10167     }
10168   /* Look for the identifier.  */
10169   else
10170     identifier = cp_parser_identifier (parser);
10171
10172   /* If we didn't find an identifier, we don't have a template-id.  */
10173   if (identifier == error_mark_node)
10174     return error_mark_node;
10175
10176   /* If the name immediately followed the `template' keyword, then it
10177      is a template-name.  However, if the next token is not `<', then
10178      we do not treat it as a template-name, since it is not being used
10179      as part of a template-id.  This enables us to handle constructs
10180      like:
10181
10182        template <typename T> struct S { S(); };
10183        template <typename T> S<T>::S();
10184
10185      correctly.  We would treat `S' as a template -- if it were `S<T>'
10186      -- but we do not if there is no `<'.  */
10187
10188   if (processing_template_decl
10189       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10190     {
10191       /* In a declaration, in a dependent context, we pretend that the
10192          "template" keyword was present in order to improve error
10193          recovery.  For example, given:
10194
10195            template <typename T> void f(T::X<int>);
10196
10197          we want to treat "X<int>" as a template-id.  */
10198       if (is_declaration
10199           && !template_keyword_p
10200           && parser->scope && TYPE_P (parser->scope)
10201           && check_dependency_p
10202           && dependent_type_p (parser->scope)
10203           /* Do not do this for dtors (or ctors), since they never
10204              need the template keyword before their name.  */
10205           && !constructor_name_p (identifier, parser->scope))
10206         {
10207           cp_token_position start = 0;
10208
10209           /* Explain what went wrong.  */
10210           error ("%Hnon-template %qD used as template",
10211                  &token->location, identifier);
10212           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10213                   parser->scope, identifier);
10214           /* If parsing tentatively, find the location of the "<" token.  */
10215           if (cp_parser_simulate_error (parser))
10216             start = cp_lexer_token_position (parser->lexer, true);
10217           /* Parse the template arguments so that we can issue error
10218              messages about them.  */
10219           cp_lexer_consume_token (parser->lexer);
10220           cp_parser_enclosed_template_argument_list (parser);
10221           /* Skip tokens until we find a good place from which to
10222              continue parsing.  */
10223           cp_parser_skip_to_closing_parenthesis (parser,
10224                                                  /*recovering=*/true,
10225                                                  /*or_comma=*/true,
10226                                                  /*consume_paren=*/false);
10227           /* If parsing tentatively, permanently remove the
10228              template argument list.  That will prevent duplicate
10229              error messages from being issued about the missing
10230              "template" keyword.  */
10231           if (start)
10232             cp_lexer_purge_tokens_after (parser->lexer, start);
10233           if (is_identifier)
10234             *is_identifier = true;
10235           return identifier;
10236         }
10237
10238       /* If the "template" keyword is present, then there is generally
10239          no point in doing name-lookup, so we just return IDENTIFIER.
10240          But, if the qualifying scope is non-dependent then we can
10241          (and must) do name-lookup normally.  */
10242       if (template_keyword_p
10243           && (!parser->scope
10244               || (TYPE_P (parser->scope)
10245                   && dependent_type_p (parser->scope))))
10246         return identifier;
10247     }
10248
10249   /* Look up the name.  */
10250   decl = cp_parser_lookup_name (parser, identifier,
10251                                 none_type,
10252                                 /*is_template=*/false,
10253                                 /*is_namespace=*/false,
10254                                 check_dependency_p,
10255                                 /*ambiguous_decls=*/NULL,
10256                                 token->location);
10257   decl = maybe_get_template_decl_from_type_decl (decl);
10258
10259   /* If DECL is a template, then the name was a template-name.  */
10260   if (TREE_CODE (decl) == TEMPLATE_DECL)
10261     ;
10262   else
10263     {
10264       tree fn = NULL_TREE;
10265
10266       /* The standard does not explicitly indicate whether a name that
10267          names a set of overloaded declarations, some of which are
10268          templates, is a template-name.  However, such a name should
10269          be a template-name; otherwise, there is no way to form a
10270          template-id for the overloaded templates.  */
10271       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10272       if (TREE_CODE (fns) == OVERLOAD)
10273         for (fn = fns; fn; fn = OVL_NEXT (fn))
10274           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10275             break;
10276
10277       if (!fn)
10278         {
10279           /* The name does not name a template.  */
10280           cp_parser_error (parser, "expected template-name");
10281           return error_mark_node;
10282         }
10283     }
10284
10285   /* If DECL is dependent, and refers to a function, then just return
10286      its name; we will look it up again during template instantiation.  */
10287   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10288     {
10289       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10290       if (TYPE_P (scope) && dependent_type_p (scope))
10291         return identifier;
10292     }
10293
10294   return decl;
10295 }
10296
10297 /* Parse a template-argument-list.
10298
10299    template-argument-list:
10300      template-argument ... [opt]
10301      template-argument-list , template-argument ... [opt]
10302
10303    Returns a TREE_VEC containing the arguments.  */
10304
10305 static tree
10306 cp_parser_template_argument_list (cp_parser* parser)
10307 {
10308   tree fixed_args[10];
10309   unsigned n_args = 0;
10310   unsigned alloced = 10;
10311   tree *arg_ary = fixed_args;
10312   tree vec;
10313   bool saved_in_template_argument_list_p;
10314   bool saved_ice_p;
10315   bool saved_non_ice_p;
10316
10317   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10318   parser->in_template_argument_list_p = true;
10319   /* Even if the template-id appears in an integral
10320      constant-expression, the contents of the argument list do
10321      not.  */
10322   saved_ice_p = parser->integral_constant_expression_p;
10323   parser->integral_constant_expression_p = false;
10324   saved_non_ice_p = parser->non_integral_constant_expression_p;
10325   parser->non_integral_constant_expression_p = false;
10326   /* Parse the arguments.  */
10327   do
10328     {
10329       tree argument;
10330
10331       if (n_args)
10332         /* Consume the comma.  */
10333         cp_lexer_consume_token (parser->lexer);
10334
10335       /* Parse the template-argument.  */
10336       argument = cp_parser_template_argument (parser);
10337
10338       /* If the next token is an ellipsis, we're expanding a template
10339          argument pack. */
10340       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10341         {
10342           /* Consume the `...' token. */
10343           cp_lexer_consume_token (parser->lexer);
10344
10345           /* Make the argument into a TYPE_PACK_EXPANSION or
10346              EXPR_PACK_EXPANSION. */
10347           argument = make_pack_expansion (argument);
10348         }
10349
10350       if (n_args == alloced)
10351         {
10352           alloced *= 2;
10353
10354           if (arg_ary == fixed_args)
10355             {
10356               arg_ary = XNEWVEC (tree, alloced);
10357               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10358             }
10359           else
10360             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10361         }
10362       arg_ary[n_args++] = argument;
10363     }
10364   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10365
10366   vec = make_tree_vec (n_args);
10367
10368   while (n_args--)
10369     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10370
10371   if (arg_ary != fixed_args)
10372     free (arg_ary);
10373   parser->non_integral_constant_expression_p = saved_non_ice_p;
10374   parser->integral_constant_expression_p = saved_ice_p;
10375   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10376   return vec;
10377 }
10378
10379 /* Parse a template-argument.
10380
10381    template-argument:
10382      assignment-expression
10383      type-id
10384      id-expression
10385
10386    The representation is that of an assignment-expression, type-id, or
10387    id-expression -- except that the qualified id-expression is
10388    evaluated, so that the value returned is either a DECL or an
10389    OVERLOAD.
10390
10391    Although the standard says "assignment-expression", it forbids
10392    throw-expressions or assignments in the template argument.
10393    Therefore, we use "conditional-expression" instead.  */
10394
10395 static tree
10396 cp_parser_template_argument (cp_parser* parser)
10397 {
10398   tree argument;
10399   bool template_p;
10400   bool address_p;
10401   bool maybe_type_id = false;
10402   cp_token *token = NULL, *argument_start_token = NULL;
10403   cp_id_kind idk;
10404
10405   /* There's really no way to know what we're looking at, so we just
10406      try each alternative in order.
10407
10408        [temp.arg]
10409
10410        In a template-argument, an ambiguity between a type-id and an
10411        expression is resolved to a type-id, regardless of the form of
10412        the corresponding template-parameter.
10413
10414      Therefore, we try a type-id first.  */
10415   cp_parser_parse_tentatively (parser);
10416   argument = cp_parser_type_id (parser);
10417   /* If there was no error parsing the type-id but the next token is a
10418      '>>', our behavior depends on which dialect of C++ we're
10419      parsing. In C++98, we probably found a typo for '> >'. But there
10420      are type-id which are also valid expressions. For instance:
10421
10422      struct X { int operator >> (int); };
10423      template <int V> struct Foo {};
10424      Foo<X () >> 5> r;
10425
10426      Here 'X()' is a valid type-id of a function type, but the user just
10427      wanted to write the expression "X() >> 5". Thus, we remember that we
10428      found a valid type-id, but we still try to parse the argument as an
10429      expression to see what happens. 
10430
10431      In C++0x, the '>>' will be considered two separate '>'
10432      tokens.  */
10433   if (!cp_parser_error_occurred (parser)
10434       && cxx_dialect == cxx98
10435       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10436     {
10437       maybe_type_id = true;
10438       cp_parser_abort_tentative_parse (parser);
10439     }
10440   else
10441     {
10442       /* If the next token isn't a `,' or a `>', then this argument wasn't
10443       really finished. This means that the argument is not a valid
10444       type-id.  */
10445       if (!cp_parser_next_token_ends_template_argument_p (parser))
10446         cp_parser_error (parser, "expected template-argument");
10447       /* If that worked, we're done.  */
10448       if (cp_parser_parse_definitely (parser))
10449         return argument;
10450     }
10451   /* We're still not sure what the argument will be.  */
10452   cp_parser_parse_tentatively (parser);
10453   /* Try a template.  */
10454   argument_start_token = cp_lexer_peek_token (parser->lexer);
10455   argument = cp_parser_id_expression (parser,
10456                                       /*template_keyword_p=*/false,
10457                                       /*check_dependency_p=*/true,
10458                                       &template_p,
10459                                       /*declarator_p=*/false,
10460                                       /*optional_p=*/false);
10461   /* If the next token isn't a `,' or a `>', then this argument wasn't
10462      really finished.  */
10463   if (!cp_parser_next_token_ends_template_argument_p (parser))
10464     cp_parser_error (parser, "expected template-argument");
10465   if (!cp_parser_error_occurred (parser))
10466     {
10467       /* Figure out what is being referred to.  If the id-expression
10468          was for a class template specialization, then we will have a
10469          TYPE_DECL at this point.  There is no need to do name lookup
10470          at this point in that case.  */
10471       if (TREE_CODE (argument) != TYPE_DECL)
10472         argument = cp_parser_lookup_name (parser, argument,
10473                                           none_type,
10474                                           /*is_template=*/template_p,
10475                                           /*is_namespace=*/false,
10476                                           /*check_dependency=*/true,
10477                                           /*ambiguous_decls=*/NULL,
10478                                           argument_start_token->location);
10479       if (TREE_CODE (argument) != TEMPLATE_DECL
10480           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10481         cp_parser_error (parser, "expected template-name");
10482     }
10483   if (cp_parser_parse_definitely (parser))
10484     return argument;
10485   /* It must be a non-type argument.  There permitted cases are given
10486      in [temp.arg.nontype]:
10487
10488      -- an integral constant-expression of integral or enumeration
10489         type; or
10490
10491      -- the name of a non-type template-parameter; or
10492
10493      -- the name of an object or function with external linkage...
10494
10495      -- the address of an object or function with external linkage...
10496
10497      -- a pointer to member...  */
10498   /* Look for a non-type template parameter.  */
10499   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10500     {
10501       cp_parser_parse_tentatively (parser);
10502       argument = cp_parser_primary_expression (parser,
10503                                                /*address_p=*/false,
10504                                                /*cast_p=*/false,
10505                                                /*template_arg_p=*/true,
10506                                                &idk);
10507       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10508           || !cp_parser_next_token_ends_template_argument_p (parser))
10509         cp_parser_simulate_error (parser);
10510       if (cp_parser_parse_definitely (parser))
10511         return argument;
10512     }
10513
10514   /* If the next token is "&", the argument must be the address of an
10515      object or function with external linkage.  */
10516   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10517   if (address_p)
10518     cp_lexer_consume_token (parser->lexer);
10519   /* See if we might have an id-expression.  */
10520   token = cp_lexer_peek_token (parser->lexer);
10521   if (token->type == CPP_NAME
10522       || token->keyword == RID_OPERATOR
10523       || token->type == CPP_SCOPE
10524       || token->type == CPP_TEMPLATE_ID
10525       || token->type == CPP_NESTED_NAME_SPECIFIER)
10526     {
10527       cp_parser_parse_tentatively (parser);
10528       argument = cp_parser_primary_expression (parser,
10529                                                address_p,
10530                                                /*cast_p=*/false,
10531                                                /*template_arg_p=*/true,
10532                                                &idk);
10533       if (cp_parser_error_occurred (parser)
10534           || !cp_parser_next_token_ends_template_argument_p (parser))
10535         cp_parser_abort_tentative_parse (parser);
10536       else
10537         {
10538           if (TREE_CODE (argument) == INDIRECT_REF)
10539             {
10540               gcc_assert (REFERENCE_REF_P (argument));
10541               argument = TREE_OPERAND (argument, 0);
10542             }
10543
10544           if (TREE_CODE (argument) == VAR_DECL)
10545             {
10546               /* A variable without external linkage might still be a
10547                  valid constant-expression, so no error is issued here
10548                  if the external-linkage check fails.  */
10549               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10550                 cp_parser_simulate_error (parser);
10551             }
10552           else if (is_overloaded_fn (argument))
10553             /* All overloaded functions are allowed; if the external
10554                linkage test does not pass, an error will be issued
10555                later.  */
10556             ;
10557           else if (address_p
10558                    && (TREE_CODE (argument) == OFFSET_REF
10559                        || TREE_CODE (argument) == SCOPE_REF))
10560             /* A pointer-to-member.  */
10561             ;
10562           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10563             ;
10564           else
10565             cp_parser_simulate_error (parser);
10566
10567           if (cp_parser_parse_definitely (parser))
10568             {
10569               if (address_p)
10570                 argument = build_x_unary_op (ADDR_EXPR, argument,
10571                                              tf_warning_or_error);
10572               return argument;
10573             }
10574         }
10575     }
10576   /* If the argument started with "&", there are no other valid
10577      alternatives at this point.  */
10578   if (address_p)
10579     {
10580       cp_parser_error (parser, "invalid non-type template argument");
10581       return error_mark_node;
10582     }
10583
10584   /* If the argument wasn't successfully parsed as a type-id followed
10585      by '>>', the argument can only be a constant expression now.
10586      Otherwise, we try parsing the constant-expression tentatively,
10587      because the argument could really be a type-id.  */
10588   if (maybe_type_id)
10589     cp_parser_parse_tentatively (parser);
10590   argument = cp_parser_constant_expression (parser,
10591                                             /*allow_non_constant_p=*/false,
10592                                             /*non_constant_p=*/NULL);
10593   argument = fold_non_dependent_expr (argument);
10594   if (!maybe_type_id)
10595     return argument;
10596   if (!cp_parser_next_token_ends_template_argument_p (parser))
10597     cp_parser_error (parser, "expected template-argument");
10598   if (cp_parser_parse_definitely (parser))
10599     return argument;
10600   /* We did our best to parse the argument as a non type-id, but that
10601      was the only alternative that matched (albeit with a '>' after
10602      it). We can assume it's just a typo from the user, and a
10603      diagnostic will then be issued.  */
10604   return cp_parser_type_id (parser);
10605 }
10606
10607 /* Parse an explicit-instantiation.
10608
10609    explicit-instantiation:
10610      template declaration
10611
10612    Although the standard says `declaration', what it really means is:
10613
10614    explicit-instantiation:
10615      template decl-specifier-seq [opt] declarator [opt] ;
10616
10617    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10618    supposed to be allowed.  A defect report has been filed about this
10619    issue.
10620
10621    GNU Extension:
10622
10623    explicit-instantiation:
10624      storage-class-specifier template
10625        decl-specifier-seq [opt] declarator [opt] ;
10626      function-specifier template
10627        decl-specifier-seq [opt] declarator [opt] ;  */
10628
10629 static void
10630 cp_parser_explicit_instantiation (cp_parser* parser)
10631 {
10632   int declares_class_or_enum;
10633   cp_decl_specifier_seq decl_specifiers;
10634   tree extension_specifier = NULL_TREE;
10635   cp_token *token;
10636
10637   /* Look for an (optional) storage-class-specifier or
10638      function-specifier.  */
10639   if (cp_parser_allow_gnu_extensions_p (parser))
10640     {
10641       extension_specifier
10642         = cp_parser_storage_class_specifier_opt (parser);
10643       if (!extension_specifier)
10644         extension_specifier
10645           = cp_parser_function_specifier_opt (parser,
10646                                               /*decl_specs=*/NULL);
10647     }
10648
10649   /* Look for the `template' keyword.  */
10650   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10651   /* Let the front end know that we are processing an explicit
10652      instantiation.  */
10653   begin_explicit_instantiation ();
10654   /* [temp.explicit] says that we are supposed to ignore access
10655      control while processing explicit instantiation directives.  */
10656   push_deferring_access_checks (dk_no_check);
10657   /* Parse a decl-specifier-seq.  */
10658   token = cp_lexer_peek_token (parser->lexer);
10659   cp_parser_decl_specifier_seq (parser,
10660                                 CP_PARSER_FLAGS_OPTIONAL,
10661                                 &decl_specifiers,
10662                                 &declares_class_or_enum);
10663   /* If there was exactly one decl-specifier, and it declared a class,
10664      and there's no declarator, then we have an explicit type
10665      instantiation.  */
10666   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10667     {
10668       tree type;
10669
10670       type = check_tag_decl (&decl_specifiers);
10671       /* Turn access control back on for names used during
10672          template instantiation.  */
10673       pop_deferring_access_checks ();
10674       if (type)
10675         do_type_instantiation (type, extension_specifier,
10676                                /*complain=*/tf_error);
10677     }
10678   else
10679     {
10680       cp_declarator *declarator;
10681       tree decl;
10682
10683       /* Parse the declarator.  */
10684       declarator
10685         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10686                                 /*ctor_dtor_or_conv_p=*/NULL,
10687                                 /*parenthesized_p=*/NULL,
10688                                 /*member_p=*/false);
10689       if (declares_class_or_enum & 2)
10690         cp_parser_check_for_definition_in_return_type (declarator,
10691                                                        decl_specifiers.type,
10692                                                        decl_specifiers.type_location);
10693       if (declarator != cp_error_declarator)
10694         {
10695           decl = grokdeclarator (declarator, &decl_specifiers,
10696                                  NORMAL, 0, &decl_specifiers.attributes);
10697           /* Turn access control back on for names used during
10698              template instantiation.  */
10699           pop_deferring_access_checks ();
10700           /* Do the explicit instantiation.  */
10701           do_decl_instantiation (decl, extension_specifier);
10702         }
10703       else
10704         {
10705           pop_deferring_access_checks ();
10706           /* Skip the body of the explicit instantiation.  */
10707           cp_parser_skip_to_end_of_statement (parser);
10708         }
10709     }
10710   /* We're done with the instantiation.  */
10711   end_explicit_instantiation ();
10712
10713   cp_parser_consume_semicolon_at_end_of_statement (parser);
10714 }
10715
10716 /* Parse an explicit-specialization.
10717
10718    explicit-specialization:
10719      template < > declaration
10720
10721    Although the standard says `declaration', what it really means is:
10722
10723    explicit-specialization:
10724      template <> decl-specifier [opt] init-declarator [opt] ;
10725      template <> function-definition
10726      template <> explicit-specialization
10727      template <> template-declaration  */
10728
10729 static void
10730 cp_parser_explicit_specialization (cp_parser* parser)
10731 {
10732   bool need_lang_pop;
10733   cp_token *token = cp_lexer_peek_token (parser->lexer);
10734
10735   /* Look for the `template' keyword.  */
10736   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10737   /* Look for the `<'.  */
10738   cp_parser_require (parser, CPP_LESS, "%<<%>");
10739   /* Look for the `>'.  */
10740   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10741   /* We have processed another parameter list.  */
10742   ++parser->num_template_parameter_lists;
10743   /* [temp]
10744
10745      A template ... explicit specialization ... shall not have C
10746      linkage.  */
10747   if (current_lang_name == lang_name_c)
10748     {
10749       error ("%Htemplate specialization with C linkage", &token->location);
10750       /* Give it C++ linkage to avoid confusing other parts of the
10751          front end.  */
10752       push_lang_context (lang_name_cplusplus);
10753       need_lang_pop = true;
10754     }
10755   else
10756     need_lang_pop = false;
10757   /* Let the front end know that we are beginning a specialization.  */
10758   if (!begin_specialization ())
10759     {
10760       end_specialization ();
10761       cp_parser_skip_to_end_of_block_or_statement (parser);
10762       return;
10763     }
10764
10765   /* If the next keyword is `template', we need to figure out whether
10766      or not we're looking a template-declaration.  */
10767   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10768     {
10769       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10770           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10771         cp_parser_template_declaration_after_export (parser,
10772                                                      /*member_p=*/false);
10773       else
10774         cp_parser_explicit_specialization (parser);
10775     }
10776   else
10777     /* Parse the dependent declaration.  */
10778     cp_parser_single_declaration (parser,
10779                                   /*checks=*/NULL,
10780                                   /*member_p=*/false,
10781                                   /*explicit_specialization_p=*/true,
10782                                   /*friend_p=*/NULL);
10783   /* We're done with the specialization.  */
10784   end_specialization ();
10785   /* For the erroneous case of a template with C linkage, we pushed an
10786      implicit C++ linkage scope; exit that scope now.  */
10787   if (need_lang_pop)
10788     pop_lang_context ();
10789   /* We're done with this parameter list.  */
10790   --parser->num_template_parameter_lists;
10791 }
10792
10793 /* Parse a type-specifier.
10794
10795    type-specifier:
10796      simple-type-specifier
10797      class-specifier
10798      enum-specifier
10799      elaborated-type-specifier
10800      cv-qualifier
10801
10802    GNU Extension:
10803
10804    type-specifier:
10805      __complex__
10806
10807    Returns a representation of the type-specifier.  For a
10808    class-specifier, enum-specifier, or elaborated-type-specifier, a
10809    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10810
10811    The parser flags FLAGS is used to control type-specifier parsing.
10812
10813    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10814    in a decl-specifier-seq.
10815
10816    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10817    class-specifier, enum-specifier, or elaborated-type-specifier, then
10818    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10819    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10820    zero.
10821
10822    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10823    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10824    is set to FALSE.  */
10825
10826 static tree
10827 cp_parser_type_specifier (cp_parser* parser,
10828                           cp_parser_flags flags,
10829                           cp_decl_specifier_seq *decl_specs,
10830                           bool is_declaration,
10831                           int* declares_class_or_enum,
10832                           bool* is_cv_qualifier)
10833 {
10834   tree type_spec = NULL_TREE;
10835   cp_token *token;
10836   enum rid keyword;
10837   cp_decl_spec ds = ds_last;
10838
10839   /* Assume this type-specifier does not declare a new type.  */
10840   if (declares_class_or_enum)
10841     *declares_class_or_enum = 0;
10842   /* And that it does not specify a cv-qualifier.  */
10843   if (is_cv_qualifier)
10844     *is_cv_qualifier = false;
10845   /* Peek at the next token.  */
10846   token = cp_lexer_peek_token (parser->lexer);
10847
10848   /* If we're looking at a keyword, we can use that to guide the
10849      production we choose.  */
10850   keyword = token->keyword;
10851   switch (keyword)
10852     {
10853     case RID_ENUM:
10854       /* Look for the enum-specifier.  */
10855       type_spec = cp_parser_enum_specifier (parser);
10856       /* If that worked, we're done.  */
10857       if (type_spec)
10858         {
10859           if (declares_class_or_enum)
10860             *declares_class_or_enum = 2;
10861           if (decl_specs)
10862             cp_parser_set_decl_spec_type (decl_specs,
10863                                           type_spec,
10864                                           token->location,
10865                                           /*user_defined_p=*/true);
10866           return type_spec;
10867         }
10868       else
10869         goto elaborated_type_specifier;
10870
10871       /* Any of these indicate either a class-specifier, or an
10872          elaborated-type-specifier.  */
10873     case RID_CLASS:
10874     case RID_STRUCT:
10875     case RID_UNION:
10876       /* Parse tentatively so that we can back up if we don't find a
10877          class-specifier.  */
10878       cp_parser_parse_tentatively (parser);
10879       /* Look for the class-specifier.  */
10880       type_spec = cp_parser_class_specifier (parser);
10881       /* If that worked, we're done.  */
10882       if (cp_parser_parse_definitely (parser))
10883         {
10884           if (declares_class_or_enum)
10885             *declares_class_or_enum = 2;
10886           if (decl_specs)
10887             cp_parser_set_decl_spec_type (decl_specs,
10888                                           type_spec,
10889                                           token->location,
10890                                           /*user_defined_p=*/true);
10891           return type_spec;
10892         }
10893
10894       /* Fall through.  */
10895     elaborated_type_specifier:
10896       /* We're declaring (not defining) a class or enum.  */
10897       if (declares_class_or_enum)
10898         *declares_class_or_enum = 1;
10899
10900       /* Fall through.  */
10901     case RID_TYPENAME:
10902       /* Look for an elaborated-type-specifier.  */
10903       type_spec
10904         = (cp_parser_elaborated_type_specifier
10905            (parser,
10906             decl_specs && decl_specs->specs[(int) ds_friend],
10907             is_declaration));
10908       if (decl_specs)
10909         cp_parser_set_decl_spec_type (decl_specs,
10910                                       type_spec,
10911                                       token->location,
10912                                       /*user_defined_p=*/true);
10913       return type_spec;
10914
10915     case RID_CONST:
10916       ds = ds_const;
10917       if (is_cv_qualifier)
10918         *is_cv_qualifier = true;
10919       break;
10920
10921     case RID_VOLATILE:
10922       ds = ds_volatile;
10923       if (is_cv_qualifier)
10924         *is_cv_qualifier = true;
10925       break;
10926
10927     case RID_RESTRICT:
10928       ds = ds_restrict;
10929       if (is_cv_qualifier)
10930         *is_cv_qualifier = true;
10931       break;
10932
10933     case RID_COMPLEX:
10934       /* The `__complex__' keyword is a GNU extension.  */
10935       ds = ds_complex;
10936       break;
10937
10938     default:
10939       break;
10940     }
10941
10942   /* Handle simple keywords.  */
10943   if (ds != ds_last)
10944     {
10945       if (decl_specs)
10946         {
10947           ++decl_specs->specs[(int)ds];
10948           decl_specs->any_specifiers_p = true;
10949         }
10950       return cp_lexer_consume_token (parser->lexer)->u.value;
10951     }
10952
10953   /* If we do not already have a type-specifier, assume we are looking
10954      at a simple-type-specifier.  */
10955   type_spec = cp_parser_simple_type_specifier (parser,
10956                                                decl_specs,
10957                                                flags);
10958
10959   /* If we didn't find a type-specifier, and a type-specifier was not
10960      optional in this context, issue an error message.  */
10961   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10962     {
10963       cp_parser_error (parser, "expected type specifier");
10964       return error_mark_node;
10965     }
10966
10967   return type_spec;
10968 }
10969
10970 /* Parse a simple-type-specifier.
10971
10972    simple-type-specifier:
10973      :: [opt] nested-name-specifier [opt] type-name
10974      :: [opt] nested-name-specifier template template-id
10975      char
10976      wchar_t
10977      bool
10978      short
10979      int
10980      long
10981      signed
10982      unsigned
10983      float
10984      double
10985      void
10986
10987    C++0x Extension:
10988
10989    simple-type-specifier:
10990      auto
10991      decltype ( expression )   
10992      char16_t
10993      char32_t
10994
10995    GNU Extension:
10996
10997    simple-type-specifier:
10998      __typeof__ unary-expression
10999      __typeof__ ( type-id )
11000
11001    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11002    appropriately updated.  */
11003
11004 static tree
11005 cp_parser_simple_type_specifier (cp_parser* parser,
11006                                  cp_decl_specifier_seq *decl_specs,
11007                                  cp_parser_flags flags)
11008 {
11009   tree type = NULL_TREE;
11010   cp_token *token;
11011
11012   /* Peek at the next token.  */
11013   token = cp_lexer_peek_token (parser->lexer);
11014
11015   /* If we're looking at a keyword, things are easy.  */
11016   switch (token->keyword)
11017     {
11018     case RID_CHAR:
11019       if (decl_specs)
11020         decl_specs->explicit_char_p = true;
11021       type = char_type_node;
11022       break;
11023     case RID_CHAR16:
11024       type = char16_type_node;
11025       break;
11026     case RID_CHAR32:
11027       type = char32_type_node;
11028       break;
11029     case RID_WCHAR:
11030       type = wchar_type_node;
11031       break;
11032     case RID_BOOL:
11033       type = boolean_type_node;
11034       break;
11035     case RID_SHORT:
11036       if (decl_specs)
11037         ++decl_specs->specs[(int) ds_short];
11038       type = short_integer_type_node;
11039       break;
11040     case RID_INT:
11041       if (decl_specs)
11042         decl_specs->explicit_int_p = true;
11043       type = integer_type_node;
11044       break;
11045     case RID_LONG:
11046       if (decl_specs)
11047         ++decl_specs->specs[(int) ds_long];
11048       type = long_integer_type_node;
11049       break;
11050     case RID_SIGNED:
11051       if (decl_specs)
11052         ++decl_specs->specs[(int) ds_signed];
11053       type = integer_type_node;
11054       break;
11055     case RID_UNSIGNED:
11056       if (decl_specs)
11057         ++decl_specs->specs[(int) ds_unsigned];
11058       type = unsigned_type_node;
11059       break;
11060     case RID_FLOAT:
11061       type = float_type_node;
11062       break;
11063     case RID_DOUBLE:
11064       type = double_type_node;
11065       break;
11066     case RID_VOID:
11067       type = void_type_node;
11068       break;
11069       
11070     case RID_AUTO:
11071       maybe_warn_cpp0x ("C++0x auto");
11072       type = make_auto ();
11073       break;
11074
11075     case RID_DECLTYPE:
11076       /* Parse the `decltype' type.  */
11077       type = cp_parser_decltype (parser);
11078
11079       if (decl_specs)
11080         cp_parser_set_decl_spec_type (decl_specs, type,
11081                                       token->location,
11082                                       /*user_defined_p=*/true);
11083
11084       return type;
11085
11086     case RID_TYPEOF:
11087       /* Consume the `typeof' token.  */
11088       cp_lexer_consume_token (parser->lexer);
11089       /* Parse the operand to `typeof'.  */
11090       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11091       /* If it is not already a TYPE, take its type.  */
11092       if (!TYPE_P (type))
11093         type = finish_typeof (type);
11094
11095       if (decl_specs)
11096         cp_parser_set_decl_spec_type (decl_specs, type,
11097                                       token->location,
11098                                       /*user_defined_p=*/true);
11099
11100       return type;
11101
11102     default:
11103       break;
11104     }
11105
11106   /* If the type-specifier was for a built-in type, we're done.  */
11107   if (type)
11108     {
11109       tree id;
11110
11111       /* Record the type.  */
11112       if (decl_specs
11113           && (token->keyword != RID_SIGNED
11114               && token->keyword != RID_UNSIGNED
11115               && token->keyword != RID_SHORT
11116               && token->keyword != RID_LONG))
11117         cp_parser_set_decl_spec_type (decl_specs,
11118                                       type,
11119                                       token->location,
11120                                       /*user_defined=*/false);
11121       if (decl_specs)
11122         decl_specs->any_specifiers_p = true;
11123
11124       /* Consume the token.  */
11125       id = cp_lexer_consume_token (parser->lexer)->u.value;
11126
11127       /* There is no valid C++ program where a non-template type is
11128          followed by a "<".  That usually indicates that the user thought
11129          that the type was a template.  */
11130       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11131
11132       return TYPE_NAME (type);
11133     }
11134
11135   /* The type-specifier must be a user-defined type.  */
11136   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11137     {
11138       bool qualified_p;
11139       bool global_p;
11140
11141       /* Don't gobble tokens or issue error messages if this is an
11142          optional type-specifier.  */
11143       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11144         cp_parser_parse_tentatively (parser);
11145
11146       /* Look for the optional `::' operator.  */
11147       global_p
11148         = (cp_parser_global_scope_opt (parser,
11149                                        /*current_scope_valid_p=*/false)
11150            != NULL_TREE);
11151       /* Look for the nested-name specifier.  */
11152       qualified_p
11153         = (cp_parser_nested_name_specifier_opt (parser,
11154                                                 /*typename_keyword_p=*/false,
11155                                                 /*check_dependency_p=*/true,
11156                                                 /*type_p=*/false,
11157                                                 /*is_declaration=*/false)
11158            != NULL_TREE);
11159       token = cp_lexer_peek_token (parser->lexer);
11160       /* If we have seen a nested-name-specifier, and the next token
11161          is `template', then we are using the template-id production.  */
11162       if (parser->scope
11163           && cp_parser_optional_template_keyword (parser))
11164         {
11165           /* Look for the template-id.  */
11166           type = cp_parser_template_id (parser,
11167                                         /*template_keyword_p=*/true,
11168                                         /*check_dependency_p=*/true,
11169                                         /*is_declaration=*/false);
11170           /* If the template-id did not name a type, we are out of
11171              luck.  */
11172           if (TREE_CODE (type) != TYPE_DECL)
11173             {
11174               cp_parser_error (parser, "expected template-id for type");
11175               type = NULL_TREE;
11176             }
11177         }
11178       /* Otherwise, look for a type-name.  */
11179       else
11180         type = cp_parser_type_name (parser);
11181       /* Keep track of all name-lookups performed in class scopes.  */
11182       if (type
11183           && !global_p
11184           && !qualified_p
11185           && TREE_CODE (type) == TYPE_DECL
11186           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11187         maybe_note_name_used_in_class (DECL_NAME (type), type);
11188       /* If it didn't work out, we don't have a TYPE.  */
11189       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11190           && !cp_parser_parse_definitely (parser))
11191         type = NULL_TREE;
11192       if (type && decl_specs)
11193         cp_parser_set_decl_spec_type (decl_specs, type,
11194                                       token->location,
11195                                       /*user_defined=*/true);
11196     }
11197
11198   /* If we didn't get a type-name, issue an error message.  */
11199   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11200     {
11201       cp_parser_error (parser, "expected type-name");
11202       return error_mark_node;
11203     }
11204
11205   /* There is no valid C++ program where a non-template type is
11206      followed by a "<".  That usually indicates that the user thought
11207      that the type was a template.  */
11208   if (type && type != error_mark_node)
11209     {
11210       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11211          If it is, then the '<'...'>' enclose protocol names rather than
11212          template arguments, and so everything is fine.  */
11213       if (c_dialect_objc ()
11214           && (objc_is_id (type) || objc_is_class_name (type)))
11215         {
11216           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11217           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11218
11219           /* Clobber the "unqualified" type previously entered into
11220              DECL_SPECS with the new, improved protocol-qualified version.  */
11221           if (decl_specs)
11222             decl_specs->type = qual_type;
11223
11224           return qual_type;
11225         }
11226
11227       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11228                                                token->location);
11229     }
11230
11231   return type;
11232 }
11233
11234 /* Parse a type-name.
11235
11236    type-name:
11237      class-name
11238      enum-name
11239      typedef-name
11240
11241    enum-name:
11242      identifier
11243
11244    typedef-name:
11245      identifier
11246
11247    Returns a TYPE_DECL for the type.  */
11248
11249 static tree
11250 cp_parser_type_name (cp_parser* parser)
11251 {
11252   tree type_decl;
11253
11254   /* We can't know yet whether it is a class-name or not.  */
11255   cp_parser_parse_tentatively (parser);
11256   /* Try a class-name.  */
11257   type_decl = cp_parser_class_name (parser,
11258                                     /*typename_keyword_p=*/false,
11259                                     /*template_keyword_p=*/false,
11260                                     none_type,
11261                                     /*check_dependency_p=*/true,
11262                                     /*class_head_p=*/false,
11263                                     /*is_declaration=*/false);
11264   /* If it's not a class-name, keep looking.  */
11265   if (!cp_parser_parse_definitely (parser))
11266     {
11267       /* It must be a typedef-name or an enum-name.  */
11268       return cp_parser_nonclass_name (parser);
11269     }
11270
11271   return type_decl;
11272 }
11273
11274 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11275
11276    enum-name:
11277      identifier
11278
11279    typedef-name:
11280      identifier
11281
11282    Returns a TYPE_DECL for the type.  */
11283
11284 static tree
11285 cp_parser_nonclass_name (cp_parser* parser)
11286 {
11287   tree type_decl;
11288   tree identifier;
11289
11290   cp_token *token = cp_lexer_peek_token (parser->lexer);
11291   identifier = cp_parser_identifier (parser);
11292   if (identifier == error_mark_node)
11293     return error_mark_node;
11294
11295   /* Look up the type-name.  */
11296   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11297
11298   if (TREE_CODE (type_decl) != TYPE_DECL
11299       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11300     {
11301       /* See if this is an Objective-C type.  */
11302       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11303       tree type = objc_get_protocol_qualified_type (identifier, protos);
11304       if (type)
11305         type_decl = TYPE_NAME (type);
11306     }
11307   
11308   /* Issue an error if we did not find a type-name.  */
11309   if (TREE_CODE (type_decl) != TYPE_DECL)
11310     {
11311       if (!cp_parser_simulate_error (parser))
11312         cp_parser_name_lookup_error (parser, identifier, type_decl,
11313                                      "is not a type", token->location);
11314       return error_mark_node;
11315     }
11316   /* Remember that the name was used in the definition of the
11317      current class so that we can check later to see if the
11318      meaning would have been different after the class was
11319      entirely defined.  */
11320   else if (type_decl != error_mark_node
11321            && !parser->scope)
11322     maybe_note_name_used_in_class (identifier, type_decl);
11323   
11324   return type_decl;
11325 }
11326
11327 /* Parse an elaborated-type-specifier.  Note that the grammar given
11328    here incorporates the resolution to DR68.
11329
11330    elaborated-type-specifier:
11331      class-key :: [opt] nested-name-specifier [opt] identifier
11332      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11333      enum-key :: [opt] nested-name-specifier [opt] identifier
11334      typename :: [opt] nested-name-specifier identifier
11335      typename :: [opt] nested-name-specifier template [opt]
11336        template-id
11337
11338    GNU extension:
11339
11340    elaborated-type-specifier:
11341      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11342      class-key attributes :: [opt] nested-name-specifier [opt]
11343                template [opt] template-id
11344      enum attributes :: [opt] nested-name-specifier [opt] identifier
11345
11346    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11347    declared `friend'.  If IS_DECLARATION is TRUE, then this
11348    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11349    something is being declared.
11350
11351    Returns the TYPE specified.  */
11352
11353 static tree
11354 cp_parser_elaborated_type_specifier (cp_parser* parser,
11355                                      bool is_friend,
11356                                      bool is_declaration)
11357 {
11358   enum tag_types tag_type;
11359   tree identifier;
11360   tree type = NULL_TREE;
11361   tree attributes = NULL_TREE;
11362   cp_token *token = NULL;
11363
11364   /* See if we're looking at the `enum' keyword.  */
11365   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11366     {
11367       /* Consume the `enum' token.  */
11368       cp_lexer_consume_token (parser->lexer);
11369       /* Remember that it's an enumeration type.  */
11370       tag_type = enum_type;
11371       /* Parse the optional `struct' or `class' key (for C++0x scoped
11372          enums).  */
11373       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11374           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11375         {
11376           if (cxx_dialect == cxx98)
11377             maybe_warn_cpp0x ("scoped enums");
11378
11379           /* Consume the `struct' or `class'.  */
11380           cp_lexer_consume_token (parser->lexer);
11381         }
11382       /* Parse the attributes.  */
11383       attributes = cp_parser_attributes_opt (parser);
11384     }
11385   /* Or, it might be `typename'.  */
11386   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11387                                            RID_TYPENAME))
11388     {
11389       /* Consume the `typename' token.  */
11390       cp_lexer_consume_token (parser->lexer);
11391       /* Remember that it's a `typename' type.  */
11392       tag_type = typename_type;
11393       /* The `typename' keyword is only allowed in templates.  */
11394       if (!processing_template_decl)
11395         permerror (input_location, "using %<typename%> outside of template");
11396     }
11397   /* Otherwise it must be a class-key.  */
11398   else
11399     {
11400       tag_type = cp_parser_class_key (parser);
11401       if (tag_type == none_type)
11402         return error_mark_node;
11403       /* Parse the attributes.  */
11404       attributes = cp_parser_attributes_opt (parser);
11405     }
11406
11407   /* Look for the `::' operator.  */
11408   cp_parser_global_scope_opt (parser,
11409                               /*current_scope_valid_p=*/false);
11410   /* Look for the nested-name-specifier.  */
11411   if (tag_type == typename_type)
11412     {
11413       if (!cp_parser_nested_name_specifier (parser,
11414                                            /*typename_keyword_p=*/true,
11415                                            /*check_dependency_p=*/true,
11416                                            /*type_p=*/true,
11417                                             is_declaration))
11418         return error_mark_node;
11419     }
11420   else
11421     /* Even though `typename' is not present, the proposed resolution
11422        to Core Issue 180 says that in `class A<T>::B', `B' should be
11423        considered a type-name, even if `A<T>' is dependent.  */
11424     cp_parser_nested_name_specifier_opt (parser,
11425                                          /*typename_keyword_p=*/true,
11426                                          /*check_dependency_p=*/true,
11427                                          /*type_p=*/true,
11428                                          is_declaration);
11429  /* For everything but enumeration types, consider a template-id.
11430     For an enumeration type, consider only a plain identifier.  */
11431   if (tag_type != enum_type)
11432     {
11433       bool template_p = false;
11434       tree decl;
11435
11436       /* Allow the `template' keyword.  */
11437       template_p = cp_parser_optional_template_keyword (parser);
11438       /* If we didn't see `template', we don't know if there's a
11439          template-id or not.  */
11440       if (!template_p)
11441         cp_parser_parse_tentatively (parser);
11442       /* Parse the template-id.  */
11443       token = cp_lexer_peek_token (parser->lexer);
11444       decl = cp_parser_template_id (parser, template_p,
11445                                     /*check_dependency_p=*/true,
11446                                     is_declaration);
11447       /* If we didn't find a template-id, look for an ordinary
11448          identifier.  */
11449       if (!template_p && !cp_parser_parse_definitely (parser))
11450         ;
11451       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11452          in effect, then we must assume that, upon instantiation, the
11453          template will correspond to a class.  */
11454       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11455                && tag_type == typename_type)
11456         type = make_typename_type (parser->scope, decl,
11457                                    typename_type,
11458                                    /*complain=*/tf_error);
11459       else
11460         type = TREE_TYPE (decl);
11461     }
11462
11463   if (!type)
11464     {
11465       token = cp_lexer_peek_token (parser->lexer);
11466       identifier = cp_parser_identifier (parser);
11467
11468       if (identifier == error_mark_node)
11469         {
11470           parser->scope = NULL_TREE;
11471           return error_mark_node;
11472         }
11473
11474       /* For a `typename', we needn't call xref_tag.  */
11475       if (tag_type == typename_type
11476           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11477         return cp_parser_make_typename_type (parser, parser->scope,
11478                                              identifier,
11479                                              token->location);
11480       /* Look up a qualified name in the usual way.  */
11481       if (parser->scope)
11482         {
11483           tree decl;
11484           tree ambiguous_decls;
11485
11486           decl = cp_parser_lookup_name (parser, identifier,
11487                                         tag_type,
11488                                         /*is_template=*/false,
11489                                         /*is_namespace=*/false,
11490                                         /*check_dependency=*/true,
11491                                         &ambiguous_decls,
11492                                         token->location);
11493
11494           /* If the lookup was ambiguous, an error will already have been
11495              issued.  */
11496           if (ambiguous_decls)
11497             return error_mark_node;
11498
11499           /* If we are parsing friend declaration, DECL may be a
11500              TEMPLATE_DECL tree node here.  However, we need to check
11501              whether this TEMPLATE_DECL results in valid code.  Consider
11502              the following example:
11503
11504                namespace N {
11505                  template <class T> class C {};
11506                }
11507                class X {
11508                  template <class T> friend class N::C; // #1, valid code
11509                };
11510                template <class T> class Y {
11511                  friend class N::C;                    // #2, invalid code
11512                };
11513
11514              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11515              name lookup of `N::C'.  We see that friend declaration must
11516              be template for the code to be valid.  Note that
11517              processing_template_decl does not work here since it is
11518              always 1 for the above two cases.  */
11519
11520           decl = (cp_parser_maybe_treat_template_as_class
11521                   (decl, /*tag_name_p=*/is_friend
11522                          && parser->num_template_parameter_lists));
11523
11524           if (TREE_CODE (decl) != TYPE_DECL)
11525             {
11526               cp_parser_diagnose_invalid_type_name (parser,
11527                                                     parser->scope,
11528                                                     identifier,
11529                                                     token->location);
11530               return error_mark_node;
11531             }
11532
11533           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11534             {
11535               bool allow_template = (parser->num_template_parameter_lists
11536                                       || DECL_SELF_REFERENCE_P (decl));
11537               type = check_elaborated_type_specifier (tag_type, decl, 
11538                                                       allow_template);
11539
11540               if (type == error_mark_node)
11541                 return error_mark_node;
11542             }
11543
11544           /* Forward declarations of nested types, such as
11545
11546                class C1::C2;
11547                class C1::C2::C3;
11548
11549              are invalid unless all components preceding the final '::'
11550              are complete.  If all enclosing types are complete, these
11551              declarations become merely pointless.
11552
11553              Invalid forward declarations of nested types are errors
11554              caught elsewhere in parsing.  Those that are pointless arrive
11555              here.  */
11556
11557           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11558               && !is_friend && !processing_explicit_instantiation)
11559             warning (0, "declaration %qD does not declare anything", decl);
11560
11561           type = TREE_TYPE (decl);
11562         }
11563       else
11564         {
11565           /* An elaborated-type-specifier sometimes introduces a new type and
11566              sometimes names an existing type.  Normally, the rule is that it
11567              introduces a new type only if there is not an existing type of
11568              the same name already in scope.  For example, given:
11569
11570                struct S {};
11571                void f() { struct S s; }
11572
11573              the `struct S' in the body of `f' is the same `struct S' as in
11574              the global scope; the existing definition is used.  However, if
11575              there were no global declaration, this would introduce a new
11576              local class named `S'.
11577
11578              An exception to this rule applies to the following code:
11579
11580                namespace N { struct S; }
11581
11582              Here, the elaborated-type-specifier names a new type
11583              unconditionally; even if there is already an `S' in the
11584              containing scope this declaration names a new type.
11585              This exception only applies if the elaborated-type-specifier
11586              forms the complete declaration:
11587
11588                [class.name]
11589
11590                A declaration consisting solely of `class-key identifier ;' is
11591                either a redeclaration of the name in the current scope or a
11592                forward declaration of the identifier as a class name.  It
11593                introduces the name into the current scope.
11594
11595              We are in this situation precisely when the next token is a `;'.
11596
11597              An exception to the exception is that a `friend' declaration does
11598              *not* name a new type; i.e., given:
11599
11600                struct S { friend struct T; };
11601
11602              `T' is not a new type in the scope of `S'.
11603
11604              Also, `new struct S' or `sizeof (struct S)' never results in the
11605              definition of a new type; a new type can only be declared in a
11606              declaration context.  */
11607
11608           tag_scope ts;
11609           bool template_p;
11610
11611           if (is_friend)
11612             /* Friends have special name lookup rules.  */
11613             ts = ts_within_enclosing_non_class;
11614           else if (is_declaration
11615                    && cp_lexer_next_token_is (parser->lexer,
11616                                               CPP_SEMICOLON))
11617             /* This is a `class-key identifier ;' */
11618             ts = ts_current;
11619           else
11620             ts = ts_global;
11621
11622           template_p =
11623             (parser->num_template_parameter_lists
11624              && (cp_parser_next_token_starts_class_definition_p (parser)
11625                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11626           /* An unqualified name was used to reference this type, so
11627              there were no qualifying templates.  */
11628           if (!cp_parser_check_template_parameters (parser,
11629                                                     /*num_templates=*/0,
11630                                                     token->location))
11631             return error_mark_node;
11632           type = xref_tag (tag_type, identifier, ts, template_p);
11633         }
11634     }
11635
11636   if (type == error_mark_node)
11637     return error_mark_node;
11638
11639   /* Allow attributes on forward declarations of classes.  */
11640   if (attributes)
11641     {
11642       if (TREE_CODE (type) == TYPENAME_TYPE)
11643         warning (OPT_Wattributes,
11644                  "attributes ignored on uninstantiated type");
11645       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11646                && ! processing_explicit_instantiation)
11647         warning (OPT_Wattributes,
11648                  "attributes ignored on template instantiation");
11649       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11650         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11651       else
11652         warning (OPT_Wattributes,
11653                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11654     }
11655
11656   if (tag_type != enum_type)
11657     cp_parser_check_class_key (tag_type, type);
11658
11659   /* A "<" cannot follow an elaborated type specifier.  If that
11660      happens, the user was probably trying to form a template-id.  */
11661   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11662
11663   return type;
11664 }
11665
11666 /* Parse an enum-specifier.
11667
11668    enum-specifier:
11669      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11670
11671    enum-key:
11672      enum
11673      enum class   [C++0x]
11674      enum struct  [C++0x]
11675
11676    enum-base:   [C++0x]
11677      : type-specifier-seq
11678
11679    GNU Extensions:
11680      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11681        { enumerator-list [opt] }attributes[opt]
11682
11683    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11684    if the token stream isn't an enum-specifier after all.  */
11685
11686 static tree
11687 cp_parser_enum_specifier (cp_parser* parser)
11688 {
11689   tree identifier;
11690   tree type;
11691   tree attributes;
11692   bool scoped_enum_p = false;
11693   tree underlying_type = NULL_TREE;
11694
11695   /* Parse tentatively so that we can back up if we don't find a
11696      enum-specifier.  */
11697   cp_parser_parse_tentatively (parser);
11698
11699   /* Caller guarantees that the current token is 'enum', an identifier
11700      possibly follows, and the token after that is an opening brace.
11701      If we don't have an identifier, fabricate an anonymous name for
11702      the enumeration being defined.  */
11703   cp_lexer_consume_token (parser->lexer);
11704
11705   /* Parse the "class" or "struct", which indicates a scoped
11706      enumeration type in C++0x.  */
11707   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11708       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11709     {
11710       if (cxx_dialect == cxx98)
11711         maybe_warn_cpp0x ("scoped enums");
11712
11713       /* Consume the `struct' or `class' token.  */
11714       cp_lexer_consume_token (parser->lexer);
11715
11716       scoped_enum_p = true;
11717     }
11718       
11719   attributes = cp_parser_attributes_opt (parser);
11720
11721   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11722     identifier = cp_parser_identifier (parser);
11723   else
11724     identifier = make_anon_name ();
11725
11726   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11727   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11728     {
11729       cp_decl_specifier_seq type_specifiers;
11730
11731       if (cxx_dialect == cxx98)
11732         maybe_warn_cpp0x ("scoped enums");
11733
11734       /* Consume the `:'.  */
11735       cp_lexer_consume_token (parser->lexer);
11736
11737       /* Parse the type-specifier-seq.  */
11738       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11739                                     &type_specifiers);
11740       if (type_specifiers.type == error_mark_node)
11741         return error_mark_node;
11742      
11743       /* If that didn't work, stop.  */
11744       if (type_specifiers.type != error_mark_node)
11745         {
11746           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11747                                             /*initialized=*/0, NULL);
11748           if (underlying_type == error_mark_node)
11749             underlying_type = NULL_TREE;
11750         }
11751       else
11752         cp_parser_error (parser, "expected underlying type of enumeration");
11753     }
11754
11755   /* Look for the `{' but don't consume it yet.  */
11756   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11757     cp_parser_simulate_error (parser);
11758
11759   if (!cp_parser_parse_definitely (parser))
11760     return NULL_TREE;
11761
11762   /* Issue an error message if type-definitions are forbidden here.  */
11763   if (!cp_parser_check_type_definition (parser))
11764     type = error_mark_node;
11765   else
11766     /* Create the new type.  We do this before consuming the opening
11767        brace so the enum will be recorded as being on the line of its
11768        tag (or the 'enum' keyword, if there is no tag).  */
11769     type = start_enum (identifier, underlying_type, scoped_enum_p);
11770   
11771   /* Consume the opening brace.  */
11772   cp_lexer_consume_token (parser->lexer);
11773
11774   if (type == error_mark_node)
11775     {
11776       cp_parser_skip_to_end_of_block_or_statement (parser);
11777       return error_mark_node;
11778     }
11779
11780   /* If the next token is not '}', then there are some enumerators.  */
11781   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11782     cp_parser_enumerator_list (parser, type);
11783
11784   /* Consume the final '}'.  */
11785   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11786
11787   /* Look for trailing attributes to apply to this enumeration, and
11788      apply them if appropriate.  */
11789   if (cp_parser_allow_gnu_extensions_p (parser))
11790     {
11791       tree trailing_attr = cp_parser_attributes_opt (parser);
11792       cplus_decl_attributes (&type,
11793                              trailing_attr,
11794                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11795     }
11796
11797   /* Finish up the enumeration.  */
11798   finish_enum (type);
11799
11800   return type;
11801 }
11802
11803 /* Parse an enumerator-list.  The enumerators all have the indicated
11804    TYPE.
11805
11806    enumerator-list:
11807      enumerator-definition
11808      enumerator-list , enumerator-definition  */
11809
11810 static void
11811 cp_parser_enumerator_list (cp_parser* parser, tree type)
11812 {
11813   while (true)
11814     {
11815       /* Parse an enumerator-definition.  */
11816       cp_parser_enumerator_definition (parser, type);
11817
11818       /* If the next token is not a ',', we've reached the end of
11819          the list.  */
11820       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11821         break;
11822       /* Otherwise, consume the `,' and keep going.  */
11823       cp_lexer_consume_token (parser->lexer);
11824       /* If the next token is a `}', there is a trailing comma.  */
11825       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11826         {
11827           if (!in_system_header)
11828             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11829           break;
11830         }
11831     }
11832 }
11833
11834 /* Parse an enumerator-definition.  The enumerator has the indicated
11835    TYPE.
11836
11837    enumerator-definition:
11838      enumerator
11839      enumerator = constant-expression
11840
11841    enumerator:
11842      identifier  */
11843
11844 static void
11845 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11846 {
11847   tree identifier;
11848   tree value;
11849
11850   /* Look for the identifier.  */
11851   identifier = cp_parser_identifier (parser);
11852   if (identifier == error_mark_node)
11853     return;
11854
11855   /* If the next token is an '=', then there is an explicit value.  */
11856   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11857     {
11858       /* Consume the `=' token.  */
11859       cp_lexer_consume_token (parser->lexer);
11860       /* Parse the value.  */
11861       value = cp_parser_constant_expression (parser,
11862                                              /*allow_non_constant_p=*/false,
11863                                              NULL);
11864     }
11865   else
11866     value = NULL_TREE;
11867
11868   /* Create the enumerator.  */
11869   build_enumerator (identifier, value, type);
11870 }
11871
11872 /* Parse a namespace-name.
11873
11874    namespace-name:
11875      original-namespace-name
11876      namespace-alias
11877
11878    Returns the NAMESPACE_DECL for the namespace.  */
11879
11880 static tree
11881 cp_parser_namespace_name (cp_parser* parser)
11882 {
11883   tree identifier;
11884   tree namespace_decl;
11885
11886   cp_token *token = cp_lexer_peek_token (parser->lexer);
11887
11888   /* Get the name of the namespace.  */
11889   identifier = cp_parser_identifier (parser);
11890   if (identifier == error_mark_node)
11891     return error_mark_node;
11892
11893   /* Look up the identifier in the currently active scope.  Look only
11894      for namespaces, due to:
11895
11896        [basic.lookup.udir]
11897
11898        When looking up a namespace-name in a using-directive or alias
11899        definition, only namespace names are considered.
11900
11901      And:
11902
11903        [basic.lookup.qual]
11904
11905        During the lookup of a name preceding the :: scope resolution
11906        operator, object, function, and enumerator names are ignored.
11907
11908      (Note that cp_parser_qualifying_entity only calls this
11909      function if the token after the name is the scope resolution
11910      operator.)  */
11911   namespace_decl = cp_parser_lookup_name (parser, identifier,
11912                                           none_type,
11913                                           /*is_template=*/false,
11914                                           /*is_namespace=*/true,
11915                                           /*check_dependency=*/true,
11916                                           /*ambiguous_decls=*/NULL,
11917                                           token->location);
11918   /* If it's not a namespace, issue an error.  */
11919   if (namespace_decl == error_mark_node
11920       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11921     {
11922       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11923         error ("%H%qD is not a namespace-name", &token->location, identifier);
11924       cp_parser_error (parser, "expected namespace-name");
11925       namespace_decl = error_mark_node;
11926     }
11927
11928   return namespace_decl;
11929 }
11930
11931 /* Parse a namespace-definition.
11932
11933    namespace-definition:
11934      named-namespace-definition
11935      unnamed-namespace-definition
11936
11937    named-namespace-definition:
11938      original-namespace-definition
11939      extension-namespace-definition
11940
11941    original-namespace-definition:
11942      namespace identifier { namespace-body }
11943
11944    extension-namespace-definition:
11945      namespace original-namespace-name { namespace-body }
11946
11947    unnamed-namespace-definition:
11948      namespace { namespace-body } */
11949
11950 static void
11951 cp_parser_namespace_definition (cp_parser* parser)
11952 {
11953   tree identifier, attribs;
11954   bool has_visibility;
11955   bool is_inline;
11956
11957   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11958     {
11959       is_inline = true;
11960       cp_lexer_consume_token (parser->lexer);
11961     }
11962   else
11963     is_inline = false;
11964
11965   /* Look for the `namespace' keyword.  */
11966   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11967
11968   /* Get the name of the namespace.  We do not attempt to distinguish
11969      between an original-namespace-definition and an
11970      extension-namespace-definition at this point.  The semantic
11971      analysis routines are responsible for that.  */
11972   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11973     identifier = cp_parser_identifier (parser);
11974   else
11975     identifier = NULL_TREE;
11976
11977   /* Parse any specified attributes.  */
11978   attribs = cp_parser_attributes_opt (parser);
11979
11980   /* Look for the `{' to start the namespace.  */
11981   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11982   /* Start the namespace.  */
11983   push_namespace (identifier);
11984
11985   /* "inline namespace" is equivalent to a stub namespace definition
11986      followed by a strong using directive.  */
11987   if (is_inline)
11988     {
11989       tree name_space = current_namespace;
11990       /* Set up namespace association.  */
11991       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11992         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11993                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11994       /* Import the contents of the inline namespace.  */
11995       pop_namespace ();
11996       do_using_directive (name_space);
11997       push_namespace (identifier);
11998     }
11999
12000   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12001
12002   /* Parse the body of the namespace.  */
12003   cp_parser_namespace_body (parser);
12004
12005 #ifdef HANDLE_PRAGMA_VISIBILITY
12006   if (has_visibility)
12007     pop_visibility ();
12008 #endif
12009
12010   /* Finish the namespace.  */
12011   pop_namespace ();
12012   /* Look for the final `}'.  */
12013   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12014 }
12015
12016 /* Parse a namespace-body.
12017
12018    namespace-body:
12019      declaration-seq [opt]  */
12020
12021 static void
12022 cp_parser_namespace_body (cp_parser* parser)
12023 {
12024   cp_parser_declaration_seq_opt (parser);
12025 }
12026
12027 /* Parse a namespace-alias-definition.
12028
12029    namespace-alias-definition:
12030      namespace identifier = qualified-namespace-specifier ;  */
12031
12032 static void
12033 cp_parser_namespace_alias_definition (cp_parser* parser)
12034 {
12035   tree identifier;
12036   tree namespace_specifier;
12037
12038   cp_token *token = cp_lexer_peek_token (parser->lexer);
12039
12040   /* Look for the `namespace' keyword.  */
12041   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12042   /* Look for the identifier.  */
12043   identifier = cp_parser_identifier (parser);
12044   if (identifier == error_mark_node)
12045     return;
12046   /* Look for the `=' token.  */
12047   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12048       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12049     {
12050       error ("%H%<namespace%> definition is not allowed here", &token->location);
12051       /* Skip the definition.  */
12052       cp_lexer_consume_token (parser->lexer);
12053       if (cp_parser_skip_to_closing_brace (parser))
12054         cp_lexer_consume_token (parser->lexer);
12055       return;
12056     }
12057   cp_parser_require (parser, CPP_EQ, "%<=%>");
12058   /* Look for the qualified-namespace-specifier.  */
12059   namespace_specifier
12060     = cp_parser_qualified_namespace_specifier (parser);
12061   /* Look for the `;' token.  */
12062   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12063
12064   /* Register the alias in the symbol table.  */
12065   do_namespace_alias (identifier, namespace_specifier);
12066 }
12067
12068 /* Parse a qualified-namespace-specifier.
12069
12070    qualified-namespace-specifier:
12071      :: [opt] nested-name-specifier [opt] namespace-name
12072
12073    Returns a NAMESPACE_DECL corresponding to the specified
12074    namespace.  */
12075
12076 static tree
12077 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12078 {
12079   /* Look for the optional `::'.  */
12080   cp_parser_global_scope_opt (parser,
12081                               /*current_scope_valid_p=*/false);
12082
12083   /* Look for the optional nested-name-specifier.  */
12084   cp_parser_nested_name_specifier_opt (parser,
12085                                        /*typename_keyword_p=*/false,
12086                                        /*check_dependency_p=*/true,
12087                                        /*type_p=*/false,
12088                                        /*is_declaration=*/true);
12089
12090   return cp_parser_namespace_name (parser);
12091 }
12092
12093 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12094    access declaration.
12095
12096    using-declaration:
12097      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12098      using :: unqualified-id ;  
12099
12100    access-declaration:
12101      qualified-id ;  
12102
12103    */
12104
12105 static bool
12106 cp_parser_using_declaration (cp_parser* parser, 
12107                              bool access_declaration_p)
12108 {
12109   cp_token *token;
12110   bool typename_p = false;
12111   bool global_scope_p;
12112   tree decl;
12113   tree identifier;
12114   tree qscope;
12115
12116   if (access_declaration_p)
12117     cp_parser_parse_tentatively (parser);
12118   else
12119     {
12120       /* Look for the `using' keyword.  */
12121       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12122       
12123       /* Peek at the next token.  */
12124       token = cp_lexer_peek_token (parser->lexer);
12125       /* See if it's `typename'.  */
12126       if (token->keyword == RID_TYPENAME)
12127         {
12128           /* Remember that we've seen it.  */
12129           typename_p = true;
12130           /* Consume the `typename' token.  */
12131           cp_lexer_consume_token (parser->lexer);
12132         }
12133     }
12134
12135   /* Look for the optional global scope qualification.  */
12136   global_scope_p
12137     = (cp_parser_global_scope_opt (parser,
12138                                    /*current_scope_valid_p=*/false)
12139        != NULL_TREE);
12140
12141   /* If we saw `typename', or didn't see `::', then there must be a
12142      nested-name-specifier present.  */
12143   if (typename_p || !global_scope_p)
12144     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12145                                               /*check_dependency_p=*/true,
12146                                               /*type_p=*/false,
12147                                               /*is_declaration=*/true);
12148   /* Otherwise, we could be in either of the two productions.  In that
12149      case, treat the nested-name-specifier as optional.  */
12150   else
12151     qscope = cp_parser_nested_name_specifier_opt (parser,
12152                                                   /*typename_keyword_p=*/false,
12153                                                   /*check_dependency_p=*/true,
12154                                                   /*type_p=*/false,
12155                                                   /*is_declaration=*/true);
12156   if (!qscope)
12157     qscope = global_namespace;
12158
12159   if (access_declaration_p && cp_parser_error_occurred (parser))
12160     /* Something has already gone wrong; there's no need to parse
12161        further.  Since an error has occurred, the return value of
12162        cp_parser_parse_definitely will be false, as required.  */
12163     return cp_parser_parse_definitely (parser);
12164
12165   token = cp_lexer_peek_token (parser->lexer);
12166   /* Parse the unqualified-id.  */
12167   identifier = cp_parser_unqualified_id (parser,
12168                                          /*template_keyword_p=*/false,
12169                                          /*check_dependency_p=*/true,
12170                                          /*declarator_p=*/true,
12171                                          /*optional_p=*/false);
12172
12173   if (access_declaration_p)
12174     {
12175       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12176         cp_parser_simulate_error (parser);
12177       if (!cp_parser_parse_definitely (parser))
12178         return false;
12179     }
12180
12181   /* The function we call to handle a using-declaration is different
12182      depending on what scope we are in.  */
12183   if (qscope == error_mark_node || identifier == error_mark_node)
12184     ;
12185   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12186            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12187     /* [namespace.udecl]
12188
12189        A using declaration shall not name a template-id.  */
12190     error ("%Ha template-id may not appear in a using-declaration",
12191             &token->location);
12192   else
12193     {
12194       if (at_class_scope_p ())
12195         {
12196           /* Create the USING_DECL.  */
12197           decl = do_class_using_decl (parser->scope, identifier);
12198
12199           if (check_for_bare_parameter_packs (decl))
12200             return false;
12201           else
12202             /* Add it to the list of members in this class.  */
12203             finish_member_declaration (decl);
12204         }
12205       else
12206         {
12207           decl = cp_parser_lookup_name_simple (parser,
12208                                                identifier,
12209                                                token->location);
12210           if (decl == error_mark_node)
12211             cp_parser_name_lookup_error (parser, identifier,
12212                                          decl, NULL,
12213                                          token->location);
12214           else if (check_for_bare_parameter_packs (decl))
12215             return false;
12216           else if (!at_namespace_scope_p ())
12217             do_local_using_decl (decl, qscope, identifier);
12218           else
12219             do_toplevel_using_decl (decl, qscope, identifier);
12220         }
12221     }
12222
12223   /* Look for the final `;'.  */
12224   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12225   
12226   return true;
12227 }
12228
12229 /* Parse a using-directive.
12230
12231    using-directive:
12232      using namespace :: [opt] nested-name-specifier [opt]
12233        namespace-name ;  */
12234
12235 static void
12236 cp_parser_using_directive (cp_parser* parser)
12237 {
12238   tree namespace_decl;
12239   tree attribs;
12240
12241   /* Look for the `using' keyword.  */
12242   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12243   /* And the `namespace' keyword.  */
12244   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12245   /* Look for the optional `::' operator.  */
12246   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12247   /* And the optional nested-name-specifier.  */
12248   cp_parser_nested_name_specifier_opt (parser,
12249                                        /*typename_keyword_p=*/false,
12250                                        /*check_dependency_p=*/true,
12251                                        /*type_p=*/false,
12252                                        /*is_declaration=*/true);
12253   /* Get the namespace being used.  */
12254   namespace_decl = cp_parser_namespace_name (parser);
12255   /* And any specified attributes.  */
12256   attribs = cp_parser_attributes_opt (parser);
12257   /* Update the symbol table.  */
12258   parse_using_directive (namespace_decl, attribs);
12259   /* Look for the final `;'.  */
12260   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12261 }
12262
12263 /* Parse an asm-definition.
12264
12265    asm-definition:
12266      asm ( string-literal ) ;
12267
12268    GNU Extension:
12269
12270    asm-definition:
12271      asm volatile [opt] ( string-literal ) ;
12272      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12273      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12274                           : asm-operand-list [opt] ) ;
12275      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12276                           : asm-operand-list [opt]
12277                           : asm-operand-list [opt] ) ;  */
12278
12279 static void
12280 cp_parser_asm_definition (cp_parser* parser)
12281 {
12282   tree string;
12283   tree outputs = NULL_TREE;
12284   tree inputs = NULL_TREE;
12285   tree clobbers = NULL_TREE;
12286   tree asm_stmt;
12287   bool volatile_p = false;
12288   bool extended_p = false;
12289   bool invalid_inputs_p = false;
12290   bool invalid_outputs_p = false;
12291
12292   /* Look for the `asm' keyword.  */
12293   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12294   /* See if the next token is `volatile'.  */
12295   if (cp_parser_allow_gnu_extensions_p (parser)
12296       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12297     {
12298       /* Remember that we saw the `volatile' keyword.  */
12299       volatile_p = true;
12300       /* Consume the token.  */
12301       cp_lexer_consume_token (parser->lexer);
12302     }
12303   /* Look for the opening `('.  */
12304   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12305     return;
12306   /* Look for the string.  */
12307   string = cp_parser_string_literal (parser, false, false);
12308   if (string == error_mark_node)
12309     {
12310       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12311                                              /*consume_paren=*/true);
12312       return;
12313     }
12314
12315   /* If we're allowing GNU extensions, check for the extended assembly
12316      syntax.  Unfortunately, the `:' tokens need not be separated by
12317      a space in C, and so, for compatibility, we tolerate that here
12318      too.  Doing that means that we have to treat the `::' operator as
12319      two `:' tokens.  */
12320   if (cp_parser_allow_gnu_extensions_p (parser)
12321       && parser->in_function_body
12322       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12323           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12324     {
12325       bool inputs_p = false;
12326       bool clobbers_p = false;
12327
12328       /* The extended syntax was used.  */
12329       extended_p = true;
12330
12331       /* Look for outputs.  */
12332       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12333         {
12334           /* Consume the `:'.  */
12335           cp_lexer_consume_token (parser->lexer);
12336           /* Parse the output-operands.  */
12337           if (cp_lexer_next_token_is_not (parser->lexer,
12338                                           CPP_COLON)
12339               && cp_lexer_next_token_is_not (parser->lexer,
12340                                              CPP_SCOPE)
12341               && cp_lexer_next_token_is_not (parser->lexer,
12342                                              CPP_CLOSE_PAREN))
12343             outputs = cp_parser_asm_operand_list (parser);
12344
12345             if (outputs == error_mark_node)
12346               invalid_outputs_p = true;
12347         }
12348       /* If the next token is `::', there are no outputs, and the
12349          next token is the beginning of the inputs.  */
12350       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12351         /* The inputs are coming next.  */
12352         inputs_p = true;
12353
12354       /* Look for inputs.  */
12355       if (inputs_p
12356           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12357         {
12358           /* Consume the `:' or `::'.  */
12359           cp_lexer_consume_token (parser->lexer);
12360           /* Parse the output-operands.  */
12361           if (cp_lexer_next_token_is_not (parser->lexer,
12362                                           CPP_COLON)
12363               && cp_lexer_next_token_is_not (parser->lexer,
12364                                              CPP_CLOSE_PAREN))
12365             inputs = cp_parser_asm_operand_list (parser);
12366
12367             if (inputs == error_mark_node)
12368               invalid_inputs_p = true;
12369         }
12370       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12371         /* The clobbers are coming next.  */
12372         clobbers_p = true;
12373
12374       /* Look for clobbers.  */
12375       if (clobbers_p
12376           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12377         {
12378           /* Consume the `:' or `::'.  */
12379           cp_lexer_consume_token (parser->lexer);
12380           /* Parse the clobbers.  */
12381           if (cp_lexer_next_token_is_not (parser->lexer,
12382                                           CPP_CLOSE_PAREN))
12383             clobbers = cp_parser_asm_clobber_list (parser);
12384         }
12385     }
12386   /* Look for the closing `)'.  */
12387   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12388     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12389                                            /*consume_paren=*/true);
12390   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12391
12392   if (!invalid_inputs_p && !invalid_outputs_p)
12393     {
12394       /* Create the ASM_EXPR.  */
12395       if (parser->in_function_body)
12396         {
12397           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12398                                       inputs, clobbers);
12399           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12400           if (!extended_p)
12401             {
12402               tree temp = asm_stmt;
12403               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12404                 temp = TREE_OPERAND (temp, 0);
12405
12406               ASM_INPUT_P (temp) = 1;
12407             }
12408         }
12409       else
12410         cgraph_add_asm_node (string);
12411     }
12412 }
12413
12414 /* Declarators [gram.dcl.decl] */
12415
12416 /* Parse an init-declarator.
12417
12418    init-declarator:
12419      declarator initializer [opt]
12420
12421    GNU Extension:
12422
12423    init-declarator:
12424      declarator asm-specification [opt] attributes [opt] initializer [opt]
12425
12426    function-definition:
12427      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12428        function-body
12429      decl-specifier-seq [opt] declarator function-try-block
12430
12431    GNU Extension:
12432
12433    function-definition:
12434      __extension__ function-definition
12435
12436    The DECL_SPECIFIERS apply to this declarator.  Returns a
12437    representation of the entity declared.  If MEMBER_P is TRUE, then
12438    this declarator appears in a class scope.  The new DECL created by
12439    this declarator is returned.
12440
12441    The CHECKS are access checks that should be performed once we know
12442    what entity is being declared (and, therefore, what classes have
12443    befriended it).
12444
12445    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12446    for a function-definition here as well.  If the declarator is a
12447    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12448    be TRUE upon return.  By that point, the function-definition will
12449    have been completely parsed.
12450
12451    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12452    is FALSE.  */
12453
12454 static tree
12455 cp_parser_init_declarator (cp_parser* parser,
12456                            cp_decl_specifier_seq *decl_specifiers,
12457                            VEC (deferred_access_check,gc)* checks,
12458                            bool function_definition_allowed_p,
12459                            bool member_p,
12460                            int declares_class_or_enum,
12461                            bool* function_definition_p)
12462 {
12463   cp_token *token = NULL, *asm_spec_start_token = NULL,
12464            *attributes_start_token = NULL;
12465   cp_declarator *declarator;
12466   tree prefix_attributes;
12467   tree attributes;
12468   tree asm_specification;
12469   tree initializer;
12470   tree decl = NULL_TREE;
12471   tree scope;
12472   int is_initialized;
12473   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12474      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12475      "(...)".  */
12476   enum cpp_ttype initialization_kind;
12477   bool is_direct_init = false;
12478   bool is_non_constant_init;
12479   int ctor_dtor_or_conv_p;
12480   bool friend_p;
12481   tree pushed_scope = NULL;
12482
12483   /* Gather the attributes that were provided with the
12484      decl-specifiers.  */
12485   prefix_attributes = decl_specifiers->attributes;
12486
12487   /* Assume that this is not the declarator for a function
12488      definition.  */
12489   if (function_definition_p)
12490     *function_definition_p = false;
12491
12492   /* Defer access checks while parsing the declarator; we cannot know
12493      what names are accessible until we know what is being
12494      declared.  */
12495   resume_deferring_access_checks ();
12496
12497   /* Parse the declarator.  */
12498   token = cp_lexer_peek_token (parser->lexer);
12499   declarator
12500     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12501                             &ctor_dtor_or_conv_p,
12502                             /*parenthesized_p=*/NULL,
12503                             /*member_p=*/false);
12504   /* Gather up the deferred checks.  */
12505   stop_deferring_access_checks ();
12506
12507   /* If the DECLARATOR was erroneous, there's no need to go
12508      further.  */
12509   if (declarator == cp_error_declarator)
12510     return error_mark_node;
12511
12512   /* Check that the number of template-parameter-lists is OK.  */
12513   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12514                                                        token->location))
12515     return error_mark_node;
12516
12517   if (declares_class_or_enum & 2)
12518     cp_parser_check_for_definition_in_return_type (declarator,
12519                                                    decl_specifiers->type,
12520                                                    decl_specifiers->type_location);
12521
12522   /* Figure out what scope the entity declared by the DECLARATOR is
12523      located in.  `grokdeclarator' sometimes changes the scope, so
12524      we compute it now.  */
12525   scope = get_scope_of_declarator (declarator);
12526
12527   /* If we're allowing GNU extensions, look for an asm-specification
12528      and attributes.  */
12529   if (cp_parser_allow_gnu_extensions_p (parser))
12530     {
12531       /* Look for an asm-specification.  */
12532       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12533       asm_specification = cp_parser_asm_specification_opt (parser);
12534       /* And attributes.  */
12535       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12536       attributes = cp_parser_attributes_opt (parser);
12537     }
12538   else
12539     {
12540       asm_specification = NULL_TREE;
12541       attributes = NULL_TREE;
12542     }
12543
12544   /* Peek at the next token.  */
12545   token = cp_lexer_peek_token (parser->lexer);
12546   /* Check to see if the token indicates the start of a
12547      function-definition.  */
12548   if (function_declarator_p (declarator)
12549       && cp_parser_token_starts_function_definition_p (token))
12550     {
12551       if (!function_definition_allowed_p)
12552         {
12553           /* If a function-definition should not appear here, issue an
12554              error message.  */
12555           cp_parser_error (parser,
12556                            "a function-definition is not allowed here");
12557           return error_mark_node;
12558         }
12559       else
12560         {
12561           /* Neither attributes nor an asm-specification are allowed
12562              on a function-definition.  */
12563           if (asm_specification)
12564             error ("%Han asm-specification is not allowed "
12565                    "on a function-definition",
12566                    &asm_spec_start_token->location);
12567           if (attributes)
12568             error ("%Hattributes are not allowed on a function-definition",
12569                    &attributes_start_token->location);
12570           /* This is a function-definition.  */
12571           *function_definition_p = true;
12572
12573           /* Parse the function definition.  */
12574           if (member_p)
12575             decl = cp_parser_save_member_function_body (parser,
12576                                                         decl_specifiers,
12577                                                         declarator,
12578                                                         prefix_attributes);
12579           else
12580             decl
12581               = (cp_parser_function_definition_from_specifiers_and_declarator
12582                  (parser, decl_specifiers, prefix_attributes, declarator));
12583
12584           return decl;
12585         }
12586     }
12587
12588   /* [dcl.dcl]
12589
12590      Only in function declarations for constructors, destructors, and
12591      type conversions can the decl-specifier-seq be omitted.
12592
12593      We explicitly postpone this check past the point where we handle
12594      function-definitions because we tolerate function-definitions
12595      that are missing their return types in some modes.  */
12596   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12597     {
12598       cp_parser_error (parser,
12599                        "expected constructor, destructor, or type conversion");
12600       return error_mark_node;
12601     }
12602
12603   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12604   if (token->type == CPP_EQ
12605       || token->type == CPP_OPEN_PAREN
12606       || token->type == CPP_OPEN_BRACE)
12607     {
12608       is_initialized = 1;
12609       initialization_kind = token->type;
12610
12611       if (token->type == CPP_EQ
12612           && function_declarator_p (declarator))
12613         {
12614           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12615           if (t2->keyword == RID_DEFAULT)
12616             is_initialized = 2;
12617           else if (t2->keyword == RID_DELETE)
12618             is_initialized = 3;
12619         }
12620     }
12621   else
12622     {
12623       /* If the init-declarator isn't initialized and isn't followed by a
12624          `,' or `;', it's not a valid init-declarator.  */
12625       if (token->type != CPP_COMMA
12626           && token->type != CPP_SEMICOLON)
12627         {
12628           cp_parser_error (parser, "expected initializer");
12629           return error_mark_node;
12630         }
12631       is_initialized = 0;
12632       initialization_kind = CPP_EOF;
12633     }
12634
12635   /* Because start_decl has side-effects, we should only call it if we
12636      know we're going ahead.  By this point, we know that we cannot
12637      possibly be looking at any other construct.  */
12638   cp_parser_commit_to_tentative_parse (parser);
12639
12640   /* If the decl specifiers were bad, issue an error now that we're
12641      sure this was intended to be a declarator.  Then continue
12642      declaring the variable(s), as int, to try to cut down on further
12643      errors.  */
12644   if (decl_specifiers->any_specifiers_p
12645       && decl_specifiers->type == error_mark_node)
12646     {
12647       cp_parser_error (parser, "invalid type in declaration");
12648       decl_specifiers->type = integer_type_node;
12649     }
12650
12651   /* Check to see whether or not this declaration is a friend.  */
12652   friend_p = cp_parser_friend_p (decl_specifiers);
12653
12654   /* Enter the newly declared entry in the symbol table.  If we're
12655      processing a declaration in a class-specifier, we wait until
12656      after processing the initializer.  */
12657   if (!member_p)
12658     {
12659       if (parser->in_unbraced_linkage_specification_p)
12660         decl_specifiers->storage_class = sc_extern;
12661       decl = start_decl (declarator, decl_specifiers,
12662                          is_initialized, attributes, prefix_attributes,
12663                          &pushed_scope);
12664     }
12665   else if (scope)
12666     /* Enter the SCOPE.  That way unqualified names appearing in the
12667        initializer will be looked up in SCOPE.  */
12668     pushed_scope = push_scope (scope);
12669
12670   /* Perform deferred access control checks, now that we know in which
12671      SCOPE the declared entity resides.  */
12672   if (!member_p && decl)
12673     {
12674       tree saved_current_function_decl = NULL_TREE;
12675
12676       /* If the entity being declared is a function, pretend that we
12677          are in its scope.  If it is a `friend', it may have access to
12678          things that would not otherwise be accessible.  */
12679       if (TREE_CODE (decl) == FUNCTION_DECL)
12680         {
12681           saved_current_function_decl = current_function_decl;
12682           current_function_decl = decl;
12683         }
12684
12685       /* Perform access checks for template parameters.  */
12686       cp_parser_perform_template_parameter_access_checks (checks);
12687
12688       /* Perform the access control checks for the declarator and the
12689          decl-specifiers.  */
12690       perform_deferred_access_checks ();
12691
12692       /* Restore the saved value.  */
12693       if (TREE_CODE (decl) == FUNCTION_DECL)
12694         current_function_decl = saved_current_function_decl;
12695     }
12696
12697   /* Parse the initializer.  */
12698   initializer = NULL_TREE;
12699   is_direct_init = false;
12700   is_non_constant_init = true;
12701   if (is_initialized)
12702     {
12703       if (function_declarator_p (declarator))
12704         {
12705           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12706            if (initialization_kind == CPP_EQ)
12707              initializer = cp_parser_pure_specifier (parser);
12708            else
12709              {
12710                /* If the declaration was erroneous, we don't really
12711                   know what the user intended, so just silently
12712                   consume the initializer.  */
12713                if (decl != error_mark_node)
12714                  error ("%Hinitializer provided for function",
12715                         &initializer_start_token->location);
12716                cp_parser_skip_to_closing_parenthesis (parser,
12717                                                       /*recovering=*/true,
12718                                                       /*or_comma=*/false,
12719                                                       /*consume_paren=*/true);
12720              }
12721         }
12722       else
12723         initializer = cp_parser_initializer (parser,
12724                                              &is_direct_init,
12725                                              &is_non_constant_init);
12726     }
12727
12728   /* The old parser allows attributes to appear after a parenthesized
12729      initializer.  Mark Mitchell proposed removing this functionality
12730      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12731      attributes -- but ignores them.  */
12732   if (cp_parser_allow_gnu_extensions_p (parser)
12733       && initialization_kind == CPP_OPEN_PAREN)
12734     if (cp_parser_attributes_opt (parser))
12735       warning (OPT_Wattributes,
12736                "attributes after parenthesized initializer ignored");
12737
12738   /* For an in-class declaration, use `grokfield' to create the
12739      declaration.  */
12740   if (member_p)
12741     {
12742       if (pushed_scope)
12743         {
12744           pop_scope (pushed_scope);
12745           pushed_scope = false;
12746         }
12747       decl = grokfield (declarator, decl_specifiers,
12748                         initializer, !is_non_constant_init,
12749                         /*asmspec=*/NULL_TREE,
12750                         prefix_attributes);
12751       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12752         cp_parser_save_default_args (parser, decl);
12753     }
12754
12755   /* Finish processing the declaration.  But, skip friend
12756      declarations.  */
12757   if (!friend_p && decl && decl != error_mark_node)
12758     {
12759       cp_finish_decl (decl,
12760                       initializer, !is_non_constant_init,
12761                       asm_specification,
12762                       /* If the initializer is in parentheses, then this is
12763                          a direct-initialization, which means that an
12764                          `explicit' constructor is OK.  Otherwise, an
12765                          `explicit' constructor cannot be used.  */
12766                       ((is_direct_init || !is_initialized)
12767                        ? 0 : LOOKUP_ONLYCONVERTING));
12768     }
12769   else if ((cxx_dialect != cxx98) && friend_p
12770            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12771     /* Core issue #226 (C++0x only): A default template-argument
12772        shall not be specified in a friend class template
12773        declaration. */
12774     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12775                              /*is_partial=*/0, /*is_friend_decl=*/1);
12776
12777   if (!friend_p && pushed_scope)
12778     pop_scope (pushed_scope);
12779
12780   return decl;
12781 }
12782
12783 /* Parse a declarator.
12784
12785    declarator:
12786      direct-declarator
12787      ptr-operator declarator
12788
12789    abstract-declarator:
12790      ptr-operator abstract-declarator [opt]
12791      direct-abstract-declarator
12792
12793    GNU Extensions:
12794
12795    declarator:
12796      attributes [opt] direct-declarator
12797      attributes [opt] ptr-operator declarator
12798
12799    abstract-declarator:
12800      attributes [opt] ptr-operator abstract-declarator [opt]
12801      attributes [opt] direct-abstract-declarator
12802
12803    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12804    detect constructor, destructor or conversion operators. It is set
12805    to -1 if the declarator is a name, and +1 if it is a
12806    function. Otherwise it is set to zero. Usually you just want to
12807    test for >0, but internally the negative value is used.
12808
12809    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12810    a decl-specifier-seq unless it declares a constructor, destructor,
12811    or conversion.  It might seem that we could check this condition in
12812    semantic analysis, rather than parsing, but that makes it difficult
12813    to handle something like `f()'.  We want to notice that there are
12814    no decl-specifiers, and therefore realize that this is an
12815    expression, not a declaration.)
12816
12817    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12818    the declarator is a direct-declarator of the form "(...)".
12819
12820    MEMBER_P is true iff this declarator is a member-declarator.  */
12821
12822 static cp_declarator *
12823 cp_parser_declarator (cp_parser* parser,
12824                       cp_parser_declarator_kind dcl_kind,
12825                       int* ctor_dtor_or_conv_p,
12826                       bool* parenthesized_p,
12827                       bool member_p)
12828 {
12829   cp_token *token;
12830   cp_declarator *declarator;
12831   enum tree_code code;
12832   cp_cv_quals cv_quals;
12833   tree class_type;
12834   tree attributes = NULL_TREE;
12835
12836   /* Assume this is not a constructor, destructor, or type-conversion
12837      operator.  */
12838   if (ctor_dtor_or_conv_p)
12839     *ctor_dtor_or_conv_p = 0;
12840
12841   if (cp_parser_allow_gnu_extensions_p (parser))
12842     attributes = cp_parser_attributes_opt (parser);
12843
12844   /* Peek at the next token.  */
12845   token = cp_lexer_peek_token (parser->lexer);
12846
12847   /* Check for the ptr-operator production.  */
12848   cp_parser_parse_tentatively (parser);
12849   /* Parse the ptr-operator.  */
12850   code = cp_parser_ptr_operator (parser,
12851                                  &class_type,
12852                                  &cv_quals);
12853   /* If that worked, then we have a ptr-operator.  */
12854   if (cp_parser_parse_definitely (parser))
12855     {
12856       /* If a ptr-operator was found, then this declarator was not
12857          parenthesized.  */
12858       if (parenthesized_p)
12859         *parenthesized_p = true;
12860       /* The dependent declarator is optional if we are parsing an
12861          abstract-declarator.  */
12862       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12863         cp_parser_parse_tentatively (parser);
12864
12865       /* Parse the dependent declarator.  */
12866       declarator = cp_parser_declarator (parser, dcl_kind,
12867                                          /*ctor_dtor_or_conv_p=*/NULL,
12868                                          /*parenthesized_p=*/NULL,
12869                                          /*member_p=*/false);
12870
12871       /* If we are parsing an abstract-declarator, we must handle the
12872          case where the dependent declarator is absent.  */
12873       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12874           && !cp_parser_parse_definitely (parser))
12875         declarator = NULL;
12876
12877       declarator = cp_parser_make_indirect_declarator
12878         (code, class_type, cv_quals, declarator);
12879     }
12880   /* Everything else is a direct-declarator.  */
12881   else
12882     {
12883       if (parenthesized_p)
12884         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12885                                                    CPP_OPEN_PAREN);
12886       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12887                                                 ctor_dtor_or_conv_p,
12888                                                 member_p);
12889     }
12890
12891   if (attributes && declarator && declarator != cp_error_declarator)
12892     declarator->attributes = attributes;
12893
12894   return declarator;
12895 }
12896
12897 /* Parse a direct-declarator or direct-abstract-declarator.
12898
12899    direct-declarator:
12900      declarator-id
12901      direct-declarator ( parameter-declaration-clause )
12902        cv-qualifier-seq [opt]
12903        exception-specification [opt]
12904      direct-declarator [ constant-expression [opt] ]
12905      ( declarator )
12906
12907    direct-abstract-declarator:
12908      direct-abstract-declarator [opt]
12909        ( parameter-declaration-clause )
12910        cv-qualifier-seq [opt]
12911        exception-specification [opt]
12912      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12913      ( abstract-declarator )
12914
12915    Returns a representation of the declarator.  DCL_KIND is
12916    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12917    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12918    we are parsing a direct-declarator.  It is
12919    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12920    of ambiguity we prefer an abstract declarator, as per
12921    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12922    cp_parser_declarator.  */
12923
12924 static cp_declarator *
12925 cp_parser_direct_declarator (cp_parser* parser,
12926                              cp_parser_declarator_kind dcl_kind,
12927                              int* ctor_dtor_or_conv_p,
12928                              bool member_p)
12929 {
12930   cp_token *token;
12931   cp_declarator *declarator = NULL;
12932   tree scope = NULL_TREE;
12933   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12934   bool saved_in_declarator_p = parser->in_declarator_p;
12935   bool first = true;
12936   tree pushed_scope = NULL_TREE;
12937
12938   while (true)
12939     {
12940       /* Peek at the next token.  */
12941       token = cp_lexer_peek_token (parser->lexer);
12942       if (token->type == CPP_OPEN_PAREN)
12943         {
12944           /* This is either a parameter-declaration-clause, or a
12945              parenthesized declarator. When we know we are parsing a
12946              named declarator, it must be a parenthesized declarator
12947              if FIRST is true. For instance, `(int)' is a
12948              parameter-declaration-clause, with an omitted
12949              direct-abstract-declarator. But `((*))', is a
12950              parenthesized abstract declarator. Finally, when T is a
12951              template parameter `(T)' is a
12952              parameter-declaration-clause, and not a parenthesized
12953              named declarator.
12954
12955              We first try and parse a parameter-declaration-clause,
12956              and then try a nested declarator (if FIRST is true).
12957
12958              It is not an error for it not to be a
12959              parameter-declaration-clause, even when FIRST is
12960              false. Consider,
12961
12962                int i (int);
12963                int i (3);
12964
12965              The first is the declaration of a function while the
12966              second is the definition of a variable, including its
12967              initializer.
12968
12969              Having seen only the parenthesis, we cannot know which of
12970              these two alternatives should be selected.  Even more
12971              complex are examples like:
12972
12973                int i (int (a));
12974                int i (int (3));
12975
12976              The former is a function-declaration; the latter is a
12977              variable initialization.
12978
12979              Thus again, we try a parameter-declaration-clause, and if
12980              that fails, we back out and return.  */
12981
12982           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12983             {
12984               cp_parameter_declarator *params;
12985               unsigned saved_num_template_parameter_lists;
12986
12987               /* In a member-declarator, the only valid interpretation
12988                  of a parenthesis is the start of a
12989                  parameter-declaration-clause.  (It is invalid to
12990                  initialize a static data member with a parenthesized
12991                  initializer; only the "=" form of initialization is
12992                  permitted.)  */
12993               if (!member_p)
12994                 cp_parser_parse_tentatively (parser);
12995
12996               /* Consume the `('.  */
12997               cp_lexer_consume_token (parser->lexer);
12998               if (first)
12999                 {
13000                   /* If this is going to be an abstract declarator, we're
13001                      in a declarator and we can't have default args.  */
13002                   parser->default_arg_ok_p = false;
13003                   parser->in_declarator_p = true;
13004                 }
13005
13006               /* Inside the function parameter list, surrounding
13007                  template-parameter-lists do not apply.  */
13008               saved_num_template_parameter_lists
13009                 = parser->num_template_parameter_lists;
13010               parser->num_template_parameter_lists = 0;
13011
13012               /* Parse the parameter-declaration-clause.  */
13013               params = cp_parser_parameter_declaration_clause (parser);
13014
13015               parser->num_template_parameter_lists
13016                 = saved_num_template_parameter_lists;
13017
13018               /* If all went well, parse the cv-qualifier-seq and the
13019                  exception-specification.  */
13020               if (member_p || cp_parser_parse_definitely (parser))
13021                 {
13022                   cp_cv_quals cv_quals;
13023                   tree exception_specification;
13024
13025                   if (ctor_dtor_or_conv_p)
13026                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13027                   first = false;
13028                   /* Consume the `)'.  */
13029                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13030
13031                   /* Parse the cv-qualifier-seq.  */
13032                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13033                   /* And the exception-specification.  */
13034                   exception_specification
13035                     = cp_parser_exception_specification_opt (parser);
13036
13037                   /* Create the function-declarator.  */
13038                   declarator = make_call_declarator (declarator,
13039                                                      params,
13040                                                      cv_quals,
13041                                                      exception_specification);
13042                   /* Any subsequent parameter lists are to do with
13043                      return type, so are not those of the declared
13044                      function.  */
13045                   parser->default_arg_ok_p = false;
13046
13047                   /* Repeat the main loop.  */
13048                   continue;
13049                 }
13050             }
13051
13052           /* If this is the first, we can try a parenthesized
13053              declarator.  */
13054           if (first)
13055             {
13056               bool saved_in_type_id_in_expr_p;
13057
13058               parser->default_arg_ok_p = saved_default_arg_ok_p;
13059               parser->in_declarator_p = saved_in_declarator_p;
13060
13061               /* Consume the `('.  */
13062               cp_lexer_consume_token (parser->lexer);
13063               /* Parse the nested declarator.  */
13064               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13065               parser->in_type_id_in_expr_p = true;
13066               declarator
13067                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13068                                         /*parenthesized_p=*/NULL,
13069                                         member_p);
13070               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13071               first = false;
13072               /* Expect a `)'.  */
13073               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13074                 declarator = cp_error_declarator;
13075               if (declarator == cp_error_declarator)
13076                 break;
13077
13078               goto handle_declarator;
13079             }
13080           /* Otherwise, we must be done.  */
13081           else
13082             break;
13083         }
13084       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13085                && token->type == CPP_OPEN_SQUARE)
13086         {
13087           /* Parse an array-declarator.  */
13088           tree bounds;
13089
13090           if (ctor_dtor_or_conv_p)
13091             *ctor_dtor_or_conv_p = 0;
13092
13093           first = false;
13094           parser->default_arg_ok_p = false;
13095           parser->in_declarator_p = true;
13096           /* Consume the `['.  */
13097           cp_lexer_consume_token (parser->lexer);
13098           /* Peek at the next token.  */
13099           token = cp_lexer_peek_token (parser->lexer);
13100           /* If the next token is `]', then there is no
13101              constant-expression.  */
13102           if (token->type != CPP_CLOSE_SQUARE)
13103             {
13104               bool non_constant_p;
13105
13106               bounds
13107                 = cp_parser_constant_expression (parser,
13108                                                  /*allow_non_constant=*/true,
13109                                                  &non_constant_p);
13110               if (!non_constant_p)
13111                 bounds = fold_non_dependent_expr (bounds);
13112               /* Normally, the array bound must be an integral constant
13113                  expression.  However, as an extension, we allow VLAs
13114                  in function scopes.  */
13115               else if (!parser->in_function_body)
13116                 {
13117                   error ("%Harray bound is not an integer constant",
13118                          &token->location);
13119                   bounds = error_mark_node;
13120                 }
13121             }
13122           else
13123             bounds = NULL_TREE;
13124           /* Look for the closing `]'.  */
13125           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13126             {
13127               declarator = cp_error_declarator;
13128               break;
13129             }
13130
13131           declarator = make_array_declarator (declarator, bounds);
13132         }
13133       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13134         {
13135           tree qualifying_scope;
13136           tree unqualified_name;
13137           special_function_kind sfk;
13138           bool abstract_ok;
13139           bool pack_expansion_p = false;
13140           cp_token *declarator_id_start_token;
13141
13142           /* Parse a declarator-id */
13143           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13144           if (abstract_ok)
13145             {
13146               cp_parser_parse_tentatively (parser);
13147
13148               /* If we see an ellipsis, we should be looking at a
13149                  parameter pack. */
13150               if (token->type == CPP_ELLIPSIS)
13151                 {
13152                   /* Consume the `...' */
13153                   cp_lexer_consume_token (parser->lexer);
13154
13155                   pack_expansion_p = true;
13156                 }
13157             }
13158
13159           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13160           unqualified_name
13161             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13162           qualifying_scope = parser->scope;
13163           if (abstract_ok)
13164             {
13165               bool okay = false;
13166
13167               if (!unqualified_name && pack_expansion_p)
13168                 {
13169                   /* Check whether an error occurred. */
13170                   okay = !cp_parser_error_occurred (parser);
13171
13172                   /* We already consumed the ellipsis to mark a
13173                      parameter pack, but we have no way to report it,
13174                      so abort the tentative parse. We will be exiting
13175                      immediately anyway. */
13176                   cp_parser_abort_tentative_parse (parser);
13177                 }
13178               else
13179                 okay = cp_parser_parse_definitely (parser);
13180
13181               if (!okay)
13182                 unqualified_name = error_mark_node;
13183               else if (unqualified_name
13184                        && (qualifying_scope
13185                            || (TREE_CODE (unqualified_name)
13186                                != IDENTIFIER_NODE)))
13187                 {
13188                   cp_parser_error (parser, "expected unqualified-id");
13189                   unqualified_name = error_mark_node;
13190                 }
13191             }
13192
13193           if (!unqualified_name)
13194             return NULL;
13195           if (unqualified_name == error_mark_node)
13196             {
13197               declarator = cp_error_declarator;
13198               pack_expansion_p = false;
13199               declarator->parameter_pack_p = false;
13200               break;
13201             }
13202
13203           if (qualifying_scope && at_namespace_scope_p ()
13204               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13205             {
13206               /* In the declaration of a member of a template class
13207                  outside of the class itself, the SCOPE will sometimes
13208                  be a TYPENAME_TYPE.  For example, given:
13209
13210                  template <typename T>
13211                  int S<T>::R::i = 3;
13212
13213                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13214                  this context, we must resolve S<T>::R to an ordinary
13215                  type, rather than a typename type.
13216
13217                  The reason we normally avoid resolving TYPENAME_TYPEs
13218                  is that a specialization of `S' might render
13219                  `S<T>::R' not a type.  However, if `S' is
13220                  specialized, then this `i' will not be used, so there
13221                  is no harm in resolving the types here.  */
13222               tree type;
13223
13224               /* Resolve the TYPENAME_TYPE.  */
13225               type = resolve_typename_type (qualifying_scope,
13226                                             /*only_current_p=*/false);
13227               /* If that failed, the declarator is invalid.  */
13228               if (TREE_CODE (type) == TYPENAME_TYPE)
13229                 error ("%H%<%T::%E%> is not a type",
13230                        &declarator_id_start_token->location,
13231                        TYPE_CONTEXT (qualifying_scope),
13232                        TYPE_IDENTIFIER (qualifying_scope));
13233               qualifying_scope = type;
13234             }
13235
13236           sfk = sfk_none;
13237
13238           if (unqualified_name)
13239             {
13240               tree class_type;
13241
13242               if (qualifying_scope
13243                   && CLASS_TYPE_P (qualifying_scope))
13244                 class_type = qualifying_scope;
13245               else
13246                 class_type = current_class_type;
13247
13248               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13249                 {
13250                   tree name_type = TREE_TYPE (unqualified_name);
13251                   if (class_type && same_type_p (name_type, class_type))
13252                     {
13253                       if (qualifying_scope
13254                           && CLASSTYPE_USE_TEMPLATE (name_type))
13255                         {
13256                           error ("%Hinvalid use of constructor as a template",
13257                                  &declarator_id_start_token->location);
13258                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13259                                   "name the constructor in a qualified name",
13260                                   class_type,
13261                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13262                                   class_type, name_type);
13263                           declarator = cp_error_declarator;
13264                           break;
13265                         }
13266                       else
13267                         unqualified_name = constructor_name (class_type);
13268                     }
13269                   else
13270                     {
13271                       /* We do not attempt to print the declarator
13272                          here because we do not have enough
13273                          information about its original syntactic
13274                          form.  */
13275                       cp_parser_error (parser, "invalid declarator");
13276                       declarator = cp_error_declarator;
13277                       break;
13278                     }
13279                 }
13280
13281               if (class_type)
13282                 {
13283                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13284                     sfk = sfk_destructor;
13285                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13286                     sfk = sfk_conversion;
13287                   else if (/* There's no way to declare a constructor
13288                               for an anonymous type, even if the type
13289                               got a name for linkage purposes.  */
13290                            !TYPE_WAS_ANONYMOUS (class_type)
13291                            && constructor_name_p (unqualified_name,
13292                                                   class_type))
13293                     {
13294                       unqualified_name = constructor_name (class_type);
13295                       sfk = sfk_constructor;
13296                     }
13297
13298                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13299                     *ctor_dtor_or_conv_p = -1;
13300                 }
13301             }
13302           declarator = make_id_declarator (qualifying_scope,
13303                                            unqualified_name,
13304                                            sfk);
13305           declarator->id_loc = token->location;
13306           declarator->parameter_pack_p = pack_expansion_p;
13307
13308           if (pack_expansion_p)
13309             maybe_warn_variadic_templates ();
13310
13311         handle_declarator:;
13312           scope = get_scope_of_declarator (declarator);
13313           if (scope)
13314             /* Any names that appear after the declarator-id for a
13315                member are looked up in the containing scope.  */
13316             pushed_scope = push_scope (scope);
13317           parser->in_declarator_p = true;
13318           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13319               || (declarator && declarator->kind == cdk_id))
13320             /* Default args are only allowed on function
13321                declarations.  */
13322             parser->default_arg_ok_p = saved_default_arg_ok_p;
13323           else
13324             parser->default_arg_ok_p = false;
13325
13326           first = false;
13327         }
13328       /* We're done.  */
13329       else
13330         break;
13331     }
13332
13333   /* For an abstract declarator, we might wind up with nothing at this
13334      point.  That's an error; the declarator is not optional.  */
13335   if (!declarator)
13336     cp_parser_error (parser, "expected declarator");
13337
13338   /* If we entered a scope, we must exit it now.  */
13339   if (pushed_scope)
13340     pop_scope (pushed_scope);
13341
13342   parser->default_arg_ok_p = saved_default_arg_ok_p;
13343   parser->in_declarator_p = saved_in_declarator_p;
13344
13345   return declarator;
13346 }
13347
13348 /* Parse a ptr-operator.
13349
13350    ptr-operator:
13351      * cv-qualifier-seq [opt]
13352      &
13353      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13354
13355    GNU Extension:
13356
13357    ptr-operator:
13358      & cv-qualifier-seq [opt]
13359
13360    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13361    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13362    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13363    filled in with the TYPE containing the member.  *CV_QUALS is
13364    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13365    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13366    Note that the tree codes returned by this function have nothing
13367    to do with the types of trees that will be eventually be created
13368    to represent the pointer or reference type being parsed. They are
13369    just constants with suggestive names. */
13370 static enum tree_code
13371 cp_parser_ptr_operator (cp_parser* parser,
13372                         tree* type,
13373                         cp_cv_quals *cv_quals)
13374 {
13375   enum tree_code code = ERROR_MARK;
13376   cp_token *token;
13377
13378   /* Assume that it's not a pointer-to-member.  */
13379   *type = NULL_TREE;
13380   /* And that there are no cv-qualifiers.  */
13381   *cv_quals = TYPE_UNQUALIFIED;
13382
13383   /* Peek at the next token.  */
13384   token = cp_lexer_peek_token (parser->lexer);
13385
13386   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13387   if (token->type == CPP_MULT)
13388     code = INDIRECT_REF;
13389   else if (token->type == CPP_AND)
13390     code = ADDR_EXPR;
13391   else if ((cxx_dialect != cxx98) &&
13392            token->type == CPP_AND_AND) /* C++0x only */
13393     code = NON_LVALUE_EXPR;
13394
13395   if (code != ERROR_MARK)
13396     {
13397       /* Consume the `*', `&' or `&&'.  */
13398       cp_lexer_consume_token (parser->lexer);
13399
13400       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13401          `&', if we are allowing GNU extensions.  (The only qualifier
13402          that can legally appear after `&' is `restrict', but that is
13403          enforced during semantic analysis.  */
13404       if (code == INDIRECT_REF
13405           || cp_parser_allow_gnu_extensions_p (parser))
13406         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13407     }
13408   else
13409     {
13410       /* Try the pointer-to-member case.  */
13411       cp_parser_parse_tentatively (parser);
13412       /* Look for the optional `::' operator.  */
13413       cp_parser_global_scope_opt (parser,
13414                                   /*current_scope_valid_p=*/false);
13415       /* Look for the nested-name specifier.  */
13416       token = cp_lexer_peek_token (parser->lexer);
13417       cp_parser_nested_name_specifier (parser,
13418                                        /*typename_keyword_p=*/false,
13419                                        /*check_dependency_p=*/true,
13420                                        /*type_p=*/false,
13421                                        /*is_declaration=*/false);
13422       /* If we found it, and the next token is a `*', then we are
13423          indeed looking at a pointer-to-member operator.  */
13424       if (!cp_parser_error_occurred (parser)
13425           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13426         {
13427           /* Indicate that the `*' operator was used.  */
13428           code = INDIRECT_REF;
13429
13430           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13431             error ("%H%qD is a namespace", &token->location, parser->scope);
13432           else
13433             {
13434               /* The type of which the member is a member is given by the
13435                  current SCOPE.  */
13436               *type = parser->scope;
13437               /* The next name will not be qualified.  */
13438               parser->scope = NULL_TREE;
13439               parser->qualifying_scope = NULL_TREE;
13440               parser->object_scope = NULL_TREE;
13441               /* Look for the optional cv-qualifier-seq.  */
13442               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13443             }
13444         }
13445       /* If that didn't work we don't have a ptr-operator.  */
13446       if (!cp_parser_parse_definitely (parser))
13447         cp_parser_error (parser, "expected ptr-operator");
13448     }
13449
13450   return code;
13451 }
13452
13453 /* Parse an (optional) cv-qualifier-seq.
13454
13455    cv-qualifier-seq:
13456      cv-qualifier cv-qualifier-seq [opt]
13457
13458    cv-qualifier:
13459      const
13460      volatile
13461
13462    GNU Extension:
13463
13464    cv-qualifier:
13465      __restrict__
13466
13467    Returns a bitmask representing the cv-qualifiers.  */
13468
13469 static cp_cv_quals
13470 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13471 {
13472   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13473
13474   while (true)
13475     {
13476       cp_token *token;
13477       cp_cv_quals cv_qualifier;
13478
13479       /* Peek at the next token.  */
13480       token = cp_lexer_peek_token (parser->lexer);
13481       /* See if it's a cv-qualifier.  */
13482       switch (token->keyword)
13483         {
13484         case RID_CONST:
13485           cv_qualifier = TYPE_QUAL_CONST;
13486           break;
13487
13488         case RID_VOLATILE:
13489           cv_qualifier = TYPE_QUAL_VOLATILE;
13490           break;
13491
13492         case RID_RESTRICT:
13493           cv_qualifier = TYPE_QUAL_RESTRICT;
13494           break;
13495
13496         default:
13497           cv_qualifier = TYPE_UNQUALIFIED;
13498           break;
13499         }
13500
13501       if (!cv_qualifier)
13502         break;
13503
13504       if (cv_quals & cv_qualifier)
13505         {
13506           error ("%Hduplicate cv-qualifier", &token->location);
13507           cp_lexer_purge_token (parser->lexer);
13508         }
13509       else
13510         {
13511           cp_lexer_consume_token (parser->lexer);
13512           cv_quals |= cv_qualifier;
13513         }
13514     }
13515
13516   return cv_quals;
13517 }
13518
13519 /* Parse a declarator-id.
13520
13521    declarator-id:
13522      id-expression
13523      :: [opt] nested-name-specifier [opt] type-name
13524
13525    In the `id-expression' case, the value returned is as for
13526    cp_parser_id_expression if the id-expression was an unqualified-id.
13527    If the id-expression was a qualified-id, then a SCOPE_REF is
13528    returned.  The first operand is the scope (either a NAMESPACE_DECL
13529    or TREE_TYPE), but the second is still just a representation of an
13530    unqualified-id.  */
13531
13532 static tree
13533 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13534 {
13535   tree id;
13536   /* The expression must be an id-expression.  Assume that qualified
13537      names are the names of types so that:
13538
13539        template <class T>
13540        int S<T>::R::i = 3;
13541
13542      will work; we must treat `S<T>::R' as the name of a type.
13543      Similarly, assume that qualified names are templates, where
13544      required, so that:
13545
13546        template <class T>
13547        int S<T>::R<T>::i = 3;
13548
13549      will work, too.  */
13550   id = cp_parser_id_expression (parser,
13551                                 /*template_keyword_p=*/false,
13552                                 /*check_dependency_p=*/false,
13553                                 /*template_p=*/NULL,
13554                                 /*declarator_p=*/true,
13555                                 optional_p);
13556   if (id && BASELINK_P (id))
13557     id = BASELINK_FUNCTIONS (id);
13558   return id;
13559 }
13560
13561 /* Parse a type-id.
13562
13563    type-id:
13564      type-specifier-seq abstract-declarator [opt]
13565
13566    Returns the TYPE specified.  */
13567
13568 static tree
13569 cp_parser_type_id (cp_parser* parser)
13570 {
13571   cp_decl_specifier_seq type_specifier_seq;
13572   cp_declarator *abstract_declarator;
13573
13574   /* Parse the type-specifier-seq.  */
13575   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13576                                 &type_specifier_seq);
13577   if (type_specifier_seq.type == error_mark_node)
13578     return error_mark_node;
13579
13580   /* There might or might not be an abstract declarator.  */
13581   cp_parser_parse_tentatively (parser);
13582   /* Look for the declarator.  */
13583   abstract_declarator
13584     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13585                             /*parenthesized_p=*/NULL,
13586                             /*member_p=*/false);
13587   /* Check to see if there really was a declarator.  */
13588   if (!cp_parser_parse_definitely (parser))
13589     abstract_declarator = NULL;
13590
13591   return groktypename (&type_specifier_seq, abstract_declarator);
13592 }
13593
13594 /* Parse a type-specifier-seq.
13595
13596    type-specifier-seq:
13597      type-specifier type-specifier-seq [opt]
13598
13599    GNU extension:
13600
13601    type-specifier-seq:
13602      attributes type-specifier-seq [opt]
13603
13604    If IS_CONDITION is true, we are at the start of a "condition",
13605    e.g., we've just seen "if (".
13606
13607    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13608
13609 static void
13610 cp_parser_type_specifier_seq (cp_parser* parser,
13611                               bool is_condition,
13612                               cp_decl_specifier_seq *type_specifier_seq)
13613 {
13614   bool seen_type_specifier = false;
13615   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13616   cp_token *start_token = NULL;
13617
13618   /* Clear the TYPE_SPECIFIER_SEQ.  */
13619   clear_decl_specs (type_specifier_seq);
13620
13621   /* Parse the type-specifiers and attributes.  */
13622   while (true)
13623     {
13624       tree type_specifier;
13625       bool is_cv_qualifier;
13626
13627       /* Check for attributes first.  */
13628       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13629         {
13630           type_specifier_seq->attributes =
13631             chainon (type_specifier_seq->attributes,
13632                      cp_parser_attributes_opt (parser));
13633           continue;
13634         }
13635
13636       /* record the token of the beginning of the type specifier seq,
13637          for error reporting purposes*/
13638      if (!start_token)
13639        start_token = cp_lexer_peek_token (parser->lexer);
13640
13641       /* Look for the type-specifier.  */
13642       type_specifier = cp_parser_type_specifier (parser,
13643                                                  flags,
13644                                                  type_specifier_seq,
13645                                                  /*is_declaration=*/false,
13646                                                  NULL,
13647                                                  &is_cv_qualifier);
13648       if (!type_specifier)
13649         {
13650           /* If the first type-specifier could not be found, this is not a
13651              type-specifier-seq at all.  */
13652           if (!seen_type_specifier)
13653             {
13654               cp_parser_error (parser, "expected type-specifier");
13655               type_specifier_seq->type = error_mark_node;
13656               return;
13657             }
13658           /* If subsequent type-specifiers could not be found, the
13659              type-specifier-seq is complete.  */
13660           break;
13661         }
13662
13663       seen_type_specifier = true;
13664       /* The standard says that a condition can be:
13665
13666             type-specifier-seq declarator = assignment-expression
13667
13668          However, given:
13669
13670            struct S {};
13671            if (int S = ...)
13672
13673          we should treat the "S" as a declarator, not as a
13674          type-specifier.  The standard doesn't say that explicitly for
13675          type-specifier-seq, but it does say that for
13676          decl-specifier-seq in an ordinary declaration.  Perhaps it
13677          would be clearer just to allow a decl-specifier-seq here, and
13678          then add a semantic restriction that if any decl-specifiers
13679          that are not type-specifiers appear, the program is invalid.  */
13680       if (is_condition && !is_cv_qualifier)
13681         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13682     }
13683
13684   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13685 }
13686
13687 /* Parse a parameter-declaration-clause.
13688
13689    parameter-declaration-clause:
13690      parameter-declaration-list [opt] ... [opt]
13691      parameter-declaration-list , ...
13692
13693    Returns a representation for the parameter declarations.  A return
13694    value of NULL indicates a parameter-declaration-clause consisting
13695    only of an ellipsis.  */
13696
13697 static cp_parameter_declarator *
13698 cp_parser_parameter_declaration_clause (cp_parser* parser)
13699 {
13700   cp_parameter_declarator *parameters;
13701   cp_token *token;
13702   bool ellipsis_p;
13703   bool is_error;
13704
13705   /* Peek at the next token.  */
13706   token = cp_lexer_peek_token (parser->lexer);
13707   /* Check for trivial parameter-declaration-clauses.  */
13708   if (token->type == CPP_ELLIPSIS)
13709     {
13710       /* Consume the `...' token.  */
13711       cp_lexer_consume_token (parser->lexer);
13712       return NULL;
13713     }
13714   else if (token->type == CPP_CLOSE_PAREN)
13715     /* There are no parameters.  */
13716     {
13717 #ifndef NO_IMPLICIT_EXTERN_C
13718       if (in_system_header && current_class_type == NULL
13719           && current_lang_name == lang_name_c)
13720         return NULL;
13721       else
13722 #endif
13723         return no_parameters;
13724     }
13725   /* Check for `(void)', too, which is a special case.  */
13726   else if (token->keyword == RID_VOID
13727            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13728                == CPP_CLOSE_PAREN))
13729     {
13730       /* Consume the `void' token.  */
13731       cp_lexer_consume_token (parser->lexer);
13732       /* There are no parameters.  */
13733       return no_parameters;
13734     }
13735
13736   /* Parse the parameter-declaration-list.  */
13737   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13738   /* If a parse error occurred while parsing the
13739      parameter-declaration-list, then the entire
13740      parameter-declaration-clause is erroneous.  */
13741   if (is_error)
13742     return NULL;
13743
13744   /* Peek at the next token.  */
13745   token = cp_lexer_peek_token (parser->lexer);
13746   /* If it's a `,', the clause should terminate with an ellipsis.  */
13747   if (token->type == CPP_COMMA)
13748     {
13749       /* Consume the `,'.  */
13750       cp_lexer_consume_token (parser->lexer);
13751       /* Expect an ellipsis.  */
13752       ellipsis_p
13753         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13754     }
13755   /* It might also be `...' if the optional trailing `,' was
13756      omitted.  */
13757   else if (token->type == CPP_ELLIPSIS)
13758     {
13759       /* Consume the `...' token.  */
13760       cp_lexer_consume_token (parser->lexer);
13761       /* And remember that we saw it.  */
13762       ellipsis_p = true;
13763     }
13764   else
13765     ellipsis_p = false;
13766
13767   /* Finish the parameter list.  */
13768   if (parameters && ellipsis_p)
13769     parameters->ellipsis_p = true;
13770
13771   return parameters;
13772 }
13773
13774 /* Parse a parameter-declaration-list.
13775
13776    parameter-declaration-list:
13777      parameter-declaration
13778      parameter-declaration-list , parameter-declaration
13779
13780    Returns a representation of the parameter-declaration-list, as for
13781    cp_parser_parameter_declaration_clause.  However, the
13782    `void_list_node' is never appended to the list.  Upon return,
13783    *IS_ERROR will be true iff an error occurred.  */
13784
13785 static cp_parameter_declarator *
13786 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13787 {
13788   cp_parameter_declarator *parameters = NULL;
13789   cp_parameter_declarator **tail = &parameters;
13790   bool saved_in_unbraced_linkage_specification_p;
13791
13792   /* Assume all will go well.  */
13793   *is_error = false;
13794   /* The special considerations that apply to a function within an
13795      unbraced linkage specifications do not apply to the parameters
13796      to the function.  */
13797   saved_in_unbraced_linkage_specification_p 
13798     = parser->in_unbraced_linkage_specification_p;
13799   parser->in_unbraced_linkage_specification_p = false;
13800
13801   /* Look for more parameters.  */
13802   while (true)
13803     {
13804       cp_parameter_declarator *parameter;
13805       bool parenthesized_p;
13806       /* Parse the parameter.  */
13807       parameter
13808         = cp_parser_parameter_declaration (parser,
13809                                            /*template_parm_p=*/false,
13810                                            &parenthesized_p);
13811
13812       /* If a parse error occurred parsing the parameter declaration,
13813          then the entire parameter-declaration-list is erroneous.  */
13814       if (!parameter)
13815         {
13816           *is_error = true;
13817           parameters = NULL;
13818           break;
13819         }
13820       /* Add the new parameter to the list.  */
13821       *tail = parameter;
13822       tail = &parameter->next;
13823
13824       /* Peek at the next token.  */
13825       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13826           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13827           /* These are for Objective-C++ */
13828           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13829           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13830         /* The parameter-declaration-list is complete.  */
13831         break;
13832       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13833         {
13834           cp_token *token;
13835
13836           /* Peek at the next token.  */
13837           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13838           /* If it's an ellipsis, then the list is complete.  */
13839           if (token->type == CPP_ELLIPSIS)
13840             break;
13841           /* Otherwise, there must be more parameters.  Consume the
13842              `,'.  */
13843           cp_lexer_consume_token (parser->lexer);
13844           /* When parsing something like:
13845
13846                 int i(float f, double d)
13847
13848              we can tell after seeing the declaration for "f" that we
13849              are not looking at an initialization of a variable "i",
13850              but rather at the declaration of a function "i".
13851
13852              Due to the fact that the parsing of template arguments
13853              (as specified to a template-id) requires backtracking we
13854              cannot use this technique when inside a template argument
13855              list.  */
13856           if (!parser->in_template_argument_list_p
13857               && !parser->in_type_id_in_expr_p
13858               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13859               /* However, a parameter-declaration of the form
13860                  "foat(f)" (which is a valid declaration of a
13861                  parameter "f") can also be interpreted as an
13862                  expression (the conversion of "f" to "float").  */
13863               && !parenthesized_p)
13864             cp_parser_commit_to_tentative_parse (parser);
13865         }
13866       else
13867         {
13868           cp_parser_error (parser, "expected %<,%> or %<...%>");
13869           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13870             cp_parser_skip_to_closing_parenthesis (parser,
13871                                                    /*recovering=*/true,
13872                                                    /*or_comma=*/false,
13873                                                    /*consume_paren=*/false);
13874           break;
13875         }
13876     }
13877
13878   parser->in_unbraced_linkage_specification_p
13879     = saved_in_unbraced_linkage_specification_p;
13880
13881   return parameters;
13882 }
13883
13884 /* Parse a parameter declaration.
13885
13886    parameter-declaration:
13887      decl-specifier-seq ... [opt] declarator
13888      decl-specifier-seq declarator = assignment-expression
13889      decl-specifier-seq ... [opt] abstract-declarator [opt]
13890      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13891
13892    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13893    declares a template parameter.  (In that case, a non-nested `>'
13894    token encountered during the parsing of the assignment-expression
13895    is not interpreted as a greater-than operator.)
13896
13897    Returns a representation of the parameter, or NULL if an error
13898    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13899    true iff the declarator is of the form "(p)".  */
13900
13901 static cp_parameter_declarator *
13902 cp_parser_parameter_declaration (cp_parser *parser,
13903                                  bool template_parm_p,
13904                                  bool *parenthesized_p)
13905 {
13906   int declares_class_or_enum;
13907   bool greater_than_is_operator_p;
13908   cp_decl_specifier_seq decl_specifiers;
13909   cp_declarator *declarator;
13910   tree default_argument;
13911   cp_token *token = NULL, *declarator_token_start = NULL;
13912   const char *saved_message;
13913
13914   /* In a template parameter, `>' is not an operator.
13915
13916      [temp.param]
13917
13918      When parsing a default template-argument for a non-type
13919      template-parameter, the first non-nested `>' is taken as the end
13920      of the template parameter-list rather than a greater-than
13921      operator.  */
13922   greater_than_is_operator_p = !template_parm_p;
13923
13924   /* Type definitions may not appear in parameter types.  */
13925   saved_message = parser->type_definition_forbidden_message;
13926   parser->type_definition_forbidden_message
13927     = "types may not be defined in parameter types";
13928
13929   /* Parse the declaration-specifiers.  */
13930   cp_parser_decl_specifier_seq (parser,
13931                                 CP_PARSER_FLAGS_NONE,
13932                                 &decl_specifiers,
13933                                 &declares_class_or_enum);
13934   /* If an error occurred, there's no reason to attempt to parse the
13935      rest of the declaration.  */
13936   if (cp_parser_error_occurred (parser))
13937     {
13938       parser->type_definition_forbidden_message = saved_message;
13939       return NULL;
13940     }
13941
13942   /* Peek at the next token.  */
13943   token = cp_lexer_peek_token (parser->lexer);
13944
13945   /* If the next token is a `)', `,', `=', `>', or `...', then there
13946      is no declarator. However, when variadic templates are enabled,
13947      there may be a declarator following `...'.  */
13948   if (token->type == CPP_CLOSE_PAREN
13949       || token->type == CPP_COMMA
13950       || token->type == CPP_EQ
13951       || token->type == CPP_GREATER)
13952     {
13953       declarator = NULL;
13954       if (parenthesized_p)
13955         *parenthesized_p = false;
13956     }
13957   /* Otherwise, there should be a declarator.  */
13958   else
13959     {
13960       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13961       parser->default_arg_ok_p = false;
13962
13963       /* After seeing a decl-specifier-seq, if the next token is not a
13964          "(", there is no possibility that the code is a valid
13965          expression.  Therefore, if parsing tentatively, we commit at
13966          this point.  */
13967       if (!parser->in_template_argument_list_p
13968           /* In an expression context, having seen:
13969
13970                (int((char ...
13971
13972              we cannot be sure whether we are looking at a
13973              function-type (taking a "char" as a parameter) or a cast
13974              of some object of type "char" to "int".  */
13975           && !parser->in_type_id_in_expr_p
13976           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13977           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13978         cp_parser_commit_to_tentative_parse (parser);
13979       /* Parse the declarator.  */
13980       declarator_token_start = token;
13981       declarator = cp_parser_declarator (parser,
13982                                          CP_PARSER_DECLARATOR_EITHER,
13983                                          /*ctor_dtor_or_conv_p=*/NULL,
13984                                          parenthesized_p,
13985                                          /*member_p=*/false);
13986       parser->default_arg_ok_p = saved_default_arg_ok_p;
13987       /* After the declarator, allow more attributes.  */
13988       decl_specifiers.attributes
13989         = chainon (decl_specifiers.attributes,
13990                    cp_parser_attributes_opt (parser));
13991     }
13992
13993   /* If the next token is an ellipsis, and we have not seen a
13994      declarator name, and the type of the declarator contains parameter
13995      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13996      a parameter pack expansion expression. Otherwise, leave the
13997      ellipsis for a C-style variadic function. */
13998   token = cp_lexer_peek_token (parser->lexer);
13999   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14000     {
14001       tree type = decl_specifiers.type;
14002
14003       if (type && DECL_P (type))
14004         type = TREE_TYPE (type);
14005
14006       if (type
14007           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14008           && declarator_can_be_parameter_pack (declarator)
14009           && (!declarator || !declarator->parameter_pack_p)
14010           && uses_parameter_packs (type))
14011         {
14012           /* Consume the `...'. */
14013           cp_lexer_consume_token (parser->lexer);
14014           maybe_warn_variadic_templates ();
14015           
14016           /* Build a pack expansion type */
14017           if (declarator)
14018             declarator->parameter_pack_p = true;
14019           else
14020             decl_specifiers.type = make_pack_expansion (type);
14021         }
14022     }
14023
14024   /* The restriction on defining new types applies only to the type
14025      of the parameter, not to the default argument.  */
14026   parser->type_definition_forbidden_message = saved_message;
14027
14028   /* If the next token is `=', then process a default argument.  */
14029   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14030     {
14031       /* Consume the `='.  */
14032       cp_lexer_consume_token (parser->lexer);
14033
14034       /* If we are defining a class, then the tokens that make up the
14035          default argument must be saved and processed later.  */
14036       if (!template_parm_p && at_class_scope_p ()
14037           && TYPE_BEING_DEFINED (current_class_type))
14038         {
14039           unsigned depth = 0;
14040           int maybe_template_id = 0;
14041           cp_token *first_token;
14042           cp_token *token;
14043
14044           /* Add tokens until we have processed the entire default
14045              argument.  We add the range [first_token, token).  */
14046           first_token = cp_lexer_peek_token (parser->lexer);
14047           while (true)
14048             {
14049               bool done = false;
14050
14051               /* Peek at the next token.  */
14052               token = cp_lexer_peek_token (parser->lexer);
14053               /* What we do depends on what token we have.  */
14054               switch (token->type)
14055                 {
14056                   /* In valid code, a default argument must be
14057                      immediately followed by a `,' `)', or `...'.  */
14058                 case CPP_COMMA:
14059                   if (depth == 0 && maybe_template_id)
14060                     {
14061                       /* If we've seen a '<', we might be in a
14062                          template-argument-list.  Until Core issue 325 is
14063                          resolved, we don't know how this situation ought
14064                          to be handled, so try to DTRT.  We check whether
14065                          what comes after the comma is a valid parameter
14066                          declaration list.  If it is, then the comma ends
14067                          the default argument; otherwise the default
14068                          argument continues.  */
14069                       bool error = false;
14070
14071                       /* Set ITALP so cp_parser_parameter_declaration_list
14072                          doesn't decide to commit to this parse.  */
14073                       bool saved_italp = parser->in_template_argument_list_p;
14074                       parser->in_template_argument_list_p = true;
14075
14076                       cp_parser_parse_tentatively (parser);
14077                       cp_lexer_consume_token (parser->lexer);
14078                       cp_parser_parameter_declaration_list (parser, &error);
14079                       if (!cp_parser_error_occurred (parser) && !error)
14080                         done = true;
14081                       cp_parser_abort_tentative_parse (parser);
14082
14083                       parser->in_template_argument_list_p = saved_italp;
14084                       break;
14085                     }
14086                 case CPP_CLOSE_PAREN:
14087                 case CPP_ELLIPSIS:
14088                   /* If we run into a non-nested `;', `}', or `]',
14089                      then the code is invalid -- but the default
14090                      argument is certainly over.  */
14091                 case CPP_SEMICOLON:
14092                 case CPP_CLOSE_BRACE:
14093                 case CPP_CLOSE_SQUARE:
14094                   if (depth == 0)
14095                     done = true;
14096                   /* Update DEPTH, if necessary.  */
14097                   else if (token->type == CPP_CLOSE_PAREN
14098                            || token->type == CPP_CLOSE_BRACE
14099                            || token->type == CPP_CLOSE_SQUARE)
14100                     --depth;
14101                   break;
14102
14103                 case CPP_OPEN_PAREN:
14104                 case CPP_OPEN_SQUARE:
14105                 case CPP_OPEN_BRACE:
14106                   ++depth;
14107                   break;
14108
14109                 case CPP_LESS:
14110                   if (depth == 0)
14111                     /* This might be the comparison operator, or it might
14112                        start a template argument list.  */
14113                     ++maybe_template_id;
14114                   break;
14115
14116                 case CPP_RSHIFT:
14117                   if (cxx_dialect == cxx98)
14118                     break;
14119                   /* Fall through for C++0x, which treats the `>>'
14120                      operator like two `>' tokens in certain
14121                      cases.  */
14122
14123                 case CPP_GREATER:
14124                   if (depth == 0)
14125                     {
14126                       /* This might be an operator, or it might close a
14127                          template argument list.  But if a previous '<'
14128                          started a template argument list, this will have
14129                          closed it, so we can't be in one anymore.  */
14130                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14131                       if (maybe_template_id < 0)
14132                         maybe_template_id = 0;
14133                     }
14134                   break;
14135
14136                   /* If we run out of tokens, issue an error message.  */
14137                 case CPP_EOF:
14138                 case CPP_PRAGMA_EOL:
14139                   error ("%Hfile ends in default argument", &token->location);
14140                   done = true;
14141                   break;
14142
14143                 case CPP_NAME:
14144                 case CPP_SCOPE:
14145                   /* In these cases, we should look for template-ids.
14146                      For example, if the default argument is
14147                      `X<int, double>()', we need to do name lookup to
14148                      figure out whether or not `X' is a template; if
14149                      so, the `,' does not end the default argument.
14150
14151                      That is not yet done.  */
14152                   break;
14153
14154                 default:
14155                   break;
14156                 }
14157
14158               /* If we've reached the end, stop.  */
14159               if (done)
14160                 break;
14161
14162               /* Add the token to the token block.  */
14163               token = cp_lexer_consume_token (parser->lexer);
14164             }
14165
14166           /* Create a DEFAULT_ARG to represent the unparsed default
14167              argument.  */
14168           default_argument = make_node (DEFAULT_ARG);
14169           DEFARG_TOKENS (default_argument)
14170             = cp_token_cache_new (first_token, token);
14171           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14172         }
14173       /* Outside of a class definition, we can just parse the
14174          assignment-expression.  */
14175       else
14176         {
14177           token = cp_lexer_peek_token (parser->lexer);
14178           default_argument 
14179             = cp_parser_default_argument (parser, template_parm_p);
14180         }
14181
14182       if (!parser->default_arg_ok_p)
14183         {
14184           if (flag_permissive)
14185             warning (0, "deprecated use of default argument for parameter of non-function");
14186           else
14187             {
14188               error ("%Hdefault arguments are only "
14189                      "permitted for function parameters",
14190                      &token->location);
14191               default_argument = NULL_TREE;
14192             }
14193         }
14194       else if ((declarator && declarator->parameter_pack_p)
14195                || (decl_specifiers.type
14196                    && PACK_EXPANSION_P (decl_specifiers.type)))
14197         {
14198           const char* kind = template_parm_p? "template " : "";
14199           
14200           /* Find the name of the parameter pack.  */     
14201           cp_declarator *id_declarator = declarator;
14202           while (id_declarator && id_declarator->kind != cdk_id)
14203             id_declarator = id_declarator->declarator;
14204           
14205           if (id_declarator && id_declarator->kind == cdk_id)
14206             error ("%H%sparameter pack %qD cannot have a default argument",
14207                    &declarator_token_start->location,
14208                    kind, id_declarator->u.id.unqualified_name);
14209           else
14210             error ("%H%sparameter pack cannot have a default argument",
14211                    &declarator_token_start->location, kind);
14212           
14213           default_argument = NULL_TREE;
14214         }
14215     }
14216   else
14217     default_argument = NULL_TREE;
14218
14219   return make_parameter_declarator (&decl_specifiers,
14220                                     declarator,
14221                                     default_argument);
14222 }
14223
14224 /* Parse a default argument and return it.
14225
14226    TEMPLATE_PARM_P is true if this is a default argument for a
14227    non-type template parameter.  */
14228 static tree
14229 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14230 {
14231   tree default_argument = NULL_TREE;
14232   bool saved_greater_than_is_operator_p;
14233   bool saved_local_variables_forbidden_p;
14234
14235   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14236      set correctly.  */
14237   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14238   parser->greater_than_is_operator_p = !template_parm_p;
14239   /* Local variable names (and the `this' keyword) may not
14240      appear in a default argument.  */
14241   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14242   parser->local_variables_forbidden_p = true;
14243   /* The default argument expression may cause implicitly
14244      defined member functions to be synthesized, which will
14245      result in garbage collection.  We must treat this
14246      situation as if we were within the body of function so as
14247      to avoid collecting live data on the stack.  */
14248   ++function_depth;
14249   /* Parse the assignment-expression.  */
14250   if (template_parm_p)
14251     push_deferring_access_checks (dk_no_deferred);
14252   default_argument
14253     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14254   if (template_parm_p)
14255     pop_deferring_access_checks ();
14256   /* Restore saved state.  */
14257   --function_depth;
14258   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14259   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14260
14261   return default_argument;
14262 }
14263
14264 /* Parse a function-body.
14265
14266    function-body:
14267      compound_statement  */
14268
14269 static void
14270 cp_parser_function_body (cp_parser *parser)
14271 {
14272   cp_parser_compound_statement (parser, NULL, false);
14273 }
14274
14275 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14276    true if a ctor-initializer was present.  */
14277
14278 static bool
14279 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14280 {
14281   tree body;
14282   bool ctor_initializer_p;
14283
14284   /* Begin the function body.  */
14285   body = begin_function_body ();
14286   /* Parse the optional ctor-initializer.  */
14287   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14288   /* Parse the function-body.  */
14289   cp_parser_function_body (parser);
14290   /* Finish the function body.  */
14291   finish_function_body (body);
14292
14293   return ctor_initializer_p;
14294 }
14295
14296 /* Parse an initializer.
14297
14298    initializer:
14299      = initializer-clause
14300      ( expression-list )
14301
14302    Returns an expression representing the initializer.  If no
14303    initializer is present, NULL_TREE is returned.
14304
14305    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14306    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14307    set to TRUE if there is no initializer present.  If there is an
14308    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14309    is set to true; otherwise it is set to false.  */
14310
14311 static tree
14312 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14313                        bool* non_constant_p)
14314 {
14315   cp_token *token;
14316   tree init;
14317
14318   /* Peek at the next token.  */
14319   token = cp_lexer_peek_token (parser->lexer);
14320
14321   /* Let our caller know whether or not this initializer was
14322      parenthesized.  */
14323   *is_direct_init = (token->type != CPP_EQ);
14324   /* Assume that the initializer is constant.  */
14325   *non_constant_p = false;
14326
14327   if (token->type == CPP_EQ)
14328     {
14329       /* Consume the `='.  */
14330       cp_lexer_consume_token (parser->lexer);
14331       /* Parse the initializer-clause.  */
14332       init = cp_parser_initializer_clause (parser, non_constant_p);
14333     }
14334   else if (token->type == CPP_OPEN_PAREN)
14335     init = cp_parser_parenthesized_expression_list (parser, false,
14336                                                     /*cast_p=*/false,
14337                                                     /*allow_expansion_p=*/true,
14338                                                     non_constant_p);
14339   else if (token->type == CPP_OPEN_BRACE)
14340     {
14341       maybe_warn_cpp0x ("extended initializer lists");
14342       init = cp_parser_braced_list (parser, non_constant_p);
14343       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14344     }
14345   else
14346     {
14347       /* Anything else is an error.  */
14348       cp_parser_error (parser, "expected initializer");
14349       init = error_mark_node;
14350     }
14351
14352   return init;
14353 }
14354
14355 /* Parse an initializer-clause.
14356
14357    initializer-clause:
14358      assignment-expression
14359      braced-init-list
14360
14361    Returns an expression representing the initializer.
14362
14363    If the `assignment-expression' production is used the value
14364    returned is simply a representation for the expression.
14365
14366    Otherwise, calls cp_parser_braced_list.  */
14367
14368 static tree
14369 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14370 {
14371   tree initializer;
14372
14373   /* Assume the expression is constant.  */
14374   *non_constant_p = false;
14375
14376   /* If it is not a `{', then we are looking at an
14377      assignment-expression.  */
14378   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14379     {
14380       initializer
14381         = cp_parser_constant_expression (parser,
14382                                         /*allow_non_constant_p=*/true,
14383                                         non_constant_p);
14384       if (!*non_constant_p)
14385         initializer = fold_non_dependent_expr (initializer);
14386     }
14387   else
14388     initializer = cp_parser_braced_list (parser, non_constant_p);
14389
14390   return initializer;
14391 }
14392
14393 /* Parse a brace-enclosed initializer list.
14394
14395    braced-init-list:
14396      { initializer-list , [opt] }
14397      { }
14398
14399    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14400    the elements of the initializer-list (or NULL, if the last
14401    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14402    NULL_TREE.  There is no way to detect whether or not the optional
14403    trailing `,' was provided.  NON_CONSTANT_P is as for
14404    cp_parser_initializer.  */     
14405
14406 static tree
14407 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14408 {
14409   tree initializer;
14410
14411   /* Consume the `{' token.  */
14412   cp_lexer_consume_token (parser->lexer);
14413   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14414   initializer = make_node (CONSTRUCTOR);
14415   /* If it's not a `}', then there is a non-trivial initializer.  */
14416   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14417     {
14418       /* Parse the initializer list.  */
14419       CONSTRUCTOR_ELTS (initializer)
14420         = cp_parser_initializer_list (parser, non_constant_p);
14421       /* A trailing `,' token is allowed.  */
14422       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14423         cp_lexer_consume_token (parser->lexer);
14424     }
14425   /* Now, there should be a trailing `}'.  */
14426   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14427   TREE_TYPE (initializer) = init_list_type_node;
14428   return initializer;
14429 }
14430
14431 /* Parse an initializer-list.
14432
14433    initializer-list:
14434      initializer-clause ... [opt]
14435      initializer-list , initializer-clause ... [opt]
14436
14437    GNU Extension:
14438
14439    initializer-list:
14440      identifier : initializer-clause
14441      initializer-list, identifier : initializer-clause
14442
14443    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14444    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14445    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14446    as for cp_parser_initializer.  */
14447
14448 static VEC(constructor_elt,gc) *
14449 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14450 {
14451   VEC(constructor_elt,gc) *v = NULL;
14452
14453   /* Assume all of the expressions are constant.  */
14454   *non_constant_p = false;
14455
14456   /* Parse the rest of the list.  */
14457   while (true)
14458     {
14459       cp_token *token;
14460       tree identifier;
14461       tree initializer;
14462       bool clause_non_constant_p;
14463
14464       /* If the next token is an identifier and the following one is a
14465          colon, we are looking at the GNU designated-initializer
14466          syntax.  */
14467       if (cp_parser_allow_gnu_extensions_p (parser)
14468           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14469           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14470         {
14471           /* Warn the user that they are using an extension.  */
14472           pedwarn (input_location, OPT_pedantic, 
14473                    "ISO C++ does not allow designated initializers");
14474           /* Consume the identifier.  */
14475           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14476           /* Consume the `:'.  */
14477           cp_lexer_consume_token (parser->lexer);
14478         }
14479       else
14480         identifier = NULL_TREE;
14481
14482       /* Parse the initializer.  */
14483       initializer = cp_parser_initializer_clause (parser,
14484                                                   &clause_non_constant_p);
14485       /* If any clause is non-constant, so is the entire initializer.  */
14486       if (clause_non_constant_p)
14487         *non_constant_p = true;
14488
14489       /* If we have an ellipsis, this is an initializer pack
14490          expansion.  */
14491       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14492         {
14493           /* Consume the `...'.  */
14494           cp_lexer_consume_token (parser->lexer);
14495
14496           /* Turn the initializer into an initializer expansion.  */
14497           initializer = make_pack_expansion (initializer);
14498         }
14499
14500       /* Add it to the vector.  */
14501       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14502
14503       /* If the next token is not a comma, we have reached the end of
14504          the list.  */
14505       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14506         break;
14507
14508       /* Peek at the next token.  */
14509       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14510       /* If the next token is a `}', then we're still done.  An
14511          initializer-clause can have a trailing `,' after the
14512          initializer-list and before the closing `}'.  */
14513       if (token->type == CPP_CLOSE_BRACE)
14514         break;
14515
14516       /* Consume the `,' token.  */
14517       cp_lexer_consume_token (parser->lexer);
14518     }
14519
14520   return v;
14521 }
14522
14523 /* Classes [gram.class] */
14524
14525 /* Parse a class-name.
14526
14527    class-name:
14528      identifier
14529      template-id
14530
14531    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14532    to indicate that names looked up in dependent types should be
14533    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14534    keyword has been used to indicate that the name that appears next
14535    is a template.  TAG_TYPE indicates the explicit tag given before
14536    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14537    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14538    is the class being defined in a class-head.
14539
14540    Returns the TYPE_DECL representing the class.  */
14541
14542 static tree
14543 cp_parser_class_name (cp_parser *parser,
14544                       bool typename_keyword_p,
14545                       bool template_keyword_p,
14546                       enum tag_types tag_type,
14547                       bool check_dependency_p,
14548                       bool class_head_p,
14549                       bool is_declaration)
14550 {
14551   tree decl;
14552   tree scope;
14553   bool typename_p;
14554   cp_token *token;
14555
14556   /* All class-names start with an identifier.  */
14557   token = cp_lexer_peek_token (parser->lexer);
14558   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14559     {
14560       cp_parser_error (parser, "expected class-name");
14561       return error_mark_node;
14562     }
14563
14564   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14565      to a template-id, so we save it here.  */
14566   scope = parser->scope;
14567   if (scope == error_mark_node)
14568     return error_mark_node;
14569
14570   /* Any name names a type if we're following the `typename' keyword
14571      in a qualified name where the enclosing scope is type-dependent.  */
14572   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14573                 && dependent_type_p (scope));
14574   /* Handle the common case (an identifier, but not a template-id)
14575      efficiently.  */
14576   if (token->type == CPP_NAME
14577       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14578     {
14579       cp_token *identifier_token;
14580       tree identifier;
14581       bool ambiguous_p;
14582
14583       /* Look for the identifier.  */
14584       identifier_token = cp_lexer_peek_token (parser->lexer);
14585       ambiguous_p = identifier_token->ambiguous_p;
14586       identifier = cp_parser_identifier (parser);
14587       /* If the next token isn't an identifier, we are certainly not
14588          looking at a class-name.  */
14589       if (identifier == error_mark_node)
14590         decl = error_mark_node;
14591       /* If we know this is a type-name, there's no need to look it
14592          up.  */
14593       else if (typename_p)
14594         decl = identifier;
14595       else
14596         {
14597           tree ambiguous_decls;
14598           /* If we already know that this lookup is ambiguous, then
14599              we've already issued an error message; there's no reason
14600              to check again.  */
14601           if (ambiguous_p)
14602             {
14603               cp_parser_simulate_error (parser);
14604               return error_mark_node;
14605             }
14606           /* If the next token is a `::', then the name must be a type
14607              name.
14608
14609              [basic.lookup.qual]
14610
14611              During the lookup for a name preceding the :: scope
14612              resolution operator, object, function, and enumerator
14613              names are ignored.  */
14614           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14615             tag_type = typename_type;
14616           /* Look up the name.  */
14617           decl = cp_parser_lookup_name (parser, identifier,
14618                                         tag_type,
14619                                         /*is_template=*/false,
14620                                         /*is_namespace=*/false,
14621                                         check_dependency_p,
14622                                         &ambiguous_decls,
14623                                         identifier_token->location);
14624           if (ambiguous_decls)
14625             {
14626               error ("%Hreference to %qD is ambiguous",
14627                      &identifier_token->location, identifier);
14628               print_candidates (ambiguous_decls);
14629               if (cp_parser_parsing_tentatively (parser))
14630                 {
14631                   identifier_token->ambiguous_p = true;
14632                   cp_parser_simulate_error (parser);
14633                 }
14634               return error_mark_node;
14635             }
14636         }
14637     }
14638   else
14639     {
14640       /* Try a template-id.  */
14641       decl = cp_parser_template_id (parser, template_keyword_p,
14642                                     check_dependency_p,
14643                                     is_declaration);
14644       if (decl == error_mark_node)
14645         return error_mark_node;
14646     }
14647
14648   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14649
14650   /* If this is a typename, create a TYPENAME_TYPE.  */
14651   if (typename_p && decl != error_mark_node)
14652     {
14653       decl = make_typename_type (scope, decl, typename_type,
14654                                  /*complain=*/tf_error);
14655       if (decl != error_mark_node)
14656         decl = TYPE_NAME (decl);
14657     }
14658
14659   /* Check to see that it is really the name of a class.  */
14660   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14661       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14662       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14663     /* Situations like this:
14664
14665          template <typename T> struct A {
14666            typename T::template X<int>::I i;
14667          };
14668
14669        are problematic.  Is `T::template X<int>' a class-name?  The
14670        standard does not seem to be definitive, but there is no other
14671        valid interpretation of the following `::'.  Therefore, those
14672        names are considered class-names.  */
14673     {
14674       decl = make_typename_type (scope, decl, tag_type, tf_error);
14675       if (decl != error_mark_node)
14676         decl = TYPE_NAME (decl);
14677     }
14678   else if (TREE_CODE (decl) != TYPE_DECL
14679            || TREE_TYPE (decl) == error_mark_node
14680            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14681     decl = error_mark_node;
14682
14683   if (decl == error_mark_node)
14684     cp_parser_error (parser, "expected class-name");
14685
14686   return decl;
14687 }
14688
14689 /* Parse a class-specifier.
14690
14691    class-specifier:
14692      class-head { member-specification [opt] }
14693
14694    Returns the TREE_TYPE representing the class.  */
14695
14696 static tree
14697 cp_parser_class_specifier (cp_parser* parser)
14698 {
14699   cp_token *token;
14700   tree type;
14701   tree attributes = NULL_TREE;
14702   int has_trailing_semicolon;
14703   bool nested_name_specifier_p;
14704   unsigned saved_num_template_parameter_lists;
14705   bool saved_in_function_body;
14706   tree old_scope = NULL_TREE;
14707   tree scope = NULL_TREE;
14708   tree bases;
14709
14710   push_deferring_access_checks (dk_no_deferred);
14711
14712   /* Parse the class-head.  */
14713   type = cp_parser_class_head (parser,
14714                                &nested_name_specifier_p,
14715                                &attributes,
14716                                &bases);
14717   /* If the class-head was a semantic disaster, skip the entire body
14718      of the class.  */
14719   if (!type)
14720     {
14721       cp_parser_skip_to_end_of_block_or_statement (parser);
14722       pop_deferring_access_checks ();
14723       return error_mark_node;
14724     }
14725
14726   /* Look for the `{'.  */
14727   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14728     {
14729       pop_deferring_access_checks ();
14730       return error_mark_node;
14731     }
14732
14733   /* Process the base classes. If they're invalid, skip the 
14734      entire class body.  */
14735   if (!xref_basetypes (type, bases))
14736     {
14737       /* Consuming the closing brace yields better error messages
14738          later on.  */
14739       if (cp_parser_skip_to_closing_brace (parser))
14740         cp_lexer_consume_token (parser->lexer);
14741       pop_deferring_access_checks ();
14742       return error_mark_node;
14743     }
14744
14745   /* Issue an error message if type-definitions are forbidden here.  */
14746   cp_parser_check_type_definition (parser);
14747   /* Remember that we are defining one more class.  */
14748   ++parser->num_classes_being_defined;
14749   /* Inside the class, surrounding template-parameter-lists do not
14750      apply.  */
14751   saved_num_template_parameter_lists
14752     = parser->num_template_parameter_lists;
14753   parser->num_template_parameter_lists = 0;
14754   /* We are not in a function body.  */
14755   saved_in_function_body = parser->in_function_body;
14756   parser->in_function_body = false;
14757
14758   /* Start the class.  */
14759   if (nested_name_specifier_p)
14760     {
14761       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14762       old_scope = push_inner_scope (scope);
14763     }
14764   type = begin_class_definition (type, attributes);
14765
14766   if (type == error_mark_node)
14767     /* If the type is erroneous, skip the entire body of the class.  */
14768     cp_parser_skip_to_closing_brace (parser);
14769   else
14770     /* Parse the member-specification.  */
14771     cp_parser_member_specification_opt (parser);
14772
14773   /* Look for the trailing `}'.  */
14774   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14775   /* We get better error messages by noticing a common problem: a
14776      missing trailing `;'.  */
14777   token = cp_lexer_peek_token (parser->lexer);
14778   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14779   /* Look for trailing attributes to apply to this class.  */
14780   if (cp_parser_allow_gnu_extensions_p (parser))
14781     attributes = cp_parser_attributes_opt (parser);
14782   if (type != error_mark_node)
14783     type = finish_struct (type, attributes);
14784   if (nested_name_specifier_p)
14785     pop_inner_scope (old_scope, scope);
14786   /* If this class is not itself within the scope of another class,
14787      then we need to parse the bodies of all of the queued function
14788      definitions.  Note that the queued functions defined in a class
14789      are not always processed immediately following the
14790      class-specifier for that class.  Consider:
14791
14792        struct A {
14793          struct B { void f() { sizeof (A); } };
14794        };
14795
14796      If `f' were processed before the processing of `A' were
14797      completed, there would be no way to compute the size of `A'.
14798      Note that the nesting we are interested in here is lexical --
14799      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14800      for:
14801
14802        struct A { struct B; };
14803        struct A::B { void f() { } };
14804
14805      there is no need to delay the parsing of `A::B::f'.  */
14806   if (--parser->num_classes_being_defined == 0)
14807     {
14808       tree queue_entry;
14809       tree fn;
14810       tree class_type = NULL_TREE;
14811       tree pushed_scope = NULL_TREE;
14812
14813       /* In a first pass, parse default arguments to the functions.
14814          Then, in a second pass, parse the bodies of the functions.
14815          This two-phased approach handles cases like:
14816
14817             struct S {
14818               void f() { g(); }
14819               void g(int i = 3);
14820             };
14821
14822          */
14823       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14824              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14825            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14826            TREE_PURPOSE (parser->unparsed_functions_queues)
14827              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14828         {
14829           fn = TREE_VALUE (queue_entry);
14830           /* If there are default arguments that have not yet been processed,
14831              take care of them now.  */
14832           if (class_type != TREE_PURPOSE (queue_entry))
14833             {
14834               if (pushed_scope)
14835                 pop_scope (pushed_scope);
14836               class_type = TREE_PURPOSE (queue_entry);
14837               pushed_scope = push_scope (class_type);
14838             }
14839           /* Make sure that any template parameters are in scope.  */
14840           maybe_begin_member_template_processing (fn);
14841           /* Parse the default argument expressions.  */
14842           cp_parser_late_parsing_default_args (parser, fn);
14843           /* Remove any template parameters from the symbol table.  */
14844           maybe_end_member_template_processing ();
14845         }
14846       if (pushed_scope)
14847         pop_scope (pushed_scope);
14848       /* Now parse the body of the functions.  */
14849       for (TREE_VALUE (parser->unparsed_functions_queues)
14850              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14851            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14852            TREE_VALUE (parser->unparsed_functions_queues)
14853              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14854         {
14855           /* Figure out which function we need to process.  */
14856           fn = TREE_VALUE (queue_entry);
14857           /* Parse the function.  */
14858           cp_parser_late_parsing_for_member (parser, fn);
14859         }
14860     }
14861
14862   /* Put back any saved access checks.  */
14863   pop_deferring_access_checks ();
14864
14865   /* Restore saved state.  */
14866   parser->in_function_body = saved_in_function_body;
14867   parser->num_template_parameter_lists
14868     = saved_num_template_parameter_lists;
14869
14870   return type;
14871 }
14872
14873 /* Parse a class-head.
14874
14875    class-head:
14876      class-key identifier [opt] base-clause [opt]
14877      class-key nested-name-specifier identifier base-clause [opt]
14878      class-key nested-name-specifier [opt] template-id
14879        base-clause [opt]
14880
14881    GNU Extensions:
14882      class-key attributes identifier [opt] base-clause [opt]
14883      class-key attributes nested-name-specifier identifier base-clause [opt]
14884      class-key attributes nested-name-specifier [opt] template-id
14885        base-clause [opt]
14886
14887    Upon return BASES is initialized to the list of base classes (or
14888    NULL, if there are none) in the same form returned by
14889    cp_parser_base_clause.
14890
14891    Returns the TYPE of the indicated class.  Sets
14892    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14893    involving a nested-name-specifier was used, and FALSE otherwise.
14894
14895    Returns error_mark_node if this is not a class-head.
14896
14897    Returns NULL_TREE if the class-head is syntactically valid, but
14898    semantically invalid in a way that means we should skip the entire
14899    body of the class.  */
14900
14901 static tree
14902 cp_parser_class_head (cp_parser* parser,
14903                       bool* nested_name_specifier_p,
14904                       tree *attributes_p,
14905                       tree *bases)
14906 {
14907   tree nested_name_specifier;
14908   enum tag_types class_key;
14909   tree id = NULL_TREE;
14910   tree type = NULL_TREE;
14911   tree attributes;
14912   bool template_id_p = false;
14913   bool qualified_p = false;
14914   bool invalid_nested_name_p = false;
14915   bool invalid_explicit_specialization_p = false;
14916   tree pushed_scope = NULL_TREE;
14917   unsigned num_templates;
14918   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14919   /* Assume no nested-name-specifier will be present.  */
14920   *nested_name_specifier_p = false;
14921   /* Assume no template parameter lists will be used in defining the
14922      type.  */
14923   num_templates = 0;
14924
14925   *bases = NULL_TREE;
14926
14927   /* Look for the class-key.  */
14928   class_key = cp_parser_class_key (parser);
14929   if (class_key == none_type)
14930     return error_mark_node;
14931
14932   /* Parse the attributes.  */
14933   attributes = cp_parser_attributes_opt (parser);
14934
14935   /* If the next token is `::', that is invalid -- but sometimes
14936      people do try to write:
14937
14938        struct ::S {};
14939
14940      Handle this gracefully by accepting the extra qualifier, and then
14941      issuing an error about it later if this really is a
14942      class-head.  If it turns out just to be an elaborated type
14943      specifier, remain silent.  */
14944   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14945     qualified_p = true;
14946
14947   push_deferring_access_checks (dk_no_check);
14948
14949   /* Determine the name of the class.  Begin by looking for an
14950      optional nested-name-specifier.  */
14951   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14952   nested_name_specifier
14953     = cp_parser_nested_name_specifier_opt (parser,
14954                                            /*typename_keyword_p=*/false,
14955                                            /*check_dependency_p=*/false,
14956                                            /*type_p=*/false,
14957                                            /*is_declaration=*/false);
14958   /* If there was a nested-name-specifier, then there *must* be an
14959      identifier.  */
14960   if (nested_name_specifier)
14961     {
14962       type_start_token = cp_lexer_peek_token (parser->lexer);
14963       /* Although the grammar says `identifier', it really means
14964          `class-name' or `template-name'.  You are only allowed to
14965          define a class that has already been declared with this
14966          syntax.
14967
14968          The proposed resolution for Core Issue 180 says that wherever
14969          you see `class T::X' you should treat `X' as a type-name.
14970
14971          It is OK to define an inaccessible class; for example:
14972
14973            class A { class B; };
14974            class A::B {};
14975
14976          We do not know if we will see a class-name, or a
14977          template-name.  We look for a class-name first, in case the
14978          class-name is a template-id; if we looked for the
14979          template-name first we would stop after the template-name.  */
14980       cp_parser_parse_tentatively (parser);
14981       type = cp_parser_class_name (parser,
14982                                    /*typename_keyword_p=*/false,
14983                                    /*template_keyword_p=*/false,
14984                                    class_type,
14985                                    /*check_dependency_p=*/false,
14986                                    /*class_head_p=*/true,
14987                                    /*is_declaration=*/false);
14988       /* If that didn't work, ignore the nested-name-specifier.  */
14989       if (!cp_parser_parse_definitely (parser))
14990         {
14991           invalid_nested_name_p = true;
14992           type_start_token = cp_lexer_peek_token (parser->lexer);
14993           id = cp_parser_identifier (parser);
14994           if (id == error_mark_node)
14995             id = NULL_TREE;
14996         }
14997       /* If we could not find a corresponding TYPE, treat this
14998          declaration like an unqualified declaration.  */
14999       if (type == error_mark_node)
15000         nested_name_specifier = NULL_TREE;
15001       /* Otherwise, count the number of templates used in TYPE and its
15002          containing scopes.  */
15003       else
15004         {
15005           tree scope;
15006
15007           for (scope = TREE_TYPE (type);
15008                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15009                scope = (TYPE_P (scope)
15010                         ? TYPE_CONTEXT (scope)
15011                         : DECL_CONTEXT (scope)))
15012             if (TYPE_P (scope)
15013                 && CLASS_TYPE_P (scope)
15014                 && CLASSTYPE_TEMPLATE_INFO (scope)
15015                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15016                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15017               ++num_templates;
15018         }
15019     }
15020   /* Otherwise, the identifier is optional.  */
15021   else
15022     {
15023       /* We don't know whether what comes next is a template-id,
15024          an identifier, or nothing at all.  */
15025       cp_parser_parse_tentatively (parser);
15026       /* Check for a template-id.  */
15027       type_start_token = cp_lexer_peek_token (parser->lexer);
15028       id = cp_parser_template_id (parser,
15029                                   /*template_keyword_p=*/false,
15030                                   /*check_dependency_p=*/true,
15031                                   /*is_declaration=*/true);
15032       /* If that didn't work, it could still be an identifier.  */
15033       if (!cp_parser_parse_definitely (parser))
15034         {
15035           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15036             {
15037               type_start_token = cp_lexer_peek_token (parser->lexer);
15038               id = cp_parser_identifier (parser);
15039             }
15040           else
15041             id = NULL_TREE;
15042         }
15043       else
15044         {
15045           template_id_p = true;
15046           ++num_templates;
15047         }
15048     }
15049
15050   pop_deferring_access_checks ();
15051
15052   if (id)
15053     cp_parser_check_for_invalid_template_id (parser, id,
15054                                              type_start_token->location);
15055
15056   /* If it's not a `:' or a `{' then we can't really be looking at a
15057      class-head, since a class-head only appears as part of a
15058      class-specifier.  We have to detect this situation before calling
15059      xref_tag, since that has irreversible side-effects.  */
15060   if (!cp_parser_next_token_starts_class_definition_p (parser))
15061     {
15062       cp_parser_error (parser, "expected %<{%> or %<:%>");
15063       return error_mark_node;
15064     }
15065
15066   /* At this point, we're going ahead with the class-specifier, even
15067      if some other problem occurs.  */
15068   cp_parser_commit_to_tentative_parse (parser);
15069   /* Issue the error about the overly-qualified name now.  */
15070   if (qualified_p)
15071     {
15072       cp_parser_error (parser,
15073                        "global qualification of class name is invalid");
15074       return error_mark_node;
15075     }
15076   else if (invalid_nested_name_p)
15077     {
15078       cp_parser_error (parser,
15079                        "qualified name does not name a class");
15080       return error_mark_node;
15081     }
15082   else if (nested_name_specifier)
15083     {
15084       tree scope;
15085
15086       /* Reject typedef-names in class heads.  */
15087       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15088         {
15089           error ("%Hinvalid class name in declaration of %qD",
15090                  &type_start_token->location, type);
15091           type = NULL_TREE;
15092           goto done;
15093         }
15094
15095       /* Figure out in what scope the declaration is being placed.  */
15096       scope = current_scope ();
15097       /* If that scope does not contain the scope in which the
15098          class was originally declared, the program is invalid.  */
15099       if (scope && !is_ancestor (scope, nested_name_specifier))
15100         {
15101           if (at_namespace_scope_p ())
15102             error ("%Hdeclaration of %qD in namespace %qD which does not "
15103                    "enclose %qD",
15104                    &type_start_token->location,
15105                    type, scope, nested_name_specifier);
15106           else
15107             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15108                    &type_start_token->location,
15109                    type, scope, nested_name_specifier);
15110           type = NULL_TREE;
15111           goto done;
15112         }
15113       /* [dcl.meaning]
15114
15115          A declarator-id shall not be qualified except for the
15116          definition of a ... nested class outside of its class
15117          ... [or] the definition or explicit instantiation of a
15118          class member of a namespace outside of its namespace.  */
15119       if (scope == nested_name_specifier)
15120         {
15121           permerror (input_location, "%Hextra qualification not allowed",
15122                      &nested_name_specifier_token_start->location);
15123           nested_name_specifier = NULL_TREE;
15124           num_templates = 0;
15125         }
15126     }
15127   /* An explicit-specialization must be preceded by "template <>".  If
15128      it is not, try to recover gracefully.  */
15129   if (at_namespace_scope_p ()
15130       && parser->num_template_parameter_lists == 0
15131       && template_id_p)
15132     {
15133       error ("%Han explicit specialization must be preceded by %<template <>%>",
15134              &type_start_token->location);
15135       invalid_explicit_specialization_p = true;
15136       /* Take the same action that would have been taken by
15137          cp_parser_explicit_specialization.  */
15138       ++parser->num_template_parameter_lists;
15139       begin_specialization ();
15140     }
15141   /* There must be no "return" statements between this point and the
15142      end of this function; set "type "to the correct return value and
15143      use "goto done;" to return.  */
15144   /* Make sure that the right number of template parameters were
15145      present.  */
15146   if (!cp_parser_check_template_parameters (parser, num_templates,
15147                                             type_start_token->location))
15148     {
15149       /* If something went wrong, there is no point in even trying to
15150          process the class-definition.  */
15151       type = NULL_TREE;
15152       goto done;
15153     }
15154
15155   /* Look up the type.  */
15156   if (template_id_p)
15157     {
15158       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15159           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15160               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15161         {
15162           error ("%Hfunction template %qD redeclared as a class template",
15163                  &type_start_token->location, id);
15164           type = error_mark_node;
15165         }
15166       else
15167         {
15168           type = TREE_TYPE (id);
15169           type = maybe_process_partial_specialization (type);
15170         }
15171       if (nested_name_specifier)
15172         pushed_scope = push_scope (nested_name_specifier);
15173     }
15174   else if (nested_name_specifier)
15175     {
15176       tree class_type;
15177
15178       /* Given:
15179
15180             template <typename T> struct S { struct T };
15181             template <typename T> struct S<T>::T { };
15182
15183          we will get a TYPENAME_TYPE when processing the definition of
15184          `S::T'.  We need to resolve it to the actual type before we
15185          try to define it.  */
15186       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15187         {
15188           class_type = resolve_typename_type (TREE_TYPE (type),
15189                                               /*only_current_p=*/false);
15190           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15191             type = TYPE_NAME (class_type);
15192           else
15193             {
15194               cp_parser_error (parser, "could not resolve typename type");
15195               type = error_mark_node;
15196             }
15197         }
15198
15199       if (maybe_process_partial_specialization (TREE_TYPE (type))
15200           == error_mark_node)
15201         {
15202           type = NULL_TREE;
15203           goto done;
15204         }
15205
15206       class_type = current_class_type;
15207       /* Enter the scope indicated by the nested-name-specifier.  */
15208       pushed_scope = push_scope (nested_name_specifier);
15209       /* Get the canonical version of this type.  */
15210       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15211       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15212           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15213         {
15214           type = push_template_decl (type);
15215           if (type == error_mark_node)
15216             {
15217               type = NULL_TREE;
15218               goto done;
15219             }
15220         }
15221
15222       type = TREE_TYPE (type);
15223       *nested_name_specifier_p = true;
15224     }
15225   else      /* The name is not a nested name.  */
15226     {
15227       /* If the class was unnamed, create a dummy name.  */
15228       if (!id)
15229         id = make_anon_name ();
15230       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15231                        parser->num_template_parameter_lists);
15232     }
15233
15234   /* Indicate whether this class was declared as a `class' or as a
15235      `struct'.  */
15236   if (TREE_CODE (type) == RECORD_TYPE)
15237     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15238   cp_parser_check_class_key (class_key, type);
15239
15240   /* If this type was already complete, and we see another definition,
15241      that's an error.  */
15242   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15243     {
15244       error ("%Hredefinition of %q#T",
15245              &type_start_token->location, type);
15246       error ("%Hprevious definition of %q+#T",
15247              &type_start_token->location, type);
15248       type = NULL_TREE;
15249       goto done;
15250     }
15251   else if (type == error_mark_node)
15252     type = NULL_TREE;
15253
15254   /* We will have entered the scope containing the class; the names of
15255      base classes should be looked up in that context.  For example:
15256
15257        struct A { struct B {}; struct C; };
15258        struct A::C : B {};
15259
15260      is valid.  */
15261
15262   /* Get the list of base-classes, if there is one.  */
15263   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15264     *bases = cp_parser_base_clause (parser);
15265
15266  done:
15267   /* Leave the scope given by the nested-name-specifier.  We will
15268      enter the class scope itself while processing the members.  */
15269   if (pushed_scope)
15270     pop_scope (pushed_scope);
15271
15272   if (invalid_explicit_specialization_p)
15273     {
15274       end_specialization ();
15275       --parser->num_template_parameter_lists;
15276     }
15277   *attributes_p = attributes;
15278   return type;
15279 }
15280
15281 /* Parse a class-key.
15282
15283    class-key:
15284      class
15285      struct
15286      union
15287
15288    Returns the kind of class-key specified, or none_type to indicate
15289    error.  */
15290
15291 static enum tag_types
15292 cp_parser_class_key (cp_parser* parser)
15293 {
15294   cp_token *token;
15295   enum tag_types tag_type;
15296
15297   /* Look for the class-key.  */
15298   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15299   if (!token)
15300     return none_type;
15301
15302   /* Check to see if the TOKEN is a class-key.  */
15303   tag_type = cp_parser_token_is_class_key (token);
15304   if (!tag_type)
15305     cp_parser_error (parser, "expected class-key");
15306   return tag_type;
15307 }
15308
15309 /* Parse an (optional) member-specification.
15310
15311    member-specification:
15312      member-declaration member-specification [opt]
15313      access-specifier : member-specification [opt]  */
15314
15315 static void
15316 cp_parser_member_specification_opt (cp_parser* parser)
15317 {
15318   while (true)
15319     {
15320       cp_token *token;
15321       enum rid keyword;
15322
15323       /* Peek at the next token.  */
15324       token = cp_lexer_peek_token (parser->lexer);
15325       /* If it's a `}', or EOF then we've seen all the members.  */
15326       if (token->type == CPP_CLOSE_BRACE
15327           || token->type == CPP_EOF
15328           || token->type == CPP_PRAGMA_EOL)
15329         break;
15330
15331       /* See if this token is a keyword.  */
15332       keyword = token->keyword;
15333       switch (keyword)
15334         {
15335         case RID_PUBLIC:
15336         case RID_PROTECTED:
15337         case RID_PRIVATE:
15338           /* Consume the access-specifier.  */
15339           cp_lexer_consume_token (parser->lexer);
15340           /* Remember which access-specifier is active.  */
15341           current_access_specifier = token->u.value;
15342           /* Look for the `:'.  */
15343           cp_parser_require (parser, CPP_COLON, "%<:%>");
15344           break;
15345
15346         default:
15347           /* Accept #pragmas at class scope.  */
15348           if (token->type == CPP_PRAGMA)
15349             {
15350               cp_parser_pragma (parser, pragma_external);
15351               break;
15352             }
15353
15354           /* Otherwise, the next construction must be a
15355              member-declaration.  */
15356           cp_parser_member_declaration (parser);
15357         }
15358     }
15359 }
15360
15361 /* Parse a member-declaration.
15362
15363    member-declaration:
15364      decl-specifier-seq [opt] member-declarator-list [opt] ;
15365      function-definition ; [opt]
15366      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15367      using-declaration
15368      template-declaration
15369
15370    member-declarator-list:
15371      member-declarator
15372      member-declarator-list , member-declarator
15373
15374    member-declarator:
15375      declarator pure-specifier [opt]
15376      declarator constant-initializer [opt]
15377      identifier [opt] : constant-expression
15378
15379    GNU Extensions:
15380
15381    member-declaration:
15382      __extension__ member-declaration
15383
15384    member-declarator:
15385      declarator attributes [opt] pure-specifier [opt]
15386      declarator attributes [opt] constant-initializer [opt]
15387      identifier [opt] attributes [opt] : constant-expression  
15388
15389    C++0x Extensions:
15390
15391    member-declaration:
15392      static_assert-declaration  */
15393
15394 static void
15395 cp_parser_member_declaration (cp_parser* parser)
15396 {
15397   cp_decl_specifier_seq decl_specifiers;
15398   tree prefix_attributes;
15399   tree decl;
15400   int declares_class_or_enum;
15401   bool friend_p;
15402   cp_token *token = NULL;
15403   cp_token *decl_spec_token_start = NULL;
15404   cp_token *initializer_token_start = NULL;
15405   int saved_pedantic;
15406
15407   /* Check for the `__extension__' keyword.  */
15408   if (cp_parser_extension_opt (parser, &saved_pedantic))
15409     {
15410       /* Recurse.  */
15411       cp_parser_member_declaration (parser);
15412       /* Restore the old value of the PEDANTIC flag.  */
15413       pedantic = saved_pedantic;
15414
15415       return;
15416     }
15417
15418   /* Check for a template-declaration.  */
15419   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15420     {
15421       /* An explicit specialization here is an error condition, and we
15422          expect the specialization handler to detect and report this.  */
15423       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15424           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15425         cp_parser_explicit_specialization (parser);
15426       else
15427         cp_parser_template_declaration (parser, /*member_p=*/true);
15428
15429       return;
15430     }
15431
15432   /* Check for a using-declaration.  */
15433   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15434     {
15435       /* Parse the using-declaration.  */
15436       cp_parser_using_declaration (parser,
15437                                    /*access_declaration_p=*/false);
15438       return;
15439     }
15440
15441   /* Check for @defs.  */
15442   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15443     {
15444       tree ivar, member;
15445       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15446       ivar = ivar_chains;
15447       while (ivar)
15448         {
15449           member = ivar;
15450           ivar = TREE_CHAIN (member);
15451           TREE_CHAIN (member) = NULL_TREE;
15452           finish_member_declaration (member);
15453         }
15454       return;
15455     }
15456
15457   /* If the next token is `static_assert' we have a static assertion.  */
15458   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15459     {
15460       cp_parser_static_assert (parser, /*member_p=*/true);
15461       return;
15462     }
15463
15464   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15465     return;
15466
15467   /* Parse the decl-specifier-seq.  */
15468   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15469   cp_parser_decl_specifier_seq (parser,
15470                                 CP_PARSER_FLAGS_OPTIONAL,
15471                                 &decl_specifiers,
15472                                 &declares_class_or_enum);
15473   prefix_attributes = decl_specifiers.attributes;
15474   decl_specifiers.attributes = NULL_TREE;
15475   /* Check for an invalid type-name.  */
15476   if (!decl_specifiers.type
15477       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15478     return;
15479   /* If there is no declarator, then the decl-specifier-seq should
15480      specify a type.  */
15481   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15482     {
15483       /* If there was no decl-specifier-seq, and the next token is a
15484          `;', then we have something like:
15485
15486            struct S { ; };
15487
15488          [class.mem]
15489
15490          Each member-declaration shall declare at least one member
15491          name of the class.  */
15492       if (!decl_specifiers.any_specifiers_p)
15493         {
15494           cp_token *token = cp_lexer_peek_token (parser->lexer);
15495           if (!in_system_header_at (token->location))
15496             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15497         }
15498       else
15499         {
15500           tree type;
15501
15502           /* See if this declaration is a friend.  */
15503           friend_p = cp_parser_friend_p (&decl_specifiers);
15504           /* If there were decl-specifiers, check to see if there was
15505              a class-declaration.  */
15506           type = check_tag_decl (&decl_specifiers);
15507           /* Nested classes have already been added to the class, but
15508              a `friend' needs to be explicitly registered.  */
15509           if (friend_p)
15510             {
15511               /* If the `friend' keyword was present, the friend must
15512                  be introduced with a class-key.  */
15513                if (!declares_class_or_enum)
15514                  error ("%Ha class-key must be used when declaring a friend",
15515                         &decl_spec_token_start->location);
15516                /* In this case:
15517
15518                     template <typename T> struct A {
15519                       friend struct A<T>::B;
15520                     };
15521
15522                   A<T>::B will be represented by a TYPENAME_TYPE, and
15523                   therefore not recognized by check_tag_decl.  */
15524                if (!type
15525                    && decl_specifiers.type
15526                    && TYPE_P (decl_specifiers.type))
15527                  type = decl_specifiers.type;
15528                if (!type || !TYPE_P (type))
15529                  error ("%Hfriend declaration does not name a class or "
15530                         "function", &decl_spec_token_start->location);
15531                else
15532                  make_friend_class (current_class_type, type,
15533                                     /*complain=*/true);
15534             }
15535           /* If there is no TYPE, an error message will already have
15536              been issued.  */
15537           else if (!type || type == error_mark_node)
15538             ;
15539           /* An anonymous aggregate has to be handled specially; such
15540              a declaration really declares a data member (with a
15541              particular type), as opposed to a nested class.  */
15542           else if (ANON_AGGR_TYPE_P (type))
15543             {
15544               /* Remove constructors and such from TYPE, now that we
15545                  know it is an anonymous aggregate.  */
15546               fixup_anonymous_aggr (type);
15547               /* And make the corresponding data member.  */
15548               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15549               /* Add it to the class.  */
15550               finish_member_declaration (decl);
15551             }
15552           else
15553             cp_parser_check_access_in_redeclaration
15554                                               (TYPE_NAME (type),
15555                                                decl_spec_token_start->location);
15556         }
15557     }
15558   else
15559     {
15560       /* See if these declarations will be friends.  */
15561       friend_p = cp_parser_friend_p (&decl_specifiers);
15562
15563       /* Keep going until we hit the `;' at the end of the
15564          declaration.  */
15565       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15566         {
15567           tree attributes = NULL_TREE;
15568           tree first_attribute;
15569
15570           /* Peek at the next token.  */
15571           token = cp_lexer_peek_token (parser->lexer);
15572
15573           /* Check for a bitfield declaration.  */
15574           if (token->type == CPP_COLON
15575               || (token->type == CPP_NAME
15576                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15577                   == CPP_COLON))
15578             {
15579               tree identifier;
15580               tree width;
15581
15582               /* Get the name of the bitfield.  Note that we cannot just
15583                  check TOKEN here because it may have been invalidated by
15584                  the call to cp_lexer_peek_nth_token above.  */
15585               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15586                 identifier = cp_parser_identifier (parser);
15587               else
15588                 identifier = NULL_TREE;
15589
15590               /* Consume the `:' token.  */
15591               cp_lexer_consume_token (parser->lexer);
15592               /* Get the width of the bitfield.  */
15593               width
15594                 = cp_parser_constant_expression (parser,
15595                                                  /*allow_non_constant=*/false,
15596                                                  NULL);
15597
15598               /* Look for attributes that apply to the bitfield.  */
15599               attributes = cp_parser_attributes_opt (parser);
15600               /* Remember which attributes are prefix attributes and
15601                  which are not.  */
15602               first_attribute = attributes;
15603               /* Combine the attributes.  */
15604               attributes = chainon (prefix_attributes, attributes);
15605
15606               /* Create the bitfield declaration.  */
15607               decl = grokbitfield (identifier
15608                                    ? make_id_declarator (NULL_TREE,
15609                                                          identifier,
15610                                                          sfk_none)
15611                                    : NULL,
15612                                    &decl_specifiers,
15613                                    width,
15614                                    attributes);
15615             }
15616           else
15617             {
15618               cp_declarator *declarator;
15619               tree initializer;
15620               tree asm_specification;
15621               int ctor_dtor_or_conv_p;
15622
15623               /* Parse the declarator.  */
15624               declarator
15625                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15626                                         &ctor_dtor_or_conv_p,
15627                                         /*parenthesized_p=*/NULL,
15628                                         /*member_p=*/true);
15629
15630               /* If something went wrong parsing the declarator, make sure
15631                  that we at least consume some tokens.  */
15632               if (declarator == cp_error_declarator)
15633                 {
15634                   /* Skip to the end of the statement.  */
15635                   cp_parser_skip_to_end_of_statement (parser);
15636                   /* If the next token is not a semicolon, that is
15637                      probably because we just skipped over the body of
15638                      a function.  So, we consume a semicolon if
15639                      present, but do not issue an error message if it
15640                      is not present.  */
15641                   if (cp_lexer_next_token_is (parser->lexer,
15642                                               CPP_SEMICOLON))
15643                     cp_lexer_consume_token (parser->lexer);
15644                   return;
15645                 }
15646
15647               if (declares_class_or_enum & 2)
15648                 cp_parser_check_for_definition_in_return_type
15649                                             (declarator, decl_specifiers.type,
15650                                              decl_specifiers.type_location);
15651
15652               /* Look for an asm-specification.  */
15653               asm_specification = cp_parser_asm_specification_opt (parser);
15654               /* Look for attributes that apply to the declaration.  */
15655               attributes = cp_parser_attributes_opt (parser);
15656               /* Remember which attributes are prefix attributes and
15657                  which are not.  */
15658               first_attribute = attributes;
15659               /* Combine the attributes.  */
15660               attributes = chainon (prefix_attributes, attributes);
15661
15662               /* If it's an `=', then we have a constant-initializer or a
15663                  pure-specifier.  It is not correct to parse the
15664                  initializer before registering the member declaration
15665                  since the member declaration should be in scope while
15666                  its initializer is processed.  However, the rest of the
15667                  front end does not yet provide an interface that allows
15668                  us to handle this correctly.  */
15669               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15670                 {
15671                   /* In [class.mem]:
15672
15673                      A pure-specifier shall be used only in the declaration of
15674                      a virtual function.
15675
15676                      A member-declarator can contain a constant-initializer
15677                      only if it declares a static member of integral or
15678                      enumeration type.
15679
15680                      Therefore, if the DECLARATOR is for a function, we look
15681                      for a pure-specifier; otherwise, we look for a
15682                      constant-initializer.  When we call `grokfield', it will
15683                      perform more stringent semantics checks.  */
15684                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15685                   if (function_declarator_p (declarator))
15686                     initializer = cp_parser_pure_specifier (parser);
15687                   else
15688                     /* Parse the initializer.  */
15689                     initializer = cp_parser_constant_initializer (parser);
15690                 }
15691               /* Otherwise, there is no initializer.  */
15692               else
15693                 initializer = NULL_TREE;
15694
15695               /* See if we are probably looking at a function
15696                  definition.  We are certainly not looking at a
15697                  member-declarator.  Calling `grokfield' has
15698                  side-effects, so we must not do it unless we are sure
15699                  that we are looking at a member-declarator.  */
15700               if (cp_parser_token_starts_function_definition_p
15701                   (cp_lexer_peek_token (parser->lexer)))
15702                 {
15703                   /* The grammar does not allow a pure-specifier to be
15704                      used when a member function is defined.  (It is
15705                      possible that this fact is an oversight in the
15706                      standard, since a pure function may be defined
15707                      outside of the class-specifier.  */
15708                   if (initializer)
15709                     error ("%Hpure-specifier on function-definition",
15710                            &initializer_token_start->location);
15711                   decl = cp_parser_save_member_function_body (parser,
15712                                                               &decl_specifiers,
15713                                                               declarator,
15714                                                               attributes);
15715                   /* If the member was not a friend, declare it here.  */
15716                   if (!friend_p)
15717                     finish_member_declaration (decl);
15718                   /* Peek at the next token.  */
15719                   token = cp_lexer_peek_token (parser->lexer);
15720                   /* If the next token is a semicolon, consume it.  */
15721                   if (token->type == CPP_SEMICOLON)
15722                     cp_lexer_consume_token (parser->lexer);
15723                   return;
15724                 }
15725               else
15726                 /* Create the declaration.  */
15727                 decl = grokfield (declarator, &decl_specifiers,
15728                                   initializer, /*init_const_expr_p=*/true,
15729                                   asm_specification,
15730                                   attributes);
15731             }
15732
15733           /* Reset PREFIX_ATTRIBUTES.  */
15734           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15735             attributes = TREE_CHAIN (attributes);
15736           if (attributes)
15737             TREE_CHAIN (attributes) = NULL_TREE;
15738
15739           /* If there is any qualification still in effect, clear it
15740              now; we will be starting fresh with the next declarator.  */
15741           parser->scope = NULL_TREE;
15742           parser->qualifying_scope = NULL_TREE;
15743           parser->object_scope = NULL_TREE;
15744           /* If it's a `,', then there are more declarators.  */
15745           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15746             cp_lexer_consume_token (parser->lexer);
15747           /* If the next token isn't a `;', then we have a parse error.  */
15748           else if (cp_lexer_next_token_is_not (parser->lexer,
15749                                                CPP_SEMICOLON))
15750             {
15751               cp_parser_error (parser, "expected %<;%>");
15752               /* Skip tokens until we find a `;'.  */
15753               cp_parser_skip_to_end_of_statement (parser);
15754
15755               break;
15756             }
15757
15758           if (decl)
15759             {
15760               /* Add DECL to the list of members.  */
15761               if (!friend_p)
15762                 finish_member_declaration (decl);
15763
15764               if (TREE_CODE (decl) == FUNCTION_DECL)
15765                 cp_parser_save_default_args (parser, decl);
15766             }
15767         }
15768     }
15769
15770   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15771 }
15772
15773 /* Parse a pure-specifier.
15774
15775    pure-specifier:
15776      = 0
15777
15778    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15779    Otherwise, ERROR_MARK_NODE is returned.  */
15780
15781 static tree
15782 cp_parser_pure_specifier (cp_parser* parser)
15783 {
15784   cp_token *token;
15785
15786   /* Look for the `=' token.  */
15787   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15788     return error_mark_node;
15789   /* Look for the `0' token.  */
15790   token = cp_lexer_consume_token (parser->lexer);
15791
15792   /* Accept = default or = delete in c++0x mode.  */
15793   if (token->keyword == RID_DEFAULT
15794       || token->keyword == RID_DELETE)
15795     {
15796       maybe_warn_cpp0x ("defaulted and deleted functions");
15797       return token->u.value;
15798     }
15799
15800   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15801   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15802     {
15803       cp_parser_error (parser,
15804                        "invalid pure specifier (only %<= 0%> is allowed)");
15805       cp_parser_skip_to_end_of_statement (parser);
15806       return error_mark_node;
15807     }
15808   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15809     {
15810       error ("%Htemplates may not be %<virtual%>", &token->location);
15811       return error_mark_node;
15812     }
15813
15814   return integer_zero_node;
15815 }
15816
15817 /* Parse a constant-initializer.
15818
15819    constant-initializer:
15820      = constant-expression
15821
15822    Returns a representation of the constant-expression.  */
15823
15824 static tree
15825 cp_parser_constant_initializer (cp_parser* parser)
15826 {
15827   /* Look for the `=' token.  */
15828   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15829     return error_mark_node;
15830
15831   /* It is invalid to write:
15832
15833        struct S { static const int i = { 7 }; };
15834
15835      */
15836   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15837     {
15838       cp_parser_error (parser,
15839                        "a brace-enclosed initializer is not allowed here");
15840       /* Consume the opening brace.  */
15841       cp_lexer_consume_token (parser->lexer);
15842       /* Skip the initializer.  */
15843       cp_parser_skip_to_closing_brace (parser);
15844       /* Look for the trailing `}'.  */
15845       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15846
15847       return error_mark_node;
15848     }
15849
15850   return cp_parser_constant_expression (parser,
15851                                         /*allow_non_constant=*/false,
15852                                         NULL);
15853 }
15854
15855 /* Derived classes [gram.class.derived] */
15856
15857 /* Parse a base-clause.
15858
15859    base-clause:
15860      : base-specifier-list
15861
15862    base-specifier-list:
15863      base-specifier ... [opt]
15864      base-specifier-list , base-specifier ... [opt]
15865
15866    Returns a TREE_LIST representing the base-classes, in the order in
15867    which they were declared.  The representation of each node is as
15868    described by cp_parser_base_specifier.
15869
15870    In the case that no bases are specified, this function will return
15871    NULL_TREE, not ERROR_MARK_NODE.  */
15872
15873 static tree
15874 cp_parser_base_clause (cp_parser* parser)
15875 {
15876   tree bases = NULL_TREE;
15877
15878   /* Look for the `:' that begins the list.  */
15879   cp_parser_require (parser, CPP_COLON, "%<:%>");
15880
15881   /* Scan the base-specifier-list.  */
15882   while (true)
15883     {
15884       cp_token *token;
15885       tree base;
15886       bool pack_expansion_p = false;
15887
15888       /* Look for the base-specifier.  */
15889       base = cp_parser_base_specifier (parser);
15890       /* Look for the (optional) ellipsis. */
15891       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15892         {
15893           /* Consume the `...'. */
15894           cp_lexer_consume_token (parser->lexer);
15895
15896           pack_expansion_p = true;
15897         }
15898
15899       /* Add BASE to the front of the list.  */
15900       if (base != error_mark_node)
15901         {
15902           if (pack_expansion_p)
15903             /* Make this a pack expansion type. */
15904             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15905           
15906
15907           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15908             {
15909               TREE_CHAIN (base) = bases;
15910               bases = base;
15911             }
15912         }
15913       /* Peek at the next token.  */
15914       token = cp_lexer_peek_token (parser->lexer);
15915       /* If it's not a comma, then the list is complete.  */
15916       if (token->type != CPP_COMMA)
15917         break;
15918       /* Consume the `,'.  */
15919       cp_lexer_consume_token (parser->lexer);
15920     }
15921
15922   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15923      base class had a qualified name.  However, the next name that
15924      appears is certainly not qualified.  */
15925   parser->scope = NULL_TREE;
15926   parser->qualifying_scope = NULL_TREE;
15927   parser->object_scope = NULL_TREE;
15928
15929   return nreverse (bases);
15930 }
15931
15932 /* Parse a base-specifier.
15933
15934    base-specifier:
15935      :: [opt] nested-name-specifier [opt] class-name
15936      virtual access-specifier [opt] :: [opt] nested-name-specifier
15937        [opt] class-name
15938      access-specifier virtual [opt] :: [opt] nested-name-specifier
15939        [opt] class-name
15940
15941    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15942    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15943    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15944    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15945
15946 static tree
15947 cp_parser_base_specifier (cp_parser* parser)
15948 {
15949   cp_token *token;
15950   bool done = false;
15951   bool virtual_p = false;
15952   bool duplicate_virtual_error_issued_p = false;
15953   bool duplicate_access_error_issued_p = false;
15954   bool class_scope_p, template_p;
15955   tree access = access_default_node;
15956   tree type;
15957
15958   /* Process the optional `virtual' and `access-specifier'.  */
15959   while (!done)
15960     {
15961       /* Peek at the next token.  */
15962       token = cp_lexer_peek_token (parser->lexer);
15963       /* Process `virtual'.  */
15964       switch (token->keyword)
15965         {
15966         case RID_VIRTUAL:
15967           /* If `virtual' appears more than once, issue an error.  */
15968           if (virtual_p && !duplicate_virtual_error_issued_p)
15969             {
15970               cp_parser_error (parser,
15971                                "%<virtual%> specified more than once in base-specified");
15972               duplicate_virtual_error_issued_p = true;
15973             }
15974
15975           virtual_p = true;
15976
15977           /* Consume the `virtual' token.  */
15978           cp_lexer_consume_token (parser->lexer);
15979
15980           break;
15981
15982         case RID_PUBLIC:
15983         case RID_PROTECTED:
15984         case RID_PRIVATE:
15985           /* If more than one access specifier appears, issue an
15986              error.  */
15987           if (access != access_default_node
15988               && !duplicate_access_error_issued_p)
15989             {
15990               cp_parser_error (parser,
15991                                "more than one access specifier in base-specified");
15992               duplicate_access_error_issued_p = true;
15993             }
15994
15995           access = ridpointers[(int) token->keyword];
15996
15997           /* Consume the access-specifier.  */
15998           cp_lexer_consume_token (parser->lexer);
15999
16000           break;
16001
16002         default:
16003           done = true;
16004           break;
16005         }
16006     }
16007   /* It is not uncommon to see programs mechanically, erroneously, use
16008      the 'typename' keyword to denote (dependent) qualified types
16009      as base classes.  */
16010   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16011     {
16012       token = cp_lexer_peek_token (parser->lexer);
16013       if (!processing_template_decl)
16014         error ("%Hkeyword %<typename%> not allowed outside of templates",
16015                &token->location);
16016       else
16017         error ("%Hkeyword %<typename%> not allowed in this context "
16018                "(the base class is implicitly a type)",
16019                &token->location);
16020       cp_lexer_consume_token (parser->lexer);
16021     }
16022
16023   /* Look for the optional `::' operator.  */
16024   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16025   /* Look for the nested-name-specifier.  The simplest way to
16026      implement:
16027
16028        [temp.res]
16029
16030        The keyword `typename' is not permitted in a base-specifier or
16031        mem-initializer; in these contexts a qualified name that
16032        depends on a template-parameter is implicitly assumed to be a
16033        type name.
16034
16035      is to pretend that we have seen the `typename' keyword at this
16036      point.  */
16037   cp_parser_nested_name_specifier_opt (parser,
16038                                        /*typename_keyword_p=*/true,
16039                                        /*check_dependency_p=*/true,
16040                                        typename_type,
16041                                        /*is_declaration=*/true);
16042   /* If the base class is given by a qualified name, assume that names
16043      we see are type names or templates, as appropriate.  */
16044   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16045   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16046
16047   /* Finally, look for the class-name.  */
16048   type = cp_parser_class_name (parser,
16049                                class_scope_p,
16050                                template_p,
16051                                typename_type,
16052                                /*check_dependency_p=*/true,
16053                                /*class_head_p=*/false,
16054                                /*is_declaration=*/true);
16055
16056   if (type == error_mark_node)
16057     return error_mark_node;
16058
16059   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16060 }
16061
16062 /* Exception handling [gram.exception] */
16063
16064 /* Parse an (optional) exception-specification.
16065
16066    exception-specification:
16067      throw ( type-id-list [opt] )
16068
16069    Returns a TREE_LIST representing the exception-specification.  The
16070    TREE_VALUE of each node is a type.  */
16071
16072 static tree
16073 cp_parser_exception_specification_opt (cp_parser* parser)
16074 {
16075   cp_token *token;
16076   tree type_id_list;
16077
16078   /* Peek at the next token.  */
16079   token = cp_lexer_peek_token (parser->lexer);
16080   /* If it's not `throw', then there's no exception-specification.  */
16081   if (!cp_parser_is_keyword (token, RID_THROW))
16082     return NULL_TREE;
16083
16084   /* Consume the `throw'.  */
16085   cp_lexer_consume_token (parser->lexer);
16086
16087   /* Look for the `('.  */
16088   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16089
16090   /* Peek at the next token.  */
16091   token = cp_lexer_peek_token (parser->lexer);
16092   /* If it's not a `)', then there is a type-id-list.  */
16093   if (token->type != CPP_CLOSE_PAREN)
16094     {
16095       const char *saved_message;
16096
16097       /* Types may not be defined in an exception-specification.  */
16098       saved_message = parser->type_definition_forbidden_message;
16099       parser->type_definition_forbidden_message
16100         = "types may not be defined in an exception-specification";
16101       /* Parse the type-id-list.  */
16102       type_id_list = cp_parser_type_id_list (parser);
16103       /* Restore the saved message.  */
16104       parser->type_definition_forbidden_message = saved_message;
16105     }
16106   else
16107     type_id_list = empty_except_spec;
16108
16109   /* Look for the `)'.  */
16110   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16111
16112   return type_id_list;
16113 }
16114
16115 /* Parse an (optional) type-id-list.
16116
16117    type-id-list:
16118      type-id ... [opt]
16119      type-id-list , type-id ... [opt]
16120
16121    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16122    in the order that the types were presented.  */
16123
16124 static tree
16125 cp_parser_type_id_list (cp_parser* parser)
16126 {
16127   tree types = NULL_TREE;
16128
16129   while (true)
16130     {
16131       cp_token *token;
16132       tree type;
16133
16134       /* Get the next type-id.  */
16135       type = cp_parser_type_id (parser);
16136       /* Parse the optional ellipsis. */
16137       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16138         {
16139           /* Consume the `...'. */
16140           cp_lexer_consume_token (parser->lexer);
16141
16142           /* Turn the type into a pack expansion expression. */
16143           type = make_pack_expansion (type);
16144         }
16145       /* Add it to the list.  */
16146       types = add_exception_specifier (types, type, /*complain=*/1);
16147       /* Peek at the next token.  */
16148       token = cp_lexer_peek_token (parser->lexer);
16149       /* If it is not a `,', we are done.  */
16150       if (token->type != CPP_COMMA)
16151         break;
16152       /* Consume the `,'.  */
16153       cp_lexer_consume_token (parser->lexer);
16154     }
16155
16156   return nreverse (types);
16157 }
16158
16159 /* Parse a try-block.
16160
16161    try-block:
16162      try compound-statement handler-seq  */
16163
16164 static tree
16165 cp_parser_try_block (cp_parser* parser)
16166 {
16167   tree try_block;
16168
16169   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16170   try_block = begin_try_block ();
16171   cp_parser_compound_statement (parser, NULL, true);
16172   finish_try_block (try_block);
16173   cp_parser_handler_seq (parser);
16174   finish_handler_sequence (try_block);
16175
16176   return try_block;
16177 }
16178
16179 /* Parse a function-try-block.
16180
16181    function-try-block:
16182      try ctor-initializer [opt] function-body handler-seq  */
16183
16184 static bool
16185 cp_parser_function_try_block (cp_parser* parser)
16186 {
16187   tree compound_stmt;
16188   tree try_block;
16189   bool ctor_initializer_p;
16190
16191   /* Look for the `try' keyword.  */
16192   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16193     return false;
16194   /* Let the rest of the front end know where we are.  */
16195   try_block = begin_function_try_block (&compound_stmt);
16196   /* Parse the function-body.  */
16197   ctor_initializer_p
16198     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16199   /* We're done with the `try' part.  */
16200   finish_function_try_block (try_block);
16201   /* Parse the handlers.  */
16202   cp_parser_handler_seq (parser);
16203   /* We're done with the handlers.  */
16204   finish_function_handler_sequence (try_block, compound_stmt);
16205
16206   return ctor_initializer_p;
16207 }
16208
16209 /* Parse a handler-seq.
16210
16211    handler-seq:
16212      handler handler-seq [opt]  */
16213
16214 static void
16215 cp_parser_handler_seq (cp_parser* parser)
16216 {
16217   while (true)
16218     {
16219       cp_token *token;
16220
16221       /* Parse the handler.  */
16222       cp_parser_handler (parser);
16223       /* Peek at the next token.  */
16224       token = cp_lexer_peek_token (parser->lexer);
16225       /* If it's not `catch' then there are no more handlers.  */
16226       if (!cp_parser_is_keyword (token, RID_CATCH))
16227         break;
16228     }
16229 }
16230
16231 /* Parse a handler.
16232
16233    handler:
16234      catch ( exception-declaration ) compound-statement  */
16235
16236 static void
16237 cp_parser_handler (cp_parser* parser)
16238 {
16239   tree handler;
16240   tree declaration;
16241
16242   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16243   handler = begin_handler ();
16244   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16245   declaration = cp_parser_exception_declaration (parser);
16246   finish_handler_parms (declaration, handler);
16247   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16248   cp_parser_compound_statement (parser, NULL, false);
16249   finish_handler (handler);
16250 }
16251
16252 /* Parse an exception-declaration.
16253
16254    exception-declaration:
16255      type-specifier-seq declarator
16256      type-specifier-seq abstract-declarator
16257      type-specifier-seq
16258      ...
16259
16260    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16261    ellipsis variant is used.  */
16262
16263 static tree
16264 cp_parser_exception_declaration (cp_parser* parser)
16265 {
16266   cp_decl_specifier_seq type_specifiers;
16267   cp_declarator *declarator;
16268   const char *saved_message;
16269
16270   /* If it's an ellipsis, it's easy to handle.  */
16271   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16272     {
16273       /* Consume the `...' token.  */
16274       cp_lexer_consume_token (parser->lexer);
16275       return NULL_TREE;
16276     }
16277
16278   /* Types may not be defined in exception-declarations.  */
16279   saved_message = parser->type_definition_forbidden_message;
16280   parser->type_definition_forbidden_message
16281     = "types may not be defined in exception-declarations";
16282
16283   /* Parse the type-specifier-seq.  */
16284   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16285                                 &type_specifiers);
16286   /* If it's a `)', then there is no declarator.  */
16287   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16288     declarator = NULL;
16289   else
16290     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16291                                        /*ctor_dtor_or_conv_p=*/NULL,
16292                                        /*parenthesized_p=*/NULL,
16293                                        /*member_p=*/false);
16294
16295   /* Restore the saved message.  */
16296   parser->type_definition_forbidden_message = saved_message;
16297
16298   if (!type_specifiers.any_specifiers_p)
16299     return error_mark_node;
16300
16301   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16302 }
16303
16304 /* Parse a throw-expression.
16305
16306    throw-expression:
16307      throw assignment-expression [opt]
16308
16309    Returns a THROW_EXPR representing the throw-expression.  */
16310
16311 static tree
16312 cp_parser_throw_expression (cp_parser* parser)
16313 {
16314   tree expression;
16315   cp_token* token;
16316
16317   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16318   token = cp_lexer_peek_token (parser->lexer);
16319   /* Figure out whether or not there is an assignment-expression
16320      following the "throw" keyword.  */
16321   if (token->type == CPP_COMMA
16322       || token->type == CPP_SEMICOLON
16323       || token->type == CPP_CLOSE_PAREN
16324       || token->type == CPP_CLOSE_SQUARE
16325       || token->type == CPP_CLOSE_BRACE
16326       || token->type == CPP_COLON)
16327     expression = NULL_TREE;
16328   else
16329     expression = cp_parser_assignment_expression (parser,
16330                                                   /*cast_p=*/false);
16331
16332   return build_throw (expression);
16333 }
16334
16335 /* GNU Extensions */
16336
16337 /* Parse an (optional) asm-specification.
16338
16339    asm-specification:
16340      asm ( string-literal )
16341
16342    If the asm-specification is present, returns a STRING_CST
16343    corresponding to the string-literal.  Otherwise, returns
16344    NULL_TREE.  */
16345
16346 static tree
16347 cp_parser_asm_specification_opt (cp_parser* parser)
16348 {
16349   cp_token *token;
16350   tree asm_specification;
16351
16352   /* Peek at the next token.  */
16353   token = cp_lexer_peek_token (parser->lexer);
16354   /* If the next token isn't the `asm' keyword, then there's no
16355      asm-specification.  */
16356   if (!cp_parser_is_keyword (token, RID_ASM))
16357     return NULL_TREE;
16358
16359   /* Consume the `asm' token.  */
16360   cp_lexer_consume_token (parser->lexer);
16361   /* Look for the `('.  */
16362   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16363
16364   /* Look for the string-literal.  */
16365   asm_specification = cp_parser_string_literal (parser, false, false);
16366
16367   /* Look for the `)'.  */
16368   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16369
16370   return asm_specification;
16371 }
16372
16373 /* Parse an asm-operand-list.
16374
16375    asm-operand-list:
16376      asm-operand
16377      asm-operand-list , asm-operand
16378
16379    asm-operand:
16380      string-literal ( expression )
16381      [ string-literal ] string-literal ( expression )
16382
16383    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16384    each node is the expression.  The TREE_PURPOSE is itself a
16385    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16386    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16387    is a STRING_CST for the string literal before the parenthesis. Returns
16388    ERROR_MARK_NODE if any of the operands are invalid.  */
16389
16390 static tree
16391 cp_parser_asm_operand_list (cp_parser* parser)
16392 {
16393   tree asm_operands = NULL_TREE;
16394   bool invalid_operands = false;
16395
16396   while (true)
16397     {
16398       tree string_literal;
16399       tree expression;
16400       tree name;
16401
16402       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16403         {
16404           /* Consume the `[' token.  */
16405           cp_lexer_consume_token (parser->lexer);
16406           /* Read the operand name.  */
16407           name = cp_parser_identifier (parser);
16408           if (name != error_mark_node)
16409             name = build_string (IDENTIFIER_LENGTH (name),
16410                                  IDENTIFIER_POINTER (name));
16411           /* Look for the closing `]'.  */
16412           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16413         }
16414       else
16415         name = NULL_TREE;
16416       /* Look for the string-literal.  */
16417       string_literal = cp_parser_string_literal (parser, false, false);
16418
16419       /* Look for the `('.  */
16420       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16421       /* Parse the expression.  */
16422       expression = cp_parser_expression (parser, /*cast_p=*/false);
16423       /* Look for the `)'.  */
16424       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16425
16426       if (name == error_mark_node 
16427           || string_literal == error_mark_node 
16428           || expression == error_mark_node)
16429         invalid_operands = true;
16430
16431       /* Add this operand to the list.  */
16432       asm_operands = tree_cons (build_tree_list (name, string_literal),
16433                                 expression,
16434                                 asm_operands);
16435       /* If the next token is not a `,', there are no more
16436          operands.  */
16437       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16438         break;
16439       /* Consume the `,'.  */
16440       cp_lexer_consume_token (parser->lexer);
16441     }
16442
16443   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16444 }
16445
16446 /* Parse an asm-clobber-list.
16447
16448    asm-clobber-list:
16449      string-literal
16450      asm-clobber-list , string-literal
16451
16452    Returns a TREE_LIST, indicating the clobbers in the order that they
16453    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16454
16455 static tree
16456 cp_parser_asm_clobber_list (cp_parser* parser)
16457 {
16458   tree clobbers = NULL_TREE;
16459
16460   while (true)
16461     {
16462       tree string_literal;
16463
16464       /* Look for the string literal.  */
16465       string_literal = cp_parser_string_literal (parser, false, false);
16466       /* Add it to the list.  */
16467       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16468       /* If the next token is not a `,', then the list is
16469          complete.  */
16470       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16471         break;
16472       /* Consume the `,' token.  */
16473       cp_lexer_consume_token (parser->lexer);
16474     }
16475
16476   return clobbers;
16477 }
16478
16479 /* Parse an (optional) series of attributes.
16480
16481    attributes:
16482      attributes attribute
16483
16484    attribute:
16485      __attribute__ (( attribute-list [opt] ))
16486
16487    The return value is as for cp_parser_attribute_list.  */
16488
16489 static tree
16490 cp_parser_attributes_opt (cp_parser* parser)
16491 {
16492   tree attributes = NULL_TREE;
16493
16494   while (true)
16495     {
16496       cp_token *token;
16497       tree attribute_list;
16498
16499       /* Peek at the next token.  */
16500       token = cp_lexer_peek_token (parser->lexer);
16501       /* If it's not `__attribute__', then we're done.  */
16502       if (token->keyword != RID_ATTRIBUTE)
16503         break;
16504
16505       /* Consume the `__attribute__' keyword.  */
16506       cp_lexer_consume_token (parser->lexer);
16507       /* Look for the two `(' tokens.  */
16508       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16509       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16510
16511       /* Peek at the next token.  */
16512       token = cp_lexer_peek_token (parser->lexer);
16513       if (token->type != CPP_CLOSE_PAREN)
16514         /* Parse the attribute-list.  */
16515         attribute_list = cp_parser_attribute_list (parser);
16516       else
16517         /* If the next token is a `)', then there is no attribute
16518            list.  */
16519         attribute_list = NULL;
16520
16521       /* Look for the two `)' tokens.  */
16522       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16523       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16524
16525       /* Add these new attributes to the list.  */
16526       attributes = chainon (attributes, attribute_list);
16527     }
16528
16529   return attributes;
16530 }
16531
16532 /* Parse an attribute-list.
16533
16534    attribute-list:
16535      attribute
16536      attribute-list , attribute
16537
16538    attribute:
16539      identifier
16540      identifier ( identifier )
16541      identifier ( identifier , expression-list )
16542      identifier ( expression-list )
16543
16544    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16545    to an attribute.  The TREE_PURPOSE of each node is the identifier
16546    indicating which attribute is in use.  The TREE_VALUE represents
16547    the arguments, if any.  */
16548
16549 static tree
16550 cp_parser_attribute_list (cp_parser* parser)
16551 {
16552   tree attribute_list = NULL_TREE;
16553   bool save_translate_strings_p = parser->translate_strings_p;
16554
16555   parser->translate_strings_p = false;
16556   while (true)
16557     {
16558       cp_token *token;
16559       tree identifier;
16560       tree attribute;
16561
16562       /* Look for the identifier.  We also allow keywords here; for
16563          example `__attribute__ ((const))' is legal.  */
16564       token = cp_lexer_peek_token (parser->lexer);
16565       if (token->type == CPP_NAME
16566           || token->type == CPP_KEYWORD)
16567         {
16568           tree arguments = NULL_TREE;
16569
16570           /* Consume the token.  */
16571           token = cp_lexer_consume_token (parser->lexer);
16572
16573           /* Save away the identifier that indicates which attribute
16574              this is.  */
16575           identifier = token->u.value;
16576           attribute = build_tree_list (identifier, NULL_TREE);
16577
16578           /* Peek at the next token.  */
16579           token = cp_lexer_peek_token (parser->lexer);
16580           /* If it's an `(', then parse the attribute arguments.  */
16581           if (token->type == CPP_OPEN_PAREN)
16582             {
16583               arguments = cp_parser_parenthesized_expression_list
16584                           (parser, true, /*cast_p=*/false,
16585                            /*allow_expansion_p=*/false,
16586                            /*non_constant_p=*/NULL);
16587               /* Save the arguments away.  */
16588               TREE_VALUE (attribute) = arguments;
16589             }
16590
16591           if (arguments != error_mark_node)
16592             {
16593               /* Add this attribute to the list.  */
16594               TREE_CHAIN (attribute) = attribute_list;
16595               attribute_list = attribute;
16596             }
16597
16598           token = cp_lexer_peek_token (parser->lexer);
16599         }
16600       /* Now, look for more attributes.  If the next token isn't a
16601          `,', we're done.  */
16602       if (token->type != CPP_COMMA)
16603         break;
16604
16605       /* Consume the comma and keep going.  */
16606       cp_lexer_consume_token (parser->lexer);
16607     }
16608   parser->translate_strings_p = save_translate_strings_p;
16609
16610   /* We built up the list in reverse order.  */
16611   return nreverse (attribute_list);
16612 }
16613
16614 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16615    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16616    current value of the PEDANTIC flag, regardless of whether or not
16617    the `__extension__' keyword is present.  The caller is responsible
16618    for restoring the value of the PEDANTIC flag.  */
16619
16620 static bool
16621 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16622 {
16623   /* Save the old value of the PEDANTIC flag.  */
16624   *saved_pedantic = pedantic;
16625
16626   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16627     {
16628       /* Consume the `__extension__' token.  */
16629       cp_lexer_consume_token (parser->lexer);
16630       /* We're not being pedantic while the `__extension__' keyword is
16631          in effect.  */
16632       pedantic = 0;
16633
16634       return true;
16635     }
16636
16637   return false;
16638 }
16639
16640 /* Parse a label declaration.
16641
16642    label-declaration:
16643      __label__ label-declarator-seq ;
16644
16645    label-declarator-seq:
16646      identifier , label-declarator-seq
16647      identifier  */
16648
16649 static void
16650 cp_parser_label_declaration (cp_parser* parser)
16651 {
16652   /* Look for the `__label__' keyword.  */
16653   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16654
16655   while (true)
16656     {
16657       tree identifier;
16658
16659       /* Look for an identifier.  */
16660       identifier = cp_parser_identifier (parser);
16661       /* If we failed, stop.  */
16662       if (identifier == error_mark_node)
16663         break;
16664       /* Declare it as a label.  */
16665       finish_label_decl (identifier);
16666       /* If the next token is a `;', stop.  */
16667       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16668         break;
16669       /* Look for the `,' separating the label declarations.  */
16670       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16671     }
16672
16673   /* Look for the final `;'.  */
16674   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16675 }
16676
16677 /* Support Functions */
16678
16679 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16680    NAME should have one of the representations used for an
16681    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16682    is returned.  If PARSER->SCOPE is a dependent type, then a
16683    SCOPE_REF is returned.
16684
16685    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16686    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16687    was formed.  Abstractly, such entities should not be passed to this
16688    function, because they do not need to be looked up, but it is
16689    simpler to check for this special case here, rather than at the
16690    call-sites.
16691
16692    In cases not explicitly covered above, this function returns a
16693    DECL, OVERLOAD, or baselink representing the result of the lookup.
16694    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16695    is returned.
16696
16697    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16698    (e.g., "struct") that was used.  In that case bindings that do not
16699    refer to types are ignored.
16700
16701    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16702    ignored.
16703
16704    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16705    are ignored.
16706
16707    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16708    types.
16709
16710    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16711    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16712    NULL_TREE otherwise.  */
16713
16714 static tree
16715 cp_parser_lookup_name (cp_parser *parser, tree name,
16716                        enum tag_types tag_type,
16717                        bool is_template,
16718                        bool is_namespace,
16719                        bool check_dependency,
16720                        tree *ambiguous_decls,
16721                        location_t name_location)
16722 {
16723   int flags = 0;
16724   tree decl;
16725   tree object_type = parser->context->object_type;
16726
16727   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16728     flags |= LOOKUP_COMPLAIN;
16729
16730   /* Assume that the lookup will be unambiguous.  */
16731   if (ambiguous_decls)
16732     *ambiguous_decls = NULL_TREE;
16733
16734   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16735      no longer valid.  Note that if we are parsing tentatively, and
16736      the parse fails, OBJECT_TYPE will be automatically restored.  */
16737   parser->context->object_type = NULL_TREE;
16738
16739   if (name == error_mark_node)
16740     return error_mark_node;
16741
16742   /* A template-id has already been resolved; there is no lookup to
16743      do.  */
16744   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16745     return name;
16746   if (BASELINK_P (name))
16747     {
16748       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16749                   == TEMPLATE_ID_EXPR);
16750       return name;
16751     }
16752
16753   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16754      it should already have been checked to make sure that the name
16755      used matches the type being destroyed.  */
16756   if (TREE_CODE (name) == BIT_NOT_EXPR)
16757     {
16758       tree type;
16759
16760       /* Figure out to which type this destructor applies.  */
16761       if (parser->scope)
16762         type = parser->scope;
16763       else if (object_type)
16764         type = object_type;
16765       else
16766         type = current_class_type;
16767       /* If that's not a class type, there is no destructor.  */
16768       if (!type || !CLASS_TYPE_P (type))
16769         return error_mark_node;
16770       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16771         lazily_declare_fn (sfk_destructor, type);
16772       if (!CLASSTYPE_DESTRUCTORS (type))
16773           return error_mark_node;
16774       /* If it was a class type, return the destructor.  */
16775       return CLASSTYPE_DESTRUCTORS (type);
16776     }
16777
16778   /* By this point, the NAME should be an ordinary identifier.  If
16779      the id-expression was a qualified name, the qualifying scope is
16780      stored in PARSER->SCOPE at this point.  */
16781   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16782
16783   /* Perform the lookup.  */
16784   if (parser->scope)
16785     {
16786       bool dependent_p;
16787
16788       if (parser->scope == error_mark_node)
16789         return error_mark_node;
16790
16791       /* If the SCOPE is dependent, the lookup must be deferred until
16792          the template is instantiated -- unless we are explicitly
16793          looking up names in uninstantiated templates.  Even then, we
16794          cannot look up the name if the scope is not a class type; it
16795          might, for example, be a template type parameter.  */
16796       dependent_p = (TYPE_P (parser->scope)
16797                      && !(parser->in_declarator_p
16798                           && currently_open_class (parser->scope))
16799                      && dependent_type_p (parser->scope));
16800       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16801            && dependent_p)
16802         {
16803           if (tag_type)
16804             {
16805               tree type;
16806
16807               /* The resolution to Core Issue 180 says that `struct
16808                  A::B' should be considered a type-name, even if `A'
16809                  is dependent.  */
16810               type = make_typename_type (parser->scope, name, tag_type,
16811                                          /*complain=*/tf_error);
16812               decl = TYPE_NAME (type);
16813             }
16814           else if (is_template
16815                    && (cp_parser_next_token_ends_template_argument_p (parser)
16816                        || cp_lexer_next_token_is (parser->lexer,
16817                                                   CPP_CLOSE_PAREN)))
16818             decl = make_unbound_class_template (parser->scope,
16819                                                 name, NULL_TREE,
16820                                                 /*complain=*/tf_error);
16821           else
16822             decl = build_qualified_name (/*type=*/NULL_TREE,
16823                                          parser->scope, name,
16824                                          is_template);
16825         }
16826       else
16827         {
16828           tree pushed_scope = NULL_TREE;
16829
16830           /* If PARSER->SCOPE is a dependent type, then it must be a
16831              class type, and we must not be checking dependencies;
16832              otherwise, we would have processed this lookup above.  So
16833              that PARSER->SCOPE is not considered a dependent base by
16834              lookup_member, we must enter the scope here.  */
16835           if (dependent_p)
16836             pushed_scope = push_scope (parser->scope);
16837           /* If the PARSER->SCOPE is a template specialization, it
16838              may be instantiated during name lookup.  In that case,
16839              errors may be issued.  Even if we rollback the current
16840              tentative parse, those errors are valid.  */
16841           decl = lookup_qualified_name (parser->scope, name,
16842                                         tag_type != none_type,
16843                                         /*complain=*/true);
16844
16845           /* If we have a single function from a using decl, pull it out.  */
16846           if (decl
16847               && TREE_CODE (decl) == OVERLOAD
16848               && !really_overloaded_fn (decl))
16849             decl = OVL_FUNCTION (decl);
16850
16851           if (pushed_scope)
16852             pop_scope (pushed_scope);
16853         }
16854       parser->qualifying_scope = parser->scope;
16855       parser->object_scope = NULL_TREE;
16856     }
16857   else if (object_type)
16858     {
16859       tree object_decl = NULL_TREE;
16860       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16861          OBJECT_TYPE is not a class.  */
16862       if (CLASS_TYPE_P (object_type))
16863         /* If the OBJECT_TYPE is a template specialization, it may
16864            be instantiated during name lookup.  In that case, errors
16865            may be issued.  Even if we rollback the current tentative
16866            parse, those errors are valid.  */
16867         object_decl = lookup_member (object_type,
16868                                      name,
16869                                      /*protect=*/0,
16870                                      tag_type != none_type);
16871       /* Look it up in the enclosing context, too.  */
16872       decl = lookup_name_real (name, tag_type != none_type,
16873                                /*nonclass=*/0,
16874                                /*block_p=*/true, is_namespace, flags);
16875       parser->object_scope = object_type;
16876       parser->qualifying_scope = NULL_TREE;
16877       if (object_decl)
16878         decl = object_decl;
16879     }
16880   else
16881     {
16882       decl = lookup_name_real (name, tag_type != none_type,
16883                                /*nonclass=*/0,
16884                                /*block_p=*/true, is_namespace, flags);
16885       parser->qualifying_scope = NULL_TREE;
16886       parser->object_scope = NULL_TREE;
16887     }
16888
16889   /* If the lookup failed, let our caller know.  */
16890   if (!decl || decl == error_mark_node)
16891     return error_mark_node;
16892
16893   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16894   if (TREE_CODE (decl) == TREE_LIST)
16895     {
16896       if (ambiguous_decls)
16897         *ambiguous_decls = decl;
16898       /* The error message we have to print is too complicated for
16899          cp_parser_error, so we incorporate its actions directly.  */
16900       if (!cp_parser_simulate_error (parser))
16901         {
16902           error ("%Hreference to %qD is ambiguous",
16903                  &name_location, name);
16904           print_candidates (decl);
16905         }
16906       return error_mark_node;
16907     }
16908
16909   gcc_assert (DECL_P (decl)
16910               || TREE_CODE (decl) == OVERLOAD
16911               || TREE_CODE (decl) == SCOPE_REF
16912               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16913               || BASELINK_P (decl));
16914
16915   /* If we have resolved the name of a member declaration, check to
16916      see if the declaration is accessible.  When the name resolves to
16917      set of overloaded functions, accessibility is checked when
16918      overload resolution is done.
16919
16920      During an explicit instantiation, access is not checked at all,
16921      as per [temp.explicit].  */
16922   if (DECL_P (decl))
16923     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16924
16925   return decl;
16926 }
16927
16928 /* Like cp_parser_lookup_name, but for use in the typical case where
16929    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16930    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16931
16932 static tree
16933 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16934 {
16935   return cp_parser_lookup_name (parser, name,
16936                                 none_type,
16937                                 /*is_template=*/false,
16938                                 /*is_namespace=*/false,
16939                                 /*check_dependency=*/true,
16940                                 /*ambiguous_decls=*/NULL,
16941                                 location);
16942 }
16943
16944 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16945    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16946    true, the DECL indicates the class being defined in a class-head,
16947    or declared in an elaborated-type-specifier.
16948
16949    Otherwise, return DECL.  */
16950
16951 static tree
16952 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16953 {
16954   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16955      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16956
16957        struct A {
16958          template <typename T> struct B;
16959        };
16960
16961        template <typename T> struct A::B {};
16962
16963      Similarly, in an elaborated-type-specifier:
16964
16965        namespace N { struct X{}; }
16966
16967        struct A {
16968          template <typename T> friend struct N::X;
16969        };
16970
16971      However, if the DECL refers to a class type, and we are in
16972      the scope of the class, then the name lookup automatically
16973      finds the TYPE_DECL created by build_self_reference rather
16974      than a TEMPLATE_DECL.  For example, in:
16975
16976        template <class T> struct S {
16977          S s;
16978        };
16979
16980      there is no need to handle such case.  */
16981
16982   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16983     return DECL_TEMPLATE_RESULT (decl);
16984
16985   return decl;
16986 }
16987
16988 /* If too many, or too few, template-parameter lists apply to the
16989    declarator, issue an error message.  Returns TRUE if all went well,
16990    and FALSE otherwise.  */
16991
16992 static bool
16993 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16994                                                 cp_declarator *declarator,
16995                                                 location_t declarator_location)
16996 {
16997   unsigned num_templates;
16998
16999   /* We haven't seen any classes that involve template parameters yet.  */
17000   num_templates = 0;
17001
17002   switch (declarator->kind)
17003     {
17004     case cdk_id:
17005       if (declarator->u.id.qualifying_scope)
17006         {
17007           tree scope;
17008           tree member;
17009
17010           scope = declarator->u.id.qualifying_scope;
17011           member = declarator->u.id.unqualified_name;
17012
17013           while (scope && CLASS_TYPE_P (scope))
17014             {
17015               /* You're supposed to have one `template <...>'
17016                  for every template class, but you don't need one
17017                  for a full specialization.  For example:
17018
17019                  template <class T> struct S{};
17020                  template <> struct S<int> { void f(); };
17021                  void S<int>::f () {}
17022
17023                  is correct; there shouldn't be a `template <>' for
17024                  the definition of `S<int>::f'.  */
17025               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17026                 /* If SCOPE does not have template information of any
17027                    kind, then it is not a template, nor is it nested
17028                    within a template.  */
17029                 break;
17030               if (explicit_class_specialization_p (scope))
17031                 break;
17032               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17033                 ++num_templates;
17034
17035               scope = TYPE_CONTEXT (scope);
17036             }
17037         }
17038       else if (TREE_CODE (declarator->u.id.unqualified_name)
17039                == TEMPLATE_ID_EXPR)
17040         /* If the DECLARATOR has the form `X<y>' then it uses one
17041            additional level of template parameters.  */
17042         ++num_templates;
17043
17044       return cp_parser_check_template_parameters (parser,
17045                                                   num_templates,
17046                                                   declarator_location);
17047
17048     case cdk_function:
17049     case cdk_array:
17050     case cdk_pointer:
17051     case cdk_reference:
17052     case cdk_ptrmem:
17053       return (cp_parser_check_declarator_template_parameters
17054               (parser, declarator->declarator, declarator_location));
17055
17056     case cdk_error:
17057       return true;
17058
17059     default:
17060       gcc_unreachable ();
17061     }
17062   return false;
17063 }
17064
17065 /* NUM_TEMPLATES were used in the current declaration.  If that is
17066    invalid, return FALSE and issue an error messages.  Otherwise,
17067    return TRUE.  */
17068
17069 static bool
17070 cp_parser_check_template_parameters (cp_parser* parser,
17071                                      unsigned num_templates,
17072                                      location_t location)
17073 {
17074   /* If there are more template classes than parameter lists, we have
17075      something like:
17076
17077        template <class T> void S<T>::R<T>::f ();  */
17078   if (parser->num_template_parameter_lists < num_templates)
17079     {
17080       error ("%Htoo few template-parameter-lists", &location);
17081       return false;
17082     }
17083   /* If there are the same number of template classes and parameter
17084      lists, that's OK.  */
17085   if (parser->num_template_parameter_lists == num_templates)
17086     return true;
17087   /* If there are more, but only one more, then we are referring to a
17088      member template.  That's OK too.  */
17089   if (parser->num_template_parameter_lists == num_templates + 1)
17090       return true;
17091   /* Otherwise, there are too many template parameter lists.  We have
17092      something like:
17093
17094      template <class T> template <class U> void S::f();  */
17095   error ("%Htoo many template-parameter-lists", &location);
17096   return false;
17097 }
17098
17099 /* Parse an optional `::' token indicating that the following name is
17100    from the global namespace.  If so, PARSER->SCOPE is set to the
17101    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17102    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17103    Returns the new value of PARSER->SCOPE, if the `::' token is
17104    present, and NULL_TREE otherwise.  */
17105
17106 static tree
17107 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17108 {
17109   cp_token *token;
17110
17111   /* Peek at the next token.  */
17112   token = cp_lexer_peek_token (parser->lexer);
17113   /* If we're looking at a `::' token then we're starting from the
17114      global namespace, not our current location.  */
17115   if (token->type == CPP_SCOPE)
17116     {
17117       /* Consume the `::' token.  */
17118       cp_lexer_consume_token (parser->lexer);
17119       /* Set the SCOPE so that we know where to start the lookup.  */
17120       parser->scope = global_namespace;
17121       parser->qualifying_scope = global_namespace;
17122       parser->object_scope = NULL_TREE;
17123
17124       return parser->scope;
17125     }
17126   else if (!current_scope_valid_p)
17127     {
17128       parser->scope = NULL_TREE;
17129       parser->qualifying_scope = NULL_TREE;
17130       parser->object_scope = NULL_TREE;
17131     }
17132
17133   return NULL_TREE;
17134 }
17135
17136 /* Returns TRUE if the upcoming token sequence is the start of a
17137    constructor declarator.  If FRIEND_P is true, the declarator is
17138    preceded by the `friend' specifier.  */
17139
17140 static bool
17141 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17142 {
17143   bool constructor_p;
17144   tree type_decl = NULL_TREE;
17145   bool nested_name_p;
17146   cp_token *next_token;
17147
17148   /* The common case is that this is not a constructor declarator, so
17149      try to avoid doing lots of work if at all possible.  It's not
17150      valid declare a constructor at function scope.  */
17151   if (parser->in_function_body)
17152     return false;
17153   /* And only certain tokens can begin a constructor declarator.  */
17154   next_token = cp_lexer_peek_token (parser->lexer);
17155   if (next_token->type != CPP_NAME
17156       && next_token->type != CPP_SCOPE
17157       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17158       && next_token->type != CPP_TEMPLATE_ID)
17159     return false;
17160
17161   /* Parse tentatively; we are going to roll back all of the tokens
17162      consumed here.  */
17163   cp_parser_parse_tentatively (parser);
17164   /* Assume that we are looking at a constructor declarator.  */
17165   constructor_p = true;
17166
17167   /* Look for the optional `::' operator.  */
17168   cp_parser_global_scope_opt (parser,
17169                               /*current_scope_valid_p=*/false);
17170   /* Look for the nested-name-specifier.  */
17171   nested_name_p
17172     = (cp_parser_nested_name_specifier_opt (parser,
17173                                             /*typename_keyword_p=*/false,
17174                                             /*check_dependency_p=*/false,
17175                                             /*type_p=*/false,
17176                                             /*is_declaration=*/false)
17177        != NULL_TREE);
17178   /* Outside of a class-specifier, there must be a
17179      nested-name-specifier.  */
17180   if (!nested_name_p &&
17181       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17182        || friend_p))
17183     constructor_p = false;
17184   /* If we still think that this might be a constructor-declarator,
17185      look for a class-name.  */
17186   if (constructor_p)
17187     {
17188       /* If we have:
17189
17190            template <typename T> struct S { S(); };
17191            template <typename T> S<T>::S ();
17192
17193          we must recognize that the nested `S' names a class.
17194          Similarly, for:
17195
17196            template <typename T> S<T>::S<T> ();
17197
17198          we must recognize that the nested `S' names a template.  */
17199       type_decl = cp_parser_class_name (parser,
17200                                         /*typename_keyword_p=*/false,
17201                                         /*template_keyword_p=*/false,
17202                                         none_type,
17203                                         /*check_dependency_p=*/false,
17204                                         /*class_head_p=*/false,
17205                                         /*is_declaration=*/false);
17206       /* If there was no class-name, then this is not a constructor.  */
17207       constructor_p = !cp_parser_error_occurred (parser);
17208     }
17209
17210   /* If we're still considering a constructor, we have to see a `(',
17211      to begin the parameter-declaration-clause, followed by either a
17212      `)', an `...', or a decl-specifier.  We need to check for a
17213      type-specifier to avoid being fooled into thinking that:
17214
17215        S::S (f) (int);
17216
17217      is a constructor.  (It is actually a function named `f' that
17218      takes one parameter (of type `int') and returns a value of type
17219      `S::S'.  */
17220   if (constructor_p
17221       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17222     {
17223       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17224           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17225           /* A parameter declaration begins with a decl-specifier,
17226              which is either the "attribute" keyword, a storage class
17227              specifier, or (usually) a type-specifier.  */
17228           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17229         {
17230           tree type;
17231           tree pushed_scope = NULL_TREE;
17232           unsigned saved_num_template_parameter_lists;
17233
17234           /* Names appearing in the type-specifier should be looked up
17235              in the scope of the class.  */
17236           if (current_class_type)
17237             type = NULL_TREE;
17238           else
17239             {
17240               type = TREE_TYPE (type_decl);
17241               if (TREE_CODE (type) == TYPENAME_TYPE)
17242                 {
17243                   type = resolve_typename_type (type,
17244                                                 /*only_current_p=*/false);
17245                   if (TREE_CODE (type) == TYPENAME_TYPE)
17246                     {
17247                       cp_parser_abort_tentative_parse (parser);
17248                       return false;
17249                     }
17250                 }
17251               pushed_scope = push_scope (type);
17252             }
17253
17254           /* Inside the constructor parameter list, surrounding
17255              template-parameter-lists do not apply.  */
17256           saved_num_template_parameter_lists
17257             = parser->num_template_parameter_lists;
17258           parser->num_template_parameter_lists = 0;
17259
17260           /* Look for the type-specifier.  */
17261           cp_parser_type_specifier (parser,
17262                                     CP_PARSER_FLAGS_NONE,
17263                                     /*decl_specs=*/NULL,
17264                                     /*is_declarator=*/true,
17265                                     /*declares_class_or_enum=*/NULL,
17266                                     /*is_cv_qualifier=*/NULL);
17267
17268           parser->num_template_parameter_lists
17269             = saved_num_template_parameter_lists;
17270
17271           /* Leave the scope of the class.  */
17272           if (pushed_scope)
17273             pop_scope (pushed_scope);
17274
17275           constructor_p = !cp_parser_error_occurred (parser);
17276         }
17277     }
17278   else
17279     constructor_p = false;
17280   /* We did not really want to consume any tokens.  */
17281   cp_parser_abort_tentative_parse (parser);
17282
17283   return constructor_p;
17284 }
17285
17286 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17287    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17288    they must be performed once we are in the scope of the function.
17289
17290    Returns the function defined.  */
17291
17292 static tree
17293 cp_parser_function_definition_from_specifiers_and_declarator
17294   (cp_parser* parser,
17295    cp_decl_specifier_seq *decl_specifiers,
17296    tree attributes,
17297    const cp_declarator *declarator)
17298 {
17299   tree fn;
17300   bool success_p;
17301
17302   /* Begin the function-definition.  */
17303   success_p = start_function (decl_specifiers, declarator, attributes);
17304
17305   /* The things we're about to see are not directly qualified by any
17306      template headers we've seen thus far.  */
17307   reset_specialization ();
17308
17309   /* If there were names looked up in the decl-specifier-seq that we
17310      did not check, check them now.  We must wait until we are in the
17311      scope of the function to perform the checks, since the function
17312      might be a friend.  */
17313   perform_deferred_access_checks ();
17314
17315   if (!success_p)
17316     {
17317       /* Skip the entire function.  */
17318       cp_parser_skip_to_end_of_block_or_statement (parser);
17319       fn = error_mark_node;
17320     }
17321   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17322     {
17323       /* Seen already, skip it.  An error message has already been output.  */
17324       cp_parser_skip_to_end_of_block_or_statement (parser);
17325       fn = current_function_decl;
17326       current_function_decl = NULL_TREE;
17327       /* If this is a function from a class, pop the nested class.  */
17328       if (current_class_name)
17329         pop_nested_class ();
17330     }
17331   else
17332     fn = cp_parser_function_definition_after_declarator (parser,
17333                                                          /*inline_p=*/false);
17334
17335   return fn;
17336 }
17337
17338 /* Parse the part of a function-definition that follows the
17339    declarator.  INLINE_P is TRUE iff this function is an inline
17340    function defined with a class-specifier.
17341
17342    Returns the function defined.  */
17343
17344 static tree
17345 cp_parser_function_definition_after_declarator (cp_parser* parser,
17346                                                 bool inline_p)
17347 {
17348   tree fn;
17349   bool ctor_initializer_p = false;
17350   bool saved_in_unbraced_linkage_specification_p;
17351   bool saved_in_function_body;
17352   unsigned saved_num_template_parameter_lists;
17353   cp_token *token;
17354
17355   saved_in_function_body = parser->in_function_body;
17356   parser->in_function_body = true;
17357   /* If the next token is `return', then the code may be trying to
17358      make use of the "named return value" extension that G++ used to
17359      support.  */
17360   token = cp_lexer_peek_token (parser->lexer);
17361   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17362     {
17363       /* Consume the `return' keyword.  */
17364       cp_lexer_consume_token (parser->lexer);
17365       /* Look for the identifier that indicates what value is to be
17366          returned.  */
17367       cp_parser_identifier (parser);
17368       /* Issue an error message.  */
17369       error ("%Hnamed return values are no longer supported",
17370              &token->location);
17371       /* Skip tokens until we reach the start of the function body.  */
17372       while (true)
17373         {
17374           cp_token *token = cp_lexer_peek_token (parser->lexer);
17375           if (token->type == CPP_OPEN_BRACE
17376               || token->type == CPP_EOF
17377               || token->type == CPP_PRAGMA_EOL)
17378             break;
17379           cp_lexer_consume_token (parser->lexer);
17380         }
17381     }
17382   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17383      anything declared inside `f'.  */
17384   saved_in_unbraced_linkage_specification_p
17385     = parser->in_unbraced_linkage_specification_p;
17386   parser->in_unbraced_linkage_specification_p = false;
17387   /* Inside the function, surrounding template-parameter-lists do not
17388      apply.  */
17389   saved_num_template_parameter_lists
17390     = parser->num_template_parameter_lists;
17391   parser->num_template_parameter_lists = 0;
17392   /* If the next token is `try', then we are looking at a
17393      function-try-block.  */
17394   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17395     ctor_initializer_p = cp_parser_function_try_block (parser);
17396   /* A function-try-block includes the function-body, so we only do
17397      this next part if we're not processing a function-try-block.  */
17398   else
17399     ctor_initializer_p
17400       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17401
17402   /* Finish the function.  */
17403   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17404                         (inline_p ? 2 : 0));
17405   /* Generate code for it, if necessary.  */
17406   expand_or_defer_fn (fn);
17407   /* Restore the saved values.  */
17408   parser->in_unbraced_linkage_specification_p
17409     = saved_in_unbraced_linkage_specification_p;
17410   parser->num_template_parameter_lists
17411     = saved_num_template_parameter_lists;
17412   parser->in_function_body = saved_in_function_body;
17413
17414   return fn;
17415 }
17416
17417 /* Parse a template-declaration, assuming that the `export' (and
17418    `extern') keywords, if present, has already been scanned.  MEMBER_P
17419    is as for cp_parser_template_declaration.  */
17420
17421 static void
17422 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17423 {
17424   tree decl = NULL_TREE;
17425   VEC (deferred_access_check,gc) *checks;
17426   tree parameter_list;
17427   bool friend_p = false;
17428   bool need_lang_pop;
17429   cp_token *token;
17430
17431   /* Look for the `template' keyword.  */
17432   token = cp_lexer_peek_token (parser->lexer);
17433   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17434     return;
17435
17436   /* And the `<'.  */
17437   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17438     return;
17439   if (at_class_scope_p () && current_function_decl)
17440     {
17441       /* 14.5.2.2 [temp.mem]
17442
17443          A local class shall not have member templates.  */
17444       error ("%Hinvalid declaration of member template in local class",
17445              &token->location);
17446       cp_parser_skip_to_end_of_block_or_statement (parser);
17447       return;
17448     }
17449   /* [temp]
17450
17451      A template ... shall not have C linkage.  */
17452   if (current_lang_name == lang_name_c)
17453     {
17454       error ("%Htemplate with C linkage", &token->location);
17455       /* Give it C++ linkage to avoid confusing other parts of the
17456          front end.  */
17457       push_lang_context (lang_name_cplusplus);
17458       need_lang_pop = true;
17459     }
17460   else
17461     need_lang_pop = false;
17462
17463   /* We cannot perform access checks on the template parameter
17464      declarations until we know what is being declared, just as we
17465      cannot check the decl-specifier list.  */
17466   push_deferring_access_checks (dk_deferred);
17467
17468   /* If the next token is `>', then we have an invalid
17469      specialization.  Rather than complain about an invalid template
17470      parameter, issue an error message here.  */
17471   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17472     {
17473       cp_parser_error (parser, "invalid explicit specialization");
17474       begin_specialization ();
17475       parameter_list = NULL_TREE;
17476     }
17477   else
17478     /* Parse the template parameters.  */
17479     parameter_list = cp_parser_template_parameter_list (parser);
17480
17481   /* Get the deferred access checks from the parameter list.  These
17482      will be checked once we know what is being declared, as for a
17483      member template the checks must be performed in the scope of the
17484      class containing the member.  */
17485   checks = get_deferred_access_checks ();
17486
17487   /* Look for the `>'.  */
17488   cp_parser_skip_to_end_of_template_parameter_list (parser);
17489   /* We just processed one more parameter list.  */
17490   ++parser->num_template_parameter_lists;
17491   /* If the next token is `template', there are more template
17492      parameters.  */
17493   if (cp_lexer_next_token_is_keyword (parser->lexer,
17494                                       RID_TEMPLATE))
17495     cp_parser_template_declaration_after_export (parser, member_p);
17496   else
17497     {
17498       /* There are no access checks when parsing a template, as we do not
17499          know if a specialization will be a friend.  */
17500       push_deferring_access_checks (dk_no_check);
17501       token = cp_lexer_peek_token (parser->lexer);
17502       decl = cp_parser_single_declaration (parser,
17503                                            checks,
17504                                            member_p,
17505                                            /*explicit_specialization_p=*/false,
17506                                            &friend_p);
17507       pop_deferring_access_checks ();
17508
17509       /* If this is a member template declaration, let the front
17510          end know.  */
17511       if (member_p && !friend_p && decl)
17512         {
17513           if (TREE_CODE (decl) == TYPE_DECL)
17514             cp_parser_check_access_in_redeclaration (decl, token->location);
17515
17516           decl = finish_member_template_decl (decl);
17517         }
17518       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17519         make_friend_class (current_class_type, TREE_TYPE (decl),
17520                            /*complain=*/true);
17521     }
17522   /* We are done with the current parameter list.  */
17523   --parser->num_template_parameter_lists;
17524
17525   pop_deferring_access_checks ();
17526
17527   /* Finish up.  */
17528   finish_template_decl (parameter_list);
17529
17530   /* Register member declarations.  */
17531   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17532     finish_member_declaration (decl);
17533   /* For the erroneous case of a template with C linkage, we pushed an
17534      implicit C++ linkage scope; exit that scope now.  */
17535   if (need_lang_pop)
17536     pop_lang_context ();
17537   /* If DECL is a function template, we must return to parse it later.
17538      (Even though there is no definition, there might be default
17539      arguments that need handling.)  */
17540   if (member_p && decl
17541       && (TREE_CODE (decl) == FUNCTION_DECL
17542           || DECL_FUNCTION_TEMPLATE_P (decl)))
17543     TREE_VALUE (parser->unparsed_functions_queues)
17544       = tree_cons (NULL_TREE, decl,
17545                    TREE_VALUE (parser->unparsed_functions_queues));
17546 }
17547
17548 /* Perform the deferred access checks from a template-parameter-list.
17549    CHECKS is a TREE_LIST of access checks, as returned by
17550    get_deferred_access_checks.  */
17551
17552 static void
17553 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17554 {
17555   ++processing_template_parmlist;
17556   perform_access_checks (checks);
17557   --processing_template_parmlist;
17558 }
17559
17560 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17561    `function-definition' sequence.  MEMBER_P is true, this declaration
17562    appears in a class scope.
17563
17564    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17565    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17566
17567 static tree
17568 cp_parser_single_declaration (cp_parser* parser,
17569                               VEC (deferred_access_check,gc)* checks,
17570                               bool member_p,
17571                               bool explicit_specialization_p,
17572                               bool* friend_p)
17573 {
17574   int declares_class_or_enum;
17575   tree decl = NULL_TREE;
17576   cp_decl_specifier_seq decl_specifiers;
17577   bool function_definition_p = false;
17578   cp_token *decl_spec_token_start;
17579
17580   /* This function is only used when processing a template
17581      declaration.  */
17582   gcc_assert (innermost_scope_kind () == sk_template_parms
17583               || innermost_scope_kind () == sk_template_spec);
17584
17585   /* Defer access checks until we know what is being declared.  */
17586   push_deferring_access_checks (dk_deferred);
17587
17588   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17589      alternative.  */
17590   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17591   cp_parser_decl_specifier_seq (parser,
17592                                 CP_PARSER_FLAGS_OPTIONAL,
17593                                 &decl_specifiers,
17594                                 &declares_class_or_enum);
17595   if (friend_p)
17596     *friend_p = cp_parser_friend_p (&decl_specifiers);
17597
17598   /* There are no template typedefs.  */
17599   if (decl_specifiers.specs[(int) ds_typedef])
17600     {
17601       error ("%Htemplate declaration of %qs",
17602              &decl_spec_token_start->location, "typedef");
17603       decl = error_mark_node;
17604     }
17605
17606   /* Gather up the access checks that occurred the
17607      decl-specifier-seq.  */
17608   stop_deferring_access_checks ();
17609
17610   /* Check for the declaration of a template class.  */
17611   if (declares_class_or_enum)
17612     {
17613       if (cp_parser_declares_only_class_p (parser))
17614         {
17615           decl = shadow_tag (&decl_specifiers);
17616
17617           /* In this case:
17618
17619                struct C {
17620                  friend template <typename T> struct A<T>::B;
17621                };
17622
17623              A<T>::B will be represented by a TYPENAME_TYPE, and
17624              therefore not recognized by shadow_tag.  */
17625           if (friend_p && *friend_p
17626               && !decl
17627               && decl_specifiers.type
17628               && TYPE_P (decl_specifiers.type))
17629             decl = decl_specifiers.type;
17630
17631           if (decl && decl != error_mark_node)
17632             decl = TYPE_NAME (decl);
17633           else
17634             decl = error_mark_node;
17635
17636           /* Perform access checks for template parameters.  */
17637           cp_parser_perform_template_parameter_access_checks (checks);
17638         }
17639     }
17640   /* If it's not a template class, try for a template function.  If
17641      the next token is a `;', then this declaration does not declare
17642      anything.  But, if there were errors in the decl-specifiers, then
17643      the error might well have come from an attempted class-specifier.
17644      In that case, there's no need to warn about a missing declarator.  */
17645   if (!decl
17646       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17647           || decl_specifiers.type != error_mark_node))
17648     {
17649       decl = cp_parser_init_declarator (parser,
17650                                         &decl_specifiers,
17651                                         checks,
17652                                         /*function_definition_allowed_p=*/true,
17653                                         member_p,
17654                                         declares_class_or_enum,
17655                                         &function_definition_p);
17656
17657     /* 7.1.1-1 [dcl.stc]
17658
17659        A storage-class-specifier shall not be specified in an explicit
17660        specialization...  */
17661     if (decl
17662         && explicit_specialization_p
17663         && decl_specifiers.storage_class != sc_none)
17664       {
17665         error ("%Hexplicit template specialization cannot have a storage class",
17666                &decl_spec_token_start->location);
17667         decl = error_mark_node;
17668       }
17669     }
17670
17671   pop_deferring_access_checks ();
17672
17673   /* Clear any current qualification; whatever comes next is the start
17674      of something new.  */
17675   parser->scope = NULL_TREE;
17676   parser->qualifying_scope = NULL_TREE;
17677   parser->object_scope = NULL_TREE;
17678   /* Look for a trailing `;' after the declaration.  */
17679   if (!function_definition_p
17680       && (decl == error_mark_node
17681           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17682     cp_parser_skip_to_end_of_block_or_statement (parser);
17683
17684   return decl;
17685 }
17686
17687 /* Parse a cast-expression that is not the operand of a unary "&".  */
17688
17689 static tree
17690 cp_parser_simple_cast_expression (cp_parser *parser)
17691 {
17692   return cp_parser_cast_expression (parser, /*address_p=*/false,
17693                                     /*cast_p=*/false);
17694 }
17695
17696 /* Parse a functional cast to TYPE.  Returns an expression
17697    representing the cast.  */
17698
17699 static tree
17700 cp_parser_functional_cast (cp_parser* parser, tree type)
17701 {
17702   tree expression_list;
17703   tree cast;
17704   bool nonconst_p;
17705
17706   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17707     {
17708       maybe_warn_cpp0x ("extended initializer lists");
17709       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17710       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17711       if (TREE_CODE (type) == TYPE_DECL)
17712         type = TREE_TYPE (type);
17713       return finish_compound_literal (type, expression_list);
17714     }
17715
17716   expression_list
17717     = cp_parser_parenthesized_expression_list (parser, false,
17718                                                /*cast_p=*/true,
17719                                                /*allow_expansion_p=*/true,
17720                                                /*non_constant_p=*/NULL);
17721
17722   cast = build_functional_cast (type, expression_list,
17723                                 tf_warning_or_error);
17724   /* [expr.const]/1: In an integral constant expression "only type
17725      conversions to integral or enumeration type can be used".  */
17726   if (TREE_CODE (type) == TYPE_DECL)
17727     type = TREE_TYPE (type);
17728   if (cast != error_mark_node
17729       && !cast_valid_in_integral_constant_expression_p (type)
17730       && (cp_parser_non_integral_constant_expression
17731           (parser, "a call to a constructor")))
17732     return error_mark_node;
17733   return cast;
17734 }
17735
17736 /* Save the tokens that make up the body of a member function defined
17737    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17738    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17739    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17740    for the member function.  */
17741
17742 static tree
17743 cp_parser_save_member_function_body (cp_parser* parser,
17744                                      cp_decl_specifier_seq *decl_specifiers,
17745                                      cp_declarator *declarator,
17746                                      tree attributes)
17747 {
17748   cp_token *first;
17749   cp_token *last;
17750   tree fn;
17751
17752   /* Create the function-declaration.  */
17753   fn = start_method (decl_specifiers, declarator, attributes);
17754   /* If something went badly wrong, bail out now.  */
17755   if (fn == error_mark_node)
17756     {
17757       /* If there's a function-body, skip it.  */
17758       if (cp_parser_token_starts_function_definition_p
17759           (cp_lexer_peek_token (parser->lexer)))
17760         cp_parser_skip_to_end_of_block_or_statement (parser);
17761       return error_mark_node;
17762     }
17763
17764   /* Remember it, if there default args to post process.  */
17765   cp_parser_save_default_args (parser, fn);
17766
17767   /* Save away the tokens that make up the body of the
17768      function.  */
17769   first = parser->lexer->next_token;
17770   /* We can have braced-init-list mem-initializers before the fn body.  */
17771   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17772     {
17773       cp_lexer_consume_token (parser->lexer);
17774       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17775              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17776         {
17777           /* cache_group will stop after an un-nested { } pair, too.  */
17778           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17779             break;
17780
17781           /* variadic mem-inits have ... after the ')'.  */
17782           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17783             cp_lexer_consume_token (parser->lexer);
17784         }
17785     }
17786   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17787   /* Handle function try blocks.  */
17788   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17789     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17790   last = parser->lexer->next_token;
17791
17792   /* Save away the inline definition; we will process it when the
17793      class is complete.  */
17794   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17795   DECL_PENDING_INLINE_P (fn) = 1;
17796
17797   /* We need to know that this was defined in the class, so that
17798      friend templates are handled correctly.  */
17799   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17800
17801   /* We're done with the inline definition.  */
17802   finish_method (fn);
17803
17804   /* Add FN to the queue of functions to be parsed later.  */
17805   TREE_VALUE (parser->unparsed_functions_queues)
17806     = tree_cons (NULL_TREE, fn,
17807                  TREE_VALUE (parser->unparsed_functions_queues));
17808
17809   return fn;
17810 }
17811
17812 /* Parse a template-argument-list, as well as the trailing ">" (but
17813    not the opening ">").  See cp_parser_template_argument_list for the
17814    return value.  */
17815
17816 static tree
17817 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17818 {
17819   tree arguments;
17820   tree saved_scope;
17821   tree saved_qualifying_scope;
17822   tree saved_object_scope;
17823   bool saved_greater_than_is_operator_p;
17824   bool saved_skip_evaluation;
17825
17826   /* [temp.names]
17827
17828      When parsing a template-id, the first non-nested `>' is taken as
17829      the end of the template-argument-list rather than a greater-than
17830      operator.  */
17831   saved_greater_than_is_operator_p
17832     = parser->greater_than_is_operator_p;
17833   parser->greater_than_is_operator_p = false;
17834   /* Parsing the argument list may modify SCOPE, so we save it
17835      here.  */
17836   saved_scope = parser->scope;
17837   saved_qualifying_scope = parser->qualifying_scope;
17838   saved_object_scope = parser->object_scope;
17839   /* We need to evaluate the template arguments, even though this
17840      template-id may be nested within a "sizeof".  */
17841   saved_skip_evaluation = skip_evaluation;
17842   skip_evaluation = false;
17843   /* Parse the template-argument-list itself.  */
17844   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17845       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17846     arguments = NULL_TREE;
17847   else
17848     arguments = cp_parser_template_argument_list (parser);
17849   /* Look for the `>' that ends the template-argument-list. If we find
17850      a '>>' instead, it's probably just a typo.  */
17851   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17852     {
17853       if (cxx_dialect != cxx98)
17854         {
17855           /* In C++0x, a `>>' in a template argument list or cast
17856              expression is considered to be two separate `>'
17857              tokens. So, change the current token to a `>', but don't
17858              consume it: it will be consumed later when the outer
17859              template argument list (or cast expression) is parsed.
17860              Note that this replacement of `>' for `>>' is necessary
17861              even if we are parsing tentatively: in the tentative
17862              case, after calling
17863              cp_parser_enclosed_template_argument_list we will always
17864              throw away all of the template arguments and the first
17865              closing `>', either because the template argument list
17866              was erroneous or because we are replacing those tokens
17867              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17868              not have been thrown away) is needed either to close an
17869              outer template argument list or to complete a new-style
17870              cast.  */
17871           cp_token *token = cp_lexer_peek_token (parser->lexer);
17872           token->type = CPP_GREATER;
17873         }
17874       else if (!saved_greater_than_is_operator_p)
17875         {
17876           /* If we're in a nested template argument list, the '>>' has
17877             to be a typo for '> >'. We emit the error message, but we
17878             continue parsing and we push a '>' as next token, so that
17879             the argument list will be parsed correctly.  Note that the
17880             global source location is still on the token before the
17881             '>>', so we need to say explicitly where we want it.  */
17882           cp_token *token = cp_lexer_peek_token (parser->lexer);
17883           error ("%H%<>>%> should be %<> >%> "
17884                  "within a nested template argument list",
17885                  &token->location);
17886
17887           token->type = CPP_GREATER;
17888         }
17889       else
17890         {
17891           /* If this is not a nested template argument list, the '>>'
17892             is a typo for '>'. Emit an error message and continue.
17893             Same deal about the token location, but here we can get it
17894             right by consuming the '>>' before issuing the diagnostic.  */
17895           cp_token *token = cp_lexer_consume_token (parser->lexer);
17896           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17897                  "a template argument list", &token->location);
17898         }
17899     }
17900   else
17901     cp_parser_skip_to_end_of_template_parameter_list (parser);
17902   /* The `>' token might be a greater-than operator again now.  */
17903   parser->greater_than_is_operator_p
17904     = saved_greater_than_is_operator_p;
17905   /* Restore the SAVED_SCOPE.  */
17906   parser->scope = saved_scope;
17907   parser->qualifying_scope = saved_qualifying_scope;
17908   parser->object_scope = saved_object_scope;
17909   skip_evaluation = saved_skip_evaluation;
17910
17911   return arguments;
17912 }
17913
17914 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17915    arguments, or the body of the function have not yet been parsed,
17916    parse them now.  */
17917
17918 static void
17919 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17920 {
17921   /* If this member is a template, get the underlying
17922      FUNCTION_DECL.  */
17923   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17924     member_function = DECL_TEMPLATE_RESULT (member_function);
17925
17926   /* There should not be any class definitions in progress at this
17927      point; the bodies of members are only parsed outside of all class
17928      definitions.  */
17929   gcc_assert (parser->num_classes_being_defined == 0);
17930   /* While we're parsing the member functions we might encounter more
17931      classes.  We want to handle them right away, but we don't want
17932      them getting mixed up with functions that are currently in the
17933      queue.  */
17934   parser->unparsed_functions_queues
17935     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17936
17937   /* Make sure that any template parameters are in scope.  */
17938   maybe_begin_member_template_processing (member_function);
17939
17940   /* If the body of the function has not yet been parsed, parse it
17941      now.  */
17942   if (DECL_PENDING_INLINE_P (member_function))
17943     {
17944       tree function_scope;
17945       cp_token_cache *tokens;
17946
17947       /* The function is no longer pending; we are processing it.  */
17948       tokens = DECL_PENDING_INLINE_INFO (member_function);
17949       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17950       DECL_PENDING_INLINE_P (member_function) = 0;
17951
17952       /* If this is a local class, enter the scope of the containing
17953          function.  */
17954       function_scope = current_function_decl;
17955       if (function_scope)
17956         push_function_context ();
17957
17958       /* Push the body of the function onto the lexer stack.  */
17959       cp_parser_push_lexer_for_tokens (parser, tokens);
17960
17961       /* Let the front end know that we going to be defining this
17962          function.  */
17963       start_preparsed_function (member_function, NULL_TREE,
17964                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17965
17966       /* Don't do access checking if it is a templated function.  */
17967       if (processing_template_decl)
17968         push_deferring_access_checks (dk_no_check);
17969
17970       /* Now, parse the body of the function.  */
17971       cp_parser_function_definition_after_declarator (parser,
17972                                                       /*inline_p=*/true);
17973
17974       if (processing_template_decl)
17975         pop_deferring_access_checks ();
17976
17977       /* Leave the scope of the containing function.  */
17978       if (function_scope)
17979         pop_function_context ();
17980       cp_parser_pop_lexer (parser);
17981     }
17982
17983   /* Remove any template parameters from the symbol table.  */
17984   maybe_end_member_template_processing ();
17985
17986   /* Restore the queue.  */
17987   parser->unparsed_functions_queues
17988     = TREE_CHAIN (parser->unparsed_functions_queues);
17989 }
17990
17991 /* If DECL contains any default args, remember it on the unparsed
17992    functions queue.  */
17993
17994 static void
17995 cp_parser_save_default_args (cp_parser* parser, tree decl)
17996 {
17997   tree probe;
17998
17999   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18000        probe;
18001        probe = TREE_CHAIN (probe))
18002     if (TREE_PURPOSE (probe))
18003       {
18004         TREE_PURPOSE (parser->unparsed_functions_queues)
18005           = tree_cons (current_class_type, decl,
18006                        TREE_PURPOSE (parser->unparsed_functions_queues));
18007         break;
18008       }
18009 }
18010
18011 /* FN is a FUNCTION_DECL which may contains a parameter with an
18012    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18013    assumes that the current scope is the scope in which the default
18014    argument should be processed.  */
18015
18016 static void
18017 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18018 {
18019   bool saved_local_variables_forbidden_p;
18020   tree parm;
18021
18022   /* While we're parsing the default args, we might (due to the
18023      statement expression extension) encounter more classes.  We want
18024      to handle them right away, but we don't want them getting mixed
18025      up with default args that are currently in the queue.  */
18026   parser->unparsed_functions_queues
18027     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18028
18029   /* Local variable names (and the `this' keyword) may not appear
18030      in a default argument.  */
18031   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18032   parser->local_variables_forbidden_p = true;
18033
18034   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18035        parm;
18036        parm = TREE_CHAIN (parm))
18037     {
18038       cp_token_cache *tokens;
18039       tree default_arg = TREE_PURPOSE (parm);
18040       tree parsed_arg;
18041       VEC(tree,gc) *insts;
18042       tree copy;
18043       unsigned ix;
18044
18045       if (!default_arg)
18046         continue;
18047
18048       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18049         /* This can happen for a friend declaration for a function
18050            already declared with default arguments.  */
18051         continue;
18052
18053        /* Push the saved tokens for the default argument onto the parser's
18054           lexer stack.  */
18055       tokens = DEFARG_TOKENS (default_arg);
18056       cp_parser_push_lexer_for_tokens (parser, tokens);
18057
18058       /* Parse the assignment-expression.  */
18059       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18060
18061       if (!processing_template_decl)
18062         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18063
18064       TREE_PURPOSE (parm) = parsed_arg;
18065
18066       /* Update any instantiations we've already created.  */
18067       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18068            VEC_iterate (tree, insts, ix, copy); ix++)
18069         TREE_PURPOSE (copy) = parsed_arg;
18070
18071       /* If the token stream has not been completely used up, then
18072          there was extra junk after the end of the default
18073          argument.  */
18074       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18075         cp_parser_error (parser, "expected %<,%>");
18076
18077       /* Revert to the main lexer.  */
18078       cp_parser_pop_lexer (parser);
18079     }
18080
18081   /* Make sure no default arg is missing.  */
18082   check_default_args (fn);
18083
18084   /* Restore the state of local_variables_forbidden_p.  */
18085   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18086
18087   /* Restore the queue.  */
18088   parser->unparsed_functions_queues
18089     = TREE_CHAIN (parser->unparsed_functions_queues);
18090 }
18091
18092 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18093    either a TYPE or an expression, depending on the form of the
18094    input.  The KEYWORD indicates which kind of expression we have
18095    encountered.  */
18096
18097 static tree
18098 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18099 {
18100   tree expr = NULL_TREE;
18101   const char *saved_message;
18102   char *tmp;
18103   bool saved_integral_constant_expression_p;
18104   bool saved_non_integral_constant_expression_p;
18105   bool pack_expansion_p = false;
18106
18107   /* Types cannot be defined in a `sizeof' expression.  Save away the
18108      old message.  */
18109   saved_message = parser->type_definition_forbidden_message;
18110   /* And create the new one.  */
18111   tmp = concat ("types may not be defined in %<",
18112                 IDENTIFIER_POINTER (ridpointers[keyword]),
18113                 "%> expressions", NULL);
18114   parser->type_definition_forbidden_message = tmp;
18115
18116   /* The restrictions on constant-expressions do not apply inside
18117      sizeof expressions.  */
18118   saved_integral_constant_expression_p
18119     = parser->integral_constant_expression_p;
18120   saved_non_integral_constant_expression_p
18121     = parser->non_integral_constant_expression_p;
18122   parser->integral_constant_expression_p = false;
18123
18124   /* If it's a `...', then we are computing the length of a parameter
18125      pack.  */
18126   if (keyword == RID_SIZEOF
18127       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18128     {
18129       /* Consume the `...'.  */
18130       cp_lexer_consume_token (parser->lexer);
18131       maybe_warn_variadic_templates ();
18132
18133       /* Note that this is an expansion.  */
18134       pack_expansion_p = true;
18135     }
18136
18137   /* Do not actually evaluate the expression.  */
18138   ++skip_evaluation;
18139   /* If it's a `(', then we might be looking at the type-id
18140      construction.  */
18141   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18142     {
18143       tree type;
18144       bool saved_in_type_id_in_expr_p;
18145
18146       /* We can't be sure yet whether we're looking at a type-id or an
18147          expression.  */
18148       cp_parser_parse_tentatively (parser);
18149       /* Consume the `('.  */
18150       cp_lexer_consume_token (parser->lexer);
18151       /* Parse the type-id.  */
18152       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18153       parser->in_type_id_in_expr_p = true;
18154       type = cp_parser_type_id (parser);
18155       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18156       /* Now, look for the trailing `)'.  */
18157       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18158       /* If all went well, then we're done.  */
18159       if (cp_parser_parse_definitely (parser))
18160         {
18161           cp_decl_specifier_seq decl_specs;
18162
18163           /* Build a trivial decl-specifier-seq.  */
18164           clear_decl_specs (&decl_specs);
18165           decl_specs.type = type;
18166
18167           /* Call grokdeclarator to figure out what type this is.  */
18168           expr = grokdeclarator (NULL,
18169                                  &decl_specs,
18170                                  TYPENAME,
18171                                  /*initialized=*/0,
18172                                  /*attrlist=*/NULL);
18173         }
18174     }
18175
18176   /* If the type-id production did not work out, then we must be
18177      looking at the unary-expression production.  */
18178   if (!expr)
18179     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18180                                        /*cast_p=*/false);
18181
18182   if (pack_expansion_p)
18183     /* Build a pack expansion. */
18184     expr = make_pack_expansion (expr);
18185
18186   /* Go back to evaluating expressions.  */
18187   --skip_evaluation;
18188
18189   /* Free the message we created.  */
18190   free (tmp);
18191   /* And restore the old one.  */
18192   parser->type_definition_forbidden_message = saved_message;
18193   parser->integral_constant_expression_p
18194     = saved_integral_constant_expression_p;
18195   parser->non_integral_constant_expression_p
18196     = saved_non_integral_constant_expression_p;
18197
18198   return expr;
18199 }
18200
18201 /* If the current declaration has no declarator, return true.  */
18202
18203 static bool
18204 cp_parser_declares_only_class_p (cp_parser *parser)
18205 {
18206   /* If the next token is a `;' or a `,' then there is no
18207      declarator.  */
18208   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18209           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18210 }
18211
18212 /* Update the DECL_SPECS to reflect the storage class indicated by
18213    KEYWORD.  */
18214
18215 static void
18216 cp_parser_set_storage_class (cp_parser *parser,
18217                              cp_decl_specifier_seq *decl_specs,
18218                              enum rid keyword,
18219                              location_t location)
18220 {
18221   cp_storage_class storage_class;
18222
18223   if (parser->in_unbraced_linkage_specification_p)
18224     {
18225       error ("%Hinvalid use of %qD in linkage specification",
18226              &location, ridpointers[keyword]);
18227       return;
18228     }
18229   else if (decl_specs->storage_class != sc_none)
18230     {
18231       decl_specs->conflicting_specifiers_p = true;
18232       return;
18233     }
18234
18235   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18236       && decl_specs->specs[(int) ds_thread])
18237     {
18238       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18239       decl_specs->specs[(int) ds_thread] = 0;
18240     }
18241
18242   switch (keyword)
18243     {
18244     case RID_AUTO:
18245       storage_class = sc_auto;
18246       break;
18247     case RID_REGISTER:
18248       storage_class = sc_register;
18249       break;
18250     case RID_STATIC:
18251       storage_class = sc_static;
18252       break;
18253     case RID_EXTERN:
18254       storage_class = sc_extern;
18255       break;
18256     case RID_MUTABLE:
18257       storage_class = sc_mutable;
18258       break;
18259     default:
18260       gcc_unreachable ();
18261     }
18262   decl_specs->storage_class = storage_class;
18263
18264   /* A storage class specifier cannot be applied alongside a typedef 
18265      specifier. If there is a typedef specifier present then set 
18266      conflicting_specifiers_p which will trigger an error later
18267      on in grokdeclarator. */
18268   if (decl_specs->specs[(int)ds_typedef])
18269     decl_specs->conflicting_specifiers_p = true;
18270 }
18271
18272 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18273    is true, the type is a user-defined type; otherwise it is a
18274    built-in type specified by a keyword.  */
18275
18276 static void
18277 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18278                               tree type_spec,
18279                               location_t location,
18280                               bool user_defined_p)
18281 {
18282   decl_specs->any_specifiers_p = true;
18283
18284   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18285      (with, for example, in "typedef int wchar_t;") we remember that
18286      this is what happened.  In system headers, we ignore these
18287      declarations so that G++ can work with system headers that are not
18288      C++-safe.  */
18289   if (decl_specs->specs[(int) ds_typedef]
18290       && !user_defined_p
18291       && (type_spec == boolean_type_node
18292           || type_spec == char16_type_node
18293           || type_spec == char32_type_node
18294           || type_spec == wchar_type_node)
18295       && (decl_specs->type
18296           || decl_specs->specs[(int) ds_long]
18297           || decl_specs->specs[(int) ds_short]
18298           || decl_specs->specs[(int) ds_unsigned]
18299           || decl_specs->specs[(int) ds_signed]))
18300     {
18301       decl_specs->redefined_builtin_type = type_spec;
18302       if (!decl_specs->type)
18303         {
18304           decl_specs->type = type_spec;
18305           decl_specs->user_defined_type_p = false;
18306           decl_specs->type_location = location;
18307         }
18308     }
18309   else if (decl_specs->type)
18310     decl_specs->multiple_types_p = true;
18311   else
18312     {
18313       decl_specs->type = type_spec;
18314       decl_specs->user_defined_type_p = user_defined_p;
18315       decl_specs->redefined_builtin_type = NULL_TREE;
18316       decl_specs->type_location = location;
18317     }
18318 }
18319
18320 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18321    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18322
18323 static bool
18324 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18325 {
18326   return decl_specifiers->specs[(int) ds_friend] != 0;
18327 }
18328
18329 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18330    issue an error message indicating that TOKEN_DESC was expected.
18331
18332    Returns the token consumed, if the token had the appropriate type.
18333    Otherwise, returns NULL.  */
18334
18335 static cp_token *
18336 cp_parser_require (cp_parser* parser,
18337                    enum cpp_ttype type,
18338                    const char* token_desc)
18339 {
18340   if (cp_lexer_next_token_is (parser->lexer, type))
18341     return cp_lexer_consume_token (parser->lexer);
18342   else
18343     {
18344       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18345       if (!cp_parser_simulate_error (parser))
18346         {
18347           char *message = concat ("expected ", token_desc, NULL);
18348           cp_parser_error (parser, message);
18349           free (message);
18350         }
18351       return NULL;
18352     }
18353 }
18354
18355 /* An error message is produced if the next token is not '>'.
18356    All further tokens are skipped until the desired token is
18357    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18358
18359 static void
18360 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18361 {
18362   /* Current level of '< ... >'.  */
18363   unsigned level = 0;
18364   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18365   unsigned nesting_depth = 0;
18366
18367   /* Are we ready, yet?  If not, issue error message.  */
18368   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18369     return;
18370
18371   /* Skip tokens until the desired token is found.  */
18372   while (true)
18373     {
18374       /* Peek at the next token.  */
18375       switch (cp_lexer_peek_token (parser->lexer)->type)
18376         {
18377         case CPP_LESS:
18378           if (!nesting_depth)
18379             ++level;
18380           break;
18381
18382         case CPP_RSHIFT:
18383           if (cxx_dialect == cxx98)
18384             /* C++0x views the `>>' operator as two `>' tokens, but
18385                C++98 does not. */
18386             break;
18387           else if (!nesting_depth && level-- == 0)
18388             {
18389               /* We've hit a `>>' where the first `>' closes the
18390                  template argument list, and the second `>' is
18391                  spurious.  Just consume the `>>' and stop; we've
18392                  already produced at least one error.  */
18393               cp_lexer_consume_token (parser->lexer);
18394               return;
18395             }
18396           /* Fall through for C++0x, so we handle the second `>' in
18397              the `>>'.  */
18398
18399         case CPP_GREATER:
18400           if (!nesting_depth && level-- == 0)
18401             {
18402               /* We've reached the token we want, consume it and stop.  */
18403               cp_lexer_consume_token (parser->lexer);
18404               return;
18405             }
18406           break;
18407
18408         case CPP_OPEN_PAREN:
18409         case CPP_OPEN_SQUARE:
18410           ++nesting_depth;
18411           break;
18412
18413         case CPP_CLOSE_PAREN:
18414         case CPP_CLOSE_SQUARE:
18415           if (nesting_depth-- == 0)
18416             return;
18417           break;
18418
18419         case CPP_EOF:
18420         case CPP_PRAGMA_EOL:
18421         case CPP_SEMICOLON:
18422         case CPP_OPEN_BRACE:
18423         case CPP_CLOSE_BRACE:
18424           /* The '>' was probably forgotten, don't look further.  */
18425           return;
18426
18427         default:
18428           break;
18429         }
18430
18431       /* Consume this token.  */
18432       cp_lexer_consume_token (parser->lexer);
18433     }
18434 }
18435
18436 /* If the next token is the indicated keyword, consume it.  Otherwise,
18437    issue an error message indicating that TOKEN_DESC was expected.
18438
18439    Returns the token consumed, if the token had the appropriate type.
18440    Otherwise, returns NULL.  */
18441
18442 static cp_token *
18443 cp_parser_require_keyword (cp_parser* parser,
18444                            enum rid keyword,
18445                            const char* token_desc)
18446 {
18447   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18448
18449   if (token && token->keyword != keyword)
18450     {
18451       dyn_string_t error_msg;
18452
18453       /* Format the error message.  */
18454       error_msg = dyn_string_new (0);
18455       dyn_string_append_cstr (error_msg, "expected ");
18456       dyn_string_append_cstr (error_msg, token_desc);
18457       cp_parser_error (parser, error_msg->s);
18458       dyn_string_delete (error_msg);
18459       return NULL;
18460     }
18461
18462   return token;
18463 }
18464
18465 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18466    function-definition.  */
18467
18468 static bool
18469 cp_parser_token_starts_function_definition_p (cp_token* token)
18470 {
18471   return (/* An ordinary function-body begins with an `{'.  */
18472           token->type == CPP_OPEN_BRACE
18473           /* A ctor-initializer begins with a `:'.  */
18474           || token->type == CPP_COLON
18475           /* A function-try-block begins with `try'.  */
18476           || token->keyword == RID_TRY
18477           /* The named return value extension begins with `return'.  */
18478           || token->keyword == RID_RETURN);
18479 }
18480
18481 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18482    definition.  */
18483
18484 static bool
18485 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18486 {
18487   cp_token *token;
18488
18489   token = cp_lexer_peek_token (parser->lexer);
18490   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18491 }
18492
18493 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18494    C++0x) ending a template-argument.  */
18495
18496 static bool
18497 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18498 {
18499   cp_token *token;
18500
18501   token = cp_lexer_peek_token (parser->lexer);
18502   return (token->type == CPP_COMMA 
18503           || token->type == CPP_GREATER
18504           || token->type == CPP_ELLIPSIS
18505           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18506 }
18507
18508 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18509    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18510
18511 static bool
18512 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18513                                                      size_t n)
18514 {
18515   cp_token *token;
18516
18517   token = cp_lexer_peek_nth_token (parser->lexer, n);
18518   if (token->type == CPP_LESS)
18519     return true;
18520   /* Check for the sequence `<::' in the original code. It would be lexed as
18521      `[:', where `[' is a digraph, and there is no whitespace before
18522      `:'.  */
18523   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18524     {
18525       cp_token *token2;
18526       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18527       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18528         return true;
18529     }
18530   return false;
18531 }
18532
18533 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18534    or none_type otherwise.  */
18535
18536 static enum tag_types
18537 cp_parser_token_is_class_key (cp_token* token)
18538 {
18539   switch (token->keyword)
18540     {
18541     case RID_CLASS:
18542       return class_type;
18543     case RID_STRUCT:
18544       return record_type;
18545     case RID_UNION:
18546       return union_type;
18547
18548     default:
18549       return none_type;
18550     }
18551 }
18552
18553 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18554
18555 static void
18556 cp_parser_check_class_key (enum tag_types class_key, tree type)
18557 {
18558   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18559     permerror (input_location, "%qs tag used in naming %q#T",
18560             class_key == union_type ? "union"
18561              : class_key == record_type ? "struct" : "class",
18562              type);
18563 }
18564
18565 /* Issue an error message if DECL is redeclared with different
18566    access than its original declaration [class.access.spec/3].
18567    This applies to nested classes and nested class templates.
18568    [class.mem/1].  */
18569
18570 static void
18571 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18572 {
18573   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18574     return;
18575
18576   if ((TREE_PRIVATE (decl)
18577        != (current_access_specifier == access_private_node))
18578       || (TREE_PROTECTED (decl)
18579           != (current_access_specifier == access_protected_node)))
18580     error ("%H%qD redeclared with different access", &location, decl);
18581 }
18582
18583 /* Look for the `template' keyword, as a syntactic disambiguator.
18584    Return TRUE iff it is present, in which case it will be
18585    consumed.  */
18586
18587 static bool
18588 cp_parser_optional_template_keyword (cp_parser *parser)
18589 {
18590   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18591     {
18592       /* The `template' keyword can only be used within templates;
18593          outside templates the parser can always figure out what is a
18594          template and what is not.  */
18595       if (!processing_template_decl)
18596         {
18597           cp_token *token = cp_lexer_peek_token (parser->lexer);
18598           error ("%H%<template%> (as a disambiguator) is only allowed "
18599                  "within templates", &token->location);
18600           /* If this part of the token stream is rescanned, the same
18601              error message would be generated.  So, we purge the token
18602              from the stream.  */
18603           cp_lexer_purge_token (parser->lexer);
18604           return false;
18605         }
18606       else
18607         {
18608           /* Consume the `template' keyword.  */
18609           cp_lexer_consume_token (parser->lexer);
18610           return true;
18611         }
18612     }
18613
18614   return false;
18615 }
18616
18617 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18618    set PARSER->SCOPE, and perform other related actions.  */
18619
18620 static void
18621 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18622 {
18623   int i;
18624   struct tree_check *check_value;
18625   deferred_access_check *chk;
18626   VEC (deferred_access_check,gc) *checks;
18627
18628   /* Get the stored value.  */
18629   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18630   /* Perform any access checks that were deferred.  */
18631   checks = check_value->checks;
18632   if (checks)
18633     {
18634       for (i = 0 ;
18635            VEC_iterate (deferred_access_check, checks, i, chk) ;
18636            ++i)
18637         {
18638           perform_or_defer_access_check (chk->binfo,
18639                                          chk->decl,
18640                                          chk->diag_decl);
18641         }
18642     }
18643   /* Set the scope from the stored value.  */
18644   parser->scope = check_value->value;
18645   parser->qualifying_scope = check_value->qualifying_scope;
18646   parser->object_scope = NULL_TREE;
18647 }
18648
18649 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18650    encounter the end of a block before what we were looking for.  */
18651
18652 static bool
18653 cp_parser_cache_group (cp_parser *parser,
18654                        enum cpp_ttype end,
18655                        unsigned depth)
18656 {
18657   while (true)
18658     {
18659       cp_token *token = cp_lexer_peek_token (parser->lexer);
18660
18661       /* Abort a parenthesized expression if we encounter a semicolon.  */
18662       if ((end == CPP_CLOSE_PAREN || depth == 0)
18663           && token->type == CPP_SEMICOLON)
18664         return true;
18665       /* If we've reached the end of the file, stop.  */
18666       if (token->type == CPP_EOF
18667           || (end != CPP_PRAGMA_EOL
18668               && token->type == CPP_PRAGMA_EOL))
18669         return true;
18670       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18671         /* We've hit the end of an enclosing block, so there's been some
18672            kind of syntax error.  */
18673         return true;
18674
18675       /* Consume the token.  */
18676       cp_lexer_consume_token (parser->lexer);
18677       /* See if it starts a new group.  */
18678       if (token->type == CPP_OPEN_BRACE)
18679         {
18680           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18681           /* In theory this should probably check end == '}', but
18682              cp_parser_save_member_function_body needs it to exit
18683              after either '}' or ')' when called with ')'.  */
18684           if (depth == 0)
18685             return false;
18686         }
18687       else if (token->type == CPP_OPEN_PAREN)
18688         {
18689           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18690           if (depth == 0 && end == CPP_CLOSE_PAREN)
18691             return false;
18692         }
18693       else if (token->type == CPP_PRAGMA)
18694         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18695       else if (token->type == end)
18696         return false;
18697     }
18698 }
18699
18700 /* Begin parsing tentatively.  We always save tokens while parsing
18701    tentatively so that if the tentative parsing fails we can restore the
18702    tokens.  */
18703
18704 static void
18705 cp_parser_parse_tentatively (cp_parser* parser)
18706 {
18707   /* Enter a new parsing context.  */
18708   parser->context = cp_parser_context_new (parser->context);
18709   /* Begin saving tokens.  */
18710   cp_lexer_save_tokens (parser->lexer);
18711   /* In order to avoid repetitive access control error messages,
18712      access checks are queued up until we are no longer parsing
18713      tentatively.  */
18714   push_deferring_access_checks (dk_deferred);
18715 }
18716
18717 /* Commit to the currently active tentative parse.  */
18718
18719 static void
18720 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18721 {
18722   cp_parser_context *context;
18723   cp_lexer *lexer;
18724
18725   /* Mark all of the levels as committed.  */
18726   lexer = parser->lexer;
18727   for (context = parser->context; context->next; context = context->next)
18728     {
18729       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18730         break;
18731       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18732       while (!cp_lexer_saving_tokens (lexer))
18733         lexer = lexer->next;
18734       cp_lexer_commit_tokens (lexer);
18735     }
18736 }
18737
18738 /* Abort the currently active tentative parse.  All consumed tokens
18739    will be rolled back, and no diagnostics will be issued.  */
18740
18741 static void
18742 cp_parser_abort_tentative_parse (cp_parser* parser)
18743 {
18744   cp_parser_simulate_error (parser);
18745   /* Now, pretend that we want to see if the construct was
18746      successfully parsed.  */
18747   cp_parser_parse_definitely (parser);
18748 }
18749
18750 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18751    token stream.  Otherwise, commit to the tokens we have consumed.
18752    Returns true if no error occurred; false otherwise.  */
18753
18754 static bool
18755 cp_parser_parse_definitely (cp_parser* parser)
18756 {
18757   bool error_occurred;
18758   cp_parser_context *context;
18759
18760   /* Remember whether or not an error occurred, since we are about to
18761      destroy that information.  */
18762   error_occurred = cp_parser_error_occurred (parser);
18763   /* Remove the topmost context from the stack.  */
18764   context = parser->context;
18765   parser->context = context->next;
18766   /* If no parse errors occurred, commit to the tentative parse.  */
18767   if (!error_occurred)
18768     {
18769       /* Commit to the tokens read tentatively, unless that was
18770          already done.  */
18771       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18772         cp_lexer_commit_tokens (parser->lexer);
18773
18774       pop_to_parent_deferring_access_checks ();
18775     }
18776   /* Otherwise, if errors occurred, roll back our state so that things
18777      are just as they were before we began the tentative parse.  */
18778   else
18779     {
18780       cp_lexer_rollback_tokens (parser->lexer);
18781       pop_deferring_access_checks ();
18782     }
18783   /* Add the context to the front of the free list.  */
18784   context->next = cp_parser_context_free_list;
18785   cp_parser_context_free_list = context;
18786
18787   return !error_occurred;
18788 }
18789
18790 /* Returns true if we are parsing tentatively and are not committed to
18791    this tentative parse.  */
18792
18793 static bool
18794 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18795 {
18796   return (cp_parser_parsing_tentatively (parser)
18797           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18798 }
18799
18800 /* Returns nonzero iff an error has occurred during the most recent
18801    tentative parse.  */
18802
18803 static bool
18804 cp_parser_error_occurred (cp_parser* parser)
18805 {
18806   return (cp_parser_parsing_tentatively (parser)
18807           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18808 }
18809
18810 /* Returns nonzero if GNU extensions are allowed.  */
18811
18812 static bool
18813 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18814 {
18815   return parser->allow_gnu_extensions_p;
18816 }
18817 \f
18818 /* Objective-C++ Productions */
18819
18820
18821 /* Parse an Objective-C expression, which feeds into a primary-expression
18822    above.
18823
18824    objc-expression:
18825      objc-message-expression
18826      objc-string-literal
18827      objc-encode-expression
18828      objc-protocol-expression
18829      objc-selector-expression
18830
18831   Returns a tree representation of the expression.  */
18832
18833 static tree
18834 cp_parser_objc_expression (cp_parser* parser)
18835 {
18836   /* Try to figure out what kind of declaration is present.  */
18837   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18838
18839   switch (kwd->type)
18840     {
18841     case CPP_OPEN_SQUARE:
18842       return cp_parser_objc_message_expression (parser);
18843
18844     case CPP_OBJC_STRING:
18845       kwd = cp_lexer_consume_token (parser->lexer);
18846       return objc_build_string_object (kwd->u.value);
18847
18848     case CPP_KEYWORD:
18849       switch (kwd->keyword)
18850         {
18851         case RID_AT_ENCODE:
18852           return cp_parser_objc_encode_expression (parser);
18853
18854         case RID_AT_PROTOCOL:
18855           return cp_parser_objc_protocol_expression (parser);
18856
18857         case RID_AT_SELECTOR:
18858           return cp_parser_objc_selector_expression (parser);
18859
18860         default:
18861           break;
18862         }
18863     default:
18864       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18865              &kwd->location, kwd->u.value);
18866       cp_parser_skip_to_end_of_block_or_statement (parser);
18867     }
18868
18869   return error_mark_node;
18870 }
18871
18872 /* Parse an Objective-C message expression.
18873
18874    objc-message-expression:
18875      [ objc-message-receiver objc-message-args ]
18876
18877    Returns a representation of an Objective-C message.  */
18878
18879 static tree
18880 cp_parser_objc_message_expression (cp_parser* parser)
18881 {
18882   tree receiver, messageargs;
18883
18884   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18885   receiver = cp_parser_objc_message_receiver (parser);
18886   messageargs = cp_parser_objc_message_args (parser);
18887   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18888
18889   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18890 }
18891
18892 /* Parse an objc-message-receiver.
18893
18894    objc-message-receiver:
18895      expression
18896      simple-type-specifier
18897
18898   Returns a representation of the type or expression.  */
18899
18900 static tree
18901 cp_parser_objc_message_receiver (cp_parser* parser)
18902 {
18903   tree rcv;
18904
18905   /* An Objective-C message receiver may be either (1) a type
18906      or (2) an expression.  */
18907   cp_parser_parse_tentatively (parser);
18908   rcv = cp_parser_expression (parser, false);
18909
18910   if (cp_parser_parse_definitely (parser))
18911     return rcv;
18912
18913   rcv = cp_parser_simple_type_specifier (parser,
18914                                          /*decl_specs=*/NULL,
18915                                          CP_PARSER_FLAGS_NONE);
18916
18917   return objc_get_class_reference (rcv);
18918 }
18919
18920 /* Parse the arguments and selectors comprising an Objective-C message.
18921
18922    objc-message-args:
18923      objc-selector
18924      objc-selector-args
18925      objc-selector-args , objc-comma-args
18926
18927    objc-selector-args:
18928      objc-selector [opt] : assignment-expression
18929      objc-selector-args objc-selector [opt] : assignment-expression
18930
18931    objc-comma-args:
18932      assignment-expression
18933      objc-comma-args , assignment-expression
18934
18935    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18936    selector arguments and TREE_VALUE containing a list of comma
18937    arguments.  */
18938
18939 static tree
18940 cp_parser_objc_message_args (cp_parser* parser)
18941 {
18942   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18943   bool maybe_unary_selector_p = true;
18944   cp_token *token = cp_lexer_peek_token (parser->lexer);
18945
18946   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18947     {
18948       tree selector = NULL_TREE, arg;
18949
18950       if (token->type != CPP_COLON)
18951         selector = cp_parser_objc_selector (parser);
18952
18953       /* Detect if we have a unary selector.  */
18954       if (maybe_unary_selector_p
18955           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18956         return build_tree_list (selector, NULL_TREE);
18957
18958       maybe_unary_selector_p = false;
18959       cp_parser_require (parser, CPP_COLON, "%<:%>");
18960       arg = cp_parser_assignment_expression (parser, false);
18961
18962       sel_args
18963         = chainon (sel_args,
18964                    build_tree_list (selector, arg));
18965
18966       token = cp_lexer_peek_token (parser->lexer);
18967     }
18968
18969   /* Handle non-selector arguments, if any. */
18970   while (token->type == CPP_COMMA)
18971     {
18972       tree arg;
18973
18974       cp_lexer_consume_token (parser->lexer);
18975       arg = cp_parser_assignment_expression (parser, false);
18976
18977       addl_args
18978         = chainon (addl_args,
18979                    build_tree_list (NULL_TREE, arg));
18980
18981       token = cp_lexer_peek_token (parser->lexer);
18982     }
18983
18984   return build_tree_list (sel_args, addl_args);
18985 }
18986
18987 /* Parse an Objective-C encode expression.
18988
18989    objc-encode-expression:
18990      @encode objc-typename
18991
18992    Returns an encoded representation of the type argument.  */
18993
18994 static tree
18995 cp_parser_objc_encode_expression (cp_parser* parser)
18996 {
18997   tree type;
18998   cp_token *token;
18999
19000   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19001   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19002   token = cp_lexer_peek_token (parser->lexer);
19003   type = complete_type (cp_parser_type_id (parser));
19004   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19005
19006   if (!type)
19007     {
19008       error ("%H%<@encode%> must specify a type as an argument",
19009              &token->location);
19010       return error_mark_node;
19011     }
19012
19013   return objc_build_encode_expr (type);
19014 }
19015
19016 /* Parse an Objective-C @defs expression.  */
19017
19018 static tree
19019 cp_parser_objc_defs_expression (cp_parser *parser)
19020 {
19021   tree name;
19022
19023   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19024   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19025   name = cp_parser_identifier (parser);
19026   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19027
19028   return objc_get_class_ivars (name);
19029 }
19030
19031 /* Parse an Objective-C protocol expression.
19032
19033   objc-protocol-expression:
19034     @protocol ( identifier )
19035
19036   Returns a representation of the protocol expression.  */
19037
19038 static tree
19039 cp_parser_objc_protocol_expression (cp_parser* parser)
19040 {
19041   tree proto;
19042
19043   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19044   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19045   proto = cp_parser_identifier (parser);
19046   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19047
19048   return objc_build_protocol_expr (proto);
19049 }
19050
19051 /* Parse an Objective-C selector expression.
19052
19053    objc-selector-expression:
19054      @selector ( objc-method-signature )
19055
19056    objc-method-signature:
19057      objc-selector
19058      objc-selector-seq
19059
19060    objc-selector-seq:
19061      objc-selector :
19062      objc-selector-seq objc-selector :
19063
19064   Returns a representation of the method selector.  */
19065
19066 static tree
19067 cp_parser_objc_selector_expression (cp_parser* parser)
19068 {
19069   tree sel_seq = NULL_TREE;
19070   bool maybe_unary_selector_p = true;
19071   cp_token *token;
19072
19073   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19074   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19075   token = cp_lexer_peek_token (parser->lexer);
19076
19077   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19078          || token->type == CPP_SCOPE)
19079     {
19080       tree selector = NULL_TREE;
19081
19082       if (token->type != CPP_COLON
19083           || token->type == CPP_SCOPE)
19084         selector = cp_parser_objc_selector (parser);
19085
19086       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19087           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19088         {
19089           /* Detect if we have a unary selector.  */
19090           if (maybe_unary_selector_p)
19091             {
19092               sel_seq = selector;
19093               goto finish_selector;
19094             }
19095           else
19096             {
19097               cp_parser_error (parser, "expected %<:%>");
19098             }
19099         }
19100       maybe_unary_selector_p = false;
19101       token = cp_lexer_consume_token (parser->lexer);
19102
19103       if (token->type == CPP_SCOPE)
19104         {
19105           sel_seq
19106             = chainon (sel_seq,
19107                        build_tree_list (selector, NULL_TREE));
19108           sel_seq
19109             = chainon (sel_seq,
19110                        build_tree_list (NULL_TREE, NULL_TREE));
19111         }
19112       else
19113         sel_seq
19114           = chainon (sel_seq,
19115                      build_tree_list (selector, NULL_TREE));
19116
19117       token = cp_lexer_peek_token (parser->lexer);
19118     }
19119
19120  finish_selector:
19121   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19122
19123   return objc_build_selector_expr (sel_seq);
19124 }
19125
19126 /* Parse a list of identifiers.
19127
19128    objc-identifier-list:
19129      identifier
19130      objc-identifier-list , identifier
19131
19132    Returns a TREE_LIST of identifier nodes.  */
19133
19134 static tree
19135 cp_parser_objc_identifier_list (cp_parser* parser)
19136 {
19137   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19138   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19139
19140   while (sep->type == CPP_COMMA)
19141     {
19142       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19143       list = chainon (list,
19144                       build_tree_list (NULL_TREE,
19145                                        cp_parser_identifier (parser)));
19146       sep = cp_lexer_peek_token (parser->lexer);
19147     }
19148
19149   return list;
19150 }
19151
19152 /* Parse an Objective-C alias declaration.
19153
19154    objc-alias-declaration:
19155      @compatibility_alias identifier identifier ;
19156
19157    This function registers the alias mapping with the Objective-C front end.
19158    It returns nothing.  */
19159
19160 static void
19161 cp_parser_objc_alias_declaration (cp_parser* parser)
19162 {
19163   tree alias, orig;
19164
19165   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19166   alias = cp_parser_identifier (parser);
19167   orig = cp_parser_identifier (parser);
19168   objc_declare_alias (alias, orig);
19169   cp_parser_consume_semicolon_at_end_of_statement (parser);
19170 }
19171
19172 /* Parse an Objective-C class forward-declaration.
19173
19174    objc-class-declaration:
19175      @class objc-identifier-list ;
19176
19177    The function registers the forward declarations with the Objective-C
19178    front end.  It returns nothing.  */
19179
19180 static void
19181 cp_parser_objc_class_declaration (cp_parser* parser)
19182 {
19183   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19184   objc_declare_class (cp_parser_objc_identifier_list (parser));
19185   cp_parser_consume_semicolon_at_end_of_statement (parser);
19186 }
19187
19188 /* Parse a list of Objective-C protocol references.
19189
19190    objc-protocol-refs-opt:
19191      objc-protocol-refs [opt]
19192
19193    objc-protocol-refs:
19194      < objc-identifier-list >
19195
19196    Returns a TREE_LIST of identifiers, if any.  */
19197
19198 static tree
19199 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19200 {
19201   tree protorefs = NULL_TREE;
19202
19203   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19204     {
19205       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19206       protorefs = cp_parser_objc_identifier_list (parser);
19207       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19208     }
19209
19210   return protorefs;
19211 }
19212
19213 /* Parse a Objective-C visibility specification.  */
19214
19215 static void
19216 cp_parser_objc_visibility_spec (cp_parser* parser)
19217 {
19218   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19219
19220   switch (vis->keyword)
19221     {
19222     case RID_AT_PRIVATE:
19223       objc_set_visibility (2);
19224       break;
19225     case RID_AT_PROTECTED:
19226       objc_set_visibility (0);
19227       break;
19228     case RID_AT_PUBLIC:
19229       objc_set_visibility (1);
19230       break;
19231     default:
19232       return;
19233     }
19234
19235   /* Eat '@private'/'@protected'/'@public'.  */
19236   cp_lexer_consume_token (parser->lexer);
19237 }
19238
19239 /* Parse an Objective-C method type.  */
19240
19241 static void
19242 cp_parser_objc_method_type (cp_parser* parser)
19243 {
19244   objc_set_method_type
19245    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19246     ? PLUS_EXPR
19247     : MINUS_EXPR);
19248 }
19249
19250 /* Parse an Objective-C protocol qualifier.  */
19251
19252 static tree
19253 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19254 {
19255   tree quals = NULL_TREE, node;
19256   cp_token *token = cp_lexer_peek_token (parser->lexer);
19257
19258   node = token->u.value;
19259
19260   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19261          && (node == ridpointers [(int) RID_IN]
19262              || node == ridpointers [(int) RID_OUT]
19263              || node == ridpointers [(int) RID_INOUT]
19264              || node == ridpointers [(int) RID_BYCOPY]
19265              || node == ridpointers [(int) RID_BYREF]
19266              || node == ridpointers [(int) RID_ONEWAY]))
19267     {
19268       quals = tree_cons (NULL_TREE, node, quals);
19269       cp_lexer_consume_token (parser->lexer);
19270       token = cp_lexer_peek_token (parser->lexer);
19271       node = token->u.value;
19272     }
19273
19274   return quals;
19275 }
19276
19277 /* Parse an Objective-C typename.  */
19278
19279 static tree
19280 cp_parser_objc_typename (cp_parser* parser)
19281 {
19282   tree type_name = NULL_TREE;
19283
19284   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19285     {
19286       tree proto_quals, cp_type = NULL_TREE;
19287
19288       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19289       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19290
19291       /* An ObjC type name may consist of just protocol qualifiers, in which
19292          case the type shall default to 'id'.  */
19293       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19294         cp_type = cp_parser_type_id (parser);
19295
19296       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19297       type_name = build_tree_list (proto_quals, cp_type);
19298     }
19299
19300   return type_name;
19301 }
19302
19303 /* Check to see if TYPE refers to an Objective-C selector name.  */
19304
19305 static bool
19306 cp_parser_objc_selector_p (enum cpp_ttype type)
19307 {
19308   return (type == CPP_NAME || type == CPP_KEYWORD
19309           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19310           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19311           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19312           || type == CPP_XOR || type == CPP_XOR_EQ);
19313 }
19314
19315 /* Parse an Objective-C selector.  */
19316
19317 static tree
19318 cp_parser_objc_selector (cp_parser* parser)
19319 {
19320   cp_token *token = cp_lexer_consume_token (parser->lexer);
19321
19322   if (!cp_parser_objc_selector_p (token->type))
19323     {
19324       error ("%Hinvalid Objective-C++ selector name", &token->location);
19325       return error_mark_node;
19326     }
19327
19328   /* C++ operator names are allowed to appear in ObjC selectors.  */
19329   switch (token->type)
19330     {
19331     case CPP_AND_AND: return get_identifier ("and");
19332     case CPP_AND_EQ: return get_identifier ("and_eq");
19333     case CPP_AND: return get_identifier ("bitand");
19334     case CPP_OR: return get_identifier ("bitor");
19335     case CPP_COMPL: return get_identifier ("compl");
19336     case CPP_NOT: return get_identifier ("not");
19337     case CPP_NOT_EQ: return get_identifier ("not_eq");
19338     case CPP_OR_OR: return get_identifier ("or");
19339     case CPP_OR_EQ: return get_identifier ("or_eq");
19340     case CPP_XOR: return get_identifier ("xor");
19341     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19342     default: return token->u.value;
19343     }
19344 }
19345
19346 /* Parse an Objective-C params list.  */
19347
19348 static tree
19349 cp_parser_objc_method_keyword_params (cp_parser* parser)
19350 {
19351   tree params = NULL_TREE;
19352   bool maybe_unary_selector_p = true;
19353   cp_token *token = cp_lexer_peek_token (parser->lexer);
19354
19355   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19356     {
19357       tree selector = NULL_TREE, type_name, identifier;
19358
19359       if (token->type != CPP_COLON)
19360         selector = cp_parser_objc_selector (parser);
19361
19362       /* Detect if we have a unary selector.  */
19363       if (maybe_unary_selector_p
19364           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19365         return selector;
19366
19367       maybe_unary_selector_p = false;
19368       cp_parser_require (parser, CPP_COLON, "%<:%>");
19369       type_name = cp_parser_objc_typename (parser);
19370       identifier = cp_parser_identifier (parser);
19371
19372       params
19373         = chainon (params,
19374                    objc_build_keyword_decl (selector,
19375                                             type_name,
19376                                             identifier));
19377
19378       token = cp_lexer_peek_token (parser->lexer);
19379     }
19380
19381   return params;
19382 }
19383
19384 /* Parse the non-keyword Objective-C params.  */
19385
19386 static tree
19387 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19388 {
19389   tree params = make_node (TREE_LIST);
19390   cp_token *token = cp_lexer_peek_token (parser->lexer);
19391   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19392
19393   while (token->type == CPP_COMMA)
19394     {
19395       cp_parameter_declarator *parmdecl;
19396       tree parm;
19397
19398       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19399       token = cp_lexer_peek_token (parser->lexer);
19400
19401       if (token->type == CPP_ELLIPSIS)
19402         {
19403           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19404           *ellipsisp = true;
19405           break;
19406         }
19407
19408       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19409       parm = grokdeclarator (parmdecl->declarator,
19410                              &parmdecl->decl_specifiers,
19411                              PARM, /*initialized=*/0,
19412                              /*attrlist=*/NULL);
19413
19414       chainon (params, build_tree_list (NULL_TREE, parm));
19415       token = cp_lexer_peek_token (parser->lexer);
19416     }
19417
19418   return params;
19419 }
19420
19421 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19422
19423 static void
19424 cp_parser_objc_interstitial_code (cp_parser* parser)
19425 {
19426   cp_token *token = cp_lexer_peek_token (parser->lexer);
19427
19428   /* If the next token is `extern' and the following token is a string
19429      literal, then we have a linkage specification.  */
19430   if (token->keyword == RID_EXTERN
19431       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19432     cp_parser_linkage_specification (parser);
19433   /* Handle #pragma, if any.  */
19434   else if (token->type == CPP_PRAGMA)
19435     cp_parser_pragma (parser, pragma_external);
19436   /* Allow stray semicolons.  */
19437   else if (token->type == CPP_SEMICOLON)
19438     cp_lexer_consume_token (parser->lexer);
19439   /* Finally, try to parse a block-declaration, or a function-definition.  */
19440   else
19441     cp_parser_block_declaration (parser, /*statement_p=*/false);
19442 }
19443
19444 /* Parse a method signature.  */
19445
19446 static tree
19447 cp_parser_objc_method_signature (cp_parser* parser)
19448 {
19449   tree rettype, kwdparms, optparms;
19450   bool ellipsis = false;
19451
19452   cp_parser_objc_method_type (parser);
19453   rettype = cp_parser_objc_typename (parser);
19454   kwdparms = cp_parser_objc_method_keyword_params (parser);
19455   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19456
19457   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19458 }
19459
19460 /* Pars an Objective-C method prototype list.  */
19461
19462 static void
19463 cp_parser_objc_method_prototype_list (cp_parser* parser)
19464 {
19465   cp_token *token = cp_lexer_peek_token (parser->lexer);
19466
19467   while (token->keyword != RID_AT_END)
19468     {
19469       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19470         {
19471           objc_add_method_declaration
19472            (cp_parser_objc_method_signature (parser));
19473           cp_parser_consume_semicolon_at_end_of_statement (parser);
19474         }
19475       else
19476         /* Allow for interspersed non-ObjC++ code.  */
19477         cp_parser_objc_interstitial_code (parser);
19478
19479       token = cp_lexer_peek_token (parser->lexer);
19480     }
19481
19482   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19483   objc_finish_interface ();
19484 }
19485
19486 /* Parse an Objective-C method definition list.  */
19487
19488 static void
19489 cp_parser_objc_method_definition_list (cp_parser* parser)
19490 {
19491   cp_token *token = cp_lexer_peek_token (parser->lexer);
19492
19493   while (token->keyword != RID_AT_END)
19494     {
19495       tree meth;
19496
19497       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19498         {
19499           push_deferring_access_checks (dk_deferred);
19500           objc_start_method_definition
19501            (cp_parser_objc_method_signature (parser));
19502
19503           /* For historical reasons, we accept an optional semicolon.  */
19504           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19505             cp_lexer_consume_token (parser->lexer);
19506
19507           perform_deferred_access_checks ();
19508           stop_deferring_access_checks ();
19509           meth = cp_parser_function_definition_after_declarator (parser,
19510                                                                  false);
19511           pop_deferring_access_checks ();
19512           objc_finish_method_definition (meth);
19513         }
19514       else
19515         /* Allow for interspersed non-ObjC++ code.  */
19516         cp_parser_objc_interstitial_code (parser);
19517
19518       token = cp_lexer_peek_token (parser->lexer);
19519     }
19520
19521   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19522   objc_finish_implementation ();
19523 }
19524
19525 /* Parse Objective-C ivars.  */
19526
19527 static void
19528 cp_parser_objc_class_ivars (cp_parser* parser)
19529 {
19530   cp_token *token = cp_lexer_peek_token (parser->lexer);
19531
19532   if (token->type != CPP_OPEN_BRACE)
19533     return;     /* No ivars specified.  */
19534
19535   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19536   token = cp_lexer_peek_token (parser->lexer);
19537
19538   while (token->type != CPP_CLOSE_BRACE)
19539     {
19540       cp_decl_specifier_seq declspecs;
19541       int decl_class_or_enum_p;
19542       tree prefix_attributes;
19543
19544       cp_parser_objc_visibility_spec (parser);
19545
19546       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19547         break;
19548
19549       cp_parser_decl_specifier_seq (parser,
19550                                     CP_PARSER_FLAGS_OPTIONAL,
19551                                     &declspecs,
19552                                     &decl_class_or_enum_p);
19553       prefix_attributes = declspecs.attributes;
19554       declspecs.attributes = NULL_TREE;
19555
19556       /* Keep going until we hit the `;' at the end of the
19557          declaration.  */
19558       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19559         {
19560           tree width = NULL_TREE, attributes, first_attribute, decl;
19561           cp_declarator *declarator = NULL;
19562           int ctor_dtor_or_conv_p;
19563
19564           /* Check for a (possibly unnamed) bitfield declaration.  */
19565           token = cp_lexer_peek_token (parser->lexer);
19566           if (token->type == CPP_COLON)
19567             goto eat_colon;
19568
19569           if (token->type == CPP_NAME
19570               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19571                   == CPP_COLON))
19572             {
19573               /* Get the name of the bitfield.  */
19574               declarator = make_id_declarator (NULL_TREE,
19575                                                cp_parser_identifier (parser),
19576                                                sfk_none);
19577
19578              eat_colon:
19579               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19580               /* Get the width of the bitfield.  */
19581               width
19582                 = cp_parser_constant_expression (parser,
19583                                                  /*allow_non_constant=*/false,
19584                                                  NULL);
19585             }
19586           else
19587             {
19588               /* Parse the declarator.  */
19589               declarator
19590                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19591                                         &ctor_dtor_or_conv_p,
19592                                         /*parenthesized_p=*/NULL,
19593                                         /*member_p=*/false);
19594             }
19595
19596           /* Look for attributes that apply to the ivar.  */
19597           attributes = cp_parser_attributes_opt (parser);
19598           /* Remember which attributes are prefix attributes and
19599              which are not.  */
19600           first_attribute = attributes;
19601           /* Combine the attributes.  */
19602           attributes = chainon (prefix_attributes, attributes);
19603
19604           if (width)
19605               /* Create the bitfield declaration.  */
19606               decl = grokbitfield (declarator, &declspecs,
19607                                    width,
19608                                    attributes);
19609           else
19610             decl = grokfield (declarator, &declspecs,
19611                               NULL_TREE, /*init_const_expr_p=*/false,
19612                               NULL_TREE, attributes);
19613
19614           /* Add the instance variable.  */
19615           objc_add_instance_variable (decl);
19616
19617           /* Reset PREFIX_ATTRIBUTES.  */
19618           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19619             attributes = TREE_CHAIN (attributes);
19620           if (attributes)
19621             TREE_CHAIN (attributes) = NULL_TREE;
19622
19623           token = cp_lexer_peek_token (parser->lexer);
19624
19625           if (token->type == CPP_COMMA)
19626             {
19627               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19628               continue;
19629             }
19630           break;
19631         }
19632
19633       cp_parser_consume_semicolon_at_end_of_statement (parser);
19634       token = cp_lexer_peek_token (parser->lexer);
19635     }
19636
19637   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19638   /* For historical reasons, we accept an optional semicolon.  */
19639   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19640     cp_lexer_consume_token (parser->lexer);
19641 }
19642
19643 /* Parse an Objective-C protocol declaration.  */
19644
19645 static void
19646 cp_parser_objc_protocol_declaration (cp_parser* parser)
19647 {
19648   tree proto, protorefs;
19649   cp_token *tok;
19650
19651   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19652   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19653     {
19654       tok = cp_lexer_peek_token (parser->lexer);
19655       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19656       goto finish;
19657     }
19658
19659   /* See if we have a forward declaration or a definition.  */
19660   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19661
19662   /* Try a forward declaration first.  */
19663   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19664     {
19665       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19666      finish:
19667       cp_parser_consume_semicolon_at_end_of_statement (parser);
19668     }
19669
19670   /* Ok, we got a full-fledged definition (or at least should).  */
19671   else
19672     {
19673       proto = cp_parser_identifier (parser);
19674       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19675       objc_start_protocol (proto, protorefs);
19676       cp_parser_objc_method_prototype_list (parser);
19677     }
19678 }
19679
19680 /* Parse an Objective-C superclass or category.  */
19681
19682 static void
19683 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19684                                                           tree *categ)
19685 {
19686   cp_token *next = cp_lexer_peek_token (parser->lexer);
19687
19688   *super = *categ = NULL_TREE;
19689   if (next->type == CPP_COLON)
19690     {
19691       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19692       *super = cp_parser_identifier (parser);
19693     }
19694   else if (next->type == CPP_OPEN_PAREN)
19695     {
19696       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19697       *categ = cp_parser_identifier (parser);
19698       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19699     }
19700 }
19701
19702 /* Parse an Objective-C class interface.  */
19703
19704 static void
19705 cp_parser_objc_class_interface (cp_parser* parser)
19706 {
19707   tree name, super, categ, protos;
19708
19709   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19710   name = cp_parser_identifier (parser);
19711   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19712   protos = cp_parser_objc_protocol_refs_opt (parser);
19713
19714   /* We have either a class or a category on our hands.  */
19715   if (categ)
19716     objc_start_category_interface (name, categ, protos);
19717   else
19718     {
19719       objc_start_class_interface (name, super, protos);
19720       /* Handle instance variable declarations, if any.  */
19721       cp_parser_objc_class_ivars (parser);
19722       objc_continue_interface ();
19723     }
19724
19725   cp_parser_objc_method_prototype_list (parser);
19726 }
19727
19728 /* Parse an Objective-C class implementation.  */
19729
19730 static void
19731 cp_parser_objc_class_implementation (cp_parser* parser)
19732 {
19733   tree name, super, categ;
19734
19735   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19736   name = cp_parser_identifier (parser);
19737   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19738
19739   /* We have either a class or a category on our hands.  */
19740   if (categ)
19741     objc_start_category_implementation (name, categ);
19742   else
19743     {
19744       objc_start_class_implementation (name, super);
19745       /* Handle instance variable declarations, if any.  */
19746       cp_parser_objc_class_ivars (parser);
19747       objc_continue_implementation ();
19748     }
19749
19750   cp_parser_objc_method_definition_list (parser);
19751 }
19752
19753 /* Consume the @end token and finish off the implementation.  */
19754
19755 static void
19756 cp_parser_objc_end_implementation (cp_parser* parser)
19757 {
19758   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19759   objc_finish_implementation ();
19760 }
19761
19762 /* Parse an Objective-C declaration.  */
19763
19764 static void
19765 cp_parser_objc_declaration (cp_parser* parser)
19766 {
19767   /* Try to figure out what kind of declaration is present.  */
19768   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19769
19770   switch (kwd->keyword)
19771     {
19772     case RID_AT_ALIAS:
19773       cp_parser_objc_alias_declaration (parser);
19774       break;
19775     case RID_AT_CLASS:
19776       cp_parser_objc_class_declaration (parser);
19777       break;
19778     case RID_AT_PROTOCOL:
19779       cp_parser_objc_protocol_declaration (parser);
19780       break;
19781     case RID_AT_INTERFACE:
19782       cp_parser_objc_class_interface (parser);
19783       break;
19784     case RID_AT_IMPLEMENTATION:
19785       cp_parser_objc_class_implementation (parser);
19786       break;
19787     case RID_AT_END:
19788       cp_parser_objc_end_implementation (parser);
19789       break;
19790     default:
19791       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19792              &kwd->location, kwd->u.value);
19793       cp_parser_skip_to_end_of_block_or_statement (parser);
19794     }
19795 }
19796
19797 /* Parse an Objective-C try-catch-finally statement.
19798
19799    objc-try-catch-finally-stmt:
19800      @try compound-statement objc-catch-clause-seq [opt]
19801        objc-finally-clause [opt]
19802
19803    objc-catch-clause-seq:
19804      objc-catch-clause objc-catch-clause-seq [opt]
19805
19806    objc-catch-clause:
19807      @catch ( exception-declaration ) compound-statement
19808
19809    objc-finally-clause
19810      @finally compound-statement
19811
19812    Returns NULL_TREE.  */
19813
19814 static tree
19815 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19816   location_t location;
19817   tree stmt;
19818
19819   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19820   location = cp_lexer_peek_token (parser->lexer)->location;
19821   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19822      node, lest it get absorbed into the surrounding block.  */
19823   stmt = push_stmt_list ();
19824   cp_parser_compound_statement (parser, NULL, false);
19825   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19826
19827   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19828     {
19829       cp_parameter_declarator *parmdecl;
19830       tree parm;
19831
19832       cp_lexer_consume_token (parser->lexer);
19833       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19834       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19835       parm = grokdeclarator (parmdecl->declarator,
19836                              &parmdecl->decl_specifiers,
19837                              PARM, /*initialized=*/0,
19838                              /*attrlist=*/NULL);
19839       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19840       objc_begin_catch_clause (parm);
19841       cp_parser_compound_statement (parser, NULL, false);
19842       objc_finish_catch_clause ();
19843     }
19844
19845   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19846     {
19847       cp_lexer_consume_token (parser->lexer);
19848       location = cp_lexer_peek_token (parser->lexer)->location;
19849       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19850          node, lest it get absorbed into the surrounding block.  */
19851       stmt = push_stmt_list ();
19852       cp_parser_compound_statement (parser, NULL, false);
19853       objc_build_finally_clause (location, pop_stmt_list (stmt));
19854     }
19855
19856   return objc_finish_try_stmt ();
19857 }
19858
19859 /* Parse an Objective-C synchronized statement.
19860
19861    objc-synchronized-stmt:
19862      @synchronized ( expression ) compound-statement
19863
19864    Returns NULL_TREE.  */
19865
19866 static tree
19867 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19868   location_t location;
19869   tree lock, stmt;
19870
19871   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19872
19873   location = cp_lexer_peek_token (parser->lexer)->location;
19874   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19875   lock = cp_parser_expression (parser, false);
19876   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19877
19878   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19879      node, lest it get absorbed into the surrounding block.  */
19880   stmt = push_stmt_list ();
19881   cp_parser_compound_statement (parser, NULL, false);
19882
19883   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19884 }
19885
19886 /* Parse an Objective-C throw statement.
19887
19888    objc-throw-stmt:
19889      @throw assignment-expression [opt] ;
19890
19891    Returns a constructed '@throw' statement.  */
19892
19893 static tree
19894 cp_parser_objc_throw_statement (cp_parser *parser) {
19895   tree expr = NULL_TREE;
19896
19897   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19898
19899   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19900     expr = cp_parser_assignment_expression (parser, false);
19901
19902   cp_parser_consume_semicolon_at_end_of_statement (parser);
19903
19904   return objc_build_throw_stmt (expr);
19905 }
19906
19907 /* Parse an Objective-C statement.  */
19908
19909 static tree
19910 cp_parser_objc_statement (cp_parser * parser) {
19911   /* Try to figure out what kind of declaration is present.  */
19912   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19913
19914   switch (kwd->keyword)
19915     {
19916     case RID_AT_TRY:
19917       return cp_parser_objc_try_catch_finally_statement (parser);
19918     case RID_AT_SYNCHRONIZED:
19919       return cp_parser_objc_synchronized_statement (parser);
19920     case RID_AT_THROW:
19921       return cp_parser_objc_throw_statement (parser);
19922     default:
19923       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19924              &kwd->location, kwd->u.value);
19925       cp_parser_skip_to_end_of_block_or_statement (parser);
19926     }
19927
19928   return error_mark_node;
19929 }
19930 \f
19931 /* OpenMP 2.5 parsing routines.  */
19932
19933 /* Returns name of the next clause.
19934    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19935    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19936    returned and the token is consumed.  */
19937
19938 static pragma_omp_clause
19939 cp_parser_omp_clause_name (cp_parser *parser)
19940 {
19941   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19942
19943   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19944     result = PRAGMA_OMP_CLAUSE_IF;
19945   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19946     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19947   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19948     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19949   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19950     {
19951       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19952       const char *p = IDENTIFIER_POINTER (id);
19953
19954       switch (p[0])
19955         {
19956         case 'c':
19957           if (!strcmp ("collapse", p))
19958             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19959           else if (!strcmp ("copyin", p))
19960             result = PRAGMA_OMP_CLAUSE_COPYIN;
19961           else if (!strcmp ("copyprivate", p))
19962             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19963           break;
19964         case 'f':
19965           if (!strcmp ("firstprivate", p))
19966             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19967           break;
19968         case 'l':
19969           if (!strcmp ("lastprivate", p))
19970             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19971           break;
19972         case 'n':
19973           if (!strcmp ("nowait", p))
19974             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19975           else if (!strcmp ("num_threads", p))
19976             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19977           break;
19978         case 'o':
19979           if (!strcmp ("ordered", p))
19980             result = PRAGMA_OMP_CLAUSE_ORDERED;
19981           break;
19982         case 'r':
19983           if (!strcmp ("reduction", p))
19984             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19985           break;
19986         case 's':
19987           if (!strcmp ("schedule", p))
19988             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19989           else if (!strcmp ("shared", p))
19990             result = PRAGMA_OMP_CLAUSE_SHARED;
19991           break;
19992         case 'u':
19993           if (!strcmp ("untied", p))
19994             result = PRAGMA_OMP_CLAUSE_UNTIED;
19995           break;
19996         }
19997     }
19998
19999   if (result != PRAGMA_OMP_CLAUSE_NONE)
20000     cp_lexer_consume_token (parser->lexer);
20001
20002   return result;
20003 }
20004
20005 /* Validate that a clause of the given type does not already exist.  */
20006
20007 static void
20008 check_no_duplicate_clause (tree clauses, enum tree_code code,
20009                            const char *name, location_t location)
20010 {
20011   tree c;
20012
20013   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20014     if (OMP_CLAUSE_CODE (c) == code)
20015       {
20016         error ("%Htoo many %qs clauses", &location, name);
20017         break;
20018       }
20019 }
20020
20021 /* OpenMP 2.5:
20022    variable-list:
20023      identifier
20024      variable-list , identifier
20025
20026    In addition, we match a closing parenthesis.  An opening parenthesis
20027    will have been consumed by the caller.
20028
20029    If KIND is nonzero, create the appropriate node and install the decl
20030    in OMP_CLAUSE_DECL and add the node to the head of the list.
20031
20032    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20033    return the list created.  */
20034
20035 static tree
20036 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20037                                 tree list)
20038 {
20039   cp_token *token;
20040   while (1)
20041     {
20042       tree name, decl;
20043
20044       token = cp_lexer_peek_token (parser->lexer);
20045       name = cp_parser_id_expression (parser, /*template_p=*/false,
20046                                       /*check_dependency_p=*/true,
20047                                       /*template_p=*/NULL,
20048                                       /*declarator_p=*/false,
20049                                       /*optional_p=*/false);
20050       if (name == error_mark_node)
20051         goto skip_comma;
20052
20053       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20054       if (decl == error_mark_node)
20055         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20056       else if (kind != 0)
20057         {
20058           tree u = build_omp_clause (kind);
20059           OMP_CLAUSE_DECL (u) = decl;
20060           OMP_CLAUSE_CHAIN (u) = list;
20061           list = u;
20062         }
20063       else
20064         list = tree_cons (decl, NULL_TREE, list);
20065
20066     get_comma:
20067       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20068         break;
20069       cp_lexer_consume_token (parser->lexer);
20070     }
20071
20072   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20073     {
20074       int ending;
20075
20076       /* Try to resync to an unnested comma.  Copied from
20077          cp_parser_parenthesized_expression_list.  */
20078     skip_comma:
20079       ending = cp_parser_skip_to_closing_parenthesis (parser,
20080                                                       /*recovering=*/true,
20081                                                       /*or_comma=*/true,
20082                                                       /*consume_paren=*/true);
20083       if (ending < 0)
20084         goto get_comma;
20085     }
20086
20087   return list;
20088 }
20089
20090 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20091    common case for omp clauses.  */
20092
20093 static tree
20094 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20095 {
20096   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20097     return cp_parser_omp_var_list_no_open (parser, kind, list);
20098   return list;
20099 }
20100
20101 /* OpenMP 3.0:
20102    collapse ( constant-expression ) */
20103
20104 static tree
20105 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20106 {
20107   tree c, num;
20108   location_t loc;
20109   HOST_WIDE_INT n;
20110
20111   loc = cp_lexer_peek_token (parser->lexer)->location;
20112   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20113     return list;
20114
20115   num = cp_parser_constant_expression (parser, false, NULL);
20116
20117   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20118     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20119                                            /*or_comma=*/false,
20120                                            /*consume_paren=*/true);
20121
20122   if (num == error_mark_node)
20123     return list;
20124   num = fold_non_dependent_expr (num);
20125   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20126       || !host_integerp (num, 0)
20127       || (n = tree_low_cst (num, 0)) <= 0
20128       || (int) n != n)
20129     {
20130       error ("%Hcollapse argument needs positive constant integer expression",
20131              &loc);
20132       return list;
20133     }
20134
20135   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20136   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20137   OMP_CLAUSE_CHAIN (c) = list;
20138   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20139
20140   return c;
20141 }
20142
20143 /* OpenMP 2.5:
20144    default ( shared | none ) */
20145
20146 static tree
20147 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20148 {
20149   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20150   tree c;
20151
20152   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20153     return list;
20154   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20155     {
20156       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20157       const char *p = IDENTIFIER_POINTER (id);
20158
20159       switch (p[0])
20160         {
20161         case 'n':
20162           if (strcmp ("none", p) != 0)
20163             goto invalid_kind;
20164           kind = OMP_CLAUSE_DEFAULT_NONE;
20165           break;
20166
20167         case 's':
20168           if (strcmp ("shared", p) != 0)
20169             goto invalid_kind;
20170           kind = OMP_CLAUSE_DEFAULT_SHARED;
20171           break;
20172
20173         default:
20174           goto invalid_kind;
20175         }
20176
20177       cp_lexer_consume_token (parser->lexer);
20178     }
20179   else
20180     {
20181     invalid_kind:
20182       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20183     }
20184
20185   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20186     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20187                                            /*or_comma=*/false,
20188                                            /*consume_paren=*/true);
20189
20190   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20191     return list;
20192
20193   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20194   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20195   OMP_CLAUSE_CHAIN (c) = list;
20196   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20197
20198   return c;
20199 }
20200
20201 /* OpenMP 2.5:
20202    if ( expression ) */
20203
20204 static tree
20205 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20206 {
20207   tree t, c;
20208
20209   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20210     return list;
20211
20212   t = cp_parser_condition (parser);
20213
20214   if (t == error_mark_node
20215       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20216     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20217                                            /*or_comma=*/false,
20218                                            /*consume_paren=*/true);
20219
20220   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20221
20222   c = build_omp_clause (OMP_CLAUSE_IF);
20223   OMP_CLAUSE_IF_EXPR (c) = t;
20224   OMP_CLAUSE_CHAIN (c) = list;
20225
20226   return c;
20227 }
20228
20229 /* OpenMP 2.5:
20230    nowait */
20231
20232 static tree
20233 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20234                              tree list, location_t location)
20235 {
20236   tree c;
20237
20238   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20239
20240   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20241   OMP_CLAUSE_CHAIN (c) = list;
20242   return c;
20243 }
20244
20245 /* OpenMP 2.5:
20246    num_threads ( expression ) */
20247
20248 static tree
20249 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20250                                   location_t location)
20251 {
20252   tree t, c;
20253
20254   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20255     return list;
20256
20257   t = cp_parser_expression (parser, false);
20258
20259   if (t == error_mark_node
20260       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20261     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20262                                            /*or_comma=*/false,
20263                                            /*consume_paren=*/true);
20264
20265   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20266                              "num_threads", location);
20267
20268   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20269   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20270   OMP_CLAUSE_CHAIN (c) = list;
20271
20272   return c;
20273 }
20274
20275 /* OpenMP 2.5:
20276    ordered */
20277
20278 static tree
20279 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20280                               tree list, location_t location)
20281 {
20282   tree c;
20283
20284   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20285                              "ordered", location);
20286
20287   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20288   OMP_CLAUSE_CHAIN (c) = list;
20289   return c;
20290 }
20291
20292 /* OpenMP 2.5:
20293    reduction ( reduction-operator : variable-list )
20294
20295    reduction-operator:
20296      One of: + * - & ^ | && || */
20297
20298 static tree
20299 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20300 {
20301   enum tree_code code;
20302   tree nlist, c;
20303
20304   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20305     return list;
20306
20307   switch (cp_lexer_peek_token (parser->lexer)->type)
20308     {
20309     case CPP_PLUS:
20310       code = PLUS_EXPR;
20311       break;
20312     case CPP_MULT:
20313       code = MULT_EXPR;
20314       break;
20315     case CPP_MINUS:
20316       code = MINUS_EXPR;
20317       break;
20318     case CPP_AND:
20319       code = BIT_AND_EXPR;
20320       break;
20321     case CPP_XOR:
20322       code = BIT_XOR_EXPR;
20323       break;
20324     case CPP_OR:
20325       code = BIT_IOR_EXPR;
20326       break;
20327     case CPP_AND_AND:
20328       code = TRUTH_ANDIF_EXPR;
20329       break;
20330     case CPP_OR_OR:
20331       code = TRUTH_ORIF_EXPR;
20332       break;
20333     default:
20334       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20335                                "%<|%>, %<&&%>, or %<||%>");
20336     resync_fail:
20337       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20338                                              /*or_comma=*/false,
20339                                              /*consume_paren=*/true);
20340       return list;
20341     }
20342   cp_lexer_consume_token (parser->lexer);
20343
20344   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20345     goto resync_fail;
20346
20347   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20348   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20349     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20350
20351   return nlist;
20352 }
20353
20354 /* OpenMP 2.5:
20355    schedule ( schedule-kind )
20356    schedule ( schedule-kind , expression )
20357
20358    schedule-kind:
20359      static | dynamic | guided | runtime | auto  */
20360
20361 static tree
20362 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20363 {
20364   tree c, t;
20365
20366   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20367     return list;
20368
20369   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20370
20371   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20372     {
20373       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20374       const char *p = IDENTIFIER_POINTER (id);
20375
20376       switch (p[0])
20377         {
20378         case 'd':
20379           if (strcmp ("dynamic", p) != 0)
20380             goto invalid_kind;
20381           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20382           break;
20383
20384         case 'g':
20385           if (strcmp ("guided", p) != 0)
20386             goto invalid_kind;
20387           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20388           break;
20389
20390         case 'r':
20391           if (strcmp ("runtime", p) != 0)
20392             goto invalid_kind;
20393           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20394           break;
20395
20396         default:
20397           goto invalid_kind;
20398         }
20399     }
20400   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20401     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20402   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20403     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20404   else
20405     goto invalid_kind;
20406   cp_lexer_consume_token (parser->lexer);
20407
20408   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20409     {
20410       cp_token *token;
20411       cp_lexer_consume_token (parser->lexer);
20412
20413       token = cp_lexer_peek_token (parser->lexer);
20414       t = cp_parser_assignment_expression (parser, false);
20415
20416       if (t == error_mark_node)
20417         goto resync_fail;
20418       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20419         error ("%Hschedule %<runtime%> does not take "
20420                "a %<chunk_size%> parameter", &token->location);
20421       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20422         error ("%Hschedule %<auto%> does not take "
20423                "a %<chunk_size%> parameter", &token->location);
20424       else
20425         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20426
20427       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20428         goto resync_fail;
20429     }
20430   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20431     goto resync_fail;
20432
20433   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20434   OMP_CLAUSE_CHAIN (c) = list;
20435   return c;
20436
20437  invalid_kind:
20438   cp_parser_error (parser, "invalid schedule kind");
20439  resync_fail:
20440   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20441                                          /*or_comma=*/false,
20442                                          /*consume_paren=*/true);
20443   return list;
20444 }
20445
20446 /* OpenMP 3.0:
20447    untied */
20448
20449 static tree
20450 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20451                              tree list, location_t location)
20452 {
20453   tree c;
20454
20455   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20456
20457   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20458   OMP_CLAUSE_CHAIN (c) = list;
20459   return c;
20460 }
20461
20462 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20463    is a bitmask in MASK.  Return the list of clauses found; the result
20464    of clause default goes in *pdefault.  */
20465
20466 static tree
20467 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20468                            const char *where, cp_token *pragma_tok)
20469 {
20470   tree clauses = NULL;
20471   bool first = true;
20472   cp_token *token = NULL;
20473
20474   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20475     {
20476       pragma_omp_clause c_kind;
20477       const char *c_name;
20478       tree prev = clauses;
20479
20480       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20481         cp_lexer_consume_token (parser->lexer);
20482
20483       token = cp_lexer_peek_token (parser->lexer);
20484       c_kind = cp_parser_omp_clause_name (parser);
20485       first = false;
20486
20487       switch (c_kind)
20488         {
20489         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20490           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20491                                                    token->location);
20492           c_name = "collapse";
20493           break;
20494         case PRAGMA_OMP_CLAUSE_COPYIN:
20495           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20496           c_name = "copyin";
20497           break;
20498         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20499           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20500                                             clauses);
20501           c_name = "copyprivate";
20502           break;
20503         case PRAGMA_OMP_CLAUSE_DEFAULT:
20504           clauses = cp_parser_omp_clause_default (parser, clauses,
20505                                                   token->location);
20506           c_name = "default";
20507           break;
20508         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20509           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20510                                             clauses);
20511           c_name = "firstprivate";
20512           break;
20513         case PRAGMA_OMP_CLAUSE_IF:
20514           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20515           c_name = "if";
20516           break;
20517         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20518           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20519                                             clauses);
20520           c_name = "lastprivate";
20521           break;
20522         case PRAGMA_OMP_CLAUSE_NOWAIT:
20523           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20524           c_name = "nowait";
20525           break;
20526         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20527           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20528                                                       token->location);
20529           c_name = "num_threads";
20530           break;
20531         case PRAGMA_OMP_CLAUSE_ORDERED:
20532           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20533                                                   token->location);
20534           c_name = "ordered";
20535           break;
20536         case PRAGMA_OMP_CLAUSE_PRIVATE:
20537           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20538                                             clauses);
20539           c_name = "private";
20540           break;
20541         case PRAGMA_OMP_CLAUSE_REDUCTION:
20542           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20543           c_name = "reduction";
20544           break;
20545         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20546           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20547                                                    token->location);
20548           c_name = "schedule";
20549           break;
20550         case PRAGMA_OMP_CLAUSE_SHARED:
20551           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20552                                             clauses);
20553           c_name = "shared";
20554           break;
20555         case PRAGMA_OMP_CLAUSE_UNTIED:
20556           clauses = cp_parser_omp_clause_untied (parser, clauses,
20557                                                  token->location);
20558           c_name = "nowait";
20559           break;
20560         default:
20561           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20562           goto saw_error;
20563         }
20564
20565       if (((mask >> c_kind) & 1) == 0)
20566         {
20567           /* Remove the invalid clause(s) from the list to avoid
20568              confusing the rest of the compiler.  */
20569           clauses = prev;
20570           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20571         }
20572     }
20573  saw_error:
20574   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20575   return finish_omp_clauses (clauses);
20576 }
20577
20578 /* OpenMP 2.5:
20579    structured-block:
20580      statement
20581
20582    In practice, we're also interested in adding the statement to an
20583    outer node.  So it is convenient if we work around the fact that
20584    cp_parser_statement calls add_stmt.  */
20585
20586 static unsigned
20587 cp_parser_begin_omp_structured_block (cp_parser *parser)
20588 {
20589   unsigned save = parser->in_statement;
20590
20591   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20592      This preserves the "not within loop or switch" style error messages
20593      for nonsense cases like
20594         void foo() {
20595         #pragma omp single
20596           break;
20597         }
20598   */
20599   if (parser->in_statement)
20600     parser->in_statement = IN_OMP_BLOCK;
20601
20602   return save;
20603 }
20604
20605 static void
20606 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20607 {
20608   parser->in_statement = save;
20609 }
20610
20611 static tree
20612 cp_parser_omp_structured_block (cp_parser *parser)
20613 {
20614   tree stmt = begin_omp_structured_block ();
20615   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20616
20617   cp_parser_statement (parser, NULL_TREE, false, NULL);
20618
20619   cp_parser_end_omp_structured_block (parser, save);
20620   return finish_omp_structured_block (stmt);
20621 }
20622
20623 /* OpenMP 2.5:
20624    # pragma omp atomic new-line
20625      expression-stmt
20626
20627    expression-stmt:
20628      x binop= expr | x++ | ++x | x-- | --x
20629    binop:
20630      +, *, -, /, &, ^, |, <<, >>
20631
20632   where x is an lvalue expression with scalar type.  */
20633
20634 static void
20635 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20636 {
20637   tree lhs, rhs;
20638   enum tree_code code;
20639
20640   cp_parser_require_pragma_eol (parser, pragma_tok);
20641
20642   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20643                                     /*cast_p=*/false);
20644   switch (TREE_CODE (lhs))
20645     {
20646     case ERROR_MARK:
20647       goto saw_error;
20648
20649     case PREINCREMENT_EXPR:
20650     case POSTINCREMENT_EXPR:
20651       lhs = TREE_OPERAND (lhs, 0);
20652       code = PLUS_EXPR;
20653       rhs = integer_one_node;
20654       break;
20655
20656     case PREDECREMENT_EXPR:
20657     case POSTDECREMENT_EXPR:
20658       lhs = TREE_OPERAND (lhs, 0);
20659       code = MINUS_EXPR;
20660       rhs = integer_one_node;
20661       break;
20662
20663     default:
20664       switch (cp_lexer_peek_token (parser->lexer)->type)
20665         {
20666         case CPP_MULT_EQ:
20667           code = MULT_EXPR;
20668           break;
20669         case CPP_DIV_EQ:
20670           code = TRUNC_DIV_EXPR;
20671           break;
20672         case CPP_PLUS_EQ:
20673           code = PLUS_EXPR;
20674           break;
20675         case CPP_MINUS_EQ:
20676           code = MINUS_EXPR;
20677           break;
20678         case CPP_LSHIFT_EQ:
20679           code = LSHIFT_EXPR;
20680           break;
20681         case CPP_RSHIFT_EQ:
20682           code = RSHIFT_EXPR;
20683           break;
20684         case CPP_AND_EQ:
20685           code = BIT_AND_EXPR;
20686           break;
20687         case CPP_OR_EQ:
20688           code = BIT_IOR_EXPR;
20689           break;
20690         case CPP_XOR_EQ:
20691           code = BIT_XOR_EXPR;
20692           break;
20693         default:
20694           cp_parser_error (parser,
20695                            "invalid operator for %<#pragma omp atomic%>");
20696           goto saw_error;
20697         }
20698       cp_lexer_consume_token (parser->lexer);
20699
20700       rhs = cp_parser_expression (parser, false);
20701       if (rhs == error_mark_node)
20702         goto saw_error;
20703       break;
20704     }
20705   finish_omp_atomic (code, lhs, rhs);
20706   cp_parser_consume_semicolon_at_end_of_statement (parser);
20707   return;
20708
20709  saw_error:
20710   cp_parser_skip_to_end_of_block_or_statement (parser);
20711 }
20712
20713
20714 /* OpenMP 2.5:
20715    # pragma omp barrier new-line  */
20716
20717 static void
20718 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20719 {
20720   cp_parser_require_pragma_eol (parser, pragma_tok);
20721   finish_omp_barrier ();
20722 }
20723
20724 /* OpenMP 2.5:
20725    # pragma omp critical [(name)] new-line
20726      structured-block  */
20727
20728 static tree
20729 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20730 {
20731   tree stmt, name = NULL;
20732
20733   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20734     {
20735       cp_lexer_consume_token (parser->lexer);
20736
20737       name = cp_parser_identifier (parser);
20738
20739       if (name == error_mark_node
20740           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20741         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20742                                                /*or_comma=*/false,
20743                                                /*consume_paren=*/true);
20744       if (name == error_mark_node)
20745         name = NULL;
20746     }
20747   cp_parser_require_pragma_eol (parser, pragma_tok);
20748
20749   stmt = cp_parser_omp_structured_block (parser);
20750   return c_finish_omp_critical (stmt, name);
20751 }
20752
20753 /* OpenMP 2.5:
20754    # pragma omp flush flush-vars[opt] new-line
20755
20756    flush-vars:
20757      ( variable-list ) */
20758
20759 static void
20760 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20761 {
20762   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20763     (void) cp_parser_omp_var_list (parser, 0, NULL);
20764   cp_parser_require_pragma_eol (parser, pragma_tok);
20765
20766   finish_omp_flush ();
20767 }
20768
20769 /* Helper function, to parse omp for increment expression.  */
20770
20771 static tree
20772 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20773 {
20774   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20775   enum tree_code op;
20776   cp_token *token;
20777
20778   if (lhs != decl)
20779     {
20780       cp_parser_skip_to_end_of_statement (parser);
20781       return error_mark_node;
20782     }
20783
20784   token = cp_lexer_peek_token (parser->lexer);
20785   op = binops_by_token [token->type].tree_type;
20786   switch (op)
20787     {
20788     case LT_EXPR:
20789     case LE_EXPR:
20790     case GT_EXPR:
20791     case GE_EXPR:
20792       break;
20793     default:
20794       cp_parser_skip_to_end_of_statement (parser);
20795       return error_mark_node;
20796     }
20797
20798   cp_lexer_consume_token (parser->lexer);
20799   rhs = cp_parser_binary_expression (parser, false,
20800                                      PREC_RELATIONAL_EXPRESSION);
20801   if (rhs == error_mark_node
20802       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20803     {
20804       cp_parser_skip_to_end_of_statement (parser);
20805       return error_mark_node;
20806     }
20807
20808   return build2 (op, boolean_type_node, lhs, rhs);
20809 }
20810
20811 /* Helper function, to parse omp for increment expression.  */
20812
20813 static tree
20814 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20815 {
20816   cp_token *token = cp_lexer_peek_token (parser->lexer);
20817   enum tree_code op;
20818   tree lhs, rhs;
20819   cp_id_kind idk;
20820   bool decl_first;
20821
20822   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20823     {
20824       op = (token->type == CPP_PLUS_PLUS
20825             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20826       cp_lexer_consume_token (parser->lexer);
20827       lhs = cp_parser_cast_expression (parser, false, false);
20828       if (lhs != decl)
20829         return error_mark_node;
20830       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20831     }
20832
20833   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20834   if (lhs != decl)
20835     return error_mark_node;
20836
20837   token = cp_lexer_peek_token (parser->lexer);
20838   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20839     {
20840       op = (token->type == CPP_PLUS_PLUS
20841             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20842       cp_lexer_consume_token (parser->lexer);
20843       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20844     }
20845
20846   op = cp_parser_assignment_operator_opt (parser);
20847   if (op == ERROR_MARK)
20848     return error_mark_node;
20849
20850   if (op != NOP_EXPR)
20851     {
20852       rhs = cp_parser_assignment_expression (parser, false);
20853       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20854       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20855     }
20856
20857   lhs = cp_parser_binary_expression (parser, false,
20858                                      PREC_ADDITIVE_EXPRESSION);
20859   token = cp_lexer_peek_token (parser->lexer);
20860   decl_first = lhs == decl;
20861   if (decl_first)
20862     lhs = NULL_TREE;
20863   if (token->type != CPP_PLUS
20864       && token->type != CPP_MINUS)
20865     return error_mark_node;
20866
20867   do
20868     {
20869       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20870       cp_lexer_consume_token (parser->lexer);
20871       rhs = cp_parser_binary_expression (parser, false,
20872                                          PREC_ADDITIVE_EXPRESSION);
20873       token = cp_lexer_peek_token (parser->lexer);
20874       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20875         {
20876           if (lhs == NULL_TREE)
20877             {
20878               if (op == PLUS_EXPR)
20879                 lhs = rhs;
20880               else
20881                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20882             }
20883           else
20884             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20885                                      NULL, tf_warning_or_error);
20886         }
20887     }
20888   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20889
20890   if (!decl_first)
20891     {
20892       if (rhs != decl || op == MINUS_EXPR)
20893         return error_mark_node;
20894       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20895     }
20896   else
20897     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20898
20899   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20900 }
20901
20902 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20903
20904 static tree
20905 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20906 {
20907   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20908   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20909   tree this_pre_body, cl;
20910   location_t loc_first;
20911   bool collapse_err = false;
20912   int i, collapse = 1, nbraces = 0;
20913
20914   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20915     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20916       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20917
20918   gcc_assert (collapse >= 1);
20919
20920   declv = make_tree_vec (collapse);
20921   initv = make_tree_vec (collapse);
20922   condv = make_tree_vec (collapse);
20923   incrv = make_tree_vec (collapse);
20924
20925   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20926
20927   for (i = 0; i < collapse; i++)
20928     {
20929       int bracecount = 0;
20930       bool add_private_clause = false;
20931       location_t loc;
20932
20933       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20934         {
20935           cp_parser_error (parser, "for statement expected");
20936           return NULL;
20937         }
20938       loc = cp_lexer_consume_token (parser->lexer)->location;
20939
20940       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20941         return NULL;
20942
20943       init = decl = real_decl = NULL;
20944       this_pre_body = push_stmt_list ();
20945       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20946         {
20947           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20948
20949              init-expr:
20950                        var = lb
20951                        integer-type var = lb
20952                        random-access-iterator-type var = lb
20953                        pointer-type var = lb
20954           */
20955           cp_decl_specifier_seq type_specifiers;
20956
20957           /* First, try to parse as an initialized declaration.  See
20958              cp_parser_condition, from whence the bulk of this is copied.  */
20959
20960           cp_parser_parse_tentatively (parser);
20961           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20962                                         &type_specifiers);
20963           if (cp_parser_parse_definitely (parser))
20964             {
20965               /* If parsing a type specifier seq succeeded, then this
20966                  MUST be a initialized declaration.  */
20967               tree asm_specification, attributes;
20968               cp_declarator *declarator;
20969
20970               declarator = cp_parser_declarator (parser,
20971                                                  CP_PARSER_DECLARATOR_NAMED,
20972                                                  /*ctor_dtor_or_conv_p=*/NULL,
20973                                                  /*parenthesized_p=*/NULL,
20974                                                  /*member_p=*/false);
20975               attributes = cp_parser_attributes_opt (parser);
20976               asm_specification = cp_parser_asm_specification_opt (parser);
20977
20978               if (declarator == cp_error_declarator) 
20979                 cp_parser_skip_to_end_of_statement (parser);
20980
20981               else 
20982                 {
20983                   tree pushed_scope;
20984
20985                   decl = start_decl (declarator, &type_specifiers,
20986                                      /*initialized_p=*/false, attributes,
20987                                      /*prefix_attributes=*/NULL_TREE,
20988                                      &pushed_scope);
20989
20990                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20991                     {
20992                       if (cp_lexer_next_token_is (parser->lexer, 
20993                                                   CPP_OPEN_PAREN))
20994                         error ("parenthesized initialization is not allowed in "
20995                                "OpenMP %<for%> loop");
20996                       else
20997                         /* Trigger an error.  */
20998                         cp_parser_require (parser, CPP_EQ, "%<=%>");
20999
21000                       init = error_mark_node;
21001                       cp_parser_skip_to_end_of_statement (parser);
21002                     }
21003                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21004                            || type_dependent_expression_p (decl))
21005                     {
21006                       bool is_direct_init, is_non_constant_init;
21007
21008                       init = cp_parser_initializer (parser,
21009                                                     &is_direct_init,
21010                                                     &is_non_constant_init);
21011
21012                       cp_finish_decl (decl, init, !is_non_constant_init,
21013                                       asm_specification,
21014                                       LOOKUP_ONLYCONVERTING);
21015                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21016                         {
21017                           for_block
21018                             = tree_cons (NULL, this_pre_body, for_block);
21019                           init = NULL_TREE;
21020                         }
21021                       else
21022                         init = pop_stmt_list (this_pre_body);
21023                       this_pre_body = NULL_TREE;
21024                     }
21025                   else
21026                     {
21027                       /* Consume '='.  */
21028                       cp_lexer_consume_token (parser->lexer);
21029                       init = cp_parser_assignment_expression (parser, false);
21030
21031                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21032                         init = error_mark_node;
21033                       else
21034                         cp_finish_decl (decl, NULL_TREE,
21035                                         /*init_const_expr_p=*/false,
21036                                         asm_specification,
21037                                         LOOKUP_ONLYCONVERTING);
21038                     }
21039
21040                   if (pushed_scope)
21041                     pop_scope (pushed_scope);
21042                 }
21043             }
21044           else 
21045             {
21046               cp_id_kind idk;
21047               /* If parsing a type specifier sequence failed, then
21048                  this MUST be a simple expression.  */
21049               cp_parser_parse_tentatively (parser);
21050               decl = cp_parser_primary_expression (parser, false, false,
21051                                                    false, &idk);
21052               if (!cp_parser_error_occurred (parser)
21053                   && decl
21054                   && DECL_P (decl)
21055                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21056                 {
21057                   tree rhs;
21058
21059                   cp_parser_parse_definitely (parser);
21060                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21061                   rhs = cp_parser_assignment_expression (parser, false);
21062                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21063                                                          rhs,
21064                                                          tf_warning_or_error));
21065                   add_private_clause = true;
21066                 }
21067               else
21068                 {
21069                   decl = NULL;
21070                   cp_parser_abort_tentative_parse (parser);
21071                   init = cp_parser_expression (parser, false);
21072                   if (init)
21073                     {
21074                       if (TREE_CODE (init) == MODIFY_EXPR
21075                           || TREE_CODE (init) == MODOP_EXPR)
21076                         real_decl = TREE_OPERAND (init, 0);
21077                     }
21078                 }
21079             }
21080         }
21081       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21082       if (this_pre_body)
21083         {
21084           this_pre_body = pop_stmt_list (this_pre_body);
21085           if (pre_body)
21086             {
21087               tree t = pre_body;
21088               pre_body = push_stmt_list ();
21089               add_stmt (t);
21090               add_stmt (this_pre_body);
21091               pre_body = pop_stmt_list (pre_body);
21092             }
21093           else
21094             pre_body = this_pre_body;
21095         }
21096
21097       if (decl)
21098         real_decl = decl;
21099       if (par_clauses != NULL && real_decl != NULL_TREE)
21100         {
21101           tree *c;
21102           for (c = par_clauses; *c ; )
21103             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21104                 && OMP_CLAUSE_DECL (*c) == real_decl)
21105               {
21106                 error ("%Hiteration variable %qD should not be firstprivate",
21107                        &loc, real_decl);
21108                 *c = OMP_CLAUSE_CHAIN (*c);
21109               }
21110             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21111                      && OMP_CLAUSE_DECL (*c) == real_decl)
21112               {
21113                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21114                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21115                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21116                 OMP_CLAUSE_DECL (l) = real_decl;
21117                 OMP_CLAUSE_CHAIN (l) = clauses;
21118                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21119                 clauses = l;
21120                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21121                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21122                 add_private_clause = false;
21123               }
21124             else
21125               {
21126                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21127                     && OMP_CLAUSE_DECL (*c) == real_decl)
21128                   add_private_clause = false;
21129                 c = &OMP_CLAUSE_CHAIN (*c);
21130               }
21131         }
21132
21133       if (add_private_clause)
21134         {
21135           tree c;
21136           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21137             {
21138               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21139                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21140                   && OMP_CLAUSE_DECL (c) == decl)
21141                 break;
21142               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21143                        && OMP_CLAUSE_DECL (c) == decl)
21144                 error ("%Hiteration variable %qD should not be firstprivate",
21145                        &loc, decl);
21146               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21147                        && OMP_CLAUSE_DECL (c) == decl)
21148                 error ("%Hiteration variable %qD should not be reduction",
21149                        &loc, decl);
21150             }
21151           if (c == NULL)
21152             {
21153               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21154               OMP_CLAUSE_DECL (c) = decl;
21155               c = finish_omp_clauses (c);
21156               if (c)
21157                 {
21158                   OMP_CLAUSE_CHAIN (c) = clauses;
21159                   clauses = c;
21160                 }
21161             }
21162         }
21163
21164       cond = NULL;
21165       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21166         {
21167           /* If decl is an iterator, preserve LHS and RHS of the relational
21168              expr until finish_omp_for.  */
21169           if (decl
21170               && (type_dependent_expression_p (decl)
21171                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21172             cond = cp_parser_omp_for_cond (parser, decl);
21173           else
21174             cond = cp_parser_condition (parser);
21175         }
21176       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21177
21178       incr = NULL;
21179       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21180         {
21181           /* If decl is an iterator, preserve the operator on decl
21182              until finish_omp_for.  */
21183           if (decl
21184               && (type_dependent_expression_p (decl)
21185                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21186             incr = cp_parser_omp_for_incr (parser, decl);
21187           else
21188             incr = cp_parser_expression (parser, false);
21189         }
21190
21191       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21192         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21193                                                /*or_comma=*/false,
21194                                                /*consume_paren=*/true);
21195
21196       TREE_VEC_ELT (declv, i) = decl;
21197       TREE_VEC_ELT (initv, i) = init;
21198       TREE_VEC_ELT (condv, i) = cond;
21199       TREE_VEC_ELT (incrv, i) = incr;
21200
21201       if (i == collapse - 1)
21202         break;
21203
21204       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21205          in between the collapsed for loops to be still considered perfectly
21206          nested.  Hopefully the final version clarifies this.
21207          For now handle (multiple) {'s and empty statements.  */
21208       cp_parser_parse_tentatively (parser);
21209       do
21210         {
21211           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21212             break;
21213           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21214             {
21215               cp_lexer_consume_token (parser->lexer);
21216               bracecount++;
21217             }
21218           else if (bracecount
21219                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21220             cp_lexer_consume_token (parser->lexer);
21221           else
21222             {
21223               loc = cp_lexer_peek_token (parser->lexer)->location;
21224               error ("%Hnot enough collapsed for loops", &loc);
21225               collapse_err = true;
21226               cp_parser_abort_tentative_parse (parser);
21227               declv = NULL_TREE;
21228               break;
21229             }
21230         }
21231       while (1);
21232
21233       if (declv)
21234         {
21235           cp_parser_parse_definitely (parser);
21236           nbraces += bracecount;
21237         }
21238     }
21239
21240   /* Note that we saved the original contents of this flag when we entered
21241      the structured block, and so we don't need to re-save it here.  */
21242   parser->in_statement = IN_OMP_FOR;
21243
21244   /* Note that the grammar doesn't call for a structured block here,
21245      though the loop as a whole is a structured block.  */
21246   body = push_stmt_list ();
21247   cp_parser_statement (parser, NULL_TREE, false, NULL);
21248   body = pop_stmt_list (body);
21249
21250   if (declv == NULL_TREE)
21251     ret = NULL_TREE;
21252   else
21253     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21254                           pre_body, clauses);
21255
21256   while (nbraces)
21257     {
21258       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21259         {
21260           cp_lexer_consume_token (parser->lexer);
21261           nbraces--;
21262         }
21263       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21264         cp_lexer_consume_token (parser->lexer);
21265       else
21266         {
21267           if (!collapse_err)
21268             {
21269               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21270               error ("%Hcollapsed loops not perfectly nested", &loc);
21271             }
21272           collapse_err = true;
21273           cp_parser_statement_seq_opt (parser, NULL);
21274           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21275         }
21276     }
21277
21278   while (for_block)
21279     {
21280       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21281       for_block = TREE_CHAIN (for_block);
21282     }
21283
21284   return ret;
21285 }
21286
21287 /* OpenMP 2.5:
21288    #pragma omp for for-clause[optseq] new-line
21289      for-loop  */
21290
21291 #define OMP_FOR_CLAUSE_MASK                             \
21292         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21293         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21294         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21295         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21296         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21297         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21298         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21299         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21300
21301 static tree
21302 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21303 {
21304   tree clauses, sb, ret;
21305   unsigned int save;
21306
21307   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21308                                        "#pragma omp for", pragma_tok);
21309
21310   sb = begin_omp_structured_block ();
21311   save = cp_parser_begin_omp_structured_block (parser);
21312
21313   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21314
21315   cp_parser_end_omp_structured_block (parser, save);
21316   add_stmt (finish_omp_structured_block (sb));
21317
21318   return ret;
21319 }
21320
21321 /* OpenMP 2.5:
21322    # pragma omp master new-line
21323      structured-block  */
21324
21325 static tree
21326 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21327 {
21328   cp_parser_require_pragma_eol (parser, pragma_tok);
21329   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21330 }
21331
21332 /* OpenMP 2.5:
21333    # pragma omp ordered new-line
21334      structured-block  */
21335
21336 static tree
21337 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21338 {
21339   cp_parser_require_pragma_eol (parser, pragma_tok);
21340   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21341 }
21342
21343 /* OpenMP 2.5:
21344
21345    section-scope:
21346      { section-sequence }
21347
21348    section-sequence:
21349      section-directive[opt] structured-block
21350      section-sequence section-directive structured-block  */
21351
21352 static tree
21353 cp_parser_omp_sections_scope (cp_parser *parser)
21354 {
21355   tree stmt, substmt;
21356   bool error_suppress = false;
21357   cp_token *tok;
21358
21359   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21360     return NULL_TREE;
21361
21362   stmt = push_stmt_list ();
21363
21364   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21365     {
21366       unsigned save;
21367
21368       substmt = begin_omp_structured_block ();
21369       save = cp_parser_begin_omp_structured_block (parser);
21370
21371       while (1)
21372         {
21373           cp_parser_statement (parser, NULL_TREE, false, NULL);
21374
21375           tok = cp_lexer_peek_token (parser->lexer);
21376           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21377             break;
21378           if (tok->type == CPP_CLOSE_BRACE)
21379             break;
21380           if (tok->type == CPP_EOF)
21381             break;
21382         }
21383
21384       cp_parser_end_omp_structured_block (parser, save);
21385       substmt = finish_omp_structured_block (substmt);
21386       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21387       add_stmt (substmt);
21388     }
21389
21390   while (1)
21391     {
21392       tok = cp_lexer_peek_token (parser->lexer);
21393       if (tok->type == CPP_CLOSE_BRACE)
21394         break;
21395       if (tok->type == CPP_EOF)
21396         break;
21397
21398       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21399         {
21400           cp_lexer_consume_token (parser->lexer);
21401           cp_parser_require_pragma_eol (parser, tok);
21402           error_suppress = false;
21403         }
21404       else if (!error_suppress)
21405         {
21406           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21407           error_suppress = true;
21408         }
21409
21410       substmt = cp_parser_omp_structured_block (parser);
21411       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21412       add_stmt (substmt);
21413     }
21414   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21415
21416   substmt = pop_stmt_list (stmt);
21417
21418   stmt = make_node (OMP_SECTIONS);
21419   TREE_TYPE (stmt) = void_type_node;
21420   OMP_SECTIONS_BODY (stmt) = substmt;
21421
21422   add_stmt (stmt);
21423   return stmt;
21424 }
21425
21426 /* OpenMP 2.5:
21427    # pragma omp sections sections-clause[optseq] newline
21428      sections-scope  */
21429
21430 #define OMP_SECTIONS_CLAUSE_MASK                        \
21431         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21432         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21433         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21434         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21435         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21436
21437 static tree
21438 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21439 {
21440   tree clauses, ret;
21441
21442   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21443                                        "#pragma omp sections", pragma_tok);
21444
21445   ret = cp_parser_omp_sections_scope (parser);
21446   if (ret)
21447     OMP_SECTIONS_CLAUSES (ret) = clauses;
21448
21449   return ret;
21450 }
21451
21452 /* OpenMP 2.5:
21453    # pragma parallel parallel-clause new-line
21454    # pragma parallel for parallel-for-clause new-line
21455    # pragma parallel sections parallel-sections-clause new-line  */
21456
21457 #define OMP_PARALLEL_CLAUSE_MASK                        \
21458         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21459         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21460         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21461         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21462         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21463         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21464         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21465         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21466
21467 static tree
21468 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21469 {
21470   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21471   const char *p_name = "#pragma omp parallel";
21472   tree stmt, clauses, par_clause, ws_clause, block;
21473   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21474   unsigned int save;
21475
21476   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21477     {
21478       cp_lexer_consume_token (parser->lexer);
21479       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21480       p_name = "#pragma omp parallel for";
21481       mask |= OMP_FOR_CLAUSE_MASK;
21482       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21483     }
21484   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21485     {
21486       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21487       const char *p = IDENTIFIER_POINTER (id);
21488       if (strcmp (p, "sections") == 0)
21489         {
21490           cp_lexer_consume_token (parser->lexer);
21491           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21492           p_name = "#pragma omp parallel sections";
21493           mask |= OMP_SECTIONS_CLAUSE_MASK;
21494           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21495         }
21496     }
21497
21498   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21499   block = begin_omp_parallel ();
21500   save = cp_parser_begin_omp_structured_block (parser);
21501
21502   switch (p_kind)
21503     {
21504     case PRAGMA_OMP_PARALLEL:
21505       cp_parser_statement (parser, NULL_TREE, false, NULL);
21506       par_clause = clauses;
21507       break;
21508
21509     case PRAGMA_OMP_PARALLEL_FOR:
21510       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21511       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21512       break;
21513
21514     case PRAGMA_OMP_PARALLEL_SECTIONS:
21515       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21516       stmt = cp_parser_omp_sections_scope (parser);
21517       if (stmt)
21518         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21519       break;
21520
21521     default:
21522       gcc_unreachable ();
21523     }
21524
21525   cp_parser_end_omp_structured_block (parser, save);
21526   stmt = finish_omp_parallel (par_clause, block);
21527   if (p_kind != PRAGMA_OMP_PARALLEL)
21528     OMP_PARALLEL_COMBINED (stmt) = 1;
21529   return stmt;
21530 }
21531
21532 /* OpenMP 2.5:
21533    # pragma omp single single-clause[optseq] new-line
21534      structured-block  */
21535
21536 #define OMP_SINGLE_CLAUSE_MASK                          \
21537         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21538         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21539         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21540         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21541
21542 static tree
21543 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21544 {
21545   tree stmt = make_node (OMP_SINGLE);
21546   TREE_TYPE (stmt) = void_type_node;
21547
21548   OMP_SINGLE_CLAUSES (stmt)
21549     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21550                                  "#pragma omp single", pragma_tok);
21551   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21552
21553   return add_stmt (stmt);
21554 }
21555
21556 /* OpenMP 3.0:
21557    # pragma omp task task-clause[optseq] new-line
21558      structured-block  */
21559
21560 #define OMP_TASK_CLAUSE_MASK                            \
21561         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21562         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21563         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21564         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21565         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21566         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21567
21568 static tree
21569 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21570 {
21571   tree clauses, block;
21572   unsigned int save;
21573
21574   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21575                                        "#pragma omp task", pragma_tok);
21576   block = begin_omp_task ();
21577   save = cp_parser_begin_omp_structured_block (parser);
21578   cp_parser_statement (parser, NULL_TREE, false, NULL);
21579   cp_parser_end_omp_structured_block (parser, save);
21580   return finish_omp_task (clauses, block);
21581 }
21582
21583 /* OpenMP 3.0:
21584    # pragma omp taskwait new-line  */
21585
21586 static void
21587 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21588 {
21589   cp_parser_require_pragma_eol (parser, pragma_tok);
21590   finish_omp_taskwait ();
21591 }
21592
21593 /* OpenMP 2.5:
21594    # pragma omp threadprivate (variable-list) */
21595
21596 static void
21597 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21598 {
21599   tree vars;
21600
21601   vars = cp_parser_omp_var_list (parser, 0, NULL);
21602   cp_parser_require_pragma_eol (parser, pragma_tok);
21603
21604   finish_omp_threadprivate (vars);
21605 }
21606
21607 /* Main entry point to OpenMP statement pragmas.  */
21608
21609 static void
21610 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21611 {
21612   tree stmt;
21613
21614   switch (pragma_tok->pragma_kind)
21615     {
21616     case PRAGMA_OMP_ATOMIC:
21617       cp_parser_omp_atomic (parser, pragma_tok);
21618       return;
21619     case PRAGMA_OMP_CRITICAL:
21620       stmt = cp_parser_omp_critical (parser, pragma_tok);
21621       break;
21622     case PRAGMA_OMP_FOR:
21623       stmt = cp_parser_omp_for (parser, pragma_tok);
21624       break;
21625     case PRAGMA_OMP_MASTER:
21626       stmt = cp_parser_omp_master (parser, pragma_tok);
21627       break;
21628     case PRAGMA_OMP_ORDERED:
21629       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21630       break;
21631     case PRAGMA_OMP_PARALLEL:
21632       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21633       break;
21634     case PRAGMA_OMP_SECTIONS:
21635       stmt = cp_parser_omp_sections (parser, pragma_tok);
21636       break;
21637     case PRAGMA_OMP_SINGLE:
21638       stmt = cp_parser_omp_single (parser, pragma_tok);
21639       break;
21640     case PRAGMA_OMP_TASK:
21641       stmt = cp_parser_omp_task (parser, pragma_tok);
21642       break;
21643     default:
21644       gcc_unreachable ();
21645     }
21646
21647   if (stmt)
21648     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21649 }
21650 \f
21651 /* The parser.  */
21652
21653 static GTY (()) cp_parser *the_parser;
21654
21655 \f
21656 /* Special handling for the first token or line in the file.  The first
21657    thing in the file might be #pragma GCC pch_preprocess, which loads a
21658    PCH file, which is a GC collection point.  So we need to handle this
21659    first pragma without benefit of an existing lexer structure.
21660
21661    Always returns one token to the caller in *FIRST_TOKEN.  This is
21662    either the true first token of the file, or the first token after
21663    the initial pragma.  */
21664
21665 static void
21666 cp_parser_initial_pragma (cp_token *first_token)
21667 {
21668   tree name = NULL;
21669
21670   cp_lexer_get_preprocessor_token (NULL, first_token);
21671   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21672     return;
21673
21674   cp_lexer_get_preprocessor_token (NULL, first_token);
21675   if (first_token->type == CPP_STRING)
21676     {
21677       name = first_token->u.value;
21678
21679       cp_lexer_get_preprocessor_token (NULL, first_token);
21680       if (first_token->type != CPP_PRAGMA_EOL)
21681         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21682                &first_token->location);
21683     }
21684   else
21685     error ("%Hexpected string literal", &first_token->location);
21686
21687   /* Skip to the end of the pragma.  */
21688   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21689     cp_lexer_get_preprocessor_token (NULL, first_token);
21690
21691   /* Now actually load the PCH file.  */
21692   if (name)
21693     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21694
21695   /* Read one more token to return to our caller.  We have to do this
21696      after reading the PCH file in, since its pointers have to be
21697      live.  */
21698   cp_lexer_get_preprocessor_token (NULL, first_token);
21699 }
21700
21701 /* Normal parsing of a pragma token.  Here we can (and must) use the
21702    regular lexer.  */
21703
21704 static bool
21705 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21706 {
21707   cp_token *pragma_tok;
21708   unsigned int id;
21709
21710   pragma_tok = cp_lexer_consume_token (parser->lexer);
21711   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21712   parser->lexer->in_pragma = true;
21713
21714   id = pragma_tok->pragma_kind;
21715   switch (id)
21716     {
21717     case PRAGMA_GCC_PCH_PREPROCESS:
21718       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21719              &pragma_tok->location);
21720       break;
21721
21722     case PRAGMA_OMP_BARRIER:
21723       switch (context)
21724         {
21725         case pragma_compound:
21726           cp_parser_omp_barrier (parser, pragma_tok);
21727           return false;
21728         case pragma_stmt:
21729           error ("%H%<#pragma omp barrier%> may only be "
21730                  "used in compound statements", &pragma_tok->location);
21731           break;
21732         default:
21733           goto bad_stmt;
21734         }
21735       break;
21736
21737     case PRAGMA_OMP_FLUSH:
21738       switch (context)
21739         {
21740         case pragma_compound:
21741           cp_parser_omp_flush (parser, pragma_tok);
21742           return false;
21743         case pragma_stmt:
21744           error ("%H%<#pragma omp flush%> may only be "
21745                  "used in compound statements", &pragma_tok->location);
21746           break;
21747         default:
21748           goto bad_stmt;
21749         }
21750       break;
21751
21752     case PRAGMA_OMP_TASKWAIT:
21753       switch (context)
21754         {
21755         case pragma_compound:
21756           cp_parser_omp_taskwait (parser, pragma_tok);
21757           return false;
21758         case pragma_stmt:
21759           error ("%H%<#pragma omp taskwait%> may only be "
21760                  "used in compound statements",
21761                  &pragma_tok->location);
21762           break;
21763         default:
21764           goto bad_stmt;
21765         }
21766       break;
21767
21768     case PRAGMA_OMP_THREADPRIVATE:
21769       cp_parser_omp_threadprivate (parser, pragma_tok);
21770       return false;
21771
21772     case PRAGMA_OMP_ATOMIC:
21773     case PRAGMA_OMP_CRITICAL:
21774     case PRAGMA_OMP_FOR:
21775     case PRAGMA_OMP_MASTER:
21776     case PRAGMA_OMP_ORDERED:
21777     case PRAGMA_OMP_PARALLEL:
21778     case PRAGMA_OMP_SECTIONS:
21779     case PRAGMA_OMP_SINGLE:
21780     case PRAGMA_OMP_TASK:
21781       if (context == pragma_external)
21782         goto bad_stmt;
21783       cp_parser_omp_construct (parser, pragma_tok);
21784       return true;
21785
21786     case PRAGMA_OMP_SECTION:
21787       error ("%H%<#pragma omp section%> may only be used in "
21788              "%<#pragma omp sections%> construct", &pragma_tok->location);
21789       break;
21790
21791     default:
21792       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21793       c_invoke_pragma_handler (id);
21794       break;
21795
21796     bad_stmt:
21797       cp_parser_error (parser, "expected declaration specifiers");
21798       break;
21799     }
21800
21801   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21802   return false;
21803 }
21804
21805 /* The interface the pragma parsers have to the lexer.  */
21806
21807 enum cpp_ttype
21808 pragma_lex (tree *value)
21809 {
21810   cp_token *tok;
21811   enum cpp_ttype ret;
21812
21813   tok = cp_lexer_peek_token (the_parser->lexer);
21814
21815   ret = tok->type;
21816   *value = tok->u.value;
21817
21818   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21819     ret = CPP_EOF;
21820   else if (ret == CPP_STRING)
21821     *value = cp_parser_string_literal (the_parser, false, false);
21822   else
21823     {
21824       cp_lexer_consume_token (the_parser->lexer);
21825       if (ret == CPP_KEYWORD)
21826         ret = CPP_NAME;
21827     }
21828
21829   return ret;
21830 }
21831
21832 \f
21833 /* External interface.  */
21834
21835 /* Parse one entire translation unit.  */
21836
21837 void
21838 c_parse_file (void)
21839 {
21840   bool error_occurred;
21841   static bool already_called = false;
21842
21843   if (already_called)
21844     {
21845       sorry ("inter-module optimizations not implemented for C++");
21846       return;
21847     }
21848   already_called = true;
21849
21850   the_parser = cp_parser_new ();
21851   push_deferring_access_checks (flag_access_control
21852                                 ? dk_no_deferred : dk_no_check);
21853   error_occurred = cp_parser_translation_unit (the_parser);
21854   the_parser = NULL;
21855 }
21856
21857 #include "gt-cp-parser.h"