OSDN Git Service

2008-08-26 Douglas Gregor <doug.gregor@gmail.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, 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           /* Consume the token.  */
8328           cp_lexer_consume_token (parser->lexer);
8329
8330           if (cxx_dialect == cxx98) 
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             /* We do not yet support the use of `auto' as a
8345                type-specifier.  */
8346             error ("%HC++0x %<auto%> specifier not supported", &token->location);
8347           break;
8348
8349         case RID_REGISTER:
8350         case RID_STATIC:
8351         case RID_EXTERN:
8352         case RID_MUTABLE:
8353           /* Consume the token.  */
8354           cp_lexer_consume_token (parser->lexer);
8355           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8356                                        token->location);
8357           break;
8358         case RID_THREAD:
8359           /* Consume the token.  */
8360           cp_lexer_consume_token (parser->lexer);
8361           ++decl_specs->specs[(int) ds_thread];
8362           break;
8363
8364         default:
8365           /* We did not yet find a decl-specifier yet.  */
8366           found_decl_spec = false;
8367           break;
8368         }
8369
8370       /* Constructors are a special case.  The `S' in `S()' is not a
8371          decl-specifier; it is the beginning of the declarator.  */
8372       constructor_p
8373         = (!found_decl_spec
8374            && constructor_possible_p
8375            && (cp_parser_constructor_declarator_p
8376                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8377
8378       /* If we don't have a DECL_SPEC yet, then we must be looking at
8379          a type-specifier.  */
8380       if (!found_decl_spec && !constructor_p)
8381         {
8382           int decl_spec_declares_class_or_enum;
8383           bool is_cv_qualifier;
8384           tree type_spec;
8385
8386           type_spec
8387             = cp_parser_type_specifier (parser, flags,
8388                                         decl_specs,
8389                                         /*is_declaration=*/true,
8390                                         &decl_spec_declares_class_or_enum,
8391                                         &is_cv_qualifier);
8392           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8393
8394           /* If this type-specifier referenced a user-defined type
8395              (a typedef, class-name, etc.), then we can't allow any
8396              more such type-specifiers henceforth.
8397
8398              [dcl.spec]
8399
8400              The longest sequence of decl-specifiers that could
8401              possibly be a type name is taken as the
8402              decl-specifier-seq of a declaration.  The sequence shall
8403              be self-consistent as described below.
8404
8405              [dcl.type]
8406
8407              As a general rule, at most one type-specifier is allowed
8408              in the complete decl-specifier-seq of a declaration.  The
8409              only exceptions are the following:
8410
8411              -- const or volatile can be combined with any other
8412                 type-specifier.
8413
8414              -- signed or unsigned can be combined with char, long,
8415                 short, or int.
8416
8417              -- ..
8418
8419              Example:
8420
8421                typedef char* Pc;
8422                void g (const int Pc);
8423
8424              Here, Pc is *not* part of the decl-specifier seq; it's
8425              the declarator.  Therefore, once we see a type-specifier
8426              (other than a cv-qualifier), we forbid any additional
8427              user-defined types.  We *do* still allow things like `int
8428              int' to be considered a decl-specifier-seq, and issue the
8429              error message later.  */
8430           if (type_spec && !is_cv_qualifier)
8431             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8432           /* A constructor declarator cannot follow a type-specifier.  */
8433           if (type_spec)
8434             {
8435               constructor_possible_p = false;
8436               found_decl_spec = true;
8437             }
8438         }
8439
8440       /* If we still do not have a DECL_SPEC, then there are no more
8441          decl-specifiers.  */
8442       if (!found_decl_spec)
8443         break;
8444
8445       decl_specs->any_specifiers_p = true;
8446       /* After we see one decl-specifier, further decl-specifiers are
8447          always optional.  */
8448       flags |= CP_PARSER_FLAGS_OPTIONAL;
8449     }
8450
8451   cp_parser_check_decl_spec (decl_specs, start_token->location);
8452
8453   /* Don't allow a friend specifier with a class definition.  */
8454   if (decl_specs->specs[(int) ds_friend] != 0
8455       && (*declares_class_or_enum & 2))
8456     error ("%Hclass definition may not be declared a friend",
8457             &start_token->location);
8458 }
8459
8460 /* Parse an (optional) storage-class-specifier.
8461
8462    storage-class-specifier:
8463      auto
8464      register
8465      static
8466      extern
8467      mutable
8468
8469    GNU Extension:
8470
8471    storage-class-specifier:
8472      thread
8473
8474    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8475
8476 static tree
8477 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8478 {
8479   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8480     {
8481     case RID_AUTO:
8482       if (cxx_dialect != cxx98)
8483         return NULL_TREE;
8484       /* Fall through for C++98.  */
8485
8486     case RID_REGISTER:
8487     case RID_STATIC:
8488     case RID_EXTERN:
8489     case RID_MUTABLE:
8490     case RID_THREAD:
8491       /* Consume the token.  */
8492       return cp_lexer_consume_token (parser->lexer)->u.value;
8493
8494     default:
8495       return NULL_TREE;
8496     }
8497 }
8498
8499 /* Parse an (optional) function-specifier.
8500
8501    function-specifier:
8502      inline
8503      virtual
8504      explicit
8505
8506    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8507    Updates DECL_SPECS, if it is non-NULL.  */
8508
8509 static tree
8510 cp_parser_function_specifier_opt (cp_parser* parser,
8511                                   cp_decl_specifier_seq *decl_specs)
8512 {
8513   cp_token *token = cp_lexer_peek_token (parser->lexer);
8514   switch (token->keyword)
8515     {
8516     case RID_INLINE:
8517       if (decl_specs)
8518         ++decl_specs->specs[(int) ds_inline];
8519       break;
8520
8521     case RID_VIRTUAL:
8522       /* 14.5.2.3 [temp.mem]
8523
8524          A member function template shall not be virtual.  */
8525       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8526         error ("%Htemplates may not be %<virtual%>", &token->location);
8527       else if (decl_specs)
8528         ++decl_specs->specs[(int) ds_virtual];
8529       break;
8530
8531     case RID_EXPLICIT:
8532       if (decl_specs)
8533         ++decl_specs->specs[(int) ds_explicit];
8534       break;
8535
8536     default:
8537       return NULL_TREE;
8538     }
8539
8540   /* Consume the token.  */
8541   return cp_lexer_consume_token (parser->lexer)->u.value;
8542 }
8543
8544 /* Parse a linkage-specification.
8545
8546    linkage-specification:
8547      extern string-literal { declaration-seq [opt] }
8548      extern string-literal declaration  */
8549
8550 static void
8551 cp_parser_linkage_specification (cp_parser* parser)
8552 {
8553   tree linkage;
8554
8555   /* Look for the `extern' keyword.  */
8556   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8557
8558   /* Look for the string-literal.  */
8559   linkage = cp_parser_string_literal (parser, false, false);
8560
8561   /* Transform the literal into an identifier.  If the literal is a
8562      wide-character string, or contains embedded NULs, then we can't
8563      handle it as the user wants.  */
8564   if (strlen (TREE_STRING_POINTER (linkage))
8565       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8566     {
8567       cp_parser_error (parser, "invalid linkage-specification");
8568       /* Assume C++ linkage.  */
8569       linkage = lang_name_cplusplus;
8570     }
8571   else
8572     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8573
8574   /* We're now using the new linkage.  */
8575   push_lang_context (linkage);
8576
8577   /* If the next token is a `{', then we're using the first
8578      production.  */
8579   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8580     {
8581       /* Consume the `{' token.  */
8582       cp_lexer_consume_token (parser->lexer);
8583       /* Parse the declarations.  */
8584       cp_parser_declaration_seq_opt (parser);
8585       /* Look for the closing `}'.  */
8586       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8587     }
8588   /* Otherwise, there's just one declaration.  */
8589   else
8590     {
8591       bool saved_in_unbraced_linkage_specification_p;
8592
8593       saved_in_unbraced_linkage_specification_p
8594         = parser->in_unbraced_linkage_specification_p;
8595       parser->in_unbraced_linkage_specification_p = true;
8596       cp_parser_declaration (parser);
8597       parser->in_unbraced_linkage_specification_p
8598         = saved_in_unbraced_linkage_specification_p;
8599     }
8600
8601   /* We're done with the linkage-specification.  */
8602   pop_lang_context ();
8603 }
8604
8605 /* Parse a static_assert-declaration.
8606
8607    static_assert-declaration:
8608      static_assert ( constant-expression , string-literal ) ; 
8609
8610    If MEMBER_P, this static_assert is a class member.  */
8611
8612 static void 
8613 cp_parser_static_assert(cp_parser *parser, bool member_p)
8614 {
8615   tree condition;
8616   tree message;
8617   cp_token *token;
8618   location_t saved_loc;
8619
8620   /* Peek at the `static_assert' token so we can keep track of exactly
8621      where the static assertion started.  */
8622   token = cp_lexer_peek_token (parser->lexer);
8623   saved_loc = token->location;
8624
8625   /* Look for the `static_assert' keyword.  */
8626   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8627                                   "%<static_assert%>"))
8628     return;
8629
8630   /*  We know we are in a static assertion; commit to any tentative
8631       parse.  */
8632   if (cp_parser_parsing_tentatively (parser))
8633     cp_parser_commit_to_tentative_parse (parser);
8634
8635   /* Parse the `(' starting the static assertion condition.  */
8636   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8637
8638   /* Parse the constant-expression.  */
8639   condition = 
8640     cp_parser_constant_expression (parser,
8641                                    /*allow_non_constant_p=*/false,
8642                                    /*non_constant_p=*/NULL);
8643
8644   /* Parse the separating `,'.  */
8645   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8646
8647   /* Parse the string-literal message.  */
8648   message = cp_parser_string_literal (parser, 
8649                                       /*translate=*/false,
8650                                       /*wide_ok=*/true);
8651
8652   /* A `)' completes the static assertion.  */
8653   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8654     cp_parser_skip_to_closing_parenthesis (parser, 
8655                                            /*recovering=*/true, 
8656                                            /*or_comma=*/false,
8657                                            /*consume_paren=*/true);
8658
8659   /* A semicolon terminates the declaration.  */
8660   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8661
8662   /* Complete the static assertion, which may mean either processing 
8663      the static assert now or saving it for template instantiation.  */
8664   finish_static_assert (condition, message, saved_loc, member_p);
8665 }
8666
8667 /* Parse a `decltype' type. Returns the type. 
8668
8669    simple-type-specifier:
8670      decltype ( expression )  */
8671
8672 static tree
8673 cp_parser_decltype (cp_parser *parser)
8674 {
8675   tree expr;
8676   bool id_expression_or_member_access_p = false;
8677   const char *saved_message;
8678   bool saved_integral_constant_expression_p;
8679   bool saved_non_integral_constant_expression_p;
8680   cp_token *id_expr_start_token;
8681
8682   /* Look for the `decltype' token.  */
8683   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8684     return error_mark_node;
8685
8686   /* Types cannot be defined in a `decltype' expression.  Save away the
8687      old message.  */
8688   saved_message = parser->type_definition_forbidden_message;
8689
8690   /* And create the new one.  */
8691   parser->type_definition_forbidden_message
8692     = "types may not be defined in %<decltype%> expressions";
8693
8694   /* The restrictions on constant-expressions do not apply inside
8695      decltype expressions.  */
8696   saved_integral_constant_expression_p
8697     = parser->integral_constant_expression_p;
8698   saved_non_integral_constant_expression_p
8699     = parser->non_integral_constant_expression_p;
8700   parser->integral_constant_expression_p = false;
8701
8702   /* Do not actually evaluate the expression.  */
8703   ++skip_evaluation;
8704
8705   /* Parse the opening `('.  */
8706   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8707     return error_mark_node;
8708   
8709   /* First, try parsing an id-expression.  */
8710   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8711   cp_parser_parse_tentatively (parser);
8712   expr = cp_parser_id_expression (parser,
8713                                   /*template_keyword_p=*/false,
8714                                   /*check_dependency_p=*/true,
8715                                   /*template_p=*/NULL,
8716                                   /*declarator_p=*/false,
8717                                   /*optional_p=*/false);
8718
8719   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8720     {
8721       bool non_integral_constant_expression_p = false;
8722       tree id_expression = expr;
8723       cp_id_kind idk;
8724       const char *error_msg;
8725
8726       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8727         /* Lookup the name we got back from the id-expression.  */
8728         expr = cp_parser_lookup_name (parser, expr,
8729                                       none_type,
8730                                       /*is_template=*/false,
8731                                       /*is_namespace=*/false,
8732                                       /*check_dependency=*/true,
8733                                       /*ambiguous_decls=*/NULL,
8734                                       id_expr_start_token->location);
8735
8736       if (expr
8737           && expr != error_mark_node
8738           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8739           && TREE_CODE (expr) != TYPE_DECL
8740           && (TREE_CODE (expr) != BIT_NOT_EXPR
8741               || !TYPE_P (TREE_OPERAND (expr, 0)))
8742           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8743         {
8744           /* Complete lookup of the id-expression.  */
8745           expr = (finish_id_expression
8746                   (id_expression, expr, parser->scope, &idk,
8747                    /*integral_constant_expression_p=*/false,
8748                    /*allow_non_integral_constant_expression_p=*/true,
8749                    &non_integral_constant_expression_p,
8750                    /*template_p=*/false,
8751                    /*done=*/true,
8752                    /*address_p=*/false,
8753                    /*template_arg_p=*/false,
8754                    &error_msg,
8755                    id_expr_start_token->location));
8756
8757           if (expr == error_mark_node)
8758             /* We found an id-expression, but it was something that we
8759                should not have found. This is an error, not something
8760                we can recover from, so note that we found an
8761                id-expression and we'll recover as gracefully as
8762                possible.  */
8763             id_expression_or_member_access_p = true;
8764         }
8765
8766       if (expr 
8767           && expr != error_mark_node
8768           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8769         /* We have an id-expression.  */
8770         id_expression_or_member_access_p = true;
8771     }
8772
8773   if (!id_expression_or_member_access_p)
8774     {
8775       /* Abort the id-expression parse.  */
8776       cp_parser_abort_tentative_parse (parser);
8777
8778       /* Parsing tentatively, again.  */
8779       cp_parser_parse_tentatively (parser);
8780
8781       /* Parse a class member access.  */
8782       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8783                                            /*cast_p=*/false,
8784                                            /*member_access_only_p=*/true);
8785
8786       if (expr 
8787           && expr != error_mark_node
8788           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8789         /* We have an id-expression.  */
8790         id_expression_or_member_access_p = true;
8791     }
8792
8793   if (id_expression_or_member_access_p)
8794     /* We have parsed the complete id-expression or member access.  */
8795     cp_parser_parse_definitely (parser);
8796   else
8797     {
8798       /* Abort our attempt to parse an id-expression or member access
8799          expression.  */
8800       cp_parser_abort_tentative_parse (parser);
8801
8802       /* Parse a full expression.  */
8803       expr = cp_parser_expression (parser, /*cast_p=*/false);
8804     }
8805
8806   /* Go back to evaluating expressions.  */
8807   --skip_evaluation;
8808
8809   /* Restore the old message and the integral constant expression
8810      flags.  */
8811   parser->type_definition_forbidden_message = saved_message;
8812   parser->integral_constant_expression_p
8813     = saved_integral_constant_expression_p;
8814   parser->non_integral_constant_expression_p
8815     = saved_non_integral_constant_expression_p;
8816
8817   if (expr == error_mark_node)
8818     {
8819       /* Skip everything up to the closing `)'.  */
8820       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8821                                              /*consume_paren=*/true);
8822       return error_mark_node;
8823     }
8824   
8825   /* Parse to the closing `)'.  */
8826   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8827     {
8828       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8829                                              /*consume_paren=*/true);
8830       return error_mark_node;
8831     }
8832
8833   return finish_decltype_type (expr, id_expression_or_member_access_p);
8834 }
8835
8836 /* Special member functions [gram.special] */
8837
8838 /* Parse a conversion-function-id.
8839
8840    conversion-function-id:
8841      operator conversion-type-id
8842
8843    Returns an IDENTIFIER_NODE representing the operator.  */
8844
8845 static tree
8846 cp_parser_conversion_function_id (cp_parser* parser)
8847 {
8848   tree type;
8849   tree saved_scope;
8850   tree saved_qualifying_scope;
8851   tree saved_object_scope;
8852   tree pushed_scope = NULL_TREE;
8853
8854   /* Look for the `operator' token.  */
8855   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8856     return error_mark_node;
8857   /* When we parse the conversion-type-id, the current scope will be
8858      reset.  However, we need that information in able to look up the
8859      conversion function later, so we save it here.  */
8860   saved_scope = parser->scope;
8861   saved_qualifying_scope = parser->qualifying_scope;
8862   saved_object_scope = parser->object_scope;
8863   /* We must enter the scope of the class so that the names of
8864      entities declared within the class are available in the
8865      conversion-type-id.  For example, consider:
8866
8867        struct S {
8868          typedef int I;
8869          operator I();
8870        };
8871
8872        S::operator I() { ... }
8873
8874      In order to see that `I' is a type-name in the definition, we
8875      must be in the scope of `S'.  */
8876   if (saved_scope)
8877     pushed_scope = push_scope (saved_scope);
8878   /* Parse the conversion-type-id.  */
8879   type = cp_parser_conversion_type_id (parser);
8880   /* Leave the scope of the class, if any.  */
8881   if (pushed_scope)
8882     pop_scope (pushed_scope);
8883   /* Restore the saved scope.  */
8884   parser->scope = saved_scope;
8885   parser->qualifying_scope = saved_qualifying_scope;
8886   parser->object_scope = saved_object_scope;
8887   /* If the TYPE is invalid, indicate failure.  */
8888   if (type == error_mark_node)
8889     return error_mark_node;
8890   return mangle_conv_op_name_for_type (type);
8891 }
8892
8893 /* Parse a conversion-type-id:
8894
8895    conversion-type-id:
8896      type-specifier-seq conversion-declarator [opt]
8897
8898    Returns the TYPE specified.  */
8899
8900 static tree
8901 cp_parser_conversion_type_id (cp_parser* parser)
8902 {
8903   tree attributes;
8904   cp_decl_specifier_seq type_specifiers;
8905   cp_declarator *declarator;
8906   tree type_specified;
8907
8908   /* Parse the attributes.  */
8909   attributes = cp_parser_attributes_opt (parser);
8910   /* Parse the type-specifiers.  */
8911   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8912                                 &type_specifiers);
8913   /* If that didn't work, stop.  */
8914   if (type_specifiers.type == error_mark_node)
8915     return error_mark_node;
8916   /* Parse the conversion-declarator.  */
8917   declarator = cp_parser_conversion_declarator_opt (parser);
8918
8919   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8920                                     /*initialized=*/0, &attributes);
8921   if (attributes)
8922     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8923   return type_specified;
8924 }
8925
8926 /* Parse an (optional) conversion-declarator.
8927
8928    conversion-declarator:
8929      ptr-operator conversion-declarator [opt]
8930
8931    */
8932
8933 static cp_declarator *
8934 cp_parser_conversion_declarator_opt (cp_parser* parser)
8935 {
8936   enum tree_code code;
8937   tree class_type;
8938   cp_cv_quals cv_quals;
8939
8940   /* We don't know if there's a ptr-operator next, or not.  */
8941   cp_parser_parse_tentatively (parser);
8942   /* Try the ptr-operator.  */
8943   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8944   /* If it worked, look for more conversion-declarators.  */
8945   if (cp_parser_parse_definitely (parser))
8946     {
8947       cp_declarator *declarator;
8948
8949       /* Parse another optional declarator.  */
8950       declarator = cp_parser_conversion_declarator_opt (parser);
8951
8952       return cp_parser_make_indirect_declarator
8953         (code, class_type, cv_quals, declarator);
8954    }
8955
8956   return NULL;
8957 }
8958
8959 /* Parse an (optional) ctor-initializer.
8960
8961    ctor-initializer:
8962      : mem-initializer-list
8963
8964    Returns TRUE iff the ctor-initializer was actually present.  */
8965
8966 static bool
8967 cp_parser_ctor_initializer_opt (cp_parser* parser)
8968 {
8969   /* If the next token is not a `:', then there is no
8970      ctor-initializer.  */
8971   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8972     {
8973       /* Do default initialization of any bases and members.  */
8974       if (DECL_CONSTRUCTOR_P (current_function_decl))
8975         finish_mem_initializers (NULL_TREE);
8976
8977       return false;
8978     }
8979
8980   /* Consume the `:' token.  */
8981   cp_lexer_consume_token (parser->lexer);
8982   /* And the mem-initializer-list.  */
8983   cp_parser_mem_initializer_list (parser);
8984
8985   return true;
8986 }
8987
8988 /* Parse a mem-initializer-list.
8989
8990    mem-initializer-list:
8991      mem-initializer ... [opt]
8992      mem-initializer ... [opt] , mem-initializer-list  */
8993
8994 static void
8995 cp_parser_mem_initializer_list (cp_parser* parser)
8996 {
8997   tree mem_initializer_list = NULL_TREE;
8998   cp_token *token = cp_lexer_peek_token (parser->lexer);
8999
9000   /* Let the semantic analysis code know that we are starting the
9001      mem-initializer-list.  */
9002   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9003     error ("%Honly constructors take base initializers",
9004            &token->location);
9005
9006   /* Loop through the list.  */
9007   while (true)
9008     {
9009       tree mem_initializer;
9010
9011       token = cp_lexer_peek_token (parser->lexer);
9012       /* Parse the mem-initializer.  */
9013       mem_initializer = cp_parser_mem_initializer (parser);
9014       /* If the next token is a `...', we're expanding member initializers. */
9015       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9016         {
9017           /* Consume the `...'. */
9018           cp_lexer_consume_token (parser->lexer);
9019
9020           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9021              can be expanded but members cannot. */
9022           if (mem_initializer != error_mark_node
9023               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9024             {
9025               error ("%Hcannot expand initializer for member %<%D%>",
9026                      &token->location, TREE_PURPOSE (mem_initializer));
9027               mem_initializer = error_mark_node;
9028             }
9029
9030           /* Construct the pack expansion type. */
9031           if (mem_initializer != error_mark_node)
9032             mem_initializer = make_pack_expansion (mem_initializer);
9033         }
9034       /* Add it to the list, unless it was erroneous.  */
9035       if (mem_initializer != error_mark_node)
9036         {
9037           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9038           mem_initializer_list = mem_initializer;
9039         }
9040       /* If the next token is not a `,', we're done.  */
9041       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9042         break;
9043       /* Consume the `,' token.  */
9044       cp_lexer_consume_token (parser->lexer);
9045     }
9046
9047   /* Perform semantic analysis.  */
9048   if (DECL_CONSTRUCTOR_P (current_function_decl))
9049     finish_mem_initializers (mem_initializer_list);
9050 }
9051
9052 /* Parse a mem-initializer.
9053
9054    mem-initializer:
9055      mem-initializer-id ( expression-list [opt] )
9056      mem-initializer-id braced-init-list
9057
9058    GNU extension:
9059
9060    mem-initializer:
9061      ( expression-list [opt] )
9062
9063    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9064    class) or FIELD_DECL (for a non-static data member) to initialize;
9065    the TREE_VALUE is the expression-list.  An empty initialization
9066    list is represented by void_list_node.  */
9067
9068 static tree
9069 cp_parser_mem_initializer (cp_parser* parser)
9070 {
9071   tree mem_initializer_id;
9072   tree expression_list;
9073   tree member;
9074   cp_token *token = cp_lexer_peek_token (parser->lexer);
9075
9076   /* Find out what is being initialized.  */
9077   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9078     {
9079       permerror (token->location,
9080                  "anachronistic old-style base class initializer");
9081       mem_initializer_id = NULL_TREE;
9082     }
9083   else
9084     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9085   member = expand_member_init (mem_initializer_id);
9086   if (member && !DECL_P (member))
9087     in_base_initializer = 1;
9088
9089   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9090     {
9091       bool expr_non_constant_p;
9092       maybe_warn_cpp0x ("extended initializer lists");
9093       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9094       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9095       expression_list = build_tree_list (NULL_TREE, expression_list);
9096     }
9097   else
9098     expression_list
9099       = cp_parser_parenthesized_expression_list (parser, false,
9100                                                  /*cast_p=*/false,
9101                                                  /*allow_expansion_p=*/true,
9102                                                  /*non_constant_p=*/NULL);
9103   if (expression_list == error_mark_node)
9104     return error_mark_node;
9105   if (!expression_list)
9106     expression_list = void_type_node;
9107
9108   in_base_initializer = 0;
9109
9110   return member ? build_tree_list (member, expression_list) : error_mark_node;
9111 }
9112
9113 /* Parse a mem-initializer-id.
9114
9115    mem-initializer-id:
9116      :: [opt] nested-name-specifier [opt] class-name
9117      identifier
9118
9119    Returns a TYPE indicating the class to be initializer for the first
9120    production.  Returns an IDENTIFIER_NODE indicating the data member
9121    to be initialized for the second production.  */
9122
9123 static tree
9124 cp_parser_mem_initializer_id (cp_parser* parser)
9125 {
9126   bool global_scope_p;
9127   bool nested_name_specifier_p;
9128   bool template_p = false;
9129   tree id;
9130
9131   cp_token *token = cp_lexer_peek_token (parser->lexer);
9132
9133   /* `typename' is not allowed in this context ([temp.res]).  */
9134   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9135     {
9136       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9137              "member initializer is implicitly a type)",
9138              &token->location);
9139       cp_lexer_consume_token (parser->lexer);
9140     }
9141   /* Look for the optional `::' operator.  */
9142   global_scope_p
9143     = (cp_parser_global_scope_opt (parser,
9144                                    /*current_scope_valid_p=*/false)
9145        != NULL_TREE);
9146   /* Look for the optional nested-name-specifier.  The simplest way to
9147      implement:
9148
9149        [temp.res]
9150
9151        The keyword `typename' is not permitted in a base-specifier or
9152        mem-initializer; in these contexts a qualified name that
9153        depends on a template-parameter is implicitly assumed to be a
9154        type name.
9155
9156      is to assume that we have seen the `typename' keyword at this
9157      point.  */
9158   nested_name_specifier_p
9159     = (cp_parser_nested_name_specifier_opt (parser,
9160                                             /*typename_keyword_p=*/true,
9161                                             /*check_dependency_p=*/true,
9162                                             /*type_p=*/true,
9163                                             /*is_declaration=*/true)
9164        != NULL_TREE);
9165   if (nested_name_specifier_p)
9166     template_p = cp_parser_optional_template_keyword (parser);
9167   /* If there is a `::' operator or a nested-name-specifier, then we
9168      are definitely looking for a class-name.  */
9169   if (global_scope_p || nested_name_specifier_p)
9170     return cp_parser_class_name (parser,
9171                                  /*typename_keyword_p=*/true,
9172                                  /*template_keyword_p=*/template_p,
9173                                  none_type,
9174                                  /*check_dependency_p=*/true,
9175                                  /*class_head_p=*/false,
9176                                  /*is_declaration=*/true);
9177   /* Otherwise, we could also be looking for an ordinary identifier.  */
9178   cp_parser_parse_tentatively (parser);
9179   /* Try a class-name.  */
9180   id = cp_parser_class_name (parser,
9181                              /*typename_keyword_p=*/true,
9182                              /*template_keyword_p=*/false,
9183                              none_type,
9184                              /*check_dependency_p=*/true,
9185                              /*class_head_p=*/false,
9186                              /*is_declaration=*/true);
9187   /* If we found one, we're done.  */
9188   if (cp_parser_parse_definitely (parser))
9189     return id;
9190   /* Otherwise, look for an ordinary identifier.  */
9191   return cp_parser_identifier (parser);
9192 }
9193
9194 /* Overloading [gram.over] */
9195
9196 /* Parse an operator-function-id.
9197
9198    operator-function-id:
9199      operator operator
9200
9201    Returns an IDENTIFIER_NODE for the operator which is a
9202    human-readable spelling of the identifier, e.g., `operator +'.  */
9203
9204 static tree
9205 cp_parser_operator_function_id (cp_parser* parser)
9206 {
9207   /* Look for the `operator' keyword.  */
9208   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9209     return error_mark_node;
9210   /* And then the name of the operator itself.  */
9211   return cp_parser_operator (parser);
9212 }
9213
9214 /* Parse an operator.
9215
9216    operator:
9217      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9218      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9219      || ++ -- , ->* -> () []
9220
9221    GNU Extensions:
9222
9223    operator:
9224      <? >? <?= >?=
9225
9226    Returns an IDENTIFIER_NODE for the operator which is a
9227    human-readable spelling of the identifier, e.g., `operator +'.  */
9228
9229 static tree
9230 cp_parser_operator (cp_parser* parser)
9231 {
9232   tree id = NULL_TREE;
9233   cp_token *token;
9234
9235   /* Peek at the next token.  */
9236   token = cp_lexer_peek_token (parser->lexer);
9237   /* Figure out which operator we have.  */
9238   switch (token->type)
9239     {
9240     case CPP_KEYWORD:
9241       {
9242         enum tree_code op;
9243
9244         /* The keyword should be either `new' or `delete'.  */
9245         if (token->keyword == RID_NEW)
9246           op = NEW_EXPR;
9247         else if (token->keyword == RID_DELETE)
9248           op = DELETE_EXPR;
9249         else
9250           break;
9251
9252         /* Consume the `new' or `delete' token.  */
9253         cp_lexer_consume_token (parser->lexer);
9254
9255         /* Peek at the next token.  */
9256         token = cp_lexer_peek_token (parser->lexer);
9257         /* If it's a `[' token then this is the array variant of the
9258            operator.  */
9259         if (token->type == CPP_OPEN_SQUARE)
9260           {
9261             /* Consume the `[' token.  */
9262             cp_lexer_consume_token (parser->lexer);
9263             /* Look for the `]' token.  */
9264             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9265             id = ansi_opname (op == NEW_EXPR
9266                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9267           }
9268         /* Otherwise, we have the non-array variant.  */
9269         else
9270           id = ansi_opname (op);
9271
9272         return id;
9273       }
9274
9275     case CPP_PLUS:
9276       id = ansi_opname (PLUS_EXPR);
9277       break;
9278
9279     case CPP_MINUS:
9280       id = ansi_opname (MINUS_EXPR);
9281       break;
9282
9283     case CPP_MULT:
9284       id = ansi_opname (MULT_EXPR);
9285       break;
9286
9287     case CPP_DIV:
9288       id = ansi_opname (TRUNC_DIV_EXPR);
9289       break;
9290
9291     case CPP_MOD:
9292       id = ansi_opname (TRUNC_MOD_EXPR);
9293       break;
9294
9295     case CPP_XOR:
9296       id = ansi_opname (BIT_XOR_EXPR);
9297       break;
9298
9299     case CPP_AND:
9300       id = ansi_opname (BIT_AND_EXPR);
9301       break;
9302
9303     case CPP_OR:
9304       id = ansi_opname (BIT_IOR_EXPR);
9305       break;
9306
9307     case CPP_COMPL:
9308       id = ansi_opname (BIT_NOT_EXPR);
9309       break;
9310
9311     case CPP_NOT:
9312       id = ansi_opname (TRUTH_NOT_EXPR);
9313       break;
9314
9315     case CPP_EQ:
9316       id = ansi_assopname (NOP_EXPR);
9317       break;
9318
9319     case CPP_LESS:
9320       id = ansi_opname (LT_EXPR);
9321       break;
9322
9323     case CPP_GREATER:
9324       id = ansi_opname (GT_EXPR);
9325       break;
9326
9327     case CPP_PLUS_EQ:
9328       id = ansi_assopname (PLUS_EXPR);
9329       break;
9330
9331     case CPP_MINUS_EQ:
9332       id = ansi_assopname (MINUS_EXPR);
9333       break;
9334
9335     case CPP_MULT_EQ:
9336       id = ansi_assopname (MULT_EXPR);
9337       break;
9338
9339     case CPP_DIV_EQ:
9340       id = ansi_assopname (TRUNC_DIV_EXPR);
9341       break;
9342
9343     case CPP_MOD_EQ:
9344       id = ansi_assopname (TRUNC_MOD_EXPR);
9345       break;
9346
9347     case CPP_XOR_EQ:
9348       id = ansi_assopname (BIT_XOR_EXPR);
9349       break;
9350
9351     case CPP_AND_EQ:
9352       id = ansi_assopname (BIT_AND_EXPR);
9353       break;
9354
9355     case CPP_OR_EQ:
9356       id = ansi_assopname (BIT_IOR_EXPR);
9357       break;
9358
9359     case CPP_LSHIFT:
9360       id = ansi_opname (LSHIFT_EXPR);
9361       break;
9362
9363     case CPP_RSHIFT:
9364       id = ansi_opname (RSHIFT_EXPR);
9365       break;
9366
9367     case CPP_LSHIFT_EQ:
9368       id = ansi_assopname (LSHIFT_EXPR);
9369       break;
9370
9371     case CPP_RSHIFT_EQ:
9372       id = ansi_assopname (RSHIFT_EXPR);
9373       break;
9374
9375     case CPP_EQ_EQ:
9376       id = ansi_opname (EQ_EXPR);
9377       break;
9378
9379     case CPP_NOT_EQ:
9380       id = ansi_opname (NE_EXPR);
9381       break;
9382
9383     case CPP_LESS_EQ:
9384       id = ansi_opname (LE_EXPR);
9385       break;
9386
9387     case CPP_GREATER_EQ:
9388       id = ansi_opname (GE_EXPR);
9389       break;
9390
9391     case CPP_AND_AND:
9392       id = ansi_opname (TRUTH_ANDIF_EXPR);
9393       break;
9394
9395     case CPP_OR_OR:
9396       id = ansi_opname (TRUTH_ORIF_EXPR);
9397       break;
9398
9399     case CPP_PLUS_PLUS:
9400       id = ansi_opname (POSTINCREMENT_EXPR);
9401       break;
9402
9403     case CPP_MINUS_MINUS:
9404       id = ansi_opname (PREDECREMENT_EXPR);
9405       break;
9406
9407     case CPP_COMMA:
9408       id = ansi_opname (COMPOUND_EXPR);
9409       break;
9410
9411     case CPP_DEREF_STAR:
9412       id = ansi_opname (MEMBER_REF);
9413       break;
9414
9415     case CPP_DEREF:
9416       id = ansi_opname (COMPONENT_REF);
9417       break;
9418
9419     case CPP_OPEN_PAREN:
9420       /* Consume the `('.  */
9421       cp_lexer_consume_token (parser->lexer);
9422       /* Look for the matching `)'.  */
9423       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9424       return ansi_opname (CALL_EXPR);
9425
9426     case CPP_OPEN_SQUARE:
9427       /* Consume the `['.  */
9428       cp_lexer_consume_token (parser->lexer);
9429       /* Look for the matching `]'.  */
9430       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9431       return ansi_opname (ARRAY_REF);
9432
9433     default:
9434       /* Anything else is an error.  */
9435       break;
9436     }
9437
9438   /* If we have selected an identifier, we need to consume the
9439      operator token.  */
9440   if (id)
9441     cp_lexer_consume_token (parser->lexer);
9442   /* Otherwise, no valid operator name was present.  */
9443   else
9444     {
9445       cp_parser_error (parser, "expected operator");
9446       id = error_mark_node;
9447     }
9448
9449   return id;
9450 }
9451
9452 /* Parse a template-declaration.
9453
9454    template-declaration:
9455      export [opt] template < template-parameter-list > declaration
9456
9457    If MEMBER_P is TRUE, this template-declaration occurs within a
9458    class-specifier.
9459
9460    The grammar rule given by the standard isn't correct.  What
9461    is really meant is:
9462
9463    template-declaration:
9464      export [opt] template-parameter-list-seq
9465        decl-specifier-seq [opt] init-declarator [opt] ;
9466      export [opt] template-parameter-list-seq
9467        function-definition
9468
9469    template-parameter-list-seq:
9470      template-parameter-list-seq [opt]
9471      template < template-parameter-list >  */
9472
9473 static void
9474 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9475 {
9476   /* Check for `export'.  */
9477   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9478     {
9479       /* Consume the `export' token.  */
9480       cp_lexer_consume_token (parser->lexer);
9481       /* Warn that we do not support `export'.  */
9482       warning (0, "keyword %<export%> not implemented, and will be ignored");
9483     }
9484
9485   cp_parser_template_declaration_after_export (parser, member_p);
9486 }
9487
9488 /* Parse a template-parameter-list.
9489
9490    template-parameter-list:
9491      template-parameter
9492      template-parameter-list , template-parameter
9493
9494    Returns a TREE_LIST.  Each node represents a template parameter.
9495    The nodes are connected via their TREE_CHAINs.  */
9496
9497 static tree
9498 cp_parser_template_parameter_list (cp_parser* parser)
9499 {
9500   tree parameter_list = NULL_TREE;
9501
9502   begin_template_parm_list ();
9503   while (true)
9504     {
9505       tree parameter;
9506       bool is_non_type;
9507       bool is_parameter_pack;
9508
9509       /* Parse the template-parameter.  */
9510       parameter = cp_parser_template_parameter (parser, 
9511                                                 &is_non_type,
9512                                                 &is_parameter_pack);
9513       /* Add it to the list.  */
9514       if (parameter != error_mark_node)
9515         parameter_list = process_template_parm (parameter_list,
9516                                                 parameter,
9517                                                 is_non_type,
9518                                                 is_parameter_pack);
9519       else
9520        {
9521          tree err_parm = build_tree_list (parameter, parameter);
9522          TREE_VALUE (err_parm) = error_mark_node;
9523          parameter_list = chainon (parameter_list, err_parm);
9524        }
9525
9526       /* If the next token is not a `,', we're done.  */
9527       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9528         break;
9529       /* Otherwise, consume the `,' token.  */
9530       cp_lexer_consume_token (parser->lexer);
9531     }
9532
9533   return end_template_parm_list (parameter_list);
9534 }
9535
9536 /* Parse a template-parameter.
9537
9538    template-parameter:
9539      type-parameter
9540      parameter-declaration
9541
9542    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9543    the parameter.  The TREE_PURPOSE is the default value, if any.
9544    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9545    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9546    set to true iff this parameter is a parameter pack. */
9547
9548 static tree
9549 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9550                               bool *is_parameter_pack)
9551 {
9552   cp_token *token;
9553   cp_parameter_declarator *parameter_declarator;
9554   cp_declarator *id_declarator;
9555   tree parm;
9556
9557   /* Assume it is a type parameter or a template parameter.  */
9558   *is_non_type = false;
9559   /* Assume it not a parameter pack. */
9560   *is_parameter_pack = false;
9561   /* Peek at the next token.  */
9562   token = cp_lexer_peek_token (parser->lexer);
9563   /* If it is `class' or `template', we have a type-parameter.  */
9564   if (token->keyword == RID_TEMPLATE)
9565     return cp_parser_type_parameter (parser, is_parameter_pack);
9566   /* If it is `class' or `typename' we do not know yet whether it is a
9567      type parameter or a non-type parameter.  Consider:
9568
9569        template <typename T, typename T::X X> ...
9570
9571      or:
9572
9573        template <class C, class D*> ...
9574
9575      Here, the first parameter is a type parameter, and the second is
9576      a non-type parameter.  We can tell by looking at the token after
9577      the identifier -- if it is a `,', `=', or `>' then we have a type
9578      parameter.  */
9579   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9580     {
9581       /* Peek at the token after `class' or `typename'.  */
9582       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9583       /* If it's an ellipsis, we have a template type parameter
9584          pack. */
9585       if (token->type == CPP_ELLIPSIS)
9586         return cp_parser_type_parameter (parser, is_parameter_pack);
9587       /* If it's an identifier, skip it.  */
9588       if (token->type == CPP_NAME)
9589         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9590       /* Now, see if the token looks like the end of a template
9591          parameter.  */
9592       if (token->type == CPP_COMMA
9593           || token->type == CPP_EQ
9594           || token->type == CPP_GREATER)
9595         return cp_parser_type_parameter (parser, is_parameter_pack);
9596     }
9597
9598   /* Otherwise, it is a non-type parameter.
9599
9600      [temp.param]
9601
9602      When parsing a default template-argument for a non-type
9603      template-parameter, the first non-nested `>' is taken as the end
9604      of the template parameter-list rather than a greater-than
9605      operator.  */
9606   *is_non_type = true;
9607   parameter_declarator
9608      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9609                                         /*parenthesized_p=*/NULL);
9610
9611   /* If the parameter declaration is marked as a parameter pack, set
9612      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9613      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9614      grokdeclarator. */
9615   if (parameter_declarator
9616       && parameter_declarator->declarator
9617       && parameter_declarator->declarator->parameter_pack_p)
9618     {
9619       *is_parameter_pack = true;
9620       parameter_declarator->declarator->parameter_pack_p = false;
9621     }
9622
9623   /* If the next token is an ellipsis, and we don't already have it
9624      marked as a parameter pack, then we have a parameter pack (that
9625      has no declarator).  */
9626   if (!*is_parameter_pack
9627       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9628       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9629     {
9630       /* Consume the `...'.  */
9631       cp_lexer_consume_token (parser->lexer);
9632       maybe_warn_variadic_templates ();
9633       
9634       *is_parameter_pack = true;
9635     }
9636   /* We might end up with a pack expansion as the type of the non-type
9637      template parameter, in which case this is a non-type template
9638      parameter pack.  */
9639   else if (parameter_declarator
9640            && parameter_declarator->decl_specifiers.type
9641            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9642     {
9643       *is_parameter_pack = true;
9644       parameter_declarator->decl_specifiers.type = 
9645         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9646     }
9647
9648   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9649     {
9650       /* Parameter packs cannot have default arguments.  However, a
9651          user may try to do so, so we'll parse them and give an
9652          appropriate diagnostic here.  */
9653
9654       /* Consume the `='.  */
9655       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9656       cp_lexer_consume_token (parser->lexer);
9657       
9658       /* Find the name of the parameter pack.  */     
9659       id_declarator = parameter_declarator->declarator;
9660       while (id_declarator && id_declarator->kind != cdk_id)
9661         id_declarator = id_declarator->declarator;
9662       
9663       if (id_declarator && id_declarator->kind == cdk_id)
9664         error ("%Htemplate parameter pack %qD cannot have a default argument",
9665                &start_token->location, id_declarator->u.id.unqualified_name);
9666       else
9667         error ("%Htemplate parameter pack cannot have a default argument",
9668                &start_token->location);
9669       
9670       /* Parse the default argument, but throw away the result.  */
9671       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9672     }
9673
9674   parm = grokdeclarator (parameter_declarator->declarator,
9675                          &parameter_declarator->decl_specifiers,
9676                          PARM, /*initialized=*/0,
9677                          /*attrlist=*/NULL);
9678   if (parm == error_mark_node)
9679     return error_mark_node;
9680
9681   return build_tree_list (parameter_declarator->default_argument, parm);
9682 }
9683
9684 /* Parse a type-parameter.
9685
9686    type-parameter:
9687      class identifier [opt]
9688      class identifier [opt] = type-id
9689      typename identifier [opt]
9690      typename identifier [opt] = type-id
9691      template < template-parameter-list > class identifier [opt]
9692      template < template-parameter-list > class identifier [opt]
9693        = id-expression
9694
9695    GNU Extension (variadic templates):
9696
9697    type-parameter:
9698      class ... identifier [opt]
9699      typename ... identifier [opt]
9700
9701    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9702    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9703    the declaration of the parameter.
9704
9705    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9706
9707 static tree
9708 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9709 {
9710   cp_token *token;
9711   tree parameter;
9712
9713   /* Look for a keyword to tell us what kind of parameter this is.  */
9714   token = cp_parser_require (parser, CPP_KEYWORD,
9715                              "%<class%>, %<typename%>, or %<template%>");
9716   if (!token)
9717     return error_mark_node;
9718
9719   switch (token->keyword)
9720     {
9721     case RID_CLASS:
9722     case RID_TYPENAME:
9723       {
9724         tree identifier;
9725         tree default_argument;
9726
9727         /* If the next token is an ellipsis, we have a template
9728            argument pack. */
9729         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9730           {
9731             /* Consume the `...' token. */
9732             cp_lexer_consume_token (parser->lexer);
9733             maybe_warn_variadic_templates ();
9734
9735             *is_parameter_pack = true;
9736           }
9737
9738         /* If the next token is an identifier, then it names the
9739            parameter.  */
9740         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9741           identifier = cp_parser_identifier (parser);
9742         else
9743           identifier = NULL_TREE;
9744
9745         /* Create the parameter.  */
9746         parameter = finish_template_type_parm (class_type_node, identifier);
9747
9748         /* If the next token is an `=', we have a default argument.  */
9749         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9750           {
9751             /* Consume the `=' token.  */
9752             cp_lexer_consume_token (parser->lexer);
9753             /* Parse the default-argument.  */
9754             push_deferring_access_checks (dk_no_deferred);
9755             default_argument = cp_parser_type_id (parser);
9756
9757             /* Template parameter packs cannot have default
9758                arguments. */
9759             if (*is_parameter_pack)
9760               {
9761                 if (identifier)
9762                   error ("%Htemplate parameter pack %qD cannot have a "
9763                          "default argument", &token->location, identifier);
9764                 else
9765                   error ("%Htemplate parameter packs cannot have "
9766                          "default arguments", &token->location);
9767                 default_argument = NULL_TREE;
9768               }
9769             pop_deferring_access_checks ();
9770           }
9771         else
9772           default_argument = NULL_TREE;
9773
9774         /* Create the combined representation of the parameter and the
9775            default argument.  */
9776         parameter = build_tree_list (default_argument, parameter);
9777       }
9778       break;
9779
9780     case RID_TEMPLATE:
9781       {
9782         tree parameter_list;
9783         tree identifier;
9784         tree default_argument;
9785
9786         /* Look for the `<'.  */
9787         cp_parser_require (parser, CPP_LESS, "%<<%>");
9788         /* Parse the template-parameter-list.  */
9789         parameter_list = cp_parser_template_parameter_list (parser);
9790         /* Look for the `>'.  */
9791         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9792         /* Look for the `class' keyword.  */
9793         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9794         /* If the next token is an ellipsis, we have a template
9795            argument pack. */
9796         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9797           {
9798             /* Consume the `...' token. */
9799             cp_lexer_consume_token (parser->lexer);
9800             maybe_warn_variadic_templates ();
9801
9802             *is_parameter_pack = true;
9803           }
9804         /* If the next token is an `=', then there is a
9805            default-argument.  If the next token is a `>', we are at
9806            the end of the parameter-list.  If the next token is a `,',
9807            then we are at the end of this parameter.  */
9808         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9809             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9810             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9811           {
9812             identifier = cp_parser_identifier (parser);
9813             /* Treat invalid names as if the parameter were nameless.  */
9814             if (identifier == error_mark_node)
9815               identifier = NULL_TREE;
9816           }
9817         else
9818           identifier = NULL_TREE;
9819
9820         /* Create the template parameter.  */
9821         parameter = finish_template_template_parm (class_type_node,
9822                                                    identifier);
9823
9824         /* If the next token is an `=', then there is a
9825            default-argument.  */
9826         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9827           {
9828             bool is_template;
9829
9830             /* Consume the `='.  */
9831             cp_lexer_consume_token (parser->lexer);
9832             /* Parse the id-expression.  */
9833             push_deferring_access_checks (dk_no_deferred);
9834             /* save token before parsing the id-expression, for error
9835                reporting */
9836             token = cp_lexer_peek_token (parser->lexer);
9837             default_argument
9838               = cp_parser_id_expression (parser,
9839                                          /*template_keyword_p=*/false,
9840                                          /*check_dependency_p=*/true,
9841                                          /*template_p=*/&is_template,
9842                                          /*declarator_p=*/false,
9843                                          /*optional_p=*/false);
9844             if (TREE_CODE (default_argument) == TYPE_DECL)
9845               /* If the id-expression was a template-id that refers to
9846                  a template-class, we already have the declaration here,
9847                  so no further lookup is needed.  */
9848                  ;
9849             else
9850               /* Look up the name.  */
9851               default_argument
9852                 = cp_parser_lookup_name (parser, default_argument,
9853                                          none_type,
9854                                          /*is_template=*/is_template,
9855                                          /*is_namespace=*/false,
9856                                          /*check_dependency=*/true,
9857                                          /*ambiguous_decls=*/NULL,
9858                                          token->location);
9859             /* See if the default argument is valid.  */
9860             default_argument
9861               = check_template_template_default_arg (default_argument);
9862
9863             /* Template parameter packs cannot have default
9864                arguments. */
9865             if (*is_parameter_pack)
9866               {
9867                 if (identifier)
9868                   error ("%Htemplate parameter pack %qD cannot "
9869                          "have a default argument",
9870                          &token->location, identifier);
9871                 else
9872                   error ("%Htemplate parameter packs cannot "
9873                          "have default arguments",
9874                          &token->location);
9875                 default_argument = NULL_TREE;
9876               }
9877             pop_deferring_access_checks ();
9878           }
9879         else
9880           default_argument = NULL_TREE;
9881
9882         /* Create the combined representation of the parameter and the
9883            default argument.  */
9884         parameter = build_tree_list (default_argument, parameter);
9885       }
9886       break;
9887
9888     default:
9889       gcc_unreachable ();
9890       break;
9891     }
9892
9893   return parameter;
9894 }
9895
9896 /* Parse a template-id.
9897
9898    template-id:
9899      template-name < template-argument-list [opt] >
9900
9901    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9902    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9903    returned.  Otherwise, if the template-name names a function, or set
9904    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9905    names a class, returns a TYPE_DECL for the specialization.
9906
9907    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9908    uninstantiated templates.  */
9909
9910 static tree
9911 cp_parser_template_id (cp_parser *parser,
9912                        bool template_keyword_p,
9913                        bool check_dependency_p,
9914                        bool is_declaration)
9915 {
9916   int i;
9917   tree templ;
9918   tree arguments;
9919   tree template_id;
9920   cp_token_position start_of_id = 0;
9921   deferred_access_check *chk;
9922   VEC (deferred_access_check,gc) *access_check;
9923   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9924   bool is_identifier;
9925
9926   /* If the next token corresponds to a template-id, there is no need
9927      to reparse it.  */
9928   next_token = cp_lexer_peek_token (parser->lexer);
9929   if (next_token->type == CPP_TEMPLATE_ID)
9930     {
9931       struct tree_check *check_value;
9932
9933       /* Get the stored value.  */
9934       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9935       /* Perform any access checks that were deferred.  */
9936       access_check = check_value->checks;
9937       if (access_check)
9938         {
9939           for (i = 0 ;
9940                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9941                ++i)
9942             {
9943               perform_or_defer_access_check (chk->binfo,
9944                                              chk->decl,
9945                                              chk->diag_decl);
9946             }
9947         }
9948       /* Return the stored value.  */
9949       return check_value->value;
9950     }
9951
9952   /* Avoid performing name lookup if there is no possibility of
9953      finding a template-id.  */
9954   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9955       || (next_token->type == CPP_NAME
9956           && !cp_parser_nth_token_starts_template_argument_list_p
9957                (parser, 2)))
9958     {
9959       cp_parser_error (parser, "expected template-id");
9960       return error_mark_node;
9961     }
9962
9963   /* Remember where the template-id starts.  */
9964   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9965     start_of_id = cp_lexer_token_position (parser->lexer, false);
9966
9967   push_deferring_access_checks (dk_deferred);
9968
9969   /* Parse the template-name.  */
9970   is_identifier = false;
9971   token = cp_lexer_peek_token (parser->lexer);
9972   templ = cp_parser_template_name (parser, template_keyword_p,
9973                                    check_dependency_p,
9974                                    is_declaration,
9975                                    &is_identifier);
9976   if (templ == error_mark_node || is_identifier)
9977     {
9978       pop_deferring_access_checks ();
9979       return templ;
9980     }
9981
9982   /* If we find the sequence `[:' after a template-name, it's probably
9983      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9984      parse correctly the argument list.  */
9985   next_token = cp_lexer_peek_token (parser->lexer);
9986   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9987   if (next_token->type == CPP_OPEN_SQUARE
9988       && next_token->flags & DIGRAPH
9989       && next_token_2->type == CPP_COLON
9990       && !(next_token_2->flags & PREV_WHITE))
9991     {
9992       cp_parser_parse_tentatively (parser);
9993       /* Change `:' into `::'.  */
9994       next_token_2->type = CPP_SCOPE;
9995       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9996          CPP_LESS.  */
9997       cp_lexer_consume_token (parser->lexer);
9998
9999       /* Parse the arguments.  */
10000       arguments = cp_parser_enclosed_template_argument_list (parser);
10001       if (!cp_parser_parse_definitely (parser))
10002         {
10003           /* If we couldn't parse an argument list, then we revert our changes
10004              and return simply an error. Maybe this is not a template-id
10005              after all.  */
10006           next_token_2->type = CPP_COLON;
10007           cp_parser_error (parser, "expected %<<%>");
10008           pop_deferring_access_checks ();
10009           return error_mark_node;
10010         }
10011       /* Otherwise, emit an error about the invalid digraph, but continue
10012          parsing because we got our argument list.  */
10013       if (permerror (next_token->location,
10014                      "%<<::%> cannot begin a template-argument list"))
10015         {
10016           static bool hint = false;
10017           inform (next_token->location,
10018                   "%<<:%> is an alternate spelling for %<[%>."
10019                   " Insert whitespace between %<<%> and %<::%>");
10020           if (!hint && !flag_permissive)
10021             {
10022               inform (next_token->location, "(if you use %<-fpermissive%>"
10023                       " G++ will accept your code)");
10024               hint = true;
10025             }
10026         }
10027     }
10028   else
10029     {
10030       /* Look for the `<' that starts the template-argument-list.  */
10031       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10032         {
10033           pop_deferring_access_checks ();
10034           return error_mark_node;
10035         }
10036       /* Parse the arguments.  */
10037       arguments = cp_parser_enclosed_template_argument_list (parser);
10038     }
10039
10040   /* Build a representation of the specialization.  */
10041   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10042     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10043   else if (DECL_CLASS_TEMPLATE_P (templ)
10044            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10045     {
10046       bool entering_scope;
10047       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10048          template (rather than some instantiation thereof) only if
10049          is not nested within some other construct.  For example, in
10050          "template <typename T> void f(T) { A<T>::", A<T> is just an
10051          instantiation of A.  */
10052       entering_scope = (template_parm_scope_p ()
10053                         && cp_lexer_next_token_is (parser->lexer,
10054                                                    CPP_SCOPE));
10055       template_id
10056         = finish_template_type (templ, arguments, entering_scope);
10057     }
10058   else
10059     {
10060       /* If it's not a class-template or a template-template, it should be
10061          a function-template.  */
10062       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10063                    || TREE_CODE (templ) == OVERLOAD
10064                    || BASELINK_P (templ)));
10065
10066       template_id = lookup_template_function (templ, arguments);
10067     }
10068
10069   /* If parsing tentatively, replace the sequence of tokens that makes
10070      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10071      should we re-parse the token stream, we will not have to repeat
10072      the effort required to do the parse, nor will we issue duplicate
10073      error messages about problems during instantiation of the
10074      template.  */
10075   if (start_of_id)
10076     {
10077       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10078
10079       /* Reset the contents of the START_OF_ID token.  */
10080       token->type = CPP_TEMPLATE_ID;
10081       /* Retrieve any deferred checks.  Do not pop this access checks yet
10082          so the memory will not be reclaimed during token replacing below.  */
10083       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10084       token->u.tree_check_value->value = template_id;
10085       token->u.tree_check_value->checks = get_deferred_access_checks ();
10086       token->keyword = RID_MAX;
10087
10088       /* Purge all subsequent tokens.  */
10089       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10090
10091       /* ??? Can we actually assume that, if template_id ==
10092          error_mark_node, we will have issued a diagnostic to the
10093          user, as opposed to simply marking the tentative parse as
10094          failed?  */
10095       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10096         error ("%Hparse error in template argument list",
10097                &token->location);
10098     }
10099
10100   pop_deferring_access_checks ();
10101   return template_id;
10102 }
10103
10104 /* Parse a template-name.
10105
10106    template-name:
10107      identifier
10108
10109    The standard should actually say:
10110
10111    template-name:
10112      identifier
10113      operator-function-id
10114
10115    A defect report has been filed about this issue.
10116
10117    A conversion-function-id cannot be a template name because they cannot
10118    be part of a template-id. In fact, looking at this code:
10119
10120    a.operator K<int>()
10121
10122    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10123    It is impossible to call a templated conversion-function-id with an
10124    explicit argument list, since the only allowed template parameter is
10125    the type to which it is converting.
10126
10127    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10128    `template' keyword, in a construction like:
10129
10130      T::template f<3>()
10131
10132    In that case `f' is taken to be a template-name, even though there
10133    is no way of knowing for sure.
10134
10135    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10136    name refers to a set of overloaded functions, at least one of which
10137    is a template, or an IDENTIFIER_NODE with the name of the template,
10138    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10139    names are looked up inside uninstantiated templates.  */
10140
10141 static tree
10142 cp_parser_template_name (cp_parser* parser,
10143                          bool template_keyword_p,
10144                          bool check_dependency_p,
10145                          bool is_declaration,
10146                          bool *is_identifier)
10147 {
10148   tree identifier;
10149   tree decl;
10150   tree fns;
10151   cp_token *token = cp_lexer_peek_token (parser->lexer);
10152
10153   /* If the next token is `operator', then we have either an
10154      operator-function-id or a conversion-function-id.  */
10155   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10156     {
10157       /* We don't know whether we're looking at an
10158          operator-function-id or a conversion-function-id.  */
10159       cp_parser_parse_tentatively (parser);
10160       /* Try an operator-function-id.  */
10161       identifier = cp_parser_operator_function_id (parser);
10162       /* If that didn't work, try a conversion-function-id.  */
10163       if (!cp_parser_parse_definitely (parser))
10164         {
10165           cp_parser_error (parser, "expected template-name");
10166           return error_mark_node;
10167         }
10168     }
10169   /* Look for the identifier.  */
10170   else
10171     identifier = cp_parser_identifier (parser);
10172
10173   /* If we didn't find an identifier, we don't have a template-id.  */
10174   if (identifier == error_mark_node)
10175     return error_mark_node;
10176
10177   /* If the name immediately followed the `template' keyword, then it
10178      is a template-name.  However, if the next token is not `<', then
10179      we do not treat it as a template-name, since it is not being used
10180      as part of a template-id.  This enables us to handle constructs
10181      like:
10182
10183        template <typename T> struct S { S(); };
10184        template <typename T> S<T>::S();
10185
10186      correctly.  We would treat `S' as a template -- if it were `S<T>'
10187      -- but we do not if there is no `<'.  */
10188
10189   if (processing_template_decl
10190       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10191     {
10192       /* In a declaration, in a dependent context, we pretend that the
10193          "template" keyword was present in order to improve error
10194          recovery.  For example, given:
10195
10196            template <typename T> void f(T::X<int>);
10197
10198          we want to treat "X<int>" as a template-id.  */
10199       if (is_declaration
10200           && !template_keyword_p
10201           && parser->scope && TYPE_P (parser->scope)
10202           && check_dependency_p
10203           && dependent_type_p (parser->scope)
10204           /* Do not do this for dtors (or ctors), since they never
10205              need the template keyword before their name.  */
10206           && !constructor_name_p (identifier, parser->scope))
10207         {
10208           cp_token_position start = 0;
10209
10210           /* Explain what went wrong.  */
10211           error ("%Hnon-template %qD used as template",
10212                  &token->location, identifier);
10213           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10214                   parser->scope, identifier);
10215           /* If parsing tentatively, find the location of the "<" token.  */
10216           if (cp_parser_simulate_error (parser))
10217             start = cp_lexer_token_position (parser->lexer, true);
10218           /* Parse the template arguments so that we can issue error
10219              messages about them.  */
10220           cp_lexer_consume_token (parser->lexer);
10221           cp_parser_enclosed_template_argument_list (parser);
10222           /* Skip tokens until we find a good place from which to
10223              continue parsing.  */
10224           cp_parser_skip_to_closing_parenthesis (parser,
10225                                                  /*recovering=*/true,
10226                                                  /*or_comma=*/true,
10227                                                  /*consume_paren=*/false);
10228           /* If parsing tentatively, permanently remove the
10229              template argument list.  That will prevent duplicate
10230              error messages from being issued about the missing
10231              "template" keyword.  */
10232           if (start)
10233             cp_lexer_purge_tokens_after (parser->lexer, start);
10234           if (is_identifier)
10235             *is_identifier = true;
10236           return identifier;
10237         }
10238
10239       /* If the "template" keyword is present, then there is generally
10240          no point in doing name-lookup, so we just return IDENTIFIER.
10241          But, if the qualifying scope is non-dependent then we can
10242          (and must) do name-lookup normally.  */
10243       if (template_keyword_p
10244           && (!parser->scope
10245               || (TYPE_P (parser->scope)
10246                   && dependent_type_p (parser->scope))))
10247         return identifier;
10248     }
10249
10250   /* Look up the name.  */
10251   decl = cp_parser_lookup_name (parser, identifier,
10252                                 none_type,
10253                                 /*is_template=*/false,
10254                                 /*is_namespace=*/false,
10255                                 check_dependency_p,
10256                                 /*ambiguous_decls=*/NULL,
10257                                 token->location);
10258   decl = maybe_get_template_decl_from_type_decl (decl);
10259
10260   /* If DECL is a template, then the name was a template-name.  */
10261   if (TREE_CODE (decl) == TEMPLATE_DECL)
10262     ;
10263   else
10264     {
10265       tree fn = NULL_TREE;
10266
10267       /* The standard does not explicitly indicate whether a name that
10268          names a set of overloaded declarations, some of which are
10269          templates, is a template-name.  However, such a name should
10270          be a template-name; otherwise, there is no way to form a
10271          template-id for the overloaded templates.  */
10272       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10273       if (TREE_CODE (fns) == OVERLOAD)
10274         for (fn = fns; fn; fn = OVL_NEXT (fn))
10275           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10276             break;
10277
10278       if (!fn)
10279         {
10280           /* The name does not name a template.  */
10281           cp_parser_error (parser, "expected template-name");
10282           return error_mark_node;
10283         }
10284     }
10285
10286   /* If DECL is dependent, and refers to a function, then just return
10287      its name; we will look it up again during template instantiation.  */
10288   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10289     {
10290       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10291       if (TYPE_P (scope) && dependent_type_p (scope))
10292         return identifier;
10293     }
10294
10295   return decl;
10296 }
10297
10298 /* Parse a template-argument-list.
10299
10300    template-argument-list:
10301      template-argument ... [opt]
10302      template-argument-list , template-argument ... [opt]
10303
10304    Returns a TREE_VEC containing the arguments.  */
10305
10306 static tree
10307 cp_parser_template_argument_list (cp_parser* parser)
10308 {
10309   tree fixed_args[10];
10310   unsigned n_args = 0;
10311   unsigned alloced = 10;
10312   tree *arg_ary = fixed_args;
10313   tree vec;
10314   bool saved_in_template_argument_list_p;
10315   bool saved_ice_p;
10316   bool saved_non_ice_p;
10317
10318   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10319   parser->in_template_argument_list_p = true;
10320   /* Even if the template-id appears in an integral
10321      constant-expression, the contents of the argument list do
10322      not.  */
10323   saved_ice_p = parser->integral_constant_expression_p;
10324   parser->integral_constant_expression_p = false;
10325   saved_non_ice_p = parser->non_integral_constant_expression_p;
10326   parser->non_integral_constant_expression_p = false;
10327   /* Parse the arguments.  */
10328   do
10329     {
10330       tree argument;
10331
10332       if (n_args)
10333         /* Consume the comma.  */
10334         cp_lexer_consume_token (parser->lexer);
10335
10336       /* Parse the template-argument.  */
10337       argument = cp_parser_template_argument (parser);
10338
10339       /* If the next token is an ellipsis, we're expanding a template
10340          argument pack. */
10341       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10342         {
10343           /* Consume the `...' token. */
10344           cp_lexer_consume_token (parser->lexer);
10345
10346           /* Make the argument into a TYPE_PACK_EXPANSION or
10347              EXPR_PACK_EXPANSION. */
10348           argument = make_pack_expansion (argument);
10349         }
10350
10351       if (n_args == alloced)
10352         {
10353           alloced *= 2;
10354
10355           if (arg_ary == fixed_args)
10356             {
10357               arg_ary = XNEWVEC (tree, alloced);
10358               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10359             }
10360           else
10361             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10362         }
10363       arg_ary[n_args++] = argument;
10364     }
10365   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10366
10367   vec = make_tree_vec (n_args);
10368
10369   while (n_args--)
10370     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10371
10372   if (arg_ary != fixed_args)
10373     free (arg_ary);
10374   parser->non_integral_constant_expression_p = saved_non_ice_p;
10375   parser->integral_constant_expression_p = saved_ice_p;
10376   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10377   return vec;
10378 }
10379
10380 /* Parse a template-argument.
10381
10382    template-argument:
10383      assignment-expression
10384      type-id
10385      id-expression
10386
10387    The representation is that of an assignment-expression, type-id, or
10388    id-expression -- except that the qualified id-expression is
10389    evaluated, so that the value returned is either a DECL or an
10390    OVERLOAD.
10391
10392    Although the standard says "assignment-expression", it forbids
10393    throw-expressions or assignments in the template argument.
10394    Therefore, we use "conditional-expression" instead.  */
10395
10396 static tree
10397 cp_parser_template_argument (cp_parser* parser)
10398 {
10399   tree argument;
10400   bool template_p;
10401   bool address_p;
10402   bool maybe_type_id = false;
10403   cp_token *token = NULL, *argument_start_token = NULL;
10404   cp_id_kind idk;
10405
10406   /* There's really no way to know what we're looking at, so we just
10407      try each alternative in order.
10408
10409        [temp.arg]
10410
10411        In a template-argument, an ambiguity between a type-id and an
10412        expression is resolved to a type-id, regardless of the form of
10413        the corresponding template-parameter.
10414
10415      Therefore, we try a type-id first.  */
10416   cp_parser_parse_tentatively (parser);
10417   argument = cp_parser_type_id (parser);
10418   /* If there was no error parsing the type-id but the next token is a
10419      '>>', our behavior depends on which dialect of C++ we're
10420      parsing. In C++98, we probably found a typo for '> >'. But there
10421      are type-id which are also valid expressions. For instance:
10422
10423      struct X { int operator >> (int); };
10424      template <int V> struct Foo {};
10425      Foo<X () >> 5> r;
10426
10427      Here 'X()' is a valid type-id of a function type, but the user just
10428      wanted to write the expression "X() >> 5". Thus, we remember that we
10429      found a valid type-id, but we still try to parse the argument as an
10430      expression to see what happens. 
10431
10432      In C++0x, the '>>' will be considered two separate '>'
10433      tokens.  */
10434   if (!cp_parser_error_occurred (parser)
10435       && cxx_dialect == cxx98
10436       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10437     {
10438       maybe_type_id = true;
10439       cp_parser_abort_tentative_parse (parser);
10440     }
10441   else
10442     {
10443       /* If the next token isn't a `,' or a `>', then this argument wasn't
10444       really finished. This means that the argument is not a valid
10445       type-id.  */
10446       if (!cp_parser_next_token_ends_template_argument_p (parser))
10447         cp_parser_error (parser, "expected template-argument");
10448       /* If that worked, we're done.  */
10449       if (cp_parser_parse_definitely (parser))
10450         return argument;
10451     }
10452   /* We're still not sure what the argument will be.  */
10453   cp_parser_parse_tentatively (parser);
10454   /* Try a template.  */
10455   argument_start_token = cp_lexer_peek_token (parser->lexer);
10456   argument = cp_parser_id_expression (parser,
10457                                       /*template_keyword_p=*/false,
10458                                       /*check_dependency_p=*/true,
10459                                       &template_p,
10460                                       /*declarator_p=*/false,
10461                                       /*optional_p=*/false);
10462   /* If the next token isn't a `,' or a `>', then this argument wasn't
10463      really finished.  */
10464   if (!cp_parser_next_token_ends_template_argument_p (parser))
10465     cp_parser_error (parser, "expected template-argument");
10466   if (!cp_parser_error_occurred (parser))
10467     {
10468       /* Figure out what is being referred to.  If the id-expression
10469          was for a class template specialization, then we will have a
10470          TYPE_DECL at this point.  There is no need to do name lookup
10471          at this point in that case.  */
10472       if (TREE_CODE (argument) != TYPE_DECL)
10473         argument = cp_parser_lookup_name (parser, argument,
10474                                           none_type,
10475                                           /*is_template=*/template_p,
10476                                           /*is_namespace=*/false,
10477                                           /*check_dependency=*/true,
10478                                           /*ambiguous_decls=*/NULL,
10479                                           argument_start_token->location);
10480       if (TREE_CODE (argument) != TEMPLATE_DECL
10481           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10482         cp_parser_error (parser, "expected template-name");
10483     }
10484   if (cp_parser_parse_definitely (parser))
10485     return argument;
10486   /* It must be a non-type argument.  There permitted cases are given
10487      in [temp.arg.nontype]:
10488
10489      -- an integral constant-expression of integral or enumeration
10490         type; or
10491
10492      -- the name of a non-type template-parameter; or
10493
10494      -- the name of an object or function with external linkage...
10495
10496      -- the address of an object or function with external linkage...
10497
10498      -- a pointer to member...  */
10499   /* Look for a non-type template parameter.  */
10500   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10501     {
10502       cp_parser_parse_tentatively (parser);
10503       argument = cp_parser_primary_expression (parser,
10504                                                /*address_p=*/false,
10505                                                /*cast_p=*/false,
10506                                                /*template_arg_p=*/true,
10507                                                &idk);
10508       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10509           || !cp_parser_next_token_ends_template_argument_p (parser))
10510         cp_parser_simulate_error (parser);
10511       if (cp_parser_parse_definitely (parser))
10512         return argument;
10513     }
10514
10515   /* If the next token is "&", the argument must be the address of an
10516      object or function with external linkage.  */
10517   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10518   if (address_p)
10519     cp_lexer_consume_token (parser->lexer);
10520   /* See if we might have an id-expression.  */
10521   token = cp_lexer_peek_token (parser->lexer);
10522   if (token->type == CPP_NAME
10523       || token->keyword == RID_OPERATOR
10524       || token->type == CPP_SCOPE
10525       || token->type == CPP_TEMPLATE_ID
10526       || token->type == CPP_NESTED_NAME_SPECIFIER)
10527     {
10528       cp_parser_parse_tentatively (parser);
10529       argument = cp_parser_primary_expression (parser,
10530                                                address_p,
10531                                                /*cast_p=*/false,
10532                                                /*template_arg_p=*/true,
10533                                                &idk);
10534       if (cp_parser_error_occurred (parser)
10535           || !cp_parser_next_token_ends_template_argument_p (parser))
10536         cp_parser_abort_tentative_parse (parser);
10537       else
10538         {
10539           if (TREE_CODE (argument) == INDIRECT_REF)
10540             {
10541               gcc_assert (REFERENCE_REF_P (argument));
10542               argument = TREE_OPERAND (argument, 0);
10543             }
10544
10545           if (TREE_CODE (argument) == VAR_DECL)
10546             {
10547               /* A variable without external linkage might still be a
10548                  valid constant-expression, so no error is issued here
10549                  if the external-linkage check fails.  */
10550               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10551                 cp_parser_simulate_error (parser);
10552             }
10553           else if (is_overloaded_fn (argument))
10554             /* All overloaded functions are allowed; if the external
10555                linkage test does not pass, an error will be issued
10556                later.  */
10557             ;
10558           else if (address_p
10559                    && (TREE_CODE (argument) == OFFSET_REF
10560                        || TREE_CODE (argument) == SCOPE_REF))
10561             /* A pointer-to-member.  */
10562             ;
10563           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10564             ;
10565           else
10566             cp_parser_simulate_error (parser);
10567
10568           if (cp_parser_parse_definitely (parser))
10569             {
10570               if (address_p)
10571                 argument = build_x_unary_op (ADDR_EXPR, argument,
10572                                              tf_warning_or_error);
10573               return argument;
10574             }
10575         }
10576     }
10577   /* If the argument started with "&", there are no other valid
10578      alternatives at this point.  */
10579   if (address_p)
10580     {
10581       cp_parser_error (parser, "invalid non-type template argument");
10582       return error_mark_node;
10583     }
10584
10585   /* If the argument wasn't successfully parsed as a type-id followed
10586      by '>>', the argument can only be a constant expression now.
10587      Otherwise, we try parsing the constant-expression tentatively,
10588      because the argument could really be a type-id.  */
10589   if (maybe_type_id)
10590     cp_parser_parse_tentatively (parser);
10591   argument = cp_parser_constant_expression (parser,
10592                                             /*allow_non_constant_p=*/false,
10593                                             /*non_constant_p=*/NULL);
10594   argument = fold_non_dependent_expr (argument);
10595   if (!maybe_type_id)
10596     return argument;
10597   if (!cp_parser_next_token_ends_template_argument_p (parser))
10598     cp_parser_error (parser, "expected template-argument");
10599   if (cp_parser_parse_definitely (parser))
10600     return argument;
10601   /* We did our best to parse the argument as a non type-id, but that
10602      was the only alternative that matched (albeit with a '>' after
10603      it). We can assume it's just a typo from the user, and a
10604      diagnostic will then be issued.  */
10605   return cp_parser_type_id (parser);
10606 }
10607
10608 /* Parse an explicit-instantiation.
10609
10610    explicit-instantiation:
10611      template declaration
10612
10613    Although the standard says `declaration', what it really means is:
10614
10615    explicit-instantiation:
10616      template decl-specifier-seq [opt] declarator [opt] ;
10617
10618    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10619    supposed to be allowed.  A defect report has been filed about this
10620    issue.
10621
10622    GNU Extension:
10623
10624    explicit-instantiation:
10625      storage-class-specifier template
10626        decl-specifier-seq [opt] declarator [opt] ;
10627      function-specifier template
10628        decl-specifier-seq [opt] declarator [opt] ;  */
10629
10630 static void
10631 cp_parser_explicit_instantiation (cp_parser* parser)
10632 {
10633   int declares_class_or_enum;
10634   cp_decl_specifier_seq decl_specifiers;
10635   tree extension_specifier = NULL_TREE;
10636   cp_token *token;
10637
10638   /* Look for an (optional) storage-class-specifier or
10639      function-specifier.  */
10640   if (cp_parser_allow_gnu_extensions_p (parser))
10641     {
10642       extension_specifier
10643         = cp_parser_storage_class_specifier_opt (parser);
10644       if (!extension_specifier)
10645         extension_specifier
10646           = cp_parser_function_specifier_opt (parser,
10647                                               /*decl_specs=*/NULL);
10648     }
10649
10650   /* Look for the `template' keyword.  */
10651   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10652   /* Let the front end know that we are processing an explicit
10653      instantiation.  */
10654   begin_explicit_instantiation ();
10655   /* [temp.explicit] says that we are supposed to ignore access
10656      control while processing explicit instantiation directives.  */
10657   push_deferring_access_checks (dk_no_check);
10658   /* Parse a decl-specifier-seq.  */
10659   token = cp_lexer_peek_token (parser->lexer);
10660   cp_parser_decl_specifier_seq (parser,
10661                                 CP_PARSER_FLAGS_OPTIONAL,
10662                                 &decl_specifiers,
10663                                 &declares_class_or_enum);
10664   /* If there was exactly one decl-specifier, and it declared a class,
10665      and there's no declarator, then we have an explicit type
10666      instantiation.  */
10667   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10668     {
10669       tree type;
10670
10671       type = check_tag_decl (&decl_specifiers);
10672       /* Turn access control back on for names used during
10673          template instantiation.  */
10674       pop_deferring_access_checks ();
10675       if (type)
10676         do_type_instantiation (type, extension_specifier,
10677                                /*complain=*/tf_error);
10678     }
10679   else
10680     {
10681       cp_declarator *declarator;
10682       tree decl;
10683
10684       /* Parse the declarator.  */
10685       declarator
10686         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10687                                 /*ctor_dtor_or_conv_p=*/NULL,
10688                                 /*parenthesized_p=*/NULL,
10689                                 /*member_p=*/false);
10690       if (declares_class_or_enum & 2)
10691         cp_parser_check_for_definition_in_return_type (declarator,
10692                                                        decl_specifiers.type,
10693                                                        decl_specifiers.type_location);
10694       if (declarator != cp_error_declarator)
10695         {
10696           decl = grokdeclarator (declarator, &decl_specifiers,
10697                                  NORMAL, 0, &decl_specifiers.attributes);
10698           /* Turn access control back on for names used during
10699              template instantiation.  */
10700           pop_deferring_access_checks ();
10701           /* Do the explicit instantiation.  */
10702           do_decl_instantiation (decl, extension_specifier);
10703         }
10704       else
10705         {
10706           pop_deferring_access_checks ();
10707           /* Skip the body of the explicit instantiation.  */
10708           cp_parser_skip_to_end_of_statement (parser);
10709         }
10710     }
10711   /* We're done with the instantiation.  */
10712   end_explicit_instantiation ();
10713
10714   cp_parser_consume_semicolon_at_end_of_statement (parser);
10715 }
10716
10717 /* Parse an explicit-specialization.
10718
10719    explicit-specialization:
10720      template < > declaration
10721
10722    Although the standard says `declaration', what it really means is:
10723
10724    explicit-specialization:
10725      template <> decl-specifier [opt] init-declarator [opt] ;
10726      template <> function-definition
10727      template <> explicit-specialization
10728      template <> template-declaration  */
10729
10730 static void
10731 cp_parser_explicit_specialization (cp_parser* parser)
10732 {
10733   bool need_lang_pop;
10734   cp_token *token = cp_lexer_peek_token (parser->lexer);
10735
10736   /* Look for the `template' keyword.  */
10737   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10738   /* Look for the `<'.  */
10739   cp_parser_require (parser, CPP_LESS, "%<<%>");
10740   /* Look for the `>'.  */
10741   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10742   /* We have processed another parameter list.  */
10743   ++parser->num_template_parameter_lists;
10744   /* [temp]
10745
10746      A template ... explicit specialization ... shall not have C
10747      linkage.  */
10748   if (current_lang_name == lang_name_c)
10749     {
10750       error ("%Htemplate specialization with C linkage", &token->location);
10751       /* Give it C++ linkage to avoid confusing other parts of the
10752          front end.  */
10753       push_lang_context (lang_name_cplusplus);
10754       need_lang_pop = true;
10755     }
10756   else
10757     need_lang_pop = false;
10758   /* Let the front end know that we are beginning a specialization.  */
10759   if (!begin_specialization ())
10760     {
10761       end_specialization ();
10762       cp_parser_skip_to_end_of_block_or_statement (parser);
10763       return;
10764     }
10765
10766   /* If the next keyword is `template', we need to figure out whether
10767      or not we're looking a template-declaration.  */
10768   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10769     {
10770       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10771           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10772         cp_parser_template_declaration_after_export (parser,
10773                                                      /*member_p=*/false);
10774       else
10775         cp_parser_explicit_specialization (parser);
10776     }
10777   else
10778     /* Parse the dependent declaration.  */
10779     cp_parser_single_declaration (parser,
10780                                   /*checks=*/NULL,
10781                                   /*member_p=*/false,
10782                                   /*explicit_specialization_p=*/true,
10783                                   /*friend_p=*/NULL);
10784   /* We're done with the specialization.  */
10785   end_specialization ();
10786   /* For the erroneous case of a template with C linkage, we pushed an
10787      implicit C++ linkage scope; exit that scope now.  */
10788   if (need_lang_pop)
10789     pop_lang_context ();
10790   /* We're done with this parameter list.  */
10791   --parser->num_template_parameter_lists;
10792 }
10793
10794 /* Parse a type-specifier.
10795
10796    type-specifier:
10797      simple-type-specifier
10798      class-specifier
10799      enum-specifier
10800      elaborated-type-specifier
10801      cv-qualifier
10802
10803    GNU Extension:
10804
10805    type-specifier:
10806      __complex__
10807
10808    Returns a representation of the type-specifier.  For a
10809    class-specifier, enum-specifier, or elaborated-type-specifier, a
10810    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10811
10812    The parser flags FLAGS is used to control type-specifier parsing.
10813
10814    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10815    in a decl-specifier-seq.
10816
10817    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10818    class-specifier, enum-specifier, or elaborated-type-specifier, then
10819    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10820    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10821    zero.
10822
10823    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10824    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10825    is set to FALSE.  */
10826
10827 static tree
10828 cp_parser_type_specifier (cp_parser* parser,
10829                           cp_parser_flags flags,
10830                           cp_decl_specifier_seq *decl_specs,
10831                           bool is_declaration,
10832                           int* declares_class_or_enum,
10833                           bool* is_cv_qualifier)
10834 {
10835   tree type_spec = NULL_TREE;
10836   cp_token *token;
10837   enum rid keyword;
10838   cp_decl_spec ds = ds_last;
10839
10840   /* Assume this type-specifier does not declare a new type.  */
10841   if (declares_class_or_enum)
10842     *declares_class_or_enum = 0;
10843   /* And that it does not specify a cv-qualifier.  */
10844   if (is_cv_qualifier)
10845     *is_cv_qualifier = false;
10846   /* Peek at the next token.  */
10847   token = cp_lexer_peek_token (parser->lexer);
10848
10849   /* If we're looking at a keyword, we can use that to guide the
10850      production we choose.  */
10851   keyword = token->keyword;
10852   switch (keyword)
10853     {
10854     case RID_ENUM:
10855       /* Look for the enum-specifier.  */
10856       type_spec = cp_parser_enum_specifier (parser);
10857       /* If that worked, we're done.  */
10858       if (type_spec)
10859         {
10860           if (declares_class_or_enum)
10861             *declares_class_or_enum = 2;
10862           if (decl_specs)
10863             cp_parser_set_decl_spec_type (decl_specs,
10864                                           type_spec,
10865                                           token->location,
10866                                           /*user_defined_p=*/true);
10867           return type_spec;
10868         }
10869       else
10870         goto elaborated_type_specifier;
10871
10872       /* Any of these indicate either a class-specifier, or an
10873          elaborated-type-specifier.  */
10874     case RID_CLASS:
10875     case RID_STRUCT:
10876     case RID_UNION:
10877       /* Parse tentatively so that we can back up if we don't find a
10878          class-specifier.  */
10879       cp_parser_parse_tentatively (parser);
10880       /* Look for the class-specifier.  */
10881       type_spec = cp_parser_class_specifier (parser);
10882       /* If that worked, we're done.  */
10883       if (cp_parser_parse_definitely (parser))
10884         {
10885           if (declares_class_or_enum)
10886             *declares_class_or_enum = 2;
10887           if (decl_specs)
10888             cp_parser_set_decl_spec_type (decl_specs,
10889                                           type_spec,
10890                                           token->location,
10891                                           /*user_defined_p=*/true);
10892           return type_spec;
10893         }
10894
10895       /* Fall through.  */
10896     elaborated_type_specifier:
10897       /* We're declaring (not defining) a class or enum.  */
10898       if (declares_class_or_enum)
10899         *declares_class_or_enum = 1;
10900
10901       /* Fall through.  */
10902     case RID_TYPENAME:
10903       /* Look for an elaborated-type-specifier.  */
10904       type_spec
10905         = (cp_parser_elaborated_type_specifier
10906            (parser,
10907             decl_specs && decl_specs->specs[(int) ds_friend],
10908             is_declaration));
10909       if (decl_specs)
10910         cp_parser_set_decl_spec_type (decl_specs,
10911                                       type_spec,
10912                                       token->location,
10913                                       /*user_defined_p=*/true);
10914       return type_spec;
10915
10916     case RID_CONST:
10917       ds = ds_const;
10918       if (is_cv_qualifier)
10919         *is_cv_qualifier = true;
10920       break;
10921
10922     case RID_VOLATILE:
10923       ds = ds_volatile;
10924       if (is_cv_qualifier)
10925         *is_cv_qualifier = true;
10926       break;
10927
10928     case RID_RESTRICT:
10929       ds = ds_restrict;
10930       if (is_cv_qualifier)
10931         *is_cv_qualifier = true;
10932       break;
10933
10934     case RID_COMPLEX:
10935       /* The `__complex__' keyword is a GNU extension.  */
10936       ds = ds_complex;
10937       break;
10938
10939     default:
10940       break;
10941     }
10942
10943   /* Handle simple keywords.  */
10944   if (ds != ds_last)
10945     {
10946       if (decl_specs)
10947         {
10948           ++decl_specs->specs[(int)ds];
10949           decl_specs->any_specifiers_p = true;
10950         }
10951       return cp_lexer_consume_token (parser->lexer)->u.value;
10952     }
10953
10954   /* If we do not already have a type-specifier, assume we are looking
10955      at a simple-type-specifier.  */
10956   type_spec = cp_parser_simple_type_specifier (parser,
10957                                                decl_specs,
10958                                                flags);
10959
10960   /* If we didn't find a type-specifier, and a type-specifier was not
10961      optional in this context, issue an error message.  */
10962   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10963     {
10964       cp_parser_error (parser, "expected type specifier");
10965       return error_mark_node;
10966     }
10967
10968   return type_spec;
10969 }
10970
10971 /* Parse a simple-type-specifier.
10972
10973    simple-type-specifier:
10974      :: [opt] nested-name-specifier [opt] type-name
10975      :: [opt] nested-name-specifier template template-id
10976      char
10977      wchar_t
10978      bool
10979      short
10980      int
10981      long
10982      signed
10983      unsigned
10984      float
10985      double
10986      void
10987
10988    C++0x Extension:
10989
10990    simple-type-specifier:
10991      auto
10992      decltype ( expression )   
10993      char16_t
10994      char32_t
10995
10996    GNU Extension:
10997
10998    simple-type-specifier:
10999      __typeof__ unary-expression
11000      __typeof__ ( type-id )
11001
11002    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11003    appropriately updated.  */
11004
11005 static tree
11006 cp_parser_simple_type_specifier (cp_parser* parser,
11007                                  cp_decl_specifier_seq *decl_specs,
11008                                  cp_parser_flags flags)
11009 {
11010   tree type = NULL_TREE;
11011   cp_token *token;
11012
11013   /* Peek at the next token.  */
11014   token = cp_lexer_peek_token (parser->lexer);
11015
11016   /* If we're looking at a keyword, things are easy.  */
11017   switch (token->keyword)
11018     {
11019     case RID_CHAR:
11020       if (decl_specs)
11021         decl_specs->explicit_char_p = true;
11022       type = char_type_node;
11023       break;
11024     case RID_CHAR16:
11025       type = char16_type_node;
11026       break;
11027     case RID_CHAR32:
11028       type = char32_type_node;
11029       break;
11030     case RID_WCHAR:
11031       type = wchar_type_node;
11032       break;
11033     case RID_BOOL:
11034       type = boolean_type_node;
11035       break;
11036     case RID_SHORT:
11037       if (decl_specs)
11038         ++decl_specs->specs[(int) ds_short];
11039       type = short_integer_type_node;
11040       break;
11041     case RID_INT:
11042       if (decl_specs)
11043         decl_specs->explicit_int_p = true;
11044       type = integer_type_node;
11045       break;
11046     case RID_LONG:
11047       if (decl_specs)
11048         ++decl_specs->specs[(int) ds_long];
11049       type = long_integer_type_node;
11050       break;
11051     case RID_SIGNED:
11052       if (decl_specs)
11053         ++decl_specs->specs[(int) ds_signed];
11054       type = integer_type_node;
11055       break;
11056     case RID_UNSIGNED:
11057       if (decl_specs)
11058         ++decl_specs->specs[(int) ds_unsigned];
11059       type = unsigned_type_node;
11060       break;
11061     case RID_FLOAT:
11062       type = float_type_node;
11063       break;
11064     case RID_DOUBLE:
11065       type = double_type_node;
11066       break;
11067     case RID_VOID:
11068       type = void_type_node;
11069       break;
11070       
11071     case RID_AUTO:
11072       if (cxx_dialect != cxx98)
11073         {
11074           /* Consume the token.  */
11075           cp_lexer_consume_token (parser->lexer);
11076           /* We do not yet support the use of `auto' as a
11077              type-specifier.  */
11078           error ("%HC++0x %<auto%> specifier not supported", &token->location);
11079         }
11080       break;
11081
11082     case RID_DECLTYPE:
11083       /* Parse the `decltype' type.  */
11084       type = cp_parser_decltype (parser);
11085
11086       if (decl_specs)
11087         cp_parser_set_decl_spec_type (decl_specs, type,
11088                                       token->location,
11089                                       /*user_defined_p=*/true);
11090
11091       return type;
11092
11093     case RID_TYPEOF:
11094       /* Consume the `typeof' token.  */
11095       cp_lexer_consume_token (parser->lexer);
11096       /* Parse the operand to `typeof'.  */
11097       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11098       /* If it is not already a TYPE, take its type.  */
11099       if (!TYPE_P (type))
11100         type = finish_typeof (type);
11101
11102       if (decl_specs)
11103         cp_parser_set_decl_spec_type (decl_specs, type,
11104                                       token->location,
11105                                       /*user_defined_p=*/true);
11106
11107       return type;
11108
11109     default:
11110       break;
11111     }
11112
11113   /* If the type-specifier was for a built-in type, we're done.  */
11114   if (type)
11115     {
11116       tree id;
11117
11118       /* Record the type.  */
11119       if (decl_specs
11120           && (token->keyword != RID_SIGNED
11121               && token->keyword != RID_UNSIGNED
11122               && token->keyword != RID_SHORT
11123               && token->keyword != RID_LONG))
11124         cp_parser_set_decl_spec_type (decl_specs,
11125                                       type,
11126                                       token->location,
11127                                       /*user_defined=*/false);
11128       if (decl_specs)
11129         decl_specs->any_specifiers_p = true;
11130
11131       /* Consume the token.  */
11132       id = cp_lexer_consume_token (parser->lexer)->u.value;
11133
11134       /* There is no valid C++ program where a non-template type is
11135          followed by a "<".  That usually indicates that the user thought
11136          that the type was a template.  */
11137       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11138
11139       return TYPE_NAME (type);
11140     }
11141
11142   /* The type-specifier must be a user-defined type.  */
11143   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11144     {
11145       bool qualified_p;
11146       bool global_p;
11147
11148       /* Don't gobble tokens or issue error messages if this is an
11149          optional type-specifier.  */
11150       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11151         cp_parser_parse_tentatively (parser);
11152
11153       /* Look for the optional `::' operator.  */
11154       global_p
11155         = (cp_parser_global_scope_opt (parser,
11156                                        /*current_scope_valid_p=*/false)
11157            != NULL_TREE);
11158       /* Look for the nested-name specifier.  */
11159       qualified_p
11160         = (cp_parser_nested_name_specifier_opt (parser,
11161                                                 /*typename_keyword_p=*/false,
11162                                                 /*check_dependency_p=*/true,
11163                                                 /*type_p=*/false,
11164                                                 /*is_declaration=*/false)
11165            != NULL_TREE);
11166       token = cp_lexer_peek_token (parser->lexer);
11167       /* If we have seen a nested-name-specifier, and the next token
11168          is `template', then we are using the template-id production.  */
11169       if (parser->scope
11170           && cp_parser_optional_template_keyword (parser))
11171         {
11172           /* Look for the template-id.  */
11173           type = cp_parser_template_id (parser,
11174                                         /*template_keyword_p=*/true,
11175                                         /*check_dependency_p=*/true,
11176                                         /*is_declaration=*/false);
11177           /* If the template-id did not name a type, we are out of
11178              luck.  */
11179           if (TREE_CODE (type) != TYPE_DECL)
11180             {
11181               cp_parser_error (parser, "expected template-id for type");
11182               type = NULL_TREE;
11183             }
11184         }
11185       /* Otherwise, look for a type-name.  */
11186       else
11187         type = cp_parser_type_name (parser);
11188       /* Keep track of all name-lookups performed in class scopes.  */
11189       if (type
11190           && !global_p
11191           && !qualified_p
11192           && TREE_CODE (type) == TYPE_DECL
11193           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11194         maybe_note_name_used_in_class (DECL_NAME (type), type);
11195       /* If it didn't work out, we don't have a TYPE.  */
11196       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11197           && !cp_parser_parse_definitely (parser))
11198         type = NULL_TREE;
11199       if (type && decl_specs)
11200         cp_parser_set_decl_spec_type (decl_specs, type,
11201                                       token->location,
11202                                       /*user_defined=*/true);
11203     }
11204
11205   /* If we didn't get a type-name, issue an error message.  */
11206   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11207     {
11208       cp_parser_error (parser, "expected type-name");
11209       return error_mark_node;
11210     }
11211
11212   /* There is no valid C++ program where a non-template type is
11213      followed by a "<".  That usually indicates that the user thought
11214      that the type was a template.  */
11215   if (type && type != error_mark_node)
11216     {
11217       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11218          If it is, then the '<'...'>' enclose protocol names rather than
11219          template arguments, and so everything is fine.  */
11220       if (c_dialect_objc ()
11221           && (objc_is_id (type) || objc_is_class_name (type)))
11222         {
11223           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11224           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11225
11226           /* Clobber the "unqualified" type previously entered into
11227              DECL_SPECS with the new, improved protocol-qualified version.  */
11228           if (decl_specs)
11229             decl_specs->type = qual_type;
11230
11231           return qual_type;
11232         }
11233
11234       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11235                                                token->location);
11236     }
11237
11238   return type;
11239 }
11240
11241 /* Parse a type-name.
11242
11243    type-name:
11244      class-name
11245      enum-name
11246      typedef-name
11247
11248    enum-name:
11249      identifier
11250
11251    typedef-name:
11252      identifier
11253
11254    Returns a TYPE_DECL for the type.  */
11255
11256 static tree
11257 cp_parser_type_name (cp_parser* parser)
11258 {
11259   tree type_decl;
11260
11261   /* We can't know yet whether it is a class-name or not.  */
11262   cp_parser_parse_tentatively (parser);
11263   /* Try a class-name.  */
11264   type_decl = cp_parser_class_name (parser,
11265                                     /*typename_keyword_p=*/false,
11266                                     /*template_keyword_p=*/false,
11267                                     none_type,
11268                                     /*check_dependency_p=*/true,
11269                                     /*class_head_p=*/false,
11270                                     /*is_declaration=*/false);
11271   /* If it's not a class-name, keep looking.  */
11272   if (!cp_parser_parse_definitely (parser))
11273     {
11274       /* It must be a typedef-name or an enum-name.  */
11275       return cp_parser_nonclass_name (parser);
11276     }
11277
11278   return type_decl;
11279 }
11280
11281 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11282
11283    enum-name:
11284      identifier
11285
11286    typedef-name:
11287      identifier
11288
11289    Returns a TYPE_DECL for the type.  */
11290
11291 static tree
11292 cp_parser_nonclass_name (cp_parser* parser)
11293 {
11294   tree type_decl;
11295   tree identifier;
11296
11297   cp_token *token = cp_lexer_peek_token (parser->lexer);
11298   identifier = cp_parser_identifier (parser);
11299   if (identifier == error_mark_node)
11300     return error_mark_node;
11301
11302   /* Look up the type-name.  */
11303   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11304
11305   if (TREE_CODE (type_decl) != TYPE_DECL
11306       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11307     {
11308       /* See if this is an Objective-C type.  */
11309       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11310       tree type = objc_get_protocol_qualified_type (identifier, protos);
11311       if (type)
11312         type_decl = TYPE_NAME (type);
11313     }
11314   
11315   /* Issue an error if we did not find a type-name.  */
11316   if (TREE_CODE (type_decl) != TYPE_DECL)
11317     {
11318       if (!cp_parser_simulate_error (parser))
11319         cp_parser_name_lookup_error (parser, identifier, type_decl,
11320                                      "is not a type", token->location);
11321       return error_mark_node;
11322     }
11323   /* Remember that the name was used in the definition of the
11324      current class so that we can check later to see if the
11325      meaning would have been different after the class was
11326      entirely defined.  */
11327   else if (type_decl != error_mark_node
11328            && !parser->scope)
11329     maybe_note_name_used_in_class (identifier, type_decl);
11330   
11331   return type_decl;
11332 }
11333
11334 /* Parse an elaborated-type-specifier.  Note that the grammar given
11335    here incorporates the resolution to DR68.
11336
11337    elaborated-type-specifier:
11338      class-key :: [opt] nested-name-specifier [opt] identifier
11339      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11340      enum-key :: [opt] nested-name-specifier [opt] identifier
11341      typename :: [opt] nested-name-specifier identifier
11342      typename :: [opt] nested-name-specifier template [opt]
11343        template-id
11344
11345    GNU extension:
11346
11347    elaborated-type-specifier:
11348      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11349      class-key attributes :: [opt] nested-name-specifier [opt]
11350                template [opt] template-id
11351      enum attributes :: [opt] nested-name-specifier [opt] identifier
11352
11353    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11354    declared `friend'.  If IS_DECLARATION is TRUE, then this
11355    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11356    something is being declared.
11357
11358    Returns the TYPE specified.  */
11359
11360 static tree
11361 cp_parser_elaborated_type_specifier (cp_parser* parser,
11362                                      bool is_friend,
11363                                      bool is_declaration)
11364 {
11365   enum tag_types tag_type;
11366   tree identifier;
11367   tree type = NULL_TREE;
11368   tree attributes = NULL_TREE;
11369   cp_token *token = NULL;
11370
11371   /* See if we're looking at the `enum' keyword.  */
11372   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11373     {
11374       /* Consume the `enum' token.  */
11375       cp_lexer_consume_token (parser->lexer);
11376       /* Remember that it's an enumeration type.  */
11377       tag_type = enum_type;
11378       /* Parse the optional `struct' or `class' key (for C++0x scoped
11379          enums).  */
11380       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11381           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11382         {
11383           if (cxx_dialect == cxx98)
11384             maybe_warn_cpp0x ("scoped enums");
11385
11386           /* Consume the `struct' or `class'.  */
11387           cp_lexer_consume_token (parser->lexer);
11388         }
11389       /* Parse the attributes.  */
11390       attributes = cp_parser_attributes_opt (parser);
11391     }
11392   /* Or, it might be `typename'.  */
11393   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11394                                            RID_TYPENAME))
11395     {
11396       /* Consume the `typename' token.  */
11397       cp_lexer_consume_token (parser->lexer);
11398       /* Remember that it's a `typename' type.  */
11399       tag_type = typename_type;
11400       /* The `typename' keyword is only allowed in templates.  */
11401       if (!processing_template_decl)
11402         permerror (input_location, "using %<typename%> outside of template");
11403     }
11404   /* Otherwise it must be a class-key.  */
11405   else
11406     {
11407       tag_type = cp_parser_class_key (parser);
11408       if (tag_type == none_type)
11409         return error_mark_node;
11410       /* Parse the attributes.  */
11411       attributes = cp_parser_attributes_opt (parser);
11412     }
11413
11414   /* Look for the `::' operator.  */
11415   cp_parser_global_scope_opt (parser,
11416                               /*current_scope_valid_p=*/false);
11417   /* Look for the nested-name-specifier.  */
11418   if (tag_type == typename_type)
11419     {
11420       if (!cp_parser_nested_name_specifier (parser,
11421                                            /*typename_keyword_p=*/true,
11422                                            /*check_dependency_p=*/true,
11423                                            /*type_p=*/true,
11424                                             is_declaration))
11425         return error_mark_node;
11426     }
11427   else
11428     /* Even though `typename' is not present, the proposed resolution
11429        to Core Issue 180 says that in `class A<T>::B', `B' should be
11430        considered a type-name, even if `A<T>' is dependent.  */
11431     cp_parser_nested_name_specifier_opt (parser,
11432                                          /*typename_keyword_p=*/true,
11433                                          /*check_dependency_p=*/true,
11434                                          /*type_p=*/true,
11435                                          is_declaration);
11436  /* For everything but enumeration types, consider a template-id.
11437     For an enumeration type, consider only a plain identifier.  */
11438   if (tag_type != enum_type)
11439     {
11440       bool template_p = false;
11441       tree decl;
11442
11443       /* Allow the `template' keyword.  */
11444       template_p = cp_parser_optional_template_keyword (parser);
11445       /* If we didn't see `template', we don't know if there's a
11446          template-id or not.  */
11447       if (!template_p)
11448         cp_parser_parse_tentatively (parser);
11449       /* Parse the template-id.  */
11450       token = cp_lexer_peek_token (parser->lexer);
11451       decl = cp_parser_template_id (parser, template_p,
11452                                     /*check_dependency_p=*/true,
11453                                     is_declaration);
11454       /* If we didn't find a template-id, look for an ordinary
11455          identifier.  */
11456       if (!template_p && !cp_parser_parse_definitely (parser))
11457         ;
11458       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11459          in effect, then we must assume that, upon instantiation, the
11460          template will correspond to a class.  */
11461       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11462                && tag_type == typename_type)
11463         type = make_typename_type (parser->scope, decl,
11464                                    typename_type,
11465                                    /*complain=*/tf_error);
11466       else
11467         type = TREE_TYPE (decl);
11468     }
11469
11470   if (!type)
11471     {
11472       token = cp_lexer_peek_token (parser->lexer);
11473       identifier = cp_parser_identifier (parser);
11474
11475       if (identifier == error_mark_node)
11476         {
11477           parser->scope = NULL_TREE;
11478           return error_mark_node;
11479         }
11480
11481       /* For a `typename', we needn't call xref_tag.  */
11482       if (tag_type == typename_type
11483           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11484         return cp_parser_make_typename_type (parser, parser->scope,
11485                                              identifier,
11486                                              token->location);
11487       /* Look up a qualified name in the usual way.  */
11488       if (parser->scope)
11489         {
11490           tree decl;
11491           tree ambiguous_decls;
11492
11493           decl = cp_parser_lookup_name (parser, identifier,
11494                                         tag_type,
11495                                         /*is_template=*/false,
11496                                         /*is_namespace=*/false,
11497                                         /*check_dependency=*/true,
11498                                         &ambiguous_decls,
11499                                         token->location);
11500
11501           /* If the lookup was ambiguous, an error will already have been
11502              issued.  */
11503           if (ambiguous_decls)
11504             return error_mark_node;
11505
11506           /* If we are parsing friend declaration, DECL may be a
11507              TEMPLATE_DECL tree node here.  However, we need to check
11508              whether this TEMPLATE_DECL results in valid code.  Consider
11509              the following example:
11510
11511                namespace N {
11512                  template <class T> class C {};
11513                }
11514                class X {
11515                  template <class T> friend class N::C; // #1, valid code
11516                };
11517                template <class T> class Y {
11518                  friend class N::C;                    // #2, invalid code
11519                };
11520
11521              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11522              name lookup of `N::C'.  We see that friend declaration must
11523              be template for the code to be valid.  Note that
11524              processing_template_decl does not work here since it is
11525              always 1 for the above two cases.  */
11526
11527           decl = (cp_parser_maybe_treat_template_as_class
11528                   (decl, /*tag_name_p=*/is_friend
11529                          && parser->num_template_parameter_lists));
11530
11531           if (TREE_CODE (decl) != TYPE_DECL)
11532             {
11533               cp_parser_diagnose_invalid_type_name (parser,
11534                                                     parser->scope,
11535                                                     identifier,
11536                                                     token->location);
11537               return error_mark_node;
11538             }
11539
11540           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11541             {
11542               bool allow_template = (parser->num_template_parameter_lists
11543                                       || DECL_SELF_REFERENCE_P (decl));
11544               type = check_elaborated_type_specifier (tag_type, decl, 
11545                                                       allow_template);
11546
11547               if (type == error_mark_node)
11548                 return error_mark_node;
11549             }
11550
11551           /* Forward declarations of nested types, such as
11552
11553                class C1::C2;
11554                class C1::C2::C3;
11555
11556              are invalid unless all components preceding the final '::'
11557              are complete.  If all enclosing types are complete, these
11558              declarations become merely pointless.
11559
11560              Invalid forward declarations of nested types are errors
11561              caught elsewhere in parsing.  Those that are pointless arrive
11562              here.  */
11563
11564           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11565               && !is_friend && !processing_explicit_instantiation)
11566             warning (0, "declaration %qD does not declare anything", decl);
11567
11568           type = TREE_TYPE (decl);
11569         }
11570       else
11571         {
11572           /* An elaborated-type-specifier sometimes introduces a new type and
11573              sometimes names an existing type.  Normally, the rule is that it
11574              introduces a new type only if there is not an existing type of
11575              the same name already in scope.  For example, given:
11576
11577                struct S {};
11578                void f() { struct S s; }
11579
11580              the `struct S' in the body of `f' is the same `struct S' as in
11581              the global scope; the existing definition is used.  However, if
11582              there were no global declaration, this would introduce a new
11583              local class named `S'.
11584
11585              An exception to this rule applies to the following code:
11586
11587                namespace N { struct S; }
11588
11589              Here, the elaborated-type-specifier names a new type
11590              unconditionally; even if there is already an `S' in the
11591              containing scope this declaration names a new type.
11592              This exception only applies if the elaborated-type-specifier
11593              forms the complete declaration:
11594
11595                [class.name]
11596
11597                A declaration consisting solely of `class-key identifier ;' is
11598                either a redeclaration of the name in the current scope or a
11599                forward declaration of the identifier as a class name.  It
11600                introduces the name into the current scope.
11601
11602              We are in this situation precisely when the next token is a `;'.
11603
11604              An exception to the exception is that a `friend' declaration does
11605              *not* name a new type; i.e., given:
11606
11607                struct S { friend struct T; };
11608
11609              `T' is not a new type in the scope of `S'.
11610
11611              Also, `new struct S' or `sizeof (struct S)' never results in the
11612              definition of a new type; a new type can only be declared in a
11613              declaration context.  */
11614
11615           tag_scope ts;
11616           bool template_p;
11617
11618           if (is_friend)
11619             /* Friends have special name lookup rules.  */
11620             ts = ts_within_enclosing_non_class;
11621           else if (is_declaration
11622                    && cp_lexer_next_token_is (parser->lexer,
11623                                               CPP_SEMICOLON))
11624             /* This is a `class-key identifier ;' */
11625             ts = ts_current;
11626           else
11627             ts = ts_global;
11628
11629           template_p =
11630             (parser->num_template_parameter_lists
11631              && (cp_parser_next_token_starts_class_definition_p (parser)
11632                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11633           /* An unqualified name was used to reference this type, so
11634              there were no qualifying templates.  */
11635           if (!cp_parser_check_template_parameters (parser,
11636                                                     /*num_templates=*/0,
11637                                                     token->location))
11638             return error_mark_node;
11639           type = xref_tag (tag_type, identifier, ts, template_p);
11640         }
11641     }
11642
11643   if (type == error_mark_node)
11644     return error_mark_node;
11645
11646   /* Allow attributes on forward declarations of classes.  */
11647   if (attributes)
11648     {
11649       if (TREE_CODE (type) == TYPENAME_TYPE)
11650         warning (OPT_Wattributes,
11651                  "attributes ignored on uninstantiated type");
11652       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11653                && ! processing_explicit_instantiation)
11654         warning (OPT_Wattributes,
11655                  "attributes ignored on template instantiation");
11656       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11657         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11658       else
11659         warning (OPT_Wattributes,
11660                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11661     }
11662
11663   if (tag_type != enum_type)
11664     cp_parser_check_class_key (tag_type, type);
11665
11666   /* A "<" cannot follow an elaborated type specifier.  If that
11667      happens, the user was probably trying to form a template-id.  */
11668   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11669
11670   return type;
11671 }
11672
11673 /* Parse an enum-specifier.
11674
11675    enum-specifier:
11676      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11677
11678    enum-key:
11679      enum
11680      enum class   [C++0x]
11681      enum struct  [C++0x]
11682
11683    enum-base:   [C++0x]
11684      : type-specifier-seq
11685
11686    GNU Extensions:
11687      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11688        { enumerator-list [opt] }attributes[opt]
11689
11690    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11691    if the token stream isn't an enum-specifier after all.  */
11692
11693 static tree
11694 cp_parser_enum_specifier (cp_parser* parser)
11695 {
11696   tree identifier;
11697   tree type;
11698   tree attributes;
11699   bool scoped_enum_p = false;
11700   tree underlying_type = NULL_TREE;
11701
11702   /* Parse tentatively so that we can back up if we don't find a
11703      enum-specifier.  */
11704   cp_parser_parse_tentatively (parser);
11705
11706   /* Caller guarantees that the current token is 'enum', an identifier
11707      possibly follows, and the token after that is an opening brace.
11708      If we don't have an identifier, fabricate an anonymous name for
11709      the enumeration being defined.  */
11710   cp_lexer_consume_token (parser->lexer);
11711
11712   /* Parse the "class" or "struct", which indicates a scoped
11713      enumeration type in C++0x.  */
11714   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11715       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11716     {
11717       if (cxx_dialect == cxx98)
11718         maybe_warn_cpp0x ("scoped enums");
11719
11720       /* Consume the `struct' or `class' token.  */
11721       cp_lexer_consume_token (parser->lexer);
11722
11723       scoped_enum_p = true;
11724     }
11725       
11726   attributes = cp_parser_attributes_opt (parser);
11727
11728   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11729     identifier = cp_parser_identifier (parser);
11730   else
11731     identifier = make_anon_name ();
11732
11733   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11734   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11735     {
11736       cp_decl_specifier_seq type_specifiers;
11737
11738       if (cxx_dialect == cxx98)
11739         maybe_warn_cpp0x ("scoped enums");
11740
11741       /* Consume the `:'.  */
11742       cp_lexer_consume_token (parser->lexer);
11743
11744       /* Parse the type-specifier-seq.  */
11745       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11746                                     &type_specifiers);
11747       if (type_specifiers.type == error_mark_node)
11748         return error_mark_node;
11749      
11750       /* If that didn't work, stop.  */
11751       if (type_specifiers.type != error_mark_node)
11752         {
11753           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11754                                             /*initialized=*/0, NULL);
11755           if (underlying_type == error_mark_node)
11756             underlying_type = NULL_TREE;
11757         }
11758       else
11759         cp_parser_error (parser, "expected underlying type of enumeration");
11760     }
11761
11762   /* Look for the `{' but don't consume it yet.  */
11763   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11764     cp_parser_simulate_error (parser);
11765
11766   if (!cp_parser_parse_definitely (parser))
11767     return NULL_TREE;
11768
11769   /* Issue an error message if type-definitions are forbidden here.  */
11770   if (!cp_parser_check_type_definition (parser))
11771     type = error_mark_node;
11772   else
11773     /* Create the new type.  We do this before consuming the opening
11774        brace so the enum will be recorded as being on the line of its
11775        tag (or the 'enum' keyword, if there is no tag).  */
11776     type = start_enum (identifier, underlying_type, scoped_enum_p);
11777   
11778   /* Consume the opening brace.  */
11779   cp_lexer_consume_token (parser->lexer);
11780
11781   if (type == error_mark_node)
11782     {
11783       cp_parser_skip_to_end_of_block_or_statement (parser);
11784       return error_mark_node;
11785     }
11786
11787   /* If the next token is not '}', then there are some enumerators.  */
11788   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11789     cp_parser_enumerator_list (parser, type);
11790
11791   /* Consume the final '}'.  */
11792   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11793
11794   /* Look for trailing attributes to apply to this enumeration, and
11795      apply them if appropriate.  */
11796   if (cp_parser_allow_gnu_extensions_p (parser))
11797     {
11798       tree trailing_attr = cp_parser_attributes_opt (parser);
11799       cplus_decl_attributes (&type,
11800                              trailing_attr,
11801                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11802     }
11803
11804   /* Finish up the enumeration.  */
11805   finish_enum (type);
11806
11807   return type;
11808 }
11809
11810 /* Parse an enumerator-list.  The enumerators all have the indicated
11811    TYPE.
11812
11813    enumerator-list:
11814      enumerator-definition
11815      enumerator-list , enumerator-definition  */
11816
11817 static void
11818 cp_parser_enumerator_list (cp_parser* parser, tree type)
11819 {
11820   while (true)
11821     {
11822       /* Parse an enumerator-definition.  */
11823       cp_parser_enumerator_definition (parser, type);
11824
11825       /* If the next token is not a ',', we've reached the end of
11826          the list.  */
11827       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11828         break;
11829       /* Otherwise, consume the `,' and keep going.  */
11830       cp_lexer_consume_token (parser->lexer);
11831       /* If the next token is a `}', there is a trailing comma.  */
11832       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11833         {
11834           if (!in_system_header)
11835             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11836           break;
11837         }
11838     }
11839 }
11840
11841 /* Parse an enumerator-definition.  The enumerator has the indicated
11842    TYPE.
11843
11844    enumerator-definition:
11845      enumerator
11846      enumerator = constant-expression
11847
11848    enumerator:
11849      identifier  */
11850
11851 static void
11852 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11853 {
11854   tree identifier;
11855   tree value;
11856
11857   /* Look for the identifier.  */
11858   identifier = cp_parser_identifier (parser);
11859   if (identifier == error_mark_node)
11860     return;
11861
11862   /* If the next token is an '=', then there is an explicit value.  */
11863   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11864     {
11865       /* Consume the `=' token.  */
11866       cp_lexer_consume_token (parser->lexer);
11867       /* Parse the value.  */
11868       value = cp_parser_constant_expression (parser,
11869                                              /*allow_non_constant_p=*/false,
11870                                              NULL);
11871     }
11872   else
11873     value = NULL_TREE;
11874
11875   /* Create the enumerator.  */
11876   build_enumerator (identifier, value, type);
11877 }
11878
11879 /* Parse a namespace-name.
11880
11881    namespace-name:
11882      original-namespace-name
11883      namespace-alias
11884
11885    Returns the NAMESPACE_DECL for the namespace.  */
11886
11887 static tree
11888 cp_parser_namespace_name (cp_parser* parser)
11889 {
11890   tree identifier;
11891   tree namespace_decl;
11892
11893   cp_token *token = cp_lexer_peek_token (parser->lexer);
11894
11895   /* Get the name of the namespace.  */
11896   identifier = cp_parser_identifier (parser);
11897   if (identifier == error_mark_node)
11898     return error_mark_node;
11899
11900   /* Look up the identifier in the currently active scope.  Look only
11901      for namespaces, due to:
11902
11903        [basic.lookup.udir]
11904
11905        When looking up a namespace-name in a using-directive or alias
11906        definition, only namespace names are considered.
11907
11908      And:
11909
11910        [basic.lookup.qual]
11911
11912        During the lookup of a name preceding the :: scope resolution
11913        operator, object, function, and enumerator names are ignored.
11914
11915      (Note that cp_parser_qualifying_entity only calls this
11916      function if the token after the name is the scope resolution
11917      operator.)  */
11918   namespace_decl = cp_parser_lookup_name (parser, identifier,
11919                                           none_type,
11920                                           /*is_template=*/false,
11921                                           /*is_namespace=*/true,
11922                                           /*check_dependency=*/true,
11923                                           /*ambiguous_decls=*/NULL,
11924                                           token->location);
11925   /* If it's not a namespace, issue an error.  */
11926   if (namespace_decl == error_mark_node
11927       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11928     {
11929       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11930         error ("%H%qD is not a namespace-name", &token->location, identifier);
11931       cp_parser_error (parser, "expected namespace-name");
11932       namespace_decl = error_mark_node;
11933     }
11934
11935   return namespace_decl;
11936 }
11937
11938 /* Parse a namespace-definition.
11939
11940    namespace-definition:
11941      named-namespace-definition
11942      unnamed-namespace-definition
11943
11944    named-namespace-definition:
11945      original-namespace-definition
11946      extension-namespace-definition
11947
11948    original-namespace-definition:
11949      namespace identifier { namespace-body }
11950
11951    extension-namespace-definition:
11952      namespace original-namespace-name { namespace-body }
11953
11954    unnamed-namespace-definition:
11955      namespace { namespace-body } */
11956
11957 static void
11958 cp_parser_namespace_definition (cp_parser* parser)
11959 {
11960   tree identifier, attribs;
11961   bool has_visibility;
11962   bool is_inline;
11963
11964   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11965     {
11966       is_inline = true;
11967       cp_lexer_consume_token (parser->lexer);
11968     }
11969   else
11970     is_inline = false;
11971
11972   /* Look for the `namespace' keyword.  */
11973   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11974
11975   /* Get the name of the namespace.  We do not attempt to distinguish
11976      between an original-namespace-definition and an
11977      extension-namespace-definition at this point.  The semantic
11978      analysis routines are responsible for that.  */
11979   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11980     identifier = cp_parser_identifier (parser);
11981   else
11982     identifier = NULL_TREE;
11983
11984   /* Parse any specified attributes.  */
11985   attribs = cp_parser_attributes_opt (parser);
11986
11987   /* Look for the `{' to start the namespace.  */
11988   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11989   /* Start the namespace.  */
11990   push_namespace (identifier);
11991
11992   /* "inline namespace" is equivalent to a stub namespace definition
11993      followed by a strong using directive.  */
11994   if (is_inline)
11995     {
11996       tree name_space = current_namespace;
11997       /* Set up namespace association.  */
11998       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11999         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12000                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12001       /* Import the contents of the inline namespace.  */
12002       pop_namespace ();
12003       do_using_directive (name_space);
12004       push_namespace (identifier);
12005     }
12006
12007   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12008
12009   /* Parse the body of the namespace.  */
12010   cp_parser_namespace_body (parser);
12011
12012 #ifdef HANDLE_PRAGMA_VISIBILITY
12013   if (has_visibility)
12014     pop_visibility ();
12015 #endif
12016
12017   /* Finish the namespace.  */
12018   pop_namespace ();
12019   /* Look for the final `}'.  */
12020   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12021 }
12022
12023 /* Parse a namespace-body.
12024
12025    namespace-body:
12026      declaration-seq [opt]  */
12027
12028 static void
12029 cp_parser_namespace_body (cp_parser* parser)
12030 {
12031   cp_parser_declaration_seq_opt (parser);
12032 }
12033
12034 /* Parse a namespace-alias-definition.
12035
12036    namespace-alias-definition:
12037      namespace identifier = qualified-namespace-specifier ;  */
12038
12039 static void
12040 cp_parser_namespace_alias_definition (cp_parser* parser)
12041 {
12042   tree identifier;
12043   tree namespace_specifier;
12044
12045   cp_token *token = cp_lexer_peek_token (parser->lexer);
12046
12047   /* Look for the `namespace' keyword.  */
12048   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12049   /* Look for the identifier.  */
12050   identifier = cp_parser_identifier (parser);
12051   if (identifier == error_mark_node)
12052     return;
12053   /* Look for the `=' token.  */
12054   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12055       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12056     {
12057       error ("%H%<namespace%> definition is not allowed here", &token->location);
12058       /* Skip the definition.  */
12059       cp_lexer_consume_token (parser->lexer);
12060       if (cp_parser_skip_to_closing_brace (parser))
12061         cp_lexer_consume_token (parser->lexer);
12062       return;
12063     }
12064   cp_parser_require (parser, CPP_EQ, "%<=%>");
12065   /* Look for the qualified-namespace-specifier.  */
12066   namespace_specifier
12067     = cp_parser_qualified_namespace_specifier (parser);
12068   /* Look for the `;' token.  */
12069   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12070
12071   /* Register the alias in the symbol table.  */
12072   do_namespace_alias (identifier, namespace_specifier);
12073 }
12074
12075 /* Parse a qualified-namespace-specifier.
12076
12077    qualified-namespace-specifier:
12078      :: [opt] nested-name-specifier [opt] namespace-name
12079
12080    Returns a NAMESPACE_DECL corresponding to the specified
12081    namespace.  */
12082
12083 static tree
12084 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12085 {
12086   /* Look for the optional `::'.  */
12087   cp_parser_global_scope_opt (parser,
12088                               /*current_scope_valid_p=*/false);
12089
12090   /* Look for the optional nested-name-specifier.  */
12091   cp_parser_nested_name_specifier_opt (parser,
12092                                        /*typename_keyword_p=*/false,
12093                                        /*check_dependency_p=*/true,
12094                                        /*type_p=*/false,
12095                                        /*is_declaration=*/true);
12096
12097   return cp_parser_namespace_name (parser);
12098 }
12099
12100 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12101    access declaration.
12102
12103    using-declaration:
12104      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12105      using :: unqualified-id ;  
12106
12107    access-declaration:
12108      qualified-id ;  
12109
12110    */
12111
12112 static bool
12113 cp_parser_using_declaration (cp_parser* parser, 
12114                              bool access_declaration_p)
12115 {
12116   cp_token *token;
12117   bool typename_p = false;
12118   bool global_scope_p;
12119   tree decl;
12120   tree identifier;
12121   tree qscope;
12122
12123   if (access_declaration_p)
12124     cp_parser_parse_tentatively (parser);
12125   else
12126     {
12127       /* Look for the `using' keyword.  */
12128       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12129       
12130       /* Peek at the next token.  */
12131       token = cp_lexer_peek_token (parser->lexer);
12132       /* See if it's `typename'.  */
12133       if (token->keyword == RID_TYPENAME)
12134         {
12135           /* Remember that we've seen it.  */
12136           typename_p = true;
12137           /* Consume the `typename' token.  */
12138           cp_lexer_consume_token (parser->lexer);
12139         }
12140     }
12141
12142   /* Look for the optional global scope qualification.  */
12143   global_scope_p
12144     = (cp_parser_global_scope_opt (parser,
12145                                    /*current_scope_valid_p=*/false)
12146        != NULL_TREE);
12147
12148   /* If we saw `typename', or didn't see `::', then there must be a
12149      nested-name-specifier present.  */
12150   if (typename_p || !global_scope_p)
12151     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12152                                               /*check_dependency_p=*/true,
12153                                               /*type_p=*/false,
12154                                               /*is_declaration=*/true);
12155   /* Otherwise, we could be in either of the two productions.  In that
12156      case, treat the nested-name-specifier as optional.  */
12157   else
12158     qscope = cp_parser_nested_name_specifier_opt (parser,
12159                                                   /*typename_keyword_p=*/false,
12160                                                   /*check_dependency_p=*/true,
12161                                                   /*type_p=*/false,
12162                                                   /*is_declaration=*/true);
12163   if (!qscope)
12164     qscope = global_namespace;
12165
12166   if (access_declaration_p && cp_parser_error_occurred (parser))
12167     /* Something has already gone wrong; there's no need to parse
12168        further.  Since an error has occurred, the return value of
12169        cp_parser_parse_definitely will be false, as required.  */
12170     return cp_parser_parse_definitely (parser);
12171
12172   token = cp_lexer_peek_token (parser->lexer);
12173   /* Parse the unqualified-id.  */
12174   identifier = cp_parser_unqualified_id (parser,
12175                                          /*template_keyword_p=*/false,
12176                                          /*check_dependency_p=*/true,
12177                                          /*declarator_p=*/true,
12178                                          /*optional_p=*/false);
12179
12180   if (access_declaration_p)
12181     {
12182       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12183         cp_parser_simulate_error (parser);
12184       if (!cp_parser_parse_definitely (parser))
12185         return false;
12186     }
12187
12188   /* The function we call to handle a using-declaration is different
12189      depending on what scope we are in.  */
12190   if (qscope == error_mark_node || identifier == error_mark_node)
12191     ;
12192   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12193            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12194     /* [namespace.udecl]
12195
12196        A using declaration shall not name a template-id.  */
12197     error ("%Ha template-id may not appear in a using-declaration",
12198             &token->location);
12199   else
12200     {
12201       if (at_class_scope_p ())
12202         {
12203           /* Create the USING_DECL.  */
12204           decl = do_class_using_decl (parser->scope, identifier);
12205
12206           if (check_for_bare_parameter_packs (decl))
12207             return false;
12208           else
12209             /* Add it to the list of members in this class.  */
12210             finish_member_declaration (decl);
12211         }
12212       else
12213         {
12214           decl = cp_parser_lookup_name_simple (parser,
12215                                                identifier,
12216                                                token->location);
12217           if (decl == error_mark_node)
12218             cp_parser_name_lookup_error (parser, identifier,
12219                                          decl, NULL,
12220                                          token->location);
12221           else if (check_for_bare_parameter_packs (decl))
12222             return false;
12223           else if (!at_namespace_scope_p ())
12224             do_local_using_decl (decl, qscope, identifier);
12225           else
12226             do_toplevel_using_decl (decl, qscope, identifier);
12227         }
12228     }
12229
12230   /* Look for the final `;'.  */
12231   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12232   
12233   return true;
12234 }
12235
12236 /* Parse a using-directive.
12237
12238    using-directive:
12239      using namespace :: [opt] nested-name-specifier [opt]
12240        namespace-name ;  */
12241
12242 static void
12243 cp_parser_using_directive (cp_parser* parser)
12244 {
12245   tree namespace_decl;
12246   tree attribs;
12247
12248   /* Look for the `using' keyword.  */
12249   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12250   /* And the `namespace' keyword.  */
12251   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12252   /* Look for the optional `::' operator.  */
12253   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12254   /* And the optional nested-name-specifier.  */
12255   cp_parser_nested_name_specifier_opt (parser,
12256                                        /*typename_keyword_p=*/false,
12257                                        /*check_dependency_p=*/true,
12258                                        /*type_p=*/false,
12259                                        /*is_declaration=*/true);
12260   /* Get the namespace being used.  */
12261   namespace_decl = cp_parser_namespace_name (parser);
12262   /* And any specified attributes.  */
12263   attribs = cp_parser_attributes_opt (parser);
12264   /* Update the symbol table.  */
12265   parse_using_directive (namespace_decl, attribs);
12266   /* Look for the final `;'.  */
12267   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12268 }
12269
12270 /* Parse an asm-definition.
12271
12272    asm-definition:
12273      asm ( string-literal ) ;
12274
12275    GNU Extension:
12276
12277    asm-definition:
12278      asm volatile [opt] ( string-literal ) ;
12279      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12280      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12281                           : asm-operand-list [opt] ) ;
12282      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12283                           : asm-operand-list [opt]
12284                           : asm-operand-list [opt] ) ;  */
12285
12286 static void
12287 cp_parser_asm_definition (cp_parser* parser)
12288 {
12289   tree string;
12290   tree outputs = NULL_TREE;
12291   tree inputs = NULL_TREE;
12292   tree clobbers = NULL_TREE;
12293   tree asm_stmt;
12294   bool volatile_p = false;
12295   bool extended_p = false;
12296   bool invalid_inputs_p = false;
12297   bool invalid_outputs_p = false;
12298
12299   /* Look for the `asm' keyword.  */
12300   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12301   /* See if the next token is `volatile'.  */
12302   if (cp_parser_allow_gnu_extensions_p (parser)
12303       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12304     {
12305       /* Remember that we saw the `volatile' keyword.  */
12306       volatile_p = true;
12307       /* Consume the token.  */
12308       cp_lexer_consume_token (parser->lexer);
12309     }
12310   /* Look for the opening `('.  */
12311   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12312     return;
12313   /* Look for the string.  */
12314   string = cp_parser_string_literal (parser, false, false);
12315   if (string == error_mark_node)
12316     {
12317       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12318                                              /*consume_paren=*/true);
12319       return;
12320     }
12321
12322   /* If we're allowing GNU extensions, check for the extended assembly
12323      syntax.  Unfortunately, the `:' tokens need not be separated by
12324      a space in C, and so, for compatibility, we tolerate that here
12325      too.  Doing that means that we have to treat the `::' operator as
12326      two `:' tokens.  */
12327   if (cp_parser_allow_gnu_extensions_p (parser)
12328       && parser->in_function_body
12329       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12330           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12331     {
12332       bool inputs_p = false;
12333       bool clobbers_p = false;
12334
12335       /* The extended syntax was used.  */
12336       extended_p = true;
12337
12338       /* Look for outputs.  */
12339       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12340         {
12341           /* Consume the `:'.  */
12342           cp_lexer_consume_token (parser->lexer);
12343           /* Parse the output-operands.  */
12344           if (cp_lexer_next_token_is_not (parser->lexer,
12345                                           CPP_COLON)
12346               && cp_lexer_next_token_is_not (parser->lexer,
12347                                              CPP_SCOPE)
12348               && cp_lexer_next_token_is_not (parser->lexer,
12349                                              CPP_CLOSE_PAREN))
12350             outputs = cp_parser_asm_operand_list (parser);
12351
12352             if (outputs == error_mark_node)
12353               invalid_outputs_p = true;
12354         }
12355       /* If the next token is `::', there are no outputs, and the
12356          next token is the beginning of the inputs.  */
12357       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12358         /* The inputs are coming next.  */
12359         inputs_p = true;
12360
12361       /* Look for inputs.  */
12362       if (inputs_p
12363           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12364         {
12365           /* Consume the `:' or `::'.  */
12366           cp_lexer_consume_token (parser->lexer);
12367           /* Parse the output-operands.  */
12368           if (cp_lexer_next_token_is_not (parser->lexer,
12369                                           CPP_COLON)
12370               && cp_lexer_next_token_is_not (parser->lexer,
12371                                              CPP_CLOSE_PAREN))
12372             inputs = cp_parser_asm_operand_list (parser);
12373
12374             if (inputs == error_mark_node)
12375               invalid_inputs_p = true;
12376         }
12377       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12378         /* The clobbers are coming next.  */
12379         clobbers_p = true;
12380
12381       /* Look for clobbers.  */
12382       if (clobbers_p
12383           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12384         {
12385           /* Consume the `:' or `::'.  */
12386           cp_lexer_consume_token (parser->lexer);
12387           /* Parse the clobbers.  */
12388           if (cp_lexer_next_token_is_not (parser->lexer,
12389                                           CPP_CLOSE_PAREN))
12390             clobbers = cp_parser_asm_clobber_list (parser);
12391         }
12392     }
12393   /* Look for the closing `)'.  */
12394   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12395     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12396                                            /*consume_paren=*/true);
12397   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12398
12399   if (!invalid_inputs_p && !invalid_outputs_p)
12400     {
12401       /* Create the ASM_EXPR.  */
12402       if (parser->in_function_body)
12403         {
12404           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12405                                       inputs, clobbers);
12406           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12407           if (!extended_p)
12408             {
12409               tree temp = asm_stmt;
12410               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12411                 temp = TREE_OPERAND (temp, 0);
12412
12413               ASM_INPUT_P (temp) = 1;
12414             }
12415         }
12416       else
12417         cgraph_add_asm_node (string);
12418     }
12419 }
12420
12421 /* Declarators [gram.dcl.decl] */
12422
12423 /* Parse an init-declarator.
12424
12425    init-declarator:
12426      declarator initializer [opt]
12427
12428    GNU Extension:
12429
12430    init-declarator:
12431      declarator asm-specification [opt] attributes [opt] initializer [opt]
12432
12433    function-definition:
12434      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12435        function-body
12436      decl-specifier-seq [opt] declarator function-try-block
12437
12438    GNU Extension:
12439
12440    function-definition:
12441      __extension__ function-definition
12442
12443    The DECL_SPECIFIERS apply to this declarator.  Returns a
12444    representation of the entity declared.  If MEMBER_P is TRUE, then
12445    this declarator appears in a class scope.  The new DECL created by
12446    this declarator is returned.
12447
12448    The CHECKS are access checks that should be performed once we know
12449    what entity is being declared (and, therefore, what classes have
12450    befriended it).
12451
12452    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12453    for a function-definition here as well.  If the declarator is a
12454    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12455    be TRUE upon return.  By that point, the function-definition will
12456    have been completely parsed.
12457
12458    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12459    is FALSE.  */
12460
12461 static tree
12462 cp_parser_init_declarator (cp_parser* parser,
12463                            cp_decl_specifier_seq *decl_specifiers,
12464                            VEC (deferred_access_check,gc)* checks,
12465                            bool function_definition_allowed_p,
12466                            bool member_p,
12467                            int declares_class_or_enum,
12468                            bool* function_definition_p)
12469 {
12470   cp_token *token = NULL, *asm_spec_start_token = NULL,
12471            *attributes_start_token = NULL;
12472   cp_declarator *declarator;
12473   tree prefix_attributes;
12474   tree attributes;
12475   tree asm_specification;
12476   tree initializer;
12477   tree decl = NULL_TREE;
12478   tree scope;
12479   int is_initialized;
12480   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12481      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12482      "(...)".  */
12483   enum cpp_ttype initialization_kind;
12484   bool is_direct_init = false;
12485   bool is_non_constant_init;
12486   int ctor_dtor_or_conv_p;
12487   bool friend_p;
12488   tree pushed_scope = NULL;
12489
12490   /* Gather the attributes that were provided with the
12491      decl-specifiers.  */
12492   prefix_attributes = decl_specifiers->attributes;
12493
12494   /* Assume that this is not the declarator for a function
12495      definition.  */
12496   if (function_definition_p)
12497     *function_definition_p = false;
12498
12499   /* Defer access checks while parsing the declarator; we cannot know
12500      what names are accessible until we know what is being
12501      declared.  */
12502   resume_deferring_access_checks ();
12503
12504   /* Parse the declarator.  */
12505   token = cp_lexer_peek_token (parser->lexer);
12506   declarator
12507     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12508                             &ctor_dtor_or_conv_p,
12509                             /*parenthesized_p=*/NULL,
12510                             /*member_p=*/false);
12511   /* Gather up the deferred checks.  */
12512   stop_deferring_access_checks ();
12513
12514   /* If the DECLARATOR was erroneous, there's no need to go
12515      further.  */
12516   if (declarator == cp_error_declarator)
12517     return error_mark_node;
12518
12519   /* Check that the number of template-parameter-lists is OK.  */
12520   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12521                                                        token->location))
12522     return error_mark_node;
12523
12524   if (declares_class_or_enum & 2)
12525     cp_parser_check_for_definition_in_return_type (declarator,
12526                                                    decl_specifiers->type,
12527                                                    decl_specifiers->type_location);
12528
12529   /* Figure out what scope the entity declared by the DECLARATOR is
12530      located in.  `grokdeclarator' sometimes changes the scope, so
12531      we compute it now.  */
12532   scope = get_scope_of_declarator (declarator);
12533
12534   /* If we're allowing GNU extensions, look for an asm-specification
12535      and attributes.  */
12536   if (cp_parser_allow_gnu_extensions_p (parser))
12537     {
12538       /* Look for an asm-specification.  */
12539       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12540       asm_specification = cp_parser_asm_specification_opt (parser);
12541       /* And attributes.  */
12542       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12543       attributes = cp_parser_attributes_opt (parser);
12544     }
12545   else
12546     {
12547       asm_specification = NULL_TREE;
12548       attributes = NULL_TREE;
12549     }
12550
12551   /* Peek at the next token.  */
12552   token = cp_lexer_peek_token (parser->lexer);
12553   /* Check to see if the token indicates the start of a
12554      function-definition.  */
12555   if (function_declarator_p (declarator)
12556       && cp_parser_token_starts_function_definition_p (token))
12557     {
12558       if (!function_definition_allowed_p)
12559         {
12560           /* If a function-definition should not appear here, issue an
12561              error message.  */
12562           cp_parser_error (parser,
12563                            "a function-definition is not allowed here");
12564           return error_mark_node;
12565         }
12566       else
12567         {
12568           /* Neither attributes nor an asm-specification are allowed
12569              on a function-definition.  */
12570           if (asm_specification)
12571             error ("%Han asm-specification is not allowed "
12572                    "on a function-definition",
12573                    &asm_spec_start_token->location);
12574           if (attributes)
12575             error ("%Hattributes are not allowed on a function-definition",
12576                    &attributes_start_token->location);
12577           /* This is a function-definition.  */
12578           *function_definition_p = true;
12579
12580           /* Parse the function definition.  */
12581           if (member_p)
12582             decl = cp_parser_save_member_function_body (parser,
12583                                                         decl_specifiers,
12584                                                         declarator,
12585                                                         prefix_attributes);
12586           else
12587             decl
12588               = (cp_parser_function_definition_from_specifiers_and_declarator
12589                  (parser, decl_specifiers, prefix_attributes, declarator));
12590
12591           return decl;
12592         }
12593     }
12594
12595   /* [dcl.dcl]
12596
12597      Only in function declarations for constructors, destructors, and
12598      type conversions can the decl-specifier-seq be omitted.
12599
12600      We explicitly postpone this check past the point where we handle
12601      function-definitions because we tolerate function-definitions
12602      that are missing their return types in some modes.  */
12603   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12604     {
12605       cp_parser_error (parser,
12606                        "expected constructor, destructor, or type conversion");
12607       return error_mark_node;
12608     }
12609
12610   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12611   if (token->type == CPP_EQ
12612       || token->type == CPP_OPEN_PAREN
12613       || token->type == CPP_OPEN_BRACE)
12614     {
12615       is_initialized = 1;
12616       initialization_kind = token->type;
12617
12618       if (token->type == CPP_EQ
12619           && function_declarator_p (declarator))
12620         {
12621           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12622           if (t2->keyword == RID_DEFAULT)
12623             is_initialized = 2;
12624           else if (t2->keyword == RID_DELETE)
12625             is_initialized = 3;
12626         }
12627     }
12628   else
12629     {
12630       /* If the init-declarator isn't initialized and isn't followed by a
12631          `,' or `;', it's not a valid init-declarator.  */
12632       if (token->type != CPP_COMMA
12633           && token->type != CPP_SEMICOLON)
12634         {
12635           cp_parser_error (parser, "expected initializer");
12636           return error_mark_node;
12637         }
12638       is_initialized = 0;
12639       initialization_kind = CPP_EOF;
12640     }
12641
12642   /* Because start_decl has side-effects, we should only call it if we
12643      know we're going ahead.  By this point, we know that we cannot
12644      possibly be looking at any other construct.  */
12645   cp_parser_commit_to_tentative_parse (parser);
12646
12647   /* If the decl specifiers were bad, issue an error now that we're
12648      sure this was intended to be a declarator.  Then continue
12649      declaring the variable(s), as int, to try to cut down on further
12650      errors.  */
12651   if (decl_specifiers->any_specifiers_p
12652       && decl_specifiers->type == error_mark_node)
12653     {
12654       cp_parser_error (parser, "invalid type in declaration");
12655       decl_specifiers->type = integer_type_node;
12656     }
12657
12658   /* Check to see whether or not this declaration is a friend.  */
12659   friend_p = cp_parser_friend_p (decl_specifiers);
12660
12661   /* Enter the newly declared entry in the symbol table.  If we're
12662      processing a declaration in a class-specifier, we wait until
12663      after processing the initializer.  */
12664   if (!member_p)
12665     {
12666       if (parser->in_unbraced_linkage_specification_p)
12667         decl_specifiers->storage_class = sc_extern;
12668       decl = start_decl (declarator, decl_specifiers,
12669                          is_initialized, attributes, prefix_attributes,
12670                          &pushed_scope);
12671     }
12672   else if (scope)
12673     /* Enter the SCOPE.  That way unqualified names appearing in the
12674        initializer will be looked up in SCOPE.  */
12675     pushed_scope = push_scope (scope);
12676
12677   /* Perform deferred access control checks, now that we know in which
12678      SCOPE the declared entity resides.  */
12679   if (!member_p && decl)
12680     {
12681       tree saved_current_function_decl = NULL_TREE;
12682
12683       /* If the entity being declared is a function, pretend that we
12684          are in its scope.  If it is a `friend', it may have access to
12685          things that would not otherwise be accessible.  */
12686       if (TREE_CODE (decl) == FUNCTION_DECL)
12687         {
12688           saved_current_function_decl = current_function_decl;
12689           current_function_decl = decl;
12690         }
12691
12692       /* Perform access checks for template parameters.  */
12693       cp_parser_perform_template_parameter_access_checks (checks);
12694
12695       /* Perform the access control checks for the declarator and the
12696          decl-specifiers.  */
12697       perform_deferred_access_checks ();
12698
12699       /* Restore the saved value.  */
12700       if (TREE_CODE (decl) == FUNCTION_DECL)
12701         current_function_decl = saved_current_function_decl;
12702     }
12703
12704   /* Parse the initializer.  */
12705   initializer = NULL_TREE;
12706   is_direct_init = false;
12707   is_non_constant_init = true;
12708   if (is_initialized)
12709     {
12710       if (function_declarator_p (declarator))
12711         {
12712           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12713            if (initialization_kind == CPP_EQ)
12714              initializer = cp_parser_pure_specifier (parser);
12715            else
12716              {
12717                /* If the declaration was erroneous, we don't really
12718                   know what the user intended, so just silently
12719                   consume the initializer.  */
12720                if (decl != error_mark_node)
12721                  error ("%Hinitializer provided for function",
12722                         &initializer_start_token->location);
12723                cp_parser_skip_to_closing_parenthesis (parser,
12724                                                       /*recovering=*/true,
12725                                                       /*or_comma=*/false,
12726                                                       /*consume_paren=*/true);
12727              }
12728         }
12729       else
12730         initializer = cp_parser_initializer (parser,
12731                                              &is_direct_init,
12732                                              &is_non_constant_init);
12733     }
12734
12735   /* The old parser allows attributes to appear after a parenthesized
12736      initializer.  Mark Mitchell proposed removing this functionality
12737      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12738      attributes -- but ignores them.  */
12739   if (cp_parser_allow_gnu_extensions_p (parser)
12740       && initialization_kind == CPP_OPEN_PAREN)
12741     if (cp_parser_attributes_opt (parser))
12742       warning (OPT_Wattributes,
12743                "attributes after parenthesized initializer ignored");
12744
12745   /* For an in-class declaration, use `grokfield' to create the
12746      declaration.  */
12747   if (member_p)
12748     {
12749       if (pushed_scope)
12750         {
12751           pop_scope (pushed_scope);
12752           pushed_scope = false;
12753         }
12754       decl = grokfield (declarator, decl_specifiers,
12755                         initializer, !is_non_constant_init,
12756                         /*asmspec=*/NULL_TREE,
12757                         prefix_attributes);
12758       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12759         cp_parser_save_default_args (parser, decl);
12760     }
12761
12762   /* Finish processing the declaration.  But, skip friend
12763      declarations.  */
12764   if (!friend_p && decl && decl != error_mark_node)
12765     {
12766       cp_finish_decl (decl,
12767                       initializer, !is_non_constant_init,
12768                       asm_specification,
12769                       /* If the initializer is in parentheses, then this is
12770                          a direct-initialization, which means that an
12771                          `explicit' constructor is OK.  Otherwise, an
12772                          `explicit' constructor cannot be used.  */
12773                       ((is_direct_init || !is_initialized)
12774                        ? 0 : LOOKUP_ONLYCONVERTING));
12775     }
12776   else if ((cxx_dialect != cxx98) && friend_p
12777            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12778     /* Core issue #226 (C++0x only): A default template-argument
12779        shall not be specified in a friend class template
12780        declaration. */
12781     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12782                              /*is_partial=*/0, /*is_friend_decl=*/1);
12783
12784   if (!friend_p && pushed_scope)
12785     pop_scope (pushed_scope);
12786
12787   return decl;
12788 }
12789
12790 /* Parse a declarator.
12791
12792    declarator:
12793      direct-declarator
12794      ptr-operator declarator
12795
12796    abstract-declarator:
12797      ptr-operator abstract-declarator [opt]
12798      direct-abstract-declarator
12799
12800    GNU Extensions:
12801
12802    declarator:
12803      attributes [opt] direct-declarator
12804      attributes [opt] ptr-operator declarator
12805
12806    abstract-declarator:
12807      attributes [opt] ptr-operator abstract-declarator [opt]
12808      attributes [opt] direct-abstract-declarator
12809
12810    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12811    detect constructor, destructor or conversion operators. It is set
12812    to -1 if the declarator is a name, and +1 if it is a
12813    function. Otherwise it is set to zero. Usually you just want to
12814    test for >0, but internally the negative value is used.
12815
12816    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12817    a decl-specifier-seq unless it declares a constructor, destructor,
12818    or conversion.  It might seem that we could check this condition in
12819    semantic analysis, rather than parsing, but that makes it difficult
12820    to handle something like `f()'.  We want to notice that there are
12821    no decl-specifiers, and therefore realize that this is an
12822    expression, not a declaration.)
12823
12824    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12825    the declarator is a direct-declarator of the form "(...)".
12826
12827    MEMBER_P is true iff this declarator is a member-declarator.  */
12828
12829 static cp_declarator *
12830 cp_parser_declarator (cp_parser* parser,
12831                       cp_parser_declarator_kind dcl_kind,
12832                       int* ctor_dtor_or_conv_p,
12833                       bool* parenthesized_p,
12834                       bool member_p)
12835 {
12836   cp_token *token;
12837   cp_declarator *declarator;
12838   enum tree_code code;
12839   cp_cv_quals cv_quals;
12840   tree class_type;
12841   tree attributes = NULL_TREE;
12842
12843   /* Assume this is not a constructor, destructor, or type-conversion
12844      operator.  */
12845   if (ctor_dtor_or_conv_p)
12846     *ctor_dtor_or_conv_p = 0;
12847
12848   if (cp_parser_allow_gnu_extensions_p (parser))
12849     attributes = cp_parser_attributes_opt (parser);
12850
12851   /* Peek at the next token.  */
12852   token = cp_lexer_peek_token (parser->lexer);
12853
12854   /* Check for the ptr-operator production.  */
12855   cp_parser_parse_tentatively (parser);
12856   /* Parse the ptr-operator.  */
12857   code = cp_parser_ptr_operator (parser,
12858                                  &class_type,
12859                                  &cv_quals);
12860   /* If that worked, then we have a ptr-operator.  */
12861   if (cp_parser_parse_definitely (parser))
12862     {
12863       /* If a ptr-operator was found, then this declarator was not
12864          parenthesized.  */
12865       if (parenthesized_p)
12866         *parenthesized_p = true;
12867       /* The dependent declarator is optional if we are parsing an
12868          abstract-declarator.  */
12869       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12870         cp_parser_parse_tentatively (parser);
12871
12872       /* Parse the dependent declarator.  */
12873       declarator = cp_parser_declarator (parser, dcl_kind,
12874                                          /*ctor_dtor_or_conv_p=*/NULL,
12875                                          /*parenthesized_p=*/NULL,
12876                                          /*member_p=*/false);
12877
12878       /* If we are parsing an abstract-declarator, we must handle the
12879          case where the dependent declarator is absent.  */
12880       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12881           && !cp_parser_parse_definitely (parser))
12882         declarator = NULL;
12883
12884       declarator = cp_parser_make_indirect_declarator
12885         (code, class_type, cv_quals, declarator);
12886     }
12887   /* Everything else is a direct-declarator.  */
12888   else
12889     {
12890       if (parenthesized_p)
12891         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12892                                                    CPP_OPEN_PAREN);
12893       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12894                                                 ctor_dtor_or_conv_p,
12895                                                 member_p);
12896     }
12897
12898   if (attributes && declarator && declarator != cp_error_declarator)
12899     declarator->attributes = attributes;
12900
12901   return declarator;
12902 }
12903
12904 /* Parse a direct-declarator or direct-abstract-declarator.
12905
12906    direct-declarator:
12907      declarator-id
12908      direct-declarator ( parameter-declaration-clause )
12909        cv-qualifier-seq [opt]
12910        exception-specification [opt]
12911      direct-declarator [ constant-expression [opt] ]
12912      ( declarator )
12913
12914    direct-abstract-declarator:
12915      direct-abstract-declarator [opt]
12916        ( parameter-declaration-clause )
12917        cv-qualifier-seq [opt]
12918        exception-specification [opt]
12919      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12920      ( abstract-declarator )
12921
12922    Returns a representation of the declarator.  DCL_KIND is
12923    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12924    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12925    we are parsing a direct-declarator.  It is
12926    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12927    of ambiguity we prefer an abstract declarator, as per
12928    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12929    cp_parser_declarator.  */
12930
12931 static cp_declarator *
12932 cp_parser_direct_declarator (cp_parser* parser,
12933                              cp_parser_declarator_kind dcl_kind,
12934                              int* ctor_dtor_or_conv_p,
12935                              bool member_p)
12936 {
12937   cp_token *token;
12938   cp_declarator *declarator = NULL;
12939   tree scope = NULL_TREE;
12940   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12941   bool saved_in_declarator_p = parser->in_declarator_p;
12942   bool first = true;
12943   tree pushed_scope = NULL_TREE;
12944
12945   while (true)
12946     {
12947       /* Peek at the next token.  */
12948       token = cp_lexer_peek_token (parser->lexer);
12949       if (token->type == CPP_OPEN_PAREN)
12950         {
12951           /* This is either a parameter-declaration-clause, or a
12952              parenthesized declarator. When we know we are parsing a
12953              named declarator, it must be a parenthesized declarator
12954              if FIRST is true. For instance, `(int)' is a
12955              parameter-declaration-clause, with an omitted
12956              direct-abstract-declarator. But `((*))', is a
12957              parenthesized abstract declarator. Finally, when T is a
12958              template parameter `(T)' is a
12959              parameter-declaration-clause, and not a parenthesized
12960              named declarator.
12961
12962              We first try and parse a parameter-declaration-clause,
12963              and then try a nested declarator (if FIRST is true).
12964
12965              It is not an error for it not to be a
12966              parameter-declaration-clause, even when FIRST is
12967              false. Consider,
12968
12969                int i (int);
12970                int i (3);
12971
12972              The first is the declaration of a function while the
12973              second is the definition of a variable, including its
12974              initializer.
12975
12976              Having seen only the parenthesis, we cannot know which of
12977              these two alternatives should be selected.  Even more
12978              complex are examples like:
12979
12980                int i (int (a));
12981                int i (int (3));
12982
12983              The former is a function-declaration; the latter is a
12984              variable initialization.
12985
12986              Thus again, we try a parameter-declaration-clause, and if
12987              that fails, we back out and return.  */
12988
12989           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12990             {
12991               cp_parameter_declarator *params;
12992               unsigned saved_num_template_parameter_lists;
12993
12994               /* In a member-declarator, the only valid interpretation
12995                  of a parenthesis is the start of a
12996                  parameter-declaration-clause.  (It is invalid to
12997                  initialize a static data member with a parenthesized
12998                  initializer; only the "=" form of initialization is
12999                  permitted.)  */
13000               if (!member_p)
13001                 cp_parser_parse_tentatively (parser);
13002
13003               /* Consume the `('.  */
13004               cp_lexer_consume_token (parser->lexer);
13005               if (first)
13006                 {
13007                   /* If this is going to be an abstract declarator, we're
13008                      in a declarator and we can't have default args.  */
13009                   parser->default_arg_ok_p = false;
13010                   parser->in_declarator_p = true;
13011                 }
13012
13013               /* Inside the function parameter list, surrounding
13014                  template-parameter-lists do not apply.  */
13015               saved_num_template_parameter_lists
13016                 = parser->num_template_parameter_lists;
13017               parser->num_template_parameter_lists = 0;
13018
13019               /* Parse the parameter-declaration-clause.  */
13020               params = cp_parser_parameter_declaration_clause (parser);
13021
13022               parser->num_template_parameter_lists
13023                 = saved_num_template_parameter_lists;
13024
13025               /* If all went well, parse the cv-qualifier-seq and the
13026                  exception-specification.  */
13027               if (member_p || cp_parser_parse_definitely (parser))
13028                 {
13029                   cp_cv_quals cv_quals;
13030                   tree exception_specification;
13031
13032                   if (ctor_dtor_or_conv_p)
13033                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13034                   first = false;
13035                   /* Consume the `)'.  */
13036                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13037
13038                   /* Parse the cv-qualifier-seq.  */
13039                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13040                   /* And the exception-specification.  */
13041                   exception_specification
13042                     = cp_parser_exception_specification_opt (parser);
13043
13044                   /* Create the function-declarator.  */
13045                   declarator = make_call_declarator (declarator,
13046                                                      params,
13047                                                      cv_quals,
13048                                                      exception_specification);
13049                   /* Any subsequent parameter lists are to do with
13050                      return type, so are not those of the declared
13051                      function.  */
13052                   parser->default_arg_ok_p = false;
13053
13054                   /* Repeat the main loop.  */
13055                   continue;
13056                 }
13057             }
13058
13059           /* If this is the first, we can try a parenthesized
13060              declarator.  */
13061           if (first)
13062             {
13063               bool saved_in_type_id_in_expr_p;
13064
13065               parser->default_arg_ok_p = saved_default_arg_ok_p;
13066               parser->in_declarator_p = saved_in_declarator_p;
13067
13068               /* Consume the `('.  */
13069               cp_lexer_consume_token (parser->lexer);
13070               /* Parse the nested declarator.  */
13071               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13072               parser->in_type_id_in_expr_p = true;
13073               declarator
13074                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13075                                         /*parenthesized_p=*/NULL,
13076                                         member_p);
13077               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13078               first = false;
13079               /* Expect a `)'.  */
13080               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13081                 declarator = cp_error_declarator;
13082               if (declarator == cp_error_declarator)
13083                 break;
13084
13085               goto handle_declarator;
13086             }
13087           /* Otherwise, we must be done.  */
13088           else
13089             break;
13090         }
13091       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13092                && token->type == CPP_OPEN_SQUARE)
13093         {
13094           /* Parse an array-declarator.  */
13095           tree bounds;
13096
13097           if (ctor_dtor_or_conv_p)
13098             *ctor_dtor_or_conv_p = 0;
13099
13100           first = false;
13101           parser->default_arg_ok_p = false;
13102           parser->in_declarator_p = true;
13103           /* Consume the `['.  */
13104           cp_lexer_consume_token (parser->lexer);
13105           /* Peek at the next token.  */
13106           token = cp_lexer_peek_token (parser->lexer);
13107           /* If the next token is `]', then there is no
13108              constant-expression.  */
13109           if (token->type != CPP_CLOSE_SQUARE)
13110             {
13111               bool non_constant_p;
13112
13113               bounds
13114                 = cp_parser_constant_expression (parser,
13115                                                  /*allow_non_constant=*/true,
13116                                                  &non_constant_p);
13117               if (!non_constant_p)
13118                 bounds = fold_non_dependent_expr (bounds);
13119               /* Normally, the array bound must be an integral constant
13120                  expression.  However, as an extension, we allow VLAs
13121                  in function scopes.  */
13122               else if (!parser->in_function_body)
13123                 {
13124                   error ("%Harray bound is not an integer constant",
13125                          &token->location);
13126                   bounds = error_mark_node;
13127                 }
13128             }
13129           else
13130             bounds = NULL_TREE;
13131           /* Look for the closing `]'.  */
13132           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13133             {
13134               declarator = cp_error_declarator;
13135               break;
13136             }
13137
13138           declarator = make_array_declarator (declarator, bounds);
13139         }
13140       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13141         {
13142           tree qualifying_scope;
13143           tree unqualified_name;
13144           special_function_kind sfk;
13145           bool abstract_ok;
13146           bool pack_expansion_p = false;
13147           cp_token *declarator_id_start_token;
13148
13149           /* Parse a declarator-id */
13150           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13151           if (abstract_ok)
13152             {
13153               cp_parser_parse_tentatively (parser);
13154
13155               /* If we see an ellipsis, we should be looking at a
13156                  parameter pack. */
13157               if (token->type == CPP_ELLIPSIS)
13158                 {
13159                   /* Consume the `...' */
13160                   cp_lexer_consume_token (parser->lexer);
13161
13162                   pack_expansion_p = true;
13163                 }
13164             }
13165
13166           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13167           unqualified_name
13168             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13169           qualifying_scope = parser->scope;
13170           if (abstract_ok)
13171             {
13172               bool okay = false;
13173
13174               if (!unqualified_name && pack_expansion_p)
13175                 {
13176                   /* Check whether an error occurred. */
13177                   okay = !cp_parser_error_occurred (parser);
13178
13179                   /* We already consumed the ellipsis to mark a
13180                      parameter pack, but we have no way to report it,
13181                      so abort the tentative parse. We will be exiting
13182                      immediately anyway. */
13183                   cp_parser_abort_tentative_parse (parser);
13184                 }
13185               else
13186                 okay = cp_parser_parse_definitely (parser);
13187
13188               if (!okay)
13189                 unqualified_name = error_mark_node;
13190               else if (unqualified_name
13191                        && (qualifying_scope
13192                            || (TREE_CODE (unqualified_name)
13193                                != IDENTIFIER_NODE)))
13194                 {
13195                   cp_parser_error (parser, "expected unqualified-id");
13196                   unqualified_name = error_mark_node;
13197                 }
13198             }
13199
13200           if (!unqualified_name)
13201             return NULL;
13202           if (unqualified_name == error_mark_node)
13203             {
13204               declarator = cp_error_declarator;
13205               pack_expansion_p = false;
13206               declarator->parameter_pack_p = false;
13207               break;
13208             }
13209
13210           if (qualifying_scope && at_namespace_scope_p ()
13211               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13212             {
13213               /* In the declaration of a member of a template class
13214                  outside of the class itself, the SCOPE will sometimes
13215                  be a TYPENAME_TYPE.  For example, given:
13216
13217                  template <typename T>
13218                  int S<T>::R::i = 3;
13219
13220                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13221                  this context, we must resolve S<T>::R to an ordinary
13222                  type, rather than a typename type.
13223
13224                  The reason we normally avoid resolving TYPENAME_TYPEs
13225                  is that a specialization of `S' might render
13226                  `S<T>::R' not a type.  However, if `S' is
13227                  specialized, then this `i' will not be used, so there
13228                  is no harm in resolving the types here.  */
13229               tree type;
13230
13231               /* Resolve the TYPENAME_TYPE.  */
13232               type = resolve_typename_type (qualifying_scope,
13233                                             /*only_current_p=*/false);
13234               /* If that failed, the declarator is invalid.  */
13235               if (TREE_CODE (type) == TYPENAME_TYPE)
13236                 error ("%H%<%T::%E%> is not a type",
13237                        &declarator_id_start_token->location,
13238                        TYPE_CONTEXT (qualifying_scope),
13239                        TYPE_IDENTIFIER (qualifying_scope));
13240               qualifying_scope = type;
13241             }
13242
13243           sfk = sfk_none;
13244
13245           if (unqualified_name)
13246             {
13247               tree class_type;
13248
13249               if (qualifying_scope
13250                   && CLASS_TYPE_P (qualifying_scope))
13251                 class_type = qualifying_scope;
13252               else
13253                 class_type = current_class_type;
13254
13255               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13256                 {
13257                   tree name_type = TREE_TYPE (unqualified_name);
13258                   if (class_type && same_type_p (name_type, class_type))
13259                     {
13260                       if (qualifying_scope
13261                           && CLASSTYPE_USE_TEMPLATE (name_type))
13262                         {
13263                           error ("%Hinvalid use of constructor as a template",
13264                                  &declarator_id_start_token->location);
13265                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13266                                   "name the constructor in a qualified name",
13267                                   class_type,
13268                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13269                                   class_type, name_type);
13270                           declarator = cp_error_declarator;
13271                           break;
13272                         }
13273                       else
13274                         unqualified_name = constructor_name (class_type);
13275                     }
13276                   else
13277                     {
13278                       /* We do not attempt to print the declarator
13279                          here because we do not have enough
13280                          information about its original syntactic
13281                          form.  */
13282                       cp_parser_error (parser, "invalid declarator");
13283                       declarator = cp_error_declarator;
13284                       break;
13285                     }
13286                 }
13287
13288               if (class_type)
13289                 {
13290                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13291                     sfk = sfk_destructor;
13292                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13293                     sfk = sfk_conversion;
13294                   else if (/* There's no way to declare a constructor
13295                               for an anonymous type, even if the type
13296                               got a name for linkage purposes.  */
13297                            !TYPE_WAS_ANONYMOUS (class_type)
13298                            && constructor_name_p (unqualified_name,
13299                                                   class_type))
13300                     {
13301                       unqualified_name = constructor_name (class_type);
13302                       sfk = sfk_constructor;
13303                     }
13304
13305                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13306                     *ctor_dtor_or_conv_p = -1;
13307                 }
13308             }
13309           declarator = make_id_declarator (qualifying_scope,
13310                                            unqualified_name,
13311                                            sfk);
13312           declarator->id_loc = token->location;
13313           declarator->parameter_pack_p = pack_expansion_p;
13314
13315           if (pack_expansion_p)
13316             maybe_warn_variadic_templates ();
13317
13318         handle_declarator:;
13319           scope = get_scope_of_declarator (declarator);
13320           if (scope)
13321             /* Any names that appear after the declarator-id for a
13322                member are looked up in the containing scope.  */
13323             pushed_scope = push_scope (scope);
13324           parser->in_declarator_p = true;
13325           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13326               || (declarator && declarator->kind == cdk_id))
13327             /* Default args are only allowed on function
13328                declarations.  */
13329             parser->default_arg_ok_p = saved_default_arg_ok_p;
13330           else
13331             parser->default_arg_ok_p = false;
13332
13333           first = false;
13334         }
13335       /* We're done.  */
13336       else
13337         break;
13338     }
13339
13340   /* For an abstract declarator, we might wind up with nothing at this
13341      point.  That's an error; the declarator is not optional.  */
13342   if (!declarator)
13343     cp_parser_error (parser, "expected declarator");
13344
13345   /* If we entered a scope, we must exit it now.  */
13346   if (pushed_scope)
13347     pop_scope (pushed_scope);
13348
13349   parser->default_arg_ok_p = saved_default_arg_ok_p;
13350   parser->in_declarator_p = saved_in_declarator_p;
13351
13352   return declarator;
13353 }
13354
13355 /* Parse a ptr-operator.
13356
13357    ptr-operator:
13358      * cv-qualifier-seq [opt]
13359      &
13360      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13361
13362    GNU Extension:
13363
13364    ptr-operator:
13365      & cv-qualifier-seq [opt]
13366
13367    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13368    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13369    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13370    filled in with the TYPE containing the member.  *CV_QUALS is
13371    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13372    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13373    Note that the tree codes returned by this function have nothing
13374    to do with the types of trees that will be eventually be created
13375    to represent the pointer or reference type being parsed. They are
13376    just constants with suggestive names. */
13377 static enum tree_code
13378 cp_parser_ptr_operator (cp_parser* parser,
13379                         tree* type,
13380                         cp_cv_quals *cv_quals)
13381 {
13382   enum tree_code code = ERROR_MARK;
13383   cp_token *token;
13384
13385   /* Assume that it's not a pointer-to-member.  */
13386   *type = NULL_TREE;
13387   /* And that there are no cv-qualifiers.  */
13388   *cv_quals = TYPE_UNQUALIFIED;
13389
13390   /* Peek at the next token.  */
13391   token = cp_lexer_peek_token (parser->lexer);
13392
13393   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13394   if (token->type == CPP_MULT)
13395     code = INDIRECT_REF;
13396   else if (token->type == CPP_AND)
13397     code = ADDR_EXPR;
13398   else if ((cxx_dialect != cxx98) &&
13399            token->type == CPP_AND_AND) /* C++0x only */
13400     code = NON_LVALUE_EXPR;
13401
13402   if (code != ERROR_MARK)
13403     {
13404       /* Consume the `*', `&' or `&&'.  */
13405       cp_lexer_consume_token (parser->lexer);
13406
13407       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13408          `&', if we are allowing GNU extensions.  (The only qualifier
13409          that can legally appear after `&' is `restrict', but that is
13410          enforced during semantic analysis.  */
13411       if (code == INDIRECT_REF
13412           || cp_parser_allow_gnu_extensions_p (parser))
13413         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13414     }
13415   else
13416     {
13417       /* Try the pointer-to-member case.  */
13418       cp_parser_parse_tentatively (parser);
13419       /* Look for the optional `::' operator.  */
13420       cp_parser_global_scope_opt (parser,
13421                                   /*current_scope_valid_p=*/false);
13422       /* Look for the nested-name specifier.  */
13423       token = cp_lexer_peek_token (parser->lexer);
13424       cp_parser_nested_name_specifier (parser,
13425                                        /*typename_keyword_p=*/false,
13426                                        /*check_dependency_p=*/true,
13427                                        /*type_p=*/false,
13428                                        /*is_declaration=*/false);
13429       /* If we found it, and the next token is a `*', then we are
13430          indeed looking at a pointer-to-member operator.  */
13431       if (!cp_parser_error_occurred (parser)
13432           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13433         {
13434           /* Indicate that the `*' operator was used.  */
13435           code = INDIRECT_REF;
13436
13437           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13438             error ("%H%qD is a namespace", &token->location, parser->scope);
13439           else
13440             {
13441               /* The type of which the member is a member is given by the
13442                  current SCOPE.  */
13443               *type = parser->scope;
13444               /* The next name will not be qualified.  */
13445               parser->scope = NULL_TREE;
13446               parser->qualifying_scope = NULL_TREE;
13447               parser->object_scope = NULL_TREE;
13448               /* Look for the optional cv-qualifier-seq.  */
13449               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13450             }
13451         }
13452       /* If that didn't work we don't have a ptr-operator.  */
13453       if (!cp_parser_parse_definitely (parser))
13454         cp_parser_error (parser, "expected ptr-operator");
13455     }
13456
13457   return code;
13458 }
13459
13460 /* Parse an (optional) cv-qualifier-seq.
13461
13462    cv-qualifier-seq:
13463      cv-qualifier cv-qualifier-seq [opt]
13464
13465    cv-qualifier:
13466      const
13467      volatile
13468
13469    GNU Extension:
13470
13471    cv-qualifier:
13472      __restrict__
13473
13474    Returns a bitmask representing the cv-qualifiers.  */
13475
13476 static cp_cv_quals
13477 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13478 {
13479   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13480
13481   while (true)
13482     {
13483       cp_token *token;
13484       cp_cv_quals cv_qualifier;
13485
13486       /* Peek at the next token.  */
13487       token = cp_lexer_peek_token (parser->lexer);
13488       /* See if it's a cv-qualifier.  */
13489       switch (token->keyword)
13490         {
13491         case RID_CONST:
13492           cv_qualifier = TYPE_QUAL_CONST;
13493           break;
13494
13495         case RID_VOLATILE:
13496           cv_qualifier = TYPE_QUAL_VOLATILE;
13497           break;
13498
13499         case RID_RESTRICT:
13500           cv_qualifier = TYPE_QUAL_RESTRICT;
13501           break;
13502
13503         default:
13504           cv_qualifier = TYPE_UNQUALIFIED;
13505           break;
13506         }
13507
13508       if (!cv_qualifier)
13509         break;
13510
13511       if (cv_quals & cv_qualifier)
13512         {
13513           error ("%Hduplicate cv-qualifier", &token->location);
13514           cp_lexer_purge_token (parser->lexer);
13515         }
13516       else
13517         {
13518           cp_lexer_consume_token (parser->lexer);
13519           cv_quals |= cv_qualifier;
13520         }
13521     }
13522
13523   return cv_quals;
13524 }
13525
13526 /* Parse a declarator-id.
13527
13528    declarator-id:
13529      id-expression
13530      :: [opt] nested-name-specifier [opt] type-name
13531
13532    In the `id-expression' case, the value returned is as for
13533    cp_parser_id_expression if the id-expression was an unqualified-id.
13534    If the id-expression was a qualified-id, then a SCOPE_REF is
13535    returned.  The first operand is the scope (either a NAMESPACE_DECL
13536    or TREE_TYPE), but the second is still just a representation of an
13537    unqualified-id.  */
13538
13539 static tree
13540 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13541 {
13542   tree id;
13543   /* The expression must be an id-expression.  Assume that qualified
13544      names are the names of types so that:
13545
13546        template <class T>
13547        int S<T>::R::i = 3;
13548
13549      will work; we must treat `S<T>::R' as the name of a type.
13550      Similarly, assume that qualified names are templates, where
13551      required, so that:
13552
13553        template <class T>
13554        int S<T>::R<T>::i = 3;
13555
13556      will work, too.  */
13557   id = cp_parser_id_expression (parser,
13558                                 /*template_keyword_p=*/false,
13559                                 /*check_dependency_p=*/false,
13560                                 /*template_p=*/NULL,
13561                                 /*declarator_p=*/true,
13562                                 optional_p);
13563   if (id && BASELINK_P (id))
13564     id = BASELINK_FUNCTIONS (id);
13565   return id;
13566 }
13567
13568 /* Parse a type-id.
13569
13570    type-id:
13571      type-specifier-seq abstract-declarator [opt]
13572
13573    Returns the TYPE specified.  */
13574
13575 static tree
13576 cp_parser_type_id (cp_parser* parser)
13577 {
13578   cp_decl_specifier_seq type_specifier_seq;
13579   cp_declarator *abstract_declarator;
13580
13581   /* Parse the type-specifier-seq.  */
13582   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13583                                 &type_specifier_seq);
13584   if (type_specifier_seq.type == error_mark_node)
13585     return error_mark_node;
13586
13587   /* There might or might not be an abstract declarator.  */
13588   cp_parser_parse_tentatively (parser);
13589   /* Look for the declarator.  */
13590   abstract_declarator
13591     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13592                             /*parenthesized_p=*/NULL,
13593                             /*member_p=*/false);
13594   /* Check to see if there really was a declarator.  */
13595   if (!cp_parser_parse_definitely (parser))
13596     abstract_declarator = NULL;
13597
13598   return groktypename (&type_specifier_seq, abstract_declarator);
13599 }
13600
13601 /* Parse a type-specifier-seq.
13602
13603    type-specifier-seq:
13604      type-specifier type-specifier-seq [opt]
13605
13606    GNU extension:
13607
13608    type-specifier-seq:
13609      attributes type-specifier-seq [opt]
13610
13611    If IS_CONDITION is true, we are at the start of a "condition",
13612    e.g., we've just seen "if (".
13613
13614    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13615
13616 static void
13617 cp_parser_type_specifier_seq (cp_parser* parser,
13618                               bool is_condition,
13619                               cp_decl_specifier_seq *type_specifier_seq)
13620 {
13621   bool seen_type_specifier = false;
13622   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13623   cp_token *start_token = NULL;
13624
13625   /* Clear the TYPE_SPECIFIER_SEQ.  */
13626   clear_decl_specs (type_specifier_seq);
13627
13628   /* Parse the type-specifiers and attributes.  */
13629   while (true)
13630     {
13631       tree type_specifier;
13632       bool is_cv_qualifier;
13633
13634       /* Check for attributes first.  */
13635       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13636         {
13637           type_specifier_seq->attributes =
13638             chainon (type_specifier_seq->attributes,
13639                      cp_parser_attributes_opt (parser));
13640           continue;
13641         }
13642
13643       /* record the token of the beginning of the type specifier seq,
13644          for error reporting purposes*/
13645      if (!start_token)
13646        start_token = cp_lexer_peek_token (parser->lexer);
13647
13648       /* Look for the type-specifier.  */
13649       type_specifier = cp_parser_type_specifier (parser,
13650                                                  flags,
13651                                                  type_specifier_seq,
13652                                                  /*is_declaration=*/false,
13653                                                  NULL,
13654                                                  &is_cv_qualifier);
13655       if (!type_specifier)
13656         {
13657           /* If the first type-specifier could not be found, this is not a
13658              type-specifier-seq at all.  */
13659           if (!seen_type_specifier)
13660             {
13661               cp_parser_error (parser, "expected type-specifier");
13662               type_specifier_seq->type = error_mark_node;
13663               return;
13664             }
13665           /* If subsequent type-specifiers could not be found, the
13666              type-specifier-seq is complete.  */
13667           break;
13668         }
13669
13670       seen_type_specifier = true;
13671       /* The standard says that a condition can be:
13672
13673             type-specifier-seq declarator = assignment-expression
13674
13675          However, given:
13676
13677            struct S {};
13678            if (int S = ...)
13679
13680          we should treat the "S" as a declarator, not as a
13681          type-specifier.  The standard doesn't say that explicitly for
13682          type-specifier-seq, but it does say that for
13683          decl-specifier-seq in an ordinary declaration.  Perhaps it
13684          would be clearer just to allow a decl-specifier-seq here, and
13685          then add a semantic restriction that if any decl-specifiers
13686          that are not type-specifiers appear, the program is invalid.  */
13687       if (is_condition && !is_cv_qualifier)
13688         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13689     }
13690
13691   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13692 }
13693
13694 /* Parse a parameter-declaration-clause.
13695
13696    parameter-declaration-clause:
13697      parameter-declaration-list [opt] ... [opt]
13698      parameter-declaration-list , ...
13699
13700    Returns a representation for the parameter declarations.  A return
13701    value of NULL indicates a parameter-declaration-clause consisting
13702    only of an ellipsis.  */
13703
13704 static cp_parameter_declarator *
13705 cp_parser_parameter_declaration_clause (cp_parser* parser)
13706 {
13707   cp_parameter_declarator *parameters;
13708   cp_token *token;
13709   bool ellipsis_p;
13710   bool is_error;
13711
13712   /* Peek at the next token.  */
13713   token = cp_lexer_peek_token (parser->lexer);
13714   /* Check for trivial parameter-declaration-clauses.  */
13715   if (token->type == CPP_ELLIPSIS)
13716     {
13717       /* Consume the `...' token.  */
13718       cp_lexer_consume_token (parser->lexer);
13719       return NULL;
13720     }
13721   else if (token->type == CPP_CLOSE_PAREN)
13722     /* There are no parameters.  */
13723     {
13724 #ifndef NO_IMPLICIT_EXTERN_C
13725       if (in_system_header && current_class_type == NULL
13726           && current_lang_name == lang_name_c)
13727         return NULL;
13728       else
13729 #endif
13730         return no_parameters;
13731     }
13732   /* Check for `(void)', too, which is a special case.  */
13733   else if (token->keyword == RID_VOID
13734            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13735                == CPP_CLOSE_PAREN))
13736     {
13737       /* Consume the `void' token.  */
13738       cp_lexer_consume_token (parser->lexer);
13739       /* There are no parameters.  */
13740       return no_parameters;
13741     }
13742
13743   /* Parse the parameter-declaration-list.  */
13744   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13745   /* If a parse error occurred while parsing the
13746      parameter-declaration-list, then the entire
13747      parameter-declaration-clause is erroneous.  */
13748   if (is_error)
13749     return NULL;
13750
13751   /* Peek at the next token.  */
13752   token = cp_lexer_peek_token (parser->lexer);
13753   /* If it's a `,', the clause should terminate with an ellipsis.  */
13754   if (token->type == CPP_COMMA)
13755     {
13756       /* Consume the `,'.  */
13757       cp_lexer_consume_token (parser->lexer);
13758       /* Expect an ellipsis.  */
13759       ellipsis_p
13760         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13761     }
13762   /* It might also be `...' if the optional trailing `,' was
13763      omitted.  */
13764   else if (token->type == CPP_ELLIPSIS)
13765     {
13766       /* Consume the `...' token.  */
13767       cp_lexer_consume_token (parser->lexer);
13768       /* And remember that we saw it.  */
13769       ellipsis_p = true;
13770     }
13771   else
13772     ellipsis_p = false;
13773
13774   /* Finish the parameter list.  */
13775   if (parameters && ellipsis_p)
13776     parameters->ellipsis_p = true;
13777
13778   return parameters;
13779 }
13780
13781 /* Parse a parameter-declaration-list.
13782
13783    parameter-declaration-list:
13784      parameter-declaration
13785      parameter-declaration-list , parameter-declaration
13786
13787    Returns a representation of the parameter-declaration-list, as for
13788    cp_parser_parameter_declaration_clause.  However, the
13789    `void_list_node' is never appended to the list.  Upon return,
13790    *IS_ERROR will be true iff an error occurred.  */
13791
13792 static cp_parameter_declarator *
13793 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13794 {
13795   cp_parameter_declarator *parameters = NULL;
13796   cp_parameter_declarator **tail = &parameters;
13797   bool saved_in_unbraced_linkage_specification_p;
13798
13799   /* Assume all will go well.  */
13800   *is_error = false;
13801   /* The special considerations that apply to a function within an
13802      unbraced linkage specifications do not apply to the parameters
13803      to the function.  */
13804   saved_in_unbraced_linkage_specification_p 
13805     = parser->in_unbraced_linkage_specification_p;
13806   parser->in_unbraced_linkage_specification_p = false;
13807
13808   /* Look for more parameters.  */
13809   while (true)
13810     {
13811       cp_parameter_declarator *parameter;
13812       bool parenthesized_p;
13813       /* Parse the parameter.  */
13814       parameter
13815         = cp_parser_parameter_declaration (parser,
13816                                            /*template_parm_p=*/false,
13817                                            &parenthesized_p);
13818
13819       /* If a parse error occurred parsing the parameter declaration,
13820          then the entire parameter-declaration-list is erroneous.  */
13821       if (!parameter)
13822         {
13823           *is_error = true;
13824           parameters = NULL;
13825           break;
13826         }
13827       /* Add the new parameter to the list.  */
13828       *tail = parameter;
13829       tail = &parameter->next;
13830
13831       /* Peek at the next token.  */
13832       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13833           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13834           /* These are for Objective-C++ */
13835           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13836           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13837         /* The parameter-declaration-list is complete.  */
13838         break;
13839       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13840         {
13841           cp_token *token;
13842
13843           /* Peek at the next token.  */
13844           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13845           /* If it's an ellipsis, then the list is complete.  */
13846           if (token->type == CPP_ELLIPSIS)
13847             break;
13848           /* Otherwise, there must be more parameters.  Consume the
13849              `,'.  */
13850           cp_lexer_consume_token (parser->lexer);
13851           /* When parsing something like:
13852
13853                 int i(float f, double d)
13854
13855              we can tell after seeing the declaration for "f" that we
13856              are not looking at an initialization of a variable "i",
13857              but rather at the declaration of a function "i".
13858
13859              Due to the fact that the parsing of template arguments
13860              (as specified to a template-id) requires backtracking we
13861              cannot use this technique when inside a template argument
13862              list.  */
13863           if (!parser->in_template_argument_list_p
13864               && !parser->in_type_id_in_expr_p
13865               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13866               /* However, a parameter-declaration of the form
13867                  "foat(f)" (which is a valid declaration of a
13868                  parameter "f") can also be interpreted as an
13869                  expression (the conversion of "f" to "float").  */
13870               && !parenthesized_p)
13871             cp_parser_commit_to_tentative_parse (parser);
13872         }
13873       else
13874         {
13875           cp_parser_error (parser, "expected %<,%> or %<...%>");
13876           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13877             cp_parser_skip_to_closing_parenthesis (parser,
13878                                                    /*recovering=*/true,
13879                                                    /*or_comma=*/false,
13880                                                    /*consume_paren=*/false);
13881           break;
13882         }
13883     }
13884
13885   parser->in_unbraced_linkage_specification_p
13886     = saved_in_unbraced_linkage_specification_p;
13887
13888   return parameters;
13889 }
13890
13891 /* Parse a parameter declaration.
13892
13893    parameter-declaration:
13894      decl-specifier-seq ... [opt] declarator
13895      decl-specifier-seq declarator = assignment-expression
13896      decl-specifier-seq ... [opt] abstract-declarator [opt]
13897      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13898
13899    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13900    declares a template parameter.  (In that case, a non-nested `>'
13901    token encountered during the parsing of the assignment-expression
13902    is not interpreted as a greater-than operator.)
13903
13904    Returns a representation of the parameter, or NULL if an error
13905    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13906    true iff the declarator is of the form "(p)".  */
13907
13908 static cp_parameter_declarator *
13909 cp_parser_parameter_declaration (cp_parser *parser,
13910                                  bool template_parm_p,
13911                                  bool *parenthesized_p)
13912 {
13913   int declares_class_or_enum;
13914   bool greater_than_is_operator_p;
13915   cp_decl_specifier_seq decl_specifiers;
13916   cp_declarator *declarator;
13917   tree default_argument;
13918   cp_token *token = NULL, *declarator_token_start = NULL;
13919   const char *saved_message;
13920
13921   /* In a template parameter, `>' is not an operator.
13922
13923      [temp.param]
13924
13925      When parsing a default template-argument for a non-type
13926      template-parameter, the first non-nested `>' is taken as the end
13927      of the template parameter-list rather than a greater-than
13928      operator.  */
13929   greater_than_is_operator_p = !template_parm_p;
13930
13931   /* Type definitions may not appear in parameter types.  */
13932   saved_message = parser->type_definition_forbidden_message;
13933   parser->type_definition_forbidden_message
13934     = "types may not be defined in parameter types";
13935
13936   /* Parse the declaration-specifiers.  */
13937   cp_parser_decl_specifier_seq (parser,
13938                                 CP_PARSER_FLAGS_NONE,
13939                                 &decl_specifiers,
13940                                 &declares_class_or_enum);
13941   /* If an error occurred, there's no reason to attempt to parse the
13942      rest of the declaration.  */
13943   if (cp_parser_error_occurred (parser))
13944     {
13945       parser->type_definition_forbidden_message = saved_message;
13946       return NULL;
13947     }
13948
13949   /* Peek at the next token.  */
13950   token = cp_lexer_peek_token (parser->lexer);
13951
13952   /* If the next token is a `)', `,', `=', `>', or `...', then there
13953      is no declarator. However, when variadic templates are enabled,
13954      there may be a declarator following `...'.  */
13955   if (token->type == CPP_CLOSE_PAREN
13956       || token->type == CPP_COMMA
13957       || token->type == CPP_EQ
13958       || token->type == CPP_GREATER)
13959     {
13960       declarator = NULL;
13961       if (parenthesized_p)
13962         *parenthesized_p = false;
13963     }
13964   /* Otherwise, there should be a declarator.  */
13965   else
13966     {
13967       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13968       parser->default_arg_ok_p = false;
13969
13970       /* After seeing a decl-specifier-seq, if the next token is not a
13971          "(", there is no possibility that the code is a valid
13972          expression.  Therefore, if parsing tentatively, we commit at
13973          this point.  */
13974       if (!parser->in_template_argument_list_p
13975           /* In an expression context, having seen:
13976
13977                (int((char ...
13978
13979              we cannot be sure whether we are looking at a
13980              function-type (taking a "char" as a parameter) or a cast
13981              of some object of type "char" to "int".  */
13982           && !parser->in_type_id_in_expr_p
13983           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13984           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13985         cp_parser_commit_to_tentative_parse (parser);
13986       /* Parse the declarator.  */
13987       declarator_token_start = token;
13988       declarator = cp_parser_declarator (parser,
13989                                          CP_PARSER_DECLARATOR_EITHER,
13990                                          /*ctor_dtor_or_conv_p=*/NULL,
13991                                          parenthesized_p,
13992                                          /*member_p=*/false);
13993       parser->default_arg_ok_p = saved_default_arg_ok_p;
13994       /* After the declarator, allow more attributes.  */
13995       decl_specifiers.attributes
13996         = chainon (decl_specifiers.attributes,
13997                    cp_parser_attributes_opt (parser));
13998     }
13999
14000   /* If the next token is an ellipsis, and we have not seen a
14001      declarator name, and the type of the declarator contains parameter
14002      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14003      a parameter pack expansion expression. Otherwise, leave the
14004      ellipsis for a C-style variadic function. */
14005   token = cp_lexer_peek_token (parser->lexer);
14006   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14007     {
14008       tree type = decl_specifiers.type;
14009
14010       if (type && DECL_P (type))
14011         type = TREE_TYPE (type);
14012
14013       if (type
14014           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14015           && declarator_can_be_parameter_pack (declarator)
14016           && (!declarator || !declarator->parameter_pack_p)
14017           && uses_parameter_packs (type))
14018         {
14019           /* Consume the `...'. */
14020           cp_lexer_consume_token (parser->lexer);
14021           maybe_warn_variadic_templates ();
14022           
14023           /* Build a pack expansion type */
14024           if (declarator)
14025             declarator->parameter_pack_p = true;
14026           else
14027             decl_specifiers.type = make_pack_expansion (type);
14028         }
14029     }
14030
14031   /* The restriction on defining new types applies only to the type
14032      of the parameter, not to the default argument.  */
14033   parser->type_definition_forbidden_message = saved_message;
14034
14035   /* If the next token is `=', then process a default argument.  */
14036   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14037     {
14038       /* Consume the `='.  */
14039       cp_lexer_consume_token (parser->lexer);
14040
14041       /* If we are defining a class, then the tokens that make up the
14042          default argument must be saved and processed later.  */
14043       if (!template_parm_p && at_class_scope_p ()
14044           && TYPE_BEING_DEFINED (current_class_type))
14045         {
14046           unsigned depth = 0;
14047           int maybe_template_id = 0;
14048           cp_token *first_token;
14049           cp_token *token;
14050
14051           /* Add tokens until we have processed the entire default
14052              argument.  We add the range [first_token, token).  */
14053           first_token = cp_lexer_peek_token (parser->lexer);
14054           while (true)
14055             {
14056               bool done = false;
14057
14058               /* Peek at the next token.  */
14059               token = cp_lexer_peek_token (parser->lexer);
14060               /* What we do depends on what token we have.  */
14061               switch (token->type)
14062                 {
14063                   /* In valid code, a default argument must be
14064                      immediately followed by a `,' `)', or `...'.  */
14065                 case CPP_COMMA:
14066                   if (depth == 0 && maybe_template_id)
14067                     {
14068                       /* If we've seen a '<', we might be in a
14069                          template-argument-list.  Until Core issue 325 is
14070                          resolved, we don't know how this situation ought
14071                          to be handled, so try to DTRT.  We check whether
14072                          what comes after the comma is a valid parameter
14073                          declaration list.  If it is, then the comma ends
14074                          the default argument; otherwise the default
14075                          argument continues.  */
14076                       bool error = false;
14077
14078                       /* Set ITALP so cp_parser_parameter_declaration_list
14079                          doesn't decide to commit to this parse.  */
14080                       bool saved_italp = parser->in_template_argument_list_p;
14081                       parser->in_template_argument_list_p = true;
14082
14083                       cp_parser_parse_tentatively (parser);
14084                       cp_lexer_consume_token (parser->lexer);
14085                       cp_parser_parameter_declaration_list (parser, &error);
14086                       if (!cp_parser_error_occurred (parser) && !error)
14087                         done = true;
14088                       cp_parser_abort_tentative_parse (parser);
14089
14090                       parser->in_template_argument_list_p = saved_italp;
14091                       break;
14092                     }
14093                 case CPP_CLOSE_PAREN:
14094                 case CPP_ELLIPSIS:
14095                   /* If we run into a non-nested `;', `}', or `]',
14096                      then the code is invalid -- but the default
14097                      argument is certainly over.  */
14098                 case CPP_SEMICOLON:
14099                 case CPP_CLOSE_BRACE:
14100                 case CPP_CLOSE_SQUARE:
14101                   if (depth == 0)
14102                     done = true;
14103                   /* Update DEPTH, if necessary.  */
14104                   else if (token->type == CPP_CLOSE_PAREN
14105                            || token->type == CPP_CLOSE_BRACE
14106                            || token->type == CPP_CLOSE_SQUARE)
14107                     --depth;
14108                   break;
14109
14110                 case CPP_OPEN_PAREN:
14111                 case CPP_OPEN_SQUARE:
14112                 case CPP_OPEN_BRACE:
14113                   ++depth;
14114                   break;
14115
14116                 case CPP_LESS:
14117                   if (depth == 0)
14118                     /* This might be the comparison operator, or it might
14119                        start a template argument list.  */
14120                     ++maybe_template_id;
14121                   break;
14122
14123                 case CPP_RSHIFT:
14124                   if (cxx_dialect == cxx98)
14125                     break;
14126                   /* Fall through for C++0x, which treats the `>>'
14127                      operator like two `>' tokens in certain
14128                      cases.  */
14129
14130                 case CPP_GREATER:
14131                   if (depth == 0)
14132                     {
14133                       /* This might be an operator, or it might close a
14134                          template argument list.  But if a previous '<'
14135                          started a template argument list, this will have
14136                          closed it, so we can't be in one anymore.  */
14137                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14138                       if (maybe_template_id < 0)
14139                         maybe_template_id = 0;
14140                     }
14141                   break;
14142
14143                   /* If we run out of tokens, issue an error message.  */
14144                 case CPP_EOF:
14145                 case CPP_PRAGMA_EOL:
14146                   error ("%Hfile ends in default argument", &token->location);
14147                   done = true;
14148                   break;
14149
14150                 case CPP_NAME:
14151                 case CPP_SCOPE:
14152                   /* In these cases, we should look for template-ids.
14153                      For example, if the default argument is
14154                      `X<int, double>()', we need to do name lookup to
14155                      figure out whether or not `X' is a template; if
14156                      so, the `,' does not end the default argument.
14157
14158                      That is not yet done.  */
14159                   break;
14160
14161                 default:
14162                   break;
14163                 }
14164
14165               /* If we've reached the end, stop.  */
14166               if (done)
14167                 break;
14168
14169               /* Add the token to the token block.  */
14170               token = cp_lexer_consume_token (parser->lexer);
14171             }
14172
14173           /* Create a DEFAULT_ARG to represent the unparsed default
14174              argument.  */
14175           default_argument = make_node (DEFAULT_ARG);
14176           DEFARG_TOKENS (default_argument)
14177             = cp_token_cache_new (first_token, token);
14178           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14179         }
14180       /* Outside of a class definition, we can just parse the
14181          assignment-expression.  */
14182       else
14183         {
14184           token = cp_lexer_peek_token (parser->lexer);
14185           default_argument 
14186             = cp_parser_default_argument (parser, template_parm_p);
14187         }
14188
14189       if (!parser->default_arg_ok_p)
14190         {
14191           if (flag_permissive)
14192             warning (0, "deprecated use of default argument for parameter of non-function");
14193           else
14194             {
14195               error ("%Hdefault arguments are only "
14196                      "permitted for function parameters",
14197                      &token->location);
14198               default_argument = NULL_TREE;
14199             }
14200         }
14201       else if ((declarator && declarator->parameter_pack_p)
14202                || (decl_specifiers.type
14203                    && PACK_EXPANSION_P (decl_specifiers.type)))
14204         {
14205           const char* kind = template_parm_p? "template " : "";
14206           
14207           /* Find the name of the parameter pack.  */     
14208           cp_declarator *id_declarator = declarator;
14209           while (id_declarator && id_declarator->kind != cdk_id)
14210             id_declarator = id_declarator->declarator;
14211           
14212           if (id_declarator && id_declarator->kind == cdk_id)
14213             error ("%H%sparameter pack %qD cannot have a default argument",
14214                    &declarator_token_start->location,
14215                    kind, id_declarator->u.id.unqualified_name);
14216           else
14217             error ("%H%sparameter pack cannot have a default argument",
14218                    &declarator_token_start->location, kind);
14219           
14220           default_argument = NULL_TREE;
14221         }
14222     }
14223   else
14224     default_argument = NULL_TREE;
14225
14226   return make_parameter_declarator (&decl_specifiers,
14227                                     declarator,
14228                                     default_argument);
14229 }
14230
14231 /* Parse a default argument and return it.
14232
14233    TEMPLATE_PARM_P is true if this is a default argument for a
14234    non-type template parameter.  */
14235 static tree
14236 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14237 {
14238   tree default_argument = NULL_TREE;
14239   bool saved_greater_than_is_operator_p;
14240   bool saved_local_variables_forbidden_p;
14241
14242   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14243      set correctly.  */
14244   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14245   parser->greater_than_is_operator_p = !template_parm_p;
14246   /* Local variable names (and the `this' keyword) may not
14247      appear in a default argument.  */
14248   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14249   parser->local_variables_forbidden_p = true;
14250   /* The default argument expression may cause implicitly
14251      defined member functions to be synthesized, which will
14252      result in garbage collection.  We must treat this
14253      situation as if we were within the body of function so as
14254      to avoid collecting live data on the stack.  */
14255   ++function_depth;
14256   /* Parse the assignment-expression.  */
14257   if (template_parm_p)
14258     push_deferring_access_checks (dk_no_deferred);
14259   default_argument
14260     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14261   if (template_parm_p)
14262     pop_deferring_access_checks ();
14263   /* Restore saved state.  */
14264   --function_depth;
14265   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14266   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14267
14268   return default_argument;
14269 }
14270
14271 /* Parse a function-body.
14272
14273    function-body:
14274      compound_statement  */
14275
14276 static void
14277 cp_parser_function_body (cp_parser *parser)
14278 {
14279   cp_parser_compound_statement (parser, NULL, false);
14280 }
14281
14282 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14283    true if a ctor-initializer was present.  */
14284
14285 static bool
14286 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14287 {
14288   tree body;
14289   bool ctor_initializer_p;
14290
14291   /* Begin the function body.  */
14292   body = begin_function_body ();
14293   /* Parse the optional ctor-initializer.  */
14294   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14295   /* Parse the function-body.  */
14296   cp_parser_function_body (parser);
14297   /* Finish the function body.  */
14298   finish_function_body (body);
14299
14300   return ctor_initializer_p;
14301 }
14302
14303 /* Parse an initializer.
14304
14305    initializer:
14306      = initializer-clause
14307      ( expression-list )
14308
14309    Returns an expression representing the initializer.  If no
14310    initializer is present, NULL_TREE is returned.
14311
14312    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14313    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14314    set to TRUE if there is no initializer present.  If there is an
14315    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14316    is set to true; otherwise it is set to false.  */
14317
14318 static tree
14319 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14320                        bool* non_constant_p)
14321 {
14322   cp_token *token;
14323   tree init;
14324
14325   /* Peek at the next token.  */
14326   token = cp_lexer_peek_token (parser->lexer);
14327
14328   /* Let our caller know whether or not this initializer was
14329      parenthesized.  */
14330   *is_direct_init = (token->type != CPP_EQ);
14331   /* Assume that the initializer is constant.  */
14332   *non_constant_p = false;
14333
14334   if (token->type == CPP_EQ)
14335     {
14336       /* Consume the `='.  */
14337       cp_lexer_consume_token (parser->lexer);
14338       /* Parse the initializer-clause.  */
14339       init = cp_parser_initializer_clause (parser, non_constant_p);
14340     }
14341   else if (token->type == CPP_OPEN_PAREN)
14342     init = cp_parser_parenthesized_expression_list (parser, false,
14343                                                     /*cast_p=*/false,
14344                                                     /*allow_expansion_p=*/true,
14345                                                     non_constant_p);
14346   else if (token->type == CPP_OPEN_BRACE)
14347     {
14348       maybe_warn_cpp0x ("extended initializer lists");
14349       init = cp_parser_braced_list (parser, non_constant_p);
14350       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14351     }
14352   else
14353     {
14354       /* Anything else is an error.  */
14355       cp_parser_error (parser, "expected initializer");
14356       init = error_mark_node;
14357     }
14358
14359   return init;
14360 }
14361
14362 /* Parse an initializer-clause.
14363
14364    initializer-clause:
14365      assignment-expression
14366      braced-init-list
14367
14368    Returns an expression representing the initializer.
14369
14370    If the `assignment-expression' production is used the value
14371    returned is simply a representation for the expression.
14372
14373    Otherwise, calls cp_parser_braced_list.  */
14374
14375 static tree
14376 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14377 {
14378   tree initializer;
14379
14380   /* Assume the expression is constant.  */
14381   *non_constant_p = false;
14382
14383   /* If it is not a `{', then we are looking at an
14384      assignment-expression.  */
14385   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14386     {
14387       initializer
14388         = cp_parser_constant_expression (parser,
14389                                         /*allow_non_constant_p=*/true,
14390                                         non_constant_p);
14391       if (!*non_constant_p)
14392         initializer = fold_non_dependent_expr (initializer);
14393     }
14394   else
14395     initializer = cp_parser_braced_list (parser, non_constant_p);
14396
14397   return initializer;
14398 }
14399
14400 /* Parse a brace-enclosed initializer list.
14401
14402    braced-init-list:
14403      { initializer-list , [opt] }
14404      { }
14405
14406    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14407    the elements of the initializer-list (or NULL, if the last
14408    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14409    NULL_TREE.  There is no way to detect whether or not the optional
14410    trailing `,' was provided.  NON_CONSTANT_P is as for
14411    cp_parser_initializer.  */     
14412
14413 static tree
14414 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14415 {
14416   tree initializer;
14417
14418   /* Consume the `{' token.  */
14419   cp_lexer_consume_token (parser->lexer);
14420   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14421   initializer = make_node (CONSTRUCTOR);
14422   /* If it's not a `}', then there is a non-trivial initializer.  */
14423   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14424     {
14425       /* Parse the initializer list.  */
14426       CONSTRUCTOR_ELTS (initializer)
14427         = cp_parser_initializer_list (parser, non_constant_p);
14428       /* A trailing `,' token is allowed.  */
14429       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14430         cp_lexer_consume_token (parser->lexer);
14431     }
14432   /* Now, there should be a trailing `}'.  */
14433   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14434   TREE_TYPE (initializer) = init_list_type_node;
14435   return initializer;
14436 }
14437
14438 /* Parse an initializer-list.
14439
14440    initializer-list:
14441      initializer-clause ... [opt]
14442      initializer-list , initializer-clause ... [opt]
14443
14444    GNU Extension:
14445
14446    initializer-list:
14447      identifier : initializer-clause
14448      initializer-list, identifier : initializer-clause
14449
14450    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14451    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14452    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14453    as for cp_parser_initializer.  */
14454
14455 static VEC(constructor_elt,gc) *
14456 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14457 {
14458   VEC(constructor_elt,gc) *v = NULL;
14459
14460   /* Assume all of the expressions are constant.  */
14461   *non_constant_p = false;
14462
14463   /* Parse the rest of the list.  */
14464   while (true)
14465     {
14466       cp_token *token;
14467       tree identifier;
14468       tree initializer;
14469       bool clause_non_constant_p;
14470
14471       /* If the next token is an identifier and the following one is a
14472          colon, we are looking at the GNU designated-initializer
14473          syntax.  */
14474       if (cp_parser_allow_gnu_extensions_p (parser)
14475           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14476           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14477         {
14478           /* Warn the user that they are using an extension.  */
14479           pedwarn (input_location, OPT_pedantic, 
14480                    "ISO C++ does not allow designated initializers");
14481           /* Consume the identifier.  */
14482           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14483           /* Consume the `:'.  */
14484           cp_lexer_consume_token (parser->lexer);
14485         }
14486       else
14487         identifier = NULL_TREE;
14488
14489       /* Parse the initializer.  */
14490       initializer = cp_parser_initializer_clause (parser,
14491                                                   &clause_non_constant_p);
14492       /* If any clause is non-constant, so is the entire initializer.  */
14493       if (clause_non_constant_p)
14494         *non_constant_p = true;
14495
14496       /* If we have an ellipsis, this is an initializer pack
14497          expansion.  */
14498       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14499         {
14500           /* Consume the `...'.  */
14501           cp_lexer_consume_token (parser->lexer);
14502
14503           /* Turn the initializer into an initializer expansion.  */
14504           initializer = make_pack_expansion (initializer);
14505         }
14506
14507       /* Add it to the vector.  */
14508       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14509
14510       /* If the next token is not a comma, we have reached the end of
14511          the list.  */
14512       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14513         break;
14514
14515       /* Peek at the next token.  */
14516       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14517       /* If the next token is a `}', then we're still done.  An
14518          initializer-clause can have a trailing `,' after the
14519          initializer-list and before the closing `}'.  */
14520       if (token->type == CPP_CLOSE_BRACE)
14521         break;
14522
14523       /* Consume the `,' token.  */
14524       cp_lexer_consume_token (parser->lexer);
14525     }
14526
14527   return v;
14528 }
14529
14530 /* Classes [gram.class] */
14531
14532 /* Parse a class-name.
14533
14534    class-name:
14535      identifier
14536      template-id
14537
14538    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14539    to indicate that names looked up in dependent types should be
14540    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14541    keyword has been used to indicate that the name that appears next
14542    is a template.  TAG_TYPE indicates the explicit tag given before
14543    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14544    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14545    is the class being defined in a class-head.
14546
14547    Returns the TYPE_DECL representing the class.  */
14548
14549 static tree
14550 cp_parser_class_name (cp_parser *parser,
14551                       bool typename_keyword_p,
14552                       bool template_keyword_p,
14553                       enum tag_types tag_type,
14554                       bool check_dependency_p,
14555                       bool class_head_p,
14556                       bool is_declaration)
14557 {
14558   tree decl;
14559   tree scope;
14560   bool typename_p;
14561   cp_token *token;
14562
14563   /* All class-names start with an identifier.  */
14564   token = cp_lexer_peek_token (parser->lexer);
14565   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14566     {
14567       cp_parser_error (parser, "expected class-name");
14568       return error_mark_node;
14569     }
14570
14571   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14572      to a template-id, so we save it here.  */
14573   scope = parser->scope;
14574   if (scope == error_mark_node)
14575     return error_mark_node;
14576
14577   /* Any name names a type if we're following the `typename' keyword
14578      in a qualified name where the enclosing scope is type-dependent.  */
14579   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14580                 && dependent_type_p (scope));
14581   /* Handle the common case (an identifier, but not a template-id)
14582      efficiently.  */
14583   if (token->type == CPP_NAME
14584       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14585     {
14586       cp_token *identifier_token;
14587       tree identifier;
14588       bool ambiguous_p;
14589
14590       /* Look for the identifier.  */
14591       identifier_token = cp_lexer_peek_token (parser->lexer);
14592       ambiguous_p = identifier_token->ambiguous_p;
14593       identifier = cp_parser_identifier (parser);
14594       /* If the next token isn't an identifier, we are certainly not
14595          looking at a class-name.  */
14596       if (identifier == error_mark_node)
14597         decl = error_mark_node;
14598       /* If we know this is a type-name, there's no need to look it
14599          up.  */
14600       else if (typename_p)
14601         decl = identifier;
14602       else
14603         {
14604           tree ambiguous_decls;
14605           /* If we already know that this lookup is ambiguous, then
14606              we've already issued an error message; there's no reason
14607              to check again.  */
14608           if (ambiguous_p)
14609             {
14610               cp_parser_simulate_error (parser);
14611               return error_mark_node;
14612             }
14613           /* If the next token is a `::', then the name must be a type
14614              name.
14615
14616              [basic.lookup.qual]
14617
14618              During the lookup for a name preceding the :: scope
14619              resolution operator, object, function, and enumerator
14620              names are ignored.  */
14621           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14622             tag_type = typename_type;
14623           /* Look up the name.  */
14624           decl = cp_parser_lookup_name (parser, identifier,
14625                                         tag_type,
14626                                         /*is_template=*/false,
14627                                         /*is_namespace=*/false,
14628                                         check_dependency_p,
14629                                         &ambiguous_decls,
14630                                         identifier_token->location);
14631           if (ambiguous_decls)
14632             {
14633               error ("%Hreference to %qD is ambiguous",
14634                      &identifier_token->location, identifier);
14635               print_candidates (ambiguous_decls);
14636               if (cp_parser_parsing_tentatively (parser))
14637                 {
14638                   identifier_token->ambiguous_p = true;
14639                   cp_parser_simulate_error (parser);
14640                 }
14641               return error_mark_node;
14642             }
14643         }
14644     }
14645   else
14646     {
14647       /* Try a template-id.  */
14648       decl = cp_parser_template_id (parser, template_keyword_p,
14649                                     check_dependency_p,
14650                                     is_declaration);
14651       if (decl == error_mark_node)
14652         return error_mark_node;
14653     }
14654
14655   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14656
14657   /* If this is a typename, create a TYPENAME_TYPE.  */
14658   if (typename_p && decl != error_mark_node)
14659     {
14660       decl = make_typename_type (scope, decl, typename_type,
14661                                  /*complain=*/tf_error);
14662       if (decl != error_mark_node)
14663         decl = TYPE_NAME (decl);
14664     }
14665
14666   /* Check to see that it is really the name of a class.  */
14667   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14668       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14669       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14670     /* Situations like this:
14671
14672          template <typename T> struct A {
14673            typename T::template X<int>::I i;
14674          };
14675
14676        are problematic.  Is `T::template X<int>' a class-name?  The
14677        standard does not seem to be definitive, but there is no other
14678        valid interpretation of the following `::'.  Therefore, those
14679        names are considered class-names.  */
14680     {
14681       decl = make_typename_type (scope, decl, tag_type, tf_error);
14682       if (decl != error_mark_node)
14683         decl = TYPE_NAME (decl);
14684     }
14685   else if (TREE_CODE (decl) != TYPE_DECL
14686            || TREE_TYPE (decl) == error_mark_node
14687            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14688     decl = error_mark_node;
14689
14690   if (decl == error_mark_node)
14691     cp_parser_error (parser, "expected class-name");
14692
14693   return decl;
14694 }
14695
14696 /* Parse a class-specifier.
14697
14698    class-specifier:
14699      class-head { member-specification [opt] }
14700
14701    Returns the TREE_TYPE representing the class.  */
14702
14703 static tree
14704 cp_parser_class_specifier (cp_parser* parser)
14705 {
14706   cp_token *token;
14707   tree type;
14708   tree attributes = NULL_TREE;
14709   int has_trailing_semicolon;
14710   bool nested_name_specifier_p;
14711   unsigned saved_num_template_parameter_lists;
14712   bool saved_in_function_body;
14713   tree old_scope = NULL_TREE;
14714   tree scope = NULL_TREE;
14715   tree bases;
14716
14717   push_deferring_access_checks (dk_no_deferred);
14718
14719   /* Parse the class-head.  */
14720   type = cp_parser_class_head (parser,
14721                                &nested_name_specifier_p,
14722                                &attributes,
14723                                &bases);
14724   /* If the class-head was a semantic disaster, skip the entire body
14725      of the class.  */
14726   if (!type)
14727     {
14728       cp_parser_skip_to_end_of_block_or_statement (parser);
14729       pop_deferring_access_checks ();
14730       return error_mark_node;
14731     }
14732
14733   /* Look for the `{'.  */
14734   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14735     {
14736       pop_deferring_access_checks ();
14737       return error_mark_node;
14738     }
14739
14740   /* Process the base classes. If they're invalid, skip the 
14741      entire class body.  */
14742   if (!xref_basetypes (type, bases))
14743     {
14744       /* Consuming the closing brace yields better error messages
14745          later on.  */
14746       if (cp_parser_skip_to_closing_brace (parser))
14747         cp_lexer_consume_token (parser->lexer);
14748       pop_deferring_access_checks ();
14749       return error_mark_node;
14750     }
14751
14752   /* Issue an error message if type-definitions are forbidden here.  */
14753   cp_parser_check_type_definition (parser);
14754   /* Remember that we are defining one more class.  */
14755   ++parser->num_classes_being_defined;
14756   /* Inside the class, surrounding template-parameter-lists do not
14757      apply.  */
14758   saved_num_template_parameter_lists
14759     = parser->num_template_parameter_lists;
14760   parser->num_template_parameter_lists = 0;
14761   /* We are not in a function body.  */
14762   saved_in_function_body = parser->in_function_body;
14763   parser->in_function_body = false;
14764
14765   /* Start the class.  */
14766   if (nested_name_specifier_p)
14767     {
14768       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14769       old_scope = push_inner_scope (scope);
14770     }
14771   type = begin_class_definition (type, attributes);
14772
14773   if (type == error_mark_node)
14774     /* If the type is erroneous, skip the entire body of the class.  */
14775     cp_parser_skip_to_closing_brace (parser);
14776   else
14777     /* Parse the member-specification.  */
14778     cp_parser_member_specification_opt (parser);
14779
14780   /* Look for the trailing `}'.  */
14781   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14782   /* We get better error messages by noticing a common problem: a
14783      missing trailing `;'.  */
14784   token = cp_lexer_peek_token (parser->lexer);
14785   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14786   /* Look for trailing attributes to apply to this class.  */
14787   if (cp_parser_allow_gnu_extensions_p (parser))
14788     attributes = cp_parser_attributes_opt (parser);
14789   if (type != error_mark_node)
14790     type = finish_struct (type, attributes);
14791   if (nested_name_specifier_p)
14792     pop_inner_scope (old_scope, scope);
14793   /* If this class is not itself within the scope of another class,
14794      then we need to parse the bodies of all of the queued function
14795      definitions.  Note that the queued functions defined in a class
14796      are not always processed immediately following the
14797      class-specifier for that class.  Consider:
14798
14799        struct A {
14800          struct B { void f() { sizeof (A); } };
14801        };
14802
14803      If `f' were processed before the processing of `A' were
14804      completed, there would be no way to compute the size of `A'.
14805      Note that the nesting we are interested in here is lexical --
14806      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14807      for:
14808
14809        struct A { struct B; };
14810        struct A::B { void f() { } };
14811
14812      there is no need to delay the parsing of `A::B::f'.  */
14813   if (--parser->num_classes_being_defined == 0)
14814     {
14815       tree queue_entry;
14816       tree fn;
14817       tree class_type = NULL_TREE;
14818       tree pushed_scope = NULL_TREE;
14819
14820       /* In a first pass, parse default arguments to the functions.
14821          Then, in a second pass, parse the bodies of the functions.
14822          This two-phased approach handles cases like:
14823
14824             struct S {
14825               void f() { g(); }
14826               void g(int i = 3);
14827             };
14828
14829          */
14830       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14831              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14832            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14833            TREE_PURPOSE (parser->unparsed_functions_queues)
14834              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14835         {
14836           fn = TREE_VALUE (queue_entry);
14837           /* If there are default arguments that have not yet been processed,
14838              take care of them now.  */
14839           if (class_type != TREE_PURPOSE (queue_entry))
14840             {
14841               if (pushed_scope)
14842                 pop_scope (pushed_scope);
14843               class_type = TREE_PURPOSE (queue_entry);
14844               pushed_scope = push_scope (class_type);
14845             }
14846           /* Make sure that any template parameters are in scope.  */
14847           maybe_begin_member_template_processing (fn);
14848           /* Parse the default argument expressions.  */
14849           cp_parser_late_parsing_default_args (parser, fn);
14850           /* Remove any template parameters from the symbol table.  */
14851           maybe_end_member_template_processing ();
14852         }
14853       if (pushed_scope)
14854         pop_scope (pushed_scope);
14855       /* Now parse the body of the functions.  */
14856       for (TREE_VALUE (parser->unparsed_functions_queues)
14857              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14858            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14859            TREE_VALUE (parser->unparsed_functions_queues)
14860              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14861         {
14862           /* Figure out which function we need to process.  */
14863           fn = TREE_VALUE (queue_entry);
14864           /* Parse the function.  */
14865           cp_parser_late_parsing_for_member (parser, fn);
14866         }
14867     }
14868
14869   /* Put back any saved access checks.  */
14870   pop_deferring_access_checks ();
14871
14872   /* Restore saved state.  */
14873   parser->in_function_body = saved_in_function_body;
14874   parser->num_template_parameter_lists
14875     = saved_num_template_parameter_lists;
14876
14877   return type;
14878 }
14879
14880 /* Parse a class-head.
14881
14882    class-head:
14883      class-key identifier [opt] base-clause [opt]
14884      class-key nested-name-specifier identifier base-clause [opt]
14885      class-key nested-name-specifier [opt] template-id
14886        base-clause [opt]
14887
14888    GNU Extensions:
14889      class-key attributes identifier [opt] base-clause [opt]
14890      class-key attributes nested-name-specifier identifier base-clause [opt]
14891      class-key attributes nested-name-specifier [opt] template-id
14892        base-clause [opt]
14893
14894    Upon return BASES is initialized to the list of base classes (or
14895    NULL, if there are none) in the same form returned by
14896    cp_parser_base_clause.
14897
14898    Returns the TYPE of the indicated class.  Sets
14899    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14900    involving a nested-name-specifier was used, and FALSE otherwise.
14901
14902    Returns error_mark_node if this is not a class-head.
14903
14904    Returns NULL_TREE if the class-head is syntactically valid, but
14905    semantically invalid in a way that means we should skip the entire
14906    body of the class.  */
14907
14908 static tree
14909 cp_parser_class_head (cp_parser* parser,
14910                       bool* nested_name_specifier_p,
14911                       tree *attributes_p,
14912                       tree *bases)
14913 {
14914   tree nested_name_specifier;
14915   enum tag_types class_key;
14916   tree id = NULL_TREE;
14917   tree type = NULL_TREE;
14918   tree attributes;
14919   bool template_id_p = false;
14920   bool qualified_p = false;
14921   bool invalid_nested_name_p = false;
14922   bool invalid_explicit_specialization_p = false;
14923   tree pushed_scope = NULL_TREE;
14924   unsigned num_templates;
14925   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14926   /* Assume no nested-name-specifier will be present.  */
14927   *nested_name_specifier_p = false;
14928   /* Assume no template parameter lists will be used in defining the
14929      type.  */
14930   num_templates = 0;
14931
14932   *bases = NULL_TREE;
14933
14934   /* Look for the class-key.  */
14935   class_key = cp_parser_class_key (parser);
14936   if (class_key == none_type)
14937     return error_mark_node;
14938
14939   /* Parse the attributes.  */
14940   attributes = cp_parser_attributes_opt (parser);
14941
14942   /* If the next token is `::', that is invalid -- but sometimes
14943      people do try to write:
14944
14945        struct ::S {};
14946
14947      Handle this gracefully by accepting the extra qualifier, and then
14948      issuing an error about it later if this really is a
14949      class-head.  If it turns out just to be an elaborated type
14950      specifier, remain silent.  */
14951   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14952     qualified_p = true;
14953
14954   push_deferring_access_checks (dk_no_check);
14955
14956   /* Determine the name of the class.  Begin by looking for an
14957      optional nested-name-specifier.  */
14958   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14959   nested_name_specifier
14960     = cp_parser_nested_name_specifier_opt (parser,
14961                                            /*typename_keyword_p=*/false,
14962                                            /*check_dependency_p=*/false,
14963                                            /*type_p=*/false,
14964                                            /*is_declaration=*/false);
14965   /* If there was a nested-name-specifier, then there *must* be an
14966      identifier.  */
14967   if (nested_name_specifier)
14968     {
14969       type_start_token = cp_lexer_peek_token (parser->lexer);
14970       /* Although the grammar says `identifier', it really means
14971          `class-name' or `template-name'.  You are only allowed to
14972          define a class that has already been declared with this
14973          syntax.
14974
14975          The proposed resolution for Core Issue 180 says that wherever
14976          you see `class T::X' you should treat `X' as a type-name.
14977
14978          It is OK to define an inaccessible class; for example:
14979
14980            class A { class B; };
14981            class A::B {};
14982
14983          We do not know if we will see a class-name, or a
14984          template-name.  We look for a class-name first, in case the
14985          class-name is a template-id; if we looked for the
14986          template-name first we would stop after the template-name.  */
14987       cp_parser_parse_tentatively (parser);
14988       type = cp_parser_class_name (parser,
14989                                    /*typename_keyword_p=*/false,
14990                                    /*template_keyword_p=*/false,
14991                                    class_type,
14992                                    /*check_dependency_p=*/false,
14993                                    /*class_head_p=*/true,
14994                                    /*is_declaration=*/false);
14995       /* If that didn't work, ignore the nested-name-specifier.  */
14996       if (!cp_parser_parse_definitely (parser))
14997         {
14998           invalid_nested_name_p = true;
14999           type_start_token = cp_lexer_peek_token (parser->lexer);
15000           id = cp_parser_identifier (parser);
15001           if (id == error_mark_node)
15002             id = NULL_TREE;
15003         }
15004       /* If we could not find a corresponding TYPE, treat this
15005          declaration like an unqualified declaration.  */
15006       if (type == error_mark_node)
15007         nested_name_specifier = NULL_TREE;
15008       /* Otherwise, count the number of templates used in TYPE and its
15009          containing scopes.  */
15010       else
15011         {
15012           tree scope;
15013
15014           for (scope = TREE_TYPE (type);
15015                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15016                scope = (TYPE_P (scope)
15017                         ? TYPE_CONTEXT (scope)
15018                         : DECL_CONTEXT (scope)))
15019             if (TYPE_P (scope)
15020                 && CLASS_TYPE_P (scope)
15021                 && CLASSTYPE_TEMPLATE_INFO (scope)
15022                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15023                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15024               ++num_templates;
15025         }
15026     }
15027   /* Otherwise, the identifier is optional.  */
15028   else
15029     {
15030       /* We don't know whether what comes next is a template-id,
15031          an identifier, or nothing at all.  */
15032       cp_parser_parse_tentatively (parser);
15033       /* Check for a template-id.  */
15034       type_start_token = cp_lexer_peek_token (parser->lexer);
15035       id = cp_parser_template_id (parser,
15036                                   /*template_keyword_p=*/false,
15037                                   /*check_dependency_p=*/true,
15038                                   /*is_declaration=*/true);
15039       /* If that didn't work, it could still be an identifier.  */
15040       if (!cp_parser_parse_definitely (parser))
15041         {
15042           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15043             {
15044               type_start_token = cp_lexer_peek_token (parser->lexer);
15045               id = cp_parser_identifier (parser);
15046             }
15047           else
15048             id = NULL_TREE;
15049         }
15050       else
15051         {
15052           template_id_p = true;
15053           ++num_templates;
15054         }
15055     }
15056
15057   pop_deferring_access_checks ();
15058
15059   if (id)
15060     cp_parser_check_for_invalid_template_id (parser, id,
15061                                              type_start_token->location);
15062
15063   /* If it's not a `:' or a `{' then we can't really be looking at a
15064      class-head, since a class-head only appears as part of a
15065      class-specifier.  We have to detect this situation before calling
15066      xref_tag, since that has irreversible side-effects.  */
15067   if (!cp_parser_next_token_starts_class_definition_p (parser))
15068     {
15069       cp_parser_error (parser, "expected %<{%> or %<:%>");
15070       return error_mark_node;
15071     }
15072
15073   /* At this point, we're going ahead with the class-specifier, even
15074      if some other problem occurs.  */
15075   cp_parser_commit_to_tentative_parse (parser);
15076   /* Issue the error about the overly-qualified name now.  */
15077   if (qualified_p)
15078     {
15079       cp_parser_error (parser,
15080                        "global qualification of class name is invalid");
15081       return error_mark_node;
15082     }
15083   else if (invalid_nested_name_p)
15084     {
15085       cp_parser_error (parser,
15086                        "qualified name does not name a class");
15087       return error_mark_node;
15088     }
15089   else if (nested_name_specifier)
15090     {
15091       tree scope;
15092
15093       /* Reject typedef-names in class heads.  */
15094       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15095         {
15096           error ("%Hinvalid class name in declaration of %qD",
15097                  &type_start_token->location, type);
15098           type = NULL_TREE;
15099           goto done;
15100         }
15101
15102       /* Figure out in what scope the declaration is being placed.  */
15103       scope = current_scope ();
15104       /* If that scope does not contain the scope in which the
15105          class was originally declared, the program is invalid.  */
15106       if (scope && !is_ancestor (scope, nested_name_specifier))
15107         {
15108           if (at_namespace_scope_p ())
15109             error ("%Hdeclaration of %qD in namespace %qD which does not "
15110                    "enclose %qD",
15111                    &type_start_token->location,
15112                    type, scope, nested_name_specifier);
15113           else
15114             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15115                    &type_start_token->location,
15116                    type, scope, nested_name_specifier);
15117           type = NULL_TREE;
15118           goto done;
15119         }
15120       /* [dcl.meaning]
15121
15122          A declarator-id shall not be qualified except for the
15123          definition of a ... nested class outside of its class
15124          ... [or] the definition or explicit instantiation of a
15125          class member of a namespace outside of its namespace.  */
15126       if (scope == nested_name_specifier)
15127         {
15128           permerror (input_location, "%Hextra qualification not allowed",
15129                      &nested_name_specifier_token_start->location);
15130           nested_name_specifier = NULL_TREE;
15131           num_templates = 0;
15132         }
15133     }
15134   /* An explicit-specialization must be preceded by "template <>".  If
15135      it is not, try to recover gracefully.  */
15136   if (at_namespace_scope_p ()
15137       && parser->num_template_parameter_lists == 0
15138       && template_id_p)
15139     {
15140       error ("%Han explicit specialization must be preceded by %<template <>%>",
15141              &type_start_token->location);
15142       invalid_explicit_specialization_p = true;
15143       /* Take the same action that would have been taken by
15144          cp_parser_explicit_specialization.  */
15145       ++parser->num_template_parameter_lists;
15146       begin_specialization ();
15147     }
15148   /* There must be no "return" statements between this point and the
15149      end of this function; set "type "to the correct return value and
15150      use "goto done;" to return.  */
15151   /* Make sure that the right number of template parameters were
15152      present.  */
15153   if (!cp_parser_check_template_parameters (parser, num_templates,
15154                                             type_start_token->location))
15155     {
15156       /* If something went wrong, there is no point in even trying to
15157          process the class-definition.  */
15158       type = NULL_TREE;
15159       goto done;
15160     }
15161
15162   /* Look up the type.  */
15163   if (template_id_p)
15164     {
15165       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15166           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15167               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15168         {
15169           error ("%Hfunction template %qD redeclared as a class template",
15170                  &type_start_token->location, id);
15171           type = error_mark_node;
15172         }
15173       else
15174         {
15175           type = TREE_TYPE (id);
15176           type = maybe_process_partial_specialization (type);
15177         }
15178       if (nested_name_specifier)
15179         pushed_scope = push_scope (nested_name_specifier);
15180     }
15181   else if (nested_name_specifier)
15182     {
15183       tree class_type;
15184
15185       /* Given:
15186
15187             template <typename T> struct S { struct T };
15188             template <typename T> struct S<T>::T { };
15189
15190          we will get a TYPENAME_TYPE when processing the definition of
15191          `S::T'.  We need to resolve it to the actual type before we
15192          try to define it.  */
15193       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15194         {
15195           class_type = resolve_typename_type (TREE_TYPE (type),
15196                                               /*only_current_p=*/false);
15197           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15198             type = TYPE_NAME (class_type);
15199           else
15200             {
15201               cp_parser_error (parser, "could not resolve typename type");
15202               type = error_mark_node;
15203             }
15204         }
15205
15206       if (maybe_process_partial_specialization (TREE_TYPE (type))
15207           == error_mark_node)
15208         {
15209           type = NULL_TREE;
15210           goto done;
15211         }
15212
15213       class_type = current_class_type;
15214       /* Enter the scope indicated by the nested-name-specifier.  */
15215       pushed_scope = push_scope (nested_name_specifier);
15216       /* Get the canonical version of this type.  */
15217       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15218       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15219           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15220         {
15221           type = push_template_decl (type);
15222           if (type == error_mark_node)
15223             {
15224               type = NULL_TREE;
15225               goto done;
15226             }
15227         }
15228
15229       type = TREE_TYPE (type);
15230       *nested_name_specifier_p = true;
15231     }
15232   else      /* The name is not a nested name.  */
15233     {
15234       /* If the class was unnamed, create a dummy name.  */
15235       if (!id)
15236         id = make_anon_name ();
15237       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15238                        parser->num_template_parameter_lists);
15239     }
15240
15241   /* Indicate whether this class was declared as a `class' or as a
15242      `struct'.  */
15243   if (TREE_CODE (type) == RECORD_TYPE)
15244     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15245   cp_parser_check_class_key (class_key, type);
15246
15247   /* If this type was already complete, and we see another definition,
15248      that's an error.  */
15249   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15250     {
15251       error ("%Hredefinition of %q#T",
15252              &type_start_token->location, type);
15253       error ("%Hprevious definition of %q+#T",
15254              &type_start_token->location, type);
15255       type = NULL_TREE;
15256       goto done;
15257     }
15258   else if (type == error_mark_node)
15259     type = NULL_TREE;
15260
15261   /* We will have entered the scope containing the class; the names of
15262      base classes should be looked up in that context.  For example:
15263
15264        struct A { struct B {}; struct C; };
15265        struct A::C : B {};
15266
15267      is valid.  */
15268
15269   /* Get the list of base-classes, if there is one.  */
15270   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15271     *bases = cp_parser_base_clause (parser);
15272
15273  done:
15274   /* Leave the scope given by the nested-name-specifier.  We will
15275      enter the class scope itself while processing the members.  */
15276   if (pushed_scope)
15277     pop_scope (pushed_scope);
15278
15279   if (invalid_explicit_specialization_p)
15280     {
15281       end_specialization ();
15282       --parser->num_template_parameter_lists;
15283     }
15284   *attributes_p = attributes;
15285   return type;
15286 }
15287
15288 /* Parse a class-key.
15289
15290    class-key:
15291      class
15292      struct
15293      union
15294
15295    Returns the kind of class-key specified, or none_type to indicate
15296    error.  */
15297
15298 static enum tag_types
15299 cp_parser_class_key (cp_parser* parser)
15300 {
15301   cp_token *token;
15302   enum tag_types tag_type;
15303
15304   /* Look for the class-key.  */
15305   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15306   if (!token)
15307     return none_type;
15308
15309   /* Check to see if the TOKEN is a class-key.  */
15310   tag_type = cp_parser_token_is_class_key (token);
15311   if (!tag_type)
15312     cp_parser_error (parser, "expected class-key");
15313   return tag_type;
15314 }
15315
15316 /* Parse an (optional) member-specification.
15317
15318    member-specification:
15319      member-declaration member-specification [opt]
15320      access-specifier : member-specification [opt]  */
15321
15322 static void
15323 cp_parser_member_specification_opt (cp_parser* parser)
15324 {
15325   while (true)
15326     {
15327       cp_token *token;
15328       enum rid keyword;
15329
15330       /* Peek at the next token.  */
15331       token = cp_lexer_peek_token (parser->lexer);
15332       /* If it's a `}', or EOF then we've seen all the members.  */
15333       if (token->type == CPP_CLOSE_BRACE
15334           || token->type == CPP_EOF
15335           || token->type == CPP_PRAGMA_EOL)
15336         break;
15337
15338       /* See if this token is a keyword.  */
15339       keyword = token->keyword;
15340       switch (keyword)
15341         {
15342         case RID_PUBLIC:
15343         case RID_PROTECTED:
15344         case RID_PRIVATE:
15345           /* Consume the access-specifier.  */
15346           cp_lexer_consume_token (parser->lexer);
15347           /* Remember which access-specifier is active.  */
15348           current_access_specifier = token->u.value;
15349           /* Look for the `:'.  */
15350           cp_parser_require (parser, CPP_COLON, "%<:%>");
15351           break;
15352
15353         default:
15354           /* Accept #pragmas at class scope.  */
15355           if (token->type == CPP_PRAGMA)
15356             {
15357               cp_parser_pragma (parser, pragma_external);
15358               break;
15359             }
15360
15361           /* Otherwise, the next construction must be a
15362              member-declaration.  */
15363           cp_parser_member_declaration (parser);
15364         }
15365     }
15366 }
15367
15368 /* Parse a member-declaration.
15369
15370    member-declaration:
15371      decl-specifier-seq [opt] member-declarator-list [opt] ;
15372      function-definition ; [opt]
15373      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15374      using-declaration
15375      template-declaration
15376
15377    member-declarator-list:
15378      member-declarator
15379      member-declarator-list , member-declarator
15380
15381    member-declarator:
15382      declarator pure-specifier [opt]
15383      declarator constant-initializer [opt]
15384      identifier [opt] : constant-expression
15385
15386    GNU Extensions:
15387
15388    member-declaration:
15389      __extension__ member-declaration
15390
15391    member-declarator:
15392      declarator attributes [opt] pure-specifier [opt]
15393      declarator attributes [opt] constant-initializer [opt]
15394      identifier [opt] attributes [opt] : constant-expression  
15395
15396    C++0x Extensions:
15397
15398    member-declaration:
15399      static_assert-declaration  */
15400
15401 static void
15402 cp_parser_member_declaration (cp_parser* parser)
15403 {
15404   cp_decl_specifier_seq decl_specifiers;
15405   tree prefix_attributes;
15406   tree decl;
15407   int declares_class_or_enum;
15408   bool friend_p;
15409   cp_token *token = NULL;
15410   cp_token *decl_spec_token_start = NULL;
15411   cp_token *initializer_token_start = NULL;
15412   int saved_pedantic;
15413
15414   /* Check for the `__extension__' keyword.  */
15415   if (cp_parser_extension_opt (parser, &saved_pedantic))
15416     {
15417       /* Recurse.  */
15418       cp_parser_member_declaration (parser);
15419       /* Restore the old value of the PEDANTIC flag.  */
15420       pedantic = saved_pedantic;
15421
15422       return;
15423     }
15424
15425   /* Check for a template-declaration.  */
15426   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15427     {
15428       /* An explicit specialization here is an error condition, and we
15429          expect the specialization handler to detect and report this.  */
15430       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15431           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15432         cp_parser_explicit_specialization (parser);
15433       else
15434         cp_parser_template_declaration (parser, /*member_p=*/true);
15435
15436       return;
15437     }
15438
15439   /* Check for a using-declaration.  */
15440   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15441     {
15442       /* Parse the using-declaration.  */
15443       cp_parser_using_declaration (parser,
15444                                    /*access_declaration_p=*/false);
15445       return;
15446     }
15447
15448   /* Check for @defs.  */
15449   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15450     {
15451       tree ivar, member;
15452       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15453       ivar = ivar_chains;
15454       while (ivar)
15455         {
15456           member = ivar;
15457           ivar = TREE_CHAIN (member);
15458           TREE_CHAIN (member) = NULL_TREE;
15459           finish_member_declaration (member);
15460         }
15461       return;
15462     }
15463
15464   /* If the next token is `static_assert' we have a static assertion.  */
15465   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15466     {
15467       cp_parser_static_assert (parser, /*member_p=*/true);
15468       return;
15469     }
15470
15471   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15472     return;
15473
15474   /* Parse the decl-specifier-seq.  */
15475   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15476   cp_parser_decl_specifier_seq (parser,
15477                                 CP_PARSER_FLAGS_OPTIONAL,
15478                                 &decl_specifiers,
15479                                 &declares_class_or_enum);
15480   prefix_attributes = decl_specifiers.attributes;
15481   decl_specifiers.attributes = NULL_TREE;
15482   /* Check for an invalid type-name.  */
15483   if (!decl_specifiers.type
15484       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15485     return;
15486   /* If there is no declarator, then the decl-specifier-seq should
15487      specify a type.  */
15488   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15489     {
15490       /* If there was no decl-specifier-seq, and the next token is a
15491          `;', then we have something like:
15492
15493            struct S { ; };
15494
15495          [class.mem]
15496
15497          Each member-declaration shall declare at least one member
15498          name of the class.  */
15499       if (!decl_specifiers.any_specifiers_p)
15500         {
15501           cp_token *token = cp_lexer_peek_token (parser->lexer);
15502           if (!in_system_header_at (token->location))
15503             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15504         }
15505       else
15506         {
15507           tree type;
15508
15509           /* See if this declaration is a friend.  */
15510           friend_p = cp_parser_friend_p (&decl_specifiers);
15511           /* If there were decl-specifiers, check to see if there was
15512              a class-declaration.  */
15513           type = check_tag_decl (&decl_specifiers);
15514           /* Nested classes have already been added to the class, but
15515              a `friend' needs to be explicitly registered.  */
15516           if (friend_p)
15517             {
15518               /* If the `friend' keyword was present, the friend must
15519                  be introduced with a class-key.  */
15520                if (!declares_class_or_enum)
15521                  error ("%Ha class-key must be used when declaring a friend",
15522                         &decl_spec_token_start->location);
15523                /* In this case:
15524
15525                     template <typename T> struct A {
15526                       friend struct A<T>::B;
15527                     };
15528
15529                   A<T>::B will be represented by a TYPENAME_TYPE, and
15530                   therefore not recognized by check_tag_decl.  */
15531                if (!type
15532                    && decl_specifiers.type
15533                    && TYPE_P (decl_specifiers.type))
15534                  type = decl_specifiers.type;
15535                if (!type || !TYPE_P (type))
15536                  error ("%Hfriend declaration does not name a class or "
15537                         "function", &decl_spec_token_start->location);
15538                else
15539                  make_friend_class (current_class_type, type,
15540                                     /*complain=*/true);
15541             }
15542           /* If there is no TYPE, an error message will already have
15543              been issued.  */
15544           else if (!type || type == error_mark_node)
15545             ;
15546           /* An anonymous aggregate has to be handled specially; such
15547              a declaration really declares a data member (with a
15548              particular type), as opposed to a nested class.  */
15549           else if (ANON_AGGR_TYPE_P (type))
15550             {
15551               /* Remove constructors and such from TYPE, now that we
15552                  know it is an anonymous aggregate.  */
15553               fixup_anonymous_aggr (type);
15554               /* And make the corresponding data member.  */
15555               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15556               /* Add it to the class.  */
15557               finish_member_declaration (decl);
15558             }
15559           else
15560             cp_parser_check_access_in_redeclaration
15561                                               (TYPE_NAME (type),
15562                                                decl_spec_token_start->location);
15563         }
15564     }
15565   else
15566     {
15567       /* See if these declarations will be friends.  */
15568       friend_p = cp_parser_friend_p (&decl_specifiers);
15569
15570       /* Keep going until we hit the `;' at the end of the
15571          declaration.  */
15572       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15573         {
15574           tree attributes = NULL_TREE;
15575           tree first_attribute;
15576
15577           /* Peek at the next token.  */
15578           token = cp_lexer_peek_token (parser->lexer);
15579
15580           /* Check for a bitfield declaration.  */
15581           if (token->type == CPP_COLON
15582               || (token->type == CPP_NAME
15583                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15584                   == CPP_COLON))
15585             {
15586               tree identifier;
15587               tree width;
15588
15589               /* Get the name of the bitfield.  Note that we cannot just
15590                  check TOKEN here because it may have been invalidated by
15591                  the call to cp_lexer_peek_nth_token above.  */
15592               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15593                 identifier = cp_parser_identifier (parser);
15594               else
15595                 identifier = NULL_TREE;
15596
15597               /* Consume the `:' token.  */
15598               cp_lexer_consume_token (parser->lexer);
15599               /* Get the width of the bitfield.  */
15600               width
15601                 = cp_parser_constant_expression (parser,
15602                                                  /*allow_non_constant=*/false,
15603                                                  NULL);
15604
15605               /* Look for attributes that apply to the bitfield.  */
15606               attributes = cp_parser_attributes_opt (parser);
15607               /* Remember which attributes are prefix attributes and
15608                  which are not.  */
15609               first_attribute = attributes;
15610               /* Combine the attributes.  */
15611               attributes = chainon (prefix_attributes, attributes);
15612
15613               /* Create the bitfield declaration.  */
15614               decl = grokbitfield (identifier
15615                                    ? make_id_declarator (NULL_TREE,
15616                                                          identifier,
15617                                                          sfk_none)
15618                                    : NULL,
15619                                    &decl_specifiers,
15620                                    width,
15621                                    attributes);
15622             }
15623           else
15624             {
15625               cp_declarator *declarator;
15626               tree initializer;
15627               tree asm_specification;
15628               int ctor_dtor_or_conv_p;
15629
15630               /* Parse the declarator.  */
15631               declarator
15632                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15633                                         &ctor_dtor_or_conv_p,
15634                                         /*parenthesized_p=*/NULL,
15635                                         /*member_p=*/true);
15636
15637               /* If something went wrong parsing the declarator, make sure
15638                  that we at least consume some tokens.  */
15639               if (declarator == cp_error_declarator)
15640                 {
15641                   /* Skip to the end of the statement.  */
15642                   cp_parser_skip_to_end_of_statement (parser);
15643                   /* If the next token is not a semicolon, that is
15644                      probably because we just skipped over the body of
15645                      a function.  So, we consume a semicolon if
15646                      present, but do not issue an error message if it
15647                      is not present.  */
15648                   if (cp_lexer_next_token_is (parser->lexer,
15649                                               CPP_SEMICOLON))
15650                     cp_lexer_consume_token (parser->lexer);
15651                   return;
15652                 }
15653
15654               if (declares_class_or_enum & 2)
15655                 cp_parser_check_for_definition_in_return_type
15656                                             (declarator, decl_specifiers.type,
15657                                              decl_specifiers.type_location);
15658
15659               /* Look for an asm-specification.  */
15660               asm_specification = cp_parser_asm_specification_opt (parser);
15661               /* Look for attributes that apply to the declaration.  */
15662               attributes = cp_parser_attributes_opt (parser);
15663               /* Remember which attributes are prefix attributes and
15664                  which are not.  */
15665               first_attribute = attributes;
15666               /* Combine the attributes.  */
15667               attributes = chainon (prefix_attributes, attributes);
15668
15669               /* If it's an `=', then we have a constant-initializer or a
15670                  pure-specifier.  It is not correct to parse the
15671                  initializer before registering the member declaration
15672                  since the member declaration should be in scope while
15673                  its initializer is processed.  However, the rest of the
15674                  front end does not yet provide an interface that allows
15675                  us to handle this correctly.  */
15676               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15677                 {
15678                   /* In [class.mem]:
15679
15680                      A pure-specifier shall be used only in the declaration of
15681                      a virtual function.
15682
15683                      A member-declarator can contain a constant-initializer
15684                      only if it declares a static member of integral or
15685                      enumeration type.
15686
15687                      Therefore, if the DECLARATOR is for a function, we look
15688                      for a pure-specifier; otherwise, we look for a
15689                      constant-initializer.  When we call `grokfield', it will
15690                      perform more stringent semantics checks.  */
15691                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15692                   if (function_declarator_p (declarator))
15693                     initializer = cp_parser_pure_specifier (parser);
15694                   else
15695                     /* Parse the initializer.  */
15696                     initializer = cp_parser_constant_initializer (parser);
15697                 }
15698               /* Otherwise, there is no initializer.  */
15699               else
15700                 initializer = NULL_TREE;
15701
15702               /* See if we are probably looking at a function
15703                  definition.  We are certainly not looking at a
15704                  member-declarator.  Calling `grokfield' has
15705                  side-effects, so we must not do it unless we are sure
15706                  that we are looking at a member-declarator.  */
15707               if (cp_parser_token_starts_function_definition_p
15708                   (cp_lexer_peek_token (parser->lexer)))
15709                 {
15710                   /* The grammar does not allow a pure-specifier to be
15711                      used when a member function is defined.  (It is
15712                      possible that this fact is an oversight in the
15713                      standard, since a pure function may be defined
15714                      outside of the class-specifier.  */
15715                   if (initializer)
15716                     error ("%Hpure-specifier on function-definition",
15717                            &initializer_token_start->location);
15718                   decl = cp_parser_save_member_function_body (parser,
15719                                                               &decl_specifiers,
15720                                                               declarator,
15721                                                               attributes);
15722                   /* If the member was not a friend, declare it here.  */
15723                   if (!friend_p)
15724                     finish_member_declaration (decl);
15725                   /* Peek at the next token.  */
15726                   token = cp_lexer_peek_token (parser->lexer);
15727                   /* If the next token is a semicolon, consume it.  */
15728                   if (token->type == CPP_SEMICOLON)
15729                     cp_lexer_consume_token (parser->lexer);
15730                   return;
15731                 }
15732               else
15733                 /* Create the declaration.  */
15734                 decl = grokfield (declarator, &decl_specifiers,
15735                                   initializer, /*init_const_expr_p=*/true,
15736                                   asm_specification,
15737                                   attributes);
15738             }
15739
15740           /* Reset PREFIX_ATTRIBUTES.  */
15741           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15742             attributes = TREE_CHAIN (attributes);
15743           if (attributes)
15744             TREE_CHAIN (attributes) = NULL_TREE;
15745
15746           /* If there is any qualification still in effect, clear it
15747              now; we will be starting fresh with the next declarator.  */
15748           parser->scope = NULL_TREE;
15749           parser->qualifying_scope = NULL_TREE;
15750           parser->object_scope = NULL_TREE;
15751           /* If it's a `,', then there are more declarators.  */
15752           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15753             cp_lexer_consume_token (parser->lexer);
15754           /* If the next token isn't a `;', then we have a parse error.  */
15755           else if (cp_lexer_next_token_is_not (parser->lexer,
15756                                                CPP_SEMICOLON))
15757             {
15758               cp_parser_error (parser, "expected %<;%>");
15759               /* Skip tokens until we find a `;'.  */
15760               cp_parser_skip_to_end_of_statement (parser);
15761
15762               break;
15763             }
15764
15765           if (decl)
15766             {
15767               /* Add DECL to the list of members.  */
15768               if (!friend_p)
15769                 finish_member_declaration (decl);
15770
15771               if (TREE_CODE (decl) == FUNCTION_DECL)
15772                 cp_parser_save_default_args (parser, decl);
15773             }
15774         }
15775     }
15776
15777   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15778 }
15779
15780 /* Parse a pure-specifier.
15781
15782    pure-specifier:
15783      = 0
15784
15785    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15786    Otherwise, ERROR_MARK_NODE is returned.  */
15787
15788 static tree
15789 cp_parser_pure_specifier (cp_parser* parser)
15790 {
15791   cp_token *token;
15792
15793   /* Look for the `=' token.  */
15794   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15795     return error_mark_node;
15796   /* Look for the `0' token.  */
15797   token = cp_lexer_consume_token (parser->lexer);
15798
15799   /* Accept = default or = delete in c++0x mode.  */
15800   if (token->keyword == RID_DEFAULT
15801       || token->keyword == RID_DELETE)
15802     {
15803       maybe_warn_cpp0x ("defaulted and deleted functions");
15804       return token->u.value;
15805     }
15806
15807   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15808   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15809     {
15810       cp_parser_error (parser,
15811                        "invalid pure specifier (only %<= 0%> is allowed)");
15812       cp_parser_skip_to_end_of_statement (parser);
15813       return error_mark_node;
15814     }
15815   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15816     {
15817       error ("%Htemplates may not be %<virtual%>", &token->location);
15818       return error_mark_node;
15819     }
15820
15821   return integer_zero_node;
15822 }
15823
15824 /* Parse a constant-initializer.
15825
15826    constant-initializer:
15827      = constant-expression
15828
15829    Returns a representation of the constant-expression.  */
15830
15831 static tree
15832 cp_parser_constant_initializer (cp_parser* parser)
15833 {
15834   /* Look for the `=' token.  */
15835   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15836     return error_mark_node;
15837
15838   /* It is invalid to write:
15839
15840        struct S { static const int i = { 7 }; };
15841
15842      */
15843   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15844     {
15845       cp_parser_error (parser,
15846                        "a brace-enclosed initializer is not allowed here");
15847       /* Consume the opening brace.  */
15848       cp_lexer_consume_token (parser->lexer);
15849       /* Skip the initializer.  */
15850       cp_parser_skip_to_closing_brace (parser);
15851       /* Look for the trailing `}'.  */
15852       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15853
15854       return error_mark_node;
15855     }
15856
15857   return cp_parser_constant_expression (parser,
15858                                         /*allow_non_constant=*/false,
15859                                         NULL);
15860 }
15861
15862 /* Derived classes [gram.class.derived] */
15863
15864 /* Parse a base-clause.
15865
15866    base-clause:
15867      : base-specifier-list
15868
15869    base-specifier-list:
15870      base-specifier ... [opt]
15871      base-specifier-list , base-specifier ... [opt]
15872
15873    Returns a TREE_LIST representing the base-classes, in the order in
15874    which they were declared.  The representation of each node is as
15875    described by cp_parser_base_specifier.
15876
15877    In the case that no bases are specified, this function will return
15878    NULL_TREE, not ERROR_MARK_NODE.  */
15879
15880 static tree
15881 cp_parser_base_clause (cp_parser* parser)
15882 {
15883   tree bases = NULL_TREE;
15884
15885   /* Look for the `:' that begins the list.  */
15886   cp_parser_require (parser, CPP_COLON, "%<:%>");
15887
15888   /* Scan the base-specifier-list.  */
15889   while (true)
15890     {
15891       cp_token *token;
15892       tree base;
15893       bool pack_expansion_p = false;
15894
15895       /* Look for the base-specifier.  */
15896       base = cp_parser_base_specifier (parser);
15897       /* Look for the (optional) ellipsis. */
15898       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15899         {
15900           /* Consume the `...'. */
15901           cp_lexer_consume_token (parser->lexer);
15902
15903           pack_expansion_p = true;
15904         }
15905
15906       /* Add BASE to the front of the list.  */
15907       if (base != error_mark_node)
15908         {
15909           if (pack_expansion_p)
15910             /* Make this a pack expansion type. */
15911             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15912           
15913
15914           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15915             {
15916               TREE_CHAIN (base) = bases;
15917               bases = base;
15918             }
15919         }
15920       /* Peek at the next token.  */
15921       token = cp_lexer_peek_token (parser->lexer);
15922       /* If it's not a comma, then the list is complete.  */
15923       if (token->type != CPP_COMMA)
15924         break;
15925       /* Consume the `,'.  */
15926       cp_lexer_consume_token (parser->lexer);
15927     }
15928
15929   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15930      base class had a qualified name.  However, the next name that
15931      appears is certainly not qualified.  */
15932   parser->scope = NULL_TREE;
15933   parser->qualifying_scope = NULL_TREE;
15934   parser->object_scope = NULL_TREE;
15935
15936   return nreverse (bases);
15937 }
15938
15939 /* Parse a base-specifier.
15940
15941    base-specifier:
15942      :: [opt] nested-name-specifier [opt] class-name
15943      virtual access-specifier [opt] :: [opt] nested-name-specifier
15944        [opt] class-name
15945      access-specifier virtual [opt] :: [opt] nested-name-specifier
15946        [opt] class-name
15947
15948    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15949    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15950    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15951    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15952
15953 static tree
15954 cp_parser_base_specifier (cp_parser* parser)
15955 {
15956   cp_token *token;
15957   bool done = false;
15958   bool virtual_p = false;
15959   bool duplicate_virtual_error_issued_p = false;
15960   bool duplicate_access_error_issued_p = false;
15961   bool class_scope_p, template_p;
15962   tree access = access_default_node;
15963   tree type;
15964
15965   /* Process the optional `virtual' and `access-specifier'.  */
15966   while (!done)
15967     {
15968       /* Peek at the next token.  */
15969       token = cp_lexer_peek_token (parser->lexer);
15970       /* Process `virtual'.  */
15971       switch (token->keyword)
15972         {
15973         case RID_VIRTUAL:
15974           /* If `virtual' appears more than once, issue an error.  */
15975           if (virtual_p && !duplicate_virtual_error_issued_p)
15976             {
15977               cp_parser_error (parser,
15978                                "%<virtual%> specified more than once in base-specified");
15979               duplicate_virtual_error_issued_p = true;
15980             }
15981
15982           virtual_p = true;
15983
15984           /* Consume the `virtual' token.  */
15985           cp_lexer_consume_token (parser->lexer);
15986
15987           break;
15988
15989         case RID_PUBLIC:
15990         case RID_PROTECTED:
15991         case RID_PRIVATE:
15992           /* If more than one access specifier appears, issue an
15993              error.  */
15994           if (access != access_default_node
15995               && !duplicate_access_error_issued_p)
15996             {
15997               cp_parser_error (parser,
15998                                "more than one access specifier in base-specified");
15999               duplicate_access_error_issued_p = true;
16000             }
16001
16002           access = ridpointers[(int) token->keyword];
16003
16004           /* Consume the access-specifier.  */
16005           cp_lexer_consume_token (parser->lexer);
16006
16007           break;
16008
16009         default:
16010           done = true;
16011           break;
16012         }
16013     }
16014   /* It is not uncommon to see programs mechanically, erroneously, use
16015      the 'typename' keyword to denote (dependent) qualified types
16016      as base classes.  */
16017   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16018     {
16019       token = cp_lexer_peek_token (parser->lexer);
16020       if (!processing_template_decl)
16021         error ("%Hkeyword %<typename%> not allowed outside of templates",
16022                &token->location);
16023       else
16024         error ("%Hkeyword %<typename%> not allowed in this context "
16025                "(the base class is implicitly a type)",
16026                &token->location);
16027       cp_lexer_consume_token (parser->lexer);
16028     }
16029
16030   /* Look for the optional `::' operator.  */
16031   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16032   /* Look for the nested-name-specifier.  The simplest way to
16033      implement:
16034
16035        [temp.res]
16036
16037        The keyword `typename' is not permitted in a base-specifier or
16038        mem-initializer; in these contexts a qualified name that
16039        depends on a template-parameter is implicitly assumed to be a
16040        type name.
16041
16042      is to pretend that we have seen the `typename' keyword at this
16043      point.  */
16044   cp_parser_nested_name_specifier_opt (parser,
16045                                        /*typename_keyword_p=*/true,
16046                                        /*check_dependency_p=*/true,
16047                                        typename_type,
16048                                        /*is_declaration=*/true);
16049   /* If the base class is given by a qualified name, assume that names
16050      we see are type names or templates, as appropriate.  */
16051   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16052   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16053
16054   /* Finally, look for the class-name.  */
16055   type = cp_parser_class_name (parser,
16056                                class_scope_p,
16057                                template_p,
16058                                typename_type,
16059                                /*check_dependency_p=*/true,
16060                                /*class_head_p=*/false,
16061                                /*is_declaration=*/true);
16062
16063   if (type == error_mark_node)
16064     return error_mark_node;
16065
16066   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16067 }
16068
16069 /* Exception handling [gram.exception] */
16070
16071 /* Parse an (optional) exception-specification.
16072
16073    exception-specification:
16074      throw ( type-id-list [opt] )
16075
16076    Returns a TREE_LIST representing the exception-specification.  The
16077    TREE_VALUE of each node is a type.  */
16078
16079 static tree
16080 cp_parser_exception_specification_opt (cp_parser* parser)
16081 {
16082   cp_token *token;
16083   tree type_id_list;
16084
16085   /* Peek at the next token.  */
16086   token = cp_lexer_peek_token (parser->lexer);
16087   /* If it's not `throw', then there's no exception-specification.  */
16088   if (!cp_parser_is_keyword (token, RID_THROW))
16089     return NULL_TREE;
16090
16091   /* Consume the `throw'.  */
16092   cp_lexer_consume_token (parser->lexer);
16093
16094   /* Look for the `('.  */
16095   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16096
16097   /* Peek at the next token.  */
16098   token = cp_lexer_peek_token (parser->lexer);
16099   /* If it's not a `)', then there is a type-id-list.  */
16100   if (token->type != CPP_CLOSE_PAREN)
16101     {
16102       const char *saved_message;
16103
16104       /* Types may not be defined in an exception-specification.  */
16105       saved_message = parser->type_definition_forbidden_message;
16106       parser->type_definition_forbidden_message
16107         = "types may not be defined in an exception-specification";
16108       /* Parse the type-id-list.  */
16109       type_id_list = cp_parser_type_id_list (parser);
16110       /* Restore the saved message.  */
16111       parser->type_definition_forbidden_message = saved_message;
16112     }
16113   else
16114     type_id_list = empty_except_spec;
16115
16116   /* Look for the `)'.  */
16117   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16118
16119   return type_id_list;
16120 }
16121
16122 /* Parse an (optional) type-id-list.
16123
16124    type-id-list:
16125      type-id ... [opt]
16126      type-id-list , type-id ... [opt]
16127
16128    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16129    in the order that the types were presented.  */
16130
16131 static tree
16132 cp_parser_type_id_list (cp_parser* parser)
16133 {
16134   tree types = NULL_TREE;
16135
16136   while (true)
16137     {
16138       cp_token *token;
16139       tree type;
16140
16141       /* Get the next type-id.  */
16142       type = cp_parser_type_id (parser);
16143       /* Parse the optional ellipsis. */
16144       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16145         {
16146           /* Consume the `...'. */
16147           cp_lexer_consume_token (parser->lexer);
16148
16149           /* Turn the type into a pack expansion expression. */
16150           type = make_pack_expansion (type);
16151         }
16152       /* Add it to the list.  */
16153       types = add_exception_specifier (types, type, /*complain=*/1);
16154       /* Peek at the next token.  */
16155       token = cp_lexer_peek_token (parser->lexer);
16156       /* If it is not a `,', we are done.  */
16157       if (token->type != CPP_COMMA)
16158         break;
16159       /* Consume the `,'.  */
16160       cp_lexer_consume_token (parser->lexer);
16161     }
16162
16163   return nreverse (types);
16164 }
16165
16166 /* Parse a try-block.
16167
16168    try-block:
16169      try compound-statement handler-seq  */
16170
16171 static tree
16172 cp_parser_try_block (cp_parser* parser)
16173 {
16174   tree try_block;
16175
16176   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16177   try_block = begin_try_block ();
16178   cp_parser_compound_statement (parser, NULL, true);
16179   finish_try_block (try_block);
16180   cp_parser_handler_seq (parser);
16181   finish_handler_sequence (try_block);
16182
16183   return try_block;
16184 }
16185
16186 /* Parse a function-try-block.
16187
16188    function-try-block:
16189      try ctor-initializer [opt] function-body handler-seq  */
16190
16191 static bool
16192 cp_parser_function_try_block (cp_parser* parser)
16193 {
16194   tree compound_stmt;
16195   tree try_block;
16196   bool ctor_initializer_p;
16197
16198   /* Look for the `try' keyword.  */
16199   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16200     return false;
16201   /* Let the rest of the front end know where we are.  */
16202   try_block = begin_function_try_block (&compound_stmt);
16203   /* Parse the function-body.  */
16204   ctor_initializer_p
16205     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16206   /* We're done with the `try' part.  */
16207   finish_function_try_block (try_block);
16208   /* Parse the handlers.  */
16209   cp_parser_handler_seq (parser);
16210   /* We're done with the handlers.  */
16211   finish_function_handler_sequence (try_block, compound_stmt);
16212
16213   return ctor_initializer_p;
16214 }
16215
16216 /* Parse a handler-seq.
16217
16218    handler-seq:
16219      handler handler-seq [opt]  */
16220
16221 static void
16222 cp_parser_handler_seq (cp_parser* parser)
16223 {
16224   while (true)
16225     {
16226       cp_token *token;
16227
16228       /* Parse the handler.  */
16229       cp_parser_handler (parser);
16230       /* Peek at the next token.  */
16231       token = cp_lexer_peek_token (parser->lexer);
16232       /* If it's not `catch' then there are no more handlers.  */
16233       if (!cp_parser_is_keyword (token, RID_CATCH))
16234         break;
16235     }
16236 }
16237
16238 /* Parse a handler.
16239
16240    handler:
16241      catch ( exception-declaration ) compound-statement  */
16242
16243 static void
16244 cp_parser_handler (cp_parser* parser)
16245 {
16246   tree handler;
16247   tree declaration;
16248
16249   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16250   handler = begin_handler ();
16251   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16252   declaration = cp_parser_exception_declaration (parser);
16253   finish_handler_parms (declaration, handler);
16254   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16255   cp_parser_compound_statement (parser, NULL, false);
16256   finish_handler (handler);
16257 }
16258
16259 /* Parse an exception-declaration.
16260
16261    exception-declaration:
16262      type-specifier-seq declarator
16263      type-specifier-seq abstract-declarator
16264      type-specifier-seq
16265      ...
16266
16267    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16268    ellipsis variant is used.  */
16269
16270 static tree
16271 cp_parser_exception_declaration (cp_parser* parser)
16272 {
16273   cp_decl_specifier_seq type_specifiers;
16274   cp_declarator *declarator;
16275   const char *saved_message;
16276
16277   /* If it's an ellipsis, it's easy to handle.  */
16278   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16279     {
16280       /* Consume the `...' token.  */
16281       cp_lexer_consume_token (parser->lexer);
16282       return NULL_TREE;
16283     }
16284
16285   /* Types may not be defined in exception-declarations.  */
16286   saved_message = parser->type_definition_forbidden_message;
16287   parser->type_definition_forbidden_message
16288     = "types may not be defined in exception-declarations";
16289
16290   /* Parse the type-specifier-seq.  */
16291   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16292                                 &type_specifiers);
16293   /* If it's a `)', then there is no declarator.  */
16294   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16295     declarator = NULL;
16296   else
16297     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16298                                        /*ctor_dtor_or_conv_p=*/NULL,
16299                                        /*parenthesized_p=*/NULL,
16300                                        /*member_p=*/false);
16301
16302   /* Restore the saved message.  */
16303   parser->type_definition_forbidden_message = saved_message;
16304
16305   if (!type_specifiers.any_specifiers_p)
16306     return error_mark_node;
16307
16308   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16309 }
16310
16311 /* Parse a throw-expression.
16312
16313    throw-expression:
16314      throw assignment-expression [opt]
16315
16316    Returns a THROW_EXPR representing the throw-expression.  */
16317
16318 static tree
16319 cp_parser_throw_expression (cp_parser* parser)
16320 {
16321   tree expression;
16322   cp_token* token;
16323
16324   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16325   token = cp_lexer_peek_token (parser->lexer);
16326   /* Figure out whether or not there is an assignment-expression
16327      following the "throw" keyword.  */
16328   if (token->type == CPP_COMMA
16329       || token->type == CPP_SEMICOLON
16330       || token->type == CPP_CLOSE_PAREN
16331       || token->type == CPP_CLOSE_SQUARE
16332       || token->type == CPP_CLOSE_BRACE
16333       || token->type == CPP_COLON)
16334     expression = NULL_TREE;
16335   else
16336     expression = cp_parser_assignment_expression (parser,
16337                                                   /*cast_p=*/false);
16338
16339   return build_throw (expression);
16340 }
16341
16342 /* GNU Extensions */
16343
16344 /* Parse an (optional) asm-specification.
16345
16346    asm-specification:
16347      asm ( string-literal )
16348
16349    If the asm-specification is present, returns a STRING_CST
16350    corresponding to the string-literal.  Otherwise, returns
16351    NULL_TREE.  */
16352
16353 static tree
16354 cp_parser_asm_specification_opt (cp_parser* parser)
16355 {
16356   cp_token *token;
16357   tree asm_specification;
16358
16359   /* Peek at the next token.  */
16360   token = cp_lexer_peek_token (parser->lexer);
16361   /* If the next token isn't the `asm' keyword, then there's no
16362      asm-specification.  */
16363   if (!cp_parser_is_keyword (token, RID_ASM))
16364     return NULL_TREE;
16365
16366   /* Consume the `asm' token.  */
16367   cp_lexer_consume_token (parser->lexer);
16368   /* Look for the `('.  */
16369   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16370
16371   /* Look for the string-literal.  */
16372   asm_specification = cp_parser_string_literal (parser, false, false);
16373
16374   /* Look for the `)'.  */
16375   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16376
16377   return asm_specification;
16378 }
16379
16380 /* Parse an asm-operand-list.
16381
16382    asm-operand-list:
16383      asm-operand
16384      asm-operand-list , asm-operand
16385
16386    asm-operand:
16387      string-literal ( expression )
16388      [ string-literal ] string-literal ( expression )
16389
16390    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16391    each node is the expression.  The TREE_PURPOSE is itself a
16392    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16393    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16394    is a STRING_CST for the string literal before the parenthesis. Returns
16395    ERROR_MARK_NODE if any of the operands are invalid.  */
16396
16397 static tree
16398 cp_parser_asm_operand_list (cp_parser* parser)
16399 {
16400   tree asm_operands = NULL_TREE;
16401   bool invalid_operands = false;
16402
16403   while (true)
16404     {
16405       tree string_literal;
16406       tree expression;
16407       tree name;
16408
16409       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16410         {
16411           /* Consume the `[' token.  */
16412           cp_lexer_consume_token (parser->lexer);
16413           /* Read the operand name.  */
16414           name = cp_parser_identifier (parser);
16415           if (name != error_mark_node)
16416             name = build_string (IDENTIFIER_LENGTH (name),
16417                                  IDENTIFIER_POINTER (name));
16418           /* Look for the closing `]'.  */
16419           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16420         }
16421       else
16422         name = NULL_TREE;
16423       /* Look for the string-literal.  */
16424       string_literal = cp_parser_string_literal (parser, false, false);
16425
16426       /* Look for the `('.  */
16427       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16428       /* Parse the expression.  */
16429       expression = cp_parser_expression (parser, /*cast_p=*/false);
16430       /* Look for the `)'.  */
16431       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16432
16433       if (name == error_mark_node 
16434           || string_literal == error_mark_node 
16435           || expression == error_mark_node)
16436         invalid_operands = true;
16437
16438       /* Add this operand to the list.  */
16439       asm_operands = tree_cons (build_tree_list (name, string_literal),
16440                                 expression,
16441                                 asm_operands);
16442       /* If the next token is not a `,', there are no more
16443          operands.  */
16444       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16445         break;
16446       /* Consume the `,'.  */
16447       cp_lexer_consume_token (parser->lexer);
16448     }
16449
16450   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16451 }
16452
16453 /* Parse an asm-clobber-list.
16454
16455    asm-clobber-list:
16456      string-literal
16457      asm-clobber-list , string-literal
16458
16459    Returns a TREE_LIST, indicating the clobbers in the order that they
16460    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16461
16462 static tree
16463 cp_parser_asm_clobber_list (cp_parser* parser)
16464 {
16465   tree clobbers = NULL_TREE;
16466
16467   while (true)
16468     {
16469       tree string_literal;
16470
16471       /* Look for the string literal.  */
16472       string_literal = cp_parser_string_literal (parser, false, false);
16473       /* Add it to the list.  */
16474       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16475       /* If the next token is not a `,', then the list is
16476          complete.  */
16477       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16478         break;
16479       /* Consume the `,' token.  */
16480       cp_lexer_consume_token (parser->lexer);
16481     }
16482
16483   return clobbers;
16484 }
16485
16486 /* Parse an (optional) series of attributes.
16487
16488    attributes:
16489      attributes attribute
16490
16491    attribute:
16492      __attribute__ (( attribute-list [opt] ))
16493
16494    The return value is as for cp_parser_attribute_list.  */
16495
16496 static tree
16497 cp_parser_attributes_opt (cp_parser* parser)
16498 {
16499   tree attributes = NULL_TREE;
16500
16501   while (true)
16502     {
16503       cp_token *token;
16504       tree attribute_list;
16505
16506       /* Peek at the next token.  */
16507       token = cp_lexer_peek_token (parser->lexer);
16508       /* If it's not `__attribute__', then we're done.  */
16509       if (token->keyword != RID_ATTRIBUTE)
16510         break;
16511
16512       /* Consume the `__attribute__' keyword.  */
16513       cp_lexer_consume_token (parser->lexer);
16514       /* Look for the two `(' tokens.  */
16515       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16516       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16517
16518       /* Peek at the next token.  */
16519       token = cp_lexer_peek_token (parser->lexer);
16520       if (token->type != CPP_CLOSE_PAREN)
16521         /* Parse the attribute-list.  */
16522         attribute_list = cp_parser_attribute_list (parser);
16523       else
16524         /* If the next token is a `)', then there is no attribute
16525            list.  */
16526         attribute_list = NULL;
16527
16528       /* Look for the two `)' tokens.  */
16529       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16530       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16531
16532       /* Add these new attributes to the list.  */
16533       attributes = chainon (attributes, attribute_list);
16534     }
16535
16536   return attributes;
16537 }
16538
16539 /* Parse an attribute-list.
16540
16541    attribute-list:
16542      attribute
16543      attribute-list , attribute
16544
16545    attribute:
16546      identifier
16547      identifier ( identifier )
16548      identifier ( identifier , expression-list )
16549      identifier ( expression-list )
16550
16551    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16552    to an attribute.  The TREE_PURPOSE of each node is the identifier
16553    indicating which attribute is in use.  The TREE_VALUE represents
16554    the arguments, if any.  */
16555
16556 static tree
16557 cp_parser_attribute_list (cp_parser* parser)
16558 {
16559   tree attribute_list = NULL_TREE;
16560   bool save_translate_strings_p = parser->translate_strings_p;
16561
16562   parser->translate_strings_p = false;
16563   while (true)
16564     {
16565       cp_token *token;
16566       tree identifier;
16567       tree attribute;
16568
16569       /* Look for the identifier.  We also allow keywords here; for
16570          example `__attribute__ ((const))' is legal.  */
16571       token = cp_lexer_peek_token (parser->lexer);
16572       if (token->type == CPP_NAME
16573           || token->type == CPP_KEYWORD)
16574         {
16575           tree arguments = NULL_TREE;
16576
16577           /* Consume the token.  */
16578           token = cp_lexer_consume_token (parser->lexer);
16579
16580           /* Save away the identifier that indicates which attribute
16581              this is.  */
16582           identifier = token->u.value;
16583           attribute = build_tree_list (identifier, NULL_TREE);
16584
16585           /* Peek at the next token.  */
16586           token = cp_lexer_peek_token (parser->lexer);
16587           /* If it's an `(', then parse the attribute arguments.  */
16588           if (token->type == CPP_OPEN_PAREN)
16589             {
16590               arguments = cp_parser_parenthesized_expression_list
16591                           (parser, true, /*cast_p=*/false,
16592                            /*allow_expansion_p=*/false,
16593                            /*non_constant_p=*/NULL);
16594               /* Save the arguments away.  */
16595               TREE_VALUE (attribute) = arguments;
16596             }
16597
16598           if (arguments != error_mark_node)
16599             {
16600               /* Add this attribute to the list.  */
16601               TREE_CHAIN (attribute) = attribute_list;
16602               attribute_list = attribute;
16603             }
16604
16605           token = cp_lexer_peek_token (parser->lexer);
16606         }
16607       /* Now, look for more attributes.  If the next token isn't a
16608          `,', we're done.  */
16609       if (token->type != CPP_COMMA)
16610         break;
16611
16612       /* Consume the comma and keep going.  */
16613       cp_lexer_consume_token (parser->lexer);
16614     }
16615   parser->translate_strings_p = save_translate_strings_p;
16616
16617   /* We built up the list in reverse order.  */
16618   return nreverse (attribute_list);
16619 }
16620
16621 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16622    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16623    current value of the PEDANTIC flag, regardless of whether or not
16624    the `__extension__' keyword is present.  The caller is responsible
16625    for restoring the value of the PEDANTIC flag.  */
16626
16627 static bool
16628 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16629 {
16630   /* Save the old value of the PEDANTIC flag.  */
16631   *saved_pedantic = pedantic;
16632
16633   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16634     {
16635       /* Consume the `__extension__' token.  */
16636       cp_lexer_consume_token (parser->lexer);
16637       /* We're not being pedantic while the `__extension__' keyword is
16638          in effect.  */
16639       pedantic = 0;
16640
16641       return true;
16642     }
16643
16644   return false;
16645 }
16646
16647 /* Parse a label declaration.
16648
16649    label-declaration:
16650      __label__ label-declarator-seq ;
16651
16652    label-declarator-seq:
16653      identifier , label-declarator-seq
16654      identifier  */
16655
16656 static void
16657 cp_parser_label_declaration (cp_parser* parser)
16658 {
16659   /* Look for the `__label__' keyword.  */
16660   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16661
16662   while (true)
16663     {
16664       tree identifier;
16665
16666       /* Look for an identifier.  */
16667       identifier = cp_parser_identifier (parser);
16668       /* If we failed, stop.  */
16669       if (identifier == error_mark_node)
16670         break;
16671       /* Declare it as a label.  */
16672       finish_label_decl (identifier);
16673       /* If the next token is a `;', stop.  */
16674       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16675         break;
16676       /* Look for the `,' separating the label declarations.  */
16677       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16678     }
16679
16680   /* Look for the final `;'.  */
16681   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16682 }
16683
16684 /* Support Functions */
16685
16686 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16687    NAME should have one of the representations used for an
16688    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16689    is returned.  If PARSER->SCOPE is a dependent type, then a
16690    SCOPE_REF is returned.
16691
16692    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16693    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16694    was formed.  Abstractly, such entities should not be passed to this
16695    function, because they do not need to be looked up, but it is
16696    simpler to check for this special case here, rather than at the
16697    call-sites.
16698
16699    In cases not explicitly covered above, this function returns a
16700    DECL, OVERLOAD, or baselink representing the result of the lookup.
16701    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16702    is returned.
16703
16704    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16705    (e.g., "struct") that was used.  In that case bindings that do not
16706    refer to types are ignored.
16707
16708    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16709    ignored.
16710
16711    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16712    are ignored.
16713
16714    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16715    types.
16716
16717    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16718    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16719    NULL_TREE otherwise.  */
16720
16721 static tree
16722 cp_parser_lookup_name (cp_parser *parser, tree name,
16723                        enum tag_types tag_type,
16724                        bool is_template,
16725                        bool is_namespace,
16726                        bool check_dependency,
16727                        tree *ambiguous_decls,
16728                        location_t name_location)
16729 {
16730   int flags = 0;
16731   tree decl;
16732   tree object_type = parser->context->object_type;
16733
16734   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16735     flags |= LOOKUP_COMPLAIN;
16736
16737   /* Assume that the lookup will be unambiguous.  */
16738   if (ambiguous_decls)
16739     *ambiguous_decls = NULL_TREE;
16740
16741   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16742      no longer valid.  Note that if we are parsing tentatively, and
16743      the parse fails, OBJECT_TYPE will be automatically restored.  */
16744   parser->context->object_type = NULL_TREE;
16745
16746   if (name == error_mark_node)
16747     return error_mark_node;
16748
16749   /* A template-id has already been resolved; there is no lookup to
16750      do.  */
16751   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16752     return name;
16753   if (BASELINK_P (name))
16754     {
16755       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16756                   == TEMPLATE_ID_EXPR);
16757       return name;
16758     }
16759
16760   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16761      it should already have been checked to make sure that the name
16762      used matches the type being destroyed.  */
16763   if (TREE_CODE (name) == BIT_NOT_EXPR)
16764     {
16765       tree type;
16766
16767       /* Figure out to which type this destructor applies.  */
16768       if (parser->scope)
16769         type = parser->scope;
16770       else if (object_type)
16771         type = object_type;
16772       else
16773         type = current_class_type;
16774       /* If that's not a class type, there is no destructor.  */
16775       if (!type || !CLASS_TYPE_P (type))
16776         return error_mark_node;
16777       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16778         lazily_declare_fn (sfk_destructor, type);
16779       if (!CLASSTYPE_DESTRUCTORS (type))
16780           return error_mark_node;
16781       /* If it was a class type, return the destructor.  */
16782       return CLASSTYPE_DESTRUCTORS (type);
16783     }
16784
16785   /* By this point, the NAME should be an ordinary identifier.  If
16786      the id-expression was a qualified name, the qualifying scope is
16787      stored in PARSER->SCOPE at this point.  */
16788   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16789
16790   /* Perform the lookup.  */
16791   if (parser->scope)
16792     {
16793       bool dependent_p;
16794
16795       if (parser->scope == error_mark_node)
16796         return error_mark_node;
16797
16798       /* If the SCOPE is dependent, the lookup must be deferred until
16799          the template is instantiated -- unless we are explicitly
16800          looking up names in uninstantiated templates.  Even then, we
16801          cannot look up the name if the scope is not a class type; it
16802          might, for example, be a template type parameter.  */
16803       dependent_p = (TYPE_P (parser->scope)
16804                      && !(parser->in_declarator_p
16805                           && currently_open_class (parser->scope))
16806                      && dependent_type_p (parser->scope));
16807       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16808            && dependent_p)
16809         {
16810           if (tag_type)
16811             {
16812               tree type;
16813
16814               /* The resolution to Core Issue 180 says that `struct
16815                  A::B' should be considered a type-name, even if `A'
16816                  is dependent.  */
16817               type = make_typename_type (parser->scope, name, tag_type,
16818                                          /*complain=*/tf_error);
16819               decl = TYPE_NAME (type);
16820             }
16821           else if (is_template
16822                    && (cp_parser_next_token_ends_template_argument_p (parser)
16823                        || cp_lexer_next_token_is (parser->lexer,
16824                                                   CPP_CLOSE_PAREN)))
16825             decl = make_unbound_class_template (parser->scope,
16826                                                 name, NULL_TREE,
16827                                                 /*complain=*/tf_error);
16828           else
16829             decl = build_qualified_name (/*type=*/NULL_TREE,
16830                                          parser->scope, name,
16831                                          is_template);
16832         }
16833       else
16834         {
16835           tree pushed_scope = NULL_TREE;
16836
16837           /* If PARSER->SCOPE is a dependent type, then it must be a
16838              class type, and we must not be checking dependencies;
16839              otherwise, we would have processed this lookup above.  So
16840              that PARSER->SCOPE is not considered a dependent base by
16841              lookup_member, we must enter the scope here.  */
16842           if (dependent_p)
16843             pushed_scope = push_scope (parser->scope);
16844           /* If the PARSER->SCOPE is a template specialization, it
16845              may be instantiated during name lookup.  In that case,
16846              errors may be issued.  Even if we rollback the current
16847              tentative parse, those errors are valid.  */
16848           decl = lookup_qualified_name (parser->scope, name,
16849                                         tag_type != none_type,
16850                                         /*complain=*/true);
16851
16852           /* If we have a single function from a using decl, pull it out.  */
16853           if (decl
16854               && TREE_CODE (decl) == OVERLOAD
16855               && !really_overloaded_fn (decl))
16856             decl = OVL_FUNCTION (decl);
16857
16858           if (pushed_scope)
16859             pop_scope (pushed_scope);
16860         }
16861       parser->qualifying_scope = parser->scope;
16862       parser->object_scope = NULL_TREE;
16863     }
16864   else if (object_type)
16865     {
16866       tree object_decl = NULL_TREE;
16867       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16868          OBJECT_TYPE is not a class.  */
16869       if (CLASS_TYPE_P (object_type))
16870         /* If the OBJECT_TYPE is a template specialization, it may
16871            be instantiated during name lookup.  In that case, errors
16872            may be issued.  Even if we rollback the current tentative
16873            parse, those errors are valid.  */
16874         object_decl = lookup_member (object_type,
16875                                      name,
16876                                      /*protect=*/0,
16877                                      tag_type != none_type);
16878       /* Look it up in the enclosing context, too.  */
16879       decl = lookup_name_real (name, tag_type != none_type,
16880                                /*nonclass=*/0,
16881                                /*block_p=*/true, is_namespace, flags);
16882       parser->object_scope = object_type;
16883       parser->qualifying_scope = NULL_TREE;
16884       if (object_decl)
16885         decl = object_decl;
16886     }
16887   else
16888     {
16889       decl = lookup_name_real (name, tag_type != none_type,
16890                                /*nonclass=*/0,
16891                                /*block_p=*/true, is_namespace, flags);
16892       parser->qualifying_scope = NULL_TREE;
16893       parser->object_scope = NULL_TREE;
16894     }
16895
16896   /* If the lookup failed, let our caller know.  */
16897   if (!decl || decl == error_mark_node)
16898     return error_mark_node;
16899
16900   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16901   if (TREE_CODE (decl) == TREE_LIST)
16902     {
16903       if (ambiguous_decls)
16904         *ambiguous_decls = decl;
16905       /* The error message we have to print is too complicated for
16906          cp_parser_error, so we incorporate its actions directly.  */
16907       if (!cp_parser_simulate_error (parser))
16908         {
16909           error ("%Hreference to %qD is ambiguous",
16910                  &name_location, name);
16911           print_candidates (decl);
16912         }
16913       return error_mark_node;
16914     }
16915
16916   gcc_assert (DECL_P (decl)
16917               || TREE_CODE (decl) == OVERLOAD
16918               || TREE_CODE (decl) == SCOPE_REF
16919               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16920               || BASELINK_P (decl));
16921
16922   /* If we have resolved the name of a member declaration, check to
16923      see if the declaration is accessible.  When the name resolves to
16924      set of overloaded functions, accessibility is checked when
16925      overload resolution is done.
16926
16927      During an explicit instantiation, access is not checked at all,
16928      as per [temp.explicit].  */
16929   if (DECL_P (decl))
16930     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16931
16932   return decl;
16933 }
16934
16935 /* Like cp_parser_lookup_name, but for use in the typical case where
16936    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16937    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16938
16939 static tree
16940 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16941 {
16942   return cp_parser_lookup_name (parser, name,
16943                                 none_type,
16944                                 /*is_template=*/false,
16945                                 /*is_namespace=*/false,
16946                                 /*check_dependency=*/true,
16947                                 /*ambiguous_decls=*/NULL,
16948                                 location);
16949 }
16950
16951 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16952    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16953    true, the DECL indicates the class being defined in a class-head,
16954    or declared in an elaborated-type-specifier.
16955
16956    Otherwise, return DECL.  */
16957
16958 static tree
16959 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16960 {
16961   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16962      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16963
16964        struct A {
16965          template <typename T> struct B;
16966        };
16967
16968        template <typename T> struct A::B {};
16969
16970      Similarly, in an elaborated-type-specifier:
16971
16972        namespace N { struct X{}; }
16973
16974        struct A {
16975          template <typename T> friend struct N::X;
16976        };
16977
16978      However, if the DECL refers to a class type, and we are in
16979      the scope of the class, then the name lookup automatically
16980      finds the TYPE_DECL created by build_self_reference rather
16981      than a TEMPLATE_DECL.  For example, in:
16982
16983        template <class T> struct S {
16984          S s;
16985        };
16986
16987      there is no need to handle such case.  */
16988
16989   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16990     return DECL_TEMPLATE_RESULT (decl);
16991
16992   return decl;
16993 }
16994
16995 /* If too many, or too few, template-parameter lists apply to the
16996    declarator, issue an error message.  Returns TRUE if all went well,
16997    and FALSE otherwise.  */
16998
16999 static bool
17000 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17001                                                 cp_declarator *declarator,
17002                                                 location_t declarator_location)
17003 {
17004   unsigned num_templates;
17005
17006   /* We haven't seen any classes that involve template parameters yet.  */
17007   num_templates = 0;
17008
17009   switch (declarator->kind)
17010     {
17011     case cdk_id:
17012       if (declarator->u.id.qualifying_scope)
17013         {
17014           tree scope;
17015           tree member;
17016
17017           scope = declarator->u.id.qualifying_scope;
17018           member = declarator->u.id.unqualified_name;
17019
17020           while (scope && CLASS_TYPE_P (scope))
17021             {
17022               /* You're supposed to have one `template <...>'
17023                  for every template class, but you don't need one
17024                  for a full specialization.  For example:
17025
17026                  template <class T> struct S{};
17027                  template <> struct S<int> { void f(); };
17028                  void S<int>::f () {}
17029
17030                  is correct; there shouldn't be a `template <>' for
17031                  the definition of `S<int>::f'.  */
17032               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17033                 /* If SCOPE does not have template information of any
17034                    kind, then it is not a template, nor is it nested
17035                    within a template.  */
17036                 break;
17037               if (explicit_class_specialization_p (scope))
17038                 break;
17039               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17040                 ++num_templates;
17041
17042               scope = TYPE_CONTEXT (scope);
17043             }
17044         }
17045       else if (TREE_CODE (declarator->u.id.unqualified_name)
17046                == TEMPLATE_ID_EXPR)
17047         /* If the DECLARATOR has the form `X<y>' then it uses one
17048            additional level of template parameters.  */
17049         ++num_templates;
17050
17051       return cp_parser_check_template_parameters (parser,
17052                                                   num_templates,
17053                                                   declarator_location);
17054
17055     case cdk_function:
17056     case cdk_array:
17057     case cdk_pointer:
17058     case cdk_reference:
17059     case cdk_ptrmem:
17060       return (cp_parser_check_declarator_template_parameters
17061               (parser, declarator->declarator, declarator_location));
17062
17063     case cdk_error:
17064       return true;
17065
17066     default:
17067       gcc_unreachable ();
17068     }
17069   return false;
17070 }
17071
17072 /* NUM_TEMPLATES were used in the current declaration.  If that is
17073    invalid, return FALSE and issue an error messages.  Otherwise,
17074    return TRUE.  */
17075
17076 static bool
17077 cp_parser_check_template_parameters (cp_parser* parser,
17078                                      unsigned num_templates,
17079                                      location_t location)
17080 {
17081   /* If there are more template classes than parameter lists, we have
17082      something like:
17083
17084        template <class T> void S<T>::R<T>::f ();  */
17085   if (parser->num_template_parameter_lists < num_templates)
17086     {
17087       error ("%Htoo few template-parameter-lists", &location);
17088       return false;
17089     }
17090   /* If there are the same number of template classes and parameter
17091      lists, that's OK.  */
17092   if (parser->num_template_parameter_lists == num_templates)
17093     return true;
17094   /* If there are more, but only one more, then we are referring to a
17095      member template.  That's OK too.  */
17096   if (parser->num_template_parameter_lists == num_templates + 1)
17097       return true;
17098   /* Otherwise, there are too many template parameter lists.  We have
17099      something like:
17100
17101      template <class T> template <class U> void S::f();  */
17102   error ("%Htoo many template-parameter-lists", &location);
17103   return false;
17104 }
17105
17106 /* Parse an optional `::' token indicating that the following name is
17107    from the global namespace.  If so, PARSER->SCOPE is set to the
17108    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17109    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17110    Returns the new value of PARSER->SCOPE, if the `::' token is
17111    present, and NULL_TREE otherwise.  */
17112
17113 static tree
17114 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17115 {
17116   cp_token *token;
17117
17118   /* Peek at the next token.  */
17119   token = cp_lexer_peek_token (parser->lexer);
17120   /* If we're looking at a `::' token then we're starting from the
17121      global namespace, not our current location.  */
17122   if (token->type == CPP_SCOPE)
17123     {
17124       /* Consume the `::' token.  */
17125       cp_lexer_consume_token (parser->lexer);
17126       /* Set the SCOPE so that we know where to start the lookup.  */
17127       parser->scope = global_namespace;
17128       parser->qualifying_scope = global_namespace;
17129       parser->object_scope = NULL_TREE;
17130
17131       return parser->scope;
17132     }
17133   else if (!current_scope_valid_p)
17134     {
17135       parser->scope = NULL_TREE;
17136       parser->qualifying_scope = NULL_TREE;
17137       parser->object_scope = NULL_TREE;
17138     }
17139
17140   return NULL_TREE;
17141 }
17142
17143 /* Returns TRUE if the upcoming token sequence is the start of a
17144    constructor declarator.  If FRIEND_P is true, the declarator is
17145    preceded by the `friend' specifier.  */
17146
17147 static bool
17148 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17149 {
17150   bool constructor_p;
17151   tree type_decl = NULL_TREE;
17152   bool nested_name_p;
17153   cp_token *next_token;
17154
17155   /* The common case is that this is not a constructor declarator, so
17156      try to avoid doing lots of work if at all possible.  It's not
17157      valid declare a constructor at function scope.  */
17158   if (parser->in_function_body)
17159     return false;
17160   /* And only certain tokens can begin a constructor declarator.  */
17161   next_token = cp_lexer_peek_token (parser->lexer);
17162   if (next_token->type != CPP_NAME
17163       && next_token->type != CPP_SCOPE
17164       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17165       && next_token->type != CPP_TEMPLATE_ID)
17166     return false;
17167
17168   /* Parse tentatively; we are going to roll back all of the tokens
17169      consumed here.  */
17170   cp_parser_parse_tentatively (parser);
17171   /* Assume that we are looking at a constructor declarator.  */
17172   constructor_p = true;
17173
17174   /* Look for the optional `::' operator.  */
17175   cp_parser_global_scope_opt (parser,
17176                               /*current_scope_valid_p=*/false);
17177   /* Look for the nested-name-specifier.  */
17178   nested_name_p
17179     = (cp_parser_nested_name_specifier_opt (parser,
17180                                             /*typename_keyword_p=*/false,
17181                                             /*check_dependency_p=*/false,
17182                                             /*type_p=*/false,
17183                                             /*is_declaration=*/false)
17184        != NULL_TREE);
17185   /* Outside of a class-specifier, there must be a
17186      nested-name-specifier.  */
17187   if (!nested_name_p &&
17188       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17189        || friend_p))
17190     constructor_p = false;
17191   /* If we still think that this might be a constructor-declarator,
17192      look for a class-name.  */
17193   if (constructor_p)
17194     {
17195       /* If we have:
17196
17197            template <typename T> struct S { S(); };
17198            template <typename T> S<T>::S ();
17199
17200          we must recognize that the nested `S' names a class.
17201          Similarly, for:
17202
17203            template <typename T> S<T>::S<T> ();
17204
17205          we must recognize that the nested `S' names a template.  */
17206       type_decl = cp_parser_class_name (parser,
17207                                         /*typename_keyword_p=*/false,
17208                                         /*template_keyword_p=*/false,
17209                                         none_type,
17210                                         /*check_dependency_p=*/false,
17211                                         /*class_head_p=*/false,
17212                                         /*is_declaration=*/false);
17213       /* If there was no class-name, then this is not a constructor.  */
17214       constructor_p = !cp_parser_error_occurred (parser);
17215     }
17216
17217   /* If we're still considering a constructor, we have to see a `(',
17218      to begin the parameter-declaration-clause, followed by either a
17219      `)', an `...', or a decl-specifier.  We need to check for a
17220      type-specifier to avoid being fooled into thinking that:
17221
17222        S::S (f) (int);
17223
17224      is a constructor.  (It is actually a function named `f' that
17225      takes one parameter (of type `int') and returns a value of type
17226      `S::S'.  */
17227   if (constructor_p
17228       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17229     {
17230       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17231           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17232           /* A parameter declaration begins with a decl-specifier,
17233              which is either the "attribute" keyword, a storage class
17234              specifier, or (usually) a type-specifier.  */
17235           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17236         {
17237           tree type;
17238           tree pushed_scope = NULL_TREE;
17239           unsigned saved_num_template_parameter_lists;
17240
17241           /* Names appearing in the type-specifier should be looked up
17242              in the scope of the class.  */
17243           if (current_class_type)
17244             type = NULL_TREE;
17245           else
17246             {
17247               type = TREE_TYPE (type_decl);
17248               if (TREE_CODE (type) == TYPENAME_TYPE)
17249                 {
17250                   type = resolve_typename_type (type,
17251                                                 /*only_current_p=*/false);
17252                   if (TREE_CODE (type) == TYPENAME_TYPE)
17253                     {
17254                       cp_parser_abort_tentative_parse (parser);
17255                       return false;
17256                     }
17257                 }
17258               pushed_scope = push_scope (type);
17259             }
17260
17261           /* Inside the constructor parameter list, surrounding
17262              template-parameter-lists do not apply.  */
17263           saved_num_template_parameter_lists
17264             = parser->num_template_parameter_lists;
17265           parser->num_template_parameter_lists = 0;
17266
17267           /* Look for the type-specifier.  */
17268           cp_parser_type_specifier (parser,
17269                                     CP_PARSER_FLAGS_NONE,
17270                                     /*decl_specs=*/NULL,
17271                                     /*is_declarator=*/true,
17272                                     /*declares_class_or_enum=*/NULL,
17273                                     /*is_cv_qualifier=*/NULL);
17274
17275           parser->num_template_parameter_lists
17276             = saved_num_template_parameter_lists;
17277
17278           /* Leave the scope of the class.  */
17279           if (pushed_scope)
17280             pop_scope (pushed_scope);
17281
17282           constructor_p = !cp_parser_error_occurred (parser);
17283         }
17284     }
17285   else
17286     constructor_p = false;
17287   /* We did not really want to consume any tokens.  */
17288   cp_parser_abort_tentative_parse (parser);
17289
17290   return constructor_p;
17291 }
17292
17293 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17294    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17295    they must be performed once we are in the scope of the function.
17296
17297    Returns the function defined.  */
17298
17299 static tree
17300 cp_parser_function_definition_from_specifiers_and_declarator
17301   (cp_parser* parser,
17302    cp_decl_specifier_seq *decl_specifiers,
17303    tree attributes,
17304    const cp_declarator *declarator)
17305 {
17306   tree fn;
17307   bool success_p;
17308
17309   /* Begin the function-definition.  */
17310   success_p = start_function (decl_specifiers, declarator, attributes);
17311
17312   /* The things we're about to see are not directly qualified by any
17313      template headers we've seen thus far.  */
17314   reset_specialization ();
17315
17316   /* If there were names looked up in the decl-specifier-seq that we
17317      did not check, check them now.  We must wait until we are in the
17318      scope of the function to perform the checks, since the function
17319      might be a friend.  */
17320   perform_deferred_access_checks ();
17321
17322   if (!success_p)
17323     {
17324       /* Skip the entire function.  */
17325       cp_parser_skip_to_end_of_block_or_statement (parser);
17326       fn = error_mark_node;
17327     }
17328   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17329     {
17330       /* Seen already, skip it.  An error message has already been output.  */
17331       cp_parser_skip_to_end_of_block_or_statement (parser);
17332       fn = current_function_decl;
17333       current_function_decl = NULL_TREE;
17334       /* If this is a function from a class, pop the nested class.  */
17335       if (current_class_name)
17336         pop_nested_class ();
17337     }
17338   else
17339     fn = cp_parser_function_definition_after_declarator (parser,
17340                                                          /*inline_p=*/false);
17341
17342   return fn;
17343 }
17344
17345 /* Parse the part of a function-definition that follows the
17346    declarator.  INLINE_P is TRUE iff this function is an inline
17347    function defined with a class-specifier.
17348
17349    Returns the function defined.  */
17350
17351 static tree
17352 cp_parser_function_definition_after_declarator (cp_parser* parser,
17353                                                 bool inline_p)
17354 {
17355   tree fn;
17356   bool ctor_initializer_p = false;
17357   bool saved_in_unbraced_linkage_specification_p;
17358   bool saved_in_function_body;
17359   unsigned saved_num_template_parameter_lists;
17360   cp_token *token;
17361
17362   saved_in_function_body = parser->in_function_body;
17363   parser->in_function_body = true;
17364   /* If the next token is `return', then the code may be trying to
17365      make use of the "named return value" extension that G++ used to
17366      support.  */
17367   token = cp_lexer_peek_token (parser->lexer);
17368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17369     {
17370       /* Consume the `return' keyword.  */
17371       cp_lexer_consume_token (parser->lexer);
17372       /* Look for the identifier that indicates what value is to be
17373          returned.  */
17374       cp_parser_identifier (parser);
17375       /* Issue an error message.  */
17376       error ("%Hnamed return values are no longer supported",
17377              &token->location);
17378       /* Skip tokens until we reach the start of the function body.  */
17379       while (true)
17380         {
17381           cp_token *token = cp_lexer_peek_token (parser->lexer);
17382           if (token->type == CPP_OPEN_BRACE
17383               || token->type == CPP_EOF
17384               || token->type == CPP_PRAGMA_EOL)
17385             break;
17386           cp_lexer_consume_token (parser->lexer);
17387         }
17388     }
17389   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17390      anything declared inside `f'.  */
17391   saved_in_unbraced_linkage_specification_p
17392     = parser->in_unbraced_linkage_specification_p;
17393   parser->in_unbraced_linkage_specification_p = false;
17394   /* Inside the function, surrounding template-parameter-lists do not
17395      apply.  */
17396   saved_num_template_parameter_lists
17397     = parser->num_template_parameter_lists;
17398   parser->num_template_parameter_lists = 0;
17399   /* If the next token is `try', then we are looking at a
17400      function-try-block.  */
17401   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17402     ctor_initializer_p = cp_parser_function_try_block (parser);
17403   /* A function-try-block includes the function-body, so we only do
17404      this next part if we're not processing a function-try-block.  */
17405   else
17406     ctor_initializer_p
17407       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17408
17409   /* Finish the function.  */
17410   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17411                         (inline_p ? 2 : 0));
17412   /* Generate code for it, if necessary.  */
17413   expand_or_defer_fn (fn);
17414   /* Restore the saved values.  */
17415   parser->in_unbraced_linkage_specification_p
17416     = saved_in_unbraced_linkage_specification_p;
17417   parser->num_template_parameter_lists
17418     = saved_num_template_parameter_lists;
17419   parser->in_function_body = saved_in_function_body;
17420
17421   return fn;
17422 }
17423
17424 /* Parse a template-declaration, assuming that the `export' (and
17425    `extern') keywords, if present, has already been scanned.  MEMBER_P
17426    is as for cp_parser_template_declaration.  */
17427
17428 static void
17429 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17430 {
17431   tree decl = NULL_TREE;
17432   VEC (deferred_access_check,gc) *checks;
17433   tree parameter_list;
17434   bool friend_p = false;
17435   bool need_lang_pop;
17436   cp_token *token;
17437
17438   /* Look for the `template' keyword.  */
17439   token = cp_lexer_peek_token (parser->lexer);
17440   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17441     return;
17442
17443   /* And the `<'.  */
17444   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17445     return;
17446   if (at_class_scope_p () && current_function_decl)
17447     {
17448       /* 14.5.2.2 [temp.mem]
17449
17450          A local class shall not have member templates.  */
17451       error ("%Hinvalid declaration of member template in local class",
17452              &token->location);
17453       cp_parser_skip_to_end_of_block_or_statement (parser);
17454       return;
17455     }
17456   /* [temp]
17457
17458      A template ... shall not have C linkage.  */
17459   if (current_lang_name == lang_name_c)
17460     {
17461       error ("%Htemplate with C linkage", &token->location);
17462       /* Give it C++ linkage to avoid confusing other parts of the
17463          front end.  */
17464       push_lang_context (lang_name_cplusplus);
17465       need_lang_pop = true;
17466     }
17467   else
17468     need_lang_pop = false;
17469
17470   /* We cannot perform access checks on the template parameter
17471      declarations until we know what is being declared, just as we
17472      cannot check the decl-specifier list.  */
17473   push_deferring_access_checks (dk_deferred);
17474
17475   /* If the next token is `>', then we have an invalid
17476      specialization.  Rather than complain about an invalid template
17477      parameter, issue an error message here.  */
17478   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17479     {
17480       cp_parser_error (parser, "invalid explicit specialization");
17481       begin_specialization ();
17482       parameter_list = NULL_TREE;
17483     }
17484   else
17485     /* Parse the template parameters.  */
17486     parameter_list = cp_parser_template_parameter_list (parser);
17487
17488   /* Get the deferred access checks from the parameter list.  These
17489      will be checked once we know what is being declared, as for a
17490      member template the checks must be performed in the scope of the
17491      class containing the member.  */
17492   checks = get_deferred_access_checks ();
17493
17494   /* Look for the `>'.  */
17495   cp_parser_skip_to_end_of_template_parameter_list (parser);
17496   /* We just processed one more parameter list.  */
17497   ++parser->num_template_parameter_lists;
17498   /* If the next token is `template', there are more template
17499      parameters.  */
17500   if (cp_lexer_next_token_is_keyword (parser->lexer,
17501                                       RID_TEMPLATE))
17502     cp_parser_template_declaration_after_export (parser, member_p);
17503   else
17504     {
17505       /* There are no access checks when parsing a template, as we do not
17506          know if a specialization will be a friend.  */
17507       push_deferring_access_checks (dk_no_check);
17508       token = cp_lexer_peek_token (parser->lexer);
17509       decl = cp_parser_single_declaration (parser,
17510                                            checks,
17511                                            member_p,
17512                                            /*explicit_specialization_p=*/false,
17513                                            &friend_p);
17514       pop_deferring_access_checks ();
17515
17516       /* If this is a member template declaration, let the front
17517          end know.  */
17518       if (member_p && !friend_p && decl)
17519         {
17520           if (TREE_CODE (decl) == TYPE_DECL)
17521             cp_parser_check_access_in_redeclaration (decl, token->location);
17522
17523           decl = finish_member_template_decl (decl);
17524         }
17525       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17526         make_friend_class (current_class_type, TREE_TYPE (decl),
17527                            /*complain=*/true);
17528     }
17529   /* We are done with the current parameter list.  */
17530   --parser->num_template_parameter_lists;
17531
17532   pop_deferring_access_checks ();
17533
17534   /* Finish up.  */
17535   finish_template_decl (parameter_list);
17536
17537   /* Register member declarations.  */
17538   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17539     finish_member_declaration (decl);
17540   /* For the erroneous case of a template with C linkage, we pushed an
17541      implicit C++ linkage scope; exit that scope now.  */
17542   if (need_lang_pop)
17543     pop_lang_context ();
17544   /* If DECL is a function template, we must return to parse it later.
17545      (Even though there is no definition, there might be default
17546      arguments that need handling.)  */
17547   if (member_p && decl
17548       && (TREE_CODE (decl) == FUNCTION_DECL
17549           || DECL_FUNCTION_TEMPLATE_P (decl)))
17550     TREE_VALUE (parser->unparsed_functions_queues)
17551       = tree_cons (NULL_TREE, decl,
17552                    TREE_VALUE (parser->unparsed_functions_queues));
17553 }
17554
17555 /* Perform the deferred access checks from a template-parameter-list.
17556    CHECKS is a TREE_LIST of access checks, as returned by
17557    get_deferred_access_checks.  */
17558
17559 static void
17560 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17561 {
17562   ++processing_template_parmlist;
17563   perform_access_checks (checks);
17564   --processing_template_parmlist;
17565 }
17566
17567 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17568    `function-definition' sequence.  MEMBER_P is true, this declaration
17569    appears in a class scope.
17570
17571    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17572    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17573
17574 static tree
17575 cp_parser_single_declaration (cp_parser* parser,
17576                               VEC (deferred_access_check,gc)* checks,
17577                               bool member_p,
17578                               bool explicit_specialization_p,
17579                               bool* friend_p)
17580 {
17581   int declares_class_or_enum;
17582   tree decl = NULL_TREE;
17583   cp_decl_specifier_seq decl_specifiers;
17584   bool function_definition_p = false;
17585   cp_token *decl_spec_token_start;
17586
17587   /* This function is only used when processing a template
17588      declaration.  */
17589   gcc_assert (innermost_scope_kind () == sk_template_parms
17590               || innermost_scope_kind () == sk_template_spec);
17591
17592   /* Defer access checks until we know what is being declared.  */
17593   push_deferring_access_checks (dk_deferred);
17594
17595   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17596      alternative.  */
17597   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17598   cp_parser_decl_specifier_seq (parser,
17599                                 CP_PARSER_FLAGS_OPTIONAL,
17600                                 &decl_specifiers,
17601                                 &declares_class_or_enum);
17602   if (friend_p)
17603     *friend_p = cp_parser_friend_p (&decl_specifiers);
17604
17605   /* There are no template typedefs.  */
17606   if (decl_specifiers.specs[(int) ds_typedef])
17607     {
17608       error ("%Htemplate declaration of %qs",
17609              &decl_spec_token_start->location, "typedef");
17610       decl = error_mark_node;
17611     }
17612
17613   /* Gather up the access checks that occurred the
17614      decl-specifier-seq.  */
17615   stop_deferring_access_checks ();
17616
17617   /* Check for the declaration of a template class.  */
17618   if (declares_class_or_enum)
17619     {
17620       if (cp_parser_declares_only_class_p (parser))
17621         {
17622           decl = shadow_tag (&decl_specifiers);
17623
17624           /* In this case:
17625
17626                struct C {
17627                  friend template <typename T> struct A<T>::B;
17628                };
17629
17630              A<T>::B will be represented by a TYPENAME_TYPE, and
17631              therefore not recognized by shadow_tag.  */
17632           if (friend_p && *friend_p
17633               && !decl
17634               && decl_specifiers.type
17635               && TYPE_P (decl_specifiers.type))
17636             decl = decl_specifiers.type;
17637
17638           if (decl && decl != error_mark_node)
17639             decl = TYPE_NAME (decl);
17640           else
17641             decl = error_mark_node;
17642
17643           /* Perform access checks for template parameters.  */
17644           cp_parser_perform_template_parameter_access_checks (checks);
17645         }
17646     }
17647   /* If it's not a template class, try for a template function.  If
17648      the next token is a `;', then this declaration does not declare
17649      anything.  But, if there were errors in the decl-specifiers, then
17650      the error might well have come from an attempted class-specifier.
17651      In that case, there's no need to warn about a missing declarator.  */
17652   if (!decl
17653       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17654           || decl_specifiers.type != error_mark_node))
17655     {
17656       decl = cp_parser_init_declarator (parser,
17657                                         &decl_specifiers,
17658                                         checks,
17659                                         /*function_definition_allowed_p=*/true,
17660                                         member_p,
17661                                         declares_class_or_enum,
17662                                         &function_definition_p);
17663
17664     /* 7.1.1-1 [dcl.stc]
17665
17666        A storage-class-specifier shall not be specified in an explicit
17667        specialization...  */
17668     if (decl
17669         && explicit_specialization_p
17670         && decl_specifiers.storage_class != sc_none)
17671       {
17672         error ("%Hexplicit template specialization cannot have a storage class",
17673                &decl_spec_token_start->location);
17674         decl = error_mark_node;
17675       }
17676     }
17677
17678   pop_deferring_access_checks ();
17679
17680   /* Clear any current qualification; whatever comes next is the start
17681      of something new.  */
17682   parser->scope = NULL_TREE;
17683   parser->qualifying_scope = NULL_TREE;
17684   parser->object_scope = NULL_TREE;
17685   /* Look for a trailing `;' after the declaration.  */
17686   if (!function_definition_p
17687       && (decl == error_mark_node
17688           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17689     cp_parser_skip_to_end_of_block_or_statement (parser);
17690
17691   return decl;
17692 }
17693
17694 /* Parse a cast-expression that is not the operand of a unary "&".  */
17695
17696 static tree
17697 cp_parser_simple_cast_expression (cp_parser *parser)
17698 {
17699   return cp_parser_cast_expression (parser, /*address_p=*/false,
17700                                     /*cast_p=*/false);
17701 }
17702
17703 /* Parse a functional cast to TYPE.  Returns an expression
17704    representing the cast.  */
17705
17706 static tree
17707 cp_parser_functional_cast (cp_parser* parser, tree type)
17708 {
17709   tree expression_list;
17710   tree cast;
17711   bool nonconst_p;
17712
17713   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17714     {
17715       maybe_warn_cpp0x ("extended initializer lists");
17716       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17717       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17718       if (TREE_CODE (type) == TYPE_DECL)
17719         type = TREE_TYPE (type);
17720       return finish_compound_literal (type, expression_list);
17721     }
17722
17723   expression_list
17724     = cp_parser_parenthesized_expression_list (parser, false,
17725                                                /*cast_p=*/true,
17726                                                /*allow_expansion_p=*/true,
17727                                                /*non_constant_p=*/NULL);
17728
17729   cast = build_functional_cast (type, expression_list,
17730                                 tf_warning_or_error);
17731   /* [expr.const]/1: In an integral constant expression "only type
17732      conversions to integral or enumeration type can be used".  */
17733   if (TREE_CODE (type) == TYPE_DECL)
17734     type = TREE_TYPE (type);
17735   if (cast != error_mark_node
17736       && !cast_valid_in_integral_constant_expression_p (type)
17737       && (cp_parser_non_integral_constant_expression
17738           (parser, "a call to a constructor")))
17739     return error_mark_node;
17740   return cast;
17741 }
17742
17743 /* Save the tokens that make up the body of a member function defined
17744    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17745    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17746    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17747    for the member function.  */
17748
17749 static tree
17750 cp_parser_save_member_function_body (cp_parser* parser,
17751                                      cp_decl_specifier_seq *decl_specifiers,
17752                                      cp_declarator *declarator,
17753                                      tree attributes)
17754 {
17755   cp_token *first;
17756   cp_token *last;
17757   tree fn;
17758
17759   /* Create the function-declaration.  */
17760   fn = start_method (decl_specifiers, declarator, attributes);
17761   /* If something went badly wrong, bail out now.  */
17762   if (fn == error_mark_node)
17763     {
17764       /* If there's a function-body, skip it.  */
17765       if (cp_parser_token_starts_function_definition_p
17766           (cp_lexer_peek_token (parser->lexer)))
17767         cp_parser_skip_to_end_of_block_or_statement (parser);
17768       return error_mark_node;
17769     }
17770
17771   /* Remember it, if there default args to post process.  */
17772   cp_parser_save_default_args (parser, fn);
17773
17774   /* Save away the tokens that make up the body of the
17775      function.  */
17776   first = parser->lexer->next_token;
17777   /* We can have braced-init-list mem-initializers before the fn body.  */
17778   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17779     {
17780       cp_lexer_consume_token (parser->lexer);
17781       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17782              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17783         {
17784           /* cache_group will stop after an un-nested { } pair, too.  */
17785           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17786             break;
17787
17788           /* variadic mem-inits have ... after the ')'.  */
17789           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17790             cp_lexer_consume_token (parser->lexer);
17791         }
17792     }
17793   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17794   /* Handle function try blocks.  */
17795   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17796     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17797   last = parser->lexer->next_token;
17798
17799   /* Save away the inline definition; we will process it when the
17800      class is complete.  */
17801   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17802   DECL_PENDING_INLINE_P (fn) = 1;
17803
17804   /* We need to know that this was defined in the class, so that
17805      friend templates are handled correctly.  */
17806   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17807
17808   /* We're done with the inline definition.  */
17809   finish_method (fn);
17810
17811   /* Add FN to the queue of functions to be parsed later.  */
17812   TREE_VALUE (parser->unparsed_functions_queues)
17813     = tree_cons (NULL_TREE, fn,
17814                  TREE_VALUE (parser->unparsed_functions_queues));
17815
17816   return fn;
17817 }
17818
17819 /* Parse a template-argument-list, as well as the trailing ">" (but
17820    not the opening ">").  See cp_parser_template_argument_list for the
17821    return value.  */
17822
17823 static tree
17824 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17825 {
17826   tree arguments;
17827   tree saved_scope;
17828   tree saved_qualifying_scope;
17829   tree saved_object_scope;
17830   bool saved_greater_than_is_operator_p;
17831   bool saved_skip_evaluation;
17832
17833   /* [temp.names]
17834
17835      When parsing a template-id, the first non-nested `>' is taken as
17836      the end of the template-argument-list rather than a greater-than
17837      operator.  */
17838   saved_greater_than_is_operator_p
17839     = parser->greater_than_is_operator_p;
17840   parser->greater_than_is_operator_p = false;
17841   /* Parsing the argument list may modify SCOPE, so we save it
17842      here.  */
17843   saved_scope = parser->scope;
17844   saved_qualifying_scope = parser->qualifying_scope;
17845   saved_object_scope = parser->object_scope;
17846   /* We need to evaluate the template arguments, even though this
17847      template-id may be nested within a "sizeof".  */
17848   saved_skip_evaluation = skip_evaluation;
17849   skip_evaluation = false;
17850   /* Parse the template-argument-list itself.  */
17851   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17852       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17853     arguments = NULL_TREE;
17854   else
17855     arguments = cp_parser_template_argument_list (parser);
17856   /* Look for the `>' that ends the template-argument-list. If we find
17857      a '>>' instead, it's probably just a typo.  */
17858   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17859     {
17860       if (cxx_dialect != cxx98)
17861         {
17862           /* In C++0x, a `>>' in a template argument list or cast
17863              expression is considered to be two separate `>'
17864              tokens. So, change the current token to a `>', but don't
17865              consume it: it will be consumed later when the outer
17866              template argument list (or cast expression) is parsed.
17867              Note that this replacement of `>' for `>>' is necessary
17868              even if we are parsing tentatively: in the tentative
17869              case, after calling
17870              cp_parser_enclosed_template_argument_list we will always
17871              throw away all of the template arguments and the first
17872              closing `>', either because the template argument list
17873              was erroneous or because we are replacing those tokens
17874              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17875              not have been thrown away) is needed either to close an
17876              outer template argument list or to complete a new-style
17877              cast.  */
17878           cp_token *token = cp_lexer_peek_token (parser->lexer);
17879           token->type = CPP_GREATER;
17880         }
17881       else if (!saved_greater_than_is_operator_p)
17882         {
17883           /* If we're in a nested template argument list, the '>>' has
17884             to be a typo for '> >'. We emit the error message, but we
17885             continue parsing and we push a '>' as next token, so that
17886             the argument list will be parsed correctly.  Note that the
17887             global source location is still on the token before the
17888             '>>', so we need to say explicitly where we want it.  */
17889           cp_token *token = cp_lexer_peek_token (parser->lexer);
17890           error ("%H%<>>%> should be %<> >%> "
17891                  "within a nested template argument list",
17892                  &token->location);
17893
17894           token->type = CPP_GREATER;
17895         }
17896       else
17897         {
17898           /* If this is not a nested template argument list, the '>>'
17899             is a typo for '>'. Emit an error message and continue.
17900             Same deal about the token location, but here we can get it
17901             right by consuming the '>>' before issuing the diagnostic.  */
17902           cp_token *token = cp_lexer_consume_token (parser->lexer);
17903           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17904                  "a template argument list", &token->location);
17905         }
17906     }
17907   else
17908     cp_parser_skip_to_end_of_template_parameter_list (parser);
17909   /* The `>' token might be a greater-than operator again now.  */
17910   parser->greater_than_is_operator_p
17911     = saved_greater_than_is_operator_p;
17912   /* Restore the SAVED_SCOPE.  */
17913   parser->scope = saved_scope;
17914   parser->qualifying_scope = saved_qualifying_scope;
17915   parser->object_scope = saved_object_scope;
17916   skip_evaluation = saved_skip_evaluation;
17917
17918   return arguments;
17919 }
17920
17921 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17922    arguments, or the body of the function have not yet been parsed,
17923    parse them now.  */
17924
17925 static void
17926 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17927 {
17928   /* If this member is a template, get the underlying
17929      FUNCTION_DECL.  */
17930   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17931     member_function = DECL_TEMPLATE_RESULT (member_function);
17932
17933   /* There should not be any class definitions in progress at this
17934      point; the bodies of members are only parsed outside of all class
17935      definitions.  */
17936   gcc_assert (parser->num_classes_being_defined == 0);
17937   /* While we're parsing the member functions we might encounter more
17938      classes.  We want to handle them right away, but we don't want
17939      them getting mixed up with functions that are currently in the
17940      queue.  */
17941   parser->unparsed_functions_queues
17942     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17943
17944   /* Make sure that any template parameters are in scope.  */
17945   maybe_begin_member_template_processing (member_function);
17946
17947   /* If the body of the function has not yet been parsed, parse it
17948      now.  */
17949   if (DECL_PENDING_INLINE_P (member_function))
17950     {
17951       tree function_scope;
17952       cp_token_cache *tokens;
17953
17954       /* The function is no longer pending; we are processing it.  */
17955       tokens = DECL_PENDING_INLINE_INFO (member_function);
17956       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17957       DECL_PENDING_INLINE_P (member_function) = 0;
17958
17959       /* If this is a local class, enter the scope of the containing
17960          function.  */
17961       function_scope = current_function_decl;
17962       if (function_scope)
17963         push_function_context ();
17964
17965       /* Push the body of the function onto the lexer stack.  */
17966       cp_parser_push_lexer_for_tokens (parser, tokens);
17967
17968       /* Let the front end know that we going to be defining this
17969          function.  */
17970       start_preparsed_function (member_function, NULL_TREE,
17971                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17972
17973       /* Don't do access checking if it is a templated function.  */
17974       if (processing_template_decl)
17975         push_deferring_access_checks (dk_no_check);
17976
17977       /* Now, parse the body of the function.  */
17978       cp_parser_function_definition_after_declarator (parser,
17979                                                       /*inline_p=*/true);
17980
17981       if (processing_template_decl)
17982         pop_deferring_access_checks ();
17983
17984       /* Leave the scope of the containing function.  */
17985       if (function_scope)
17986         pop_function_context ();
17987       cp_parser_pop_lexer (parser);
17988     }
17989
17990   /* Remove any template parameters from the symbol table.  */
17991   maybe_end_member_template_processing ();
17992
17993   /* Restore the queue.  */
17994   parser->unparsed_functions_queues
17995     = TREE_CHAIN (parser->unparsed_functions_queues);
17996 }
17997
17998 /* If DECL contains any default args, remember it on the unparsed
17999    functions queue.  */
18000
18001 static void
18002 cp_parser_save_default_args (cp_parser* parser, tree decl)
18003 {
18004   tree probe;
18005
18006   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18007        probe;
18008        probe = TREE_CHAIN (probe))
18009     if (TREE_PURPOSE (probe))
18010       {
18011         TREE_PURPOSE (parser->unparsed_functions_queues)
18012           = tree_cons (current_class_type, decl,
18013                        TREE_PURPOSE (parser->unparsed_functions_queues));
18014         break;
18015       }
18016 }
18017
18018 /* FN is a FUNCTION_DECL which may contains a parameter with an
18019    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18020    assumes that the current scope is the scope in which the default
18021    argument should be processed.  */
18022
18023 static void
18024 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18025 {
18026   bool saved_local_variables_forbidden_p;
18027   tree parm;
18028
18029   /* While we're parsing the default args, we might (due to the
18030      statement expression extension) encounter more classes.  We want
18031      to handle them right away, but we don't want them getting mixed
18032      up with default args that are currently in the queue.  */
18033   parser->unparsed_functions_queues
18034     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18035
18036   /* Local variable names (and the `this' keyword) may not appear
18037      in a default argument.  */
18038   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18039   parser->local_variables_forbidden_p = true;
18040
18041   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18042        parm;
18043        parm = TREE_CHAIN (parm))
18044     {
18045       cp_token_cache *tokens;
18046       tree default_arg = TREE_PURPOSE (parm);
18047       tree parsed_arg;
18048       VEC(tree,gc) *insts;
18049       tree copy;
18050       unsigned ix;
18051
18052       if (!default_arg)
18053         continue;
18054
18055       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18056         /* This can happen for a friend declaration for a function
18057            already declared with default arguments.  */
18058         continue;
18059
18060        /* Push the saved tokens for the default argument onto the parser's
18061           lexer stack.  */
18062       tokens = DEFARG_TOKENS (default_arg);
18063       cp_parser_push_lexer_for_tokens (parser, tokens);
18064
18065       /* Parse the assignment-expression.  */
18066       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18067
18068       if (!processing_template_decl)
18069         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18070
18071       TREE_PURPOSE (parm) = parsed_arg;
18072
18073       /* Update any instantiations we've already created.  */
18074       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18075            VEC_iterate (tree, insts, ix, copy); ix++)
18076         TREE_PURPOSE (copy) = parsed_arg;
18077
18078       /* If the token stream has not been completely used up, then
18079          there was extra junk after the end of the default
18080          argument.  */
18081       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18082         cp_parser_error (parser, "expected %<,%>");
18083
18084       /* Revert to the main lexer.  */
18085       cp_parser_pop_lexer (parser);
18086     }
18087
18088   /* Make sure no default arg is missing.  */
18089   check_default_args (fn);
18090
18091   /* Restore the state of local_variables_forbidden_p.  */
18092   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18093
18094   /* Restore the queue.  */
18095   parser->unparsed_functions_queues
18096     = TREE_CHAIN (parser->unparsed_functions_queues);
18097 }
18098
18099 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18100    either a TYPE or an expression, depending on the form of the
18101    input.  The KEYWORD indicates which kind of expression we have
18102    encountered.  */
18103
18104 static tree
18105 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18106 {
18107   tree expr = NULL_TREE;
18108   const char *saved_message;
18109   char *tmp;
18110   bool saved_integral_constant_expression_p;
18111   bool saved_non_integral_constant_expression_p;
18112   bool pack_expansion_p = false;
18113
18114   /* Types cannot be defined in a `sizeof' expression.  Save away the
18115      old message.  */
18116   saved_message = parser->type_definition_forbidden_message;
18117   /* And create the new one.  */
18118   tmp = concat ("types may not be defined in %<",
18119                 IDENTIFIER_POINTER (ridpointers[keyword]),
18120                 "%> expressions", NULL);
18121   parser->type_definition_forbidden_message = tmp;
18122
18123   /* The restrictions on constant-expressions do not apply inside
18124      sizeof expressions.  */
18125   saved_integral_constant_expression_p
18126     = parser->integral_constant_expression_p;
18127   saved_non_integral_constant_expression_p
18128     = parser->non_integral_constant_expression_p;
18129   parser->integral_constant_expression_p = false;
18130
18131   /* If it's a `...', then we are computing the length of a parameter
18132      pack.  */
18133   if (keyword == RID_SIZEOF
18134       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18135     {
18136       /* Consume the `...'.  */
18137       cp_lexer_consume_token (parser->lexer);
18138       maybe_warn_variadic_templates ();
18139
18140       /* Note that this is an expansion.  */
18141       pack_expansion_p = true;
18142     }
18143
18144   /* Do not actually evaluate the expression.  */
18145   ++skip_evaluation;
18146   /* If it's a `(', then we might be looking at the type-id
18147      construction.  */
18148   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18149     {
18150       tree type;
18151       bool saved_in_type_id_in_expr_p;
18152
18153       /* We can't be sure yet whether we're looking at a type-id or an
18154          expression.  */
18155       cp_parser_parse_tentatively (parser);
18156       /* Consume the `('.  */
18157       cp_lexer_consume_token (parser->lexer);
18158       /* Parse the type-id.  */
18159       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18160       parser->in_type_id_in_expr_p = true;
18161       type = cp_parser_type_id (parser);
18162       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18163       /* Now, look for the trailing `)'.  */
18164       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18165       /* If all went well, then we're done.  */
18166       if (cp_parser_parse_definitely (parser))
18167         {
18168           cp_decl_specifier_seq decl_specs;
18169
18170           /* Build a trivial decl-specifier-seq.  */
18171           clear_decl_specs (&decl_specs);
18172           decl_specs.type = type;
18173
18174           /* Call grokdeclarator to figure out what type this is.  */
18175           expr = grokdeclarator (NULL,
18176                                  &decl_specs,
18177                                  TYPENAME,
18178                                  /*initialized=*/0,
18179                                  /*attrlist=*/NULL);
18180         }
18181     }
18182
18183   /* If the type-id production did not work out, then we must be
18184      looking at the unary-expression production.  */
18185   if (!expr)
18186     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18187                                        /*cast_p=*/false);
18188
18189   if (pack_expansion_p)
18190     /* Build a pack expansion. */
18191     expr = make_pack_expansion (expr);
18192
18193   /* Go back to evaluating expressions.  */
18194   --skip_evaluation;
18195
18196   /* Free the message we created.  */
18197   free (tmp);
18198   /* And restore the old one.  */
18199   parser->type_definition_forbidden_message = saved_message;
18200   parser->integral_constant_expression_p
18201     = saved_integral_constant_expression_p;
18202   parser->non_integral_constant_expression_p
18203     = saved_non_integral_constant_expression_p;
18204
18205   return expr;
18206 }
18207
18208 /* If the current declaration has no declarator, return true.  */
18209
18210 static bool
18211 cp_parser_declares_only_class_p (cp_parser *parser)
18212 {
18213   /* If the next token is a `;' or a `,' then there is no
18214      declarator.  */
18215   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18216           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18217 }
18218
18219 /* Update the DECL_SPECS to reflect the storage class indicated by
18220    KEYWORD.  */
18221
18222 static void
18223 cp_parser_set_storage_class (cp_parser *parser,
18224                              cp_decl_specifier_seq *decl_specs,
18225                              enum rid keyword,
18226                              location_t location)
18227 {
18228   cp_storage_class storage_class;
18229
18230   if (parser->in_unbraced_linkage_specification_p)
18231     {
18232       error ("%Hinvalid use of %qD in linkage specification",
18233              &location, ridpointers[keyword]);
18234       return;
18235     }
18236   else if (decl_specs->storage_class != sc_none)
18237     {
18238       decl_specs->conflicting_specifiers_p = true;
18239       return;
18240     }
18241
18242   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18243       && decl_specs->specs[(int) ds_thread])
18244     {
18245       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18246       decl_specs->specs[(int) ds_thread] = 0;
18247     }
18248
18249   switch (keyword)
18250     {
18251     case RID_AUTO:
18252       storage_class = sc_auto;
18253       break;
18254     case RID_REGISTER:
18255       storage_class = sc_register;
18256       break;
18257     case RID_STATIC:
18258       storage_class = sc_static;
18259       break;
18260     case RID_EXTERN:
18261       storage_class = sc_extern;
18262       break;
18263     case RID_MUTABLE:
18264       storage_class = sc_mutable;
18265       break;
18266     default:
18267       gcc_unreachable ();
18268     }
18269   decl_specs->storage_class = storage_class;
18270
18271   /* A storage class specifier cannot be applied alongside a typedef 
18272      specifier. If there is a typedef specifier present then set 
18273      conflicting_specifiers_p which will trigger an error later
18274      on in grokdeclarator. */
18275   if (decl_specs->specs[(int)ds_typedef])
18276     decl_specs->conflicting_specifiers_p = true;
18277 }
18278
18279 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18280    is true, the type is a user-defined type; otherwise it is a
18281    built-in type specified by a keyword.  */
18282
18283 static void
18284 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18285                               tree type_spec,
18286                               location_t location,
18287                               bool user_defined_p)
18288 {
18289   decl_specs->any_specifiers_p = true;
18290
18291   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18292      (with, for example, in "typedef int wchar_t;") we remember that
18293      this is what happened.  In system headers, we ignore these
18294      declarations so that G++ can work with system headers that are not
18295      C++-safe.  */
18296   if (decl_specs->specs[(int) ds_typedef]
18297       && !user_defined_p
18298       && (type_spec == boolean_type_node
18299           || type_spec == char16_type_node
18300           || type_spec == char32_type_node
18301           || type_spec == wchar_type_node)
18302       && (decl_specs->type
18303           || decl_specs->specs[(int) ds_long]
18304           || decl_specs->specs[(int) ds_short]
18305           || decl_specs->specs[(int) ds_unsigned]
18306           || decl_specs->specs[(int) ds_signed]))
18307     {
18308       decl_specs->redefined_builtin_type = type_spec;
18309       if (!decl_specs->type)
18310         {
18311           decl_specs->type = type_spec;
18312           decl_specs->user_defined_type_p = false;
18313           decl_specs->type_location = location;
18314         }
18315     }
18316   else if (decl_specs->type)
18317     decl_specs->multiple_types_p = true;
18318   else
18319     {
18320       decl_specs->type = type_spec;
18321       decl_specs->user_defined_type_p = user_defined_p;
18322       decl_specs->redefined_builtin_type = NULL_TREE;
18323       decl_specs->type_location = location;
18324     }
18325 }
18326
18327 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18328    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18329
18330 static bool
18331 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18332 {
18333   return decl_specifiers->specs[(int) ds_friend] != 0;
18334 }
18335
18336 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18337    issue an error message indicating that TOKEN_DESC was expected.
18338
18339    Returns the token consumed, if the token had the appropriate type.
18340    Otherwise, returns NULL.  */
18341
18342 static cp_token *
18343 cp_parser_require (cp_parser* parser,
18344                    enum cpp_ttype type,
18345                    const char* token_desc)
18346 {
18347   if (cp_lexer_next_token_is (parser->lexer, type))
18348     return cp_lexer_consume_token (parser->lexer);
18349   else
18350     {
18351       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18352       if (!cp_parser_simulate_error (parser))
18353         {
18354           char *message = concat ("expected ", token_desc, NULL);
18355           cp_parser_error (parser, message);
18356           free (message);
18357         }
18358       return NULL;
18359     }
18360 }
18361
18362 /* An error message is produced if the next token is not '>'.
18363    All further tokens are skipped until the desired token is
18364    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18365
18366 static void
18367 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18368 {
18369   /* Current level of '< ... >'.  */
18370   unsigned level = 0;
18371   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18372   unsigned nesting_depth = 0;
18373
18374   /* Are we ready, yet?  If not, issue error message.  */
18375   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18376     return;
18377
18378   /* Skip tokens until the desired token is found.  */
18379   while (true)
18380     {
18381       /* Peek at the next token.  */
18382       switch (cp_lexer_peek_token (parser->lexer)->type)
18383         {
18384         case CPP_LESS:
18385           if (!nesting_depth)
18386             ++level;
18387           break;
18388
18389         case CPP_RSHIFT:
18390           if (cxx_dialect == cxx98)
18391             /* C++0x views the `>>' operator as two `>' tokens, but
18392                C++98 does not. */
18393             break;
18394           else if (!nesting_depth && level-- == 0)
18395             {
18396               /* We've hit a `>>' where the first `>' closes the
18397                  template argument list, and the second `>' is
18398                  spurious.  Just consume the `>>' and stop; we've
18399                  already produced at least one error.  */
18400               cp_lexer_consume_token (parser->lexer);
18401               return;
18402             }
18403           /* Fall through for C++0x, so we handle the second `>' in
18404              the `>>'.  */
18405
18406         case CPP_GREATER:
18407           if (!nesting_depth && level-- == 0)
18408             {
18409               /* We've reached the token we want, consume it and stop.  */
18410               cp_lexer_consume_token (parser->lexer);
18411               return;
18412             }
18413           break;
18414
18415         case CPP_OPEN_PAREN:
18416         case CPP_OPEN_SQUARE:
18417           ++nesting_depth;
18418           break;
18419
18420         case CPP_CLOSE_PAREN:
18421         case CPP_CLOSE_SQUARE:
18422           if (nesting_depth-- == 0)
18423             return;
18424           break;
18425
18426         case CPP_EOF:
18427         case CPP_PRAGMA_EOL:
18428         case CPP_SEMICOLON:
18429         case CPP_OPEN_BRACE:
18430         case CPP_CLOSE_BRACE:
18431           /* The '>' was probably forgotten, don't look further.  */
18432           return;
18433
18434         default:
18435           break;
18436         }
18437
18438       /* Consume this token.  */
18439       cp_lexer_consume_token (parser->lexer);
18440     }
18441 }
18442
18443 /* If the next token is the indicated keyword, consume it.  Otherwise,
18444    issue an error message indicating that TOKEN_DESC was expected.
18445
18446    Returns the token consumed, if the token had the appropriate type.
18447    Otherwise, returns NULL.  */
18448
18449 static cp_token *
18450 cp_parser_require_keyword (cp_parser* parser,
18451                            enum rid keyword,
18452                            const char* token_desc)
18453 {
18454   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18455
18456   if (token && token->keyword != keyword)
18457     {
18458       dyn_string_t error_msg;
18459
18460       /* Format the error message.  */
18461       error_msg = dyn_string_new (0);
18462       dyn_string_append_cstr (error_msg, "expected ");
18463       dyn_string_append_cstr (error_msg, token_desc);
18464       cp_parser_error (parser, error_msg->s);
18465       dyn_string_delete (error_msg);
18466       return NULL;
18467     }
18468
18469   return token;
18470 }
18471
18472 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18473    function-definition.  */
18474
18475 static bool
18476 cp_parser_token_starts_function_definition_p (cp_token* token)
18477 {
18478   return (/* An ordinary function-body begins with an `{'.  */
18479           token->type == CPP_OPEN_BRACE
18480           /* A ctor-initializer begins with a `:'.  */
18481           || token->type == CPP_COLON
18482           /* A function-try-block begins with `try'.  */
18483           || token->keyword == RID_TRY
18484           /* The named return value extension begins with `return'.  */
18485           || token->keyword == RID_RETURN);
18486 }
18487
18488 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18489    definition.  */
18490
18491 static bool
18492 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18493 {
18494   cp_token *token;
18495
18496   token = cp_lexer_peek_token (parser->lexer);
18497   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18498 }
18499
18500 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18501    C++0x) ending a template-argument.  */
18502
18503 static bool
18504 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18505 {
18506   cp_token *token;
18507
18508   token = cp_lexer_peek_token (parser->lexer);
18509   return (token->type == CPP_COMMA 
18510           || token->type == CPP_GREATER
18511           || token->type == CPP_ELLIPSIS
18512           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18513 }
18514
18515 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18516    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18517
18518 static bool
18519 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18520                                                      size_t n)
18521 {
18522   cp_token *token;
18523
18524   token = cp_lexer_peek_nth_token (parser->lexer, n);
18525   if (token->type == CPP_LESS)
18526     return true;
18527   /* Check for the sequence `<::' in the original code. It would be lexed as
18528      `[:', where `[' is a digraph, and there is no whitespace before
18529      `:'.  */
18530   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18531     {
18532       cp_token *token2;
18533       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18534       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18535         return true;
18536     }
18537   return false;
18538 }
18539
18540 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18541    or none_type otherwise.  */
18542
18543 static enum tag_types
18544 cp_parser_token_is_class_key (cp_token* token)
18545 {
18546   switch (token->keyword)
18547     {
18548     case RID_CLASS:
18549       return class_type;
18550     case RID_STRUCT:
18551       return record_type;
18552     case RID_UNION:
18553       return union_type;
18554
18555     default:
18556       return none_type;
18557     }
18558 }
18559
18560 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18561
18562 static void
18563 cp_parser_check_class_key (enum tag_types class_key, tree type)
18564 {
18565   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18566     permerror (input_location, "%qs tag used in naming %q#T",
18567             class_key == union_type ? "union"
18568              : class_key == record_type ? "struct" : "class",
18569              type);
18570 }
18571
18572 /* Issue an error message if DECL is redeclared with different
18573    access than its original declaration [class.access.spec/3].
18574    This applies to nested classes and nested class templates.
18575    [class.mem/1].  */
18576
18577 static void
18578 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18579 {
18580   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18581     return;
18582
18583   if ((TREE_PRIVATE (decl)
18584        != (current_access_specifier == access_private_node))
18585       || (TREE_PROTECTED (decl)
18586           != (current_access_specifier == access_protected_node)))
18587     error ("%H%qD redeclared with different access", &location, decl);
18588 }
18589
18590 /* Look for the `template' keyword, as a syntactic disambiguator.
18591    Return TRUE iff it is present, in which case it will be
18592    consumed.  */
18593
18594 static bool
18595 cp_parser_optional_template_keyword (cp_parser *parser)
18596 {
18597   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18598     {
18599       /* The `template' keyword can only be used within templates;
18600          outside templates the parser can always figure out what is a
18601          template and what is not.  */
18602       if (!processing_template_decl)
18603         {
18604           cp_token *token = cp_lexer_peek_token (parser->lexer);
18605           error ("%H%<template%> (as a disambiguator) is only allowed "
18606                  "within templates", &token->location);
18607           /* If this part of the token stream is rescanned, the same
18608              error message would be generated.  So, we purge the token
18609              from the stream.  */
18610           cp_lexer_purge_token (parser->lexer);
18611           return false;
18612         }
18613       else
18614         {
18615           /* Consume the `template' keyword.  */
18616           cp_lexer_consume_token (parser->lexer);
18617           return true;
18618         }
18619     }
18620
18621   return false;
18622 }
18623
18624 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18625    set PARSER->SCOPE, and perform other related actions.  */
18626
18627 static void
18628 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18629 {
18630   int i;
18631   struct tree_check *check_value;
18632   deferred_access_check *chk;
18633   VEC (deferred_access_check,gc) *checks;
18634
18635   /* Get the stored value.  */
18636   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18637   /* Perform any access checks that were deferred.  */
18638   checks = check_value->checks;
18639   if (checks)
18640     {
18641       for (i = 0 ;
18642            VEC_iterate (deferred_access_check, checks, i, chk) ;
18643            ++i)
18644         {
18645           perform_or_defer_access_check (chk->binfo,
18646                                          chk->decl,
18647                                          chk->diag_decl);
18648         }
18649     }
18650   /* Set the scope from the stored value.  */
18651   parser->scope = check_value->value;
18652   parser->qualifying_scope = check_value->qualifying_scope;
18653   parser->object_scope = NULL_TREE;
18654 }
18655
18656 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18657    encounter the end of a block before what we were looking for.  */
18658
18659 static bool
18660 cp_parser_cache_group (cp_parser *parser,
18661                        enum cpp_ttype end,
18662                        unsigned depth)
18663 {
18664   while (true)
18665     {
18666       cp_token *token = cp_lexer_peek_token (parser->lexer);
18667
18668       /* Abort a parenthesized expression if we encounter a semicolon.  */
18669       if ((end == CPP_CLOSE_PAREN || depth == 0)
18670           && token->type == CPP_SEMICOLON)
18671         return true;
18672       /* If we've reached the end of the file, stop.  */
18673       if (token->type == CPP_EOF
18674           || (end != CPP_PRAGMA_EOL
18675               && token->type == CPP_PRAGMA_EOL))
18676         return true;
18677       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18678         /* We've hit the end of an enclosing block, so there's been some
18679            kind of syntax error.  */
18680         return true;
18681
18682       /* Consume the token.  */
18683       cp_lexer_consume_token (parser->lexer);
18684       /* See if it starts a new group.  */
18685       if (token->type == CPP_OPEN_BRACE)
18686         {
18687           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18688           /* In theory this should probably check end == '}', but
18689              cp_parser_save_member_function_body needs it to exit
18690              after either '}' or ')' when called with ')'.  */
18691           if (depth == 0)
18692             return false;
18693         }
18694       else if (token->type == CPP_OPEN_PAREN)
18695         {
18696           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18697           if (depth == 0 && end == CPP_CLOSE_PAREN)
18698             return false;
18699         }
18700       else if (token->type == CPP_PRAGMA)
18701         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18702       else if (token->type == end)
18703         return false;
18704     }
18705 }
18706
18707 /* Begin parsing tentatively.  We always save tokens while parsing
18708    tentatively so that if the tentative parsing fails we can restore the
18709    tokens.  */
18710
18711 static void
18712 cp_parser_parse_tentatively (cp_parser* parser)
18713 {
18714   /* Enter a new parsing context.  */
18715   parser->context = cp_parser_context_new (parser->context);
18716   /* Begin saving tokens.  */
18717   cp_lexer_save_tokens (parser->lexer);
18718   /* In order to avoid repetitive access control error messages,
18719      access checks are queued up until we are no longer parsing
18720      tentatively.  */
18721   push_deferring_access_checks (dk_deferred);
18722 }
18723
18724 /* Commit to the currently active tentative parse.  */
18725
18726 static void
18727 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18728 {
18729   cp_parser_context *context;
18730   cp_lexer *lexer;
18731
18732   /* Mark all of the levels as committed.  */
18733   lexer = parser->lexer;
18734   for (context = parser->context; context->next; context = context->next)
18735     {
18736       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18737         break;
18738       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18739       while (!cp_lexer_saving_tokens (lexer))
18740         lexer = lexer->next;
18741       cp_lexer_commit_tokens (lexer);
18742     }
18743 }
18744
18745 /* Abort the currently active tentative parse.  All consumed tokens
18746    will be rolled back, and no diagnostics will be issued.  */
18747
18748 static void
18749 cp_parser_abort_tentative_parse (cp_parser* parser)
18750 {
18751   cp_parser_simulate_error (parser);
18752   /* Now, pretend that we want to see if the construct was
18753      successfully parsed.  */
18754   cp_parser_parse_definitely (parser);
18755 }
18756
18757 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18758    token stream.  Otherwise, commit to the tokens we have consumed.
18759    Returns true if no error occurred; false otherwise.  */
18760
18761 static bool
18762 cp_parser_parse_definitely (cp_parser* parser)
18763 {
18764   bool error_occurred;
18765   cp_parser_context *context;
18766
18767   /* Remember whether or not an error occurred, since we are about to
18768      destroy that information.  */
18769   error_occurred = cp_parser_error_occurred (parser);
18770   /* Remove the topmost context from the stack.  */
18771   context = parser->context;
18772   parser->context = context->next;
18773   /* If no parse errors occurred, commit to the tentative parse.  */
18774   if (!error_occurred)
18775     {
18776       /* Commit to the tokens read tentatively, unless that was
18777          already done.  */
18778       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18779         cp_lexer_commit_tokens (parser->lexer);
18780
18781       pop_to_parent_deferring_access_checks ();
18782     }
18783   /* Otherwise, if errors occurred, roll back our state so that things
18784      are just as they were before we began the tentative parse.  */
18785   else
18786     {
18787       cp_lexer_rollback_tokens (parser->lexer);
18788       pop_deferring_access_checks ();
18789     }
18790   /* Add the context to the front of the free list.  */
18791   context->next = cp_parser_context_free_list;
18792   cp_parser_context_free_list = context;
18793
18794   return !error_occurred;
18795 }
18796
18797 /* Returns true if we are parsing tentatively and are not committed to
18798    this tentative parse.  */
18799
18800 static bool
18801 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18802 {
18803   return (cp_parser_parsing_tentatively (parser)
18804           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18805 }
18806
18807 /* Returns nonzero iff an error has occurred during the most recent
18808    tentative parse.  */
18809
18810 static bool
18811 cp_parser_error_occurred (cp_parser* parser)
18812 {
18813   return (cp_parser_parsing_tentatively (parser)
18814           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18815 }
18816
18817 /* Returns nonzero if GNU extensions are allowed.  */
18818
18819 static bool
18820 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18821 {
18822   return parser->allow_gnu_extensions_p;
18823 }
18824 \f
18825 /* Objective-C++ Productions */
18826
18827
18828 /* Parse an Objective-C expression, which feeds into a primary-expression
18829    above.
18830
18831    objc-expression:
18832      objc-message-expression
18833      objc-string-literal
18834      objc-encode-expression
18835      objc-protocol-expression
18836      objc-selector-expression
18837
18838   Returns a tree representation of the expression.  */
18839
18840 static tree
18841 cp_parser_objc_expression (cp_parser* parser)
18842 {
18843   /* Try to figure out what kind of declaration is present.  */
18844   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18845
18846   switch (kwd->type)
18847     {
18848     case CPP_OPEN_SQUARE:
18849       return cp_parser_objc_message_expression (parser);
18850
18851     case CPP_OBJC_STRING:
18852       kwd = cp_lexer_consume_token (parser->lexer);
18853       return objc_build_string_object (kwd->u.value);
18854
18855     case CPP_KEYWORD:
18856       switch (kwd->keyword)
18857         {
18858         case RID_AT_ENCODE:
18859           return cp_parser_objc_encode_expression (parser);
18860
18861         case RID_AT_PROTOCOL:
18862           return cp_parser_objc_protocol_expression (parser);
18863
18864         case RID_AT_SELECTOR:
18865           return cp_parser_objc_selector_expression (parser);
18866
18867         default:
18868           break;
18869         }
18870     default:
18871       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18872              &kwd->location, kwd->u.value);
18873       cp_parser_skip_to_end_of_block_or_statement (parser);
18874     }
18875
18876   return error_mark_node;
18877 }
18878
18879 /* Parse an Objective-C message expression.
18880
18881    objc-message-expression:
18882      [ objc-message-receiver objc-message-args ]
18883
18884    Returns a representation of an Objective-C message.  */
18885
18886 static tree
18887 cp_parser_objc_message_expression (cp_parser* parser)
18888 {
18889   tree receiver, messageargs;
18890
18891   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18892   receiver = cp_parser_objc_message_receiver (parser);
18893   messageargs = cp_parser_objc_message_args (parser);
18894   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18895
18896   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18897 }
18898
18899 /* Parse an objc-message-receiver.
18900
18901    objc-message-receiver:
18902      expression
18903      simple-type-specifier
18904
18905   Returns a representation of the type or expression.  */
18906
18907 static tree
18908 cp_parser_objc_message_receiver (cp_parser* parser)
18909 {
18910   tree rcv;
18911
18912   /* An Objective-C message receiver may be either (1) a type
18913      or (2) an expression.  */
18914   cp_parser_parse_tentatively (parser);
18915   rcv = cp_parser_expression (parser, false);
18916
18917   if (cp_parser_parse_definitely (parser))
18918     return rcv;
18919
18920   rcv = cp_parser_simple_type_specifier (parser,
18921                                          /*decl_specs=*/NULL,
18922                                          CP_PARSER_FLAGS_NONE);
18923
18924   return objc_get_class_reference (rcv);
18925 }
18926
18927 /* Parse the arguments and selectors comprising an Objective-C message.
18928
18929    objc-message-args:
18930      objc-selector
18931      objc-selector-args
18932      objc-selector-args , objc-comma-args
18933
18934    objc-selector-args:
18935      objc-selector [opt] : assignment-expression
18936      objc-selector-args objc-selector [opt] : assignment-expression
18937
18938    objc-comma-args:
18939      assignment-expression
18940      objc-comma-args , assignment-expression
18941
18942    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18943    selector arguments and TREE_VALUE containing a list of comma
18944    arguments.  */
18945
18946 static tree
18947 cp_parser_objc_message_args (cp_parser* parser)
18948 {
18949   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18950   bool maybe_unary_selector_p = true;
18951   cp_token *token = cp_lexer_peek_token (parser->lexer);
18952
18953   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18954     {
18955       tree selector = NULL_TREE, arg;
18956
18957       if (token->type != CPP_COLON)
18958         selector = cp_parser_objc_selector (parser);
18959
18960       /* Detect if we have a unary selector.  */
18961       if (maybe_unary_selector_p
18962           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18963         return build_tree_list (selector, NULL_TREE);
18964
18965       maybe_unary_selector_p = false;
18966       cp_parser_require (parser, CPP_COLON, "%<:%>");
18967       arg = cp_parser_assignment_expression (parser, false);
18968
18969       sel_args
18970         = chainon (sel_args,
18971                    build_tree_list (selector, arg));
18972
18973       token = cp_lexer_peek_token (parser->lexer);
18974     }
18975
18976   /* Handle non-selector arguments, if any. */
18977   while (token->type == CPP_COMMA)
18978     {
18979       tree arg;
18980
18981       cp_lexer_consume_token (parser->lexer);
18982       arg = cp_parser_assignment_expression (parser, false);
18983
18984       addl_args
18985         = chainon (addl_args,
18986                    build_tree_list (NULL_TREE, arg));
18987
18988       token = cp_lexer_peek_token (parser->lexer);
18989     }
18990
18991   return build_tree_list (sel_args, addl_args);
18992 }
18993
18994 /* Parse an Objective-C encode expression.
18995
18996    objc-encode-expression:
18997      @encode objc-typename
18998
18999    Returns an encoded representation of the type argument.  */
19000
19001 static tree
19002 cp_parser_objc_encode_expression (cp_parser* parser)
19003 {
19004   tree type;
19005   cp_token *token;
19006
19007   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19008   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19009   token = cp_lexer_peek_token (parser->lexer);
19010   type = complete_type (cp_parser_type_id (parser));
19011   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19012
19013   if (!type)
19014     {
19015       error ("%H%<@encode%> must specify a type as an argument",
19016              &token->location);
19017       return error_mark_node;
19018     }
19019
19020   return objc_build_encode_expr (type);
19021 }
19022
19023 /* Parse an Objective-C @defs expression.  */
19024
19025 static tree
19026 cp_parser_objc_defs_expression (cp_parser *parser)
19027 {
19028   tree name;
19029
19030   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19031   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19032   name = cp_parser_identifier (parser);
19033   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19034
19035   return objc_get_class_ivars (name);
19036 }
19037
19038 /* Parse an Objective-C protocol expression.
19039
19040   objc-protocol-expression:
19041     @protocol ( identifier )
19042
19043   Returns a representation of the protocol expression.  */
19044
19045 static tree
19046 cp_parser_objc_protocol_expression (cp_parser* parser)
19047 {
19048   tree proto;
19049
19050   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19051   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19052   proto = cp_parser_identifier (parser);
19053   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19054
19055   return objc_build_protocol_expr (proto);
19056 }
19057
19058 /* Parse an Objective-C selector expression.
19059
19060    objc-selector-expression:
19061      @selector ( objc-method-signature )
19062
19063    objc-method-signature:
19064      objc-selector
19065      objc-selector-seq
19066
19067    objc-selector-seq:
19068      objc-selector :
19069      objc-selector-seq objc-selector :
19070
19071   Returns a representation of the method selector.  */
19072
19073 static tree
19074 cp_parser_objc_selector_expression (cp_parser* parser)
19075 {
19076   tree sel_seq = NULL_TREE;
19077   bool maybe_unary_selector_p = true;
19078   cp_token *token;
19079
19080   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19081   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19082   token = cp_lexer_peek_token (parser->lexer);
19083
19084   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19085          || token->type == CPP_SCOPE)
19086     {
19087       tree selector = NULL_TREE;
19088
19089       if (token->type != CPP_COLON
19090           || token->type == CPP_SCOPE)
19091         selector = cp_parser_objc_selector (parser);
19092
19093       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19094           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19095         {
19096           /* Detect if we have a unary selector.  */
19097           if (maybe_unary_selector_p)
19098             {
19099               sel_seq = selector;
19100               goto finish_selector;
19101             }
19102           else
19103             {
19104               cp_parser_error (parser, "expected %<:%>");
19105             }
19106         }
19107       maybe_unary_selector_p = false;
19108       token = cp_lexer_consume_token (parser->lexer);
19109
19110       if (token->type == CPP_SCOPE)
19111         {
19112           sel_seq
19113             = chainon (sel_seq,
19114                        build_tree_list (selector, NULL_TREE));
19115           sel_seq
19116             = chainon (sel_seq,
19117                        build_tree_list (NULL_TREE, NULL_TREE));
19118         }
19119       else
19120         sel_seq
19121           = chainon (sel_seq,
19122                      build_tree_list (selector, NULL_TREE));
19123
19124       token = cp_lexer_peek_token (parser->lexer);
19125     }
19126
19127  finish_selector:
19128   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19129
19130   return objc_build_selector_expr (sel_seq);
19131 }
19132
19133 /* Parse a list of identifiers.
19134
19135    objc-identifier-list:
19136      identifier
19137      objc-identifier-list , identifier
19138
19139    Returns a TREE_LIST of identifier nodes.  */
19140
19141 static tree
19142 cp_parser_objc_identifier_list (cp_parser* parser)
19143 {
19144   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19145   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19146
19147   while (sep->type == CPP_COMMA)
19148     {
19149       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19150       list = chainon (list,
19151                       build_tree_list (NULL_TREE,
19152                                        cp_parser_identifier (parser)));
19153       sep = cp_lexer_peek_token (parser->lexer);
19154     }
19155
19156   return list;
19157 }
19158
19159 /* Parse an Objective-C alias declaration.
19160
19161    objc-alias-declaration:
19162      @compatibility_alias identifier identifier ;
19163
19164    This function registers the alias mapping with the Objective-C front end.
19165    It returns nothing.  */
19166
19167 static void
19168 cp_parser_objc_alias_declaration (cp_parser* parser)
19169 {
19170   tree alias, orig;
19171
19172   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19173   alias = cp_parser_identifier (parser);
19174   orig = cp_parser_identifier (parser);
19175   objc_declare_alias (alias, orig);
19176   cp_parser_consume_semicolon_at_end_of_statement (parser);
19177 }
19178
19179 /* Parse an Objective-C class forward-declaration.
19180
19181    objc-class-declaration:
19182      @class objc-identifier-list ;
19183
19184    The function registers the forward declarations with the Objective-C
19185    front end.  It returns nothing.  */
19186
19187 static void
19188 cp_parser_objc_class_declaration (cp_parser* parser)
19189 {
19190   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19191   objc_declare_class (cp_parser_objc_identifier_list (parser));
19192   cp_parser_consume_semicolon_at_end_of_statement (parser);
19193 }
19194
19195 /* Parse a list of Objective-C protocol references.
19196
19197    objc-protocol-refs-opt:
19198      objc-protocol-refs [opt]
19199
19200    objc-protocol-refs:
19201      < objc-identifier-list >
19202
19203    Returns a TREE_LIST of identifiers, if any.  */
19204
19205 static tree
19206 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19207 {
19208   tree protorefs = NULL_TREE;
19209
19210   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19211     {
19212       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19213       protorefs = cp_parser_objc_identifier_list (parser);
19214       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19215     }
19216
19217   return protorefs;
19218 }
19219
19220 /* Parse a Objective-C visibility specification.  */
19221
19222 static void
19223 cp_parser_objc_visibility_spec (cp_parser* parser)
19224 {
19225   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19226
19227   switch (vis->keyword)
19228     {
19229     case RID_AT_PRIVATE:
19230       objc_set_visibility (2);
19231       break;
19232     case RID_AT_PROTECTED:
19233       objc_set_visibility (0);
19234       break;
19235     case RID_AT_PUBLIC:
19236       objc_set_visibility (1);
19237       break;
19238     default:
19239       return;
19240     }
19241
19242   /* Eat '@private'/'@protected'/'@public'.  */
19243   cp_lexer_consume_token (parser->lexer);
19244 }
19245
19246 /* Parse an Objective-C method type.  */
19247
19248 static void
19249 cp_parser_objc_method_type (cp_parser* parser)
19250 {
19251   objc_set_method_type
19252    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19253     ? PLUS_EXPR
19254     : MINUS_EXPR);
19255 }
19256
19257 /* Parse an Objective-C protocol qualifier.  */
19258
19259 static tree
19260 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19261 {
19262   tree quals = NULL_TREE, node;
19263   cp_token *token = cp_lexer_peek_token (parser->lexer);
19264
19265   node = token->u.value;
19266
19267   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19268          && (node == ridpointers [(int) RID_IN]
19269              || node == ridpointers [(int) RID_OUT]
19270              || node == ridpointers [(int) RID_INOUT]
19271              || node == ridpointers [(int) RID_BYCOPY]
19272              || node == ridpointers [(int) RID_BYREF]
19273              || node == ridpointers [(int) RID_ONEWAY]))
19274     {
19275       quals = tree_cons (NULL_TREE, node, quals);
19276       cp_lexer_consume_token (parser->lexer);
19277       token = cp_lexer_peek_token (parser->lexer);
19278       node = token->u.value;
19279     }
19280
19281   return quals;
19282 }
19283
19284 /* Parse an Objective-C typename.  */
19285
19286 static tree
19287 cp_parser_objc_typename (cp_parser* parser)
19288 {
19289   tree type_name = NULL_TREE;
19290
19291   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19292     {
19293       tree proto_quals, cp_type = NULL_TREE;
19294
19295       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19296       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19297
19298       /* An ObjC type name may consist of just protocol qualifiers, in which
19299          case the type shall default to 'id'.  */
19300       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19301         cp_type = cp_parser_type_id (parser);
19302
19303       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19304       type_name = build_tree_list (proto_quals, cp_type);
19305     }
19306
19307   return type_name;
19308 }
19309
19310 /* Check to see if TYPE refers to an Objective-C selector name.  */
19311
19312 static bool
19313 cp_parser_objc_selector_p (enum cpp_ttype type)
19314 {
19315   return (type == CPP_NAME || type == CPP_KEYWORD
19316           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19317           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19318           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19319           || type == CPP_XOR || type == CPP_XOR_EQ);
19320 }
19321
19322 /* Parse an Objective-C selector.  */
19323
19324 static tree
19325 cp_parser_objc_selector (cp_parser* parser)
19326 {
19327   cp_token *token = cp_lexer_consume_token (parser->lexer);
19328
19329   if (!cp_parser_objc_selector_p (token->type))
19330     {
19331       error ("%Hinvalid Objective-C++ selector name", &token->location);
19332       return error_mark_node;
19333     }
19334
19335   /* C++ operator names are allowed to appear in ObjC selectors.  */
19336   switch (token->type)
19337     {
19338     case CPP_AND_AND: return get_identifier ("and");
19339     case CPP_AND_EQ: return get_identifier ("and_eq");
19340     case CPP_AND: return get_identifier ("bitand");
19341     case CPP_OR: return get_identifier ("bitor");
19342     case CPP_COMPL: return get_identifier ("compl");
19343     case CPP_NOT: return get_identifier ("not");
19344     case CPP_NOT_EQ: return get_identifier ("not_eq");
19345     case CPP_OR_OR: return get_identifier ("or");
19346     case CPP_OR_EQ: return get_identifier ("or_eq");
19347     case CPP_XOR: return get_identifier ("xor");
19348     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19349     default: return token->u.value;
19350     }
19351 }
19352
19353 /* Parse an Objective-C params list.  */
19354
19355 static tree
19356 cp_parser_objc_method_keyword_params (cp_parser* parser)
19357 {
19358   tree params = NULL_TREE;
19359   bool maybe_unary_selector_p = true;
19360   cp_token *token = cp_lexer_peek_token (parser->lexer);
19361
19362   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19363     {
19364       tree selector = NULL_TREE, type_name, identifier;
19365
19366       if (token->type != CPP_COLON)
19367         selector = cp_parser_objc_selector (parser);
19368
19369       /* Detect if we have a unary selector.  */
19370       if (maybe_unary_selector_p
19371           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19372         return selector;
19373
19374       maybe_unary_selector_p = false;
19375       cp_parser_require (parser, CPP_COLON, "%<:%>");
19376       type_name = cp_parser_objc_typename (parser);
19377       identifier = cp_parser_identifier (parser);
19378
19379       params
19380         = chainon (params,
19381                    objc_build_keyword_decl (selector,
19382                                             type_name,
19383                                             identifier));
19384
19385       token = cp_lexer_peek_token (parser->lexer);
19386     }
19387
19388   return params;
19389 }
19390
19391 /* Parse the non-keyword Objective-C params.  */
19392
19393 static tree
19394 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19395 {
19396   tree params = make_node (TREE_LIST);
19397   cp_token *token = cp_lexer_peek_token (parser->lexer);
19398   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19399
19400   while (token->type == CPP_COMMA)
19401     {
19402       cp_parameter_declarator *parmdecl;
19403       tree parm;
19404
19405       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19406       token = cp_lexer_peek_token (parser->lexer);
19407
19408       if (token->type == CPP_ELLIPSIS)
19409         {
19410           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19411           *ellipsisp = true;
19412           break;
19413         }
19414
19415       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19416       parm = grokdeclarator (parmdecl->declarator,
19417                              &parmdecl->decl_specifiers,
19418                              PARM, /*initialized=*/0,
19419                              /*attrlist=*/NULL);
19420
19421       chainon (params, build_tree_list (NULL_TREE, parm));
19422       token = cp_lexer_peek_token (parser->lexer);
19423     }
19424
19425   return params;
19426 }
19427
19428 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19429
19430 static void
19431 cp_parser_objc_interstitial_code (cp_parser* parser)
19432 {
19433   cp_token *token = cp_lexer_peek_token (parser->lexer);
19434
19435   /* If the next token is `extern' and the following token is a string
19436      literal, then we have a linkage specification.  */
19437   if (token->keyword == RID_EXTERN
19438       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19439     cp_parser_linkage_specification (parser);
19440   /* Handle #pragma, if any.  */
19441   else if (token->type == CPP_PRAGMA)
19442     cp_parser_pragma (parser, pragma_external);
19443   /* Allow stray semicolons.  */
19444   else if (token->type == CPP_SEMICOLON)
19445     cp_lexer_consume_token (parser->lexer);
19446   /* Finally, try to parse a block-declaration, or a function-definition.  */
19447   else
19448     cp_parser_block_declaration (parser, /*statement_p=*/false);
19449 }
19450
19451 /* Parse a method signature.  */
19452
19453 static tree
19454 cp_parser_objc_method_signature (cp_parser* parser)
19455 {
19456   tree rettype, kwdparms, optparms;
19457   bool ellipsis = false;
19458
19459   cp_parser_objc_method_type (parser);
19460   rettype = cp_parser_objc_typename (parser);
19461   kwdparms = cp_parser_objc_method_keyword_params (parser);
19462   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19463
19464   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19465 }
19466
19467 /* Pars an Objective-C method prototype list.  */
19468
19469 static void
19470 cp_parser_objc_method_prototype_list (cp_parser* parser)
19471 {
19472   cp_token *token = cp_lexer_peek_token (parser->lexer);
19473
19474   while (token->keyword != RID_AT_END)
19475     {
19476       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19477         {
19478           objc_add_method_declaration
19479            (cp_parser_objc_method_signature (parser));
19480           cp_parser_consume_semicolon_at_end_of_statement (parser);
19481         }
19482       else
19483         /* Allow for interspersed non-ObjC++ code.  */
19484         cp_parser_objc_interstitial_code (parser);
19485
19486       token = cp_lexer_peek_token (parser->lexer);
19487     }
19488
19489   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19490   objc_finish_interface ();
19491 }
19492
19493 /* Parse an Objective-C method definition list.  */
19494
19495 static void
19496 cp_parser_objc_method_definition_list (cp_parser* parser)
19497 {
19498   cp_token *token = cp_lexer_peek_token (parser->lexer);
19499
19500   while (token->keyword != RID_AT_END)
19501     {
19502       tree meth;
19503
19504       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19505         {
19506           push_deferring_access_checks (dk_deferred);
19507           objc_start_method_definition
19508            (cp_parser_objc_method_signature (parser));
19509
19510           /* For historical reasons, we accept an optional semicolon.  */
19511           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19512             cp_lexer_consume_token (parser->lexer);
19513
19514           perform_deferred_access_checks ();
19515           stop_deferring_access_checks ();
19516           meth = cp_parser_function_definition_after_declarator (parser,
19517                                                                  false);
19518           pop_deferring_access_checks ();
19519           objc_finish_method_definition (meth);
19520         }
19521       else
19522         /* Allow for interspersed non-ObjC++ code.  */
19523         cp_parser_objc_interstitial_code (parser);
19524
19525       token = cp_lexer_peek_token (parser->lexer);
19526     }
19527
19528   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19529   objc_finish_implementation ();
19530 }
19531
19532 /* Parse Objective-C ivars.  */
19533
19534 static void
19535 cp_parser_objc_class_ivars (cp_parser* parser)
19536 {
19537   cp_token *token = cp_lexer_peek_token (parser->lexer);
19538
19539   if (token->type != CPP_OPEN_BRACE)
19540     return;     /* No ivars specified.  */
19541
19542   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19543   token = cp_lexer_peek_token (parser->lexer);
19544
19545   while (token->type != CPP_CLOSE_BRACE)
19546     {
19547       cp_decl_specifier_seq declspecs;
19548       int decl_class_or_enum_p;
19549       tree prefix_attributes;
19550
19551       cp_parser_objc_visibility_spec (parser);
19552
19553       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19554         break;
19555
19556       cp_parser_decl_specifier_seq (parser,
19557                                     CP_PARSER_FLAGS_OPTIONAL,
19558                                     &declspecs,
19559                                     &decl_class_or_enum_p);
19560       prefix_attributes = declspecs.attributes;
19561       declspecs.attributes = NULL_TREE;
19562
19563       /* Keep going until we hit the `;' at the end of the
19564          declaration.  */
19565       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19566         {
19567           tree width = NULL_TREE, attributes, first_attribute, decl;
19568           cp_declarator *declarator = NULL;
19569           int ctor_dtor_or_conv_p;
19570
19571           /* Check for a (possibly unnamed) bitfield declaration.  */
19572           token = cp_lexer_peek_token (parser->lexer);
19573           if (token->type == CPP_COLON)
19574             goto eat_colon;
19575
19576           if (token->type == CPP_NAME
19577               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19578                   == CPP_COLON))
19579             {
19580               /* Get the name of the bitfield.  */
19581               declarator = make_id_declarator (NULL_TREE,
19582                                                cp_parser_identifier (parser),
19583                                                sfk_none);
19584
19585              eat_colon:
19586               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19587               /* Get the width of the bitfield.  */
19588               width
19589                 = cp_parser_constant_expression (parser,
19590                                                  /*allow_non_constant=*/false,
19591                                                  NULL);
19592             }
19593           else
19594             {
19595               /* Parse the declarator.  */
19596               declarator
19597                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19598                                         &ctor_dtor_or_conv_p,
19599                                         /*parenthesized_p=*/NULL,
19600                                         /*member_p=*/false);
19601             }
19602
19603           /* Look for attributes that apply to the ivar.  */
19604           attributes = cp_parser_attributes_opt (parser);
19605           /* Remember which attributes are prefix attributes and
19606              which are not.  */
19607           first_attribute = attributes;
19608           /* Combine the attributes.  */
19609           attributes = chainon (prefix_attributes, attributes);
19610
19611           if (width)
19612               /* Create the bitfield declaration.  */
19613               decl = grokbitfield (declarator, &declspecs,
19614                                    width,
19615                                    attributes);
19616           else
19617             decl = grokfield (declarator, &declspecs,
19618                               NULL_TREE, /*init_const_expr_p=*/false,
19619                               NULL_TREE, attributes);
19620
19621           /* Add the instance variable.  */
19622           objc_add_instance_variable (decl);
19623
19624           /* Reset PREFIX_ATTRIBUTES.  */
19625           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19626             attributes = TREE_CHAIN (attributes);
19627           if (attributes)
19628             TREE_CHAIN (attributes) = NULL_TREE;
19629
19630           token = cp_lexer_peek_token (parser->lexer);
19631
19632           if (token->type == CPP_COMMA)
19633             {
19634               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19635               continue;
19636             }
19637           break;
19638         }
19639
19640       cp_parser_consume_semicolon_at_end_of_statement (parser);
19641       token = cp_lexer_peek_token (parser->lexer);
19642     }
19643
19644   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19645   /* For historical reasons, we accept an optional semicolon.  */
19646   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19647     cp_lexer_consume_token (parser->lexer);
19648 }
19649
19650 /* Parse an Objective-C protocol declaration.  */
19651
19652 static void
19653 cp_parser_objc_protocol_declaration (cp_parser* parser)
19654 {
19655   tree proto, protorefs;
19656   cp_token *tok;
19657
19658   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19659   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19660     {
19661       tok = cp_lexer_peek_token (parser->lexer);
19662       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19663       goto finish;
19664     }
19665
19666   /* See if we have a forward declaration or a definition.  */
19667   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19668
19669   /* Try a forward declaration first.  */
19670   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19671     {
19672       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19673      finish:
19674       cp_parser_consume_semicolon_at_end_of_statement (parser);
19675     }
19676
19677   /* Ok, we got a full-fledged definition (or at least should).  */
19678   else
19679     {
19680       proto = cp_parser_identifier (parser);
19681       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19682       objc_start_protocol (proto, protorefs);
19683       cp_parser_objc_method_prototype_list (parser);
19684     }
19685 }
19686
19687 /* Parse an Objective-C superclass or category.  */
19688
19689 static void
19690 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19691                                                           tree *categ)
19692 {
19693   cp_token *next = cp_lexer_peek_token (parser->lexer);
19694
19695   *super = *categ = NULL_TREE;
19696   if (next->type == CPP_COLON)
19697     {
19698       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19699       *super = cp_parser_identifier (parser);
19700     }
19701   else if (next->type == CPP_OPEN_PAREN)
19702     {
19703       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19704       *categ = cp_parser_identifier (parser);
19705       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19706     }
19707 }
19708
19709 /* Parse an Objective-C class interface.  */
19710
19711 static void
19712 cp_parser_objc_class_interface (cp_parser* parser)
19713 {
19714   tree name, super, categ, protos;
19715
19716   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19717   name = cp_parser_identifier (parser);
19718   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19719   protos = cp_parser_objc_protocol_refs_opt (parser);
19720
19721   /* We have either a class or a category on our hands.  */
19722   if (categ)
19723     objc_start_category_interface (name, categ, protos);
19724   else
19725     {
19726       objc_start_class_interface (name, super, protos);
19727       /* Handle instance variable declarations, if any.  */
19728       cp_parser_objc_class_ivars (parser);
19729       objc_continue_interface ();
19730     }
19731
19732   cp_parser_objc_method_prototype_list (parser);
19733 }
19734
19735 /* Parse an Objective-C class implementation.  */
19736
19737 static void
19738 cp_parser_objc_class_implementation (cp_parser* parser)
19739 {
19740   tree name, super, categ;
19741
19742   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19743   name = cp_parser_identifier (parser);
19744   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19745
19746   /* We have either a class or a category on our hands.  */
19747   if (categ)
19748     objc_start_category_implementation (name, categ);
19749   else
19750     {
19751       objc_start_class_implementation (name, super);
19752       /* Handle instance variable declarations, if any.  */
19753       cp_parser_objc_class_ivars (parser);
19754       objc_continue_implementation ();
19755     }
19756
19757   cp_parser_objc_method_definition_list (parser);
19758 }
19759
19760 /* Consume the @end token and finish off the implementation.  */
19761
19762 static void
19763 cp_parser_objc_end_implementation (cp_parser* parser)
19764 {
19765   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19766   objc_finish_implementation ();
19767 }
19768
19769 /* Parse an Objective-C declaration.  */
19770
19771 static void
19772 cp_parser_objc_declaration (cp_parser* parser)
19773 {
19774   /* Try to figure out what kind of declaration is present.  */
19775   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19776
19777   switch (kwd->keyword)
19778     {
19779     case RID_AT_ALIAS:
19780       cp_parser_objc_alias_declaration (parser);
19781       break;
19782     case RID_AT_CLASS:
19783       cp_parser_objc_class_declaration (parser);
19784       break;
19785     case RID_AT_PROTOCOL:
19786       cp_parser_objc_protocol_declaration (parser);
19787       break;
19788     case RID_AT_INTERFACE:
19789       cp_parser_objc_class_interface (parser);
19790       break;
19791     case RID_AT_IMPLEMENTATION:
19792       cp_parser_objc_class_implementation (parser);
19793       break;
19794     case RID_AT_END:
19795       cp_parser_objc_end_implementation (parser);
19796       break;
19797     default:
19798       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19799              &kwd->location, kwd->u.value);
19800       cp_parser_skip_to_end_of_block_or_statement (parser);
19801     }
19802 }
19803
19804 /* Parse an Objective-C try-catch-finally statement.
19805
19806    objc-try-catch-finally-stmt:
19807      @try compound-statement objc-catch-clause-seq [opt]
19808        objc-finally-clause [opt]
19809
19810    objc-catch-clause-seq:
19811      objc-catch-clause objc-catch-clause-seq [opt]
19812
19813    objc-catch-clause:
19814      @catch ( exception-declaration ) compound-statement
19815
19816    objc-finally-clause
19817      @finally compound-statement
19818
19819    Returns NULL_TREE.  */
19820
19821 static tree
19822 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19823   location_t location;
19824   tree stmt;
19825
19826   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19827   location = cp_lexer_peek_token (parser->lexer)->location;
19828   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19829      node, lest it get absorbed into the surrounding block.  */
19830   stmt = push_stmt_list ();
19831   cp_parser_compound_statement (parser, NULL, false);
19832   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19833
19834   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19835     {
19836       cp_parameter_declarator *parmdecl;
19837       tree parm;
19838
19839       cp_lexer_consume_token (parser->lexer);
19840       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19841       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19842       parm = grokdeclarator (parmdecl->declarator,
19843                              &parmdecl->decl_specifiers,
19844                              PARM, /*initialized=*/0,
19845                              /*attrlist=*/NULL);
19846       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19847       objc_begin_catch_clause (parm);
19848       cp_parser_compound_statement (parser, NULL, false);
19849       objc_finish_catch_clause ();
19850     }
19851
19852   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19853     {
19854       cp_lexer_consume_token (parser->lexer);
19855       location = cp_lexer_peek_token (parser->lexer)->location;
19856       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19857          node, lest it get absorbed into the surrounding block.  */
19858       stmt = push_stmt_list ();
19859       cp_parser_compound_statement (parser, NULL, false);
19860       objc_build_finally_clause (location, pop_stmt_list (stmt));
19861     }
19862
19863   return objc_finish_try_stmt ();
19864 }
19865
19866 /* Parse an Objective-C synchronized statement.
19867
19868    objc-synchronized-stmt:
19869      @synchronized ( expression ) compound-statement
19870
19871    Returns NULL_TREE.  */
19872
19873 static tree
19874 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19875   location_t location;
19876   tree lock, stmt;
19877
19878   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19879
19880   location = cp_lexer_peek_token (parser->lexer)->location;
19881   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19882   lock = cp_parser_expression (parser, false);
19883   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19884
19885   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19886      node, lest it get absorbed into the surrounding block.  */
19887   stmt = push_stmt_list ();
19888   cp_parser_compound_statement (parser, NULL, false);
19889
19890   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19891 }
19892
19893 /* Parse an Objective-C throw statement.
19894
19895    objc-throw-stmt:
19896      @throw assignment-expression [opt] ;
19897
19898    Returns a constructed '@throw' statement.  */
19899
19900 static tree
19901 cp_parser_objc_throw_statement (cp_parser *parser) {
19902   tree expr = NULL_TREE;
19903
19904   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19905
19906   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19907     expr = cp_parser_assignment_expression (parser, false);
19908
19909   cp_parser_consume_semicolon_at_end_of_statement (parser);
19910
19911   return objc_build_throw_stmt (expr);
19912 }
19913
19914 /* Parse an Objective-C statement.  */
19915
19916 static tree
19917 cp_parser_objc_statement (cp_parser * parser) {
19918   /* Try to figure out what kind of declaration is present.  */
19919   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19920
19921   switch (kwd->keyword)
19922     {
19923     case RID_AT_TRY:
19924       return cp_parser_objc_try_catch_finally_statement (parser);
19925     case RID_AT_SYNCHRONIZED:
19926       return cp_parser_objc_synchronized_statement (parser);
19927     case RID_AT_THROW:
19928       return cp_parser_objc_throw_statement (parser);
19929     default:
19930       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19931              &kwd->location, kwd->u.value);
19932       cp_parser_skip_to_end_of_block_or_statement (parser);
19933     }
19934
19935   return error_mark_node;
19936 }
19937 \f
19938 /* OpenMP 2.5 parsing routines.  */
19939
19940 /* Returns name of the next clause.
19941    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19942    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19943    returned and the token is consumed.  */
19944
19945 static pragma_omp_clause
19946 cp_parser_omp_clause_name (cp_parser *parser)
19947 {
19948   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19949
19950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19951     result = PRAGMA_OMP_CLAUSE_IF;
19952   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19953     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19954   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19955     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19956   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19957     {
19958       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19959       const char *p = IDENTIFIER_POINTER (id);
19960
19961       switch (p[0])
19962         {
19963         case 'c':
19964           if (!strcmp ("collapse", p))
19965             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19966           else if (!strcmp ("copyin", p))
19967             result = PRAGMA_OMP_CLAUSE_COPYIN;
19968           else if (!strcmp ("copyprivate", p))
19969             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19970           break;
19971         case 'f':
19972           if (!strcmp ("firstprivate", p))
19973             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19974           break;
19975         case 'l':
19976           if (!strcmp ("lastprivate", p))
19977             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19978           break;
19979         case 'n':
19980           if (!strcmp ("nowait", p))
19981             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19982           else if (!strcmp ("num_threads", p))
19983             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19984           break;
19985         case 'o':
19986           if (!strcmp ("ordered", p))
19987             result = PRAGMA_OMP_CLAUSE_ORDERED;
19988           break;
19989         case 'r':
19990           if (!strcmp ("reduction", p))
19991             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19992           break;
19993         case 's':
19994           if (!strcmp ("schedule", p))
19995             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19996           else if (!strcmp ("shared", p))
19997             result = PRAGMA_OMP_CLAUSE_SHARED;
19998           break;
19999         case 'u':
20000           if (!strcmp ("untied", p))
20001             result = PRAGMA_OMP_CLAUSE_UNTIED;
20002           break;
20003         }
20004     }
20005
20006   if (result != PRAGMA_OMP_CLAUSE_NONE)
20007     cp_lexer_consume_token (parser->lexer);
20008
20009   return result;
20010 }
20011
20012 /* Validate that a clause of the given type does not already exist.  */
20013
20014 static void
20015 check_no_duplicate_clause (tree clauses, enum tree_code code,
20016                            const char *name, location_t location)
20017 {
20018   tree c;
20019
20020   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20021     if (OMP_CLAUSE_CODE (c) == code)
20022       {
20023         error ("%Htoo many %qs clauses", &location, name);
20024         break;
20025       }
20026 }
20027
20028 /* OpenMP 2.5:
20029    variable-list:
20030      identifier
20031      variable-list , identifier
20032
20033    In addition, we match a closing parenthesis.  An opening parenthesis
20034    will have been consumed by the caller.
20035
20036    If KIND is nonzero, create the appropriate node and install the decl
20037    in OMP_CLAUSE_DECL and add the node to the head of the list.
20038
20039    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20040    return the list created.  */
20041
20042 static tree
20043 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20044                                 tree list)
20045 {
20046   cp_token *token;
20047   while (1)
20048     {
20049       tree name, decl;
20050
20051       token = cp_lexer_peek_token (parser->lexer);
20052       name = cp_parser_id_expression (parser, /*template_p=*/false,
20053                                       /*check_dependency_p=*/true,
20054                                       /*template_p=*/NULL,
20055                                       /*declarator_p=*/false,
20056                                       /*optional_p=*/false);
20057       if (name == error_mark_node)
20058         goto skip_comma;
20059
20060       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20061       if (decl == error_mark_node)
20062         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20063       else if (kind != 0)
20064         {
20065           tree u = build_omp_clause (kind);
20066           OMP_CLAUSE_DECL (u) = decl;
20067           OMP_CLAUSE_CHAIN (u) = list;
20068           list = u;
20069         }
20070       else
20071         list = tree_cons (decl, NULL_TREE, list);
20072
20073     get_comma:
20074       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20075         break;
20076       cp_lexer_consume_token (parser->lexer);
20077     }
20078
20079   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20080     {
20081       int ending;
20082
20083       /* Try to resync to an unnested comma.  Copied from
20084          cp_parser_parenthesized_expression_list.  */
20085     skip_comma:
20086       ending = cp_parser_skip_to_closing_parenthesis (parser,
20087                                                       /*recovering=*/true,
20088                                                       /*or_comma=*/true,
20089                                                       /*consume_paren=*/true);
20090       if (ending < 0)
20091         goto get_comma;
20092     }
20093
20094   return list;
20095 }
20096
20097 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20098    common case for omp clauses.  */
20099
20100 static tree
20101 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20102 {
20103   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20104     return cp_parser_omp_var_list_no_open (parser, kind, list);
20105   return list;
20106 }
20107
20108 /* OpenMP 3.0:
20109    collapse ( constant-expression ) */
20110
20111 static tree
20112 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20113 {
20114   tree c, num;
20115   location_t loc;
20116   HOST_WIDE_INT n;
20117
20118   loc = cp_lexer_peek_token (parser->lexer)->location;
20119   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20120     return list;
20121
20122   num = cp_parser_constant_expression (parser, false, NULL);
20123
20124   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20125     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20126                                            /*or_comma=*/false,
20127                                            /*consume_paren=*/true);
20128
20129   if (num == error_mark_node)
20130     return list;
20131   num = fold_non_dependent_expr (num);
20132   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20133       || !host_integerp (num, 0)
20134       || (n = tree_low_cst (num, 0)) <= 0
20135       || (int) n != n)
20136     {
20137       error ("%Hcollapse argument needs positive constant integer expression",
20138              &loc);
20139       return list;
20140     }
20141
20142   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20143   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20144   OMP_CLAUSE_CHAIN (c) = list;
20145   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20146
20147   return c;
20148 }
20149
20150 /* OpenMP 2.5:
20151    default ( shared | none ) */
20152
20153 static tree
20154 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20155 {
20156   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20157   tree c;
20158
20159   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20160     return list;
20161   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20162     {
20163       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20164       const char *p = IDENTIFIER_POINTER (id);
20165
20166       switch (p[0])
20167         {
20168         case 'n':
20169           if (strcmp ("none", p) != 0)
20170             goto invalid_kind;
20171           kind = OMP_CLAUSE_DEFAULT_NONE;
20172           break;
20173
20174         case 's':
20175           if (strcmp ("shared", p) != 0)
20176             goto invalid_kind;
20177           kind = OMP_CLAUSE_DEFAULT_SHARED;
20178           break;
20179
20180         default:
20181           goto invalid_kind;
20182         }
20183
20184       cp_lexer_consume_token (parser->lexer);
20185     }
20186   else
20187     {
20188     invalid_kind:
20189       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20190     }
20191
20192   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20193     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20194                                            /*or_comma=*/false,
20195                                            /*consume_paren=*/true);
20196
20197   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20198     return list;
20199
20200   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20201   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20202   OMP_CLAUSE_CHAIN (c) = list;
20203   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20204
20205   return c;
20206 }
20207
20208 /* OpenMP 2.5:
20209    if ( expression ) */
20210
20211 static tree
20212 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20213 {
20214   tree t, c;
20215
20216   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20217     return list;
20218
20219   t = cp_parser_condition (parser);
20220
20221   if (t == error_mark_node
20222       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20223     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20224                                            /*or_comma=*/false,
20225                                            /*consume_paren=*/true);
20226
20227   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20228
20229   c = build_omp_clause (OMP_CLAUSE_IF);
20230   OMP_CLAUSE_IF_EXPR (c) = t;
20231   OMP_CLAUSE_CHAIN (c) = list;
20232
20233   return c;
20234 }
20235
20236 /* OpenMP 2.5:
20237    nowait */
20238
20239 static tree
20240 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20241                              tree list, location_t location)
20242 {
20243   tree c;
20244
20245   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20246
20247   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20248   OMP_CLAUSE_CHAIN (c) = list;
20249   return c;
20250 }
20251
20252 /* OpenMP 2.5:
20253    num_threads ( expression ) */
20254
20255 static tree
20256 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20257                                   location_t location)
20258 {
20259   tree t, c;
20260
20261   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20262     return list;
20263
20264   t = cp_parser_expression (parser, false);
20265
20266   if (t == error_mark_node
20267       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20268     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20269                                            /*or_comma=*/false,
20270                                            /*consume_paren=*/true);
20271
20272   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20273                              "num_threads", location);
20274
20275   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20276   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20277   OMP_CLAUSE_CHAIN (c) = list;
20278
20279   return c;
20280 }
20281
20282 /* OpenMP 2.5:
20283    ordered */
20284
20285 static tree
20286 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20287                               tree list, location_t location)
20288 {
20289   tree c;
20290
20291   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20292                              "ordered", location);
20293
20294   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20295   OMP_CLAUSE_CHAIN (c) = list;
20296   return c;
20297 }
20298
20299 /* OpenMP 2.5:
20300    reduction ( reduction-operator : variable-list )
20301
20302    reduction-operator:
20303      One of: + * - & ^ | && || */
20304
20305 static tree
20306 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20307 {
20308   enum tree_code code;
20309   tree nlist, c;
20310
20311   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20312     return list;
20313
20314   switch (cp_lexer_peek_token (parser->lexer)->type)
20315     {
20316     case CPP_PLUS:
20317       code = PLUS_EXPR;
20318       break;
20319     case CPP_MULT:
20320       code = MULT_EXPR;
20321       break;
20322     case CPP_MINUS:
20323       code = MINUS_EXPR;
20324       break;
20325     case CPP_AND:
20326       code = BIT_AND_EXPR;
20327       break;
20328     case CPP_XOR:
20329       code = BIT_XOR_EXPR;
20330       break;
20331     case CPP_OR:
20332       code = BIT_IOR_EXPR;
20333       break;
20334     case CPP_AND_AND:
20335       code = TRUTH_ANDIF_EXPR;
20336       break;
20337     case CPP_OR_OR:
20338       code = TRUTH_ORIF_EXPR;
20339       break;
20340     default:
20341       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20342                                "%<|%>, %<&&%>, or %<||%>");
20343     resync_fail:
20344       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20345                                              /*or_comma=*/false,
20346                                              /*consume_paren=*/true);
20347       return list;
20348     }
20349   cp_lexer_consume_token (parser->lexer);
20350
20351   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20352     goto resync_fail;
20353
20354   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20355   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20356     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20357
20358   return nlist;
20359 }
20360
20361 /* OpenMP 2.5:
20362    schedule ( schedule-kind )
20363    schedule ( schedule-kind , expression )
20364
20365    schedule-kind:
20366      static | dynamic | guided | runtime | auto  */
20367
20368 static tree
20369 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20370 {
20371   tree c, t;
20372
20373   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20374     return list;
20375
20376   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20377
20378   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20379     {
20380       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20381       const char *p = IDENTIFIER_POINTER (id);
20382
20383       switch (p[0])
20384         {
20385         case 'd':
20386           if (strcmp ("dynamic", p) != 0)
20387             goto invalid_kind;
20388           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20389           break;
20390
20391         case 'g':
20392           if (strcmp ("guided", p) != 0)
20393             goto invalid_kind;
20394           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20395           break;
20396
20397         case 'r':
20398           if (strcmp ("runtime", p) != 0)
20399             goto invalid_kind;
20400           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20401           break;
20402
20403         default:
20404           goto invalid_kind;
20405         }
20406     }
20407   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20408     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20409   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20410     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20411   else
20412     goto invalid_kind;
20413   cp_lexer_consume_token (parser->lexer);
20414
20415   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20416     {
20417       cp_token *token;
20418       cp_lexer_consume_token (parser->lexer);
20419
20420       token = cp_lexer_peek_token (parser->lexer);
20421       t = cp_parser_assignment_expression (parser, false);
20422
20423       if (t == error_mark_node)
20424         goto resync_fail;
20425       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20426         error ("%Hschedule %<runtime%> does not take "
20427                "a %<chunk_size%> parameter", &token->location);
20428       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20429         error ("%Hschedule %<auto%> does not take "
20430                "a %<chunk_size%> parameter", &token->location);
20431       else
20432         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20433
20434       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20435         goto resync_fail;
20436     }
20437   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20438     goto resync_fail;
20439
20440   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20441   OMP_CLAUSE_CHAIN (c) = list;
20442   return c;
20443
20444  invalid_kind:
20445   cp_parser_error (parser, "invalid schedule kind");
20446  resync_fail:
20447   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20448                                          /*or_comma=*/false,
20449                                          /*consume_paren=*/true);
20450   return list;
20451 }
20452
20453 /* OpenMP 3.0:
20454    untied */
20455
20456 static tree
20457 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20458                              tree list, location_t location)
20459 {
20460   tree c;
20461
20462   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20463
20464   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20465   OMP_CLAUSE_CHAIN (c) = list;
20466   return c;
20467 }
20468
20469 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20470    is a bitmask in MASK.  Return the list of clauses found; the result
20471    of clause default goes in *pdefault.  */
20472
20473 static tree
20474 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20475                            const char *where, cp_token *pragma_tok)
20476 {
20477   tree clauses = NULL;
20478   bool first = true;
20479   cp_token *token = NULL;
20480
20481   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20482     {
20483       pragma_omp_clause c_kind;
20484       const char *c_name;
20485       tree prev = clauses;
20486
20487       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20488         cp_lexer_consume_token (parser->lexer);
20489
20490       token = cp_lexer_peek_token (parser->lexer);
20491       c_kind = cp_parser_omp_clause_name (parser);
20492       first = false;
20493
20494       switch (c_kind)
20495         {
20496         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20497           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20498                                                    token->location);
20499           c_name = "collapse";
20500           break;
20501         case PRAGMA_OMP_CLAUSE_COPYIN:
20502           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20503           c_name = "copyin";
20504           break;
20505         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20506           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20507                                             clauses);
20508           c_name = "copyprivate";
20509           break;
20510         case PRAGMA_OMP_CLAUSE_DEFAULT:
20511           clauses = cp_parser_omp_clause_default (parser, clauses,
20512                                                   token->location);
20513           c_name = "default";
20514           break;
20515         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20516           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20517                                             clauses);
20518           c_name = "firstprivate";
20519           break;
20520         case PRAGMA_OMP_CLAUSE_IF:
20521           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20522           c_name = "if";
20523           break;
20524         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20525           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20526                                             clauses);
20527           c_name = "lastprivate";
20528           break;
20529         case PRAGMA_OMP_CLAUSE_NOWAIT:
20530           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20531           c_name = "nowait";
20532           break;
20533         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20534           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20535                                                       token->location);
20536           c_name = "num_threads";
20537           break;
20538         case PRAGMA_OMP_CLAUSE_ORDERED:
20539           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20540                                                   token->location);
20541           c_name = "ordered";
20542           break;
20543         case PRAGMA_OMP_CLAUSE_PRIVATE:
20544           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20545                                             clauses);
20546           c_name = "private";
20547           break;
20548         case PRAGMA_OMP_CLAUSE_REDUCTION:
20549           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20550           c_name = "reduction";
20551           break;
20552         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20553           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20554                                                    token->location);
20555           c_name = "schedule";
20556           break;
20557         case PRAGMA_OMP_CLAUSE_SHARED:
20558           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20559                                             clauses);
20560           c_name = "shared";
20561           break;
20562         case PRAGMA_OMP_CLAUSE_UNTIED:
20563           clauses = cp_parser_omp_clause_untied (parser, clauses,
20564                                                  token->location);
20565           c_name = "nowait";
20566           break;
20567         default:
20568           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20569           goto saw_error;
20570         }
20571
20572       if (((mask >> c_kind) & 1) == 0)
20573         {
20574           /* Remove the invalid clause(s) from the list to avoid
20575              confusing the rest of the compiler.  */
20576           clauses = prev;
20577           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20578         }
20579     }
20580  saw_error:
20581   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20582   return finish_omp_clauses (clauses);
20583 }
20584
20585 /* OpenMP 2.5:
20586    structured-block:
20587      statement
20588
20589    In practice, we're also interested in adding the statement to an
20590    outer node.  So it is convenient if we work around the fact that
20591    cp_parser_statement calls add_stmt.  */
20592
20593 static unsigned
20594 cp_parser_begin_omp_structured_block (cp_parser *parser)
20595 {
20596   unsigned save = parser->in_statement;
20597
20598   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20599      This preserves the "not within loop or switch" style error messages
20600      for nonsense cases like
20601         void foo() {
20602         #pragma omp single
20603           break;
20604         }
20605   */
20606   if (parser->in_statement)
20607     parser->in_statement = IN_OMP_BLOCK;
20608
20609   return save;
20610 }
20611
20612 static void
20613 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20614 {
20615   parser->in_statement = save;
20616 }
20617
20618 static tree
20619 cp_parser_omp_structured_block (cp_parser *parser)
20620 {
20621   tree stmt = begin_omp_structured_block ();
20622   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20623
20624   cp_parser_statement (parser, NULL_TREE, false, NULL);
20625
20626   cp_parser_end_omp_structured_block (parser, save);
20627   return finish_omp_structured_block (stmt);
20628 }
20629
20630 /* OpenMP 2.5:
20631    # pragma omp atomic new-line
20632      expression-stmt
20633
20634    expression-stmt:
20635      x binop= expr | x++ | ++x | x-- | --x
20636    binop:
20637      +, *, -, /, &, ^, |, <<, >>
20638
20639   where x is an lvalue expression with scalar type.  */
20640
20641 static void
20642 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20643 {
20644   tree lhs, rhs;
20645   enum tree_code code;
20646
20647   cp_parser_require_pragma_eol (parser, pragma_tok);
20648
20649   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20650                                     /*cast_p=*/false);
20651   switch (TREE_CODE (lhs))
20652     {
20653     case ERROR_MARK:
20654       goto saw_error;
20655
20656     case PREINCREMENT_EXPR:
20657     case POSTINCREMENT_EXPR:
20658       lhs = TREE_OPERAND (lhs, 0);
20659       code = PLUS_EXPR;
20660       rhs = integer_one_node;
20661       break;
20662
20663     case PREDECREMENT_EXPR:
20664     case POSTDECREMENT_EXPR:
20665       lhs = TREE_OPERAND (lhs, 0);
20666       code = MINUS_EXPR;
20667       rhs = integer_one_node;
20668       break;
20669
20670     default:
20671       switch (cp_lexer_peek_token (parser->lexer)->type)
20672         {
20673         case CPP_MULT_EQ:
20674           code = MULT_EXPR;
20675           break;
20676         case CPP_DIV_EQ:
20677           code = TRUNC_DIV_EXPR;
20678           break;
20679         case CPP_PLUS_EQ:
20680           code = PLUS_EXPR;
20681           break;
20682         case CPP_MINUS_EQ:
20683           code = MINUS_EXPR;
20684           break;
20685         case CPP_LSHIFT_EQ:
20686           code = LSHIFT_EXPR;
20687           break;
20688         case CPP_RSHIFT_EQ:
20689           code = RSHIFT_EXPR;
20690           break;
20691         case CPP_AND_EQ:
20692           code = BIT_AND_EXPR;
20693           break;
20694         case CPP_OR_EQ:
20695           code = BIT_IOR_EXPR;
20696           break;
20697         case CPP_XOR_EQ:
20698           code = BIT_XOR_EXPR;
20699           break;
20700         default:
20701           cp_parser_error (parser,
20702                            "invalid operator for %<#pragma omp atomic%>");
20703           goto saw_error;
20704         }
20705       cp_lexer_consume_token (parser->lexer);
20706
20707       rhs = cp_parser_expression (parser, false);
20708       if (rhs == error_mark_node)
20709         goto saw_error;
20710       break;
20711     }
20712   finish_omp_atomic (code, lhs, rhs);
20713   cp_parser_consume_semicolon_at_end_of_statement (parser);
20714   return;
20715
20716  saw_error:
20717   cp_parser_skip_to_end_of_block_or_statement (parser);
20718 }
20719
20720
20721 /* OpenMP 2.5:
20722    # pragma omp barrier new-line  */
20723
20724 static void
20725 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20726 {
20727   cp_parser_require_pragma_eol (parser, pragma_tok);
20728   finish_omp_barrier ();
20729 }
20730
20731 /* OpenMP 2.5:
20732    # pragma omp critical [(name)] new-line
20733      structured-block  */
20734
20735 static tree
20736 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20737 {
20738   tree stmt, name = NULL;
20739
20740   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20741     {
20742       cp_lexer_consume_token (parser->lexer);
20743
20744       name = cp_parser_identifier (parser);
20745
20746       if (name == error_mark_node
20747           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20748         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20749                                                /*or_comma=*/false,
20750                                                /*consume_paren=*/true);
20751       if (name == error_mark_node)
20752         name = NULL;
20753     }
20754   cp_parser_require_pragma_eol (parser, pragma_tok);
20755
20756   stmt = cp_parser_omp_structured_block (parser);
20757   return c_finish_omp_critical (stmt, name);
20758 }
20759
20760 /* OpenMP 2.5:
20761    # pragma omp flush flush-vars[opt] new-line
20762
20763    flush-vars:
20764      ( variable-list ) */
20765
20766 static void
20767 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20768 {
20769   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20770     (void) cp_parser_omp_var_list (parser, 0, NULL);
20771   cp_parser_require_pragma_eol (parser, pragma_tok);
20772
20773   finish_omp_flush ();
20774 }
20775
20776 /* Helper function, to parse omp for increment expression.  */
20777
20778 static tree
20779 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20780 {
20781   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20782   enum tree_code op;
20783   cp_token *token;
20784
20785   if (lhs != decl)
20786     {
20787       cp_parser_skip_to_end_of_statement (parser);
20788       return error_mark_node;
20789     }
20790
20791   token = cp_lexer_peek_token (parser->lexer);
20792   op = binops_by_token [token->type].tree_type;
20793   switch (op)
20794     {
20795     case LT_EXPR:
20796     case LE_EXPR:
20797     case GT_EXPR:
20798     case GE_EXPR:
20799       break;
20800     default:
20801       cp_parser_skip_to_end_of_statement (parser);
20802       return error_mark_node;
20803     }
20804
20805   cp_lexer_consume_token (parser->lexer);
20806   rhs = cp_parser_binary_expression (parser, false,
20807                                      PREC_RELATIONAL_EXPRESSION);
20808   if (rhs == error_mark_node
20809       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20810     {
20811       cp_parser_skip_to_end_of_statement (parser);
20812       return error_mark_node;
20813     }
20814
20815   return build2 (op, boolean_type_node, lhs, rhs);
20816 }
20817
20818 /* Helper function, to parse omp for increment expression.  */
20819
20820 static tree
20821 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20822 {
20823   cp_token *token = cp_lexer_peek_token (parser->lexer);
20824   enum tree_code op;
20825   tree lhs, rhs;
20826   cp_id_kind idk;
20827   bool decl_first;
20828
20829   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20830     {
20831       op = (token->type == CPP_PLUS_PLUS
20832             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20833       cp_lexer_consume_token (parser->lexer);
20834       lhs = cp_parser_cast_expression (parser, false, false);
20835       if (lhs != decl)
20836         return error_mark_node;
20837       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20838     }
20839
20840   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20841   if (lhs != decl)
20842     return error_mark_node;
20843
20844   token = cp_lexer_peek_token (parser->lexer);
20845   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20846     {
20847       op = (token->type == CPP_PLUS_PLUS
20848             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20849       cp_lexer_consume_token (parser->lexer);
20850       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20851     }
20852
20853   op = cp_parser_assignment_operator_opt (parser);
20854   if (op == ERROR_MARK)
20855     return error_mark_node;
20856
20857   if (op != NOP_EXPR)
20858     {
20859       rhs = cp_parser_assignment_expression (parser, false);
20860       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20861       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20862     }
20863
20864   lhs = cp_parser_binary_expression (parser, false,
20865                                      PREC_ADDITIVE_EXPRESSION);
20866   token = cp_lexer_peek_token (parser->lexer);
20867   decl_first = lhs == decl;
20868   if (decl_first)
20869     lhs = NULL_TREE;
20870   if (token->type != CPP_PLUS
20871       && token->type != CPP_MINUS)
20872     return error_mark_node;
20873
20874   do
20875     {
20876       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20877       cp_lexer_consume_token (parser->lexer);
20878       rhs = cp_parser_binary_expression (parser, false,
20879                                          PREC_ADDITIVE_EXPRESSION);
20880       token = cp_lexer_peek_token (parser->lexer);
20881       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20882         {
20883           if (lhs == NULL_TREE)
20884             {
20885               if (op == PLUS_EXPR)
20886                 lhs = rhs;
20887               else
20888                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20889             }
20890           else
20891             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20892                                      NULL, tf_warning_or_error);
20893         }
20894     }
20895   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20896
20897   if (!decl_first)
20898     {
20899       if (rhs != decl || op == MINUS_EXPR)
20900         return error_mark_node;
20901       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20902     }
20903   else
20904     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20905
20906   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20907 }
20908
20909 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20910
20911 static tree
20912 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20913 {
20914   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20915   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20916   tree this_pre_body, cl;
20917   location_t loc_first;
20918   bool collapse_err = false;
20919   int i, collapse = 1, nbraces = 0;
20920
20921   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20922     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20923       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20924
20925   gcc_assert (collapse >= 1);
20926
20927   declv = make_tree_vec (collapse);
20928   initv = make_tree_vec (collapse);
20929   condv = make_tree_vec (collapse);
20930   incrv = make_tree_vec (collapse);
20931
20932   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20933
20934   for (i = 0; i < collapse; i++)
20935     {
20936       int bracecount = 0;
20937       bool add_private_clause = false;
20938       location_t loc;
20939
20940       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20941         {
20942           cp_parser_error (parser, "for statement expected");
20943           return NULL;
20944         }
20945       loc = cp_lexer_consume_token (parser->lexer)->location;
20946
20947       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20948         return NULL;
20949
20950       init = decl = real_decl = NULL;
20951       this_pre_body = push_stmt_list ();
20952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20953         {
20954           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
20955
20956              init-expr:
20957                        var = lb
20958                        integer-type var = lb
20959                        random-access-iterator-type var = lb
20960                        pointer-type var = lb
20961           */
20962           cp_decl_specifier_seq type_specifiers;
20963
20964           /* First, try to parse as an initialized declaration.  See
20965              cp_parser_condition, from whence the bulk of this is copied.  */
20966
20967           cp_parser_parse_tentatively (parser);
20968           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20969                                         &type_specifiers);
20970           if (cp_parser_parse_definitely (parser))
20971             {
20972               /* If parsing a type specifier seq succeeded, then this
20973                  MUST be a initialized declaration.  */
20974               tree asm_specification, attributes;
20975               cp_declarator *declarator;
20976
20977               declarator = cp_parser_declarator (parser,
20978                                                  CP_PARSER_DECLARATOR_NAMED,
20979                                                  /*ctor_dtor_or_conv_p=*/NULL,
20980                                                  /*parenthesized_p=*/NULL,
20981                                                  /*member_p=*/false);
20982               attributes = cp_parser_attributes_opt (parser);
20983               asm_specification = cp_parser_asm_specification_opt (parser);
20984
20985               if (declarator == cp_error_declarator) 
20986                 cp_parser_skip_to_end_of_statement (parser);
20987
20988               else 
20989                 {
20990                   tree pushed_scope;
20991
20992                   decl = start_decl (declarator, &type_specifiers,
20993                                      /*initialized_p=*/false, attributes,
20994                                      /*prefix_attributes=*/NULL_TREE,
20995                                      &pushed_scope);
20996
20997                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20998                     {
20999                       if (cp_lexer_next_token_is (parser->lexer, 
21000                                                   CPP_OPEN_PAREN))
21001                         error ("parenthesized initialization is not allowed in "
21002                                "OpenMP %<for%> loop");
21003                       else
21004                         /* Trigger an error.  */
21005                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21006
21007                       init = error_mark_node;
21008                       cp_parser_skip_to_end_of_statement (parser);
21009                     }
21010                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21011                            || type_dependent_expression_p (decl))
21012                     {
21013                       bool is_direct_init, is_non_constant_init;
21014
21015                       init = cp_parser_initializer (parser,
21016                                                     &is_direct_init,
21017                                                     &is_non_constant_init);
21018
21019                       cp_finish_decl (decl, init, !is_non_constant_init,
21020                                       asm_specification,
21021                                       LOOKUP_ONLYCONVERTING);
21022                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21023                         {
21024                           for_block
21025                             = tree_cons (NULL, this_pre_body, for_block);
21026                           init = NULL_TREE;
21027                         }
21028                       else
21029                         init = pop_stmt_list (this_pre_body);
21030                       this_pre_body = NULL_TREE;
21031                     }
21032                   else
21033                     {
21034                       /* Consume '='.  */
21035                       cp_lexer_consume_token (parser->lexer);
21036                       init = cp_parser_assignment_expression (parser, false);
21037
21038                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21039                         init = error_mark_node;
21040                       else
21041                         cp_finish_decl (decl, NULL_TREE,
21042                                         /*init_const_expr_p=*/false,
21043                                         asm_specification,
21044                                         LOOKUP_ONLYCONVERTING);
21045                     }
21046
21047                   if (pushed_scope)
21048                     pop_scope (pushed_scope);
21049                 }
21050             }
21051           else 
21052             {
21053               cp_id_kind idk;
21054               /* If parsing a type specifier sequence failed, then
21055                  this MUST be a simple expression.  */
21056               cp_parser_parse_tentatively (parser);
21057               decl = cp_parser_primary_expression (parser, false, false,
21058                                                    false, &idk);
21059               if (!cp_parser_error_occurred (parser)
21060                   && decl
21061                   && DECL_P (decl)
21062                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21063                 {
21064                   tree rhs;
21065
21066                   cp_parser_parse_definitely (parser);
21067                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21068                   rhs = cp_parser_assignment_expression (parser, false);
21069                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21070                                                          rhs,
21071                                                          tf_warning_or_error));
21072                   add_private_clause = true;
21073                 }
21074               else
21075                 {
21076                   decl = NULL;
21077                   cp_parser_abort_tentative_parse (parser);
21078                   init = cp_parser_expression (parser, false);
21079                   if (init)
21080                     {
21081                       if (TREE_CODE (init) == MODIFY_EXPR
21082                           || TREE_CODE (init) == MODOP_EXPR)
21083                         real_decl = TREE_OPERAND (init, 0);
21084                     }
21085                 }
21086             }
21087         }
21088       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21089       if (this_pre_body)
21090         {
21091           this_pre_body = pop_stmt_list (this_pre_body);
21092           if (pre_body)
21093             {
21094               tree t = pre_body;
21095               pre_body = push_stmt_list ();
21096               add_stmt (t);
21097               add_stmt (this_pre_body);
21098               pre_body = pop_stmt_list (pre_body);
21099             }
21100           else
21101             pre_body = this_pre_body;
21102         }
21103
21104       if (decl)
21105         real_decl = decl;
21106       if (par_clauses != NULL && real_decl != NULL_TREE)
21107         {
21108           tree *c;
21109           for (c = par_clauses; *c ; )
21110             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21111                 && OMP_CLAUSE_DECL (*c) == real_decl)
21112               {
21113                 error ("%Hiteration variable %qD should not be firstprivate",
21114                        &loc, real_decl);
21115                 *c = OMP_CLAUSE_CHAIN (*c);
21116               }
21117             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21118                      && OMP_CLAUSE_DECL (*c) == real_decl)
21119               {
21120                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21121                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21122                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21123                 OMP_CLAUSE_DECL (l) = real_decl;
21124                 OMP_CLAUSE_CHAIN (l) = clauses;
21125                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21126                 clauses = l;
21127                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21128                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21129                 add_private_clause = false;
21130               }
21131             else
21132               {
21133                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21134                     && OMP_CLAUSE_DECL (*c) == real_decl)
21135                   add_private_clause = false;
21136                 c = &OMP_CLAUSE_CHAIN (*c);
21137               }
21138         }
21139
21140       if (add_private_clause)
21141         {
21142           tree c;
21143           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21144             {
21145               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21146                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21147                   && OMP_CLAUSE_DECL (c) == decl)
21148                 break;
21149               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21150                        && OMP_CLAUSE_DECL (c) == decl)
21151                 error ("%Hiteration variable %qD should not be firstprivate",
21152                        &loc, decl);
21153               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21154                        && OMP_CLAUSE_DECL (c) == decl)
21155                 error ("%Hiteration variable %qD should not be reduction",
21156                        &loc, decl);
21157             }
21158           if (c == NULL)
21159             {
21160               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21161               OMP_CLAUSE_DECL (c) = decl;
21162               c = finish_omp_clauses (c);
21163               if (c)
21164                 {
21165                   OMP_CLAUSE_CHAIN (c) = clauses;
21166                   clauses = c;
21167                 }
21168             }
21169         }
21170
21171       cond = NULL;
21172       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21173         {
21174           /* If decl is an iterator, preserve LHS and RHS of the relational
21175              expr until finish_omp_for.  */
21176           if (decl
21177               && (type_dependent_expression_p (decl)
21178                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21179             cond = cp_parser_omp_for_cond (parser, decl);
21180           else
21181             cond = cp_parser_condition (parser);
21182         }
21183       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21184
21185       incr = NULL;
21186       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21187         {
21188           /* If decl is an iterator, preserve the operator on decl
21189              until finish_omp_for.  */
21190           if (decl
21191               && (type_dependent_expression_p (decl)
21192                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21193             incr = cp_parser_omp_for_incr (parser, decl);
21194           else
21195             incr = cp_parser_expression (parser, false);
21196         }
21197
21198       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21199         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21200                                                /*or_comma=*/false,
21201                                                /*consume_paren=*/true);
21202
21203       TREE_VEC_ELT (declv, i) = decl;
21204       TREE_VEC_ELT (initv, i) = init;
21205       TREE_VEC_ELT (condv, i) = cond;
21206       TREE_VEC_ELT (incrv, i) = incr;
21207
21208       if (i == collapse - 1)
21209         break;
21210
21211       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21212          in between the collapsed for loops to be still considered perfectly
21213          nested.  Hopefully the final version clarifies this.
21214          For now handle (multiple) {'s and empty statements.  */
21215       cp_parser_parse_tentatively (parser);
21216       do
21217         {
21218           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21219             break;
21220           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21221             {
21222               cp_lexer_consume_token (parser->lexer);
21223               bracecount++;
21224             }
21225           else if (bracecount
21226                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21227             cp_lexer_consume_token (parser->lexer);
21228           else
21229             {
21230               loc = cp_lexer_peek_token (parser->lexer)->location;
21231               error ("%Hnot enough collapsed for loops", &loc);
21232               collapse_err = true;
21233               cp_parser_abort_tentative_parse (parser);
21234               declv = NULL_TREE;
21235               break;
21236             }
21237         }
21238       while (1);
21239
21240       if (declv)
21241         {
21242           cp_parser_parse_definitely (parser);
21243           nbraces += bracecount;
21244         }
21245     }
21246
21247   /* Note that we saved the original contents of this flag when we entered
21248      the structured block, and so we don't need to re-save it here.  */
21249   parser->in_statement = IN_OMP_FOR;
21250
21251   /* Note that the grammar doesn't call for a structured block here,
21252      though the loop as a whole is a structured block.  */
21253   body = push_stmt_list ();
21254   cp_parser_statement (parser, NULL_TREE, false, NULL);
21255   body = pop_stmt_list (body);
21256
21257   if (declv == NULL_TREE)
21258     ret = NULL_TREE;
21259   else
21260     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21261                           pre_body, clauses);
21262
21263   while (nbraces)
21264     {
21265       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21266         {
21267           cp_lexer_consume_token (parser->lexer);
21268           nbraces--;
21269         }
21270       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21271         cp_lexer_consume_token (parser->lexer);
21272       else
21273         {
21274           if (!collapse_err)
21275             {
21276               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21277               error ("%Hcollapsed loops not perfectly nested", &loc);
21278             }
21279           collapse_err = true;
21280           cp_parser_statement_seq_opt (parser, NULL);
21281           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21282         }
21283     }
21284
21285   while (for_block)
21286     {
21287       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21288       for_block = TREE_CHAIN (for_block);
21289     }
21290
21291   return ret;
21292 }
21293
21294 /* OpenMP 2.5:
21295    #pragma omp for for-clause[optseq] new-line
21296      for-loop  */
21297
21298 #define OMP_FOR_CLAUSE_MASK                             \
21299         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21300         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21301         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21302         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21303         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21304         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21305         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21306         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21307
21308 static tree
21309 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21310 {
21311   tree clauses, sb, ret;
21312   unsigned int save;
21313
21314   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21315                                        "#pragma omp for", pragma_tok);
21316
21317   sb = begin_omp_structured_block ();
21318   save = cp_parser_begin_omp_structured_block (parser);
21319
21320   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21321
21322   cp_parser_end_omp_structured_block (parser, save);
21323   add_stmt (finish_omp_structured_block (sb));
21324
21325   return ret;
21326 }
21327
21328 /* OpenMP 2.5:
21329    # pragma omp master new-line
21330      structured-block  */
21331
21332 static tree
21333 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21334 {
21335   cp_parser_require_pragma_eol (parser, pragma_tok);
21336   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21337 }
21338
21339 /* OpenMP 2.5:
21340    # pragma omp ordered new-line
21341      structured-block  */
21342
21343 static tree
21344 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21345 {
21346   cp_parser_require_pragma_eol (parser, pragma_tok);
21347   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21348 }
21349
21350 /* OpenMP 2.5:
21351
21352    section-scope:
21353      { section-sequence }
21354
21355    section-sequence:
21356      section-directive[opt] structured-block
21357      section-sequence section-directive structured-block  */
21358
21359 static tree
21360 cp_parser_omp_sections_scope (cp_parser *parser)
21361 {
21362   tree stmt, substmt;
21363   bool error_suppress = false;
21364   cp_token *tok;
21365
21366   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21367     return NULL_TREE;
21368
21369   stmt = push_stmt_list ();
21370
21371   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21372     {
21373       unsigned save;
21374
21375       substmt = begin_omp_structured_block ();
21376       save = cp_parser_begin_omp_structured_block (parser);
21377
21378       while (1)
21379         {
21380           cp_parser_statement (parser, NULL_TREE, false, NULL);
21381
21382           tok = cp_lexer_peek_token (parser->lexer);
21383           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21384             break;
21385           if (tok->type == CPP_CLOSE_BRACE)
21386             break;
21387           if (tok->type == CPP_EOF)
21388             break;
21389         }
21390
21391       cp_parser_end_omp_structured_block (parser, save);
21392       substmt = finish_omp_structured_block (substmt);
21393       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21394       add_stmt (substmt);
21395     }
21396
21397   while (1)
21398     {
21399       tok = cp_lexer_peek_token (parser->lexer);
21400       if (tok->type == CPP_CLOSE_BRACE)
21401         break;
21402       if (tok->type == CPP_EOF)
21403         break;
21404
21405       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21406         {
21407           cp_lexer_consume_token (parser->lexer);
21408           cp_parser_require_pragma_eol (parser, tok);
21409           error_suppress = false;
21410         }
21411       else if (!error_suppress)
21412         {
21413           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21414           error_suppress = true;
21415         }
21416
21417       substmt = cp_parser_omp_structured_block (parser);
21418       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21419       add_stmt (substmt);
21420     }
21421   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21422
21423   substmt = pop_stmt_list (stmt);
21424
21425   stmt = make_node (OMP_SECTIONS);
21426   TREE_TYPE (stmt) = void_type_node;
21427   OMP_SECTIONS_BODY (stmt) = substmt;
21428
21429   add_stmt (stmt);
21430   return stmt;
21431 }
21432
21433 /* OpenMP 2.5:
21434    # pragma omp sections sections-clause[optseq] newline
21435      sections-scope  */
21436
21437 #define OMP_SECTIONS_CLAUSE_MASK                        \
21438         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21439         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21440         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21441         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21442         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21443
21444 static tree
21445 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21446 {
21447   tree clauses, ret;
21448
21449   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21450                                        "#pragma omp sections", pragma_tok);
21451
21452   ret = cp_parser_omp_sections_scope (parser);
21453   if (ret)
21454     OMP_SECTIONS_CLAUSES (ret) = clauses;
21455
21456   return ret;
21457 }
21458
21459 /* OpenMP 2.5:
21460    # pragma parallel parallel-clause new-line
21461    # pragma parallel for parallel-for-clause new-line
21462    # pragma parallel sections parallel-sections-clause new-line  */
21463
21464 #define OMP_PARALLEL_CLAUSE_MASK                        \
21465         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21466         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21467         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21468         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21469         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21470         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21471         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21472         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21473
21474 static tree
21475 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21476 {
21477   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21478   const char *p_name = "#pragma omp parallel";
21479   tree stmt, clauses, par_clause, ws_clause, block;
21480   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21481   unsigned int save;
21482
21483   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21484     {
21485       cp_lexer_consume_token (parser->lexer);
21486       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21487       p_name = "#pragma omp parallel for";
21488       mask |= OMP_FOR_CLAUSE_MASK;
21489       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21490     }
21491   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21492     {
21493       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21494       const char *p = IDENTIFIER_POINTER (id);
21495       if (strcmp (p, "sections") == 0)
21496         {
21497           cp_lexer_consume_token (parser->lexer);
21498           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21499           p_name = "#pragma omp parallel sections";
21500           mask |= OMP_SECTIONS_CLAUSE_MASK;
21501           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21502         }
21503     }
21504
21505   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21506   block = begin_omp_parallel ();
21507   save = cp_parser_begin_omp_structured_block (parser);
21508
21509   switch (p_kind)
21510     {
21511     case PRAGMA_OMP_PARALLEL:
21512       cp_parser_statement (parser, NULL_TREE, false, NULL);
21513       par_clause = clauses;
21514       break;
21515
21516     case PRAGMA_OMP_PARALLEL_FOR:
21517       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21518       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21519       break;
21520
21521     case PRAGMA_OMP_PARALLEL_SECTIONS:
21522       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21523       stmt = cp_parser_omp_sections_scope (parser);
21524       if (stmt)
21525         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21526       break;
21527
21528     default:
21529       gcc_unreachable ();
21530     }
21531
21532   cp_parser_end_omp_structured_block (parser, save);
21533   stmt = finish_omp_parallel (par_clause, block);
21534   if (p_kind != PRAGMA_OMP_PARALLEL)
21535     OMP_PARALLEL_COMBINED (stmt) = 1;
21536   return stmt;
21537 }
21538
21539 /* OpenMP 2.5:
21540    # pragma omp single single-clause[optseq] new-line
21541      structured-block  */
21542
21543 #define OMP_SINGLE_CLAUSE_MASK                          \
21544         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21545         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21546         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21547         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21548
21549 static tree
21550 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21551 {
21552   tree stmt = make_node (OMP_SINGLE);
21553   TREE_TYPE (stmt) = void_type_node;
21554
21555   OMP_SINGLE_CLAUSES (stmt)
21556     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21557                                  "#pragma omp single", pragma_tok);
21558   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21559
21560   return add_stmt (stmt);
21561 }
21562
21563 /* OpenMP 3.0:
21564    # pragma omp task task-clause[optseq] new-line
21565      structured-block  */
21566
21567 #define OMP_TASK_CLAUSE_MASK                            \
21568         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21569         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21570         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21571         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21572         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21573         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21574
21575 static tree
21576 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21577 {
21578   tree clauses, block;
21579   unsigned int save;
21580
21581   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21582                                        "#pragma omp task", pragma_tok);
21583   block = begin_omp_task ();
21584   save = cp_parser_begin_omp_structured_block (parser);
21585   cp_parser_statement (parser, NULL_TREE, false, NULL);
21586   cp_parser_end_omp_structured_block (parser, save);
21587   return finish_omp_task (clauses, block);
21588 }
21589
21590 /* OpenMP 3.0:
21591    # pragma omp taskwait new-line  */
21592
21593 static void
21594 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21595 {
21596   cp_parser_require_pragma_eol (parser, pragma_tok);
21597   finish_omp_taskwait ();
21598 }
21599
21600 /* OpenMP 2.5:
21601    # pragma omp threadprivate (variable-list) */
21602
21603 static void
21604 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21605 {
21606   tree vars;
21607
21608   vars = cp_parser_omp_var_list (parser, 0, NULL);
21609   cp_parser_require_pragma_eol (parser, pragma_tok);
21610
21611   finish_omp_threadprivate (vars);
21612 }
21613
21614 /* Main entry point to OpenMP statement pragmas.  */
21615
21616 static void
21617 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21618 {
21619   tree stmt;
21620
21621   switch (pragma_tok->pragma_kind)
21622     {
21623     case PRAGMA_OMP_ATOMIC:
21624       cp_parser_omp_atomic (parser, pragma_tok);
21625       return;
21626     case PRAGMA_OMP_CRITICAL:
21627       stmt = cp_parser_omp_critical (parser, pragma_tok);
21628       break;
21629     case PRAGMA_OMP_FOR:
21630       stmt = cp_parser_omp_for (parser, pragma_tok);
21631       break;
21632     case PRAGMA_OMP_MASTER:
21633       stmt = cp_parser_omp_master (parser, pragma_tok);
21634       break;
21635     case PRAGMA_OMP_ORDERED:
21636       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21637       break;
21638     case PRAGMA_OMP_PARALLEL:
21639       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21640       break;
21641     case PRAGMA_OMP_SECTIONS:
21642       stmt = cp_parser_omp_sections (parser, pragma_tok);
21643       break;
21644     case PRAGMA_OMP_SINGLE:
21645       stmt = cp_parser_omp_single (parser, pragma_tok);
21646       break;
21647     case PRAGMA_OMP_TASK:
21648       stmt = cp_parser_omp_task (parser, pragma_tok);
21649       break;
21650     default:
21651       gcc_unreachable ();
21652     }
21653
21654   if (stmt)
21655     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21656 }
21657 \f
21658 /* The parser.  */
21659
21660 static GTY (()) cp_parser *the_parser;
21661
21662 \f
21663 /* Special handling for the first token or line in the file.  The first
21664    thing in the file might be #pragma GCC pch_preprocess, which loads a
21665    PCH file, which is a GC collection point.  So we need to handle this
21666    first pragma without benefit of an existing lexer structure.
21667
21668    Always returns one token to the caller in *FIRST_TOKEN.  This is
21669    either the true first token of the file, or the first token after
21670    the initial pragma.  */
21671
21672 static void
21673 cp_parser_initial_pragma (cp_token *first_token)
21674 {
21675   tree name = NULL;
21676
21677   cp_lexer_get_preprocessor_token (NULL, first_token);
21678   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21679     return;
21680
21681   cp_lexer_get_preprocessor_token (NULL, first_token);
21682   if (first_token->type == CPP_STRING)
21683     {
21684       name = first_token->u.value;
21685
21686       cp_lexer_get_preprocessor_token (NULL, first_token);
21687       if (first_token->type != CPP_PRAGMA_EOL)
21688         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21689                &first_token->location);
21690     }
21691   else
21692     error ("%Hexpected string literal", &first_token->location);
21693
21694   /* Skip to the end of the pragma.  */
21695   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21696     cp_lexer_get_preprocessor_token (NULL, first_token);
21697
21698   /* Now actually load the PCH file.  */
21699   if (name)
21700     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21701
21702   /* Read one more token to return to our caller.  We have to do this
21703      after reading the PCH file in, since its pointers have to be
21704      live.  */
21705   cp_lexer_get_preprocessor_token (NULL, first_token);
21706 }
21707
21708 /* Normal parsing of a pragma token.  Here we can (and must) use the
21709    regular lexer.  */
21710
21711 static bool
21712 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21713 {
21714   cp_token *pragma_tok;
21715   unsigned int id;
21716
21717   pragma_tok = cp_lexer_consume_token (parser->lexer);
21718   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21719   parser->lexer->in_pragma = true;
21720
21721   id = pragma_tok->pragma_kind;
21722   switch (id)
21723     {
21724     case PRAGMA_GCC_PCH_PREPROCESS:
21725       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21726              &pragma_tok->location);
21727       break;
21728
21729     case PRAGMA_OMP_BARRIER:
21730       switch (context)
21731         {
21732         case pragma_compound:
21733           cp_parser_omp_barrier (parser, pragma_tok);
21734           return false;
21735         case pragma_stmt:
21736           error ("%H%<#pragma omp barrier%> may only be "
21737                  "used in compound statements", &pragma_tok->location);
21738           break;
21739         default:
21740           goto bad_stmt;
21741         }
21742       break;
21743
21744     case PRAGMA_OMP_FLUSH:
21745       switch (context)
21746         {
21747         case pragma_compound:
21748           cp_parser_omp_flush (parser, pragma_tok);
21749           return false;
21750         case pragma_stmt:
21751           error ("%H%<#pragma omp flush%> may only be "
21752                  "used in compound statements", &pragma_tok->location);
21753           break;
21754         default:
21755           goto bad_stmt;
21756         }
21757       break;
21758
21759     case PRAGMA_OMP_TASKWAIT:
21760       switch (context)
21761         {
21762         case pragma_compound:
21763           cp_parser_omp_taskwait (parser, pragma_tok);
21764           return false;
21765         case pragma_stmt:
21766           error ("%H%<#pragma omp taskwait%> may only be "
21767                  "used in compound statements",
21768                  &pragma_tok->location);
21769           break;
21770         default:
21771           goto bad_stmt;
21772         }
21773       break;
21774
21775     case PRAGMA_OMP_THREADPRIVATE:
21776       cp_parser_omp_threadprivate (parser, pragma_tok);
21777       return false;
21778
21779     case PRAGMA_OMP_ATOMIC:
21780     case PRAGMA_OMP_CRITICAL:
21781     case PRAGMA_OMP_FOR:
21782     case PRAGMA_OMP_MASTER:
21783     case PRAGMA_OMP_ORDERED:
21784     case PRAGMA_OMP_PARALLEL:
21785     case PRAGMA_OMP_SECTIONS:
21786     case PRAGMA_OMP_SINGLE:
21787     case PRAGMA_OMP_TASK:
21788       if (context == pragma_external)
21789         goto bad_stmt;
21790       cp_parser_omp_construct (parser, pragma_tok);
21791       return true;
21792
21793     case PRAGMA_OMP_SECTION:
21794       error ("%H%<#pragma omp section%> may only be used in "
21795              "%<#pragma omp sections%> construct", &pragma_tok->location);
21796       break;
21797
21798     default:
21799       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21800       c_invoke_pragma_handler (id);
21801       break;
21802
21803     bad_stmt:
21804       cp_parser_error (parser, "expected declaration specifiers");
21805       break;
21806     }
21807
21808   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21809   return false;
21810 }
21811
21812 /* The interface the pragma parsers have to the lexer.  */
21813
21814 enum cpp_ttype
21815 pragma_lex (tree *value)
21816 {
21817   cp_token *tok;
21818   enum cpp_ttype ret;
21819
21820   tok = cp_lexer_peek_token (the_parser->lexer);
21821
21822   ret = tok->type;
21823   *value = tok->u.value;
21824
21825   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21826     ret = CPP_EOF;
21827   else if (ret == CPP_STRING)
21828     *value = cp_parser_string_literal (the_parser, false, false);
21829   else
21830     {
21831       cp_lexer_consume_token (the_parser->lexer);
21832       if (ret == CPP_KEYWORD)
21833         ret = CPP_NAME;
21834     }
21835
21836   return ret;
21837 }
21838
21839 \f
21840 /* External interface.  */
21841
21842 /* Parse one entire translation unit.  */
21843
21844 void
21845 c_parse_file (void)
21846 {
21847   bool error_occurred;
21848   static bool already_called = false;
21849
21850   if (already_called)
21851     {
21852       sorry ("inter-module optimizations not implemented for C++");
21853       return;
21854     }
21855   already_called = true;
21856
21857   the_parser = cp_parser_new ();
21858   push_deferring_access_checks (flag_access_control
21859                                 ? dk_no_deferred : dk_no_check);
21860   error_occurred = cp_parser_translation_unit (the_parser);
21861   the_parser = NULL;
21862 }
21863
21864 #include "gt-cp-parser.h"