OSDN Git Service

2007-07-21 Rafael Avila de Espindola <espindola@google.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 bottomost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_class_or_namespace_name
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_declarator_id
1730   (cp_parser *, bool);
1731 static tree cp_parser_type_id
1732   (cp_parser *);
1733 static void cp_parser_type_specifier_seq
1734   (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1736   (cp_parser *);
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738   (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740   (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument 
1742   (cp_parser *, bool);
1743 static void cp_parser_function_body
1744   (cp_parser *);
1745 static tree cp_parser_initializer
1746   (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748   (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750   (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752   (cp_parser *, bool *);
1753
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1755   (cp_parser *);
1756
1757 /* Classes [gram.class] */
1758
1759 static tree cp_parser_class_name
1760   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1762   (cp_parser *);
1763 static tree cp_parser_class_head
1764   (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1766   (cp_parser *);
1767 static void cp_parser_member_specification_opt
1768   (cp_parser *);
1769 static void cp_parser_member_declaration
1770   (cp_parser *);
1771 static tree cp_parser_pure_specifier
1772   (cp_parser *);
1773 static tree cp_parser_constant_initializer
1774   (cp_parser *);
1775
1776 /* Derived classes [gram.class.derived] */
1777
1778 static tree cp_parser_base_clause
1779   (cp_parser *);
1780 static tree cp_parser_base_specifier
1781   (cp_parser *);
1782
1783 /* Special member functions [gram.special] */
1784
1785 static tree cp_parser_conversion_function_id
1786   (cp_parser *);
1787 static tree cp_parser_conversion_type_id
1788   (cp_parser *);
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1790   (cp_parser *);
1791 static bool cp_parser_ctor_initializer_opt
1792   (cp_parser *);
1793 static void cp_parser_mem_initializer_list
1794   (cp_parser *);
1795 static tree cp_parser_mem_initializer
1796   (cp_parser *);
1797 static tree cp_parser_mem_initializer_id
1798   (cp_parser *);
1799
1800 /* Overloading [gram.over] */
1801
1802 static tree cp_parser_operator_function_id
1803   (cp_parser *);
1804 static tree cp_parser_operator
1805   (cp_parser *);
1806
1807 /* Templates [gram.temp] */
1808
1809 static void cp_parser_template_declaration
1810   (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1812   (cp_parser *);
1813 static tree cp_parser_template_parameter
1814   (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816   (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818   (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820   (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1822   (cp_parser *);
1823 static tree cp_parser_template_argument
1824   (cp_parser *);
1825 static void cp_parser_explicit_instantiation
1826   (cp_parser *);
1827 static void cp_parser_explicit_specialization
1828   (cp_parser *);
1829
1830 /* Exception handling [gram.exception] */
1831
1832 static tree cp_parser_try_block
1833   (cp_parser *);
1834 static bool cp_parser_function_try_block
1835   (cp_parser *);
1836 static void cp_parser_handler_seq
1837   (cp_parser *);
1838 static void cp_parser_handler
1839   (cp_parser *);
1840 static tree cp_parser_exception_declaration
1841   (cp_parser *);
1842 static tree cp_parser_throw_expression
1843   (cp_parser *);
1844 static tree cp_parser_exception_specification_opt
1845   (cp_parser *);
1846 static tree cp_parser_type_id_list
1847   (cp_parser *);
1848
1849 /* GNU Extensions */
1850
1851 static tree cp_parser_asm_specification_opt
1852   (cp_parser *);
1853 static tree cp_parser_asm_operand_list
1854   (cp_parser *);
1855 static tree cp_parser_asm_clobber_list
1856   (cp_parser *);
1857 static tree cp_parser_attributes_opt
1858   (cp_parser *);
1859 static tree cp_parser_attribute_list
1860   (cp_parser *);
1861 static bool cp_parser_extension_opt
1862   (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1864   (cp_parser *);
1865
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868   (cp_parser *, enum pragma_context);
1869
1870 /* Objective-C++ Productions */
1871
1872 static tree cp_parser_objc_message_receiver
1873   (cp_parser *);
1874 static tree cp_parser_objc_message_args
1875   (cp_parser *);
1876 static tree cp_parser_objc_message_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_encode_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_defs_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_protocol_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_selector_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_expression
1887   (cp_parser *);
1888 static bool cp_parser_objc_selector_p
1889   (enum cpp_ttype);
1890 static tree cp_parser_objc_selector
1891   (cp_parser *);
1892 static tree cp_parser_objc_protocol_refs_opt
1893   (cp_parser *);
1894 static void cp_parser_objc_declaration
1895   (cp_parser *);
1896 static tree cp_parser_objc_statement
1897   (cp_parser *);
1898
1899 /* Utility Routines */
1900
1901 static tree cp_parser_lookup_name
1902   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904   (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1906   (tree, bool);
1907 static bool cp_parser_check_declarator_template_parameters
1908   (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910   (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1912   (cp_parser *);
1913 static tree cp_parser_global_scope_opt
1914   (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916   (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920   (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922   (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924   (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928   (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1932   (cp_parser *);
1933 static void cp_parser_save_default_args
1934   (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936   (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938   (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940   (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942   (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1944   (cp_parser *);
1945 static void cp_parser_set_storage_class
1946   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948   (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950   (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952   (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954   (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1956   (cp_token *);
1957 static bool cp_parser_next_token_starts_class_definition_p
1958   (cp_parser *);
1959 static bool cp_parser_next_token_ends_template_argument_p
1960   (cp_parser *);
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962   (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1964   (cp_token *);
1965 static void cp_parser_check_class_key
1966   (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968   (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1970   (cp_parser *);
1971 static void cp_parser_pre_parsed_nested_name_specifier
1972   (cp_parser *);
1973 static bool cp_parser_cache_group
1974   (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1976   (cp_parser *);
1977 static void cp_parser_commit_to_tentative_parse
1978   (cp_parser *);
1979 static void cp_parser_abort_tentative_parse
1980   (cp_parser *);
1981 static bool cp_parser_parse_definitely
1982   (cp_parser *);
1983 static inline bool cp_parser_parsing_tentatively
1984   (cp_parser *);
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1986   (cp_parser *);
1987 static void cp_parser_error
1988   (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990   (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1992   (cp_parser *);
1993 static bool cp_parser_check_type_definition
1994   (cp_parser *);
1995 static void cp_parser_check_for_definition_in_return_type
1996   (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998   (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000   (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002   (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2004   (cp_parser *);
2005 static int cp_parser_skip_to_closing_parenthesis
2006   (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2008   (cp_parser *);
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2010   (cp_parser *);
2011 static void cp_parser_skip_to_end_of_block_or_statement
2012   (cp_parser *);
2013 static bool cp_parser_skip_to_closing_brace
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2016   (cp_parser *);
2017 static void cp_parser_skip_to_pragma_eol
2018   (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2020   (cp_parser *);
2021 static bool cp_parser_allow_gnu_extensions_p
2022   (cp_parser *);
2023 static bool cp_parser_is_string_literal
2024   (cp_token *);
2025 static bool cp_parser_is_keyword
2026   (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028   (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2031
2032 /* Returns nonzero if we are parsing tentatively.  */
2033
2034 static inline bool
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2036 {
2037   return parser->context->next != NULL;
2038 }
2039
2040 /* Returns nonzero if TOKEN is a string literal.  */
2041
2042 static bool
2043 cp_parser_is_string_literal (cp_token* token)
2044 {
2045   return (token->type == CPP_STRING ||
2046           token->type == CPP_STRING16 ||
2047           token->type == CPP_STRING32 ||
2048           token->type == CPP_WSTRING);
2049 }
2050
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2052
2053 static bool
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2055 {
2056   return token->keyword == keyword;
2057 }
2058
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060       FILE:LINE: MESSAGE before TOKEN
2061    where TOKEN is the next token in the input stream.  MESSAGE
2062    (specified by the caller) is usually of the form "expected
2063    OTHER-TOKEN".  */
2064
2065 static void
2066 cp_parser_error (cp_parser* parser, const char* message)
2067 {
2068   if (!cp_parser_simulate_error (parser))
2069     {
2070       cp_token *token = cp_lexer_peek_token (parser->lexer);
2071       /* This diagnostic makes more sense if it is tagged to the line
2072          of the token we just peeked at.  */
2073       cp_lexer_set_source_position_from_token (token);
2074
2075       if (token->type == CPP_PRAGMA)
2076         {
2077           error ("%H%<#pragma%> is not allowed here", &token->location);
2078           cp_parser_skip_to_pragma_eol (parser, token);
2079           return;
2080         }
2081
2082       c_parse_error (message,
2083                      /* Because c_parser_error does not understand
2084                         CPP_KEYWORD, keywords are treated like
2085                         identifiers.  */
2086                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2087                      token->u.value);
2088     }
2089 }
2090
2091 /* Issue an error about name-lookup failing.  NAME is the
2092    IDENTIFIER_NODE DECL is the result of
2093    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2094    the thing that we hoped to find.  */
2095
2096 static void
2097 cp_parser_name_lookup_error (cp_parser* parser,
2098                              tree name,
2099                              tree decl,
2100                              const char* desired,
2101                              location_t location)
2102 {
2103   /* If name lookup completely failed, tell the user that NAME was not
2104      declared.  */
2105   if (decl == error_mark_node)
2106     {
2107       if (parser->scope && parser->scope != global_namespace)
2108         error ("%H%<%E::%E%> has not been declared",
2109                &location, parser->scope, name);
2110       else if (parser->scope == global_namespace)
2111         error ("%H%<::%E%> has not been declared", &location, name);
2112       else if (parser->object_scope
2113                && !CLASS_TYPE_P (parser->object_scope))
2114         error ("%Hrequest for member %qE in non-class type %qT",
2115                &location, name, parser->object_scope);
2116       else if (parser->object_scope)
2117         error ("%H%<%T::%E%> has not been declared",
2118                &location, parser->object_scope, name);
2119       else
2120         error ("%H%qE has not been declared", &location, name);
2121     }
2122   else if (parser->scope && parser->scope != global_namespace)
2123     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124   else if (parser->scope == global_namespace)
2125     error ("%H%<::%E%> %s", &location, name, desired);
2126   else
2127     error ("%H%qE %s", &location, name, desired);
2128 }
2129
2130 /* If we are parsing tentatively, remember that an error has occurred
2131    during this tentative parse.  Returns true if the error was
2132    simulated; false if a message should be issued by the caller.  */
2133
2134 static bool
2135 cp_parser_simulate_error (cp_parser* parser)
2136 {
2137   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2138     {
2139       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2140       return true;
2141     }
2142   return false;
2143 }
2144
2145 /* Check for repeated decl-specifiers.  */
2146
2147 static void
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149                            location_t location)
2150 {
2151   cp_decl_spec ds;
2152
2153   for (ds = ds_first; ds != ds_last; ++ds)
2154     {
2155       unsigned count = decl_specs->specs[(int)ds];
2156       if (count < 2)
2157         continue;
2158       /* The "long" specifier is a special case because of "long long".  */
2159       if (ds == ds_long)
2160         {
2161           if (count > 2)
2162             error ("%H%<long long long%> is too long for GCC", &location);
2163           else if (pedantic && !in_system_header && warn_long_long
2164                    && cxx_dialect == cxx98)
2165             pedwarn ("%HISO C++ 1998 does not support %<long long%>",
2166                      &location);
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 ("%H(perhaps a semicolon is missing after the definition of %qT)",
2232               &type_location, type);
2233     }
2234 }
2235
2236 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2237    "<" in any valid C++ program.  If the next token is indeed "<",
2238    issue a message warning the user about what appears to be an
2239    invalid attempt to form a template-id. LOCATION is the location
2240    of the type-specifier (TYPE) */
2241
2242 static void
2243 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2244                                          tree type, location_t location)
2245 {
2246   cp_token_position start = 0;
2247
2248   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2249     {
2250       if (TYPE_P (type))
2251         error ("%H%qT is not a template", &location, type);
2252       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2253         error ("%H%qE is not a template", &location, type);
2254       else
2255         error ("%Hinvalid template-id", &location);
2256       /* Remember the location of the invalid "<".  */
2257       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2258         start = cp_lexer_token_position (parser->lexer, true);
2259       /* Consume the "<".  */
2260       cp_lexer_consume_token (parser->lexer);
2261       /* Parse the template arguments.  */
2262       cp_parser_enclosed_template_argument_list (parser);
2263       /* Permanently remove the invalid template arguments so that
2264          this error message is not issued again.  */
2265       if (start)
2266         cp_lexer_purge_tokens_after (parser->lexer, start);
2267     }
2268 }
2269
2270 /* If parsing an integral constant-expression, issue an error message
2271    about the fact that THING appeared and return true.  Otherwise,
2272    return false.  In either case, set
2273    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2274
2275 static bool
2276 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2277                                             const char *thing)
2278 {
2279   parser->non_integral_constant_expression_p = true;
2280   if (parser->integral_constant_expression_p)
2281     {
2282       if (!parser->allow_non_integral_constant_expression_p)
2283         {
2284           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2285              in the message need to be interpreted.  */
2286           char *message = concat (thing,
2287                                   " cannot appear in a constant-expression",
2288                                   NULL);
2289           error (message);
2290           free (message);
2291           return true;
2292         }
2293     }
2294   return false;
2295 }
2296
2297 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2298    qualifying scope (or NULL, if none) for ID.  This function commits
2299    to the current active tentative parse, if any.  (Otherwise, the
2300    problematic construct might be encountered again later, resulting
2301    in duplicate error messages.) LOCATION is the location of ID.  */
2302
2303 static void
2304 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2305                                       tree scope, tree id,
2306                                       location_t location)
2307 {
2308   tree decl, old_scope;
2309   /* Try to lookup the identifier.  */
2310   old_scope = parser->scope;
2311   parser->scope = scope;
2312   decl = cp_parser_lookup_name_simple (parser, id, location);
2313   parser->scope = old_scope;
2314   /* If the lookup found a template-name, it means that the user forgot
2315   to specify an argument list. Emit a useful error message.  */
2316   if (TREE_CODE (decl) == TEMPLATE_DECL)
2317     error ("%Hinvalid use of template-name %qE without an argument list",
2318            &location, decl);
2319   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2320     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2321   else if (TREE_CODE (decl) == TYPE_DECL)
2322     /* Something like 'unsigned A a;'  */
2323     error ("%Hinvalid combination of multiple type-specifiers",
2324            &location);
2325   else if (!parser->scope)
2326     {
2327       /* Issue an error message.  */
2328       error ("%H%qE does not name a type", &location, id);
2329       /* If we're in a template class, it's possible that the user was
2330          referring to a type from a base class.  For example:
2331
2332            template <typename T> struct A { typedef T X; };
2333            template <typename T> struct B : public A<T> { X x; };
2334
2335          The user should have said "typename A<T>::X".  */
2336       if (processing_template_decl && current_class_type
2337           && TYPE_BINFO (current_class_type))
2338         {
2339           tree b;
2340
2341           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2342                b;
2343                b = TREE_CHAIN (b))
2344             {
2345               tree base_type = BINFO_TYPE (b);
2346               if (CLASS_TYPE_P (base_type)
2347                   && dependent_type_p (base_type))
2348                 {
2349                   tree field;
2350                   /* Go from a particular instantiation of the
2351                      template (which will have an empty TYPE_FIELDs),
2352                      to the main version.  */
2353                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2354                   for (field = TYPE_FIELDS (base_type);
2355                        field;
2356                        field = TREE_CHAIN (field))
2357                     if (TREE_CODE (field) == TYPE_DECL
2358                         && DECL_NAME (field) == id)
2359                       {
2360                         inform ("%H(perhaps %<typename %T::%E%> was intended)",
2361                                 &location, BINFO_TYPE (b), id);
2362                         break;
2363                       }
2364                   if (field)
2365                     break;
2366                 }
2367             }
2368         }
2369     }
2370   /* Here we diagnose qualified-ids where the scope is actually correct,
2371      but the identifier does not resolve to a valid type name.  */
2372   else if (parser->scope != error_mark_node)
2373     {
2374       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2375         error ("%H%qE in namespace %qE does not name a type",
2376                &location, id, parser->scope);
2377       else if (TYPE_P (parser->scope))
2378         error ("%H%qE in class %qT does not name a type",
2379                &location, id, parser->scope);
2380       else
2381         gcc_unreachable ();
2382     }
2383   cp_parser_commit_to_tentative_parse (parser);
2384 }
2385
2386 /* Check for a common situation where a type-name should be present,
2387    but is not, and issue a sensible error message.  Returns true if an
2388    invalid type-name was detected.
2389
2390    The situation handled by this function are variable declarations of the
2391    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2392    Usually, `ID' should name a type, but if we got here it means that it
2393    does not. We try to emit the best possible error message depending on
2394    how exactly the id-expression looks like.  */
2395
2396 static bool
2397 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2398 {
2399   tree id;
2400   cp_token *token = cp_lexer_peek_token (parser->lexer);
2401
2402   cp_parser_parse_tentatively (parser);
2403   id = cp_parser_id_expression (parser,
2404                                 /*template_keyword_p=*/false,
2405                                 /*check_dependency_p=*/true,
2406                                 /*template_p=*/NULL,
2407                                 /*declarator_p=*/true,
2408                                 /*optional_p=*/false);
2409   /* After the id-expression, there should be a plain identifier,
2410      otherwise this is not a simple variable declaration. Also, if
2411      the scope is dependent, we cannot do much.  */
2412   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2413       || (parser->scope && TYPE_P (parser->scope)
2414           && dependent_type_p (parser->scope))
2415       || TREE_CODE (id) == TYPE_DECL)
2416     {
2417       cp_parser_abort_tentative_parse (parser);
2418       return false;
2419     }
2420   if (!cp_parser_parse_definitely (parser))
2421     return false;
2422
2423   /* Emit a diagnostic for the invalid type.  */
2424   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2425                                         id, token->location);
2426   /* Skip to the end of the declaration; there's no point in
2427      trying to process it.  */
2428   cp_parser_skip_to_end_of_block_or_statement (parser);
2429   return true;
2430 }
2431
2432 /* Consume tokens up to, and including, the next non-nested closing `)'.
2433    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2434    are doing error recovery. Returns -1 if OR_COMMA is true and we
2435    found an unnested comma.  */
2436
2437 static int
2438 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2439                                        bool recovering,
2440                                        bool or_comma,
2441                                        bool consume_paren)
2442 {
2443   unsigned paren_depth = 0;
2444   unsigned brace_depth = 0;
2445
2446   if (recovering && !or_comma
2447       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2448     return 0;
2449
2450   while (true)
2451     {
2452       cp_token * token = cp_lexer_peek_token (parser->lexer);
2453
2454       switch (token->type)
2455         {
2456         case CPP_EOF:
2457         case CPP_PRAGMA_EOL:
2458           /* If we've run out of tokens, then there is no closing `)'.  */
2459           return 0;
2460
2461         case CPP_SEMICOLON:
2462           /* This matches the processing in skip_to_end_of_statement.  */
2463           if (!brace_depth)
2464             return 0;
2465           break;
2466
2467         case CPP_OPEN_BRACE:
2468           ++brace_depth;
2469           break;
2470         case CPP_CLOSE_BRACE:
2471           if (!brace_depth--)
2472             return 0;
2473           break;
2474
2475         case CPP_COMMA:
2476           if (recovering && or_comma && !brace_depth && !paren_depth)
2477             return -1;
2478           break;
2479
2480         case CPP_OPEN_PAREN:
2481           if (!brace_depth)
2482             ++paren_depth;
2483           break;
2484
2485         case CPP_CLOSE_PAREN:
2486           if (!brace_depth && !paren_depth--)
2487             {
2488               if (consume_paren)
2489                 cp_lexer_consume_token (parser->lexer);
2490               return 1;
2491             }
2492           break;
2493
2494         default:
2495           break;
2496         }
2497
2498       /* Consume the token.  */
2499       cp_lexer_consume_token (parser->lexer);
2500     }
2501 }
2502
2503 /* Consume tokens until we reach the end of the current statement.
2504    Normally, that will be just before consuming a `;'.  However, if a
2505    non-nested `}' comes first, then we stop before consuming that.  */
2506
2507 static void
2508 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2509 {
2510   unsigned nesting_depth = 0;
2511
2512   while (true)
2513     {
2514       cp_token *token = cp_lexer_peek_token (parser->lexer);
2515
2516       switch (token->type)
2517         {
2518         case CPP_EOF:
2519         case CPP_PRAGMA_EOL:
2520           /* If we've run out of tokens, stop.  */
2521           return;
2522
2523         case CPP_SEMICOLON:
2524           /* If the next token is a `;', we have reached the end of the
2525              statement.  */
2526           if (!nesting_depth)
2527             return;
2528           break;
2529
2530         case CPP_CLOSE_BRACE:
2531           /* If this is a non-nested '}', stop before consuming it.
2532              That way, when confronted with something like:
2533
2534                { 3 + }
2535
2536              we stop before consuming the closing '}', even though we
2537              have not yet reached a `;'.  */
2538           if (nesting_depth == 0)
2539             return;
2540
2541           /* If it is the closing '}' for a block that we have
2542              scanned, stop -- but only after consuming the token.
2543              That way given:
2544
2545                 void f g () { ... }
2546                 typedef int I;
2547
2548              we will stop after the body of the erroneously declared
2549              function, but before consuming the following `typedef'
2550              declaration.  */
2551           if (--nesting_depth == 0)
2552             {
2553               cp_lexer_consume_token (parser->lexer);
2554               return;
2555             }
2556
2557         case CPP_OPEN_BRACE:
2558           ++nesting_depth;
2559           break;
2560
2561         default:
2562           break;
2563         }
2564
2565       /* Consume the token.  */
2566       cp_lexer_consume_token (parser->lexer);
2567     }
2568 }
2569
2570 /* This function is called at the end of a statement or declaration.
2571    If the next token is a semicolon, it is consumed; otherwise, error
2572    recovery is attempted.  */
2573
2574 static void
2575 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2576 {
2577   /* Look for the trailing `;'.  */
2578   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2579     {
2580       /* If there is additional (erroneous) input, skip to the end of
2581          the statement.  */
2582       cp_parser_skip_to_end_of_statement (parser);
2583       /* If the next token is now a `;', consume it.  */
2584       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2585         cp_lexer_consume_token (parser->lexer);
2586     }
2587 }
2588
2589 /* Skip tokens until we have consumed an entire block, or until we
2590    have consumed a non-nested `;'.  */
2591
2592 static void
2593 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2594 {
2595   int nesting_depth = 0;
2596
2597   while (nesting_depth >= 0)
2598     {
2599       cp_token *token = cp_lexer_peek_token (parser->lexer);
2600
2601       switch (token->type)
2602         {
2603         case CPP_EOF:
2604         case CPP_PRAGMA_EOL:
2605           /* If we've run out of tokens, stop.  */
2606           return;
2607
2608         case CPP_SEMICOLON:
2609           /* Stop if this is an unnested ';'. */
2610           if (!nesting_depth)
2611             nesting_depth = -1;
2612           break;
2613
2614         case CPP_CLOSE_BRACE:
2615           /* Stop if this is an unnested '}', or closes the outermost
2616              nesting level.  */
2617           nesting_depth--;
2618           if (!nesting_depth)
2619             nesting_depth = -1;
2620           break;
2621
2622         case CPP_OPEN_BRACE:
2623           /* Nest. */
2624           nesting_depth++;
2625           break;
2626
2627         default:
2628           break;
2629         }
2630
2631       /* Consume the token.  */
2632       cp_lexer_consume_token (parser->lexer);
2633     }
2634 }
2635
2636 /* Skip tokens until a non-nested closing curly brace is the next
2637    token, or there are no more tokens. Return true in the first case,
2638    false otherwise.  */
2639
2640 static bool
2641 cp_parser_skip_to_closing_brace (cp_parser *parser)
2642 {
2643   unsigned nesting_depth = 0;
2644
2645   while (true)
2646     {
2647       cp_token *token = cp_lexer_peek_token (parser->lexer);
2648
2649       switch (token->type)
2650         {
2651         case CPP_EOF:
2652         case CPP_PRAGMA_EOL:
2653           /* If we've run out of tokens, stop.  */
2654           return false;
2655
2656         case CPP_CLOSE_BRACE:
2657           /* If the next token is a non-nested `}', then we have reached
2658              the end of the current block.  */
2659           if (nesting_depth-- == 0)
2660             return true;
2661           break;
2662
2663         case CPP_OPEN_BRACE:
2664           /* If it the next token is a `{', then we are entering a new
2665              block.  Consume the entire block.  */
2666           ++nesting_depth;
2667           break;
2668
2669         default:
2670           break;
2671         }
2672
2673       /* Consume the token.  */
2674       cp_lexer_consume_token (parser->lexer);
2675     }
2676 }
2677
2678 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2679    parameter is the PRAGMA token, allowing us to purge the entire pragma
2680    sequence.  */
2681
2682 static void
2683 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2684 {
2685   cp_token *token;
2686
2687   parser->lexer->in_pragma = false;
2688
2689   do
2690     token = cp_lexer_consume_token (parser->lexer);
2691   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2692
2693   /* Ensure that the pragma is not parsed again.  */
2694   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2695 }
2696
2697 /* Require pragma end of line, resyncing with it as necessary.  The
2698    arguments are as for cp_parser_skip_to_pragma_eol.  */
2699
2700 static void
2701 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2702 {
2703   parser->lexer->in_pragma = false;
2704   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2705     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2706 }
2707
2708 /* This is a simple wrapper around make_typename_type. When the id is
2709    an unresolved identifier node, we can provide a superior diagnostic
2710    using cp_parser_diagnose_invalid_type_name.  */
2711
2712 static tree
2713 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2714                               tree id, location_t id_location)
2715 {
2716   tree result;
2717   if (TREE_CODE (id) == IDENTIFIER_NODE)
2718     {
2719       result = make_typename_type (scope, id, typename_type,
2720                                    /*complain=*/tf_none);
2721       if (result == error_mark_node)
2722         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2723       return result;
2724     }
2725   return make_typename_type (scope, id, typename_type, tf_error);
2726 }
2727
2728 /* This is a wrapper around the
2729    make_{pointer,ptrmem,reference}_declarator functions that decides
2730    which one to call based on the CODE and CLASS_TYPE arguments. The
2731    CODE argument should be one of the values returned by
2732    cp_parser_ptr_operator. */
2733 static cp_declarator *
2734 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2735                                     cp_cv_quals cv_qualifiers,
2736                                     cp_declarator *target)
2737 {
2738   if (code == ERROR_MARK)
2739     return cp_error_declarator;
2740
2741   if (code == INDIRECT_REF)
2742     if (class_type == NULL_TREE)
2743       return make_pointer_declarator (cv_qualifiers, target);
2744     else
2745       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2746   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2747     return make_reference_declarator (cv_qualifiers, target, false);
2748   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2749     return make_reference_declarator (cv_qualifiers, target, true);
2750   gcc_unreachable ();
2751 }
2752
2753 /* Create a new C++ parser.  */
2754
2755 static cp_parser *
2756 cp_parser_new (void)
2757 {
2758   cp_parser *parser;
2759   cp_lexer *lexer;
2760   unsigned i;
2761
2762   /* cp_lexer_new_main is called before calling ggc_alloc because
2763      cp_lexer_new_main might load a PCH file.  */
2764   lexer = cp_lexer_new_main ();
2765
2766   /* Initialize the binops_by_token so that we can get the tree
2767      directly from the token.  */
2768   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2769     binops_by_token[binops[i].token_type] = binops[i];
2770
2771   parser = GGC_CNEW (cp_parser);
2772   parser->lexer = lexer;
2773   parser->context = cp_parser_context_new (NULL);
2774
2775   /* For now, we always accept GNU extensions.  */
2776   parser->allow_gnu_extensions_p = 1;
2777
2778   /* The `>' token is a greater-than operator, not the end of a
2779      template-id.  */
2780   parser->greater_than_is_operator_p = true;
2781
2782   parser->default_arg_ok_p = true;
2783
2784   /* We are not parsing a constant-expression.  */
2785   parser->integral_constant_expression_p = false;
2786   parser->allow_non_integral_constant_expression_p = false;
2787   parser->non_integral_constant_expression_p = false;
2788
2789   /* Local variable names are not forbidden.  */
2790   parser->local_variables_forbidden_p = false;
2791
2792   /* We are not processing an `extern "C"' declaration.  */
2793   parser->in_unbraced_linkage_specification_p = false;
2794
2795   /* We are not processing a declarator.  */
2796   parser->in_declarator_p = false;
2797
2798   /* We are not processing a template-argument-list.  */
2799   parser->in_template_argument_list_p = false;
2800
2801   /* We are not in an iteration statement.  */
2802   parser->in_statement = 0;
2803
2804   /* We are not in a switch statement.  */
2805   parser->in_switch_statement_p = false;
2806
2807   /* We are not parsing a type-id inside an expression.  */
2808   parser->in_type_id_in_expr_p = false;
2809
2810   /* Declarations aren't implicitly extern "C".  */
2811   parser->implicit_extern_c = false;
2812
2813   /* String literals should be translated to the execution character set.  */
2814   parser->translate_strings_p = true;
2815
2816   /* We are not parsing a function body.  */
2817   parser->in_function_body = false;
2818
2819   /* The unparsed function queue is empty.  */
2820   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2821
2822   /* There are no classes being defined.  */
2823   parser->num_classes_being_defined = 0;
2824
2825   /* No template parameters apply.  */
2826   parser->num_template_parameter_lists = 0;
2827
2828   return parser;
2829 }
2830
2831 /* Create a cp_lexer structure which will emit the tokens in CACHE
2832    and push it onto the parser's lexer stack.  This is used for delayed
2833    parsing of in-class method bodies and default arguments, and should
2834    not be confused with tentative parsing.  */
2835 static void
2836 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2837 {
2838   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2839   lexer->next = parser->lexer;
2840   parser->lexer = lexer;
2841
2842   /* Move the current source position to that of the first token in the
2843      new lexer.  */
2844   cp_lexer_set_source_position_from_token (lexer->next_token);
2845 }
2846
2847 /* Pop the top lexer off the parser stack.  This is never used for the
2848    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2849 static void
2850 cp_parser_pop_lexer (cp_parser *parser)
2851 {
2852   cp_lexer *lexer = parser->lexer;
2853   parser->lexer = lexer->next;
2854   cp_lexer_destroy (lexer);
2855
2856   /* Put the current source position back where it was before this
2857      lexer was pushed.  */
2858   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2859 }
2860
2861 /* Lexical conventions [gram.lex]  */
2862
2863 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2864    identifier.  */
2865
2866 static tree
2867 cp_parser_identifier (cp_parser* parser)
2868 {
2869   cp_token *token;
2870
2871   /* Look for the identifier.  */
2872   token = cp_parser_require (parser, CPP_NAME, "identifier");
2873   /* Return the value.  */
2874   return token ? token->u.value : error_mark_node;
2875 }
2876
2877 /* Parse a sequence of adjacent string constants.  Returns a
2878    TREE_STRING representing the combined, nul-terminated string
2879    constant.  If TRANSLATE is true, translate the string to the
2880    execution character set.  If WIDE_OK is true, a wide string is
2881    invalid here.
2882
2883    C++98 [lex.string] says that if a narrow string literal token is
2884    adjacent to a wide string literal token, the behavior is undefined.
2885    However, C99 6.4.5p4 says that this results in a wide string literal.
2886    We follow C99 here, for consistency with the C front end.
2887
2888    This code is largely lifted from lex_string() in c-lex.c.
2889
2890    FUTURE: ObjC++ will need to handle @-strings here.  */
2891 static tree
2892 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2893 {
2894   tree value;
2895   size_t count;
2896   struct obstack str_ob;
2897   cpp_string str, istr, *strs;
2898   cp_token *tok;
2899   enum cpp_ttype type;
2900
2901   tok = cp_lexer_peek_token (parser->lexer);
2902   if (!cp_parser_is_string_literal (tok))
2903     {
2904       cp_parser_error (parser, "expected string-literal");
2905       return error_mark_node;
2906     }
2907
2908   type = tok->type;
2909
2910   /* Try to avoid the overhead of creating and destroying an obstack
2911      for the common case of just one string.  */
2912   if (!cp_parser_is_string_literal
2913       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2914     {
2915       cp_lexer_consume_token (parser->lexer);
2916
2917       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2918       str.len = TREE_STRING_LENGTH (tok->u.value);
2919       count = 1;
2920
2921       strs = &str;
2922     }
2923   else
2924     {
2925       gcc_obstack_init (&str_ob);
2926       count = 0;
2927
2928       do
2929         {
2930           cp_lexer_consume_token (parser->lexer);
2931           count++;
2932           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2933           str.len = TREE_STRING_LENGTH (tok->u.value);
2934
2935           if (type != tok->type)
2936             {
2937               if (type == CPP_STRING)
2938                 type = tok->type;
2939               else if (tok->type != CPP_STRING)
2940                 error ("%Hunsupported non-standard concatenation "
2941                        "of string literals", &tok->location);
2942             }
2943
2944           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2945
2946           tok = cp_lexer_peek_token (parser->lexer);
2947         }
2948       while (cp_parser_is_string_literal (tok));
2949
2950       strs = (cpp_string *) obstack_finish (&str_ob);
2951     }
2952
2953   if (type != CPP_STRING && !wide_ok)
2954     {
2955       cp_parser_error (parser, "a wide string is invalid in this context");
2956       type = CPP_STRING;
2957     }
2958
2959   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2960       (parse_in, strs, count, &istr, type))
2961     {
2962       value = build_string (istr.len, (const char *)istr.text);
2963       free (CONST_CAST (unsigned char *, istr.text));
2964
2965       switch (type)
2966         {
2967         default:
2968         case CPP_STRING:
2969           TREE_TYPE (value) = char_array_type_node;
2970           break;
2971         case CPP_STRING16:
2972           TREE_TYPE (value) = char16_array_type_node;
2973           break;
2974         case CPP_STRING32:
2975           TREE_TYPE (value) = char32_array_type_node;
2976           break;
2977         case CPP_WSTRING:
2978           TREE_TYPE (value) = wchar_array_type_node;
2979           break;
2980         }
2981
2982       value = fix_string_type (value);
2983     }
2984   else
2985     /* cpp_interpret_string has issued an error.  */
2986     value = error_mark_node;
2987
2988   if (count > 1)
2989     obstack_free (&str_ob, 0);
2990
2991   return value;
2992 }
2993
2994
2995 /* Basic concepts [gram.basic]  */
2996
2997 /* Parse a translation-unit.
2998
2999    translation-unit:
3000      declaration-seq [opt]
3001
3002    Returns TRUE if all went well.  */
3003
3004 static bool
3005 cp_parser_translation_unit (cp_parser* parser)
3006 {
3007   /* The address of the first non-permanent object on the declarator
3008      obstack.  */
3009   static void *declarator_obstack_base;
3010
3011   bool success;
3012
3013   /* Create the declarator obstack, if necessary.  */
3014   if (!cp_error_declarator)
3015     {
3016       gcc_obstack_init (&declarator_obstack);
3017       /* Create the error declarator.  */
3018       cp_error_declarator = make_declarator (cdk_error);
3019       /* Create the empty parameter list.  */
3020       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3021       /* Remember where the base of the declarator obstack lies.  */
3022       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3023     }
3024
3025   cp_parser_declaration_seq_opt (parser);
3026
3027   /* If there are no tokens left then all went well.  */
3028   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3029     {
3030       /* Get rid of the token array; we don't need it any more.  */
3031       cp_lexer_destroy (parser->lexer);
3032       parser->lexer = NULL;
3033
3034       /* This file might have been a context that's implicitly extern
3035          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3036       if (parser->implicit_extern_c)
3037         {
3038           pop_lang_context ();
3039           parser->implicit_extern_c = false;
3040         }
3041
3042       /* Finish up.  */
3043       finish_translation_unit ();
3044
3045       success = true;
3046     }
3047   else
3048     {
3049       cp_parser_error (parser, "expected declaration");
3050       success = false;
3051     }
3052
3053   /* Make sure the declarator obstack was fully cleaned up.  */
3054   gcc_assert (obstack_next_free (&declarator_obstack)
3055               == declarator_obstack_base);
3056
3057   /* All went well.  */
3058   return success;
3059 }
3060
3061 /* Expressions [gram.expr] */
3062
3063 /* Parse a primary-expression.
3064
3065    primary-expression:
3066      literal
3067      this
3068      ( expression )
3069      id-expression
3070
3071    GNU Extensions:
3072
3073    primary-expression:
3074      ( compound-statement )
3075      __builtin_va_arg ( assignment-expression , type-id )
3076      __builtin_offsetof ( type-id , offsetof-expression )
3077
3078    C++ Extensions:
3079      __has_nothrow_assign ( type-id )   
3080      __has_nothrow_constructor ( type-id )
3081      __has_nothrow_copy ( type-id )
3082      __has_trivial_assign ( type-id )   
3083      __has_trivial_constructor ( type-id )
3084      __has_trivial_copy ( type-id )
3085      __has_trivial_destructor ( type-id )
3086      __has_virtual_destructor ( type-id )     
3087      __is_abstract ( type-id )
3088      __is_base_of ( type-id , type-id )
3089      __is_class ( type-id )
3090      __is_convertible_to ( type-id , type-id )     
3091      __is_empty ( type-id )
3092      __is_enum ( type-id )
3093      __is_pod ( type-id )
3094      __is_polymorphic ( type-id )
3095      __is_union ( type-id )
3096
3097    Objective-C++ Extension:
3098
3099    primary-expression:
3100      objc-expression
3101
3102    literal:
3103      __null
3104
3105    ADDRESS_P is true iff this expression was immediately preceded by
3106    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3107    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3108    true iff this expression is a template argument.
3109
3110    Returns a representation of the expression.  Upon return, *IDK
3111    indicates what kind of id-expression (if any) was present.  */
3112
3113 static tree
3114 cp_parser_primary_expression (cp_parser *parser,
3115                               bool address_p,
3116                               bool cast_p,
3117                               bool template_arg_p,
3118                               cp_id_kind *idk)
3119 {
3120   cp_token *token = NULL;
3121
3122   /* Assume the primary expression is not an id-expression.  */
3123   *idk = CP_ID_KIND_NONE;
3124
3125   /* Peek at the next token.  */
3126   token = cp_lexer_peek_token (parser->lexer);
3127   switch (token->type)
3128     {
3129       /* literal:
3130            integer-literal
3131            character-literal
3132            floating-literal
3133            string-literal
3134            boolean-literal  */
3135     case CPP_CHAR:
3136     case CPP_CHAR16:
3137     case CPP_CHAR32:
3138     case CPP_WCHAR:
3139     case CPP_NUMBER:
3140       token = cp_lexer_consume_token (parser->lexer);
3141       /* Floating-point literals are only allowed in an integral
3142          constant expression if they are cast to an integral or
3143          enumeration type.  */
3144       if (TREE_CODE (token->u.value) == REAL_CST
3145           && parser->integral_constant_expression_p
3146           && pedantic)
3147         {
3148           /* CAST_P will be set even in invalid code like "int(2.7 +
3149              ...)".   Therefore, we have to check that the next token
3150              is sure to end the cast.  */
3151           if (cast_p)
3152             {
3153               cp_token *next_token;
3154
3155               next_token = cp_lexer_peek_token (parser->lexer);
3156               if (/* The comma at the end of an
3157                      enumerator-definition.  */
3158                   next_token->type != CPP_COMMA
3159                   /* The curly brace at the end of an enum-specifier.  */
3160                   && next_token->type != CPP_CLOSE_BRACE
3161                   /* The end of a statement.  */
3162                   && next_token->type != CPP_SEMICOLON
3163                   /* The end of the cast-expression.  */
3164                   && next_token->type != CPP_CLOSE_PAREN
3165                   /* The end of an array bound.  */
3166                   && next_token->type != CPP_CLOSE_SQUARE
3167                   /* The closing ">" in a template-argument-list.  */
3168                   && (next_token->type != CPP_GREATER
3169                       || parser->greater_than_is_operator_p)
3170                   /* C++0x only: A ">>" treated like two ">" tokens,
3171                      in a template-argument-list.  */
3172                   && (next_token->type != CPP_RSHIFT
3173                       || (cxx_dialect == cxx98)
3174                       || parser->greater_than_is_operator_p))
3175                 cast_p = false;
3176             }
3177
3178           /* If we are within a cast, then the constraint that the
3179              cast is to an integral or enumeration type will be
3180              checked at that point.  If we are not within a cast, then
3181              this code is invalid.  */
3182           if (!cast_p)
3183             cp_parser_non_integral_constant_expression
3184               (parser, "floating-point literal");
3185         }
3186       return token->u.value;
3187
3188     case CPP_STRING:
3189     case CPP_STRING16:
3190     case CPP_STRING32:
3191     case CPP_WSTRING:
3192       /* ??? Should wide strings be allowed when parser->translate_strings_p
3193          is false (i.e. in attributes)?  If not, we can kill the third
3194          argument to cp_parser_string_literal.  */
3195       return cp_parser_string_literal (parser,
3196                                        parser->translate_strings_p,
3197                                        true);
3198
3199     case CPP_OPEN_PAREN:
3200       {
3201         tree expr;
3202         bool saved_greater_than_is_operator_p;
3203
3204         /* Consume the `('.  */
3205         cp_lexer_consume_token (parser->lexer);
3206         /* Within a parenthesized expression, a `>' token is always
3207            the greater-than operator.  */
3208         saved_greater_than_is_operator_p
3209           = parser->greater_than_is_operator_p;
3210         parser->greater_than_is_operator_p = true;
3211         /* If we see `( { ' then we are looking at the beginning of
3212            a GNU statement-expression.  */
3213         if (cp_parser_allow_gnu_extensions_p (parser)
3214             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3215           {
3216             /* Statement-expressions are not allowed by the standard.  */
3217             if (pedantic)
3218               pedwarn ("%HISO C++ forbids braced-groups within expressions",
3219                        &token->location);
3220
3221             /* And they're not allowed outside of a function-body; you
3222                cannot, for example, write:
3223
3224                  int i = ({ int j = 3; j + 1; });
3225
3226                at class or namespace scope.  */
3227             if (!parser->in_function_body
3228                 || parser->in_template_argument_list_p)
3229               {
3230                 error ("%Hstatement-expressions are not allowed outside "
3231                        "functions nor in template-argument lists",
3232                        &token->location);
3233                 cp_parser_skip_to_end_of_block_or_statement (parser);
3234                 expr = error_mark_node;
3235               }
3236             else
3237               {
3238                 /* Start the statement-expression.  */
3239                 expr = begin_stmt_expr ();
3240                 /* Parse the compound-statement.  */
3241                 cp_parser_compound_statement (parser, expr, false);
3242                 /* Finish up.  */
3243                 expr = finish_stmt_expr (expr, false);
3244               }
3245           }
3246         else
3247           {
3248             /* Parse the parenthesized expression.  */
3249             expr = cp_parser_expression (parser, cast_p);
3250             /* Let the front end know that this expression was
3251                enclosed in parentheses. This matters in case, for
3252                example, the expression is of the form `A::B', since
3253                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3254                not.  */
3255             finish_parenthesized_expr (expr);
3256           }
3257         /* The `>' token might be the end of a template-id or
3258            template-parameter-list now.  */
3259         parser->greater_than_is_operator_p
3260           = saved_greater_than_is_operator_p;
3261         /* Consume the `)'.  */
3262         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3263           cp_parser_skip_to_end_of_statement (parser);
3264
3265         return expr;
3266       }
3267
3268     case CPP_KEYWORD:
3269       switch (token->keyword)
3270         {
3271           /* These two are the boolean literals.  */
3272         case RID_TRUE:
3273           cp_lexer_consume_token (parser->lexer);
3274           return boolean_true_node;
3275         case RID_FALSE:
3276           cp_lexer_consume_token (parser->lexer);
3277           return boolean_false_node;
3278
3279           /* The `__null' literal.  */
3280         case RID_NULL:
3281           cp_lexer_consume_token (parser->lexer);
3282           return null_node;
3283
3284           /* Recognize the `this' keyword.  */
3285         case RID_THIS:
3286           cp_lexer_consume_token (parser->lexer);
3287           if (parser->local_variables_forbidden_p)
3288             {
3289               error ("%H%<this%> may not be used in this context",
3290                      &token->location);
3291               return error_mark_node;
3292             }
3293           /* Pointers cannot appear in constant-expressions.  */
3294           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3295             return error_mark_node;
3296           return finish_this_expr ();
3297
3298           /* The `operator' keyword can be the beginning of an
3299              id-expression.  */
3300         case RID_OPERATOR:
3301           goto id_expression;
3302
3303         case RID_FUNCTION_NAME:
3304         case RID_PRETTY_FUNCTION_NAME:
3305         case RID_C99_FUNCTION_NAME:
3306           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3307              __func__ are the names of variables -- but they are
3308              treated specially.  Therefore, they are handled here,
3309              rather than relying on the generic id-expression logic
3310              below.  Grammatically, these names are id-expressions.
3311
3312              Consume the token.  */
3313           token = cp_lexer_consume_token (parser->lexer);
3314           /* Look up the name.  */
3315           return finish_fname (token->u.value);
3316
3317         case RID_VA_ARG:
3318           {
3319             tree expression;
3320             tree type;
3321
3322             /* The `__builtin_va_arg' construct is used to handle
3323                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3324             cp_lexer_consume_token (parser->lexer);
3325             /* Look for the opening `('.  */
3326             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3327             /* Now, parse the assignment-expression.  */
3328             expression = cp_parser_assignment_expression (parser,
3329                                                           /*cast_p=*/false);
3330             /* Look for the `,'.  */
3331             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3332             /* Parse the type-id.  */
3333             type = cp_parser_type_id (parser);
3334             /* Look for the closing `)'.  */
3335             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3336             /* Using `va_arg' in a constant-expression is not
3337                allowed.  */
3338             if (cp_parser_non_integral_constant_expression (parser,
3339                                                             "%<va_arg%>"))
3340               return error_mark_node;
3341             return build_x_va_arg (expression, type);
3342           }
3343
3344         case RID_OFFSETOF:
3345           return cp_parser_builtin_offsetof (parser);
3346
3347         case RID_HAS_NOTHROW_ASSIGN:
3348         case RID_HAS_NOTHROW_CONSTRUCTOR:
3349         case RID_HAS_NOTHROW_COPY:        
3350         case RID_HAS_TRIVIAL_ASSIGN:
3351         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3352         case RID_HAS_TRIVIAL_COPY:        
3353         case RID_HAS_TRIVIAL_DESTRUCTOR:
3354         case RID_HAS_VIRTUAL_DESTRUCTOR:
3355         case RID_IS_ABSTRACT:
3356         case RID_IS_BASE_OF:
3357         case RID_IS_CLASS:
3358         case RID_IS_CONVERTIBLE_TO:
3359         case RID_IS_EMPTY:
3360         case RID_IS_ENUM:
3361         case RID_IS_POD:
3362         case RID_IS_POLYMORPHIC:
3363         case RID_IS_UNION:
3364           return cp_parser_trait_expr (parser, token->keyword);
3365
3366         /* Objective-C++ expressions.  */
3367         case RID_AT_ENCODE:
3368         case RID_AT_PROTOCOL:
3369         case RID_AT_SELECTOR:
3370           return cp_parser_objc_expression (parser);
3371
3372         default:
3373           cp_parser_error (parser, "expected primary-expression");
3374           return error_mark_node;
3375         }
3376
3377       /* An id-expression can start with either an identifier, a
3378          `::' as the beginning of a qualified-id, or the "operator"
3379          keyword.  */
3380     case CPP_NAME:
3381     case CPP_SCOPE:
3382     case CPP_TEMPLATE_ID:
3383     case CPP_NESTED_NAME_SPECIFIER:
3384       {
3385         tree id_expression;
3386         tree decl;
3387         const char *error_msg;
3388         bool template_p;
3389         bool done;
3390         cp_token *id_expr_token;
3391
3392       id_expression:
3393         /* Parse the id-expression.  */
3394         id_expression
3395           = cp_parser_id_expression (parser,
3396                                      /*template_keyword_p=*/false,
3397                                      /*check_dependency_p=*/true,
3398                                      &template_p,
3399                                      /*declarator_p=*/false,
3400                                      /*optional_p=*/false);
3401         if (id_expression == error_mark_node)
3402           return error_mark_node;
3403         id_expr_token = token;
3404         token = cp_lexer_peek_token (parser->lexer);
3405         done = (token->type != CPP_OPEN_SQUARE
3406                 && token->type != CPP_OPEN_PAREN
3407                 && token->type != CPP_DOT
3408                 && token->type != CPP_DEREF
3409                 && token->type != CPP_PLUS_PLUS
3410                 && token->type != CPP_MINUS_MINUS);
3411         /* If we have a template-id, then no further lookup is
3412            required.  If the template-id was for a template-class, we
3413            will sometimes have a TYPE_DECL at this point.  */
3414         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3415                  || TREE_CODE (id_expression) == TYPE_DECL)
3416           decl = id_expression;
3417         /* Look up the name.  */
3418         else
3419           {
3420             tree ambiguous_decls;
3421
3422             decl = cp_parser_lookup_name (parser, id_expression,
3423                                           none_type,
3424                                           template_p,
3425                                           /*is_namespace=*/false,
3426                                           /*check_dependency=*/true,
3427                                           &ambiguous_decls,
3428                                           id_expr_token->location);
3429             /* If the lookup was ambiguous, an error will already have
3430                been issued.  */
3431             if (ambiguous_decls)
3432               return error_mark_node;
3433
3434             /* In Objective-C++, an instance variable (ivar) may be preferred
3435                to whatever cp_parser_lookup_name() found.  */
3436             decl = objc_lookup_ivar (decl, id_expression);
3437
3438             /* If name lookup gives us a SCOPE_REF, then the
3439                qualifying scope was dependent.  */
3440             if (TREE_CODE (decl) == SCOPE_REF)
3441               {
3442                 /* At this point, we do not know if DECL is a valid
3443                    integral constant expression.  We assume that it is
3444                    in fact such an expression, so that code like:
3445
3446                       template <int N> struct A {
3447                         int a[B<N>::i];
3448                       };
3449                      
3450                    is accepted.  At template-instantiation time, we
3451                    will check that B<N>::i is actually a constant.  */
3452                 return decl;
3453               }
3454             /* Check to see if DECL is a local variable in a context
3455                where that is forbidden.  */
3456             if (parser->local_variables_forbidden_p
3457                 && local_variable_p (decl))
3458               {
3459                 /* It might be that we only found DECL because we are
3460                    trying to be generous with pre-ISO scoping rules.
3461                    For example, consider:
3462
3463                      int i;
3464                      void g() {
3465                        for (int i = 0; i < 10; ++i) {}
3466                        extern void f(int j = i);
3467                      }
3468
3469                    Here, name look up will originally find the out
3470                    of scope `i'.  We need to issue a warning message,
3471                    but then use the global `i'.  */
3472                 decl = check_for_out_of_scope_variable (decl);
3473                 if (local_variable_p (decl))
3474                   {
3475                     error ("%Hlocal variable %qD may not appear in this context",
3476                            &id_expr_token->location, decl);
3477                     return error_mark_node;
3478                   }
3479               }
3480           }
3481
3482         decl = (finish_id_expression
3483                 (id_expression, decl, parser->scope,
3484                  idk,
3485                  parser->integral_constant_expression_p,
3486                  parser->allow_non_integral_constant_expression_p,
3487                  &parser->non_integral_constant_expression_p,
3488                  template_p, done, address_p,
3489                  template_arg_p,
3490                  &error_msg,
3491                  id_expr_token->location));
3492         if (error_msg)
3493           cp_parser_error (parser, error_msg);
3494         return decl;
3495       }
3496
3497       /* Anything else is an error.  */
3498     default:
3499       /* ...unless we have an Objective-C++ message or string literal,
3500          that is.  */
3501       if (c_dialect_objc ()
3502           && (token->type == CPP_OPEN_SQUARE
3503               || token->type == CPP_OBJC_STRING))
3504         return cp_parser_objc_expression (parser);
3505
3506       cp_parser_error (parser, "expected primary-expression");
3507       return error_mark_node;
3508     }
3509 }
3510
3511 /* Parse an id-expression.
3512
3513    id-expression:
3514      unqualified-id
3515      qualified-id
3516
3517    qualified-id:
3518      :: [opt] nested-name-specifier template [opt] unqualified-id
3519      :: identifier
3520      :: operator-function-id
3521      :: template-id
3522
3523    Return a representation of the unqualified portion of the
3524    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3525    a `::' or nested-name-specifier.
3526
3527    Often, if the id-expression was a qualified-id, the caller will
3528    want to make a SCOPE_REF to represent the qualified-id.  This
3529    function does not do this in order to avoid wastefully creating
3530    SCOPE_REFs when they are not required.
3531
3532    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3533    `template' keyword.
3534
3535    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3536    uninstantiated templates.
3537
3538    If *TEMPLATE_P is non-NULL, it is set to true iff the
3539    `template' keyword is used to explicitly indicate that the entity
3540    named is a template.
3541
3542    If DECLARATOR_P is true, the id-expression is appearing as part of
3543    a declarator, rather than as part of an expression.  */
3544
3545 static tree
3546 cp_parser_id_expression (cp_parser *parser,
3547                          bool template_keyword_p,
3548                          bool check_dependency_p,
3549                          bool *template_p,
3550                          bool declarator_p,
3551                          bool optional_p)
3552 {
3553   bool global_scope_p;
3554   bool nested_name_specifier_p;
3555
3556   /* Assume the `template' keyword was not used.  */
3557   if (template_p)
3558     *template_p = template_keyword_p;
3559
3560   /* Look for the optional `::' operator.  */
3561   global_scope_p
3562     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3563        != NULL_TREE);
3564   /* Look for the optional nested-name-specifier.  */
3565   nested_name_specifier_p
3566     = (cp_parser_nested_name_specifier_opt (parser,
3567                                             /*typename_keyword_p=*/false,
3568                                             check_dependency_p,
3569                                             /*type_p=*/false,
3570                                             declarator_p)
3571        != NULL_TREE);
3572   /* If there is a nested-name-specifier, then we are looking at
3573      the first qualified-id production.  */
3574   if (nested_name_specifier_p)
3575     {
3576       tree saved_scope;
3577       tree saved_object_scope;
3578       tree saved_qualifying_scope;
3579       tree unqualified_id;
3580       bool is_template;
3581
3582       /* See if the next token is the `template' keyword.  */
3583       if (!template_p)
3584         template_p = &is_template;
3585       *template_p = cp_parser_optional_template_keyword (parser);
3586       /* Name lookup we do during the processing of the
3587          unqualified-id might obliterate SCOPE.  */
3588       saved_scope = parser->scope;
3589       saved_object_scope = parser->object_scope;
3590       saved_qualifying_scope = parser->qualifying_scope;
3591       /* Process the final unqualified-id.  */
3592       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3593                                                  check_dependency_p,
3594                                                  declarator_p,
3595                                                  /*optional_p=*/false);
3596       /* Restore the SAVED_SCOPE for our caller.  */
3597       parser->scope = saved_scope;
3598       parser->object_scope = saved_object_scope;
3599       parser->qualifying_scope = saved_qualifying_scope;
3600
3601       return unqualified_id;
3602     }
3603   /* Otherwise, if we are in global scope, then we are looking at one
3604      of the other qualified-id productions.  */
3605   else if (global_scope_p)
3606     {
3607       cp_token *token;
3608       tree id;
3609
3610       /* Peek at the next token.  */
3611       token = cp_lexer_peek_token (parser->lexer);
3612
3613       /* If it's an identifier, and the next token is not a "<", then
3614          we can avoid the template-id case.  This is an optimization
3615          for this common case.  */
3616       if (token->type == CPP_NAME
3617           && !cp_parser_nth_token_starts_template_argument_list_p
3618                (parser, 2))
3619         return cp_parser_identifier (parser);
3620
3621       cp_parser_parse_tentatively (parser);
3622       /* Try a template-id.  */
3623       id = cp_parser_template_id (parser,
3624                                   /*template_keyword_p=*/false,
3625                                   /*check_dependency_p=*/true,
3626                                   declarator_p);
3627       /* If that worked, we're done.  */
3628       if (cp_parser_parse_definitely (parser))
3629         return id;
3630
3631       /* Peek at the next token.  (Changes in the token buffer may
3632          have invalidated the pointer obtained above.)  */
3633       token = cp_lexer_peek_token (parser->lexer);
3634
3635       switch (token->type)
3636         {
3637         case CPP_NAME:
3638           return cp_parser_identifier (parser);
3639
3640         case CPP_KEYWORD:
3641           if (token->keyword == RID_OPERATOR)
3642             return cp_parser_operator_function_id (parser);
3643           /* Fall through.  */
3644
3645         default:
3646           cp_parser_error (parser, "expected id-expression");
3647           return error_mark_node;
3648         }
3649     }
3650   else
3651     return cp_parser_unqualified_id (parser, template_keyword_p,
3652                                      /*check_dependency_p=*/true,
3653                                      declarator_p,
3654                                      optional_p);
3655 }
3656
3657 /* Parse an unqualified-id.
3658
3659    unqualified-id:
3660      identifier
3661      operator-function-id
3662      conversion-function-id
3663      ~ class-name
3664      template-id
3665
3666    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3667    keyword, in a construct like `A::template ...'.
3668
3669    Returns a representation of unqualified-id.  For the `identifier'
3670    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3671    production a BIT_NOT_EXPR is returned; the operand of the
3672    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3673    other productions, see the documentation accompanying the
3674    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3675    names are looked up in uninstantiated templates.  If DECLARATOR_P
3676    is true, the unqualified-id is appearing as part of a declarator,
3677    rather than as part of an expression.  */
3678
3679 static tree
3680 cp_parser_unqualified_id (cp_parser* parser,
3681                           bool template_keyword_p,
3682                           bool check_dependency_p,
3683                           bool declarator_p,
3684                           bool optional_p)
3685 {
3686   cp_token *token;
3687
3688   /* Peek at the next token.  */
3689   token = cp_lexer_peek_token (parser->lexer);
3690
3691   switch (token->type)
3692     {
3693     case CPP_NAME:
3694       {
3695         tree id;
3696
3697         /* We don't know yet whether or not this will be a
3698            template-id.  */
3699         cp_parser_parse_tentatively (parser);
3700         /* Try a template-id.  */
3701         id = cp_parser_template_id (parser, template_keyword_p,
3702                                     check_dependency_p,
3703                                     declarator_p);
3704         /* If it worked, we're done.  */
3705         if (cp_parser_parse_definitely (parser))
3706           return id;
3707         /* Otherwise, it's an ordinary identifier.  */
3708         return cp_parser_identifier (parser);
3709       }
3710
3711     case CPP_TEMPLATE_ID:
3712       return cp_parser_template_id (parser, template_keyword_p,
3713                                     check_dependency_p,
3714                                     declarator_p);
3715
3716     case CPP_COMPL:
3717       {
3718         tree type_decl;
3719         tree qualifying_scope;
3720         tree object_scope;
3721         tree scope;
3722         bool done;
3723
3724         /* Consume the `~' token.  */
3725         cp_lexer_consume_token (parser->lexer);
3726         /* Parse the class-name.  The standard, as written, seems to
3727            say that:
3728
3729              template <typename T> struct S { ~S (); };
3730              template <typename T> S<T>::~S() {}
3731
3732            is invalid, since `~' must be followed by a class-name, but
3733            `S<T>' is dependent, and so not known to be a class.
3734            That's not right; we need to look in uninstantiated
3735            templates.  A further complication arises from:
3736
3737              template <typename T> void f(T t) {
3738                t.T::~T();
3739              }
3740
3741            Here, it is not possible to look up `T' in the scope of `T'
3742            itself.  We must look in both the current scope, and the
3743            scope of the containing complete expression.
3744
3745            Yet another issue is:
3746
3747              struct S {
3748                int S;
3749                ~S();
3750              };
3751
3752              S::~S() {}
3753
3754            The standard does not seem to say that the `S' in `~S'
3755            should refer to the type `S' and not the data member
3756            `S::S'.  */
3757
3758         /* DR 244 says that we look up the name after the "~" in the
3759            same scope as we looked up the qualifying name.  That idea
3760            isn't fully worked out; it's more complicated than that.  */
3761         scope = parser->scope;
3762         object_scope = parser->object_scope;
3763         qualifying_scope = parser->qualifying_scope;
3764
3765         /* Check for invalid scopes.  */
3766         if (scope == error_mark_node)
3767           {
3768             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3769               cp_lexer_consume_token (parser->lexer);
3770             return error_mark_node;
3771           }
3772         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3773           {
3774             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3775               error ("%Hscope %qT before %<~%> is not a class-name",
3776                      &token->location, scope);
3777             cp_parser_simulate_error (parser);
3778             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3779               cp_lexer_consume_token (parser->lexer);
3780             return error_mark_node;
3781           }
3782         gcc_assert (!scope || TYPE_P (scope));
3783
3784         /* If the name is of the form "X::~X" it's OK.  */
3785         token = cp_lexer_peek_token (parser->lexer);
3786         if (scope
3787             && token->type == CPP_NAME
3788             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3789                 == CPP_OPEN_PAREN)
3790             && constructor_name_p (token->u.value, scope))
3791           {
3792             cp_lexer_consume_token (parser->lexer);
3793             return build_nt (BIT_NOT_EXPR, scope);
3794           }
3795
3796         /* If there was an explicit qualification (S::~T), first look
3797            in the scope given by the qualification (i.e., S).  */
3798         done = false;
3799         type_decl = NULL_TREE;
3800         if (scope)
3801           {
3802             cp_parser_parse_tentatively (parser);
3803             type_decl = cp_parser_class_name (parser,
3804                                               /*typename_keyword_p=*/false,
3805                                               /*template_keyword_p=*/false,
3806                                               none_type,
3807                                               /*check_dependency=*/false,
3808                                               /*class_head_p=*/false,
3809                                               declarator_p);
3810             if (cp_parser_parse_definitely (parser))
3811               done = true;
3812           }
3813         /* In "N::S::~S", look in "N" as well.  */
3814         if (!done && scope && qualifying_scope)
3815           {
3816             cp_parser_parse_tentatively (parser);
3817             parser->scope = qualifying_scope;
3818             parser->object_scope = NULL_TREE;
3819             parser->qualifying_scope = NULL_TREE;
3820             type_decl
3821               = cp_parser_class_name (parser,
3822                                       /*typename_keyword_p=*/false,
3823                                       /*template_keyword_p=*/false,
3824                                       none_type,
3825                                       /*check_dependency=*/false,
3826                                       /*class_head_p=*/false,
3827                                       declarator_p);
3828             if (cp_parser_parse_definitely (parser))
3829               done = true;
3830           }
3831         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3832         else if (!done && object_scope)
3833           {
3834             cp_parser_parse_tentatively (parser);
3835             parser->scope = object_scope;
3836             parser->object_scope = NULL_TREE;
3837             parser->qualifying_scope = NULL_TREE;
3838             type_decl
3839               = cp_parser_class_name (parser,
3840                                       /*typename_keyword_p=*/false,
3841                                       /*template_keyword_p=*/false,
3842                                       none_type,
3843                                       /*check_dependency=*/false,
3844                                       /*class_head_p=*/false,
3845                                       declarator_p);
3846             if (cp_parser_parse_definitely (parser))
3847               done = true;
3848           }
3849         /* Look in the surrounding context.  */
3850         if (!done)
3851           {
3852             parser->scope = NULL_TREE;
3853             parser->object_scope = NULL_TREE;
3854             parser->qualifying_scope = NULL_TREE;
3855             type_decl
3856               = cp_parser_class_name (parser,
3857                                       /*typename_keyword_p=*/false,
3858                                       /*template_keyword_p=*/false,
3859                                       none_type,
3860                                       /*check_dependency=*/false,
3861                                       /*class_head_p=*/false,
3862                                       declarator_p);
3863           }
3864         /* If an error occurred, assume that the name of the
3865            destructor is the same as the name of the qualifying
3866            class.  That allows us to keep parsing after running
3867            into ill-formed destructor names.  */
3868         if (type_decl == error_mark_node && scope)
3869           return build_nt (BIT_NOT_EXPR, scope);
3870         else if (type_decl == error_mark_node)
3871           return error_mark_node;
3872
3873         /* Check that destructor name and scope match.  */
3874         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3875           {
3876             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3877               error ("%Hdeclaration of %<~%T%> as member of %qT",
3878                      &token->location, type_decl, scope);
3879             cp_parser_simulate_error (parser);
3880             return error_mark_node;
3881           }
3882
3883         /* [class.dtor]
3884
3885            A typedef-name that names a class shall not be used as the
3886            identifier in the declarator for a destructor declaration.  */
3887         if (declarator_p
3888             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3889             && !DECL_SELF_REFERENCE_P (type_decl)
3890             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3891           error ("%Htypedef-name %qD used as destructor declarator",
3892                  &token->location, type_decl);
3893
3894         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3895       }
3896
3897     case CPP_KEYWORD:
3898       if (token->keyword == RID_OPERATOR)
3899         {
3900           tree id;
3901
3902           /* This could be a template-id, so we try that first.  */
3903           cp_parser_parse_tentatively (parser);
3904           /* Try a template-id.  */
3905           id = cp_parser_template_id (parser, template_keyword_p,
3906                                       /*check_dependency_p=*/true,
3907                                       declarator_p);
3908           /* If that worked, we're done.  */
3909           if (cp_parser_parse_definitely (parser))
3910             return id;
3911           /* We still don't know whether we're looking at an
3912              operator-function-id or a conversion-function-id.  */
3913           cp_parser_parse_tentatively (parser);
3914           /* Try an operator-function-id.  */
3915           id = cp_parser_operator_function_id (parser);
3916           /* If that didn't work, try a conversion-function-id.  */
3917           if (!cp_parser_parse_definitely (parser))
3918             id = cp_parser_conversion_function_id (parser);
3919
3920           return id;
3921         }
3922       /* Fall through.  */
3923
3924     default:
3925       if (optional_p)
3926         return NULL_TREE;
3927       cp_parser_error (parser, "expected unqualified-id");
3928       return error_mark_node;
3929     }
3930 }
3931
3932 /* Parse an (optional) nested-name-specifier.
3933
3934    nested-name-specifier:
3935      class-or-namespace-name :: nested-name-specifier [opt]
3936      class-or-namespace-name :: template nested-name-specifier [opt]
3937
3938    PARSER->SCOPE should be set appropriately before this function is
3939    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3940    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3941    in name lookups.
3942
3943    Sets PARSER->SCOPE to the class (TYPE) or namespace
3944    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3945    it unchanged if there is no nested-name-specifier.  Returns the new
3946    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3947
3948    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3949    part of a declaration and/or decl-specifier.  */
3950
3951 static tree
3952 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3953                                      bool typename_keyword_p,
3954                                      bool check_dependency_p,
3955                                      bool type_p,
3956                                      bool is_declaration)
3957 {
3958   bool success = false;
3959   cp_token_position start = 0;
3960   cp_token *token;
3961
3962   /* Remember where the nested-name-specifier starts.  */
3963   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3964     {
3965       start = cp_lexer_token_position (parser->lexer, false);
3966       push_deferring_access_checks (dk_deferred);
3967     }
3968
3969   while (true)
3970     {
3971       tree new_scope;
3972       tree old_scope;
3973       tree saved_qualifying_scope;
3974       bool template_keyword_p;
3975
3976       /* Spot cases that cannot be the beginning of a
3977          nested-name-specifier.  */
3978       token = cp_lexer_peek_token (parser->lexer);
3979
3980       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3981          the already parsed nested-name-specifier.  */
3982       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3983         {
3984           /* Grab the nested-name-specifier and continue the loop.  */
3985           cp_parser_pre_parsed_nested_name_specifier (parser);
3986           /* If we originally encountered this nested-name-specifier
3987              with IS_DECLARATION set to false, we will not have
3988              resolved TYPENAME_TYPEs, so we must do so here.  */
3989           if (is_declaration
3990               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3991             {
3992               new_scope = resolve_typename_type (parser->scope,
3993                                                  /*only_current_p=*/false);
3994               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3995                 parser->scope = new_scope;
3996             }
3997           success = true;
3998           continue;
3999         }
4000
4001       /* Spot cases that cannot be the beginning of a
4002          nested-name-specifier.  On the second and subsequent times
4003          through the loop, we look for the `template' keyword.  */
4004       if (success && token->keyword == RID_TEMPLATE)
4005         ;
4006       /* A template-id can start a nested-name-specifier.  */
4007       else if (token->type == CPP_TEMPLATE_ID)
4008         ;
4009       else
4010         {
4011           /* If the next token is not an identifier, then it is
4012              definitely not a class-or-namespace-name.  */
4013           if (token->type != CPP_NAME)
4014             break;
4015           /* If the following token is neither a `<' (to begin a
4016              template-id), nor a `::', then we are not looking at a
4017              nested-name-specifier.  */
4018           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4019           if (token->type != CPP_SCOPE
4020               && !cp_parser_nth_token_starts_template_argument_list_p
4021                   (parser, 2))
4022             break;
4023         }
4024
4025       /* The nested-name-specifier is optional, so we parse
4026          tentatively.  */
4027       cp_parser_parse_tentatively (parser);
4028
4029       /* Look for the optional `template' keyword, if this isn't the
4030          first time through the loop.  */
4031       if (success)
4032         template_keyword_p = cp_parser_optional_template_keyword (parser);
4033       else
4034         template_keyword_p = false;
4035
4036       /* Save the old scope since the name lookup we are about to do
4037          might destroy it.  */
4038       old_scope = parser->scope;
4039       saved_qualifying_scope = parser->qualifying_scope;
4040       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4041          look up names in "X<T>::I" in order to determine that "Y" is
4042          a template.  So, if we have a typename at this point, we make
4043          an effort to look through it.  */
4044       if (is_declaration
4045           && !typename_keyword_p
4046           && parser->scope
4047           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4048         parser->scope = resolve_typename_type (parser->scope,
4049                                                /*only_current_p=*/false);
4050       /* Parse the qualifying entity.  */
4051       new_scope
4052         = cp_parser_class_or_namespace_name (parser,
4053                                              typename_keyword_p,
4054                                              template_keyword_p,
4055                                              check_dependency_p,
4056                                              type_p,
4057                                              is_declaration);
4058       /* Look for the `::' token.  */
4059       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4060
4061       /* If we found what we wanted, we keep going; otherwise, we're
4062          done.  */
4063       if (!cp_parser_parse_definitely (parser))
4064         {
4065           bool error_p = false;
4066
4067           /* Restore the OLD_SCOPE since it was valid before the
4068              failed attempt at finding the last
4069              class-or-namespace-name.  */
4070           parser->scope = old_scope;
4071           parser->qualifying_scope = saved_qualifying_scope;
4072           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4073             break;
4074           /* If the next token is an identifier, and the one after
4075              that is a `::', then any valid interpretation would have
4076              found a class-or-namespace-name.  */
4077           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4078                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4079                      == CPP_SCOPE)
4080                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4081                      != CPP_COMPL))
4082             {
4083               token = cp_lexer_consume_token (parser->lexer);
4084               if (!error_p)
4085                 {
4086                   if (!token->ambiguous_p)
4087                     {
4088                       tree decl;
4089                       tree ambiguous_decls;
4090
4091                       decl = cp_parser_lookup_name (parser, token->u.value,
4092                                                     none_type,
4093                                                     /*is_template=*/false,
4094                                                     /*is_namespace=*/false,
4095                                                     /*check_dependency=*/true,
4096                                                     &ambiguous_decls,
4097                                                     token->location);
4098                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4099                         error ("%H%qD used without template parameters",
4100                                &token->location, decl);
4101                       else if (ambiguous_decls)
4102                         {
4103                           error ("%Hreference to %qD is ambiguous",
4104                                  &token->location, token->u.value);
4105                           print_candidates (ambiguous_decls);
4106                           decl = error_mark_node;
4107                         }
4108                       else
4109                         cp_parser_name_lookup_error
4110                           (parser, token->u.value, decl,
4111                            "is not a class or namespace",
4112                            token->location);
4113                     }
4114                   parser->scope = error_mark_node;
4115                   error_p = true;
4116                   /* Treat this as a successful nested-name-specifier
4117                      due to:
4118
4119                      [basic.lookup.qual]
4120
4121                      If the name found is not a class-name (clause
4122                      _class_) or namespace-name (_namespace.def_), the
4123                      program is ill-formed.  */
4124                   success = true;
4125                 }
4126               cp_lexer_consume_token (parser->lexer);
4127             }
4128           break;
4129         }
4130       /* We've found one valid nested-name-specifier.  */
4131       success = true;
4132       /* Name lookup always gives us a DECL.  */
4133       if (TREE_CODE (new_scope) == TYPE_DECL)
4134         new_scope = TREE_TYPE (new_scope);
4135       /* Uses of "template" must be followed by actual templates.  */
4136       if (template_keyword_p
4137           && !(CLASS_TYPE_P (new_scope)
4138                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4139                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4140                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4141           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4142                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4143                    == TEMPLATE_ID_EXPR)))
4144         permerror (TYPE_P (new_scope)
4145                    ? "%qT is not a template"
4146                    : "%qD is not a template",
4147                    new_scope);
4148       /* If it is a class scope, try to complete it; we are about to
4149          be looking up names inside the class.  */
4150       if (TYPE_P (new_scope)
4151           /* Since checking types for dependency can be expensive,
4152              avoid doing it if the type is already complete.  */
4153           && !COMPLETE_TYPE_P (new_scope)
4154           /* Do not try to complete dependent types.  */
4155           && !dependent_type_p (new_scope))
4156         {
4157           new_scope = complete_type (new_scope);
4158           /* If it is a typedef to current class, use the current
4159              class instead, as the typedef won't have any names inside
4160              it yet.  */
4161           if (!COMPLETE_TYPE_P (new_scope)
4162               && currently_open_class (new_scope))
4163             new_scope = TYPE_MAIN_VARIANT (new_scope);
4164         }
4165       /* Make sure we look in the right scope the next time through
4166          the loop.  */
4167       parser->scope = new_scope;
4168     }
4169
4170   /* If parsing tentatively, replace the sequence of tokens that makes
4171      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4172      token.  That way, should we re-parse the token stream, we will
4173      not have to repeat the effort required to do the parse, nor will
4174      we issue duplicate error messages.  */
4175   if (success && start)
4176     {
4177       cp_token *token;
4178
4179       token = cp_lexer_token_at (parser->lexer, start);
4180       /* Reset the contents of the START token.  */
4181       token->type = CPP_NESTED_NAME_SPECIFIER;
4182       /* Retrieve any deferred checks.  Do not pop this access checks yet
4183          so the memory will not be reclaimed during token replacing below.  */
4184       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4185       token->u.tree_check_value->value = parser->scope;
4186       token->u.tree_check_value->checks = get_deferred_access_checks ();
4187       token->u.tree_check_value->qualifying_scope =
4188         parser->qualifying_scope;
4189       token->keyword = RID_MAX;
4190
4191       /* Purge all subsequent tokens.  */
4192       cp_lexer_purge_tokens_after (parser->lexer, start);
4193     }
4194
4195   if (start)
4196     pop_to_parent_deferring_access_checks ();
4197
4198   return success ? parser->scope : NULL_TREE;
4199 }
4200
4201 /* Parse a nested-name-specifier.  See
4202    cp_parser_nested_name_specifier_opt for details.  This function
4203    behaves identically, except that it will an issue an error if no
4204    nested-name-specifier is present.  */
4205
4206 static tree
4207 cp_parser_nested_name_specifier (cp_parser *parser,
4208                                  bool typename_keyword_p,
4209                                  bool check_dependency_p,
4210                                  bool type_p,
4211                                  bool is_declaration)
4212 {
4213   tree scope;
4214
4215   /* Look for the nested-name-specifier.  */
4216   scope = cp_parser_nested_name_specifier_opt (parser,
4217                                                typename_keyword_p,
4218                                                check_dependency_p,
4219                                                type_p,
4220                                                is_declaration);
4221   /* If it was not present, issue an error message.  */
4222   if (!scope)
4223     {
4224       cp_parser_error (parser, "expected nested-name-specifier");
4225       parser->scope = NULL_TREE;
4226     }
4227
4228   return scope;
4229 }
4230
4231 /* Parse a class-or-namespace-name.
4232
4233    class-or-namespace-name:
4234      class-name
4235      namespace-name
4236
4237    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4238    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4239    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4240    TYPE_P is TRUE iff the next name should be taken as a class-name,
4241    even the same name is declared to be another entity in the same
4242    scope.
4243
4244    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4245    specified by the class-or-namespace-name.  If neither is found the
4246    ERROR_MARK_NODE is returned.  */
4247
4248 static tree
4249 cp_parser_class_or_namespace_name (cp_parser *parser,
4250                                    bool typename_keyword_p,
4251                                    bool template_keyword_p,
4252                                    bool check_dependency_p,
4253                                    bool type_p,
4254                                    bool is_declaration)
4255 {
4256   tree saved_scope;
4257   tree saved_qualifying_scope;
4258   tree saved_object_scope;
4259   tree scope;
4260   bool only_class_p;
4261
4262   /* Before we try to parse the class-name, we must save away the
4263      current PARSER->SCOPE since cp_parser_class_name will destroy
4264      it.  */
4265   saved_scope = parser->scope;
4266   saved_qualifying_scope = parser->qualifying_scope;
4267   saved_object_scope = parser->object_scope;
4268   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4269      there is no need to look for a namespace-name.  */
4270   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4271   if (!only_class_p)
4272     cp_parser_parse_tentatively (parser);
4273   scope = cp_parser_class_name (parser,
4274                                 typename_keyword_p,
4275                                 template_keyword_p,
4276                                 type_p ? class_type : none_type,
4277                                 check_dependency_p,
4278                                 /*class_head_p=*/false,
4279                                 is_declaration);
4280   /* If that didn't work, try for a namespace-name.  */
4281   if (!only_class_p && !cp_parser_parse_definitely (parser))
4282     {
4283       /* Restore the saved scope.  */
4284       parser->scope = saved_scope;
4285       parser->qualifying_scope = saved_qualifying_scope;
4286       parser->object_scope = saved_object_scope;
4287       /* If we are not looking at an identifier followed by the scope
4288          resolution operator, then this is not part of a
4289          nested-name-specifier.  (Note that this function is only used
4290          to parse the components of a nested-name-specifier.)  */
4291       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4292           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4293         return error_mark_node;
4294       scope = cp_parser_namespace_name (parser);
4295     }
4296
4297   return scope;
4298 }
4299
4300 /* Parse a postfix-expression.
4301
4302    postfix-expression:
4303      primary-expression
4304      postfix-expression [ expression ]
4305      postfix-expression ( expression-list [opt] )
4306      simple-type-specifier ( expression-list [opt] )
4307      typename :: [opt] nested-name-specifier identifier
4308        ( expression-list [opt] )
4309      typename :: [opt] nested-name-specifier template [opt] template-id
4310        ( expression-list [opt] )
4311      postfix-expression . template [opt] id-expression
4312      postfix-expression -> template [opt] id-expression
4313      postfix-expression . pseudo-destructor-name
4314      postfix-expression -> pseudo-destructor-name
4315      postfix-expression ++
4316      postfix-expression --
4317      dynamic_cast < type-id > ( expression )
4318      static_cast < type-id > ( expression )
4319      reinterpret_cast < type-id > ( expression )
4320      const_cast < type-id > ( expression )
4321      typeid ( expression )
4322      typeid ( type-id )
4323
4324    GNU Extension:
4325
4326    postfix-expression:
4327      ( type-id ) { initializer-list , [opt] }
4328
4329    This extension is a GNU version of the C99 compound-literal
4330    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4331    but they are essentially the same concept.)
4332
4333    If ADDRESS_P is true, the postfix expression is the operand of the
4334    `&' operator.  CAST_P is true if this expression is the target of a
4335    cast.
4336
4337    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4338    class member access expressions [expr.ref].
4339
4340    Returns a representation of the expression.  */
4341
4342 static tree
4343 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4344                               bool member_access_only_p)
4345 {
4346   cp_token *token;
4347   enum rid keyword;
4348   cp_id_kind idk = CP_ID_KIND_NONE;
4349   tree postfix_expression = NULL_TREE;
4350   bool is_member_access = false;
4351
4352   /* Peek at the next token.  */
4353   token = cp_lexer_peek_token (parser->lexer);
4354   /* Some of the productions are determined by keywords.  */
4355   keyword = token->keyword;
4356   switch (keyword)
4357     {
4358     case RID_DYNCAST:
4359     case RID_STATCAST:
4360     case RID_REINTCAST:
4361     case RID_CONSTCAST:
4362       {
4363         tree type;
4364         tree expression;
4365         const char *saved_message;
4366
4367         /* All of these can be handled in the same way from the point
4368            of view of parsing.  Begin by consuming the token
4369            identifying the cast.  */
4370         cp_lexer_consume_token (parser->lexer);
4371
4372         /* New types cannot be defined in the cast.  */
4373         saved_message = parser->type_definition_forbidden_message;
4374         parser->type_definition_forbidden_message
4375           = "types may not be defined in casts";
4376
4377         /* Look for the opening `<'.  */
4378         cp_parser_require (parser, CPP_LESS, "%<<%>");
4379         /* Parse the type to which we are casting.  */
4380         type = cp_parser_type_id (parser);
4381         /* Look for the closing `>'.  */
4382         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4383         /* Restore the old message.  */
4384         parser->type_definition_forbidden_message = saved_message;
4385
4386         /* And the expression which is being cast.  */
4387         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4388         expression = cp_parser_expression (parser, /*cast_p=*/true);
4389         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4390
4391         /* Only type conversions to integral or enumeration types
4392            can be used in constant-expressions.  */
4393         if (!cast_valid_in_integral_constant_expression_p (type)
4394             && (cp_parser_non_integral_constant_expression
4395                 (parser,
4396                  "a cast to a type other than an integral or "
4397                  "enumeration type")))
4398           return error_mark_node;
4399
4400         switch (keyword)
4401           {
4402           case RID_DYNCAST:
4403             postfix_expression
4404               = build_dynamic_cast (type, expression, tf_warning_or_error);
4405             break;
4406           case RID_STATCAST:
4407             postfix_expression
4408               = build_static_cast (type, expression, tf_warning_or_error);
4409             break;
4410           case RID_REINTCAST:
4411             postfix_expression
4412               = build_reinterpret_cast (type, expression, 
4413                                         tf_warning_or_error);
4414             break;
4415           case RID_CONSTCAST:
4416             postfix_expression
4417               = build_const_cast (type, expression, tf_warning_or_error);
4418             break;
4419           default:
4420             gcc_unreachable ();
4421           }
4422       }
4423       break;
4424
4425     case RID_TYPEID:
4426       {
4427         tree type;
4428         const char *saved_message;
4429         bool saved_in_type_id_in_expr_p;
4430
4431         /* Consume the `typeid' token.  */
4432         cp_lexer_consume_token (parser->lexer);
4433         /* Look for the `(' token.  */
4434         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4435         /* Types cannot be defined in a `typeid' expression.  */
4436         saved_message = parser->type_definition_forbidden_message;
4437         parser->type_definition_forbidden_message
4438           = "types may not be defined in a %<typeid%> expression";
4439         /* We can't be sure yet whether we're looking at a type-id or an
4440            expression.  */
4441         cp_parser_parse_tentatively (parser);
4442         /* Try a type-id first.  */
4443         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4444         parser->in_type_id_in_expr_p = true;
4445         type = cp_parser_type_id (parser);
4446         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4447         /* Look for the `)' token.  Otherwise, we can't be sure that
4448            we're not looking at an expression: consider `typeid (int
4449            (3))', for example.  */
4450         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4451         /* If all went well, simply lookup the type-id.  */
4452         if (cp_parser_parse_definitely (parser))
4453           postfix_expression = get_typeid (type);
4454         /* Otherwise, fall back to the expression variant.  */
4455         else
4456           {
4457             tree expression;
4458
4459             /* Look for an expression.  */
4460             expression = cp_parser_expression (parser, /*cast_p=*/false);
4461             /* Compute its typeid.  */
4462             postfix_expression = build_typeid (expression);
4463             /* Look for the `)' token.  */
4464             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4465           }
4466         /* Restore the saved message.  */
4467         parser->type_definition_forbidden_message = saved_message;
4468         /* `typeid' may not appear in an integral constant expression.  */
4469         if (cp_parser_non_integral_constant_expression(parser,
4470                                                        "%<typeid%> operator"))
4471           return error_mark_node;
4472       }
4473       break;
4474
4475     case RID_TYPENAME:
4476       {
4477         tree type;
4478         /* The syntax permitted here is the same permitted for an
4479            elaborated-type-specifier.  */
4480         type = cp_parser_elaborated_type_specifier (parser,
4481                                                     /*is_friend=*/false,
4482                                                     /*is_declaration=*/false);
4483         postfix_expression = cp_parser_functional_cast (parser, type);
4484       }
4485       break;
4486
4487     default:
4488       {
4489         tree type;
4490
4491         /* If the next thing is a simple-type-specifier, we may be
4492            looking at a functional cast.  We could also be looking at
4493            an id-expression.  So, we try the functional cast, and if
4494            that doesn't work we fall back to the primary-expression.  */
4495         cp_parser_parse_tentatively (parser);
4496         /* Look for the simple-type-specifier.  */
4497         type = cp_parser_simple_type_specifier (parser,
4498                                                 /*decl_specs=*/NULL,
4499                                                 CP_PARSER_FLAGS_NONE);
4500         /* Parse the cast itself.  */
4501         if (!cp_parser_error_occurred (parser))
4502           postfix_expression
4503             = cp_parser_functional_cast (parser, type);
4504         /* If that worked, we're done.  */
4505         if (cp_parser_parse_definitely (parser))
4506           break;
4507
4508         /* If the functional-cast didn't work out, try a
4509            compound-literal.  */
4510         if (cp_parser_allow_gnu_extensions_p (parser)
4511             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4512           {
4513             VEC(constructor_elt,gc) *initializer_list = NULL;
4514             bool saved_in_type_id_in_expr_p;
4515
4516             cp_parser_parse_tentatively (parser);
4517             /* Consume the `('.  */
4518             cp_lexer_consume_token (parser->lexer);
4519             /* Parse the type.  */
4520             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4521             parser->in_type_id_in_expr_p = true;
4522             type = cp_parser_type_id (parser);
4523             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4524             /* Look for the `)'.  */
4525             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526             /* Look for the `{'.  */
4527             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4528             /* If things aren't going well, there's no need to
4529                keep going.  */
4530             if (!cp_parser_error_occurred (parser))
4531               {
4532                 bool non_constant_p;
4533                 /* Parse the initializer-list.  */
4534                 initializer_list
4535                   = cp_parser_initializer_list (parser, &non_constant_p);
4536                 /* Allow a trailing `,'.  */
4537                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4538                   cp_lexer_consume_token (parser->lexer);
4539                 /* Look for the final `}'.  */
4540                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4541               }
4542             /* If that worked, we're definitely looking at a
4543                compound-literal expression.  */
4544             if (cp_parser_parse_definitely (parser))
4545               {
4546                 /* Warn the user that a compound literal is not
4547                    allowed in standard C++.  */
4548                 if (pedantic)
4549                   pedwarn ("ISO C++ forbids compound-literals");
4550                 /* For simplicity, we disallow compound literals in
4551                    constant-expressions.  We could
4552                    allow compound literals of integer type, whose
4553                    initializer was a constant, in constant
4554                    expressions.  Permitting that usage, as a further
4555                    extension, would not change the meaning of any
4556                    currently accepted programs.  (Of course, as
4557                    compound literals are not part of ISO C++, the
4558                    standard has nothing to say.)  */
4559                 if (cp_parser_non_integral_constant_expression 
4560                     (parser, "non-constant compound literals"))
4561                   {
4562                     postfix_expression = error_mark_node;
4563                     break;
4564                   }
4565                 /* Form the representation of the compound-literal.  */
4566                 postfix_expression
4567                   = (finish_compound_literal
4568                      (type, build_constructor (init_list_type_node,
4569                                                initializer_list)));
4570                 break;
4571               }
4572           }
4573
4574         /* It must be a primary-expression.  */
4575         postfix_expression
4576           = cp_parser_primary_expression (parser, address_p, cast_p,
4577                                           /*template_arg_p=*/false,
4578                                           &idk);
4579       }
4580       break;
4581     }
4582
4583   /* Keep looping until the postfix-expression is complete.  */
4584   while (true)
4585     {
4586       if (idk == CP_ID_KIND_UNQUALIFIED
4587           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4588           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4589         /* It is not a Koenig lookup function call.  */
4590         postfix_expression
4591           = unqualified_name_lookup_error (postfix_expression);
4592
4593       /* Peek at the next token.  */
4594       token = cp_lexer_peek_token (parser->lexer);
4595
4596       switch (token->type)
4597         {
4598         case CPP_OPEN_SQUARE:
4599           postfix_expression
4600             = cp_parser_postfix_open_square_expression (parser,
4601                                                         postfix_expression,
4602                                                         false);
4603           idk = CP_ID_KIND_NONE;
4604           is_member_access = false;
4605           break;
4606
4607         case CPP_OPEN_PAREN:
4608           /* postfix-expression ( expression-list [opt] ) */
4609           {
4610             bool koenig_p;
4611             bool is_builtin_constant_p;
4612             bool saved_integral_constant_expression_p = false;
4613             bool saved_non_integral_constant_expression_p = false;
4614             tree args;
4615
4616             is_member_access = false;
4617
4618             is_builtin_constant_p
4619               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4620             if (is_builtin_constant_p)
4621               {
4622                 /* The whole point of __builtin_constant_p is to allow
4623                    non-constant expressions to appear as arguments.  */
4624                 saved_integral_constant_expression_p
4625                   = parser->integral_constant_expression_p;
4626                 saved_non_integral_constant_expression_p
4627                   = parser->non_integral_constant_expression_p;
4628                 parser->integral_constant_expression_p = false;
4629               }
4630             args = (cp_parser_parenthesized_expression_list
4631                     (parser, /*is_attribute_list=*/false,
4632                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4633                      /*non_constant_p=*/NULL));
4634             if (is_builtin_constant_p)
4635               {
4636                 parser->integral_constant_expression_p
4637                   = saved_integral_constant_expression_p;
4638                 parser->non_integral_constant_expression_p
4639                   = saved_non_integral_constant_expression_p;
4640               }
4641
4642             if (args == error_mark_node)
4643               {
4644                 postfix_expression = error_mark_node;
4645                 break;
4646               }
4647
4648             /* Function calls are not permitted in
4649                constant-expressions.  */
4650             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4651                 && cp_parser_non_integral_constant_expression (parser,
4652                                                                "a function call"))
4653               {
4654                 postfix_expression = error_mark_node;
4655                 break;
4656               }
4657
4658             koenig_p = false;
4659             if (idk == CP_ID_KIND_UNQUALIFIED)
4660               {
4661                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4662                   {
4663                     if (args)
4664                       {
4665                         koenig_p = true;
4666                         postfix_expression
4667                           = perform_koenig_lookup (postfix_expression, args);
4668                       }
4669                     else
4670                       postfix_expression
4671                         = unqualified_fn_lookup_error (postfix_expression);
4672                   }
4673                 /* We do not perform argument-dependent lookup if
4674                    normal lookup finds a non-function, in accordance
4675                    with the expected resolution of DR 218.  */
4676                 else if (args && is_overloaded_fn (postfix_expression))
4677                   {
4678                     tree fn = get_first_fn (postfix_expression);
4679
4680                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4681                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4682
4683                     /* Only do argument dependent lookup if regular
4684                        lookup does not find a set of member functions.
4685                        [basic.lookup.koenig]/2a  */
4686                     if (!DECL_FUNCTION_MEMBER_P (fn))
4687                       {
4688                         koenig_p = true;
4689                         postfix_expression
4690                           = perform_koenig_lookup (postfix_expression, args);
4691                       }
4692                   }
4693               }
4694
4695             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4696               {
4697                 tree instance = TREE_OPERAND (postfix_expression, 0);
4698                 tree fn = TREE_OPERAND (postfix_expression, 1);
4699
4700                 if (processing_template_decl
4701                     && (type_dependent_expression_p (instance)
4702                         || (!BASELINK_P (fn)
4703                             && TREE_CODE (fn) != FIELD_DECL)
4704                         || type_dependent_expression_p (fn)
4705                         || any_type_dependent_arguments_p (args)))
4706                   {
4707                     postfix_expression
4708                       = build_nt_call_list (postfix_expression, args);
4709                     break;
4710                   }
4711
4712                 if (BASELINK_P (fn))
4713                   postfix_expression
4714                     = (build_new_method_call
4715                        (instance, fn, args, NULL_TREE,
4716                         (idk == CP_ID_KIND_QUALIFIED
4717                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4718                         /*fn_p=*/NULL,
4719                         tf_warning_or_error));
4720                 else
4721                   postfix_expression
4722                     = finish_call_expr (postfix_expression, args,
4723                                         /*disallow_virtual=*/false,
4724                                         /*koenig_p=*/false,
4725                                         tf_warning_or_error);
4726               }
4727             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4728                      || TREE_CODE (postfix_expression) == MEMBER_REF
4729                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4730               postfix_expression = (build_offset_ref_call_from_tree
4731                                     (postfix_expression, args));
4732             else if (idk == CP_ID_KIND_QUALIFIED)
4733               /* A call to a static class member, or a namespace-scope
4734                  function.  */
4735               postfix_expression
4736                 = finish_call_expr (postfix_expression, args,
4737                                     /*disallow_virtual=*/true,
4738                                     koenig_p,
4739                                     tf_warning_or_error);
4740             else
4741               /* All other function calls.  */
4742               postfix_expression
4743                 = finish_call_expr (postfix_expression, args,
4744                                     /*disallow_virtual=*/false,
4745                                     koenig_p,
4746                                     tf_warning_or_error);
4747
4748             if (warn_disallowed_functions)
4749               warn_if_disallowed_function_p (postfix_expression);
4750
4751             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4752             idk = CP_ID_KIND_NONE;
4753           }
4754           break;
4755
4756         case CPP_DOT:
4757         case CPP_DEREF:
4758           /* postfix-expression . template [opt] id-expression
4759              postfix-expression . pseudo-destructor-name
4760              postfix-expression -> template [opt] id-expression
4761              postfix-expression -> pseudo-destructor-name */
4762
4763           /* Consume the `.' or `->' operator.  */
4764           cp_lexer_consume_token (parser->lexer);
4765
4766           postfix_expression
4767             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4768                                                       postfix_expression,
4769                                                       false, &idk,
4770                                                       token->location);
4771
4772           is_member_access = true;
4773           break;
4774
4775         case CPP_PLUS_PLUS:
4776           /* postfix-expression ++  */
4777           /* Consume the `++' token.  */
4778           cp_lexer_consume_token (parser->lexer);
4779           /* Generate a representation for the complete expression.  */
4780           postfix_expression
4781             = finish_increment_expr (postfix_expression,
4782                                      POSTINCREMENT_EXPR);
4783           /* Increments may not appear in constant-expressions.  */
4784           if (cp_parser_non_integral_constant_expression (parser,
4785                                                           "an increment"))
4786             postfix_expression = error_mark_node;
4787           idk = CP_ID_KIND_NONE;
4788           is_member_access = false;
4789           break;
4790
4791         case CPP_MINUS_MINUS:
4792           /* postfix-expression -- */
4793           /* Consume the `--' token.  */
4794           cp_lexer_consume_token (parser->lexer);
4795           /* Generate a representation for the complete expression.  */
4796           postfix_expression
4797             = finish_increment_expr (postfix_expression,
4798                                      POSTDECREMENT_EXPR);
4799           /* Decrements may not appear in constant-expressions.  */
4800           if (cp_parser_non_integral_constant_expression (parser,
4801                                                           "a decrement"))
4802             postfix_expression = error_mark_node;
4803           idk = CP_ID_KIND_NONE;
4804           is_member_access = false;
4805           break;
4806
4807         default:
4808           if (member_access_only_p)
4809             return is_member_access? postfix_expression : error_mark_node;
4810           else
4811             return postfix_expression;
4812         }
4813     }
4814
4815   /* We should never get here.  */
4816   gcc_unreachable ();
4817   return error_mark_node;
4818 }
4819
4820 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4821    by cp_parser_builtin_offsetof.  We're looking for
4822
4823      postfix-expression [ expression ]
4824
4825    FOR_OFFSETOF is set if we're being called in that context, which
4826    changes how we deal with integer constant expressions.  */
4827
4828 static tree
4829 cp_parser_postfix_open_square_expression (cp_parser *parser,
4830                                           tree postfix_expression,
4831                                           bool for_offsetof)
4832 {
4833   tree index;
4834
4835   /* Consume the `[' token.  */
4836   cp_lexer_consume_token (parser->lexer);
4837
4838   /* Parse the index expression.  */
4839   /* ??? For offsetof, there is a question of what to allow here.  If
4840      offsetof is not being used in an integral constant expression context,
4841      then we *could* get the right answer by computing the value at runtime.
4842      If we are in an integral constant expression context, then we might
4843      could accept any constant expression; hard to say without analysis.
4844      Rather than open the barn door too wide right away, allow only integer
4845      constant expressions here.  */
4846   if (for_offsetof)
4847     index = cp_parser_constant_expression (parser, false, NULL);
4848   else
4849     index = cp_parser_expression (parser, /*cast_p=*/false);
4850
4851   /* Look for the closing `]'.  */
4852   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4853
4854   /* Build the ARRAY_REF.  */
4855   postfix_expression = grok_array_decl (postfix_expression, index);
4856
4857   /* When not doing offsetof, array references are not permitted in
4858      constant-expressions.  */
4859   if (!for_offsetof
4860       && (cp_parser_non_integral_constant_expression
4861           (parser, "an array reference")))
4862     postfix_expression = error_mark_node;
4863
4864   return postfix_expression;
4865 }
4866
4867 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4868    by cp_parser_builtin_offsetof.  We're looking for
4869
4870      postfix-expression . template [opt] id-expression
4871      postfix-expression . pseudo-destructor-name
4872      postfix-expression -> template [opt] id-expression
4873      postfix-expression -> pseudo-destructor-name
4874
4875    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4876    limits what of the above we'll actually accept, but nevermind.
4877    TOKEN_TYPE is the "." or "->" token, which will already have been
4878    removed from the stream.  */
4879
4880 static tree
4881 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4882                                         enum cpp_ttype token_type,
4883                                         tree postfix_expression,
4884                                         bool for_offsetof, cp_id_kind *idk,
4885                                         location_t location)
4886 {
4887   tree name;
4888   bool dependent_p;
4889   bool pseudo_destructor_p;
4890   tree scope = NULL_TREE;
4891
4892   /* If this is a `->' operator, dereference the pointer.  */
4893   if (token_type == CPP_DEREF)
4894     postfix_expression = build_x_arrow (postfix_expression);
4895   /* Check to see whether or not the expression is type-dependent.  */
4896   dependent_p = type_dependent_expression_p (postfix_expression);
4897   /* The identifier following the `->' or `.' is not qualified.  */
4898   parser->scope = NULL_TREE;
4899   parser->qualifying_scope = NULL_TREE;
4900   parser->object_scope = NULL_TREE;
4901   *idk = CP_ID_KIND_NONE;
4902   /* Enter the scope corresponding to the type of the object
4903      given by the POSTFIX_EXPRESSION.  */
4904   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4905     {
4906       scope = TREE_TYPE (postfix_expression);
4907       /* According to the standard, no expression should ever have
4908          reference type.  Unfortunately, we do not currently match
4909          the standard in this respect in that our internal representation
4910          of an expression may have reference type even when the standard
4911          says it does not.  Therefore, we have to manually obtain the
4912          underlying type here.  */
4913       scope = non_reference (scope);
4914       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4915       if (scope == unknown_type_node)
4916         {
4917           error ("%H%qE does not have class type", &location, postfix_expression);
4918           scope = NULL_TREE;
4919         }
4920       else
4921         scope = complete_type_or_else (scope, NULL_TREE);
4922       /* Let the name lookup machinery know that we are processing a
4923          class member access expression.  */
4924       parser->context->object_type = scope;
4925       /* If something went wrong, we want to be able to discern that case,
4926          as opposed to the case where there was no SCOPE due to the type
4927          of expression being dependent.  */
4928       if (!scope)
4929         scope = error_mark_node;
4930       /* If the SCOPE was erroneous, make the various semantic analysis
4931          functions exit quickly -- and without issuing additional error
4932          messages.  */
4933       if (scope == error_mark_node)
4934         postfix_expression = error_mark_node;
4935     }
4936
4937   /* Assume this expression is not a pseudo-destructor access.  */
4938   pseudo_destructor_p = false;
4939
4940   /* If the SCOPE is a scalar type, then, if this is a valid program,
4941      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4942      is type dependent, it can be pseudo-destructor-name or something else.
4943      Try to parse it as pseudo-destructor-name first.  */
4944   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4945     {
4946       tree s;
4947       tree type;
4948
4949       cp_parser_parse_tentatively (parser);
4950       /* Parse the pseudo-destructor-name.  */
4951       s = NULL_TREE;
4952       cp_parser_pseudo_destructor_name (parser, &s, &type);
4953       if (dependent_p
4954           && (cp_parser_error_occurred (parser)
4955               || TREE_CODE (type) != TYPE_DECL
4956               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4957         cp_parser_abort_tentative_parse (parser);
4958       else if (cp_parser_parse_definitely (parser))
4959         {
4960           pseudo_destructor_p = true;
4961           postfix_expression
4962             = finish_pseudo_destructor_expr (postfix_expression,
4963                                              s, TREE_TYPE (type));
4964         }
4965     }
4966
4967   if (!pseudo_destructor_p)
4968     {
4969       /* If the SCOPE is not a scalar type, we are looking at an
4970          ordinary class member access expression, rather than a
4971          pseudo-destructor-name.  */
4972       bool template_p;
4973       cp_token *token = cp_lexer_peek_token (parser->lexer);
4974       /* Parse the id-expression.  */
4975       name = (cp_parser_id_expression
4976               (parser,
4977                cp_parser_optional_template_keyword (parser),
4978                /*check_dependency_p=*/true,
4979                &template_p,
4980                /*declarator_p=*/false,
4981                /*optional_p=*/false));
4982       /* In general, build a SCOPE_REF if the member name is qualified.
4983          However, if the name was not dependent and has already been
4984          resolved; there is no need to build the SCOPE_REF.  For example;
4985
4986              struct X { void f(); };
4987              template <typename T> void f(T* t) { t->X::f(); }
4988
4989          Even though "t" is dependent, "X::f" is not and has been resolved
4990          to a BASELINK; there is no need to include scope information.  */
4991
4992       /* But we do need to remember that there was an explicit scope for
4993          virtual function calls.  */
4994       if (parser->scope)
4995         *idk = CP_ID_KIND_QUALIFIED;
4996
4997       /* If the name is a template-id that names a type, we will get a
4998          TYPE_DECL here.  That is invalid code.  */
4999       if (TREE_CODE (name) == TYPE_DECL)
5000         {
5001           error ("%Hinvalid use of %qD", &token->location, name);
5002           postfix_expression = error_mark_node;
5003         }
5004       else
5005         {
5006           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5007             {
5008               name = build_qualified_name (/*type=*/NULL_TREE,
5009                                            parser->scope,
5010                                            name,
5011                                            template_p);
5012               parser->scope = NULL_TREE;
5013               parser->qualifying_scope = NULL_TREE;
5014               parser->object_scope = NULL_TREE;
5015             }
5016           if (scope && name && BASELINK_P (name))
5017             adjust_result_of_qualified_name_lookup
5018               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5019           postfix_expression
5020             = finish_class_member_access_expr (postfix_expression, name,
5021                                                template_p, 
5022                                                tf_warning_or_error);
5023         }
5024     }
5025
5026   /* We no longer need to look up names in the scope of the object on
5027      the left-hand side of the `.' or `->' operator.  */
5028   parser->context->object_type = NULL_TREE;
5029
5030   /* Outside of offsetof, these operators may not appear in
5031      constant-expressions.  */
5032   if (!for_offsetof
5033       && (cp_parser_non_integral_constant_expression
5034           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5035     postfix_expression = error_mark_node;
5036
5037   return postfix_expression;
5038 }
5039
5040 /* Parse a parenthesized expression-list.
5041
5042    expression-list:
5043      assignment-expression
5044      expression-list, assignment-expression
5045
5046    attribute-list:
5047      expression-list
5048      identifier
5049      identifier, expression-list
5050
5051    CAST_P is true if this expression is the target of a cast.
5052
5053    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5054    argument pack.
5055
5056    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5057    representation of an assignment-expression.  Note that a TREE_LIST
5058    is returned even if there is only a single expression in the list.
5059    error_mark_node is returned if the ( and or ) are
5060    missing. NULL_TREE is returned on no expressions. The parentheses
5061    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5062    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5063    indicates whether or not all of the expressions in the list were
5064    constant.  */
5065
5066 static tree
5067 cp_parser_parenthesized_expression_list (cp_parser* parser,
5068                                          bool is_attribute_list,
5069                                          bool cast_p,
5070                                          bool allow_expansion_p,
5071                                          bool *non_constant_p)
5072 {
5073   tree expression_list = NULL_TREE;
5074   bool fold_expr_p = is_attribute_list;
5075   tree identifier = NULL_TREE;
5076   bool saved_greater_than_is_operator_p;
5077
5078   /* Assume all the expressions will be constant.  */
5079   if (non_constant_p)
5080     *non_constant_p = false;
5081
5082   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5083     return error_mark_node;
5084
5085   /* Within a parenthesized expression, a `>' token is always
5086      the greater-than operator.  */
5087   saved_greater_than_is_operator_p
5088     = parser->greater_than_is_operator_p;
5089   parser->greater_than_is_operator_p = true;
5090
5091   /* Consume expressions until there are no more.  */
5092   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5093     while (true)
5094       {
5095         tree expr;
5096
5097         /* At the beginning of attribute lists, check to see if the
5098            next token is an identifier.  */
5099         if (is_attribute_list
5100             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5101           {
5102             cp_token *token;
5103
5104             /* Consume the identifier.  */
5105             token = cp_lexer_consume_token (parser->lexer);
5106             /* Save the identifier.  */
5107             identifier = token->u.value;
5108           }
5109         else
5110           {
5111             bool expr_non_constant_p;
5112
5113             /* Parse the next assignment-expression.  */
5114             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5115               {
5116                 /* A braced-init-list.  */
5117                 maybe_warn_cpp0x ("extended initializer lists");
5118                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5119                 if (non_constant_p && expr_non_constant_p)
5120                   *non_constant_p = true;
5121               }
5122             else if (non_constant_p)
5123               {
5124                 expr = (cp_parser_constant_expression
5125                         (parser, /*allow_non_constant_p=*/true,
5126                          &expr_non_constant_p));
5127                 if (expr_non_constant_p)
5128                   *non_constant_p = true;
5129               }
5130             else
5131               expr = cp_parser_assignment_expression (parser, cast_p);
5132
5133             if (fold_expr_p)
5134               expr = fold_non_dependent_expr (expr);
5135
5136             /* If we have an ellipsis, then this is an expression
5137                expansion.  */
5138             if (allow_expansion_p
5139                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5140               {
5141                 /* Consume the `...'.  */
5142                 cp_lexer_consume_token (parser->lexer);
5143
5144                 /* Build the argument pack.  */
5145                 expr = make_pack_expansion (expr);
5146               }
5147
5148              /* Add it to the list.  We add error_mark_node
5149                 expressions to the list, so that we can still tell if
5150                 the correct form for a parenthesized expression-list
5151                 is found. That gives better errors.  */
5152             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5153
5154             if (expr == error_mark_node)
5155               goto skip_comma;
5156           }
5157
5158         /* After the first item, attribute lists look the same as
5159            expression lists.  */
5160         is_attribute_list = false;
5161
5162       get_comma:;
5163         /* If the next token isn't a `,', then we are done.  */
5164         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5165           break;
5166
5167         /* Otherwise, consume the `,' and keep going.  */
5168         cp_lexer_consume_token (parser->lexer);
5169       }
5170
5171   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5172     {
5173       int ending;
5174
5175     skip_comma:;
5176       /* We try and resync to an unnested comma, as that will give the
5177          user better diagnostics.  */
5178       ending = cp_parser_skip_to_closing_parenthesis (parser,
5179                                                       /*recovering=*/true,
5180                                                       /*or_comma=*/true,
5181                                                       /*consume_paren=*/true);
5182       if (ending < 0)
5183         goto get_comma;
5184       if (!ending)
5185         {
5186           parser->greater_than_is_operator_p
5187             = saved_greater_than_is_operator_p;
5188           return error_mark_node;
5189         }
5190     }
5191
5192   parser->greater_than_is_operator_p
5193     = saved_greater_than_is_operator_p;
5194
5195   /* We built up the list in reverse order so we must reverse it now.  */
5196   expression_list = nreverse (expression_list);
5197   if (identifier)
5198     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5199
5200   return expression_list;
5201 }
5202
5203 /* Parse a pseudo-destructor-name.
5204
5205    pseudo-destructor-name:
5206      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5207      :: [opt] nested-name-specifier template template-id :: ~ type-name
5208      :: [opt] nested-name-specifier [opt] ~ type-name
5209
5210    If either of the first two productions is used, sets *SCOPE to the
5211    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5212    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5213    or ERROR_MARK_NODE if the parse fails.  */
5214
5215 static void
5216 cp_parser_pseudo_destructor_name (cp_parser* parser,
5217                                   tree* scope,
5218                                   tree* type)
5219 {
5220   bool nested_name_specifier_p;
5221
5222   /* Assume that things will not work out.  */
5223   *type = error_mark_node;
5224
5225   /* Look for the optional `::' operator.  */
5226   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5227   /* Look for the optional nested-name-specifier.  */
5228   nested_name_specifier_p
5229     = (cp_parser_nested_name_specifier_opt (parser,
5230                                             /*typename_keyword_p=*/false,
5231                                             /*check_dependency_p=*/true,
5232                                             /*type_p=*/false,
5233                                             /*is_declaration=*/true)
5234        != NULL_TREE);
5235   /* Now, if we saw a nested-name-specifier, we might be doing the
5236      second production.  */
5237   if (nested_name_specifier_p
5238       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5239     {
5240       /* Consume the `template' keyword.  */
5241       cp_lexer_consume_token (parser->lexer);
5242       /* Parse the template-id.  */
5243       cp_parser_template_id (parser,
5244                              /*template_keyword_p=*/true,
5245                              /*check_dependency_p=*/false,
5246                              /*is_declaration=*/true);
5247       /* Look for the `::' token.  */
5248       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5249     }
5250   /* If the next token is not a `~', then there might be some
5251      additional qualification.  */
5252   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5253     {
5254       /* At this point, we're looking for "type-name :: ~".  The type-name
5255          must not be a class-name, since this is a pseudo-destructor.  So,
5256          it must be either an enum-name, or a typedef-name -- both of which
5257          are just identifiers.  So, we peek ahead to check that the "::"
5258          and "~" tokens are present; if they are not, then we can avoid
5259          calling type_name.  */
5260       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5261           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5262           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5263         {
5264           cp_parser_error (parser, "non-scalar type");
5265           return;
5266         }
5267
5268       /* Look for the type-name.  */
5269       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5270       if (*scope == error_mark_node)
5271         return;
5272
5273       /* Look for the `::' token.  */
5274       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5275     }
5276   else
5277     *scope = NULL_TREE;
5278
5279   /* Look for the `~'.  */
5280   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5281   /* Look for the type-name again.  We are not responsible for
5282      checking that it matches the first type-name.  */
5283   *type = cp_parser_nonclass_name (parser);
5284 }
5285
5286 /* Parse a unary-expression.
5287
5288    unary-expression:
5289      postfix-expression
5290      ++ cast-expression
5291      -- cast-expression
5292      unary-operator cast-expression
5293      sizeof unary-expression
5294      sizeof ( type-id )
5295      new-expression
5296      delete-expression
5297
5298    GNU Extensions:
5299
5300    unary-expression:
5301      __extension__ cast-expression
5302      __alignof__ unary-expression
5303      __alignof__ ( type-id )
5304      __real__ cast-expression
5305      __imag__ cast-expression
5306      && identifier
5307
5308    ADDRESS_P is true iff the unary-expression is appearing as the
5309    operand of the `&' operator.   CAST_P is true if this expression is
5310    the target of a cast.
5311
5312    Returns a representation of the expression.  */
5313
5314 static tree
5315 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5316 {
5317   cp_token *token;
5318   enum tree_code unary_operator;
5319
5320   /* Peek at the next token.  */
5321   token = cp_lexer_peek_token (parser->lexer);
5322   /* Some keywords give away the kind of expression.  */
5323   if (token->type == CPP_KEYWORD)
5324     {
5325       enum rid keyword = token->keyword;
5326
5327       switch (keyword)
5328         {
5329         case RID_ALIGNOF:
5330         case RID_SIZEOF:
5331           {
5332             tree operand;
5333             enum tree_code op;
5334
5335             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5336             /* Consume the token.  */
5337             cp_lexer_consume_token (parser->lexer);
5338             /* Parse the operand.  */
5339             operand = cp_parser_sizeof_operand (parser, keyword);
5340
5341             if (TYPE_P (operand))
5342               return cxx_sizeof_or_alignof_type (operand, op, true);
5343             else
5344               return cxx_sizeof_or_alignof_expr (operand, op, true);
5345           }
5346
5347         case RID_NEW:
5348           return cp_parser_new_expression (parser);
5349
5350         case RID_DELETE:
5351           return cp_parser_delete_expression (parser);
5352
5353         case RID_EXTENSION:
5354           {
5355             /* The saved value of the PEDANTIC flag.  */
5356             int saved_pedantic;
5357             tree expr;
5358
5359             /* Save away the PEDANTIC flag.  */
5360             cp_parser_extension_opt (parser, &saved_pedantic);
5361             /* Parse the cast-expression.  */
5362             expr = cp_parser_simple_cast_expression (parser);
5363             /* Restore the PEDANTIC flag.  */
5364             pedantic = saved_pedantic;
5365
5366             return expr;
5367           }
5368
5369         case RID_REALPART:
5370         case RID_IMAGPART:
5371           {
5372             tree expression;
5373
5374             /* Consume the `__real__' or `__imag__' token.  */
5375             cp_lexer_consume_token (parser->lexer);
5376             /* Parse the cast-expression.  */
5377             expression = cp_parser_simple_cast_expression (parser);
5378             /* Create the complete representation.  */
5379             return build_x_unary_op ((keyword == RID_REALPART
5380                                       ? REALPART_EXPR : IMAGPART_EXPR),
5381                                      expression,
5382                                      tf_warning_or_error);
5383           }
5384           break;
5385
5386         default:
5387           break;
5388         }
5389     }
5390
5391   /* Look for the `:: new' and `:: delete', which also signal the
5392      beginning of a new-expression, or delete-expression,
5393      respectively.  If the next token is `::', then it might be one of
5394      these.  */
5395   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5396     {
5397       enum rid keyword;
5398
5399       /* See if the token after the `::' is one of the keywords in
5400          which we're interested.  */
5401       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5402       /* If it's `new', we have a new-expression.  */
5403       if (keyword == RID_NEW)
5404         return cp_parser_new_expression (parser);
5405       /* Similarly, for `delete'.  */
5406       else if (keyword == RID_DELETE)
5407         return cp_parser_delete_expression (parser);
5408     }
5409
5410   /* Look for a unary operator.  */
5411   unary_operator = cp_parser_unary_operator (token);
5412   /* The `++' and `--' operators can be handled similarly, even though
5413      they are not technically unary-operators in the grammar.  */
5414   if (unary_operator == ERROR_MARK)
5415     {
5416       if (token->type == CPP_PLUS_PLUS)
5417         unary_operator = PREINCREMENT_EXPR;
5418       else if (token->type == CPP_MINUS_MINUS)
5419         unary_operator = PREDECREMENT_EXPR;
5420       /* Handle the GNU address-of-label extension.  */
5421       else if (cp_parser_allow_gnu_extensions_p (parser)
5422                && token->type == CPP_AND_AND)
5423         {
5424           tree identifier;
5425           tree expression;
5426
5427           /* Consume the '&&' token.  */
5428           cp_lexer_consume_token (parser->lexer);
5429           /* Look for the identifier.  */
5430           identifier = cp_parser_identifier (parser);
5431           /* Create an expression representing the address.  */
5432           expression = finish_label_address_expr (identifier);
5433           if (cp_parser_non_integral_constant_expression (parser,
5434                                                 "the address of a label"))
5435             expression = error_mark_node;
5436           return expression;
5437         }
5438     }
5439   if (unary_operator != ERROR_MARK)
5440     {
5441       tree cast_expression;
5442       tree expression = error_mark_node;
5443       const char *non_constant_p = NULL;
5444
5445       /* Consume the operator token.  */
5446       token = cp_lexer_consume_token (parser->lexer);
5447       /* Parse the cast-expression.  */
5448       cast_expression
5449         = cp_parser_cast_expression (parser,
5450                                      unary_operator == ADDR_EXPR,
5451                                      /*cast_p=*/false);
5452       /* Now, build an appropriate representation.  */
5453       switch (unary_operator)
5454         {
5455         case INDIRECT_REF:
5456           non_constant_p = "%<*%>";
5457           expression = build_x_indirect_ref (cast_expression, "unary *",
5458                                              tf_warning_or_error);
5459           break;
5460
5461         case ADDR_EXPR:
5462           non_constant_p = "%<&%>";
5463           /* Fall through.  */
5464         case BIT_NOT_EXPR:
5465           expression = build_x_unary_op (unary_operator, cast_expression,
5466                                          tf_warning_or_error);
5467           break;
5468
5469         case PREINCREMENT_EXPR:
5470         case PREDECREMENT_EXPR:
5471           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5472                             ? "%<++%>" : "%<--%>");
5473           /* Fall through.  */
5474         case UNARY_PLUS_EXPR:
5475         case NEGATE_EXPR:
5476         case TRUTH_NOT_EXPR:
5477           expression = finish_unary_op_expr (unary_operator, cast_expression);
5478           break;
5479
5480         default:
5481           gcc_unreachable ();
5482         }
5483
5484       if (non_constant_p
5485           && cp_parser_non_integral_constant_expression (parser,
5486                                                          non_constant_p))
5487         expression = error_mark_node;
5488
5489       return expression;
5490     }
5491
5492   return cp_parser_postfix_expression (parser, address_p, cast_p,
5493                                        /*member_access_only_p=*/false);
5494 }
5495
5496 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5497    unary-operator, the corresponding tree code is returned.  */
5498
5499 static enum tree_code
5500 cp_parser_unary_operator (cp_token* token)
5501 {
5502   switch (token->type)
5503     {
5504     case CPP_MULT:
5505       return INDIRECT_REF;
5506
5507     case CPP_AND:
5508       return ADDR_EXPR;
5509
5510     case CPP_PLUS:
5511       return UNARY_PLUS_EXPR;
5512
5513     case CPP_MINUS:
5514       return NEGATE_EXPR;
5515
5516     case CPP_NOT:
5517       return TRUTH_NOT_EXPR;
5518
5519     case CPP_COMPL:
5520       return BIT_NOT_EXPR;
5521
5522     default:
5523       return ERROR_MARK;
5524     }
5525 }
5526
5527 /* Parse a new-expression.
5528
5529    new-expression:
5530      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5531      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5532
5533    Returns a representation of the expression.  */
5534
5535 static tree
5536 cp_parser_new_expression (cp_parser* parser)
5537 {
5538   bool global_scope_p;
5539   tree placement;
5540   tree type;
5541   tree initializer;
5542   tree nelts;
5543
5544   /* Look for the optional `::' operator.  */
5545   global_scope_p
5546     = (cp_parser_global_scope_opt (parser,
5547                                    /*current_scope_valid_p=*/false)
5548        != NULL_TREE);
5549   /* Look for the `new' operator.  */
5550   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5551   /* There's no easy way to tell a new-placement from the
5552      `( type-id )' construct.  */
5553   cp_parser_parse_tentatively (parser);
5554   /* Look for a new-placement.  */
5555   placement = cp_parser_new_placement (parser);
5556   /* If that didn't work out, there's no new-placement.  */
5557   if (!cp_parser_parse_definitely (parser))
5558     placement = NULL_TREE;
5559
5560   /* If the next token is a `(', then we have a parenthesized
5561      type-id.  */
5562   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5563     {
5564       cp_token *token;
5565       /* Consume the `('.  */
5566       cp_lexer_consume_token (parser->lexer);
5567       /* Parse the type-id.  */
5568       type = cp_parser_type_id (parser);
5569       /* Look for the closing `)'.  */
5570       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5571       token = cp_lexer_peek_token (parser->lexer);
5572       /* There should not be a direct-new-declarator in this production,
5573          but GCC used to allowed this, so we check and emit a sensible error
5574          message for this case.  */
5575       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5576         {
5577           error ("%Harray bound forbidden after parenthesized type-id",
5578                  &token->location);
5579           inform ("%Htry removing the parentheses around the type-id",
5580                  &token->location);
5581           cp_parser_direct_new_declarator (parser);
5582         }
5583       nelts = NULL_TREE;
5584     }
5585   /* Otherwise, there must be a new-type-id.  */
5586   else
5587     type = cp_parser_new_type_id (parser, &nelts);
5588
5589   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5590   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5591       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5592     initializer = cp_parser_new_initializer (parser);
5593   else
5594     initializer = NULL_TREE;
5595
5596   /* A new-expression may not appear in an integral constant
5597      expression.  */
5598   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5599     return error_mark_node;
5600
5601   /* Create a representation of the new-expression.  */
5602   return build_new (placement, type, nelts, initializer, global_scope_p,
5603                     tf_warning_or_error);
5604 }
5605
5606 /* Parse a new-placement.
5607
5608    new-placement:
5609      ( expression-list )
5610
5611    Returns the same representation as for an expression-list.  */
5612
5613 static tree
5614 cp_parser_new_placement (cp_parser* parser)
5615 {
5616   tree expression_list;
5617
5618   /* Parse the expression-list.  */
5619   expression_list = (cp_parser_parenthesized_expression_list
5620                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5621                       /*non_constant_p=*/NULL));
5622
5623   return expression_list;
5624 }
5625
5626 /* Parse a new-type-id.
5627
5628    new-type-id:
5629      type-specifier-seq new-declarator [opt]
5630
5631    Returns the TYPE allocated.  If the new-type-id indicates an array
5632    type, *NELTS is set to the number of elements in the last array
5633    bound; the TYPE will not include the last array bound.  */
5634
5635 static tree
5636 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5637 {
5638   cp_decl_specifier_seq type_specifier_seq;
5639   cp_declarator *new_declarator;
5640   cp_declarator *declarator;
5641   cp_declarator *outer_declarator;
5642   const char *saved_message;
5643   tree type;
5644
5645   /* The type-specifier sequence must not contain type definitions.
5646      (It cannot contain declarations of new types either, but if they
5647      are not definitions we will catch that because they are not
5648      complete.)  */
5649   saved_message = parser->type_definition_forbidden_message;
5650   parser->type_definition_forbidden_message
5651     = "types may not be defined in a new-type-id";
5652   /* Parse the type-specifier-seq.  */
5653   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5654                                 &type_specifier_seq);
5655   /* Restore the old message.  */
5656   parser->type_definition_forbidden_message = saved_message;
5657   /* Parse the new-declarator.  */
5658   new_declarator = cp_parser_new_declarator_opt (parser);
5659
5660   /* Determine the number of elements in the last array dimension, if
5661      any.  */
5662   *nelts = NULL_TREE;
5663   /* Skip down to the last array dimension.  */
5664   declarator = new_declarator;
5665   outer_declarator = NULL;
5666   while (declarator && (declarator->kind == cdk_pointer
5667                         || declarator->kind == cdk_ptrmem))
5668     {
5669       outer_declarator = declarator;
5670       declarator = declarator->declarator;
5671     }
5672   while (declarator
5673          && declarator->kind == cdk_array
5674          && declarator->declarator
5675          && declarator->declarator->kind == cdk_array)
5676     {
5677       outer_declarator = declarator;
5678       declarator = declarator->declarator;
5679     }
5680
5681   if (declarator && declarator->kind == cdk_array)
5682     {
5683       *nelts = declarator->u.array.bounds;
5684       if (*nelts == error_mark_node)
5685         *nelts = integer_one_node;
5686
5687       if (outer_declarator)
5688         outer_declarator->declarator = declarator->declarator;
5689       else
5690         new_declarator = NULL;
5691     }
5692
5693   type = groktypename (&type_specifier_seq, new_declarator);
5694   return type;
5695 }
5696
5697 /* Parse an (optional) new-declarator.
5698
5699    new-declarator:
5700      ptr-operator new-declarator [opt]
5701      direct-new-declarator
5702
5703    Returns the declarator.  */
5704
5705 static cp_declarator *
5706 cp_parser_new_declarator_opt (cp_parser* parser)
5707 {
5708   enum tree_code code;
5709   tree type;
5710   cp_cv_quals cv_quals;
5711
5712   /* We don't know if there's a ptr-operator next, or not.  */
5713   cp_parser_parse_tentatively (parser);
5714   /* Look for a ptr-operator.  */
5715   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5716   /* If that worked, look for more new-declarators.  */
5717   if (cp_parser_parse_definitely (parser))
5718     {
5719       cp_declarator *declarator;
5720
5721       /* Parse another optional declarator.  */
5722       declarator = cp_parser_new_declarator_opt (parser);
5723
5724       return cp_parser_make_indirect_declarator
5725         (code, type, cv_quals, declarator);
5726     }
5727
5728   /* If the next token is a `[', there is a direct-new-declarator.  */
5729   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5730     return cp_parser_direct_new_declarator (parser);
5731
5732   return NULL;
5733 }
5734
5735 /* Parse a direct-new-declarator.
5736
5737    direct-new-declarator:
5738      [ expression ]
5739      direct-new-declarator [constant-expression]
5740
5741    */
5742
5743 static cp_declarator *
5744 cp_parser_direct_new_declarator (cp_parser* parser)
5745 {
5746   cp_declarator *declarator = NULL;
5747
5748   while (true)
5749     {
5750       tree expression;
5751
5752       /* Look for the opening `['.  */
5753       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5754       /* The first expression is not required to be constant.  */
5755       if (!declarator)
5756         {
5757           cp_token *token = cp_lexer_peek_token (parser->lexer);
5758           expression = cp_parser_expression (parser, /*cast_p=*/false);
5759           /* The standard requires that the expression have integral
5760              type.  DR 74 adds enumeration types.  We believe that the
5761              real intent is that these expressions be handled like the
5762              expression in a `switch' condition, which also allows
5763              classes with a single conversion to integral or
5764              enumeration type.  */
5765           if (!processing_template_decl)
5766             {
5767               expression
5768                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5769                                               expression,
5770                                               /*complain=*/true);
5771               if (!expression)
5772                 {
5773                   error ("%Hexpression in new-declarator must have integral "
5774                          "or enumeration type", &token->location);
5775                   expression = error_mark_node;
5776                 }
5777             }
5778         }
5779       /* But all the other expressions must be.  */
5780       else
5781         expression
5782           = cp_parser_constant_expression (parser,
5783                                            /*allow_non_constant=*/false,
5784                                            NULL);
5785       /* Look for the closing `]'.  */
5786       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5787
5788       /* Add this bound to the declarator.  */
5789       declarator = make_array_declarator (declarator, expression);
5790
5791       /* If the next token is not a `[', then there are no more
5792          bounds.  */
5793       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5794         break;
5795     }
5796
5797   return declarator;
5798 }
5799
5800 /* Parse a new-initializer.
5801
5802    new-initializer:
5803      ( expression-list [opt] )
5804      braced-init-list
5805
5806    Returns a representation of the expression-list.  If there is no
5807    expression-list, VOID_ZERO_NODE is returned.  */
5808
5809 static tree
5810 cp_parser_new_initializer (cp_parser* parser)
5811 {
5812   tree expression_list;
5813
5814   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5815     {
5816       bool expr_non_constant_p;
5817       maybe_warn_cpp0x ("extended initializer lists");
5818       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5819       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5820       expression_list = build_tree_list (NULL_TREE, expression_list);
5821     }
5822   else
5823     expression_list = (cp_parser_parenthesized_expression_list
5824                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5825                         /*non_constant_p=*/NULL));
5826   if (!expression_list)
5827     expression_list = void_zero_node;
5828
5829   return expression_list;
5830 }
5831
5832 /* Parse a delete-expression.
5833
5834    delete-expression:
5835      :: [opt] delete cast-expression
5836      :: [opt] delete [ ] cast-expression
5837
5838    Returns a representation of the expression.  */
5839
5840 static tree
5841 cp_parser_delete_expression (cp_parser* parser)
5842 {
5843   bool global_scope_p;
5844   bool array_p;
5845   tree expression;
5846
5847   /* Look for the optional `::' operator.  */
5848   global_scope_p
5849     = (cp_parser_global_scope_opt (parser,
5850                                    /*current_scope_valid_p=*/false)
5851        != NULL_TREE);
5852   /* Look for the `delete' keyword.  */
5853   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5854   /* See if the array syntax is in use.  */
5855   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5856     {
5857       /* Consume the `[' token.  */
5858       cp_lexer_consume_token (parser->lexer);
5859       /* Look for the `]' token.  */
5860       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5861       /* Remember that this is the `[]' construct.  */
5862       array_p = true;
5863     }
5864   else
5865     array_p = false;
5866
5867   /* Parse the cast-expression.  */
5868   expression = cp_parser_simple_cast_expression (parser);
5869
5870   /* A delete-expression may not appear in an integral constant
5871      expression.  */
5872   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5873     return error_mark_node;
5874
5875   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5876 }
5877
5878 /* Parse a cast-expression.
5879
5880    cast-expression:
5881      unary-expression
5882      ( type-id ) cast-expression
5883
5884    ADDRESS_P is true iff the unary-expression is appearing as the
5885    operand of the `&' operator.   CAST_P is true if this expression is
5886    the target of a cast.
5887
5888    Returns a representation of the expression.  */
5889
5890 static tree
5891 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5892 {
5893   /* If it's a `(', then we might be looking at a cast.  */
5894   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5895     {
5896       tree type = NULL_TREE;
5897       tree expr = NULL_TREE;
5898       bool compound_literal_p;
5899       const char *saved_message;
5900
5901       /* There's no way to know yet whether or not this is a cast.
5902          For example, `(int (3))' is a unary-expression, while `(int)
5903          3' is a cast.  So, we resort to parsing tentatively.  */
5904       cp_parser_parse_tentatively (parser);
5905       /* Types may not be defined in a cast.  */
5906       saved_message = parser->type_definition_forbidden_message;
5907       parser->type_definition_forbidden_message
5908         = "types may not be defined in casts";
5909       /* Consume the `('.  */
5910       cp_lexer_consume_token (parser->lexer);
5911       /* A very tricky bit is that `(struct S) { 3 }' is a
5912          compound-literal (which we permit in C++ as an extension).
5913          But, that construct is not a cast-expression -- it is a
5914          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5915          is legal; if the compound-literal were a cast-expression,
5916          you'd need an extra set of parentheses.)  But, if we parse
5917          the type-id, and it happens to be a class-specifier, then we
5918          will commit to the parse at that point, because we cannot
5919          undo the action that is done when creating a new class.  So,
5920          then we cannot back up and do a postfix-expression.
5921
5922          Therefore, we scan ahead to the closing `)', and check to see
5923          if the token after the `)' is a `{'.  If so, we are not
5924          looking at a cast-expression.
5925
5926          Save tokens so that we can put them back.  */
5927       cp_lexer_save_tokens (parser->lexer);
5928       /* Skip tokens until the next token is a closing parenthesis.
5929          If we find the closing `)', and the next token is a `{', then
5930          we are looking at a compound-literal.  */
5931       compound_literal_p
5932         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5933                                                   /*consume_paren=*/true)
5934            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5935       /* Roll back the tokens we skipped.  */
5936       cp_lexer_rollback_tokens (parser->lexer);
5937       /* If we were looking at a compound-literal, simulate an error
5938          so that the call to cp_parser_parse_definitely below will
5939          fail.  */
5940       if (compound_literal_p)
5941         cp_parser_simulate_error (parser);
5942       else
5943         {
5944           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5945           parser->in_type_id_in_expr_p = true;
5946           /* Look for the type-id.  */
5947           type = cp_parser_type_id (parser);
5948           /* Look for the closing `)'.  */
5949           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5950           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5951         }
5952
5953       /* Restore the saved message.  */
5954       parser->type_definition_forbidden_message = saved_message;
5955
5956       /* If ok so far, parse the dependent expression. We cannot be
5957          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5958          ctor of T, but looks like a cast to function returning T
5959          without a dependent expression.  */
5960       if (!cp_parser_error_occurred (parser))
5961         expr = cp_parser_cast_expression (parser,
5962                                           /*address_p=*/false,
5963                                           /*cast_p=*/true);
5964
5965       if (cp_parser_parse_definitely (parser))
5966         {
5967           /* Warn about old-style casts, if so requested.  */
5968           if (warn_old_style_cast
5969               && !in_system_header
5970               && !VOID_TYPE_P (type)
5971               && current_lang_name != lang_name_c)
5972             warning (OPT_Wold_style_cast, "use of old-style cast");
5973
5974           /* Only type conversions to integral or enumeration types
5975              can be used in constant-expressions.  */
5976           if (!cast_valid_in_integral_constant_expression_p (type)
5977               && (cp_parser_non_integral_constant_expression
5978                   (parser,
5979                    "a cast to a type other than an integral or "
5980                    "enumeration type")))
5981             return error_mark_node;
5982
5983           /* Perform the cast.  */
5984           expr = build_c_cast (type, expr);
5985           return expr;
5986         }
5987     }
5988
5989   /* If we get here, then it's not a cast, so it must be a
5990      unary-expression.  */
5991   return cp_parser_unary_expression (parser, address_p, cast_p);
5992 }
5993
5994 /* Parse a binary expression of the general form:
5995
5996    pm-expression:
5997      cast-expression
5998      pm-expression .* cast-expression
5999      pm-expression ->* cast-expression
6000
6001    multiplicative-expression:
6002      pm-expression
6003      multiplicative-expression * pm-expression
6004      multiplicative-expression / pm-expression
6005      multiplicative-expression % pm-expression
6006
6007    additive-expression:
6008      multiplicative-expression
6009      additive-expression + multiplicative-expression
6010      additive-expression - multiplicative-expression
6011
6012    shift-expression:
6013      additive-expression
6014      shift-expression << additive-expression
6015      shift-expression >> additive-expression
6016
6017    relational-expression:
6018      shift-expression
6019      relational-expression < shift-expression
6020      relational-expression > shift-expression
6021      relational-expression <= shift-expression
6022      relational-expression >= shift-expression
6023
6024   GNU Extension:
6025
6026    relational-expression:
6027      relational-expression <? shift-expression
6028      relational-expression >? shift-expression
6029
6030    equality-expression:
6031      relational-expression
6032      equality-expression == relational-expression
6033      equality-expression != relational-expression
6034
6035    and-expression:
6036      equality-expression
6037      and-expression & equality-expression
6038
6039    exclusive-or-expression:
6040      and-expression
6041      exclusive-or-expression ^ and-expression
6042
6043    inclusive-or-expression:
6044      exclusive-or-expression
6045      inclusive-or-expression | exclusive-or-expression
6046
6047    logical-and-expression:
6048      inclusive-or-expression
6049      logical-and-expression && inclusive-or-expression
6050
6051    logical-or-expression:
6052      logical-and-expression
6053      logical-or-expression || logical-and-expression
6054
6055    All these are implemented with a single function like:
6056
6057    binary-expression:
6058      simple-cast-expression
6059      binary-expression <token> binary-expression
6060
6061    CAST_P is true if this expression is the target of a cast.
6062
6063    The binops_by_token map is used to get the tree codes for each <token> type.
6064    binary-expressions are associated according to a precedence table.  */
6065
6066 #define TOKEN_PRECEDENCE(token)                              \
6067 (((token->type == CPP_GREATER                                \
6068    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6069   && !parser->greater_than_is_operator_p)                    \
6070  ? PREC_NOT_OPERATOR                                         \
6071  : binops_by_token[token->type].prec)
6072
6073 static tree
6074 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6075                              enum cp_parser_prec prec)
6076 {
6077   cp_parser_expression_stack stack;
6078   cp_parser_expression_stack_entry *sp = &stack[0];
6079   tree lhs, rhs;
6080   cp_token *token;
6081   enum tree_code tree_type, lhs_type, rhs_type;
6082   enum cp_parser_prec new_prec, lookahead_prec;
6083   bool overloaded_p;
6084
6085   /* Parse the first expression.  */
6086   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6087   lhs_type = ERROR_MARK;
6088
6089   for (;;)
6090     {
6091       /* Get an operator token.  */
6092       token = cp_lexer_peek_token (parser->lexer);
6093
6094       if (warn_cxx0x_compat
6095           && token->type == CPP_RSHIFT
6096           && !parser->greater_than_is_operator_p)
6097         {
6098           warning (OPT_Wc__0x_compat, 
6099                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6100                    &token->location);
6101           warning (OPT_Wc__0x_compat, 
6102                    "suggest parentheses around %<>>%> expression");
6103         }
6104
6105       new_prec = TOKEN_PRECEDENCE (token);
6106
6107       /* Popping an entry off the stack means we completed a subexpression:
6108          - either we found a token which is not an operator (`>' where it is not
6109            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6110            will happen repeatedly;
6111          - or, we found an operator which has lower priority.  This is the case
6112            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6113            parsing `3 * 4'.  */
6114       if (new_prec <= prec)
6115         {
6116           if (sp == stack)
6117             break;
6118           else
6119             goto pop;
6120         }
6121
6122      get_rhs:
6123       tree_type = binops_by_token[token->type].tree_type;
6124
6125       /* We used the operator token.  */
6126       cp_lexer_consume_token (parser->lexer);
6127
6128       /* Extract another operand.  It may be the RHS of this expression
6129          or the LHS of a new, higher priority expression.  */
6130       rhs = cp_parser_simple_cast_expression (parser);
6131       rhs_type = ERROR_MARK;
6132
6133       /* Get another operator token.  Look up its precedence to avoid
6134          building a useless (immediately popped) stack entry for common
6135          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6136       token = cp_lexer_peek_token (parser->lexer);
6137       lookahead_prec = TOKEN_PRECEDENCE (token);
6138       if (lookahead_prec > new_prec)
6139         {
6140           /* ... and prepare to parse the RHS of the new, higher priority
6141              expression.  Since precedence levels on the stack are
6142              monotonically increasing, we do not have to care about
6143              stack overflows.  */
6144           sp->prec = prec;
6145           sp->tree_type = tree_type;
6146           sp->lhs = lhs;
6147           sp->lhs_type = lhs_type;
6148           sp++;
6149           lhs = rhs;
6150           lhs_type = rhs_type;
6151           prec = new_prec;
6152           new_prec = lookahead_prec;
6153           goto get_rhs;
6154
6155          pop:
6156           /* If the stack is not empty, we have parsed into LHS the right side
6157              (`4' in the example above) of an expression we had suspended.
6158              We can use the information on the stack to recover the LHS (`3')
6159              from the stack together with the tree code (`MULT_EXPR'), and
6160              the precedence of the higher level subexpression
6161              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6162              which will be used to actually build the additive expression.  */
6163           --sp;
6164           prec = sp->prec;
6165           tree_type = sp->tree_type;
6166           rhs = lhs;
6167           rhs_type = lhs_type;
6168           lhs = sp->lhs;
6169           lhs_type = sp->lhs_type;
6170         }
6171
6172       overloaded_p = false;
6173       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6174                                &overloaded_p, tf_warning_or_error);
6175       lhs_type = tree_type;
6176
6177       /* If the binary operator required the use of an overloaded operator,
6178          then this expression cannot be an integral constant-expression.
6179          An overloaded operator can be used even if both operands are
6180          otherwise permissible in an integral constant-expression if at
6181          least one of the operands is of enumeration type.  */
6182
6183       if (overloaded_p
6184           && (cp_parser_non_integral_constant_expression
6185               (parser, "calls to overloaded operators")))
6186         return error_mark_node;
6187     }
6188
6189   return lhs;
6190 }
6191
6192
6193 /* Parse the `? expression : assignment-expression' part of a
6194    conditional-expression.  The LOGICAL_OR_EXPR is the
6195    logical-or-expression that started the conditional-expression.
6196    Returns a representation of the entire conditional-expression.
6197
6198    This routine is used by cp_parser_assignment_expression.
6199
6200      ? expression : assignment-expression
6201
6202    GNU Extensions:
6203
6204      ? : assignment-expression */
6205
6206 static tree
6207 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6208 {
6209   tree expr;
6210   tree assignment_expr;
6211
6212   /* Consume the `?' token.  */
6213   cp_lexer_consume_token (parser->lexer);
6214   if (cp_parser_allow_gnu_extensions_p (parser)
6215       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6216     /* Implicit true clause.  */
6217     expr = NULL_TREE;
6218   else
6219     /* Parse the expression.  */
6220     expr = cp_parser_expression (parser, /*cast_p=*/false);
6221
6222   /* The next token should be a `:'.  */
6223   cp_parser_require (parser, CPP_COLON, "%<:%>");
6224   /* Parse the assignment-expression.  */
6225   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6226
6227   /* Build the conditional-expression.  */
6228   return build_x_conditional_expr (logical_or_expr,
6229                                    expr,
6230                                    assignment_expr,
6231                                    tf_warning_or_error);
6232 }
6233
6234 /* Parse an assignment-expression.
6235
6236    assignment-expression:
6237      conditional-expression
6238      logical-or-expression assignment-operator assignment_expression
6239      throw-expression
6240
6241    CAST_P is true if this expression is the target of a cast.
6242
6243    Returns a representation for the expression.  */
6244
6245 static tree
6246 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6247 {
6248   tree expr;
6249
6250   /* If the next token is the `throw' keyword, then we're looking at
6251      a throw-expression.  */
6252   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6253     expr = cp_parser_throw_expression (parser);
6254   /* Otherwise, it must be that we are looking at a
6255      logical-or-expression.  */
6256   else
6257     {
6258       /* Parse the binary expressions (logical-or-expression).  */
6259       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6260       /* If the next token is a `?' then we're actually looking at a
6261          conditional-expression.  */
6262       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6263         return cp_parser_question_colon_clause (parser, expr);
6264       else
6265         {
6266           enum tree_code assignment_operator;
6267
6268           /* If it's an assignment-operator, we're using the second
6269              production.  */
6270           assignment_operator
6271             = cp_parser_assignment_operator_opt (parser);
6272           if (assignment_operator != ERROR_MARK)
6273             {
6274               bool non_constant_p;
6275
6276               /* Parse the right-hand side of the assignment.  */
6277               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6278
6279               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6280                 maybe_warn_cpp0x ("extended initializer lists");
6281
6282               /* An assignment may not appear in a
6283                  constant-expression.  */
6284               if (cp_parser_non_integral_constant_expression (parser,
6285                                                               "an assignment"))
6286                 return error_mark_node;
6287               /* Build the assignment expression.  */
6288               expr = build_x_modify_expr (expr,
6289                                           assignment_operator,
6290                                           rhs,
6291                                           tf_warning_or_error);
6292             }
6293         }
6294     }
6295
6296   return expr;
6297 }
6298
6299 /* Parse an (optional) assignment-operator.
6300
6301    assignment-operator: one of
6302      = *= /= %= += -= >>= <<= &= ^= |=
6303
6304    GNU Extension:
6305
6306    assignment-operator: one of
6307      <?= >?=
6308
6309    If the next token is an assignment operator, the corresponding tree
6310    code is returned, and the token is consumed.  For example, for
6311    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6312    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6313    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6314    operator, ERROR_MARK is returned.  */
6315
6316 static enum tree_code
6317 cp_parser_assignment_operator_opt (cp_parser* parser)
6318 {
6319   enum tree_code op;
6320   cp_token *token;
6321
6322   /* Peek at the next toen.  */
6323   token = cp_lexer_peek_token (parser->lexer);
6324
6325   switch (token->type)
6326     {
6327     case CPP_EQ:
6328       op = NOP_EXPR;
6329       break;
6330
6331     case CPP_MULT_EQ:
6332       op = MULT_EXPR;
6333       break;
6334
6335     case CPP_DIV_EQ:
6336       op = TRUNC_DIV_EXPR;
6337       break;
6338
6339     case CPP_MOD_EQ:
6340       op = TRUNC_MOD_EXPR;
6341       break;
6342
6343     case CPP_PLUS_EQ:
6344       op = PLUS_EXPR;
6345       break;
6346
6347     case CPP_MINUS_EQ:
6348       op = MINUS_EXPR;
6349       break;
6350
6351     case CPP_RSHIFT_EQ:
6352       op = RSHIFT_EXPR;
6353       break;
6354
6355     case CPP_LSHIFT_EQ:
6356       op = LSHIFT_EXPR;
6357       break;
6358
6359     case CPP_AND_EQ:
6360       op = BIT_AND_EXPR;
6361       break;
6362
6363     case CPP_XOR_EQ:
6364       op = BIT_XOR_EXPR;
6365       break;
6366
6367     case CPP_OR_EQ:
6368       op = BIT_IOR_EXPR;
6369       break;
6370
6371     default:
6372       /* Nothing else is an assignment operator.  */
6373       op = ERROR_MARK;
6374     }
6375
6376   /* If it was an assignment operator, consume it.  */
6377   if (op != ERROR_MARK)
6378     cp_lexer_consume_token (parser->lexer);
6379
6380   return op;
6381 }
6382
6383 /* Parse an expression.
6384
6385    expression:
6386      assignment-expression
6387      expression , assignment-expression
6388
6389    CAST_P is true if this expression is the target of a cast.
6390
6391    Returns a representation of the expression.  */
6392
6393 static tree
6394 cp_parser_expression (cp_parser* parser, bool cast_p)
6395 {
6396   tree expression = NULL_TREE;
6397
6398   while (true)
6399     {
6400       tree assignment_expression;
6401
6402       /* Parse the next assignment-expression.  */
6403       assignment_expression
6404         = cp_parser_assignment_expression (parser, cast_p);
6405       /* If this is the first assignment-expression, we can just
6406          save it away.  */
6407       if (!expression)
6408         expression = assignment_expression;
6409       else
6410         expression = build_x_compound_expr (expression,
6411                                             assignment_expression,
6412                                             tf_warning_or_error);
6413       /* If the next token is not a comma, then we are done with the
6414          expression.  */
6415       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6416         break;
6417       /* Consume the `,'.  */
6418       cp_lexer_consume_token (parser->lexer);
6419       /* A comma operator cannot appear in a constant-expression.  */
6420       if (cp_parser_non_integral_constant_expression (parser,
6421                                                       "a comma operator"))
6422         expression = error_mark_node;
6423     }
6424
6425   return expression;
6426 }
6427
6428 /* Parse a constant-expression.
6429
6430    constant-expression:
6431      conditional-expression
6432
6433   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6434   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6435   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6436   is false, NON_CONSTANT_P should be NULL.  */
6437
6438 static tree
6439 cp_parser_constant_expression (cp_parser* parser,
6440                                bool allow_non_constant_p,
6441                                bool *non_constant_p)
6442 {
6443   bool saved_integral_constant_expression_p;
6444   bool saved_allow_non_integral_constant_expression_p;
6445   bool saved_non_integral_constant_expression_p;
6446   tree expression;
6447
6448   /* It might seem that we could simply parse the
6449      conditional-expression, and then check to see if it were
6450      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6451      one that the compiler can figure out is constant, possibly after
6452      doing some simplifications or optimizations.  The standard has a
6453      precise definition of constant-expression, and we must honor
6454      that, even though it is somewhat more restrictive.
6455
6456      For example:
6457
6458        int i[(2, 3)];
6459
6460      is not a legal declaration, because `(2, 3)' is not a
6461      constant-expression.  The `,' operator is forbidden in a
6462      constant-expression.  However, GCC's constant-folding machinery
6463      will fold this operation to an INTEGER_CST for `3'.  */
6464
6465   /* Save the old settings.  */
6466   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6467   saved_allow_non_integral_constant_expression_p
6468     = parser->allow_non_integral_constant_expression_p;
6469   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6470   /* We are now parsing a constant-expression.  */
6471   parser->integral_constant_expression_p = true;
6472   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6473   parser->non_integral_constant_expression_p = false;
6474   /* Although the grammar says "conditional-expression", we parse an
6475      "assignment-expression", which also permits "throw-expression"
6476      and the use of assignment operators.  In the case that
6477      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6478      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6479      actually essential that we look for an assignment-expression.
6480      For example, cp_parser_initializer_clauses uses this function to
6481      determine whether a particular assignment-expression is in fact
6482      constant.  */
6483   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6484   /* Restore the old settings.  */
6485   parser->integral_constant_expression_p
6486     = saved_integral_constant_expression_p;
6487   parser->allow_non_integral_constant_expression_p
6488     = saved_allow_non_integral_constant_expression_p;
6489   if (allow_non_constant_p)
6490     *non_constant_p = parser->non_integral_constant_expression_p;
6491   else if (parser->non_integral_constant_expression_p)
6492     expression = error_mark_node;
6493   parser->non_integral_constant_expression_p
6494     = saved_non_integral_constant_expression_p;
6495
6496   return expression;
6497 }
6498
6499 /* Parse __builtin_offsetof.
6500
6501    offsetof-expression:
6502      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6503
6504    offsetof-member-designator:
6505      id-expression
6506      | offsetof-member-designator "." id-expression
6507      | offsetof-member-designator "[" expression "]"  */
6508
6509 static tree
6510 cp_parser_builtin_offsetof (cp_parser *parser)
6511 {
6512   int save_ice_p, save_non_ice_p;
6513   tree type, expr;
6514   cp_id_kind dummy;
6515   cp_token *token;
6516
6517   /* We're about to accept non-integral-constant things, but will
6518      definitely yield an integral constant expression.  Save and
6519      restore these values around our local parsing.  */
6520   save_ice_p = parser->integral_constant_expression_p;
6521   save_non_ice_p = parser->non_integral_constant_expression_p;
6522
6523   /* Consume the "__builtin_offsetof" token.  */
6524   cp_lexer_consume_token (parser->lexer);
6525   /* Consume the opening `('.  */
6526   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6527   /* Parse the type-id.  */
6528   type = cp_parser_type_id (parser);
6529   /* Look for the `,'.  */
6530   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6531   token = cp_lexer_peek_token (parser->lexer);
6532
6533   /* Build the (type *)null that begins the traditional offsetof macro.  */
6534   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6535                             tf_warning_or_error);
6536
6537   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6538   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6539                                                  true, &dummy, token->location);
6540   while (true)
6541     {
6542       token = cp_lexer_peek_token (parser->lexer);
6543       switch (token->type)
6544         {
6545         case CPP_OPEN_SQUARE:
6546           /* offsetof-member-designator "[" expression "]" */
6547           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6548           break;
6549
6550         case CPP_DOT:
6551           /* offsetof-member-designator "." identifier */
6552           cp_lexer_consume_token (parser->lexer);
6553           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6554                                                          true, &dummy,
6555                                                          token->location);
6556           break;
6557
6558         case CPP_CLOSE_PAREN:
6559           /* Consume the ")" token.  */
6560           cp_lexer_consume_token (parser->lexer);
6561           goto success;
6562
6563         default:
6564           /* Error.  We know the following require will fail, but
6565              that gives the proper error message.  */
6566           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6567           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6568           expr = error_mark_node;
6569           goto failure;
6570         }
6571     }
6572
6573  success:
6574   /* If we're processing a template, we can't finish the semantics yet.
6575      Otherwise we can fold the entire expression now.  */
6576   if (processing_template_decl)
6577     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6578   else
6579     expr = finish_offsetof (expr);
6580
6581  failure:
6582   parser->integral_constant_expression_p = save_ice_p;
6583   parser->non_integral_constant_expression_p = save_non_ice_p;
6584
6585   return expr;
6586 }
6587
6588 /* Parse a trait expression.  */
6589
6590 static tree
6591 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6592 {
6593   cp_trait_kind kind;
6594   tree type1, type2 = NULL_TREE;
6595   bool binary = false;
6596   cp_decl_specifier_seq decl_specs;
6597
6598   switch (keyword)
6599     {
6600     case RID_HAS_NOTHROW_ASSIGN:
6601       kind = CPTK_HAS_NOTHROW_ASSIGN;
6602       break;
6603     case RID_HAS_NOTHROW_CONSTRUCTOR:
6604       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6605       break;
6606     case RID_HAS_NOTHROW_COPY:
6607       kind = CPTK_HAS_NOTHROW_COPY;
6608       break;
6609     case RID_HAS_TRIVIAL_ASSIGN:
6610       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6611       break;
6612     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6613       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6614       break;
6615     case RID_HAS_TRIVIAL_COPY:
6616       kind = CPTK_HAS_TRIVIAL_COPY;
6617       break;
6618     case RID_HAS_TRIVIAL_DESTRUCTOR:
6619       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6620       break;
6621     case RID_HAS_VIRTUAL_DESTRUCTOR:
6622       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6623       break;
6624     case RID_IS_ABSTRACT:
6625       kind = CPTK_IS_ABSTRACT;
6626       break;
6627     case RID_IS_BASE_OF:
6628       kind = CPTK_IS_BASE_OF;
6629       binary = true;
6630       break;
6631     case RID_IS_CLASS:
6632       kind = CPTK_IS_CLASS;
6633       break;
6634     case RID_IS_CONVERTIBLE_TO:
6635       kind = CPTK_IS_CONVERTIBLE_TO;
6636       binary = true;
6637       break;
6638     case RID_IS_EMPTY:
6639       kind = CPTK_IS_EMPTY;
6640       break;
6641     case RID_IS_ENUM:
6642       kind = CPTK_IS_ENUM;
6643       break;
6644     case RID_IS_POD:
6645       kind = CPTK_IS_POD;
6646       break;
6647     case RID_IS_POLYMORPHIC:
6648       kind = CPTK_IS_POLYMORPHIC;
6649       break;
6650     case RID_IS_UNION:
6651       kind = CPTK_IS_UNION;
6652       break;
6653     default:
6654       gcc_unreachable ();
6655     }
6656
6657   /* Consume the token.  */
6658   cp_lexer_consume_token (parser->lexer);
6659
6660   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6661
6662   type1 = cp_parser_type_id (parser);
6663
6664   if (type1 == error_mark_node)
6665     return error_mark_node;
6666
6667   /* Build a trivial decl-specifier-seq.  */
6668   clear_decl_specs (&decl_specs);
6669   decl_specs.type = type1;
6670
6671   /* Call grokdeclarator to figure out what type this is.  */
6672   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6673                           /*initialized=*/0, /*attrlist=*/NULL);
6674
6675   if (binary)
6676     {
6677       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6678  
6679       type2 = cp_parser_type_id (parser);
6680
6681       if (type2 == error_mark_node)
6682         return error_mark_node;
6683
6684       /* Build a trivial decl-specifier-seq.  */
6685       clear_decl_specs (&decl_specs);
6686       decl_specs.type = type2;
6687
6688       /* Call grokdeclarator to figure out what type this is.  */
6689       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6690                               /*initialized=*/0, /*attrlist=*/NULL);
6691     }
6692
6693   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6694
6695   /* Complete the trait expression, which may mean either processing
6696      the trait expr now or saving it for template instantiation.  */
6697   return finish_trait_expr (kind, type1, type2);
6698 }
6699
6700 /* Statements [gram.stmt.stmt]  */
6701
6702 /* Parse a statement.
6703
6704    statement:
6705      labeled-statement
6706      expression-statement
6707      compound-statement
6708      selection-statement
6709      iteration-statement
6710      jump-statement
6711      declaration-statement
6712      try-block
6713
6714   IN_COMPOUND is true when the statement is nested inside a
6715   cp_parser_compound_statement; this matters for certain pragmas.
6716
6717   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6718   is a (possibly labeled) if statement which is not enclosed in braces
6719   and has an else clause.  This is used to implement -Wparentheses.  */
6720
6721 static void
6722 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6723                      bool in_compound, bool *if_p)
6724 {
6725   tree statement;
6726   cp_token *token;
6727   location_t statement_location;
6728
6729  restart:
6730   if (if_p != NULL)
6731     *if_p = false;
6732   /* There is no statement yet.  */
6733   statement = NULL_TREE;
6734   /* Peek at the next token.  */
6735   token = cp_lexer_peek_token (parser->lexer);
6736   /* Remember the location of the first token in the statement.  */
6737   statement_location = token->location;
6738   /* If this is a keyword, then that will often determine what kind of
6739      statement we have.  */
6740   if (token->type == CPP_KEYWORD)
6741     {
6742       enum rid keyword = token->keyword;
6743
6744       switch (keyword)
6745         {
6746         case RID_CASE:
6747         case RID_DEFAULT:
6748           /* Looks like a labeled-statement with a case label.
6749              Parse the label, and then use tail recursion to parse
6750              the statement.  */
6751           cp_parser_label_for_labeled_statement (parser);
6752           goto restart;
6753
6754         case RID_IF:
6755         case RID_SWITCH:
6756           statement = cp_parser_selection_statement (parser, if_p);
6757           break;
6758
6759         case RID_WHILE:
6760         case RID_DO:
6761         case RID_FOR:
6762           statement = cp_parser_iteration_statement (parser);
6763           break;
6764
6765         case RID_BREAK:
6766         case RID_CONTINUE:
6767         case RID_RETURN:
6768         case RID_GOTO:
6769           statement = cp_parser_jump_statement (parser);
6770           break;
6771
6772           /* Objective-C++ exception-handling constructs.  */
6773         case RID_AT_TRY:
6774         case RID_AT_CATCH:
6775         case RID_AT_FINALLY:
6776         case RID_AT_SYNCHRONIZED:
6777         case RID_AT_THROW:
6778           statement = cp_parser_objc_statement (parser);
6779           break;
6780
6781         case RID_TRY:
6782           statement = cp_parser_try_block (parser);
6783           break;
6784
6785         case RID_NAMESPACE:
6786           /* This must be a namespace alias definition.  */
6787           cp_parser_declaration_statement (parser);
6788           return;
6789           
6790         default:
6791           /* It might be a keyword like `int' that can start a
6792              declaration-statement.  */
6793           break;
6794         }
6795     }
6796   else if (token->type == CPP_NAME)
6797     {
6798       /* If the next token is a `:', then we are looking at a
6799          labeled-statement.  */
6800       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6801       if (token->type == CPP_COLON)
6802         {
6803           /* Looks like a labeled-statement with an ordinary label.
6804              Parse the label, and then use tail recursion to parse
6805              the statement.  */
6806           cp_parser_label_for_labeled_statement (parser);
6807           goto restart;
6808         }
6809     }
6810   /* Anything that starts with a `{' must be a compound-statement.  */
6811   else if (token->type == CPP_OPEN_BRACE)
6812     statement = cp_parser_compound_statement (parser, NULL, false);
6813   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6814      a statement all its own.  */
6815   else if (token->type == CPP_PRAGMA)
6816     {
6817       /* Only certain OpenMP pragmas are attached to statements, and thus
6818          are considered statements themselves.  All others are not.  In
6819          the context of a compound, accept the pragma as a "statement" and
6820          return so that we can check for a close brace.  Otherwise we
6821          require a real statement and must go back and read one.  */
6822       if (in_compound)
6823         cp_parser_pragma (parser, pragma_compound);
6824       else if (!cp_parser_pragma (parser, pragma_stmt))
6825         goto restart;
6826       return;
6827     }
6828   else if (token->type == CPP_EOF)
6829     {
6830       cp_parser_error (parser, "expected statement");
6831       return;
6832     }
6833
6834   /* Everything else must be a declaration-statement or an
6835      expression-statement.  Try for the declaration-statement
6836      first, unless we are looking at a `;', in which case we know that
6837      we have an expression-statement.  */
6838   if (!statement)
6839     {
6840       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6841         {
6842           cp_parser_parse_tentatively (parser);
6843           /* Try to parse the declaration-statement.  */
6844           cp_parser_declaration_statement (parser);
6845           /* If that worked, we're done.  */
6846           if (cp_parser_parse_definitely (parser))
6847             return;
6848         }
6849       /* Look for an expression-statement instead.  */
6850       statement = cp_parser_expression_statement (parser, in_statement_expr);
6851     }
6852
6853   /* Set the line number for the statement.  */
6854   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6855     SET_EXPR_LOCATION (statement, statement_location);
6856 }
6857
6858 /* Parse the label for a labeled-statement, i.e.
6859
6860    identifier :
6861    case constant-expression :
6862    default :
6863
6864    GNU Extension:
6865    case constant-expression ... constant-expression : statement
6866
6867    When a label is parsed without errors, the label is added to the
6868    parse tree by the finish_* functions, so this function doesn't
6869    have to return the label.  */
6870
6871 static void
6872 cp_parser_label_for_labeled_statement (cp_parser* parser)
6873 {
6874   cp_token *token;
6875
6876   /* The next token should be an identifier.  */
6877   token = cp_lexer_peek_token (parser->lexer);
6878   if (token->type != CPP_NAME
6879       && token->type != CPP_KEYWORD)
6880     {
6881       cp_parser_error (parser, "expected labeled-statement");
6882       return;
6883     }
6884
6885   switch (token->keyword)
6886     {
6887     case RID_CASE:
6888       {
6889         tree expr, expr_hi;
6890         cp_token *ellipsis;
6891
6892         /* Consume the `case' token.  */
6893         cp_lexer_consume_token (parser->lexer);
6894         /* Parse the constant-expression.  */
6895         expr = cp_parser_constant_expression (parser,
6896                                               /*allow_non_constant_p=*/false,
6897                                               NULL);
6898
6899         ellipsis = cp_lexer_peek_token (parser->lexer);
6900         if (ellipsis->type == CPP_ELLIPSIS)
6901           {
6902             /* Consume the `...' token.  */
6903             cp_lexer_consume_token (parser->lexer);
6904             expr_hi =
6905               cp_parser_constant_expression (parser,
6906                                              /*allow_non_constant_p=*/false,
6907                                              NULL);
6908             /* We don't need to emit warnings here, as the common code
6909                will do this for us.  */
6910           }
6911         else
6912           expr_hi = NULL_TREE;
6913
6914         if (parser->in_switch_statement_p)
6915           finish_case_label (expr, expr_hi);
6916         else
6917           error ("%Hcase label %qE not within a switch statement",
6918                  &token->location, expr);
6919       }
6920       break;
6921
6922     case RID_DEFAULT:
6923       /* Consume the `default' token.  */
6924       cp_lexer_consume_token (parser->lexer);
6925
6926       if (parser->in_switch_statement_p)
6927         finish_case_label (NULL_TREE, NULL_TREE);
6928       else
6929         error ("%Hcase label not within a switch statement", &token->location);
6930       break;
6931
6932     default:
6933       /* Anything else must be an ordinary label.  */
6934       finish_label_stmt (cp_parser_identifier (parser));
6935       break;
6936     }
6937
6938   /* Require the `:' token.  */
6939   cp_parser_require (parser, CPP_COLON, "%<:%>");
6940 }
6941
6942 /* Parse an expression-statement.
6943
6944    expression-statement:
6945      expression [opt] ;
6946
6947    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6948    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6949    indicates whether this expression-statement is part of an
6950    expression statement.  */
6951
6952 static tree
6953 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6954 {
6955   tree statement = NULL_TREE;
6956
6957   /* If the next token is a ';', then there is no expression
6958      statement.  */
6959   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6960     statement = cp_parser_expression (parser, /*cast_p=*/false);
6961
6962   /* Consume the final `;'.  */
6963   cp_parser_consume_semicolon_at_end_of_statement (parser);
6964
6965   if (in_statement_expr
6966       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6967     /* This is the final expression statement of a statement
6968        expression.  */
6969     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6970   else if (statement)
6971     statement = finish_expr_stmt (statement);
6972   else
6973     finish_stmt ();
6974
6975   return statement;
6976 }
6977
6978 /* Parse a compound-statement.
6979
6980    compound-statement:
6981      { statement-seq [opt] }
6982
6983    GNU extension:
6984
6985    compound-statement:
6986      { label-declaration-seq [opt] statement-seq [opt] }
6987
6988    label-declaration-seq:
6989      label-declaration
6990      label-declaration-seq label-declaration
6991
6992    Returns a tree representing the statement.  */
6993
6994 static tree
6995 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6996                               bool in_try)
6997 {
6998   tree compound_stmt;
6999
7000   /* Consume the `{'.  */
7001   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7002     return error_mark_node;
7003   /* Begin the compound-statement.  */
7004   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7005   /* If the next keyword is `__label__' we have a label declaration.  */
7006   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7007     cp_parser_label_declaration (parser);
7008   /* Parse an (optional) statement-seq.  */
7009   cp_parser_statement_seq_opt (parser, in_statement_expr);
7010   /* Finish the compound-statement.  */
7011   finish_compound_stmt (compound_stmt);
7012   /* Consume the `}'.  */
7013   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7014
7015   return compound_stmt;
7016 }
7017
7018 /* Parse an (optional) statement-seq.
7019
7020    statement-seq:
7021      statement
7022      statement-seq [opt] statement  */
7023
7024 static void
7025 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7026 {
7027   /* Scan statements until there aren't any more.  */
7028   while (true)
7029     {
7030       cp_token *token = cp_lexer_peek_token (parser->lexer);
7031
7032       /* If we're looking at a `}', then we've run out of statements.  */
7033       if (token->type == CPP_CLOSE_BRACE
7034           || token->type == CPP_EOF
7035           || token->type == CPP_PRAGMA_EOL)
7036         break;
7037       
7038       /* If we are in a compound statement and find 'else' then
7039          something went wrong.  */
7040       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7041         {
7042           if (parser->in_statement & IN_IF_STMT) 
7043             break;
7044           else
7045             {
7046               token = cp_lexer_consume_token (parser->lexer);
7047               error ("%H%<else%> without a previous %<if%>", &token->location);
7048             }
7049         }
7050
7051       /* Parse the statement.  */
7052       cp_parser_statement (parser, in_statement_expr, true, NULL);
7053     }
7054 }
7055
7056 /* Parse a selection-statement.
7057
7058    selection-statement:
7059      if ( condition ) statement
7060      if ( condition ) statement else statement
7061      switch ( condition ) statement
7062
7063    Returns the new IF_STMT or SWITCH_STMT.
7064
7065    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7066    is a (possibly labeled) if statement which is not enclosed in
7067    braces and has an else clause.  This is used to implement
7068    -Wparentheses.  */
7069
7070 static tree
7071 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7072 {
7073   cp_token *token;
7074   enum rid keyword;
7075
7076   if (if_p != NULL)
7077     *if_p = false;
7078
7079   /* Peek at the next token.  */
7080   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7081
7082   /* See what kind of keyword it is.  */
7083   keyword = token->keyword;
7084   switch (keyword)
7085     {
7086     case RID_IF:
7087     case RID_SWITCH:
7088       {
7089         tree statement;
7090         tree condition;
7091
7092         /* Look for the `('.  */
7093         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7094           {
7095             cp_parser_skip_to_end_of_statement (parser);
7096             return error_mark_node;
7097           }
7098
7099         /* Begin the selection-statement.  */
7100         if (keyword == RID_IF)
7101           statement = begin_if_stmt ();
7102         else
7103           statement = begin_switch_stmt ();
7104
7105         /* Parse the condition.  */
7106         condition = cp_parser_condition (parser);
7107         /* Look for the `)'.  */
7108         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7109           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7110                                                  /*consume_paren=*/true);
7111
7112         if (keyword == RID_IF)
7113           {
7114             bool nested_if;
7115             unsigned char in_statement;
7116
7117             /* Add the condition.  */
7118             finish_if_stmt_cond (condition, statement);
7119
7120             /* Parse the then-clause.  */
7121             in_statement = parser->in_statement;
7122             parser->in_statement |= IN_IF_STMT;
7123             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7124             parser->in_statement = in_statement;
7125
7126             finish_then_clause (statement);
7127
7128             /* If the next token is `else', parse the else-clause.  */
7129             if (cp_lexer_next_token_is_keyword (parser->lexer,
7130                                                 RID_ELSE))
7131               {
7132                 /* Consume the `else' keyword.  */
7133                 cp_lexer_consume_token (parser->lexer);
7134                 begin_else_clause (statement);
7135                 /* Parse the else-clause.  */
7136                 cp_parser_implicitly_scoped_statement (parser, NULL);
7137                 finish_else_clause (statement);
7138
7139                 /* If we are currently parsing a then-clause, then
7140                    IF_P will not be NULL.  We set it to true to
7141                    indicate that this if statement has an else clause.
7142                    This may trigger the Wparentheses warning below
7143                    when we get back up to the parent if statement.  */
7144                 if (if_p != NULL)
7145                   *if_p = true;
7146               }
7147             else
7148               {
7149                 /* This if statement does not have an else clause.  If
7150                    NESTED_IF is true, then the then-clause is an if
7151                    statement which does have an else clause.  We warn
7152                    about the potential ambiguity.  */
7153                 if (nested_if)
7154                   warning (OPT_Wparentheses,
7155                            ("%Hsuggest explicit braces "
7156                             "to avoid ambiguous %<else%>"),
7157                            EXPR_LOCUS (statement));
7158               }
7159
7160             /* Now we're all done with the if-statement.  */
7161             finish_if_stmt (statement);
7162           }
7163         else
7164           {
7165             bool in_switch_statement_p;
7166             unsigned char in_statement;
7167
7168             /* Add the condition.  */
7169             finish_switch_cond (condition, statement);
7170
7171             /* Parse the body of the switch-statement.  */
7172             in_switch_statement_p = parser->in_switch_statement_p;
7173             in_statement = parser->in_statement;
7174             parser->in_switch_statement_p = true;
7175             parser->in_statement |= IN_SWITCH_STMT;
7176             cp_parser_implicitly_scoped_statement (parser, NULL);
7177             parser->in_switch_statement_p = in_switch_statement_p;
7178             parser->in_statement = in_statement;
7179
7180             /* Now we're all done with the switch-statement.  */
7181             finish_switch_stmt (statement);
7182           }
7183
7184         return statement;
7185       }
7186       break;
7187
7188     default:
7189       cp_parser_error (parser, "expected selection-statement");
7190       return error_mark_node;
7191     }
7192 }
7193
7194 /* Parse a condition.
7195
7196    condition:
7197      expression
7198      type-specifier-seq declarator = initializer-clause
7199      type-specifier-seq declarator braced-init-list
7200
7201    GNU Extension:
7202
7203    condition:
7204      type-specifier-seq declarator asm-specification [opt]
7205        attributes [opt] = assignment-expression
7206
7207    Returns the expression that should be tested.  */
7208
7209 static tree
7210 cp_parser_condition (cp_parser* parser)
7211 {
7212   cp_decl_specifier_seq type_specifiers;
7213   const char *saved_message;
7214
7215   /* Try the declaration first.  */
7216   cp_parser_parse_tentatively (parser);
7217   /* New types are not allowed in the type-specifier-seq for a
7218      condition.  */
7219   saved_message = parser->type_definition_forbidden_message;
7220   parser->type_definition_forbidden_message
7221     = "types may not be defined in conditions";
7222   /* Parse the type-specifier-seq.  */
7223   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7224                                 &type_specifiers);
7225   /* Restore the saved message.  */
7226   parser->type_definition_forbidden_message = saved_message;
7227   /* If all is well, we might be looking at a declaration.  */
7228   if (!cp_parser_error_occurred (parser))
7229     {
7230       tree decl;
7231       tree asm_specification;
7232       tree attributes;
7233       cp_declarator *declarator;
7234       tree initializer = NULL_TREE;
7235
7236       /* Parse the declarator.  */
7237       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7238                                          /*ctor_dtor_or_conv_p=*/NULL,
7239                                          /*parenthesized_p=*/NULL,
7240                                          /*member_p=*/false);
7241       /* Parse the attributes.  */
7242       attributes = cp_parser_attributes_opt (parser);
7243       /* Parse the asm-specification.  */
7244       asm_specification = cp_parser_asm_specification_opt (parser);
7245       /* If the next token is not an `=' or '{', then we might still be
7246          looking at an expression.  For example:
7247
7248            if (A(a).x)
7249
7250          looks like a decl-specifier-seq and a declarator -- but then
7251          there is no `=', so this is an expression.  */
7252       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7253           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7254         cp_parser_simulate_error (parser);
7255         
7256       /* If we did see an `=' or '{', then we are looking at a declaration
7257          for sure.  */
7258       if (cp_parser_parse_definitely (parser))
7259         {
7260           tree pushed_scope;
7261           bool non_constant_p;
7262           bool flags = LOOKUP_ONLYCONVERTING;
7263
7264           /* Create the declaration.  */
7265           decl = start_decl (declarator, &type_specifiers,
7266                              /*initialized_p=*/true,
7267                              attributes, /*prefix_attributes=*/NULL_TREE,
7268                              &pushed_scope);
7269
7270           /* Parse the initializer.  */
7271           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7272             {
7273               initializer = cp_parser_braced_list (parser, &non_constant_p);
7274               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7275               flags = 0;
7276             }
7277           else
7278             {
7279               /* Consume the `='.  */
7280               cp_lexer_consume_token (parser->lexer);
7281               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7282             }
7283           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7284             maybe_warn_cpp0x ("extended initializer lists");
7285
7286           if (!non_constant_p)
7287             initializer = fold_non_dependent_expr (initializer);
7288
7289           /* Process the initializer.  */
7290           cp_finish_decl (decl,
7291                           initializer, !non_constant_p,
7292                           asm_specification,
7293                           flags);
7294
7295           if (pushed_scope)
7296             pop_scope (pushed_scope);
7297
7298           return convert_from_reference (decl);
7299         }
7300     }
7301   /* If we didn't even get past the declarator successfully, we are
7302      definitely not looking at a declaration.  */
7303   else
7304     cp_parser_abort_tentative_parse (parser);
7305
7306   /* Otherwise, we are looking at an expression.  */
7307   return cp_parser_expression (parser, /*cast_p=*/false);
7308 }
7309
7310 /* We check for a ) immediately followed by ; with no whitespacing
7311    between.  This is used to issue a warning for:
7312
7313      while (...);
7314
7315    and:
7316
7317      for (...);
7318
7319    as the semicolon is probably extraneous.
7320
7321    On parse errors, the next token might not be a ), so do nothing in
7322    that case. */
7323
7324 static void
7325 check_empty_body (cp_parser* parser, const char* type)
7326 {
7327   cp_token *token;
7328   cp_token *close_paren;
7329   expanded_location close_loc;
7330   expanded_location semi_loc;
7331   
7332   close_paren = cp_lexer_peek_token (parser->lexer);
7333   if (close_paren->type != CPP_CLOSE_PAREN)
7334     return;
7335
7336   close_loc = expand_location (close_paren->location);
7337   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7338
7339   if (token->type != CPP_SEMICOLON
7340       || (token->flags & PREV_WHITE))
7341     return;
7342
7343   semi_loc =  expand_location (token->location);
7344   if (close_loc.line == semi_loc.line
7345       && close_loc.column+1 == semi_loc.column)
7346     warning (OPT_Wempty_body,
7347              "suggest a space before %<;%> or explicit braces around empty "
7348              "body in %<%s%> statement",
7349              type);
7350 }
7351
7352 /* Parse an iteration-statement.
7353
7354    iteration-statement:
7355      while ( condition ) statement
7356      do statement while ( expression ) ;
7357      for ( for-init-statement condition [opt] ; expression [opt] )
7358        statement
7359
7360    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7361
7362 static tree
7363 cp_parser_iteration_statement (cp_parser* parser)
7364 {
7365   cp_token *token;
7366   enum rid keyword;
7367   tree statement;
7368   unsigned char in_statement;
7369
7370   /* Peek at the next token.  */
7371   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7372   if (!token)
7373     return error_mark_node;
7374
7375   /* Remember whether or not we are already within an iteration
7376      statement.  */
7377   in_statement = parser->in_statement;
7378
7379   /* See what kind of keyword it is.  */
7380   keyword = token->keyword;
7381   switch (keyword)
7382     {
7383     case RID_WHILE:
7384       {
7385         tree condition;
7386
7387         /* Begin the while-statement.  */
7388         statement = begin_while_stmt ();
7389         /* Look for the `('.  */
7390         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7391         /* Parse the condition.  */
7392         condition = cp_parser_condition (parser);
7393         finish_while_stmt_cond (condition, statement);
7394         check_empty_body (parser, "while");
7395         /* Look for the `)'.  */
7396         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7397         /* Parse the dependent statement.  */
7398         parser->in_statement = IN_ITERATION_STMT;
7399         cp_parser_already_scoped_statement (parser);
7400         parser->in_statement = in_statement;
7401         /* We're done with the while-statement.  */
7402         finish_while_stmt (statement);
7403       }
7404       break;
7405
7406     case RID_DO:
7407       {
7408         tree expression;
7409
7410         /* Begin the do-statement.  */
7411         statement = begin_do_stmt ();
7412         /* Parse the body of the do-statement.  */
7413         parser->in_statement = IN_ITERATION_STMT;
7414         cp_parser_implicitly_scoped_statement (parser, NULL);
7415         parser->in_statement = in_statement;
7416         finish_do_body (statement);
7417         /* Look for the `while' keyword.  */
7418         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7419         /* Look for the `('.  */
7420         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7421         /* Parse the expression.  */
7422         expression = cp_parser_expression (parser, /*cast_p=*/false);
7423         /* We're done with the do-statement.  */
7424         finish_do_stmt (expression, statement);
7425         /* Look for the `)'.  */
7426         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7427         /* Look for the `;'.  */
7428         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7429       }
7430       break;
7431
7432     case RID_FOR:
7433       {
7434         tree condition = NULL_TREE;
7435         tree expression = NULL_TREE;
7436
7437         /* Begin the for-statement.  */
7438         statement = begin_for_stmt ();
7439         /* Look for the `('.  */
7440         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7441         /* Parse the initialization.  */
7442         cp_parser_for_init_statement (parser);
7443         finish_for_init_stmt (statement);
7444
7445         /* If there's a condition, process it.  */
7446         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7447           condition = cp_parser_condition (parser);
7448         finish_for_cond (condition, statement);
7449         /* Look for the `;'.  */
7450         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7451
7452         /* If there's an expression, process it.  */
7453         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7454           expression = cp_parser_expression (parser, /*cast_p=*/false);
7455         finish_for_expr (expression, statement);
7456         check_empty_body (parser, "for");
7457         /* Look for the `)'.  */
7458         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7459
7460         /* Parse the body of the for-statement.  */
7461         parser->in_statement = IN_ITERATION_STMT;
7462         cp_parser_already_scoped_statement (parser);
7463         parser->in_statement = in_statement;
7464
7465         /* We're done with the for-statement.  */
7466         finish_for_stmt (statement);
7467       }
7468       break;
7469
7470     default:
7471       cp_parser_error (parser, "expected iteration-statement");
7472       statement = error_mark_node;
7473       break;
7474     }
7475
7476   return statement;
7477 }
7478
7479 /* Parse a for-init-statement.
7480
7481    for-init-statement:
7482      expression-statement
7483      simple-declaration  */
7484
7485 static void
7486 cp_parser_for_init_statement (cp_parser* parser)
7487 {
7488   /* If the next token is a `;', then we have an empty
7489      expression-statement.  Grammatically, this is also a
7490      simple-declaration, but an invalid one, because it does not
7491      declare anything.  Therefore, if we did not handle this case
7492      specially, we would issue an error message about an invalid
7493      declaration.  */
7494   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7495     {
7496       /* We're going to speculatively look for a declaration, falling back
7497          to an expression, if necessary.  */
7498       cp_parser_parse_tentatively (parser);
7499       /* Parse the declaration.  */
7500       cp_parser_simple_declaration (parser,
7501                                     /*function_definition_allowed_p=*/false);
7502       /* If the tentative parse failed, then we shall need to look for an
7503          expression-statement.  */
7504       if (cp_parser_parse_definitely (parser))
7505         return;
7506     }
7507
7508   cp_parser_expression_statement (parser, false);
7509 }
7510
7511 /* Parse a jump-statement.
7512
7513    jump-statement:
7514      break ;
7515      continue ;
7516      return expression [opt] ;
7517      return braced-init-list ;
7518      goto identifier ;
7519
7520    GNU extension:
7521
7522    jump-statement:
7523      goto * expression ;
7524
7525    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7526
7527 static tree
7528 cp_parser_jump_statement (cp_parser* parser)
7529 {
7530   tree statement = error_mark_node;
7531   cp_token *token;
7532   enum rid keyword;
7533   unsigned char in_statement;
7534
7535   /* Peek at the next token.  */
7536   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7537   if (!token)
7538     return error_mark_node;
7539
7540   /* See what kind of keyword it is.  */
7541   keyword = token->keyword;
7542   switch (keyword)
7543     {
7544     case RID_BREAK:
7545       in_statement = parser->in_statement & ~IN_IF_STMT;      
7546       switch (in_statement)
7547         {
7548         case 0:
7549           error ("%Hbreak statement not within loop or switch", &token->location);
7550           break;
7551         default:
7552           gcc_assert ((in_statement & IN_SWITCH_STMT)
7553                       || in_statement == IN_ITERATION_STMT);
7554           statement = finish_break_stmt ();
7555           break;
7556         case IN_OMP_BLOCK:
7557           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7558           break;
7559         case IN_OMP_FOR:
7560           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7561           break;
7562         }
7563       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7564       break;
7565
7566     case RID_CONTINUE:
7567       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7568         {
7569         case 0:
7570           error ("%Hcontinue statement not within a loop", &token->location);
7571           break;
7572         case IN_ITERATION_STMT:
7573         case IN_OMP_FOR:
7574           statement = finish_continue_stmt ();
7575           break;
7576         case IN_OMP_BLOCK:
7577           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7578           break;
7579         default:
7580           gcc_unreachable ();
7581         }
7582       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7583       break;
7584
7585     case RID_RETURN:
7586       {
7587         tree expr;
7588         bool expr_non_constant_p;
7589
7590         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7591           {
7592             maybe_warn_cpp0x ("extended initializer lists");
7593             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7594           }
7595         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7596           expr = cp_parser_expression (parser, /*cast_p=*/false);
7597         else
7598           /* If the next token is a `;', then there is no
7599              expression.  */
7600           expr = NULL_TREE;
7601         /* Build the return-statement.  */
7602         statement = finish_return_stmt (expr);
7603         /* Look for the final `;'.  */
7604         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7605       }
7606       break;
7607
7608     case RID_GOTO:
7609       /* Create the goto-statement.  */
7610       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7611         {
7612           /* Issue a warning about this use of a GNU extension.  */
7613           if (pedantic)
7614             pedwarn ("%HISO C++ forbids computed gotos", &token->location);
7615           /* Consume the '*' token.  */
7616           cp_lexer_consume_token (parser->lexer);
7617           /* Parse the dependent expression.  */
7618           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7619         }
7620       else
7621         finish_goto_stmt (cp_parser_identifier (parser));
7622       /* Look for the final `;'.  */
7623       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7624       break;
7625
7626     default:
7627       cp_parser_error (parser, "expected jump-statement");
7628       break;
7629     }
7630
7631   return statement;
7632 }
7633
7634 /* Parse a declaration-statement.
7635
7636    declaration-statement:
7637      block-declaration  */
7638
7639 static void
7640 cp_parser_declaration_statement (cp_parser* parser)
7641 {
7642   void *p;
7643
7644   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7645   p = obstack_alloc (&declarator_obstack, 0);
7646
7647  /* Parse the block-declaration.  */
7648   cp_parser_block_declaration (parser, /*statement_p=*/true);
7649
7650   /* Free any declarators allocated.  */
7651   obstack_free (&declarator_obstack, p);
7652
7653   /* Finish off the statement.  */
7654   finish_stmt ();
7655 }
7656
7657 /* Some dependent statements (like `if (cond) statement'), are
7658    implicitly in their own scope.  In other words, if the statement is
7659    a single statement (as opposed to a compound-statement), it is
7660    none-the-less treated as if it were enclosed in braces.  Any
7661    declarations appearing in the dependent statement are out of scope
7662    after control passes that point.  This function parses a statement,
7663    but ensures that is in its own scope, even if it is not a
7664    compound-statement.
7665
7666    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7667    is a (possibly labeled) if statement which is not enclosed in
7668    braces and has an else clause.  This is used to implement
7669    -Wparentheses.
7670
7671    Returns the new statement.  */
7672
7673 static tree
7674 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7675 {
7676   tree statement;
7677
7678   if (if_p != NULL)
7679     *if_p = false;
7680
7681   /* Mark if () ; with a special NOP_EXPR.  */
7682   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7683     {
7684       cp_lexer_consume_token (parser->lexer);
7685       statement = add_stmt (build_empty_stmt ());
7686     }
7687   /* if a compound is opened, we simply parse the statement directly.  */
7688   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7689     statement = cp_parser_compound_statement (parser, NULL, false);
7690   /* If the token is not a `{', then we must take special action.  */
7691   else
7692     {
7693       /* Create a compound-statement.  */
7694       statement = begin_compound_stmt (0);
7695       /* Parse the dependent-statement.  */
7696       cp_parser_statement (parser, NULL_TREE, false, if_p);
7697       /* Finish the dummy compound-statement.  */
7698       finish_compound_stmt (statement);
7699     }
7700
7701   /* Return the statement.  */
7702   return statement;
7703 }
7704
7705 /* For some dependent statements (like `while (cond) statement'), we
7706    have already created a scope.  Therefore, even if the dependent
7707    statement is a compound-statement, we do not want to create another
7708    scope.  */
7709
7710 static void
7711 cp_parser_already_scoped_statement (cp_parser* parser)
7712 {
7713   /* If the token is a `{', then we must take special action.  */
7714   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7715     cp_parser_statement (parser, NULL_TREE, false, NULL);
7716   else
7717     {
7718       /* Avoid calling cp_parser_compound_statement, so that we
7719          don't create a new scope.  Do everything else by hand.  */
7720       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7721       cp_parser_statement_seq_opt (parser, NULL_TREE);
7722       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7723     }
7724 }
7725
7726 /* Declarations [gram.dcl.dcl] */
7727
7728 /* Parse an optional declaration-sequence.
7729
7730    declaration-seq:
7731      declaration
7732      declaration-seq declaration  */
7733
7734 static void
7735 cp_parser_declaration_seq_opt (cp_parser* parser)
7736 {
7737   while (true)
7738     {
7739       cp_token *token;
7740
7741       token = cp_lexer_peek_token (parser->lexer);
7742
7743       if (token->type == CPP_CLOSE_BRACE
7744           || token->type == CPP_EOF
7745           || token->type == CPP_PRAGMA_EOL)
7746         break;
7747
7748       if (token->type == CPP_SEMICOLON)
7749         {
7750           /* A declaration consisting of a single semicolon is
7751              invalid.  Allow it unless we're being pedantic.  */
7752           cp_lexer_consume_token (parser->lexer);
7753           if (pedantic && !in_system_header)
7754             pedwarn ("extra %<;%>");
7755           continue;
7756         }
7757
7758       /* If we're entering or exiting a region that's implicitly
7759          extern "C", modify the lang context appropriately.  */
7760       if (!parser->implicit_extern_c && token->implicit_extern_c)
7761         {
7762           push_lang_context (lang_name_c);
7763           parser->implicit_extern_c = true;
7764         }
7765       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7766         {
7767           pop_lang_context ();
7768           parser->implicit_extern_c = false;
7769         }
7770
7771       if (token->type == CPP_PRAGMA)
7772         {
7773           /* A top-level declaration can consist solely of a #pragma.
7774              A nested declaration cannot, so this is done here and not
7775              in cp_parser_declaration.  (A #pragma at block scope is
7776              handled in cp_parser_statement.)  */
7777           cp_parser_pragma (parser, pragma_external);
7778           continue;
7779         }
7780
7781       /* Parse the declaration itself.  */
7782       cp_parser_declaration (parser);
7783     }
7784 }
7785
7786 /* Parse a declaration.
7787
7788    declaration:
7789      block-declaration
7790      function-definition
7791      template-declaration
7792      explicit-instantiation
7793      explicit-specialization
7794      linkage-specification
7795      namespace-definition
7796
7797    GNU extension:
7798
7799    declaration:
7800       __extension__ declaration */
7801
7802 static void
7803 cp_parser_declaration (cp_parser* parser)
7804 {
7805   cp_token token1;
7806   cp_token token2;
7807   int saved_pedantic;
7808   void *p;
7809
7810   /* Check for the `__extension__' keyword.  */
7811   if (cp_parser_extension_opt (parser, &saved_pedantic))
7812     {
7813       /* Parse the qualified declaration.  */
7814       cp_parser_declaration (parser);
7815       /* Restore the PEDANTIC flag.  */
7816       pedantic = saved_pedantic;
7817
7818       return;
7819     }
7820
7821   /* Try to figure out what kind of declaration is present.  */
7822   token1 = *cp_lexer_peek_token (parser->lexer);
7823
7824   if (token1.type != CPP_EOF)
7825     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7826   else
7827     {
7828       token2.type = CPP_EOF;
7829       token2.keyword = RID_MAX;
7830     }
7831
7832   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7833   p = obstack_alloc (&declarator_obstack, 0);
7834
7835   /* If the next token is `extern' and the following token is a string
7836      literal, then we have a linkage specification.  */
7837   if (token1.keyword == RID_EXTERN
7838       && cp_parser_is_string_literal (&token2))
7839     cp_parser_linkage_specification (parser);
7840   /* If the next token is `template', then we have either a template
7841      declaration, an explicit instantiation, or an explicit
7842      specialization.  */
7843   else if (token1.keyword == RID_TEMPLATE)
7844     {
7845       /* `template <>' indicates a template specialization.  */
7846       if (token2.type == CPP_LESS
7847           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7848         cp_parser_explicit_specialization (parser);
7849       /* `template <' indicates a template declaration.  */
7850       else if (token2.type == CPP_LESS)
7851         cp_parser_template_declaration (parser, /*member_p=*/false);
7852       /* Anything else must be an explicit instantiation.  */
7853       else
7854         cp_parser_explicit_instantiation (parser);
7855     }
7856   /* If the next token is `export', then we have a template
7857      declaration.  */
7858   else if (token1.keyword == RID_EXPORT)
7859     cp_parser_template_declaration (parser, /*member_p=*/false);
7860   /* If the next token is `extern', 'static' or 'inline' and the one
7861      after that is `template', we have a GNU extended explicit
7862      instantiation directive.  */
7863   else if (cp_parser_allow_gnu_extensions_p (parser)
7864            && (token1.keyword == RID_EXTERN
7865                || token1.keyword == RID_STATIC
7866                || token1.keyword == RID_INLINE)
7867            && token2.keyword == RID_TEMPLATE)
7868     cp_parser_explicit_instantiation (parser);
7869   /* If the next token is `namespace', check for a named or unnamed
7870      namespace definition.  */
7871   else if (token1.keyword == RID_NAMESPACE
7872            && (/* A named namespace definition.  */
7873                (token2.type == CPP_NAME
7874                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7875                     != CPP_EQ))
7876                /* An unnamed namespace definition.  */
7877                || token2.type == CPP_OPEN_BRACE
7878                || token2.keyword == RID_ATTRIBUTE))
7879     cp_parser_namespace_definition (parser);
7880   /* An inline (associated) namespace definition.  */
7881   else if (token1.keyword == RID_INLINE
7882            && token2.keyword == RID_NAMESPACE)
7883     cp_parser_namespace_definition (parser);
7884   /* Objective-C++ declaration/definition.  */
7885   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7886     cp_parser_objc_declaration (parser);
7887   /* We must have either a block declaration or a function
7888      definition.  */
7889   else
7890     /* Try to parse a block-declaration, or a function-definition.  */
7891     cp_parser_block_declaration (parser, /*statement_p=*/false);
7892
7893   /* Free any declarators allocated.  */
7894   obstack_free (&declarator_obstack, p);
7895 }
7896
7897 /* Parse a block-declaration.
7898
7899    block-declaration:
7900      simple-declaration
7901      asm-definition
7902      namespace-alias-definition
7903      using-declaration
7904      using-directive
7905
7906    GNU Extension:
7907
7908    block-declaration:
7909      __extension__ block-declaration
7910
7911    C++0x Extension:
7912
7913    block-declaration:
7914      static_assert-declaration
7915
7916    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7917    part of a declaration-statement.  */
7918
7919 static void
7920 cp_parser_block_declaration (cp_parser *parser,
7921                              bool      statement_p)
7922 {
7923   cp_token *token1;
7924   int saved_pedantic;
7925
7926   /* Check for the `__extension__' keyword.  */
7927   if (cp_parser_extension_opt (parser, &saved_pedantic))
7928     {
7929       /* Parse the qualified declaration.  */
7930       cp_parser_block_declaration (parser, statement_p);
7931       /* Restore the PEDANTIC flag.  */
7932       pedantic = saved_pedantic;
7933
7934       return;
7935     }
7936
7937   /* Peek at the next token to figure out which kind of declaration is
7938      present.  */
7939   token1 = cp_lexer_peek_token (parser->lexer);
7940
7941   /* If the next keyword is `asm', we have an asm-definition.  */
7942   if (token1->keyword == RID_ASM)
7943     {
7944       if (statement_p)
7945         cp_parser_commit_to_tentative_parse (parser);
7946       cp_parser_asm_definition (parser);
7947     }
7948   /* If the next keyword is `namespace', we have a
7949      namespace-alias-definition.  */
7950   else if (token1->keyword == RID_NAMESPACE)
7951     cp_parser_namespace_alias_definition (parser);
7952   /* If the next keyword is `using', we have either a
7953      using-declaration or a using-directive.  */
7954   else if (token1->keyword == RID_USING)
7955     {
7956       cp_token *token2;
7957
7958       if (statement_p)
7959         cp_parser_commit_to_tentative_parse (parser);
7960       /* If the token after `using' is `namespace', then we have a
7961          using-directive.  */
7962       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7963       if (token2->keyword == RID_NAMESPACE)
7964         cp_parser_using_directive (parser);
7965       /* Otherwise, it's a using-declaration.  */
7966       else
7967         cp_parser_using_declaration (parser,
7968                                      /*access_declaration_p=*/false);
7969     }
7970   /* If the next keyword is `__label__' we have a misplaced label
7971      declaration.  */
7972   else if (token1->keyword == RID_LABEL)
7973     {
7974       cp_lexer_consume_token (parser->lexer);
7975       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7976       cp_parser_skip_to_end_of_statement (parser);
7977       /* If the next token is now a `;', consume it.  */
7978       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7979         cp_lexer_consume_token (parser->lexer);
7980     }
7981   /* If the next token is `static_assert' we have a static assertion.  */
7982   else if (token1->keyword == RID_STATIC_ASSERT)
7983     cp_parser_static_assert (parser, /*member_p=*/false);
7984   /* Anything else must be a simple-declaration.  */
7985   else
7986     cp_parser_simple_declaration (parser, !statement_p);
7987 }
7988
7989 /* Parse a simple-declaration.
7990
7991    simple-declaration:
7992      decl-specifier-seq [opt] init-declarator-list [opt] ;
7993
7994    init-declarator-list:
7995      init-declarator
7996      init-declarator-list , init-declarator
7997
7998    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7999    function-definition as a simple-declaration.  */
8000
8001 static void
8002 cp_parser_simple_declaration (cp_parser* parser,
8003                               bool function_definition_allowed_p)
8004 {
8005   cp_decl_specifier_seq decl_specifiers;
8006   int declares_class_or_enum;
8007   bool saw_declarator;
8008
8009   /* Defer access checks until we know what is being declared; the
8010      checks for names appearing in the decl-specifier-seq should be
8011      done as if we were in the scope of the thing being declared.  */
8012   push_deferring_access_checks (dk_deferred);
8013
8014   /* Parse the decl-specifier-seq.  We have to keep track of whether
8015      or not the decl-specifier-seq declares a named class or
8016      enumeration type, since that is the only case in which the
8017      init-declarator-list is allowed to be empty.
8018
8019      [dcl.dcl]
8020
8021      In a simple-declaration, the optional init-declarator-list can be
8022      omitted only when declaring a class or enumeration, that is when
8023      the decl-specifier-seq contains either a class-specifier, an
8024      elaborated-type-specifier, or an enum-specifier.  */
8025   cp_parser_decl_specifier_seq (parser,
8026                                 CP_PARSER_FLAGS_OPTIONAL,
8027                                 &decl_specifiers,
8028                                 &declares_class_or_enum);
8029   /* We no longer need to defer access checks.  */
8030   stop_deferring_access_checks ();
8031
8032   /* In a block scope, a valid declaration must always have a
8033      decl-specifier-seq.  By not trying to parse declarators, we can
8034      resolve the declaration/expression ambiguity more quickly.  */
8035   if (!function_definition_allowed_p
8036       && !decl_specifiers.any_specifiers_p)
8037     {
8038       cp_parser_error (parser, "expected declaration");
8039       goto done;
8040     }
8041
8042   /* If the next two tokens are both identifiers, the code is
8043      erroneous. The usual cause of this situation is code like:
8044
8045        T t;
8046
8047      where "T" should name a type -- but does not.  */
8048   if (!decl_specifiers.type
8049       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8050     {
8051       /* If parsing tentatively, we should commit; we really are
8052          looking at a declaration.  */
8053       cp_parser_commit_to_tentative_parse (parser);
8054       /* Give up.  */
8055       goto done;
8056     }
8057
8058   /* If we have seen at least one decl-specifier, and the next token
8059      is not a parenthesis, then we must be looking at a declaration.
8060      (After "int (" we might be looking at a functional cast.)  */
8061   if (decl_specifiers.any_specifiers_p
8062       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8063       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8064     cp_parser_commit_to_tentative_parse (parser);
8065
8066   /* Keep going until we hit the `;' at the end of the simple
8067      declaration.  */
8068   saw_declarator = false;
8069   while (cp_lexer_next_token_is_not (parser->lexer,
8070                                      CPP_SEMICOLON))
8071     {
8072       cp_token *token;
8073       bool function_definition_p;
8074       tree decl;
8075
8076       if (saw_declarator)
8077         {
8078           /* If we are processing next declarator, coma is expected */
8079           token = cp_lexer_peek_token (parser->lexer);
8080           gcc_assert (token->type == CPP_COMMA);
8081           cp_lexer_consume_token (parser->lexer);
8082         }
8083       else
8084         saw_declarator = true;
8085
8086       /* Parse the init-declarator.  */
8087       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8088                                         /*checks=*/NULL,
8089                                         function_definition_allowed_p,
8090                                         /*member_p=*/false,
8091                                         declares_class_or_enum,
8092                                         &function_definition_p);
8093       /* If an error occurred while parsing tentatively, exit quickly.
8094          (That usually happens when in the body of a function; each
8095          statement is treated as a declaration-statement until proven
8096          otherwise.)  */
8097       if (cp_parser_error_occurred (parser))
8098         goto done;
8099       /* Handle function definitions specially.  */
8100       if (function_definition_p)
8101         {
8102           /* If the next token is a `,', then we are probably
8103              processing something like:
8104
8105                void f() {}, *p;
8106
8107              which is erroneous.  */
8108           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8109             {
8110               cp_token *token = cp_lexer_peek_token (parser->lexer);
8111               error ("%Hmixing declarations and function-definitions is forbidden",
8112                      &token->location);
8113             }
8114           /* Otherwise, we're done with the list of declarators.  */
8115           else
8116             {
8117               pop_deferring_access_checks ();
8118               return;
8119             }
8120         }
8121       /* The next token should be either a `,' or a `;'.  */
8122       token = cp_lexer_peek_token (parser->lexer);
8123       /* If it's a `,', there are more declarators to come.  */
8124       if (token->type == CPP_COMMA)
8125         /* will be consumed next time around */;
8126       /* If it's a `;', we are done.  */
8127       else if (token->type == CPP_SEMICOLON)
8128         break;
8129       /* Anything else is an error.  */
8130       else
8131         {
8132           /* If we have already issued an error message we don't need
8133              to issue another one.  */
8134           if (decl != error_mark_node
8135               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8136             cp_parser_error (parser, "expected %<,%> or %<;%>");
8137           /* Skip tokens until we reach the end of the statement.  */
8138           cp_parser_skip_to_end_of_statement (parser);
8139           /* If the next token is now a `;', consume it.  */
8140           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8141             cp_lexer_consume_token (parser->lexer);
8142           goto done;
8143         }
8144       /* After the first time around, a function-definition is not
8145          allowed -- even if it was OK at first.  For example:
8146
8147            int i, f() {}
8148
8149          is not valid.  */
8150       function_definition_allowed_p = false;
8151     }
8152
8153   /* Issue an error message if no declarators are present, and the
8154      decl-specifier-seq does not itself declare a class or
8155      enumeration.  */
8156   if (!saw_declarator)
8157     {
8158       if (cp_parser_declares_only_class_p (parser))
8159         shadow_tag (&decl_specifiers);
8160       /* Perform any deferred access checks.  */
8161       perform_deferred_access_checks ();
8162     }
8163
8164   /* Consume the `;'.  */
8165   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8166
8167  done:
8168   pop_deferring_access_checks ();
8169 }
8170
8171 /* Parse a decl-specifier-seq.
8172
8173    decl-specifier-seq:
8174      decl-specifier-seq [opt] decl-specifier
8175
8176    decl-specifier:
8177      storage-class-specifier
8178      type-specifier
8179      function-specifier
8180      friend
8181      typedef
8182
8183    GNU Extension:
8184
8185    decl-specifier:
8186      attributes
8187
8188    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8189
8190    The parser flags FLAGS is used to control type-specifier parsing.
8191
8192    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8193    flags:
8194
8195      1: one of the decl-specifiers is an elaborated-type-specifier
8196         (i.e., a type declaration)
8197      2: one of the decl-specifiers is an enum-specifier or a
8198         class-specifier (i.e., a type definition)
8199
8200    */
8201
8202 static void
8203 cp_parser_decl_specifier_seq (cp_parser* parser,
8204                               cp_parser_flags flags,
8205                               cp_decl_specifier_seq *decl_specs,
8206                               int* declares_class_or_enum)
8207 {
8208   bool constructor_possible_p = !parser->in_declarator_p;
8209   cp_token *start_token = NULL;
8210
8211   /* Clear DECL_SPECS.  */
8212   clear_decl_specs (decl_specs);
8213
8214   /* Assume no class or enumeration type is declared.  */
8215   *declares_class_or_enum = 0;
8216
8217   /* Keep reading specifiers until there are no more to read.  */
8218   while (true)
8219     {
8220       bool constructor_p;
8221       bool found_decl_spec;
8222       cp_token *token;
8223
8224       /* Peek at the next token.  */
8225       token = cp_lexer_peek_token (parser->lexer);
8226
8227       /* Save the first token of the decl spec list for error
8228          reporting.  */
8229       if (!start_token)
8230         start_token = token;
8231       /* Handle attributes.  */
8232       if (token->keyword == RID_ATTRIBUTE)
8233         {
8234           /* Parse the attributes.  */
8235           decl_specs->attributes
8236             = chainon (decl_specs->attributes,
8237                        cp_parser_attributes_opt (parser));
8238           continue;
8239         }
8240       /* Assume we will find a decl-specifier keyword.  */
8241       found_decl_spec = true;
8242       /* If the next token is an appropriate keyword, we can simply
8243          add it to the list.  */
8244       switch (token->keyword)
8245         {
8246           /* decl-specifier:
8247                friend  */
8248         case RID_FRIEND:
8249           if (!at_class_scope_p ())
8250             {
8251               error ("%H%<friend%> used outside of class", &token->location);
8252               cp_lexer_purge_token (parser->lexer);
8253             }
8254           else
8255             {
8256               ++decl_specs->specs[(int) ds_friend];
8257               /* Consume the token.  */
8258               cp_lexer_consume_token (parser->lexer);
8259             }
8260           break;
8261
8262           /* function-specifier:
8263                inline
8264                virtual
8265                explicit  */
8266         case RID_INLINE:
8267         case RID_VIRTUAL:
8268         case RID_EXPLICIT:
8269           cp_parser_function_specifier_opt (parser, decl_specs);
8270           break;
8271
8272           /* decl-specifier:
8273                typedef  */
8274         case RID_TYPEDEF:
8275           ++decl_specs->specs[(int) ds_typedef];
8276           /* Consume the token.  */
8277           cp_lexer_consume_token (parser->lexer);
8278           /* A constructor declarator cannot appear in a typedef.  */
8279           constructor_possible_p = false;
8280           /* The "typedef" keyword can only occur in a declaration; we
8281              may as well commit at this point.  */
8282           cp_parser_commit_to_tentative_parse (parser);
8283
8284           if (decl_specs->storage_class != sc_none)
8285             decl_specs->conflicting_specifiers_p = true;
8286           break;
8287
8288           /* storage-class-specifier:
8289                auto
8290                register
8291                static
8292                extern
8293                mutable
8294
8295              GNU Extension:
8296                thread  */
8297         case RID_AUTO:
8298           /* Consume the token.  */
8299           cp_lexer_consume_token (parser->lexer);
8300
8301           if (cxx_dialect == cxx98) 
8302             {
8303               /* Complain about `auto' as a storage specifier, if
8304                  we're complaining about C++0x compatibility.  */
8305               warning 
8306                 (OPT_Wc__0x_compat, 
8307                  "%H%<auto%> will change meaning in C++0x; please remove it",
8308                  &token->location);
8309
8310               /* Set the storage class anyway.  */
8311               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8312                                            token->location);
8313             }
8314           else 
8315             /* We do not yet support the use of `auto' as a
8316                type-specifier.  */
8317             error ("%HC++0x %<auto%> specifier not supported", &token->location);
8318           break;
8319
8320         case RID_REGISTER:
8321         case RID_STATIC:
8322         case RID_EXTERN:
8323         case RID_MUTABLE:
8324           /* Consume the token.  */
8325           cp_lexer_consume_token (parser->lexer);
8326           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8327                                        token->location);
8328           break;
8329         case RID_THREAD:
8330           /* Consume the token.  */
8331           cp_lexer_consume_token (parser->lexer);
8332           ++decl_specs->specs[(int) ds_thread];
8333           break;
8334
8335         default:
8336           /* We did not yet find a decl-specifier yet.  */
8337           found_decl_spec = false;
8338           break;
8339         }
8340
8341       /* Constructors are a special case.  The `S' in `S()' is not a
8342          decl-specifier; it is the beginning of the declarator.  */
8343       constructor_p
8344         = (!found_decl_spec
8345            && constructor_possible_p
8346            && (cp_parser_constructor_declarator_p
8347                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8348
8349       /* If we don't have a DECL_SPEC yet, then we must be looking at
8350          a type-specifier.  */
8351       if (!found_decl_spec && !constructor_p)
8352         {
8353           int decl_spec_declares_class_or_enum;
8354           bool is_cv_qualifier;
8355           tree type_spec;
8356
8357           type_spec
8358             = cp_parser_type_specifier (parser, flags,
8359                                         decl_specs,
8360                                         /*is_declaration=*/true,
8361                                         &decl_spec_declares_class_or_enum,
8362                                         &is_cv_qualifier);
8363           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8364
8365           /* If this type-specifier referenced a user-defined type
8366              (a typedef, class-name, etc.), then we can't allow any
8367              more such type-specifiers henceforth.
8368
8369              [dcl.spec]
8370
8371              The longest sequence of decl-specifiers that could
8372              possibly be a type name is taken as the
8373              decl-specifier-seq of a declaration.  The sequence shall
8374              be self-consistent as described below.
8375
8376              [dcl.type]
8377
8378              As a general rule, at most one type-specifier is allowed
8379              in the complete decl-specifier-seq of a declaration.  The
8380              only exceptions are the following:
8381
8382              -- const or volatile can be combined with any other
8383                 type-specifier.
8384
8385              -- signed or unsigned can be combined with char, long,
8386                 short, or int.
8387
8388              -- ..
8389
8390              Example:
8391
8392                typedef char* Pc;
8393                void g (const int Pc);
8394
8395              Here, Pc is *not* part of the decl-specifier seq; it's
8396              the declarator.  Therefore, once we see a type-specifier
8397              (other than a cv-qualifier), we forbid any additional
8398              user-defined types.  We *do* still allow things like `int
8399              int' to be considered a decl-specifier-seq, and issue the
8400              error message later.  */
8401           if (type_spec && !is_cv_qualifier)
8402             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8403           /* A constructor declarator cannot follow a type-specifier.  */
8404           if (type_spec)
8405             {
8406               constructor_possible_p = false;
8407               found_decl_spec = true;
8408             }
8409         }
8410
8411       /* If we still do not have a DECL_SPEC, then there are no more
8412          decl-specifiers.  */
8413       if (!found_decl_spec)
8414         break;
8415
8416       decl_specs->any_specifiers_p = true;
8417       /* After we see one decl-specifier, further decl-specifiers are
8418          always optional.  */
8419       flags |= CP_PARSER_FLAGS_OPTIONAL;
8420     }
8421
8422   cp_parser_check_decl_spec (decl_specs, start_token->location);
8423
8424   /* Don't allow a friend specifier with a class definition.  */
8425   if (decl_specs->specs[(int) ds_friend] != 0
8426       && (*declares_class_or_enum & 2))
8427     error ("%Hclass definition may not be declared a friend",
8428             &start_token->location);
8429 }
8430
8431 /* Parse an (optional) storage-class-specifier.
8432
8433    storage-class-specifier:
8434      auto
8435      register
8436      static
8437      extern
8438      mutable
8439
8440    GNU Extension:
8441
8442    storage-class-specifier:
8443      thread
8444
8445    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8446
8447 static tree
8448 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8449 {
8450   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8451     {
8452     case RID_AUTO:
8453       if (cxx_dialect != cxx98)
8454         return NULL_TREE;
8455       /* Fall through for C++98.  */
8456
8457     case RID_REGISTER:
8458     case RID_STATIC:
8459     case RID_EXTERN:
8460     case RID_MUTABLE:
8461     case RID_THREAD:
8462       /* Consume the token.  */
8463       return cp_lexer_consume_token (parser->lexer)->u.value;
8464
8465     default:
8466       return NULL_TREE;
8467     }
8468 }
8469
8470 /* Parse an (optional) function-specifier.
8471
8472    function-specifier:
8473      inline
8474      virtual
8475      explicit
8476
8477    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8478    Updates DECL_SPECS, if it is non-NULL.  */
8479
8480 static tree
8481 cp_parser_function_specifier_opt (cp_parser* parser,
8482                                   cp_decl_specifier_seq *decl_specs)
8483 {
8484   cp_token *token = cp_lexer_peek_token (parser->lexer);
8485   switch (token->keyword)
8486     {
8487     case RID_INLINE:
8488       if (decl_specs)
8489         ++decl_specs->specs[(int) ds_inline];
8490       break;
8491
8492     case RID_VIRTUAL:
8493       /* 14.5.2.3 [temp.mem]
8494
8495          A member function template shall not be virtual.  */
8496       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8497         error ("%Htemplates may not be %<virtual%>", &token->location);
8498       else if (decl_specs)
8499         ++decl_specs->specs[(int) ds_virtual];
8500       break;
8501
8502     case RID_EXPLICIT:
8503       if (decl_specs)
8504         ++decl_specs->specs[(int) ds_explicit];
8505       break;
8506
8507     default:
8508       return NULL_TREE;
8509     }
8510
8511   /* Consume the token.  */
8512   return cp_lexer_consume_token (parser->lexer)->u.value;
8513 }
8514
8515 /* Parse a linkage-specification.
8516
8517    linkage-specification:
8518      extern string-literal { declaration-seq [opt] }
8519      extern string-literal declaration  */
8520
8521 static void
8522 cp_parser_linkage_specification (cp_parser* parser)
8523 {
8524   tree linkage;
8525
8526   /* Look for the `extern' keyword.  */
8527   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8528
8529   /* Look for the string-literal.  */
8530   linkage = cp_parser_string_literal (parser, false, false);
8531
8532   /* Transform the literal into an identifier.  If the literal is a
8533      wide-character string, or contains embedded NULs, then we can't
8534      handle it as the user wants.  */
8535   if (strlen (TREE_STRING_POINTER (linkage))
8536       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8537     {
8538       cp_parser_error (parser, "invalid linkage-specification");
8539       /* Assume C++ linkage.  */
8540       linkage = lang_name_cplusplus;
8541     }
8542   else
8543     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8544
8545   /* We're now using the new linkage.  */
8546   push_lang_context (linkage);
8547
8548   /* If the next token is a `{', then we're using the first
8549      production.  */
8550   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8551     {
8552       /* Consume the `{' token.  */
8553       cp_lexer_consume_token (parser->lexer);
8554       /* Parse the declarations.  */
8555       cp_parser_declaration_seq_opt (parser);
8556       /* Look for the closing `}'.  */
8557       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8558     }
8559   /* Otherwise, there's just one declaration.  */
8560   else
8561     {
8562       bool saved_in_unbraced_linkage_specification_p;
8563
8564       saved_in_unbraced_linkage_specification_p
8565         = parser->in_unbraced_linkage_specification_p;
8566       parser->in_unbraced_linkage_specification_p = true;
8567       cp_parser_declaration (parser);
8568       parser->in_unbraced_linkage_specification_p
8569         = saved_in_unbraced_linkage_specification_p;
8570     }
8571
8572   /* We're done with the linkage-specification.  */
8573   pop_lang_context ();
8574 }
8575
8576 /* Parse a static_assert-declaration.
8577
8578    static_assert-declaration:
8579      static_assert ( constant-expression , string-literal ) ; 
8580
8581    If MEMBER_P, this static_assert is a class member.  */
8582
8583 static void 
8584 cp_parser_static_assert(cp_parser *parser, bool member_p)
8585 {
8586   tree condition;
8587   tree message;
8588   cp_token *token;
8589   location_t saved_loc;
8590
8591   /* Peek at the `static_assert' token so we can keep track of exactly
8592      where the static assertion started.  */
8593   token = cp_lexer_peek_token (parser->lexer);
8594   saved_loc = token->location;
8595
8596   /* Look for the `static_assert' keyword.  */
8597   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8598                                   "%<static_assert%>"))
8599     return;
8600
8601   /*  We know we are in a static assertion; commit to any tentative
8602       parse.  */
8603   if (cp_parser_parsing_tentatively (parser))
8604     cp_parser_commit_to_tentative_parse (parser);
8605
8606   /* Parse the `(' starting the static assertion condition.  */
8607   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8608
8609   /* Parse the constant-expression.  */
8610   condition = 
8611     cp_parser_constant_expression (parser,
8612                                    /*allow_non_constant_p=*/false,
8613                                    /*non_constant_p=*/NULL);
8614
8615   /* Parse the separating `,'.  */
8616   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8617
8618   /* Parse the string-literal message.  */
8619   message = cp_parser_string_literal (parser, 
8620                                       /*translate=*/false,
8621                                       /*wide_ok=*/true);
8622
8623   /* A `)' completes the static assertion.  */
8624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8625     cp_parser_skip_to_closing_parenthesis (parser, 
8626                                            /*recovering=*/true, 
8627                                            /*or_comma=*/false,
8628                                            /*consume_paren=*/true);
8629
8630   /* A semicolon terminates the declaration.  */
8631   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8632
8633   /* Complete the static assertion, which may mean either processing 
8634      the static assert now or saving it for template instantiation.  */
8635   finish_static_assert (condition, message, saved_loc, member_p);
8636 }
8637
8638 /* Parse a `decltype' type. Returns the type. 
8639
8640    simple-type-specifier:
8641      decltype ( expression )  */
8642
8643 static tree
8644 cp_parser_decltype (cp_parser *parser)
8645 {
8646   tree expr;
8647   bool id_expression_or_member_access_p = false;
8648   const char *saved_message;
8649   bool saved_integral_constant_expression_p;
8650   bool saved_non_integral_constant_expression_p;
8651   cp_token *id_expr_start_token;
8652
8653   /* Look for the `decltype' token.  */
8654   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8655     return error_mark_node;
8656
8657   /* Types cannot be defined in a `decltype' expression.  Save away the
8658      old message.  */
8659   saved_message = parser->type_definition_forbidden_message;
8660
8661   /* And create the new one.  */
8662   parser->type_definition_forbidden_message
8663     = "types may not be defined in %<decltype%> expressions";
8664
8665   /* The restrictions on constant-expressions do not apply inside
8666      decltype expressions.  */
8667   saved_integral_constant_expression_p
8668     = parser->integral_constant_expression_p;
8669   saved_non_integral_constant_expression_p
8670     = parser->non_integral_constant_expression_p;
8671   parser->integral_constant_expression_p = false;
8672
8673   /* Do not actually evaluate the expression.  */
8674   ++skip_evaluation;
8675
8676   /* Parse the opening `('.  */
8677   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8678     return error_mark_node;
8679   
8680   /* First, try parsing an id-expression.  */
8681   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8682   cp_parser_parse_tentatively (parser);
8683   expr = cp_parser_id_expression (parser,
8684                                   /*template_keyword_p=*/false,
8685                                   /*check_dependency_p=*/true,
8686                                   /*template_p=*/NULL,
8687                                   /*declarator_p=*/false,
8688                                   /*optional_p=*/false);
8689
8690   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8691     {
8692       bool non_integral_constant_expression_p = false;
8693       tree id_expression = expr;
8694       cp_id_kind idk;
8695       const char *error_msg;
8696
8697       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8698         /* Lookup the name we got back from the id-expression.  */
8699         expr = cp_parser_lookup_name (parser, expr,
8700                                       none_type,
8701                                       /*is_template=*/false,
8702                                       /*is_namespace=*/false,
8703                                       /*check_dependency=*/true,
8704                                       /*ambiguous_decls=*/NULL,
8705                                       id_expr_start_token->location);
8706
8707       if (expr
8708           && expr != error_mark_node
8709           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8710           && TREE_CODE (expr) != TYPE_DECL
8711           && (TREE_CODE (expr) != BIT_NOT_EXPR
8712               || !TYPE_P (TREE_OPERAND (expr, 0)))
8713           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8714         {
8715           /* Complete lookup of the id-expression.  */
8716           expr = (finish_id_expression
8717                   (id_expression, expr, parser->scope, &idk,
8718                    /*integral_constant_expression_p=*/false,
8719                    /*allow_non_integral_constant_expression_p=*/true,
8720                    &non_integral_constant_expression_p,
8721                    /*template_p=*/false,
8722                    /*done=*/true,
8723                    /*address_p=*/false,
8724                    /*template_arg_p=*/false,
8725                    &error_msg,
8726                    id_expr_start_token->location));
8727
8728           if (expr == error_mark_node)
8729             /* We found an id-expression, but it was something that we
8730                should not have found. This is an error, not something
8731                we can recover from, so note that we found an
8732                id-expression and we'll recover as gracefully as
8733                possible.  */
8734             id_expression_or_member_access_p = true;
8735         }
8736
8737       if (expr 
8738           && expr != error_mark_node
8739           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8740         /* We have an id-expression.  */
8741         id_expression_or_member_access_p = true;
8742     }
8743
8744   if (!id_expression_or_member_access_p)
8745     {
8746       /* Abort the id-expression parse.  */
8747       cp_parser_abort_tentative_parse (parser);
8748
8749       /* Parsing tentatively, again.  */
8750       cp_parser_parse_tentatively (parser);
8751
8752       /* Parse a class member access.  */
8753       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8754                                            /*cast_p=*/false,
8755                                            /*member_access_only_p=*/true);
8756
8757       if (expr 
8758           && expr != error_mark_node
8759           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8760         /* We have an id-expression.  */
8761         id_expression_or_member_access_p = true;
8762     }
8763
8764   if (id_expression_or_member_access_p)
8765     /* We have parsed the complete id-expression or member access.  */
8766     cp_parser_parse_definitely (parser);
8767   else
8768     {
8769       /* Abort our attempt to parse an id-expression or member access
8770          expression.  */
8771       cp_parser_abort_tentative_parse (parser);
8772
8773       /* Parse a full expression.  */
8774       expr = cp_parser_expression (parser, /*cast_p=*/false);
8775     }
8776
8777   /* Go back to evaluating expressions.  */
8778   --skip_evaluation;
8779
8780   /* Restore the old message and the integral constant expression
8781      flags.  */
8782   parser->type_definition_forbidden_message = saved_message;
8783   parser->integral_constant_expression_p
8784     = saved_integral_constant_expression_p;
8785   parser->non_integral_constant_expression_p
8786     = saved_non_integral_constant_expression_p;
8787
8788   if (expr == error_mark_node)
8789     {
8790       /* Skip everything up to the closing `)'.  */
8791       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8792                                              /*consume_paren=*/true);
8793       return error_mark_node;
8794     }
8795   
8796   /* Parse to the closing `)'.  */
8797   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8798     {
8799       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8800                                              /*consume_paren=*/true);
8801       return error_mark_node;
8802     }
8803
8804   return finish_decltype_type (expr, id_expression_or_member_access_p);
8805 }
8806
8807 /* Special member functions [gram.special] */
8808
8809 /* Parse a conversion-function-id.
8810
8811    conversion-function-id:
8812      operator conversion-type-id
8813
8814    Returns an IDENTIFIER_NODE representing the operator.  */
8815
8816 static tree
8817 cp_parser_conversion_function_id (cp_parser* parser)
8818 {
8819   tree type;
8820   tree saved_scope;
8821   tree saved_qualifying_scope;
8822   tree saved_object_scope;
8823   tree pushed_scope = NULL_TREE;
8824
8825   /* Look for the `operator' token.  */
8826   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8827     return error_mark_node;
8828   /* When we parse the conversion-type-id, the current scope will be
8829      reset.  However, we need that information in able to look up the
8830      conversion function later, so we save it here.  */
8831   saved_scope = parser->scope;
8832   saved_qualifying_scope = parser->qualifying_scope;
8833   saved_object_scope = parser->object_scope;
8834   /* We must enter the scope of the class so that the names of
8835      entities declared within the class are available in the
8836      conversion-type-id.  For example, consider:
8837
8838        struct S {
8839          typedef int I;
8840          operator I();
8841        };
8842
8843        S::operator I() { ... }
8844
8845      In order to see that `I' is a type-name in the definition, we
8846      must be in the scope of `S'.  */
8847   if (saved_scope)
8848     pushed_scope = push_scope (saved_scope);
8849   /* Parse the conversion-type-id.  */
8850   type = cp_parser_conversion_type_id (parser);
8851   /* Leave the scope of the class, if any.  */
8852   if (pushed_scope)
8853     pop_scope (pushed_scope);
8854   /* Restore the saved scope.  */
8855   parser->scope = saved_scope;
8856   parser->qualifying_scope = saved_qualifying_scope;
8857   parser->object_scope = saved_object_scope;
8858   /* If the TYPE is invalid, indicate failure.  */
8859   if (type == error_mark_node)
8860     return error_mark_node;
8861   return mangle_conv_op_name_for_type (type);
8862 }
8863
8864 /* Parse a conversion-type-id:
8865
8866    conversion-type-id:
8867      type-specifier-seq conversion-declarator [opt]
8868
8869    Returns the TYPE specified.  */
8870
8871 static tree
8872 cp_parser_conversion_type_id (cp_parser* parser)
8873 {
8874   tree attributes;
8875   cp_decl_specifier_seq type_specifiers;
8876   cp_declarator *declarator;
8877   tree type_specified;
8878
8879   /* Parse the attributes.  */
8880   attributes = cp_parser_attributes_opt (parser);
8881   /* Parse the type-specifiers.  */
8882   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8883                                 &type_specifiers);
8884   /* If that didn't work, stop.  */
8885   if (type_specifiers.type == error_mark_node)
8886     return error_mark_node;
8887   /* Parse the conversion-declarator.  */
8888   declarator = cp_parser_conversion_declarator_opt (parser);
8889
8890   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8891                                     /*initialized=*/0, &attributes);
8892   if (attributes)
8893     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8894   return type_specified;
8895 }
8896
8897 /* Parse an (optional) conversion-declarator.
8898
8899    conversion-declarator:
8900      ptr-operator conversion-declarator [opt]
8901
8902    */
8903
8904 static cp_declarator *
8905 cp_parser_conversion_declarator_opt (cp_parser* parser)
8906 {
8907   enum tree_code code;
8908   tree class_type;
8909   cp_cv_quals cv_quals;
8910
8911   /* We don't know if there's a ptr-operator next, or not.  */
8912   cp_parser_parse_tentatively (parser);
8913   /* Try the ptr-operator.  */
8914   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8915   /* If it worked, look for more conversion-declarators.  */
8916   if (cp_parser_parse_definitely (parser))
8917     {
8918       cp_declarator *declarator;
8919
8920       /* Parse another optional declarator.  */
8921       declarator = cp_parser_conversion_declarator_opt (parser);
8922
8923       return cp_parser_make_indirect_declarator
8924         (code, class_type, cv_quals, declarator);
8925    }
8926
8927   return NULL;
8928 }
8929
8930 /* Parse an (optional) ctor-initializer.
8931
8932    ctor-initializer:
8933      : mem-initializer-list
8934
8935    Returns TRUE iff the ctor-initializer was actually present.  */
8936
8937 static bool
8938 cp_parser_ctor_initializer_opt (cp_parser* parser)
8939 {
8940   /* If the next token is not a `:', then there is no
8941      ctor-initializer.  */
8942   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8943     {
8944       /* Do default initialization of any bases and members.  */
8945       if (DECL_CONSTRUCTOR_P (current_function_decl))
8946         finish_mem_initializers (NULL_TREE);
8947
8948       return false;
8949     }
8950
8951   /* Consume the `:' token.  */
8952   cp_lexer_consume_token (parser->lexer);
8953   /* And the mem-initializer-list.  */
8954   cp_parser_mem_initializer_list (parser);
8955
8956   return true;
8957 }
8958
8959 /* Parse a mem-initializer-list.
8960
8961    mem-initializer-list:
8962      mem-initializer ... [opt]
8963      mem-initializer ... [opt] , mem-initializer-list  */
8964
8965 static void
8966 cp_parser_mem_initializer_list (cp_parser* parser)
8967 {
8968   tree mem_initializer_list = NULL_TREE;
8969   cp_token *token = cp_lexer_peek_token (parser->lexer);
8970
8971   /* Let the semantic analysis code know that we are starting the
8972      mem-initializer-list.  */
8973   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8974     error ("%Honly constructors take base initializers",
8975            &token->location);
8976
8977   /* Loop through the list.  */
8978   while (true)
8979     {
8980       tree mem_initializer;
8981
8982       token = cp_lexer_peek_token (parser->lexer);
8983       /* Parse the mem-initializer.  */
8984       mem_initializer = cp_parser_mem_initializer (parser);
8985       /* If the next token is a `...', we're expanding member initializers. */
8986       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8987         {
8988           /* Consume the `...'. */
8989           cp_lexer_consume_token (parser->lexer);
8990
8991           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8992              can be expanded but members cannot. */
8993           if (mem_initializer != error_mark_node
8994               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8995             {
8996               error ("%Hcannot expand initializer for member %<%D%>",
8997                      &token->location, TREE_PURPOSE (mem_initializer));
8998               mem_initializer = error_mark_node;
8999             }
9000
9001           /* Construct the pack expansion type. */
9002           if (mem_initializer != error_mark_node)
9003             mem_initializer = make_pack_expansion (mem_initializer);
9004         }
9005       /* Add it to the list, unless it was erroneous.  */
9006       if (mem_initializer != error_mark_node)
9007         {
9008           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9009           mem_initializer_list = mem_initializer;
9010         }
9011       /* If the next token is not a `,', we're done.  */
9012       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9013         break;
9014       /* Consume the `,' token.  */
9015       cp_lexer_consume_token (parser->lexer);
9016     }
9017
9018   /* Perform semantic analysis.  */
9019   if (DECL_CONSTRUCTOR_P (current_function_decl))
9020     finish_mem_initializers (mem_initializer_list);
9021 }
9022
9023 /* Parse a mem-initializer.
9024
9025    mem-initializer:
9026      mem-initializer-id ( expression-list [opt] )
9027      mem-initializer-id braced-init-list
9028
9029    GNU extension:
9030
9031    mem-initializer:
9032      ( expression-list [opt] )
9033
9034    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9035    class) or FIELD_DECL (for a non-static data member) to initialize;
9036    the TREE_VALUE is the expression-list.  An empty initialization
9037    list is represented by void_list_node.  */
9038
9039 static tree
9040 cp_parser_mem_initializer (cp_parser* parser)
9041 {
9042   tree mem_initializer_id;
9043   tree expression_list;
9044   tree member;
9045   cp_token *token = cp_lexer_peek_token (parser->lexer);
9046
9047   /* Find out what is being initialized.  */
9048   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9049     {
9050       permerror ("%Hanachronistic old-style base class initializer",
9051                  &token->location);
9052       mem_initializer_id = NULL_TREE;
9053     }
9054   else
9055     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9056   member = expand_member_init (mem_initializer_id);
9057   if (member && !DECL_P (member))
9058     in_base_initializer = 1;
9059
9060   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9061     {
9062       bool expr_non_constant_p;
9063       maybe_warn_cpp0x ("extended initializer lists");
9064       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9065       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9066       expression_list = build_tree_list (NULL_TREE, expression_list);
9067     }
9068   else
9069     expression_list
9070       = cp_parser_parenthesized_expression_list (parser, false,
9071                                                  /*cast_p=*/false,
9072                                                  /*allow_expansion_p=*/true,
9073                                                  /*non_constant_p=*/NULL);
9074   if (expression_list == error_mark_node)
9075     return error_mark_node;
9076   if (!expression_list)
9077     expression_list = void_type_node;
9078
9079   in_base_initializer = 0;
9080
9081   return member ? build_tree_list (member, expression_list) : error_mark_node;
9082 }
9083
9084 /* Parse a mem-initializer-id.
9085
9086    mem-initializer-id:
9087      :: [opt] nested-name-specifier [opt] class-name
9088      identifier
9089
9090    Returns a TYPE indicating the class to be initializer for the first
9091    production.  Returns an IDENTIFIER_NODE indicating the data member
9092    to be initialized for the second production.  */
9093
9094 static tree
9095 cp_parser_mem_initializer_id (cp_parser* parser)
9096 {
9097   bool global_scope_p;
9098   bool nested_name_specifier_p;
9099   bool template_p = false;
9100   tree id;
9101
9102   cp_token *token = cp_lexer_peek_token (parser->lexer);
9103
9104   /* `typename' is not allowed in this context ([temp.res]).  */
9105   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9106     {
9107       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9108              "member initializer is implicitly a type)",
9109              &token->location);
9110       cp_lexer_consume_token (parser->lexer);
9111     }
9112   /* Look for the optional `::' operator.  */
9113   global_scope_p
9114     = (cp_parser_global_scope_opt (parser,
9115                                    /*current_scope_valid_p=*/false)
9116        != NULL_TREE);
9117   /* Look for the optional nested-name-specifier.  The simplest way to
9118      implement:
9119
9120        [temp.res]
9121
9122        The keyword `typename' is not permitted in a base-specifier or
9123        mem-initializer; in these contexts a qualified name that
9124        depends on a template-parameter is implicitly assumed to be a
9125        type name.
9126
9127      is to assume that we have seen the `typename' keyword at this
9128      point.  */
9129   nested_name_specifier_p
9130     = (cp_parser_nested_name_specifier_opt (parser,
9131                                             /*typename_keyword_p=*/true,
9132                                             /*check_dependency_p=*/true,
9133                                             /*type_p=*/true,
9134                                             /*is_declaration=*/true)
9135        != NULL_TREE);
9136   if (nested_name_specifier_p)
9137     template_p = cp_parser_optional_template_keyword (parser);
9138   /* If there is a `::' operator or a nested-name-specifier, then we
9139      are definitely looking for a class-name.  */
9140   if (global_scope_p || nested_name_specifier_p)
9141     return cp_parser_class_name (parser,
9142                                  /*typename_keyword_p=*/true,
9143                                  /*template_keyword_p=*/template_p,
9144                                  none_type,
9145                                  /*check_dependency_p=*/true,
9146                                  /*class_head_p=*/false,
9147                                  /*is_declaration=*/true);
9148   /* Otherwise, we could also be looking for an ordinary identifier.  */
9149   cp_parser_parse_tentatively (parser);
9150   /* Try a class-name.  */
9151   id = cp_parser_class_name (parser,
9152                              /*typename_keyword_p=*/true,
9153                              /*template_keyword_p=*/false,
9154                              none_type,
9155                              /*check_dependency_p=*/true,
9156                              /*class_head_p=*/false,
9157                              /*is_declaration=*/true);
9158   /* If we found one, we're done.  */
9159   if (cp_parser_parse_definitely (parser))
9160     return id;
9161   /* Otherwise, look for an ordinary identifier.  */
9162   return cp_parser_identifier (parser);
9163 }
9164
9165 /* Overloading [gram.over] */
9166
9167 /* Parse an operator-function-id.
9168
9169    operator-function-id:
9170      operator operator
9171
9172    Returns an IDENTIFIER_NODE for the operator which is a
9173    human-readable spelling of the identifier, e.g., `operator +'.  */
9174
9175 static tree
9176 cp_parser_operator_function_id (cp_parser* parser)
9177 {
9178   /* Look for the `operator' keyword.  */
9179   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9180     return error_mark_node;
9181   /* And then the name of the operator itself.  */
9182   return cp_parser_operator (parser);
9183 }
9184
9185 /* Parse an operator.
9186
9187    operator:
9188      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9189      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9190      || ++ -- , ->* -> () []
9191
9192    GNU Extensions:
9193
9194    operator:
9195      <? >? <?= >?=
9196
9197    Returns an IDENTIFIER_NODE for the operator which is a
9198    human-readable spelling of the identifier, e.g., `operator +'.  */
9199
9200 static tree
9201 cp_parser_operator (cp_parser* parser)
9202 {
9203   tree id = NULL_TREE;
9204   cp_token *token;
9205
9206   /* Peek at the next token.  */
9207   token = cp_lexer_peek_token (parser->lexer);
9208   /* Figure out which operator we have.  */
9209   switch (token->type)
9210     {
9211     case CPP_KEYWORD:
9212       {
9213         enum tree_code op;
9214
9215         /* The keyword should be either `new' or `delete'.  */
9216         if (token->keyword == RID_NEW)
9217           op = NEW_EXPR;
9218         else if (token->keyword == RID_DELETE)
9219           op = DELETE_EXPR;
9220         else
9221           break;
9222
9223         /* Consume the `new' or `delete' token.  */
9224         cp_lexer_consume_token (parser->lexer);
9225
9226         /* Peek at the next token.  */
9227         token = cp_lexer_peek_token (parser->lexer);
9228         /* If it's a `[' token then this is the array variant of the
9229            operator.  */
9230         if (token->type == CPP_OPEN_SQUARE)
9231           {
9232             /* Consume the `[' token.  */
9233             cp_lexer_consume_token (parser->lexer);
9234             /* Look for the `]' token.  */
9235             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9236             id = ansi_opname (op == NEW_EXPR
9237                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9238           }
9239         /* Otherwise, we have the non-array variant.  */
9240         else
9241           id = ansi_opname (op);
9242
9243         return id;
9244       }
9245
9246     case CPP_PLUS:
9247       id = ansi_opname (PLUS_EXPR);
9248       break;
9249
9250     case CPP_MINUS:
9251       id = ansi_opname (MINUS_EXPR);
9252       break;
9253
9254     case CPP_MULT:
9255       id = ansi_opname (MULT_EXPR);
9256       break;
9257
9258     case CPP_DIV:
9259       id = ansi_opname (TRUNC_DIV_EXPR);
9260       break;
9261
9262     case CPP_MOD:
9263       id = ansi_opname (TRUNC_MOD_EXPR);
9264       break;
9265
9266     case CPP_XOR:
9267       id = ansi_opname (BIT_XOR_EXPR);
9268       break;
9269
9270     case CPP_AND:
9271       id = ansi_opname (BIT_AND_EXPR);
9272       break;
9273
9274     case CPP_OR:
9275       id = ansi_opname (BIT_IOR_EXPR);
9276       break;
9277
9278     case CPP_COMPL:
9279       id = ansi_opname (BIT_NOT_EXPR);
9280       break;
9281
9282     case CPP_NOT:
9283       id = ansi_opname (TRUTH_NOT_EXPR);
9284       break;
9285
9286     case CPP_EQ:
9287       id = ansi_assopname (NOP_EXPR);
9288       break;
9289
9290     case CPP_LESS:
9291       id = ansi_opname (LT_EXPR);
9292       break;
9293
9294     case CPP_GREATER:
9295       id = ansi_opname (GT_EXPR);
9296       break;
9297
9298     case CPP_PLUS_EQ:
9299       id = ansi_assopname (PLUS_EXPR);
9300       break;
9301
9302     case CPP_MINUS_EQ:
9303       id = ansi_assopname (MINUS_EXPR);
9304       break;
9305
9306     case CPP_MULT_EQ:
9307       id = ansi_assopname (MULT_EXPR);
9308       break;
9309
9310     case CPP_DIV_EQ:
9311       id = ansi_assopname (TRUNC_DIV_EXPR);
9312       break;
9313
9314     case CPP_MOD_EQ:
9315       id = ansi_assopname (TRUNC_MOD_EXPR);
9316       break;
9317
9318     case CPP_XOR_EQ:
9319       id = ansi_assopname (BIT_XOR_EXPR);
9320       break;
9321
9322     case CPP_AND_EQ:
9323       id = ansi_assopname (BIT_AND_EXPR);
9324       break;
9325
9326     case CPP_OR_EQ:
9327       id = ansi_assopname (BIT_IOR_EXPR);
9328       break;
9329
9330     case CPP_LSHIFT:
9331       id = ansi_opname (LSHIFT_EXPR);
9332       break;
9333
9334     case CPP_RSHIFT:
9335       id = ansi_opname (RSHIFT_EXPR);
9336       break;
9337
9338     case CPP_LSHIFT_EQ:
9339       id = ansi_assopname (LSHIFT_EXPR);
9340       break;
9341
9342     case CPP_RSHIFT_EQ:
9343       id = ansi_assopname (RSHIFT_EXPR);
9344       break;
9345
9346     case CPP_EQ_EQ:
9347       id = ansi_opname (EQ_EXPR);
9348       break;
9349
9350     case CPP_NOT_EQ:
9351       id = ansi_opname (NE_EXPR);
9352       break;
9353
9354     case CPP_LESS_EQ:
9355       id = ansi_opname (LE_EXPR);
9356       break;
9357
9358     case CPP_GREATER_EQ:
9359       id = ansi_opname (GE_EXPR);
9360       break;
9361
9362     case CPP_AND_AND:
9363       id = ansi_opname (TRUTH_ANDIF_EXPR);
9364       break;
9365
9366     case CPP_OR_OR:
9367       id = ansi_opname (TRUTH_ORIF_EXPR);
9368       break;
9369
9370     case CPP_PLUS_PLUS:
9371       id = ansi_opname (POSTINCREMENT_EXPR);
9372       break;
9373
9374     case CPP_MINUS_MINUS:
9375       id = ansi_opname (PREDECREMENT_EXPR);
9376       break;
9377
9378     case CPP_COMMA:
9379       id = ansi_opname (COMPOUND_EXPR);
9380       break;
9381
9382     case CPP_DEREF_STAR:
9383       id = ansi_opname (MEMBER_REF);
9384       break;
9385
9386     case CPP_DEREF:
9387       id = ansi_opname (COMPONENT_REF);
9388       break;
9389
9390     case CPP_OPEN_PAREN:
9391       /* Consume the `('.  */
9392       cp_lexer_consume_token (parser->lexer);
9393       /* Look for the matching `)'.  */
9394       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9395       return ansi_opname (CALL_EXPR);
9396
9397     case CPP_OPEN_SQUARE:
9398       /* Consume the `['.  */
9399       cp_lexer_consume_token (parser->lexer);
9400       /* Look for the matching `]'.  */
9401       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9402       return ansi_opname (ARRAY_REF);
9403
9404     default:
9405       /* Anything else is an error.  */
9406       break;
9407     }
9408
9409   /* If we have selected an identifier, we need to consume the
9410      operator token.  */
9411   if (id)
9412     cp_lexer_consume_token (parser->lexer);
9413   /* Otherwise, no valid operator name was present.  */
9414   else
9415     {
9416       cp_parser_error (parser, "expected operator");
9417       id = error_mark_node;
9418     }
9419
9420   return id;
9421 }
9422
9423 /* Parse a template-declaration.
9424
9425    template-declaration:
9426      export [opt] template < template-parameter-list > declaration
9427
9428    If MEMBER_P is TRUE, this template-declaration occurs within a
9429    class-specifier.
9430
9431    The grammar rule given by the standard isn't correct.  What
9432    is really meant is:
9433
9434    template-declaration:
9435      export [opt] template-parameter-list-seq
9436        decl-specifier-seq [opt] init-declarator [opt] ;
9437      export [opt] template-parameter-list-seq
9438        function-definition
9439
9440    template-parameter-list-seq:
9441      template-parameter-list-seq [opt]
9442      template < template-parameter-list >  */
9443
9444 static void
9445 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9446 {
9447   /* Check for `export'.  */
9448   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9449     {
9450       /* Consume the `export' token.  */
9451       cp_lexer_consume_token (parser->lexer);
9452       /* Warn that we do not support `export'.  */
9453       warning (0, "keyword %<export%> not implemented, and will be ignored");
9454     }
9455
9456   cp_parser_template_declaration_after_export (parser, member_p);
9457 }
9458
9459 /* Parse a template-parameter-list.
9460
9461    template-parameter-list:
9462      template-parameter
9463      template-parameter-list , template-parameter
9464
9465    Returns a TREE_LIST.  Each node represents a template parameter.
9466    The nodes are connected via their TREE_CHAINs.  */
9467
9468 static tree
9469 cp_parser_template_parameter_list (cp_parser* parser)
9470 {
9471   tree parameter_list = NULL_TREE;
9472
9473   begin_template_parm_list ();
9474   while (true)
9475     {
9476       tree parameter;
9477       bool is_non_type;
9478       bool is_parameter_pack;
9479
9480       /* Parse the template-parameter.  */
9481       parameter = cp_parser_template_parameter (parser, 
9482                                                 &is_non_type,
9483                                                 &is_parameter_pack);
9484       /* Add it to the list.  */
9485       if (parameter != error_mark_node)
9486         parameter_list = process_template_parm (parameter_list,
9487                                                 parameter,
9488                                                 is_non_type,
9489                                                 is_parameter_pack);
9490       else
9491        {
9492          tree err_parm = build_tree_list (parameter, parameter);
9493          TREE_VALUE (err_parm) = error_mark_node;
9494          parameter_list = chainon (parameter_list, err_parm);
9495        }
9496
9497       /* If the next token is not a `,', we're done.  */
9498       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9499         break;
9500       /* Otherwise, consume the `,' token.  */
9501       cp_lexer_consume_token (parser->lexer);
9502     }
9503
9504   return end_template_parm_list (parameter_list);
9505 }
9506
9507 /* Parse a template-parameter.
9508
9509    template-parameter:
9510      type-parameter
9511      parameter-declaration
9512
9513    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9514    the parameter.  The TREE_PURPOSE is the default value, if any.
9515    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9516    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9517    set to true iff this parameter is a parameter pack. */
9518
9519 static tree
9520 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9521                               bool *is_parameter_pack)
9522 {
9523   cp_token *token;
9524   cp_parameter_declarator *parameter_declarator;
9525   cp_declarator *id_declarator;
9526   tree parm;
9527
9528   /* Assume it is a type parameter or a template parameter.  */
9529   *is_non_type = false;
9530   /* Assume it not a parameter pack. */
9531   *is_parameter_pack = false;
9532   /* Peek at the next token.  */
9533   token = cp_lexer_peek_token (parser->lexer);
9534   /* If it is `class' or `template', we have a type-parameter.  */
9535   if (token->keyword == RID_TEMPLATE)
9536     return cp_parser_type_parameter (parser, is_parameter_pack);
9537   /* If it is `class' or `typename' we do not know yet whether it is a
9538      type parameter or a non-type parameter.  Consider:
9539
9540        template <typename T, typename T::X X> ...
9541
9542      or:
9543
9544        template <class C, class D*> ...
9545
9546      Here, the first parameter is a type parameter, and the second is
9547      a non-type parameter.  We can tell by looking at the token after
9548      the identifier -- if it is a `,', `=', or `>' then we have a type
9549      parameter.  */
9550   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9551     {
9552       /* Peek at the token after `class' or `typename'.  */
9553       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9554       /* If it's an ellipsis, we have a template type parameter
9555          pack. */
9556       if (token->type == CPP_ELLIPSIS)
9557         return cp_parser_type_parameter (parser, is_parameter_pack);
9558       /* If it's an identifier, skip it.  */
9559       if (token->type == CPP_NAME)
9560         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9561       /* Now, see if the token looks like the end of a template
9562          parameter.  */
9563       if (token->type == CPP_COMMA
9564           || token->type == CPP_EQ
9565           || token->type == CPP_GREATER)
9566         return cp_parser_type_parameter (parser, is_parameter_pack);
9567     }
9568
9569   /* Otherwise, it is a non-type parameter.
9570
9571      [temp.param]
9572
9573      When parsing a default template-argument for a non-type
9574      template-parameter, the first non-nested `>' is taken as the end
9575      of the template parameter-list rather than a greater-than
9576      operator.  */
9577   *is_non_type = true;
9578   parameter_declarator
9579      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9580                                         /*parenthesized_p=*/NULL);
9581
9582   /* If the parameter declaration is marked as a parameter pack, set
9583      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9584      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9585      grokdeclarator. */
9586   if (parameter_declarator
9587       && parameter_declarator->declarator
9588       && parameter_declarator->declarator->parameter_pack_p)
9589     {
9590       *is_parameter_pack = true;
9591       parameter_declarator->declarator->parameter_pack_p = false;
9592     }
9593
9594   /* If the next token is an ellipsis, and we don't already have it
9595      marked as a parameter pack, then we have a parameter pack (that
9596      has no declarator).  */
9597   if (!*is_parameter_pack
9598       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9599       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9600     {
9601       /* Consume the `...'.  */
9602       cp_lexer_consume_token (parser->lexer);
9603       maybe_warn_variadic_templates ();
9604       
9605       *is_parameter_pack = true;
9606     }
9607   /* We might end up with a pack expansion as the type of the non-type
9608      template parameter, in which case this is a non-type template
9609      parameter pack.  */
9610   else if (parameter_declarator
9611            && parameter_declarator->decl_specifiers.type
9612            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9613     {
9614       *is_parameter_pack = true;
9615       parameter_declarator->decl_specifiers.type = 
9616         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9617     }
9618
9619   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9620     {
9621       /* Parameter packs cannot have default arguments.  However, a
9622          user may try to do so, so we'll parse them and give an
9623          appropriate diagnostic here.  */
9624
9625       /* Consume the `='.  */
9626       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9627       cp_lexer_consume_token (parser->lexer);
9628       
9629       /* Find the name of the parameter pack.  */     
9630       id_declarator = parameter_declarator->declarator;
9631       while (id_declarator && id_declarator->kind != cdk_id)
9632         id_declarator = id_declarator->declarator;
9633       
9634       if (id_declarator && id_declarator->kind == cdk_id)
9635         error ("%Htemplate parameter pack %qD cannot have a default argument",
9636                &start_token->location, id_declarator->u.id.unqualified_name);
9637       else
9638         error ("%Htemplate parameter pack cannot have a default argument",
9639                &start_token->location);
9640       
9641       /* Parse the default argument, but throw away the result.  */
9642       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9643     }
9644
9645   parm = grokdeclarator (parameter_declarator->declarator,
9646                          &parameter_declarator->decl_specifiers,
9647                          PARM, /*initialized=*/0,
9648                          /*attrlist=*/NULL);
9649   if (parm == error_mark_node)
9650     return error_mark_node;
9651
9652   return build_tree_list (parameter_declarator->default_argument, parm);
9653 }
9654
9655 /* Parse a type-parameter.
9656
9657    type-parameter:
9658      class identifier [opt]
9659      class identifier [opt] = type-id
9660      typename identifier [opt]
9661      typename identifier [opt] = type-id
9662      template < template-parameter-list > class identifier [opt]
9663      template < template-parameter-list > class identifier [opt]
9664        = id-expression
9665
9666    GNU Extension (variadic templates):
9667
9668    type-parameter:
9669      class ... identifier [opt]
9670      typename ... identifier [opt]
9671
9672    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9673    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9674    the declaration of the parameter.
9675
9676    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9677
9678 static tree
9679 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9680 {
9681   cp_token *token;
9682   tree parameter;
9683
9684   /* Look for a keyword to tell us what kind of parameter this is.  */
9685   token = cp_parser_require (parser, CPP_KEYWORD,
9686                              "%<class%>, %<typename%>, or %<template%>");
9687   if (!token)
9688     return error_mark_node;
9689
9690   switch (token->keyword)
9691     {
9692     case RID_CLASS:
9693     case RID_TYPENAME:
9694       {
9695         tree identifier;
9696         tree default_argument;
9697
9698         /* If the next token is an ellipsis, we have a template
9699            argument pack. */
9700         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9701           {
9702             /* Consume the `...' token. */
9703             cp_lexer_consume_token (parser->lexer);
9704             maybe_warn_variadic_templates ();
9705
9706             *is_parameter_pack = true;
9707           }
9708
9709         /* If the next token is an identifier, then it names the
9710            parameter.  */
9711         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9712           identifier = cp_parser_identifier (parser);
9713         else
9714           identifier = NULL_TREE;
9715
9716         /* Create the parameter.  */
9717         parameter = finish_template_type_parm (class_type_node, identifier);
9718
9719         /* If the next token is an `=', we have a default argument.  */
9720         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9721           {
9722             /* Consume the `=' token.  */
9723             cp_lexer_consume_token (parser->lexer);
9724             /* Parse the default-argument.  */
9725             push_deferring_access_checks (dk_no_deferred);
9726             default_argument = cp_parser_type_id (parser);
9727
9728             /* Template parameter packs cannot have default
9729                arguments. */
9730             if (*is_parameter_pack)
9731               {
9732                 if (identifier)
9733                   error ("%Htemplate parameter pack %qD cannot have a "
9734                          "default argument", &token->location, identifier);
9735                 else
9736                   error ("%Htemplate parameter packs cannot have "
9737                          "default arguments", &token->location);
9738                 default_argument = NULL_TREE;
9739               }
9740             pop_deferring_access_checks ();
9741           }
9742         else
9743           default_argument = NULL_TREE;
9744
9745         /* Create the combined representation of the parameter and the
9746            default argument.  */
9747         parameter = build_tree_list (default_argument, parameter);
9748       }
9749       break;
9750
9751     case RID_TEMPLATE:
9752       {
9753         tree parameter_list;
9754         tree identifier;
9755         tree default_argument;
9756
9757         /* Look for the `<'.  */
9758         cp_parser_require (parser, CPP_LESS, "%<<%>");
9759         /* Parse the template-parameter-list.  */
9760         parameter_list = cp_parser_template_parameter_list (parser);
9761         /* Look for the `>'.  */
9762         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9763         /* Look for the `class' keyword.  */
9764         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9765         /* If the next token is an ellipsis, we have a template
9766            argument pack. */
9767         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9768           {
9769             /* Consume the `...' token. */
9770             cp_lexer_consume_token (parser->lexer);
9771             maybe_warn_variadic_templates ();
9772
9773             *is_parameter_pack = true;
9774           }
9775         /* If the next token is an `=', then there is a
9776            default-argument.  If the next token is a `>', we are at
9777            the end of the parameter-list.  If the next token is a `,',
9778            then we are at the end of this parameter.  */
9779         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9780             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9781             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9782           {
9783             identifier = cp_parser_identifier (parser);
9784             /* Treat invalid names as if the parameter were nameless.  */
9785             if (identifier == error_mark_node)
9786               identifier = NULL_TREE;
9787           }
9788         else
9789           identifier = NULL_TREE;
9790
9791         /* Create the template parameter.  */
9792         parameter = finish_template_template_parm (class_type_node,
9793                                                    identifier);
9794
9795         /* If the next token is an `=', then there is a
9796            default-argument.  */
9797         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9798           {
9799             bool is_template;
9800
9801             /* Consume the `='.  */
9802             cp_lexer_consume_token (parser->lexer);
9803             /* Parse the id-expression.  */
9804             push_deferring_access_checks (dk_no_deferred);
9805             /* save token before parsing the id-expression, for error
9806                reporting */
9807             token = cp_lexer_peek_token (parser->lexer);
9808             default_argument
9809               = cp_parser_id_expression (parser,
9810                                          /*template_keyword_p=*/false,
9811                                          /*check_dependency_p=*/true,
9812                                          /*template_p=*/&is_template,
9813                                          /*declarator_p=*/false,
9814                                          /*optional_p=*/false);
9815             if (TREE_CODE (default_argument) == TYPE_DECL)
9816               /* If the id-expression was a template-id that refers to
9817                  a template-class, we already have the declaration here,
9818                  so no further lookup is needed.  */
9819                  ;
9820             else
9821               /* Look up the name.  */
9822               default_argument
9823                 = cp_parser_lookup_name (parser, default_argument,
9824                                          none_type,
9825                                          /*is_template=*/is_template,
9826                                          /*is_namespace=*/false,
9827                                          /*check_dependency=*/true,
9828                                          /*ambiguous_decls=*/NULL,
9829                                          token->location);
9830             /* See if the default argument is valid.  */
9831             default_argument
9832               = check_template_template_default_arg (default_argument);
9833
9834             /* Template parameter packs cannot have default
9835                arguments. */
9836             if (*is_parameter_pack)
9837               {
9838                 if (identifier)
9839                   error ("%Htemplate parameter pack %qD cannot "
9840                          "have a default argument",
9841                          &token->location, identifier);
9842                 else
9843                   error ("%Htemplate parameter packs cannot "
9844                          "have default arguments",
9845                          &token->location);
9846                 default_argument = NULL_TREE;
9847               }
9848             pop_deferring_access_checks ();
9849           }
9850         else
9851           default_argument = NULL_TREE;
9852
9853         /* Create the combined representation of the parameter and the
9854            default argument.  */
9855         parameter = build_tree_list (default_argument, parameter);
9856       }
9857       break;
9858
9859     default:
9860       gcc_unreachable ();
9861       break;
9862     }
9863
9864   return parameter;
9865 }
9866
9867 /* Parse a template-id.
9868
9869    template-id:
9870      template-name < template-argument-list [opt] >
9871
9872    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9873    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9874    returned.  Otherwise, if the template-name names a function, or set
9875    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9876    names a class, returns a TYPE_DECL for the specialization.
9877
9878    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9879    uninstantiated templates.  */
9880
9881 static tree
9882 cp_parser_template_id (cp_parser *parser,
9883                        bool template_keyword_p,
9884                        bool check_dependency_p,
9885                        bool is_declaration)
9886 {
9887   int i;
9888   tree templ;
9889   tree arguments;
9890   tree template_id;
9891   cp_token_position start_of_id = 0;
9892   deferred_access_check *chk;
9893   VEC (deferred_access_check,gc) *access_check;
9894   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9895   bool is_identifier;
9896
9897   /* If the next token corresponds to a template-id, there is no need
9898      to reparse it.  */
9899   next_token = cp_lexer_peek_token (parser->lexer);
9900   if (next_token->type == CPP_TEMPLATE_ID)
9901     {
9902       struct tree_check *check_value;
9903
9904       /* Get the stored value.  */
9905       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9906       /* Perform any access checks that were deferred.  */
9907       access_check = check_value->checks;
9908       if (access_check)
9909         {
9910           for (i = 0 ;
9911                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9912                ++i)
9913             {
9914               perform_or_defer_access_check (chk->binfo,
9915                                              chk->decl,
9916                                              chk->diag_decl);
9917             }
9918         }
9919       /* Return the stored value.  */
9920       return check_value->value;
9921     }
9922
9923   /* Avoid performing name lookup if there is no possibility of
9924      finding a template-id.  */
9925   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9926       || (next_token->type == CPP_NAME
9927           && !cp_parser_nth_token_starts_template_argument_list_p
9928                (parser, 2)))
9929     {
9930       cp_parser_error (parser, "expected template-id");
9931       return error_mark_node;
9932     }
9933
9934   /* Remember where the template-id starts.  */
9935   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9936     start_of_id = cp_lexer_token_position (parser->lexer, false);
9937
9938   push_deferring_access_checks (dk_deferred);
9939
9940   /* Parse the template-name.  */
9941   is_identifier = false;
9942   token = cp_lexer_peek_token (parser->lexer);
9943   templ = cp_parser_template_name (parser, template_keyword_p,
9944                                    check_dependency_p,
9945                                    is_declaration,
9946                                    &is_identifier);
9947   if (templ == error_mark_node || is_identifier)
9948     {
9949       pop_deferring_access_checks ();
9950       return templ;
9951     }
9952
9953   /* If we find the sequence `[:' after a template-name, it's probably
9954      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9955      parse correctly the argument list.  */
9956   next_token = cp_lexer_peek_token (parser->lexer);
9957   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9958   if (next_token->type == CPP_OPEN_SQUARE
9959       && next_token->flags & DIGRAPH
9960       && next_token_2->type == CPP_COLON
9961       && !(next_token_2->flags & PREV_WHITE))
9962     {
9963       cp_parser_parse_tentatively (parser);
9964       /* Change `:' into `::'.  */
9965       next_token_2->type = CPP_SCOPE;
9966       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9967          CPP_LESS.  */
9968       cp_lexer_consume_token (parser->lexer);
9969
9970       /* Parse the arguments.  */
9971       arguments = cp_parser_enclosed_template_argument_list (parser);
9972       if (!cp_parser_parse_definitely (parser))
9973         {
9974           /* If we couldn't parse an argument list, then we revert our changes
9975              and return simply an error. Maybe this is not a template-id
9976              after all.  */
9977           next_token_2->type = CPP_COLON;
9978           cp_parser_error (parser, "expected %<<%>");
9979           pop_deferring_access_checks ();
9980           return error_mark_node;
9981         }
9982       /* Otherwise, emit an error about the invalid digraph, but continue
9983          parsing because we got our argument list.  */
9984       permerror ("%H%<<::%> cannot begin a template-argument list",
9985                  &next_token->location);
9986       inform ("%H%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9987               "between %<<%> and %<::%>",
9988               &next_token->location);
9989       if (!flag_permissive)
9990         {
9991           static bool hint;
9992           if (!hint)
9993             {
9994               inform ("%H(if you use %<-fpermissive%> G++ will accept your code)",
9995                       &next_token->location);
9996               hint = true;
9997             }
9998         }
9999     }
10000   else
10001     {
10002       /* Look for the `<' that starts the template-argument-list.  */
10003       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10004         {
10005           pop_deferring_access_checks ();
10006           return error_mark_node;
10007         }
10008       /* Parse the arguments.  */
10009       arguments = cp_parser_enclosed_template_argument_list (parser);
10010     }
10011
10012   /* Build a representation of the specialization.  */
10013   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10014     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10015   else if (DECL_CLASS_TEMPLATE_P (templ)
10016            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10017     {
10018       bool entering_scope;
10019       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10020          template (rather than some instantiation thereof) only if
10021          is not nested within some other construct.  For example, in
10022          "template <typename T> void f(T) { A<T>::", A<T> is just an
10023          instantiation of A.  */
10024       entering_scope = (template_parm_scope_p ()
10025                         && cp_lexer_next_token_is (parser->lexer,
10026                                                    CPP_SCOPE));
10027       template_id
10028         = finish_template_type (templ, arguments, entering_scope);
10029     }
10030   else
10031     {
10032       /* If it's not a class-template or a template-template, it should be
10033          a function-template.  */
10034       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10035                    || TREE_CODE (templ) == OVERLOAD
10036                    || BASELINK_P (templ)));
10037
10038       template_id = lookup_template_function (templ, arguments);
10039     }
10040
10041   /* If parsing tentatively, replace the sequence of tokens that makes
10042      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10043      should we re-parse the token stream, we will not have to repeat
10044      the effort required to do the parse, nor will we issue duplicate
10045      error messages about problems during instantiation of the
10046      template.  */
10047   if (start_of_id)
10048     {
10049       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10050
10051       /* Reset the contents of the START_OF_ID token.  */
10052       token->type = CPP_TEMPLATE_ID;
10053       /* Retrieve any deferred checks.  Do not pop this access checks yet
10054          so the memory will not be reclaimed during token replacing below.  */
10055       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10056       token->u.tree_check_value->value = template_id;
10057       token->u.tree_check_value->checks = get_deferred_access_checks ();
10058       token->keyword = RID_MAX;
10059
10060       /* Purge all subsequent tokens.  */
10061       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10062
10063       /* ??? Can we actually assume that, if template_id ==
10064          error_mark_node, we will have issued a diagnostic to the
10065          user, as opposed to simply marking the tentative parse as
10066          failed?  */
10067       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10068         error ("%Hparse error in template argument list",
10069                &token->location);
10070     }
10071
10072   pop_deferring_access_checks ();
10073   return template_id;
10074 }
10075
10076 /* Parse a template-name.
10077
10078    template-name:
10079      identifier
10080
10081    The standard should actually say:
10082
10083    template-name:
10084      identifier
10085      operator-function-id
10086
10087    A defect report has been filed about this issue.
10088
10089    A conversion-function-id cannot be a template name because they cannot
10090    be part of a template-id. In fact, looking at this code:
10091
10092    a.operator K<int>()
10093
10094    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10095    It is impossible to call a templated conversion-function-id with an
10096    explicit argument list, since the only allowed template parameter is
10097    the type to which it is converting.
10098
10099    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10100    `template' keyword, in a construction like:
10101
10102      T::template f<3>()
10103
10104    In that case `f' is taken to be a template-name, even though there
10105    is no way of knowing for sure.
10106
10107    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10108    name refers to a set of overloaded functions, at least one of which
10109    is a template, or an IDENTIFIER_NODE with the name of the template,
10110    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10111    names are looked up inside uninstantiated templates.  */
10112
10113 static tree
10114 cp_parser_template_name (cp_parser* parser,
10115                          bool template_keyword_p,
10116                          bool check_dependency_p,
10117                          bool is_declaration,
10118                          bool *is_identifier)
10119 {
10120   tree identifier;
10121   tree decl;
10122   tree fns;
10123   cp_token *token = cp_lexer_peek_token (parser->lexer);
10124
10125   /* If the next token is `operator', then we have either an
10126      operator-function-id or a conversion-function-id.  */
10127   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10128     {
10129       /* We don't know whether we're looking at an
10130          operator-function-id or a conversion-function-id.  */
10131       cp_parser_parse_tentatively (parser);
10132       /* Try an operator-function-id.  */
10133       identifier = cp_parser_operator_function_id (parser);
10134       /* If that didn't work, try a conversion-function-id.  */
10135       if (!cp_parser_parse_definitely (parser))
10136         {
10137           cp_parser_error (parser, "expected template-name");
10138           return error_mark_node;
10139         }
10140     }
10141   /* Look for the identifier.  */
10142   else
10143     identifier = cp_parser_identifier (parser);
10144
10145   /* If we didn't find an identifier, we don't have a template-id.  */
10146   if (identifier == error_mark_node)
10147     return error_mark_node;
10148
10149   /* If the name immediately followed the `template' keyword, then it
10150      is a template-name.  However, if the next token is not `<', then
10151      we do not treat it as a template-name, since it is not being used
10152      as part of a template-id.  This enables us to handle constructs
10153      like:
10154
10155        template <typename T> struct S { S(); };
10156        template <typename T> S<T>::S();
10157
10158      correctly.  We would treat `S' as a template -- if it were `S<T>'
10159      -- but we do not if there is no `<'.  */
10160
10161   if (processing_template_decl
10162       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10163     {
10164       /* In a declaration, in a dependent context, we pretend that the
10165          "template" keyword was present in order to improve error
10166          recovery.  For example, given:
10167
10168            template <typename T> void f(T::X<int>);
10169
10170          we want to treat "X<int>" as a template-id.  */
10171       if (is_declaration
10172           && !template_keyword_p
10173           && parser->scope && TYPE_P (parser->scope)
10174           && check_dependency_p
10175           && dependent_type_p (parser->scope)
10176           /* Do not do this for dtors (or ctors), since they never
10177              need the template keyword before their name.  */
10178           && !constructor_name_p (identifier, parser->scope))
10179         {
10180           cp_token_position start = 0;
10181
10182           /* Explain what went wrong.  */
10183           error ("%Hnon-template %qD used as template",
10184                  &token->location, identifier);
10185           inform ("use %<%T::template %D%> to indicate that it is a template",
10186                   parser->scope, identifier);
10187           /* If parsing tentatively, find the location of the "<" token.  */
10188           if (cp_parser_simulate_error (parser))
10189             start = cp_lexer_token_position (parser->lexer, true);
10190           /* Parse the template arguments so that we can issue error
10191              messages about them.  */
10192           cp_lexer_consume_token (parser->lexer);
10193           cp_parser_enclosed_template_argument_list (parser);
10194           /* Skip tokens until we find a good place from which to
10195              continue parsing.  */
10196           cp_parser_skip_to_closing_parenthesis (parser,
10197                                                  /*recovering=*/true,
10198                                                  /*or_comma=*/true,
10199                                                  /*consume_paren=*/false);
10200           /* If parsing tentatively, permanently remove the
10201              template argument list.  That will prevent duplicate
10202              error messages from being issued about the missing
10203              "template" keyword.  */
10204           if (start)
10205             cp_lexer_purge_tokens_after (parser->lexer, start);
10206           if (is_identifier)
10207             *is_identifier = true;
10208           return identifier;
10209         }
10210
10211       /* If the "template" keyword is present, then there is generally
10212          no point in doing name-lookup, so we just return IDENTIFIER.
10213          But, if the qualifying scope is non-dependent then we can
10214          (and must) do name-lookup normally.  */
10215       if (template_keyword_p
10216           && (!parser->scope
10217               || (TYPE_P (parser->scope)
10218                   && dependent_type_p (parser->scope))))
10219         return identifier;
10220     }
10221
10222   /* Look up the name.  */
10223   decl = cp_parser_lookup_name (parser, identifier,
10224                                 none_type,
10225                                 /*is_template=*/false,
10226                                 /*is_namespace=*/false,
10227                                 check_dependency_p,
10228                                 /*ambiguous_decls=*/NULL,
10229                                 token->location);
10230   decl = maybe_get_template_decl_from_type_decl (decl);
10231
10232   /* If DECL is a template, then the name was a template-name.  */
10233   if (TREE_CODE (decl) == TEMPLATE_DECL)
10234     ;
10235   else
10236     {
10237       tree fn = NULL_TREE;
10238
10239       /* The standard does not explicitly indicate whether a name that
10240          names a set of overloaded declarations, some of which are
10241          templates, is a template-name.  However, such a name should
10242          be a template-name; otherwise, there is no way to form a
10243          template-id for the overloaded templates.  */
10244       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10245       if (TREE_CODE (fns) == OVERLOAD)
10246         for (fn = fns; fn; fn = OVL_NEXT (fn))
10247           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10248             break;
10249
10250       if (!fn)
10251         {
10252           /* The name does not name a template.  */
10253           cp_parser_error (parser, "expected template-name");
10254           return error_mark_node;
10255         }
10256     }
10257
10258   /* If DECL is dependent, and refers to a function, then just return
10259      its name; we will look it up again during template instantiation.  */
10260   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10261     {
10262       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10263       if (TYPE_P (scope) && dependent_type_p (scope))
10264         return identifier;
10265     }
10266
10267   return decl;
10268 }
10269
10270 /* Parse a template-argument-list.
10271
10272    template-argument-list:
10273      template-argument ... [opt]
10274      template-argument-list , template-argument ... [opt]
10275
10276    Returns a TREE_VEC containing the arguments.  */
10277
10278 static tree
10279 cp_parser_template_argument_list (cp_parser* parser)
10280 {
10281   tree fixed_args[10];
10282   unsigned n_args = 0;
10283   unsigned alloced = 10;
10284   tree *arg_ary = fixed_args;
10285   tree vec;
10286   bool saved_in_template_argument_list_p;
10287   bool saved_ice_p;
10288   bool saved_non_ice_p;
10289
10290   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10291   parser->in_template_argument_list_p = true;
10292   /* Even if the template-id appears in an integral
10293      constant-expression, the contents of the argument list do
10294      not.  */
10295   saved_ice_p = parser->integral_constant_expression_p;
10296   parser->integral_constant_expression_p = false;
10297   saved_non_ice_p = parser->non_integral_constant_expression_p;
10298   parser->non_integral_constant_expression_p = false;
10299   /* Parse the arguments.  */
10300   do
10301     {
10302       tree argument;
10303
10304       if (n_args)
10305         /* Consume the comma.  */
10306         cp_lexer_consume_token (parser->lexer);
10307
10308       /* Parse the template-argument.  */
10309       argument = cp_parser_template_argument (parser);
10310
10311       /* If the next token is an ellipsis, we're expanding a template
10312          argument pack. */
10313       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10314         {
10315           /* Consume the `...' token. */
10316           cp_lexer_consume_token (parser->lexer);
10317
10318           /* Make the argument into a TYPE_PACK_EXPANSION or
10319              EXPR_PACK_EXPANSION. */
10320           argument = make_pack_expansion (argument);
10321         }
10322
10323       if (n_args == alloced)
10324         {
10325           alloced *= 2;
10326
10327           if (arg_ary == fixed_args)
10328             {
10329               arg_ary = XNEWVEC (tree, alloced);
10330               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10331             }
10332           else
10333             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10334         }
10335       arg_ary[n_args++] = argument;
10336     }
10337   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10338
10339   vec = make_tree_vec (n_args);
10340
10341   while (n_args--)
10342     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10343
10344   if (arg_ary != fixed_args)
10345     free (arg_ary);
10346   parser->non_integral_constant_expression_p = saved_non_ice_p;
10347   parser->integral_constant_expression_p = saved_ice_p;
10348   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10349   return vec;
10350 }
10351
10352 /* Parse a template-argument.
10353
10354    template-argument:
10355      assignment-expression
10356      type-id
10357      id-expression
10358
10359    The representation is that of an assignment-expression, type-id, or
10360    id-expression -- except that the qualified id-expression is
10361    evaluated, so that the value returned is either a DECL or an
10362    OVERLOAD.
10363
10364    Although the standard says "assignment-expression", it forbids
10365    throw-expressions or assignments in the template argument.
10366    Therefore, we use "conditional-expression" instead.  */
10367
10368 static tree
10369 cp_parser_template_argument (cp_parser* parser)
10370 {
10371   tree argument;
10372   bool template_p;
10373   bool address_p;
10374   bool maybe_type_id = false;
10375   cp_token *token = NULL, *argument_start_token = NULL;
10376   cp_id_kind idk;
10377
10378   /* There's really no way to know what we're looking at, so we just
10379      try each alternative in order.
10380
10381        [temp.arg]
10382
10383        In a template-argument, an ambiguity between a type-id and an
10384        expression is resolved to a type-id, regardless of the form of
10385        the corresponding template-parameter.
10386
10387      Therefore, we try a type-id first.  */
10388   cp_parser_parse_tentatively (parser);
10389   argument = cp_parser_type_id (parser);
10390   /* If there was no error parsing the type-id but the next token is a '>>',
10391      we probably found a typo for '> >'. But there are type-id which are
10392      also valid expressions. For instance:
10393
10394      struct X { int operator >> (int); };
10395      template <int V> struct Foo {};
10396      Foo<X () >> 5> r;
10397
10398      Here 'X()' is a valid type-id of a function type, but the user just
10399      wanted to write the expression "X() >> 5". Thus, we remember that we
10400      found a valid type-id, but we still try to parse the argument as an
10401      expression to see what happens.  */
10402   if (!cp_parser_error_occurred (parser)
10403       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10404     {
10405       maybe_type_id = true;
10406       cp_parser_abort_tentative_parse (parser);
10407     }
10408   else
10409     {
10410       /* If the next token isn't a `,' or a `>', then this argument wasn't
10411       really finished. This means that the argument is not a valid
10412       type-id.  */
10413       if (!cp_parser_next_token_ends_template_argument_p (parser))
10414         cp_parser_error (parser, "expected template-argument");
10415       /* If that worked, we're done.  */
10416       if (cp_parser_parse_definitely (parser))
10417         return argument;
10418     }
10419   /* We're still not sure what the argument will be.  */
10420   cp_parser_parse_tentatively (parser);
10421   /* Try a template.  */
10422   argument_start_token = cp_lexer_peek_token (parser->lexer);
10423   argument = cp_parser_id_expression (parser,
10424                                       /*template_keyword_p=*/false,
10425                                       /*check_dependency_p=*/true,
10426                                       &template_p,
10427                                       /*declarator_p=*/false,
10428                                       /*optional_p=*/false);
10429   /* If the next token isn't a `,' or a `>', then this argument wasn't
10430      really finished.  */
10431   if (!cp_parser_next_token_ends_template_argument_p (parser))
10432     cp_parser_error (parser, "expected template-argument");
10433   if (!cp_parser_error_occurred (parser))
10434     {
10435       /* Figure out what is being referred to.  If the id-expression
10436          was for a class template specialization, then we will have a
10437          TYPE_DECL at this point.  There is no need to do name lookup
10438          at this point in that case.  */
10439       if (TREE_CODE (argument) != TYPE_DECL)
10440         argument = cp_parser_lookup_name (parser, argument,
10441                                           none_type,
10442                                           /*is_template=*/template_p,
10443                                           /*is_namespace=*/false,
10444                                           /*check_dependency=*/true,
10445                                           /*ambiguous_decls=*/NULL,
10446                                           argument_start_token->location);
10447       if (TREE_CODE (argument) != TEMPLATE_DECL
10448           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10449         cp_parser_error (parser, "expected template-name");
10450     }
10451   if (cp_parser_parse_definitely (parser))
10452     return argument;
10453   /* It must be a non-type argument.  There permitted cases are given
10454      in [temp.arg.nontype]:
10455
10456      -- an integral constant-expression of integral or enumeration
10457         type; or
10458
10459      -- the name of a non-type template-parameter; or
10460
10461      -- the name of an object or function with external linkage...
10462
10463      -- the address of an object or function with external linkage...
10464
10465      -- a pointer to member...  */
10466   /* Look for a non-type template parameter.  */
10467   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10468     {
10469       cp_parser_parse_tentatively (parser);
10470       argument = cp_parser_primary_expression (parser,
10471                                                /*adress_p=*/false,
10472                                                /*cast_p=*/false,
10473                                                /*template_arg_p=*/true,
10474                                                &idk);
10475       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10476           || !cp_parser_next_token_ends_template_argument_p (parser))
10477         cp_parser_simulate_error (parser);
10478       if (cp_parser_parse_definitely (parser))
10479         return argument;
10480     }
10481
10482   /* If the next token is "&", the argument must be the address of an
10483      object or function with external linkage.  */
10484   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10485   if (address_p)
10486     cp_lexer_consume_token (parser->lexer);
10487   /* See if we might have an id-expression.  */
10488   token = cp_lexer_peek_token (parser->lexer);
10489   if (token->type == CPP_NAME
10490       || token->keyword == RID_OPERATOR
10491       || token->type == CPP_SCOPE
10492       || token->type == CPP_TEMPLATE_ID
10493       || token->type == CPP_NESTED_NAME_SPECIFIER)
10494     {
10495       cp_parser_parse_tentatively (parser);
10496       argument = cp_parser_primary_expression (parser,
10497                                                address_p,
10498                                                /*cast_p=*/false,
10499                                                /*template_arg_p=*/true,
10500                                                &idk);
10501       if (cp_parser_error_occurred (parser)
10502           || !cp_parser_next_token_ends_template_argument_p (parser))
10503         cp_parser_abort_tentative_parse (parser);
10504       else
10505         {
10506           if (TREE_CODE (argument) == INDIRECT_REF)
10507             {
10508               gcc_assert (REFERENCE_REF_P (argument));
10509               argument = TREE_OPERAND (argument, 0);
10510             }
10511
10512           if (TREE_CODE (argument) == VAR_DECL)
10513             {
10514               /* A variable without external linkage might still be a
10515                  valid constant-expression, so no error is issued here
10516                  if the external-linkage check fails.  */
10517               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10518                 cp_parser_simulate_error (parser);
10519             }
10520           else if (is_overloaded_fn (argument))
10521             /* All overloaded functions are allowed; if the external
10522                linkage test does not pass, an error will be issued
10523                later.  */
10524             ;
10525           else if (address_p
10526                    && (TREE_CODE (argument) == OFFSET_REF
10527                        || TREE_CODE (argument) == SCOPE_REF))
10528             /* A pointer-to-member.  */
10529             ;
10530           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10531             ;
10532           else
10533             cp_parser_simulate_error (parser);
10534
10535           if (cp_parser_parse_definitely (parser))
10536             {
10537               if (address_p)
10538                 argument = build_x_unary_op (ADDR_EXPR, argument,
10539                                              tf_warning_or_error);
10540               return argument;
10541             }
10542         }
10543     }
10544   /* If the argument started with "&", there are no other valid
10545      alternatives at this point.  */
10546   if (address_p)
10547     {
10548       cp_parser_error (parser, "invalid non-type template argument");
10549       return error_mark_node;
10550     }
10551
10552   /* If the argument wasn't successfully parsed as a type-id followed
10553      by '>>', the argument can only be a constant expression now.
10554      Otherwise, we try parsing the constant-expression tentatively,
10555      because the argument could really be a type-id.  */
10556   if (maybe_type_id)
10557     cp_parser_parse_tentatively (parser);
10558   argument = cp_parser_constant_expression (parser,
10559                                             /*allow_non_constant_p=*/false,
10560                                             /*non_constant_p=*/NULL);
10561   argument = fold_non_dependent_expr (argument);
10562   if (!maybe_type_id)
10563     return argument;
10564   if (!cp_parser_next_token_ends_template_argument_p (parser))
10565     cp_parser_error (parser, "expected template-argument");
10566   if (cp_parser_parse_definitely (parser))
10567     return argument;
10568   /* We did our best to parse the argument as a non type-id, but that
10569      was the only alternative that matched (albeit with a '>' after
10570      it). We can assume it's just a typo from the user, and a
10571      diagnostic will then be issued.  */
10572   return cp_parser_type_id (parser);
10573 }
10574
10575 /* Parse an explicit-instantiation.
10576
10577    explicit-instantiation:
10578      template declaration
10579
10580    Although the standard says `declaration', what it really means is:
10581
10582    explicit-instantiation:
10583      template decl-specifier-seq [opt] declarator [opt] ;
10584
10585    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10586    supposed to be allowed.  A defect report has been filed about this
10587    issue.
10588
10589    GNU Extension:
10590
10591    explicit-instantiation:
10592      storage-class-specifier template
10593        decl-specifier-seq [opt] declarator [opt] ;
10594      function-specifier template
10595        decl-specifier-seq [opt] declarator [opt] ;  */
10596
10597 static void
10598 cp_parser_explicit_instantiation (cp_parser* parser)
10599 {
10600   int declares_class_or_enum;
10601   cp_decl_specifier_seq decl_specifiers;
10602   tree extension_specifier = NULL_TREE;
10603   cp_token *token;
10604
10605   /* Look for an (optional) storage-class-specifier or
10606      function-specifier.  */
10607   if (cp_parser_allow_gnu_extensions_p (parser))
10608     {
10609       extension_specifier
10610         = cp_parser_storage_class_specifier_opt (parser);
10611       if (!extension_specifier)
10612         extension_specifier
10613           = cp_parser_function_specifier_opt (parser,
10614                                               /*decl_specs=*/NULL);
10615     }
10616
10617   /* Look for the `template' keyword.  */
10618   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10619   /* Let the front end know that we are processing an explicit
10620      instantiation.  */
10621   begin_explicit_instantiation ();
10622   /* [temp.explicit] says that we are supposed to ignore access
10623      control while processing explicit instantiation directives.  */
10624   push_deferring_access_checks (dk_no_check);
10625   /* Parse a decl-specifier-seq.  */
10626   token = cp_lexer_peek_token (parser->lexer);
10627   cp_parser_decl_specifier_seq (parser,
10628                                 CP_PARSER_FLAGS_OPTIONAL,
10629                                 &decl_specifiers,
10630                                 &declares_class_or_enum);
10631   /* If there was exactly one decl-specifier, and it declared a class,
10632      and there's no declarator, then we have an explicit type
10633      instantiation.  */
10634   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10635     {
10636       tree type;
10637
10638       type = check_tag_decl (&decl_specifiers);
10639       /* Turn access control back on for names used during
10640          template instantiation.  */
10641       pop_deferring_access_checks ();
10642       if (type)
10643         do_type_instantiation (type, extension_specifier,
10644                                /*complain=*/tf_error);
10645     }
10646   else
10647     {
10648       cp_declarator *declarator;
10649       tree decl;
10650
10651       /* Parse the declarator.  */
10652       declarator
10653         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10654                                 /*ctor_dtor_or_conv_p=*/NULL,
10655                                 /*parenthesized_p=*/NULL,
10656                                 /*member_p=*/false);
10657       if (declares_class_or_enum & 2)
10658         cp_parser_check_for_definition_in_return_type (declarator,
10659                                                        decl_specifiers.type,
10660                                                        decl_specifiers.type_location);
10661       if (declarator != cp_error_declarator)
10662         {
10663           decl = grokdeclarator (declarator, &decl_specifiers,
10664                                  NORMAL, 0, &decl_specifiers.attributes);
10665           /* Turn access control back on for names used during
10666              template instantiation.  */
10667           pop_deferring_access_checks ();
10668           /* Do the explicit instantiation.  */
10669           do_decl_instantiation (decl, extension_specifier);
10670         }
10671       else
10672         {
10673           pop_deferring_access_checks ();
10674           /* Skip the body of the explicit instantiation.  */
10675           cp_parser_skip_to_end_of_statement (parser);
10676         }
10677     }
10678   /* We're done with the instantiation.  */
10679   end_explicit_instantiation ();
10680
10681   cp_parser_consume_semicolon_at_end_of_statement (parser);
10682 }
10683
10684 /* Parse an explicit-specialization.
10685
10686    explicit-specialization:
10687      template < > declaration
10688
10689    Although the standard says `declaration', what it really means is:
10690
10691    explicit-specialization:
10692      template <> decl-specifier [opt] init-declarator [opt] ;
10693      template <> function-definition
10694      template <> explicit-specialization
10695      template <> template-declaration  */
10696
10697 static void
10698 cp_parser_explicit_specialization (cp_parser* parser)
10699 {
10700   bool need_lang_pop;
10701   cp_token *token = cp_lexer_peek_token (parser->lexer);
10702
10703   /* Look for the `template' keyword.  */
10704   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10705   /* Look for the `<'.  */
10706   cp_parser_require (parser, CPP_LESS, "%<<%>");
10707   /* Look for the `>'.  */
10708   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10709   /* We have processed another parameter list.  */
10710   ++parser->num_template_parameter_lists;
10711   /* [temp]
10712
10713      A template ... explicit specialization ... shall not have C
10714      linkage.  */
10715   if (current_lang_name == lang_name_c)
10716     {
10717       error ("%Htemplate specialization with C linkage", &token->location);
10718       /* Give it C++ linkage to avoid confusing other parts of the
10719          front end.  */
10720       push_lang_context (lang_name_cplusplus);
10721       need_lang_pop = true;
10722     }
10723   else
10724     need_lang_pop = false;
10725   /* Let the front end know that we are beginning a specialization.  */
10726   if (!begin_specialization ())
10727     {
10728       end_specialization ();
10729       cp_parser_skip_to_end_of_block_or_statement (parser);
10730       return;
10731     }
10732
10733   /* If the next keyword is `template', we need to figure out whether
10734      or not we're looking a template-declaration.  */
10735   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10736     {
10737       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10738           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10739         cp_parser_template_declaration_after_export (parser,
10740                                                      /*member_p=*/false);
10741       else
10742         cp_parser_explicit_specialization (parser);
10743     }
10744   else
10745     /* Parse the dependent declaration.  */
10746     cp_parser_single_declaration (parser,
10747                                   /*checks=*/NULL,
10748                                   /*member_p=*/false,
10749                                   /*explicit_specialization_p=*/true,
10750                                   /*friend_p=*/NULL);
10751   /* We're done with the specialization.  */
10752   end_specialization ();
10753   /* For the erroneous case of a template with C linkage, we pushed an
10754      implicit C++ linkage scope; exit that scope now.  */
10755   if (need_lang_pop)
10756     pop_lang_context ();
10757   /* We're done with this parameter list.  */
10758   --parser->num_template_parameter_lists;
10759 }
10760
10761 /* Parse a type-specifier.
10762
10763    type-specifier:
10764      simple-type-specifier
10765      class-specifier
10766      enum-specifier
10767      elaborated-type-specifier
10768      cv-qualifier
10769
10770    GNU Extension:
10771
10772    type-specifier:
10773      __complex__
10774
10775    Returns a representation of the type-specifier.  For a
10776    class-specifier, enum-specifier, or elaborated-type-specifier, a
10777    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10778
10779    The parser flags FLAGS is used to control type-specifier parsing.
10780
10781    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10782    in a decl-specifier-seq.
10783
10784    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10785    class-specifier, enum-specifier, or elaborated-type-specifier, then
10786    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10787    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10788    zero.
10789
10790    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10791    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10792    is set to FALSE.  */
10793
10794 static tree
10795 cp_parser_type_specifier (cp_parser* parser,
10796                           cp_parser_flags flags,
10797                           cp_decl_specifier_seq *decl_specs,
10798                           bool is_declaration,
10799                           int* declares_class_or_enum,
10800                           bool* is_cv_qualifier)
10801 {
10802   tree type_spec = NULL_TREE;
10803   cp_token *token;
10804   enum rid keyword;
10805   cp_decl_spec ds = ds_last;
10806
10807   /* Assume this type-specifier does not declare a new type.  */
10808   if (declares_class_or_enum)
10809     *declares_class_or_enum = 0;
10810   /* And that it does not specify a cv-qualifier.  */
10811   if (is_cv_qualifier)
10812     *is_cv_qualifier = false;
10813   /* Peek at the next token.  */
10814   token = cp_lexer_peek_token (parser->lexer);
10815
10816   /* If we're looking at a keyword, we can use that to guide the
10817      production we choose.  */
10818   keyword = token->keyword;
10819   switch (keyword)
10820     {
10821     case RID_ENUM:
10822       /* Look for the enum-specifier.  */
10823       type_spec = cp_parser_enum_specifier (parser);
10824       /* If that worked, we're done.  */
10825       if (type_spec)
10826         {
10827           if (declares_class_or_enum)
10828             *declares_class_or_enum = 2;
10829           if (decl_specs)
10830             cp_parser_set_decl_spec_type (decl_specs,
10831                                           type_spec,
10832                                           token->location,
10833                                           /*user_defined_p=*/true);
10834           return type_spec;
10835         }
10836       else
10837         goto elaborated_type_specifier;
10838
10839       /* Any of these indicate either a class-specifier, or an
10840          elaborated-type-specifier.  */
10841     case RID_CLASS:
10842     case RID_STRUCT:
10843     case RID_UNION:
10844       /* Parse tentatively so that we can back up if we don't find a
10845          class-specifier.  */
10846       cp_parser_parse_tentatively (parser);
10847       /* Look for the class-specifier.  */
10848       type_spec = cp_parser_class_specifier (parser);
10849       /* If that worked, we're done.  */
10850       if (cp_parser_parse_definitely (parser))
10851         {
10852           if (declares_class_or_enum)
10853             *declares_class_or_enum = 2;
10854           if (decl_specs)
10855             cp_parser_set_decl_spec_type (decl_specs,
10856                                           type_spec,
10857                                           token->location,
10858                                           /*user_defined_p=*/true);
10859           return type_spec;
10860         }
10861
10862       /* Fall through.  */
10863     elaborated_type_specifier:
10864       /* We're declaring (not defining) a class or enum.  */
10865       if (declares_class_or_enum)
10866         *declares_class_or_enum = 1;
10867
10868       /* Fall through.  */
10869     case RID_TYPENAME:
10870       /* Look for an elaborated-type-specifier.  */
10871       type_spec
10872         = (cp_parser_elaborated_type_specifier
10873            (parser,
10874             decl_specs && decl_specs->specs[(int) ds_friend],
10875             is_declaration));
10876       if (decl_specs)
10877         cp_parser_set_decl_spec_type (decl_specs,
10878                                       type_spec,
10879                                       token->location,
10880                                       /*user_defined_p=*/true);
10881       return type_spec;
10882
10883     case RID_CONST:
10884       ds = ds_const;
10885       if (is_cv_qualifier)
10886         *is_cv_qualifier = true;
10887       break;
10888
10889     case RID_VOLATILE:
10890       ds = ds_volatile;
10891       if (is_cv_qualifier)
10892         *is_cv_qualifier = true;
10893       break;
10894
10895     case RID_RESTRICT:
10896       ds = ds_restrict;
10897       if (is_cv_qualifier)
10898         *is_cv_qualifier = true;
10899       break;
10900
10901     case RID_COMPLEX:
10902       /* The `__complex__' keyword is a GNU extension.  */
10903       ds = ds_complex;
10904       break;
10905
10906     default:
10907       break;
10908     }
10909
10910   /* Handle simple keywords.  */
10911   if (ds != ds_last)
10912     {
10913       if (decl_specs)
10914         {
10915           ++decl_specs->specs[(int)ds];
10916           decl_specs->any_specifiers_p = true;
10917         }
10918       return cp_lexer_consume_token (parser->lexer)->u.value;
10919     }
10920
10921   /* If we do not already have a type-specifier, assume we are looking
10922      at a simple-type-specifier.  */
10923   type_spec = cp_parser_simple_type_specifier (parser,
10924                                                decl_specs,
10925                                                flags);
10926
10927   /* If we didn't find a type-specifier, and a type-specifier was not
10928      optional in this context, issue an error message.  */
10929   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10930     {
10931       cp_parser_error (parser, "expected type specifier");
10932       return error_mark_node;
10933     }
10934
10935   return type_spec;
10936 }
10937
10938 /* Parse a simple-type-specifier.
10939
10940    simple-type-specifier:
10941      :: [opt] nested-name-specifier [opt] type-name
10942      :: [opt] nested-name-specifier template template-id
10943      char
10944      wchar_t
10945      bool
10946      short
10947      int
10948      long
10949      signed
10950      unsigned
10951      float
10952      double
10953      void
10954
10955    C++0x Extension:
10956
10957    simple-type-specifier:
10958      auto
10959      decltype ( expression )   
10960      char16_t
10961      char32_t
10962
10963    GNU Extension:
10964
10965    simple-type-specifier:
10966      __typeof__ unary-expression
10967      __typeof__ ( type-id )
10968
10969    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10970    appropriately updated.  */
10971
10972 static tree
10973 cp_parser_simple_type_specifier (cp_parser* parser,
10974                                  cp_decl_specifier_seq *decl_specs,
10975                                  cp_parser_flags flags)
10976 {
10977   tree type = NULL_TREE;
10978   cp_token *token;
10979
10980   /* Peek at the next token.  */
10981   token = cp_lexer_peek_token (parser->lexer);
10982
10983   /* If we're looking at a keyword, things are easy.  */
10984   switch (token->keyword)
10985     {
10986     case RID_CHAR:
10987       if (decl_specs)
10988         decl_specs->explicit_char_p = true;
10989       type = char_type_node;
10990       break;
10991     case RID_CHAR16:
10992       type = char16_type_node;
10993       break;
10994     case RID_CHAR32:
10995       type = char32_type_node;
10996       break;
10997     case RID_WCHAR:
10998       type = wchar_type_node;
10999       break;
11000     case RID_BOOL:
11001       type = boolean_type_node;
11002       break;
11003     case RID_SHORT:
11004       if (decl_specs)
11005         ++decl_specs->specs[(int) ds_short];
11006       type = short_integer_type_node;
11007       break;
11008     case RID_INT:
11009       if (decl_specs)
11010         decl_specs->explicit_int_p = true;
11011       type = integer_type_node;
11012       break;
11013     case RID_LONG:
11014       if (decl_specs)
11015         ++decl_specs->specs[(int) ds_long];
11016       type = long_integer_type_node;
11017       break;
11018     case RID_SIGNED:
11019       if (decl_specs)
11020         ++decl_specs->specs[(int) ds_signed];
11021       type = integer_type_node;
11022       break;
11023     case RID_UNSIGNED:
11024       if (decl_specs)
11025         ++decl_specs->specs[(int) ds_unsigned];
11026       type = unsigned_type_node;
11027       break;
11028     case RID_FLOAT:
11029       type = float_type_node;
11030       break;
11031     case RID_DOUBLE:
11032       type = double_type_node;
11033       break;
11034     case RID_VOID:
11035       type = void_type_node;
11036       break;
11037       
11038     case RID_AUTO:
11039       if (cxx_dialect != cxx98)
11040         {
11041           /* Consume the token.  */
11042           cp_lexer_consume_token (parser->lexer);
11043           /* We do not yet support the use of `auto' as a
11044              type-specifier.  */
11045           error ("%HC++0x %<auto%> specifier not supported", &token->location);
11046         }
11047       break;
11048
11049     case RID_DECLTYPE:
11050       /* Parse the `decltype' type.  */
11051       type = cp_parser_decltype (parser);
11052
11053       if (decl_specs)
11054         cp_parser_set_decl_spec_type (decl_specs, type,
11055                                       token->location,
11056                                       /*user_defined_p=*/true);
11057
11058       return type;
11059
11060     case RID_TYPEOF:
11061       /* Consume the `typeof' token.  */
11062       cp_lexer_consume_token (parser->lexer);
11063       /* Parse the operand to `typeof'.  */
11064       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11065       /* If it is not already a TYPE, take its type.  */
11066       if (!TYPE_P (type))
11067         type = finish_typeof (type);
11068
11069       if (decl_specs)
11070         cp_parser_set_decl_spec_type (decl_specs, type,
11071                                       token->location,
11072                                       /*user_defined_p=*/true);
11073
11074       return type;
11075
11076     default:
11077       break;
11078     }
11079
11080   /* If the type-specifier was for a built-in type, we're done.  */
11081   if (type)
11082     {
11083       tree id;
11084
11085       /* Record the type.  */
11086       if (decl_specs
11087           && (token->keyword != RID_SIGNED
11088               && token->keyword != RID_UNSIGNED
11089               && token->keyword != RID_SHORT
11090               && token->keyword != RID_LONG))
11091         cp_parser_set_decl_spec_type (decl_specs,
11092                                       type,
11093                                       token->location,
11094                                       /*user_defined=*/false);
11095       if (decl_specs)
11096         decl_specs->any_specifiers_p = true;
11097
11098       /* Consume the token.  */
11099       id = cp_lexer_consume_token (parser->lexer)->u.value;
11100
11101       /* There is no valid C++ program where a non-template type is
11102          followed by a "<".  That usually indicates that the user thought
11103          that the type was a template.  */
11104       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11105
11106       return TYPE_NAME (type);
11107     }
11108
11109   /* The type-specifier must be a user-defined type.  */
11110   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11111     {
11112       bool qualified_p;
11113       bool global_p;
11114
11115       /* Don't gobble tokens or issue error messages if this is an
11116          optional type-specifier.  */
11117       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11118         cp_parser_parse_tentatively (parser);
11119
11120       /* Look for the optional `::' operator.  */
11121       global_p
11122         = (cp_parser_global_scope_opt (parser,
11123                                        /*current_scope_valid_p=*/false)
11124            != NULL_TREE);
11125       /* Look for the nested-name specifier.  */
11126       qualified_p
11127         = (cp_parser_nested_name_specifier_opt (parser,
11128                                                 /*typename_keyword_p=*/false,
11129                                                 /*check_dependency_p=*/true,
11130                                                 /*type_p=*/false,
11131                                                 /*is_declaration=*/false)
11132            != NULL_TREE);
11133       token = cp_lexer_peek_token (parser->lexer);
11134       /* If we have seen a nested-name-specifier, and the next token
11135          is `template', then we are using the template-id production.  */
11136       if (parser->scope
11137           && cp_parser_optional_template_keyword (parser))
11138         {
11139           /* Look for the template-id.  */
11140           type = cp_parser_template_id (parser,
11141                                         /*template_keyword_p=*/true,
11142                                         /*check_dependency_p=*/true,
11143                                         /*is_declaration=*/false);
11144           /* If the template-id did not name a type, we are out of
11145              luck.  */
11146           if (TREE_CODE (type) != TYPE_DECL)
11147             {
11148               cp_parser_error (parser, "expected template-id for type");
11149               type = NULL_TREE;
11150             }
11151         }
11152       /* Otherwise, look for a type-name.  */
11153       else
11154         type = cp_parser_type_name (parser);
11155       /* Keep track of all name-lookups performed in class scopes.  */
11156       if (type
11157           && !global_p
11158           && !qualified_p
11159           && TREE_CODE (type) == TYPE_DECL
11160           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11161         maybe_note_name_used_in_class (DECL_NAME (type), type);
11162       /* If it didn't work out, we don't have a TYPE.  */
11163       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11164           && !cp_parser_parse_definitely (parser))
11165         type = NULL_TREE;
11166       if (type && decl_specs)
11167         cp_parser_set_decl_spec_type (decl_specs, type,
11168                                       token->location,
11169                                       /*user_defined=*/true);
11170     }
11171
11172   /* If we didn't get a type-name, issue an error message.  */
11173   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11174     {
11175       cp_parser_error (parser, "expected type-name");
11176       return error_mark_node;
11177     }
11178
11179   /* There is no valid C++ program where a non-template type is
11180      followed by a "<".  That usually indicates that the user thought
11181      that the type was a template.  */
11182   if (type && type != error_mark_node)
11183     {
11184       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11185          If it is, then the '<'...'>' enclose protocol names rather than
11186          template arguments, and so everything is fine.  */
11187       if (c_dialect_objc ()
11188           && (objc_is_id (type) || objc_is_class_name (type)))
11189         {
11190           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11191           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11192
11193           /* Clobber the "unqualified" type previously entered into
11194              DECL_SPECS with the new, improved protocol-qualified version.  */
11195           if (decl_specs)
11196             decl_specs->type = qual_type;
11197
11198           return qual_type;
11199         }
11200
11201       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11202                                                token->location);
11203     }
11204
11205   return type;
11206 }
11207
11208 /* Parse a type-name.
11209
11210    type-name:
11211      class-name
11212      enum-name
11213      typedef-name
11214
11215    enum-name:
11216      identifier
11217
11218    typedef-name:
11219      identifier
11220
11221    Returns a TYPE_DECL for the type.  */
11222
11223 static tree
11224 cp_parser_type_name (cp_parser* parser)
11225 {
11226   tree type_decl;
11227
11228   /* We can't know yet whether it is a class-name or not.  */
11229   cp_parser_parse_tentatively (parser);
11230   /* Try a class-name.  */
11231   type_decl = cp_parser_class_name (parser,
11232                                     /*typename_keyword_p=*/false,
11233                                     /*template_keyword_p=*/false,
11234                                     none_type,
11235                                     /*check_dependency_p=*/true,
11236                                     /*class_head_p=*/false,
11237                                     /*is_declaration=*/false);
11238   /* If it's not a class-name, keep looking.  */
11239   if (!cp_parser_parse_definitely (parser))
11240     {
11241       /* It must be a typedef-name or an enum-name.  */
11242       return cp_parser_nonclass_name (parser);
11243     }
11244
11245   return type_decl;
11246 }
11247
11248 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11249
11250    enum-name:
11251      identifier
11252
11253    typedef-name:
11254      identifier
11255
11256    Returns a TYPE_DECL for the type.  */
11257
11258 static tree
11259 cp_parser_nonclass_name (cp_parser* parser)
11260 {
11261   tree type_decl;
11262   tree identifier;
11263
11264   cp_token *token = cp_lexer_peek_token (parser->lexer);
11265   identifier = cp_parser_identifier (parser);
11266   if (identifier == error_mark_node)
11267     return error_mark_node;
11268
11269   /* Look up the type-name.  */
11270   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11271
11272   if (TREE_CODE (type_decl) != TYPE_DECL
11273       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11274     {
11275       /* See if this is an Objective-C type.  */
11276       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11277       tree type = objc_get_protocol_qualified_type (identifier, protos);
11278       if (type)
11279         type_decl = TYPE_NAME (type);
11280     }
11281   
11282   /* Issue an error if we did not find a type-name.  */
11283   if (TREE_CODE (type_decl) != TYPE_DECL)
11284     {
11285       if (!cp_parser_simulate_error (parser))
11286         cp_parser_name_lookup_error (parser, identifier, type_decl,
11287                                      "is not a type", token->location);
11288       return error_mark_node;
11289     }
11290   /* Remember that the name was used in the definition of the
11291      current class so that we can check later to see if the
11292      meaning would have been different after the class was
11293      entirely defined.  */
11294   else if (type_decl != error_mark_node
11295            && !parser->scope)
11296     maybe_note_name_used_in_class (identifier, type_decl);
11297   
11298   return type_decl;
11299 }
11300
11301 /* Parse an elaborated-type-specifier.  Note that the grammar given
11302    here incorporates the resolution to DR68.
11303
11304    elaborated-type-specifier:
11305      class-key :: [opt] nested-name-specifier [opt] identifier
11306      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11307      enum :: [opt] nested-name-specifier [opt] identifier
11308      typename :: [opt] nested-name-specifier identifier
11309      typename :: [opt] nested-name-specifier template [opt]
11310        template-id
11311
11312    GNU extension:
11313
11314    elaborated-type-specifier:
11315      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11316      class-key attributes :: [opt] nested-name-specifier [opt]
11317                template [opt] template-id
11318      enum attributes :: [opt] nested-name-specifier [opt] identifier
11319
11320    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11321    declared `friend'.  If IS_DECLARATION is TRUE, then this
11322    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11323    something is being declared.
11324
11325    Returns the TYPE specified.  */
11326
11327 static tree
11328 cp_parser_elaborated_type_specifier (cp_parser* parser,
11329                                      bool is_friend,
11330                                      bool is_declaration)
11331 {
11332   enum tag_types tag_type;
11333   tree identifier;
11334   tree type = NULL_TREE;
11335   tree attributes = NULL_TREE;
11336   cp_token *token = NULL;
11337
11338   /* See if we're looking at the `enum' keyword.  */
11339   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11340     {
11341       /* Consume the `enum' token.  */
11342       cp_lexer_consume_token (parser->lexer);
11343       /* Remember that it's an enumeration type.  */
11344       tag_type = enum_type;
11345       /* Parse the attributes.  */
11346       attributes = cp_parser_attributes_opt (parser);
11347     }
11348   /* Or, it might be `typename'.  */
11349   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11350                                            RID_TYPENAME))
11351     {
11352       /* Consume the `typename' token.  */
11353       cp_lexer_consume_token (parser->lexer);
11354       /* Remember that it's a `typename' type.  */
11355       tag_type = typename_type;
11356       /* The `typename' keyword is only allowed in templates.  */
11357       if (!processing_template_decl)
11358         permerror ("using %<typename%> outside of template");
11359     }
11360   /* Otherwise it must be a class-key.  */
11361   else
11362     {
11363       tag_type = cp_parser_class_key (parser);
11364       if (tag_type == none_type)
11365         return error_mark_node;
11366       /* Parse the attributes.  */
11367       attributes = cp_parser_attributes_opt (parser);
11368     }
11369
11370   /* Look for the `::' operator.  */
11371   cp_parser_global_scope_opt (parser,
11372                               /*current_scope_valid_p=*/false);
11373   /* Look for the nested-name-specifier.  */
11374   if (tag_type == typename_type)
11375     {
11376       if (!cp_parser_nested_name_specifier (parser,
11377                                            /*typename_keyword_p=*/true,
11378                                            /*check_dependency_p=*/true,
11379                                            /*type_p=*/true,
11380                                             is_declaration))
11381         return error_mark_node;
11382     }
11383   else
11384     /* Even though `typename' is not present, the proposed resolution
11385        to Core Issue 180 says that in `class A<T>::B', `B' should be
11386        considered a type-name, even if `A<T>' is dependent.  */
11387     cp_parser_nested_name_specifier_opt (parser,
11388                                          /*typename_keyword_p=*/true,
11389                                          /*check_dependency_p=*/true,
11390                                          /*type_p=*/true,
11391                                          is_declaration);
11392  /* For everything but enumeration types, consider a template-id.
11393     For an enumeration type, consider only a plain identifier.  */
11394   if (tag_type != enum_type)
11395     {
11396       bool template_p = false;
11397       tree decl;
11398
11399       /* Allow the `template' keyword.  */
11400       template_p = cp_parser_optional_template_keyword (parser);
11401       /* If we didn't see `template', we don't know if there's a
11402          template-id or not.  */
11403       if (!template_p)
11404         cp_parser_parse_tentatively (parser);
11405       /* Parse the template-id.  */
11406       token = cp_lexer_peek_token (parser->lexer);
11407       decl = cp_parser_template_id (parser, template_p,
11408                                     /*check_dependency_p=*/true,
11409                                     is_declaration);
11410       /* If we didn't find a template-id, look for an ordinary
11411          identifier.  */
11412       if (!template_p && !cp_parser_parse_definitely (parser))
11413         ;
11414       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11415          in effect, then we must assume that, upon instantiation, the
11416          template will correspond to a class.  */
11417       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11418                && tag_type == typename_type)
11419         type = make_typename_type (parser->scope, decl,
11420                                    typename_type,
11421                                    /*complain=*/tf_error);
11422       else
11423         type = TREE_TYPE (decl);
11424     }
11425
11426   if (!type)
11427     {
11428       token = cp_lexer_peek_token (parser->lexer);
11429       identifier = cp_parser_identifier (parser);
11430
11431       if (identifier == error_mark_node)
11432         {
11433           parser->scope = NULL_TREE;
11434           return error_mark_node;
11435         }
11436
11437       /* For a `typename', we needn't call xref_tag.  */
11438       if (tag_type == typename_type
11439           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11440         return cp_parser_make_typename_type (parser, parser->scope,
11441                                              identifier,
11442                                              token->location);
11443       /* Look up a qualified name in the usual way.  */
11444       if (parser->scope)
11445         {
11446           tree decl;
11447           tree ambiguous_decls;
11448
11449           decl = cp_parser_lookup_name (parser, identifier,
11450                                         tag_type,
11451                                         /*is_template=*/false,
11452                                         /*is_namespace=*/false,
11453                                         /*check_dependency=*/true,
11454                                         &ambiguous_decls,
11455                                         token->location);
11456
11457           /* If the lookup was ambiguous, an error will already have been
11458              issued.  */
11459           if (ambiguous_decls)
11460             return error_mark_node;
11461
11462           /* If we are parsing friend declaration, DECL may be a
11463              TEMPLATE_DECL tree node here.  However, we need to check
11464              whether this TEMPLATE_DECL results in valid code.  Consider
11465              the following example:
11466
11467                namespace N {
11468                  template <class T> class C {};
11469                }
11470                class X {
11471                  template <class T> friend class N::C; // #1, valid code
11472                };
11473                template <class T> class Y {
11474                  friend class N::C;                    // #2, invalid code
11475                };
11476
11477              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11478              name lookup of `N::C'.  We see that friend declaration must
11479              be template for the code to be valid.  Note that
11480              processing_template_decl does not work here since it is
11481              always 1 for the above two cases.  */
11482
11483           decl = (cp_parser_maybe_treat_template_as_class
11484                   (decl, /*tag_name_p=*/is_friend
11485                          && parser->num_template_parameter_lists));
11486
11487           if (TREE_CODE (decl) != TYPE_DECL)
11488             {
11489               cp_parser_diagnose_invalid_type_name (parser,
11490                                                     parser->scope,
11491                                                     identifier,
11492                                                     token->location);
11493               return error_mark_node;
11494             }
11495
11496           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11497             {
11498               bool allow_template = (parser->num_template_parameter_lists
11499                                       || DECL_SELF_REFERENCE_P (decl));
11500               type = check_elaborated_type_specifier (tag_type, decl, 
11501                                                       allow_template);
11502
11503               if (type == error_mark_node)
11504                 return error_mark_node;
11505             }
11506
11507           /* Forward declarations of nested types, such as
11508
11509                class C1::C2;
11510                class C1::C2::C3;
11511
11512              are invalid unless all components preceding the final '::'
11513              are complete.  If all enclosing types are complete, these
11514              declarations become merely pointless.
11515
11516              Invalid forward declarations of nested types are errors
11517              caught elsewhere in parsing.  Those that are pointless arrive
11518              here.  */
11519
11520           if (cp_parser_declares_only_class_p (parser)
11521               && !is_friend && !processing_explicit_instantiation)
11522             warning (0, "declaration %qD does not declare anything", decl);
11523
11524           type = TREE_TYPE (decl);
11525         }
11526       else
11527         {
11528           /* An elaborated-type-specifier sometimes introduces a new type and
11529              sometimes names an existing type.  Normally, the rule is that it
11530              introduces a new type only if there is not an existing type of
11531              the same name already in scope.  For example, given:
11532
11533                struct S {};
11534                void f() { struct S s; }
11535
11536              the `struct S' in the body of `f' is the same `struct S' as in
11537              the global scope; the existing definition is used.  However, if
11538              there were no global declaration, this would introduce a new
11539              local class named `S'.
11540
11541              An exception to this rule applies to the following code:
11542
11543                namespace N { struct S; }
11544
11545              Here, the elaborated-type-specifier names a new type
11546              unconditionally; even if there is already an `S' in the
11547              containing scope this declaration names a new type.
11548              This exception only applies if the elaborated-type-specifier
11549              forms the complete declaration:
11550
11551                [class.name]
11552
11553                A declaration consisting solely of `class-key identifier ;' is
11554                either a redeclaration of the name in the current scope or a
11555                forward declaration of the identifier as a class name.  It
11556                introduces the name into the current scope.
11557
11558              We are in this situation precisely when the next token is a `;'.
11559
11560              An exception to the exception is that a `friend' declaration does
11561              *not* name a new type; i.e., given:
11562
11563                struct S { friend struct T; };
11564
11565              `T' is not a new type in the scope of `S'.
11566
11567              Also, `new struct S' or `sizeof (struct S)' never results in the
11568              definition of a new type; a new type can only be declared in a
11569              declaration context.  */
11570
11571           tag_scope ts;
11572           bool template_p;
11573
11574           if (is_friend)
11575             /* Friends have special name lookup rules.  */
11576             ts = ts_within_enclosing_non_class;
11577           else if (is_declaration
11578                    && cp_lexer_next_token_is (parser->lexer,
11579                                               CPP_SEMICOLON))
11580             /* This is a `class-key identifier ;' */
11581             ts = ts_current;
11582           else
11583             ts = ts_global;
11584
11585           template_p =
11586             (parser->num_template_parameter_lists
11587              && (cp_parser_next_token_starts_class_definition_p (parser)
11588                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11589           /* An unqualified name was used to reference this type, so
11590              there were no qualifying templates.  */
11591           if (!cp_parser_check_template_parameters (parser,
11592                                                     /*num_templates=*/0,
11593                                                     token->location))
11594             return error_mark_node;
11595           type = xref_tag (tag_type, identifier, ts, template_p);
11596         }
11597     }
11598
11599   if (type == error_mark_node)
11600     return error_mark_node;
11601
11602   /* Allow attributes on forward declarations of classes.  */
11603   if (attributes)
11604     {
11605       if (TREE_CODE (type) == TYPENAME_TYPE)
11606         warning (OPT_Wattributes,
11607                  "attributes ignored on uninstantiated type");
11608       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11609                && ! processing_explicit_instantiation)
11610         warning (OPT_Wattributes,
11611                  "attributes ignored on template instantiation");
11612       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11613         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11614       else
11615         warning (OPT_Wattributes,
11616                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11617     }
11618
11619   if (tag_type != enum_type)
11620     cp_parser_check_class_key (tag_type, type);
11621
11622   /* A "<" cannot follow an elaborated type specifier.  If that
11623      happens, the user was probably trying to form a template-id.  */
11624   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11625
11626   return type;
11627 }
11628
11629 /* Parse an enum-specifier.
11630
11631    enum-specifier:
11632      enum identifier [opt] { enumerator-list [opt] }
11633
11634    GNU Extensions:
11635      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11636        attributes[opt]
11637
11638    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11639    if the token stream isn't an enum-specifier after all.  */
11640
11641 static tree
11642 cp_parser_enum_specifier (cp_parser* parser)
11643 {
11644   tree identifier;
11645   tree type;
11646   tree attributes;
11647
11648   /* Parse tentatively so that we can back up if we don't find a
11649      enum-specifier.  */
11650   cp_parser_parse_tentatively (parser);
11651
11652   /* Caller guarantees that the current token is 'enum', an identifier
11653      possibly follows, and the token after that is an opening brace.
11654      If we don't have an identifier, fabricate an anonymous name for
11655      the enumeration being defined.  */
11656   cp_lexer_consume_token (parser->lexer);
11657
11658   attributes = cp_parser_attributes_opt (parser);
11659
11660   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11661     identifier = cp_parser_identifier (parser);
11662   else
11663     identifier = make_anon_name ();
11664
11665   /* Look for the `{' but don't consume it yet.  */
11666   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11667     cp_parser_simulate_error (parser);
11668
11669   if (!cp_parser_parse_definitely (parser))
11670     return NULL_TREE;
11671
11672   /* Issue an error message if type-definitions are forbidden here.  */
11673   if (!cp_parser_check_type_definition (parser))
11674     type = error_mark_node;
11675   else
11676     /* Create the new type.  We do this before consuming the opening
11677        brace so the enum will be recorded as being on the line of its
11678        tag (or the 'enum' keyword, if there is no tag).  */
11679     type = start_enum (identifier);
11680   
11681   /* Consume the opening brace.  */
11682   cp_lexer_consume_token (parser->lexer);
11683
11684   if (type == error_mark_node)
11685     {
11686       cp_parser_skip_to_end_of_block_or_statement (parser);
11687       return error_mark_node;
11688     }
11689
11690   /* If the next token is not '}', then there are some enumerators.  */
11691   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11692     cp_parser_enumerator_list (parser, type);
11693
11694   /* Consume the final '}'.  */
11695   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11696
11697   /* Look for trailing attributes to apply to this enumeration, and
11698      apply them if appropriate.  */
11699   if (cp_parser_allow_gnu_extensions_p (parser))
11700     {
11701       tree trailing_attr = cp_parser_attributes_opt (parser);
11702       cplus_decl_attributes (&type,
11703                              trailing_attr,
11704                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11705     }
11706
11707   /* Finish up the enumeration.  */
11708   finish_enum (type);
11709
11710   return type;
11711 }
11712
11713 /* Parse an enumerator-list.  The enumerators all have the indicated
11714    TYPE.
11715
11716    enumerator-list:
11717      enumerator-definition
11718      enumerator-list , enumerator-definition  */
11719
11720 static void
11721 cp_parser_enumerator_list (cp_parser* parser, tree type)
11722 {
11723   while (true)
11724     {
11725       /* Parse an enumerator-definition.  */
11726       cp_parser_enumerator_definition (parser, type);
11727
11728       /* If the next token is not a ',', we've reached the end of
11729          the list.  */
11730       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11731         break;
11732       /* Otherwise, consume the `,' and keep going.  */
11733       cp_lexer_consume_token (parser->lexer);
11734       /* If the next token is a `}', there is a trailing comma.  */
11735       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11736         {
11737           if (pedantic && !in_system_header)
11738             pedwarn ("comma at end of enumerator list");
11739           break;
11740         }
11741     }
11742 }
11743
11744 /* Parse an enumerator-definition.  The enumerator has the indicated
11745    TYPE.
11746
11747    enumerator-definition:
11748      enumerator
11749      enumerator = constant-expression
11750
11751    enumerator:
11752      identifier  */
11753
11754 static void
11755 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11756 {
11757   tree identifier;
11758   tree value;
11759
11760   /* Look for the identifier.  */
11761   identifier = cp_parser_identifier (parser);
11762   if (identifier == error_mark_node)
11763     return;
11764
11765   /* If the next token is an '=', then there is an explicit value.  */
11766   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11767     {
11768       /* Consume the `=' token.  */
11769       cp_lexer_consume_token (parser->lexer);
11770       /* Parse the value.  */
11771       value = cp_parser_constant_expression (parser,
11772                                              /*allow_non_constant_p=*/false,
11773                                              NULL);
11774     }
11775   else
11776     value = NULL_TREE;
11777
11778   /* Create the enumerator.  */
11779   build_enumerator (identifier, value, type);
11780 }
11781
11782 /* Parse a namespace-name.
11783
11784    namespace-name:
11785      original-namespace-name
11786      namespace-alias
11787
11788    Returns the NAMESPACE_DECL for the namespace.  */
11789
11790 static tree
11791 cp_parser_namespace_name (cp_parser* parser)
11792 {
11793   tree identifier;
11794   tree namespace_decl;
11795
11796   cp_token *token = cp_lexer_peek_token (parser->lexer);
11797
11798   /* Get the name of the namespace.  */
11799   identifier = cp_parser_identifier (parser);
11800   if (identifier == error_mark_node)
11801     return error_mark_node;
11802
11803   /* Look up the identifier in the currently active scope.  Look only
11804      for namespaces, due to:
11805
11806        [basic.lookup.udir]
11807
11808        When looking up a namespace-name in a using-directive or alias
11809        definition, only namespace names are considered.
11810
11811      And:
11812
11813        [basic.lookup.qual]
11814
11815        During the lookup of a name preceding the :: scope resolution
11816        operator, object, function, and enumerator names are ignored.
11817
11818      (Note that cp_parser_class_or_namespace_name only calls this
11819      function if the token after the name is the scope resolution
11820      operator.)  */
11821   namespace_decl = cp_parser_lookup_name (parser, identifier,
11822                                           none_type,
11823                                           /*is_template=*/false,
11824                                           /*is_namespace=*/true,
11825                                           /*check_dependency=*/true,
11826                                           /*ambiguous_decls=*/NULL,
11827                                           token->location);
11828   /* If it's not a namespace, issue an error.  */
11829   if (namespace_decl == error_mark_node
11830       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11831     {
11832       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11833         error ("%H%qD is not a namespace-name", &token->location, identifier);
11834       cp_parser_error (parser, "expected namespace-name");
11835       namespace_decl = error_mark_node;
11836     }
11837
11838   return namespace_decl;
11839 }
11840
11841 /* Parse a namespace-definition.
11842
11843    namespace-definition:
11844      named-namespace-definition
11845      unnamed-namespace-definition
11846
11847    named-namespace-definition:
11848      original-namespace-definition
11849      extension-namespace-definition
11850
11851    original-namespace-definition:
11852      namespace identifier { namespace-body }
11853
11854    extension-namespace-definition:
11855      namespace original-namespace-name { namespace-body }
11856
11857    unnamed-namespace-definition:
11858      namespace { namespace-body } */
11859
11860 static void
11861 cp_parser_namespace_definition (cp_parser* parser)
11862 {
11863   tree identifier, attribs;
11864   bool has_visibility;
11865   bool is_inline;
11866
11867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11868     {
11869       is_inline = true;
11870       cp_lexer_consume_token (parser->lexer);
11871     }
11872   else
11873     is_inline = false;
11874
11875   /* Look for the `namespace' keyword.  */
11876   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11877
11878   /* Get the name of the namespace.  We do not attempt to distinguish
11879      between an original-namespace-definition and an
11880      extension-namespace-definition at this point.  The semantic
11881      analysis routines are responsible for that.  */
11882   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11883     identifier = cp_parser_identifier (parser);
11884   else
11885     identifier = NULL_TREE;
11886
11887   /* Parse any specified attributes.  */
11888   attribs = cp_parser_attributes_opt (parser);
11889
11890   /* Look for the `{' to start the namespace.  */
11891   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11892   /* Start the namespace.  */
11893   push_namespace (identifier);
11894
11895   /* "inline namespace" is equivalent to a stub namespace definition
11896      followed by a strong using directive.  */
11897   if (is_inline)
11898     {
11899       tree name_space = current_namespace;
11900       /* Set up namespace association.  */
11901       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11902         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11903                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11904       /* Import the contents of the inline namespace.  */
11905       pop_namespace ();
11906       do_using_directive (name_space);
11907       push_namespace (identifier);
11908     }
11909
11910   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11911
11912   /* Parse the body of the namespace.  */
11913   cp_parser_namespace_body (parser);
11914
11915 #ifdef HANDLE_PRAGMA_VISIBILITY
11916   if (has_visibility)
11917     pop_visibility ();
11918 #endif
11919
11920   /* Finish the namespace.  */
11921   pop_namespace ();
11922   /* Look for the final `}'.  */
11923   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11924 }
11925
11926 /* Parse a namespace-body.
11927
11928    namespace-body:
11929      declaration-seq [opt]  */
11930
11931 static void
11932 cp_parser_namespace_body (cp_parser* parser)
11933 {
11934   cp_parser_declaration_seq_opt (parser);
11935 }
11936
11937 /* Parse a namespace-alias-definition.
11938
11939    namespace-alias-definition:
11940      namespace identifier = qualified-namespace-specifier ;  */
11941
11942 static void
11943 cp_parser_namespace_alias_definition (cp_parser* parser)
11944 {
11945   tree identifier;
11946   tree namespace_specifier;
11947
11948   cp_token *token = cp_lexer_peek_token (parser->lexer);
11949
11950   /* Look for the `namespace' keyword.  */
11951   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11952   /* Look for the identifier.  */
11953   identifier = cp_parser_identifier (parser);
11954   if (identifier == error_mark_node)
11955     return;
11956   /* Look for the `=' token.  */
11957   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11958       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11959     {
11960       error ("%H%<namespace%> definition is not allowed here", &token->location);
11961       /* Skip the definition.  */
11962       cp_lexer_consume_token (parser->lexer);
11963       if (cp_parser_skip_to_closing_brace (parser))
11964         cp_lexer_consume_token (parser->lexer);
11965       return;
11966     }
11967   cp_parser_require (parser, CPP_EQ, "%<=%>");
11968   /* Look for the qualified-namespace-specifier.  */
11969   namespace_specifier
11970     = cp_parser_qualified_namespace_specifier (parser);
11971   /* Look for the `;' token.  */
11972   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11973
11974   /* Register the alias in the symbol table.  */
11975   do_namespace_alias (identifier, namespace_specifier);
11976 }
11977
11978 /* Parse a qualified-namespace-specifier.
11979
11980    qualified-namespace-specifier:
11981      :: [opt] nested-name-specifier [opt] namespace-name
11982
11983    Returns a NAMESPACE_DECL corresponding to the specified
11984    namespace.  */
11985
11986 static tree
11987 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11988 {
11989   /* Look for the optional `::'.  */
11990   cp_parser_global_scope_opt (parser,
11991                               /*current_scope_valid_p=*/false);
11992
11993   /* Look for the optional nested-name-specifier.  */
11994   cp_parser_nested_name_specifier_opt (parser,
11995                                        /*typename_keyword_p=*/false,
11996                                        /*check_dependency_p=*/true,
11997                                        /*type_p=*/false,
11998                                        /*is_declaration=*/true);
11999
12000   return cp_parser_namespace_name (parser);
12001 }
12002
12003 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12004    access declaration.
12005
12006    using-declaration:
12007      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12008      using :: unqualified-id ;  
12009
12010    access-declaration:
12011      qualified-id ;  
12012
12013    */
12014
12015 static bool
12016 cp_parser_using_declaration (cp_parser* parser, 
12017                              bool access_declaration_p)
12018 {
12019   cp_token *token;
12020   bool typename_p = false;
12021   bool global_scope_p;
12022   tree decl;
12023   tree identifier;
12024   tree qscope;
12025
12026   if (access_declaration_p)
12027     cp_parser_parse_tentatively (parser);
12028   else
12029     {
12030       /* Look for the `using' keyword.  */
12031       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12032       
12033       /* Peek at the next token.  */
12034       token = cp_lexer_peek_token (parser->lexer);
12035       /* See if it's `typename'.  */
12036       if (token->keyword == RID_TYPENAME)
12037         {
12038           /* Remember that we've seen it.  */
12039           typename_p = true;
12040           /* Consume the `typename' token.  */
12041           cp_lexer_consume_token (parser->lexer);
12042         }
12043     }
12044
12045   /* Look for the optional global scope qualification.  */
12046   global_scope_p
12047     = (cp_parser_global_scope_opt (parser,
12048                                    /*current_scope_valid_p=*/false)
12049        != NULL_TREE);
12050
12051   /* If we saw `typename', or didn't see `::', then there must be a
12052      nested-name-specifier present.  */
12053   if (typename_p || !global_scope_p)
12054     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12055                                               /*check_dependency_p=*/true,
12056                                               /*type_p=*/false,
12057                                               /*is_declaration=*/true);
12058   /* Otherwise, we could be in either of the two productions.  In that
12059      case, treat the nested-name-specifier as optional.  */
12060   else
12061     qscope = cp_parser_nested_name_specifier_opt (parser,
12062                                                   /*typename_keyword_p=*/false,
12063                                                   /*check_dependency_p=*/true,
12064                                                   /*type_p=*/false,
12065                                                   /*is_declaration=*/true);
12066   if (!qscope)
12067     qscope = global_namespace;
12068
12069   if (access_declaration_p && cp_parser_error_occurred (parser))
12070     /* Something has already gone wrong; there's no need to parse
12071        further.  Since an error has occurred, the return value of
12072        cp_parser_parse_definitely will be false, as required.  */
12073     return cp_parser_parse_definitely (parser);
12074
12075   token = cp_lexer_peek_token (parser->lexer);
12076   /* Parse the unqualified-id.  */
12077   identifier = cp_parser_unqualified_id (parser,
12078                                          /*template_keyword_p=*/false,
12079                                          /*check_dependency_p=*/true,
12080                                          /*declarator_p=*/true,
12081                                          /*optional_p=*/false);
12082
12083   if (access_declaration_p)
12084     {
12085       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12086         cp_parser_simulate_error (parser);
12087       if (!cp_parser_parse_definitely (parser))
12088         return false;
12089     }
12090
12091   /* The function we call to handle a using-declaration is different
12092      depending on what scope we are in.  */
12093   if (qscope == error_mark_node || identifier == error_mark_node)
12094     ;
12095   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12096            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12097     /* [namespace.udecl]
12098
12099        A using declaration shall not name a template-id.  */
12100     error ("%Ha template-id may not appear in a using-declaration",
12101             &token->location);
12102   else
12103     {
12104       if (at_class_scope_p ())
12105         {
12106           /* Create the USING_DECL.  */
12107           decl = do_class_using_decl (parser->scope, identifier);
12108
12109           if (check_for_bare_parameter_packs (decl))
12110             return false;
12111           else
12112             /* Add it to the list of members in this class.  */
12113             finish_member_declaration (decl);
12114         }
12115       else
12116         {
12117           decl = cp_parser_lookup_name_simple (parser,
12118                                                identifier,
12119                                                token->location);
12120           if (decl == error_mark_node)
12121             cp_parser_name_lookup_error (parser, identifier,
12122                                          decl, NULL,
12123                                          token->location);
12124           else if (check_for_bare_parameter_packs (decl))
12125             return false;
12126           else if (!at_namespace_scope_p ())
12127             do_local_using_decl (decl, qscope, identifier);
12128           else
12129             do_toplevel_using_decl (decl, qscope, identifier);
12130         }
12131     }
12132
12133   /* Look for the final `;'.  */
12134   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12135   
12136   return true;
12137 }
12138
12139 /* Parse a using-directive.
12140
12141    using-directive:
12142      using namespace :: [opt] nested-name-specifier [opt]
12143        namespace-name ;  */
12144
12145 static void
12146 cp_parser_using_directive (cp_parser* parser)
12147 {
12148   tree namespace_decl;
12149   tree attribs;
12150
12151   /* Look for the `using' keyword.  */
12152   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12153   /* And the `namespace' keyword.  */
12154   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12155   /* Look for the optional `::' operator.  */
12156   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12157   /* And the optional nested-name-specifier.  */
12158   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   /* Get the namespace being used.  */
12164   namespace_decl = cp_parser_namespace_name (parser);
12165   /* And any specified attributes.  */
12166   attribs = cp_parser_attributes_opt (parser);
12167   /* Update the symbol table.  */
12168   parse_using_directive (namespace_decl, attribs);
12169   /* Look for the final `;'.  */
12170   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12171 }
12172
12173 /* Parse an asm-definition.
12174
12175    asm-definition:
12176      asm ( string-literal ) ;
12177
12178    GNU Extension:
12179
12180    asm-definition:
12181      asm volatile [opt] ( string-literal ) ;
12182      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12183      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12184                           : asm-operand-list [opt] ) ;
12185      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12186                           : asm-operand-list [opt]
12187                           : asm-operand-list [opt] ) ;  */
12188
12189 static void
12190 cp_parser_asm_definition (cp_parser* parser)
12191 {
12192   tree string;
12193   tree outputs = NULL_TREE;
12194   tree inputs = NULL_TREE;
12195   tree clobbers = NULL_TREE;
12196   tree asm_stmt;
12197   bool volatile_p = false;
12198   bool extended_p = false;
12199   bool invalid_inputs_p = false;
12200   bool invalid_outputs_p = false;
12201
12202   /* Look for the `asm' keyword.  */
12203   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12204   /* See if the next token is `volatile'.  */
12205   if (cp_parser_allow_gnu_extensions_p (parser)
12206       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12207     {
12208       /* Remember that we saw the `volatile' keyword.  */
12209       volatile_p = true;
12210       /* Consume the token.  */
12211       cp_lexer_consume_token (parser->lexer);
12212     }
12213   /* Look for the opening `('.  */
12214   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12215     return;
12216   /* Look for the string.  */
12217   string = cp_parser_string_literal (parser, false, false);
12218   if (string == error_mark_node)
12219     {
12220       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12221                                              /*consume_paren=*/true);
12222       return;
12223     }
12224
12225   /* If we're allowing GNU extensions, check for the extended assembly
12226      syntax.  Unfortunately, the `:' tokens need not be separated by
12227      a space in C, and so, for compatibility, we tolerate that here
12228      too.  Doing that means that we have to treat the `::' operator as
12229      two `:' tokens.  */
12230   if (cp_parser_allow_gnu_extensions_p (parser)
12231       && parser->in_function_body
12232       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12233           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12234     {
12235       bool inputs_p = false;
12236       bool clobbers_p = false;
12237
12238       /* The extended syntax was used.  */
12239       extended_p = true;
12240
12241       /* Look for outputs.  */
12242       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12243         {
12244           /* Consume the `:'.  */
12245           cp_lexer_consume_token (parser->lexer);
12246           /* Parse the output-operands.  */
12247           if (cp_lexer_next_token_is_not (parser->lexer,
12248                                           CPP_COLON)
12249               && cp_lexer_next_token_is_not (parser->lexer,
12250                                              CPP_SCOPE)
12251               && cp_lexer_next_token_is_not (parser->lexer,
12252                                              CPP_CLOSE_PAREN))
12253             outputs = cp_parser_asm_operand_list (parser);
12254
12255             if (outputs == error_mark_node)
12256               invalid_outputs_p = true;
12257         }
12258       /* If the next token is `::', there are no outputs, and the
12259          next token is the beginning of the inputs.  */
12260       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12261         /* The inputs are coming next.  */
12262         inputs_p = true;
12263
12264       /* Look for inputs.  */
12265       if (inputs_p
12266           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12267         {
12268           /* Consume the `:' or `::'.  */
12269           cp_lexer_consume_token (parser->lexer);
12270           /* Parse the output-operands.  */
12271           if (cp_lexer_next_token_is_not (parser->lexer,
12272                                           CPP_COLON)
12273               && cp_lexer_next_token_is_not (parser->lexer,
12274                                              CPP_CLOSE_PAREN))
12275             inputs = cp_parser_asm_operand_list (parser);
12276
12277             if (inputs == error_mark_node)
12278               invalid_inputs_p = true;
12279         }
12280       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12281         /* The clobbers are coming next.  */
12282         clobbers_p = true;
12283
12284       /* Look for clobbers.  */
12285       if (clobbers_p
12286           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12287         {
12288           /* Consume the `:' or `::'.  */
12289           cp_lexer_consume_token (parser->lexer);
12290           /* Parse the clobbers.  */
12291           if (cp_lexer_next_token_is_not (parser->lexer,
12292                                           CPP_CLOSE_PAREN))
12293             clobbers = cp_parser_asm_clobber_list (parser);
12294         }
12295     }
12296   /* Look for the closing `)'.  */
12297   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12298     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12299                                            /*consume_paren=*/true);
12300   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12301
12302   if (!invalid_inputs_p && !invalid_outputs_p)
12303     {
12304       /* Create the ASM_EXPR.  */
12305       if (parser->in_function_body)
12306         {
12307           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12308                                       inputs, clobbers);
12309           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12310           if (!extended_p)
12311             {
12312               tree temp = asm_stmt;
12313               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12314                 temp = TREE_OPERAND (temp, 0);
12315
12316               ASM_INPUT_P (temp) = 1;
12317             }
12318         }
12319       else
12320         cgraph_add_asm_node (string);
12321     }
12322 }
12323
12324 /* Declarators [gram.dcl.decl] */
12325
12326 /* Parse an init-declarator.
12327
12328    init-declarator:
12329      declarator initializer [opt]
12330
12331    GNU Extension:
12332
12333    init-declarator:
12334      declarator asm-specification [opt] attributes [opt] initializer [opt]
12335
12336    function-definition:
12337      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12338        function-body
12339      decl-specifier-seq [opt] declarator function-try-block
12340
12341    GNU Extension:
12342
12343    function-definition:
12344      __extension__ function-definition
12345
12346    The DECL_SPECIFIERS apply to this declarator.  Returns a
12347    representation of the entity declared.  If MEMBER_P is TRUE, then
12348    this declarator appears in a class scope.  The new DECL created by
12349    this declarator is returned.
12350
12351    The CHECKS are access checks that should be performed once we know
12352    what entity is being declared (and, therefore, what classes have
12353    befriended it).
12354
12355    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12356    for a function-definition here as well.  If the declarator is a
12357    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12358    be TRUE upon return.  By that point, the function-definition will
12359    have been completely parsed.
12360
12361    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12362    is FALSE.  */
12363
12364 static tree
12365 cp_parser_init_declarator (cp_parser* parser,
12366                            cp_decl_specifier_seq *decl_specifiers,
12367                            VEC (deferred_access_check,gc)* checks,
12368                            bool function_definition_allowed_p,
12369                            bool member_p,
12370                            int declares_class_or_enum,
12371                            bool* function_definition_p)
12372 {
12373   cp_token *token = NULL, *asm_spec_start_token = NULL,
12374            *attributes_start_token = NULL;
12375   cp_declarator *declarator;
12376   tree prefix_attributes;
12377   tree attributes;
12378   tree asm_specification;
12379   tree initializer;
12380   tree decl = NULL_TREE;
12381   tree scope;
12382   bool is_initialized;
12383   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12384      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12385      "(...)".  */
12386   enum cpp_ttype initialization_kind;
12387   bool is_direct_init = false;
12388   bool is_non_constant_init;
12389   int ctor_dtor_or_conv_p;
12390   bool friend_p;
12391   tree pushed_scope = NULL;
12392
12393   /* Gather the attributes that were provided with the
12394      decl-specifiers.  */
12395   prefix_attributes = decl_specifiers->attributes;
12396
12397   /* Assume that this is not the declarator for a function
12398      definition.  */
12399   if (function_definition_p)
12400     *function_definition_p = false;
12401
12402   /* Defer access checks while parsing the declarator; we cannot know
12403      what names are accessible until we know what is being
12404      declared.  */
12405   resume_deferring_access_checks ();
12406
12407   /* Parse the declarator.  */
12408   token = cp_lexer_peek_token (parser->lexer);
12409   declarator
12410     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12411                             &ctor_dtor_or_conv_p,
12412                             /*parenthesized_p=*/NULL,
12413                             /*member_p=*/false);
12414   /* Gather up the deferred checks.  */
12415   stop_deferring_access_checks ();
12416
12417   /* If the DECLARATOR was erroneous, there's no need to go
12418      further.  */
12419   if (declarator == cp_error_declarator)
12420     return error_mark_node;
12421
12422   /* Check that the number of template-parameter-lists is OK.  */
12423   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12424                                                        token->location))
12425     return error_mark_node;
12426
12427   if (declares_class_or_enum & 2)
12428     cp_parser_check_for_definition_in_return_type (declarator,
12429                                                    decl_specifiers->type,
12430                                                    decl_specifiers->type_location);
12431
12432   /* Figure out what scope the entity declared by the DECLARATOR is
12433      located in.  `grokdeclarator' sometimes changes the scope, so
12434      we compute it now.  */
12435   scope = get_scope_of_declarator (declarator);
12436
12437   /* If we're allowing GNU extensions, look for an asm-specification
12438      and attributes.  */
12439   if (cp_parser_allow_gnu_extensions_p (parser))
12440     {
12441       /* Look for an asm-specification.  */
12442       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12443       asm_specification = cp_parser_asm_specification_opt (parser);
12444       /* And attributes.  */
12445       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12446       attributes = cp_parser_attributes_opt (parser);
12447     }
12448   else
12449     {
12450       asm_specification = NULL_TREE;
12451       attributes = NULL_TREE;
12452     }
12453
12454   /* Peek at the next token.  */
12455   token = cp_lexer_peek_token (parser->lexer);
12456   /* Check to see if the token indicates the start of a
12457      function-definition.  */
12458   if (function_declarator_p (declarator)
12459       && cp_parser_token_starts_function_definition_p (token))
12460     {
12461       if (!function_definition_allowed_p)
12462         {
12463           /* If a function-definition should not appear here, issue an
12464              error message.  */
12465           cp_parser_error (parser,
12466                            "a function-definition is not allowed here");
12467           return error_mark_node;
12468         }
12469       else
12470         {
12471           /* Neither attributes nor an asm-specification are allowed
12472              on a function-definition.  */
12473           if (asm_specification)
12474             error ("%Han asm-specification is not allowed "
12475                    "on a function-definition",
12476                    &asm_spec_start_token->location);
12477           if (attributes)
12478             error ("%Hattributes are not allowed on a function-definition",
12479                    &attributes_start_token->location);
12480           /* This is a function-definition.  */
12481           *function_definition_p = true;
12482
12483           /* Parse the function definition.  */
12484           if (member_p)
12485             decl = cp_parser_save_member_function_body (parser,
12486                                                         decl_specifiers,
12487                                                         declarator,
12488                                                         prefix_attributes);
12489           else
12490             decl
12491               = (cp_parser_function_definition_from_specifiers_and_declarator
12492                  (parser, decl_specifiers, prefix_attributes, declarator));
12493
12494           return decl;
12495         }
12496     }
12497
12498   /* [dcl.dcl]
12499
12500      Only in function declarations for constructors, destructors, and
12501      type conversions can the decl-specifier-seq be omitted.
12502
12503      We explicitly postpone this check past the point where we handle
12504      function-definitions because we tolerate function-definitions
12505      that are missing their return types in some modes.  */
12506   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12507     {
12508       cp_parser_error (parser,
12509                        "expected constructor, destructor, or type conversion");
12510       return error_mark_node;
12511     }
12512
12513   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12514   if (token->type == CPP_EQ
12515       || token->type == CPP_OPEN_PAREN
12516       || token->type == CPP_OPEN_BRACE)
12517     {
12518       is_initialized = true;
12519       initialization_kind = token->type;
12520     }
12521   else
12522     {
12523       /* If the init-declarator isn't initialized and isn't followed by a
12524          `,' or `;', it's not a valid init-declarator.  */
12525       if (token->type != CPP_COMMA
12526           && token->type != CPP_SEMICOLON)
12527         {
12528           cp_parser_error (parser, "expected initializer");
12529           return error_mark_node;
12530         }
12531       is_initialized = false;
12532       initialization_kind = CPP_EOF;
12533     }
12534
12535   /* Because start_decl has side-effects, we should only call it if we
12536      know we're going ahead.  By this point, we know that we cannot
12537      possibly be looking at any other construct.  */
12538   cp_parser_commit_to_tentative_parse (parser);
12539
12540   /* If the decl specifiers were bad, issue an error now that we're
12541      sure this was intended to be a declarator.  Then continue
12542      declaring the variable(s), as int, to try to cut down on further
12543      errors.  */
12544   if (decl_specifiers->any_specifiers_p
12545       && decl_specifiers->type == error_mark_node)
12546     {
12547       cp_parser_error (parser, "invalid type in declaration");
12548       decl_specifiers->type = integer_type_node;
12549     }
12550
12551   /* Check to see whether or not this declaration is a friend.  */
12552   friend_p = cp_parser_friend_p (decl_specifiers);
12553
12554   /* Enter the newly declared entry in the symbol table.  If we're
12555      processing a declaration in a class-specifier, we wait until
12556      after processing the initializer.  */
12557   if (!member_p)
12558     {
12559       if (parser->in_unbraced_linkage_specification_p)
12560         decl_specifiers->storage_class = sc_extern;
12561       decl = start_decl (declarator, decl_specifiers,
12562                          is_initialized, attributes, prefix_attributes,
12563                          &pushed_scope);
12564     }
12565   else if (scope)
12566     /* Enter the SCOPE.  That way unqualified names appearing in the
12567        initializer will be looked up in SCOPE.  */
12568     pushed_scope = push_scope (scope);
12569
12570   /* Perform deferred access control checks, now that we know in which
12571      SCOPE the declared entity resides.  */
12572   if (!member_p && decl)
12573     {
12574       tree saved_current_function_decl = NULL_TREE;
12575
12576       /* If the entity being declared is a function, pretend that we
12577          are in its scope.  If it is a `friend', it may have access to
12578          things that would not otherwise be accessible.  */
12579       if (TREE_CODE (decl) == FUNCTION_DECL)
12580         {
12581           saved_current_function_decl = current_function_decl;
12582           current_function_decl = decl;
12583         }
12584
12585       /* Perform access checks for template parameters.  */
12586       cp_parser_perform_template_parameter_access_checks (checks);
12587
12588       /* Perform the access control checks for the declarator and the
12589          the decl-specifiers.  */
12590       perform_deferred_access_checks ();
12591
12592       /* Restore the saved value.  */
12593       if (TREE_CODE (decl) == FUNCTION_DECL)
12594         current_function_decl = saved_current_function_decl;
12595     }
12596
12597   /* Parse the initializer.  */
12598   initializer = NULL_TREE;
12599   is_direct_init = false;
12600   is_non_constant_init = true;
12601   if (is_initialized)
12602     {
12603       if (function_declarator_p (declarator))
12604         {
12605           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12606            if (initialization_kind == CPP_EQ)
12607              initializer = cp_parser_pure_specifier (parser);
12608            else
12609              {
12610                /* If the declaration was erroneous, we don't really
12611                   know what the user intended, so just silently
12612                   consume the initializer.  */
12613                if (decl != error_mark_node)
12614                  error ("%Hinitializer provided for function",
12615                         &initializer_start_token->location);
12616                cp_parser_skip_to_closing_parenthesis (parser,
12617                                                       /*recovering=*/true,
12618                                                       /*or_comma=*/false,
12619                                                       /*consume_paren=*/true);
12620              }
12621         }
12622       else
12623         initializer = cp_parser_initializer (parser,
12624                                              &is_direct_init,
12625                                              &is_non_constant_init);
12626     }
12627
12628   /* The old parser allows attributes to appear after a parenthesized
12629      initializer.  Mark Mitchell proposed removing this functionality
12630      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12631      attributes -- but ignores them.  */
12632   if (cp_parser_allow_gnu_extensions_p (parser)
12633       && initialization_kind == CPP_OPEN_PAREN)
12634     if (cp_parser_attributes_opt (parser))
12635       warning (OPT_Wattributes,
12636                "attributes after parenthesized initializer ignored");
12637
12638   /* For an in-class declaration, use `grokfield' to create the
12639      declaration.  */
12640   if (member_p)
12641     {
12642       if (pushed_scope)
12643         {
12644           pop_scope (pushed_scope);
12645           pushed_scope = false;
12646         }
12647       decl = grokfield (declarator, decl_specifiers,
12648                         initializer, !is_non_constant_init,
12649                         /*asmspec=*/NULL_TREE,
12650                         prefix_attributes);
12651       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12652         cp_parser_save_default_args (parser, decl);
12653     }
12654
12655   /* Finish processing the declaration.  But, skip friend
12656      declarations.  */
12657   if (!friend_p && decl && decl != error_mark_node)
12658     {
12659       cp_finish_decl (decl,
12660                       initializer, !is_non_constant_init,
12661                       asm_specification,
12662                       /* If the initializer is in parentheses, then this is
12663                          a direct-initialization, which means that an
12664                          `explicit' constructor is OK.  Otherwise, an
12665                          `explicit' constructor cannot be used.  */
12666                       ((is_direct_init || !is_initialized)
12667                        ? 0 : LOOKUP_ONLYCONVERTING));
12668     }
12669   else if ((cxx_dialect != cxx98) && friend_p
12670            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12671     /* Core issue #226 (C++0x only): A default template-argument
12672        shall not be specified in a friend class template
12673        declaration. */
12674     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12675                              /*is_partial=*/0, /*is_friend_decl=*/1);
12676
12677   if (!friend_p && pushed_scope)
12678     pop_scope (pushed_scope);
12679
12680   return decl;
12681 }
12682
12683 /* Parse a declarator.
12684
12685    declarator:
12686      direct-declarator
12687      ptr-operator declarator
12688
12689    abstract-declarator:
12690      ptr-operator abstract-declarator [opt]
12691      direct-abstract-declarator
12692
12693    GNU Extensions:
12694
12695    declarator:
12696      attributes [opt] direct-declarator
12697      attributes [opt] ptr-operator declarator
12698
12699    abstract-declarator:
12700      attributes [opt] ptr-operator abstract-declarator [opt]
12701      attributes [opt] direct-abstract-declarator
12702
12703    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12704    detect constructor, destructor or conversion operators. It is set
12705    to -1 if the declarator is a name, and +1 if it is a
12706    function. Otherwise it is set to zero. Usually you just want to
12707    test for >0, but internally the negative value is used.
12708
12709    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12710    a decl-specifier-seq unless it declares a constructor, destructor,
12711    or conversion.  It might seem that we could check this condition in
12712    semantic analysis, rather than parsing, but that makes it difficult
12713    to handle something like `f()'.  We want to notice that there are
12714    no decl-specifiers, and therefore realize that this is an
12715    expression, not a declaration.)
12716
12717    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12718    the declarator is a direct-declarator of the form "(...)".
12719
12720    MEMBER_P is true iff this declarator is a member-declarator.  */
12721
12722 static cp_declarator *
12723 cp_parser_declarator (cp_parser* parser,
12724                       cp_parser_declarator_kind dcl_kind,
12725                       int* ctor_dtor_or_conv_p,
12726                       bool* parenthesized_p,
12727                       bool member_p)
12728 {
12729   cp_token *token;
12730   cp_declarator *declarator;
12731   enum tree_code code;
12732   cp_cv_quals cv_quals;
12733   tree class_type;
12734   tree attributes = NULL_TREE;
12735
12736   /* Assume this is not a constructor, destructor, or type-conversion
12737      operator.  */
12738   if (ctor_dtor_or_conv_p)
12739     *ctor_dtor_or_conv_p = 0;
12740
12741   if (cp_parser_allow_gnu_extensions_p (parser))
12742     attributes = cp_parser_attributes_opt (parser);
12743
12744   /* Peek at the next token.  */
12745   token = cp_lexer_peek_token (parser->lexer);
12746
12747   /* Check for the ptr-operator production.  */
12748   cp_parser_parse_tentatively (parser);
12749   /* Parse the ptr-operator.  */
12750   code = cp_parser_ptr_operator (parser,
12751                                  &class_type,
12752                                  &cv_quals);
12753   /* If that worked, then we have a ptr-operator.  */
12754   if (cp_parser_parse_definitely (parser))
12755     {
12756       /* If a ptr-operator was found, then this declarator was not
12757          parenthesized.  */
12758       if (parenthesized_p)
12759         *parenthesized_p = true;
12760       /* The dependent declarator is optional if we are parsing an
12761          abstract-declarator.  */
12762       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12763         cp_parser_parse_tentatively (parser);
12764
12765       /* Parse the dependent declarator.  */
12766       declarator = cp_parser_declarator (parser, dcl_kind,
12767                                          /*ctor_dtor_or_conv_p=*/NULL,
12768                                          /*parenthesized_p=*/NULL,
12769                                          /*member_p=*/false);
12770
12771       /* If we are parsing an abstract-declarator, we must handle the
12772          case where the dependent declarator is absent.  */
12773       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12774           && !cp_parser_parse_definitely (parser))
12775         declarator = NULL;
12776
12777       declarator = cp_parser_make_indirect_declarator
12778         (code, class_type, cv_quals, declarator);
12779     }
12780   /* Everything else is a direct-declarator.  */
12781   else
12782     {
12783       if (parenthesized_p)
12784         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12785                                                    CPP_OPEN_PAREN);
12786       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12787                                                 ctor_dtor_or_conv_p,
12788                                                 member_p);
12789     }
12790
12791   if (attributes && declarator && declarator != cp_error_declarator)
12792     declarator->attributes = attributes;
12793
12794   return declarator;
12795 }
12796
12797 /* Parse a direct-declarator or direct-abstract-declarator.
12798
12799    direct-declarator:
12800      declarator-id
12801      direct-declarator ( parameter-declaration-clause )
12802        cv-qualifier-seq [opt]
12803        exception-specification [opt]
12804      direct-declarator [ constant-expression [opt] ]
12805      ( declarator )
12806
12807    direct-abstract-declarator:
12808      direct-abstract-declarator [opt]
12809        ( parameter-declaration-clause )
12810        cv-qualifier-seq [opt]
12811        exception-specification [opt]
12812      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12813      ( abstract-declarator )
12814
12815    Returns a representation of the declarator.  DCL_KIND is
12816    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12817    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12818    we are parsing a direct-declarator.  It is
12819    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12820    of ambiguity we prefer an abstract declarator, as per
12821    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12822    cp_parser_declarator.  */
12823
12824 static cp_declarator *
12825 cp_parser_direct_declarator (cp_parser* parser,
12826                              cp_parser_declarator_kind dcl_kind,
12827                              int* ctor_dtor_or_conv_p,
12828                              bool member_p)
12829 {
12830   cp_token *token;
12831   cp_declarator *declarator = NULL;
12832   tree scope = NULL_TREE;
12833   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12834   bool saved_in_declarator_p = parser->in_declarator_p;
12835   bool first = true;
12836   tree pushed_scope = NULL_TREE;
12837
12838   while (true)
12839     {
12840       /* Peek at the next token.  */
12841       token = cp_lexer_peek_token (parser->lexer);
12842       if (token->type == CPP_OPEN_PAREN)
12843         {
12844           /* This is either a parameter-declaration-clause, or a
12845              parenthesized declarator. When we know we are parsing a
12846              named declarator, it must be a parenthesized declarator
12847              if FIRST is true. For instance, `(int)' is a
12848              parameter-declaration-clause, with an omitted
12849              direct-abstract-declarator. But `((*))', is a
12850              parenthesized abstract declarator. Finally, when T is a
12851              template parameter `(T)' is a
12852              parameter-declaration-clause, and not a parenthesized
12853              named declarator.
12854
12855              We first try and parse a parameter-declaration-clause,
12856              and then try a nested declarator (if FIRST is true).
12857
12858              It is not an error for it not to be a
12859              parameter-declaration-clause, even when FIRST is
12860              false. Consider,
12861
12862                int i (int);
12863                int i (3);
12864
12865              The first is the declaration of a function while the
12866              second is a the definition of a variable, including its
12867              initializer.
12868
12869              Having seen only the parenthesis, we cannot know which of
12870              these two alternatives should be selected.  Even more
12871              complex are examples like:
12872
12873                int i (int (a));
12874                int i (int (3));
12875
12876              The former is a function-declaration; the latter is a
12877              variable initialization.
12878
12879              Thus again, we try a parameter-declaration-clause, and if
12880              that fails, we back out and return.  */
12881
12882           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12883             {
12884               cp_parameter_declarator *params;
12885               unsigned saved_num_template_parameter_lists;
12886
12887               /* In a member-declarator, the only valid interpretation
12888                  of a parenthesis is the start of a
12889                  parameter-declaration-clause.  (It is invalid to
12890                  initialize a static data member with a parenthesized
12891                  initializer; only the "=" form of initialization is
12892                  permitted.)  */
12893               if (!member_p)
12894                 cp_parser_parse_tentatively (parser);
12895
12896               /* Consume the `('.  */
12897               cp_lexer_consume_token (parser->lexer);
12898               if (first)
12899                 {
12900                   /* If this is going to be an abstract declarator, we're
12901                      in a declarator and we can't have default args.  */
12902                   parser->default_arg_ok_p = false;
12903                   parser->in_declarator_p = true;
12904                 }
12905
12906               /* Inside the function parameter list, surrounding
12907                  template-parameter-lists do not apply.  */
12908               saved_num_template_parameter_lists
12909                 = parser->num_template_parameter_lists;
12910               parser->num_template_parameter_lists = 0;
12911
12912               /* Parse the parameter-declaration-clause.  */
12913               params = cp_parser_parameter_declaration_clause (parser);
12914
12915               parser->num_template_parameter_lists
12916                 = saved_num_template_parameter_lists;
12917
12918               /* If all went well, parse the cv-qualifier-seq and the
12919                  exception-specification.  */
12920               if (member_p || cp_parser_parse_definitely (parser))
12921                 {
12922                   cp_cv_quals cv_quals;
12923                   tree exception_specification;
12924
12925                   if (ctor_dtor_or_conv_p)
12926                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12927                   first = false;
12928                   /* Consume the `)'.  */
12929                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12930
12931                   /* Parse the cv-qualifier-seq.  */
12932                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12933                   /* And the exception-specification.  */
12934                   exception_specification
12935                     = cp_parser_exception_specification_opt (parser);
12936
12937                   /* Create the function-declarator.  */
12938                   declarator = make_call_declarator (declarator,
12939                                                      params,
12940                                                      cv_quals,
12941                                                      exception_specification);
12942                   /* Any subsequent parameter lists are to do with
12943                      return type, so are not those of the declared
12944                      function.  */
12945                   parser->default_arg_ok_p = false;
12946
12947                   /* Repeat the main loop.  */
12948                   continue;
12949                 }
12950             }
12951
12952           /* If this is the first, we can try a parenthesized
12953              declarator.  */
12954           if (first)
12955             {
12956               bool saved_in_type_id_in_expr_p;
12957
12958               parser->default_arg_ok_p = saved_default_arg_ok_p;
12959               parser->in_declarator_p = saved_in_declarator_p;
12960
12961               /* Consume the `('.  */
12962               cp_lexer_consume_token (parser->lexer);
12963               /* Parse the nested declarator.  */
12964               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12965               parser->in_type_id_in_expr_p = true;
12966               declarator
12967                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12968                                         /*parenthesized_p=*/NULL,
12969                                         member_p);
12970               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12971               first = false;
12972               /* Expect a `)'.  */
12973               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12974                 declarator = cp_error_declarator;
12975               if (declarator == cp_error_declarator)
12976                 break;
12977
12978               goto handle_declarator;
12979             }
12980           /* Otherwise, we must be done.  */
12981           else
12982             break;
12983         }
12984       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12985                && token->type == CPP_OPEN_SQUARE)
12986         {
12987           /* Parse an array-declarator.  */
12988           tree bounds;
12989
12990           if (ctor_dtor_or_conv_p)
12991             *ctor_dtor_or_conv_p = 0;
12992
12993           first = false;
12994           parser->default_arg_ok_p = false;
12995           parser->in_declarator_p = true;
12996           /* Consume the `['.  */
12997           cp_lexer_consume_token (parser->lexer);
12998           /* Peek at the next token.  */
12999           token = cp_lexer_peek_token (parser->lexer);
13000           /* If the next token is `]', then there is no
13001              constant-expression.  */
13002           if (token->type != CPP_CLOSE_SQUARE)
13003             {
13004               bool non_constant_p;
13005
13006               bounds
13007                 = cp_parser_constant_expression (parser,
13008                                                  /*allow_non_constant=*/true,
13009                                                  &non_constant_p);
13010               if (!non_constant_p)
13011                 bounds = fold_non_dependent_expr (bounds);
13012               /* Normally, the array bound must be an integral constant
13013                  expression.  However, as an extension, we allow VLAs
13014                  in function scopes.  */
13015               else if (!parser->in_function_body)
13016                 {
13017                   error ("%Harray bound is not an integer constant",
13018                          &token->location);
13019                   bounds = error_mark_node;
13020                 }
13021             }
13022           else
13023             bounds = NULL_TREE;
13024           /* Look for the closing `]'.  */
13025           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13026             {
13027               declarator = cp_error_declarator;
13028               break;
13029             }
13030
13031           declarator = make_array_declarator (declarator, bounds);
13032         }
13033       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13034         {
13035           tree qualifying_scope;
13036           tree unqualified_name;
13037           special_function_kind sfk;
13038           bool abstract_ok;
13039           bool pack_expansion_p = false;
13040           cp_token *declarator_id_start_token;
13041
13042           /* Parse a declarator-id */
13043           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13044           if (abstract_ok)
13045             {
13046               cp_parser_parse_tentatively (parser);
13047
13048               /* If we see an ellipsis, we should be looking at a
13049                  parameter pack. */
13050               if (token->type == CPP_ELLIPSIS)
13051                 {
13052                   /* Consume the `...' */
13053                   cp_lexer_consume_token (parser->lexer);
13054
13055                   pack_expansion_p = true;
13056                 }
13057             }
13058
13059           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13060           unqualified_name
13061             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13062           qualifying_scope = parser->scope;
13063           if (abstract_ok)
13064             {
13065               bool okay = false;
13066
13067               if (!unqualified_name && pack_expansion_p)
13068                 {
13069                   /* Check whether an error occurred. */
13070                   okay = !cp_parser_error_occurred (parser);
13071
13072                   /* We already consumed the ellipsis to mark a
13073                      parameter pack, but we have no way to report it,
13074                      so abort the tentative parse. We will be exiting
13075                      immediately anyway. */
13076                   cp_parser_abort_tentative_parse (parser);
13077                 }
13078               else
13079                 okay = cp_parser_parse_definitely (parser);
13080
13081               if (!okay)
13082                 unqualified_name = error_mark_node;
13083               else if (unqualified_name
13084                        && (qualifying_scope
13085                            || (TREE_CODE (unqualified_name)
13086                                != IDENTIFIER_NODE)))
13087                 {
13088                   cp_parser_error (parser, "expected unqualified-id");
13089                   unqualified_name = error_mark_node;
13090                 }
13091             }
13092
13093           if (!unqualified_name)
13094             return NULL;
13095           if (unqualified_name == error_mark_node)
13096             {
13097               declarator = cp_error_declarator;
13098               pack_expansion_p = false;
13099               declarator->parameter_pack_p = false;
13100               break;
13101             }
13102
13103           if (qualifying_scope && at_namespace_scope_p ()
13104               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13105             {
13106               /* In the declaration of a member of a template class
13107                  outside of the class itself, the SCOPE will sometimes
13108                  be a TYPENAME_TYPE.  For example, given:
13109
13110                  template <typename T>
13111                  int S<T>::R::i = 3;
13112
13113                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13114                  this context, we must resolve S<T>::R to an ordinary
13115                  type, rather than a typename type.
13116
13117                  The reason we normally avoid resolving TYPENAME_TYPEs
13118                  is that a specialization of `S' might render
13119                  `S<T>::R' not a type.  However, if `S' is
13120                  specialized, then this `i' will not be used, so there
13121                  is no harm in resolving the types here.  */
13122               tree type;
13123
13124               /* Resolve the TYPENAME_TYPE.  */
13125               type = resolve_typename_type (qualifying_scope,
13126                                             /*only_current_p=*/false);
13127               /* If that failed, the declarator is invalid.  */
13128               if (TREE_CODE (type) == TYPENAME_TYPE)
13129                 error ("%H%<%T::%E%> is not a type",
13130                        &declarator_id_start_token->location,
13131                        TYPE_CONTEXT (qualifying_scope),
13132                        TYPE_IDENTIFIER (qualifying_scope));
13133               qualifying_scope = type;
13134             }
13135
13136           sfk = sfk_none;
13137
13138           if (unqualified_name)
13139             {
13140               tree class_type;
13141
13142               if (qualifying_scope
13143                   && CLASS_TYPE_P (qualifying_scope))
13144                 class_type = qualifying_scope;
13145               else
13146                 class_type = current_class_type;
13147
13148               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13149                 {
13150                   tree name_type = TREE_TYPE (unqualified_name);
13151                   if (class_type && same_type_p (name_type, class_type))
13152                     {
13153                       if (qualifying_scope
13154                           && CLASSTYPE_USE_TEMPLATE (name_type))
13155                         {
13156                           error ("%Hinvalid use of constructor as a template",
13157                                  &declarator_id_start_token->location);
13158                           inform ("use %<%T::%D%> instead of %<%T::%D%> to "
13159                                   "name the constructor in a qualified name",
13160                                   class_type,
13161                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13162                                   class_type, name_type);
13163                           declarator = cp_error_declarator;
13164                           break;
13165                         }
13166                       else
13167                         unqualified_name = constructor_name (class_type);
13168                     }
13169                   else
13170                     {
13171                       /* We do not attempt to print the declarator
13172                          here because we do not have enough
13173                          information about its original syntactic
13174                          form.  */
13175                       cp_parser_error (parser, "invalid declarator");
13176                       declarator = cp_error_declarator;
13177                       break;
13178                     }
13179                 }
13180
13181               if (class_type)
13182                 {
13183                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13184                     sfk = sfk_destructor;
13185                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13186                     sfk = sfk_conversion;
13187                   else if (/* There's no way to declare a constructor
13188                               for an anonymous type, even if the type
13189                               got a name for linkage purposes.  */
13190                            !TYPE_WAS_ANONYMOUS (class_type)
13191                            && constructor_name_p (unqualified_name,
13192                                                   class_type))
13193                     {
13194                       unqualified_name = constructor_name (class_type);
13195                       sfk = sfk_constructor;
13196                     }
13197
13198                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13199                     *ctor_dtor_or_conv_p = -1;
13200                 }
13201             }
13202           declarator = make_id_declarator (qualifying_scope,
13203                                            unqualified_name,
13204                                            sfk);
13205           declarator->id_loc = token->location;
13206           declarator->parameter_pack_p = pack_expansion_p;
13207
13208           if (pack_expansion_p)
13209             maybe_warn_variadic_templates ();
13210
13211         handle_declarator:;
13212           scope = get_scope_of_declarator (declarator);
13213           if (scope)
13214             /* Any names that appear after the declarator-id for a
13215                member are looked up in the containing scope.  */
13216             pushed_scope = push_scope (scope);
13217           parser->in_declarator_p = true;
13218           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13219               || (declarator && declarator->kind == cdk_id))
13220             /* Default args are only allowed on function
13221                declarations.  */
13222             parser->default_arg_ok_p = saved_default_arg_ok_p;
13223           else
13224             parser->default_arg_ok_p = false;
13225
13226           first = false;
13227         }
13228       /* We're done.  */
13229       else
13230         break;
13231     }
13232
13233   /* For an abstract declarator, we might wind up with nothing at this
13234      point.  That's an error; the declarator is not optional.  */
13235   if (!declarator)
13236     cp_parser_error (parser, "expected declarator");
13237
13238   /* If we entered a scope, we must exit it now.  */
13239   if (pushed_scope)
13240     pop_scope (pushed_scope);
13241
13242   parser->default_arg_ok_p = saved_default_arg_ok_p;
13243   parser->in_declarator_p = saved_in_declarator_p;
13244
13245   return declarator;
13246 }
13247
13248 /* Parse a ptr-operator.
13249
13250    ptr-operator:
13251      * cv-qualifier-seq [opt]
13252      &
13253      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13254
13255    GNU Extension:
13256
13257    ptr-operator:
13258      & cv-qualifier-seq [opt]
13259
13260    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13261    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13262    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13263    filled in with the TYPE containing the member.  *CV_QUALS is
13264    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13265    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13266    Note that the tree codes returned by this function have nothing
13267    to do with the types of trees that will be eventually be created
13268    to represent the pointer or reference type being parsed. They are
13269    just constants with suggestive names. */
13270 static enum tree_code
13271 cp_parser_ptr_operator (cp_parser* parser,
13272                         tree* type,
13273                         cp_cv_quals *cv_quals)
13274 {
13275   enum tree_code code = ERROR_MARK;
13276   cp_token *token;
13277
13278   /* Assume that it's not a pointer-to-member.  */
13279   *type = NULL_TREE;
13280   /* And that there are no cv-qualifiers.  */
13281   *cv_quals = TYPE_UNQUALIFIED;
13282
13283   /* Peek at the next token.  */
13284   token = cp_lexer_peek_token (parser->lexer);
13285
13286   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13287   if (token->type == CPP_MULT)
13288     code = INDIRECT_REF;
13289   else if (token->type == CPP_AND)
13290     code = ADDR_EXPR;
13291   else if ((cxx_dialect != cxx98) &&
13292            token->type == CPP_AND_AND) /* C++0x only */
13293     code = NON_LVALUE_EXPR;
13294
13295   if (code != ERROR_MARK)
13296     {
13297       /* Consume the `*', `&' or `&&'.  */
13298       cp_lexer_consume_token (parser->lexer);
13299
13300       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13301          `&', if we are allowing GNU extensions.  (The only qualifier
13302          that can legally appear after `&' is `restrict', but that is
13303          enforced during semantic analysis.  */
13304       if (code == INDIRECT_REF
13305           || cp_parser_allow_gnu_extensions_p (parser))
13306         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13307     }
13308   else
13309     {
13310       /* Try the pointer-to-member case.  */
13311       cp_parser_parse_tentatively (parser);
13312       /* Look for the optional `::' operator.  */
13313       cp_parser_global_scope_opt (parser,
13314                                   /*current_scope_valid_p=*/false);
13315       /* Look for the nested-name specifier.  */
13316       token = cp_lexer_peek_token (parser->lexer);
13317       cp_parser_nested_name_specifier (parser,
13318                                        /*typename_keyword_p=*/false,
13319                                        /*check_dependency_p=*/true,
13320                                        /*type_p=*/false,
13321                                        /*is_declaration=*/false);
13322       /* If we found it, and the next token is a `*', then we are
13323          indeed looking at a pointer-to-member operator.  */
13324       if (!cp_parser_error_occurred (parser)
13325           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13326         {
13327           /* Indicate that the `*' operator was used.  */
13328           code = INDIRECT_REF;
13329
13330           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13331             error ("%H%qD is a namespace", &token->location, parser->scope);
13332           else
13333             {
13334               /* The type of which the member is a member is given by the
13335                  current SCOPE.  */
13336               *type = parser->scope;
13337               /* The next name will not be qualified.  */
13338               parser->scope = NULL_TREE;
13339               parser->qualifying_scope = NULL_TREE;
13340               parser->object_scope = NULL_TREE;
13341               /* Look for the optional cv-qualifier-seq.  */
13342               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13343             }
13344         }
13345       /* If that didn't work we don't have a ptr-operator.  */
13346       if (!cp_parser_parse_definitely (parser))
13347         cp_parser_error (parser, "expected ptr-operator");
13348     }
13349
13350   return code;
13351 }
13352
13353 /* Parse an (optional) cv-qualifier-seq.
13354
13355    cv-qualifier-seq:
13356      cv-qualifier cv-qualifier-seq [opt]
13357
13358    cv-qualifier:
13359      const
13360      volatile
13361
13362    GNU Extension:
13363
13364    cv-qualifier:
13365      __restrict__
13366
13367    Returns a bitmask representing the cv-qualifiers.  */
13368
13369 static cp_cv_quals
13370 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13371 {
13372   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13373
13374   while (true)
13375     {
13376       cp_token *token;
13377       cp_cv_quals cv_qualifier;
13378
13379       /* Peek at the next token.  */
13380       token = cp_lexer_peek_token (parser->lexer);
13381       /* See if it's a cv-qualifier.  */
13382       switch (token->keyword)
13383         {
13384         case RID_CONST:
13385           cv_qualifier = TYPE_QUAL_CONST;
13386           break;
13387
13388         case RID_VOLATILE:
13389           cv_qualifier = TYPE_QUAL_VOLATILE;
13390           break;
13391
13392         case RID_RESTRICT:
13393           cv_qualifier = TYPE_QUAL_RESTRICT;
13394           break;
13395
13396         default:
13397           cv_qualifier = TYPE_UNQUALIFIED;
13398           break;
13399         }
13400
13401       if (!cv_qualifier)
13402         break;
13403
13404       if (cv_quals & cv_qualifier)
13405         {
13406           error ("%Hduplicate cv-qualifier", &token->location);
13407           cp_lexer_purge_token (parser->lexer);
13408         }
13409       else
13410         {
13411           cp_lexer_consume_token (parser->lexer);
13412           cv_quals |= cv_qualifier;
13413         }
13414     }
13415
13416   return cv_quals;
13417 }
13418
13419 /* Parse a declarator-id.
13420
13421    declarator-id:
13422      id-expression
13423      :: [opt] nested-name-specifier [opt] type-name
13424
13425    In the `id-expression' case, the value returned is as for
13426    cp_parser_id_expression if the id-expression was an unqualified-id.
13427    If the id-expression was a qualified-id, then a SCOPE_REF is
13428    returned.  The first operand is the scope (either a NAMESPACE_DECL
13429    or TREE_TYPE), but the second is still just a representation of an
13430    unqualified-id.  */
13431
13432 static tree
13433 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13434 {
13435   tree id;
13436   /* The expression must be an id-expression.  Assume that qualified
13437      names are the names of types so that:
13438
13439        template <class T>
13440        int S<T>::R::i = 3;
13441
13442      will work; we must treat `S<T>::R' as the name of a type.
13443      Similarly, assume that qualified names are templates, where
13444      required, so that:
13445
13446        template <class T>
13447        int S<T>::R<T>::i = 3;
13448
13449      will work, too.  */
13450   id = cp_parser_id_expression (parser,
13451                                 /*template_keyword_p=*/false,
13452                                 /*check_dependency_p=*/false,
13453                                 /*template_p=*/NULL,
13454                                 /*declarator_p=*/true,
13455                                 optional_p);
13456   if (id && BASELINK_P (id))
13457     id = BASELINK_FUNCTIONS (id);
13458   return id;
13459 }
13460
13461 /* Parse a type-id.
13462
13463    type-id:
13464      type-specifier-seq abstract-declarator [opt]
13465
13466    Returns the TYPE specified.  */
13467
13468 static tree
13469 cp_parser_type_id (cp_parser* parser)
13470 {
13471   cp_decl_specifier_seq type_specifier_seq;
13472   cp_declarator *abstract_declarator;
13473
13474   /* Parse the type-specifier-seq.  */
13475   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13476                                 &type_specifier_seq);
13477   if (type_specifier_seq.type == error_mark_node)
13478     return error_mark_node;
13479
13480   /* There might or might not be an abstract declarator.  */
13481   cp_parser_parse_tentatively (parser);
13482   /* Look for the declarator.  */
13483   abstract_declarator
13484     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13485                             /*parenthesized_p=*/NULL,
13486                             /*member_p=*/false);
13487   /* Check to see if there really was a declarator.  */
13488   if (!cp_parser_parse_definitely (parser))
13489     abstract_declarator = NULL;
13490
13491   return groktypename (&type_specifier_seq, abstract_declarator);
13492 }
13493
13494 /* Parse a type-specifier-seq.
13495
13496    type-specifier-seq:
13497      type-specifier type-specifier-seq [opt]
13498
13499    GNU extension:
13500
13501    type-specifier-seq:
13502      attributes type-specifier-seq [opt]
13503
13504    If IS_CONDITION is true, we are at the start of a "condition",
13505    e.g., we've just seen "if (".
13506
13507    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13508
13509 static void
13510 cp_parser_type_specifier_seq (cp_parser* parser,
13511                               bool is_condition,
13512                               cp_decl_specifier_seq *type_specifier_seq)
13513 {
13514   bool seen_type_specifier = false;
13515   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13516   cp_token *start_token = NULL;
13517
13518   /* Clear the TYPE_SPECIFIER_SEQ.  */
13519   clear_decl_specs (type_specifier_seq);
13520
13521   /* Parse the type-specifiers and attributes.  */
13522   while (true)
13523     {
13524       tree type_specifier;
13525       bool is_cv_qualifier;
13526
13527       /* Check for attributes first.  */
13528       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13529         {
13530           type_specifier_seq->attributes =
13531             chainon (type_specifier_seq->attributes,
13532                      cp_parser_attributes_opt (parser));
13533           continue;
13534         }
13535
13536       /* record the token of the beginning of the type specifier seq,
13537          for error reporting purposes*/
13538      if (!start_token)
13539        start_token = cp_lexer_peek_token (parser->lexer);
13540
13541       /* Look for the type-specifier.  */
13542       type_specifier = cp_parser_type_specifier (parser,
13543                                                  flags,
13544                                                  type_specifier_seq,
13545                                                  /*is_declaration=*/false,
13546                                                  NULL,
13547                                                  &is_cv_qualifier);
13548       if (!type_specifier)
13549         {
13550           /* If the first type-specifier could not be found, this is not a
13551              type-specifier-seq at all.  */
13552           if (!seen_type_specifier)
13553             {
13554               cp_parser_error (parser, "expected type-specifier");
13555               type_specifier_seq->type = error_mark_node;
13556               return;
13557             }
13558           /* If subsequent type-specifiers could not be found, the
13559              type-specifier-seq is complete.  */
13560           break;
13561         }
13562
13563       seen_type_specifier = true;
13564       /* The standard says that a condition can be:
13565
13566             type-specifier-seq declarator = assignment-expression
13567
13568          However, given:
13569
13570            struct S {};
13571            if (int S = ...)
13572
13573          we should treat the "S" as a declarator, not as a
13574          type-specifier.  The standard doesn't say that explicitly for
13575          type-specifier-seq, but it does say that for
13576          decl-specifier-seq in an ordinary declaration.  Perhaps it
13577          would be clearer just to allow a decl-specifier-seq here, and
13578          then add a semantic restriction that if any decl-specifiers
13579          that are not type-specifiers appear, the program is invalid.  */
13580       if (is_condition && !is_cv_qualifier)
13581         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13582     }
13583
13584   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13585 }
13586
13587 /* Parse a parameter-declaration-clause.
13588
13589    parameter-declaration-clause:
13590      parameter-declaration-list [opt] ... [opt]
13591      parameter-declaration-list , ...
13592
13593    Returns a representation for the parameter declarations.  A return
13594    value of NULL indicates a parameter-declaration-clause consisting
13595    only of an ellipsis.  */
13596
13597 static cp_parameter_declarator *
13598 cp_parser_parameter_declaration_clause (cp_parser* parser)
13599 {
13600   cp_parameter_declarator *parameters;
13601   cp_token *token;
13602   bool ellipsis_p;
13603   bool is_error;
13604
13605   /* Peek at the next token.  */
13606   token = cp_lexer_peek_token (parser->lexer);
13607   /* Check for trivial parameter-declaration-clauses.  */
13608   if (token->type == CPP_ELLIPSIS)
13609     {
13610       /* Consume the `...' token.  */
13611       cp_lexer_consume_token (parser->lexer);
13612       return NULL;
13613     }
13614   else if (token->type == CPP_CLOSE_PAREN)
13615     /* There are no parameters.  */
13616     {
13617 #ifndef NO_IMPLICIT_EXTERN_C
13618       if (in_system_header && current_class_type == NULL
13619           && current_lang_name == lang_name_c)
13620         return NULL;
13621       else
13622 #endif
13623         return no_parameters;
13624     }
13625   /* Check for `(void)', too, which is a special case.  */
13626   else if (token->keyword == RID_VOID
13627            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13628                == CPP_CLOSE_PAREN))
13629     {
13630       /* Consume the `void' token.  */
13631       cp_lexer_consume_token (parser->lexer);
13632       /* There are no parameters.  */
13633       return no_parameters;
13634     }
13635
13636   /* Parse the parameter-declaration-list.  */
13637   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13638   /* If a parse error occurred while parsing the
13639      parameter-declaration-list, then the entire
13640      parameter-declaration-clause is erroneous.  */
13641   if (is_error)
13642     return NULL;
13643
13644   /* Peek at the next token.  */
13645   token = cp_lexer_peek_token (parser->lexer);
13646   /* If it's a `,', the clause should terminate with an ellipsis.  */
13647   if (token->type == CPP_COMMA)
13648     {
13649       /* Consume the `,'.  */
13650       cp_lexer_consume_token (parser->lexer);
13651       /* Expect an ellipsis.  */
13652       ellipsis_p
13653         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13654     }
13655   /* It might also be `...' if the optional trailing `,' was
13656      omitted.  */
13657   else if (token->type == CPP_ELLIPSIS)
13658     {
13659       /* Consume the `...' token.  */
13660       cp_lexer_consume_token (parser->lexer);
13661       /* And remember that we saw it.  */
13662       ellipsis_p = true;
13663     }
13664   else
13665     ellipsis_p = false;
13666
13667   /* Finish the parameter list.  */
13668   if (parameters && ellipsis_p)
13669     parameters->ellipsis_p = true;
13670
13671   return parameters;
13672 }
13673
13674 /* Parse a parameter-declaration-list.
13675
13676    parameter-declaration-list:
13677      parameter-declaration
13678      parameter-declaration-list , parameter-declaration
13679
13680    Returns a representation of the parameter-declaration-list, as for
13681    cp_parser_parameter_declaration_clause.  However, the
13682    `void_list_node' is never appended to the list.  Upon return,
13683    *IS_ERROR will be true iff an error occurred.  */
13684
13685 static cp_parameter_declarator *
13686 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13687 {
13688   cp_parameter_declarator *parameters = NULL;
13689   cp_parameter_declarator **tail = &parameters;
13690   bool saved_in_unbraced_linkage_specification_p;
13691
13692   /* Assume all will go well.  */
13693   *is_error = false;
13694   /* The special considerations that apply to a function within an
13695      unbraced linkage specifications do not apply to the parameters
13696      to the function.  */
13697   saved_in_unbraced_linkage_specification_p 
13698     = parser->in_unbraced_linkage_specification_p;
13699   parser->in_unbraced_linkage_specification_p = false;
13700
13701   /* Look for more parameters.  */
13702   while (true)
13703     {
13704       cp_parameter_declarator *parameter;
13705       bool parenthesized_p;
13706       /* Parse the parameter.  */
13707       parameter
13708         = cp_parser_parameter_declaration (parser,
13709                                            /*template_parm_p=*/false,
13710                                            &parenthesized_p);
13711
13712       /* If a parse error occurred parsing the parameter declaration,
13713          then the entire parameter-declaration-list is erroneous.  */
13714       if (!parameter)
13715         {
13716           *is_error = true;
13717           parameters = NULL;
13718           break;
13719         }
13720       /* Add the new parameter to the list.  */
13721       *tail = parameter;
13722       tail = &parameter->next;
13723
13724       /* Peek at the next token.  */
13725       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13726           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13727           /* These are for Objective-C++ */
13728           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13729           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13730         /* The parameter-declaration-list is complete.  */
13731         break;
13732       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13733         {
13734           cp_token *token;
13735
13736           /* Peek at the next token.  */
13737           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13738           /* If it's an ellipsis, then the list is complete.  */
13739           if (token->type == CPP_ELLIPSIS)
13740             break;
13741           /* Otherwise, there must be more parameters.  Consume the
13742              `,'.  */
13743           cp_lexer_consume_token (parser->lexer);
13744           /* When parsing something like:
13745
13746                 int i(float f, double d)
13747
13748              we can tell after seeing the declaration for "f" that we
13749              are not looking at an initialization of a variable "i",
13750              but rather at the declaration of a function "i".
13751
13752              Due to the fact that the parsing of template arguments
13753              (as specified to a template-id) requires backtracking we
13754              cannot use this technique when inside a template argument
13755              list.  */
13756           if (!parser->in_template_argument_list_p
13757               && !parser->in_type_id_in_expr_p
13758               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13759               /* However, a parameter-declaration of the form
13760                  "foat(f)" (which is a valid declaration of a
13761                  parameter "f") can also be interpreted as an
13762                  expression (the conversion of "f" to "float").  */
13763               && !parenthesized_p)
13764             cp_parser_commit_to_tentative_parse (parser);
13765         }
13766       else
13767         {
13768           cp_parser_error (parser, "expected %<,%> or %<...%>");
13769           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13770             cp_parser_skip_to_closing_parenthesis (parser,
13771                                                    /*recovering=*/true,
13772                                                    /*or_comma=*/false,
13773                                                    /*consume_paren=*/false);
13774           break;
13775         }
13776     }
13777
13778   parser->in_unbraced_linkage_specification_p
13779     = saved_in_unbraced_linkage_specification_p;
13780
13781   return parameters;
13782 }
13783
13784 /* Parse a parameter declaration.
13785
13786    parameter-declaration:
13787      decl-specifier-seq ... [opt] declarator
13788      decl-specifier-seq declarator = assignment-expression
13789      decl-specifier-seq ... [opt] abstract-declarator [opt]
13790      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13791
13792    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13793    declares a template parameter.  (In that case, a non-nested `>'
13794    token encountered during the parsing of the assignment-expression
13795    is not interpreted as a greater-than operator.)
13796
13797    Returns a representation of the parameter, or NULL if an error
13798    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13799    true iff the declarator is of the form "(p)".  */
13800
13801 static cp_parameter_declarator *
13802 cp_parser_parameter_declaration (cp_parser *parser,
13803                                  bool template_parm_p,
13804                                  bool *parenthesized_p)
13805 {
13806   int declares_class_or_enum;
13807   bool greater_than_is_operator_p;
13808   cp_decl_specifier_seq decl_specifiers;
13809   cp_declarator *declarator;
13810   tree default_argument;
13811   cp_token *token = NULL, *declarator_token_start = NULL;
13812   const char *saved_message;
13813
13814   /* In a template parameter, `>' is not an operator.
13815
13816      [temp.param]
13817
13818      When parsing a default template-argument for a non-type
13819      template-parameter, the first non-nested `>' is taken as the end
13820      of the template parameter-list rather than a greater-than
13821      operator.  */
13822   greater_than_is_operator_p = !template_parm_p;
13823
13824   /* Type definitions may not appear in parameter types.  */
13825   saved_message = parser->type_definition_forbidden_message;
13826   parser->type_definition_forbidden_message
13827     = "types may not be defined in parameter types";
13828
13829   /* Parse the declaration-specifiers.  */
13830   cp_parser_decl_specifier_seq (parser,
13831                                 CP_PARSER_FLAGS_NONE,
13832                                 &decl_specifiers,
13833                                 &declares_class_or_enum);
13834   /* If an error occurred, there's no reason to attempt to parse the
13835      rest of the declaration.  */
13836   if (cp_parser_error_occurred (parser))
13837     {
13838       parser->type_definition_forbidden_message = saved_message;
13839       return NULL;
13840     }
13841
13842   /* Peek at the next token.  */
13843   token = cp_lexer_peek_token (parser->lexer);
13844
13845   /* If the next token is a `)', `,', `=', `>', or `...', then there
13846      is no declarator. However, when variadic templates are enabled,
13847      there may be a declarator following `...'.  */
13848   if (token->type == CPP_CLOSE_PAREN
13849       || token->type == CPP_COMMA
13850       || token->type == CPP_EQ
13851       || token->type == CPP_GREATER)
13852     {
13853       declarator = NULL;
13854       if (parenthesized_p)
13855         *parenthesized_p = false;
13856     }
13857   /* Otherwise, there should be a declarator.  */
13858   else
13859     {
13860       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13861       parser->default_arg_ok_p = false;
13862
13863       /* After seeing a decl-specifier-seq, if the next token is not a
13864          "(", there is no possibility that the code is a valid
13865          expression.  Therefore, if parsing tentatively, we commit at
13866          this point.  */
13867       if (!parser->in_template_argument_list_p
13868           /* In an expression context, having seen:
13869
13870                (int((char ...
13871
13872              we cannot be sure whether we are looking at a
13873              function-type (taking a "char" as a parameter) or a cast
13874              of some object of type "char" to "int".  */
13875           && !parser->in_type_id_in_expr_p
13876           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13877           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13878         cp_parser_commit_to_tentative_parse (parser);
13879       /* Parse the declarator.  */
13880       declarator_token_start = token;
13881       declarator = cp_parser_declarator (parser,
13882                                          CP_PARSER_DECLARATOR_EITHER,
13883                                          /*ctor_dtor_or_conv_p=*/NULL,
13884                                          parenthesized_p,
13885                                          /*member_p=*/false);
13886       parser->default_arg_ok_p = saved_default_arg_ok_p;
13887       /* After the declarator, allow more attributes.  */
13888       decl_specifiers.attributes
13889         = chainon (decl_specifiers.attributes,
13890                    cp_parser_attributes_opt (parser));
13891     }
13892
13893   /* If the next token is an ellipsis, and we have not seen a
13894      declarator name, and the type of the declarator contains parameter
13895      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13896      a parameter pack expansion expression. Otherwise, leave the
13897      ellipsis for a C-style variadic function. */
13898   token = cp_lexer_peek_token (parser->lexer);
13899   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13900     {
13901       tree type = decl_specifiers.type;
13902
13903       if (type && DECL_P (type))
13904         type = TREE_TYPE (type);
13905
13906       if (type
13907           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13908           && declarator_can_be_parameter_pack (declarator)
13909           && (!declarator || !declarator->parameter_pack_p)
13910           && uses_parameter_packs (type))
13911         {
13912           /* Consume the `...'. */
13913           cp_lexer_consume_token (parser->lexer);
13914           maybe_warn_variadic_templates ();
13915           
13916           /* Build a pack expansion type */
13917           if (declarator)
13918             declarator->parameter_pack_p = true;
13919           else
13920             decl_specifiers.type = make_pack_expansion (type);
13921         }
13922     }
13923
13924   /* The restriction on defining new types applies only to the type
13925      of the parameter, not to the default argument.  */
13926   parser->type_definition_forbidden_message = saved_message;
13927
13928   /* If the next token is `=', then process a default argument.  */
13929   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13930     {
13931       /* Consume the `='.  */
13932       cp_lexer_consume_token (parser->lexer);
13933
13934       /* If we are defining a class, then the tokens that make up the
13935          default argument must be saved and processed later.  */
13936       if (!template_parm_p && at_class_scope_p ()
13937           && TYPE_BEING_DEFINED (current_class_type))
13938         {
13939           unsigned depth = 0;
13940           int maybe_template_id = 0;
13941           cp_token *first_token;
13942           cp_token *token;
13943
13944           /* Add tokens until we have processed the entire default
13945              argument.  We add the range [first_token, token).  */
13946           first_token = cp_lexer_peek_token (parser->lexer);
13947           while (true)
13948             {
13949               bool done = false;
13950
13951               /* Peek at the next token.  */
13952               token = cp_lexer_peek_token (parser->lexer);
13953               /* What we do depends on what token we have.  */
13954               switch (token->type)
13955                 {
13956                   /* In valid code, a default argument must be
13957                      immediately followed by a `,' `)', or `...'.  */
13958                 case CPP_COMMA:
13959                   if (depth == 0 && maybe_template_id)
13960                     {
13961                       /* If we've seen a '<', we might be in a
13962                          template-argument-list.  Until Core issue 325 is
13963                          resolved, we don't know how this situation ought
13964                          to be handled, so try to DTRT.  We check whether
13965                          what comes after the comma is a valid parameter
13966                          declaration list.  If it is, then the comma ends
13967                          the default argument; otherwise the default
13968                          argument continues.  */
13969                       bool error = false;
13970
13971                       /* Set ITALP so cp_parser_parameter_declaration_list
13972                          doesn't decide to commit to this parse.  */
13973                       bool saved_italp = parser->in_template_argument_list_p;
13974                       parser->in_template_argument_list_p = true;
13975
13976                       cp_parser_parse_tentatively (parser);
13977                       cp_lexer_consume_token (parser->lexer);
13978                       cp_parser_parameter_declaration_list (parser, &error);
13979                       if (!cp_parser_error_occurred (parser) && !error)
13980                         done = true;
13981                       cp_parser_abort_tentative_parse (parser);
13982
13983                       parser->in_template_argument_list_p = saved_italp;
13984                       break;
13985                     }
13986                 case CPP_CLOSE_PAREN:
13987                 case CPP_ELLIPSIS:
13988                   /* If we run into a non-nested `;', `}', or `]',
13989                      then the code is invalid -- but the default
13990                      argument is certainly over.  */
13991                 case CPP_SEMICOLON:
13992                 case CPP_CLOSE_BRACE:
13993                 case CPP_CLOSE_SQUARE:
13994                   if (depth == 0)
13995                     done = true;
13996                   /* Update DEPTH, if necessary.  */
13997                   else if (token->type == CPP_CLOSE_PAREN
13998                            || token->type == CPP_CLOSE_BRACE
13999                            || token->type == CPP_CLOSE_SQUARE)
14000                     --depth;
14001                   break;
14002
14003                 case CPP_OPEN_PAREN:
14004                 case CPP_OPEN_SQUARE:
14005                 case CPP_OPEN_BRACE:
14006                   ++depth;
14007                   break;
14008
14009                 case CPP_LESS:
14010                   if (depth == 0)
14011                     /* This might be the comparison operator, or it might
14012                        start a template argument list.  */
14013                     ++maybe_template_id;
14014                   break;
14015
14016                 case CPP_RSHIFT:
14017                   if (cxx_dialect == cxx98)
14018                     break;
14019                   /* Fall through for C++0x, which treats the `>>'
14020                      operator like two `>' tokens in certain
14021                      cases.  */
14022
14023                 case CPP_GREATER:
14024                   if (depth == 0)
14025                     {
14026                       /* This might be an operator, or it might close a
14027                          template argument list.  But if a previous '<'
14028                          started a template argument list, this will have
14029                          closed it, so we can't be in one anymore.  */
14030                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14031                       if (maybe_template_id < 0)
14032                         maybe_template_id = 0;
14033                     }
14034                   break;
14035
14036                   /* If we run out of tokens, issue an error message.  */
14037                 case CPP_EOF:
14038                 case CPP_PRAGMA_EOL:
14039                   error ("%Hfile ends in default argument", &token->location);
14040                   done = true;
14041                   break;
14042
14043                 case CPP_NAME:
14044                 case CPP_SCOPE:
14045                   /* In these cases, we should look for template-ids.
14046                      For example, if the default argument is
14047                      `X<int, double>()', we need to do name lookup to
14048                      figure out whether or not `X' is a template; if
14049                      so, the `,' does not end the default argument.
14050
14051                      That is not yet done.  */
14052                   break;
14053
14054                 default:
14055                   break;
14056                 }
14057
14058               /* If we've reached the end, stop.  */
14059               if (done)
14060                 break;
14061
14062               /* Add the token to the token block.  */
14063               token = cp_lexer_consume_token (parser->lexer);
14064             }
14065
14066           /* Create a DEFAULT_ARG to represent the unparsed default
14067              argument.  */
14068           default_argument = make_node (DEFAULT_ARG);
14069           DEFARG_TOKENS (default_argument)
14070             = cp_token_cache_new (first_token, token);
14071           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14072         }
14073       /* Outside of a class definition, we can just parse the
14074          assignment-expression.  */
14075       else
14076         {
14077           token = cp_lexer_peek_token (parser->lexer);
14078           default_argument 
14079             = cp_parser_default_argument (parser, template_parm_p);
14080         }
14081
14082       if (!parser->default_arg_ok_p)
14083         {
14084           if (flag_permissive)
14085             warning (0, "deprecated use of default argument for parameter of non-function");
14086           else
14087             {
14088               error ("%Hdefault arguments are only "
14089                      "permitted for function parameters",
14090                      &token->location);
14091               default_argument = NULL_TREE;
14092             }
14093         }
14094       else if ((declarator && declarator->parameter_pack_p)
14095                || (decl_specifiers.type
14096                    && PACK_EXPANSION_P (decl_specifiers.type)))
14097         {
14098           const char* kind = template_parm_p? "template " : "";
14099           
14100           /* Find the name of the parameter pack.  */     
14101           cp_declarator *id_declarator = declarator;
14102           while (id_declarator && id_declarator->kind != cdk_id)
14103             id_declarator = id_declarator->declarator;
14104           
14105           if (id_declarator && id_declarator->kind == cdk_id)
14106             error ("%H%sparameter pack %qD cannot have a default argument",
14107                    &declarator_token_start->location,
14108                    kind, id_declarator->u.id.unqualified_name);
14109           else
14110             error ("%H%sparameter pack cannot have a default argument",
14111                    &declarator_token_start->location, kind);
14112           
14113           default_argument = NULL_TREE;
14114         }
14115     }
14116   else
14117     default_argument = NULL_TREE;
14118
14119   return make_parameter_declarator (&decl_specifiers,
14120                                     declarator,
14121                                     default_argument);
14122 }
14123
14124 /* Parse a default argument and return it.
14125
14126    TEMPLATE_PARM_P is true if this is a default argument for a
14127    non-type template parameter.  */
14128 static tree
14129 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14130 {
14131   tree default_argument = NULL_TREE;
14132   bool saved_greater_than_is_operator_p;
14133   bool saved_local_variables_forbidden_p;
14134
14135   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14136      set correctly.  */
14137   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14138   parser->greater_than_is_operator_p = !template_parm_p;
14139   /* Local variable names (and the `this' keyword) may not
14140      appear in a default argument.  */
14141   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14142   parser->local_variables_forbidden_p = true;
14143   /* The default argument expression may cause implicitly
14144      defined member functions to be synthesized, which will
14145      result in garbage collection.  We must treat this
14146      situation as if we were within the body of function so as
14147      to avoid collecting live data on the stack.  */
14148   ++function_depth;
14149   /* Parse the assignment-expression.  */
14150   if (template_parm_p)
14151     push_deferring_access_checks (dk_no_deferred);
14152   default_argument
14153     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14154   if (template_parm_p)
14155     pop_deferring_access_checks ();
14156   /* Restore saved state.  */
14157   --function_depth;
14158   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14159   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14160
14161   return default_argument;
14162 }
14163
14164 /* Parse a function-body.
14165
14166    function-body:
14167      compound_statement  */
14168
14169 static void
14170 cp_parser_function_body (cp_parser *parser)
14171 {
14172   cp_parser_compound_statement (parser, NULL, false);
14173 }
14174
14175 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14176    true if a ctor-initializer was present.  */
14177
14178 static bool
14179 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14180 {
14181   tree body;
14182   bool ctor_initializer_p;
14183
14184   /* Begin the function body.  */
14185   body = begin_function_body ();
14186   /* Parse the optional ctor-initializer.  */
14187   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14188   /* Parse the function-body.  */
14189   cp_parser_function_body (parser);
14190   /* Finish the function body.  */
14191   finish_function_body (body);
14192
14193   return ctor_initializer_p;
14194 }
14195
14196 /* Parse an initializer.
14197
14198    initializer:
14199      = initializer-clause
14200      ( expression-list )
14201
14202    Returns an expression representing the initializer.  If no
14203    initializer is present, NULL_TREE is returned.
14204
14205    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14206    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14207    set to TRUE if there is no initializer present.  If there is an
14208    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14209    is set to true; otherwise it is set to false.  */
14210
14211 static tree
14212 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14213                        bool* non_constant_p)
14214 {
14215   cp_token *token;
14216   tree init;
14217
14218   /* Peek at the next token.  */
14219   token = cp_lexer_peek_token (parser->lexer);
14220
14221   /* Let our caller know whether or not this initializer was
14222      parenthesized.  */
14223   *is_direct_init = (token->type != CPP_EQ);
14224   /* Assume that the initializer is constant.  */
14225   *non_constant_p = false;
14226
14227   if (token->type == CPP_EQ)
14228     {
14229       /* Consume the `='.  */
14230       cp_lexer_consume_token (parser->lexer);
14231       /* Parse the initializer-clause.  */
14232       init = cp_parser_initializer_clause (parser, non_constant_p);
14233     }
14234   else if (token->type == CPP_OPEN_PAREN)
14235     init = cp_parser_parenthesized_expression_list (parser, false,
14236                                                     /*cast_p=*/false,
14237                                                     /*allow_expansion_p=*/true,
14238                                                     non_constant_p);
14239   else if (token->type == CPP_OPEN_BRACE)
14240     {
14241       maybe_warn_cpp0x ("extended initializer lists");
14242       init = cp_parser_braced_list (parser, non_constant_p);
14243       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14244     }
14245   else
14246     {
14247       /* Anything else is an error.  */
14248       cp_parser_error (parser, "expected initializer");
14249       init = error_mark_node;
14250     }
14251
14252   return init;
14253 }
14254
14255 /* Parse an initializer-clause.
14256
14257    initializer-clause:
14258      assignment-expression
14259      braced-init-list
14260
14261    Returns an expression representing the initializer.
14262
14263    If the `assignment-expression' production is used the value
14264    returned is simply a representation for the expression.
14265
14266    Otherwise, calls cp_parser_braced_list.  */
14267
14268 static tree
14269 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14270 {
14271   tree initializer;
14272
14273   /* Assume the expression is constant.  */
14274   *non_constant_p = false;
14275
14276   /* If it is not a `{', then we are looking at an
14277      assignment-expression.  */
14278   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14279     {
14280       initializer
14281         = cp_parser_constant_expression (parser,
14282                                         /*allow_non_constant_p=*/true,
14283                                         non_constant_p);
14284       if (!*non_constant_p)
14285         initializer = fold_non_dependent_expr (initializer);
14286     }
14287   else
14288     initializer = cp_parser_braced_list (parser, non_constant_p);
14289
14290   return initializer;
14291 }
14292
14293 /* Parse a brace-enclosed initializer list.
14294
14295    braced-init-list:
14296      { initializer-list , [opt] }
14297      { }
14298
14299    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14300    the elements of the initializer-list (or NULL, if the last
14301    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14302    NULL_TREE.  There is no way to detect whether or not the optional
14303    trailing `,' was provided.  NON_CONSTANT_P is as for
14304    cp_parser_initializer.  */     
14305
14306 static tree
14307 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14308 {
14309   tree initializer;
14310
14311   /* Consume the `{' token.  */
14312   cp_lexer_consume_token (parser->lexer);
14313   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14314   initializer = make_node (CONSTRUCTOR);
14315   /* If it's not a `}', then there is a non-trivial initializer.  */
14316   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14317     {
14318       /* Parse the initializer list.  */
14319       CONSTRUCTOR_ELTS (initializer)
14320         = cp_parser_initializer_list (parser, non_constant_p);
14321       /* A trailing `,' token is allowed.  */
14322       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14323         cp_lexer_consume_token (parser->lexer);
14324     }
14325   /* Now, there should be a trailing `}'.  */
14326   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14327   TREE_TYPE (initializer) = init_list_type_node;
14328   return initializer;
14329 }
14330
14331 /* Parse an initializer-list.
14332
14333    initializer-list:
14334      initializer-clause ... [opt]
14335      initializer-list , initializer-clause ... [opt]
14336
14337    GNU Extension:
14338
14339    initializer-list:
14340      identifier : initializer-clause
14341      initializer-list, identifier : initializer-clause
14342
14343    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14344    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14345    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14346    as for cp_parser_initializer.  */
14347
14348 static VEC(constructor_elt,gc) *
14349 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14350 {
14351   VEC(constructor_elt,gc) *v = NULL;
14352
14353   /* Assume all of the expressions are constant.  */
14354   *non_constant_p = false;
14355
14356   /* Parse the rest of the list.  */
14357   while (true)
14358     {
14359       cp_token *token;
14360       tree identifier;
14361       tree initializer;
14362       bool clause_non_constant_p;
14363
14364       /* If the next token is an identifier and the following one is a
14365          colon, we are looking at the GNU designated-initializer
14366          syntax.  */
14367       if (cp_parser_allow_gnu_extensions_p (parser)
14368           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14369           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14370         {
14371           /* Warn the user that they are using an extension.  */
14372           if (pedantic)
14373             pedwarn ("ISO C++ does not allow designated initializers");
14374           /* Consume the identifier.  */
14375           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14376           /* Consume the `:'.  */
14377           cp_lexer_consume_token (parser->lexer);
14378         }
14379       else
14380         identifier = NULL_TREE;
14381
14382       /* Parse the initializer.  */
14383       initializer = cp_parser_initializer_clause (parser,
14384                                                   &clause_non_constant_p);
14385       /* If any clause is non-constant, so is the entire initializer.  */
14386       if (clause_non_constant_p)
14387         *non_constant_p = true;
14388
14389       /* If we have an ellipsis, this is an initializer pack
14390          expansion.  */
14391       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14392         {
14393           /* Consume the `...'.  */
14394           cp_lexer_consume_token (parser->lexer);
14395
14396           /* Turn the initializer into an initializer expansion.  */
14397           initializer = make_pack_expansion (initializer);
14398         }
14399
14400       /* Add it to the vector.  */
14401       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14402
14403       /* If the next token is not a comma, we have reached the end of
14404          the list.  */
14405       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14406         break;
14407
14408       /* Peek at the next token.  */
14409       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14410       /* If the next token is a `}', then we're still done.  An
14411          initializer-clause can have a trailing `,' after the
14412          initializer-list and before the closing `}'.  */
14413       if (token->type == CPP_CLOSE_BRACE)
14414         break;
14415
14416       /* Consume the `,' token.  */
14417       cp_lexer_consume_token (parser->lexer);
14418     }
14419
14420   return v;
14421 }
14422
14423 /* Classes [gram.class] */
14424
14425 /* Parse a class-name.
14426
14427    class-name:
14428      identifier
14429      template-id
14430
14431    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14432    to indicate that names looked up in dependent types should be
14433    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14434    keyword has been used to indicate that the name that appears next
14435    is a template.  TAG_TYPE indicates the explicit tag given before
14436    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14437    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14438    is the class being defined in a class-head.
14439
14440    Returns the TYPE_DECL representing the class.  */
14441
14442 static tree
14443 cp_parser_class_name (cp_parser *parser,
14444                       bool typename_keyword_p,
14445                       bool template_keyword_p,
14446                       enum tag_types tag_type,
14447                       bool check_dependency_p,
14448                       bool class_head_p,
14449                       bool is_declaration)
14450 {
14451   tree decl;
14452   tree scope;
14453   bool typename_p;
14454   cp_token *token;
14455
14456   /* All class-names start with an identifier.  */
14457   token = cp_lexer_peek_token (parser->lexer);
14458   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14459     {
14460       cp_parser_error (parser, "expected class-name");
14461       return error_mark_node;
14462     }
14463
14464   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14465      to a template-id, so we save it here.  */
14466   scope = parser->scope;
14467   if (scope == error_mark_node)
14468     return error_mark_node;
14469
14470   /* Any name names a type if we're following the `typename' keyword
14471      in a qualified name where the enclosing scope is type-dependent.  */
14472   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14473                 && dependent_type_p (scope));
14474   /* Handle the common case (an identifier, but not a template-id)
14475      efficiently.  */
14476   if (token->type == CPP_NAME
14477       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14478     {
14479       cp_token *identifier_token;
14480       tree identifier;
14481       bool ambiguous_p;
14482
14483       /* Look for the identifier.  */
14484       identifier_token = cp_lexer_peek_token (parser->lexer);
14485       ambiguous_p = identifier_token->ambiguous_p;
14486       identifier = cp_parser_identifier (parser);
14487       /* If the next token isn't an identifier, we are certainly not
14488          looking at a class-name.  */
14489       if (identifier == error_mark_node)
14490         decl = error_mark_node;
14491       /* If we know this is a type-name, there's no need to look it
14492          up.  */
14493       else if (typename_p)
14494         decl = identifier;
14495       else
14496         {
14497           tree ambiguous_decls;
14498           /* If we already know that this lookup is ambiguous, then
14499              we've already issued an error message; there's no reason
14500              to check again.  */
14501           if (ambiguous_p)
14502             {
14503               cp_parser_simulate_error (parser);
14504               return error_mark_node;
14505             }
14506           /* If the next token is a `::', then the name must be a type
14507              name.
14508
14509              [basic.lookup.qual]
14510
14511              During the lookup for a name preceding the :: scope
14512              resolution operator, object, function, and enumerator
14513              names are ignored.  */
14514           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14515             tag_type = typename_type;
14516           /* Look up the name.  */
14517           decl = cp_parser_lookup_name (parser, identifier,
14518                                         tag_type,
14519                                         /*is_template=*/false,
14520                                         /*is_namespace=*/false,
14521                                         check_dependency_p,
14522                                         &ambiguous_decls,
14523                                         identifier_token->location);
14524           if (ambiguous_decls)
14525             {
14526               error ("%Hreference to %qD is ambiguous",
14527                      &identifier_token->location, identifier);
14528               print_candidates (ambiguous_decls);
14529               if (cp_parser_parsing_tentatively (parser))
14530                 {
14531                   identifier_token->ambiguous_p = true;
14532                   cp_parser_simulate_error (parser);
14533                 }
14534               return error_mark_node;
14535             }
14536         }
14537     }
14538   else
14539     {
14540       /* Try a template-id.  */
14541       decl = cp_parser_template_id (parser, template_keyword_p,
14542                                     check_dependency_p,
14543                                     is_declaration);
14544       if (decl == error_mark_node)
14545         return error_mark_node;
14546     }
14547
14548   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14549
14550   /* If this is a typename, create a TYPENAME_TYPE.  */
14551   if (typename_p && decl != error_mark_node)
14552     {
14553       decl = make_typename_type (scope, decl, typename_type,
14554                                  /*complain=*/tf_error);
14555       if (decl != error_mark_node)
14556         decl = TYPE_NAME (decl);
14557     }
14558
14559   /* Check to see that it is really the name of a class.  */
14560   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14561       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14562       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14563     /* Situations like this:
14564
14565          template <typename T> struct A {
14566            typename T::template X<int>::I i;
14567          };
14568
14569        are problematic.  Is `T::template X<int>' a class-name?  The
14570        standard does not seem to be definitive, but there is no other
14571        valid interpretation of the following `::'.  Therefore, those
14572        names are considered class-names.  */
14573     {
14574       decl = make_typename_type (scope, decl, tag_type, tf_error);
14575       if (decl != error_mark_node)
14576         decl = TYPE_NAME (decl);
14577     }
14578   else if (TREE_CODE (decl) != TYPE_DECL
14579            || TREE_TYPE (decl) == error_mark_node
14580            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14581     decl = error_mark_node;
14582
14583   if (decl == error_mark_node)
14584     cp_parser_error (parser, "expected class-name");
14585
14586   return decl;
14587 }
14588
14589 /* Parse a class-specifier.
14590
14591    class-specifier:
14592      class-head { member-specification [opt] }
14593
14594    Returns the TREE_TYPE representing the class.  */
14595
14596 static tree
14597 cp_parser_class_specifier (cp_parser* parser)
14598 {
14599   cp_token *token;
14600   tree type;
14601   tree attributes = NULL_TREE;
14602   int has_trailing_semicolon;
14603   bool nested_name_specifier_p;
14604   unsigned saved_num_template_parameter_lists;
14605   bool saved_in_function_body;
14606   tree old_scope = NULL_TREE;
14607   tree scope = NULL_TREE;
14608   tree bases;
14609
14610   push_deferring_access_checks (dk_no_deferred);
14611
14612   /* Parse the class-head.  */
14613   type = cp_parser_class_head (parser,
14614                                &nested_name_specifier_p,
14615                                &attributes,
14616                                &bases);
14617   /* If the class-head was a semantic disaster, skip the entire body
14618      of the class.  */
14619   if (!type)
14620     {
14621       cp_parser_skip_to_end_of_block_or_statement (parser);
14622       pop_deferring_access_checks ();
14623       return error_mark_node;
14624     }
14625
14626   /* Look for the `{'.  */
14627   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14628     {
14629       pop_deferring_access_checks ();
14630       return error_mark_node;
14631     }
14632
14633   /* Process the base classes. If they're invalid, skip the 
14634      entire class body.  */
14635   if (!xref_basetypes (type, bases))
14636     {
14637       /* Consuming the closing brace yields better error messages
14638          later on.  */
14639       if (cp_parser_skip_to_closing_brace (parser))
14640         cp_lexer_consume_token (parser->lexer);
14641       pop_deferring_access_checks ();
14642       return error_mark_node;
14643     }
14644
14645   /* Issue an error message if type-definitions are forbidden here.  */
14646   cp_parser_check_type_definition (parser);
14647   /* Remember that we are defining one more class.  */
14648   ++parser->num_classes_being_defined;
14649   /* Inside the class, surrounding template-parameter-lists do not
14650      apply.  */
14651   saved_num_template_parameter_lists
14652     = parser->num_template_parameter_lists;
14653   parser->num_template_parameter_lists = 0;
14654   /* We are not in a function body.  */
14655   saved_in_function_body = parser->in_function_body;
14656   parser->in_function_body = false;
14657
14658   /* Start the class.  */
14659   if (nested_name_specifier_p)
14660     {
14661       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14662       old_scope = push_inner_scope (scope);
14663     }
14664   type = begin_class_definition (type, attributes);
14665
14666   if (type == error_mark_node)
14667     /* If the type is erroneous, skip the entire body of the class.  */
14668     cp_parser_skip_to_closing_brace (parser);
14669   else
14670     /* Parse the member-specification.  */
14671     cp_parser_member_specification_opt (parser);
14672
14673   /* Look for the trailing `}'.  */
14674   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14675   /* We get better error messages by noticing a common problem: a
14676      missing trailing `;'.  */
14677   token = cp_lexer_peek_token (parser->lexer);
14678   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14679   /* Look for trailing attributes to apply to this class.  */
14680   if (cp_parser_allow_gnu_extensions_p (parser))
14681     attributes = cp_parser_attributes_opt (parser);
14682   if (type != error_mark_node)
14683     type = finish_struct (type, attributes);
14684   if (nested_name_specifier_p)
14685     pop_inner_scope (old_scope, scope);
14686   /* If this class is not itself within the scope of another class,
14687      then we need to parse the bodies of all of the queued function
14688      definitions.  Note that the queued functions defined in a class
14689      are not always processed immediately following the
14690      class-specifier for that class.  Consider:
14691
14692        struct A {
14693          struct B { void f() { sizeof (A); } };
14694        };
14695
14696      If `f' were processed before the processing of `A' were
14697      completed, there would be no way to compute the size of `A'.
14698      Note that the nesting we are interested in here is lexical --
14699      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14700      for:
14701
14702        struct A { struct B; };
14703        struct A::B { void f() { } };
14704
14705      there is no need to delay the parsing of `A::B::f'.  */
14706   if (--parser->num_classes_being_defined == 0)
14707     {
14708       tree queue_entry;
14709       tree fn;
14710       tree class_type = NULL_TREE;
14711       tree pushed_scope = NULL_TREE;
14712
14713       /* In a first pass, parse default arguments to the functions.
14714          Then, in a second pass, parse the bodies of the functions.
14715          This two-phased approach handles cases like:
14716
14717             struct S {
14718               void f() { g(); }
14719               void g(int i = 3);
14720             };
14721
14722          */
14723       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14724              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14725            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14726            TREE_PURPOSE (parser->unparsed_functions_queues)
14727              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14728         {
14729           fn = TREE_VALUE (queue_entry);
14730           /* If there are default arguments that have not yet been processed,
14731              take care of them now.  */
14732           if (class_type != TREE_PURPOSE (queue_entry))
14733             {
14734               if (pushed_scope)
14735                 pop_scope (pushed_scope);
14736               class_type = TREE_PURPOSE (queue_entry);
14737               pushed_scope = push_scope (class_type);
14738             }
14739           /* Make sure that any template parameters are in scope.  */
14740           maybe_begin_member_template_processing (fn);
14741           /* Parse the default argument expressions.  */
14742           cp_parser_late_parsing_default_args (parser, fn);
14743           /* Remove any template parameters from the symbol table.  */
14744           maybe_end_member_template_processing ();
14745         }
14746       if (pushed_scope)
14747         pop_scope (pushed_scope);
14748       /* Now parse the body of the functions.  */
14749       for (TREE_VALUE (parser->unparsed_functions_queues)
14750              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14751            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14752            TREE_VALUE (parser->unparsed_functions_queues)
14753              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14754         {
14755           /* Figure out which function we need to process.  */
14756           fn = TREE_VALUE (queue_entry);
14757           /* Parse the function.  */
14758           cp_parser_late_parsing_for_member (parser, fn);
14759         }
14760     }
14761
14762   /* Put back any saved access checks.  */
14763   pop_deferring_access_checks ();
14764
14765   /* Restore saved state.  */
14766   parser->in_function_body = saved_in_function_body;
14767   parser->num_template_parameter_lists
14768     = saved_num_template_parameter_lists;
14769
14770   return type;
14771 }
14772
14773 /* Parse a class-head.
14774
14775    class-head:
14776      class-key identifier [opt] base-clause [opt]
14777      class-key nested-name-specifier identifier base-clause [opt]
14778      class-key nested-name-specifier [opt] template-id
14779        base-clause [opt]
14780
14781    GNU Extensions:
14782      class-key attributes identifier [opt] base-clause [opt]
14783      class-key attributes nested-name-specifier identifier base-clause [opt]
14784      class-key attributes nested-name-specifier [opt] template-id
14785        base-clause [opt]
14786
14787    Upon return BASES is initialized to the list of base classes (or
14788    NULL, if there are none) in the same form returned by
14789    cp_parser_base_clause.
14790
14791    Returns the TYPE of the indicated class.  Sets
14792    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14793    involving a nested-name-specifier was used, and FALSE otherwise.
14794
14795    Returns error_mark_node if this is not a class-head.
14796
14797    Returns NULL_TREE if the class-head is syntactically valid, but
14798    semantically invalid in a way that means we should skip the entire
14799    body of the class.  */
14800
14801 static tree
14802 cp_parser_class_head (cp_parser* parser,
14803                       bool* nested_name_specifier_p,
14804                       tree *attributes_p,
14805                       tree *bases)
14806 {
14807   tree nested_name_specifier;
14808   enum tag_types class_key;
14809   tree id = NULL_TREE;
14810   tree type = NULL_TREE;
14811   tree attributes;
14812   bool template_id_p = false;
14813   bool qualified_p = false;
14814   bool invalid_nested_name_p = false;
14815   bool invalid_explicit_specialization_p = false;
14816   tree pushed_scope = NULL_TREE;
14817   unsigned num_templates;
14818   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14819   /* Assume no nested-name-specifier will be present.  */
14820   *nested_name_specifier_p = false;
14821   /* Assume no template parameter lists will be used in defining the
14822      type.  */
14823   num_templates = 0;
14824
14825   *bases = NULL_TREE;
14826
14827   /* Look for the class-key.  */
14828   class_key = cp_parser_class_key (parser);
14829   if (class_key == none_type)
14830     return error_mark_node;
14831
14832   /* Parse the attributes.  */
14833   attributes = cp_parser_attributes_opt (parser);
14834
14835   /* If the next token is `::', that is invalid -- but sometimes
14836      people do try to write:
14837
14838        struct ::S {};
14839
14840      Handle this gracefully by accepting the extra qualifier, and then
14841      issuing an error about it later if this really is a
14842      class-head.  If it turns out just to be an elaborated type
14843      specifier, remain silent.  */
14844   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14845     qualified_p = true;
14846
14847   push_deferring_access_checks (dk_no_check);
14848
14849   /* Determine the name of the class.  Begin by looking for an
14850      optional nested-name-specifier.  */
14851   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14852   nested_name_specifier
14853     = cp_parser_nested_name_specifier_opt (parser,
14854                                            /*typename_keyword_p=*/false,
14855                                            /*check_dependency_p=*/false,
14856                                            /*type_p=*/false,
14857                                            /*is_declaration=*/false);
14858   /* If there was a nested-name-specifier, then there *must* be an
14859      identifier.  */
14860   if (nested_name_specifier)
14861     {
14862       type_start_token = cp_lexer_peek_token (parser->lexer);
14863       /* Although the grammar says `identifier', it really means
14864          `class-name' or `template-name'.  You are only allowed to
14865          define a class that has already been declared with this
14866          syntax.
14867
14868          The proposed resolution for Core Issue 180 says that wherever
14869          you see `class T::X' you should treat `X' as a type-name.
14870
14871          It is OK to define an inaccessible class; for example:
14872
14873            class A { class B; };
14874            class A::B {};
14875
14876          We do not know if we will see a class-name, or a
14877          template-name.  We look for a class-name first, in case the
14878          class-name is a template-id; if we looked for the
14879          template-name first we would stop after the template-name.  */
14880       cp_parser_parse_tentatively (parser);
14881       type = cp_parser_class_name (parser,
14882                                    /*typename_keyword_p=*/false,
14883                                    /*template_keyword_p=*/false,
14884                                    class_type,
14885                                    /*check_dependency_p=*/false,
14886                                    /*class_head_p=*/true,
14887                                    /*is_declaration=*/false);
14888       /* If that didn't work, ignore the nested-name-specifier.  */
14889       if (!cp_parser_parse_definitely (parser))
14890         {
14891           invalid_nested_name_p = true;
14892           type_start_token = cp_lexer_peek_token (parser->lexer);
14893           id = cp_parser_identifier (parser);
14894           if (id == error_mark_node)
14895             id = NULL_TREE;
14896         }
14897       /* If we could not find a corresponding TYPE, treat this
14898          declaration like an unqualified declaration.  */
14899       if (type == error_mark_node)
14900         nested_name_specifier = NULL_TREE;
14901       /* Otherwise, count the number of templates used in TYPE and its
14902          containing scopes.  */
14903       else
14904         {
14905           tree scope;
14906
14907           for (scope = TREE_TYPE (type);
14908                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14909                scope = (TYPE_P (scope)
14910                         ? TYPE_CONTEXT (scope)
14911                         : DECL_CONTEXT (scope)))
14912             if (TYPE_P (scope)
14913                 && CLASS_TYPE_P (scope)
14914                 && CLASSTYPE_TEMPLATE_INFO (scope)
14915                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14916                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14917               ++num_templates;
14918         }
14919     }
14920   /* Otherwise, the identifier is optional.  */
14921   else
14922     {
14923       /* We don't know whether what comes next is a template-id,
14924          an identifier, or nothing at all.  */
14925       cp_parser_parse_tentatively (parser);
14926       /* Check for a template-id.  */
14927       type_start_token = cp_lexer_peek_token (parser->lexer);
14928       id = cp_parser_template_id (parser,
14929                                   /*template_keyword_p=*/false,
14930                                   /*check_dependency_p=*/true,
14931                                   /*is_declaration=*/true);
14932       /* If that didn't work, it could still be an identifier.  */
14933       if (!cp_parser_parse_definitely (parser))
14934         {
14935           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14936             {
14937               type_start_token = cp_lexer_peek_token (parser->lexer);
14938               id = cp_parser_identifier (parser);
14939             }
14940           else
14941             id = NULL_TREE;
14942         }
14943       else
14944         {
14945           template_id_p = true;
14946           ++num_templates;
14947         }
14948     }
14949
14950   pop_deferring_access_checks ();
14951
14952   if (id)
14953     cp_parser_check_for_invalid_template_id (parser, id,
14954                                              type_start_token->location);
14955
14956   /* If it's not a `:' or a `{' then we can't really be looking at a
14957      class-head, since a class-head only appears as part of a
14958      class-specifier.  We have to detect this situation before calling
14959      xref_tag, since that has irreversible side-effects.  */
14960   if (!cp_parser_next_token_starts_class_definition_p (parser))
14961     {
14962       cp_parser_error (parser, "expected %<{%> or %<:%>");
14963       return error_mark_node;
14964     }
14965
14966   /* At this point, we're going ahead with the class-specifier, even
14967      if some other problem occurs.  */
14968   cp_parser_commit_to_tentative_parse (parser);
14969   /* Issue the error about the overly-qualified name now.  */
14970   if (qualified_p)
14971     cp_parser_error (parser,
14972                      "global qualification of class name is invalid");
14973   else if (invalid_nested_name_p)
14974     cp_parser_error (parser,
14975                      "qualified name does not name a class");
14976   else if (nested_name_specifier)
14977     {
14978       tree scope;
14979
14980       /* Reject typedef-names in class heads.  */
14981       if (!DECL_IMPLICIT_TYPEDEF_P (type))
14982         {
14983           error ("%Hinvalid class name in declaration of %qD",
14984                  &type_start_token->location, type);
14985           type = NULL_TREE;
14986           goto done;
14987         }
14988
14989       /* Figure out in what scope the declaration is being placed.  */
14990       scope = current_scope ();
14991       /* If that scope does not contain the scope in which the
14992          class was originally declared, the program is invalid.  */
14993       if (scope && !is_ancestor (scope, nested_name_specifier))
14994         {
14995           if (at_namespace_scope_p ())
14996             error ("%Hdeclaration of %qD in namespace %qD which does not "
14997                    "enclose %qD",
14998                    &type_start_token->location,
14999                    type, scope, nested_name_specifier);
15000           else
15001             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15002                    &type_start_token->location,
15003                    type, scope, nested_name_specifier);
15004           type = NULL_TREE;
15005           goto done;
15006         }
15007       /* [dcl.meaning]
15008
15009          A declarator-id shall not be qualified exception of the
15010          definition of a ... nested class outside of its class
15011          ... [or] a the definition or explicit instantiation of a
15012          class member of a namespace outside of its namespace.  */
15013       if (scope == nested_name_specifier)
15014         {
15015           permerror ("%Hextra qualification not allowed",
15016                      &nested_name_specifier_token_start->location);
15017           nested_name_specifier = NULL_TREE;
15018           num_templates = 0;
15019         }
15020     }
15021   /* An explicit-specialization must be preceded by "template <>".  If
15022      it is not, try to recover gracefully.  */
15023   if (at_namespace_scope_p ()
15024       && parser->num_template_parameter_lists == 0
15025       && template_id_p)
15026     {
15027       error ("%Han explicit specialization must be preceded by %<template <>%>",
15028              &type_start_token->location);
15029       invalid_explicit_specialization_p = true;
15030       /* Take the same action that would have been taken by
15031          cp_parser_explicit_specialization.  */
15032       ++parser->num_template_parameter_lists;
15033       begin_specialization ();
15034     }
15035   /* There must be no "return" statements between this point and the
15036      end of this function; set "type "to the correct return value and
15037      use "goto done;" to return.  */
15038   /* Make sure that the right number of template parameters were
15039      present.  */
15040   if (!cp_parser_check_template_parameters (parser, num_templates,
15041                                             type_start_token->location))
15042     {
15043       /* If something went wrong, there is no point in even trying to
15044          process the class-definition.  */
15045       type = NULL_TREE;
15046       goto done;
15047     }
15048
15049   /* Look up the type.  */
15050   if (template_id_p)
15051     {
15052       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15053           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15054               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15055         {
15056           error ("%Hfunction template %qD redeclared as a class template",
15057                  &type_start_token->location, id);
15058           type = error_mark_node;
15059         }
15060       else
15061         {
15062           type = TREE_TYPE (id);
15063           type = maybe_process_partial_specialization (type);
15064         }
15065       if (nested_name_specifier)
15066         pushed_scope = push_scope (nested_name_specifier);
15067     }
15068   else if (nested_name_specifier)
15069     {
15070       tree class_type;
15071
15072       /* Given:
15073
15074             template <typename T> struct S { struct T };
15075             template <typename T> struct S<T>::T { };
15076
15077          we will get a TYPENAME_TYPE when processing the definition of
15078          `S::T'.  We need to resolve it to the actual type before we
15079          try to define it.  */
15080       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15081         {
15082           class_type = resolve_typename_type (TREE_TYPE (type),
15083                                               /*only_current_p=*/false);
15084           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15085             type = TYPE_NAME (class_type);
15086           else
15087             {
15088               cp_parser_error (parser, "could not resolve typename type");
15089               type = error_mark_node;
15090             }
15091         }
15092
15093       if (maybe_process_partial_specialization (TREE_TYPE (type))
15094           == error_mark_node)
15095         {
15096           type = NULL_TREE;
15097           goto done;
15098         }
15099
15100       class_type = current_class_type;
15101       /* Enter the scope indicated by the nested-name-specifier.  */
15102       pushed_scope = push_scope (nested_name_specifier);
15103       /* Get the canonical version of this type.  */
15104       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15105       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15106           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15107         {
15108           type = push_template_decl (type);
15109           if (type == error_mark_node)
15110             {
15111               type = NULL_TREE;
15112               goto done;
15113             }
15114         }
15115
15116       type = TREE_TYPE (type);
15117       *nested_name_specifier_p = true;
15118     }
15119   else      /* The name is not a nested name.  */
15120     {
15121       /* If the class was unnamed, create a dummy name.  */
15122       if (!id)
15123         id = make_anon_name ();
15124       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15125                        parser->num_template_parameter_lists);
15126     }
15127
15128   /* Indicate whether this class was declared as a `class' or as a
15129      `struct'.  */
15130   if (TREE_CODE (type) == RECORD_TYPE)
15131     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15132   cp_parser_check_class_key (class_key, type);
15133
15134   /* If this type was already complete, and we see another definition,
15135      that's an error.  */
15136   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15137     {
15138       error ("%Hredefinition of %q#T",
15139              &type_start_token->location, type);
15140       error ("%Hprevious definition of %q+#T",
15141              &type_start_token->location, type);
15142       type = NULL_TREE;
15143       goto done;
15144     }
15145   else if (type == error_mark_node)
15146     type = NULL_TREE;
15147
15148   /* We will have entered the scope containing the class; the names of
15149      base classes should be looked up in that context.  For example:
15150
15151        struct A { struct B {}; struct C; };
15152        struct A::C : B {};
15153
15154      is valid.  */
15155
15156   /* Get the list of base-classes, if there is one.  */
15157   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15158     *bases = cp_parser_base_clause (parser);
15159
15160  done:
15161   /* Leave the scope given by the nested-name-specifier.  We will
15162      enter the class scope itself while processing the members.  */
15163   if (pushed_scope)
15164     pop_scope (pushed_scope);
15165
15166   if (invalid_explicit_specialization_p)
15167     {
15168       end_specialization ();
15169       --parser->num_template_parameter_lists;
15170     }
15171   *attributes_p = attributes;
15172   return type;
15173 }
15174
15175 /* Parse a class-key.
15176
15177    class-key:
15178      class
15179      struct
15180      union
15181
15182    Returns the kind of class-key specified, or none_type to indicate
15183    error.  */
15184
15185 static enum tag_types
15186 cp_parser_class_key (cp_parser* parser)
15187 {
15188   cp_token *token;
15189   enum tag_types tag_type;
15190
15191   /* Look for the class-key.  */
15192   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15193   if (!token)
15194     return none_type;
15195
15196   /* Check to see if the TOKEN is a class-key.  */
15197   tag_type = cp_parser_token_is_class_key (token);
15198   if (!tag_type)
15199     cp_parser_error (parser, "expected class-key");
15200   return tag_type;
15201 }
15202
15203 /* Parse an (optional) member-specification.
15204
15205    member-specification:
15206      member-declaration member-specification [opt]
15207      access-specifier : member-specification [opt]  */
15208
15209 static void
15210 cp_parser_member_specification_opt (cp_parser* parser)
15211 {
15212   while (true)
15213     {
15214       cp_token *token;
15215       enum rid keyword;
15216
15217       /* Peek at the next token.  */
15218       token = cp_lexer_peek_token (parser->lexer);
15219       /* If it's a `}', or EOF then we've seen all the members.  */
15220       if (token->type == CPP_CLOSE_BRACE
15221           || token->type == CPP_EOF
15222           || token->type == CPP_PRAGMA_EOL)
15223         break;
15224
15225       /* See if this token is a keyword.  */
15226       keyword = token->keyword;
15227       switch (keyword)
15228         {
15229         case RID_PUBLIC:
15230         case RID_PROTECTED:
15231         case RID_PRIVATE:
15232           /* Consume the access-specifier.  */
15233           cp_lexer_consume_token (parser->lexer);
15234           /* Remember which access-specifier is active.  */
15235           current_access_specifier = token->u.value;
15236           /* Look for the `:'.  */
15237           cp_parser_require (parser, CPP_COLON, "%<:%>");
15238           break;
15239
15240         default:
15241           /* Accept #pragmas at class scope.  */
15242           if (token->type == CPP_PRAGMA)
15243             {
15244               cp_parser_pragma (parser, pragma_external);
15245               break;
15246             }
15247
15248           /* Otherwise, the next construction must be a
15249              member-declaration.  */
15250           cp_parser_member_declaration (parser);
15251         }
15252     }
15253 }
15254
15255 /* Parse a member-declaration.
15256
15257    member-declaration:
15258      decl-specifier-seq [opt] member-declarator-list [opt] ;
15259      function-definition ; [opt]
15260      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15261      using-declaration
15262      template-declaration
15263
15264    member-declarator-list:
15265      member-declarator
15266      member-declarator-list , member-declarator
15267
15268    member-declarator:
15269      declarator pure-specifier [opt]
15270      declarator constant-initializer [opt]
15271      identifier [opt] : constant-expression
15272
15273    GNU Extensions:
15274
15275    member-declaration:
15276      __extension__ member-declaration
15277
15278    member-declarator:
15279      declarator attributes [opt] pure-specifier [opt]
15280      declarator attributes [opt] constant-initializer [opt]
15281      identifier [opt] attributes [opt] : constant-expression  
15282
15283    C++0x Extensions:
15284
15285    member-declaration:
15286      static_assert-declaration  */
15287
15288 static void
15289 cp_parser_member_declaration (cp_parser* parser)
15290 {
15291   cp_decl_specifier_seq decl_specifiers;
15292   tree prefix_attributes;
15293   tree decl;
15294   int declares_class_or_enum;
15295   bool friend_p;
15296   cp_token *token = NULL;
15297   cp_token *decl_spec_token_start = NULL;
15298   cp_token *initializer_token_start = NULL;
15299   int saved_pedantic;
15300
15301   /* Check for the `__extension__' keyword.  */
15302   if (cp_parser_extension_opt (parser, &saved_pedantic))
15303     {
15304       /* Recurse.  */
15305       cp_parser_member_declaration (parser);
15306       /* Restore the old value of the PEDANTIC flag.  */
15307       pedantic = saved_pedantic;
15308
15309       return;
15310     }
15311
15312   /* Check for a template-declaration.  */
15313   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15314     {
15315       /* An explicit specialization here is an error condition, and we
15316          expect the specialization handler to detect and report this.  */
15317       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15318           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15319         cp_parser_explicit_specialization (parser);
15320       else
15321         cp_parser_template_declaration (parser, /*member_p=*/true);
15322
15323       return;
15324     }
15325
15326   /* Check for a using-declaration.  */
15327   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15328     {
15329       /* Parse the using-declaration.  */
15330       cp_parser_using_declaration (parser,
15331                                    /*access_declaration_p=*/false);
15332       return;
15333     }
15334
15335   /* Check for @defs.  */
15336   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15337     {
15338       tree ivar, member;
15339       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15340       ivar = ivar_chains;
15341       while (ivar)
15342         {
15343           member = ivar;
15344           ivar = TREE_CHAIN (member);
15345           TREE_CHAIN (member) = NULL_TREE;
15346           finish_member_declaration (member);
15347         }
15348       return;
15349     }
15350
15351   /* If the next token is `static_assert' we have a static assertion.  */
15352   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15353     {
15354       cp_parser_static_assert (parser, /*member_p=*/true);
15355       return;
15356     }
15357
15358   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15359     return;
15360
15361   /* Parse the decl-specifier-seq.  */
15362   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15363   cp_parser_decl_specifier_seq (parser,
15364                                 CP_PARSER_FLAGS_OPTIONAL,
15365                                 &decl_specifiers,
15366                                 &declares_class_or_enum);
15367   prefix_attributes = decl_specifiers.attributes;
15368   decl_specifiers.attributes = NULL_TREE;
15369   /* Check for an invalid type-name.  */
15370   if (!decl_specifiers.type
15371       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15372     return;
15373   /* If there is no declarator, then the decl-specifier-seq should
15374      specify a type.  */
15375   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15376     {
15377       /* If there was no decl-specifier-seq, and the next token is a
15378          `;', then we have something like:
15379
15380            struct S { ; };
15381
15382          [class.mem]
15383
15384          Each member-declaration shall declare at least one member
15385          name of the class.  */
15386       if (!decl_specifiers.any_specifiers_p)
15387         {
15388           cp_token *token = cp_lexer_peek_token (parser->lexer);
15389           if (pedantic && !in_system_header_at (token->location))
15390             pedwarn ("%Hextra %<;%>", &token->location);
15391         }
15392       else
15393         {
15394           tree type;
15395
15396           /* See if this declaration is a friend.  */
15397           friend_p = cp_parser_friend_p (&decl_specifiers);
15398           /* If there were decl-specifiers, check to see if there was
15399              a class-declaration.  */
15400           type = check_tag_decl (&decl_specifiers);
15401           /* Nested classes have already been added to the class, but
15402              a `friend' needs to be explicitly registered.  */
15403           if (friend_p)
15404             {
15405               /* If the `friend' keyword was present, the friend must
15406                  be introduced with a class-key.  */
15407                if (!declares_class_or_enum)
15408                  error ("%Ha class-key must be used when declaring a friend",
15409                         &decl_spec_token_start->location);
15410                /* In this case:
15411
15412                     template <typename T> struct A {
15413                       friend struct A<T>::B;
15414                     };
15415
15416                   A<T>::B will be represented by a TYPENAME_TYPE, and
15417                   therefore not recognized by check_tag_decl.  */
15418                if (!type
15419                    && decl_specifiers.type
15420                    && TYPE_P (decl_specifiers.type))
15421                  type = decl_specifiers.type;
15422                if (!type || !TYPE_P (type))
15423                  error ("%Hfriend declaration does not name a class or "
15424                         "function", &decl_spec_token_start->location);
15425                else
15426                  make_friend_class (current_class_type, type,
15427                                     /*complain=*/true);
15428             }
15429           /* If there is no TYPE, an error message will already have
15430              been issued.  */
15431           else if (!type || type == error_mark_node)
15432             ;
15433           /* An anonymous aggregate has to be handled specially; such
15434              a declaration really declares a data member (with a
15435              particular type), as opposed to a nested class.  */
15436           else if (ANON_AGGR_TYPE_P (type))
15437             {
15438               /* Remove constructors and such from TYPE, now that we
15439                  know it is an anonymous aggregate.  */
15440               fixup_anonymous_aggr (type);
15441               /* And make the corresponding data member.  */
15442               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15443               /* Add it to the class.  */
15444               finish_member_declaration (decl);
15445             }
15446           else
15447             cp_parser_check_access_in_redeclaration
15448                                               (TYPE_NAME (type),
15449                                                decl_spec_token_start->location);
15450         }
15451     }
15452   else
15453     {
15454       /* See if these declarations will be friends.  */
15455       friend_p = cp_parser_friend_p (&decl_specifiers);
15456
15457       /* Keep going until we hit the `;' at the end of the
15458          declaration.  */
15459       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15460         {
15461           tree attributes = NULL_TREE;
15462           tree first_attribute;
15463
15464           /* Peek at the next token.  */
15465           token = cp_lexer_peek_token (parser->lexer);
15466
15467           /* Check for a bitfield declaration.  */
15468           if (token->type == CPP_COLON
15469               || (token->type == CPP_NAME
15470                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15471                   == CPP_COLON))
15472             {
15473               tree identifier;
15474               tree width;
15475
15476               /* Get the name of the bitfield.  Note that we cannot just
15477                  check TOKEN here because it may have been invalidated by
15478                  the call to cp_lexer_peek_nth_token above.  */
15479               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15480                 identifier = cp_parser_identifier (parser);
15481               else
15482                 identifier = NULL_TREE;
15483
15484               /* Consume the `:' token.  */
15485               cp_lexer_consume_token (parser->lexer);
15486               /* Get the width of the bitfield.  */
15487               width
15488                 = cp_parser_constant_expression (parser,
15489                                                  /*allow_non_constant=*/false,
15490                                                  NULL);
15491
15492               /* Look for attributes that apply to the bitfield.  */
15493               attributes = cp_parser_attributes_opt (parser);
15494               /* Remember which attributes are prefix attributes and
15495                  which are not.  */
15496               first_attribute = attributes;
15497               /* Combine the attributes.  */
15498               attributes = chainon (prefix_attributes, attributes);
15499
15500               /* Create the bitfield declaration.  */
15501               decl = grokbitfield (identifier
15502                                    ? make_id_declarator (NULL_TREE,
15503                                                          identifier,
15504                                                          sfk_none)
15505                                    : NULL,
15506                                    &decl_specifiers,
15507                                    width,
15508                                    attributes);
15509             }
15510           else
15511             {
15512               cp_declarator *declarator;
15513               tree initializer;
15514               tree asm_specification;
15515               int ctor_dtor_or_conv_p;
15516
15517               /* Parse the declarator.  */
15518               declarator
15519                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15520                                         &ctor_dtor_or_conv_p,
15521                                         /*parenthesized_p=*/NULL,
15522                                         /*member_p=*/true);
15523
15524               /* If something went wrong parsing the declarator, make sure
15525                  that we at least consume some tokens.  */
15526               if (declarator == cp_error_declarator)
15527                 {
15528                   /* Skip to the end of the statement.  */
15529                   cp_parser_skip_to_end_of_statement (parser);
15530                   /* If the next token is not a semicolon, that is
15531                      probably because we just skipped over the body of
15532                      a function.  So, we consume a semicolon if
15533                      present, but do not issue an error message if it
15534                      is not present.  */
15535                   if (cp_lexer_next_token_is (parser->lexer,
15536                                               CPP_SEMICOLON))
15537                     cp_lexer_consume_token (parser->lexer);
15538                   return;
15539                 }
15540
15541               if (declares_class_or_enum & 2)
15542                 cp_parser_check_for_definition_in_return_type
15543                                             (declarator, decl_specifiers.type,
15544                                              decl_specifiers.type_location);
15545
15546               /* Look for an asm-specification.  */
15547               asm_specification = cp_parser_asm_specification_opt (parser);
15548               /* Look for attributes that apply to the declaration.  */
15549               attributes = cp_parser_attributes_opt (parser);
15550               /* Remember which attributes are prefix attributes and
15551                  which are not.  */
15552               first_attribute = attributes;
15553               /* Combine the attributes.  */
15554               attributes = chainon (prefix_attributes, attributes);
15555
15556               /* If it's an `=', then we have a constant-initializer or a
15557                  pure-specifier.  It is not correct to parse the
15558                  initializer before registering the member declaration
15559                  since the member declaration should be in scope while
15560                  its initializer is processed.  However, the rest of the
15561                  front end does not yet provide an interface that allows
15562                  us to handle this correctly.  */
15563               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15564                 {
15565                   /* In [class.mem]:
15566
15567                      A pure-specifier shall be used only in the declaration of
15568                      a virtual function.
15569
15570                      A member-declarator can contain a constant-initializer
15571                      only if it declares a static member of integral or
15572                      enumeration type.
15573
15574                      Therefore, if the DECLARATOR is for a function, we look
15575                      for a pure-specifier; otherwise, we look for a
15576                      constant-initializer.  When we call `grokfield', it will
15577                      perform more stringent semantics checks.  */
15578                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15579                   if (function_declarator_p (declarator))
15580                     initializer = cp_parser_pure_specifier (parser);
15581                   else
15582                     /* Parse the initializer.  */
15583                     initializer = cp_parser_constant_initializer (parser);
15584                 }
15585               /* Otherwise, there is no initializer.  */
15586               else
15587                 initializer = NULL_TREE;
15588
15589               /* See if we are probably looking at a function
15590                  definition.  We are certainly not looking at a
15591                  member-declarator.  Calling `grokfield' has
15592                  side-effects, so we must not do it unless we are sure
15593                  that we are looking at a member-declarator.  */
15594               if (cp_parser_token_starts_function_definition_p
15595                   (cp_lexer_peek_token (parser->lexer)))
15596                 {
15597                   /* The grammar does not allow a pure-specifier to be
15598                      used when a member function is defined.  (It is
15599                      possible that this fact is an oversight in the
15600                      standard, since a pure function may be defined
15601                      outside of the class-specifier.  */
15602                   if (initializer)
15603                     error ("%Hpure-specifier on function-definition",
15604                            &initializer_token_start->location);
15605                   decl = cp_parser_save_member_function_body (parser,
15606                                                               &decl_specifiers,
15607                                                               declarator,
15608                                                               attributes);
15609                   /* If the member was not a friend, declare it here.  */
15610                   if (!friend_p)
15611                     finish_member_declaration (decl);
15612                   /* Peek at the next token.  */
15613                   token = cp_lexer_peek_token (parser->lexer);
15614                   /* If the next token is a semicolon, consume it.  */
15615                   if (token->type == CPP_SEMICOLON)
15616                     cp_lexer_consume_token (parser->lexer);
15617                   return;
15618                 }
15619               else
15620                 /* Create the declaration.  */
15621                 decl = grokfield (declarator, &decl_specifiers,
15622                                   initializer, /*init_const_expr_p=*/true,
15623                                   asm_specification,
15624                                   attributes);
15625             }
15626
15627           /* Reset PREFIX_ATTRIBUTES.  */
15628           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15629             attributes = TREE_CHAIN (attributes);
15630           if (attributes)
15631             TREE_CHAIN (attributes) = NULL_TREE;
15632
15633           /* If there is any qualification still in effect, clear it
15634              now; we will be starting fresh with the next declarator.  */
15635           parser->scope = NULL_TREE;
15636           parser->qualifying_scope = NULL_TREE;
15637           parser->object_scope = NULL_TREE;
15638           /* If it's a `,', then there are more declarators.  */
15639           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15640             cp_lexer_consume_token (parser->lexer);
15641           /* If the next token isn't a `;', then we have a parse error.  */
15642           else if (cp_lexer_next_token_is_not (parser->lexer,
15643                                                CPP_SEMICOLON))
15644             {
15645               cp_parser_error (parser, "expected %<;%>");
15646               /* Skip tokens until we find a `;'.  */
15647               cp_parser_skip_to_end_of_statement (parser);
15648
15649               break;
15650             }
15651
15652           if (decl)
15653             {
15654               /* Add DECL to the list of members.  */
15655               if (!friend_p)
15656                 finish_member_declaration (decl);
15657
15658               if (TREE_CODE (decl) == FUNCTION_DECL)
15659                 cp_parser_save_default_args (parser, decl);
15660             }
15661         }
15662     }
15663
15664   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15665 }
15666
15667 /* Parse a pure-specifier.
15668
15669    pure-specifier:
15670      = 0
15671
15672    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15673    Otherwise, ERROR_MARK_NODE is returned.  */
15674
15675 static tree
15676 cp_parser_pure_specifier (cp_parser* parser)
15677 {
15678   cp_token *token;
15679
15680   /* Look for the `=' token.  */
15681   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15682     return error_mark_node;
15683   /* Look for the `0' token.  */
15684   token = cp_lexer_consume_token (parser->lexer);
15685   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15686   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15687     {
15688       cp_parser_error (parser,
15689                        "invalid pure specifier (only %<= 0%> is allowed)");
15690       cp_parser_skip_to_end_of_statement (parser);
15691       return error_mark_node;
15692     }
15693   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15694     {
15695       error ("%Htemplates may not be %<virtual%>", &token->location);
15696       return error_mark_node;
15697     }
15698
15699   return integer_zero_node;
15700 }
15701
15702 /* Parse a constant-initializer.
15703
15704    constant-initializer:
15705      = constant-expression
15706
15707    Returns a representation of the constant-expression.  */
15708
15709 static tree
15710 cp_parser_constant_initializer (cp_parser* parser)
15711 {
15712   /* Look for the `=' token.  */
15713   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15714     return error_mark_node;
15715
15716   /* It is invalid to write:
15717
15718        struct S { static const int i = { 7 }; };
15719
15720      */
15721   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15722     {
15723       cp_parser_error (parser,
15724                        "a brace-enclosed initializer is not allowed here");
15725       /* Consume the opening brace.  */
15726       cp_lexer_consume_token (parser->lexer);
15727       /* Skip the initializer.  */
15728       cp_parser_skip_to_closing_brace (parser);
15729       /* Look for the trailing `}'.  */
15730       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15731
15732       return error_mark_node;
15733     }
15734
15735   return cp_parser_constant_expression (parser,
15736                                         /*allow_non_constant=*/false,
15737                                         NULL);
15738 }
15739
15740 /* Derived classes [gram.class.derived] */
15741
15742 /* Parse a base-clause.
15743
15744    base-clause:
15745      : base-specifier-list
15746
15747    base-specifier-list:
15748      base-specifier ... [opt]
15749      base-specifier-list , base-specifier ... [opt]
15750
15751    Returns a TREE_LIST representing the base-classes, in the order in
15752    which they were declared.  The representation of each node is as
15753    described by cp_parser_base_specifier.
15754
15755    In the case that no bases are specified, this function will return
15756    NULL_TREE, not ERROR_MARK_NODE.  */
15757
15758 static tree
15759 cp_parser_base_clause (cp_parser* parser)
15760 {
15761   tree bases = NULL_TREE;
15762
15763   /* Look for the `:' that begins the list.  */
15764   cp_parser_require (parser, CPP_COLON, "%<:%>");
15765
15766   /* Scan the base-specifier-list.  */
15767   while (true)
15768     {
15769       cp_token *token;
15770       tree base;
15771       bool pack_expansion_p = false;
15772
15773       /* Look for the base-specifier.  */
15774       base = cp_parser_base_specifier (parser);
15775       /* Look for the (optional) ellipsis. */
15776       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15777         {
15778           /* Consume the `...'. */
15779           cp_lexer_consume_token (parser->lexer);
15780
15781           pack_expansion_p = true;
15782         }
15783
15784       /* Add BASE to the front of the list.  */
15785       if (base != error_mark_node)
15786         {
15787           if (pack_expansion_p)
15788             /* Make this a pack expansion type. */
15789             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15790           
15791
15792           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15793             {
15794               TREE_CHAIN (base) = bases;
15795               bases = base;
15796             }
15797         }
15798       /* Peek at the next token.  */
15799       token = cp_lexer_peek_token (parser->lexer);
15800       /* If it's not a comma, then the list is complete.  */
15801       if (token->type != CPP_COMMA)
15802         break;
15803       /* Consume the `,'.  */
15804       cp_lexer_consume_token (parser->lexer);
15805     }
15806
15807   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15808      base class had a qualified name.  However, the next name that
15809      appears is certainly not qualified.  */
15810   parser->scope = NULL_TREE;
15811   parser->qualifying_scope = NULL_TREE;
15812   parser->object_scope = NULL_TREE;
15813
15814   return nreverse (bases);
15815 }
15816
15817 /* Parse a base-specifier.
15818
15819    base-specifier:
15820      :: [opt] nested-name-specifier [opt] class-name
15821      virtual access-specifier [opt] :: [opt] nested-name-specifier
15822        [opt] class-name
15823      access-specifier virtual [opt] :: [opt] nested-name-specifier
15824        [opt] class-name
15825
15826    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15827    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15828    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15829    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15830
15831 static tree
15832 cp_parser_base_specifier (cp_parser* parser)
15833 {
15834   cp_token *token;
15835   bool done = false;
15836   bool virtual_p = false;
15837   bool duplicate_virtual_error_issued_p = false;
15838   bool duplicate_access_error_issued_p = false;
15839   bool class_scope_p, template_p;
15840   tree access = access_default_node;
15841   tree type;
15842
15843   /* Process the optional `virtual' and `access-specifier'.  */
15844   while (!done)
15845     {
15846       /* Peek at the next token.  */
15847       token = cp_lexer_peek_token (parser->lexer);
15848       /* Process `virtual'.  */
15849       switch (token->keyword)
15850         {
15851         case RID_VIRTUAL:
15852           /* If `virtual' appears more than once, issue an error.  */
15853           if (virtual_p && !duplicate_virtual_error_issued_p)
15854             {
15855               cp_parser_error (parser,
15856                                "%<virtual%> specified more than once in base-specified");
15857               duplicate_virtual_error_issued_p = true;
15858             }
15859
15860           virtual_p = true;
15861
15862           /* Consume the `virtual' token.  */
15863           cp_lexer_consume_token (parser->lexer);
15864
15865           break;
15866
15867         case RID_PUBLIC:
15868         case RID_PROTECTED:
15869         case RID_PRIVATE:
15870           /* If more than one access specifier appears, issue an
15871              error.  */
15872           if (access != access_default_node
15873               && !duplicate_access_error_issued_p)
15874             {
15875               cp_parser_error (parser,
15876                                "more than one access specifier in base-specified");
15877               duplicate_access_error_issued_p = true;
15878             }
15879
15880           access = ridpointers[(int) token->keyword];
15881
15882           /* Consume the access-specifier.  */
15883           cp_lexer_consume_token (parser->lexer);
15884
15885           break;
15886
15887         default:
15888           done = true;
15889           break;
15890         }
15891     }
15892   /* It is not uncommon to see programs mechanically, erroneously, use
15893      the 'typename' keyword to denote (dependent) qualified types
15894      as base classes.  */
15895   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15896     {
15897       token = cp_lexer_peek_token (parser->lexer);
15898       if (!processing_template_decl)
15899         error ("%Hkeyword %<typename%> not allowed outside of templates",
15900                &token->location);
15901       else
15902         error ("%Hkeyword %<typename%> not allowed in this context "
15903                "(the base class is implicitly a type)",
15904                &token->location);
15905       cp_lexer_consume_token (parser->lexer);
15906     }
15907
15908   /* Look for the optional `::' operator.  */
15909   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15910   /* Look for the nested-name-specifier.  The simplest way to
15911      implement:
15912
15913        [temp.res]
15914
15915        The keyword `typename' is not permitted in a base-specifier or
15916        mem-initializer; in these contexts a qualified name that
15917        depends on a template-parameter is implicitly assumed to be a
15918        type name.
15919
15920      is to pretend that we have seen the `typename' keyword at this
15921      point.  */
15922   cp_parser_nested_name_specifier_opt (parser,
15923                                        /*typename_keyword_p=*/true,
15924                                        /*check_dependency_p=*/true,
15925                                        typename_type,
15926                                        /*is_declaration=*/true);
15927   /* If the base class is given by a qualified name, assume that names
15928      we see are type names or templates, as appropriate.  */
15929   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15930   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15931
15932   /* Finally, look for the class-name.  */
15933   type = cp_parser_class_name (parser,
15934                                class_scope_p,
15935                                template_p,
15936                                typename_type,
15937                                /*check_dependency_p=*/true,
15938                                /*class_head_p=*/false,
15939                                /*is_declaration=*/true);
15940
15941   if (type == error_mark_node)
15942     return error_mark_node;
15943
15944   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15945 }
15946
15947 /* Exception handling [gram.exception] */
15948
15949 /* Parse an (optional) exception-specification.
15950
15951    exception-specification:
15952      throw ( type-id-list [opt] )
15953
15954    Returns a TREE_LIST representing the exception-specification.  The
15955    TREE_VALUE of each node is a type.  */
15956
15957 static tree
15958 cp_parser_exception_specification_opt (cp_parser* parser)
15959 {
15960   cp_token *token;
15961   tree type_id_list;
15962
15963   /* Peek at the next token.  */
15964   token = cp_lexer_peek_token (parser->lexer);
15965   /* If it's not `throw', then there's no exception-specification.  */
15966   if (!cp_parser_is_keyword (token, RID_THROW))
15967     return NULL_TREE;
15968
15969   /* Consume the `throw'.  */
15970   cp_lexer_consume_token (parser->lexer);
15971
15972   /* Look for the `('.  */
15973   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
15974
15975   /* Peek at the next token.  */
15976   token = cp_lexer_peek_token (parser->lexer);
15977   /* If it's not a `)', then there is a type-id-list.  */
15978   if (token->type != CPP_CLOSE_PAREN)
15979     {
15980       const char *saved_message;
15981
15982       /* Types may not be defined in an exception-specification.  */
15983       saved_message = parser->type_definition_forbidden_message;
15984       parser->type_definition_forbidden_message
15985         = "types may not be defined in an exception-specification";
15986       /* Parse the type-id-list.  */
15987       type_id_list = cp_parser_type_id_list (parser);
15988       /* Restore the saved message.  */
15989       parser->type_definition_forbidden_message = saved_message;
15990     }
15991   else
15992     type_id_list = empty_except_spec;
15993
15994   /* Look for the `)'.  */
15995   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15996
15997   return type_id_list;
15998 }
15999
16000 /* Parse an (optional) type-id-list.
16001
16002    type-id-list:
16003      type-id ... [opt]
16004      type-id-list , type-id ... [opt]
16005
16006    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16007    in the order that the types were presented.  */
16008
16009 static tree
16010 cp_parser_type_id_list (cp_parser* parser)
16011 {
16012   tree types = NULL_TREE;
16013
16014   while (true)
16015     {
16016       cp_token *token;
16017       tree type;
16018
16019       /* Get the next type-id.  */
16020       type = cp_parser_type_id (parser);
16021       /* Parse the optional ellipsis. */
16022       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16023         {
16024           /* Consume the `...'. */
16025           cp_lexer_consume_token (parser->lexer);
16026
16027           /* Turn the type into a pack expansion expression. */
16028           type = make_pack_expansion (type);
16029         }
16030       /* Add it to the list.  */
16031       types = add_exception_specifier (types, type, /*complain=*/1);
16032       /* Peek at the next token.  */
16033       token = cp_lexer_peek_token (parser->lexer);
16034       /* If it is not a `,', we are done.  */
16035       if (token->type != CPP_COMMA)
16036         break;
16037       /* Consume the `,'.  */
16038       cp_lexer_consume_token (parser->lexer);
16039     }
16040
16041   return nreverse (types);
16042 }
16043
16044 /* Parse a try-block.
16045
16046    try-block:
16047      try compound-statement handler-seq  */
16048
16049 static tree
16050 cp_parser_try_block (cp_parser* parser)
16051 {
16052   tree try_block;
16053
16054   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16055   try_block = begin_try_block ();
16056   cp_parser_compound_statement (parser, NULL, true);
16057   finish_try_block (try_block);
16058   cp_parser_handler_seq (parser);
16059   finish_handler_sequence (try_block);
16060
16061   return try_block;
16062 }
16063
16064 /* Parse a function-try-block.
16065
16066    function-try-block:
16067      try ctor-initializer [opt] function-body handler-seq  */
16068
16069 static bool
16070 cp_parser_function_try_block (cp_parser* parser)
16071 {
16072   tree compound_stmt;
16073   tree try_block;
16074   bool ctor_initializer_p;
16075
16076   /* Look for the `try' keyword.  */
16077   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16078     return false;
16079   /* Let the rest of the front end know where we are.  */
16080   try_block = begin_function_try_block (&compound_stmt);
16081   /* Parse the function-body.  */
16082   ctor_initializer_p
16083     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16084   /* We're done with the `try' part.  */
16085   finish_function_try_block (try_block);
16086   /* Parse the handlers.  */
16087   cp_parser_handler_seq (parser);
16088   /* We're done with the handlers.  */
16089   finish_function_handler_sequence (try_block, compound_stmt);
16090
16091   return ctor_initializer_p;
16092 }
16093
16094 /* Parse a handler-seq.
16095
16096    handler-seq:
16097      handler handler-seq [opt]  */
16098
16099 static void
16100 cp_parser_handler_seq (cp_parser* parser)
16101 {
16102   while (true)
16103     {
16104       cp_token *token;
16105
16106       /* Parse the handler.  */
16107       cp_parser_handler (parser);
16108       /* Peek at the next token.  */
16109       token = cp_lexer_peek_token (parser->lexer);
16110       /* If it's not `catch' then there are no more handlers.  */
16111       if (!cp_parser_is_keyword (token, RID_CATCH))
16112         break;
16113     }
16114 }
16115
16116 /* Parse a handler.
16117
16118    handler:
16119      catch ( exception-declaration ) compound-statement  */
16120
16121 static void
16122 cp_parser_handler (cp_parser* parser)
16123 {
16124   tree handler;
16125   tree declaration;
16126
16127   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16128   handler = begin_handler ();
16129   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16130   declaration = cp_parser_exception_declaration (parser);
16131   finish_handler_parms (declaration, handler);
16132   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16133   cp_parser_compound_statement (parser, NULL, false);
16134   finish_handler (handler);
16135 }
16136
16137 /* Parse an exception-declaration.
16138
16139    exception-declaration:
16140      type-specifier-seq declarator
16141      type-specifier-seq abstract-declarator
16142      type-specifier-seq
16143      ...
16144
16145    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16146    ellipsis variant is used.  */
16147
16148 static tree
16149 cp_parser_exception_declaration (cp_parser* parser)
16150 {
16151   cp_decl_specifier_seq type_specifiers;
16152   cp_declarator *declarator;
16153   const char *saved_message;
16154
16155   /* If it's an ellipsis, it's easy to handle.  */
16156   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16157     {
16158       /* Consume the `...' token.  */
16159       cp_lexer_consume_token (parser->lexer);
16160       return NULL_TREE;
16161     }
16162
16163   /* Types may not be defined in exception-declarations.  */
16164   saved_message = parser->type_definition_forbidden_message;
16165   parser->type_definition_forbidden_message
16166     = "types may not be defined in exception-declarations";
16167
16168   /* Parse the type-specifier-seq.  */
16169   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16170                                 &type_specifiers);
16171   /* If it's a `)', then there is no declarator.  */
16172   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16173     declarator = NULL;
16174   else
16175     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16176                                        /*ctor_dtor_or_conv_p=*/NULL,
16177                                        /*parenthesized_p=*/NULL,
16178                                        /*member_p=*/false);
16179
16180   /* Restore the saved message.  */
16181   parser->type_definition_forbidden_message = saved_message;
16182
16183   if (!type_specifiers.any_specifiers_p)
16184     return error_mark_node;
16185
16186   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16187 }
16188
16189 /* Parse a throw-expression.
16190
16191    throw-expression:
16192      throw assignment-expression [opt]
16193
16194    Returns a THROW_EXPR representing the throw-expression.  */
16195
16196 static tree
16197 cp_parser_throw_expression (cp_parser* parser)
16198 {
16199   tree expression;
16200   cp_token* token;
16201
16202   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16203   token = cp_lexer_peek_token (parser->lexer);
16204   /* Figure out whether or not there is an assignment-expression
16205      following the "throw" keyword.  */
16206   if (token->type == CPP_COMMA
16207       || token->type == CPP_SEMICOLON
16208       || token->type == CPP_CLOSE_PAREN
16209       || token->type == CPP_CLOSE_SQUARE
16210       || token->type == CPP_CLOSE_BRACE
16211       || token->type == CPP_COLON)
16212     expression = NULL_TREE;
16213   else
16214     expression = cp_parser_assignment_expression (parser,
16215                                                   /*cast_p=*/false);
16216
16217   return build_throw (expression);
16218 }
16219
16220 /* GNU Extensions */
16221
16222 /* Parse an (optional) asm-specification.
16223
16224    asm-specification:
16225      asm ( string-literal )
16226
16227    If the asm-specification is present, returns a STRING_CST
16228    corresponding to the string-literal.  Otherwise, returns
16229    NULL_TREE.  */
16230
16231 static tree
16232 cp_parser_asm_specification_opt (cp_parser* parser)
16233 {
16234   cp_token *token;
16235   tree asm_specification;
16236
16237   /* Peek at the next token.  */
16238   token = cp_lexer_peek_token (parser->lexer);
16239   /* If the next token isn't the `asm' keyword, then there's no
16240      asm-specification.  */
16241   if (!cp_parser_is_keyword (token, RID_ASM))
16242     return NULL_TREE;
16243
16244   /* Consume the `asm' token.  */
16245   cp_lexer_consume_token (parser->lexer);
16246   /* Look for the `('.  */
16247   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16248
16249   /* Look for the string-literal.  */
16250   asm_specification = cp_parser_string_literal (parser, false, false);
16251
16252   /* Look for the `)'.  */
16253   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16254
16255   return asm_specification;
16256 }
16257
16258 /* Parse an asm-operand-list.
16259
16260    asm-operand-list:
16261      asm-operand
16262      asm-operand-list , asm-operand
16263
16264    asm-operand:
16265      string-literal ( expression )
16266      [ string-literal ] string-literal ( expression )
16267
16268    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16269    each node is the expression.  The TREE_PURPOSE is itself a
16270    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16271    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16272    is a STRING_CST for the string literal before the parenthesis. Returns
16273    ERROR_MARK_NODE if any of the operands are invalid.  */
16274
16275 static tree
16276 cp_parser_asm_operand_list (cp_parser* parser)
16277 {
16278   tree asm_operands = NULL_TREE;
16279   bool invalid_operands = false;
16280
16281   while (true)
16282     {
16283       tree string_literal;
16284       tree expression;
16285       tree name;
16286
16287       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16288         {
16289           /* Consume the `[' token.  */
16290           cp_lexer_consume_token (parser->lexer);
16291           /* Read the operand name.  */
16292           name = cp_parser_identifier (parser);
16293           if (name != error_mark_node)
16294             name = build_string (IDENTIFIER_LENGTH (name),
16295                                  IDENTIFIER_POINTER (name));
16296           /* Look for the closing `]'.  */
16297           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16298         }
16299       else
16300         name = NULL_TREE;
16301       /* Look for the string-literal.  */
16302       string_literal = cp_parser_string_literal (parser, false, false);
16303
16304       /* Look for the `('.  */
16305       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16306       /* Parse the expression.  */
16307       expression = cp_parser_expression (parser, /*cast_p=*/false);
16308       /* Look for the `)'.  */
16309       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16310
16311       if (name == error_mark_node 
16312           || string_literal == error_mark_node 
16313           || expression == error_mark_node)
16314         invalid_operands = true;
16315
16316       /* Add this operand to the list.  */
16317       asm_operands = tree_cons (build_tree_list (name, string_literal),
16318                                 expression,
16319                                 asm_operands);
16320       /* If the next token is not a `,', there are no more
16321          operands.  */
16322       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16323         break;
16324       /* Consume the `,'.  */
16325       cp_lexer_consume_token (parser->lexer);
16326     }
16327
16328   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16329 }
16330
16331 /* Parse an asm-clobber-list.
16332
16333    asm-clobber-list:
16334      string-literal
16335      asm-clobber-list , string-literal
16336
16337    Returns a TREE_LIST, indicating the clobbers in the order that they
16338    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16339
16340 static tree
16341 cp_parser_asm_clobber_list (cp_parser* parser)
16342 {
16343   tree clobbers = NULL_TREE;
16344
16345   while (true)
16346     {
16347       tree string_literal;
16348
16349       /* Look for the string literal.  */
16350       string_literal = cp_parser_string_literal (parser, false, false);
16351       /* Add it to the list.  */
16352       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16353       /* If the next token is not a `,', then the list is
16354          complete.  */
16355       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16356         break;
16357       /* Consume the `,' token.  */
16358       cp_lexer_consume_token (parser->lexer);
16359     }
16360
16361   return clobbers;
16362 }
16363
16364 /* Parse an (optional) series of attributes.
16365
16366    attributes:
16367      attributes attribute
16368
16369    attribute:
16370      __attribute__ (( attribute-list [opt] ))
16371
16372    The return value is as for cp_parser_attribute_list.  */
16373
16374 static tree
16375 cp_parser_attributes_opt (cp_parser* parser)
16376 {
16377   tree attributes = NULL_TREE;
16378
16379   while (true)
16380     {
16381       cp_token *token;
16382       tree attribute_list;
16383
16384       /* Peek at the next token.  */
16385       token = cp_lexer_peek_token (parser->lexer);
16386       /* If it's not `__attribute__', then we're done.  */
16387       if (token->keyword != RID_ATTRIBUTE)
16388         break;
16389
16390       /* Consume the `__attribute__' keyword.  */
16391       cp_lexer_consume_token (parser->lexer);
16392       /* Look for the two `(' tokens.  */
16393       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16394       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16395
16396       /* Peek at the next token.  */
16397       token = cp_lexer_peek_token (parser->lexer);
16398       if (token->type != CPP_CLOSE_PAREN)
16399         /* Parse the attribute-list.  */
16400         attribute_list = cp_parser_attribute_list (parser);
16401       else
16402         /* If the next token is a `)', then there is no attribute
16403            list.  */
16404         attribute_list = NULL;
16405
16406       /* Look for the two `)' tokens.  */
16407       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16408       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16409
16410       /* Add these new attributes to the list.  */
16411       attributes = chainon (attributes, attribute_list);
16412     }
16413
16414   return attributes;
16415 }
16416
16417 /* Parse an attribute-list.
16418
16419    attribute-list:
16420      attribute
16421      attribute-list , attribute
16422
16423    attribute:
16424      identifier
16425      identifier ( identifier )
16426      identifier ( identifier , expression-list )
16427      identifier ( expression-list )
16428
16429    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16430    to an attribute.  The TREE_PURPOSE of each node is the identifier
16431    indicating which attribute is in use.  The TREE_VALUE represents
16432    the arguments, if any.  */
16433
16434 static tree
16435 cp_parser_attribute_list (cp_parser* parser)
16436 {
16437   tree attribute_list = NULL_TREE;
16438   bool save_translate_strings_p = parser->translate_strings_p;
16439
16440   parser->translate_strings_p = false;
16441   while (true)
16442     {
16443       cp_token *token;
16444       tree identifier;
16445       tree attribute;
16446
16447       /* Look for the identifier.  We also allow keywords here; for
16448          example `__attribute__ ((const))' is legal.  */
16449       token = cp_lexer_peek_token (parser->lexer);
16450       if (token->type == CPP_NAME
16451           || token->type == CPP_KEYWORD)
16452         {
16453           tree arguments = NULL_TREE;
16454
16455           /* Consume the token.  */
16456           token = cp_lexer_consume_token (parser->lexer);
16457
16458           /* Save away the identifier that indicates which attribute
16459              this is.  */
16460           identifier = token->u.value;
16461           attribute = build_tree_list (identifier, NULL_TREE);
16462
16463           /* Peek at the next token.  */
16464           token = cp_lexer_peek_token (parser->lexer);
16465           /* If it's an `(', then parse the attribute arguments.  */
16466           if (token->type == CPP_OPEN_PAREN)
16467             {
16468               arguments = cp_parser_parenthesized_expression_list
16469                           (parser, true, /*cast_p=*/false,
16470                            /*allow_expansion_p=*/false,
16471                            /*non_constant_p=*/NULL);
16472               /* Save the arguments away.  */
16473               TREE_VALUE (attribute) = arguments;
16474             }
16475
16476           if (arguments != error_mark_node)
16477             {
16478               /* Add this attribute to the list.  */
16479               TREE_CHAIN (attribute) = attribute_list;
16480               attribute_list = attribute;
16481             }
16482
16483           token = cp_lexer_peek_token (parser->lexer);
16484         }
16485       /* Now, look for more attributes.  If the next token isn't a
16486          `,', we're done.  */
16487       if (token->type != CPP_COMMA)
16488         break;
16489
16490       /* Consume the comma and keep going.  */
16491       cp_lexer_consume_token (parser->lexer);
16492     }
16493   parser->translate_strings_p = save_translate_strings_p;
16494
16495   /* We built up the list in reverse order.  */
16496   return nreverse (attribute_list);
16497 }
16498
16499 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16500    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16501    current value of the PEDANTIC flag, regardless of whether or not
16502    the `__extension__' keyword is present.  The caller is responsible
16503    for restoring the value of the PEDANTIC flag.  */
16504
16505 static bool
16506 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16507 {
16508   /* Save the old value of the PEDANTIC flag.  */
16509   *saved_pedantic = pedantic;
16510
16511   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16512     {
16513       /* Consume the `__extension__' token.  */
16514       cp_lexer_consume_token (parser->lexer);
16515       /* We're not being pedantic while the `__extension__' keyword is
16516          in effect.  */
16517       pedantic = 0;
16518
16519       return true;
16520     }
16521
16522   return false;
16523 }
16524
16525 /* Parse a label declaration.
16526
16527    label-declaration:
16528      __label__ label-declarator-seq ;
16529
16530    label-declarator-seq:
16531      identifier , label-declarator-seq
16532      identifier  */
16533
16534 static void
16535 cp_parser_label_declaration (cp_parser* parser)
16536 {
16537   /* Look for the `__label__' keyword.  */
16538   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16539
16540   while (true)
16541     {
16542       tree identifier;
16543
16544       /* Look for an identifier.  */
16545       identifier = cp_parser_identifier (parser);
16546       /* If we failed, stop.  */
16547       if (identifier == error_mark_node)
16548         break;
16549       /* Declare it as a label.  */
16550       finish_label_decl (identifier);
16551       /* If the next token is a `;', stop.  */
16552       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16553         break;
16554       /* Look for the `,' separating the label declarations.  */
16555       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16556     }
16557
16558   /* Look for the final `;'.  */
16559   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16560 }
16561
16562 /* Support Functions */
16563
16564 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16565    NAME should have one of the representations used for an
16566    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16567    is returned.  If PARSER->SCOPE is a dependent type, then a
16568    SCOPE_REF is returned.
16569
16570    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16571    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16572    was formed.  Abstractly, such entities should not be passed to this
16573    function, because they do not need to be looked up, but it is
16574    simpler to check for this special case here, rather than at the
16575    call-sites.
16576
16577    In cases not explicitly covered above, this function returns a
16578    DECL, OVERLOAD, or baselink representing the result of the lookup.
16579    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16580    is returned.
16581
16582    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16583    (e.g., "struct") that was used.  In that case bindings that do not
16584    refer to types are ignored.
16585
16586    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16587    ignored.
16588
16589    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16590    are ignored.
16591
16592    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16593    types.
16594
16595    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16596    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16597    NULL_TREE otherwise.  */
16598
16599 static tree
16600 cp_parser_lookup_name (cp_parser *parser, tree name,
16601                        enum tag_types tag_type,
16602                        bool is_template,
16603                        bool is_namespace,
16604                        bool check_dependency,
16605                        tree *ambiguous_decls,
16606                        location_t name_location)
16607 {
16608   int flags = 0;
16609   tree decl;
16610   tree object_type = parser->context->object_type;
16611
16612   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16613     flags |= LOOKUP_COMPLAIN;
16614
16615   /* Assume that the lookup will be unambiguous.  */
16616   if (ambiguous_decls)
16617     *ambiguous_decls = NULL_TREE;
16618
16619   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16620      no longer valid.  Note that if we are parsing tentatively, and
16621      the parse fails, OBJECT_TYPE will be automatically restored.  */
16622   parser->context->object_type = NULL_TREE;
16623
16624   if (name == error_mark_node)
16625     return error_mark_node;
16626
16627   /* A template-id has already been resolved; there is no lookup to
16628      do.  */
16629   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16630     return name;
16631   if (BASELINK_P (name))
16632     {
16633       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16634                   == TEMPLATE_ID_EXPR);
16635       return name;
16636     }
16637
16638   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16639      it should already have been checked to make sure that the name
16640      used matches the type being destroyed.  */
16641   if (TREE_CODE (name) == BIT_NOT_EXPR)
16642     {
16643       tree type;
16644
16645       /* Figure out to which type this destructor applies.  */
16646       if (parser->scope)
16647         type = parser->scope;
16648       else if (object_type)
16649         type = object_type;
16650       else
16651         type = current_class_type;
16652       /* If that's not a class type, there is no destructor.  */
16653       if (!type || !CLASS_TYPE_P (type))
16654         return error_mark_node;
16655       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16656         lazily_declare_fn (sfk_destructor, type);
16657       if (!CLASSTYPE_DESTRUCTORS (type))
16658           return error_mark_node;
16659       /* If it was a class type, return the destructor.  */
16660       return CLASSTYPE_DESTRUCTORS (type);
16661     }
16662
16663   /* By this point, the NAME should be an ordinary identifier.  If
16664      the id-expression was a qualified name, the qualifying scope is
16665      stored in PARSER->SCOPE at this point.  */
16666   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16667
16668   /* Perform the lookup.  */
16669   if (parser->scope)
16670     {
16671       bool dependent_p;
16672
16673       if (parser->scope == error_mark_node)
16674         return error_mark_node;
16675
16676       /* If the SCOPE is dependent, the lookup must be deferred until
16677          the template is instantiated -- unless we are explicitly
16678          looking up names in uninstantiated templates.  Even then, we
16679          cannot look up the name if the scope is not a class type; it
16680          might, for example, be a template type parameter.  */
16681       dependent_p = (TYPE_P (parser->scope)
16682                      && !(parser->in_declarator_p
16683                           && currently_open_class (parser->scope))
16684                      && dependent_type_p (parser->scope));
16685       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16686            && dependent_p)
16687         {
16688           if (tag_type)
16689             {
16690               tree type;
16691
16692               /* The resolution to Core Issue 180 says that `struct
16693                  A::B' should be considered a type-name, even if `A'
16694                  is dependent.  */
16695               type = make_typename_type (parser->scope, name, tag_type,
16696                                          /*complain=*/tf_error);
16697               decl = TYPE_NAME (type);
16698             }
16699           else if (is_template
16700                    && (cp_parser_next_token_ends_template_argument_p (parser)
16701                        || cp_lexer_next_token_is (parser->lexer,
16702                                                   CPP_CLOSE_PAREN)))
16703             decl = make_unbound_class_template (parser->scope,
16704                                                 name, NULL_TREE,
16705                                                 /*complain=*/tf_error);
16706           else
16707             decl = build_qualified_name (/*type=*/NULL_TREE,
16708                                          parser->scope, name,
16709                                          is_template);
16710         }
16711       else
16712         {
16713           tree pushed_scope = NULL_TREE;
16714
16715           /* If PARSER->SCOPE is a dependent type, then it must be a
16716              class type, and we must not be checking dependencies;
16717              otherwise, we would have processed this lookup above.  So
16718              that PARSER->SCOPE is not considered a dependent base by
16719              lookup_member, we must enter the scope here.  */
16720           if (dependent_p)
16721             pushed_scope = push_scope (parser->scope);
16722           /* If the PARSER->SCOPE is a template specialization, it
16723              may be instantiated during name lookup.  In that case,
16724              errors may be issued.  Even if we rollback the current
16725              tentative parse, those errors are valid.  */
16726           decl = lookup_qualified_name (parser->scope, name,
16727                                         tag_type != none_type,
16728                                         /*complain=*/true);
16729
16730           /* If we have a single function from a using decl, pull it out.  */
16731           if (decl
16732               && TREE_CODE (decl) == OVERLOAD
16733               && !really_overloaded_fn (decl))
16734             decl = OVL_FUNCTION (decl);
16735
16736           if (pushed_scope)
16737             pop_scope (pushed_scope);
16738         }
16739       parser->qualifying_scope = parser->scope;
16740       parser->object_scope = NULL_TREE;
16741     }
16742   else if (object_type)
16743     {
16744       tree object_decl = NULL_TREE;
16745       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16746          OBJECT_TYPE is not a class.  */
16747       if (CLASS_TYPE_P (object_type))
16748         /* If the OBJECT_TYPE is a template specialization, it may
16749            be instantiated during name lookup.  In that case, errors
16750            may be issued.  Even if we rollback the current tentative
16751            parse, those errors are valid.  */
16752         object_decl = lookup_member (object_type,
16753                                      name,
16754                                      /*protect=*/0,
16755                                      tag_type != none_type);
16756       /* Look it up in the enclosing context, too.  */
16757       decl = lookup_name_real (name, tag_type != none_type,
16758                                /*nonclass=*/0,
16759                                /*block_p=*/true, is_namespace, flags);
16760       parser->object_scope = object_type;
16761       parser->qualifying_scope = NULL_TREE;
16762       if (object_decl)
16763         decl = object_decl;
16764     }
16765   else
16766     {
16767       decl = lookup_name_real (name, tag_type != none_type,
16768                                /*nonclass=*/0,
16769                                /*block_p=*/true, is_namespace, flags);
16770       parser->qualifying_scope = NULL_TREE;
16771       parser->object_scope = NULL_TREE;
16772     }
16773
16774   /* If the lookup failed, let our caller know.  */
16775   if (!decl || decl == error_mark_node)
16776     return error_mark_node;
16777
16778   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16779   if (TREE_CODE (decl) == TREE_LIST)
16780     {
16781       if (ambiguous_decls)
16782         *ambiguous_decls = decl;
16783       /* The error message we have to print is too complicated for
16784          cp_parser_error, so we incorporate its actions directly.  */
16785       if (!cp_parser_simulate_error (parser))
16786         {
16787           error ("%Hreference to %qD is ambiguous",
16788                  &name_location, name);
16789           print_candidates (decl);
16790         }
16791       return error_mark_node;
16792     }
16793
16794   gcc_assert (DECL_P (decl)
16795               || TREE_CODE (decl) == OVERLOAD
16796               || TREE_CODE (decl) == SCOPE_REF
16797               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16798               || BASELINK_P (decl));
16799
16800   /* If we have resolved the name of a member declaration, check to
16801      see if the declaration is accessible.  When the name resolves to
16802      set of overloaded functions, accessibility is checked when
16803      overload resolution is done.
16804
16805      During an explicit instantiation, access is not checked at all,
16806      as per [temp.explicit].  */
16807   if (DECL_P (decl))
16808     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16809
16810   return decl;
16811 }
16812
16813 /* Like cp_parser_lookup_name, but for use in the typical case where
16814    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16815    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16816
16817 static tree
16818 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16819 {
16820   return cp_parser_lookup_name (parser, name,
16821                                 none_type,
16822                                 /*is_template=*/false,
16823                                 /*is_namespace=*/false,
16824                                 /*check_dependency=*/true,
16825                                 /*ambiguous_decls=*/NULL,
16826                                 location);
16827 }
16828
16829 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16830    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16831    true, the DECL indicates the class being defined in a class-head,
16832    or declared in an elaborated-type-specifier.
16833
16834    Otherwise, return DECL.  */
16835
16836 static tree
16837 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16838 {
16839   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16840      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16841
16842        struct A {
16843          template <typename T> struct B;
16844        };
16845
16846        template <typename T> struct A::B {};
16847
16848      Similarly, in an elaborated-type-specifier:
16849
16850        namespace N { struct X{}; }
16851
16852        struct A {
16853          template <typename T> friend struct N::X;
16854        };
16855
16856      However, if the DECL refers to a class type, and we are in
16857      the scope of the class, then the name lookup automatically
16858      finds the TYPE_DECL created by build_self_reference rather
16859      than a TEMPLATE_DECL.  For example, in:
16860
16861        template <class T> struct S {
16862          S s;
16863        };
16864
16865      there is no need to handle such case.  */
16866
16867   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16868     return DECL_TEMPLATE_RESULT (decl);
16869
16870   return decl;
16871 }
16872
16873 /* If too many, or too few, template-parameter lists apply to the
16874    declarator, issue an error message.  Returns TRUE if all went well,
16875    and FALSE otherwise.  */
16876
16877 static bool
16878 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16879                                                 cp_declarator *declarator,
16880                                                 location_t declarator_location)
16881 {
16882   unsigned num_templates;
16883
16884   /* We haven't seen any classes that involve template parameters yet.  */
16885   num_templates = 0;
16886
16887   switch (declarator->kind)
16888     {
16889     case cdk_id:
16890       if (declarator->u.id.qualifying_scope)
16891         {
16892           tree scope;
16893           tree member;
16894
16895           scope = declarator->u.id.qualifying_scope;
16896           member = declarator->u.id.unqualified_name;
16897
16898           while (scope && CLASS_TYPE_P (scope))
16899             {
16900               /* You're supposed to have one `template <...>'
16901                  for every template class, but you don't need one
16902                  for a full specialization.  For example:
16903
16904                  template <class T> struct S{};
16905                  template <> struct S<int> { void f(); };
16906                  void S<int>::f () {}
16907
16908                  is correct; there shouldn't be a `template <>' for
16909                  the definition of `S<int>::f'.  */
16910               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16911                 /* If SCOPE does not have template information of any
16912                    kind, then it is not a template, nor is it nested
16913                    within a template.  */
16914                 break;
16915               if (explicit_class_specialization_p (scope))
16916                 break;
16917               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16918                 ++num_templates;
16919
16920               scope = TYPE_CONTEXT (scope);
16921             }
16922         }
16923       else if (TREE_CODE (declarator->u.id.unqualified_name)
16924                == TEMPLATE_ID_EXPR)
16925         /* If the DECLARATOR has the form `X<y>' then it uses one
16926            additional level of template parameters.  */
16927         ++num_templates;
16928
16929       return cp_parser_check_template_parameters (parser,
16930                                                   num_templates,
16931                                                   declarator_location);
16932
16933     case cdk_function:
16934     case cdk_array:
16935     case cdk_pointer:
16936     case cdk_reference:
16937     case cdk_ptrmem:
16938       return (cp_parser_check_declarator_template_parameters
16939               (parser, declarator->declarator, declarator_location));
16940
16941     case cdk_error:
16942       return true;
16943
16944     default:
16945       gcc_unreachable ();
16946     }
16947   return false;
16948 }
16949
16950 /* NUM_TEMPLATES were used in the current declaration.  If that is
16951    invalid, return FALSE and issue an error messages.  Otherwise,
16952    return TRUE.  */
16953
16954 static bool
16955 cp_parser_check_template_parameters (cp_parser* parser,
16956                                      unsigned num_templates,
16957                                      location_t location)
16958 {
16959   /* If there are more template classes than parameter lists, we have
16960      something like:
16961
16962        template <class T> void S<T>::R<T>::f ();  */
16963   if (parser->num_template_parameter_lists < num_templates)
16964     {
16965       error ("%Htoo few template-parameter-lists", &location);
16966       return false;
16967     }
16968   /* If there are the same number of template classes and parameter
16969      lists, that's OK.  */
16970   if (parser->num_template_parameter_lists == num_templates)
16971     return true;
16972   /* If there are more, but only one more, then we are referring to a
16973      member template.  That's OK too.  */
16974   if (parser->num_template_parameter_lists == num_templates + 1)
16975       return true;
16976   /* Otherwise, there are too many template parameter lists.  We have
16977      something like:
16978
16979      template <class T> template <class U> void S::f();  */
16980   error ("%Htoo many template-parameter-lists", &location);
16981   return false;
16982 }
16983
16984 /* Parse an optional `::' token indicating that the following name is
16985    from the global namespace.  If so, PARSER->SCOPE is set to the
16986    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16987    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16988    Returns the new value of PARSER->SCOPE, if the `::' token is
16989    present, and NULL_TREE otherwise.  */
16990
16991 static tree
16992 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16993 {
16994   cp_token *token;
16995
16996   /* Peek at the next token.  */
16997   token = cp_lexer_peek_token (parser->lexer);
16998   /* If we're looking at a `::' token then we're starting from the
16999      global namespace, not our current location.  */
17000   if (token->type == CPP_SCOPE)
17001     {
17002       /* Consume the `::' token.  */
17003       cp_lexer_consume_token (parser->lexer);
17004       /* Set the SCOPE so that we know where to start the lookup.  */
17005       parser->scope = global_namespace;
17006       parser->qualifying_scope = global_namespace;
17007       parser->object_scope = NULL_TREE;
17008
17009       return parser->scope;
17010     }
17011   else if (!current_scope_valid_p)
17012     {
17013       parser->scope = NULL_TREE;
17014       parser->qualifying_scope = NULL_TREE;
17015       parser->object_scope = NULL_TREE;
17016     }
17017
17018   return NULL_TREE;
17019 }
17020
17021 /* Returns TRUE if the upcoming token sequence is the start of a
17022    constructor declarator.  If FRIEND_P is true, the declarator is
17023    preceded by the `friend' specifier.  */
17024
17025 static bool
17026 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17027 {
17028   bool constructor_p;
17029   tree type_decl = NULL_TREE;
17030   bool nested_name_p;
17031   cp_token *next_token;
17032
17033   /* The common case is that this is not a constructor declarator, so
17034      try to avoid doing lots of work if at all possible.  It's not
17035      valid declare a constructor at function scope.  */
17036   if (parser->in_function_body)
17037     return false;
17038   /* And only certain tokens can begin a constructor declarator.  */
17039   next_token = cp_lexer_peek_token (parser->lexer);
17040   if (next_token->type != CPP_NAME
17041       && next_token->type != CPP_SCOPE
17042       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17043       && next_token->type != CPP_TEMPLATE_ID)
17044     return false;
17045
17046   /* Parse tentatively; we are going to roll back all of the tokens
17047      consumed here.  */
17048   cp_parser_parse_tentatively (parser);
17049   /* Assume that we are looking at a constructor declarator.  */
17050   constructor_p = true;
17051
17052   /* Look for the optional `::' operator.  */
17053   cp_parser_global_scope_opt (parser,
17054                               /*current_scope_valid_p=*/false);
17055   /* Look for the nested-name-specifier.  */
17056   nested_name_p
17057     = (cp_parser_nested_name_specifier_opt (parser,
17058                                             /*typename_keyword_p=*/false,
17059                                             /*check_dependency_p=*/false,
17060                                             /*type_p=*/false,
17061                                             /*is_declaration=*/false)
17062        != NULL_TREE);
17063   /* Outside of a class-specifier, there must be a
17064      nested-name-specifier.  */
17065   if (!nested_name_p &&
17066       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17067        || friend_p))
17068     constructor_p = false;
17069   /* If we still think that this might be a constructor-declarator,
17070      look for a class-name.  */
17071   if (constructor_p)
17072     {
17073       /* If we have:
17074
17075            template <typename T> struct S { S(); };
17076            template <typename T> S<T>::S ();
17077
17078          we must recognize that the nested `S' names a class.
17079          Similarly, for:
17080
17081            template <typename T> S<T>::S<T> ();
17082
17083          we must recognize that the nested `S' names a template.  */
17084       type_decl = cp_parser_class_name (parser,
17085                                         /*typename_keyword_p=*/false,
17086                                         /*template_keyword_p=*/false,
17087                                         none_type,
17088                                         /*check_dependency_p=*/false,
17089                                         /*class_head_p=*/false,
17090                                         /*is_declaration=*/false);
17091       /* If there was no class-name, then this is not a constructor.  */
17092       constructor_p = !cp_parser_error_occurred (parser);
17093     }
17094
17095   /* If we're still considering a constructor, we have to see a `(',
17096      to begin the parameter-declaration-clause, followed by either a
17097      `)', an `...', or a decl-specifier.  We need to check for a
17098      type-specifier to avoid being fooled into thinking that:
17099
17100        S::S (f) (int);
17101
17102      is a constructor.  (It is actually a function named `f' that
17103      takes one parameter (of type `int') and returns a value of type
17104      `S::S'.  */
17105   if (constructor_p
17106       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17107     {
17108       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17109           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17110           /* A parameter declaration begins with a decl-specifier,
17111              which is either the "attribute" keyword, a storage class
17112              specifier, or (usually) a type-specifier.  */
17113           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17114         {
17115           tree type;
17116           tree pushed_scope = NULL_TREE;
17117           unsigned saved_num_template_parameter_lists;
17118
17119           /* Names appearing in the type-specifier should be looked up
17120              in the scope of the class.  */
17121           if (current_class_type)
17122             type = NULL_TREE;
17123           else
17124             {
17125               type = TREE_TYPE (type_decl);
17126               if (TREE_CODE (type) == TYPENAME_TYPE)
17127                 {
17128                   type = resolve_typename_type (type,
17129                                                 /*only_current_p=*/false);
17130                   if (TREE_CODE (type) == TYPENAME_TYPE)
17131                     {
17132                       cp_parser_abort_tentative_parse (parser);
17133                       return false;
17134                     }
17135                 }
17136               pushed_scope = push_scope (type);
17137             }
17138
17139           /* Inside the constructor parameter list, surrounding
17140              template-parameter-lists do not apply.  */
17141           saved_num_template_parameter_lists
17142             = parser->num_template_parameter_lists;
17143           parser->num_template_parameter_lists = 0;
17144
17145           /* Look for the type-specifier.  */
17146           cp_parser_type_specifier (parser,
17147                                     CP_PARSER_FLAGS_NONE,
17148                                     /*decl_specs=*/NULL,
17149                                     /*is_declarator=*/true,
17150                                     /*declares_class_or_enum=*/NULL,
17151                                     /*is_cv_qualifier=*/NULL);
17152
17153           parser->num_template_parameter_lists
17154             = saved_num_template_parameter_lists;
17155
17156           /* Leave the scope of the class.  */
17157           if (pushed_scope)
17158             pop_scope (pushed_scope);
17159
17160           constructor_p = !cp_parser_error_occurred (parser);
17161         }
17162     }
17163   else
17164     constructor_p = false;
17165   /* We did not really want to consume any tokens.  */
17166   cp_parser_abort_tentative_parse (parser);
17167
17168   return constructor_p;
17169 }
17170
17171 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17172    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17173    they must be performed once we are in the scope of the function.
17174
17175    Returns the function defined.  */
17176
17177 static tree
17178 cp_parser_function_definition_from_specifiers_and_declarator
17179   (cp_parser* parser,
17180    cp_decl_specifier_seq *decl_specifiers,
17181    tree attributes,
17182    const cp_declarator *declarator)
17183 {
17184   tree fn;
17185   bool success_p;
17186
17187   /* Begin the function-definition.  */
17188   success_p = start_function (decl_specifiers, declarator, attributes);
17189
17190   /* The things we're about to see are not directly qualified by any
17191      template headers we've seen thus far.  */
17192   reset_specialization ();
17193
17194   /* If there were names looked up in the decl-specifier-seq that we
17195      did not check, check them now.  We must wait until we are in the
17196      scope of the function to perform the checks, since the function
17197      might be a friend.  */
17198   perform_deferred_access_checks ();
17199
17200   if (!success_p)
17201     {
17202       /* Skip the entire function.  */
17203       cp_parser_skip_to_end_of_block_or_statement (parser);
17204       fn = error_mark_node;
17205     }
17206   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17207     {
17208       /* Seen already, skip it.  An error message has already been output.  */
17209       cp_parser_skip_to_end_of_block_or_statement (parser);
17210       fn = current_function_decl;
17211       current_function_decl = NULL_TREE;
17212       /* If this is a function from a class, pop the nested class.  */
17213       if (current_class_name)
17214         pop_nested_class ();
17215     }
17216   else
17217     fn = cp_parser_function_definition_after_declarator (parser,
17218                                                          /*inline_p=*/false);
17219
17220   return fn;
17221 }
17222
17223 /* Parse the part of a function-definition that follows the
17224    declarator.  INLINE_P is TRUE iff this function is an inline
17225    function defined with a class-specifier.
17226
17227    Returns the function defined.  */
17228
17229 static tree
17230 cp_parser_function_definition_after_declarator (cp_parser* parser,
17231                                                 bool inline_p)
17232 {
17233   tree fn;
17234   bool ctor_initializer_p = false;
17235   bool saved_in_unbraced_linkage_specification_p;
17236   bool saved_in_function_body;
17237   unsigned saved_num_template_parameter_lists;
17238   cp_token *token;
17239
17240   saved_in_function_body = parser->in_function_body;
17241   parser->in_function_body = true;
17242   /* If the next token is `return', then the code may be trying to
17243      make use of the "named return value" extension that G++ used to
17244      support.  */
17245   token = cp_lexer_peek_token (parser->lexer);
17246   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17247     {
17248       /* Consume the `return' keyword.  */
17249       cp_lexer_consume_token (parser->lexer);
17250       /* Look for the identifier that indicates what value is to be
17251          returned.  */
17252       cp_parser_identifier (parser);
17253       /* Issue an error message.  */
17254       error ("%Hnamed return values are no longer supported",
17255              &token->location);
17256       /* Skip tokens until we reach the start of the function body.  */
17257       while (true)
17258         {
17259           cp_token *token = cp_lexer_peek_token (parser->lexer);
17260           if (token->type == CPP_OPEN_BRACE
17261               || token->type == CPP_EOF
17262               || token->type == CPP_PRAGMA_EOL)
17263             break;
17264           cp_lexer_consume_token (parser->lexer);
17265         }
17266     }
17267   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17268      anything declared inside `f'.  */
17269   saved_in_unbraced_linkage_specification_p
17270     = parser->in_unbraced_linkage_specification_p;
17271   parser->in_unbraced_linkage_specification_p = false;
17272   /* Inside the function, surrounding template-parameter-lists do not
17273      apply.  */
17274   saved_num_template_parameter_lists
17275     = parser->num_template_parameter_lists;
17276   parser->num_template_parameter_lists = 0;
17277   /* If the next token is `try', then we are looking at a
17278      function-try-block.  */
17279   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17280     ctor_initializer_p = cp_parser_function_try_block (parser);
17281   /* A function-try-block includes the function-body, so we only do
17282      this next part if we're not processing a function-try-block.  */
17283   else
17284     ctor_initializer_p
17285       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17286
17287   /* Finish the function.  */
17288   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17289                         (inline_p ? 2 : 0));
17290   /* Generate code for it, if necessary.  */
17291   expand_or_defer_fn (fn);
17292   /* Restore the saved values.  */
17293   parser->in_unbraced_linkage_specification_p
17294     = saved_in_unbraced_linkage_specification_p;
17295   parser->num_template_parameter_lists
17296     = saved_num_template_parameter_lists;
17297   parser->in_function_body = saved_in_function_body;
17298
17299   return fn;
17300 }
17301
17302 /* Parse a template-declaration, assuming that the `export' (and
17303    `extern') keywords, if present, has already been scanned.  MEMBER_P
17304    is as for cp_parser_template_declaration.  */
17305
17306 static void
17307 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17308 {
17309   tree decl = NULL_TREE;
17310   VEC (deferred_access_check,gc) *checks;
17311   tree parameter_list;
17312   bool friend_p = false;
17313   bool need_lang_pop;
17314   cp_token *token;
17315
17316   /* Look for the `template' keyword.  */
17317   token = cp_lexer_peek_token (parser->lexer);
17318   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17319     return;
17320
17321   /* And the `<'.  */
17322   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17323     return;
17324   if (at_class_scope_p () && current_function_decl)
17325     {
17326       /* 14.5.2.2 [temp.mem]
17327
17328          A local class shall not have member templates.  */
17329       error ("%Hinvalid declaration of member template in local class",
17330              &token->location);
17331       cp_parser_skip_to_end_of_block_or_statement (parser);
17332       return;
17333     }
17334   /* [temp]
17335
17336      A template ... shall not have C linkage.  */
17337   if (current_lang_name == lang_name_c)
17338     {
17339       error ("%Htemplate with C linkage", &token->location);
17340       /* Give it C++ linkage to avoid confusing other parts of the
17341          front end.  */
17342       push_lang_context (lang_name_cplusplus);
17343       need_lang_pop = true;
17344     }
17345   else
17346     need_lang_pop = false;
17347
17348   /* We cannot perform access checks on the template parameter
17349      declarations until we know what is being declared, just as we
17350      cannot check the decl-specifier list.  */
17351   push_deferring_access_checks (dk_deferred);
17352
17353   /* If the next token is `>', then we have an invalid
17354      specialization.  Rather than complain about an invalid template
17355      parameter, issue an error message here.  */
17356   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17357     {
17358       cp_parser_error (parser, "invalid explicit specialization");
17359       begin_specialization ();
17360       parameter_list = NULL_TREE;
17361     }
17362   else
17363     /* Parse the template parameters.  */
17364     parameter_list = cp_parser_template_parameter_list (parser);
17365
17366   /* Get the deferred access checks from the parameter list.  These
17367      will be checked once we know what is being declared, as for a
17368      member template the checks must be performed in the scope of the
17369      class containing the member.  */
17370   checks = get_deferred_access_checks ();
17371
17372   /* Look for the `>'.  */
17373   cp_parser_skip_to_end_of_template_parameter_list (parser);
17374   /* We just processed one more parameter list.  */
17375   ++parser->num_template_parameter_lists;
17376   /* If the next token is `template', there are more template
17377      parameters.  */
17378   if (cp_lexer_next_token_is_keyword (parser->lexer,
17379                                       RID_TEMPLATE))
17380     cp_parser_template_declaration_after_export (parser, member_p);
17381   else
17382     {
17383       /* There are no access checks when parsing a template, as we do not
17384          know if a specialization will be a friend.  */
17385       push_deferring_access_checks (dk_no_check);
17386       token = cp_lexer_peek_token (parser->lexer);
17387       decl = cp_parser_single_declaration (parser,
17388                                            checks,
17389                                            member_p,
17390                                            /*explicit_specialization_p=*/false,
17391                                            &friend_p);
17392       pop_deferring_access_checks ();
17393
17394       /* If this is a member template declaration, let the front
17395          end know.  */
17396       if (member_p && !friend_p && decl)
17397         {
17398           if (TREE_CODE (decl) == TYPE_DECL)
17399             cp_parser_check_access_in_redeclaration (decl, token->location);
17400
17401           decl = finish_member_template_decl (decl);
17402         }
17403       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17404         make_friend_class (current_class_type, TREE_TYPE (decl),
17405                            /*complain=*/true);
17406     }
17407   /* We are done with the current parameter list.  */
17408   --parser->num_template_parameter_lists;
17409
17410   pop_deferring_access_checks ();
17411
17412   /* Finish up.  */
17413   finish_template_decl (parameter_list);
17414
17415   /* Register member declarations.  */
17416   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17417     finish_member_declaration (decl);
17418   /* For the erroneous case of a template with C linkage, we pushed an
17419      implicit C++ linkage scope; exit that scope now.  */
17420   if (need_lang_pop)
17421     pop_lang_context ();
17422   /* If DECL is a function template, we must return to parse it later.
17423      (Even though there is no definition, there might be default
17424      arguments that need handling.)  */
17425   if (member_p && decl
17426       && (TREE_CODE (decl) == FUNCTION_DECL
17427           || DECL_FUNCTION_TEMPLATE_P (decl)))
17428     TREE_VALUE (parser->unparsed_functions_queues)
17429       = tree_cons (NULL_TREE, decl,
17430                    TREE_VALUE (parser->unparsed_functions_queues));
17431 }
17432
17433 /* Perform the deferred access checks from a template-parameter-list.
17434    CHECKS is a TREE_LIST of access checks, as returned by
17435    get_deferred_access_checks.  */
17436
17437 static void
17438 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17439 {
17440   ++processing_template_parmlist;
17441   perform_access_checks (checks);
17442   --processing_template_parmlist;
17443 }
17444
17445 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17446    `function-definition' sequence.  MEMBER_P is true, this declaration
17447    appears in a class scope.
17448
17449    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17450    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17451
17452 static tree
17453 cp_parser_single_declaration (cp_parser* parser,
17454                               VEC (deferred_access_check,gc)* checks,
17455                               bool member_p,
17456                               bool explicit_specialization_p,
17457                               bool* friend_p)
17458 {
17459   int declares_class_or_enum;
17460   tree decl = NULL_TREE;
17461   cp_decl_specifier_seq decl_specifiers;
17462   bool function_definition_p = false;
17463   cp_token *decl_spec_token_start;
17464
17465   /* This function is only used when processing a template
17466      declaration.  */
17467   gcc_assert (innermost_scope_kind () == sk_template_parms
17468               || innermost_scope_kind () == sk_template_spec);
17469
17470   /* Defer access checks until we know what is being declared.  */
17471   push_deferring_access_checks (dk_deferred);
17472
17473   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17474      alternative.  */
17475   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17476   cp_parser_decl_specifier_seq (parser,
17477                                 CP_PARSER_FLAGS_OPTIONAL,
17478                                 &decl_specifiers,
17479                                 &declares_class_or_enum);
17480   if (friend_p)
17481     *friend_p = cp_parser_friend_p (&decl_specifiers);
17482
17483   /* There are no template typedefs.  */
17484   if (decl_specifiers.specs[(int) ds_typedef])
17485     {
17486       error ("%Htemplate declaration of %qs",
17487              &decl_spec_token_start->location, "typedef");
17488       decl = error_mark_node;
17489     }
17490
17491   /* Gather up the access checks that occurred the
17492      decl-specifier-seq.  */
17493   stop_deferring_access_checks ();
17494
17495   /* Check for the declaration of a template class.  */
17496   if (declares_class_or_enum)
17497     {
17498       if (cp_parser_declares_only_class_p (parser))
17499         {
17500           decl = shadow_tag (&decl_specifiers);
17501
17502           /* In this case:
17503
17504                struct C {
17505                  friend template <typename T> struct A<T>::B;
17506                };
17507
17508              A<T>::B will be represented by a TYPENAME_TYPE, and
17509              therefore not recognized by shadow_tag.  */
17510           if (friend_p && *friend_p
17511               && !decl
17512               && decl_specifiers.type
17513               && TYPE_P (decl_specifiers.type))
17514             decl = decl_specifiers.type;
17515
17516           if (decl && decl != error_mark_node)
17517             decl = TYPE_NAME (decl);
17518           else
17519             decl = error_mark_node;
17520
17521           /* Perform access checks for template parameters.  */
17522           cp_parser_perform_template_parameter_access_checks (checks);
17523         }
17524     }
17525   /* If it's not a template class, try for a template function.  If
17526      the next token is a `;', then this declaration does not declare
17527      anything.  But, if there were errors in the decl-specifiers, then
17528      the error might well have come from an attempted class-specifier.
17529      In that case, there's no need to warn about a missing declarator.  */
17530   if (!decl
17531       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17532           || decl_specifiers.type != error_mark_node))
17533     {
17534       decl = cp_parser_init_declarator (parser,
17535                                         &decl_specifiers,
17536                                         checks,
17537                                         /*function_definition_allowed_p=*/true,
17538                                         member_p,
17539                                         declares_class_or_enum,
17540                                         &function_definition_p);
17541
17542     /* 7.1.1-1 [dcl.stc]
17543
17544        A storage-class-specifier shall not be specified in an explicit
17545        specialization...  */
17546     if (decl
17547         && explicit_specialization_p
17548         && decl_specifiers.storage_class != sc_none)
17549       {
17550         error ("%Hexplicit template specialization cannot have a storage class",
17551                &decl_spec_token_start->location);
17552         decl = error_mark_node;
17553       }
17554     }
17555
17556   pop_deferring_access_checks ();
17557
17558   /* Clear any current qualification; whatever comes next is the start
17559      of something new.  */
17560   parser->scope = NULL_TREE;
17561   parser->qualifying_scope = NULL_TREE;
17562   parser->object_scope = NULL_TREE;
17563   /* Look for a trailing `;' after the declaration.  */
17564   if (!function_definition_p
17565       && (decl == error_mark_node
17566           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17567     cp_parser_skip_to_end_of_block_or_statement (parser);
17568
17569   return decl;
17570 }
17571
17572 /* Parse a cast-expression that is not the operand of a unary "&".  */
17573
17574 static tree
17575 cp_parser_simple_cast_expression (cp_parser *parser)
17576 {
17577   return cp_parser_cast_expression (parser, /*address_p=*/false,
17578                                     /*cast_p=*/false);
17579 }
17580
17581 /* Parse a functional cast to TYPE.  Returns an expression
17582    representing the cast.  */
17583
17584 static tree
17585 cp_parser_functional_cast (cp_parser* parser, tree type)
17586 {
17587   tree expression_list;
17588   tree cast;
17589   bool nonconst_p;
17590
17591   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17592     {
17593       maybe_warn_cpp0x ("extended initializer lists");
17594       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17595       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17596       if (TREE_CODE (type) == TYPE_DECL)
17597         type = TREE_TYPE (type);
17598       return finish_compound_literal (type, expression_list);
17599     }
17600
17601   expression_list
17602     = cp_parser_parenthesized_expression_list (parser, false,
17603                                                /*cast_p=*/true,
17604                                                /*allow_expansion_p=*/true,
17605                                                /*non_constant_p=*/NULL);
17606
17607   cast = build_functional_cast (type, expression_list,
17608                                 tf_warning_or_error);
17609   /* [expr.const]/1: In an integral constant expression "only type
17610      conversions to integral or enumeration type can be used".  */
17611   if (TREE_CODE (type) == TYPE_DECL)
17612     type = TREE_TYPE (type);
17613   if (cast != error_mark_node
17614       && !cast_valid_in_integral_constant_expression_p (type)
17615       && (cp_parser_non_integral_constant_expression
17616           (parser, "a call to a constructor")))
17617     return error_mark_node;
17618   return cast;
17619 }
17620
17621 /* Save the tokens that make up the body of a member function defined
17622    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17623    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17624    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17625    for the member function.  */
17626
17627 static tree
17628 cp_parser_save_member_function_body (cp_parser* parser,
17629                                      cp_decl_specifier_seq *decl_specifiers,
17630                                      cp_declarator *declarator,
17631                                      tree attributes)
17632 {
17633   cp_token *first;
17634   cp_token *last;
17635   tree fn;
17636
17637   /* Create the function-declaration.  */
17638   fn = start_method (decl_specifiers, declarator, attributes);
17639   /* If something went badly wrong, bail out now.  */
17640   if (fn == error_mark_node)
17641     {
17642       /* If there's a function-body, skip it.  */
17643       if (cp_parser_token_starts_function_definition_p
17644           (cp_lexer_peek_token (parser->lexer)))
17645         cp_parser_skip_to_end_of_block_or_statement (parser);
17646       return error_mark_node;
17647     }
17648
17649   /* Remember it, if there default args to post process.  */
17650   cp_parser_save_default_args (parser, fn);
17651
17652   /* Save away the tokens that make up the body of the
17653      function.  */
17654   first = parser->lexer->next_token;
17655   /* We can have braced-init-list mem-initializers before the fn body.  */
17656   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17657     {
17658       cp_lexer_consume_token (parser->lexer);
17659       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17660              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17661         {
17662           /* cache_group will stop after an un-nested { } pair, too.  */
17663           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17664             break;
17665
17666           /* variadic mem-inits have ... after the ')'.  */
17667           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17668             cp_lexer_consume_token (parser->lexer);
17669         }
17670     }
17671   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17672   /* Handle function try blocks.  */
17673   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17674     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17675   last = parser->lexer->next_token;
17676
17677   /* Save away the inline definition; we will process it when the
17678      class is complete.  */
17679   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17680   DECL_PENDING_INLINE_P (fn) = 1;
17681
17682   /* We need to know that this was defined in the class, so that
17683      friend templates are handled correctly.  */
17684   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17685
17686   /* We're done with the inline definition.  */
17687   finish_method (fn);
17688
17689   /* Add FN to the queue of functions to be parsed later.  */
17690   TREE_VALUE (parser->unparsed_functions_queues)
17691     = tree_cons (NULL_TREE, fn,
17692                  TREE_VALUE (parser->unparsed_functions_queues));
17693
17694   return fn;
17695 }
17696
17697 /* Parse a template-argument-list, as well as the trailing ">" (but
17698    not the opening ">").  See cp_parser_template_argument_list for the
17699    return value.  */
17700
17701 static tree
17702 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17703 {
17704   tree arguments;
17705   tree saved_scope;
17706   tree saved_qualifying_scope;
17707   tree saved_object_scope;
17708   bool saved_greater_than_is_operator_p;
17709   bool saved_skip_evaluation;
17710
17711   /* [temp.names]
17712
17713      When parsing a template-id, the first non-nested `>' is taken as
17714      the end of the template-argument-list rather than a greater-than
17715      operator.  */
17716   saved_greater_than_is_operator_p
17717     = parser->greater_than_is_operator_p;
17718   parser->greater_than_is_operator_p = false;
17719   /* Parsing the argument list may modify SCOPE, so we save it
17720      here.  */
17721   saved_scope = parser->scope;
17722   saved_qualifying_scope = parser->qualifying_scope;
17723   saved_object_scope = parser->object_scope;
17724   /* We need to evaluate the template arguments, even though this
17725      template-id may be nested within a "sizeof".  */
17726   saved_skip_evaluation = skip_evaluation;
17727   skip_evaluation = false;
17728   /* Parse the template-argument-list itself.  */
17729   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17730       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17731     arguments = NULL_TREE;
17732   else
17733     arguments = cp_parser_template_argument_list (parser);
17734   /* Look for the `>' that ends the template-argument-list. If we find
17735      a '>>' instead, it's probably just a typo.  */
17736   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17737     {
17738       if (cxx_dialect != cxx98)
17739         {
17740           /* In C++0x, a `>>' in a template argument list or cast
17741              expression is considered to be two separate `>'
17742              tokens. So, change the current token to a `>', but don't
17743              consume it: it will be consumed later when the outer
17744              template argument list (or cast expression) is parsed.
17745              Note that this replacement of `>' for `>>' is necessary
17746              even if we are parsing tentatively: in the tentative
17747              case, after calling
17748              cp_parser_enclosed_template_argument_list we will always
17749              throw away all of the template arguments and the first
17750              closing `>', either because the template argument list
17751              was erroneous or because we are replacing those tokens
17752              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17753              not have been thrown away) is needed either to close an
17754              outer template argument list or to complete a new-style
17755              cast.  */
17756           cp_token *token = cp_lexer_peek_token (parser->lexer);
17757           token->type = CPP_GREATER;
17758         }
17759       else if (!saved_greater_than_is_operator_p)
17760         {
17761           /* If we're in a nested template argument list, the '>>' has
17762             to be a typo for '> >'. We emit the error message, but we
17763             continue parsing and we push a '>' as next token, so that
17764             the argument list will be parsed correctly.  Note that the
17765             global source location is still on the token before the
17766             '>>', so we need to say explicitly where we want it.  */
17767           cp_token *token = cp_lexer_peek_token (parser->lexer);
17768           error ("%H%<>>%> should be %<> >%> "
17769                  "within a nested template argument list",
17770                  &token->location);
17771
17772           token->type = CPP_GREATER;
17773         }
17774       else
17775         {
17776           /* If this is not a nested template argument list, the '>>'
17777             is a typo for '>'. Emit an error message and continue.
17778             Same deal about the token location, but here we can get it
17779             right by consuming the '>>' before issuing the diagnostic.  */
17780           cp_token *token = cp_lexer_consume_token (parser->lexer);
17781           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17782                  "a template argument list", &token->location);
17783         }
17784     }
17785   else
17786     cp_parser_skip_to_end_of_template_parameter_list (parser);
17787   /* The `>' token might be a greater-than operator again now.  */
17788   parser->greater_than_is_operator_p
17789     = saved_greater_than_is_operator_p;
17790   /* Restore the SAVED_SCOPE.  */
17791   parser->scope = saved_scope;
17792   parser->qualifying_scope = saved_qualifying_scope;
17793   parser->object_scope = saved_object_scope;
17794   skip_evaluation = saved_skip_evaluation;
17795
17796   return arguments;
17797 }
17798
17799 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17800    arguments, or the body of the function have not yet been parsed,
17801    parse them now.  */
17802
17803 static void
17804 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17805 {
17806   /* If this member is a template, get the underlying
17807      FUNCTION_DECL.  */
17808   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17809     member_function = DECL_TEMPLATE_RESULT (member_function);
17810
17811   /* There should not be any class definitions in progress at this
17812      point; the bodies of members are only parsed outside of all class
17813      definitions.  */
17814   gcc_assert (parser->num_classes_being_defined == 0);
17815   /* While we're parsing the member functions we might encounter more
17816      classes.  We want to handle them right away, but we don't want
17817      them getting mixed up with functions that are currently in the
17818      queue.  */
17819   parser->unparsed_functions_queues
17820     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17821
17822   /* Make sure that any template parameters are in scope.  */
17823   maybe_begin_member_template_processing (member_function);
17824
17825   /* If the body of the function has not yet been parsed, parse it
17826      now.  */
17827   if (DECL_PENDING_INLINE_P (member_function))
17828     {
17829       tree function_scope;
17830       cp_token_cache *tokens;
17831
17832       /* The function is no longer pending; we are processing it.  */
17833       tokens = DECL_PENDING_INLINE_INFO (member_function);
17834       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17835       DECL_PENDING_INLINE_P (member_function) = 0;
17836
17837       /* If this is a local class, enter the scope of the containing
17838          function.  */
17839       function_scope = current_function_decl;
17840       if (function_scope)
17841         push_function_context ();
17842
17843       /* Push the body of the function onto the lexer stack.  */
17844       cp_parser_push_lexer_for_tokens (parser, tokens);
17845
17846       /* Let the front end know that we going to be defining this
17847          function.  */
17848       start_preparsed_function (member_function, NULL_TREE,
17849                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17850
17851       /* Don't do access checking if it is a templated function.  */
17852       if (processing_template_decl)
17853         push_deferring_access_checks (dk_no_check);
17854
17855       /* Now, parse the body of the function.  */
17856       cp_parser_function_definition_after_declarator (parser,
17857                                                       /*inline_p=*/true);
17858
17859       if (processing_template_decl)
17860         pop_deferring_access_checks ();
17861
17862       /* Leave the scope of the containing function.  */
17863       if (function_scope)
17864         pop_function_context ();
17865       cp_parser_pop_lexer (parser);
17866     }
17867
17868   /* Remove any template parameters from the symbol table.  */
17869   maybe_end_member_template_processing ();
17870
17871   /* Restore the queue.  */
17872   parser->unparsed_functions_queues
17873     = TREE_CHAIN (parser->unparsed_functions_queues);
17874 }
17875
17876 /* If DECL contains any default args, remember it on the unparsed
17877    functions queue.  */
17878
17879 static void
17880 cp_parser_save_default_args (cp_parser* parser, tree decl)
17881 {
17882   tree probe;
17883
17884   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17885        probe;
17886        probe = TREE_CHAIN (probe))
17887     if (TREE_PURPOSE (probe))
17888       {
17889         TREE_PURPOSE (parser->unparsed_functions_queues)
17890           = tree_cons (current_class_type, decl,
17891                        TREE_PURPOSE (parser->unparsed_functions_queues));
17892         break;
17893       }
17894 }
17895
17896 /* FN is a FUNCTION_DECL which may contains a parameter with an
17897    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17898    assumes that the current scope is the scope in which the default
17899    argument should be processed.  */
17900
17901 static void
17902 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17903 {
17904   bool saved_local_variables_forbidden_p;
17905   tree parm;
17906
17907   /* While we're parsing the default args, we might (due to the
17908      statement expression extension) encounter more classes.  We want
17909      to handle them right away, but we don't want them getting mixed
17910      up with default args that are currently in the queue.  */
17911   parser->unparsed_functions_queues
17912     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17913
17914   /* Local variable names (and the `this' keyword) may not appear
17915      in a default argument.  */
17916   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17917   parser->local_variables_forbidden_p = true;
17918
17919   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17920        parm;
17921        parm = TREE_CHAIN (parm))
17922     {
17923       cp_token_cache *tokens;
17924       tree default_arg = TREE_PURPOSE (parm);
17925       tree parsed_arg;
17926       VEC(tree,gc) *insts;
17927       tree copy;
17928       unsigned ix;
17929
17930       if (!default_arg)
17931         continue;
17932
17933       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17934         /* This can happen for a friend declaration for a function
17935            already declared with default arguments.  */
17936         continue;
17937
17938        /* Push the saved tokens for the default argument onto the parser's
17939           lexer stack.  */
17940       tokens = DEFARG_TOKENS (default_arg);
17941       cp_parser_push_lexer_for_tokens (parser, tokens);
17942
17943       /* Parse the assignment-expression.  */
17944       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17945
17946       if (!processing_template_decl)
17947         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17948
17949       TREE_PURPOSE (parm) = parsed_arg;
17950
17951       /* Update any instantiations we've already created.  */
17952       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17953            VEC_iterate (tree, insts, ix, copy); ix++)
17954         TREE_PURPOSE (copy) = parsed_arg;
17955
17956       /* If the token stream has not been completely used up, then
17957          there was extra junk after the end of the default
17958          argument.  */
17959       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17960         cp_parser_error (parser, "expected %<,%>");
17961
17962       /* Revert to the main lexer.  */
17963       cp_parser_pop_lexer (parser);
17964     }
17965
17966   /* Make sure no default arg is missing.  */
17967   check_default_args (fn);
17968
17969   /* Restore the state of local_variables_forbidden_p.  */
17970   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17971
17972   /* Restore the queue.  */
17973   parser->unparsed_functions_queues
17974     = TREE_CHAIN (parser->unparsed_functions_queues);
17975 }
17976
17977 /* Parse the operand of `sizeof' (or a similar operator).  Returns
17978    either a TYPE or an expression, depending on the form of the
17979    input.  The KEYWORD indicates which kind of expression we have
17980    encountered.  */
17981
17982 static tree
17983 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17984 {
17985   tree expr = NULL_TREE;
17986   const char *saved_message;
17987   char *tmp;
17988   bool saved_integral_constant_expression_p;
17989   bool saved_non_integral_constant_expression_p;
17990   bool pack_expansion_p = false;
17991
17992   /* Types cannot be defined in a `sizeof' expression.  Save away the
17993      old message.  */
17994   saved_message = parser->type_definition_forbidden_message;
17995   /* And create the new one.  */
17996   tmp = concat ("types may not be defined in %<",
17997                 IDENTIFIER_POINTER (ridpointers[keyword]),
17998                 "%> expressions", NULL);
17999   parser->type_definition_forbidden_message = tmp;
18000
18001   /* The restrictions on constant-expressions do not apply inside
18002      sizeof expressions.  */
18003   saved_integral_constant_expression_p
18004     = parser->integral_constant_expression_p;
18005   saved_non_integral_constant_expression_p
18006     = parser->non_integral_constant_expression_p;
18007   parser->integral_constant_expression_p = false;
18008
18009   /* If it's a `...', then we are computing the length of a parameter
18010      pack.  */
18011   if (keyword == RID_SIZEOF
18012       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18013     {
18014       /* Consume the `...'.  */
18015       cp_lexer_consume_token (parser->lexer);
18016       maybe_warn_variadic_templates ();
18017
18018       /* Note that this is an expansion.  */
18019       pack_expansion_p = true;
18020     }
18021
18022   /* Do not actually evaluate the expression.  */
18023   ++skip_evaluation;
18024   /* If it's a `(', then we might be looking at the type-id
18025      construction.  */
18026   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18027     {
18028       tree type;
18029       bool saved_in_type_id_in_expr_p;
18030
18031       /* We can't be sure yet whether we're looking at a type-id or an
18032          expression.  */
18033       cp_parser_parse_tentatively (parser);
18034       /* Consume the `('.  */
18035       cp_lexer_consume_token (parser->lexer);
18036       /* Parse the type-id.  */
18037       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18038       parser->in_type_id_in_expr_p = true;
18039       type = cp_parser_type_id (parser);
18040       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18041       /* Now, look for the trailing `)'.  */
18042       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18043       /* If all went well, then we're done.  */
18044       if (cp_parser_parse_definitely (parser))
18045         {
18046           cp_decl_specifier_seq decl_specs;
18047
18048           /* Build a trivial decl-specifier-seq.  */
18049           clear_decl_specs (&decl_specs);
18050           decl_specs.type = type;
18051
18052           /* Call grokdeclarator to figure out what type this is.  */
18053           expr = grokdeclarator (NULL,
18054                                  &decl_specs,
18055                                  TYPENAME,
18056                                  /*initialized=*/0,
18057                                  /*attrlist=*/NULL);
18058         }
18059     }
18060
18061   /* If the type-id production did not work out, then we must be
18062      looking at the unary-expression production.  */
18063   if (!expr)
18064     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18065                                        /*cast_p=*/false);
18066
18067   if (pack_expansion_p)
18068     /* Build a pack expansion. */
18069     expr = make_pack_expansion (expr);
18070
18071   /* Go back to evaluating expressions.  */
18072   --skip_evaluation;
18073
18074   /* Free the message we created.  */
18075   free (tmp);
18076   /* And restore the old one.  */
18077   parser->type_definition_forbidden_message = saved_message;
18078   parser->integral_constant_expression_p
18079     = saved_integral_constant_expression_p;
18080   parser->non_integral_constant_expression_p
18081     = saved_non_integral_constant_expression_p;
18082
18083   return expr;
18084 }
18085
18086 /* If the current declaration has no declarator, return true.  */
18087
18088 static bool
18089 cp_parser_declares_only_class_p (cp_parser *parser)
18090 {
18091   /* If the next token is a `;' or a `,' then there is no
18092      declarator.  */
18093   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18094           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18095 }
18096
18097 /* Update the DECL_SPECS to reflect the storage class indicated by
18098    KEYWORD.  */
18099
18100 static void
18101 cp_parser_set_storage_class (cp_parser *parser,
18102                              cp_decl_specifier_seq *decl_specs,
18103                              enum rid keyword,
18104                              location_t location)
18105 {
18106   cp_storage_class storage_class;
18107
18108   if (parser->in_unbraced_linkage_specification_p)
18109     {
18110       error ("%Hinvalid use of %qD in linkage specification",
18111              &location, ridpointers[keyword]);
18112       return;
18113     }
18114   else if (decl_specs->storage_class != sc_none)
18115     {
18116       decl_specs->conflicting_specifiers_p = true;
18117       return;
18118     }
18119
18120   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18121       && decl_specs->specs[(int) ds_thread])
18122     {
18123       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18124       decl_specs->specs[(int) ds_thread] = 0;
18125     }
18126
18127   switch (keyword)
18128     {
18129     case RID_AUTO:
18130       storage_class = sc_auto;
18131       break;
18132     case RID_REGISTER:
18133       storage_class = sc_register;
18134       break;
18135     case RID_STATIC:
18136       storage_class = sc_static;
18137       break;
18138     case RID_EXTERN:
18139       storage_class = sc_extern;
18140       break;
18141     case RID_MUTABLE:
18142       storage_class = sc_mutable;
18143       break;
18144     default:
18145       gcc_unreachable ();
18146     }
18147   decl_specs->storage_class = storage_class;
18148
18149   /* A storage class specifier cannot be applied alongside a typedef 
18150      specifier. If there is a typedef specifier present then set 
18151      conflicting_specifiers_p which will trigger an error later
18152      on in grokdeclarator. */
18153   if (decl_specs->specs[(int)ds_typedef])
18154     decl_specs->conflicting_specifiers_p = true;
18155 }
18156
18157 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18158    is true, the type is a user-defined type; otherwise it is a
18159    built-in type specified by a keyword.  */
18160
18161 static void
18162 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18163                               tree type_spec,
18164                               location_t location,
18165                               bool user_defined_p)
18166 {
18167   decl_specs->any_specifiers_p = true;
18168
18169   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18170      (with, for example, in "typedef int wchar_t;") we remember that
18171      this is what happened.  In system headers, we ignore these
18172      declarations so that G++ can work with system headers that are not
18173      C++-safe.  */
18174   if (decl_specs->specs[(int) ds_typedef]
18175       && !user_defined_p
18176       && (type_spec == boolean_type_node
18177           || type_spec == char16_type_node
18178           || type_spec == char32_type_node
18179           || type_spec == wchar_type_node)
18180       && (decl_specs->type
18181           || decl_specs->specs[(int) ds_long]
18182           || decl_specs->specs[(int) ds_short]
18183           || decl_specs->specs[(int) ds_unsigned]
18184           || decl_specs->specs[(int) ds_signed]))
18185     {
18186       decl_specs->redefined_builtin_type = type_spec;
18187       if (!decl_specs->type)
18188         {
18189           decl_specs->type = type_spec;
18190           decl_specs->user_defined_type_p = false;
18191           decl_specs->type_location = location;
18192         }
18193     }
18194   else if (decl_specs->type)
18195     decl_specs->multiple_types_p = true;
18196   else
18197     {
18198       decl_specs->type = type_spec;
18199       decl_specs->user_defined_type_p = user_defined_p;
18200       decl_specs->redefined_builtin_type = NULL_TREE;
18201       decl_specs->type_location = location;
18202     }
18203 }
18204
18205 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18206    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18207
18208 static bool
18209 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18210 {
18211   return decl_specifiers->specs[(int) ds_friend] != 0;
18212 }
18213
18214 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18215    issue an error message indicating that TOKEN_DESC was expected.
18216
18217    Returns the token consumed, if the token had the appropriate type.
18218    Otherwise, returns NULL.  */
18219
18220 static cp_token *
18221 cp_parser_require (cp_parser* parser,
18222                    enum cpp_ttype type,
18223                    const char* token_desc)
18224 {
18225   if (cp_lexer_next_token_is (parser->lexer, type))
18226     return cp_lexer_consume_token (parser->lexer);
18227   else
18228     {
18229       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18230       if (!cp_parser_simulate_error (parser))
18231         {
18232           char *message = concat ("expected ", token_desc, NULL);
18233           cp_parser_error (parser, message);
18234           free (message);
18235         }
18236       return NULL;
18237     }
18238 }
18239
18240 /* An error message is produced if the next token is not '>'.
18241    All further tokens are skipped until the desired token is
18242    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18243
18244 static void
18245 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18246 {
18247   /* Current level of '< ... >'.  */
18248   unsigned level = 0;
18249   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18250   unsigned nesting_depth = 0;
18251
18252   /* Are we ready, yet?  If not, issue error message.  */
18253   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18254     return;
18255
18256   /* Skip tokens until the desired token is found.  */
18257   while (true)
18258     {
18259       /* Peek at the next token.  */
18260       switch (cp_lexer_peek_token (parser->lexer)->type)
18261         {
18262         case CPP_LESS:
18263           if (!nesting_depth)
18264             ++level;
18265           break;
18266
18267         case CPP_RSHIFT:
18268           if (cxx_dialect == cxx98)
18269             /* C++0x views the `>>' operator as two `>' tokens, but
18270                C++98 does not. */
18271             break;
18272           else if (!nesting_depth && level-- == 0)
18273             {
18274               /* We've hit a `>>' where the first `>' closes the
18275                  template argument list, and the second `>' is
18276                  spurious.  Just consume the `>>' and stop; we've
18277                  already produced at least one error.  */
18278               cp_lexer_consume_token (parser->lexer);
18279               return;
18280             }
18281           /* Fall through for C++0x, so we handle the second `>' in
18282              the `>>'.  */
18283
18284         case CPP_GREATER:
18285           if (!nesting_depth && level-- == 0)
18286             {
18287               /* We've reached the token we want, consume it and stop.  */
18288               cp_lexer_consume_token (parser->lexer);
18289               return;
18290             }
18291           break;
18292
18293         case CPP_OPEN_PAREN:
18294         case CPP_OPEN_SQUARE:
18295           ++nesting_depth;
18296           break;
18297
18298         case CPP_CLOSE_PAREN:
18299         case CPP_CLOSE_SQUARE:
18300           if (nesting_depth-- == 0)
18301             return;
18302           break;
18303
18304         case CPP_EOF:
18305         case CPP_PRAGMA_EOL:
18306         case CPP_SEMICOLON:
18307         case CPP_OPEN_BRACE:
18308         case CPP_CLOSE_BRACE:
18309           /* The '>' was probably forgotten, don't look further.  */
18310           return;
18311
18312         default:
18313           break;
18314         }
18315
18316       /* Consume this token.  */
18317       cp_lexer_consume_token (parser->lexer);
18318     }
18319 }
18320
18321 /* If the next token is the indicated keyword, consume it.  Otherwise,
18322    issue an error message indicating that TOKEN_DESC was expected.
18323
18324    Returns the token consumed, if the token had the appropriate type.
18325    Otherwise, returns NULL.  */
18326
18327 static cp_token *
18328 cp_parser_require_keyword (cp_parser* parser,
18329                            enum rid keyword,
18330                            const char* token_desc)
18331 {
18332   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18333
18334   if (token && token->keyword != keyword)
18335     {
18336       dyn_string_t error_msg;
18337
18338       /* Format the error message.  */
18339       error_msg = dyn_string_new (0);
18340       dyn_string_append_cstr (error_msg, "expected ");
18341       dyn_string_append_cstr (error_msg, token_desc);
18342       cp_parser_error (parser, error_msg->s);
18343       dyn_string_delete (error_msg);
18344       return NULL;
18345     }
18346
18347   return token;
18348 }
18349
18350 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18351    function-definition.  */
18352
18353 static bool
18354 cp_parser_token_starts_function_definition_p (cp_token* token)
18355 {
18356   return (/* An ordinary function-body begins with an `{'.  */
18357           token->type == CPP_OPEN_BRACE
18358           /* A ctor-initializer begins with a `:'.  */
18359           || token->type == CPP_COLON
18360           /* A function-try-block begins with `try'.  */
18361           || token->keyword == RID_TRY
18362           /* The named return value extension begins with `return'.  */
18363           || token->keyword == RID_RETURN);
18364 }
18365
18366 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18367    definition.  */
18368
18369 static bool
18370 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18371 {
18372   cp_token *token;
18373
18374   token = cp_lexer_peek_token (parser->lexer);
18375   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18376 }
18377
18378 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18379    C++0x) ending a template-argument.  */
18380
18381 static bool
18382 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18383 {
18384   cp_token *token;
18385
18386   token = cp_lexer_peek_token (parser->lexer);
18387   return (token->type == CPP_COMMA 
18388           || token->type == CPP_GREATER
18389           || token->type == CPP_ELLIPSIS
18390           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18391 }
18392
18393 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18394    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18395
18396 static bool
18397 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18398                                                      size_t n)
18399 {
18400   cp_token *token;
18401
18402   token = cp_lexer_peek_nth_token (parser->lexer, n);
18403   if (token->type == CPP_LESS)
18404     return true;
18405   /* Check for the sequence `<::' in the original code. It would be lexed as
18406      `[:', where `[' is a digraph, and there is no whitespace before
18407      `:'.  */
18408   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18409     {
18410       cp_token *token2;
18411       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18412       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18413         return true;
18414     }
18415   return false;
18416 }
18417
18418 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18419    or none_type otherwise.  */
18420
18421 static enum tag_types
18422 cp_parser_token_is_class_key (cp_token* token)
18423 {
18424   switch (token->keyword)
18425     {
18426     case RID_CLASS:
18427       return class_type;
18428     case RID_STRUCT:
18429       return record_type;
18430     case RID_UNION:
18431       return union_type;
18432
18433     default:
18434       return none_type;
18435     }
18436 }
18437
18438 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18439
18440 static void
18441 cp_parser_check_class_key (enum tag_types class_key, tree type)
18442 {
18443   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18444     permerror ("%qs tag used in naming %q#T",
18445             class_key == union_type ? "union"
18446              : class_key == record_type ? "struct" : "class",
18447              type);
18448 }
18449
18450 /* Issue an error message if DECL is redeclared with different
18451    access than its original declaration [class.access.spec/3].
18452    This applies to nested classes and nested class templates.
18453    [class.mem/1].  */
18454
18455 static void
18456 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18457 {
18458   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18459     return;
18460
18461   if ((TREE_PRIVATE (decl)
18462        != (current_access_specifier == access_private_node))
18463       || (TREE_PROTECTED (decl)
18464           != (current_access_specifier == access_protected_node)))
18465     error ("%H%qD redeclared with different access", &location, decl);
18466 }
18467
18468 /* Look for the `template' keyword, as a syntactic disambiguator.
18469    Return TRUE iff it is present, in which case it will be
18470    consumed.  */
18471
18472 static bool
18473 cp_parser_optional_template_keyword (cp_parser *parser)
18474 {
18475   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18476     {
18477       /* The `template' keyword can only be used within templates;
18478          outside templates the parser can always figure out what is a
18479          template and what is not.  */
18480       if (!processing_template_decl)
18481         {
18482           cp_token *token = cp_lexer_peek_token (parser->lexer);
18483           error ("%H%<template%> (as a disambiguator) is only allowed "
18484                  "within templates", &token->location);
18485           /* If this part of the token stream is rescanned, the same
18486              error message would be generated.  So, we purge the token
18487              from the stream.  */
18488           cp_lexer_purge_token (parser->lexer);
18489           return false;
18490         }
18491       else
18492         {
18493           /* Consume the `template' keyword.  */
18494           cp_lexer_consume_token (parser->lexer);
18495           return true;
18496         }
18497     }
18498
18499   return false;
18500 }
18501
18502 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18503    set PARSER->SCOPE, and perform other related actions.  */
18504
18505 static void
18506 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18507 {
18508   int i;
18509   struct tree_check *check_value;
18510   deferred_access_check *chk;
18511   VEC (deferred_access_check,gc) *checks;
18512
18513   /* Get the stored value.  */
18514   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18515   /* Perform any access checks that were deferred.  */
18516   checks = check_value->checks;
18517   if (checks)
18518     {
18519       for (i = 0 ;
18520            VEC_iterate (deferred_access_check, checks, i, chk) ;
18521            ++i)
18522         {
18523           perform_or_defer_access_check (chk->binfo,
18524                                          chk->decl,
18525                                          chk->diag_decl);
18526         }
18527     }
18528   /* Set the scope from the stored value.  */
18529   parser->scope = check_value->value;
18530   parser->qualifying_scope = check_value->qualifying_scope;
18531   parser->object_scope = NULL_TREE;
18532 }
18533
18534 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18535    encounter the end of a block before what we were looking for.  */
18536
18537 static bool
18538 cp_parser_cache_group (cp_parser *parser,
18539                        enum cpp_ttype end,
18540                        unsigned depth)
18541 {
18542   while (true)
18543     {
18544       cp_token *token = cp_lexer_peek_token (parser->lexer);
18545
18546       /* Abort a parenthesized expression if we encounter a semicolon.  */
18547       if ((end == CPP_CLOSE_PAREN || depth == 0)
18548           && token->type == CPP_SEMICOLON)
18549         return true;
18550       /* If we've reached the end of the file, stop.  */
18551       if (token->type == CPP_EOF
18552           || (end != CPP_PRAGMA_EOL
18553               && token->type == CPP_PRAGMA_EOL))
18554         return true;
18555       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18556         /* We've hit the end of an enclosing block, so there's been some
18557            kind of syntax error.  */
18558         return true;
18559
18560       /* Consume the token.  */
18561       cp_lexer_consume_token (parser->lexer);
18562       /* See if it starts a new group.  */
18563       if (token->type == CPP_OPEN_BRACE)
18564         {
18565           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18566           /* In theory this should probably check end == '}', but
18567              cp_parser_save_member_function_body needs it to exit
18568              after either '}' or ')' when called with ')'.  */
18569           if (depth == 0)
18570             return false;
18571         }
18572       else if (token->type == CPP_OPEN_PAREN)
18573         {
18574           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18575           if (depth == 0 && end == CPP_CLOSE_PAREN)
18576             return false;
18577         }
18578       else if (token->type == CPP_PRAGMA)
18579         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18580       else if (token->type == end)
18581         return false;
18582     }
18583 }
18584
18585 /* Begin parsing tentatively.  We always save tokens while parsing
18586    tentatively so that if the tentative parsing fails we can restore the
18587    tokens.  */
18588
18589 static void
18590 cp_parser_parse_tentatively (cp_parser* parser)
18591 {
18592   /* Enter a new parsing context.  */
18593   parser->context = cp_parser_context_new (parser->context);
18594   /* Begin saving tokens.  */
18595   cp_lexer_save_tokens (parser->lexer);
18596   /* In order to avoid repetitive access control error messages,
18597      access checks are queued up until we are no longer parsing
18598      tentatively.  */
18599   push_deferring_access_checks (dk_deferred);
18600 }
18601
18602 /* Commit to the currently active tentative parse.  */
18603
18604 static void
18605 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18606 {
18607   cp_parser_context *context;
18608   cp_lexer *lexer;
18609
18610   /* Mark all of the levels as committed.  */
18611   lexer = parser->lexer;
18612   for (context = parser->context; context->next; context = context->next)
18613     {
18614       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18615         break;
18616       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18617       while (!cp_lexer_saving_tokens (lexer))
18618         lexer = lexer->next;
18619       cp_lexer_commit_tokens (lexer);
18620     }
18621 }
18622
18623 /* Abort the currently active tentative parse.  All consumed tokens
18624    will be rolled back, and no diagnostics will be issued.  */
18625
18626 static void
18627 cp_parser_abort_tentative_parse (cp_parser* parser)
18628 {
18629   cp_parser_simulate_error (parser);
18630   /* Now, pretend that we want to see if the construct was
18631      successfully parsed.  */
18632   cp_parser_parse_definitely (parser);
18633 }
18634
18635 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18636    token stream.  Otherwise, commit to the tokens we have consumed.
18637    Returns true if no error occurred; false otherwise.  */
18638
18639 static bool
18640 cp_parser_parse_definitely (cp_parser* parser)
18641 {
18642   bool error_occurred;
18643   cp_parser_context *context;
18644
18645   /* Remember whether or not an error occurred, since we are about to
18646      destroy that information.  */
18647   error_occurred = cp_parser_error_occurred (parser);
18648   /* Remove the topmost context from the stack.  */
18649   context = parser->context;
18650   parser->context = context->next;
18651   /* If no parse errors occurred, commit to the tentative parse.  */
18652   if (!error_occurred)
18653     {
18654       /* Commit to the tokens read tentatively, unless that was
18655          already done.  */
18656       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18657         cp_lexer_commit_tokens (parser->lexer);
18658
18659       pop_to_parent_deferring_access_checks ();
18660     }
18661   /* Otherwise, if errors occurred, roll back our state so that things
18662      are just as they were before we began the tentative parse.  */
18663   else
18664     {
18665       cp_lexer_rollback_tokens (parser->lexer);
18666       pop_deferring_access_checks ();
18667     }
18668   /* Add the context to the front of the free list.  */
18669   context->next = cp_parser_context_free_list;
18670   cp_parser_context_free_list = context;
18671
18672   return !error_occurred;
18673 }
18674
18675 /* Returns true if we are parsing tentatively and are not committed to
18676    this tentative parse.  */
18677
18678 static bool
18679 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18680 {
18681   return (cp_parser_parsing_tentatively (parser)
18682           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18683 }
18684
18685 /* Returns nonzero iff an error has occurred during the most recent
18686    tentative parse.  */
18687
18688 static bool
18689 cp_parser_error_occurred (cp_parser* parser)
18690 {
18691   return (cp_parser_parsing_tentatively (parser)
18692           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18693 }
18694
18695 /* Returns nonzero if GNU extensions are allowed.  */
18696
18697 static bool
18698 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18699 {
18700   return parser->allow_gnu_extensions_p;
18701 }
18702 \f
18703 /* Objective-C++ Productions */
18704
18705
18706 /* Parse an Objective-C expression, which feeds into a primary-expression
18707    above.
18708
18709    objc-expression:
18710      objc-message-expression
18711      objc-string-literal
18712      objc-encode-expression
18713      objc-protocol-expression
18714      objc-selector-expression
18715
18716   Returns a tree representation of the expression.  */
18717
18718 static tree
18719 cp_parser_objc_expression (cp_parser* parser)
18720 {
18721   /* Try to figure out what kind of declaration is present.  */
18722   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18723
18724   switch (kwd->type)
18725     {
18726     case CPP_OPEN_SQUARE:
18727       return cp_parser_objc_message_expression (parser);
18728
18729     case CPP_OBJC_STRING:
18730       kwd = cp_lexer_consume_token (parser->lexer);
18731       return objc_build_string_object (kwd->u.value);
18732
18733     case CPP_KEYWORD:
18734       switch (kwd->keyword)
18735         {
18736         case RID_AT_ENCODE:
18737           return cp_parser_objc_encode_expression (parser);
18738
18739         case RID_AT_PROTOCOL:
18740           return cp_parser_objc_protocol_expression (parser);
18741
18742         case RID_AT_SELECTOR:
18743           return cp_parser_objc_selector_expression (parser);
18744
18745         default:
18746           break;
18747         }
18748     default:
18749       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18750              &kwd->location, kwd->u.value);
18751       cp_parser_skip_to_end_of_block_or_statement (parser);
18752     }
18753
18754   return error_mark_node;
18755 }
18756
18757 /* Parse an Objective-C message expression.
18758
18759    objc-message-expression:
18760      [ objc-message-receiver objc-message-args ]
18761
18762    Returns a representation of an Objective-C message.  */
18763
18764 static tree
18765 cp_parser_objc_message_expression (cp_parser* parser)
18766 {
18767   tree receiver, messageargs;
18768
18769   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18770   receiver = cp_parser_objc_message_receiver (parser);
18771   messageargs = cp_parser_objc_message_args (parser);
18772   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18773
18774   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18775 }
18776
18777 /* Parse an objc-message-receiver.
18778
18779    objc-message-receiver:
18780      expression
18781      simple-type-specifier
18782
18783   Returns a representation of the type or expression.  */
18784
18785 static tree
18786 cp_parser_objc_message_receiver (cp_parser* parser)
18787 {
18788   tree rcv;
18789
18790   /* An Objective-C message receiver may be either (1) a type
18791      or (2) an expression.  */
18792   cp_parser_parse_tentatively (parser);
18793   rcv = cp_parser_expression (parser, false);
18794
18795   if (cp_parser_parse_definitely (parser))
18796     return rcv;
18797
18798   rcv = cp_parser_simple_type_specifier (parser,
18799                                          /*decl_specs=*/NULL,
18800                                          CP_PARSER_FLAGS_NONE);
18801
18802   return objc_get_class_reference (rcv);
18803 }
18804
18805 /* Parse the arguments and selectors comprising an Objective-C message.
18806
18807    objc-message-args:
18808      objc-selector
18809      objc-selector-args
18810      objc-selector-args , objc-comma-args
18811
18812    objc-selector-args:
18813      objc-selector [opt] : assignment-expression
18814      objc-selector-args objc-selector [opt] : assignment-expression
18815
18816    objc-comma-args:
18817      assignment-expression
18818      objc-comma-args , assignment-expression
18819
18820    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18821    selector arguments and TREE_VALUE containing a list of comma
18822    arguments.  */
18823
18824 static tree
18825 cp_parser_objc_message_args (cp_parser* parser)
18826 {
18827   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18828   bool maybe_unary_selector_p = true;
18829   cp_token *token = cp_lexer_peek_token (parser->lexer);
18830
18831   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18832     {
18833       tree selector = NULL_TREE, arg;
18834
18835       if (token->type != CPP_COLON)
18836         selector = cp_parser_objc_selector (parser);
18837
18838       /* Detect if we have a unary selector.  */
18839       if (maybe_unary_selector_p
18840           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18841         return build_tree_list (selector, NULL_TREE);
18842
18843       maybe_unary_selector_p = false;
18844       cp_parser_require (parser, CPP_COLON, "%<:%>");
18845       arg = cp_parser_assignment_expression (parser, false);
18846
18847       sel_args
18848         = chainon (sel_args,
18849                    build_tree_list (selector, arg));
18850
18851       token = cp_lexer_peek_token (parser->lexer);
18852     }
18853
18854   /* Handle non-selector arguments, if any. */
18855   while (token->type == CPP_COMMA)
18856     {
18857       tree arg;
18858
18859       cp_lexer_consume_token (parser->lexer);
18860       arg = cp_parser_assignment_expression (parser, false);
18861
18862       addl_args
18863         = chainon (addl_args,
18864                    build_tree_list (NULL_TREE, arg));
18865
18866       token = cp_lexer_peek_token (parser->lexer);
18867     }
18868
18869   return build_tree_list (sel_args, addl_args);
18870 }
18871
18872 /* Parse an Objective-C encode expression.
18873
18874    objc-encode-expression:
18875      @encode objc-typename
18876
18877    Returns an encoded representation of the type argument.  */
18878
18879 static tree
18880 cp_parser_objc_encode_expression (cp_parser* parser)
18881 {
18882   tree type;
18883   cp_token *token;
18884
18885   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18886   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18887   token = cp_lexer_peek_token (parser->lexer);
18888   type = complete_type (cp_parser_type_id (parser));
18889   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18890
18891   if (!type)
18892     {
18893       error ("%H%<@encode%> must specify a type as an argument",
18894              &token->location);
18895       return error_mark_node;
18896     }
18897
18898   return objc_build_encode_expr (type);
18899 }
18900
18901 /* Parse an Objective-C @defs expression.  */
18902
18903 static tree
18904 cp_parser_objc_defs_expression (cp_parser *parser)
18905 {
18906   tree name;
18907
18908   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18909   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18910   name = cp_parser_identifier (parser);
18911   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18912
18913   return objc_get_class_ivars (name);
18914 }
18915
18916 /* Parse an Objective-C protocol expression.
18917
18918   objc-protocol-expression:
18919     @protocol ( identifier )
18920
18921   Returns a representation of the protocol expression.  */
18922
18923 static tree
18924 cp_parser_objc_protocol_expression (cp_parser* parser)
18925 {
18926   tree proto;
18927
18928   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18929   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18930   proto = cp_parser_identifier (parser);
18931   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18932
18933   return objc_build_protocol_expr (proto);
18934 }
18935
18936 /* Parse an Objective-C selector expression.
18937
18938    objc-selector-expression:
18939      @selector ( objc-method-signature )
18940
18941    objc-method-signature:
18942      objc-selector
18943      objc-selector-seq
18944
18945    objc-selector-seq:
18946      objc-selector :
18947      objc-selector-seq objc-selector :
18948
18949   Returns a representation of the method selector.  */
18950
18951 static tree
18952 cp_parser_objc_selector_expression (cp_parser* parser)
18953 {
18954   tree sel_seq = NULL_TREE;
18955   bool maybe_unary_selector_p = true;
18956   cp_token *token;
18957
18958   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18959   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18960   token = cp_lexer_peek_token (parser->lexer);
18961
18962   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18963          || token->type == CPP_SCOPE)
18964     {
18965       tree selector = NULL_TREE;
18966
18967       if (token->type != CPP_COLON
18968           || token->type == CPP_SCOPE)
18969         selector = cp_parser_objc_selector (parser);
18970
18971       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18972           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18973         {
18974           /* Detect if we have a unary selector.  */
18975           if (maybe_unary_selector_p)
18976             {
18977               sel_seq = selector;
18978               goto finish_selector;
18979             }
18980           else
18981             {
18982               cp_parser_error (parser, "expected %<:%>");
18983             }
18984         }
18985       maybe_unary_selector_p = false;
18986       token = cp_lexer_consume_token (parser->lexer);
18987
18988       if (token->type == CPP_SCOPE)
18989         {
18990           sel_seq
18991             = chainon (sel_seq,
18992                        build_tree_list (selector, NULL_TREE));
18993           sel_seq
18994             = chainon (sel_seq,
18995                        build_tree_list (NULL_TREE, NULL_TREE));
18996         }
18997       else
18998         sel_seq
18999           = chainon (sel_seq,
19000                      build_tree_list (selector, NULL_TREE));
19001
19002       token = cp_lexer_peek_token (parser->lexer);
19003     }
19004
19005  finish_selector:
19006   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19007
19008   return objc_build_selector_expr (sel_seq);
19009 }
19010
19011 /* Parse a list of identifiers.
19012
19013    objc-identifier-list:
19014      identifier
19015      objc-identifier-list , identifier
19016
19017    Returns a TREE_LIST of identifier nodes.  */
19018
19019 static tree
19020 cp_parser_objc_identifier_list (cp_parser* parser)
19021 {
19022   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19023   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19024
19025   while (sep->type == CPP_COMMA)
19026     {
19027       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19028       list = chainon (list,
19029                       build_tree_list (NULL_TREE,
19030                                        cp_parser_identifier (parser)));
19031       sep = cp_lexer_peek_token (parser->lexer);
19032     }
19033
19034   return list;
19035 }
19036
19037 /* Parse an Objective-C alias declaration.
19038
19039    objc-alias-declaration:
19040      @compatibility_alias identifier identifier ;
19041
19042    This function registers the alias mapping with the Objective-C front end.
19043    It returns nothing.  */
19044
19045 static void
19046 cp_parser_objc_alias_declaration (cp_parser* parser)
19047 {
19048   tree alias, orig;
19049
19050   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19051   alias = cp_parser_identifier (parser);
19052   orig = cp_parser_identifier (parser);
19053   objc_declare_alias (alias, orig);
19054   cp_parser_consume_semicolon_at_end_of_statement (parser);
19055 }
19056
19057 /* Parse an Objective-C class forward-declaration.
19058
19059    objc-class-declaration:
19060      @class objc-identifier-list ;
19061
19062    The function registers the forward declarations with the Objective-C
19063    front end.  It returns nothing.  */
19064
19065 static void
19066 cp_parser_objc_class_declaration (cp_parser* parser)
19067 {
19068   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19069   objc_declare_class (cp_parser_objc_identifier_list (parser));
19070   cp_parser_consume_semicolon_at_end_of_statement (parser);
19071 }
19072
19073 /* Parse a list of Objective-C protocol references.
19074
19075    objc-protocol-refs-opt:
19076      objc-protocol-refs [opt]
19077
19078    objc-protocol-refs:
19079      < objc-identifier-list >
19080
19081    Returns a TREE_LIST of identifiers, if any.  */
19082
19083 static tree
19084 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19085 {
19086   tree protorefs = NULL_TREE;
19087
19088   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19089     {
19090       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19091       protorefs = cp_parser_objc_identifier_list (parser);
19092       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19093     }
19094
19095   return protorefs;
19096 }
19097
19098 /* Parse a Objective-C visibility specification.  */
19099
19100 static void
19101 cp_parser_objc_visibility_spec (cp_parser* parser)
19102 {
19103   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19104
19105   switch (vis->keyword)
19106     {
19107     case RID_AT_PRIVATE:
19108       objc_set_visibility (2);
19109       break;
19110     case RID_AT_PROTECTED:
19111       objc_set_visibility (0);
19112       break;
19113     case RID_AT_PUBLIC:
19114       objc_set_visibility (1);
19115       break;
19116     default:
19117       return;
19118     }
19119
19120   /* Eat '@private'/'@protected'/'@public'.  */
19121   cp_lexer_consume_token (parser->lexer);
19122 }
19123
19124 /* Parse an Objective-C method type.  */
19125
19126 static void
19127 cp_parser_objc_method_type (cp_parser* parser)
19128 {
19129   objc_set_method_type
19130    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19131     ? PLUS_EXPR
19132     : MINUS_EXPR);
19133 }
19134
19135 /* Parse an Objective-C protocol qualifier.  */
19136
19137 static tree
19138 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19139 {
19140   tree quals = NULL_TREE, node;
19141   cp_token *token = cp_lexer_peek_token (parser->lexer);
19142
19143   node = token->u.value;
19144
19145   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19146          && (node == ridpointers [(int) RID_IN]
19147              || node == ridpointers [(int) RID_OUT]
19148              || node == ridpointers [(int) RID_INOUT]
19149              || node == ridpointers [(int) RID_BYCOPY]
19150              || node == ridpointers [(int) RID_BYREF]
19151              || node == ridpointers [(int) RID_ONEWAY]))
19152     {
19153       quals = tree_cons (NULL_TREE, node, quals);
19154       cp_lexer_consume_token (parser->lexer);
19155       token = cp_lexer_peek_token (parser->lexer);
19156       node = token->u.value;
19157     }
19158
19159   return quals;
19160 }
19161
19162 /* Parse an Objective-C typename.  */
19163
19164 static tree
19165 cp_parser_objc_typename (cp_parser* parser)
19166 {
19167   tree type_name = NULL_TREE;
19168
19169   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19170     {
19171       tree proto_quals, cp_type = NULL_TREE;
19172
19173       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19174       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19175
19176       /* An ObjC type name may consist of just protocol qualifiers, in which
19177          case the type shall default to 'id'.  */
19178       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19179         cp_type = cp_parser_type_id (parser);
19180
19181       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19182       type_name = build_tree_list (proto_quals, cp_type);
19183     }
19184
19185   return type_name;
19186 }
19187
19188 /* Check to see if TYPE refers to an Objective-C selector name.  */
19189
19190 static bool
19191 cp_parser_objc_selector_p (enum cpp_ttype type)
19192 {
19193   return (type == CPP_NAME || type == CPP_KEYWORD
19194           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19195           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19196           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19197           || type == CPP_XOR || type == CPP_XOR_EQ);
19198 }
19199
19200 /* Parse an Objective-C selector.  */
19201
19202 static tree
19203 cp_parser_objc_selector (cp_parser* parser)
19204 {
19205   cp_token *token = cp_lexer_consume_token (parser->lexer);
19206
19207   if (!cp_parser_objc_selector_p (token->type))
19208     {
19209       error ("%Hinvalid Objective-C++ selector name", &token->location);
19210       return error_mark_node;
19211     }
19212
19213   /* C++ operator names are allowed to appear in ObjC selectors.  */
19214   switch (token->type)
19215     {
19216     case CPP_AND_AND: return get_identifier ("and");
19217     case CPP_AND_EQ: return get_identifier ("and_eq");
19218     case CPP_AND: return get_identifier ("bitand");
19219     case CPP_OR: return get_identifier ("bitor");
19220     case CPP_COMPL: return get_identifier ("compl");
19221     case CPP_NOT: return get_identifier ("not");
19222     case CPP_NOT_EQ: return get_identifier ("not_eq");
19223     case CPP_OR_OR: return get_identifier ("or");
19224     case CPP_OR_EQ: return get_identifier ("or_eq");
19225     case CPP_XOR: return get_identifier ("xor");
19226     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19227     default: return token->u.value;
19228     }
19229 }
19230
19231 /* Parse an Objective-C params list.  */
19232
19233 static tree
19234 cp_parser_objc_method_keyword_params (cp_parser* parser)
19235 {
19236   tree params = NULL_TREE;
19237   bool maybe_unary_selector_p = true;
19238   cp_token *token = cp_lexer_peek_token (parser->lexer);
19239
19240   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19241     {
19242       tree selector = NULL_TREE, type_name, identifier;
19243
19244       if (token->type != CPP_COLON)
19245         selector = cp_parser_objc_selector (parser);
19246
19247       /* Detect if we have a unary selector.  */
19248       if (maybe_unary_selector_p
19249           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19250         return selector;
19251
19252       maybe_unary_selector_p = false;
19253       cp_parser_require (parser, CPP_COLON, "%<:%>");
19254       type_name = cp_parser_objc_typename (parser);
19255       identifier = cp_parser_identifier (parser);
19256
19257       params
19258         = chainon (params,
19259                    objc_build_keyword_decl (selector,
19260                                             type_name,
19261                                             identifier));
19262
19263       token = cp_lexer_peek_token (parser->lexer);
19264     }
19265
19266   return params;
19267 }
19268
19269 /* Parse the non-keyword Objective-C params.  */
19270
19271 static tree
19272 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19273 {
19274   tree params = make_node (TREE_LIST);
19275   cp_token *token = cp_lexer_peek_token (parser->lexer);
19276   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19277
19278   while (token->type == CPP_COMMA)
19279     {
19280       cp_parameter_declarator *parmdecl;
19281       tree parm;
19282
19283       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19284       token = cp_lexer_peek_token (parser->lexer);
19285
19286       if (token->type == CPP_ELLIPSIS)
19287         {
19288           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19289           *ellipsisp = true;
19290           break;
19291         }
19292
19293       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19294       parm = grokdeclarator (parmdecl->declarator,
19295                              &parmdecl->decl_specifiers,
19296                              PARM, /*initialized=*/0,
19297                              /*attrlist=*/NULL);
19298
19299       chainon (params, build_tree_list (NULL_TREE, parm));
19300       token = cp_lexer_peek_token (parser->lexer);
19301     }
19302
19303   return params;
19304 }
19305
19306 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19307
19308 static void
19309 cp_parser_objc_interstitial_code (cp_parser* parser)
19310 {
19311   cp_token *token = cp_lexer_peek_token (parser->lexer);
19312
19313   /* If the next token is `extern' and the following token is a string
19314      literal, then we have a linkage specification.  */
19315   if (token->keyword == RID_EXTERN
19316       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19317     cp_parser_linkage_specification (parser);
19318   /* Handle #pragma, if any.  */
19319   else if (token->type == CPP_PRAGMA)
19320     cp_parser_pragma (parser, pragma_external);
19321   /* Allow stray semicolons.  */
19322   else if (token->type == CPP_SEMICOLON)
19323     cp_lexer_consume_token (parser->lexer);
19324   /* Finally, try to parse a block-declaration, or a function-definition.  */
19325   else
19326     cp_parser_block_declaration (parser, /*statement_p=*/false);
19327 }
19328
19329 /* Parse a method signature.  */
19330
19331 static tree
19332 cp_parser_objc_method_signature (cp_parser* parser)
19333 {
19334   tree rettype, kwdparms, optparms;
19335   bool ellipsis = false;
19336
19337   cp_parser_objc_method_type (parser);
19338   rettype = cp_parser_objc_typename (parser);
19339   kwdparms = cp_parser_objc_method_keyword_params (parser);
19340   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19341
19342   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19343 }
19344
19345 /* Pars an Objective-C method prototype list.  */
19346
19347 static void
19348 cp_parser_objc_method_prototype_list (cp_parser* parser)
19349 {
19350   cp_token *token = cp_lexer_peek_token (parser->lexer);
19351
19352   while (token->keyword != RID_AT_END)
19353     {
19354       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19355         {
19356           objc_add_method_declaration
19357            (cp_parser_objc_method_signature (parser));
19358           cp_parser_consume_semicolon_at_end_of_statement (parser);
19359         }
19360       else
19361         /* Allow for interspersed non-ObjC++ code.  */
19362         cp_parser_objc_interstitial_code (parser);
19363
19364       token = cp_lexer_peek_token (parser->lexer);
19365     }
19366
19367   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19368   objc_finish_interface ();
19369 }
19370
19371 /* Parse an Objective-C method definition list.  */
19372
19373 static void
19374 cp_parser_objc_method_definition_list (cp_parser* parser)
19375 {
19376   cp_token *token = cp_lexer_peek_token (parser->lexer);
19377
19378   while (token->keyword != RID_AT_END)
19379     {
19380       tree meth;
19381
19382       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19383         {
19384           push_deferring_access_checks (dk_deferred);
19385           objc_start_method_definition
19386            (cp_parser_objc_method_signature (parser));
19387
19388           /* For historical reasons, we accept an optional semicolon.  */
19389           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19390             cp_lexer_consume_token (parser->lexer);
19391
19392           perform_deferred_access_checks ();
19393           stop_deferring_access_checks ();
19394           meth = cp_parser_function_definition_after_declarator (parser,
19395                                                                  false);
19396           pop_deferring_access_checks ();
19397           objc_finish_method_definition (meth);
19398         }
19399       else
19400         /* Allow for interspersed non-ObjC++ code.  */
19401         cp_parser_objc_interstitial_code (parser);
19402
19403       token = cp_lexer_peek_token (parser->lexer);
19404     }
19405
19406   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19407   objc_finish_implementation ();
19408 }
19409
19410 /* Parse Objective-C ivars.  */
19411
19412 static void
19413 cp_parser_objc_class_ivars (cp_parser* parser)
19414 {
19415   cp_token *token = cp_lexer_peek_token (parser->lexer);
19416
19417   if (token->type != CPP_OPEN_BRACE)
19418     return;     /* No ivars specified.  */
19419
19420   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19421   token = cp_lexer_peek_token (parser->lexer);
19422
19423   while (token->type != CPP_CLOSE_BRACE)
19424     {
19425       cp_decl_specifier_seq declspecs;
19426       int decl_class_or_enum_p;
19427       tree prefix_attributes;
19428
19429       cp_parser_objc_visibility_spec (parser);
19430
19431       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19432         break;
19433
19434       cp_parser_decl_specifier_seq (parser,
19435                                     CP_PARSER_FLAGS_OPTIONAL,
19436                                     &declspecs,
19437                                     &decl_class_or_enum_p);
19438       prefix_attributes = declspecs.attributes;
19439       declspecs.attributes = NULL_TREE;
19440
19441       /* Keep going until we hit the `;' at the end of the
19442          declaration.  */
19443       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19444         {
19445           tree width = NULL_TREE, attributes, first_attribute, decl;
19446           cp_declarator *declarator = NULL;
19447           int ctor_dtor_or_conv_p;
19448
19449           /* Check for a (possibly unnamed) bitfield declaration.  */
19450           token = cp_lexer_peek_token (parser->lexer);
19451           if (token->type == CPP_COLON)
19452             goto eat_colon;
19453
19454           if (token->type == CPP_NAME
19455               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19456                   == CPP_COLON))
19457             {
19458               /* Get the name of the bitfield.  */
19459               declarator = make_id_declarator (NULL_TREE,
19460                                                cp_parser_identifier (parser),
19461                                                sfk_none);
19462
19463              eat_colon:
19464               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19465               /* Get the width of the bitfield.  */
19466               width
19467                 = cp_parser_constant_expression (parser,
19468                                                  /*allow_non_constant=*/false,
19469                                                  NULL);
19470             }
19471           else
19472             {
19473               /* Parse the declarator.  */
19474               declarator
19475                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19476                                         &ctor_dtor_or_conv_p,
19477                                         /*parenthesized_p=*/NULL,
19478                                         /*member_p=*/false);
19479             }
19480
19481           /* Look for attributes that apply to the ivar.  */
19482           attributes = cp_parser_attributes_opt (parser);
19483           /* Remember which attributes are prefix attributes and
19484              which are not.  */
19485           first_attribute = attributes;
19486           /* Combine the attributes.  */
19487           attributes = chainon (prefix_attributes, attributes);
19488
19489           if (width)
19490               /* Create the bitfield declaration.  */
19491               decl = grokbitfield (declarator, &declspecs,
19492                                    width,
19493                                    attributes);
19494           else
19495             decl = grokfield (declarator, &declspecs,
19496                               NULL_TREE, /*init_const_expr_p=*/false,
19497                               NULL_TREE, attributes);
19498
19499           /* Add the instance variable.  */
19500           objc_add_instance_variable (decl);
19501
19502           /* Reset PREFIX_ATTRIBUTES.  */
19503           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19504             attributes = TREE_CHAIN (attributes);
19505           if (attributes)
19506             TREE_CHAIN (attributes) = NULL_TREE;
19507
19508           token = cp_lexer_peek_token (parser->lexer);
19509
19510           if (token->type == CPP_COMMA)
19511             {
19512               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19513               continue;
19514             }
19515           break;
19516         }
19517
19518       cp_parser_consume_semicolon_at_end_of_statement (parser);
19519       token = cp_lexer_peek_token (parser->lexer);
19520     }
19521
19522   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19523   /* For historical reasons, we accept an optional semicolon.  */
19524   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19525     cp_lexer_consume_token (parser->lexer);
19526 }
19527
19528 /* Parse an Objective-C protocol declaration.  */
19529
19530 static void
19531 cp_parser_objc_protocol_declaration (cp_parser* parser)
19532 {
19533   tree proto, protorefs;
19534   cp_token *tok;
19535
19536   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19537   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19538     {
19539       tok = cp_lexer_peek_token (parser->lexer);
19540       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19541       goto finish;
19542     }
19543
19544   /* See if we have a forward declaration or a definition.  */
19545   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19546
19547   /* Try a forward declaration first.  */
19548   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19549     {
19550       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19551      finish:
19552       cp_parser_consume_semicolon_at_end_of_statement (parser);
19553     }
19554
19555   /* Ok, we got a full-fledged definition (or at least should).  */
19556   else
19557     {
19558       proto = cp_parser_identifier (parser);
19559       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19560       objc_start_protocol (proto, protorefs);
19561       cp_parser_objc_method_prototype_list (parser);
19562     }
19563 }
19564
19565 /* Parse an Objective-C superclass or category.  */
19566
19567 static void
19568 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19569                                                           tree *categ)
19570 {
19571   cp_token *next = cp_lexer_peek_token (parser->lexer);
19572
19573   *super = *categ = NULL_TREE;
19574   if (next->type == CPP_COLON)
19575     {
19576       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19577       *super = cp_parser_identifier (parser);
19578     }
19579   else if (next->type == CPP_OPEN_PAREN)
19580     {
19581       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19582       *categ = cp_parser_identifier (parser);
19583       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19584     }
19585 }
19586
19587 /* Parse an Objective-C class interface.  */
19588
19589 static void
19590 cp_parser_objc_class_interface (cp_parser* parser)
19591 {
19592   tree name, super, categ, protos;
19593
19594   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19595   name = cp_parser_identifier (parser);
19596   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19597   protos = cp_parser_objc_protocol_refs_opt (parser);
19598
19599   /* We have either a class or a category on our hands.  */
19600   if (categ)
19601     objc_start_category_interface (name, categ, protos);
19602   else
19603     {
19604       objc_start_class_interface (name, super, protos);
19605       /* Handle instance variable declarations, if any.  */
19606       cp_parser_objc_class_ivars (parser);
19607       objc_continue_interface ();
19608     }
19609
19610   cp_parser_objc_method_prototype_list (parser);
19611 }
19612
19613 /* Parse an Objective-C class implementation.  */
19614
19615 static void
19616 cp_parser_objc_class_implementation (cp_parser* parser)
19617 {
19618   tree name, super, categ;
19619
19620   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19621   name = cp_parser_identifier (parser);
19622   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19623
19624   /* We have either a class or a category on our hands.  */
19625   if (categ)
19626     objc_start_category_implementation (name, categ);
19627   else
19628     {
19629       objc_start_class_implementation (name, super);
19630       /* Handle instance variable declarations, if any.  */
19631       cp_parser_objc_class_ivars (parser);
19632       objc_continue_implementation ();
19633     }
19634
19635   cp_parser_objc_method_definition_list (parser);
19636 }
19637
19638 /* Consume the @end token and finish off the implementation.  */
19639
19640 static void
19641 cp_parser_objc_end_implementation (cp_parser* parser)
19642 {
19643   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19644   objc_finish_implementation ();
19645 }
19646
19647 /* Parse an Objective-C declaration.  */
19648
19649 static void
19650 cp_parser_objc_declaration (cp_parser* parser)
19651 {
19652   /* Try to figure out what kind of declaration is present.  */
19653   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19654
19655   switch (kwd->keyword)
19656     {
19657     case RID_AT_ALIAS:
19658       cp_parser_objc_alias_declaration (parser);
19659       break;
19660     case RID_AT_CLASS:
19661       cp_parser_objc_class_declaration (parser);
19662       break;
19663     case RID_AT_PROTOCOL:
19664       cp_parser_objc_protocol_declaration (parser);
19665       break;
19666     case RID_AT_INTERFACE:
19667       cp_parser_objc_class_interface (parser);
19668       break;
19669     case RID_AT_IMPLEMENTATION:
19670       cp_parser_objc_class_implementation (parser);
19671       break;
19672     case RID_AT_END:
19673       cp_parser_objc_end_implementation (parser);
19674       break;
19675     default:
19676       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19677              &kwd->location, kwd->u.value);
19678       cp_parser_skip_to_end_of_block_or_statement (parser);
19679     }
19680 }
19681
19682 /* Parse an Objective-C try-catch-finally statement.
19683
19684    objc-try-catch-finally-stmt:
19685      @try compound-statement objc-catch-clause-seq [opt]
19686        objc-finally-clause [opt]
19687
19688    objc-catch-clause-seq:
19689      objc-catch-clause objc-catch-clause-seq [opt]
19690
19691    objc-catch-clause:
19692      @catch ( exception-declaration ) compound-statement
19693
19694    objc-finally-clause
19695      @finally compound-statement
19696
19697    Returns NULL_TREE.  */
19698
19699 static tree
19700 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19701   location_t location;
19702   tree stmt;
19703
19704   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19705   location = cp_lexer_peek_token (parser->lexer)->location;
19706   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19707      node, lest it get absorbed into the surrounding block.  */
19708   stmt = push_stmt_list ();
19709   cp_parser_compound_statement (parser, NULL, false);
19710   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19711
19712   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19713     {
19714       cp_parameter_declarator *parmdecl;
19715       tree parm;
19716
19717       cp_lexer_consume_token (parser->lexer);
19718       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19719       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19720       parm = grokdeclarator (parmdecl->declarator,
19721                              &parmdecl->decl_specifiers,
19722                              PARM, /*initialized=*/0,
19723                              /*attrlist=*/NULL);
19724       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19725       objc_begin_catch_clause (parm);
19726       cp_parser_compound_statement (parser, NULL, false);
19727       objc_finish_catch_clause ();
19728     }
19729
19730   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19731     {
19732       cp_lexer_consume_token (parser->lexer);
19733       location = cp_lexer_peek_token (parser->lexer)->location;
19734       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19735          node, lest it get absorbed into the surrounding block.  */
19736       stmt = push_stmt_list ();
19737       cp_parser_compound_statement (parser, NULL, false);
19738       objc_build_finally_clause (location, pop_stmt_list (stmt));
19739     }
19740
19741   return objc_finish_try_stmt ();
19742 }
19743
19744 /* Parse an Objective-C synchronized statement.
19745
19746    objc-synchronized-stmt:
19747      @synchronized ( expression ) compound-statement
19748
19749    Returns NULL_TREE.  */
19750
19751 static tree
19752 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19753   location_t location;
19754   tree lock, stmt;
19755
19756   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19757
19758   location = cp_lexer_peek_token (parser->lexer)->location;
19759   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19760   lock = cp_parser_expression (parser, false);
19761   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19762
19763   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19764      node, lest it get absorbed into the surrounding block.  */
19765   stmt = push_stmt_list ();
19766   cp_parser_compound_statement (parser, NULL, false);
19767
19768   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19769 }
19770
19771 /* Parse an Objective-C throw statement.
19772
19773    objc-throw-stmt:
19774      @throw assignment-expression [opt] ;
19775
19776    Returns a constructed '@throw' statement.  */
19777
19778 static tree
19779 cp_parser_objc_throw_statement (cp_parser *parser) {
19780   tree expr = NULL_TREE;
19781
19782   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19783
19784   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19785     expr = cp_parser_assignment_expression (parser, false);
19786
19787   cp_parser_consume_semicolon_at_end_of_statement (parser);
19788
19789   return objc_build_throw_stmt (expr);
19790 }
19791
19792 /* Parse an Objective-C statement.  */
19793
19794 static tree
19795 cp_parser_objc_statement (cp_parser * parser) {
19796   /* Try to figure out what kind of declaration is present.  */
19797   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19798
19799   switch (kwd->keyword)
19800     {
19801     case RID_AT_TRY:
19802       return cp_parser_objc_try_catch_finally_statement (parser);
19803     case RID_AT_SYNCHRONIZED:
19804       return cp_parser_objc_synchronized_statement (parser);
19805     case RID_AT_THROW:
19806       return cp_parser_objc_throw_statement (parser);
19807     default:
19808       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19809              &kwd->location, kwd->u.value);
19810       cp_parser_skip_to_end_of_block_or_statement (parser);
19811     }
19812
19813   return error_mark_node;
19814 }
19815 \f
19816 /* OpenMP 2.5 parsing routines.  */
19817
19818 /* Returns name of the next clause.
19819    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19820    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19821    returned and the token is consumed.  */
19822
19823 static pragma_omp_clause
19824 cp_parser_omp_clause_name (cp_parser *parser)
19825 {
19826   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19827
19828   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19829     result = PRAGMA_OMP_CLAUSE_IF;
19830   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19831     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19832   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19833     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19834   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19835     {
19836       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19837       const char *p = IDENTIFIER_POINTER (id);
19838
19839       switch (p[0])
19840         {
19841         case 'c':
19842           if (!strcmp ("collapse", p))
19843             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19844           else if (!strcmp ("copyin", p))
19845             result = PRAGMA_OMP_CLAUSE_COPYIN;
19846           else if (!strcmp ("copyprivate", p))
19847             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19848           break;
19849         case 'f':
19850           if (!strcmp ("firstprivate", p))
19851             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19852           break;
19853         case 'l':
19854           if (!strcmp ("lastprivate", p))
19855             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19856           break;
19857         case 'n':
19858           if (!strcmp ("nowait", p))
19859             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19860           else if (!strcmp ("num_threads", p))
19861             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19862           break;
19863         case 'o':
19864           if (!strcmp ("ordered", p))
19865             result = PRAGMA_OMP_CLAUSE_ORDERED;
19866           break;
19867         case 'r':
19868           if (!strcmp ("reduction", p))
19869             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19870           break;
19871         case 's':
19872           if (!strcmp ("schedule", p))
19873             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19874           else if (!strcmp ("shared", p))
19875             result = PRAGMA_OMP_CLAUSE_SHARED;
19876           break;
19877         case 'u':
19878           if (!strcmp ("untied", p))
19879             result = PRAGMA_OMP_CLAUSE_UNTIED;
19880           break;
19881         }
19882     }
19883
19884   if (result != PRAGMA_OMP_CLAUSE_NONE)
19885     cp_lexer_consume_token (parser->lexer);
19886
19887   return result;
19888 }
19889
19890 /* Validate that a clause of the given type does not already exist.  */
19891
19892 static void
19893 check_no_duplicate_clause (tree clauses, enum tree_code code,
19894                            const char *name, location_t location)
19895 {
19896   tree c;
19897
19898   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19899     if (OMP_CLAUSE_CODE (c) == code)
19900       {
19901         error ("%Htoo many %qs clauses", &location, name);
19902         break;
19903       }
19904 }
19905
19906 /* OpenMP 2.5:
19907    variable-list:
19908      identifier
19909      variable-list , identifier
19910
19911    In addition, we match a closing parenthesis.  An opening parenthesis
19912    will have been consumed by the caller.
19913
19914    If KIND is nonzero, create the appropriate node and install the decl
19915    in OMP_CLAUSE_DECL and add the node to the head of the list.
19916
19917    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19918    return the list created.  */
19919
19920 static tree
19921 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19922                                 tree list)
19923 {
19924   cp_token *token;
19925   while (1)
19926     {
19927       tree name, decl;
19928
19929       token = cp_lexer_peek_token (parser->lexer);
19930       name = cp_parser_id_expression (parser, /*template_p=*/false,
19931                                       /*check_dependency_p=*/true,
19932                                       /*template_p=*/NULL,
19933                                       /*declarator_p=*/false,
19934                                       /*optional_p=*/false);
19935       if (name == error_mark_node)
19936         goto skip_comma;
19937
19938       decl = cp_parser_lookup_name_simple (parser, name, token->location);
19939       if (decl == error_mark_node)
19940         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19941       else if (kind != 0)
19942         {
19943           tree u = build_omp_clause (kind);
19944           OMP_CLAUSE_DECL (u) = decl;
19945           OMP_CLAUSE_CHAIN (u) = list;
19946           list = u;
19947         }
19948       else
19949         list = tree_cons (decl, NULL_TREE, list);
19950
19951     get_comma:
19952       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19953         break;
19954       cp_lexer_consume_token (parser->lexer);
19955     }
19956
19957   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19958     {
19959       int ending;
19960
19961       /* Try to resync to an unnested comma.  Copied from
19962          cp_parser_parenthesized_expression_list.  */
19963     skip_comma:
19964       ending = cp_parser_skip_to_closing_parenthesis (parser,
19965                                                       /*recovering=*/true,
19966                                                       /*or_comma=*/true,
19967                                                       /*consume_paren=*/true);
19968       if (ending < 0)
19969         goto get_comma;
19970     }
19971
19972   return list;
19973 }
19974
19975 /* Similarly, but expect leading and trailing parenthesis.  This is a very
19976    common case for omp clauses.  */
19977
19978 static tree
19979 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19980 {
19981   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19982     return cp_parser_omp_var_list_no_open (parser, kind, list);
19983   return list;
19984 }
19985
19986 /* OpenMP 3.0:
19987    collapse ( constant-expression ) */
19988
19989 static tree
19990 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
19991 {
19992   tree c, num;
19993   location_t loc;
19994   HOST_WIDE_INT n;
19995
19996   loc = cp_lexer_peek_token (parser->lexer)->location;
19997   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
19998     return list;
19999
20000   num = cp_parser_constant_expression (parser, false, NULL);
20001
20002   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20003     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20004                                            /*or_comma=*/false,
20005                                            /*consume_paren=*/true);
20006
20007   if (num == error_mark_node)
20008     return list;
20009   num = fold_non_dependent_expr (num);
20010   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20011       || !host_integerp (num, 0)
20012       || (n = tree_low_cst (num, 0)) <= 0
20013       || (int) n != n)
20014     {
20015       error ("%Hcollapse argument needs positive constant integer expression",
20016              &loc);
20017       return list;
20018     }
20019
20020   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20021   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20022   OMP_CLAUSE_CHAIN (c) = list;
20023   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20024
20025   return c;
20026 }
20027
20028 /* OpenMP 2.5:
20029    default ( shared | none ) */
20030
20031 static tree
20032 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20033 {
20034   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20035   tree c;
20036
20037   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20038     return list;
20039   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20040     {
20041       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20042       const char *p = IDENTIFIER_POINTER (id);
20043
20044       switch (p[0])
20045         {
20046         case 'n':
20047           if (strcmp ("none", p) != 0)
20048             goto invalid_kind;
20049           kind = OMP_CLAUSE_DEFAULT_NONE;
20050           break;
20051
20052         case 's':
20053           if (strcmp ("shared", p) != 0)
20054             goto invalid_kind;
20055           kind = OMP_CLAUSE_DEFAULT_SHARED;
20056           break;
20057
20058         default:
20059           goto invalid_kind;
20060         }
20061
20062       cp_lexer_consume_token (parser->lexer);
20063     }
20064   else
20065     {
20066     invalid_kind:
20067       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20068     }
20069
20070   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20071     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20072                                            /*or_comma=*/false,
20073                                            /*consume_paren=*/true);
20074
20075   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20076     return list;
20077
20078   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20079   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20080   OMP_CLAUSE_CHAIN (c) = list;
20081   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20082
20083   return c;
20084 }
20085
20086 /* OpenMP 2.5:
20087    if ( expression ) */
20088
20089 static tree
20090 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20091 {
20092   tree t, c;
20093
20094   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20095     return list;
20096
20097   t = cp_parser_condition (parser);
20098
20099   if (t == error_mark_node
20100       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20101     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20102                                            /*or_comma=*/false,
20103                                            /*consume_paren=*/true);
20104
20105   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20106
20107   c = build_omp_clause (OMP_CLAUSE_IF);
20108   OMP_CLAUSE_IF_EXPR (c) = t;
20109   OMP_CLAUSE_CHAIN (c) = list;
20110
20111   return c;
20112 }
20113
20114 /* OpenMP 2.5:
20115    nowait */
20116
20117 static tree
20118 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20119                              tree list, location_t location)
20120 {
20121   tree c;
20122
20123   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20124
20125   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20126   OMP_CLAUSE_CHAIN (c) = list;
20127   return c;
20128 }
20129
20130 /* OpenMP 2.5:
20131    num_threads ( expression ) */
20132
20133 static tree
20134 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20135                                   location_t location)
20136 {
20137   tree t, c;
20138
20139   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20140     return list;
20141
20142   t = cp_parser_expression (parser, false);
20143
20144   if (t == error_mark_node
20145       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20146     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20147                                            /*or_comma=*/false,
20148                                            /*consume_paren=*/true);
20149
20150   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20151                              "num_threads", location);
20152
20153   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20154   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20155   OMP_CLAUSE_CHAIN (c) = list;
20156
20157   return c;
20158 }
20159
20160 /* OpenMP 2.5:
20161    ordered */
20162
20163 static tree
20164 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20165                               tree list, location_t location)
20166 {
20167   tree c;
20168
20169   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20170                              "ordered", location);
20171
20172   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20173   OMP_CLAUSE_CHAIN (c) = list;
20174   return c;
20175 }
20176
20177 /* OpenMP 2.5:
20178    reduction ( reduction-operator : variable-list )
20179
20180    reduction-operator:
20181      One of: + * - & ^ | && || */
20182
20183 static tree
20184 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20185 {
20186   enum tree_code code;
20187   tree nlist, c;
20188
20189   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20190     return list;
20191
20192   switch (cp_lexer_peek_token (parser->lexer)->type)
20193     {
20194     case CPP_PLUS:
20195       code = PLUS_EXPR;
20196       break;
20197     case CPP_MULT:
20198       code = MULT_EXPR;
20199       break;
20200     case CPP_MINUS:
20201       code = MINUS_EXPR;
20202       break;
20203     case CPP_AND:
20204       code = BIT_AND_EXPR;
20205       break;
20206     case CPP_XOR:
20207       code = BIT_XOR_EXPR;
20208       break;
20209     case CPP_OR:
20210       code = BIT_IOR_EXPR;
20211       break;
20212     case CPP_AND_AND:
20213       code = TRUTH_ANDIF_EXPR;
20214       break;
20215     case CPP_OR_OR:
20216       code = TRUTH_ORIF_EXPR;
20217       break;
20218     default:
20219       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20220                                "%<|%>, %<&&%>, or %<||%>");
20221     resync_fail:
20222       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20223                                              /*or_comma=*/false,
20224                                              /*consume_paren=*/true);
20225       return list;
20226     }
20227   cp_lexer_consume_token (parser->lexer);
20228
20229   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20230     goto resync_fail;
20231
20232   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20233   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20234     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20235
20236   return nlist;
20237 }
20238
20239 /* OpenMP 2.5:
20240    schedule ( schedule-kind )
20241    schedule ( schedule-kind , expression )
20242
20243    schedule-kind:
20244      static | dynamic | guided | runtime | auto  */
20245
20246 static tree
20247 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20248 {
20249   tree c, t;
20250
20251   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20252     return list;
20253
20254   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20255
20256   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20257     {
20258       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20259       const char *p = IDENTIFIER_POINTER (id);
20260
20261       switch (p[0])
20262         {
20263         case 'd':
20264           if (strcmp ("dynamic", p) != 0)
20265             goto invalid_kind;
20266           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20267           break;
20268
20269         case 'g':
20270           if (strcmp ("guided", p) != 0)
20271             goto invalid_kind;
20272           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20273           break;
20274
20275         case 'r':
20276           if (strcmp ("runtime", p) != 0)
20277             goto invalid_kind;
20278           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20279           break;
20280
20281         default:
20282           goto invalid_kind;
20283         }
20284     }
20285   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20286     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20287   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20288     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20289   else
20290     goto invalid_kind;
20291   cp_lexer_consume_token (parser->lexer);
20292
20293   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20294     {
20295       cp_token *token;
20296       cp_lexer_consume_token (parser->lexer);
20297
20298       token = cp_lexer_peek_token (parser->lexer);
20299       t = cp_parser_assignment_expression (parser, false);
20300
20301       if (t == error_mark_node)
20302         goto resync_fail;
20303       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20304         error ("%Hschedule %<runtime%> does not take "
20305                "a %<chunk_size%> parameter", &token->location);
20306       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20307         error ("%Hschedule %<auto%> does not take "
20308                "a %<chunk_size%> parameter", &token->location);
20309       else
20310         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20311
20312       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20313         goto resync_fail;
20314     }
20315   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20316     goto resync_fail;
20317
20318   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20319   OMP_CLAUSE_CHAIN (c) = list;
20320   return c;
20321
20322  invalid_kind:
20323   cp_parser_error (parser, "invalid schedule kind");
20324  resync_fail:
20325   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20326                                          /*or_comma=*/false,
20327                                          /*consume_paren=*/true);
20328   return list;
20329 }
20330
20331 /* OpenMP 3.0:
20332    untied */
20333
20334 static tree
20335 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20336                              tree list, location_t location)
20337 {
20338   tree c;
20339
20340   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20341
20342   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20343   OMP_CLAUSE_CHAIN (c) = list;
20344   return c;
20345 }
20346
20347 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20348    is a bitmask in MASK.  Return the list of clauses found; the result
20349    of clause default goes in *pdefault.  */
20350
20351 static tree
20352 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20353                            const char *where, cp_token *pragma_tok)
20354 {
20355   tree clauses = NULL;
20356   bool first = true;
20357   cp_token *token = NULL;
20358
20359   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20360     {
20361       pragma_omp_clause c_kind;
20362       const char *c_name;
20363       tree prev = clauses;
20364
20365       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20366         cp_lexer_consume_token (parser->lexer);
20367
20368       token = cp_lexer_peek_token (parser->lexer);
20369       c_kind = cp_parser_omp_clause_name (parser);
20370       first = false;
20371
20372       switch (c_kind)
20373         {
20374         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20375           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20376                                                    token->location);
20377           c_name = "collapse";
20378           break;
20379         case PRAGMA_OMP_CLAUSE_COPYIN:
20380           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20381           c_name = "copyin";
20382           break;
20383         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20384           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20385                                             clauses);
20386           c_name = "copyprivate";
20387           break;
20388         case PRAGMA_OMP_CLAUSE_DEFAULT:
20389           clauses = cp_parser_omp_clause_default (parser, clauses,
20390                                                   token->location);
20391           c_name = "default";
20392           break;
20393         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20394           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20395                                             clauses);
20396           c_name = "firstprivate";
20397           break;
20398         case PRAGMA_OMP_CLAUSE_IF:
20399           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20400           c_name = "if";
20401           break;
20402         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20403           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20404                                             clauses);
20405           c_name = "lastprivate";
20406           break;
20407         case PRAGMA_OMP_CLAUSE_NOWAIT:
20408           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20409           c_name = "nowait";
20410           break;
20411         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20412           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20413                                                       token->location);
20414           c_name = "num_threads";
20415           break;
20416         case PRAGMA_OMP_CLAUSE_ORDERED:
20417           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20418                                                   token->location);
20419           c_name = "ordered";
20420           break;
20421         case PRAGMA_OMP_CLAUSE_PRIVATE:
20422           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20423                                             clauses);
20424           c_name = "private";
20425           break;
20426         case PRAGMA_OMP_CLAUSE_REDUCTION:
20427           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20428           c_name = "reduction";
20429           break;
20430         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20431           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20432                                                    token->location);
20433           c_name = "schedule";
20434           break;
20435         case PRAGMA_OMP_CLAUSE_SHARED:
20436           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20437                                             clauses);
20438           c_name = "shared";
20439           break;
20440         case PRAGMA_OMP_CLAUSE_UNTIED:
20441           clauses = cp_parser_omp_clause_untied (parser, clauses,
20442                                                  token->location);
20443           c_name = "nowait";
20444           break;
20445         default:
20446           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20447           goto saw_error;
20448         }
20449
20450       if (((mask >> c_kind) & 1) == 0)
20451         {
20452           /* Remove the invalid clause(s) from the list to avoid
20453              confusing the rest of the compiler.  */
20454           clauses = prev;
20455           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20456         }
20457     }
20458  saw_error:
20459   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20460   return finish_omp_clauses (clauses);
20461 }
20462
20463 /* OpenMP 2.5:
20464    structured-block:
20465      statement
20466
20467    In practice, we're also interested in adding the statement to an
20468    outer node.  So it is convenient if we work around the fact that
20469    cp_parser_statement calls add_stmt.  */
20470
20471 static unsigned
20472 cp_parser_begin_omp_structured_block (cp_parser *parser)
20473 {
20474   unsigned save = parser->in_statement;
20475
20476   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20477      This preserves the "not within loop or switch" style error messages
20478      for nonsense cases like
20479         void foo() {
20480         #pragma omp single
20481           break;
20482         }
20483   */
20484   if (parser->in_statement)
20485     parser->in_statement = IN_OMP_BLOCK;
20486
20487   return save;
20488 }
20489
20490 static void
20491 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20492 {
20493   parser->in_statement = save;
20494 }
20495
20496 static tree
20497 cp_parser_omp_structured_block (cp_parser *parser)
20498 {
20499   tree stmt = begin_omp_structured_block ();
20500   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20501
20502   cp_parser_statement (parser, NULL_TREE, false, NULL);
20503
20504   cp_parser_end_omp_structured_block (parser, save);
20505   return finish_omp_structured_block (stmt);
20506 }
20507
20508 /* OpenMP 2.5:
20509    # pragma omp atomic new-line
20510      expression-stmt
20511
20512    expression-stmt:
20513      x binop= expr | x++ | ++x | x-- | --x
20514    binop:
20515      +, *, -, /, &, ^, |, <<, >>
20516
20517   where x is an lvalue expression with scalar type.  */
20518
20519 static void
20520 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20521 {
20522   tree lhs, rhs;
20523   enum tree_code code;
20524
20525   cp_parser_require_pragma_eol (parser, pragma_tok);
20526
20527   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20528                                     /*cast_p=*/false);
20529   switch (TREE_CODE (lhs))
20530     {
20531     case ERROR_MARK:
20532       goto saw_error;
20533
20534     case PREINCREMENT_EXPR:
20535     case POSTINCREMENT_EXPR:
20536       lhs = TREE_OPERAND (lhs, 0);
20537       code = PLUS_EXPR;
20538       rhs = integer_one_node;
20539       break;
20540
20541     case PREDECREMENT_EXPR:
20542     case POSTDECREMENT_EXPR:
20543       lhs = TREE_OPERAND (lhs, 0);
20544       code = MINUS_EXPR;
20545       rhs = integer_one_node;
20546       break;
20547
20548     default:
20549       switch (cp_lexer_peek_token (parser->lexer)->type)
20550         {
20551         case CPP_MULT_EQ:
20552           code = MULT_EXPR;
20553           break;
20554         case CPP_DIV_EQ:
20555           code = TRUNC_DIV_EXPR;
20556           break;
20557         case CPP_PLUS_EQ:
20558           code = PLUS_EXPR;
20559           break;
20560         case CPP_MINUS_EQ:
20561           code = MINUS_EXPR;
20562           break;
20563         case CPP_LSHIFT_EQ:
20564           code = LSHIFT_EXPR;
20565           break;
20566         case CPP_RSHIFT_EQ:
20567           code = RSHIFT_EXPR;
20568           break;
20569         case CPP_AND_EQ:
20570           code = BIT_AND_EXPR;
20571           break;
20572         case CPP_OR_EQ:
20573           code = BIT_IOR_EXPR;
20574           break;
20575         case CPP_XOR_EQ:
20576           code = BIT_XOR_EXPR;
20577           break;
20578         default:
20579           cp_parser_error (parser,
20580                            "invalid operator for %<#pragma omp atomic%>");
20581           goto saw_error;
20582         }
20583       cp_lexer_consume_token (parser->lexer);
20584
20585       rhs = cp_parser_expression (parser, false);
20586       if (rhs == error_mark_node)
20587         goto saw_error;
20588       break;
20589     }
20590   finish_omp_atomic (code, lhs, rhs);
20591   cp_parser_consume_semicolon_at_end_of_statement (parser);
20592   return;
20593
20594  saw_error:
20595   cp_parser_skip_to_end_of_block_or_statement (parser);
20596 }
20597
20598
20599 /* OpenMP 2.5:
20600    # pragma omp barrier new-line  */
20601
20602 static void
20603 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20604 {
20605   cp_parser_require_pragma_eol (parser, pragma_tok);
20606   finish_omp_barrier ();
20607 }
20608
20609 /* OpenMP 2.5:
20610    # pragma omp critical [(name)] new-line
20611      structured-block  */
20612
20613 static tree
20614 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20615 {
20616   tree stmt, name = NULL;
20617
20618   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20619     {
20620       cp_lexer_consume_token (parser->lexer);
20621
20622       name = cp_parser_identifier (parser);
20623
20624       if (name == error_mark_node
20625           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20626         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20627                                                /*or_comma=*/false,
20628                                                /*consume_paren=*/true);
20629       if (name == error_mark_node)
20630         name = NULL;
20631     }
20632   cp_parser_require_pragma_eol (parser, pragma_tok);
20633
20634   stmt = cp_parser_omp_structured_block (parser);
20635   return c_finish_omp_critical (stmt, name);
20636 }
20637
20638 /* OpenMP 2.5:
20639    # pragma omp flush flush-vars[opt] new-line
20640
20641    flush-vars:
20642      ( variable-list ) */
20643
20644 static void
20645 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20646 {
20647   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20648     (void) cp_parser_omp_var_list (parser, 0, NULL);
20649   cp_parser_require_pragma_eol (parser, pragma_tok);
20650
20651   finish_omp_flush ();
20652 }
20653
20654 /* Helper function, to parse omp for increment expression.  */
20655
20656 static tree
20657 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20658 {
20659   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20660   enum tree_code op;
20661   cp_token *token;
20662
20663   if (lhs != decl)
20664     {
20665       cp_parser_skip_to_end_of_statement (parser);
20666       return error_mark_node;
20667     }
20668
20669   token = cp_lexer_peek_token (parser->lexer);
20670   op = binops_by_token [token->type].tree_type;
20671   switch (op)
20672     {
20673     case LT_EXPR:
20674     case LE_EXPR:
20675     case GT_EXPR:
20676     case GE_EXPR:
20677       break;
20678     default:
20679       cp_parser_skip_to_end_of_statement (parser);
20680       return error_mark_node;
20681     }
20682
20683   cp_lexer_consume_token (parser->lexer);
20684   rhs = cp_parser_binary_expression (parser, false,
20685                                      PREC_RELATIONAL_EXPRESSION);
20686   if (rhs == error_mark_node
20687       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20688     {
20689       cp_parser_skip_to_end_of_statement (parser);
20690       return error_mark_node;
20691     }
20692
20693   return build2 (op, boolean_type_node, lhs, rhs);
20694 }
20695
20696 /* Helper function, to parse omp for increment expression.  */
20697
20698 static tree
20699 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20700 {
20701   cp_token *token = cp_lexer_peek_token (parser->lexer);
20702   enum tree_code op;
20703   tree lhs, rhs;
20704   cp_id_kind idk;
20705   bool decl_first;
20706
20707   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20708     {
20709       op = (token->type == CPP_PLUS_PLUS
20710             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20711       cp_lexer_consume_token (parser->lexer);
20712       lhs = cp_parser_cast_expression (parser, false, false);
20713       if (lhs != decl)
20714         return error_mark_node;
20715       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20716     }
20717
20718   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20719   if (lhs != decl)
20720     return error_mark_node;
20721
20722   token = cp_lexer_peek_token (parser->lexer);
20723   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20724     {
20725       op = (token->type == CPP_PLUS_PLUS
20726             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20727       cp_lexer_consume_token (parser->lexer);
20728       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20729     }
20730
20731   op = cp_parser_assignment_operator_opt (parser);
20732   if (op == ERROR_MARK)
20733     return error_mark_node;
20734
20735   if (op != NOP_EXPR)
20736     {
20737       rhs = cp_parser_assignment_expression (parser, false);
20738       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20739       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20740     }
20741
20742   lhs = cp_parser_binary_expression (parser, false,
20743                                      PREC_ADDITIVE_EXPRESSION);
20744   token = cp_lexer_peek_token (parser->lexer);
20745   decl_first = lhs == decl;
20746   if (decl_first)
20747     lhs = NULL_TREE;
20748   if (token->type != CPP_PLUS
20749       && token->type != CPP_MINUS)
20750     return error_mark_node;
20751
20752   do
20753     {
20754       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20755       cp_lexer_consume_token (parser->lexer);
20756       rhs = cp_parser_binary_expression (parser, false,
20757                                          PREC_ADDITIVE_EXPRESSION);
20758       token = cp_lexer_peek_token (parser->lexer);
20759       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20760         {
20761           if (lhs == NULL_TREE)
20762             {
20763               if (op == PLUS_EXPR)
20764                 lhs = rhs;
20765               else
20766                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20767             }
20768           else
20769             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20770                                      NULL, tf_warning_or_error);
20771         }
20772     }
20773   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20774
20775   if (!decl_first)
20776     {
20777       if (rhs != decl || op == MINUS_EXPR)
20778         return error_mark_node;
20779       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20780     }
20781   else
20782     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20783
20784   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20785 }
20786
20787 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20788
20789 static tree
20790 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20791 {
20792   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20793   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20794   tree this_pre_body, cl;
20795   location_t loc_first;
20796   bool collapse_err = false;
20797   int i, collapse = 1, nbraces = 0;
20798
20799   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20800     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20801       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20802
20803   gcc_assert (collapse >= 1);
20804
20805   declv = make_tree_vec (collapse);
20806   initv = make_tree_vec (collapse);
20807   condv = make_tree_vec (collapse);
20808   incrv = make_tree_vec (collapse);
20809
20810   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20811
20812   for (i = 0; i < collapse; i++)
20813     {
20814       int bracecount = 0;
20815       bool add_private_clause = false;
20816       location_t loc;
20817
20818       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20819         {
20820           cp_parser_error (parser, "for statement expected");
20821           return NULL;
20822         }
20823       loc = cp_lexer_consume_token (parser->lexer)->location;
20824
20825       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20826         return NULL;
20827
20828       init = decl = real_decl = NULL;
20829       this_pre_body = push_stmt_list ();
20830       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20831         {
20832           cp_decl_specifier_seq type_specifiers;
20833
20834           /* First, try to parse as an initialized declaration.  See
20835              cp_parser_condition, from whence the bulk of this is copied.  */
20836
20837           cp_parser_parse_tentatively (parser);
20838           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20839                                         &type_specifiers);
20840           if (!cp_parser_error_occurred (parser))
20841             {
20842               tree asm_specification, attributes;
20843               cp_declarator *declarator;
20844
20845               declarator = cp_parser_declarator (parser,
20846                                                  CP_PARSER_DECLARATOR_NAMED,
20847                                                  /*ctor_dtor_or_conv_p=*/NULL,
20848                                                  /*parenthesized_p=*/NULL,
20849                                                  /*member_p=*/false);
20850               attributes = cp_parser_attributes_opt (parser);
20851               asm_specification = cp_parser_asm_specification_opt (parser);
20852
20853               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20854                 cp_parser_require (parser, CPP_EQ, "%<=%>");
20855               if (cp_parser_parse_definitely (parser))
20856                 {
20857                   tree pushed_scope;
20858
20859                   decl = start_decl (declarator, &type_specifiers,
20860                                      /*initialized_p=*/false, attributes,
20861                                      /*prefix_attributes=*/NULL_TREE,
20862                                      &pushed_scope);
20863
20864                   if (CLASS_TYPE_P (TREE_TYPE (decl))
20865                       || type_dependent_expression_p (decl))
20866                     {
20867                       bool is_direct_init, is_non_constant_init;
20868
20869                       init = cp_parser_initializer (parser,
20870                                                     &is_direct_init,
20871                                                     &is_non_constant_init);
20872
20873                       cp_finish_decl (decl, init, !is_non_constant_init,
20874                                       asm_specification,
20875                                       LOOKUP_ONLYCONVERTING);
20876                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
20877                         {
20878                           for_block
20879                             = tree_cons (NULL, this_pre_body, for_block);
20880                           init = NULL_TREE;
20881                         }
20882                       else
20883                         init = pop_stmt_list (this_pre_body);
20884                       this_pre_body = NULL_TREE;
20885                     }
20886                   else
20887                     {
20888                       cp_parser_require (parser, CPP_EQ, "%<=%>");
20889                       init = cp_parser_assignment_expression (parser, false);
20890
20891                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20892                         init = error_mark_node;
20893                       else
20894                         cp_finish_decl (decl, NULL_TREE,
20895                                         /*init_const_expr_p=*/false,
20896                                         asm_specification,
20897                                         LOOKUP_ONLYCONVERTING);
20898                     }
20899
20900                   if (pushed_scope)
20901                     pop_scope (pushed_scope);
20902                 }
20903             }
20904           else
20905             cp_parser_abort_tentative_parse (parser);
20906
20907           /* If parsing as an initialized declaration failed, try again as
20908              a simple expression.  */
20909           if (decl == NULL)
20910             {
20911               cp_id_kind idk;
20912               cp_parser_parse_tentatively (parser);
20913               decl = cp_parser_primary_expression (parser, false, false,
20914                                                    false, &idk);
20915               if (!cp_parser_error_occurred (parser)
20916                   && decl
20917                   && DECL_P (decl)
20918                   && CLASS_TYPE_P (TREE_TYPE (decl)))
20919                 {
20920                   tree rhs;
20921
20922                   cp_parser_parse_definitely (parser);
20923                   cp_parser_require (parser, CPP_EQ, "%<=%>");
20924                   rhs = cp_parser_assignment_expression (parser, false);
20925                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20926                                                          rhs,
20927                                                          tf_warning_or_error));
20928                   add_private_clause = true;
20929                 }
20930               else
20931                 {
20932                   decl = NULL;
20933                   cp_parser_abort_tentative_parse (parser);
20934                   init = cp_parser_expression (parser, false);
20935                   if (init)
20936                     {
20937                       if (TREE_CODE (init) == MODIFY_EXPR
20938                           || TREE_CODE (init) == MODOP_EXPR)
20939                         real_decl = TREE_OPERAND (init, 0);
20940                     }
20941                 }
20942             }
20943         }
20944       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20945       if (this_pre_body)
20946         {
20947           this_pre_body = pop_stmt_list (this_pre_body);
20948           if (pre_body)
20949             {
20950               tree t = pre_body;
20951               pre_body = push_stmt_list ();
20952               add_stmt (t);
20953               add_stmt (this_pre_body);
20954               pre_body = pop_stmt_list (pre_body);
20955             }
20956           else
20957             pre_body = this_pre_body;
20958         }
20959
20960       if (decl)
20961         real_decl = decl;
20962       if (par_clauses != NULL && real_decl != NULL_TREE)
20963         {
20964           tree *c;
20965           for (c = par_clauses; *c ; )
20966             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20967                 && OMP_CLAUSE_DECL (*c) == real_decl)
20968               {
20969                 error ("%Hiteration variable %qD should not be firstprivate",
20970                        &loc, real_decl);
20971                 *c = OMP_CLAUSE_CHAIN (*c);
20972               }
20973             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
20974                      && OMP_CLAUSE_DECL (*c) == real_decl)
20975               {
20976                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
20977                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
20978                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
20979                 OMP_CLAUSE_DECL (l) = real_decl;
20980                 OMP_CLAUSE_CHAIN (l) = clauses;
20981                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
20982                 clauses = l;
20983                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
20984                 CP_OMP_CLAUSE_INFO (*c) = NULL;
20985                 add_private_clause = false;
20986               }
20987             else
20988               {
20989                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
20990                     && OMP_CLAUSE_DECL (*c) == real_decl)
20991                   add_private_clause = false;
20992                 c = &OMP_CLAUSE_CHAIN (*c);
20993               }
20994         }
20995
20996       if (add_private_clause)
20997         {
20998           tree c;
20999           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21000             {
21001               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21002                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21003                   && OMP_CLAUSE_DECL (c) == decl)
21004                 break;
21005               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21006                        && OMP_CLAUSE_DECL (c) == decl)
21007                 error ("%Hiteration variable %qD should not be firstprivate",
21008                        &loc, decl);
21009               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21010                        && OMP_CLAUSE_DECL (c) == decl)
21011                 error ("%Hiteration variable %qD should not be reduction",
21012                        &loc, decl);
21013             }
21014           if (c == NULL)
21015             {
21016               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21017               OMP_CLAUSE_DECL (c) = decl;
21018               c = finish_omp_clauses (c);
21019               if (c)
21020                 {
21021                   OMP_CLAUSE_CHAIN (c) = clauses;
21022                   clauses = c;
21023                 }
21024             }
21025         }
21026
21027       cond = NULL;
21028       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21029         {
21030           /* If decl is an iterator, preserve LHS and RHS of the relational
21031              expr until finish_omp_for.  */
21032           if (decl
21033               && (type_dependent_expression_p (decl)
21034                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21035             cond = cp_parser_omp_for_cond (parser, decl);
21036           else
21037             cond = cp_parser_condition (parser);
21038         }
21039       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21040
21041       incr = NULL;
21042       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21043         {
21044           /* If decl is an iterator, preserve the operator on decl
21045              until finish_omp_for.  */
21046           if (decl
21047               && (type_dependent_expression_p (decl)
21048                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21049             incr = cp_parser_omp_for_incr (parser, decl);
21050           else
21051             incr = cp_parser_expression (parser, false);
21052         }
21053
21054       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21055         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21056                                                /*or_comma=*/false,
21057                                                /*consume_paren=*/true);
21058
21059       TREE_VEC_ELT (declv, i) = decl;
21060       TREE_VEC_ELT (initv, i) = init;
21061       TREE_VEC_ELT (condv, i) = cond;
21062       TREE_VEC_ELT (incrv, i) = incr;
21063
21064       if (i == collapse - 1)
21065         break;
21066
21067       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21068          in between the collapsed for loops to be still considered perfectly
21069          nested.  Hopefully the final version clarifies this.
21070          For now handle (multiple) {'s and empty statements.  */
21071       cp_parser_parse_tentatively (parser);
21072       do
21073         {
21074           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21075             break;
21076           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21077             {
21078               cp_lexer_consume_token (parser->lexer);
21079               bracecount++;
21080             }
21081           else if (bracecount
21082                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21083             cp_lexer_consume_token (parser->lexer);
21084           else
21085             {
21086               loc = cp_lexer_peek_token (parser->lexer)->location;
21087               error ("%Hnot enough collapsed for loops", &loc);
21088               collapse_err = true;
21089               cp_parser_abort_tentative_parse (parser);
21090               declv = NULL_TREE;
21091               break;
21092             }
21093         }
21094       while (1);
21095
21096       if (declv)
21097         {
21098           cp_parser_parse_definitely (parser);
21099           nbraces += bracecount;
21100         }
21101     }
21102
21103   /* Note that we saved the original contents of this flag when we entered
21104      the structured block, and so we don't need to re-save it here.  */
21105   parser->in_statement = IN_OMP_FOR;
21106
21107   /* Note that the grammar doesn't call for a structured block here,
21108      though the loop as a whole is a structured block.  */
21109   body = push_stmt_list ();
21110   cp_parser_statement (parser, NULL_TREE, false, NULL);
21111   body = pop_stmt_list (body);
21112
21113   if (declv == NULL_TREE)
21114     ret = NULL_TREE;
21115   else
21116     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21117                           pre_body, clauses);
21118
21119   while (nbraces)
21120     {
21121       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21122         {
21123           cp_lexer_consume_token (parser->lexer);
21124           nbraces--;
21125         }
21126       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21127         cp_lexer_consume_token (parser->lexer);
21128       else
21129         {
21130           if (!collapse_err)
21131             {
21132               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21133               error ("%Hcollapsed loops not perfectly nested", &loc);
21134             }
21135           collapse_err = true;
21136           cp_parser_statement_seq_opt (parser, NULL);
21137           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21138         }
21139     }
21140
21141   while (for_block)
21142     {
21143       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21144       for_block = TREE_CHAIN (for_block);
21145     }
21146
21147   return ret;
21148 }
21149
21150 /* OpenMP 2.5:
21151    #pragma omp for for-clause[optseq] new-line
21152      for-loop  */
21153
21154 #define OMP_FOR_CLAUSE_MASK                             \
21155         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21156         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21157         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21158         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21159         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21160         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21161         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21162         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21163
21164 static tree
21165 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21166 {
21167   tree clauses, sb, ret;
21168   unsigned int save;
21169
21170   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21171                                        "#pragma omp for", pragma_tok);
21172
21173   sb = begin_omp_structured_block ();
21174   save = cp_parser_begin_omp_structured_block (parser);
21175
21176   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21177
21178   cp_parser_end_omp_structured_block (parser, save);
21179   add_stmt (finish_omp_structured_block (sb));
21180
21181   return ret;
21182 }
21183
21184 /* OpenMP 2.5:
21185    # pragma omp master new-line
21186      structured-block  */
21187
21188 static tree
21189 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21190 {
21191   cp_parser_require_pragma_eol (parser, pragma_tok);
21192   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21193 }
21194
21195 /* OpenMP 2.5:
21196    # pragma omp ordered new-line
21197      structured-block  */
21198
21199 static tree
21200 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21201 {
21202   cp_parser_require_pragma_eol (parser, pragma_tok);
21203   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21204 }
21205
21206 /* OpenMP 2.5:
21207
21208    section-scope:
21209      { section-sequence }
21210
21211    section-sequence:
21212      section-directive[opt] structured-block
21213      section-sequence section-directive structured-block  */
21214
21215 static tree
21216 cp_parser_omp_sections_scope (cp_parser *parser)
21217 {
21218   tree stmt, substmt;
21219   bool error_suppress = false;
21220   cp_token *tok;
21221
21222   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21223     return NULL_TREE;
21224
21225   stmt = push_stmt_list ();
21226
21227   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21228     {
21229       unsigned save;
21230
21231       substmt = begin_omp_structured_block ();
21232       save = cp_parser_begin_omp_structured_block (parser);
21233
21234       while (1)
21235         {
21236           cp_parser_statement (parser, NULL_TREE, false, NULL);
21237
21238           tok = cp_lexer_peek_token (parser->lexer);
21239           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21240             break;
21241           if (tok->type == CPP_CLOSE_BRACE)
21242             break;
21243           if (tok->type == CPP_EOF)
21244             break;
21245         }
21246
21247       cp_parser_end_omp_structured_block (parser, save);
21248       substmt = finish_omp_structured_block (substmt);
21249       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21250       add_stmt (substmt);
21251     }
21252
21253   while (1)
21254     {
21255       tok = cp_lexer_peek_token (parser->lexer);
21256       if (tok->type == CPP_CLOSE_BRACE)
21257         break;
21258       if (tok->type == CPP_EOF)
21259         break;
21260
21261       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21262         {
21263           cp_lexer_consume_token (parser->lexer);
21264           cp_parser_require_pragma_eol (parser, tok);
21265           error_suppress = false;
21266         }
21267       else if (!error_suppress)
21268         {
21269           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21270           error_suppress = true;
21271         }
21272
21273       substmt = cp_parser_omp_structured_block (parser);
21274       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21275       add_stmt (substmt);
21276     }
21277   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21278
21279   substmt = pop_stmt_list (stmt);
21280
21281   stmt = make_node (OMP_SECTIONS);
21282   TREE_TYPE (stmt) = void_type_node;
21283   OMP_SECTIONS_BODY (stmt) = substmt;
21284
21285   add_stmt (stmt);
21286   return stmt;
21287 }
21288
21289 /* OpenMP 2.5:
21290    # pragma omp sections sections-clause[optseq] newline
21291      sections-scope  */
21292
21293 #define OMP_SECTIONS_CLAUSE_MASK                        \
21294         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21295         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21296         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21297         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21298         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21299
21300 static tree
21301 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21302 {
21303   tree clauses, ret;
21304
21305   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21306                                        "#pragma omp sections", pragma_tok);
21307
21308   ret = cp_parser_omp_sections_scope (parser);
21309   if (ret)
21310     OMP_SECTIONS_CLAUSES (ret) = clauses;
21311
21312   return ret;
21313 }
21314
21315 /* OpenMP 2.5:
21316    # pragma parallel parallel-clause new-line
21317    # pragma parallel for parallel-for-clause new-line
21318    # pragma parallel sections parallel-sections-clause new-line  */
21319
21320 #define OMP_PARALLEL_CLAUSE_MASK                        \
21321         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21322         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21323         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21324         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21325         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21326         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21327         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21328         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21329
21330 static tree
21331 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21332 {
21333   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21334   const char *p_name = "#pragma omp parallel";
21335   tree stmt, clauses, par_clause, ws_clause, block;
21336   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21337   unsigned int save;
21338
21339   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21340     {
21341       cp_lexer_consume_token (parser->lexer);
21342       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21343       p_name = "#pragma omp parallel for";
21344       mask |= OMP_FOR_CLAUSE_MASK;
21345       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21346     }
21347   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21348     {
21349       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21350       const char *p = IDENTIFIER_POINTER (id);
21351       if (strcmp (p, "sections") == 0)
21352         {
21353           cp_lexer_consume_token (parser->lexer);
21354           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21355           p_name = "#pragma omp parallel sections";
21356           mask |= OMP_SECTIONS_CLAUSE_MASK;
21357           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21358         }
21359     }
21360
21361   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21362   block = begin_omp_parallel ();
21363   save = cp_parser_begin_omp_structured_block (parser);
21364
21365   switch (p_kind)
21366     {
21367     case PRAGMA_OMP_PARALLEL:
21368       cp_parser_statement (parser, NULL_TREE, false, NULL);
21369       par_clause = clauses;
21370       break;
21371
21372     case PRAGMA_OMP_PARALLEL_FOR:
21373       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21374       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21375       break;
21376
21377     case PRAGMA_OMP_PARALLEL_SECTIONS:
21378       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21379       stmt = cp_parser_omp_sections_scope (parser);
21380       if (stmt)
21381         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21382       break;
21383
21384     default:
21385       gcc_unreachable ();
21386     }
21387
21388   cp_parser_end_omp_structured_block (parser, save);
21389   stmt = finish_omp_parallel (par_clause, block);
21390   if (p_kind != PRAGMA_OMP_PARALLEL)
21391     OMP_PARALLEL_COMBINED (stmt) = 1;
21392   return stmt;
21393 }
21394
21395 /* OpenMP 2.5:
21396    # pragma omp single single-clause[optseq] new-line
21397      structured-block  */
21398
21399 #define OMP_SINGLE_CLAUSE_MASK                          \
21400         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21401         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21402         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21403         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21404
21405 static tree
21406 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21407 {
21408   tree stmt = make_node (OMP_SINGLE);
21409   TREE_TYPE (stmt) = void_type_node;
21410
21411   OMP_SINGLE_CLAUSES (stmt)
21412     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21413                                  "#pragma omp single", pragma_tok);
21414   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21415
21416   return add_stmt (stmt);
21417 }
21418
21419 /* OpenMP 3.0:
21420    # pragma omp task task-clause[optseq] new-line
21421      structured-block  */
21422
21423 #define OMP_TASK_CLAUSE_MASK                            \
21424         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21425         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21426         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21427         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21428         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21429         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21430
21431 static tree
21432 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21433 {
21434   tree clauses, block;
21435   unsigned int save;
21436
21437   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21438                                        "#pragma omp task", pragma_tok);
21439   block = begin_omp_task ();
21440   save = cp_parser_begin_omp_structured_block (parser);
21441   cp_parser_statement (parser, NULL_TREE, false, NULL);
21442   cp_parser_end_omp_structured_block (parser, save);
21443   return finish_omp_task (clauses, block);
21444 }
21445
21446 /* OpenMP 3.0:
21447    # pragma omp taskwait new-line  */
21448
21449 static void
21450 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21451 {
21452   cp_parser_require_pragma_eol (parser, pragma_tok);
21453   finish_omp_taskwait ();
21454 }
21455
21456 /* OpenMP 2.5:
21457    # pragma omp threadprivate (variable-list) */
21458
21459 static void
21460 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21461 {
21462   tree vars;
21463
21464   vars = cp_parser_omp_var_list (parser, 0, NULL);
21465   cp_parser_require_pragma_eol (parser, pragma_tok);
21466
21467   finish_omp_threadprivate (vars);
21468 }
21469
21470 /* Main entry point to OpenMP statement pragmas.  */
21471
21472 static void
21473 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21474 {
21475   tree stmt;
21476
21477   switch (pragma_tok->pragma_kind)
21478     {
21479     case PRAGMA_OMP_ATOMIC:
21480       cp_parser_omp_atomic (parser, pragma_tok);
21481       return;
21482     case PRAGMA_OMP_CRITICAL:
21483       stmt = cp_parser_omp_critical (parser, pragma_tok);
21484       break;
21485     case PRAGMA_OMP_FOR:
21486       stmt = cp_parser_omp_for (parser, pragma_tok);
21487       break;
21488     case PRAGMA_OMP_MASTER:
21489       stmt = cp_parser_omp_master (parser, pragma_tok);
21490       break;
21491     case PRAGMA_OMP_ORDERED:
21492       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21493       break;
21494     case PRAGMA_OMP_PARALLEL:
21495       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21496       break;
21497     case PRAGMA_OMP_SECTIONS:
21498       stmt = cp_parser_omp_sections (parser, pragma_tok);
21499       break;
21500     case PRAGMA_OMP_SINGLE:
21501       stmt = cp_parser_omp_single (parser, pragma_tok);
21502       break;
21503     case PRAGMA_OMP_TASK:
21504       stmt = cp_parser_omp_task (parser, pragma_tok);
21505       break;
21506     default:
21507       gcc_unreachable ();
21508     }
21509
21510   if (stmt)
21511     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21512 }
21513 \f
21514 /* The parser.  */
21515
21516 static GTY (()) cp_parser *the_parser;
21517
21518 \f
21519 /* Special handling for the first token or line in the file.  The first
21520    thing in the file might be #pragma GCC pch_preprocess, which loads a
21521    PCH file, which is a GC collection point.  So we need to handle this
21522    first pragma without benefit of an existing lexer structure.
21523
21524    Always returns one token to the caller in *FIRST_TOKEN.  This is
21525    either the true first token of the file, or the first token after
21526    the initial pragma.  */
21527
21528 static void
21529 cp_parser_initial_pragma (cp_token *first_token)
21530 {
21531   tree name = NULL;
21532
21533   cp_lexer_get_preprocessor_token (NULL, first_token);
21534   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21535     return;
21536
21537   cp_lexer_get_preprocessor_token (NULL, first_token);
21538   if (first_token->type == CPP_STRING)
21539     {
21540       name = first_token->u.value;
21541
21542       cp_lexer_get_preprocessor_token (NULL, first_token);
21543       if (first_token->type != CPP_PRAGMA_EOL)
21544         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21545                &first_token->location);
21546     }
21547   else
21548     error ("%Hexpected string literal", &first_token->location);
21549
21550   /* Skip to the end of the pragma.  */
21551   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21552     cp_lexer_get_preprocessor_token (NULL, first_token);
21553
21554   /* Now actually load the PCH file.  */
21555   if (name)
21556     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21557
21558   /* Read one more token to return to our caller.  We have to do this
21559      after reading the PCH file in, since its pointers have to be
21560      live.  */
21561   cp_lexer_get_preprocessor_token (NULL, first_token);
21562 }
21563
21564 /* Normal parsing of a pragma token.  Here we can (and must) use the
21565    regular lexer.  */
21566
21567 static bool
21568 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21569 {
21570   cp_token *pragma_tok;
21571   unsigned int id;
21572
21573   pragma_tok = cp_lexer_consume_token (parser->lexer);
21574   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21575   parser->lexer->in_pragma = true;
21576
21577   id = pragma_tok->pragma_kind;
21578   switch (id)
21579     {
21580     case PRAGMA_GCC_PCH_PREPROCESS:
21581       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21582              &pragma_tok->location);
21583       break;
21584
21585     case PRAGMA_OMP_BARRIER:
21586       switch (context)
21587         {
21588         case pragma_compound:
21589           cp_parser_omp_barrier (parser, pragma_tok);
21590           return false;
21591         case pragma_stmt:
21592           error ("%H%<#pragma omp barrier%> may only be "
21593                  "used in compound statements", &pragma_tok->location);
21594           break;
21595         default:
21596           goto bad_stmt;
21597         }
21598       break;
21599
21600     case PRAGMA_OMP_FLUSH:
21601       switch (context)
21602         {
21603         case pragma_compound:
21604           cp_parser_omp_flush (parser, pragma_tok);
21605           return false;
21606         case pragma_stmt:
21607           error ("%H%<#pragma omp flush%> may only be "
21608                  "used in compound statements", &pragma_tok->location);
21609           break;
21610         default:
21611           goto bad_stmt;
21612         }
21613       break;
21614
21615     case PRAGMA_OMP_TASKWAIT:
21616       switch (context)
21617         {
21618         case pragma_compound:
21619           cp_parser_omp_taskwait (parser, pragma_tok);
21620           return false;
21621         case pragma_stmt:
21622           error ("%H%<#pragma omp taskwait%> may only be "
21623                  "used in compound statements",
21624                  &pragma_tok->location);
21625           break;
21626         default:
21627           goto bad_stmt;
21628         }
21629       break;
21630
21631     case PRAGMA_OMP_THREADPRIVATE:
21632       cp_parser_omp_threadprivate (parser, pragma_tok);
21633       return false;
21634
21635     case PRAGMA_OMP_ATOMIC:
21636     case PRAGMA_OMP_CRITICAL:
21637     case PRAGMA_OMP_FOR:
21638     case PRAGMA_OMP_MASTER:
21639     case PRAGMA_OMP_ORDERED:
21640     case PRAGMA_OMP_PARALLEL:
21641     case PRAGMA_OMP_SECTIONS:
21642     case PRAGMA_OMP_SINGLE:
21643     case PRAGMA_OMP_TASK:
21644       if (context == pragma_external)
21645         goto bad_stmt;
21646       cp_parser_omp_construct (parser, pragma_tok);
21647       return true;
21648
21649     case PRAGMA_OMP_SECTION:
21650       error ("%H%<#pragma omp section%> may only be used in "
21651              "%<#pragma omp sections%> construct", &pragma_tok->location);
21652       break;
21653
21654     default:
21655       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21656       c_invoke_pragma_handler (id);
21657       break;
21658
21659     bad_stmt:
21660       cp_parser_error (parser, "expected declaration specifiers");
21661       break;
21662     }
21663
21664   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21665   return false;
21666 }
21667
21668 /* The interface the pragma parsers have to the lexer.  */
21669
21670 enum cpp_ttype
21671 pragma_lex (tree *value)
21672 {
21673   cp_token *tok;
21674   enum cpp_ttype ret;
21675
21676   tok = cp_lexer_peek_token (the_parser->lexer);
21677
21678   ret = tok->type;
21679   *value = tok->u.value;
21680
21681   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21682     ret = CPP_EOF;
21683   else if (ret == CPP_STRING)
21684     *value = cp_parser_string_literal (the_parser, false, false);
21685   else
21686     {
21687       cp_lexer_consume_token (the_parser->lexer);
21688       if (ret == CPP_KEYWORD)
21689         ret = CPP_NAME;
21690     }
21691
21692   return ret;
21693 }
21694
21695 \f
21696 /* External interface.  */
21697
21698 /* Parse one entire translation unit.  */
21699
21700 void
21701 c_parse_file (void)
21702 {
21703   bool error_occurred;
21704   static bool already_called = false;
21705
21706   if (already_called)
21707     {
21708       sorry ("inter-module optimizations not implemented for C++");
21709       return;
21710     }
21711   already_called = true;
21712
21713   the_parser = cp_parser_new ();
21714   push_deferring_access_checks (flag_access_control
21715                                 ? dk_no_deferred : dk_no_check);
21716   error_occurred = cp_parser_translation_unit (the_parser);
21717   the_parser = NULL;
21718 }
21719
21720 #include "gt-cp-parser.h"