OSDN Git Service

gcc/cp:
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       tree parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification,
1019                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591   (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597   (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599   (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1601   (cp_token *);
1602 static tree cp_parser_new_expression
1603   (cp_parser *);
1604 static tree cp_parser_new_placement
1605   (cp_parser *);
1606 static tree cp_parser_new_type_id
1607   (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609   (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611   (cp_parser *);
1612 static tree cp_parser_new_initializer
1613   (cp_parser *);
1614 static tree cp_parser_delete_expression
1615   (cp_parser *);
1616 static tree cp_parser_cast_expression
1617   (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       /* Floating-point literals are only allowed in an integral
3148          constant expression if they are cast to an integral or
3149          enumeration type.  */
3150       if (TREE_CODE (token->u.value) == REAL_CST
3151           && parser->integral_constant_expression_p
3152           && pedantic)
3153         {
3154           /* CAST_P will be set even in invalid code like "int(2.7 +
3155              ...)".   Therefore, we have to check that the next token
3156              is sure to end the cast.  */
3157           if (cast_p)
3158             {
3159               cp_token *next_token;
3160
3161               next_token = cp_lexer_peek_token (parser->lexer);
3162               if (/* The comma at the end of an
3163                      enumerator-definition.  */
3164                   next_token->type != CPP_COMMA
3165                   /* The curly brace at the end of an enum-specifier.  */
3166                   && next_token->type != CPP_CLOSE_BRACE
3167                   /* The end of a statement.  */
3168                   && next_token->type != CPP_SEMICOLON
3169                   /* The end of the cast-expression.  */
3170                   && next_token->type != CPP_CLOSE_PAREN
3171                   /* The end of an array bound.  */
3172                   && next_token->type != CPP_CLOSE_SQUARE
3173                   /* The closing ">" in a template-argument-list.  */
3174                   && (next_token->type != CPP_GREATER
3175                       || parser->greater_than_is_operator_p)
3176                   /* C++0x only: A ">>" treated like two ">" tokens,
3177                      in a template-argument-list.  */
3178                   && (next_token->type != CPP_RSHIFT
3179                       || (cxx_dialect == cxx98)
3180                       || parser->greater_than_is_operator_p))
3181                 cast_p = false;
3182             }
3183
3184           /* If we are within a cast, then the constraint that the
3185              cast is to an integral or enumeration type will be
3186              checked at that point.  If we are not within a cast, then
3187              this code is invalid.  */
3188           if (!cast_p)
3189             cp_parser_non_integral_constant_expression
3190               (parser, "floating-point literal");
3191         }
3192       return token->u.value;
3193
3194     case CPP_STRING:
3195     case CPP_STRING16:
3196     case CPP_STRING32:
3197     case CPP_WSTRING:
3198       /* ??? Should wide strings be allowed when parser->translate_strings_p
3199          is false (i.e. in attributes)?  If not, we can kill the third
3200          argument to cp_parser_string_literal.  */
3201       return cp_parser_string_literal (parser,
3202                                        parser->translate_strings_p,
3203                                        true);
3204
3205     case CPP_OPEN_PAREN:
3206       {
3207         tree expr;
3208         bool saved_greater_than_is_operator_p;
3209
3210         /* Consume the `('.  */
3211         cp_lexer_consume_token (parser->lexer);
3212         /* Within a parenthesized expression, a `>' token is always
3213            the greater-than operator.  */
3214         saved_greater_than_is_operator_p
3215           = parser->greater_than_is_operator_p;
3216         parser->greater_than_is_operator_p = true;
3217         /* If we see `( { ' then we are looking at the beginning of
3218            a GNU statement-expression.  */
3219         if (cp_parser_allow_gnu_extensions_p (parser)
3220             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221           {
3222             /* Statement-expressions are not allowed by the standard.  */
3223             pedwarn (token->location, OPT_pedantic, 
3224                      "ISO C++ forbids braced-groups within expressions");
3225
3226             /* And they're not allowed outside of a function-body; you
3227                cannot, for example, write:
3228
3229                  int i = ({ int j = 3; j + 1; });
3230
3231                at class or namespace scope.  */
3232             if (!parser->in_function_body
3233                 || parser->in_template_argument_list_p)
3234               {
3235                 error ("%Hstatement-expressions are not allowed outside "
3236                        "functions nor in template-argument lists",
3237                        &token->location);
3238                 cp_parser_skip_to_end_of_block_or_statement (parser);
3239                 expr = error_mark_node;
3240               }
3241             else
3242               {
3243                 /* Start the statement-expression.  */
3244                 expr = begin_stmt_expr ();
3245                 /* Parse the compound-statement.  */
3246                 cp_parser_compound_statement (parser, expr, false);
3247                 /* Finish up.  */
3248                 expr = finish_stmt_expr (expr, false);
3249               }
3250           }
3251         else
3252           {
3253             /* Parse the parenthesized expression.  */
3254             expr = cp_parser_expression (parser, cast_p);
3255             /* Let the front end know that this expression was
3256                enclosed in parentheses. This matters in case, for
3257                example, the expression is of the form `A::B', since
3258                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259                not.  */
3260             finish_parenthesized_expr (expr);
3261           }
3262         /* The `>' token might be the end of a template-id or
3263            template-parameter-list now.  */
3264         parser->greater_than_is_operator_p
3265           = saved_greater_than_is_operator_p;
3266         /* Consume the `)'.  */
3267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268           cp_parser_skip_to_end_of_statement (parser);
3269
3270         return expr;
3271       }
3272
3273     case CPP_KEYWORD:
3274       switch (token->keyword)
3275         {
3276           /* These two are the boolean literals.  */
3277         case RID_TRUE:
3278           cp_lexer_consume_token (parser->lexer);
3279           return boolean_true_node;
3280         case RID_FALSE:
3281           cp_lexer_consume_token (parser->lexer);
3282           return boolean_false_node;
3283
3284           /* The `__null' literal.  */
3285         case RID_NULL:
3286           cp_lexer_consume_token (parser->lexer);
3287           return null_node;
3288
3289           /* Recognize the `this' keyword.  */
3290         case RID_THIS:
3291           cp_lexer_consume_token (parser->lexer);
3292           if (parser->local_variables_forbidden_p)
3293             {
3294               error ("%H%<this%> may not be used in this context",
3295                      &token->location);
3296               return error_mark_node;
3297             }
3298           /* Pointers cannot appear in constant-expressions.  */
3299           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300             return error_mark_node;
3301           return finish_this_expr ();
3302
3303           /* The `operator' keyword can be the beginning of an
3304              id-expression.  */
3305         case RID_OPERATOR:
3306           goto id_expression;
3307
3308         case RID_FUNCTION_NAME:
3309         case RID_PRETTY_FUNCTION_NAME:
3310         case RID_C99_FUNCTION_NAME:
3311           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312              __func__ are the names of variables -- but they are
3313              treated specially.  Therefore, they are handled here,
3314              rather than relying on the generic id-expression logic
3315              below.  Grammatically, these names are id-expressions.
3316
3317              Consume the token.  */
3318           token = cp_lexer_consume_token (parser->lexer);
3319           /* Look up the name.  */
3320           return finish_fname (token->u.value);
3321
3322         case RID_VA_ARG:
3323           {
3324             tree expression;
3325             tree type;
3326
3327             /* The `__builtin_va_arg' construct is used to handle
3328                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3329             cp_lexer_consume_token (parser->lexer);
3330             /* Look for the opening `('.  */
3331             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332             /* Now, parse the assignment-expression.  */
3333             expression = cp_parser_assignment_expression (parser,
3334                                                           /*cast_p=*/false);
3335             /* Look for the `,'.  */
3336             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337             /* Parse the type-id.  */
3338             type = cp_parser_type_id (parser);
3339             /* Look for the closing `)'.  */
3340             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341             /* Using `va_arg' in a constant-expression is not
3342                allowed.  */
3343             if (cp_parser_non_integral_constant_expression (parser,
3344                                                             "%<va_arg%>"))
3345               return error_mark_node;
3346             return build_x_va_arg (expression, type);
3347           }
3348
3349         case RID_OFFSETOF:
3350           return cp_parser_builtin_offsetof (parser);
3351
3352         case RID_HAS_NOTHROW_ASSIGN:
3353         case RID_HAS_NOTHROW_CONSTRUCTOR:
3354         case RID_HAS_NOTHROW_COPY:        
3355         case RID_HAS_TRIVIAL_ASSIGN:
3356         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357         case RID_HAS_TRIVIAL_COPY:        
3358         case RID_HAS_TRIVIAL_DESTRUCTOR:
3359         case RID_HAS_VIRTUAL_DESTRUCTOR:
3360         case RID_IS_ABSTRACT:
3361         case RID_IS_BASE_OF:
3362         case RID_IS_CLASS:
3363         case RID_IS_CONVERTIBLE_TO:
3364         case RID_IS_EMPTY:
3365         case RID_IS_ENUM:
3366         case RID_IS_POD:
3367         case RID_IS_POLYMORPHIC:
3368         case RID_IS_UNION:
3369           return cp_parser_trait_expr (parser, token->keyword);
3370
3371         /* Objective-C++ expressions.  */
3372         case RID_AT_ENCODE:
3373         case RID_AT_PROTOCOL:
3374         case RID_AT_SELECTOR:
3375           return cp_parser_objc_expression (parser);
3376
3377         default:
3378           cp_parser_error (parser, "expected primary-expression");
3379           return error_mark_node;
3380         }
3381
3382       /* An id-expression can start with either an identifier, a
3383          `::' as the beginning of a qualified-id, or the "operator"
3384          keyword.  */
3385     case CPP_NAME:
3386     case CPP_SCOPE:
3387     case CPP_TEMPLATE_ID:
3388     case CPP_NESTED_NAME_SPECIFIER:
3389       {
3390         tree id_expression;
3391         tree decl;
3392         const char *error_msg;
3393         bool template_p;
3394         bool done;
3395         cp_token *id_expr_token;
3396
3397       id_expression:
3398         /* Parse the id-expression.  */
3399         id_expression
3400           = cp_parser_id_expression (parser,
3401                                      /*template_keyword_p=*/false,
3402                                      /*check_dependency_p=*/true,
3403                                      &template_p,
3404                                      /*declarator_p=*/false,
3405                                      /*optional_p=*/false);
3406         if (id_expression == error_mark_node)
3407           return error_mark_node;
3408         id_expr_token = token;
3409         token = cp_lexer_peek_token (parser->lexer);
3410         done = (token->type != CPP_OPEN_SQUARE
3411                 && token->type != CPP_OPEN_PAREN
3412                 && token->type != CPP_DOT
3413                 && token->type != CPP_DEREF
3414                 && token->type != CPP_PLUS_PLUS
3415                 && token->type != CPP_MINUS_MINUS);
3416         /* If we have a template-id, then no further lookup is
3417            required.  If the template-id was for a template-class, we
3418            will sometimes have a TYPE_DECL at this point.  */
3419         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420                  || TREE_CODE (id_expression) == TYPE_DECL)
3421           decl = id_expression;
3422         /* Look up the name.  */
3423         else
3424           {
3425             tree ambiguous_decls;
3426
3427             decl = cp_parser_lookup_name (parser, id_expression,
3428                                           none_type,
3429                                           template_p,
3430                                           /*is_namespace=*/false,
3431                                           /*check_dependency=*/true,
3432                                           &ambiguous_decls,
3433                                           id_expr_token->location);
3434             /* If the lookup was ambiguous, an error will already have
3435                been issued.  */
3436             if (ambiguous_decls)
3437               return error_mark_node;
3438
3439             /* In Objective-C++, an instance variable (ivar) may be preferred
3440                to whatever cp_parser_lookup_name() found.  */
3441             decl = objc_lookup_ivar (decl, id_expression);
3442
3443             /* If name lookup gives us a SCOPE_REF, then the
3444                qualifying scope was dependent.  */
3445             if (TREE_CODE (decl) == SCOPE_REF)
3446               {
3447                 /* At this point, we do not know if DECL is a valid
3448                    integral constant expression.  We assume that it is
3449                    in fact such an expression, so that code like:
3450
3451                       template <int N> struct A {
3452                         int a[B<N>::i];
3453                       };
3454                      
3455                    is accepted.  At template-instantiation time, we
3456                    will check that B<N>::i is actually a constant.  */
3457                 return decl;
3458               }
3459             /* Check to see if DECL is a local variable in a context
3460                where that is forbidden.  */
3461             if (parser->local_variables_forbidden_p
3462                 && local_variable_p (decl))
3463               {
3464                 /* It might be that we only found DECL because we are
3465                    trying to be generous with pre-ISO scoping rules.
3466                    For example, consider:
3467
3468                      int i;
3469                      void g() {
3470                        for (int i = 0; i < 10; ++i) {}
3471                        extern void f(int j = i);
3472                      }
3473
3474                    Here, name look up will originally find the out
3475                    of scope `i'.  We need to issue a warning message,
3476                    but then use the global `i'.  */
3477                 decl = check_for_out_of_scope_variable (decl);
3478                 if (local_variable_p (decl))
3479                   {
3480                     error ("%Hlocal variable %qD may not appear in this context",
3481                            &id_expr_token->location, decl);
3482                     return error_mark_node;
3483                   }
3484               }
3485           }
3486
3487         decl = (finish_id_expression
3488                 (id_expression, decl, parser->scope,
3489                  idk,
3490                  parser->integral_constant_expression_p,
3491                  parser->allow_non_integral_constant_expression_p,
3492                  &parser->non_integral_constant_expression_p,
3493                  template_p, done, address_p,
3494                  template_arg_p,
3495                  &error_msg,
3496                  id_expr_token->location));
3497         if (error_msg)
3498           cp_parser_error (parser, error_msg);
3499         return decl;
3500       }
3501
3502       /* Anything else is an error.  */
3503     default:
3504       /* ...unless we have an Objective-C++ message or string literal,
3505          that is.  */
3506       if (c_dialect_objc ()
3507           && (token->type == CPP_OPEN_SQUARE
3508               || token->type == CPP_OBJC_STRING))
3509         return cp_parser_objc_expression (parser);
3510
3511       cp_parser_error (parser, "expected primary-expression");
3512       return error_mark_node;
3513     }
3514 }
3515
3516 /* Parse an id-expression.
3517
3518    id-expression:
3519      unqualified-id
3520      qualified-id
3521
3522    qualified-id:
3523      :: [opt] nested-name-specifier template [opt] unqualified-id
3524      :: identifier
3525      :: operator-function-id
3526      :: template-id
3527
3528    Return a representation of the unqualified portion of the
3529    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3530    a `::' or nested-name-specifier.
3531
3532    Often, if the id-expression was a qualified-id, the caller will
3533    want to make a SCOPE_REF to represent the qualified-id.  This
3534    function does not do this in order to avoid wastefully creating
3535    SCOPE_REFs when they are not required.
3536
3537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538    `template' keyword.
3539
3540    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541    uninstantiated templates.
3542
3543    If *TEMPLATE_P is non-NULL, it is set to true iff the
3544    `template' keyword is used to explicitly indicate that the entity
3545    named is a template.
3546
3547    If DECLARATOR_P is true, the id-expression is appearing as part of
3548    a declarator, rather than as part of an expression.  */
3549
3550 static tree
3551 cp_parser_id_expression (cp_parser *parser,
3552                          bool template_keyword_p,
3553                          bool check_dependency_p,
3554                          bool *template_p,
3555                          bool declarator_p,
3556                          bool optional_p)
3557 {
3558   bool global_scope_p;
3559   bool nested_name_specifier_p;
3560
3561   /* Assume the `template' keyword was not used.  */
3562   if (template_p)
3563     *template_p = template_keyword_p;
3564
3565   /* Look for the optional `::' operator.  */
3566   global_scope_p
3567     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3568        != NULL_TREE);
3569   /* Look for the optional nested-name-specifier.  */
3570   nested_name_specifier_p
3571     = (cp_parser_nested_name_specifier_opt (parser,
3572                                             /*typename_keyword_p=*/false,
3573                                             check_dependency_p,
3574                                             /*type_p=*/false,
3575                                             declarator_p)
3576        != NULL_TREE);
3577   /* If there is a nested-name-specifier, then we are looking at
3578      the first qualified-id production.  */
3579   if (nested_name_specifier_p)
3580     {
3581       tree saved_scope;
3582       tree saved_object_scope;
3583       tree saved_qualifying_scope;
3584       tree unqualified_id;
3585       bool is_template;
3586
3587       /* See if the next token is the `template' keyword.  */
3588       if (!template_p)
3589         template_p = &is_template;
3590       *template_p = cp_parser_optional_template_keyword (parser);
3591       /* Name lookup we do during the processing of the
3592          unqualified-id might obliterate SCOPE.  */
3593       saved_scope = parser->scope;
3594       saved_object_scope = parser->object_scope;
3595       saved_qualifying_scope = parser->qualifying_scope;
3596       /* Process the final unqualified-id.  */
3597       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598                                                  check_dependency_p,
3599                                                  declarator_p,
3600                                                  /*optional_p=*/false);
3601       /* Restore the SAVED_SCOPE for our caller.  */
3602       parser->scope = saved_scope;
3603       parser->object_scope = saved_object_scope;
3604       parser->qualifying_scope = saved_qualifying_scope;
3605
3606       return unqualified_id;
3607     }
3608   /* Otherwise, if we are in global scope, then we are looking at one
3609      of the other qualified-id productions.  */
3610   else if (global_scope_p)
3611     {
3612       cp_token *token;
3613       tree id;
3614
3615       /* Peek at the next token.  */
3616       token = cp_lexer_peek_token (parser->lexer);
3617
3618       /* If it's an identifier, and the next token is not a "<", then
3619          we can avoid the template-id case.  This is an optimization
3620          for this common case.  */
3621       if (token->type == CPP_NAME
3622           && !cp_parser_nth_token_starts_template_argument_list_p
3623                (parser, 2))
3624         return cp_parser_identifier (parser);
3625
3626       cp_parser_parse_tentatively (parser);
3627       /* Try a template-id.  */
3628       id = cp_parser_template_id (parser,
3629                                   /*template_keyword_p=*/false,
3630                                   /*check_dependency_p=*/true,
3631                                   declarator_p);
3632       /* If that worked, we're done.  */
3633       if (cp_parser_parse_definitely (parser))
3634         return id;
3635
3636       /* Peek at the next token.  (Changes in the token buffer may
3637          have invalidated the pointer obtained above.)  */
3638       token = cp_lexer_peek_token (parser->lexer);
3639
3640       switch (token->type)
3641         {
3642         case CPP_NAME:
3643           return cp_parser_identifier (parser);
3644
3645         case CPP_KEYWORD:
3646           if (token->keyword == RID_OPERATOR)
3647             return cp_parser_operator_function_id (parser);
3648           /* Fall through.  */
3649
3650         default:
3651           cp_parser_error (parser, "expected id-expression");
3652           return error_mark_node;
3653         }
3654     }
3655   else
3656     return cp_parser_unqualified_id (parser, template_keyword_p,
3657                                      /*check_dependency_p=*/true,
3658                                      declarator_p,
3659                                      optional_p);
3660 }
3661
3662 /* Parse an unqualified-id.
3663
3664    unqualified-id:
3665      identifier
3666      operator-function-id
3667      conversion-function-id
3668      ~ class-name
3669      template-id
3670
3671    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672    keyword, in a construct like `A::template ...'.
3673
3674    Returns a representation of unqualified-id.  For the `identifier'
3675    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3676    production a BIT_NOT_EXPR is returned; the operand of the
3677    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3678    other productions, see the documentation accompanying the
3679    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3680    names are looked up in uninstantiated templates.  If DECLARATOR_P
3681    is true, the unqualified-id is appearing as part of a declarator,
3682    rather than as part of an expression.  */
3683
3684 static tree
3685 cp_parser_unqualified_id (cp_parser* parser,
3686                           bool template_keyword_p,
3687                           bool check_dependency_p,
3688                           bool declarator_p,
3689                           bool optional_p)
3690 {
3691   cp_token *token;
3692
3693   /* Peek at the next token.  */
3694   token = cp_lexer_peek_token (parser->lexer);
3695
3696   switch (token->type)
3697     {
3698     case CPP_NAME:
3699       {
3700         tree id;
3701
3702         /* We don't know yet whether or not this will be a
3703            template-id.  */
3704         cp_parser_parse_tentatively (parser);
3705         /* Try a template-id.  */
3706         id = cp_parser_template_id (parser, template_keyword_p,
3707                                     check_dependency_p,
3708                                     declarator_p);
3709         /* If it worked, we're done.  */
3710         if (cp_parser_parse_definitely (parser))
3711           return id;
3712         /* Otherwise, it's an ordinary identifier.  */
3713         return cp_parser_identifier (parser);
3714       }
3715
3716     case CPP_TEMPLATE_ID:
3717       return cp_parser_template_id (parser, template_keyword_p,
3718                                     check_dependency_p,
3719                                     declarator_p);
3720
3721     case CPP_COMPL:
3722       {
3723         tree type_decl;
3724         tree qualifying_scope;
3725         tree object_scope;
3726         tree scope;
3727         bool done;
3728
3729         /* Consume the `~' token.  */
3730         cp_lexer_consume_token (parser->lexer);
3731         /* Parse the class-name.  The standard, as written, seems to
3732            say that:
3733
3734              template <typename T> struct S { ~S (); };
3735              template <typename T> S<T>::~S() {}
3736
3737            is invalid, since `~' must be followed by a class-name, but
3738            `S<T>' is dependent, and so not known to be a class.
3739            That's not right; we need to look in uninstantiated
3740            templates.  A further complication arises from:
3741
3742              template <typename T> void f(T t) {
3743                t.T::~T();
3744              }
3745
3746            Here, it is not possible to look up `T' in the scope of `T'
3747            itself.  We must look in both the current scope, and the
3748            scope of the containing complete expression.
3749
3750            Yet another issue is:
3751
3752              struct S {
3753                int S;
3754                ~S();
3755              };
3756
3757              S::~S() {}
3758
3759            The standard does not seem to say that the `S' in `~S'
3760            should refer to the type `S' and not the data member
3761            `S::S'.  */
3762
3763         /* DR 244 says that we look up the name after the "~" in the
3764            same scope as we looked up the qualifying name.  That idea
3765            isn't fully worked out; it's more complicated than that.  */
3766         scope = parser->scope;
3767         object_scope = parser->object_scope;
3768         qualifying_scope = parser->qualifying_scope;
3769
3770         /* Check for invalid scopes.  */
3771         if (scope == error_mark_node)
3772           {
3773             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774               cp_lexer_consume_token (parser->lexer);
3775             return error_mark_node;
3776           }
3777         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3778           {
3779             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780               error ("%Hscope %qT before %<~%> is not a class-name",
3781                      &token->location, scope);
3782             cp_parser_simulate_error (parser);
3783             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784               cp_lexer_consume_token (parser->lexer);
3785             return error_mark_node;
3786           }
3787         gcc_assert (!scope || TYPE_P (scope));
3788
3789         /* If the name is of the form "X::~X" it's OK.  */
3790         token = cp_lexer_peek_token (parser->lexer);
3791         if (scope
3792             && token->type == CPP_NAME
3793             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3794                 == CPP_OPEN_PAREN)
3795             && constructor_name_p (token->u.value, scope))
3796           {
3797             cp_lexer_consume_token (parser->lexer);
3798             return build_nt (BIT_NOT_EXPR, scope);
3799           }
3800
3801         /* If there was an explicit qualification (S::~T), first look
3802            in the scope given by the qualification (i.e., S).  */
3803         done = false;
3804         type_decl = NULL_TREE;
3805         if (scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             type_decl = cp_parser_class_name (parser,
3809                                               /*typename_keyword_p=*/false,
3810                                               /*template_keyword_p=*/false,
3811                                               none_type,
3812                                               /*check_dependency=*/false,
3813                                               /*class_head_p=*/false,
3814                                               declarator_p);
3815             if (cp_parser_parse_definitely (parser))
3816               done = true;
3817           }
3818         /* In "N::S::~S", look in "N" as well.  */
3819         if (!done && scope && qualifying_scope)
3820           {
3821             cp_parser_parse_tentatively (parser);
3822             parser->scope = qualifying_scope;
3823             parser->object_scope = NULL_TREE;
3824             parser->qualifying_scope = NULL_TREE;
3825             type_decl
3826               = cp_parser_class_name (parser,
3827                                       /*typename_keyword_p=*/false,
3828                                       /*template_keyword_p=*/false,
3829                                       none_type,
3830                                       /*check_dependency=*/false,
3831                                       /*class_head_p=*/false,
3832                                       declarator_p);
3833             if (cp_parser_parse_definitely (parser))
3834               done = true;
3835           }
3836         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3837         else if (!done && object_scope)
3838           {
3839             cp_parser_parse_tentatively (parser);
3840             parser->scope = object_scope;
3841             parser->object_scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             type_decl
3844               = cp_parser_class_name (parser,
3845                                       /*typename_keyword_p=*/false,
3846                                       /*template_keyword_p=*/false,
3847                                       none_type,
3848                                       /*check_dependency=*/false,
3849                                       /*class_head_p=*/false,
3850                                       declarator_p);
3851             if (cp_parser_parse_definitely (parser))
3852               done = true;
3853           }
3854         /* Look in the surrounding context.  */
3855         if (!done)
3856           {
3857             parser->scope = NULL_TREE;
3858             parser->object_scope = NULL_TREE;
3859             parser->qualifying_scope = NULL_TREE;
3860             type_decl
3861               = cp_parser_class_name (parser,
3862                                       /*typename_keyword_p=*/false,
3863                                       /*template_keyword_p=*/false,
3864                                       none_type,
3865                                       /*check_dependency=*/false,
3866                                       /*class_head_p=*/false,
3867                                       declarator_p);
3868           }
3869         /* If an error occurred, assume that the name of the
3870            destructor is the same as the name of the qualifying
3871            class.  That allows us to keep parsing after running
3872            into ill-formed destructor names.  */
3873         if (type_decl == error_mark_node && scope)
3874           return build_nt (BIT_NOT_EXPR, scope);
3875         else if (type_decl == error_mark_node)
3876           return error_mark_node;
3877
3878         /* Check that destructor name and scope match.  */
3879         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3880           {
3881             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882               error ("%Hdeclaration of %<~%T%> as member of %qT",
3883                      &token->location, type_decl, scope);
3884             cp_parser_simulate_error (parser);
3885             return error_mark_node;
3886           }
3887
3888         /* [class.dtor]
3889
3890            A typedef-name that names a class shall not be used as the
3891            identifier in the declarator for a destructor declaration.  */
3892         if (declarator_p
3893             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894             && !DECL_SELF_REFERENCE_P (type_decl)
3895             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896           error ("%Htypedef-name %qD used as destructor declarator",
3897                  &token->location, type_decl);
3898
3899         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3900       }
3901
3902     case CPP_KEYWORD:
3903       if (token->keyword == RID_OPERATOR)
3904         {
3905           tree id;
3906
3907           /* This could be a template-id, so we try that first.  */
3908           cp_parser_parse_tentatively (parser);
3909           /* Try a template-id.  */
3910           id = cp_parser_template_id (parser, template_keyword_p,
3911                                       /*check_dependency_p=*/true,
3912                                       declarator_p);
3913           /* If that worked, we're done.  */
3914           if (cp_parser_parse_definitely (parser))
3915             return id;
3916           /* We still don't know whether we're looking at an
3917              operator-function-id or a conversion-function-id.  */
3918           cp_parser_parse_tentatively (parser);
3919           /* Try an operator-function-id.  */
3920           id = cp_parser_operator_function_id (parser);
3921           /* If that didn't work, try a conversion-function-id.  */
3922           if (!cp_parser_parse_definitely (parser))
3923             id = cp_parser_conversion_function_id (parser);
3924
3925           return id;
3926         }
3927       /* Fall through.  */
3928
3929     default:
3930       if (optional_p)
3931         return NULL_TREE;
3932       cp_parser_error (parser, "expected unqualified-id");
3933       return error_mark_node;
3934     }
3935 }
3936
3937 /* Parse an (optional) nested-name-specifier.
3938
3939    nested-name-specifier: [C++98]
3940      class-or-namespace-name :: nested-name-specifier [opt]
3941      class-or-namespace-name :: template nested-name-specifier [opt]
3942
3943    nested-name-specifier: [C++0x]
3944      type-name ::
3945      namespace-name ::
3946      nested-name-specifier identifier ::
3947      nested-name-specifier template [opt] simple-template-id ::
3948
3949    PARSER->SCOPE should be set appropriately before this function is
3950    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3951    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3952    in name lookups.
3953
3954    Sets PARSER->SCOPE to the class (TYPE) or namespace
3955    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3956    it unchanged if there is no nested-name-specifier.  Returns the new
3957    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3958
3959    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3960    part of a declaration and/or decl-specifier.  */
3961
3962 static tree
3963 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3964                                      bool typename_keyword_p,
3965                                      bool check_dependency_p,
3966                                      bool type_p,
3967                                      bool is_declaration)
3968 {
3969   bool success = false;
3970   cp_token_position start = 0;
3971   cp_token *token;
3972
3973   /* Remember where the nested-name-specifier starts.  */
3974   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3975     {
3976       start = cp_lexer_token_position (parser->lexer, false);
3977       push_deferring_access_checks (dk_deferred);
3978     }
3979
3980   while (true)
3981     {
3982       tree new_scope;
3983       tree old_scope;
3984       tree saved_qualifying_scope;
3985       bool template_keyword_p;
3986
3987       /* Spot cases that cannot be the beginning of a
3988          nested-name-specifier.  */
3989       token = cp_lexer_peek_token (parser->lexer);
3990
3991       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3992          the already parsed nested-name-specifier.  */
3993       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3994         {
3995           /* Grab the nested-name-specifier and continue the loop.  */
3996           cp_parser_pre_parsed_nested_name_specifier (parser);
3997           /* If we originally encountered this nested-name-specifier
3998              with IS_DECLARATION set to false, we will not have
3999              resolved TYPENAME_TYPEs, so we must do so here.  */
4000           if (is_declaration
4001               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4002             {
4003               new_scope = resolve_typename_type (parser->scope,
4004                                                  /*only_current_p=*/false);
4005               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4006                 parser->scope = new_scope;
4007             }
4008           success = true;
4009           continue;
4010         }
4011
4012       /* Spot cases that cannot be the beginning of a
4013          nested-name-specifier.  On the second and subsequent times
4014          through the loop, we look for the `template' keyword.  */
4015       if (success && token->keyword == RID_TEMPLATE)
4016         ;
4017       /* A template-id can start a nested-name-specifier.  */
4018       else if (token->type == CPP_TEMPLATE_ID)
4019         ;
4020       else
4021         {
4022           /* If the next token is not an identifier, then it is
4023              definitely not a type-name or namespace-name.  */
4024           if (token->type != CPP_NAME)
4025             break;
4026           /* If the following token is neither a `<' (to begin a
4027              template-id), nor a `::', then we are not looking at a
4028              nested-name-specifier.  */
4029           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4030           if (token->type != CPP_SCOPE
4031               && !cp_parser_nth_token_starts_template_argument_list_p
4032                   (parser, 2))
4033             break;
4034         }
4035
4036       /* The nested-name-specifier is optional, so we parse
4037          tentatively.  */
4038       cp_parser_parse_tentatively (parser);
4039
4040       /* Look for the optional `template' keyword, if this isn't the
4041          first time through the loop.  */
4042       if (success)
4043         template_keyword_p = cp_parser_optional_template_keyword (parser);
4044       else
4045         template_keyword_p = false;
4046
4047       /* Save the old scope since the name lookup we are about to do
4048          might destroy it.  */
4049       old_scope = parser->scope;
4050       saved_qualifying_scope = parser->qualifying_scope;
4051       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4052          look up names in "X<T>::I" in order to determine that "Y" is
4053          a template.  So, if we have a typename at this point, we make
4054          an effort to look through it.  */
4055       if (is_declaration
4056           && !typename_keyword_p
4057           && parser->scope
4058           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059         parser->scope = resolve_typename_type (parser->scope,
4060                                                /*only_current_p=*/false);
4061       /* Parse the qualifying entity.  */
4062       new_scope
4063         = cp_parser_qualifying_entity (parser,
4064                                        typename_keyword_p,
4065                                        template_keyword_p,
4066                                        check_dependency_p,
4067                                        type_p,
4068                                        is_declaration);
4069       /* Look for the `::' token.  */
4070       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4071
4072       /* If we found what we wanted, we keep going; otherwise, we're
4073          done.  */
4074       if (!cp_parser_parse_definitely (parser))
4075         {
4076           bool error_p = false;
4077
4078           /* Restore the OLD_SCOPE since it was valid before the
4079              failed attempt at finding the last
4080              class-or-namespace-name.  */
4081           parser->scope = old_scope;
4082           parser->qualifying_scope = saved_qualifying_scope;
4083           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4084             break;
4085           /* If the next token is an identifier, and the one after
4086              that is a `::', then any valid interpretation would have
4087              found a class-or-namespace-name.  */
4088           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4089                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4090                      == CPP_SCOPE)
4091                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4092                      != CPP_COMPL))
4093             {
4094               token = cp_lexer_consume_token (parser->lexer);
4095               if (!error_p)
4096                 {
4097                   if (!token->ambiguous_p)
4098                     {
4099                       tree decl;
4100                       tree ambiguous_decls;
4101
4102                       decl = cp_parser_lookup_name (parser, token->u.value,
4103                                                     none_type,
4104                                                     /*is_template=*/false,
4105                                                     /*is_namespace=*/false,
4106                                                     /*check_dependency=*/true,
4107                                                     &ambiguous_decls,
4108                                                     token->location);
4109                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4110                         error ("%H%qD used without template parameters",
4111                                &token->location, decl);
4112                       else if (ambiguous_decls)
4113                         {
4114                           error ("%Hreference to %qD is ambiguous",
4115                                  &token->location, token->u.value);
4116                           print_candidates (ambiguous_decls);
4117                           decl = error_mark_node;
4118                         }
4119                       else
4120                         {
4121                           const char* msg = "is not a class or namespace";
4122                           if (cxx_dialect != cxx98)
4123                             msg = "is not a class, namespace, or enumeration";
4124                           cp_parser_name_lookup_error
4125                             (parser, token->u.value, decl, msg,
4126                              token->location);
4127                         }
4128                     }
4129                   parser->scope = error_mark_node;
4130                   error_p = true;
4131                   /* Treat this as a successful nested-name-specifier
4132                      due to:
4133
4134                      [basic.lookup.qual]
4135
4136                      If the name found is not a class-name (clause
4137                      _class_) or namespace-name (_namespace.def_), the
4138                      program is ill-formed.  */
4139                   success = true;
4140                 }
4141               cp_lexer_consume_token (parser->lexer);
4142             }
4143           break;
4144         }
4145       /* We've found one valid nested-name-specifier.  */
4146       success = true;
4147       /* Name lookup always gives us a DECL.  */
4148       if (TREE_CODE (new_scope) == TYPE_DECL)
4149         new_scope = TREE_TYPE (new_scope);
4150       /* Uses of "template" must be followed by actual templates.  */
4151       if (template_keyword_p
4152           && !(CLASS_TYPE_P (new_scope)
4153                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4154                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4155                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4156           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4157                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4158                    == TEMPLATE_ID_EXPR)))
4159         permerror (input_location, TYPE_P (new_scope)
4160                    ? "%qT is not a template"
4161                    : "%qD is not a template",
4162                    new_scope);
4163       /* If it is a class scope, try to complete it; we are about to
4164          be looking up names inside the class.  */
4165       if (TYPE_P (new_scope)
4166           /* Since checking types for dependency can be expensive,
4167              avoid doing it if the type is already complete.  */
4168           && !COMPLETE_TYPE_P (new_scope)
4169           /* Do not try to complete dependent types.  */
4170           && !dependent_type_p (new_scope))
4171         {
4172           new_scope = complete_type (new_scope);
4173           /* If it is a typedef to current class, use the current
4174              class instead, as the typedef won't have any names inside
4175              it yet.  */
4176           if (!COMPLETE_TYPE_P (new_scope)
4177               && currently_open_class (new_scope))
4178             new_scope = TYPE_MAIN_VARIANT (new_scope);
4179         }
4180       /* Make sure we look in the right scope the next time through
4181          the loop.  */
4182       parser->scope = new_scope;
4183     }
4184
4185   /* If parsing tentatively, replace the sequence of tokens that makes
4186      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4187      token.  That way, should we re-parse the token stream, we will
4188      not have to repeat the effort required to do the parse, nor will
4189      we issue duplicate error messages.  */
4190   if (success && start)
4191     {
4192       cp_token *token;
4193
4194       token = cp_lexer_token_at (parser->lexer, start);
4195       /* Reset the contents of the START token.  */
4196       token->type = CPP_NESTED_NAME_SPECIFIER;
4197       /* Retrieve any deferred checks.  Do not pop this access checks yet
4198          so the memory will not be reclaimed during token replacing below.  */
4199       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4200       token->u.tree_check_value->value = parser->scope;
4201       token->u.tree_check_value->checks = get_deferred_access_checks ();
4202       token->u.tree_check_value->qualifying_scope =
4203         parser->qualifying_scope;
4204       token->keyword = RID_MAX;
4205
4206       /* Purge all subsequent tokens.  */
4207       cp_lexer_purge_tokens_after (parser->lexer, start);
4208     }
4209
4210   if (start)
4211     pop_to_parent_deferring_access_checks ();
4212
4213   return success ? parser->scope : NULL_TREE;
4214 }
4215
4216 /* Parse a nested-name-specifier.  See
4217    cp_parser_nested_name_specifier_opt for details.  This function
4218    behaves identically, except that it will an issue an error if no
4219    nested-name-specifier is present.  */
4220
4221 static tree
4222 cp_parser_nested_name_specifier (cp_parser *parser,
4223                                  bool typename_keyword_p,
4224                                  bool check_dependency_p,
4225                                  bool type_p,
4226                                  bool is_declaration)
4227 {
4228   tree scope;
4229
4230   /* Look for the nested-name-specifier.  */
4231   scope = cp_parser_nested_name_specifier_opt (parser,
4232                                                typename_keyword_p,
4233                                                check_dependency_p,
4234                                                type_p,
4235                                                is_declaration);
4236   /* If it was not present, issue an error message.  */
4237   if (!scope)
4238     {
4239       cp_parser_error (parser, "expected nested-name-specifier");
4240       parser->scope = NULL_TREE;
4241     }
4242
4243   return scope;
4244 }
4245
4246 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4247    this is either a class-name or a namespace-name (which corresponds
4248    to the class-or-namespace-name production in the grammar). For
4249    C++0x, it can also be a type-name that refers to an enumeration
4250    type.
4251
4252    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4253    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4254    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4255    TYPE_P is TRUE iff the next name should be taken as a class-name,
4256    even the same name is declared to be another entity in the same
4257    scope.
4258
4259    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4260    specified by the class-or-namespace-name.  If neither is found the
4261    ERROR_MARK_NODE is returned.  */
4262
4263 static tree
4264 cp_parser_qualifying_entity (cp_parser *parser,
4265                              bool typename_keyword_p,
4266                              bool template_keyword_p,
4267                              bool check_dependency_p,
4268                              bool type_p,
4269                              bool is_declaration)
4270 {
4271   tree saved_scope;
4272   tree saved_qualifying_scope;
4273   tree saved_object_scope;
4274   tree scope;
4275   bool only_class_p;
4276   bool successful_parse_p;
4277
4278   /* Before we try to parse the class-name, we must save away the
4279      current PARSER->SCOPE since cp_parser_class_name will destroy
4280      it.  */
4281   saved_scope = parser->scope;
4282   saved_qualifying_scope = parser->qualifying_scope;
4283   saved_object_scope = parser->object_scope;
4284   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4285      there is no need to look for a namespace-name.  */
4286   only_class_p = template_keyword_p 
4287     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4288   if (!only_class_p)
4289     cp_parser_parse_tentatively (parser);
4290   scope = cp_parser_class_name (parser,
4291                                 typename_keyword_p,
4292                                 template_keyword_p,
4293                                 type_p ? class_type : none_type,
4294                                 check_dependency_p,
4295                                 /*class_head_p=*/false,
4296                                 is_declaration);
4297   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4298   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4299   if (!only_class_p 
4300       && cxx_dialect != cxx98
4301       && !successful_parse_p)
4302     {
4303       /* Restore the saved scope.  */
4304       parser->scope = saved_scope;
4305       parser->qualifying_scope = saved_qualifying_scope;
4306       parser->object_scope = saved_object_scope;
4307
4308       /* Parse tentatively.  */
4309       cp_parser_parse_tentatively (parser);
4310      
4311       /* Parse a typedef-name or enum-name.  */
4312       scope = cp_parser_nonclass_name (parser);
4313       successful_parse_p = cp_parser_parse_definitely (parser);
4314     }
4315   /* If that didn't work, try for a namespace-name.  */
4316   if (!only_class_p && !successful_parse_p)
4317     {
4318       /* Restore the saved scope.  */
4319       parser->scope = saved_scope;
4320       parser->qualifying_scope = saved_qualifying_scope;
4321       parser->object_scope = saved_object_scope;
4322       /* If we are not looking at an identifier followed by the scope
4323          resolution operator, then this is not part of a
4324          nested-name-specifier.  (Note that this function is only used
4325          to parse the components of a nested-name-specifier.)  */
4326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4327           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4328         return error_mark_node;
4329       scope = cp_parser_namespace_name (parser);
4330     }
4331
4332   return scope;
4333 }
4334
4335 /* Parse a postfix-expression.
4336
4337    postfix-expression:
4338      primary-expression
4339      postfix-expression [ expression ]
4340      postfix-expression ( expression-list [opt] )
4341      simple-type-specifier ( expression-list [opt] )
4342      typename :: [opt] nested-name-specifier identifier
4343        ( expression-list [opt] )
4344      typename :: [opt] nested-name-specifier template [opt] template-id
4345        ( expression-list [opt] )
4346      postfix-expression . template [opt] id-expression
4347      postfix-expression -> template [opt] id-expression
4348      postfix-expression . pseudo-destructor-name
4349      postfix-expression -> pseudo-destructor-name
4350      postfix-expression ++
4351      postfix-expression --
4352      dynamic_cast < type-id > ( expression )
4353      static_cast < type-id > ( expression )
4354      reinterpret_cast < type-id > ( expression )
4355      const_cast < type-id > ( expression )
4356      typeid ( expression )
4357      typeid ( type-id )
4358
4359    GNU Extension:
4360
4361    postfix-expression:
4362      ( type-id ) { initializer-list , [opt] }
4363
4364    This extension is a GNU version of the C99 compound-literal
4365    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4366    but they are essentially the same concept.)
4367
4368    If ADDRESS_P is true, the postfix expression is the operand of the
4369    `&' operator.  CAST_P is true if this expression is the target of a
4370    cast.
4371
4372    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4373    class member access expressions [expr.ref].
4374
4375    Returns a representation of the expression.  */
4376
4377 static tree
4378 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4379                               bool member_access_only_p)
4380 {
4381   cp_token *token;
4382   enum rid keyword;
4383   cp_id_kind idk = CP_ID_KIND_NONE;
4384   tree postfix_expression = NULL_TREE;
4385   bool is_member_access = false;
4386
4387   /* Peek at the next token.  */
4388   token = cp_lexer_peek_token (parser->lexer);
4389   /* Some of the productions are determined by keywords.  */
4390   keyword = token->keyword;
4391   switch (keyword)
4392     {
4393     case RID_DYNCAST:
4394     case RID_STATCAST:
4395     case RID_REINTCAST:
4396     case RID_CONSTCAST:
4397       {
4398         tree type;
4399         tree expression;
4400         const char *saved_message;
4401
4402         /* All of these can be handled in the same way from the point
4403            of view of parsing.  Begin by consuming the token
4404            identifying the cast.  */
4405         cp_lexer_consume_token (parser->lexer);
4406
4407         /* New types cannot be defined in the cast.  */
4408         saved_message = parser->type_definition_forbidden_message;
4409         parser->type_definition_forbidden_message
4410           = "types may not be defined in casts";
4411
4412         /* Look for the opening `<'.  */
4413         cp_parser_require (parser, CPP_LESS, "%<<%>");
4414         /* Parse the type to which we are casting.  */
4415         type = cp_parser_type_id (parser);
4416         /* Look for the closing `>'.  */
4417         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4418         /* Restore the old message.  */
4419         parser->type_definition_forbidden_message = saved_message;
4420
4421         /* And the expression which is being cast.  */
4422         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4423         expression = cp_parser_expression (parser, /*cast_p=*/true);
4424         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4425
4426         /* Only type conversions to integral or enumeration types
4427            can be used in constant-expressions.  */
4428         if (!cast_valid_in_integral_constant_expression_p (type)
4429             && (cp_parser_non_integral_constant_expression
4430                 (parser,
4431                  "a cast to a type other than an integral or "
4432                  "enumeration type")))
4433           return error_mark_node;
4434
4435         switch (keyword)
4436           {
4437           case RID_DYNCAST:
4438             postfix_expression
4439               = build_dynamic_cast (type, expression, tf_warning_or_error);
4440             break;
4441           case RID_STATCAST:
4442             postfix_expression
4443               = build_static_cast (type, expression, tf_warning_or_error);
4444             break;
4445           case RID_REINTCAST:
4446             postfix_expression
4447               = build_reinterpret_cast (type, expression, 
4448                                         tf_warning_or_error);
4449             break;
4450           case RID_CONSTCAST:
4451             postfix_expression
4452               = build_const_cast (type, expression, tf_warning_or_error);
4453             break;
4454           default:
4455             gcc_unreachable ();
4456           }
4457       }
4458       break;
4459
4460     case RID_TYPEID:
4461       {
4462         tree type;
4463         const char *saved_message;
4464         bool saved_in_type_id_in_expr_p;
4465
4466         /* Consume the `typeid' token.  */
4467         cp_lexer_consume_token (parser->lexer);
4468         /* Look for the `(' token.  */
4469         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470         /* Types cannot be defined in a `typeid' expression.  */
4471         saved_message = parser->type_definition_forbidden_message;
4472         parser->type_definition_forbidden_message
4473           = "types may not be defined in a %<typeid%> expression";
4474         /* We can't be sure yet whether we're looking at a type-id or an
4475            expression.  */
4476         cp_parser_parse_tentatively (parser);
4477         /* Try a type-id first.  */
4478         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4479         parser->in_type_id_in_expr_p = true;
4480         type = cp_parser_type_id (parser);
4481         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4482         /* Look for the `)' token.  Otherwise, we can't be sure that
4483            we're not looking at an expression: consider `typeid (int
4484            (3))', for example.  */
4485         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4486         /* If all went well, simply lookup the type-id.  */
4487         if (cp_parser_parse_definitely (parser))
4488           postfix_expression = get_typeid (type);
4489         /* Otherwise, fall back to the expression variant.  */
4490         else
4491           {
4492             tree expression;
4493
4494             /* Look for an expression.  */
4495             expression = cp_parser_expression (parser, /*cast_p=*/false);
4496             /* Compute its typeid.  */
4497             postfix_expression = build_typeid (expression);
4498             /* Look for the `)' token.  */
4499             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4500           }
4501         /* Restore the saved message.  */
4502         parser->type_definition_forbidden_message = saved_message;
4503         /* `typeid' may not appear in an integral constant expression.  */
4504         if (cp_parser_non_integral_constant_expression(parser,
4505                                                        "%<typeid%> operator"))
4506           return error_mark_node;
4507       }
4508       break;
4509
4510     case RID_TYPENAME:
4511       {
4512         tree type;
4513         /* The syntax permitted here is the same permitted for an
4514            elaborated-type-specifier.  */
4515         type = cp_parser_elaborated_type_specifier (parser,
4516                                                     /*is_friend=*/false,
4517                                                     /*is_declaration=*/false);
4518         postfix_expression = cp_parser_functional_cast (parser, type);
4519       }
4520       break;
4521
4522     default:
4523       {
4524         tree type;
4525
4526         /* If the next thing is a simple-type-specifier, we may be
4527            looking at a functional cast.  We could also be looking at
4528            an id-expression.  So, we try the functional cast, and if
4529            that doesn't work we fall back to the primary-expression.  */
4530         cp_parser_parse_tentatively (parser);
4531         /* Look for the simple-type-specifier.  */
4532         type = cp_parser_simple_type_specifier (parser,
4533                                                 /*decl_specs=*/NULL,
4534                                                 CP_PARSER_FLAGS_NONE);
4535         /* Parse the cast itself.  */
4536         if (!cp_parser_error_occurred (parser))
4537           postfix_expression
4538             = cp_parser_functional_cast (parser, type);
4539         /* If that worked, we're done.  */
4540         if (cp_parser_parse_definitely (parser))
4541           break;
4542
4543         /* If the functional-cast didn't work out, try a
4544            compound-literal.  */
4545         if (cp_parser_allow_gnu_extensions_p (parser)
4546             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4547           {
4548             VEC(constructor_elt,gc) *initializer_list = NULL;
4549             bool saved_in_type_id_in_expr_p;
4550
4551             cp_parser_parse_tentatively (parser);
4552             /* Consume the `('.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Parse the type.  */
4555             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4556             parser->in_type_id_in_expr_p = true;
4557             type = cp_parser_type_id (parser);
4558             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4559             /* Look for the `)'.  */
4560             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4561             /* Look for the `{'.  */
4562             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4563             /* If things aren't going well, there's no need to
4564                keep going.  */
4565             if (!cp_parser_error_occurred (parser))
4566               {
4567                 bool non_constant_p;
4568                 /* Parse the initializer-list.  */
4569                 initializer_list
4570                   = cp_parser_initializer_list (parser, &non_constant_p);
4571                 /* Allow a trailing `,'.  */
4572                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4573                   cp_lexer_consume_token (parser->lexer);
4574                 /* Look for the final `}'.  */
4575                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4576               }
4577             /* If that worked, we're definitely looking at a
4578                compound-literal expression.  */
4579             if (cp_parser_parse_definitely (parser))
4580               {
4581                 /* Warn the user that a compound literal is not
4582                    allowed in standard C++.  */
4583                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4584                 /* For simplicity, we disallow compound literals in
4585                    constant-expressions.  We could
4586                    allow compound literals of integer type, whose
4587                    initializer was a constant, in constant
4588                    expressions.  Permitting that usage, as a further
4589                    extension, would not change the meaning of any
4590                    currently accepted programs.  (Of course, as
4591                    compound literals are not part of ISO C++, the
4592                    standard has nothing to say.)  */
4593                 if (cp_parser_non_integral_constant_expression 
4594                     (parser, "non-constant compound literals"))
4595                   {
4596                     postfix_expression = error_mark_node;
4597                     break;
4598                   }
4599                 /* Form the representation of the compound-literal.  */
4600                 postfix_expression
4601                   = (finish_compound_literal
4602                      (type, build_constructor (init_list_type_node,
4603                                                initializer_list)));
4604                 break;
4605               }
4606           }
4607
4608         /* It must be a primary-expression.  */
4609         postfix_expression
4610           = cp_parser_primary_expression (parser, address_p, cast_p,
4611                                           /*template_arg_p=*/false,
4612                                           &idk);
4613       }
4614       break;
4615     }
4616
4617   /* Keep looping until the postfix-expression is complete.  */
4618   while (true)
4619     {
4620       if (idk == CP_ID_KIND_UNQUALIFIED
4621           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4622           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4623         /* It is not a Koenig lookup function call.  */
4624         postfix_expression
4625           = unqualified_name_lookup_error (postfix_expression);
4626
4627       /* Peek at the next token.  */
4628       token = cp_lexer_peek_token (parser->lexer);
4629
4630       switch (token->type)
4631         {
4632         case CPP_OPEN_SQUARE:
4633           postfix_expression
4634             = cp_parser_postfix_open_square_expression (parser,
4635                                                         postfix_expression,
4636                                                         false);
4637           idk = CP_ID_KIND_NONE;
4638           is_member_access = false;
4639           break;
4640
4641         case CPP_OPEN_PAREN:
4642           /* postfix-expression ( expression-list [opt] ) */
4643           {
4644             bool koenig_p;
4645             bool is_builtin_constant_p;
4646             bool saved_integral_constant_expression_p = false;
4647             bool saved_non_integral_constant_expression_p = false;
4648             tree args;
4649
4650             is_member_access = false;
4651
4652             is_builtin_constant_p
4653               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4654             if (is_builtin_constant_p)
4655               {
4656                 /* The whole point of __builtin_constant_p is to allow
4657                    non-constant expressions to appear as arguments.  */
4658                 saved_integral_constant_expression_p
4659                   = parser->integral_constant_expression_p;
4660                 saved_non_integral_constant_expression_p
4661                   = parser->non_integral_constant_expression_p;
4662                 parser->integral_constant_expression_p = false;
4663               }
4664             args = (cp_parser_parenthesized_expression_list
4665                     (parser, /*is_attribute_list=*/false,
4666                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4667                      /*non_constant_p=*/NULL));
4668             if (is_builtin_constant_p)
4669               {
4670                 parser->integral_constant_expression_p
4671                   = saved_integral_constant_expression_p;
4672                 parser->non_integral_constant_expression_p
4673                   = saved_non_integral_constant_expression_p;
4674               }
4675
4676             if (args == error_mark_node)
4677               {
4678                 postfix_expression = error_mark_node;
4679                 break;
4680               }
4681
4682             /* Function calls are not permitted in
4683                constant-expressions.  */
4684             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4685                 && cp_parser_non_integral_constant_expression (parser,
4686                                                                "a function call"))
4687               {
4688                 postfix_expression = error_mark_node;
4689                 break;
4690               }
4691
4692             koenig_p = false;
4693             if (idk == CP_ID_KIND_UNQUALIFIED)
4694               {
4695                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4696                   {
4697                     if (args)
4698                       {
4699                         koenig_p = true;
4700                         postfix_expression
4701                           = perform_koenig_lookup (postfix_expression, args);
4702                       }
4703                     else
4704                       postfix_expression
4705                         = unqualified_fn_lookup_error (postfix_expression);
4706                   }
4707                 /* We do not perform argument-dependent lookup if
4708                    normal lookup finds a non-function, in accordance
4709                    with the expected resolution of DR 218.  */
4710                 else if (args && is_overloaded_fn (postfix_expression))
4711                   {
4712                     tree fn = get_first_fn (postfix_expression);
4713
4714                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4715                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4716
4717                     /* Only do argument dependent lookup if regular
4718                        lookup does not find a set of member functions.
4719                        [basic.lookup.koenig]/2a  */
4720                     if (!DECL_FUNCTION_MEMBER_P (fn))
4721                       {
4722                         koenig_p = true;
4723                         postfix_expression
4724                           = perform_koenig_lookup (postfix_expression, args);
4725                       }
4726                   }
4727               }
4728
4729             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4730               {
4731                 tree instance = TREE_OPERAND (postfix_expression, 0);
4732                 tree fn = TREE_OPERAND (postfix_expression, 1);
4733
4734                 if (processing_template_decl
4735                     && (type_dependent_expression_p (instance)
4736                         || (!BASELINK_P (fn)
4737                             && TREE_CODE (fn) != FIELD_DECL)
4738                         || type_dependent_expression_p (fn)
4739                         || any_type_dependent_arguments_p (args)))
4740                   {
4741                     postfix_expression
4742                       = build_nt_call_list (postfix_expression, args);
4743                     break;
4744                   }
4745
4746                 if (BASELINK_P (fn))
4747                   postfix_expression
4748                     = (build_new_method_call
4749                        (instance, fn, args, NULL_TREE,
4750                         (idk == CP_ID_KIND_QUALIFIED
4751                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4752                         /*fn_p=*/NULL,
4753                         tf_warning_or_error));
4754                 else
4755                   postfix_expression
4756                     = finish_call_expr (postfix_expression, args,
4757                                         /*disallow_virtual=*/false,
4758                                         /*koenig_p=*/false,
4759                                         tf_warning_or_error);
4760               }
4761             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4762                      || TREE_CODE (postfix_expression) == MEMBER_REF
4763                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4764               postfix_expression = (build_offset_ref_call_from_tree
4765                                     (postfix_expression, args));
4766             else if (idk == CP_ID_KIND_QUALIFIED)
4767               /* A call to a static class member, or a namespace-scope
4768                  function.  */
4769               postfix_expression
4770                 = finish_call_expr (postfix_expression, args,
4771                                     /*disallow_virtual=*/true,
4772                                     koenig_p,
4773                                     tf_warning_or_error);
4774             else
4775               /* All other function calls.  */
4776               postfix_expression
4777                 = finish_call_expr (postfix_expression, args,
4778                                     /*disallow_virtual=*/false,
4779                                     koenig_p,
4780                                     tf_warning_or_error);
4781
4782             if (warn_disallowed_functions)
4783               warn_if_disallowed_function_p (postfix_expression);
4784
4785             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4786             idk = CP_ID_KIND_NONE;
4787           }
4788           break;
4789
4790         case CPP_DOT:
4791         case CPP_DEREF:
4792           /* postfix-expression . template [opt] id-expression
4793              postfix-expression . pseudo-destructor-name
4794              postfix-expression -> template [opt] id-expression
4795              postfix-expression -> pseudo-destructor-name */
4796
4797           /* Consume the `.' or `->' operator.  */
4798           cp_lexer_consume_token (parser->lexer);
4799
4800           postfix_expression
4801             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4802                                                       postfix_expression,
4803                                                       false, &idk,
4804                                                       token->location);
4805
4806           is_member_access = true;
4807           break;
4808
4809         case CPP_PLUS_PLUS:
4810           /* postfix-expression ++  */
4811           /* Consume the `++' token.  */
4812           cp_lexer_consume_token (parser->lexer);
4813           /* Generate a representation for the complete expression.  */
4814           postfix_expression
4815             = finish_increment_expr (postfix_expression,
4816                                      POSTINCREMENT_EXPR);
4817           /* Increments may not appear in constant-expressions.  */
4818           if (cp_parser_non_integral_constant_expression (parser,
4819                                                           "an increment"))
4820             postfix_expression = error_mark_node;
4821           idk = CP_ID_KIND_NONE;
4822           is_member_access = false;
4823           break;
4824
4825         case CPP_MINUS_MINUS:
4826           /* postfix-expression -- */
4827           /* Consume the `--' token.  */
4828           cp_lexer_consume_token (parser->lexer);
4829           /* Generate a representation for the complete expression.  */
4830           postfix_expression
4831             = finish_increment_expr (postfix_expression,
4832                                      POSTDECREMENT_EXPR);
4833           /* Decrements may not appear in constant-expressions.  */
4834           if (cp_parser_non_integral_constant_expression (parser,
4835                                                           "a decrement"))
4836             postfix_expression = error_mark_node;
4837           idk = CP_ID_KIND_NONE;
4838           is_member_access = false;
4839           break;
4840
4841         default:
4842           if (member_access_only_p)
4843             return is_member_access? postfix_expression : error_mark_node;
4844           else
4845             return postfix_expression;
4846         }
4847     }
4848
4849   /* We should never get here.  */
4850   gcc_unreachable ();
4851   return error_mark_node;
4852 }
4853
4854 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4855    by cp_parser_builtin_offsetof.  We're looking for
4856
4857      postfix-expression [ expression ]
4858
4859    FOR_OFFSETOF is set if we're being called in that context, which
4860    changes how we deal with integer constant expressions.  */
4861
4862 static tree
4863 cp_parser_postfix_open_square_expression (cp_parser *parser,
4864                                           tree postfix_expression,
4865                                           bool for_offsetof)
4866 {
4867   tree index;
4868
4869   /* Consume the `[' token.  */
4870   cp_lexer_consume_token (parser->lexer);
4871
4872   /* Parse the index expression.  */
4873   /* ??? For offsetof, there is a question of what to allow here.  If
4874      offsetof is not being used in an integral constant expression context,
4875      then we *could* get the right answer by computing the value at runtime.
4876      If we are in an integral constant expression context, then we might
4877      could accept any constant expression; hard to say without analysis.
4878      Rather than open the barn door too wide right away, allow only integer
4879      constant expressions here.  */
4880   if (for_offsetof)
4881     index = cp_parser_constant_expression (parser, false, NULL);
4882   else
4883     index = cp_parser_expression (parser, /*cast_p=*/false);
4884
4885   /* Look for the closing `]'.  */
4886   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4887
4888   /* Build the ARRAY_REF.  */
4889   postfix_expression = grok_array_decl (postfix_expression, index);
4890
4891   /* When not doing offsetof, array references are not permitted in
4892      constant-expressions.  */
4893   if (!for_offsetof
4894       && (cp_parser_non_integral_constant_expression
4895           (parser, "an array reference")))
4896     postfix_expression = error_mark_node;
4897
4898   return postfix_expression;
4899 }
4900
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902    by cp_parser_builtin_offsetof.  We're looking for
4903
4904      postfix-expression . template [opt] id-expression
4905      postfix-expression . pseudo-destructor-name
4906      postfix-expression -> template [opt] id-expression
4907      postfix-expression -> pseudo-destructor-name
4908
4909    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4910    limits what of the above we'll actually accept, but nevermind.
4911    TOKEN_TYPE is the "." or "->" token, which will already have been
4912    removed from the stream.  */
4913
4914 static tree
4915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4916                                         enum cpp_ttype token_type,
4917                                         tree postfix_expression,
4918                                         bool for_offsetof, cp_id_kind *idk,
4919                                         location_t location)
4920 {
4921   tree name;
4922   bool dependent_p;
4923   bool pseudo_destructor_p;
4924   tree scope = NULL_TREE;
4925
4926   /* If this is a `->' operator, dereference the pointer.  */
4927   if (token_type == CPP_DEREF)
4928     postfix_expression = build_x_arrow (postfix_expression);
4929   /* Check to see whether or not the expression is type-dependent.  */
4930   dependent_p = type_dependent_expression_p (postfix_expression);
4931   /* The identifier following the `->' or `.' is not qualified.  */
4932   parser->scope = NULL_TREE;
4933   parser->qualifying_scope = NULL_TREE;
4934   parser->object_scope = NULL_TREE;
4935   *idk = CP_ID_KIND_NONE;
4936   /* Enter the scope corresponding to the type of the object
4937      given by the POSTFIX_EXPRESSION.  */
4938   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4939     {
4940       scope = TREE_TYPE (postfix_expression);
4941       /* According to the standard, no expression should ever have
4942          reference type.  Unfortunately, we do not currently match
4943          the standard in this respect in that our internal representation
4944          of an expression may have reference type even when the standard
4945          says it does not.  Therefore, we have to manually obtain the
4946          underlying type here.  */
4947       scope = non_reference (scope);
4948       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4949       if (scope == unknown_type_node)
4950         {
4951           error ("%H%qE does not have class type", &location, postfix_expression);
4952           scope = NULL_TREE;
4953         }
4954       else
4955         scope = complete_type_or_else (scope, NULL_TREE);
4956       /* Let the name lookup machinery know that we are processing a
4957          class member access expression.  */
4958       parser->context->object_type = scope;
4959       /* If something went wrong, we want to be able to discern that case,
4960          as opposed to the case where there was no SCOPE due to the type
4961          of expression being dependent.  */
4962       if (!scope)
4963         scope = error_mark_node;
4964       /* If the SCOPE was erroneous, make the various semantic analysis
4965          functions exit quickly -- and without issuing additional error
4966          messages.  */
4967       if (scope == error_mark_node)
4968         postfix_expression = error_mark_node;
4969     }
4970
4971   /* Assume this expression is not a pseudo-destructor access.  */
4972   pseudo_destructor_p = false;
4973
4974   /* If the SCOPE is a scalar type, then, if this is a valid program,
4975      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4976      is type dependent, it can be pseudo-destructor-name or something else.
4977      Try to parse it as pseudo-destructor-name first.  */
4978   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4979     {
4980       tree s;
4981       tree type;
4982
4983       cp_parser_parse_tentatively (parser);
4984       /* Parse the pseudo-destructor-name.  */
4985       s = NULL_TREE;
4986       cp_parser_pseudo_destructor_name (parser, &s, &type);
4987       if (dependent_p
4988           && (cp_parser_error_occurred (parser)
4989               || TREE_CODE (type) != TYPE_DECL
4990               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4991         cp_parser_abort_tentative_parse (parser);
4992       else if (cp_parser_parse_definitely (parser))
4993         {
4994           pseudo_destructor_p = true;
4995           postfix_expression
4996             = finish_pseudo_destructor_expr (postfix_expression,
4997                                              s, TREE_TYPE (type));
4998         }
4999     }
5000
5001   if (!pseudo_destructor_p)
5002     {
5003       /* If the SCOPE is not a scalar type, we are looking at an
5004          ordinary class member access expression, rather than a
5005          pseudo-destructor-name.  */
5006       bool template_p;
5007       cp_token *token = cp_lexer_peek_token (parser->lexer);
5008       /* Parse the id-expression.  */
5009       name = (cp_parser_id_expression
5010               (parser,
5011                cp_parser_optional_template_keyword (parser),
5012                /*check_dependency_p=*/true,
5013                &template_p,
5014                /*declarator_p=*/false,
5015                /*optional_p=*/false));
5016       /* In general, build a SCOPE_REF if the member name is qualified.
5017          However, if the name was not dependent and has already been
5018          resolved; there is no need to build the SCOPE_REF.  For example;
5019
5020              struct X { void f(); };
5021              template <typename T> void f(T* t) { t->X::f(); }
5022
5023          Even though "t" is dependent, "X::f" is not and has been resolved
5024          to a BASELINK; there is no need to include scope information.  */
5025
5026       /* But we do need to remember that there was an explicit scope for
5027          virtual function calls.  */
5028       if (parser->scope)
5029         *idk = CP_ID_KIND_QUALIFIED;
5030
5031       /* If the name is a template-id that names a type, we will get a
5032          TYPE_DECL here.  That is invalid code.  */
5033       if (TREE_CODE (name) == TYPE_DECL)
5034         {
5035           error ("%Hinvalid use of %qD", &token->location, name);
5036           postfix_expression = error_mark_node;
5037         }
5038       else
5039         {
5040           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5041             {
5042               name = build_qualified_name (/*type=*/NULL_TREE,
5043                                            parser->scope,
5044                                            name,
5045                                            template_p);
5046               parser->scope = NULL_TREE;
5047               parser->qualifying_scope = NULL_TREE;
5048               parser->object_scope = NULL_TREE;
5049             }
5050           if (scope && name && BASELINK_P (name))
5051             adjust_result_of_qualified_name_lookup
5052               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5053           postfix_expression
5054             = finish_class_member_access_expr (postfix_expression, name,
5055                                                template_p, 
5056                                                tf_warning_or_error);
5057         }
5058     }
5059
5060   /* We no longer need to look up names in the scope of the object on
5061      the left-hand side of the `.' or `->' operator.  */
5062   parser->context->object_type = NULL_TREE;
5063
5064   /* Outside of offsetof, these operators may not appear in
5065      constant-expressions.  */
5066   if (!for_offsetof
5067       && (cp_parser_non_integral_constant_expression
5068           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5069     postfix_expression = error_mark_node;
5070
5071   return postfix_expression;
5072 }
5073
5074 /* Parse a parenthesized expression-list.
5075
5076    expression-list:
5077      assignment-expression
5078      expression-list, assignment-expression
5079
5080    attribute-list:
5081      expression-list
5082      identifier
5083      identifier, expression-list
5084
5085    CAST_P is true if this expression is the target of a cast.
5086
5087    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5088    argument pack.
5089
5090    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5091    representation of an assignment-expression.  Note that a TREE_LIST
5092    is returned even if there is only a single expression in the list.
5093    error_mark_node is returned if the ( and or ) are
5094    missing. NULL_TREE is returned on no expressions. The parentheses
5095    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5096    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5097    indicates whether or not all of the expressions in the list were
5098    constant.  */
5099
5100 static tree
5101 cp_parser_parenthesized_expression_list (cp_parser* parser,
5102                                          bool is_attribute_list,
5103                                          bool cast_p,
5104                                          bool allow_expansion_p,
5105                                          bool *non_constant_p)
5106 {
5107   tree expression_list = NULL_TREE;
5108   bool fold_expr_p = is_attribute_list;
5109   tree identifier = NULL_TREE;
5110   bool saved_greater_than_is_operator_p;
5111
5112   /* Assume all the expressions will be constant.  */
5113   if (non_constant_p)
5114     *non_constant_p = false;
5115
5116   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5117     return error_mark_node;
5118
5119   /* Within a parenthesized expression, a `>' token is always
5120      the greater-than operator.  */
5121   saved_greater_than_is_operator_p
5122     = parser->greater_than_is_operator_p;
5123   parser->greater_than_is_operator_p = true;
5124
5125   /* Consume expressions until there are no more.  */
5126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5127     while (true)
5128       {
5129         tree expr;
5130
5131         /* At the beginning of attribute lists, check to see if the
5132            next token is an identifier.  */
5133         if (is_attribute_list
5134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5135           {
5136             cp_token *token;
5137
5138             /* Consume the identifier.  */
5139             token = cp_lexer_consume_token (parser->lexer);
5140             /* Save the identifier.  */
5141             identifier = token->u.value;
5142           }
5143         else
5144           {
5145             bool expr_non_constant_p;
5146
5147             /* Parse the next assignment-expression.  */
5148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5149               {
5150                 /* A braced-init-list.  */
5151                 maybe_warn_cpp0x ("extended initializer lists");
5152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5153                 if (non_constant_p && expr_non_constant_p)
5154                   *non_constant_p = true;
5155               }
5156             else if (non_constant_p)
5157               {
5158                 expr = (cp_parser_constant_expression
5159                         (parser, /*allow_non_constant_p=*/true,
5160                          &expr_non_constant_p));
5161                 if (expr_non_constant_p)
5162                   *non_constant_p = true;
5163               }
5164             else
5165               expr = cp_parser_assignment_expression (parser, cast_p);
5166
5167             if (fold_expr_p)
5168               expr = fold_non_dependent_expr (expr);
5169
5170             /* If we have an ellipsis, then this is an expression
5171                expansion.  */
5172             if (allow_expansion_p
5173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5174               {
5175                 /* Consume the `...'.  */
5176                 cp_lexer_consume_token (parser->lexer);
5177
5178                 /* Build the argument pack.  */
5179                 expr = make_pack_expansion (expr);
5180               }
5181
5182              /* Add it to the list.  We add error_mark_node
5183                 expressions to the list, so that we can still tell if
5184                 the correct form for a parenthesized expression-list
5185                 is found. That gives better errors.  */
5186             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5187
5188             if (expr == error_mark_node)
5189               goto skip_comma;
5190           }
5191
5192         /* After the first item, attribute lists look the same as
5193            expression lists.  */
5194         is_attribute_list = false;
5195
5196       get_comma:;
5197         /* If the next token isn't a `,', then we are done.  */
5198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5199           break;
5200
5201         /* Otherwise, consume the `,' and keep going.  */
5202         cp_lexer_consume_token (parser->lexer);
5203       }
5204
5205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5206     {
5207       int ending;
5208
5209     skip_comma:;
5210       /* We try and resync to an unnested comma, as that will give the
5211          user better diagnostics.  */
5212       ending = cp_parser_skip_to_closing_parenthesis (parser,
5213                                                       /*recovering=*/true,
5214                                                       /*or_comma=*/true,
5215                                                       /*consume_paren=*/true);
5216       if (ending < 0)
5217         goto get_comma;
5218       if (!ending)
5219         {
5220           parser->greater_than_is_operator_p
5221             = saved_greater_than_is_operator_p;
5222           return error_mark_node;
5223         }
5224     }
5225
5226   parser->greater_than_is_operator_p
5227     = saved_greater_than_is_operator_p;
5228
5229   /* We built up the list in reverse order so we must reverse it now.  */
5230   expression_list = nreverse (expression_list);
5231   if (identifier)
5232     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5233
5234   return expression_list;
5235 }
5236
5237 /* Parse a pseudo-destructor-name.
5238
5239    pseudo-destructor-name:
5240      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5241      :: [opt] nested-name-specifier template template-id :: ~ type-name
5242      :: [opt] nested-name-specifier [opt] ~ type-name
5243
5244    If either of the first two productions is used, sets *SCOPE to the
5245    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5246    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5247    or ERROR_MARK_NODE if the parse fails.  */
5248
5249 static void
5250 cp_parser_pseudo_destructor_name (cp_parser* parser,
5251                                   tree* scope,
5252                                   tree* type)
5253 {
5254   bool nested_name_specifier_p;
5255
5256   /* Assume that things will not work out.  */
5257   *type = error_mark_node;
5258
5259   /* Look for the optional `::' operator.  */
5260   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5261   /* Look for the optional nested-name-specifier.  */
5262   nested_name_specifier_p
5263     = (cp_parser_nested_name_specifier_opt (parser,
5264                                             /*typename_keyword_p=*/false,
5265                                             /*check_dependency_p=*/true,
5266                                             /*type_p=*/false,
5267                                             /*is_declaration=*/false)
5268        != NULL_TREE);
5269   /* Now, if we saw a nested-name-specifier, we might be doing the
5270      second production.  */
5271   if (nested_name_specifier_p
5272       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5273     {
5274       /* Consume the `template' keyword.  */
5275       cp_lexer_consume_token (parser->lexer);
5276       /* Parse the template-id.  */
5277       cp_parser_template_id (parser,
5278                              /*template_keyword_p=*/true,
5279                              /*check_dependency_p=*/false,
5280                              /*is_declaration=*/true);
5281       /* Look for the `::' token.  */
5282       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5283     }
5284   /* If the next token is not a `~', then there might be some
5285      additional qualification.  */
5286   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5287     {
5288       /* At this point, we're looking for "type-name :: ~".  The type-name
5289          must not be a class-name, since this is a pseudo-destructor.  So,
5290          it must be either an enum-name, or a typedef-name -- both of which
5291          are just identifiers.  So, we peek ahead to check that the "::"
5292          and "~" tokens are present; if they are not, then we can avoid
5293          calling type_name.  */
5294       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5295           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5296           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5297         {
5298           cp_parser_error (parser, "non-scalar type");
5299           return;
5300         }
5301
5302       /* Look for the type-name.  */
5303       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5304       if (*scope == error_mark_node)
5305         return;
5306
5307       /* Look for the `::' token.  */
5308       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5309     }
5310   else
5311     *scope = NULL_TREE;
5312
5313   /* Look for the `~'.  */
5314   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5315   /* Look for the type-name again.  We are not responsible for
5316      checking that it matches the first type-name.  */
5317   *type = cp_parser_nonclass_name (parser);
5318 }
5319
5320 /* Parse a unary-expression.
5321
5322    unary-expression:
5323      postfix-expression
5324      ++ cast-expression
5325      -- cast-expression
5326      unary-operator cast-expression
5327      sizeof unary-expression
5328      sizeof ( type-id )
5329      new-expression
5330      delete-expression
5331
5332    GNU Extensions:
5333
5334    unary-expression:
5335      __extension__ cast-expression
5336      __alignof__ unary-expression
5337      __alignof__ ( type-id )
5338      __real__ cast-expression
5339      __imag__ cast-expression
5340      && identifier
5341
5342    ADDRESS_P is true iff the unary-expression is appearing as the
5343    operand of the `&' operator.   CAST_P is true if this expression is
5344    the target of a cast.
5345
5346    Returns a representation of the expression.  */
5347
5348 static tree
5349 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5350 {
5351   cp_token *token;
5352   enum tree_code unary_operator;
5353
5354   /* Peek at the next token.  */
5355   token = cp_lexer_peek_token (parser->lexer);
5356   /* Some keywords give away the kind of expression.  */
5357   if (token->type == CPP_KEYWORD)
5358     {
5359       enum rid keyword = token->keyword;
5360
5361       switch (keyword)
5362         {
5363         case RID_ALIGNOF:
5364         case RID_SIZEOF:
5365           {
5366             tree operand;
5367             enum tree_code op;
5368
5369             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5370             /* Consume the token.  */
5371             cp_lexer_consume_token (parser->lexer);
5372             /* Parse the operand.  */
5373             operand = cp_parser_sizeof_operand (parser, keyword);
5374
5375             if (TYPE_P (operand))
5376               return cxx_sizeof_or_alignof_type (operand, op, true);
5377             else
5378               return cxx_sizeof_or_alignof_expr (operand, op, true);
5379           }
5380
5381         case RID_NEW:
5382           return cp_parser_new_expression (parser);
5383
5384         case RID_DELETE:
5385           return cp_parser_delete_expression (parser);
5386
5387         case RID_EXTENSION:
5388           {
5389             /* The saved value of the PEDANTIC flag.  */
5390             int saved_pedantic;
5391             tree expr;
5392
5393             /* Save away the PEDANTIC flag.  */
5394             cp_parser_extension_opt (parser, &saved_pedantic);
5395             /* Parse the cast-expression.  */
5396             expr = cp_parser_simple_cast_expression (parser);
5397             /* Restore the PEDANTIC flag.  */
5398             pedantic = saved_pedantic;
5399
5400             return expr;
5401           }
5402
5403         case RID_REALPART:
5404         case RID_IMAGPART:
5405           {
5406             tree expression;
5407
5408             /* Consume the `__real__' or `__imag__' token.  */
5409             cp_lexer_consume_token (parser->lexer);
5410             /* Parse the cast-expression.  */
5411             expression = cp_parser_simple_cast_expression (parser);
5412             /* Create the complete representation.  */
5413             return build_x_unary_op ((keyword == RID_REALPART
5414                                       ? REALPART_EXPR : IMAGPART_EXPR),
5415                                      expression,
5416                                      tf_warning_or_error);
5417           }
5418           break;
5419
5420         default:
5421           break;
5422         }
5423     }
5424
5425   /* Look for the `:: new' and `:: delete', which also signal the
5426      beginning of a new-expression, or delete-expression,
5427      respectively.  If the next token is `::', then it might be one of
5428      these.  */
5429   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5430     {
5431       enum rid keyword;
5432
5433       /* See if the token after the `::' is one of the keywords in
5434          which we're interested.  */
5435       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5436       /* If it's `new', we have a new-expression.  */
5437       if (keyword == RID_NEW)
5438         return cp_parser_new_expression (parser);
5439       /* Similarly, for `delete'.  */
5440       else if (keyword == RID_DELETE)
5441         return cp_parser_delete_expression (parser);
5442     }
5443
5444   /* Look for a unary operator.  */
5445   unary_operator = cp_parser_unary_operator (token);
5446   /* The `++' and `--' operators can be handled similarly, even though
5447      they are not technically unary-operators in the grammar.  */
5448   if (unary_operator == ERROR_MARK)
5449     {
5450       if (token->type == CPP_PLUS_PLUS)
5451         unary_operator = PREINCREMENT_EXPR;
5452       else if (token->type == CPP_MINUS_MINUS)
5453         unary_operator = PREDECREMENT_EXPR;
5454       /* Handle the GNU address-of-label extension.  */
5455       else if (cp_parser_allow_gnu_extensions_p (parser)
5456                && token->type == CPP_AND_AND)
5457         {
5458           tree identifier;
5459           tree expression;
5460           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5461
5462           /* Consume the '&&' token.  */
5463           cp_lexer_consume_token (parser->lexer);
5464           /* Look for the identifier.  */
5465           identifier = cp_parser_identifier (parser);
5466           /* Create an expression representing the address.  */
5467           expression = finish_label_address_expr (identifier, loc);
5468           if (cp_parser_non_integral_constant_expression (parser,
5469                                                 "the address of a label"))
5470             expression = error_mark_node;
5471           return expression;
5472         }
5473     }
5474   if (unary_operator != ERROR_MARK)
5475     {
5476       tree cast_expression;
5477       tree expression = error_mark_node;
5478       const char *non_constant_p = NULL;
5479
5480       /* Consume the operator token.  */
5481       token = cp_lexer_consume_token (parser->lexer);
5482       /* Parse the cast-expression.  */
5483       cast_expression
5484         = cp_parser_cast_expression (parser,
5485                                      unary_operator == ADDR_EXPR,
5486                                      /*cast_p=*/false);
5487       /* Now, build an appropriate representation.  */
5488       switch (unary_operator)
5489         {
5490         case INDIRECT_REF:
5491           non_constant_p = "%<*%>";
5492           expression = build_x_indirect_ref (cast_expression, "unary *",
5493                                              tf_warning_or_error);
5494           break;
5495
5496         case ADDR_EXPR:
5497           non_constant_p = "%<&%>";
5498           /* Fall through.  */
5499         case BIT_NOT_EXPR:
5500           expression = build_x_unary_op (unary_operator, cast_expression,
5501                                          tf_warning_or_error);
5502           break;
5503
5504         case PREINCREMENT_EXPR:
5505         case PREDECREMENT_EXPR:
5506           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5507                             ? "%<++%>" : "%<--%>");
5508           /* Fall through.  */
5509         case UNARY_PLUS_EXPR:
5510         case NEGATE_EXPR:
5511         case TRUTH_NOT_EXPR:
5512           expression = finish_unary_op_expr (unary_operator, cast_expression);
5513           break;
5514
5515         default:
5516           gcc_unreachable ();
5517         }
5518
5519       if (non_constant_p
5520           && cp_parser_non_integral_constant_expression (parser,
5521                                                          non_constant_p))
5522         expression = error_mark_node;
5523
5524       return expression;
5525     }
5526
5527   return cp_parser_postfix_expression (parser, address_p, cast_p,
5528                                        /*member_access_only_p=*/false);
5529 }
5530
5531 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5532    unary-operator, the corresponding tree code is returned.  */
5533
5534 static enum tree_code
5535 cp_parser_unary_operator (cp_token* token)
5536 {
5537   switch (token->type)
5538     {
5539     case CPP_MULT:
5540       return INDIRECT_REF;
5541
5542     case CPP_AND:
5543       return ADDR_EXPR;
5544
5545     case CPP_PLUS:
5546       return UNARY_PLUS_EXPR;
5547
5548     case CPP_MINUS:
5549       return NEGATE_EXPR;
5550
5551     case CPP_NOT:
5552       return TRUTH_NOT_EXPR;
5553
5554     case CPP_COMPL:
5555       return BIT_NOT_EXPR;
5556
5557     default:
5558       return ERROR_MARK;
5559     }
5560 }
5561
5562 /* Parse a new-expression.
5563
5564    new-expression:
5565      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5566      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5567
5568    Returns a representation of the expression.  */
5569
5570 static tree
5571 cp_parser_new_expression (cp_parser* parser)
5572 {
5573   bool global_scope_p;
5574   tree placement;
5575   tree type;
5576   tree initializer;
5577   tree nelts;
5578
5579   /* Look for the optional `::' operator.  */
5580   global_scope_p
5581     = (cp_parser_global_scope_opt (parser,
5582                                    /*current_scope_valid_p=*/false)
5583        != NULL_TREE);
5584   /* Look for the `new' operator.  */
5585   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5586   /* There's no easy way to tell a new-placement from the
5587      `( type-id )' construct.  */
5588   cp_parser_parse_tentatively (parser);
5589   /* Look for a new-placement.  */
5590   placement = cp_parser_new_placement (parser);
5591   /* If that didn't work out, there's no new-placement.  */
5592   if (!cp_parser_parse_definitely (parser))
5593     placement = NULL_TREE;
5594
5595   /* If the next token is a `(', then we have a parenthesized
5596      type-id.  */
5597   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5598     {
5599       cp_token *token;
5600       /* Consume the `('.  */
5601       cp_lexer_consume_token (parser->lexer);
5602       /* Parse the type-id.  */
5603       type = cp_parser_type_id (parser);
5604       /* Look for the closing `)'.  */
5605       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5606       token = cp_lexer_peek_token (parser->lexer);
5607       /* There should not be a direct-new-declarator in this production,
5608          but GCC used to allowed this, so we check and emit a sensible error
5609          message for this case.  */
5610       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5611         {
5612           error ("%Harray bound forbidden after parenthesized type-id",
5613                  &token->location);
5614           inform (token->location, 
5615                   "try removing the parentheses around the type-id");
5616           cp_parser_direct_new_declarator (parser);
5617         }
5618       nelts = NULL_TREE;
5619     }
5620   /* Otherwise, there must be a new-type-id.  */
5621   else
5622     type = cp_parser_new_type_id (parser, &nelts);
5623
5624   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5625   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5626       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5627     initializer = cp_parser_new_initializer (parser);
5628   else
5629     initializer = NULL_TREE;
5630
5631   /* A new-expression may not appear in an integral constant
5632      expression.  */
5633   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5634     return error_mark_node;
5635
5636   /* Create a representation of the new-expression.  */
5637   return build_new (placement, type, nelts, initializer, global_scope_p,
5638                     tf_warning_or_error);
5639 }
5640
5641 /* Parse a new-placement.
5642
5643    new-placement:
5644      ( expression-list )
5645
5646    Returns the same representation as for an expression-list.  */
5647
5648 static tree
5649 cp_parser_new_placement (cp_parser* parser)
5650 {
5651   tree expression_list;
5652
5653   /* Parse the expression-list.  */
5654   expression_list = (cp_parser_parenthesized_expression_list
5655                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5656                       /*non_constant_p=*/NULL));
5657
5658   return expression_list;
5659 }
5660
5661 /* Parse a new-type-id.
5662
5663    new-type-id:
5664      type-specifier-seq new-declarator [opt]
5665
5666    Returns the TYPE allocated.  If the new-type-id indicates an array
5667    type, *NELTS is set to the number of elements in the last array
5668    bound; the TYPE will not include the last array bound.  */
5669
5670 static tree
5671 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5672 {
5673   cp_decl_specifier_seq type_specifier_seq;
5674   cp_declarator *new_declarator;
5675   cp_declarator *declarator;
5676   cp_declarator *outer_declarator;
5677   const char *saved_message;
5678   tree type;
5679
5680   /* The type-specifier sequence must not contain type definitions.
5681      (It cannot contain declarations of new types either, but if they
5682      are not definitions we will catch that because they are not
5683      complete.)  */
5684   saved_message = parser->type_definition_forbidden_message;
5685   parser->type_definition_forbidden_message
5686     = "types may not be defined in a new-type-id";
5687   /* Parse the type-specifier-seq.  */
5688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5689                                 &type_specifier_seq);
5690   /* Restore the old message.  */
5691   parser->type_definition_forbidden_message = saved_message;
5692   /* Parse the new-declarator.  */
5693   new_declarator = cp_parser_new_declarator_opt (parser);
5694
5695   /* Determine the number of elements in the last array dimension, if
5696      any.  */
5697   *nelts = NULL_TREE;
5698   /* Skip down to the last array dimension.  */
5699   declarator = new_declarator;
5700   outer_declarator = NULL;
5701   while (declarator && (declarator->kind == cdk_pointer
5702                         || declarator->kind == cdk_ptrmem))
5703     {
5704       outer_declarator = declarator;
5705       declarator = declarator->declarator;
5706     }
5707   while (declarator
5708          && declarator->kind == cdk_array
5709          && declarator->declarator
5710          && declarator->declarator->kind == cdk_array)
5711     {
5712       outer_declarator = declarator;
5713       declarator = declarator->declarator;
5714     }
5715
5716   if (declarator && declarator->kind == cdk_array)
5717     {
5718       *nelts = declarator->u.array.bounds;
5719       if (*nelts == error_mark_node)
5720         *nelts = integer_one_node;
5721
5722       if (outer_declarator)
5723         outer_declarator->declarator = declarator->declarator;
5724       else
5725         new_declarator = NULL;
5726     }
5727
5728   type = groktypename (&type_specifier_seq, new_declarator);
5729   return type;
5730 }
5731
5732 /* Parse an (optional) new-declarator.
5733
5734    new-declarator:
5735      ptr-operator new-declarator [opt]
5736      direct-new-declarator
5737
5738    Returns the declarator.  */
5739
5740 static cp_declarator *
5741 cp_parser_new_declarator_opt (cp_parser* parser)
5742 {
5743   enum tree_code code;
5744   tree type;
5745   cp_cv_quals cv_quals;
5746
5747   /* We don't know if there's a ptr-operator next, or not.  */
5748   cp_parser_parse_tentatively (parser);
5749   /* Look for a ptr-operator.  */
5750   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5751   /* If that worked, look for more new-declarators.  */
5752   if (cp_parser_parse_definitely (parser))
5753     {
5754       cp_declarator *declarator;
5755
5756       /* Parse another optional declarator.  */
5757       declarator = cp_parser_new_declarator_opt (parser);
5758
5759       return cp_parser_make_indirect_declarator
5760         (code, type, cv_quals, declarator);
5761     }
5762
5763   /* If the next token is a `[', there is a direct-new-declarator.  */
5764   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5765     return cp_parser_direct_new_declarator (parser);
5766
5767   return NULL;
5768 }
5769
5770 /* Parse a direct-new-declarator.
5771
5772    direct-new-declarator:
5773      [ expression ]
5774      direct-new-declarator [constant-expression]
5775
5776    */
5777
5778 static cp_declarator *
5779 cp_parser_direct_new_declarator (cp_parser* parser)
5780 {
5781   cp_declarator *declarator = NULL;
5782
5783   while (true)
5784     {
5785       tree expression;
5786
5787       /* Look for the opening `['.  */
5788       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5789       /* The first expression is not required to be constant.  */
5790       if (!declarator)
5791         {
5792           cp_token *token = cp_lexer_peek_token (parser->lexer);
5793           expression = cp_parser_expression (parser, /*cast_p=*/false);
5794           /* The standard requires that the expression have integral
5795              type.  DR 74 adds enumeration types.  We believe that the
5796              real intent is that these expressions be handled like the
5797              expression in a `switch' condition, which also allows
5798              classes with a single conversion to integral or
5799              enumeration type.  */
5800           if (!processing_template_decl)
5801             {
5802               expression
5803                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5804                                               expression,
5805                                               /*complain=*/true);
5806               if (!expression)
5807                 {
5808                   error ("%Hexpression in new-declarator must have integral "
5809                          "or enumeration type", &token->location);
5810                   expression = error_mark_node;
5811                 }
5812             }
5813         }
5814       /* But all the other expressions must be.  */
5815       else
5816         expression
5817           = cp_parser_constant_expression (parser,
5818                                            /*allow_non_constant=*/false,
5819                                            NULL);
5820       /* Look for the closing `]'.  */
5821       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5822
5823       /* Add this bound to the declarator.  */
5824       declarator = make_array_declarator (declarator, expression);
5825
5826       /* If the next token is not a `[', then there are no more
5827          bounds.  */
5828       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5829         break;
5830     }
5831
5832   return declarator;
5833 }
5834
5835 /* Parse a new-initializer.
5836
5837    new-initializer:
5838      ( expression-list [opt] )
5839      braced-init-list
5840
5841    Returns a representation of the expression-list.  If there is no
5842    expression-list, VOID_ZERO_NODE is returned.  */
5843
5844 static tree
5845 cp_parser_new_initializer (cp_parser* parser)
5846 {
5847   tree expression_list;
5848
5849   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5850     {
5851       bool expr_non_constant_p;
5852       maybe_warn_cpp0x ("extended initializer lists");
5853       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5854       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5855       expression_list = build_tree_list (NULL_TREE, expression_list);
5856     }
5857   else
5858     expression_list = (cp_parser_parenthesized_expression_list
5859                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5860                         /*non_constant_p=*/NULL));
5861   if (!expression_list)
5862     expression_list = void_zero_node;
5863
5864   return expression_list;
5865 }
5866
5867 /* Parse a delete-expression.
5868
5869    delete-expression:
5870      :: [opt] delete cast-expression
5871      :: [opt] delete [ ] cast-expression
5872
5873    Returns a representation of the expression.  */
5874
5875 static tree
5876 cp_parser_delete_expression (cp_parser* parser)
5877 {
5878   bool global_scope_p;
5879   bool array_p;
5880   tree expression;
5881
5882   /* Look for the optional `::' operator.  */
5883   global_scope_p
5884     = (cp_parser_global_scope_opt (parser,
5885                                    /*current_scope_valid_p=*/false)
5886        != NULL_TREE);
5887   /* Look for the `delete' keyword.  */
5888   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5889   /* See if the array syntax is in use.  */
5890   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5891     {
5892       /* Consume the `[' token.  */
5893       cp_lexer_consume_token (parser->lexer);
5894       /* Look for the `]' token.  */
5895       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5896       /* Remember that this is the `[]' construct.  */
5897       array_p = true;
5898     }
5899   else
5900     array_p = false;
5901
5902   /* Parse the cast-expression.  */
5903   expression = cp_parser_simple_cast_expression (parser);
5904
5905   /* A delete-expression may not appear in an integral constant
5906      expression.  */
5907   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5908     return error_mark_node;
5909
5910   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5911 }
5912
5913 /* Returns true if TOKEN may start a cast-expression and false
5914    otherwise.  */
5915
5916 static bool
5917 cp_parser_token_starts_cast_expression (cp_token *token)
5918 {
5919   switch (token->type)
5920     {
5921     case CPP_COMMA:
5922     case CPP_SEMICOLON:
5923     case CPP_QUERY:
5924     case CPP_COLON:
5925     case CPP_CLOSE_SQUARE:
5926     case CPP_CLOSE_PAREN:
5927     case CPP_CLOSE_BRACE:
5928     case CPP_DOT:
5929     case CPP_DOT_STAR:
5930     case CPP_DEREF:
5931     case CPP_DEREF_STAR:
5932     case CPP_DIV:
5933     case CPP_MOD:
5934     case CPP_LSHIFT:
5935     case CPP_RSHIFT:
5936     case CPP_LESS:
5937     case CPP_GREATER:
5938     case CPP_LESS_EQ:
5939     case CPP_GREATER_EQ:
5940     case CPP_EQ_EQ:
5941     case CPP_NOT_EQ:
5942     case CPP_EQ:
5943     case CPP_MULT_EQ:
5944     case CPP_DIV_EQ:
5945     case CPP_MOD_EQ:
5946     case CPP_PLUS_EQ:
5947     case CPP_MINUS_EQ:
5948     case CPP_RSHIFT_EQ:
5949     case CPP_LSHIFT_EQ:
5950     case CPP_AND_EQ:
5951     case CPP_XOR_EQ:
5952     case CPP_OR_EQ:
5953     case CPP_XOR:
5954     case CPP_OR:
5955     case CPP_OR_OR:
5956       return false;
5957
5958       /* '[' may start a primary-expression in obj-c++.  */
5959     case CPP_OPEN_SQUARE:
5960       return c_dialect_objc ();
5961
5962     default:
5963       return true;
5964     }
5965 }
5966
5967 /* Parse a cast-expression.
5968
5969    cast-expression:
5970      unary-expression
5971      ( type-id ) cast-expression
5972
5973    ADDRESS_P is true iff the unary-expression is appearing as the
5974    operand of the `&' operator.   CAST_P is true if this expression is
5975    the target of a cast.
5976
5977    Returns a representation of the expression.  */
5978
5979 static tree
5980 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5981 {
5982   /* If it's a `(', then we might be looking at a cast.  */
5983   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5984     {
5985       tree type = NULL_TREE;
5986       tree expr = NULL_TREE;
5987       bool compound_literal_p;
5988       const char *saved_message;
5989
5990       /* There's no way to know yet whether or not this is a cast.
5991          For example, `(int (3))' is a unary-expression, while `(int)
5992          3' is a cast.  So, we resort to parsing tentatively.  */
5993       cp_parser_parse_tentatively (parser);
5994       /* Types may not be defined in a cast.  */
5995       saved_message = parser->type_definition_forbidden_message;
5996       parser->type_definition_forbidden_message
5997         = "types may not be defined in casts";
5998       /* Consume the `('.  */
5999       cp_lexer_consume_token (parser->lexer);
6000       /* A very tricky bit is that `(struct S) { 3 }' is a
6001          compound-literal (which we permit in C++ as an extension).
6002          But, that construct is not a cast-expression -- it is a
6003          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6004          is legal; if the compound-literal were a cast-expression,
6005          you'd need an extra set of parentheses.)  But, if we parse
6006          the type-id, and it happens to be a class-specifier, then we
6007          will commit to the parse at that point, because we cannot
6008          undo the action that is done when creating a new class.  So,
6009          then we cannot back up and do a postfix-expression.
6010
6011          Therefore, we scan ahead to the closing `)', and check to see
6012          if the token after the `)' is a `{'.  If so, we are not
6013          looking at a cast-expression.
6014
6015          Save tokens so that we can put them back.  */
6016       cp_lexer_save_tokens (parser->lexer);
6017       /* Skip tokens until the next token is a closing parenthesis.
6018          If we find the closing `)', and the next token is a `{', then
6019          we are looking at a compound-literal.  */
6020       compound_literal_p
6021         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6022                                                   /*consume_paren=*/true)
6023            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6024       /* Roll back the tokens we skipped.  */
6025       cp_lexer_rollback_tokens (parser->lexer);
6026       /* If we were looking at a compound-literal, simulate an error
6027          so that the call to cp_parser_parse_definitely below will
6028          fail.  */
6029       if (compound_literal_p)
6030         cp_parser_simulate_error (parser);
6031       else
6032         {
6033           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6034           parser->in_type_id_in_expr_p = true;
6035           /* Look for the type-id.  */
6036           type = cp_parser_type_id (parser);
6037           /* Look for the closing `)'.  */
6038           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6039           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6040         }
6041
6042       /* Restore the saved message.  */
6043       parser->type_definition_forbidden_message = saved_message;
6044
6045       /* At this point this can only be either a cast or a
6046          parenthesized ctor such as `(T ())' that looks like a cast to
6047          function returning T.  */
6048       if (!cp_parser_error_occurred (parser)
6049           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6050                                                      (parser->lexer)))
6051         {
6052           cp_parser_parse_definitely (parser);
6053           expr = cp_parser_cast_expression (parser,
6054                                             /*address_p=*/false,
6055                                             /*cast_p=*/true);
6056
6057           /* Warn about old-style casts, if so requested.  */
6058           if (warn_old_style_cast
6059               && !in_system_header
6060               && !VOID_TYPE_P (type)
6061               && current_lang_name != lang_name_c)
6062             warning (OPT_Wold_style_cast, "use of old-style cast");
6063
6064           /* Only type conversions to integral or enumeration types
6065              can be used in constant-expressions.  */
6066           if (!cast_valid_in_integral_constant_expression_p (type)
6067               && (cp_parser_non_integral_constant_expression
6068                   (parser,
6069                    "a cast to a type other than an integral or "
6070                    "enumeration type")))
6071             return error_mark_node;
6072
6073           /* Perform the cast.  */
6074           expr = build_c_cast (type, expr);
6075           return expr;
6076         }
6077       else 
6078         cp_parser_abort_tentative_parse (parser);
6079     }
6080
6081   /* If we get here, then it's not a cast, so it must be a
6082      unary-expression.  */
6083   return cp_parser_unary_expression (parser, address_p, cast_p);
6084 }
6085
6086 /* Parse a binary expression of the general form:
6087
6088    pm-expression:
6089      cast-expression
6090      pm-expression .* cast-expression
6091      pm-expression ->* cast-expression
6092
6093    multiplicative-expression:
6094      pm-expression
6095      multiplicative-expression * pm-expression
6096      multiplicative-expression / pm-expression
6097      multiplicative-expression % pm-expression
6098
6099    additive-expression:
6100      multiplicative-expression
6101      additive-expression + multiplicative-expression
6102      additive-expression - multiplicative-expression
6103
6104    shift-expression:
6105      additive-expression
6106      shift-expression << additive-expression
6107      shift-expression >> additive-expression
6108
6109    relational-expression:
6110      shift-expression
6111      relational-expression < shift-expression
6112      relational-expression > shift-expression
6113      relational-expression <= shift-expression
6114      relational-expression >= shift-expression
6115
6116   GNU Extension:
6117
6118    relational-expression:
6119      relational-expression <? shift-expression
6120      relational-expression >? shift-expression
6121
6122    equality-expression:
6123      relational-expression
6124      equality-expression == relational-expression
6125      equality-expression != relational-expression
6126
6127    and-expression:
6128      equality-expression
6129      and-expression & equality-expression
6130
6131    exclusive-or-expression:
6132      and-expression
6133      exclusive-or-expression ^ and-expression
6134
6135    inclusive-or-expression:
6136      exclusive-or-expression
6137      inclusive-or-expression | exclusive-or-expression
6138
6139    logical-and-expression:
6140      inclusive-or-expression
6141      logical-and-expression && inclusive-or-expression
6142
6143    logical-or-expression:
6144      logical-and-expression
6145      logical-or-expression || logical-and-expression
6146
6147    All these are implemented with a single function like:
6148
6149    binary-expression:
6150      simple-cast-expression
6151      binary-expression <token> binary-expression
6152
6153    CAST_P is true if this expression is the target of a cast.
6154
6155    The binops_by_token map is used to get the tree codes for each <token> type.
6156    binary-expressions are associated according to a precedence table.  */
6157
6158 #define TOKEN_PRECEDENCE(token)                              \
6159 (((token->type == CPP_GREATER                                \
6160    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6161   && !parser->greater_than_is_operator_p)                    \
6162  ? PREC_NOT_OPERATOR                                         \
6163  : binops_by_token[token->type].prec)
6164
6165 static tree
6166 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6167                              enum cp_parser_prec prec)
6168 {
6169   cp_parser_expression_stack stack;
6170   cp_parser_expression_stack_entry *sp = &stack[0];
6171   tree lhs, rhs;
6172   cp_token *token;
6173   enum tree_code tree_type, lhs_type, rhs_type;
6174   enum cp_parser_prec new_prec, lookahead_prec;
6175   bool overloaded_p;
6176
6177   /* Parse the first expression.  */
6178   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6179   lhs_type = ERROR_MARK;
6180
6181   for (;;)
6182     {
6183       /* Get an operator token.  */
6184       token = cp_lexer_peek_token (parser->lexer);
6185
6186       if (warn_cxx0x_compat
6187           && token->type == CPP_RSHIFT
6188           && !parser->greater_than_is_operator_p)
6189         {
6190           warning (OPT_Wc__0x_compat, 
6191                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6192                    &token->location);
6193           warning (OPT_Wc__0x_compat, 
6194                    "suggest parentheses around %<>>%> expression");
6195         }
6196
6197       new_prec = TOKEN_PRECEDENCE (token);
6198
6199       /* Popping an entry off the stack means we completed a subexpression:
6200          - either we found a token which is not an operator (`>' where it is not
6201            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6202            will happen repeatedly;
6203          - or, we found an operator which has lower priority.  This is the case
6204            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6205            parsing `3 * 4'.  */
6206       if (new_prec <= prec)
6207         {
6208           if (sp == stack)
6209             break;
6210           else
6211             goto pop;
6212         }
6213
6214      get_rhs:
6215       tree_type = binops_by_token[token->type].tree_type;
6216
6217       /* We used the operator token.  */
6218       cp_lexer_consume_token (parser->lexer);
6219
6220       /* Extract another operand.  It may be the RHS of this expression
6221          or the LHS of a new, higher priority expression.  */
6222       rhs = cp_parser_simple_cast_expression (parser);
6223       rhs_type = ERROR_MARK;
6224
6225       /* Get another operator token.  Look up its precedence to avoid
6226          building a useless (immediately popped) stack entry for common
6227          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6228       token = cp_lexer_peek_token (parser->lexer);
6229       lookahead_prec = TOKEN_PRECEDENCE (token);
6230       if (lookahead_prec > new_prec)
6231         {
6232           /* ... and prepare to parse the RHS of the new, higher priority
6233              expression.  Since precedence levels on the stack are
6234              monotonically increasing, we do not have to care about
6235              stack overflows.  */
6236           sp->prec = prec;
6237           sp->tree_type = tree_type;
6238           sp->lhs = lhs;
6239           sp->lhs_type = lhs_type;
6240           sp++;
6241           lhs = rhs;
6242           lhs_type = rhs_type;
6243           prec = new_prec;
6244           new_prec = lookahead_prec;
6245           goto get_rhs;
6246
6247          pop:
6248           /* If the stack is not empty, we have parsed into LHS the right side
6249              (`4' in the example above) of an expression we had suspended.
6250              We can use the information on the stack to recover the LHS (`3')
6251              from the stack together with the tree code (`MULT_EXPR'), and
6252              the precedence of the higher level subexpression
6253              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6254              which will be used to actually build the additive expression.  */
6255           --sp;
6256           prec = sp->prec;
6257           tree_type = sp->tree_type;
6258           rhs = lhs;
6259           rhs_type = lhs_type;
6260           lhs = sp->lhs;
6261           lhs_type = sp->lhs_type;
6262         }
6263
6264       overloaded_p = false;
6265       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6266          ERROR_MARK for everything that is not a binary expression.
6267          This makes warn_about_parentheses miss some warnings that
6268          involve unary operators.  For unary expressions we should
6269          pass the correct tree_code unless the unary expression was
6270          surrounded by parentheses.
6271       */
6272       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6273                                &overloaded_p, tf_warning_or_error);
6274       lhs_type = tree_type;
6275
6276       /* If the binary operator required the use of an overloaded operator,
6277          then this expression cannot be an integral constant-expression.
6278          An overloaded operator can be used even if both operands are
6279          otherwise permissible in an integral constant-expression if at
6280          least one of the operands is of enumeration type.  */
6281
6282       if (overloaded_p
6283           && (cp_parser_non_integral_constant_expression
6284               (parser, "calls to overloaded operators")))
6285         return error_mark_node;
6286     }
6287
6288   return lhs;
6289 }
6290
6291
6292 /* Parse the `? expression : assignment-expression' part of a
6293    conditional-expression.  The LOGICAL_OR_EXPR is the
6294    logical-or-expression that started the conditional-expression.
6295    Returns a representation of the entire conditional-expression.
6296
6297    This routine is used by cp_parser_assignment_expression.
6298
6299      ? expression : assignment-expression
6300
6301    GNU Extensions:
6302
6303      ? : assignment-expression */
6304
6305 static tree
6306 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6307 {
6308   tree expr;
6309   tree assignment_expr;
6310
6311   /* Consume the `?' token.  */
6312   cp_lexer_consume_token (parser->lexer);
6313   if (cp_parser_allow_gnu_extensions_p (parser)
6314       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6315     /* Implicit true clause.  */
6316     expr = NULL_TREE;
6317   else
6318     /* Parse the expression.  */
6319     expr = cp_parser_expression (parser, /*cast_p=*/false);
6320
6321   /* The next token should be a `:'.  */
6322   cp_parser_require (parser, CPP_COLON, "%<:%>");
6323   /* Parse the assignment-expression.  */
6324   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6325
6326   /* Build the conditional-expression.  */
6327   return build_x_conditional_expr (logical_or_expr,
6328                                    expr,
6329                                    assignment_expr,
6330                                    tf_warning_or_error);
6331 }
6332
6333 /* Parse an assignment-expression.
6334
6335    assignment-expression:
6336      conditional-expression
6337      logical-or-expression assignment-operator assignment_expression
6338      throw-expression
6339
6340    CAST_P is true if this expression is the target of a cast.
6341
6342    Returns a representation for the expression.  */
6343
6344 static tree
6345 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6346 {
6347   tree expr;
6348
6349   /* If the next token is the `throw' keyword, then we're looking at
6350      a throw-expression.  */
6351   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6352     expr = cp_parser_throw_expression (parser);
6353   /* Otherwise, it must be that we are looking at a
6354      logical-or-expression.  */
6355   else
6356     {
6357       /* Parse the binary expressions (logical-or-expression).  */
6358       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6359       /* If the next token is a `?' then we're actually looking at a
6360          conditional-expression.  */
6361       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6362         return cp_parser_question_colon_clause (parser, expr);
6363       else
6364         {
6365           enum tree_code assignment_operator;
6366
6367           /* If it's an assignment-operator, we're using the second
6368              production.  */
6369           assignment_operator
6370             = cp_parser_assignment_operator_opt (parser);
6371           if (assignment_operator != ERROR_MARK)
6372             {
6373               bool non_constant_p;
6374
6375               /* Parse the right-hand side of the assignment.  */
6376               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6377
6378               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6379                 maybe_warn_cpp0x ("extended initializer lists");
6380
6381               /* An assignment may not appear in a
6382                  constant-expression.  */
6383               if (cp_parser_non_integral_constant_expression (parser,
6384                                                               "an assignment"))
6385                 return error_mark_node;
6386               /* Build the assignment expression.  */
6387               expr = build_x_modify_expr (expr,
6388                                           assignment_operator,
6389                                           rhs,
6390                                           tf_warning_or_error);
6391             }
6392         }
6393     }
6394
6395   return expr;
6396 }
6397
6398 /* Parse an (optional) assignment-operator.
6399
6400    assignment-operator: one of
6401      = *= /= %= += -= >>= <<= &= ^= |=
6402
6403    GNU Extension:
6404
6405    assignment-operator: one of
6406      <?= >?=
6407
6408    If the next token is an assignment operator, the corresponding tree
6409    code is returned, and the token is consumed.  For example, for
6410    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6411    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6412    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6413    operator, ERROR_MARK is returned.  */
6414
6415 static enum tree_code
6416 cp_parser_assignment_operator_opt (cp_parser* parser)
6417 {
6418   enum tree_code op;
6419   cp_token *token;
6420
6421   /* Peek at the next token.  */
6422   token = cp_lexer_peek_token (parser->lexer);
6423
6424   switch (token->type)
6425     {
6426     case CPP_EQ:
6427       op = NOP_EXPR;
6428       break;
6429
6430     case CPP_MULT_EQ:
6431       op = MULT_EXPR;
6432       break;
6433
6434     case CPP_DIV_EQ:
6435       op = TRUNC_DIV_EXPR;
6436       break;
6437
6438     case CPP_MOD_EQ:
6439       op = TRUNC_MOD_EXPR;
6440       break;
6441
6442     case CPP_PLUS_EQ:
6443       op = PLUS_EXPR;
6444       break;
6445
6446     case CPP_MINUS_EQ:
6447       op = MINUS_EXPR;
6448       break;
6449
6450     case CPP_RSHIFT_EQ:
6451       op = RSHIFT_EXPR;
6452       break;
6453
6454     case CPP_LSHIFT_EQ:
6455       op = LSHIFT_EXPR;
6456       break;
6457
6458     case CPP_AND_EQ:
6459       op = BIT_AND_EXPR;
6460       break;
6461
6462     case CPP_XOR_EQ:
6463       op = BIT_XOR_EXPR;
6464       break;
6465
6466     case CPP_OR_EQ:
6467       op = BIT_IOR_EXPR;
6468       break;
6469
6470     default:
6471       /* Nothing else is an assignment operator.  */
6472       op = ERROR_MARK;
6473     }
6474
6475   /* If it was an assignment operator, consume it.  */
6476   if (op != ERROR_MARK)
6477     cp_lexer_consume_token (parser->lexer);
6478
6479   return op;
6480 }
6481
6482 /* Parse an expression.
6483
6484    expression:
6485      assignment-expression
6486      expression , assignment-expression
6487
6488    CAST_P is true if this expression is the target of a cast.
6489
6490    Returns a representation of the expression.  */
6491
6492 static tree
6493 cp_parser_expression (cp_parser* parser, bool cast_p)
6494 {
6495   tree expression = NULL_TREE;
6496
6497   while (true)
6498     {
6499       tree assignment_expression;
6500
6501       /* Parse the next assignment-expression.  */
6502       assignment_expression
6503         = cp_parser_assignment_expression (parser, cast_p);
6504       /* If this is the first assignment-expression, we can just
6505          save it away.  */
6506       if (!expression)
6507         expression = assignment_expression;
6508       else
6509         expression = build_x_compound_expr (expression,
6510                                             assignment_expression,
6511                                             tf_warning_or_error);
6512       /* If the next token is not a comma, then we are done with the
6513          expression.  */
6514       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6515         break;
6516       /* Consume the `,'.  */
6517       cp_lexer_consume_token (parser->lexer);
6518       /* A comma operator cannot appear in a constant-expression.  */
6519       if (cp_parser_non_integral_constant_expression (parser,
6520                                                       "a comma operator"))
6521         expression = error_mark_node;
6522     }
6523
6524   return expression;
6525 }
6526
6527 /* Parse a constant-expression.
6528
6529    constant-expression:
6530      conditional-expression
6531
6532   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6533   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6534   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6535   is false, NON_CONSTANT_P should be NULL.  */
6536
6537 static tree
6538 cp_parser_constant_expression (cp_parser* parser,
6539                                bool allow_non_constant_p,
6540                                bool *non_constant_p)
6541 {
6542   bool saved_integral_constant_expression_p;
6543   bool saved_allow_non_integral_constant_expression_p;
6544   bool saved_non_integral_constant_expression_p;
6545   tree expression;
6546
6547   /* It might seem that we could simply parse the
6548      conditional-expression, and then check to see if it were
6549      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6550      one that the compiler can figure out is constant, possibly after
6551      doing some simplifications or optimizations.  The standard has a
6552      precise definition of constant-expression, and we must honor
6553      that, even though it is somewhat more restrictive.
6554
6555      For example:
6556
6557        int i[(2, 3)];
6558
6559      is not a legal declaration, because `(2, 3)' is not a
6560      constant-expression.  The `,' operator is forbidden in a
6561      constant-expression.  However, GCC's constant-folding machinery
6562      will fold this operation to an INTEGER_CST for `3'.  */
6563
6564   /* Save the old settings.  */
6565   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6566   saved_allow_non_integral_constant_expression_p
6567     = parser->allow_non_integral_constant_expression_p;
6568   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6569   /* We are now parsing a constant-expression.  */
6570   parser->integral_constant_expression_p = true;
6571   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6572   parser->non_integral_constant_expression_p = false;
6573   /* Although the grammar says "conditional-expression", we parse an
6574      "assignment-expression", which also permits "throw-expression"
6575      and the use of assignment operators.  In the case that
6576      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6577      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6578      actually essential that we look for an assignment-expression.
6579      For example, cp_parser_initializer_clauses uses this function to
6580      determine whether a particular assignment-expression is in fact
6581      constant.  */
6582   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6583   /* Restore the old settings.  */
6584   parser->integral_constant_expression_p
6585     = saved_integral_constant_expression_p;
6586   parser->allow_non_integral_constant_expression_p
6587     = saved_allow_non_integral_constant_expression_p;
6588   if (allow_non_constant_p)
6589     *non_constant_p = parser->non_integral_constant_expression_p;
6590   else if (parser->non_integral_constant_expression_p)
6591     expression = error_mark_node;
6592   parser->non_integral_constant_expression_p
6593     = saved_non_integral_constant_expression_p;
6594
6595   return expression;
6596 }
6597
6598 /* Parse __builtin_offsetof.
6599
6600    offsetof-expression:
6601      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6602
6603    offsetof-member-designator:
6604      id-expression
6605      | offsetof-member-designator "." id-expression
6606      | offsetof-member-designator "[" expression "]"  */
6607
6608 static tree
6609 cp_parser_builtin_offsetof (cp_parser *parser)
6610 {
6611   int save_ice_p, save_non_ice_p;
6612   tree type, expr;
6613   cp_id_kind dummy;
6614   cp_token *token;
6615
6616   /* We're about to accept non-integral-constant things, but will
6617      definitely yield an integral constant expression.  Save and
6618      restore these values around our local parsing.  */
6619   save_ice_p = parser->integral_constant_expression_p;
6620   save_non_ice_p = parser->non_integral_constant_expression_p;
6621
6622   /* Consume the "__builtin_offsetof" token.  */
6623   cp_lexer_consume_token (parser->lexer);
6624   /* Consume the opening `('.  */
6625   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6626   /* Parse the type-id.  */
6627   type = cp_parser_type_id (parser);
6628   /* Look for the `,'.  */
6629   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6630   token = cp_lexer_peek_token (parser->lexer);
6631
6632   /* Build the (type *)null that begins the traditional offsetof macro.  */
6633   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6634                             tf_warning_or_error);
6635
6636   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6637   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6638                                                  true, &dummy, token->location);
6639   while (true)
6640     {
6641       token = cp_lexer_peek_token (parser->lexer);
6642       switch (token->type)
6643         {
6644         case CPP_OPEN_SQUARE:
6645           /* offsetof-member-designator "[" expression "]" */
6646           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6647           break;
6648
6649         case CPP_DOT:
6650           /* offsetof-member-designator "." identifier */
6651           cp_lexer_consume_token (parser->lexer);
6652           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6653                                                          true, &dummy,
6654                                                          token->location);
6655           break;
6656
6657         case CPP_CLOSE_PAREN:
6658           /* Consume the ")" token.  */
6659           cp_lexer_consume_token (parser->lexer);
6660           goto success;
6661
6662         default:
6663           /* Error.  We know the following require will fail, but
6664              that gives the proper error message.  */
6665           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6666           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6667           expr = error_mark_node;
6668           goto failure;
6669         }
6670     }
6671
6672  success:
6673   /* If we're processing a template, we can't finish the semantics yet.
6674      Otherwise we can fold the entire expression now.  */
6675   if (processing_template_decl)
6676     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6677   else
6678     expr = finish_offsetof (expr);
6679
6680  failure:
6681   parser->integral_constant_expression_p = save_ice_p;
6682   parser->non_integral_constant_expression_p = save_non_ice_p;
6683
6684   return expr;
6685 }
6686
6687 /* Parse a trait expression.  */
6688
6689 static tree
6690 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6691 {
6692   cp_trait_kind kind;
6693   tree type1, type2 = NULL_TREE;
6694   bool binary = false;
6695   cp_decl_specifier_seq decl_specs;
6696
6697   switch (keyword)
6698     {
6699     case RID_HAS_NOTHROW_ASSIGN:
6700       kind = CPTK_HAS_NOTHROW_ASSIGN;
6701       break;
6702     case RID_HAS_NOTHROW_CONSTRUCTOR:
6703       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6704       break;
6705     case RID_HAS_NOTHROW_COPY:
6706       kind = CPTK_HAS_NOTHROW_COPY;
6707       break;
6708     case RID_HAS_TRIVIAL_ASSIGN:
6709       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6710       break;
6711     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6712       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6713       break;
6714     case RID_HAS_TRIVIAL_COPY:
6715       kind = CPTK_HAS_TRIVIAL_COPY;
6716       break;
6717     case RID_HAS_TRIVIAL_DESTRUCTOR:
6718       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6719       break;
6720     case RID_HAS_VIRTUAL_DESTRUCTOR:
6721       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6722       break;
6723     case RID_IS_ABSTRACT:
6724       kind = CPTK_IS_ABSTRACT;
6725       break;
6726     case RID_IS_BASE_OF:
6727       kind = CPTK_IS_BASE_OF;
6728       binary = true;
6729       break;
6730     case RID_IS_CLASS:
6731       kind = CPTK_IS_CLASS;
6732       break;
6733     case RID_IS_CONVERTIBLE_TO:
6734       kind = CPTK_IS_CONVERTIBLE_TO;
6735       binary = true;
6736       break;
6737     case RID_IS_EMPTY:
6738       kind = CPTK_IS_EMPTY;
6739       break;
6740     case RID_IS_ENUM:
6741       kind = CPTK_IS_ENUM;
6742       break;
6743     case RID_IS_POD:
6744       kind = CPTK_IS_POD;
6745       break;
6746     case RID_IS_POLYMORPHIC:
6747       kind = CPTK_IS_POLYMORPHIC;
6748       break;
6749     case RID_IS_UNION:
6750       kind = CPTK_IS_UNION;
6751       break;
6752     default:
6753       gcc_unreachable ();
6754     }
6755
6756   /* Consume the token.  */
6757   cp_lexer_consume_token (parser->lexer);
6758
6759   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6760
6761   type1 = cp_parser_type_id (parser);
6762
6763   if (type1 == error_mark_node)
6764     return error_mark_node;
6765
6766   /* Build a trivial decl-specifier-seq.  */
6767   clear_decl_specs (&decl_specs);
6768   decl_specs.type = type1;
6769
6770   /* Call grokdeclarator to figure out what type this is.  */
6771   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6772                           /*initialized=*/0, /*attrlist=*/NULL);
6773
6774   if (binary)
6775     {
6776       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6777  
6778       type2 = cp_parser_type_id (parser);
6779
6780       if (type2 == error_mark_node)
6781         return error_mark_node;
6782
6783       /* Build a trivial decl-specifier-seq.  */
6784       clear_decl_specs (&decl_specs);
6785       decl_specs.type = type2;
6786
6787       /* Call grokdeclarator to figure out what type this is.  */
6788       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6789                               /*initialized=*/0, /*attrlist=*/NULL);
6790     }
6791
6792   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6793
6794   /* Complete the trait expression, which may mean either processing
6795      the trait expr now or saving it for template instantiation.  */
6796   return finish_trait_expr (kind, type1, type2);
6797 }
6798
6799 /* Statements [gram.stmt.stmt]  */
6800
6801 /* Parse a statement.
6802
6803    statement:
6804      labeled-statement
6805      expression-statement
6806      compound-statement
6807      selection-statement
6808      iteration-statement
6809      jump-statement
6810      declaration-statement
6811      try-block
6812
6813   IN_COMPOUND is true when the statement is nested inside a
6814   cp_parser_compound_statement; this matters for certain pragmas.
6815
6816   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6817   is a (possibly labeled) if statement which is not enclosed in braces
6818   and has an else clause.  This is used to implement -Wparentheses.  */
6819
6820 static void
6821 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6822                      bool in_compound, bool *if_p)
6823 {
6824   tree statement;
6825   cp_token *token;
6826   location_t statement_location;
6827
6828  restart:
6829   if (if_p != NULL)
6830     *if_p = false;
6831   /* There is no statement yet.  */
6832   statement = NULL_TREE;
6833   /* Peek at the next token.  */
6834   token = cp_lexer_peek_token (parser->lexer);
6835   /* Remember the location of the first token in the statement.  */
6836   statement_location = token->location;
6837   /* If this is a keyword, then that will often determine what kind of
6838      statement we have.  */
6839   if (token->type == CPP_KEYWORD)
6840     {
6841       enum rid keyword = token->keyword;
6842
6843       switch (keyword)
6844         {
6845         case RID_CASE:
6846         case RID_DEFAULT:
6847           /* Looks like a labeled-statement with a case label.
6848              Parse the label, and then use tail recursion to parse
6849              the statement.  */
6850           cp_parser_label_for_labeled_statement (parser);
6851           goto restart;
6852
6853         case RID_IF:
6854         case RID_SWITCH:
6855           statement = cp_parser_selection_statement (parser, if_p);
6856           break;
6857
6858         case RID_WHILE:
6859         case RID_DO:
6860         case RID_FOR:
6861           statement = cp_parser_iteration_statement (parser);
6862           break;
6863
6864         case RID_BREAK:
6865         case RID_CONTINUE:
6866         case RID_RETURN:
6867         case RID_GOTO:
6868           statement = cp_parser_jump_statement (parser);
6869           break;
6870
6871           /* Objective-C++ exception-handling constructs.  */
6872         case RID_AT_TRY:
6873         case RID_AT_CATCH:
6874         case RID_AT_FINALLY:
6875         case RID_AT_SYNCHRONIZED:
6876         case RID_AT_THROW:
6877           statement = cp_parser_objc_statement (parser);
6878           break;
6879
6880         case RID_TRY:
6881           statement = cp_parser_try_block (parser);
6882           break;
6883
6884         case RID_NAMESPACE:
6885           /* This must be a namespace alias definition.  */
6886           cp_parser_declaration_statement (parser);
6887           return;
6888           
6889         default:
6890           /* It might be a keyword like `int' that can start a
6891              declaration-statement.  */
6892           break;
6893         }
6894     }
6895   else if (token->type == CPP_NAME)
6896     {
6897       /* If the next token is a `:', then we are looking at a
6898          labeled-statement.  */
6899       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6900       if (token->type == CPP_COLON)
6901         {
6902           /* Looks like a labeled-statement with an ordinary label.
6903              Parse the label, and then use tail recursion to parse
6904              the statement.  */
6905           cp_parser_label_for_labeled_statement (parser);
6906           goto restart;
6907         }
6908     }
6909   /* Anything that starts with a `{' must be a compound-statement.  */
6910   else if (token->type == CPP_OPEN_BRACE)
6911     statement = cp_parser_compound_statement (parser, NULL, false);
6912   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6913      a statement all its own.  */
6914   else if (token->type == CPP_PRAGMA)
6915     {
6916       /* Only certain OpenMP pragmas are attached to statements, and thus
6917          are considered statements themselves.  All others are not.  In
6918          the context of a compound, accept the pragma as a "statement" and
6919          return so that we can check for a close brace.  Otherwise we
6920          require a real statement and must go back and read one.  */
6921       if (in_compound)
6922         cp_parser_pragma (parser, pragma_compound);
6923       else if (!cp_parser_pragma (parser, pragma_stmt))
6924         goto restart;
6925       return;
6926     }
6927   else if (token->type == CPP_EOF)
6928     {
6929       cp_parser_error (parser, "expected statement");
6930       return;
6931     }
6932
6933   /* Everything else must be a declaration-statement or an
6934      expression-statement.  Try for the declaration-statement
6935      first, unless we are looking at a `;', in which case we know that
6936      we have an expression-statement.  */
6937   if (!statement)
6938     {
6939       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6940         {
6941           cp_parser_parse_tentatively (parser);
6942           /* Try to parse the declaration-statement.  */
6943           cp_parser_declaration_statement (parser);
6944           /* If that worked, we're done.  */
6945           if (cp_parser_parse_definitely (parser))
6946             return;
6947         }
6948       /* Look for an expression-statement instead.  */
6949       statement = cp_parser_expression_statement (parser, in_statement_expr);
6950     }
6951
6952   /* Set the line number for the statement.  */
6953   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6954     SET_EXPR_LOCATION (statement, statement_location);
6955 }
6956
6957 /* Parse the label for a labeled-statement, i.e.
6958
6959    identifier :
6960    case constant-expression :
6961    default :
6962
6963    GNU Extension:
6964    case constant-expression ... constant-expression : statement
6965
6966    When a label is parsed without errors, the label is added to the
6967    parse tree by the finish_* functions, so this function doesn't
6968    have to return the label.  */
6969
6970 static void
6971 cp_parser_label_for_labeled_statement (cp_parser* parser)
6972 {
6973   cp_token *token;
6974
6975   /* The next token should be an identifier.  */
6976   token = cp_lexer_peek_token (parser->lexer);
6977   if (token->type != CPP_NAME
6978       && token->type != CPP_KEYWORD)
6979     {
6980       cp_parser_error (parser, "expected labeled-statement");
6981       return;
6982     }
6983
6984   switch (token->keyword)
6985     {
6986     case RID_CASE:
6987       {
6988         tree expr, expr_hi;
6989         cp_token *ellipsis;
6990
6991         /* Consume the `case' token.  */
6992         cp_lexer_consume_token (parser->lexer);
6993         /* Parse the constant-expression.  */
6994         expr = cp_parser_constant_expression (parser,
6995                                               /*allow_non_constant_p=*/false,
6996                                               NULL);
6997
6998         ellipsis = cp_lexer_peek_token (parser->lexer);
6999         if (ellipsis->type == CPP_ELLIPSIS)
7000           {
7001             /* Consume the `...' token.  */
7002             cp_lexer_consume_token (parser->lexer);
7003             expr_hi =
7004               cp_parser_constant_expression (parser,
7005                                              /*allow_non_constant_p=*/false,
7006                                              NULL);
7007             /* We don't need to emit warnings here, as the common code
7008                will do this for us.  */
7009           }
7010         else
7011           expr_hi = NULL_TREE;
7012
7013         if (parser->in_switch_statement_p)
7014           finish_case_label (expr, expr_hi);
7015         else
7016           error ("%Hcase label %qE not within a switch statement",
7017                  &token->location, expr);
7018       }
7019       break;
7020
7021     case RID_DEFAULT:
7022       /* Consume the `default' token.  */
7023       cp_lexer_consume_token (parser->lexer);
7024
7025       if (parser->in_switch_statement_p)
7026         finish_case_label (NULL_TREE, NULL_TREE);
7027       else
7028         error ("%Hcase label not within a switch statement", &token->location);
7029       break;
7030
7031     default:
7032       /* Anything else must be an ordinary label.  */
7033       finish_label_stmt (cp_parser_identifier (parser));
7034       break;
7035     }
7036
7037   /* Require the `:' token.  */
7038   cp_parser_require (parser, CPP_COLON, "%<:%>");
7039 }
7040
7041 /* Parse an expression-statement.
7042
7043    expression-statement:
7044      expression [opt] ;
7045
7046    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7047    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7048    indicates whether this expression-statement is part of an
7049    expression statement.  */
7050
7051 static tree
7052 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7053 {
7054   tree statement = NULL_TREE;
7055
7056   /* If the next token is a ';', then there is no expression
7057      statement.  */
7058   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7059     statement = cp_parser_expression (parser, /*cast_p=*/false);
7060
7061   /* Consume the final `;'.  */
7062   cp_parser_consume_semicolon_at_end_of_statement (parser);
7063
7064   if (in_statement_expr
7065       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7066     /* This is the final expression statement of a statement
7067        expression.  */
7068     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7069   else if (statement)
7070     statement = finish_expr_stmt (statement);
7071   else
7072     finish_stmt ();
7073
7074   return statement;
7075 }
7076
7077 /* Parse a compound-statement.
7078
7079    compound-statement:
7080      { statement-seq [opt] }
7081
7082    GNU extension:
7083
7084    compound-statement:
7085      { label-declaration-seq [opt] statement-seq [opt] }
7086
7087    label-declaration-seq:
7088      label-declaration
7089      label-declaration-seq label-declaration
7090
7091    Returns a tree representing the statement.  */
7092
7093 static tree
7094 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7095                               bool in_try)
7096 {
7097   tree compound_stmt;
7098
7099   /* Consume the `{'.  */
7100   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7101     return error_mark_node;
7102   /* Begin the compound-statement.  */
7103   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7104   /* If the next keyword is `__label__' we have a label declaration.  */
7105   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7106     cp_parser_label_declaration (parser);
7107   /* Parse an (optional) statement-seq.  */
7108   cp_parser_statement_seq_opt (parser, in_statement_expr);
7109   /* Finish the compound-statement.  */
7110   finish_compound_stmt (compound_stmt);
7111   /* Consume the `}'.  */
7112   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7113
7114   return compound_stmt;
7115 }
7116
7117 /* Parse an (optional) statement-seq.
7118
7119    statement-seq:
7120      statement
7121      statement-seq [opt] statement  */
7122
7123 static void
7124 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7125 {
7126   /* Scan statements until there aren't any more.  */
7127   while (true)
7128     {
7129       cp_token *token = cp_lexer_peek_token (parser->lexer);
7130
7131       /* If we're looking at a `}', then we've run out of statements.  */
7132       if (token->type == CPP_CLOSE_BRACE
7133           || token->type == CPP_EOF
7134           || token->type == CPP_PRAGMA_EOL)
7135         break;
7136       
7137       /* If we are in a compound statement and find 'else' then
7138          something went wrong.  */
7139       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7140         {
7141           if (parser->in_statement & IN_IF_STMT) 
7142             break;
7143           else
7144             {
7145               token = cp_lexer_consume_token (parser->lexer);
7146               error ("%H%<else%> without a previous %<if%>", &token->location);
7147             }
7148         }
7149
7150       /* Parse the statement.  */
7151       cp_parser_statement (parser, in_statement_expr, true, NULL);
7152     }
7153 }
7154
7155 /* Parse a selection-statement.
7156
7157    selection-statement:
7158      if ( condition ) statement
7159      if ( condition ) statement else statement
7160      switch ( condition ) statement
7161
7162    Returns the new IF_STMT or SWITCH_STMT.
7163
7164    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7165    is a (possibly labeled) if statement which is not enclosed in
7166    braces and has an else clause.  This is used to implement
7167    -Wparentheses.  */
7168
7169 static tree
7170 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7171 {
7172   cp_token *token;
7173   enum rid keyword;
7174
7175   if (if_p != NULL)
7176     *if_p = false;
7177
7178   /* Peek at the next token.  */
7179   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7180
7181   /* See what kind of keyword it is.  */
7182   keyword = token->keyword;
7183   switch (keyword)
7184     {
7185     case RID_IF:
7186     case RID_SWITCH:
7187       {
7188         tree statement;
7189         tree condition;
7190
7191         /* Look for the `('.  */
7192         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7193           {
7194             cp_parser_skip_to_end_of_statement (parser);
7195             return error_mark_node;
7196           }
7197
7198         /* Begin the selection-statement.  */
7199         if (keyword == RID_IF)
7200           statement = begin_if_stmt ();
7201         else
7202           statement = begin_switch_stmt ();
7203
7204         /* Parse the condition.  */
7205         condition = cp_parser_condition (parser);
7206         /* Look for the `)'.  */
7207         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7208           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7209                                                  /*consume_paren=*/true);
7210
7211         if (keyword == RID_IF)
7212           {
7213             bool nested_if;
7214             unsigned char in_statement;
7215
7216             /* Add the condition.  */
7217             finish_if_stmt_cond (condition, statement);
7218
7219             /* Parse the then-clause.  */
7220             in_statement = parser->in_statement;
7221             parser->in_statement |= IN_IF_STMT;
7222             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7223               {
7224                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7225                 add_stmt (build_empty_stmt ());
7226                 cp_lexer_consume_token (parser->lexer);
7227                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7228                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7229                               "empty body in an %<if%> statement");
7230                 nested_if = false;
7231               }
7232             else
7233               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7234             parser->in_statement = in_statement;
7235
7236             finish_then_clause (statement);
7237
7238             /* If the next token is `else', parse the else-clause.  */
7239             if (cp_lexer_next_token_is_keyword (parser->lexer,
7240                                                 RID_ELSE))
7241               {
7242                 /* Consume the `else' keyword.  */
7243                 cp_lexer_consume_token (parser->lexer);
7244                 begin_else_clause (statement);
7245                 /* Parse the else-clause.  */
7246                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7247                   {
7248                     warning_at (cp_lexer_peek_token (parser->lexer)->location,
7249                                 OPT_Wempty_body, "suggest braces around "
7250                                 "empty body in an %<else%> statement");
7251                     add_stmt (build_empty_stmt ());
7252                     cp_lexer_consume_token (parser->lexer);
7253                   }
7254                 else
7255                   cp_parser_implicitly_scoped_statement (parser, NULL);
7256
7257                 finish_else_clause (statement);
7258
7259                 /* If we are currently parsing a then-clause, then
7260                    IF_P will not be NULL.  We set it to true to
7261                    indicate that this if statement has an else clause.
7262                    This may trigger the Wparentheses warning below
7263                    when we get back up to the parent if statement.  */
7264                 if (if_p != NULL)
7265                   *if_p = true;
7266               }
7267             else
7268               {
7269                 /* This if statement does not have an else clause.  If
7270                    NESTED_IF is true, then the then-clause is an if
7271                    statement which does have an else clause.  We warn
7272                    about the potential ambiguity.  */
7273                 if (nested_if)
7274                   warning (OPT_Wparentheses,
7275                            ("%Hsuggest explicit braces "
7276                             "to avoid ambiguous %<else%>"),
7277                            EXPR_LOCUS (statement));
7278               }
7279
7280             /* Now we're all done with the if-statement.  */
7281             finish_if_stmt (statement);
7282           }
7283         else
7284           {
7285             bool in_switch_statement_p;
7286             unsigned char in_statement;
7287
7288             /* Add the condition.  */
7289             finish_switch_cond (condition, statement);
7290
7291             /* Parse the body of the switch-statement.  */
7292             in_switch_statement_p = parser->in_switch_statement_p;
7293             in_statement = parser->in_statement;
7294             parser->in_switch_statement_p = true;
7295             parser->in_statement |= IN_SWITCH_STMT;
7296             cp_parser_implicitly_scoped_statement (parser, NULL);
7297             parser->in_switch_statement_p = in_switch_statement_p;
7298             parser->in_statement = in_statement;
7299
7300             /* Now we're all done with the switch-statement.  */
7301             finish_switch_stmt (statement);
7302           }
7303
7304         return statement;
7305       }
7306       break;
7307
7308     default:
7309       cp_parser_error (parser, "expected selection-statement");
7310       return error_mark_node;
7311     }
7312 }
7313
7314 /* Parse a condition.
7315
7316    condition:
7317      expression
7318      type-specifier-seq declarator = initializer-clause
7319      type-specifier-seq declarator braced-init-list
7320
7321    GNU Extension:
7322
7323    condition:
7324      type-specifier-seq declarator asm-specification [opt]
7325        attributes [opt] = assignment-expression
7326
7327    Returns the expression that should be tested.  */
7328
7329 static tree
7330 cp_parser_condition (cp_parser* parser)
7331 {
7332   cp_decl_specifier_seq type_specifiers;
7333   const char *saved_message;
7334
7335   /* Try the declaration first.  */
7336   cp_parser_parse_tentatively (parser);
7337   /* New types are not allowed in the type-specifier-seq for a
7338      condition.  */
7339   saved_message = parser->type_definition_forbidden_message;
7340   parser->type_definition_forbidden_message
7341     = "types may not be defined in conditions";
7342   /* Parse the type-specifier-seq.  */
7343   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7344                                 &type_specifiers);
7345   /* Restore the saved message.  */
7346   parser->type_definition_forbidden_message = saved_message;
7347   /* If all is well, we might be looking at a declaration.  */
7348   if (!cp_parser_error_occurred (parser))
7349     {
7350       tree decl;
7351       tree asm_specification;
7352       tree attributes;
7353       cp_declarator *declarator;
7354       tree initializer = NULL_TREE;
7355
7356       /* Parse the declarator.  */
7357       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7358                                          /*ctor_dtor_or_conv_p=*/NULL,
7359                                          /*parenthesized_p=*/NULL,
7360                                          /*member_p=*/false);
7361       /* Parse the attributes.  */
7362       attributes = cp_parser_attributes_opt (parser);
7363       /* Parse the asm-specification.  */
7364       asm_specification = cp_parser_asm_specification_opt (parser);
7365       /* If the next token is not an `=' or '{', then we might still be
7366          looking at an expression.  For example:
7367
7368            if (A(a).x)
7369
7370          looks like a decl-specifier-seq and a declarator -- but then
7371          there is no `=', so this is an expression.  */
7372       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7373           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7374         cp_parser_simulate_error (parser);
7375         
7376       /* If we did see an `=' or '{', then we are looking at a declaration
7377          for sure.  */
7378       if (cp_parser_parse_definitely (parser))
7379         {
7380           tree pushed_scope;
7381           bool non_constant_p;
7382           bool flags = LOOKUP_ONLYCONVERTING;
7383
7384           /* Create the declaration.  */
7385           decl = start_decl (declarator, &type_specifiers,
7386                              /*initialized_p=*/true,
7387                              attributes, /*prefix_attributes=*/NULL_TREE,
7388                              &pushed_scope);
7389
7390           /* Parse the initializer.  */
7391           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7392             {
7393               initializer = cp_parser_braced_list (parser, &non_constant_p);
7394               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7395               flags = 0;
7396             }
7397           else
7398             {
7399               /* Consume the `='.  */
7400               cp_lexer_consume_token (parser->lexer);
7401               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7402             }
7403           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7404             maybe_warn_cpp0x ("extended initializer lists");
7405
7406           if (!non_constant_p)
7407             initializer = fold_non_dependent_expr (initializer);
7408
7409           /* Process the initializer.  */
7410           cp_finish_decl (decl,
7411                           initializer, !non_constant_p,
7412                           asm_specification,
7413                           flags);
7414
7415           if (pushed_scope)
7416             pop_scope (pushed_scope);
7417
7418           return convert_from_reference (decl);
7419         }
7420     }
7421   /* If we didn't even get past the declarator successfully, we are
7422      definitely not looking at a declaration.  */
7423   else
7424     cp_parser_abort_tentative_parse (parser);
7425
7426   /* Otherwise, we are looking at an expression.  */
7427   return cp_parser_expression (parser, /*cast_p=*/false);
7428 }
7429
7430 /* Parse an iteration-statement.
7431
7432    iteration-statement:
7433      while ( condition ) statement
7434      do statement while ( expression ) ;
7435      for ( for-init-statement condition [opt] ; expression [opt] )
7436        statement
7437
7438    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7439
7440 static tree
7441 cp_parser_iteration_statement (cp_parser* parser)
7442 {
7443   cp_token *token;
7444   enum rid keyword;
7445   tree statement;
7446   unsigned char in_statement;
7447
7448   /* Peek at the next token.  */
7449   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7450   if (!token)
7451     return error_mark_node;
7452
7453   /* Remember whether or not we are already within an iteration
7454      statement.  */
7455   in_statement = parser->in_statement;
7456
7457   /* See what kind of keyword it is.  */
7458   keyword = token->keyword;
7459   switch (keyword)
7460     {
7461     case RID_WHILE:
7462       {
7463         tree condition;
7464
7465         /* Begin the while-statement.  */
7466         statement = begin_while_stmt ();
7467         /* Look for the `('.  */
7468         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7469         /* Parse the condition.  */
7470         condition = cp_parser_condition (parser);
7471         finish_while_stmt_cond (condition, statement);
7472         /* Look for the `)'.  */
7473         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7474         /* Parse the dependent statement.  */
7475         parser->in_statement = IN_ITERATION_STMT;
7476         cp_parser_already_scoped_statement (parser);
7477         parser->in_statement = in_statement;
7478         /* We're done with the while-statement.  */
7479         finish_while_stmt (statement);
7480       }
7481       break;
7482
7483     case RID_DO:
7484       {
7485         tree expression;
7486
7487         /* Begin the do-statement.  */
7488         statement = begin_do_stmt ();
7489         /* Parse the body of the do-statement.  */
7490         parser->in_statement = IN_ITERATION_STMT;
7491         cp_parser_implicitly_scoped_statement (parser, NULL);
7492         parser->in_statement = in_statement;
7493         finish_do_body (statement);
7494         /* Look for the `while' keyword.  */
7495         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7496         /* Look for the `('.  */
7497         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7498         /* Parse the expression.  */
7499         expression = cp_parser_expression (parser, /*cast_p=*/false);
7500         /* We're done with the do-statement.  */
7501         finish_do_stmt (expression, statement);
7502         /* Look for the `)'.  */
7503         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7504         /* Look for the `;'.  */
7505         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7506       }
7507       break;
7508
7509     case RID_FOR:
7510       {
7511         tree condition = NULL_TREE;
7512         tree expression = NULL_TREE;
7513
7514         /* Begin the for-statement.  */
7515         statement = begin_for_stmt ();
7516         /* Look for the `('.  */
7517         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7518         /* Parse the initialization.  */
7519         cp_parser_for_init_statement (parser);
7520         finish_for_init_stmt (statement);
7521
7522         /* If there's a condition, process it.  */
7523         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7524           condition = cp_parser_condition (parser);
7525         finish_for_cond (condition, statement);
7526         /* Look for the `;'.  */
7527         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7528
7529         /* If there's an expression, process it.  */
7530         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7531           expression = cp_parser_expression (parser, /*cast_p=*/false);
7532         finish_for_expr (expression, statement);
7533         /* Look for the `)'.  */
7534         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7535
7536         /* Parse the body of the for-statement.  */
7537         parser->in_statement = IN_ITERATION_STMT;
7538         cp_parser_already_scoped_statement (parser);
7539         parser->in_statement = in_statement;
7540
7541         /* We're done with the for-statement.  */
7542         finish_for_stmt (statement);
7543       }
7544       break;
7545
7546     default:
7547       cp_parser_error (parser, "expected iteration-statement");
7548       statement = error_mark_node;
7549       break;
7550     }
7551
7552   return statement;
7553 }
7554
7555 /* Parse a for-init-statement.
7556
7557    for-init-statement:
7558      expression-statement
7559      simple-declaration  */
7560
7561 static void
7562 cp_parser_for_init_statement (cp_parser* parser)
7563 {
7564   /* If the next token is a `;', then we have an empty
7565      expression-statement.  Grammatically, this is also a
7566      simple-declaration, but an invalid one, because it does not
7567      declare anything.  Therefore, if we did not handle this case
7568      specially, we would issue an error message about an invalid
7569      declaration.  */
7570   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7571     {
7572       /* We're going to speculatively look for a declaration, falling back
7573          to an expression, if necessary.  */
7574       cp_parser_parse_tentatively (parser);
7575       /* Parse the declaration.  */
7576       cp_parser_simple_declaration (parser,
7577                                     /*function_definition_allowed_p=*/false);
7578       /* If the tentative parse failed, then we shall need to look for an
7579          expression-statement.  */
7580       if (cp_parser_parse_definitely (parser))
7581         return;
7582     }
7583
7584   cp_parser_expression_statement (parser, false);
7585 }
7586
7587 /* Parse a jump-statement.
7588
7589    jump-statement:
7590      break ;
7591      continue ;
7592      return expression [opt] ;
7593      return braced-init-list ;
7594      goto identifier ;
7595
7596    GNU extension:
7597
7598    jump-statement:
7599      goto * expression ;
7600
7601    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7602
7603 static tree
7604 cp_parser_jump_statement (cp_parser* parser)
7605 {
7606   tree statement = error_mark_node;
7607   cp_token *token;
7608   enum rid keyword;
7609   unsigned char in_statement;
7610
7611   /* Peek at the next token.  */
7612   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7613   if (!token)
7614     return error_mark_node;
7615
7616   /* See what kind of keyword it is.  */
7617   keyword = token->keyword;
7618   switch (keyword)
7619     {
7620     case RID_BREAK:
7621       in_statement = parser->in_statement & ~IN_IF_STMT;      
7622       switch (in_statement)
7623         {
7624         case 0:
7625           error ("%Hbreak statement not within loop or switch", &token->location);
7626           break;
7627         default:
7628           gcc_assert ((in_statement & IN_SWITCH_STMT)
7629                       || in_statement == IN_ITERATION_STMT);
7630           statement = finish_break_stmt ();
7631           break;
7632         case IN_OMP_BLOCK:
7633           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7634           break;
7635         case IN_OMP_FOR:
7636           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7637           break;
7638         }
7639       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7640       break;
7641
7642     case RID_CONTINUE:
7643       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7644         {
7645         case 0:
7646           error ("%Hcontinue statement not within a loop", &token->location);
7647           break;
7648         case IN_ITERATION_STMT:
7649         case IN_OMP_FOR:
7650           statement = finish_continue_stmt ();
7651           break;
7652         case IN_OMP_BLOCK:
7653           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7654           break;
7655         default:
7656           gcc_unreachable ();
7657         }
7658       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7659       break;
7660
7661     case RID_RETURN:
7662       {
7663         tree expr;
7664         bool expr_non_constant_p;
7665
7666         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7667           {
7668             maybe_warn_cpp0x ("extended initializer lists");
7669             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7670           }
7671         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7672           expr = cp_parser_expression (parser, /*cast_p=*/false);
7673         else
7674           /* If the next token is a `;', then there is no
7675              expression.  */
7676           expr = NULL_TREE;
7677         /* Build the return-statement.  */
7678         statement = finish_return_stmt (expr);
7679         /* Look for the final `;'.  */
7680         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7681       }
7682       break;
7683
7684     case RID_GOTO:
7685       /* Create the goto-statement.  */
7686       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7687         {
7688           /* Issue a warning about this use of a GNU extension.  */
7689           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7690           /* Consume the '*' token.  */
7691           cp_lexer_consume_token (parser->lexer);
7692           /* Parse the dependent expression.  */
7693           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7694         }
7695       else
7696         finish_goto_stmt (cp_parser_identifier (parser));
7697       /* Look for the final `;'.  */
7698       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7699       break;
7700
7701     default:
7702       cp_parser_error (parser, "expected jump-statement");
7703       break;
7704     }
7705
7706   return statement;
7707 }
7708
7709 /* Parse a declaration-statement.
7710
7711    declaration-statement:
7712      block-declaration  */
7713
7714 static void
7715 cp_parser_declaration_statement (cp_parser* parser)
7716 {
7717   void *p;
7718
7719   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7720   p = obstack_alloc (&declarator_obstack, 0);
7721
7722  /* Parse the block-declaration.  */
7723   cp_parser_block_declaration (parser, /*statement_p=*/true);
7724
7725   /* Free any declarators allocated.  */
7726   obstack_free (&declarator_obstack, p);
7727
7728   /* Finish off the statement.  */
7729   finish_stmt ();
7730 }
7731
7732 /* Some dependent statements (like `if (cond) statement'), are
7733    implicitly in their own scope.  In other words, if the statement is
7734    a single statement (as opposed to a compound-statement), it is
7735    none-the-less treated as if it were enclosed in braces.  Any
7736    declarations appearing in the dependent statement are out of scope
7737    after control passes that point.  This function parses a statement,
7738    but ensures that is in its own scope, even if it is not a
7739    compound-statement.
7740
7741    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7742    is a (possibly labeled) if statement which is not enclosed in
7743    braces and has an else clause.  This is used to implement
7744    -Wparentheses.
7745
7746    Returns the new statement.  */
7747
7748 static tree
7749 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7750 {
7751   tree statement;
7752
7753   if (if_p != NULL)
7754     *if_p = false;
7755
7756   /* Mark if () ; with a special NOP_EXPR.  */
7757   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7758     {
7759       cp_lexer_consume_token (parser->lexer);
7760       statement = add_stmt (build_empty_stmt ());
7761     }
7762   /* if a compound is opened, we simply parse the statement directly.  */
7763   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7764     statement = cp_parser_compound_statement (parser, NULL, false);
7765   /* If the token is not a `{', then we must take special action.  */
7766   else
7767     {
7768       /* Create a compound-statement.  */
7769       statement = begin_compound_stmt (0);
7770       /* Parse the dependent-statement.  */
7771       cp_parser_statement (parser, NULL_TREE, false, if_p);
7772       /* Finish the dummy compound-statement.  */
7773       finish_compound_stmt (statement);
7774     }
7775
7776   /* Return the statement.  */
7777   return statement;
7778 }
7779
7780 /* For some dependent statements (like `while (cond) statement'), we
7781    have already created a scope.  Therefore, even if the dependent
7782    statement is a compound-statement, we do not want to create another
7783    scope.  */
7784
7785 static void
7786 cp_parser_already_scoped_statement (cp_parser* parser)
7787 {
7788   /* If the token is a `{', then we must take special action.  */
7789   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7790     cp_parser_statement (parser, NULL_TREE, false, NULL);
7791   else
7792     {
7793       /* Avoid calling cp_parser_compound_statement, so that we
7794          don't create a new scope.  Do everything else by hand.  */
7795       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7796       cp_parser_statement_seq_opt (parser, NULL_TREE);
7797       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7798     }
7799 }
7800
7801 /* Declarations [gram.dcl.dcl] */
7802
7803 /* Parse an optional declaration-sequence.
7804
7805    declaration-seq:
7806      declaration
7807      declaration-seq declaration  */
7808
7809 static void
7810 cp_parser_declaration_seq_opt (cp_parser* parser)
7811 {
7812   while (true)
7813     {
7814       cp_token *token;
7815
7816       token = cp_lexer_peek_token (parser->lexer);
7817
7818       if (token->type == CPP_CLOSE_BRACE
7819           || token->type == CPP_EOF
7820           || token->type == CPP_PRAGMA_EOL)
7821         break;
7822
7823       if (token->type == CPP_SEMICOLON)
7824         {
7825           /* A declaration consisting of a single semicolon is
7826              invalid.  Allow it unless we're being pedantic.  */
7827           cp_lexer_consume_token (parser->lexer);
7828           if (!in_system_header)
7829             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7830           continue;
7831         }
7832
7833       /* If we're entering or exiting a region that's implicitly
7834          extern "C", modify the lang context appropriately.  */
7835       if (!parser->implicit_extern_c && token->implicit_extern_c)
7836         {
7837           push_lang_context (lang_name_c);
7838           parser->implicit_extern_c = true;
7839         }
7840       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7841         {
7842           pop_lang_context ();
7843           parser->implicit_extern_c = false;
7844         }
7845
7846       if (token->type == CPP_PRAGMA)
7847         {
7848           /* A top-level declaration can consist solely of a #pragma.
7849              A nested declaration cannot, so this is done here and not
7850              in cp_parser_declaration.  (A #pragma at block scope is
7851              handled in cp_parser_statement.)  */
7852           cp_parser_pragma (parser, pragma_external);
7853           continue;
7854         }
7855
7856       /* Parse the declaration itself.  */
7857       cp_parser_declaration (parser);
7858     }
7859 }
7860
7861 /* Parse a declaration.
7862
7863    declaration:
7864      block-declaration
7865      function-definition
7866      template-declaration
7867      explicit-instantiation
7868      explicit-specialization
7869      linkage-specification
7870      namespace-definition
7871
7872    GNU extension:
7873
7874    declaration:
7875       __extension__ declaration */
7876
7877 static void
7878 cp_parser_declaration (cp_parser* parser)
7879 {
7880   cp_token token1;
7881   cp_token token2;
7882   int saved_pedantic;
7883   void *p;
7884
7885   /* Check for the `__extension__' keyword.  */
7886   if (cp_parser_extension_opt (parser, &saved_pedantic))
7887     {
7888       /* Parse the qualified declaration.  */
7889       cp_parser_declaration (parser);
7890       /* Restore the PEDANTIC flag.  */
7891       pedantic = saved_pedantic;
7892
7893       return;
7894     }
7895
7896   /* Try to figure out what kind of declaration is present.  */
7897   token1 = *cp_lexer_peek_token (parser->lexer);
7898
7899   if (token1.type != CPP_EOF)
7900     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7901   else
7902     {
7903       token2.type = CPP_EOF;
7904       token2.keyword = RID_MAX;
7905     }
7906
7907   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7908   p = obstack_alloc (&declarator_obstack, 0);
7909
7910   /* If the next token is `extern' and the following token is a string
7911      literal, then we have a linkage specification.  */
7912   if (token1.keyword == RID_EXTERN
7913       && cp_parser_is_string_literal (&token2))
7914     cp_parser_linkage_specification (parser);
7915   /* If the next token is `template', then we have either a template
7916      declaration, an explicit instantiation, or an explicit
7917      specialization.  */
7918   else if (token1.keyword == RID_TEMPLATE)
7919     {
7920       /* `template <>' indicates a template specialization.  */
7921       if (token2.type == CPP_LESS
7922           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7923         cp_parser_explicit_specialization (parser);
7924       /* `template <' indicates a template declaration.  */
7925       else if (token2.type == CPP_LESS)
7926         cp_parser_template_declaration (parser, /*member_p=*/false);
7927       /* Anything else must be an explicit instantiation.  */
7928       else
7929         cp_parser_explicit_instantiation (parser);
7930     }
7931   /* If the next token is `export', then we have a template
7932      declaration.  */
7933   else if (token1.keyword == RID_EXPORT)
7934     cp_parser_template_declaration (parser, /*member_p=*/false);
7935   /* If the next token is `extern', 'static' or 'inline' and the one
7936      after that is `template', we have a GNU extended explicit
7937      instantiation directive.  */
7938   else if (cp_parser_allow_gnu_extensions_p (parser)
7939            && (token1.keyword == RID_EXTERN
7940                || token1.keyword == RID_STATIC
7941                || token1.keyword == RID_INLINE)
7942            && token2.keyword == RID_TEMPLATE)
7943     cp_parser_explicit_instantiation (parser);
7944   /* If the next token is `namespace', check for a named or unnamed
7945      namespace definition.  */
7946   else if (token1.keyword == RID_NAMESPACE
7947            && (/* A named namespace definition.  */
7948                (token2.type == CPP_NAME
7949                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7950                     != CPP_EQ))
7951                /* An unnamed namespace definition.  */
7952                || token2.type == CPP_OPEN_BRACE
7953                || token2.keyword == RID_ATTRIBUTE))
7954     cp_parser_namespace_definition (parser);
7955   /* An inline (associated) namespace definition.  */
7956   else if (token1.keyword == RID_INLINE
7957            && token2.keyword == RID_NAMESPACE)
7958     cp_parser_namespace_definition (parser);
7959   /* Objective-C++ declaration/definition.  */
7960   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7961     cp_parser_objc_declaration (parser);
7962   /* We must have either a block declaration or a function
7963      definition.  */
7964   else
7965     /* Try to parse a block-declaration, or a function-definition.  */
7966     cp_parser_block_declaration (parser, /*statement_p=*/false);
7967
7968   /* Free any declarators allocated.  */
7969   obstack_free (&declarator_obstack, p);
7970 }
7971
7972 /* Parse a block-declaration.
7973
7974    block-declaration:
7975      simple-declaration
7976      asm-definition
7977      namespace-alias-definition
7978      using-declaration
7979      using-directive
7980
7981    GNU Extension:
7982
7983    block-declaration:
7984      __extension__ block-declaration
7985
7986    C++0x Extension:
7987
7988    block-declaration:
7989      static_assert-declaration
7990
7991    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7992    part of a declaration-statement.  */
7993
7994 static void
7995 cp_parser_block_declaration (cp_parser *parser,
7996                              bool      statement_p)
7997 {
7998   cp_token *token1;
7999   int saved_pedantic;
8000
8001   /* Check for the `__extension__' keyword.  */
8002   if (cp_parser_extension_opt (parser, &saved_pedantic))
8003     {
8004       /* Parse the qualified declaration.  */
8005       cp_parser_block_declaration (parser, statement_p);
8006       /* Restore the PEDANTIC flag.  */
8007       pedantic = saved_pedantic;
8008
8009       return;
8010     }
8011
8012   /* Peek at the next token to figure out which kind of declaration is
8013      present.  */
8014   token1 = cp_lexer_peek_token (parser->lexer);
8015
8016   /* If the next keyword is `asm', we have an asm-definition.  */
8017   if (token1->keyword == RID_ASM)
8018     {
8019       if (statement_p)
8020         cp_parser_commit_to_tentative_parse (parser);
8021       cp_parser_asm_definition (parser);
8022     }
8023   /* If the next keyword is `namespace', we have a
8024      namespace-alias-definition.  */
8025   else if (token1->keyword == RID_NAMESPACE)
8026     cp_parser_namespace_alias_definition (parser);
8027   /* If the next keyword is `using', we have either a
8028      using-declaration or a using-directive.  */
8029   else if (token1->keyword == RID_USING)
8030     {
8031       cp_token *token2;
8032
8033       if (statement_p)
8034         cp_parser_commit_to_tentative_parse (parser);
8035       /* If the token after `using' is `namespace', then we have a
8036          using-directive.  */
8037       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8038       if (token2->keyword == RID_NAMESPACE)
8039         cp_parser_using_directive (parser);
8040       /* Otherwise, it's a using-declaration.  */
8041       else
8042         cp_parser_using_declaration (parser,
8043                                      /*access_declaration_p=*/false);
8044     }
8045   /* If the next keyword is `__label__' we have a misplaced label
8046      declaration.  */
8047   else if (token1->keyword == RID_LABEL)
8048     {
8049       cp_lexer_consume_token (parser->lexer);
8050       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8051       cp_parser_skip_to_end_of_statement (parser);
8052       /* If the next token is now a `;', consume it.  */
8053       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8054         cp_lexer_consume_token (parser->lexer);
8055     }
8056   /* If the next token is `static_assert' we have a static assertion.  */
8057   else if (token1->keyword == RID_STATIC_ASSERT)
8058     cp_parser_static_assert (parser, /*member_p=*/false);
8059   /* Anything else must be a simple-declaration.  */
8060   else
8061     cp_parser_simple_declaration (parser, !statement_p);
8062 }
8063
8064 /* Parse a simple-declaration.
8065
8066    simple-declaration:
8067      decl-specifier-seq [opt] init-declarator-list [opt] ;
8068
8069    init-declarator-list:
8070      init-declarator
8071      init-declarator-list , init-declarator
8072
8073    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8074    function-definition as a simple-declaration.  */
8075
8076 static void
8077 cp_parser_simple_declaration (cp_parser* parser,
8078                               bool function_definition_allowed_p)
8079 {
8080   cp_decl_specifier_seq decl_specifiers;
8081   int declares_class_or_enum;
8082   bool saw_declarator;
8083
8084   /* Defer access checks until we know what is being declared; the
8085      checks for names appearing in the decl-specifier-seq should be
8086      done as if we were in the scope of the thing being declared.  */
8087   push_deferring_access_checks (dk_deferred);
8088
8089   /* Parse the decl-specifier-seq.  We have to keep track of whether
8090      or not the decl-specifier-seq declares a named class or
8091      enumeration type, since that is the only case in which the
8092      init-declarator-list is allowed to be empty.
8093
8094      [dcl.dcl]
8095
8096      In a simple-declaration, the optional init-declarator-list can be
8097      omitted only when declaring a class or enumeration, that is when
8098      the decl-specifier-seq contains either a class-specifier, an
8099      elaborated-type-specifier, or an enum-specifier.  */
8100   cp_parser_decl_specifier_seq (parser,
8101                                 CP_PARSER_FLAGS_OPTIONAL,
8102                                 &decl_specifiers,
8103                                 &declares_class_or_enum);
8104   /* We no longer need to defer access checks.  */
8105   stop_deferring_access_checks ();
8106
8107   /* In a block scope, a valid declaration must always have a
8108      decl-specifier-seq.  By not trying to parse declarators, we can
8109      resolve the declaration/expression ambiguity more quickly.  */
8110   if (!function_definition_allowed_p
8111       && !decl_specifiers.any_specifiers_p)
8112     {
8113       cp_parser_error (parser, "expected declaration");
8114       goto done;
8115     }
8116
8117   /* If the next two tokens are both identifiers, the code is
8118      erroneous. The usual cause of this situation is code like:
8119
8120        T t;
8121
8122      where "T" should name a type -- but does not.  */
8123   if (!decl_specifiers.type
8124       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8125     {
8126       /* If parsing tentatively, we should commit; we really are
8127          looking at a declaration.  */
8128       cp_parser_commit_to_tentative_parse (parser);
8129       /* Give up.  */
8130       goto done;
8131     }
8132
8133   /* If we have seen at least one decl-specifier, and the next token
8134      is not a parenthesis, then we must be looking at a declaration.
8135      (After "int (" we might be looking at a functional cast.)  */
8136   if (decl_specifiers.any_specifiers_p
8137       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8138       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8139       && !cp_parser_error_occurred (parser))
8140     cp_parser_commit_to_tentative_parse (parser);
8141
8142   /* Keep going until we hit the `;' at the end of the simple
8143      declaration.  */
8144   saw_declarator = false;
8145   while (cp_lexer_next_token_is_not (parser->lexer,
8146                                      CPP_SEMICOLON))
8147     {
8148       cp_token *token;
8149       bool function_definition_p;
8150       tree decl;
8151
8152       if (saw_declarator)
8153         {
8154           /* If we are processing next declarator, coma is expected */
8155           token = cp_lexer_peek_token (parser->lexer);
8156           gcc_assert (token->type == CPP_COMMA);
8157           cp_lexer_consume_token (parser->lexer);
8158         }
8159       else
8160         saw_declarator = true;
8161
8162       /* Parse the init-declarator.  */
8163       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8164                                         /*checks=*/NULL,
8165                                         function_definition_allowed_p,
8166                                         /*member_p=*/false,
8167                                         declares_class_or_enum,
8168                                         &function_definition_p);
8169       /* If an error occurred while parsing tentatively, exit quickly.
8170          (That usually happens when in the body of a function; each
8171          statement is treated as a declaration-statement until proven
8172          otherwise.)  */
8173       if (cp_parser_error_occurred (parser))
8174         goto done;
8175       /* Handle function definitions specially.  */
8176       if (function_definition_p)
8177         {
8178           /* If the next token is a `,', then we are probably
8179              processing something like:
8180
8181                void f() {}, *p;
8182
8183              which is erroneous.  */
8184           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8185             {
8186               cp_token *token = cp_lexer_peek_token (parser->lexer);
8187               error ("%Hmixing declarations and function-definitions is forbidden",
8188                      &token->location);
8189             }
8190           /* Otherwise, we're done with the list of declarators.  */
8191           else
8192             {
8193               pop_deferring_access_checks ();
8194               return;
8195             }
8196         }
8197       /* The next token should be either a `,' or a `;'.  */
8198       token = cp_lexer_peek_token (parser->lexer);
8199       /* If it's a `,', there are more declarators to come.  */
8200       if (token->type == CPP_COMMA)
8201         /* will be consumed next time around */;
8202       /* If it's a `;', we are done.  */
8203       else if (token->type == CPP_SEMICOLON)
8204         break;
8205       /* Anything else is an error.  */
8206       else
8207         {
8208           /* If we have already issued an error message we don't need
8209              to issue another one.  */
8210           if (decl != error_mark_node
8211               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8212             cp_parser_error (parser, "expected %<,%> or %<;%>");
8213           /* Skip tokens until we reach the end of the statement.  */
8214           cp_parser_skip_to_end_of_statement (parser);
8215           /* If the next token is now a `;', consume it.  */
8216           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8217             cp_lexer_consume_token (parser->lexer);
8218           goto done;
8219         }
8220       /* After the first time around, a function-definition is not
8221          allowed -- even if it was OK at first.  For example:
8222
8223            int i, f() {}
8224
8225          is not valid.  */
8226       function_definition_allowed_p = false;
8227     }
8228
8229   /* Issue an error message if no declarators are present, and the
8230      decl-specifier-seq does not itself declare a class or
8231      enumeration.  */
8232   if (!saw_declarator)
8233     {
8234       if (cp_parser_declares_only_class_p (parser))
8235         shadow_tag (&decl_specifiers);
8236       /* Perform any deferred access checks.  */
8237       perform_deferred_access_checks ();
8238     }
8239
8240   /* Consume the `;'.  */
8241   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8242
8243  done:
8244   pop_deferring_access_checks ();
8245 }
8246
8247 /* Parse a decl-specifier-seq.
8248
8249    decl-specifier-seq:
8250      decl-specifier-seq [opt] decl-specifier
8251
8252    decl-specifier:
8253      storage-class-specifier
8254      type-specifier
8255      function-specifier
8256      friend
8257      typedef
8258
8259    GNU Extension:
8260
8261    decl-specifier:
8262      attributes
8263
8264    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8265
8266    The parser flags FLAGS is used to control type-specifier parsing.
8267
8268    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8269    flags:
8270
8271      1: one of the decl-specifiers is an elaborated-type-specifier
8272         (i.e., a type declaration)
8273      2: one of the decl-specifiers is an enum-specifier or a
8274         class-specifier (i.e., a type definition)
8275
8276    */
8277
8278 static void
8279 cp_parser_decl_specifier_seq (cp_parser* parser,
8280                               cp_parser_flags flags,
8281                               cp_decl_specifier_seq *decl_specs,
8282                               int* declares_class_or_enum)
8283 {
8284   bool constructor_possible_p = !parser->in_declarator_p;
8285   cp_token *start_token = NULL;
8286
8287   /* Clear DECL_SPECS.  */
8288   clear_decl_specs (decl_specs);
8289
8290   /* Assume no class or enumeration type is declared.  */
8291   *declares_class_or_enum = 0;
8292
8293   /* Keep reading specifiers until there are no more to read.  */
8294   while (true)
8295     {
8296       bool constructor_p;
8297       bool found_decl_spec;
8298       cp_token *token;
8299
8300       /* Peek at the next token.  */
8301       token = cp_lexer_peek_token (parser->lexer);
8302
8303       /* Save the first token of the decl spec list for error
8304          reporting.  */
8305       if (!start_token)
8306         start_token = token;
8307       /* Handle attributes.  */
8308       if (token->keyword == RID_ATTRIBUTE)
8309         {
8310           /* Parse the attributes.  */
8311           decl_specs->attributes
8312             = chainon (decl_specs->attributes,
8313                        cp_parser_attributes_opt (parser));
8314           continue;
8315         }
8316       /* Assume we will find a decl-specifier keyword.  */
8317       found_decl_spec = true;
8318       /* If the next token is an appropriate keyword, we can simply
8319          add it to the list.  */
8320       switch (token->keyword)
8321         {
8322           /* decl-specifier:
8323                friend  */
8324         case RID_FRIEND:
8325           if (!at_class_scope_p ())
8326             {
8327               error ("%H%<friend%> used outside of class", &token->location);
8328               cp_lexer_purge_token (parser->lexer);
8329             }
8330           else
8331             {
8332               ++decl_specs->specs[(int) ds_friend];
8333               /* Consume the token.  */
8334               cp_lexer_consume_token (parser->lexer);
8335             }
8336           break;
8337
8338           /* function-specifier:
8339                inline
8340                virtual
8341                explicit  */
8342         case RID_INLINE:
8343         case RID_VIRTUAL:
8344         case RID_EXPLICIT:
8345           cp_parser_function_specifier_opt (parser, decl_specs);
8346           break;
8347
8348           /* decl-specifier:
8349                typedef  */
8350         case RID_TYPEDEF:
8351           ++decl_specs->specs[(int) ds_typedef];
8352           /* Consume the token.  */
8353           cp_lexer_consume_token (parser->lexer);
8354           /* A constructor declarator cannot appear in a typedef.  */
8355           constructor_possible_p = false;
8356           /* The "typedef" keyword can only occur in a declaration; we
8357              may as well commit at this point.  */
8358           cp_parser_commit_to_tentative_parse (parser);
8359
8360           if (decl_specs->storage_class != sc_none)
8361             decl_specs->conflicting_specifiers_p = true;
8362           break;
8363
8364           /* storage-class-specifier:
8365                auto
8366                register
8367                static
8368                extern
8369                mutable
8370
8371              GNU Extension:
8372                thread  */
8373         case RID_AUTO:
8374           if (cxx_dialect == cxx98) 
8375             {
8376               /* Consume the token.  */
8377               cp_lexer_consume_token (parser->lexer);
8378
8379               /* Complain about `auto' as a storage specifier, if
8380                  we're complaining about C++0x compatibility.  */
8381               warning 
8382                 (OPT_Wc__0x_compat, 
8383                  "%H%<auto%> will change meaning in C++0x; please remove it",
8384                  &token->location);
8385
8386               /* Set the storage class anyway.  */
8387               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8388                                            token->location);
8389             }
8390           else
8391             /* C++0x auto type-specifier.  */
8392             found_decl_spec = false;
8393           break;
8394
8395         case RID_REGISTER:
8396         case RID_STATIC:
8397         case RID_EXTERN:
8398         case RID_MUTABLE:
8399           /* Consume the token.  */
8400           cp_lexer_consume_token (parser->lexer);
8401           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8402                                        token->location);
8403           break;
8404         case RID_THREAD:
8405           /* Consume the token.  */
8406           cp_lexer_consume_token (parser->lexer);
8407           ++decl_specs->specs[(int) ds_thread];
8408           break;
8409
8410         default:
8411           /* We did not yet find a decl-specifier yet.  */
8412           found_decl_spec = false;
8413           break;
8414         }
8415
8416       /* Constructors are a special case.  The `S' in `S()' is not a
8417          decl-specifier; it is the beginning of the declarator.  */
8418       constructor_p
8419         = (!found_decl_spec
8420            && constructor_possible_p
8421            && (cp_parser_constructor_declarator_p
8422                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8423
8424       /* If we don't have a DECL_SPEC yet, then we must be looking at
8425          a type-specifier.  */
8426       if (!found_decl_spec && !constructor_p)
8427         {
8428           int decl_spec_declares_class_or_enum;
8429           bool is_cv_qualifier;
8430           tree type_spec;
8431
8432           type_spec
8433             = cp_parser_type_specifier (parser, flags,
8434                                         decl_specs,
8435                                         /*is_declaration=*/true,
8436                                         &decl_spec_declares_class_or_enum,
8437                                         &is_cv_qualifier);
8438           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8439
8440           /* If this type-specifier referenced a user-defined type
8441              (a typedef, class-name, etc.), then we can't allow any
8442              more such type-specifiers henceforth.
8443
8444              [dcl.spec]
8445
8446              The longest sequence of decl-specifiers that could
8447              possibly be a type name is taken as the
8448              decl-specifier-seq of a declaration.  The sequence shall
8449              be self-consistent as described below.
8450
8451              [dcl.type]
8452
8453              As a general rule, at most one type-specifier is allowed
8454              in the complete decl-specifier-seq of a declaration.  The
8455              only exceptions are the following:
8456
8457              -- const or volatile can be combined with any other
8458                 type-specifier.
8459
8460              -- signed or unsigned can be combined with char, long,
8461                 short, or int.
8462
8463              -- ..
8464
8465              Example:
8466
8467                typedef char* Pc;
8468                void g (const int Pc);
8469
8470              Here, Pc is *not* part of the decl-specifier seq; it's
8471              the declarator.  Therefore, once we see a type-specifier
8472              (other than a cv-qualifier), we forbid any additional
8473              user-defined types.  We *do* still allow things like `int
8474              int' to be considered a decl-specifier-seq, and issue the
8475              error message later.  */
8476           if (type_spec && !is_cv_qualifier)
8477             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8478           /* A constructor declarator cannot follow a type-specifier.  */
8479           if (type_spec)
8480             {
8481               constructor_possible_p = false;
8482               found_decl_spec = true;
8483             }
8484         }
8485
8486       /* If we still do not have a DECL_SPEC, then there are no more
8487          decl-specifiers.  */
8488       if (!found_decl_spec)
8489         break;
8490
8491       decl_specs->any_specifiers_p = true;
8492       /* After we see one decl-specifier, further decl-specifiers are
8493          always optional.  */
8494       flags |= CP_PARSER_FLAGS_OPTIONAL;
8495     }
8496
8497   cp_parser_check_decl_spec (decl_specs, start_token->location);
8498
8499   /* Don't allow a friend specifier with a class definition.  */
8500   if (decl_specs->specs[(int) ds_friend] != 0
8501       && (*declares_class_or_enum & 2))
8502     error ("%Hclass definition may not be declared a friend",
8503             &start_token->location);
8504 }
8505
8506 /* Parse an (optional) storage-class-specifier.
8507
8508    storage-class-specifier:
8509      auto
8510      register
8511      static
8512      extern
8513      mutable
8514
8515    GNU Extension:
8516
8517    storage-class-specifier:
8518      thread
8519
8520    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8521
8522 static tree
8523 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8524 {
8525   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8526     {
8527     case RID_AUTO:
8528       if (cxx_dialect != cxx98)
8529         return NULL_TREE;
8530       /* Fall through for C++98.  */
8531
8532     case RID_REGISTER:
8533     case RID_STATIC:
8534     case RID_EXTERN:
8535     case RID_MUTABLE:
8536     case RID_THREAD:
8537       /* Consume the token.  */
8538       return cp_lexer_consume_token (parser->lexer)->u.value;
8539
8540     default:
8541       return NULL_TREE;
8542     }
8543 }
8544
8545 /* Parse an (optional) function-specifier.
8546
8547    function-specifier:
8548      inline
8549      virtual
8550      explicit
8551
8552    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8553    Updates DECL_SPECS, if it is non-NULL.  */
8554
8555 static tree
8556 cp_parser_function_specifier_opt (cp_parser* parser,
8557                                   cp_decl_specifier_seq *decl_specs)
8558 {
8559   cp_token *token = cp_lexer_peek_token (parser->lexer);
8560   switch (token->keyword)
8561     {
8562     case RID_INLINE:
8563       if (decl_specs)
8564         ++decl_specs->specs[(int) ds_inline];
8565       break;
8566
8567     case RID_VIRTUAL:
8568       /* 14.5.2.3 [temp.mem]
8569
8570          A member function template shall not be virtual.  */
8571       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8572         error ("%Htemplates may not be %<virtual%>", &token->location);
8573       else if (decl_specs)
8574         ++decl_specs->specs[(int) ds_virtual];
8575       break;
8576
8577     case RID_EXPLICIT:
8578       if (decl_specs)
8579         ++decl_specs->specs[(int) ds_explicit];
8580       break;
8581
8582     default:
8583       return NULL_TREE;
8584     }
8585
8586   /* Consume the token.  */
8587   return cp_lexer_consume_token (parser->lexer)->u.value;
8588 }
8589
8590 /* Parse a linkage-specification.
8591
8592    linkage-specification:
8593      extern string-literal { declaration-seq [opt] }
8594      extern string-literal declaration  */
8595
8596 static void
8597 cp_parser_linkage_specification (cp_parser* parser)
8598 {
8599   tree linkage;
8600
8601   /* Look for the `extern' keyword.  */
8602   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8603
8604   /* Look for the string-literal.  */
8605   linkage = cp_parser_string_literal (parser, false, false);
8606
8607   /* Transform the literal into an identifier.  If the literal is a
8608      wide-character string, or contains embedded NULs, then we can't
8609      handle it as the user wants.  */
8610   if (strlen (TREE_STRING_POINTER (linkage))
8611       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8612     {
8613       cp_parser_error (parser, "invalid linkage-specification");
8614       /* Assume C++ linkage.  */
8615       linkage = lang_name_cplusplus;
8616     }
8617   else
8618     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8619
8620   /* We're now using the new linkage.  */
8621   push_lang_context (linkage);
8622
8623   /* If the next token is a `{', then we're using the first
8624      production.  */
8625   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8626     {
8627       /* Consume the `{' token.  */
8628       cp_lexer_consume_token (parser->lexer);
8629       /* Parse the declarations.  */
8630       cp_parser_declaration_seq_opt (parser);
8631       /* Look for the closing `}'.  */
8632       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8633     }
8634   /* Otherwise, there's just one declaration.  */
8635   else
8636     {
8637       bool saved_in_unbraced_linkage_specification_p;
8638
8639       saved_in_unbraced_linkage_specification_p
8640         = parser->in_unbraced_linkage_specification_p;
8641       parser->in_unbraced_linkage_specification_p = true;
8642       cp_parser_declaration (parser);
8643       parser->in_unbraced_linkage_specification_p
8644         = saved_in_unbraced_linkage_specification_p;
8645     }
8646
8647   /* We're done with the linkage-specification.  */
8648   pop_lang_context ();
8649 }
8650
8651 /* Parse a static_assert-declaration.
8652
8653    static_assert-declaration:
8654      static_assert ( constant-expression , string-literal ) ; 
8655
8656    If MEMBER_P, this static_assert is a class member.  */
8657
8658 static void 
8659 cp_parser_static_assert(cp_parser *parser, bool member_p)
8660 {
8661   tree condition;
8662   tree message;
8663   cp_token *token;
8664   location_t saved_loc;
8665
8666   /* Peek at the `static_assert' token so we can keep track of exactly
8667      where the static assertion started.  */
8668   token = cp_lexer_peek_token (parser->lexer);
8669   saved_loc = token->location;
8670
8671   /* Look for the `static_assert' keyword.  */
8672   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8673                                   "%<static_assert%>"))
8674     return;
8675
8676   /*  We know we are in a static assertion; commit to any tentative
8677       parse.  */
8678   if (cp_parser_parsing_tentatively (parser))
8679     cp_parser_commit_to_tentative_parse (parser);
8680
8681   /* Parse the `(' starting the static assertion condition.  */
8682   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8683
8684   /* Parse the constant-expression.  */
8685   condition = 
8686     cp_parser_constant_expression (parser,
8687                                    /*allow_non_constant_p=*/false,
8688                                    /*non_constant_p=*/NULL);
8689
8690   /* Parse the separating `,'.  */
8691   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8692
8693   /* Parse the string-literal message.  */
8694   message = cp_parser_string_literal (parser, 
8695                                       /*translate=*/false,
8696                                       /*wide_ok=*/true);
8697
8698   /* A `)' completes the static assertion.  */
8699   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8700     cp_parser_skip_to_closing_parenthesis (parser, 
8701                                            /*recovering=*/true, 
8702                                            /*or_comma=*/false,
8703                                            /*consume_paren=*/true);
8704
8705   /* A semicolon terminates the declaration.  */
8706   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8707
8708   /* Complete the static assertion, which may mean either processing 
8709      the static assert now or saving it for template instantiation.  */
8710   finish_static_assert (condition, message, saved_loc, member_p);
8711 }
8712
8713 /* Parse a `decltype' type. Returns the type. 
8714
8715    simple-type-specifier:
8716      decltype ( expression )  */
8717
8718 static tree
8719 cp_parser_decltype (cp_parser *parser)
8720 {
8721   tree expr;
8722   bool id_expression_or_member_access_p = false;
8723   const char *saved_message;
8724   bool saved_integral_constant_expression_p;
8725   bool saved_non_integral_constant_expression_p;
8726   cp_token *id_expr_start_token;
8727
8728   /* Look for the `decltype' token.  */
8729   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8730     return error_mark_node;
8731
8732   /* Types cannot be defined in a `decltype' expression.  Save away the
8733      old message.  */
8734   saved_message = parser->type_definition_forbidden_message;
8735
8736   /* And create the new one.  */
8737   parser->type_definition_forbidden_message
8738     = "types may not be defined in %<decltype%> expressions";
8739
8740   /* The restrictions on constant-expressions do not apply inside
8741      decltype expressions.  */
8742   saved_integral_constant_expression_p
8743     = parser->integral_constant_expression_p;
8744   saved_non_integral_constant_expression_p
8745     = parser->non_integral_constant_expression_p;
8746   parser->integral_constant_expression_p = false;
8747
8748   /* Do not actually evaluate the expression.  */
8749   ++skip_evaluation;
8750
8751   /* Parse the opening `('.  */
8752   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8753     return error_mark_node;
8754   
8755   /* First, try parsing an id-expression.  */
8756   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8757   cp_parser_parse_tentatively (parser);
8758   expr = cp_parser_id_expression (parser,
8759                                   /*template_keyword_p=*/false,
8760                                   /*check_dependency_p=*/true,
8761                                   /*template_p=*/NULL,
8762                                   /*declarator_p=*/false,
8763                                   /*optional_p=*/false);
8764
8765   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8766     {
8767       bool non_integral_constant_expression_p = false;
8768       tree id_expression = expr;
8769       cp_id_kind idk;
8770       const char *error_msg;
8771
8772       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8773         /* Lookup the name we got back from the id-expression.  */
8774         expr = cp_parser_lookup_name (parser, expr,
8775                                       none_type,
8776                                       /*is_template=*/false,
8777                                       /*is_namespace=*/false,
8778                                       /*check_dependency=*/true,
8779                                       /*ambiguous_decls=*/NULL,
8780                                       id_expr_start_token->location);
8781
8782       if (expr
8783           && expr != error_mark_node
8784           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8785           && TREE_CODE (expr) != TYPE_DECL
8786           && (TREE_CODE (expr) != BIT_NOT_EXPR
8787               || !TYPE_P (TREE_OPERAND (expr, 0)))
8788           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8789         {
8790           /* Complete lookup of the id-expression.  */
8791           expr = (finish_id_expression
8792                   (id_expression, expr, parser->scope, &idk,
8793                    /*integral_constant_expression_p=*/false,
8794                    /*allow_non_integral_constant_expression_p=*/true,
8795                    &non_integral_constant_expression_p,
8796                    /*template_p=*/false,
8797                    /*done=*/true,
8798                    /*address_p=*/false,
8799                    /*template_arg_p=*/false,
8800                    &error_msg,
8801                    id_expr_start_token->location));
8802
8803           if (expr == error_mark_node)
8804             /* We found an id-expression, but it was something that we
8805                should not have found. This is an error, not something
8806                we can recover from, so note that we found an
8807                id-expression and we'll recover as gracefully as
8808                possible.  */
8809             id_expression_or_member_access_p = true;
8810         }
8811
8812       if (expr 
8813           && expr != error_mark_node
8814           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8815         /* We have an id-expression.  */
8816         id_expression_or_member_access_p = true;
8817     }
8818
8819   if (!id_expression_or_member_access_p)
8820     {
8821       /* Abort the id-expression parse.  */
8822       cp_parser_abort_tentative_parse (parser);
8823
8824       /* Parsing tentatively, again.  */
8825       cp_parser_parse_tentatively (parser);
8826
8827       /* Parse a class member access.  */
8828       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8829                                            /*cast_p=*/false,
8830                                            /*member_access_only_p=*/true);
8831
8832       if (expr 
8833           && expr != error_mark_node
8834           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8835         /* We have an id-expression.  */
8836         id_expression_or_member_access_p = true;
8837     }
8838
8839   if (id_expression_or_member_access_p)
8840     /* We have parsed the complete id-expression or member access.  */
8841     cp_parser_parse_definitely (parser);
8842   else
8843     {
8844       /* Abort our attempt to parse an id-expression or member access
8845          expression.  */
8846       cp_parser_abort_tentative_parse (parser);
8847
8848       /* Parse a full expression.  */
8849       expr = cp_parser_expression (parser, /*cast_p=*/false);
8850     }
8851
8852   /* Go back to evaluating expressions.  */
8853   --skip_evaluation;
8854
8855   /* Restore the old message and the integral constant expression
8856      flags.  */
8857   parser->type_definition_forbidden_message = saved_message;
8858   parser->integral_constant_expression_p
8859     = saved_integral_constant_expression_p;
8860   parser->non_integral_constant_expression_p
8861     = saved_non_integral_constant_expression_p;
8862
8863   if (expr == error_mark_node)
8864     {
8865       /* Skip everything up to the closing `)'.  */
8866       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8867                                              /*consume_paren=*/true);
8868       return error_mark_node;
8869     }
8870   
8871   /* Parse to the closing `)'.  */
8872   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8873     {
8874       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8875                                              /*consume_paren=*/true);
8876       return error_mark_node;
8877     }
8878
8879   return finish_decltype_type (expr, id_expression_or_member_access_p);
8880 }
8881
8882 /* Special member functions [gram.special] */
8883
8884 /* Parse a conversion-function-id.
8885
8886    conversion-function-id:
8887      operator conversion-type-id
8888
8889    Returns an IDENTIFIER_NODE representing the operator.  */
8890
8891 static tree
8892 cp_parser_conversion_function_id (cp_parser* parser)
8893 {
8894   tree type;
8895   tree saved_scope;
8896   tree saved_qualifying_scope;
8897   tree saved_object_scope;
8898   tree pushed_scope = NULL_TREE;
8899
8900   /* Look for the `operator' token.  */
8901   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8902     return error_mark_node;
8903   /* When we parse the conversion-type-id, the current scope will be
8904      reset.  However, we need that information in able to look up the
8905      conversion function later, so we save it here.  */
8906   saved_scope = parser->scope;
8907   saved_qualifying_scope = parser->qualifying_scope;
8908   saved_object_scope = parser->object_scope;
8909   /* We must enter the scope of the class so that the names of
8910      entities declared within the class are available in the
8911      conversion-type-id.  For example, consider:
8912
8913        struct S {
8914          typedef int I;
8915          operator I();
8916        };
8917
8918        S::operator I() { ... }
8919
8920      In order to see that `I' is a type-name in the definition, we
8921      must be in the scope of `S'.  */
8922   if (saved_scope)
8923     pushed_scope = push_scope (saved_scope);
8924   /* Parse the conversion-type-id.  */
8925   type = cp_parser_conversion_type_id (parser);
8926   /* Leave the scope of the class, if any.  */
8927   if (pushed_scope)
8928     pop_scope (pushed_scope);
8929   /* Restore the saved scope.  */
8930   parser->scope = saved_scope;
8931   parser->qualifying_scope = saved_qualifying_scope;
8932   parser->object_scope = saved_object_scope;
8933   /* If the TYPE is invalid, indicate failure.  */
8934   if (type == error_mark_node)
8935     return error_mark_node;
8936   return mangle_conv_op_name_for_type (type);
8937 }
8938
8939 /* Parse a conversion-type-id:
8940
8941    conversion-type-id:
8942      type-specifier-seq conversion-declarator [opt]
8943
8944    Returns the TYPE specified.  */
8945
8946 static tree
8947 cp_parser_conversion_type_id (cp_parser* parser)
8948 {
8949   tree attributes;
8950   cp_decl_specifier_seq type_specifiers;
8951   cp_declarator *declarator;
8952   tree type_specified;
8953
8954   /* Parse the attributes.  */
8955   attributes = cp_parser_attributes_opt (parser);
8956   /* Parse the type-specifiers.  */
8957   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8958                                 &type_specifiers);
8959   /* If that didn't work, stop.  */
8960   if (type_specifiers.type == error_mark_node)
8961     return error_mark_node;
8962   /* Parse the conversion-declarator.  */
8963   declarator = cp_parser_conversion_declarator_opt (parser);
8964
8965   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8966                                     /*initialized=*/0, &attributes);
8967   if (attributes)
8968     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8969
8970   /* Don't give this error when parsing tentatively.  This happens to
8971      work because we always parse this definitively once.  */
8972   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
8973       && type_uses_auto (type_specified))
8974     {
8975       error ("invalid use of %<auto%> in conversion operator");
8976       return error_mark_node;
8977     }
8978
8979   return type_specified;
8980 }
8981
8982 /* Parse an (optional) conversion-declarator.
8983
8984    conversion-declarator:
8985      ptr-operator conversion-declarator [opt]
8986
8987    */
8988
8989 static cp_declarator *
8990 cp_parser_conversion_declarator_opt (cp_parser* parser)
8991 {
8992   enum tree_code code;
8993   tree class_type;
8994   cp_cv_quals cv_quals;
8995
8996   /* We don't know if there's a ptr-operator next, or not.  */
8997   cp_parser_parse_tentatively (parser);
8998   /* Try the ptr-operator.  */
8999   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9000   /* If it worked, look for more conversion-declarators.  */
9001   if (cp_parser_parse_definitely (parser))
9002     {
9003       cp_declarator *declarator;
9004
9005       /* Parse another optional declarator.  */
9006       declarator = cp_parser_conversion_declarator_opt (parser);
9007
9008       return cp_parser_make_indirect_declarator
9009         (code, class_type, cv_quals, declarator);
9010    }
9011
9012   return NULL;
9013 }
9014
9015 /* Parse an (optional) ctor-initializer.
9016
9017    ctor-initializer:
9018      : mem-initializer-list
9019
9020    Returns TRUE iff the ctor-initializer was actually present.  */
9021
9022 static bool
9023 cp_parser_ctor_initializer_opt (cp_parser* parser)
9024 {
9025   /* If the next token is not a `:', then there is no
9026      ctor-initializer.  */
9027   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9028     {
9029       /* Do default initialization of any bases and members.  */
9030       if (DECL_CONSTRUCTOR_P (current_function_decl))
9031         finish_mem_initializers (NULL_TREE);
9032
9033       return false;
9034     }
9035
9036   /* Consume the `:' token.  */
9037   cp_lexer_consume_token (parser->lexer);
9038   /* And the mem-initializer-list.  */
9039   cp_parser_mem_initializer_list (parser);
9040
9041   return true;
9042 }
9043
9044 /* Parse a mem-initializer-list.
9045
9046    mem-initializer-list:
9047      mem-initializer ... [opt]
9048      mem-initializer ... [opt] , mem-initializer-list  */
9049
9050 static void
9051 cp_parser_mem_initializer_list (cp_parser* parser)
9052 {
9053   tree mem_initializer_list = NULL_TREE;
9054   cp_token *token = cp_lexer_peek_token (parser->lexer);
9055
9056   /* Let the semantic analysis code know that we are starting the
9057      mem-initializer-list.  */
9058   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9059     error ("%Honly constructors take base initializers",
9060            &token->location);
9061
9062   /* Loop through the list.  */
9063   while (true)
9064     {
9065       tree mem_initializer;
9066
9067       token = cp_lexer_peek_token (parser->lexer);
9068       /* Parse the mem-initializer.  */
9069       mem_initializer = cp_parser_mem_initializer (parser);
9070       /* If the next token is a `...', we're expanding member initializers. */
9071       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9072         {
9073           /* Consume the `...'. */
9074           cp_lexer_consume_token (parser->lexer);
9075
9076           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9077              can be expanded but members cannot. */
9078           if (mem_initializer != error_mark_node
9079               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9080             {
9081               error ("%Hcannot expand initializer for member %<%D%>",
9082                      &token->location, TREE_PURPOSE (mem_initializer));
9083               mem_initializer = error_mark_node;
9084             }
9085
9086           /* Construct the pack expansion type. */
9087           if (mem_initializer != error_mark_node)
9088             mem_initializer = make_pack_expansion (mem_initializer);
9089         }
9090       /* Add it to the list, unless it was erroneous.  */
9091       if (mem_initializer != error_mark_node)
9092         {
9093           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9094           mem_initializer_list = mem_initializer;
9095         }
9096       /* If the next token is not a `,', we're done.  */
9097       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9098         break;
9099       /* Consume the `,' token.  */
9100       cp_lexer_consume_token (parser->lexer);
9101     }
9102
9103   /* Perform semantic analysis.  */
9104   if (DECL_CONSTRUCTOR_P (current_function_decl))
9105     finish_mem_initializers (mem_initializer_list);
9106 }
9107
9108 /* Parse a mem-initializer.
9109
9110    mem-initializer:
9111      mem-initializer-id ( expression-list [opt] )
9112      mem-initializer-id braced-init-list
9113
9114    GNU extension:
9115
9116    mem-initializer:
9117      ( expression-list [opt] )
9118
9119    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9120    class) or FIELD_DECL (for a non-static data member) to initialize;
9121    the TREE_VALUE is the expression-list.  An empty initialization
9122    list is represented by void_list_node.  */
9123
9124 static tree
9125 cp_parser_mem_initializer (cp_parser* parser)
9126 {
9127   tree mem_initializer_id;
9128   tree expression_list;
9129   tree member;
9130   cp_token *token = cp_lexer_peek_token (parser->lexer);
9131
9132   /* Find out what is being initialized.  */
9133   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9134     {
9135       permerror (token->location,
9136                  "anachronistic old-style base class initializer");
9137       mem_initializer_id = NULL_TREE;
9138     }
9139   else
9140     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9141   member = expand_member_init (mem_initializer_id);
9142   if (member && !DECL_P (member))
9143     in_base_initializer = 1;
9144
9145   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9146     {
9147       bool expr_non_constant_p;
9148       maybe_warn_cpp0x ("extended initializer lists");
9149       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9150       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9151       expression_list = build_tree_list (NULL_TREE, expression_list);
9152     }
9153   else
9154     expression_list
9155       = cp_parser_parenthesized_expression_list (parser, false,
9156                                                  /*cast_p=*/false,
9157                                                  /*allow_expansion_p=*/true,
9158                                                  /*non_constant_p=*/NULL);
9159   if (expression_list == error_mark_node)
9160     return error_mark_node;
9161   if (!expression_list)
9162     expression_list = void_type_node;
9163
9164   in_base_initializer = 0;
9165
9166   return member ? build_tree_list (member, expression_list) : error_mark_node;
9167 }
9168
9169 /* Parse a mem-initializer-id.
9170
9171    mem-initializer-id:
9172      :: [opt] nested-name-specifier [opt] class-name
9173      identifier
9174
9175    Returns a TYPE indicating the class to be initializer for the first
9176    production.  Returns an IDENTIFIER_NODE indicating the data member
9177    to be initialized for the second production.  */
9178
9179 static tree
9180 cp_parser_mem_initializer_id (cp_parser* parser)
9181 {
9182   bool global_scope_p;
9183   bool nested_name_specifier_p;
9184   bool template_p = false;
9185   tree id;
9186
9187   cp_token *token = cp_lexer_peek_token (parser->lexer);
9188
9189   /* `typename' is not allowed in this context ([temp.res]).  */
9190   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9191     {
9192       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9193              "member initializer is implicitly a type)",
9194              &token->location);
9195       cp_lexer_consume_token (parser->lexer);
9196     }
9197   /* Look for the optional `::' operator.  */
9198   global_scope_p
9199     = (cp_parser_global_scope_opt (parser,
9200                                    /*current_scope_valid_p=*/false)
9201        != NULL_TREE);
9202   /* Look for the optional nested-name-specifier.  The simplest way to
9203      implement:
9204
9205        [temp.res]
9206
9207        The keyword `typename' is not permitted in a base-specifier or
9208        mem-initializer; in these contexts a qualified name that
9209        depends on a template-parameter is implicitly assumed to be a
9210        type name.
9211
9212      is to assume that we have seen the `typename' keyword at this
9213      point.  */
9214   nested_name_specifier_p
9215     = (cp_parser_nested_name_specifier_opt (parser,
9216                                             /*typename_keyword_p=*/true,
9217                                             /*check_dependency_p=*/true,
9218                                             /*type_p=*/true,
9219                                             /*is_declaration=*/true)
9220        != NULL_TREE);
9221   if (nested_name_specifier_p)
9222     template_p = cp_parser_optional_template_keyword (parser);
9223   /* If there is a `::' operator or a nested-name-specifier, then we
9224      are definitely looking for a class-name.  */
9225   if (global_scope_p || nested_name_specifier_p)
9226     return cp_parser_class_name (parser,
9227                                  /*typename_keyword_p=*/true,
9228                                  /*template_keyword_p=*/template_p,
9229                                  none_type,
9230                                  /*check_dependency_p=*/true,
9231                                  /*class_head_p=*/false,
9232                                  /*is_declaration=*/true);
9233   /* Otherwise, we could also be looking for an ordinary identifier.  */
9234   cp_parser_parse_tentatively (parser);
9235   /* Try a class-name.  */
9236   id = cp_parser_class_name (parser,
9237                              /*typename_keyword_p=*/true,
9238                              /*template_keyword_p=*/false,
9239                              none_type,
9240                              /*check_dependency_p=*/true,
9241                              /*class_head_p=*/false,
9242                              /*is_declaration=*/true);
9243   /* If we found one, we're done.  */
9244   if (cp_parser_parse_definitely (parser))
9245     return id;
9246   /* Otherwise, look for an ordinary identifier.  */
9247   return cp_parser_identifier (parser);
9248 }
9249
9250 /* Overloading [gram.over] */
9251
9252 /* Parse an operator-function-id.
9253
9254    operator-function-id:
9255      operator operator
9256
9257    Returns an IDENTIFIER_NODE for the operator which is a
9258    human-readable spelling of the identifier, e.g., `operator +'.  */
9259
9260 static tree
9261 cp_parser_operator_function_id (cp_parser* parser)
9262 {
9263   /* Look for the `operator' keyword.  */
9264   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9265     return error_mark_node;
9266   /* And then the name of the operator itself.  */
9267   return cp_parser_operator (parser);
9268 }
9269
9270 /* Parse an operator.
9271
9272    operator:
9273      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9274      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9275      || ++ -- , ->* -> () []
9276
9277    GNU Extensions:
9278
9279    operator:
9280      <? >? <?= >?=
9281
9282    Returns an IDENTIFIER_NODE for the operator which is a
9283    human-readable spelling of the identifier, e.g., `operator +'.  */
9284
9285 static tree
9286 cp_parser_operator (cp_parser* parser)
9287 {
9288   tree id = NULL_TREE;
9289   cp_token *token;
9290
9291   /* Peek at the next token.  */
9292   token = cp_lexer_peek_token (parser->lexer);
9293   /* Figure out which operator we have.  */
9294   switch (token->type)
9295     {
9296     case CPP_KEYWORD:
9297       {
9298         enum tree_code op;
9299
9300         /* The keyword should be either `new' or `delete'.  */
9301         if (token->keyword == RID_NEW)
9302           op = NEW_EXPR;
9303         else if (token->keyword == RID_DELETE)
9304           op = DELETE_EXPR;
9305         else
9306           break;
9307
9308         /* Consume the `new' or `delete' token.  */
9309         cp_lexer_consume_token (parser->lexer);
9310
9311         /* Peek at the next token.  */
9312         token = cp_lexer_peek_token (parser->lexer);
9313         /* If it's a `[' token then this is the array variant of the
9314            operator.  */
9315         if (token->type == CPP_OPEN_SQUARE)
9316           {
9317             /* Consume the `[' token.  */
9318             cp_lexer_consume_token (parser->lexer);
9319             /* Look for the `]' token.  */
9320             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9321             id = ansi_opname (op == NEW_EXPR
9322                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9323           }
9324         /* Otherwise, we have the non-array variant.  */
9325         else
9326           id = ansi_opname (op);
9327
9328         return id;
9329       }
9330
9331     case CPP_PLUS:
9332       id = ansi_opname (PLUS_EXPR);
9333       break;
9334
9335     case CPP_MINUS:
9336       id = ansi_opname (MINUS_EXPR);
9337       break;
9338
9339     case CPP_MULT:
9340       id = ansi_opname (MULT_EXPR);
9341       break;
9342
9343     case CPP_DIV:
9344       id = ansi_opname (TRUNC_DIV_EXPR);
9345       break;
9346
9347     case CPP_MOD:
9348       id = ansi_opname (TRUNC_MOD_EXPR);
9349       break;
9350
9351     case CPP_XOR:
9352       id = ansi_opname (BIT_XOR_EXPR);
9353       break;
9354
9355     case CPP_AND:
9356       id = ansi_opname (BIT_AND_EXPR);
9357       break;
9358
9359     case CPP_OR:
9360       id = ansi_opname (BIT_IOR_EXPR);
9361       break;
9362
9363     case CPP_COMPL:
9364       id = ansi_opname (BIT_NOT_EXPR);
9365       break;
9366
9367     case CPP_NOT:
9368       id = ansi_opname (TRUTH_NOT_EXPR);
9369       break;
9370
9371     case CPP_EQ:
9372       id = ansi_assopname (NOP_EXPR);
9373       break;
9374
9375     case CPP_LESS:
9376       id = ansi_opname (LT_EXPR);
9377       break;
9378
9379     case CPP_GREATER:
9380       id = ansi_opname (GT_EXPR);
9381       break;
9382
9383     case CPP_PLUS_EQ:
9384       id = ansi_assopname (PLUS_EXPR);
9385       break;
9386
9387     case CPP_MINUS_EQ:
9388       id = ansi_assopname (MINUS_EXPR);
9389       break;
9390
9391     case CPP_MULT_EQ:
9392       id = ansi_assopname (MULT_EXPR);
9393       break;
9394
9395     case CPP_DIV_EQ:
9396       id = ansi_assopname (TRUNC_DIV_EXPR);
9397       break;
9398
9399     case CPP_MOD_EQ:
9400       id = ansi_assopname (TRUNC_MOD_EXPR);
9401       break;
9402
9403     case CPP_XOR_EQ:
9404       id = ansi_assopname (BIT_XOR_EXPR);
9405       break;
9406
9407     case CPP_AND_EQ:
9408       id = ansi_assopname (BIT_AND_EXPR);
9409       break;
9410
9411     case CPP_OR_EQ:
9412       id = ansi_assopname (BIT_IOR_EXPR);
9413       break;
9414
9415     case CPP_LSHIFT:
9416       id = ansi_opname (LSHIFT_EXPR);
9417       break;
9418
9419     case CPP_RSHIFT:
9420       id = ansi_opname (RSHIFT_EXPR);
9421       break;
9422
9423     case CPP_LSHIFT_EQ:
9424       id = ansi_assopname (LSHIFT_EXPR);
9425       break;
9426
9427     case CPP_RSHIFT_EQ:
9428       id = ansi_assopname (RSHIFT_EXPR);
9429       break;
9430
9431     case CPP_EQ_EQ:
9432       id = ansi_opname (EQ_EXPR);
9433       break;
9434
9435     case CPP_NOT_EQ:
9436       id = ansi_opname (NE_EXPR);
9437       break;
9438
9439     case CPP_LESS_EQ:
9440       id = ansi_opname (LE_EXPR);
9441       break;
9442
9443     case CPP_GREATER_EQ:
9444       id = ansi_opname (GE_EXPR);
9445       break;
9446
9447     case CPP_AND_AND:
9448       id = ansi_opname (TRUTH_ANDIF_EXPR);
9449       break;
9450
9451     case CPP_OR_OR:
9452       id = ansi_opname (TRUTH_ORIF_EXPR);
9453       break;
9454
9455     case CPP_PLUS_PLUS:
9456       id = ansi_opname (POSTINCREMENT_EXPR);
9457       break;
9458
9459     case CPP_MINUS_MINUS:
9460       id = ansi_opname (PREDECREMENT_EXPR);
9461       break;
9462
9463     case CPP_COMMA:
9464       id = ansi_opname (COMPOUND_EXPR);
9465       break;
9466
9467     case CPP_DEREF_STAR:
9468       id = ansi_opname (MEMBER_REF);
9469       break;
9470
9471     case CPP_DEREF:
9472       id = ansi_opname (COMPONENT_REF);
9473       break;
9474
9475     case CPP_OPEN_PAREN:
9476       /* Consume the `('.  */
9477       cp_lexer_consume_token (parser->lexer);
9478       /* Look for the matching `)'.  */
9479       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9480       return ansi_opname (CALL_EXPR);
9481
9482     case CPP_OPEN_SQUARE:
9483       /* Consume the `['.  */
9484       cp_lexer_consume_token (parser->lexer);
9485       /* Look for the matching `]'.  */
9486       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9487       return ansi_opname (ARRAY_REF);
9488
9489     default:
9490       /* Anything else is an error.  */
9491       break;
9492     }
9493
9494   /* If we have selected an identifier, we need to consume the
9495      operator token.  */
9496   if (id)
9497     cp_lexer_consume_token (parser->lexer);
9498   /* Otherwise, no valid operator name was present.  */
9499   else
9500     {
9501       cp_parser_error (parser, "expected operator");
9502       id = error_mark_node;
9503     }
9504
9505   return id;
9506 }
9507
9508 /* Parse a template-declaration.
9509
9510    template-declaration:
9511      export [opt] template < template-parameter-list > declaration
9512
9513    If MEMBER_P is TRUE, this template-declaration occurs within a
9514    class-specifier.
9515
9516    The grammar rule given by the standard isn't correct.  What
9517    is really meant is:
9518
9519    template-declaration:
9520      export [opt] template-parameter-list-seq
9521        decl-specifier-seq [opt] init-declarator [opt] ;
9522      export [opt] template-parameter-list-seq
9523        function-definition
9524
9525    template-parameter-list-seq:
9526      template-parameter-list-seq [opt]
9527      template < template-parameter-list >  */
9528
9529 static void
9530 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9531 {
9532   /* Check for `export'.  */
9533   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9534     {
9535       /* Consume the `export' token.  */
9536       cp_lexer_consume_token (parser->lexer);
9537       /* Warn that we do not support `export'.  */
9538       warning (0, "keyword %<export%> not implemented, and will be ignored");
9539     }
9540
9541   cp_parser_template_declaration_after_export (parser, member_p);
9542 }
9543
9544 /* Parse a template-parameter-list.
9545
9546    template-parameter-list:
9547      template-parameter
9548      template-parameter-list , template-parameter
9549
9550    Returns a TREE_LIST.  Each node represents a template parameter.
9551    The nodes are connected via their TREE_CHAINs.  */
9552
9553 static tree
9554 cp_parser_template_parameter_list (cp_parser* parser)
9555 {
9556   tree parameter_list = NULL_TREE;
9557
9558   begin_template_parm_list ();
9559   while (true)
9560     {
9561       tree parameter;
9562       bool is_non_type;
9563       bool is_parameter_pack;
9564
9565       /* Parse the template-parameter.  */
9566       parameter = cp_parser_template_parameter (parser, 
9567                                                 &is_non_type,
9568                                                 &is_parameter_pack);
9569       /* Add it to the list.  */
9570       if (parameter != error_mark_node)
9571         parameter_list = process_template_parm (parameter_list,
9572                                                 parameter,
9573                                                 is_non_type,
9574                                                 is_parameter_pack);
9575       else
9576        {
9577          tree err_parm = build_tree_list (parameter, parameter);
9578          TREE_VALUE (err_parm) = error_mark_node;
9579          parameter_list = chainon (parameter_list, err_parm);
9580        }
9581
9582       /* If the next token is not a `,', we're done.  */
9583       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9584         break;
9585       /* Otherwise, consume the `,' token.  */
9586       cp_lexer_consume_token (parser->lexer);
9587     }
9588
9589   return end_template_parm_list (parameter_list);
9590 }
9591
9592 /* Parse a template-parameter.
9593
9594    template-parameter:
9595      type-parameter
9596      parameter-declaration
9597
9598    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9599    the parameter.  The TREE_PURPOSE is the default value, if any.
9600    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9601    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9602    set to true iff this parameter is a parameter pack. */
9603
9604 static tree
9605 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9606                               bool *is_parameter_pack)
9607 {
9608   cp_token *token;
9609   cp_parameter_declarator *parameter_declarator;
9610   cp_declarator *id_declarator;
9611   tree parm;
9612
9613   /* Assume it is a type parameter or a template parameter.  */
9614   *is_non_type = false;
9615   /* Assume it not a parameter pack. */
9616   *is_parameter_pack = false;
9617   /* Peek at the next token.  */
9618   token = cp_lexer_peek_token (parser->lexer);
9619   /* If it is `class' or `template', we have a type-parameter.  */
9620   if (token->keyword == RID_TEMPLATE)
9621     return cp_parser_type_parameter (parser, is_parameter_pack);
9622   /* If it is `class' or `typename' we do not know yet whether it is a
9623      type parameter or a non-type parameter.  Consider:
9624
9625        template <typename T, typename T::X X> ...
9626
9627      or:
9628
9629        template <class C, class D*> ...
9630
9631      Here, the first parameter is a type parameter, and the second is
9632      a non-type parameter.  We can tell by looking at the token after
9633      the identifier -- if it is a `,', `=', or `>' then we have a type
9634      parameter.  */
9635   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9636     {
9637       /* Peek at the token after `class' or `typename'.  */
9638       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9639       /* If it's an ellipsis, we have a template type parameter
9640          pack. */
9641       if (token->type == CPP_ELLIPSIS)
9642         return cp_parser_type_parameter (parser, is_parameter_pack);
9643       /* If it's an identifier, skip it.  */
9644       if (token->type == CPP_NAME)
9645         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9646       /* Now, see if the token looks like the end of a template
9647          parameter.  */
9648       if (token->type == CPP_COMMA
9649           || token->type == CPP_EQ
9650           || token->type == CPP_GREATER)
9651         return cp_parser_type_parameter (parser, is_parameter_pack);
9652     }
9653
9654   /* Otherwise, it is a non-type parameter.
9655
9656      [temp.param]
9657
9658      When parsing a default template-argument for a non-type
9659      template-parameter, the first non-nested `>' is taken as the end
9660      of the template parameter-list rather than a greater-than
9661      operator.  */
9662   *is_non_type = true;
9663   parameter_declarator
9664      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9665                                         /*parenthesized_p=*/NULL);
9666
9667   /* If the parameter declaration is marked as a parameter pack, set
9668      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9669      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9670      grokdeclarator. */
9671   if (parameter_declarator
9672       && parameter_declarator->declarator
9673       && parameter_declarator->declarator->parameter_pack_p)
9674     {
9675       *is_parameter_pack = true;
9676       parameter_declarator->declarator->parameter_pack_p = false;
9677     }
9678
9679   /* If the next token is an ellipsis, and we don't already have it
9680      marked as a parameter pack, then we have a parameter pack (that
9681      has no declarator).  */
9682   if (!*is_parameter_pack
9683       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9684       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9685     {
9686       /* Consume the `...'.  */
9687       cp_lexer_consume_token (parser->lexer);
9688       maybe_warn_variadic_templates ();
9689       
9690       *is_parameter_pack = true;
9691     }
9692   /* We might end up with a pack expansion as the type of the non-type
9693      template parameter, in which case this is a non-type template
9694      parameter pack.  */
9695   else if (parameter_declarator
9696            && parameter_declarator->decl_specifiers.type
9697            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9698     {
9699       *is_parameter_pack = true;
9700       parameter_declarator->decl_specifiers.type = 
9701         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9702     }
9703
9704   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9705     {
9706       /* Parameter packs cannot have default arguments.  However, a
9707          user may try to do so, so we'll parse them and give an
9708          appropriate diagnostic here.  */
9709
9710       /* Consume the `='.  */
9711       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9712       cp_lexer_consume_token (parser->lexer);
9713       
9714       /* Find the name of the parameter pack.  */     
9715       id_declarator = parameter_declarator->declarator;
9716       while (id_declarator && id_declarator->kind != cdk_id)
9717         id_declarator = id_declarator->declarator;
9718       
9719       if (id_declarator && id_declarator->kind == cdk_id)
9720         error ("%Htemplate parameter pack %qD cannot have a default argument",
9721                &start_token->location, id_declarator->u.id.unqualified_name);
9722       else
9723         error ("%Htemplate parameter pack cannot have a default argument",
9724                &start_token->location);
9725       
9726       /* Parse the default argument, but throw away the result.  */
9727       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9728     }
9729
9730   parm = grokdeclarator (parameter_declarator->declarator,
9731                          &parameter_declarator->decl_specifiers,
9732                          PARM, /*initialized=*/0,
9733                          /*attrlist=*/NULL);
9734   if (parm == error_mark_node)
9735     return error_mark_node;
9736
9737   return build_tree_list (parameter_declarator->default_argument, parm);
9738 }
9739
9740 /* Parse a type-parameter.
9741
9742    type-parameter:
9743      class identifier [opt]
9744      class identifier [opt] = type-id
9745      typename identifier [opt]
9746      typename identifier [opt] = type-id
9747      template < template-parameter-list > class identifier [opt]
9748      template < template-parameter-list > class identifier [opt]
9749        = id-expression
9750
9751    GNU Extension (variadic templates):
9752
9753    type-parameter:
9754      class ... identifier [opt]
9755      typename ... identifier [opt]
9756
9757    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9758    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9759    the declaration of the parameter.
9760
9761    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9762
9763 static tree
9764 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9765 {
9766   cp_token *token;
9767   tree parameter;
9768
9769   /* Look for a keyword to tell us what kind of parameter this is.  */
9770   token = cp_parser_require (parser, CPP_KEYWORD,
9771                              "%<class%>, %<typename%>, or %<template%>");
9772   if (!token)
9773     return error_mark_node;
9774
9775   switch (token->keyword)
9776     {
9777     case RID_CLASS:
9778     case RID_TYPENAME:
9779       {
9780         tree identifier;
9781         tree default_argument;
9782
9783         /* If the next token is an ellipsis, we have a template
9784            argument pack. */
9785         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9786           {
9787             /* Consume the `...' token. */
9788             cp_lexer_consume_token (parser->lexer);
9789             maybe_warn_variadic_templates ();
9790
9791             *is_parameter_pack = true;
9792           }
9793
9794         /* If the next token is an identifier, then it names the
9795            parameter.  */
9796         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9797           identifier = cp_parser_identifier (parser);
9798         else
9799           identifier = NULL_TREE;
9800
9801         /* Create the parameter.  */
9802         parameter = finish_template_type_parm (class_type_node, identifier);
9803
9804         /* If the next token is an `=', we have a default argument.  */
9805         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9806           {
9807             /* Consume the `=' token.  */
9808             cp_lexer_consume_token (parser->lexer);
9809             /* Parse the default-argument.  */
9810             push_deferring_access_checks (dk_no_deferred);
9811             default_argument = cp_parser_type_id (parser);
9812
9813             /* Template parameter packs cannot have default
9814                arguments. */
9815             if (*is_parameter_pack)
9816               {
9817                 if (identifier)
9818                   error ("%Htemplate parameter pack %qD cannot have a "
9819                          "default argument", &token->location, identifier);
9820                 else
9821                   error ("%Htemplate parameter packs cannot have "
9822                          "default arguments", &token->location);
9823                 default_argument = NULL_TREE;
9824               }
9825             pop_deferring_access_checks ();
9826           }
9827         else
9828           default_argument = NULL_TREE;
9829
9830         /* Create the combined representation of the parameter and the
9831            default argument.  */
9832         parameter = build_tree_list (default_argument, parameter);
9833       }
9834       break;
9835
9836     case RID_TEMPLATE:
9837       {
9838         tree parameter_list;
9839         tree identifier;
9840         tree default_argument;
9841
9842         /* Look for the `<'.  */
9843         cp_parser_require (parser, CPP_LESS, "%<<%>");
9844         /* Parse the template-parameter-list.  */
9845         parameter_list = cp_parser_template_parameter_list (parser);
9846         /* Look for the `>'.  */
9847         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9848         /* Look for the `class' keyword.  */
9849         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9850         /* If the next token is an ellipsis, we have a template
9851            argument pack. */
9852         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9853           {
9854             /* Consume the `...' token. */
9855             cp_lexer_consume_token (parser->lexer);
9856             maybe_warn_variadic_templates ();
9857
9858             *is_parameter_pack = true;
9859           }
9860         /* If the next token is an `=', then there is a
9861            default-argument.  If the next token is a `>', we are at
9862            the end of the parameter-list.  If the next token is a `,',
9863            then we are at the end of this parameter.  */
9864         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9865             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9866             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9867           {
9868             identifier = cp_parser_identifier (parser);
9869             /* Treat invalid names as if the parameter were nameless.  */
9870             if (identifier == error_mark_node)
9871               identifier = NULL_TREE;
9872           }
9873         else
9874           identifier = NULL_TREE;
9875
9876         /* Create the template parameter.  */
9877         parameter = finish_template_template_parm (class_type_node,
9878                                                    identifier);
9879
9880         /* If the next token is an `=', then there is a
9881            default-argument.  */
9882         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9883           {
9884             bool is_template;
9885
9886             /* Consume the `='.  */
9887             cp_lexer_consume_token (parser->lexer);
9888             /* Parse the id-expression.  */
9889             push_deferring_access_checks (dk_no_deferred);
9890             /* save token before parsing the id-expression, for error
9891                reporting */
9892             token = cp_lexer_peek_token (parser->lexer);
9893             default_argument
9894               = cp_parser_id_expression (parser,
9895                                          /*template_keyword_p=*/false,
9896                                          /*check_dependency_p=*/true,
9897                                          /*template_p=*/&is_template,
9898                                          /*declarator_p=*/false,
9899                                          /*optional_p=*/false);
9900             if (TREE_CODE (default_argument) == TYPE_DECL)
9901               /* If the id-expression was a template-id that refers to
9902                  a template-class, we already have the declaration here,
9903                  so no further lookup is needed.  */
9904                  ;
9905             else
9906               /* Look up the name.  */
9907               default_argument
9908                 = cp_parser_lookup_name (parser, default_argument,
9909                                          none_type,
9910                                          /*is_template=*/is_template,
9911                                          /*is_namespace=*/false,
9912                                          /*check_dependency=*/true,
9913                                          /*ambiguous_decls=*/NULL,
9914                                          token->location);
9915             /* See if the default argument is valid.  */
9916             default_argument
9917               = check_template_template_default_arg (default_argument);
9918
9919             /* Template parameter packs cannot have default
9920                arguments. */
9921             if (*is_parameter_pack)
9922               {
9923                 if (identifier)
9924                   error ("%Htemplate parameter pack %qD cannot "
9925                          "have a default argument",
9926                          &token->location, identifier);
9927                 else
9928                   error ("%Htemplate parameter packs cannot "
9929                          "have default arguments",
9930                          &token->location);
9931                 default_argument = NULL_TREE;
9932               }
9933             pop_deferring_access_checks ();
9934           }
9935         else
9936           default_argument = NULL_TREE;
9937
9938         /* Create the combined representation of the parameter and the
9939            default argument.  */
9940         parameter = build_tree_list (default_argument, parameter);
9941       }
9942       break;
9943
9944     default:
9945       gcc_unreachable ();
9946       break;
9947     }
9948
9949   return parameter;
9950 }
9951
9952 /* Parse a template-id.
9953
9954    template-id:
9955      template-name < template-argument-list [opt] >
9956
9957    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9958    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9959    returned.  Otherwise, if the template-name names a function, or set
9960    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9961    names a class, returns a TYPE_DECL for the specialization.
9962
9963    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9964    uninstantiated templates.  */
9965
9966 static tree
9967 cp_parser_template_id (cp_parser *parser,
9968                        bool template_keyword_p,
9969                        bool check_dependency_p,
9970                        bool is_declaration)
9971 {
9972   int i;
9973   tree templ;
9974   tree arguments;
9975   tree template_id;
9976   cp_token_position start_of_id = 0;
9977   deferred_access_check *chk;
9978   VEC (deferred_access_check,gc) *access_check;
9979   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9980   bool is_identifier;
9981
9982   /* If the next token corresponds to a template-id, there is no need
9983      to reparse it.  */
9984   next_token = cp_lexer_peek_token (parser->lexer);
9985   if (next_token->type == CPP_TEMPLATE_ID)
9986     {
9987       struct tree_check *check_value;
9988
9989       /* Get the stored value.  */
9990       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9991       /* Perform any access checks that were deferred.  */
9992       access_check = check_value->checks;
9993       if (access_check)
9994         {
9995           for (i = 0 ;
9996                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9997                ++i)
9998             {
9999               perform_or_defer_access_check (chk->binfo,
10000                                              chk->decl,
10001                                              chk->diag_decl);
10002             }
10003         }
10004       /* Return the stored value.  */
10005       return check_value->value;
10006     }
10007
10008   /* Avoid performing name lookup if there is no possibility of
10009      finding a template-id.  */
10010   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10011       || (next_token->type == CPP_NAME
10012           && !cp_parser_nth_token_starts_template_argument_list_p
10013                (parser, 2)))
10014     {
10015       cp_parser_error (parser, "expected template-id");
10016       return error_mark_node;
10017     }
10018
10019   /* Remember where the template-id starts.  */
10020   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10021     start_of_id = cp_lexer_token_position (parser->lexer, false);
10022
10023   push_deferring_access_checks (dk_deferred);
10024
10025   /* Parse the template-name.  */
10026   is_identifier = false;
10027   token = cp_lexer_peek_token (parser->lexer);
10028   templ = cp_parser_template_name (parser, template_keyword_p,
10029                                    check_dependency_p,
10030                                    is_declaration,
10031                                    &is_identifier);
10032   if (templ == error_mark_node || is_identifier)
10033     {
10034       pop_deferring_access_checks ();
10035       return templ;
10036     }
10037
10038   /* If we find the sequence `[:' after a template-name, it's probably
10039      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10040      parse correctly the argument list.  */
10041   next_token = cp_lexer_peek_token (parser->lexer);
10042   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10043   if (next_token->type == CPP_OPEN_SQUARE
10044       && next_token->flags & DIGRAPH
10045       && next_token_2->type == CPP_COLON
10046       && !(next_token_2->flags & PREV_WHITE))
10047     {
10048       cp_parser_parse_tentatively (parser);
10049       /* Change `:' into `::'.  */
10050       next_token_2->type = CPP_SCOPE;
10051       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10052          CPP_LESS.  */
10053       cp_lexer_consume_token (parser->lexer);
10054
10055       /* Parse the arguments.  */
10056       arguments = cp_parser_enclosed_template_argument_list (parser);
10057       if (!cp_parser_parse_definitely (parser))
10058         {
10059           /* If we couldn't parse an argument list, then we revert our changes
10060              and return simply an error. Maybe this is not a template-id
10061              after all.  */
10062           next_token_2->type = CPP_COLON;
10063           cp_parser_error (parser, "expected %<<%>");
10064           pop_deferring_access_checks ();
10065           return error_mark_node;
10066         }
10067       /* Otherwise, emit an error about the invalid digraph, but continue
10068          parsing because we got our argument list.  */
10069       if (permerror (next_token->location,
10070                      "%<<::%> cannot begin a template-argument list"))
10071         {
10072           static bool hint = false;
10073           inform (next_token->location,
10074                   "%<<:%> is an alternate spelling for %<[%>."
10075                   " Insert whitespace between %<<%> and %<::%>");
10076           if (!hint && !flag_permissive)
10077             {
10078               inform (next_token->location, "(if you use %<-fpermissive%>"
10079                       " G++ will accept your code)");
10080               hint = true;
10081             }
10082         }
10083     }
10084   else
10085     {
10086       /* Look for the `<' that starts the template-argument-list.  */
10087       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10088         {
10089           pop_deferring_access_checks ();
10090           return error_mark_node;
10091         }
10092       /* Parse the arguments.  */
10093       arguments = cp_parser_enclosed_template_argument_list (parser);
10094     }
10095
10096   /* Build a representation of the specialization.  */
10097   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10098     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10099   else if (DECL_CLASS_TEMPLATE_P (templ)
10100            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10101     {
10102       bool entering_scope;
10103       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10104          template (rather than some instantiation thereof) only if
10105          is not nested within some other construct.  For example, in
10106          "template <typename T> void f(T) { A<T>::", A<T> is just an
10107          instantiation of A.  */
10108       entering_scope = (template_parm_scope_p ()
10109                         && cp_lexer_next_token_is (parser->lexer,
10110                                                    CPP_SCOPE));
10111       template_id
10112         = finish_template_type (templ, arguments, entering_scope);
10113     }
10114   else
10115     {
10116       /* If it's not a class-template or a template-template, it should be
10117          a function-template.  */
10118       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10119                    || TREE_CODE (templ) == OVERLOAD
10120                    || BASELINK_P (templ)));
10121
10122       template_id = lookup_template_function (templ, arguments);
10123     }
10124
10125   /* If parsing tentatively, replace the sequence of tokens that makes
10126      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10127      should we re-parse the token stream, we will not have to repeat
10128      the effort required to do the parse, nor will we issue duplicate
10129      error messages about problems during instantiation of the
10130      template.  */
10131   if (start_of_id)
10132     {
10133       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10134
10135       /* Reset the contents of the START_OF_ID token.  */
10136       token->type = CPP_TEMPLATE_ID;
10137       /* Retrieve any deferred checks.  Do not pop this access checks yet
10138          so the memory will not be reclaimed during token replacing below.  */
10139       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10140       token->u.tree_check_value->value = template_id;
10141       token->u.tree_check_value->checks = get_deferred_access_checks ();
10142       token->keyword = RID_MAX;
10143
10144       /* Purge all subsequent tokens.  */
10145       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10146
10147       /* ??? Can we actually assume that, if template_id ==
10148          error_mark_node, we will have issued a diagnostic to the
10149          user, as opposed to simply marking the tentative parse as
10150          failed?  */
10151       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10152         error ("%Hparse error in template argument list",
10153                &token->location);
10154     }
10155
10156   pop_deferring_access_checks ();
10157   return template_id;
10158 }
10159
10160 /* Parse a template-name.
10161
10162    template-name:
10163      identifier
10164
10165    The standard should actually say:
10166
10167    template-name:
10168      identifier
10169      operator-function-id
10170
10171    A defect report has been filed about this issue.
10172
10173    A conversion-function-id cannot be a template name because they cannot
10174    be part of a template-id. In fact, looking at this code:
10175
10176    a.operator K<int>()
10177
10178    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10179    It is impossible to call a templated conversion-function-id with an
10180    explicit argument list, since the only allowed template parameter is
10181    the type to which it is converting.
10182
10183    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10184    `template' keyword, in a construction like:
10185
10186      T::template f<3>()
10187
10188    In that case `f' is taken to be a template-name, even though there
10189    is no way of knowing for sure.
10190
10191    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10192    name refers to a set of overloaded functions, at least one of which
10193    is a template, or an IDENTIFIER_NODE with the name of the template,
10194    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10195    names are looked up inside uninstantiated templates.  */
10196
10197 static tree
10198 cp_parser_template_name (cp_parser* parser,
10199                          bool template_keyword_p,
10200                          bool check_dependency_p,
10201                          bool is_declaration,
10202                          bool *is_identifier)
10203 {
10204   tree identifier;
10205   tree decl;
10206   tree fns;
10207   cp_token *token = cp_lexer_peek_token (parser->lexer);
10208
10209   /* If the next token is `operator', then we have either an
10210      operator-function-id or a conversion-function-id.  */
10211   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10212     {
10213       /* We don't know whether we're looking at an
10214          operator-function-id or a conversion-function-id.  */
10215       cp_parser_parse_tentatively (parser);
10216       /* Try an operator-function-id.  */
10217       identifier = cp_parser_operator_function_id (parser);
10218       /* If that didn't work, try a conversion-function-id.  */
10219       if (!cp_parser_parse_definitely (parser))
10220         {
10221           cp_parser_error (parser, "expected template-name");
10222           return error_mark_node;
10223         }
10224     }
10225   /* Look for the identifier.  */
10226   else
10227     identifier = cp_parser_identifier (parser);
10228
10229   /* If we didn't find an identifier, we don't have a template-id.  */
10230   if (identifier == error_mark_node)
10231     return error_mark_node;
10232
10233   /* If the name immediately followed the `template' keyword, then it
10234      is a template-name.  However, if the next token is not `<', then
10235      we do not treat it as a template-name, since it is not being used
10236      as part of a template-id.  This enables us to handle constructs
10237      like:
10238
10239        template <typename T> struct S { S(); };
10240        template <typename T> S<T>::S();
10241
10242      correctly.  We would treat `S' as a template -- if it were `S<T>'
10243      -- but we do not if there is no `<'.  */
10244
10245   if (processing_template_decl
10246       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10247     {
10248       /* In a declaration, in a dependent context, we pretend that the
10249          "template" keyword was present in order to improve error
10250          recovery.  For example, given:
10251
10252            template <typename T> void f(T::X<int>);
10253
10254          we want to treat "X<int>" as a template-id.  */
10255       if (is_declaration
10256           && !template_keyword_p
10257           && parser->scope && TYPE_P (parser->scope)
10258           && check_dependency_p
10259           && dependent_type_p (parser->scope)
10260           /* Do not do this for dtors (or ctors), since they never
10261              need the template keyword before their name.  */
10262           && !constructor_name_p (identifier, parser->scope))
10263         {
10264           cp_token_position start = 0;
10265
10266           /* Explain what went wrong.  */
10267           error ("%Hnon-template %qD used as template",
10268                  &token->location, identifier);
10269           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10270                   parser->scope, identifier);
10271           /* If parsing tentatively, find the location of the "<" token.  */
10272           if (cp_parser_simulate_error (parser))
10273             start = cp_lexer_token_position (parser->lexer, true);
10274           /* Parse the template arguments so that we can issue error
10275              messages about them.  */
10276           cp_lexer_consume_token (parser->lexer);
10277           cp_parser_enclosed_template_argument_list (parser);
10278           /* Skip tokens until we find a good place from which to
10279              continue parsing.  */
10280           cp_parser_skip_to_closing_parenthesis (parser,
10281                                                  /*recovering=*/true,
10282                                                  /*or_comma=*/true,
10283                                                  /*consume_paren=*/false);
10284           /* If parsing tentatively, permanently remove the
10285              template argument list.  That will prevent duplicate
10286              error messages from being issued about the missing
10287              "template" keyword.  */
10288           if (start)
10289             cp_lexer_purge_tokens_after (parser->lexer, start);
10290           if (is_identifier)
10291             *is_identifier = true;
10292           return identifier;
10293         }
10294
10295       /* If the "template" keyword is present, then there is generally
10296          no point in doing name-lookup, so we just return IDENTIFIER.
10297          But, if the qualifying scope is non-dependent then we can
10298          (and must) do name-lookup normally.  */
10299       if (template_keyword_p
10300           && (!parser->scope
10301               || (TYPE_P (parser->scope)
10302                   && dependent_type_p (parser->scope))))
10303         return identifier;
10304     }
10305
10306   /* Look up the name.  */
10307   decl = cp_parser_lookup_name (parser, identifier,
10308                                 none_type,
10309                                 /*is_template=*/false,
10310                                 /*is_namespace=*/false,
10311                                 check_dependency_p,
10312                                 /*ambiguous_decls=*/NULL,
10313                                 token->location);
10314   decl = maybe_get_template_decl_from_type_decl (decl);
10315
10316   /* If DECL is a template, then the name was a template-name.  */
10317   if (TREE_CODE (decl) == TEMPLATE_DECL)
10318     ;
10319   else
10320     {
10321       tree fn = NULL_TREE;
10322
10323       /* The standard does not explicitly indicate whether a name that
10324          names a set of overloaded declarations, some of which are
10325          templates, is a template-name.  However, such a name should
10326          be a template-name; otherwise, there is no way to form a
10327          template-id for the overloaded templates.  */
10328       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10329       if (TREE_CODE (fns) == OVERLOAD)
10330         for (fn = fns; fn; fn = OVL_NEXT (fn))
10331           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10332             break;
10333
10334       if (!fn)
10335         {
10336           /* The name does not name a template.  */
10337           cp_parser_error (parser, "expected template-name");
10338           return error_mark_node;
10339         }
10340     }
10341
10342   /* If DECL is dependent, and refers to a function, then just return
10343      its name; we will look it up again during template instantiation.  */
10344   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10345     {
10346       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10347       if (TYPE_P (scope) && dependent_type_p (scope))
10348         return identifier;
10349     }
10350
10351   return decl;
10352 }
10353
10354 /* Parse a template-argument-list.
10355
10356    template-argument-list:
10357      template-argument ... [opt]
10358      template-argument-list , template-argument ... [opt]
10359
10360    Returns a TREE_VEC containing the arguments.  */
10361
10362 static tree
10363 cp_parser_template_argument_list (cp_parser* parser)
10364 {
10365   tree fixed_args[10];
10366   unsigned n_args = 0;
10367   unsigned alloced = 10;
10368   tree *arg_ary = fixed_args;
10369   tree vec;
10370   bool saved_in_template_argument_list_p;
10371   bool saved_ice_p;
10372   bool saved_non_ice_p;
10373
10374   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10375   parser->in_template_argument_list_p = true;
10376   /* Even if the template-id appears in an integral
10377      constant-expression, the contents of the argument list do
10378      not.  */
10379   saved_ice_p = parser->integral_constant_expression_p;
10380   parser->integral_constant_expression_p = false;
10381   saved_non_ice_p = parser->non_integral_constant_expression_p;
10382   parser->non_integral_constant_expression_p = false;
10383   /* Parse the arguments.  */
10384   do
10385     {
10386       tree argument;
10387
10388       if (n_args)
10389         /* Consume the comma.  */
10390         cp_lexer_consume_token (parser->lexer);
10391
10392       /* Parse the template-argument.  */
10393       argument = cp_parser_template_argument (parser);
10394
10395       /* If the next token is an ellipsis, we're expanding a template
10396          argument pack. */
10397       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10398         {
10399           /* Consume the `...' token. */
10400           cp_lexer_consume_token (parser->lexer);
10401
10402           /* Make the argument into a TYPE_PACK_EXPANSION or
10403              EXPR_PACK_EXPANSION. */
10404           argument = make_pack_expansion (argument);
10405         }
10406
10407       if (n_args == alloced)
10408         {
10409           alloced *= 2;
10410
10411           if (arg_ary == fixed_args)
10412             {
10413               arg_ary = XNEWVEC (tree, alloced);
10414               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10415             }
10416           else
10417             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10418         }
10419       arg_ary[n_args++] = argument;
10420     }
10421   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10422
10423   vec = make_tree_vec (n_args);
10424
10425   while (n_args--)
10426     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10427
10428   if (arg_ary != fixed_args)
10429     free (arg_ary);
10430   parser->non_integral_constant_expression_p = saved_non_ice_p;
10431   parser->integral_constant_expression_p = saved_ice_p;
10432   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10433   return vec;
10434 }
10435
10436 /* Parse a template-argument.
10437
10438    template-argument:
10439      assignment-expression
10440      type-id
10441      id-expression
10442
10443    The representation is that of an assignment-expression, type-id, or
10444    id-expression -- except that the qualified id-expression is
10445    evaluated, so that the value returned is either a DECL or an
10446    OVERLOAD.
10447
10448    Although the standard says "assignment-expression", it forbids
10449    throw-expressions or assignments in the template argument.
10450    Therefore, we use "conditional-expression" instead.  */
10451
10452 static tree
10453 cp_parser_template_argument (cp_parser* parser)
10454 {
10455   tree argument;
10456   bool template_p;
10457   bool address_p;
10458   bool maybe_type_id = false;
10459   cp_token *token = NULL, *argument_start_token = NULL;
10460   cp_id_kind idk;
10461
10462   /* There's really no way to know what we're looking at, so we just
10463      try each alternative in order.
10464
10465        [temp.arg]
10466
10467        In a template-argument, an ambiguity between a type-id and an
10468        expression is resolved to a type-id, regardless of the form of
10469        the corresponding template-parameter.
10470
10471      Therefore, we try a type-id first.  */
10472   cp_parser_parse_tentatively (parser);
10473   argument = cp_parser_type_id (parser);
10474   /* If there was no error parsing the type-id but the next token is a
10475      '>>', our behavior depends on which dialect of C++ we're
10476      parsing. In C++98, we probably found a typo for '> >'. But there
10477      are type-id which are also valid expressions. For instance:
10478
10479      struct X { int operator >> (int); };
10480      template <int V> struct Foo {};
10481      Foo<X () >> 5> r;
10482
10483      Here 'X()' is a valid type-id of a function type, but the user just
10484      wanted to write the expression "X() >> 5". Thus, we remember that we
10485      found a valid type-id, but we still try to parse the argument as an
10486      expression to see what happens. 
10487
10488      In C++0x, the '>>' will be considered two separate '>'
10489      tokens.  */
10490   if (!cp_parser_error_occurred (parser)
10491       && cxx_dialect == cxx98
10492       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10493     {
10494       maybe_type_id = true;
10495       cp_parser_abort_tentative_parse (parser);
10496     }
10497   else
10498     {
10499       /* If the next token isn't a `,' or a `>', then this argument wasn't
10500       really finished. This means that the argument is not a valid
10501       type-id.  */
10502       if (!cp_parser_next_token_ends_template_argument_p (parser))
10503         cp_parser_error (parser, "expected template-argument");
10504       /* If that worked, we're done.  */
10505       if (cp_parser_parse_definitely (parser))
10506         return argument;
10507     }
10508   /* We're still not sure what the argument will be.  */
10509   cp_parser_parse_tentatively (parser);
10510   /* Try a template.  */
10511   argument_start_token = cp_lexer_peek_token (parser->lexer);
10512   argument = cp_parser_id_expression (parser,
10513                                       /*template_keyword_p=*/false,
10514                                       /*check_dependency_p=*/true,
10515                                       &template_p,
10516                                       /*declarator_p=*/false,
10517                                       /*optional_p=*/false);
10518   /* If the next token isn't a `,' or a `>', then this argument wasn't
10519      really finished.  */
10520   if (!cp_parser_next_token_ends_template_argument_p (parser))
10521     cp_parser_error (parser, "expected template-argument");
10522   if (!cp_parser_error_occurred (parser))
10523     {
10524       /* Figure out what is being referred to.  If the id-expression
10525          was for a class template specialization, then we will have a
10526          TYPE_DECL at this point.  There is no need to do name lookup
10527          at this point in that case.  */
10528       if (TREE_CODE (argument) != TYPE_DECL)
10529         argument = cp_parser_lookup_name (parser, argument,
10530                                           none_type,
10531                                           /*is_template=*/template_p,
10532                                           /*is_namespace=*/false,
10533                                           /*check_dependency=*/true,
10534                                           /*ambiguous_decls=*/NULL,
10535                                           argument_start_token->location);
10536       if (TREE_CODE (argument) != TEMPLATE_DECL
10537           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10538         cp_parser_error (parser, "expected template-name");
10539     }
10540   if (cp_parser_parse_definitely (parser))
10541     return argument;
10542   /* It must be a non-type argument.  There permitted cases are given
10543      in [temp.arg.nontype]:
10544
10545      -- an integral constant-expression of integral or enumeration
10546         type; or
10547
10548      -- the name of a non-type template-parameter; or
10549
10550      -- the name of an object or function with external linkage...
10551
10552      -- the address of an object or function with external linkage...
10553
10554      -- a pointer to member...  */
10555   /* Look for a non-type template parameter.  */
10556   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10557     {
10558       cp_parser_parse_tentatively (parser);
10559       argument = cp_parser_primary_expression (parser,
10560                                                /*address_p=*/false,
10561                                                /*cast_p=*/false,
10562                                                /*template_arg_p=*/true,
10563                                                &idk);
10564       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10565           || !cp_parser_next_token_ends_template_argument_p (parser))
10566         cp_parser_simulate_error (parser);
10567       if (cp_parser_parse_definitely (parser))
10568         return argument;
10569     }
10570
10571   /* If the next token is "&", the argument must be the address of an
10572      object or function with external linkage.  */
10573   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10574   if (address_p)
10575     cp_lexer_consume_token (parser->lexer);
10576   /* See if we might have an id-expression.  */
10577   token = cp_lexer_peek_token (parser->lexer);
10578   if (token->type == CPP_NAME
10579       || token->keyword == RID_OPERATOR
10580       || token->type == CPP_SCOPE
10581       || token->type == CPP_TEMPLATE_ID
10582       || token->type == CPP_NESTED_NAME_SPECIFIER)
10583     {
10584       cp_parser_parse_tentatively (parser);
10585       argument = cp_parser_primary_expression (parser,
10586                                                address_p,
10587                                                /*cast_p=*/false,
10588                                                /*template_arg_p=*/true,
10589                                                &idk);
10590       if (cp_parser_error_occurred (parser)
10591           || !cp_parser_next_token_ends_template_argument_p (parser))
10592         cp_parser_abort_tentative_parse (parser);
10593       else
10594         {
10595           if (TREE_CODE (argument) == INDIRECT_REF)
10596             {
10597               gcc_assert (REFERENCE_REF_P (argument));
10598               argument = TREE_OPERAND (argument, 0);
10599             }
10600
10601           if (TREE_CODE (argument) == VAR_DECL)
10602             {
10603               /* A variable without external linkage might still be a
10604                  valid constant-expression, so no error is issued here
10605                  if the external-linkage check fails.  */
10606               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10607                 cp_parser_simulate_error (parser);
10608             }
10609           else if (is_overloaded_fn (argument))
10610             /* All overloaded functions are allowed; if the external
10611                linkage test does not pass, an error will be issued
10612                later.  */
10613             ;
10614           else if (address_p
10615                    && (TREE_CODE (argument) == OFFSET_REF
10616                        || TREE_CODE (argument) == SCOPE_REF))
10617             /* A pointer-to-member.  */
10618             ;
10619           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10620             ;
10621           else
10622             cp_parser_simulate_error (parser);
10623
10624           if (cp_parser_parse_definitely (parser))
10625             {
10626               if (address_p)
10627                 argument = build_x_unary_op (ADDR_EXPR, argument,
10628                                              tf_warning_or_error);
10629               return argument;
10630             }
10631         }
10632     }
10633   /* If the argument started with "&", there are no other valid
10634      alternatives at this point.  */
10635   if (address_p)
10636     {
10637       cp_parser_error (parser, "invalid non-type template argument");
10638       return error_mark_node;
10639     }
10640
10641   /* If the argument wasn't successfully parsed as a type-id followed
10642      by '>>', the argument can only be a constant expression now.
10643      Otherwise, we try parsing the constant-expression tentatively,
10644      because the argument could really be a type-id.  */
10645   if (maybe_type_id)
10646     cp_parser_parse_tentatively (parser);
10647   argument = cp_parser_constant_expression (parser,
10648                                             /*allow_non_constant_p=*/false,
10649                                             /*non_constant_p=*/NULL);
10650   argument = fold_non_dependent_expr (argument);
10651   if (!maybe_type_id)
10652     return argument;
10653   if (!cp_parser_next_token_ends_template_argument_p (parser))
10654     cp_parser_error (parser, "expected template-argument");
10655   if (cp_parser_parse_definitely (parser))
10656     return argument;
10657   /* We did our best to parse the argument as a non type-id, but that
10658      was the only alternative that matched (albeit with a '>' after
10659      it). We can assume it's just a typo from the user, and a
10660      diagnostic will then be issued.  */
10661   return cp_parser_type_id (parser);
10662 }
10663
10664 /* Parse an explicit-instantiation.
10665
10666    explicit-instantiation:
10667      template declaration
10668
10669    Although the standard says `declaration', what it really means is:
10670
10671    explicit-instantiation:
10672      template decl-specifier-seq [opt] declarator [opt] ;
10673
10674    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10675    supposed to be allowed.  A defect report has been filed about this
10676    issue.
10677
10678    GNU Extension:
10679
10680    explicit-instantiation:
10681      storage-class-specifier template
10682        decl-specifier-seq [opt] declarator [opt] ;
10683      function-specifier template
10684        decl-specifier-seq [opt] declarator [opt] ;  */
10685
10686 static void
10687 cp_parser_explicit_instantiation (cp_parser* parser)
10688 {
10689   int declares_class_or_enum;
10690   cp_decl_specifier_seq decl_specifiers;
10691   tree extension_specifier = NULL_TREE;
10692   cp_token *token;
10693
10694   /* Look for an (optional) storage-class-specifier or
10695      function-specifier.  */
10696   if (cp_parser_allow_gnu_extensions_p (parser))
10697     {
10698       extension_specifier
10699         = cp_parser_storage_class_specifier_opt (parser);
10700       if (!extension_specifier)
10701         extension_specifier
10702           = cp_parser_function_specifier_opt (parser,
10703                                               /*decl_specs=*/NULL);
10704     }
10705
10706   /* Look for the `template' keyword.  */
10707   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10708   /* Let the front end know that we are processing an explicit
10709      instantiation.  */
10710   begin_explicit_instantiation ();
10711   /* [temp.explicit] says that we are supposed to ignore access
10712      control while processing explicit instantiation directives.  */
10713   push_deferring_access_checks (dk_no_check);
10714   /* Parse a decl-specifier-seq.  */
10715   token = cp_lexer_peek_token (parser->lexer);
10716   cp_parser_decl_specifier_seq (parser,
10717                                 CP_PARSER_FLAGS_OPTIONAL,
10718                                 &decl_specifiers,
10719                                 &declares_class_or_enum);
10720   /* If there was exactly one decl-specifier, and it declared a class,
10721      and there's no declarator, then we have an explicit type
10722      instantiation.  */
10723   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10724     {
10725       tree type;
10726
10727       type = check_tag_decl (&decl_specifiers);
10728       /* Turn access control back on for names used during
10729          template instantiation.  */
10730       pop_deferring_access_checks ();
10731       if (type)
10732         do_type_instantiation (type, extension_specifier,
10733                                /*complain=*/tf_error);
10734     }
10735   else
10736     {
10737       cp_declarator *declarator;
10738       tree decl;
10739
10740       /* Parse the declarator.  */
10741       declarator
10742         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10743                                 /*ctor_dtor_or_conv_p=*/NULL,
10744                                 /*parenthesized_p=*/NULL,
10745                                 /*member_p=*/false);
10746       if (declares_class_or_enum & 2)
10747         cp_parser_check_for_definition_in_return_type (declarator,
10748                                                        decl_specifiers.type,
10749                                                        decl_specifiers.type_location);
10750       if (declarator != cp_error_declarator)
10751         {
10752           decl = grokdeclarator (declarator, &decl_specifiers,
10753                                  NORMAL, 0, &decl_specifiers.attributes);
10754           /* Turn access control back on for names used during
10755              template instantiation.  */
10756           pop_deferring_access_checks ();
10757           /* Do the explicit instantiation.  */
10758           do_decl_instantiation (decl, extension_specifier);
10759         }
10760       else
10761         {
10762           pop_deferring_access_checks ();
10763           /* Skip the body of the explicit instantiation.  */
10764           cp_parser_skip_to_end_of_statement (parser);
10765         }
10766     }
10767   /* We're done with the instantiation.  */
10768   end_explicit_instantiation ();
10769
10770   cp_parser_consume_semicolon_at_end_of_statement (parser);
10771 }
10772
10773 /* Parse an explicit-specialization.
10774
10775    explicit-specialization:
10776      template < > declaration
10777
10778    Although the standard says `declaration', what it really means is:
10779
10780    explicit-specialization:
10781      template <> decl-specifier [opt] init-declarator [opt] ;
10782      template <> function-definition
10783      template <> explicit-specialization
10784      template <> template-declaration  */
10785
10786 static void
10787 cp_parser_explicit_specialization (cp_parser* parser)
10788 {
10789   bool need_lang_pop;
10790   cp_token *token = cp_lexer_peek_token (parser->lexer);
10791
10792   /* Look for the `template' keyword.  */
10793   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10794   /* Look for the `<'.  */
10795   cp_parser_require (parser, CPP_LESS, "%<<%>");
10796   /* Look for the `>'.  */
10797   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10798   /* We have processed another parameter list.  */
10799   ++parser->num_template_parameter_lists;
10800   /* [temp]
10801
10802      A template ... explicit specialization ... shall not have C
10803      linkage.  */
10804   if (current_lang_name == lang_name_c)
10805     {
10806       error ("%Htemplate specialization with C linkage", &token->location);
10807       /* Give it C++ linkage to avoid confusing other parts of the
10808          front end.  */
10809       push_lang_context (lang_name_cplusplus);
10810       need_lang_pop = true;
10811     }
10812   else
10813     need_lang_pop = false;
10814   /* Let the front end know that we are beginning a specialization.  */
10815   if (!begin_specialization ())
10816     {
10817       end_specialization ();
10818       cp_parser_skip_to_end_of_block_or_statement (parser);
10819       return;
10820     }
10821
10822   /* If the next keyword is `template', we need to figure out whether
10823      or not we're looking a template-declaration.  */
10824   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10825     {
10826       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10827           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10828         cp_parser_template_declaration_after_export (parser,
10829                                                      /*member_p=*/false);
10830       else
10831         cp_parser_explicit_specialization (parser);
10832     }
10833   else
10834     /* Parse the dependent declaration.  */
10835     cp_parser_single_declaration (parser,
10836                                   /*checks=*/NULL,
10837                                   /*member_p=*/false,
10838                                   /*explicit_specialization_p=*/true,
10839                                   /*friend_p=*/NULL);
10840   /* We're done with the specialization.  */
10841   end_specialization ();
10842   /* For the erroneous case of a template with C linkage, we pushed an
10843      implicit C++ linkage scope; exit that scope now.  */
10844   if (need_lang_pop)
10845     pop_lang_context ();
10846   /* We're done with this parameter list.  */
10847   --parser->num_template_parameter_lists;
10848 }
10849
10850 /* Parse a type-specifier.
10851
10852    type-specifier:
10853      simple-type-specifier
10854      class-specifier
10855      enum-specifier
10856      elaborated-type-specifier
10857      cv-qualifier
10858
10859    GNU Extension:
10860
10861    type-specifier:
10862      __complex__
10863
10864    Returns a representation of the type-specifier.  For a
10865    class-specifier, enum-specifier, or elaborated-type-specifier, a
10866    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10867
10868    The parser flags FLAGS is used to control type-specifier parsing.
10869
10870    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10871    in a decl-specifier-seq.
10872
10873    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10874    class-specifier, enum-specifier, or elaborated-type-specifier, then
10875    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10876    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10877    zero.
10878
10879    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10880    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10881    is set to FALSE.  */
10882
10883 static tree
10884 cp_parser_type_specifier (cp_parser* parser,
10885                           cp_parser_flags flags,
10886                           cp_decl_specifier_seq *decl_specs,
10887                           bool is_declaration,
10888                           int* declares_class_or_enum,
10889                           bool* is_cv_qualifier)
10890 {
10891   tree type_spec = NULL_TREE;
10892   cp_token *token;
10893   enum rid keyword;
10894   cp_decl_spec ds = ds_last;
10895
10896   /* Assume this type-specifier does not declare a new type.  */
10897   if (declares_class_or_enum)
10898     *declares_class_or_enum = 0;
10899   /* And that it does not specify a cv-qualifier.  */
10900   if (is_cv_qualifier)
10901     *is_cv_qualifier = false;
10902   /* Peek at the next token.  */
10903   token = cp_lexer_peek_token (parser->lexer);
10904
10905   /* If we're looking at a keyword, we can use that to guide the
10906      production we choose.  */
10907   keyword = token->keyword;
10908   switch (keyword)
10909     {
10910     case RID_ENUM:
10911       /* Look for the enum-specifier.  */
10912       type_spec = cp_parser_enum_specifier (parser);
10913       /* If that worked, we're done.  */
10914       if (type_spec)
10915         {
10916           if (declares_class_or_enum)
10917             *declares_class_or_enum = 2;
10918           if (decl_specs)
10919             cp_parser_set_decl_spec_type (decl_specs,
10920                                           type_spec,
10921                                           token->location,
10922                                           /*user_defined_p=*/true);
10923           return type_spec;
10924         }
10925       else
10926         goto elaborated_type_specifier;
10927
10928       /* Any of these indicate either a class-specifier, or an
10929          elaborated-type-specifier.  */
10930     case RID_CLASS:
10931     case RID_STRUCT:
10932     case RID_UNION:
10933       /* Parse tentatively so that we can back up if we don't find a
10934          class-specifier.  */
10935       cp_parser_parse_tentatively (parser);
10936       /* Look for the class-specifier.  */
10937       type_spec = cp_parser_class_specifier (parser);
10938       /* If that worked, we're done.  */
10939       if (cp_parser_parse_definitely (parser))
10940         {
10941           if (declares_class_or_enum)
10942             *declares_class_or_enum = 2;
10943           if (decl_specs)
10944             cp_parser_set_decl_spec_type (decl_specs,
10945                                           type_spec,
10946                                           token->location,
10947                                           /*user_defined_p=*/true);
10948           return type_spec;
10949         }
10950
10951       /* Fall through.  */
10952     elaborated_type_specifier:
10953       /* We're declaring (not defining) a class or enum.  */
10954       if (declares_class_or_enum)
10955         *declares_class_or_enum = 1;
10956
10957       /* Fall through.  */
10958     case RID_TYPENAME:
10959       /* Look for an elaborated-type-specifier.  */
10960       type_spec
10961         = (cp_parser_elaborated_type_specifier
10962            (parser,
10963             decl_specs && decl_specs->specs[(int) ds_friend],
10964             is_declaration));
10965       if (decl_specs)
10966         cp_parser_set_decl_spec_type (decl_specs,
10967                                       type_spec,
10968                                       token->location,
10969                                       /*user_defined_p=*/true);
10970       return type_spec;
10971
10972     case RID_CONST:
10973       ds = ds_const;
10974       if (is_cv_qualifier)
10975         *is_cv_qualifier = true;
10976       break;
10977
10978     case RID_VOLATILE:
10979       ds = ds_volatile;
10980       if (is_cv_qualifier)
10981         *is_cv_qualifier = true;
10982       break;
10983
10984     case RID_RESTRICT:
10985       ds = ds_restrict;
10986       if (is_cv_qualifier)
10987         *is_cv_qualifier = true;
10988       break;
10989
10990     case RID_COMPLEX:
10991       /* The `__complex__' keyword is a GNU extension.  */
10992       ds = ds_complex;
10993       break;
10994
10995     default:
10996       break;
10997     }
10998
10999   /* Handle simple keywords.  */
11000   if (ds != ds_last)
11001     {
11002       if (decl_specs)
11003         {
11004           ++decl_specs->specs[(int)ds];
11005           decl_specs->any_specifiers_p = true;
11006         }
11007       return cp_lexer_consume_token (parser->lexer)->u.value;
11008     }
11009
11010   /* If we do not already have a type-specifier, assume we are looking
11011      at a simple-type-specifier.  */
11012   type_spec = cp_parser_simple_type_specifier (parser,
11013                                                decl_specs,
11014                                                flags);
11015
11016   /* If we didn't find a type-specifier, and a type-specifier was not
11017      optional in this context, issue an error message.  */
11018   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11019     {
11020       cp_parser_error (parser, "expected type specifier");
11021       return error_mark_node;
11022     }
11023
11024   return type_spec;
11025 }
11026
11027 /* Parse a simple-type-specifier.
11028
11029    simple-type-specifier:
11030      :: [opt] nested-name-specifier [opt] type-name
11031      :: [opt] nested-name-specifier template template-id
11032      char
11033      wchar_t
11034      bool
11035      short
11036      int
11037      long
11038      signed
11039      unsigned
11040      float
11041      double
11042      void
11043
11044    C++0x Extension:
11045
11046    simple-type-specifier:
11047      auto
11048      decltype ( expression )   
11049      char16_t
11050      char32_t
11051
11052    GNU Extension:
11053
11054    simple-type-specifier:
11055      __typeof__ unary-expression
11056      __typeof__ ( type-id )
11057
11058    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11059    appropriately updated.  */
11060
11061 static tree
11062 cp_parser_simple_type_specifier (cp_parser* parser,
11063                                  cp_decl_specifier_seq *decl_specs,
11064                                  cp_parser_flags flags)
11065 {
11066   tree type = NULL_TREE;
11067   cp_token *token;
11068
11069   /* Peek at the next token.  */
11070   token = cp_lexer_peek_token (parser->lexer);
11071
11072   /* If we're looking at a keyword, things are easy.  */
11073   switch (token->keyword)
11074     {
11075     case RID_CHAR:
11076       if (decl_specs)
11077         decl_specs->explicit_char_p = true;
11078       type = char_type_node;
11079       break;
11080     case RID_CHAR16:
11081       type = char16_type_node;
11082       break;
11083     case RID_CHAR32:
11084       type = char32_type_node;
11085       break;
11086     case RID_WCHAR:
11087       type = wchar_type_node;
11088       break;
11089     case RID_BOOL:
11090       type = boolean_type_node;
11091       break;
11092     case RID_SHORT:
11093       if (decl_specs)
11094         ++decl_specs->specs[(int) ds_short];
11095       type = short_integer_type_node;
11096       break;
11097     case RID_INT:
11098       if (decl_specs)
11099         decl_specs->explicit_int_p = true;
11100       type = integer_type_node;
11101       break;
11102     case RID_LONG:
11103       if (decl_specs)
11104         ++decl_specs->specs[(int) ds_long];
11105       type = long_integer_type_node;
11106       break;
11107     case RID_SIGNED:
11108       if (decl_specs)
11109         ++decl_specs->specs[(int) ds_signed];
11110       type = integer_type_node;
11111       break;
11112     case RID_UNSIGNED:
11113       if (decl_specs)
11114         ++decl_specs->specs[(int) ds_unsigned];
11115       type = unsigned_type_node;
11116       break;
11117     case RID_FLOAT:
11118       type = float_type_node;
11119       break;
11120     case RID_DOUBLE:
11121       type = double_type_node;
11122       break;
11123     case RID_VOID:
11124       type = void_type_node;
11125       break;
11126       
11127     case RID_AUTO:
11128       maybe_warn_cpp0x ("C++0x auto");
11129       type = make_auto ();
11130       break;
11131
11132     case RID_DECLTYPE:
11133       /* Parse the `decltype' type.  */
11134       type = cp_parser_decltype (parser);
11135
11136       if (decl_specs)
11137         cp_parser_set_decl_spec_type (decl_specs, type,
11138                                       token->location,
11139                                       /*user_defined_p=*/true);
11140
11141       return type;
11142
11143     case RID_TYPEOF:
11144       /* Consume the `typeof' token.  */
11145       cp_lexer_consume_token (parser->lexer);
11146       /* Parse the operand to `typeof'.  */
11147       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11148       /* If it is not already a TYPE, take its type.  */
11149       if (!TYPE_P (type))
11150         type = finish_typeof (type);
11151
11152       if (decl_specs)
11153         cp_parser_set_decl_spec_type (decl_specs, type,
11154                                       token->location,
11155                                       /*user_defined_p=*/true);
11156
11157       return type;
11158
11159     default:
11160       break;
11161     }
11162
11163   /* If the type-specifier was for a built-in type, we're done.  */
11164   if (type)
11165     {
11166       tree id;
11167
11168       /* Record the type.  */
11169       if (decl_specs
11170           && (token->keyword != RID_SIGNED
11171               && token->keyword != RID_UNSIGNED
11172               && token->keyword != RID_SHORT
11173               && token->keyword != RID_LONG))
11174         cp_parser_set_decl_spec_type (decl_specs,
11175                                       type,
11176                                       token->location,
11177                                       /*user_defined=*/false);
11178       if (decl_specs)
11179         decl_specs->any_specifiers_p = true;
11180
11181       /* Consume the token.  */
11182       id = cp_lexer_consume_token (parser->lexer)->u.value;
11183
11184       /* There is no valid C++ program where a non-template type is
11185          followed by a "<".  That usually indicates that the user thought
11186          that the type was a template.  */
11187       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11188
11189       return TYPE_NAME (type);
11190     }
11191
11192   /* The type-specifier must be a user-defined type.  */
11193   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11194     {
11195       bool qualified_p;
11196       bool global_p;
11197
11198       /* Don't gobble tokens or issue error messages if this is an
11199          optional type-specifier.  */
11200       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11201         cp_parser_parse_tentatively (parser);
11202
11203       /* Look for the optional `::' operator.  */
11204       global_p
11205         = (cp_parser_global_scope_opt (parser,
11206                                        /*current_scope_valid_p=*/false)
11207            != NULL_TREE);
11208       /* Look for the nested-name specifier.  */
11209       qualified_p
11210         = (cp_parser_nested_name_specifier_opt (parser,
11211                                                 /*typename_keyword_p=*/false,
11212                                                 /*check_dependency_p=*/true,
11213                                                 /*type_p=*/false,
11214                                                 /*is_declaration=*/false)
11215            != NULL_TREE);
11216       token = cp_lexer_peek_token (parser->lexer);
11217       /* If we have seen a nested-name-specifier, and the next token
11218          is `template', then we are using the template-id production.  */
11219       if (parser->scope
11220           && cp_parser_optional_template_keyword (parser))
11221         {
11222           /* Look for the template-id.  */
11223           type = cp_parser_template_id (parser,
11224                                         /*template_keyword_p=*/true,
11225                                         /*check_dependency_p=*/true,
11226                                         /*is_declaration=*/false);
11227           /* If the template-id did not name a type, we are out of
11228              luck.  */
11229           if (TREE_CODE (type) != TYPE_DECL)
11230             {
11231               cp_parser_error (parser, "expected template-id for type");
11232               type = NULL_TREE;
11233             }
11234         }
11235       /* Otherwise, look for a type-name.  */
11236       else
11237         type = cp_parser_type_name (parser);
11238       /* Keep track of all name-lookups performed in class scopes.  */
11239       if (type
11240           && !global_p
11241           && !qualified_p
11242           && TREE_CODE (type) == TYPE_DECL
11243           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11244         maybe_note_name_used_in_class (DECL_NAME (type), type);
11245       /* If it didn't work out, we don't have a TYPE.  */
11246       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11247           && !cp_parser_parse_definitely (parser))
11248         type = NULL_TREE;
11249       if (type && decl_specs)
11250         cp_parser_set_decl_spec_type (decl_specs, type,
11251                                       token->location,
11252                                       /*user_defined=*/true);
11253     }
11254
11255   /* If we didn't get a type-name, issue an error message.  */
11256   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11257     {
11258       cp_parser_error (parser, "expected type-name");
11259       return error_mark_node;
11260     }
11261
11262   /* There is no valid C++ program where a non-template type is
11263      followed by a "<".  That usually indicates that the user thought
11264      that the type was a template.  */
11265   if (type && type != error_mark_node)
11266     {
11267       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11268          If it is, then the '<'...'>' enclose protocol names rather than
11269          template arguments, and so everything is fine.  */
11270       if (c_dialect_objc ()
11271           && (objc_is_id (type) || objc_is_class_name (type)))
11272         {
11273           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11274           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11275
11276           /* Clobber the "unqualified" type previously entered into
11277              DECL_SPECS with the new, improved protocol-qualified version.  */
11278           if (decl_specs)
11279             decl_specs->type = qual_type;
11280
11281           return qual_type;
11282         }
11283
11284       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11285                                                token->location);
11286     }
11287
11288   return type;
11289 }
11290
11291 /* Parse a type-name.
11292
11293    type-name:
11294      class-name
11295      enum-name
11296      typedef-name
11297
11298    enum-name:
11299      identifier
11300
11301    typedef-name:
11302      identifier
11303
11304    Returns a TYPE_DECL for the type.  */
11305
11306 static tree
11307 cp_parser_type_name (cp_parser* parser)
11308 {
11309   tree type_decl;
11310
11311   /* We can't know yet whether it is a class-name or not.  */
11312   cp_parser_parse_tentatively (parser);
11313   /* Try a class-name.  */
11314   type_decl = cp_parser_class_name (parser,
11315                                     /*typename_keyword_p=*/false,
11316                                     /*template_keyword_p=*/false,
11317                                     none_type,
11318                                     /*check_dependency_p=*/true,
11319                                     /*class_head_p=*/false,
11320                                     /*is_declaration=*/false);
11321   /* If it's not a class-name, keep looking.  */
11322   if (!cp_parser_parse_definitely (parser))
11323     {
11324       /* It must be a typedef-name or an enum-name.  */
11325       return cp_parser_nonclass_name (parser);
11326     }
11327
11328   return type_decl;
11329 }
11330
11331 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11332
11333    enum-name:
11334      identifier
11335
11336    typedef-name:
11337      identifier
11338
11339    Returns a TYPE_DECL for the type.  */
11340
11341 static tree
11342 cp_parser_nonclass_name (cp_parser* parser)
11343 {
11344   tree type_decl;
11345   tree identifier;
11346
11347   cp_token *token = cp_lexer_peek_token (parser->lexer);
11348   identifier = cp_parser_identifier (parser);
11349   if (identifier == error_mark_node)
11350     return error_mark_node;
11351
11352   /* Look up the type-name.  */
11353   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11354
11355   if (TREE_CODE (type_decl) != TYPE_DECL
11356       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11357     {
11358       /* See if this is an Objective-C type.  */
11359       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11360       tree type = objc_get_protocol_qualified_type (identifier, protos);
11361       if (type)
11362         type_decl = TYPE_NAME (type);
11363     }
11364   
11365   /* Issue an error if we did not find a type-name.  */
11366   if (TREE_CODE (type_decl) != TYPE_DECL)
11367     {
11368       if (!cp_parser_simulate_error (parser))
11369         cp_parser_name_lookup_error (parser, identifier, type_decl,
11370                                      "is not a type", token->location);
11371       return error_mark_node;
11372     }
11373   /* Remember that the name was used in the definition of the
11374      current class so that we can check later to see if the
11375      meaning would have been different after the class was
11376      entirely defined.  */
11377   else if (type_decl != error_mark_node
11378            && !parser->scope)
11379     maybe_note_name_used_in_class (identifier, type_decl);
11380   
11381   return type_decl;
11382 }
11383
11384 /* Parse an elaborated-type-specifier.  Note that the grammar given
11385    here incorporates the resolution to DR68.
11386
11387    elaborated-type-specifier:
11388      class-key :: [opt] nested-name-specifier [opt] identifier
11389      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11390      enum-key :: [opt] nested-name-specifier [opt] identifier
11391      typename :: [opt] nested-name-specifier identifier
11392      typename :: [opt] nested-name-specifier template [opt]
11393        template-id
11394
11395    GNU extension:
11396
11397    elaborated-type-specifier:
11398      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11399      class-key attributes :: [opt] nested-name-specifier [opt]
11400                template [opt] template-id
11401      enum attributes :: [opt] nested-name-specifier [opt] identifier
11402
11403    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11404    declared `friend'.  If IS_DECLARATION is TRUE, then this
11405    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11406    something is being declared.
11407
11408    Returns the TYPE specified.  */
11409
11410 static tree
11411 cp_parser_elaborated_type_specifier (cp_parser* parser,
11412                                      bool is_friend,
11413                                      bool is_declaration)
11414 {
11415   enum tag_types tag_type;
11416   tree identifier;
11417   tree type = NULL_TREE;
11418   tree attributes = NULL_TREE;
11419   cp_token *token = NULL;
11420
11421   /* See if we're looking at the `enum' keyword.  */
11422   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11423     {
11424       /* Consume the `enum' token.  */
11425       cp_lexer_consume_token (parser->lexer);
11426       /* Remember that it's an enumeration type.  */
11427       tag_type = enum_type;
11428       /* Parse the optional `struct' or `class' key (for C++0x scoped
11429          enums).  */
11430       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11431           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11432         {
11433           if (cxx_dialect == cxx98)
11434             maybe_warn_cpp0x ("scoped enums");
11435
11436           /* Consume the `struct' or `class'.  */
11437           cp_lexer_consume_token (parser->lexer);
11438         }
11439       /* Parse the attributes.  */
11440       attributes = cp_parser_attributes_opt (parser);
11441     }
11442   /* Or, it might be `typename'.  */
11443   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11444                                            RID_TYPENAME))
11445     {
11446       /* Consume the `typename' token.  */
11447       cp_lexer_consume_token (parser->lexer);
11448       /* Remember that it's a `typename' type.  */
11449       tag_type = typename_type;
11450       /* The `typename' keyword is only allowed in templates.  */
11451       if (!processing_template_decl)
11452         permerror (input_location, "using %<typename%> outside of template");
11453     }
11454   /* Otherwise it must be a class-key.  */
11455   else
11456     {
11457       tag_type = cp_parser_class_key (parser);
11458       if (tag_type == none_type)
11459         return error_mark_node;
11460       /* Parse the attributes.  */
11461       attributes = cp_parser_attributes_opt (parser);
11462     }
11463
11464   /* Look for the `::' operator.  */
11465   cp_parser_global_scope_opt (parser,
11466                               /*current_scope_valid_p=*/false);
11467   /* Look for the nested-name-specifier.  */
11468   if (tag_type == typename_type)
11469     {
11470       if (!cp_parser_nested_name_specifier (parser,
11471                                            /*typename_keyword_p=*/true,
11472                                            /*check_dependency_p=*/true,
11473                                            /*type_p=*/true,
11474                                             is_declaration))
11475         return error_mark_node;
11476     }
11477   else
11478     /* Even though `typename' is not present, the proposed resolution
11479        to Core Issue 180 says that in `class A<T>::B', `B' should be
11480        considered a type-name, even if `A<T>' is dependent.  */
11481     cp_parser_nested_name_specifier_opt (parser,
11482                                          /*typename_keyword_p=*/true,
11483                                          /*check_dependency_p=*/true,
11484                                          /*type_p=*/true,
11485                                          is_declaration);
11486  /* For everything but enumeration types, consider a template-id.
11487     For an enumeration type, consider only a plain identifier.  */
11488   if (tag_type != enum_type)
11489     {
11490       bool template_p = false;
11491       tree decl;
11492
11493       /* Allow the `template' keyword.  */
11494       template_p = cp_parser_optional_template_keyword (parser);
11495       /* If we didn't see `template', we don't know if there's a
11496          template-id or not.  */
11497       if (!template_p)
11498         cp_parser_parse_tentatively (parser);
11499       /* Parse the template-id.  */
11500       token = cp_lexer_peek_token (parser->lexer);
11501       decl = cp_parser_template_id (parser, template_p,
11502                                     /*check_dependency_p=*/true,
11503                                     is_declaration);
11504       /* If we didn't find a template-id, look for an ordinary
11505          identifier.  */
11506       if (!template_p && !cp_parser_parse_definitely (parser))
11507         ;
11508       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11509          in effect, then we must assume that, upon instantiation, the
11510          template will correspond to a class.  */
11511       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11512                && tag_type == typename_type)
11513         type = make_typename_type (parser->scope, decl,
11514                                    typename_type,
11515                                    /*complain=*/tf_error);
11516       else
11517         type = TREE_TYPE (decl);
11518     }
11519
11520   if (!type)
11521     {
11522       token = cp_lexer_peek_token (parser->lexer);
11523       identifier = cp_parser_identifier (parser);
11524
11525       if (identifier == error_mark_node)
11526         {
11527           parser->scope = NULL_TREE;
11528           return error_mark_node;
11529         }
11530
11531       /* For a `typename', we needn't call xref_tag.  */
11532       if (tag_type == typename_type
11533           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11534         return cp_parser_make_typename_type (parser, parser->scope,
11535                                              identifier,
11536                                              token->location);
11537       /* Look up a qualified name in the usual way.  */
11538       if (parser->scope)
11539         {
11540           tree decl;
11541           tree ambiguous_decls;
11542
11543           decl = cp_parser_lookup_name (parser, identifier,
11544                                         tag_type,
11545                                         /*is_template=*/false,
11546                                         /*is_namespace=*/false,
11547                                         /*check_dependency=*/true,
11548                                         &ambiguous_decls,
11549                                         token->location);
11550
11551           /* If the lookup was ambiguous, an error will already have been
11552              issued.  */
11553           if (ambiguous_decls)
11554             return error_mark_node;
11555
11556           /* If we are parsing friend declaration, DECL may be a
11557              TEMPLATE_DECL tree node here.  However, we need to check
11558              whether this TEMPLATE_DECL results in valid code.  Consider
11559              the following example:
11560
11561                namespace N {
11562                  template <class T> class C {};
11563                }
11564                class X {
11565                  template <class T> friend class N::C; // #1, valid code
11566                };
11567                template <class T> class Y {
11568                  friend class N::C;                    // #2, invalid code
11569                };
11570
11571              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11572              name lookup of `N::C'.  We see that friend declaration must
11573              be template for the code to be valid.  Note that
11574              processing_template_decl does not work here since it is
11575              always 1 for the above two cases.  */
11576
11577           decl = (cp_parser_maybe_treat_template_as_class
11578                   (decl, /*tag_name_p=*/is_friend
11579                          && parser->num_template_parameter_lists));
11580
11581           if (TREE_CODE (decl) != TYPE_DECL)
11582             {
11583               cp_parser_diagnose_invalid_type_name (parser,
11584                                                     parser->scope,
11585                                                     identifier,
11586                                                     token->location);
11587               return error_mark_node;
11588             }
11589
11590           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11591             {
11592               bool allow_template = (parser->num_template_parameter_lists
11593                                       || DECL_SELF_REFERENCE_P (decl));
11594               type = check_elaborated_type_specifier (tag_type, decl, 
11595                                                       allow_template);
11596
11597               if (type == error_mark_node)
11598                 return error_mark_node;
11599             }
11600
11601           /* Forward declarations of nested types, such as
11602
11603                class C1::C2;
11604                class C1::C2::C3;
11605
11606              are invalid unless all components preceding the final '::'
11607              are complete.  If all enclosing types are complete, these
11608              declarations become merely pointless.
11609
11610              Invalid forward declarations of nested types are errors
11611              caught elsewhere in parsing.  Those that are pointless arrive
11612              here.  */
11613
11614           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11615               && !is_friend && !processing_explicit_instantiation)
11616             warning (0, "declaration %qD does not declare anything", decl);
11617
11618           type = TREE_TYPE (decl);
11619         }
11620       else
11621         {
11622           /* An elaborated-type-specifier sometimes introduces a new type and
11623              sometimes names an existing type.  Normally, the rule is that it
11624              introduces a new type only if there is not an existing type of
11625              the same name already in scope.  For example, given:
11626
11627                struct S {};
11628                void f() { struct S s; }
11629
11630              the `struct S' in the body of `f' is the same `struct S' as in
11631              the global scope; the existing definition is used.  However, if
11632              there were no global declaration, this would introduce a new
11633              local class named `S'.
11634
11635              An exception to this rule applies to the following code:
11636
11637                namespace N { struct S; }
11638
11639              Here, the elaborated-type-specifier names a new type
11640              unconditionally; even if there is already an `S' in the
11641              containing scope this declaration names a new type.
11642              This exception only applies if the elaborated-type-specifier
11643              forms the complete declaration:
11644
11645                [class.name]
11646
11647                A declaration consisting solely of `class-key identifier ;' is
11648                either a redeclaration of the name in the current scope or a
11649                forward declaration of the identifier as a class name.  It
11650                introduces the name into the current scope.
11651
11652              We are in this situation precisely when the next token is a `;'.
11653
11654              An exception to the exception is that a `friend' declaration does
11655              *not* name a new type; i.e., given:
11656
11657                struct S { friend struct T; };
11658
11659              `T' is not a new type in the scope of `S'.
11660
11661              Also, `new struct S' or `sizeof (struct S)' never results in the
11662              definition of a new type; a new type can only be declared in a
11663              declaration context.  */
11664
11665           tag_scope ts;
11666           bool template_p;
11667
11668           if (is_friend)
11669             /* Friends have special name lookup rules.  */
11670             ts = ts_within_enclosing_non_class;
11671           else if (is_declaration
11672                    && cp_lexer_next_token_is (parser->lexer,
11673                                               CPP_SEMICOLON))
11674             /* This is a `class-key identifier ;' */
11675             ts = ts_current;
11676           else
11677             ts = ts_global;
11678
11679           template_p =
11680             (parser->num_template_parameter_lists
11681              && (cp_parser_next_token_starts_class_definition_p (parser)
11682                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11683           /* An unqualified name was used to reference this type, so
11684              there were no qualifying templates.  */
11685           if (!cp_parser_check_template_parameters (parser,
11686                                                     /*num_templates=*/0,
11687                                                     token->location))
11688             return error_mark_node;
11689           type = xref_tag (tag_type, identifier, ts, template_p);
11690         }
11691     }
11692
11693   if (type == error_mark_node)
11694     return error_mark_node;
11695
11696   /* Allow attributes on forward declarations of classes.  */
11697   if (attributes)
11698     {
11699       if (TREE_CODE (type) == TYPENAME_TYPE)
11700         warning (OPT_Wattributes,
11701                  "attributes ignored on uninstantiated type");
11702       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11703                && ! processing_explicit_instantiation)
11704         warning (OPT_Wattributes,
11705                  "attributes ignored on template instantiation");
11706       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11707         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11708       else
11709         warning (OPT_Wattributes,
11710                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11711     }
11712
11713   if (tag_type != enum_type)
11714     cp_parser_check_class_key (tag_type, type);
11715
11716   /* A "<" cannot follow an elaborated type specifier.  If that
11717      happens, the user was probably trying to form a template-id.  */
11718   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11719
11720   return type;
11721 }
11722
11723 /* Parse an enum-specifier.
11724
11725    enum-specifier:
11726      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11727
11728    enum-key:
11729      enum
11730      enum class   [C++0x]
11731      enum struct  [C++0x]
11732
11733    enum-base:   [C++0x]
11734      : type-specifier-seq
11735
11736    GNU Extensions:
11737      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11738        { enumerator-list [opt] }attributes[opt]
11739
11740    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11741    if the token stream isn't an enum-specifier after all.  */
11742
11743 static tree
11744 cp_parser_enum_specifier (cp_parser* parser)
11745 {
11746   tree identifier;
11747   tree type;
11748   tree attributes;
11749   bool scoped_enum_p = false;
11750   bool has_underlying_type = false;
11751   tree underlying_type = NULL_TREE;
11752
11753   /* Parse tentatively so that we can back up if we don't find a
11754      enum-specifier.  */
11755   cp_parser_parse_tentatively (parser);
11756
11757   /* Caller guarantees that the current token is 'enum', an identifier
11758      possibly follows, and the token after that is an opening brace.
11759      If we don't have an identifier, fabricate an anonymous name for
11760      the enumeration being defined.  */
11761   cp_lexer_consume_token (parser->lexer);
11762
11763   /* Parse the "class" or "struct", which indicates a scoped
11764      enumeration type in C++0x.  */
11765   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11766       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11767     {
11768       if (cxx_dialect == cxx98)
11769         maybe_warn_cpp0x ("scoped enums");
11770
11771       /* Consume the `struct' or `class' token.  */
11772       cp_lexer_consume_token (parser->lexer);
11773
11774       scoped_enum_p = true;
11775     }
11776
11777   attributes = cp_parser_attributes_opt (parser);
11778
11779   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11780     identifier = cp_parser_identifier (parser);
11781   else
11782     identifier = make_anon_name ();
11783
11784   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11785   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11786     {
11787       cp_decl_specifier_seq type_specifiers;
11788
11789       /* At this point this is surely not elaborated type specifier.  */
11790       if (!cp_parser_parse_definitely (parser))
11791         return NULL_TREE;
11792
11793       if (cxx_dialect == cxx98)
11794         maybe_warn_cpp0x ("scoped enums");
11795
11796       /* Consume the `:'.  */
11797       cp_lexer_consume_token (parser->lexer);
11798
11799       has_underlying_type = true;
11800
11801       /* Parse the type-specifier-seq.  */
11802       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11803                                     &type_specifiers);
11804
11805       /* If that didn't work, stop.  */
11806       if (type_specifiers.type != error_mark_node)
11807         {
11808           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11809                                             /*initialized=*/0, NULL);
11810           if (underlying_type == error_mark_node)
11811             underlying_type = NULL_TREE;
11812         }
11813     }
11814
11815   /* Look for the `{' but don't consume it yet.  */
11816   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11817     {
11818       cp_parser_error (parser, "expected %<{%>");
11819       if (has_underlying_type)
11820         return NULL_TREE;
11821     }
11822
11823   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11824     return NULL_TREE;
11825
11826   /* Issue an error message if type-definitions are forbidden here.  */
11827   if (!cp_parser_check_type_definition (parser))
11828     type = error_mark_node;
11829   else
11830     /* Create the new type.  We do this before consuming the opening
11831        brace so the enum will be recorded as being on the line of its
11832        tag (or the 'enum' keyword, if there is no tag).  */
11833     type = start_enum (identifier, underlying_type, scoped_enum_p);
11834   
11835   /* Consume the opening brace.  */
11836   cp_lexer_consume_token (parser->lexer);
11837
11838   if (type == error_mark_node)
11839     {
11840       cp_parser_skip_to_end_of_block_or_statement (parser);
11841       return error_mark_node;
11842     }
11843
11844   /* If the next token is not '}', then there are some enumerators.  */
11845   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11846     cp_parser_enumerator_list (parser, type);
11847
11848   /* Consume the final '}'.  */
11849   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11850
11851   /* Look for trailing attributes to apply to this enumeration, and
11852      apply them if appropriate.  */
11853   if (cp_parser_allow_gnu_extensions_p (parser))
11854     {
11855       tree trailing_attr = cp_parser_attributes_opt (parser);
11856       cplus_decl_attributes (&type,
11857                              trailing_attr,
11858                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11859     }
11860
11861   /* Finish up the enumeration.  */
11862   finish_enum (type);
11863
11864   return type;
11865 }
11866
11867 /* Parse an enumerator-list.  The enumerators all have the indicated
11868    TYPE.
11869
11870    enumerator-list:
11871      enumerator-definition
11872      enumerator-list , enumerator-definition  */
11873
11874 static void
11875 cp_parser_enumerator_list (cp_parser* parser, tree type)
11876 {
11877   while (true)
11878     {
11879       /* Parse an enumerator-definition.  */
11880       cp_parser_enumerator_definition (parser, type);
11881
11882       /* If the next token is not a ',', we've reached the end of
11883          the list.  */
11884       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11885         break;
11886       /* Otherwise, consume the `,' and keep going.  */
11887       cp_lexer_consume_token (parser->lexer);
11888       /* If the next token is a `}', there is a trailing comma.  */
11889       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11890         {
11891           if (!in_system_header)
11892             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11893           break;
11894         }
11895     }
11896 }
11897
11898 /* Parse an enumerator-definition.  The enumerator has the indicated
11899    TYPE.
11900
11901    enumerator-definition:
11902      enumerator
11903      enumerator = constant-expression
11904
11905    enumerator:
11906      identifier  */
11907
11908 static void
11909 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11910 {
11911   tree identifier;
11912   tree value;
11913
11914   /* Look for the identifier.  */
11915   identifier = cp_parser_identifier (parser);
11916   if (identifier == error_mark_node)
11917     return;
11918
11919   /* If the next token is an '=', then there is an explicit value.  */
11920   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11921     {
11922       /* Consume the `=' token.  */
11923       cp_lexer_consume_token (parser->lexer);
11924       /* Parse the value.  */
11925       value = cp_parser_constant_expression (parser,
11926                                              /*allow_non_constant_p=*/false,
11927                                              NULL);
11928     }
11929   else
11930     value = NULL_TREE;
11931
11932   /* Create the enumerator.  */
11933   build_enumerator (identifier, value, type);
11934 }
11935
11936 /* Parse a namespace-name.
11937
11938    namespace-name:
11939      original-namespace-name
11940      namespace-alias
11941
11942    Returns the NAMESPACE_DECL for the namespace.  */
11943
11944 static tree
11945 cp_parser_namespace_name (cp_parser* parser)
11946 {
11947   tree identifier;
11948   tree namespace_decl;
11949
11950   cp_token *token = cp_lexer_peek_token (parser->lexer);
11951
11952   /* Get the name of the namespace.  */
11953   identifier = cp_parser_identifier (parser);
11954   if (identifier == error_mark_node)
11955     return error_mark_node;
11956
11957   /* Look up the identifier in the currently active scope.  Look only
11958      for namespaces, due to:
11959
11960        [basic.lookup.udir]
11961
11962        When looking up a namespace-name in a using-directive or alias
11963        definition, only namespace names are considered.
11964
11965      And:
11966
11967        [basic.lookup.qual]
11968
11969        During the lookup of a name preceding the :: scope resolution
11970        operator, object, function, and enumerator names are ignored.
11971
11972      (Note that cp_parser_qualifying_entity only calls this
11973      function if the token after the name is the scope resolution
11974      operator.)  */
11975   namespace_decl = cp_parser_lookup_name (parser, identifier,
11976                                           none_type,
11977                                           /*is_template=*/false,
11978                                           /*is_namespace=*/true,
11979                                           /*check_dependency=*/true,
11980                                           /*ambiguous_decls=*/NULL,
11981                                           token->location);
11982   /* If it's not a namespace, issue an error.  */
11983   if (namespace_decl == error_mark_node
11984       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11985     {
11986       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11987         error ("%H%qD is not a namespace-name", &token->location, identifier);
11988       cp_parser_error (parser, "expected namespace-name");
11989       namespace_decl = error_mark_node;
11990     }
11991
11992   return namespace_decl;
11993 }
11994
11995 /* Parse a namespace-definition.
11996
11997    namespace-definition:
11998      named-namespace-definition
11999      unnamed-namespace-definition
12000
12001    named-namespace-definition:
12002      original-namespace-definition
12003      extension-namespace-definition
12004
12005    original-namespace-definition:
12006      namespace identifier { namespace-body }
12007
12008    extension-namespace-definition:
12009      namespace original-namespace-name { namespace-body }
12010
12011    unnamed-namespace-definition:
12012      namespace { namespace-body } */
12013
12014 static void
12015 cp_parser_namespace_definition (cp_parser* parser)
12016 {
12017   tree identifier, attribs;
12018   bool has_visibility;
12019   bool is_inline;
12020
12021   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12022     {
12023       is_inline = true;
12024       cp_lexer_consume_token (parser->lexer);
12025     }
12026   else
12027     is_inline = false;
12028
12029   /* Look for the `namespace' keyword.  */
12030   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12031
12032   /* Get the name of the namespace.  We do not attempt to distinguish
12033      between an original-namespace-definition and an
12034      extension-namespace-definition at this point.  The semantic
12035      analysis routines are responsible for that.  */
12036   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12037     identifier = cp_parser_identifier (parser);
12038   else
12039     identifier = NULL_TREE;
12040
12041   /* Parse any specified attributes.  */
12042   attribs = cp_parser_attributes_opt (parser);
12043
12044   /* Look for the `{' to start the namespace.  */
12045   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12046   /* Start the namespace.  */
12047   push_namespace (identifier);
12048
12049   /* "inline namespace" is equivalent to a stub namespace definition
12050      followed by a strong using directive.  */
12051   if (is_inline)
12052     {
12053       tree name_space = current_namespace;
12054       /* Set up namespace association.  */
12055       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12056         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12057                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12058       /* Import the contents of the inline namespace.  */
12059       pop_namespace ();
12060       do_using_directive (name_space);
12061       push_namespace (identifier);
12062     }
12063
12064   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12065
12066   /* Parse the body of the namespace.  */
12067   cp_parser_namespace_body (parser);
12068
12069 #ifdef HANDLE_PRAGMA_VISIBILITY
12070   if (has_visibility)
12071     pop_visibility ();
12072 #endif
12073
12074   /* Finish the namespace.  */
12075   pop_namespace ();
12076   /* Look for the final `}'.  */
12077   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12078 }
12079
12080 /* Parse a namespace-body.
12081
12082    namespace-body:
12083      declaration-seq [opt]  */
12084
12085 static void
12086 cp_parser_namespace_body (cp_parser* parser)
12087 {
12088   cp_parser_declaration_seq_opt (parser);
12089 }
12090
12091 /* Parse a namespace-alias-definition.
12092
12093    namespace-alias-definition:
12094      namespace identifier = qualified-namespace-specifier ;  */
12095
12096 static void
12097 cp_parser_namespace_alias_definition (cp_parser* parser)
12098 {
12099   tree identifier;
12100   tree namespace_specifier;
12101
12102   cp_token *token = cp_lexer_peek_token (parser->lexer);
12103
12104   /* Look for the `namespace' keyword.  */
12105   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12106   /* Look for the identifier.  */
12107   identifier = cp_parser_identifier (parser);
12108   if (identifier == error_mark_node)
12109     return;
12110   /* Look for the `=' token.  */
12111   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12112       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12113     {
12114       error ("%H%<namespace%> definition is not allowed here", &token->location);
12115       /* Skip the definition.  */
12116       cp_lexer_consume_token (parser->lexer);
12117       if (cp_parser_skip_to_closing_brace (parser))
12118         cp_lexer_consume_token (parser->lexer);
12119       return;
12120     }
12121   cp_parser_require (parser, CPP_EQ, "%<=%>");
12122   /* Look for the qualified-namespace-specifier.  */
12123   namespace_specifier
12124     = cp_parser_qualified_namespace_specifier (parser);
12125   /* Look for the `;' token.  */
12126   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12127
12128   /* Register the alias in the symbol table.  */
12129   do_namespace_alias (identifier, namespace_specifier);
12130 }
12131
12132 /* Parse a qualified-namespace-specifier.
12133
12134    qualified-namespace-specifier:
12135      :: [opt] nested-name-specifier [opt] namespace-name
12136
12137    Returns a NAMESPACE_DECL corresponding to the specified
12138    namespace.  */
12139
12140 static tree
12141 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12142 {
12143   /* Look for the optional `::'.  */
12144   cp_parser_global_scope_opt (parser,
12145                               /*current_scope_valid_p=*/false);
12146
12147   /* Look for the optional nested-name-specifier.  */
12148   cp_parser_nested_name_specifier_opt (parser,
12149                                        /*typename_keyword_p=*/false,
12150                                        /*check_dependency_p=*/true,
12151                                        /*type_p=*/false,
12152                                        /*is_declaration=*/true);
12153
12154   return cp_parser_namespace_name (parser);
12155 }
12156
12157 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12158    access declaration.
12159
12160    using-declaration:
12161      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12162      using :: unqualified-id ;  
12163
12164    access-declaration:
12165      qualified-id ;  
12166
12167    */
12168
12169 static bool
12170 cp_parser_using_declaration (cp_parser* parser, 
12171                              bool access_declaration_p)
12172 {
12173   cp_token *token;
12174   bool typename_p = false;
12175   bool global_scope_p;
12176   tree decl;
12177   tree identifier;
12178   tree qscope;
12179
12180   if (access_declaration_p)
12181     cp_parser_parse_tentatively (parser);
12182   else
12183     {
12184       /* Look for the `using' keyword.  */
12185       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12186       
12187       /* Peek at the next token.  */
12188       token = cp_lexer_peek_token (parser->lexer);
12189       /* See if it's `typename'.  */
12190       if (token->keyword == RID_TYPENAME)
12191         {
12192           /* Remember that we've seen it.  */
12193           typename_p = true;
12194           /* Consume the `typename' token.  */
12195           cp_lexer_consume_token (parser->lexer);
12196         }
12197     }
12198
12199   /* Look for the optional global scope qualification.  */
12200   global_scope_p
12201     = (cp_parser_global_scope_opt (parser,
12202                                    /*current_scope_valid_p=*/false)
12203        != NULL_TREE);
12204
12205   /* If we saw `typename', or didn't see `::', then there must be a
12206      nested-name-specifier present.  */
12207   if (typename_p || !global_scope_p)
12208     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12209                                               /*check_dependency_p=*/true,
12210                                               /*type_p=*/false,
12211                                               /*is_declaration=*/true);
12212   /* Otherwise, we could be in either of the two productions.  In that
12213      case, treat the nested-name-specifier as optional.  */
12214   else
12215     qscope = cp_parser_nested_name_specifier_opt (parser,
12216                                                   /*typename_keyword_p=*/false,
12217                                                   /*check_dependency_p=*/true,
12218                                                   /*type_p=*/false,
12219                                                   /*is_declaration=*/true);
12220   if (!qscope)
12221     qscope = global_namespace;
12222
12223   if (access_declaration_p && cp_parser_error_occurred (parser))
12224     /* Something has already gone wrong; there's no need to parse
12225        further.  Since an error has occurred, the return value of
12226        cp_parser_parse_definitely will be false, as required.  */
12227     return cp_parser_parse_definitely (parser);
12228
12229   token = cp_lexer_peek_token (parser->lexer);
12230   /* Parse the unqualified-id.  */
12231   identifier = cp_parser_unqualified_id (parser,
12232                                          /*template_keyword_p=*/false,
12233                                          /*check_dependency_p=*/true,
12234                                          /*declarator_p=*/true,
12235                                          /*optional_p=*/false);
12236
12237   if (access_declaration_p)
12238     {
12239       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12240         cp_parser_simulate_error (parser);
12241       if (!cp_parser_parse_definitely (parser))
12242         return false;
12243     }
12244
12245   /* The function we call to handle a using-declaration is different
12246      depending on what scope we are in.  */
12247   if (qscope == error_mark_node || identifier == error_mark_node)
12248     ;
12249   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12250            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12251     /* [namespace.udecl]
12252
12253        A using declaration shall not name a template-id.  */
12254     error ("%Ha template-id may not appear in a using-declaration",
12255             &token->location);
12256   else
12257     {
12258       if (at_class_scope_p ())
12259         {
12260           /* Create the USING_DECL.  */
12261           decl = do_class_using_decl (parser->scope, identifier);
12262
12263           if (check_for_bare_parameter_packs (decl))
12264             return false;
12265           else
12266             /* Add it to the list of members in this class.  */
12267             finish_member_declaration (decl);
12268         }
12269       else
12270         {
12271           decl = cp_parser_lookup_name_simple (parser,
12272                                                identifier,
12273                                                token->location);
12274           if (decl == error_mark_node)
12275             cp_parser_name_lookup_error (parser, identifier,
12276                                          decl, NULL,
12277                                          token->location);
12278           else if (check_for_bare_parameter_packs (decl))
12279             return false;
12280           else if (!at_namespace_scope_p ())
12281             do_local_using_decl (decl, qscope, identifier);
12282           else
12283             do_toplevel_using_decl (decl, qscope, identifier);
12284         }
12285     }
12286
12287   /* Look for the final `;'.  */
12288   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12289   
12290   return true;
12291 }
12292
12293 /* Parse a using-directive.
12294
12295    using-directive:
12296      using namespace :: [opt] nested-name-specifier [opt]
12297        namespace-name ;  */
12298
12299 static void
12300 cp_parser_using_directive (cp_parser* parser)
12301 {
12302   tree namespace_decl;
12303   tree attribs;
12304
12305   /* Look for the `using' keyword.  */
12306   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12307   /* And the `namespace' keyword.  */
12308   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12309   /* Look for the optional `::' operator.  */
12310   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12311   /* And the optional nested-name-specifier.  */
12312   cp_parser_nested_name_specifier_opt (parser,
12313                                        /*typename_keyword_p=*/false,
12314                                        /*check_dependency_p=*/true,
12315                                        /*type_p=*/false,
12316                                        /*is_declaration=*/true);
12317   /* Get the namespace being used.  */
12318   namespace_decl = cp_parser_namespace_name (parser);
12319   /* And any specified attributes.  */
12320   attribs = cp_parser_attributes_opt (parser);
12321   /* Update the symbol table.  */
12322   parse_using_directive (namespace_decl, attribs);
12323   /* Look for the final `;'.  */
12324   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12325 }
12326
12327 /* Parse an asm-definition.
12328
12329    asm-definition:
12330      asm ( string-literal ) ;
12331
12332    GNU Extension:
12333
12334    asm-definition:
12335      asm volatile [opt] ( string-literal ) ;
12336      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12337      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12338                           : asm-operand-list [opt] ) ;
12339      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12340                           : asm-operand-list [opt]
12341                           : asm-operand-list [opt] ) ;  */
12342
12343 static void
12344 cp_parser_asm_definition (cp_parser* parser)
12345 {
12346   tree string;
12347   tree outputs = NULL_TREE;
12348   tree inputs = NULL_TREE;
12349   tree clobbers = NULL_TREE;
12350   tree asm_stmt;
12351   bool volatile_p = false;
12352   bool extended_p = false;
12353   bool invalid_inputs_p = false;
12354   bool invalid_outputs_p = false;
12355
12356   /* Look for the `asm' keyword.  */
12357   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12358   /* See if the next token is `volatile'.  */
12359   if (cp_parser_allow_gnu_extensions_p (parser)
12360       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12361     {
12362       /* Remember that we saw the `volatile' keyword.  */
12363       volatile_p = true;
12364       /* Consume the token.  */
12365       cp_lexer_consume_token (parser->lexer);
12366     }
12367   /* Look for the opening `('.  */
12368   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12369     return;
12370   /* Look for the string.  */
12371   string = cp_parser_string_literal (parser, false, false);
12372   if (string == error_mark_node)
12373     {
12374       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12375                                              /*consume_paren=*/true);
12376       return;
12377     }
12378
12379   /* If we're allowing GNU extensions, check for the extended assembly
12380      syntax.  Unfortunately, the `:' tokens need not be separated by
12381      a space in C, and so, for compatibility, we tolerate that here
12382      too.  Doing that means that we have to treat the `::' operator as
12383      two `:' tokens.  */
12384   if (cp_parser_allow_gnu_extensions_p (parser)
12385       && parser->in_function_body
12386       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12387           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12388     {
12389       bool inputs_p = false;
12390       bool clobbers_p = false;
12391
12392       /* The extended syntax was used.  */
12393       extended_p = true;
12394
12395       /* Look for outputs.  */
12396       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12397         {
12398           /* Consume the `:'.  */
12399           cp_lexer_consume_token (parser->lexer);
12400           /* Parse the output-operands.  */
12401           if (cp_lexer_next_token_is_not (parser->lexer,
12402                                           CPP_COLON)
12403               && cp_lexer_next_token_is_not (parser->lexer,
12404                                              CPP_SCOPE)
12405               && cp_lexer_next_token_is_not (parser->lexer,
12406                                              CPP_CLOSE_PAREN))
12407             outputs = cp_parser_asm_operand_list (parser);
12408
12409             if (outputs == error_mark_node)
12410               invalid_outputs_p = true;
12411         }
12412       /* If the next token is `::', there are no outputs, and the
12413          next token is the beginning of the inputs.  */
12414       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12415         /* The inputs are coming next.  */
12416         inputs_p = true;
12417
12418       /* Look for inputs.  */
12419       if (inputs_p
12420           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12421         {
12422           /* Consume the `:' or `::'.  */
12423           cp_lexer_consume_token (parser->lexer);
12424           /* Parse the output-operands.  */
12425           if (cp_lexer_next_token_is_not (parser->lexer,
12426                                           CPP_COLON)
12427               && cp_lexer_next_token_is_not (parser->lexer,
12428                                              CPP_CLOSE_PAREN))
12429             inputs = cp_parser_asm_operand_list (parser);
12430
12431             if (inputs == error_mark_node)
12432               invalid_inputs_p = true;
12433         }
12434       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12435         /* The clobbers are coming next.  */
12436         clobbers_p = true;
12437
12438       /* Look for clobbers.  */
12439       if (clobbers_p
12440           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12441         {
12442           /* Consume the `:' or `::'.  */
12443           cp_lexer_consume_token (parser->lexer);
12444           /* Parse the clobbers.  */
12445           if (cp_lexer_next_token_is_not (parser->lexer,
12446                                           CPP_CLOSE_PAREN))
12447             clobbers = cp_parser_asm_clobber_list (parser);
12448         }
12449     }
12450   /* Look for the closing `)'.  */
12451   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12452     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12453                                            /*consume_paren=*/true);
12454   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12455
12456   if (!invalid_inputs_p && !invalid_outputs_p)
12457     {
12458       /* Create the ASM_EXPR.  */
12459       if (parser->in_function_body)
12460         {
12461           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12462                                       inputs, clobbers);
12463           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12464           if (!extended_p)
12465             {
12466               tree temp = asm_stmt;
12467               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12468                 temp = TREE_OPERAND (temp, 0);
12469
12470               ASM_INPUT_P (temp) = 1;
12471             }
12472         }
12473       else
12474         cgraph_add_asm_node (string);
12475     }
12476 }
12477
12478 /* Declarators [gram.dcl.decl] */
12479
12480 /* Parse an init-declarator.
12481
12482    init-declarator:
12483      declarator initializer [opt]
12484
12485    GNU Extension:
12486
12487    init-declarator:
12488      declarator asm-specification [opt] attributes [opt] initializer [opt]
12489
12490    function-definition:
12491      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12492        function-body
12493      decl-specifier-seq [opt] declarator function-try-block
12494
12495    GNU Extension:
12496
12497    function-definition:
12498      __extension__ function-definition
12499
12500    The DECL_SPECIFIERS apply to this declarator.  Returns a
12501    representation of the entity declared.  If MEMBER_P is TRUE, then
12502    this declarator appears in a class scope.  The new DECL created by
12503    this declarator is returned.
12504
12505    The CHECKS are access checks that should be performed once we know
12506    what entity is being declared (and, therefore, what classes have
12507    befriended it).
12508
12509    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12510    for a function-definition here as well.  If the declarator is a
12511    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12512    be TRUE upon return.  By that point, the function-definition will
12513    have been completely parsed.
12514
12515    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12516    is FALSE.  */
12517
12518 static tree
12519 cp_parser_init_declarator (cp_parser* parser,
12520                            cp_decl_specifier_seq *decl_specifiers,
12521                            VEC (deferred_access_check,gc)* checks,
12522                            bool function_definition_allowed_p,
12523                            bool member_p,
12524                            int declares_class_or_enum,
12525                            bool* function_definition_p)
12526 {
12527   cp_token *token = NULL, *asm_spec_start_token = NULL,
12528            *attributes_start_token = NULL;
12529   cp_declarator *declarator;
12530   tree prefix_attributes;
12531   tree attributes;
12532   tree asm_specification;
12533   tree initializer;
12534   tree decl = NULL_TREE;
12535   tree scope;
12536   int is_initialized;
12537   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12538      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12539      "(...)".  */
12540   enum cpp_ttype initialization_kind;
12541   bool is_direct_init = false;
12542   bool is_non_constant_init;
12543   int ctor_dtor_or_conv_p;
12544   bool friend_p;
12545   tree pushed_scope = NULL;
12546
12547   /* Gather the attributes that were provided with the
12548      decl-specifiers.  */
12549   prefix_attributes = decl_specifiers->attributes;
12550
12551   /* Assume that this is not the declarator for a function
12552      definition.  */
12553   if (function_definition_p)
12554     *function_definition_p = false;
12555
12556   /* Defer access checks while parsing the declarator; we cannot know
12557      what names are accessible until we know what is being
12558      declared.  */
12559   resume_deferring_access_checks ();
12560
12561   /* Parse the declarator.  */
12562   token = cp_lexer_peek_token (parser->lexer);
12563   declarator
12564     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12565                             &ctor_dtor_or_conv_p,
12566                             /*parenthesized_p=*/NULL,
12567                             /*member_p=*/false);
12568   /* Gather up the deferred checks.  */
12569   stop_deferring_access_checks ();
12570
12571   /* If the DECLARATOR was erroneous, there's no need to go
12572      further.  */
12573   if (declarator == cp_error_declarator)
12574     return error_mark_node;
12575
12576   /* Check that the number of template-parameter-lists is OK.  */
12577   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12578                                                        token->location))
12579     return error_mark_node;
12580
12581   if (declares_class_or_enum & 2)
12582     cp_parser_check_for_definition_in_return_type (declarator,
12583                                                    decl_specifiers->type,
12584                                                    decl_specifiers->type_location);
12585
12586   /* Figure out what scope the entity declared by the DECLARATOR is
12587      located in.  `grokdeclarator' sometimes changes the scope, so
12588      we compute it now.  */
12589   scope = get_scope_of_declarator (declarator);
12590
12591   /* If we're allowing GNU extensions, look for an asm-specification
12592      and attributes.  */
12593   if (cp_parser_allow_gnu_extensions_p (parser))
12594     {
12595       /* Look for an asm-specification.  */
12596       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12597       asm_specification = cp_parser_asm_specification_opt (parser);
12598       /* And attributes.  */
12599       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12600       attributes = cp_parser_attributes_opt (parser);
12601     }
12602   else
12603     {
12604       asm_specification = NULL_TREE;
12605       attributes = NULL_TREE;
12606     }
12607
12608   /* Peek at the next token.  */
12609   token = cp_lexer_peek_token (parser->lexer);
12610   /* Check to see if the token indicates the start of a
12611      function-definition.  */
12612   if (function_declarator_p (declarator)
12613       && cp_parser_token_starts_function_definition_p (token))
12614     {
12615       if (!function_definition_allowed_p)
12616         {
12617           /* If a function-definition should not appear here, issue an
12618              error message.  */
12619           cp_parser_error (parser,
12620                            "a function-definition is not allowed here");
12621           return error_mark_node;
12622         }
12623       else
12624         {
12625           location_t func_brace_location
12626             = cp_lexer_peek_token (parser->lexer)->location;
12627
12628           /* Neither attributes nor an asm-specification are allowed
12629              on a function-definition.  */
12630           if (asm_specification)
12631             error ("%Han asm-specification is not allowed "
12632                    "on a function-definition",
12633                    &asm_spec_start_token->location);
12634           if (attributes)
12635             error ("%Hattributes are not allowed on a function-definition",
12636                    &attributes_start_token->location);
12637           /* This is a function-definition.  */
12638           *function_definition_p = true;
12639
12640           /* Parse the function definition.  */
12641           if (member_p)
12642             decl = cp_parser_save_member_function_body (parser,
12643                                                         decl_specifiers,
12644                                                         declarator,
12645                                                         prefix_attributes);
12646           else
12647             decl
12648               = (cp_parser_function_definition_from_specifiers_and_declarator
12649                  (parser, decl_specifiers, prefix_attributes, declarator));
12650
12651           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12652             {
12653               /* This is where the prologue starts...  */
12654               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12655                 = func_brace_location;
12656             }
12657
12658           return decl;
12659         }
12660     }
12661
12662   /* [dcl.dcl]
12663
12664      Only in function declarations for constructors, destructors, and
12665      type conversions can the decl-specifier-seq be omitted.
12666
12667      We explicitly postpone this check past the point where we handle
12668      function-definitions because we tolerate function-definitions
12669      that are missing their return types in some modes.  */
12670   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12671     {
12672       cp_parser_error (parser,
12673                        "expected constructor, destructor, or type conversion");
12674       return error_mark_node;
12675     }
12676
12677   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12678   if (token->type == CPP_EQ
12679       || token->type == CPP_OPEN_PAREN
12680       || token->type == CPP_OPEN_BRACE)
12681     {
12682       is_initialized = SD_INITIALIZED;
12683       initialization_kind = token->type;
12684
12685       if (token->type == CPP_EQ
12686           && function_declarator_p (declarator))
12687         {
12688           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12689           if (t2->keyword == RID_DEFAULT)
12690             is_initialized = SD_DEFAULTED;
12691           else if (t2->keyword == RID_DELETE)
12692             is_initialized = SD_DELETED;
12693         }
12694     }
12695   else
12696     {
12697       /* If the init-declarator isn't initialized and isn't followed by a
12698          `,' or `;', it's not a valid init-declarator.  */
12699       if (token->type != CPP_COMMA
12700           && token->type != CPP_SEMICOLON)
12701         {
12702           cp_parser_error (parser, "expected initializer");
12703           return error_mark_node;
12704         }
12705       is_initialized = SD_UNINITIALIZED;
12706       initialization_kind = CPP_EOF;
12707     }
12708
12709   /* Because start_decl has side-effects, we should only call it if we
12710      know we're going ahead.  By this point, we know that we cannot
12711      possibly be looking at any other construct.  */
12712   cp_parser_commit_to_tentative_parse (parser);
12713
12714   /* If the decl specifiers were bad, issue an error now that we're
12715      sure this was intended to be a declarator.  Then continue
12716      declaring the variable(s), as int, to try to cut down on further
12717      errors.  */
12718   if (decl_specifiers->any_specifiers_p
12719       && decl_specifiers->type == error_mark_node)
12720     {
12721       cp_parser_error (parser, "invalid type in declaration");
12722       decl_specifiers->type = integer_type_node;
12723     }
12724
12725   /* Check to see whether or not this declaration is a friend.  */
12726   friend_p = cp_parser_friend_p (decl_specifiers);
12727
12728   /* Enter the newly declared entry in the symbol table.  If we're
12729      processing a declaration in a class-specifier, we wait until
12730      after processing the initializer.  */
12731   if (!member_p)
12732     {
12733       if (parser->in_unbraced_linkage_specification_p)
12734         decl_specifiers->storage_class = sc_extern;
12735       decl = start_decl (declarator, decl_specifiers,
12736                          is_initialized, attributes, prefix_attributes,
12737                          &pushed_scope);
12738     }
12739   else if (scope)
12740     /* Enter the SCOPE.  That way unqualified names appearing in the
12741        initializer will be looked up in SCOPE.  */
12742     pushed_scope = push_scope (scope);
12743
12744   /* Perform deferred access control checks, now that we know in which
12745      SCOPE the declared entity resides.  */
12746   if (!member_p && decl)
12747     {
12748       tree saved_current_function_decl = NULL_TREE;
12749
12750       /* If the entity being declared is a function, pretend that we
12751          are in its scope.  If it is a `friend', it may have access to
12752          things that would not otherwise be accessible.  */
12753       if (TREE_CODE (decl) == FUNCTION_DECL)
12754         {
12755           saved_current_function_decl = current_function_decl;
12756           current_function_decl = decl;
12757         }
12758
12759       /* Perform access checks for template parameters.  */
12760       cp_parser_perform_template_parameter_access_checks (checks);
12761
12762       /* Perform the access control checks for the declarator and the
12763          decl-specifiers.  */
12764       perform_deferred_access_checks ();
12765
12766       /* Restore the saved value.  */
12767       if (TREE_CODE (decl) == FUNCTION_DECL)
12768         current_function_decl = saved_current_function_decl;
12769     }
12770
12771   /* Parse the initializer.  */
12772   initializer = NULL_TREE;
12773   is_direct_init = false;
12774   is_non_constant_init = true;
12775   if (is_initialized)
12776     {
12777       if (function_declarator_p (declarator))
12778         {
12779           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12780            if (initialization_kind == CPP_EQ)
12781              initializer = cp_parser_pure_specifier (parser);
12782            else
12783              {
12784                /* If the declaration was erroneous, we don't really
12785                   know what the user intended, so just silently
12786                   consume the initializer.  */
12787                if (decl != error_mark_node)
12788                  error ("%Hinitializer provided for function",
12789                         &initializer_start_token->location);
12790                cp_parser_skip_to_closing_parenthesis (parser,
12791                                                       /*recovering=*/true,
12792                                                       /*or_comma=*/false,
12793                                                       /*consume_paren=*/true);
12794              }
12795         }
12796       else
12797         initializer = cp_parser_initializer (parser,
12798                                              &is_direct_init,
12799                                              &is_non_constant_init);
12800     }
12801
12802   /* The old parser allows attributes to appear after a parenthesized
12803      initializer.  Mark Mitchell proposed removing this functionality
12804      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12805      attributes -- but ignores them.  */
12806   if (cp_parser_allow_gnu_extensions_p (parser)
12807       && initialization_kind == CPP_OPEN_PAREN)
12808     if (cp_parser_attributes_opt (parser))
12809       warning (OPT_Wattributes,
12810                "attributes after parenthesized initializer ignored");
12811
12812   /* For an in-class declaration, use `grokfield' to create the
12813      declaration.  */
12814   if (member_p)
12815     {
12816       if (pushed_scope)
12817         {
12818           pop_scope (pushed_scope);
12819           pushed_scope = false;
12820         }
12821       decl = grokfield (declarator, decl_specifiers,
12822                         initializer, !is_non_constant_init,
12823                         /*asmspec=*/NULL_TREE,
12824                         prefix_attributes);
12825       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12826         cp_parser_save_default_args (parser, decl);
12827     }
12828
12829   /* Finish processing the declaration.  But, skip friend
12830      declarations.  */
12831   if (!friend_p && decl && decl != error_mark_node)
12832     {
12833       cp_finish_decl (decl,
12834                       initializer, !is_non_constant_init,
12835                       asm_specification,
12836                       /* If the initializer is in parentheses, then this is
12837                          a direct-initialization, which means that an
12838                          `explicit' constructor is OK.  Otherwise, an
12839                          `explicit' constructor cannot be used.  */
12840                       ((is_direct_init || !is_initialized)
12841                        ? 0 : LOOKUP_ONLYCONVERTING));
12842     }
12843   else if ((cxx_dialect != cxx98) && friend_p
12844            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12845     /* Core issue #226 (C++0x only): A default template-argument
12846        shall not be specified in a friend class template
12847        declaration. */
12848     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12849                              /*is_partial=*/0, /*is_friend_decl=*/1);
12850
12851   if (!friend_p && pushed_scope)
12852     pop_scope (pushed_scope);
12853
12854   return decl;
12855 }
12856
12857 /* Parse a declarator.
12858
12859    declarator:
12860      direct-declarator
12861      ptr-operator declarator
12862
12863    abstract-declarator:
12864      ptr-operator abstract-declarator [opt]
12865      direct-abstract-declarator
12866
12867    GNU Extensions:
12868
12869    declarator:
12870      attributes [opt] direct-declarator
12871      attributes [opt] ptr-operator declarator
12872
12873    abstract-declarator:
12874      attributes [opt] ptr-operator abstract-declarator [opt]
12875      attributes [opt] direct-abstract-declarator
12876
12877    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12878    detect constructor, destructor or conversion operators. It is set
12879    to -1 if the declarator is a name, and +1 if it is a
12880    function. Otherwise it is set to zero. Usually you just want to
12881    test for >0, but internally the negative value is used.
12882
12883    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12884    a decl-specifier-seq unless it declares a constructor, destructor,
12885    or conversion.  It might seem that we could check this condition in
12886    semantic analysis, rather than parsing, but that makes it difficult
12887    to handle something like `f()'.  We want to notice that there are
12888    no decl-specifiers, and therefore realize that this is an
12889    expression, not a declaration.)
12890
12891    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12892    the declarator is a direct-declarator of the form "(...)".
12893
12894    MEMBER_P is true iff this declarator is a member-declarator.  */
12895
12896 static cp_declarator *
12897 cp_parser_declarator (cp_parser* parser,
12898                       cp_parser_declarator_kind dcl_kind,
12899                       int* ctor_dtor_or_conv_p,
12900                       bool* parenthesized_p,
12901                       bool member_p)
12902 {
12903   cp_token *token;
12904   cp_declarator *declarator;
12905   enum tree_code code;
12906   cp_cv_quals cv_quals;
12907   tree class_type;
12908   tree attributes = NULL_TREE;
12909
12910   /* Assume this is not a constructor, destructor, or type-conversion
12911      operator.  */
12912   if (ctor_dtor_or_conv_p)
12913     *ctor_dtor_or_conv_p = 0;
12914
12915   if (cp_parser_allow_gnu_extensions_p (parser))
12916     attributes = cp_parser_attributes_opt (parser);
12917
12918   /* Peek at the next token.  */
12919   token = cp_lexer_peek_token (parser->lexer);
12920
12921   /* Check for the ptr-operator production.  */
12922   cp_parser_parse_tentatively (parser);
12923   /* Parse the ptr-operator.  */
12924   code = cp_parser_ptr_operator (parser,
12925                                  &class_type,
12926                                  &cv_quals);
12927   /* If that worked, then we have a ptr-operator.  */
12928   if (cp_parser_parse_definitely (parser))
12929     {
12930       /* If a ptr-operator was found, then this declarator was not
12931          parenthesized.  */
12932       if (parenthesized_p)
12933         *parenthesized_p = true;
12934       /* The dependent declarator is optional if we are parsing an
12935          abstract-declarator.  */
12936       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12937         cp_parser_parse_tentatively (parser);
12938
12939       /* Parse the dependent declarator.  */
12940       declarator = cp_parser_declarator (parser, dcl_kind,
12941                                          /*ctor_dtor_or_conv_p=*/NULL,
12942                                          /*parenthesized_p=*/NULL,
12943                                          /*member_p=*/false);
12944
12945       /* If we are parsing an abstract-declarator, we must handle the
12946          case where the dependent declarator is absent.  */
12947       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12948           && !cp_parser_parse_definitely (parser))
12949         declarator = NULL;
12950
12951       declarator = cp_parser_make_indirect_declarator
12952         (code, class_type, cv_quals, declarator);
12953     }
12954   /* Everything else is a direct-declarator.  */
12955   else
12956     {
12957       if (parenthesized_p)
12958         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12959                                                    CPP_OPEN_PAREN);
12960       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12961                                                 ctor_dtor_or_conv_p,
12962                                                 member_p);
12963     }
12964
12965   if (attributes && declarator && declarator != cp_error_declarator)
12966     declarator->attributes = attributes;
12967
12968   return declarator;
12969 }
12970
12971 /* Parse a direct-declarator or direct-abstract-declarator.
12972
12973    direct-declarator:
12974      declarator-id
12975      direct-declarator ( parameter-declaration-clause )
12976        cv-qualifier-seq [opt]
12977        exception-specification [opt]
12978      direct-declarator [ constant-expression [opt] ]
12979      ( declarator )
12980
12981    direct-abstract-declarator:
12982      direct-abstract-declarator [opt]
12983        ( parameter-declaration-clause )
12984        cv-qualifier-seq [opt]
12985        exception-specification [opt]
12986      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12987      ( abstract-declarator )
12988
12989    Returns a representation of the declarator.  DCL_KIND is
12990    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12991    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12992    we are parsing a direct-declarator.  It is
12993    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12994    of ambiguity we prefer an abstract declarator, as per
12995    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12996    cp_parser_declarator.  */
12997
12998 static cp_declarator *
12999 cp_parser_direct_declarator (cp_parser* parser,
13000                              cp_parser_declarator_kind dcl_kind,
13001                              int* ctor_dtor_or_conv_p,
13002                              bool member_p)
13003 {
13004   cp_token *token;
13005   cp_declarator *declarator = NULL;
13006   tree scope = NULL_TREE;
13007   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13008   bool saved_in_declarator_p = parser->in_declarator_p;
13009   bool first = true;
13010   tree pushed_scope = NULL_TREE;
13011
13012   while (true)
13013     {
13014       /* Peek at the next token.  */
13015       token = cp_lexer_peek_token (parser->lexer);
13016       if (token->type == CPP_OPEN_PAREN)
13017         {
13018           /* This is either a parameter-declaration-clause, or a
13019              parenthesized declarator. When we know we are parsing a
13020              named declarator, it must be a parenthesized declarator
13021              if FIRST is true. For instance, `(int)' is a
13022              parameter-declaration-clause, with an omitted
13023              direct-abstract-declarator. But `((*))', is a
13024              parenthesized abstract declarator. Finally, when T is a
13025              template parameter `(T)' is a
13026              parameter-declaration-clause, and not a parenthesized
13027              named declarator.
13028
13029              We first try and parse a parameter-declaration-clause,
13030              and then try a nested declarator (if FIRST is true).
13031
13032              It is not an error for it not to be a
13033              parameter-declaration-clause, even when FIRST is
13034              false. Consider,
13035
13036                int i (int);
13037                int i (3);
13038
13039              The first is the declaration of a function while the
13040              second is the definition of a variable, including its
13041              initializer.
13042
13043              Having seen only the parenthesis, we cannot know which of
13044              these two alternatives should be selected.  Even more
13045              complex are examples like:
13046
13047                int i (int (a));
13048                int i (int (3));
13049
13050              The former is a function-declaration; the latter is a
13051              variable initialization.
13052
13053              Thus again, we try a parameter-declaration-clause, and if
13054              that fails, we back out and return.  */
13055
13056           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13057             {
13058               tree params;
13059               unsigned saved_num_template_parameter_lists;
13060               bool is_declarator = false;
13061               tree t;
13062
13063               /* In a member-declarator, the only valid interpretation
13064                  of a parenthesis is the start of a
13065                  parameter-declaration-clause.  (It is invalid to
13066                  initialize a static data member with a parenthesized
13067                  initializer; only the "=" form of initialization is
13068                  permitted.)  */
13069               if (!member_p)
13070                 cp_parser_parse_tentatively (parser);
13071
13072               /* Consume the `('.  */
13073               cp_lexer_consume_token (parser->lexer);
13074               if (first)
13075                 {
13076                   /* If this is going to be an abstract declarator, we're
13077                      in a declarator and we can't have default args.  */
13078                   parser->default_arg_ok_p = false;
13079                   parser->in_declarator_p = true;
13080                 }
13081
13082               /* Inside the function parameter list, surrounding
13083                  template-parameter-lists do not apply.  */
13084               saved_num_template_parameter_lists
13085                 = parser->num_template_parameter_lists;
13086               parser->num_template_parameter_lists = 0;
13087
13088               begin_scope (sk_function_parms, NULL_TREE);
13089
13090               /* Parse the parameter-declaration-clause.  */
13091               params = cp_parser_parameter_declaration_clause (parser);
13092
13093               parser->num_template_parameter_lists
13094                 = saved_num_template_parameter_lists;
13095
13096               /* If all went well, parse the cv-qualifier-seq and the
13097                  exception-specification.  */
13098               if (member_p || cp_parser_parse_definitely (parser))
13099                 {
13100                   cp_cv_quals cv_quals;
13101                   tree exception_specification;
13102                   tree late_return;
13103
13104                   is_declarator = true;
13105
13106                   if (ctor_dtor_or_conv_p)
13107                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13108                   first = false;
13109                   /* Consume the `)'.  */
13110                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13111
13112                   /* Parse the cv-qualifier-seq.  */
13113                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13114                   /* And the exception-specification.  */
13115                   exception_specification
13116                     = cp_parser_exception_specification_opt (parser);
13117
13118                   late_return
13119                     = cp_parser_late_return_type_opt (parser);
13120
13121                   /* Create the function-declarator.  */
13122                   declarator = make_call_declarator (declarator,
13123                                                      params,
13124                                                      cv_quals,
13125                                                      exception_specification,
13126                                                      late_return);
13127                   /* Any subsequent parameter lists are to do with
13128                      return type, so are not those of the declared
13129                      function.  */
13130                   parser->default_arg_ok_p = false;
13131                 }
13132
13133               /* Remove the function parms from scope.  */
13134               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13135                 pop_binding (DECL_NAME (t), t);
13136               leave_scope();
13137
13138               if (is_declarator)
13139                 /* Repeat the main loop.  */
13140                 continue;
13141             }
13142
13143           /* If this is the first, we can try a parenthesized
13144              declarator.  */
13145           if (first)
13146             {
13147               bool saved_in_type_id_in_expr_p;
13148
13149               parser->default_arg_ok_p = saved_default_arg_ok_p;
13150               parser->in_declarator_p = saved_in_declarator_p;
13151
13152               /* Consume the `('.  */
13153               cp_lexer_consume_token (parser->lexer);
13154               /* Parse the nested declarator.  */
13155               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13156               parser->in_type_id_in_expr_p = true;
13157               declarator
13158                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13159                                         /*parenthesized_p=*/NULL,
13160                                         member_p);
13161               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13162               first = false;
13163               /* Expect a `)'.  */
13164               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13165                 declarator = cp_error_declarator;
13166               if (declarator == cp_error_declarator)
13167                 break;
13168
13169               goto handle_declarator;
13170             }
13171           /* Otherwise, we must be done.  */
13172           else
13173             break;
13174         }
13175       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13176                && token->type == CPP_OPEN_SQUARE)
13177         {
13178           /* Parse an array-declarator.  */
13179           tree bounds;
13180
13181           if (ctor_dtor_or_conv_p)
13182             *ctor_dtor_or_conv_p = 0;
13183
13184           first = false;
13185           parser->default_arg_ok_p = false;
13186           parser->in_declarator_p = true;
13187           /* Consume the `['.  */
13188           cp_lexer_consume_token (parser->lexer);
13189           /* Peek at the next token.  */
13190           token = cp_lexer_peek_token (parser->lexer);
13191           /* If the next token is `]', then there is no
13192              constant-expression.  */
13193           if (token->type != CPP_CLOSE_SQUARE)
13194             {
13195               bool non_constant_p;
13196
13197               bounds
13198                 = cp_parser_constant_expression (parser,
13199                                                  /*allow_non_constant=*/true,
13200                                                  &non_constant_p);
13201               if (!non_constant_p)
13202                 bounds = fold_non_dependent_expr (bounds);
13203               /* Normally, the array bound must be an integral constant
13204                  expression.  However, as an extension, we allow VLAs
13205                  in function scopes.  */
13206               else if (!parser->in_function_body)
13207                 {
13208                   error ("%Harray bound is not an integer constant",
13209                          &token->location);
13210                   bounds = error_mark_node;
13211                 }
13212             }
13213           else
13214             bounds = NULL_TREE;
13215           /* Look for the closing `]'.  */
13216           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13217             {
13218               declarator = cp_error_declarator;
13219               break;
13220             }
13221
13222           declarator = make_array_declarator (declarator, bounds);
13223         }
13224       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13225         {
13226           tree qualifying_scope;
13227           tree unqualified_name;
13228           special_function_kind sfk;
13229           bool abstract_ok;
13230           bool pack_expansion_p = false;
13231           cp_token *declarator_id_start_token;
13232
13233           /* Parse a declarator-id */
13234           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13235           if (abstract_ok)
13236             {
13237               cp_parser_parse_tentatively (parser);
13238
13239               /* If we see an ellipsis, we should be looking at a
13240                  parameter pack. */
13241               if (token->type == CPP_ELLIPSIS)
13242                 {
13243                   /* Consume the `...' */
13244                   cp_lexer_consume_token (parser->lexer);
13245
13246                   pack_expansion_p = true;
13247                 }
13248             }
13249
13250           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13251           unqualified_name
13252             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13253           qualifying_scope = parser->scope;
13254           if (abstract_ok)
13255             {
13256               bool okay = false;
13257
13258               if (!unqualified_name && pack_expansion_p)
13259                 {
13260                   /* Check whether an error occurred. */
13261                   okay = !cp_parser_error_occurred (parser);
13262
13263                   /* We already consumed the ellipsis to mark a
13264                      parameter pack, but we have no way to report it,
13265                      so abort the tentative parse. We will be exiting
13266                      immediately anyway. */
13267                   cp_parser_abort_tentative_parse (parser);
13268                 }
13269               else
13270                 okay = cp_parser_parse_definitely (parser);
13271
13272               if (!okay)
13273                 unqualified_name = error_mark_node;
13274               else if (unqualified_name
13275                        && (qualifying_scope
13276                            || (TREE_CODE (unqualified_name)
13277                                != IDENTIFIER_NODE)))
13278                 {
13279                   cp_parser_error (parser, "expected unqualified-id");
13280                   unqualified_name = error_mark_node;
13281                 }
13282             }
13283
13284           if (!unqualified_name)
13285             return NULL;
13286           if (unqualified_name == error_mark_node)
13287             {
13288               declarator = cp_error_declarator;
13289               pack_expansion_p = false;
13290               declarator->parameter_pack_p = false;
13291               break;
13292             }
13293
13294           if (qualifying_scope && at_namespace_scope_p ()
13295               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13296             {
13297               /* In the declaration of a member of a template class
13298                  outside of the class itself, the SCOPE will sometimes
13299                  be a TYPENAME_TYPE.  For example, given:
13300
13301                  template <typename T>
13302                  int S<T>::R::i = 3;
13303
13304                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13305                  this context, we must resolve S<T>::R to an ordinary
13306                  type, rather than a typename type.
13307
13308                  The reason we normally avoid resolving TYPENAME_TYPEs
13309                  is that a specialization of `S' might render
13310                  `S<T>::R' not a type.  However, if `S' is
13311                  specialized, then this `i' will not be used, so there
13312                  is no harm in resolving the types here.  */
13313               tree type;
13314
13315               /* Resolve the TYPENAME_TYPE.  */
13316               type = resolve_typename_type (qualifying_scope,
13317                                             /*only_current_p=*/false);
13318               /* If that failed, the declarator is invalid.  */
13319               if (TREE_CODE (type) == TYPENAME_TYPE)
13320                 error ("%H%<%T::%E%> is not a type",
13321                        &declarator_id_start_token->location,
13322                        TYPE_CONTEXT (qualifying_scope),
13323                        TYPE_IDENTIFIER (qualifying_scope));
13324               qualifying_scope = type;
13325             }
13326
13327           sfk = sfk_none;
13328
13329           if (unqualified_name)
13330             {
13331               tree class_type;
13332
13333               if (qualifying_scope
13334                   && CLASS_TYPE_P (qualifying_scope))
13335                 class_type = qualifying_scope;
13336               else
13337                 class_type = current_class_type;
13338
13339               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13340                 {
13341                   tree name_type = TREE_TYPE (unqualified_name);
13342                   if (class_type && same_type_p (name_type, class_type))
13343                     {
13344                       if (qualifying_scope
13345                           && CLASSTYPE_USE_TEMPLATE (name_type))
13346                         {
13347                           error ("%Hinvalid use of constructor as a template",
13348                                  &declarator_id_start_token->location);
13349                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13350                                   "name the constructor in a qualified name",
13351                                   class_type,
13352                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13353                                   class_type, name_type);
13354                           declarator = cp_error_declarator;
13355                           break;
13356                         }
13357                       else
13358                         unqualified_name = constructor_name (class_type);
13359                     }
13360                   else
13361                     {
13362                       /* We do not attempt to print the declarator
13363                          here because we do not have enough
13364                          information about its original syntactic
13365                          form.  */
13366                       cp_parser_error (parser, "invalid declarator");
13367                       declarator = cp_error_declarator;
13368                       break;
13369                     }
13370                 }
13371
13372               if (class_type)
13373                 {
13374                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13375                     sfk = sfk_destructor;
13376                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13377                     sfk = sfk_conversion;
13378                   else if (/* There's no way to declare a constructor
13379                               for an anonymous type, even if the type
13380                               got a name for linkage purposes.  */
13381                            !TYPE_WAS_ANONYMOUS (class_type)
13382                            && constructor_name_p (unqualified_name,
13383                                                   class_type))
13384                     {
13385                       unqualified_name = constructor_name (class_type);
13386                       sfk = sfk_constructor;
13387                     }
13388
13389                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13390                     *ctor_dtor_or_conv_p = -1;
13391                 }
13392             }
13393           declarator = make_id_declarator (qualifying_scope,
13394                                            unqualified_name,
13395                                            sfk);
13396           declarator->id_loc = token->location;
13397           declarator->parameter_pack_p = pack_expansion_p;
13398
13399           if (pack_expansion_p)
13400             maybe_warn_variadic_templates ();
13401
13402         handle_declarator:;
13403           scope = get_scope_of_declarator (declarator);
13404           if (scope)
13405             /* Any names that appear after the declarator-id for a
13406                member are looked up in the containing scope.  */
13407             pushed_scope = push_scope (scope);
13408           parser->in_declarator_p = true;
13409           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13410               || (declarator && declarator->kind == cdk_id))
13411             /* Default args are only allowed on function
13412                declarations.  */
13413             parser->default_arg_ok_p = saved_default_arg_ok_p;
13414           else
13415             parser->default_arg_ok_p = false;
13416
13417           first = false;
13418         }
13419       /* We're done.  */
13420       else
13421         break;
13422     }
13423
13424   /* For an abstract declarator, we might wind up with nothing at this
13425      point.  That's an error; the declarator is not optional.  */
13426   if (!declarator)
13427     cp_parser_error (parser, "expected declarator");
13428
13429   /* If we entered a scope, we must exit it now.  */
13430   if (pushed_scope)
13431     pop_scope (pushed_scope);
13432
13433   parser->default_arg_ok_p = saved_default_arg_ok_p;
13434   parser->in_declarator_p = saved_in_declarator_p;
13435
13436   return declarator;
13437 }
13438
13439 /* Parse a ptr-operator.
13440
13441    ptr-operator:
13442      * cv-qualifier-seq [opt]
13443      &
13444      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13445
13446    GNU Extension:
13447
13448    ptr-operator:
13449      & cv-qualifier-seq [opt]
13450
13451    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13452    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13453    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13454    filled in with the TYPE containing the member.  *CV_QUALS is
13455    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13456    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13457    Note that the tree codes returned by this function have nothing
13458    to do with the types of trees that will be eventually be created
13459    to represent the pointer or reference type being parsed. They are
13460    just constants with suggestive names. */
13461 static enum tree_code
13462 cp_parser_ptr_operator (cp_parser* parser,
13463                         tree* type,
13464                         cp_cv_quals *cv_quals)
13465 {
13466   enum tree_code code = ERROR_MARK;
13467   cp_token *token;
13468
13469   /* Assume that it's not a pointer-to-member.  */
13470   *type = NULL_TREE;
13471   /* And that there are no cv-qualifiers.  */
13472   *cv_quals = TYPE_UNQUALIFIED;
13473
13474   /* Peek at the next token.  */
13475   token = cp_lexer_peek_token (parser->lexer);
13476
13477   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13478   if (token->type == CPP_MULT)
13479     code = INDIRECT_REF;
13480   else if (token->type == CPP_AND)
13481     code = ADDR_EXPR;
13482   else if ((cxx_dialect != cxx98) &&
13483            token->type == CPP_AND_AND) /* C++0x only */
13484     code = NON_LVALUE_EXPR;
13485
13486   if (code != ERROR_MARK)
13487     {
13488       /* Consume the `*', `&' or `&&'.  */
13489       cp_lexer_consume_token (parser->lexer);
13490
13491       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13492          `&', if we are allowing GNU extensions.  (The only qualifier
13493          that can legally appear after `&' is `restrict', but that is
13494          enforced during semantic analysis.  */
13495       if (code == INDIRECT_REF
13496           || cp_parser_allow_gnu_extensions_p (parser))
13497         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13498     }
13499   else
13500     {
13501       /* Try the pointer-to-member case.  */
13502       cp_parser_parse_tentatively (parser);
13503       /* Look for the optional `::' operator.  */
13504       cp_parser_global_scope_opt (parser,
13505                                   /*current_scope_valid_p=*/false);
13506       /* Look for the nested-name specifier.  */
13507       token = cp_lexer_peek_token (parser->lexer);
13508       cp_parser_nested_name_specifier (parser,
13509                                        /*typename_keyword_p=*/false,
13510                                        /*check_dependency_p=*/true,
13511                                        /*type_p=*/false,
13512                                        /*is_declaration=*/false);
13513       /* If we found it, and the next token is a `*', then we are
13514          indeed looking at a pointer-to-member operator.  */
13515       if (!cp_parser_error_occurred (parser)
13516           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13517         {
13518           /* Indicate that the `*' operator was used.  */
13519           code = INDIRECT_REF;
13520
13521           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13522             error ("%H%qD is a namespace", &token->location, parser->scope);
13523           else
13524             {
13525               /* The type of which the member is a member is given by the
13526                  current SCOPE.  */
13527               *type = parser->scope;
13528               /* The next name will not be qualified.  */
13529               parser->scope = NULL_TREE;
13530               parser->qualifying_scope = NULL_TREE;
13531               parser->object_scope = NULL_TREE;
13532               /* Look for the optional cv-qualifier-seq.  */
13533               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13534             }
13535         }
13536       /* If that didn't work we don't have a ptr-operator.  */
13537       if (!cp_parser_parse_definitely (parser))
13538         cp_parser_error (parser, "expected ptr-operator");
13539     }
13540
13541   return code;
13542 }
13543
13544 /* Parse an (optional) cv-qualifier-seq.
13545
13546    cv-qualifier-seq:
13547      cv-qualifier cv-qualifier-seq [opt]
13548
13549    cv-qualifier:
13550      const
13551      volatile
13552
13553    GNU Extension:
13554
13555    cv-qualifier:
13556      __restrict__
13557
13558    Returns a bitmask representing the cv-qualifiers.  */
13559
13560 static cp_cv_quals
13561 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13562 {
13563   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13564
13565   while (true)
13566     {
13567       cp_token *token;
13568       cp_cv_quals cv_qualifier;
13569
13570       /* Peek at the next token.  */
13571       token = cp_lexer_peek_token (parser->lexer);
13572       /* See if it's a cv-qualifier.  */
13573       switch (token->keyword)
13574         {
13575         case RID_CONST:
13576           cv_qualifier = TYPE_QUAL_CONST;
13577           break;
13578
13579         case RID_VOLATILE:
13580           cv_qualifier = TYPE_QUAL_VOLATILE;
13581           break;
13582
13583         case RID_RESTRICT:
13584           cv_qualifier = TYPE_QUAL_RESTRICT;
13585           break;
13586
13587         default:
13588           cv_qualifier = TYPE_UNQUALIFIED;
13589           break;
13590         }
13591
13592       if (!cv_qualifier)
13593         break;
13594
13595       if (cv_quals & cv_qualifier)
13596         {
13597           error ("%Hduplicate cv-qualifier", &token->location);
13598           cp_lexer_purge_token (parser->lexer);
13599         }
13600       else
13601         {
13602           cp_lexer_consume_token (parser->lexer);
13603           cv_quals |= cv_qualifier;
13604         }
13605     }
13606
13607   return cv_quals;
13608 }
13609
13610 /* Parse a late-specified return type, if any.  This is not a separate
13611    non-terminal, but part of a function declarator, which looks like
13612
13613    -> type-id
13614
13615    Returns the type indicated by the type-id.  */
13616
13617 static tree
13618 cp_parser_late_return_type_opt (cp_parser* parser)
13619 {
13620   cp_token *token;
13621
13622   /* Peek at the next token.  */
13623   token = cp_lexer_peek_token (parser->lexer);
13624   /* A late-specified return type is indicated by an initial '->'. */
13625   if (token->type != CPP_DEREF)
13626     return NULL_TREE;
13627
13628   /* Consume the ->.  */
13629   cp_lexer_consume_token (parser->lexer);
13630
13631   return cp_parser_type_id (parser);
13632 }
13633
13634 /* Parse a declarator-id.
13635
13636    declarator-id:
13637      id-expression
13638      :: [opt] nested-name-specifier [opt] type-name
13639
13640    In the `id-expression' case, the value returned is as for
13641    cp_parser_id_expression if the id-expression was an unqualified-id.
13642    If the id-expression was a qualified-id, then a SCOPE_REF is
13643    returned.  The first operand is the scope (either a NAMESPACE_DECL
13644    or TREE_TYPE), but the second is still just a representation of an
13645    unqualified-id.  */
13646
13647 static tree
13648 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13649 {
13650   tree id;
13651   /* The expression must be an id-expression.  Assume that qualified
13652      names are the names of types so that:
13653
13654        template <class T>
13655        int S<T>::R::i = 3;
13656
13657      will work; we must treat `S<T>::R' as the name of a type.
13658      Similarly, assume that qualified names are templates, where
13659      required, so that:
13660
13661        template <class T>
13662        int S<T>::R<T>::i = 3;
13663
13664      will work, too.  */
13665   id = cp_parser_id_expression (parser,
13666                                 /*template_keyword_p=*/false,
13667                                 /*check_dependency_p=*/false,
13668                                 /*template_p=*/NULL,
13669                                 /*declarator_p=*/true,
13670                                 optional_p);
13671   if (id && BASELINK_P (id))
13672     id = BASELINK_FUNCTIONS (id);
13673   return id;
13674 }
13675
13676 /* Parse a type-id.
13677
13678    type-id:
13679      type-specifier-seq abstract-declarator [opt]
13680
13681    Returns the TYPE specified.  */
13682
13683 static tree
13684 cp_parser_type_id (cp_parser* parser)
13685 {
13686   cp_decl_specifier_seq type_specifier_seq;
13687   cp_declarator *abstract_declarator;
13688
13689   /* Parse the type-specifier-seq.  */
13690   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13691                                 &type_specifier_seq);
13692   if (type_specifier_seq.type == error_mark_node)
13693     return error_mark_node;
13694
13695   /* There might or might not be an abstract declarator.  */
13696   cp_parser_parse_tentatively (parser);
13697   /* Look for the declarator.  */
13698   abstract_declarator
13699     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13700                             /*parenthesized_p=*/NULL,
13701                             /*member_p=*/false);
13702   /* Check to see if there really was a declarator.  */
13703   if (!cp_parser_parse_definitely (parser))
13704     abstract_declarator = NULL;
13705
13706   if (type_specifier_seq.type
13707       && type_uses_auto (type_specifier_seq.type))
13708     {
13709       error ("invalid use of %<auto%>");
13710       return error_mark_node;
13711     }
13712   
13713   return groktypename (&type_specifier_seq, abstract_declarator);
13714 }
13715
13716 /* Parse a type-specifier-seq.
13717
13718    type-specifier-seq:
13719      type-specifier type-specifier-seq [opt]
13720
13721    GNU extension:
13722
13723    type-specifier-seq:
13724      attributes type-specifier-seq [opt]
13725
13726    If IS_CONDITION is true, we are at the start of a "condition",
13727    e.g., we've just seen "if (".
13728
13729    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13730
13731 static void
13732 cp_parser_type_specifier_seq (cp_parser* parser,
13733                               bool is_condition,
13734                               cp_decl_specifier_seq *type_specifier_seq)
13735 {
13736   bool seen_type_specifier = false;
13737   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13738   cp_token *start_token = NULL;
13739
13740   /* Clear the TYPE_SPECIFIER_SEQ.  */
13741   clear_decl_specs (type_specifier_seq);
13742
13743   /* Parse the type-specifiers and attributes.  */
13744   while (true)
13745     {
13746       tree type_specifier;
13747       bool is_cv_qualifier;
13748
13749       /* Check for attributes first.  */
13750       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13751         {
13752           type_specifier_seq->attributes =
13753             chainon (type_specifier_seq->attributes,
13754                      cp_parser_attributes_opt (parser));
13755           continue;
13756         }
13757
13758       /* record the token of the beginning of the type specifier seq,
13759          for error reporting purposes*/
13760      if (!start_token)
13761        start_token = cp_lexer_peek_token (parser->lexer);
13762
13763       /* Look for the type-specifier.  */
13764       type_specifier = cp_parser_type_specifier (parser,
13765                                                  flags,
13766                                                  type_specifier_seq,
13767                                                  /*is_declaration=*/false,
13768                                                  NULL,
13769                                                  &is_cv_qualifier);
13770       if (!type_specifier)
13771         {
13772           /* If the first type-specifier could not be found, this is not a
13773              type-specifier-seq at all.  */
13774           if (!seen_type_specifier)
13775             {
13776               cp_parser_error (parser, "expected type-specifier");
13777               type_specifier_seq->type = error_mark_node;
13778               return;
13779             }
13780           /* If subsequent type-specifiers could not be found, the
13781              type-specifier-seq is complete.  */
13782           break;
13783         }
13784
13785       seen_type_specifier = true;
13786       /* The standard says that a condition can be:
13787
13788             type-specifier-seq declarator = assignment-expression
13789
13790          However, given:
13791
13792            struct S {};
13793            if (int S = ...)
13794
13795          we should treat the "S" as a declarator, not as a
13796          type-specifier.  The standard doesn't say that explicitly for
13797          type-specifier-seq, but it does say that for
13798          decl-specifier-seq in an ordinary declaration.  Perhaps it
13799          would be clearer just to allow a decl-specifier-seq here, and
13800          then add a semantic restriction that if any decl-specifiers
13801          that are not type-specifiers appear, the program is invalid.  */
13802       if (is_condition && !is_cv_qualifier)
13803         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13804     }
13805
13806   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13807 }
13808
13809 /* Parse a parameter-declaration-clause.
13810
13811    parameter-declaration-clause:
13812      parameter-declaration-list [opt] ... [opt]
13813      parameter-declaration-list , ...
13814
13815    Returns a representation for the parameter declarations.  A return
13816    value of NULL indicates a parameter-declaration-clause consisting
13817    only of an ellipsis.  */
13818
13819 static tree
13820 cp_parser_parameter_declaration_clause (cp_parser* parser)
13821 {
13822   tree parameters;
13823   cp_token *token;
13824   bool ellipsis_p;
13825   bool is_error;
13826
13827   /* Peek at the next token.  */
13828   token = cp_lexer_peek_token (parser->lexer);
13829   /* Check for trivial parameter-declaration-clauses.  */
13830   if (token->type == CPP_ELLIPSIS)
13831     {
13832       /* Consume the `...' token.  */
13833       cp_lexer_consume_token (parser->lexer);
13834       return NULL_TREE;
13835     }
13836   else if (token->type == CPP_CLOSE_PAREN)
13837     /* There are no parameters.  */
13838     {
13839 #ifndef NO_IMPLICIT_EXTERN_C
13840       if (in_system_header && current_class_type == NULL
13841           && current_lang_name == lang_name_c)
13842         return NULL_TREE;
13843       else
13844 #endif
13845         return void_list_node;
13846     }
13847   /* Check for `(void)', too, which is a special case.  */
13848   else if (token->keyword == RID_VOID
13849            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13850                == CPP_CLOSE_PAREN))
13851     {
13852       /* Consume the `void' token.  */
13853       cp_lexer_consume_token (parser->lexer);
13854       /* There are no parameters.  */
13855       return void_list_node;
13856     }
13857
13858   /* Parse the parameter-declaration-list.  */
13859   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13860   /* If a parse error occurred while parsing the
13861      parameter-declaration-list, then the entire
13862      parameter-declaration-clause is erroneous.  */
13863   if (is_error)
13864     return NULL;
13865
13866   /* Peek at the next token.  */
13867   token = cp_lexer_peek_token (parser->lexer);
13868   /* If it's a `,', the clause should terminate with an ellipsis.  */
13869   if (token->type == CPP_COMMA)
13870     {
13871       /* Consume the `,'.  */
13872       cp_lexer_consume_token (parser->lexer);
13873       /* Expect an ellipsis.  */
13874       ellipsis_p
13875         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13876     }
13877   /* It might also be `...' if the optional trailing `,' was
13878      omitted.  */
13879   else if (token->type == CPP_ELLIPSIS)
13880     {
13881       /* Consume the `...' token.  */
13882       cp_lexer_consume_token (parser->lexer);
13883       /* And remember that we saw it.  */
13884       ellipsis_p = true;
13885     }
13886   else
13887     ellipsis_p = false;
13888
13889   /* Finish the parameter list.  */
13890   if (!ellipsis_p)
13891     parameters = chainon (parameters, void_list_node);
13892
13893   return parameters;
13894 }
13895
13896 /* Parse a parameter-declaration-list.
13897
13898    parameter-declaration-list:
13899      parameter-declaration
13900      parameter-declaration-list , parameter-declaration
13901
13902    Returns a representation of the parameter-declaration-list, as for
13903    cp_parser_parameter_declaration_clause.  However, the
13904    `void_list_node' is never appended to the list.  Upon return,
13905    *IS_ERROR will be true iff an error occurred.  */
13906
13907 static tree
13908 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13909 {
13910   tree parameters = NULL_TREE;
13911   tree *tail = &parameters; 
13912   bool saved_in_unbraced_linkage_specification_p;
13913
13914   /* Assume all will go well.  */
13915   *is_error = false;
13916   /* The special considerations that apply to a function within an
13917      unbraced linkage specifications do not apply to the parameters
13918      to the function.  */
13919   saved_in_unbraced_linkage_specification_p 
13920     = parser->in_unbraced_linkage_specification_p;
13921   parser->in_unbraced_linkage_specification_p = false;
13922
13923   /* Look for more parameters.  */
13924   while (true)
13925     {
13926       cp_parameter_declarator *parameter;
13927       tree decl = error_mark_node;
13928       bool parenthesized_p;
13929       /* Parse the parameter.  */
13930       parameter
13931         = cp_parser_parameter_declaration (parser,
13932                                            /*template_parm_p=*/false,
13933                                            &parenthesized_p);
13934
13935       /* We don't know yet if the enclosing context is deprecated, so wait
13936          and warn in grokparms if appropriate.  */
13937       deprecated_state = DEPRECATED_SUPPRESS;
13938
13939       if (parameter)
13940         decl = grokdeclarator (parameter->declarator,
13941                                &parameter->decl_specifiers,
13942                                PARM,
13943                                parameter->default_argument != NULL_TREE,
13944                                &parameter->decl_specifiers.attributes);
13945
13946       deprecated_state = DEPRECATED_NORMAL;
13947
13948       /* If a parse error occurred parsing the parameter declaration,
13949          then the entire parameter-declaration-list is erroneous.  */
13950       if (decl == error_mark_node)
13951         {
13952           *is_error = true;
13953           parameters = error_mark_node;
13954           break;
13955         }
13956
13957       if (parameter->decl_specifiers.attributes)
13958         cplus_decl_attributes (&decl,
13959                                parameter->decl_specifiers.attributes,
13960                                0);
13961       if (DECL_NAME (decl))
13962         decl = pushdecl (decl);
13963
13964       /* Add the new parameter to the list.  */
13965       *tail = build_tree_list (parameter->default_argument, decl);
13966       tail = &TREE_CHAIN (*tail);
13967
13968       /* Peek at the next token.  */
13969       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13970           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13971           /* These are for Objective-C++ */
13972           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13973           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13974         /* The parameter-declaration-list is complete.  */
13975         break;
13976       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13977         {
13978           cp_token *token;
13979
13980           /* Peek at the next token.  */
13981           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13982           /* If it's an ellipsis, then the list is complete.  */
13983           if (token->type == CPP_ELLIPSIS)
13984             break;
13985           /* Otherwise, there must be more parameters.  Consume the
13986              `,'.  */
13987           cp_lexer_consume_token (parser->lexer);
13988           /* When parsing something like:
13989
13990                 int i(float f, double d)
13991
13992              we can tell after seeing the declaration for "f" that we
13993              are not looking at an initialization of a variable "i",
13994              but rather at the declaration of a function "i".
13995
13996              Due to the fact that the parsing of template arguments
13997              (as specified to a template-id) requires backtracking we
13998              cannot use this technique when inside a template argument
13999              list.  */
14000           if (!parser->in_template_argument_list_p
14001               && !parser->in_type_id_in_expr_p
14002               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14003               /* However, a parameter-declaration of the form
14004                  "foat(f)" (which is a valid declaration of a
14005                  parameter "f") can also be interpreted as an
14006                  expression (the conversion of "f" to "float").  */
14007               && !parenthesized_p)
14008             cp_parser_commit_to_tentative_parse (parser);
14009         }
14010       else
14011         {
14012           cp_parser_error (parser, "expected %<,%> or %<...%>");
14013           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14014             cp_parser_skip_to_closing_parenthesis (parser,
14015                                                    /*recovering=*/true,
14016                                                    /*or_comma=*/false,
14017                                                    /*consume_paren=*/false);
14018           break;
14019         }
14020     }
14021
14022   parser->in_unbraced_linkage_specification_p
14023     = saved_in_unbraced_linkage_specification_p;
14024
14025   return parameters;
14026 }
14027
14028 /* Parse a parameter declaration.
14029
14030    parameter-declaration:
14031      decl-specifier-seq ... [opt] declarator
14032      decl-specifier-seq declarator = assignment-expression
14033      decl-specifier-seq ... [opt] abstract-declarator [opt]
14034      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14035
14036    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14037    declares a template parameter.  (In that case, a non-nested `>'
14038    token encountered during the parsing of the assignment-expression
14039    is not interpreted as a greater-than operator.)
14040
14041    Returns a representation of the parameter, or NULL if an error
14042    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14043    true iff the declarator is of the form "(p)".  */
14044
14045 static cp_parameter_declarator *
14046 cp_parser_parameter_declaration (cp_parser *parser,
14047                                  bool template_parm_p,
14048                                  bool *parenthesized_p)
14049 {
14050   int declares_class_or_enum;
14051   bool greater_than_is_operator_p;
14052   cp_decl_specifier_seq decl_specifiers;
14053   cp_declarator *declarator;
14054   tree default_argument;
14055   cp_token *token = NULL, *declarator_token_start = NULL;
14056   const char *saved_message;
14057
14058   /* In a template parameter, `>' is not an operator.
14059
14060      [temp.param]
14061
14062      When parsing a default template-argument for a non-type
14063      template-parameter, the first non-nested `>' is taken as the end
14064      of the template parameter-list rather than a greater-than
14065      operator.  */
14066   greater_than_is_operator_p = !template_parm_p;
14067
14068   /* Type definitions may not appear in parameter types.  */
14069   saved_message = parser->type_definition_forbidden_message;
14070   parser->type_definition_forbidden_message
14071     = "types may not be defined in parameter types";
14072
14073   /* Parse the declaration-specifiers.  */
14074   cp_parser_decl_specifier_seq (parser,
14075                                 CP_PARSER_FLAGS_NONE,
14076                                 &decl_specifiers,
14077                                 &declares_class_or_enum);
14078   /* If an error occurred, there's no reason to attempt to parse the
14079      rest of the declaration.  */
14080   if (cp_parser_error_occurred (parser))
14081     {
14082       parser->type_definition_forbidden_message = saved_message;
14083       return NULL;
14084     }
14085
14086   /* Peek at the next token.  */
14087   token = cp_lexer_peek_token (parser->lexer);
14088
14089   /* If the next token is a `)', `,', `=', `>', or `...', then there
14090      is no declarator. However, when variadic templates are enabled,
14091      there may be a declarator following `...'.  */
14092   if (token->type == CPP_CLOSE_PAREN
14093       || token->type == CPP_COMMA
14094       || token->type == CPP_EQ
14095       || token->type == CPP_GREATER)
14096     {
14097       declarator = NULL;
14098       if (parenthesized_p)
14099         *parenthesized_p = false;
14100     }
14101   /* Otherwise, there should be a declarator.  */
14102   else
14103     {
14104       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14105       parser->default_arg_ok_p = false;
14106
14107       /* After seeing a decl-specifier-seq, if the next token is not a
14108          "(", there is no possibility that the code is a valid
14109          expression.  Therefore, if parsing tentatively, we commit at
14110          this point.  */
14111       if (!parser->in_template_argument_list_p
14112           /* In an expression context, having seen:
14113
14114                (int((char ...
14115
14116              we cannot be sure whether we are looking at a
14117              function-type (taking a "char" as a parameter) or a cast
14118              of some object of type "char" to "int".  */
14119           && !parser->in_type_id_in_expr_p
14120           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14121           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14122         cp_parser_commit_to_tentative_parse (parser);
14123       /* Parse the declarator.  */
14124       declarator_token_start = token;
14125       declarator = cp_parser_declarator (parser,
14126                                          CP_PARSER_DECLARATOR_EITHER,
14127                                          /*ctor_dtor_or_conv_p=*/NULL,
14128                                          parenthesized_p,
14129                                          /*member_p=*/false);
14130       parser->default_arg_ok_p = saved_default_arg_ok_p;
14131       /* After the declarator, allow more attributes.  */
14132       decl_specifiers.attributes
14133         = chainon (decl_specifiers.attributes,
14134                    cp_parser_attributes_opt (parser));
14135     }
14136
14137   /* If the next token is an ellipsis, and we have not seen a
14138      declarator name, and the type of the declarator contains parameter
14139      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14140      a parameter pack expansion expression. Otherwise, leave the
14141      ellipsis for a C-style variadic function. */
14142   token = cp_lexer_peek_token (parser->lexer);
14143   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14144     {
14145       tree type = decl_specifiers.type;
14146
14147       if (type && DECL_P (type))
14148         type = TREE_TYPE (type);
14149
14150       if (type
14151           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14152           && declarator_can_be_parameter_pack (declarator)
14153           && (!declarator || !declarator->parameter_pack_p)
14154           && uses_parameter_packs (type))
14155         {
14156           /* Consume the `...'. */
14157           cp_lexer_consume_token (parser->lexer);
14158           maybe_warn_variadic_templates ();
14159           
14160           /* Build a pack expansion type */
14161           if (declarator)
14162             declarator->parameter_pack_p = true;
14163           else
14164             decl_specifiers.type = make_pack_expansion (type);
14165         }
14166     }
14167
14168   /* The restriction on defining new types applies only to the type
14169      of the parameter, not to the default argument.  */
14170   parser->type_definition_forbidden_message = saved_message;
14171
14172   /* If the next token is `=', then process a default argument.  */
14173   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14174     {
14175       /* Consume the `='.  */
14176       cp_lexer_consume_token (parser->lexer);
14177
14178       /* If we are defining a class, then the tokens that make up the
14179          default argument must be saved and processed later.  */
14180       if (!template_parm_p && at_class_scope_p ()
14181           && TYPE_BEING_DEFINED (current_class_type))
14182         {
14183           unsigned depth = 0;
14184           int maybe_template_id = 0;
14185           cp_token *first_token;
14186           cp_token *token;
14187
14188           /* Add tokens until we have processed the entire default
14189              argument.  We add the range [first_token, token).  */
14190           first_token = cp_lexer_peek_token (parser->lexer);
14191           while (true)
14192             {
14193               bool done = false;
14194
14195               /* Peek at the next token.  */
14196               token = cp_lexer_peek_token (parser->lexer);
14197               /* What we do depends on what token we have.  */
14198               switch (token->type)
14199                 {
14200                   /* In valid code, a default argument must be
14201                      immediately followed by a `,' `)', or `...'.  */
14202                 case CPP_COMMA:
14203                   if (depth == 0 && maybe_template_id)
14204                     {
14205                       /* If we've seen a '<', we might be in a
14206                          template-argument-list.  Until Core issue 325 is
14207                          resolved, we don't know how this situation ought
14208                          to be handled, so try to DTRT.  We check whether
14209                          what comes after the comma is a valid parameter
14210                          declaration list.  If it is, then the comma ends
14211                          the default argument; otherwise the default
14212                          argument continues.  */
14213                       bool error = false;
14214
14215                       /* Set ITALP so cp_parser_parameter_declaration_list
14216                          doesn't decide to commit to this parse.  */
14217                       bool saved_italp = parser->in_template_argument_list_p;
14218                       parser->in_template_argument_list_p = true;
14219
14220                       cp_parser_parse_tentatively (parser);
14221                       cp_lexer_consume_token (parser->lexer);
14222                       cp_parser_parameter_declaration_list (parser, &error);
14223                       if (!cp_parser_error_occurred (parser) && !error)
14224                         done = true;
14225                       cp_parser_abort_tentative_parse (parser);
14226
14227                       parser->in_template_argument_list_p = saved_italp;
14228                       break;
14229                     }
14230                 case CPP_CLOSE_PAREN:
14231                 case CPP_ELLIPSIS:
14232                   /* If we run into a non-nested `;', `}', or `]',
14233                      then the code is invalid -- but the default
14234                      argument is certainly over.  */
14235                 case CPP_SEMICOLON:
14236                 case CPP_CLOSE_BRACE:
14237                 case CPP_CLOSE_SQUARE:
14238                   if (depth == 0)
14239                     done = true;
14240                   /* Update DEPTH, if necessary.  */
14241                   else if (token->type == CPP_CLOSE_PAREN
14242                            || token->type == CPP_CLOSE_BRACE
14243                            || token->type == CPP_CLOSE_SQUARE)
14244                     --depth;
14245                   break;
14246
14247                 case CPP_OPEN_PAREN:
14248                 case CPP_OPEN_SQUARE:
14249                 case CPP_OPEN_BRACE:
14250                   ++depth;
14251                   break;
14252
14253                 case CPP_LESS:
14254                   if (depth == 0)
14255                     /* This might be the comparison operator, or it might
14256                        start a template argument list.  */
14257                     ++maybe_template_id;
14258                   break;
14259
14260                 case CPP_RSHIFT:
14261                   if (cxx_dialect == cxx98)
14262                     break;
14263                   /* Fall through for C++0x, which treats the `>>'
14264                      operator like two `>' tokens in certain
14265                      cases.  */
14266
14267                 case CPP_GREATER:
14268                   if (depth == 0)
14269                     {
14270                       /* This might be an operator, or it might close a
14271                          template argument list.  But if a previous '<'
14272                          started a template argument list, this will have
14273                          closed it, so we can't be in one anymore.  */
14274                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14275                       if (maybe_template_id < 0)
14276                         maybe_template_id = 0;
14277                     }
14278                   break;
14279
14280                   /* If we run out of tokens, issue an error message.  */
14281                 case CPP_EOF:
14282                 case CPP_PRAGMA_EOL:
14283                   error ("%Hfile ends in default argument", &token->location);
14284                   done = true;
14285                   break;
14286
14287                 case CPP_NAME:
14288                 case CPP_SCOPE:
14289                   /* In these cases, we should look for template-ids.
14290                      For example, if the default argument is
14291                      `X<int, double>()', we need to do name lookup to
14292                      figure out whether or not `X' is a template; if
14293                      so, the `,' does not end the default argument.
14294
14295                      That is not yet done.  */
14296                   break;
14297
14298                 default:
14299                   break;
14300                 }
14301
14302               /* If we've reached the end, stop.  */
14303               if (done)
14304                 break;
14305
14306               /* Add the token to the token block.  */
14307               token = cp_lexer_consume_token (parser->lexer);
14308             }
14309
14310           /* Create a DEFAULT_ARG to represent the unparsed default
14311              argument.  */
14312           default_argument = make_node (DEFAULT_ARG);
14313           DEFARG_TOKENS (default_argument)
14314             = cp_token_cache_new (first_token, token);
14315           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14316         }
14317       /* Outside of a class definition, we can just parse the
14318          assignment-expression.  */
14319       else
14320         {
14321           token = cp_lexer_peek_token (parser->lexer);
14322           default_argument 
14323             = cp_parser_default_argument (parser, template_parm_p);
14324         }
14325
14326       if (!parser->default_arg_ok_p)
14327         {
14328           if (flag_permissive)
14329             warning (0, "deprecated use of default argument for parameter of non-function");
14330           else
14331             {
14332               error ("%Hdefault arguments are only "
14333                      "permitted for function parameters",
14334                      &token->location);
14335               default_argument = NULL_TREE;
14336             }
14337         }
14338       else if ((declarator && declarator->parameter_pack_p)
14339                || (decl_specifiers.type
14340                    && PACK_EXPANSION_P (decl_specifiers.type)))
14341         {
14342           const char* kind = template_parm_p? "template " : "";
14343           
14344           /* Find the name of the parameter pack.  */     
14345           cp_declarator *id_declarator = declarator;
14346           while (id_declarator && id_declarator->kind != cdk_id)
14347             id_declarator = id_declarator->declarator;
14348           
14349           if (id_declarator && id_declarator->kind == cdk_id)
14350             error ("%H%sparameter pack %qD cannot have a default argument",
14351                    &declarator_token_start->location,
14352                    kind, id_declarator->u.id.unqualified_name);
14353           else
14354             error ("%H%sparameter pack cannot have a default argument",
14355                    &declarator_token_start->location, kind);
14356           
14357           default_argument = NULL_TREE;
14358         }
14359     }
14360   else
14361     default_argument = NULL_TREE;
14362
14363   return make_parameter_declarator (&decl_specifiers,
14364                                     declarator,
14365                                     default_argument);
14366 }
14367
14368 /* Parse a default argument and return it.
14369
14370    TEMPLATE_PARM_P is true if this is a default argument for a
14371    non-type template parameter.  */
14372 static tree
14373 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14374 {
14375   tree default_argument = NULL_TREE;
14376   bool saved_greater_than_is_operator_p;
14377   bool saved_local_variables_forbidden_p;
14378
14379   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14380      set correctly.  */
14381   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14382   parser->greater_than_is_operator_p = !template_parm_p;
14383   /* Local variable names (and the `this' keyword) may not
14384      appear in a default argument.  */
14385   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14386   parser->local_variables_forbidden_p = true;
14387   /* The default argument expression may cause implicitly
14388      defined member functions to be synthesized, which will
14389      result in garbage collection.  We must treat this
14390      situation as if we were within the body of function so as
14391      to avoid collecting live data on the stack.  */
14392   ++function_depth;
14393   /* Parse the assignment-expression.  */
14394   if (template_parm_p)
14395     push_deferring_access_checks (dk_no_deferred);
14396   default_argument
14397     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14398   if (template_parm_p)
14399     pop_deferring_access_checks ();
14400   /* Restore saved state.  */
14401   --function_depth;
14402   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14403   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14404
14405   return default_argument;
14406 }
14407
14408 /* Parse a function-body.
14409
14410    function-body:
14411      compound_statement  */
14412
14413 static void
14414 cp_parser_function_body (cp_parser *parser)
14415 {
14416   cp_parser_compound_statement (parser, NULL, false);
14417 }
14418
14419 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14420    true if a ctor-initializer was present.  */
14421
14422 static bool
14423 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14424 {
14425   tree body;
14426   bool ctor_initializer_p;
14427
14428   /* Begin the function body.  */
14429   body = begin_function_body ();
14430   /* Parse the optional ctor-initializer.  */
14431   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14432   /* Parse the function-body.  */
14433   cp_parser_function_body (parser);
14434   /* Finish the function body.  */
14435   finish_function_body (body);
14436
14437   return ctor_initializer_p;
14438 }
14439
14440 /* Parse an initializer.
14441
14442    initializer:
14443      = initializer-clause
14444      ( expression-list )
14445
14446    Returns an expression representing the initializer.  If no
14447    initializer is present, NULL_TREE is returned.
14448
14449    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14450    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14451    set to TRUE if there is no initializer present.  If there is an
14452    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14453    is set to true; otherwise it is set to false.  */
14454
14455 static tree
14456 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14457                        bool* non_constant_p)
14458 {
14459   cp_token *token;
14460   tree init;
14461
14462   /* Peek at the next token.  */
14463   token = cp_lexer_peek_token (parser->lexer);
14464
14465   /* Let our caller know whether or not this initializer was
14466      parenthesized.  */
14467   *is_direct_init = (token->type != CPP_EQ);
14468   /* Assume that the initializer is constant.  */
14469   *non_constant_p = false;
14470
14471   if (token->type == CPP_EQ)
14472     {
14473       /* Consume the `='.  */
14474       cp_lexer_consume_token (parser->lexer);
14475       /* Parse the initializer-clause.  */
14476       init = cp_parser_initializer_clause (parser, non_constant_p);
14477     }
14478   else if (token->type == CPP_OPEN_PAREN)
14479     init = cp_parser_parenthesized_expression_list (parser, false,
14480                                                     /*cast_p=*/false,
14481                                                     /*allow_expansion_p=*/true,
14482                                                     non_constant_p);
14483   else if (token->type == CPP_OPEN_BRACE)
14484     {
14485       maybe_warn_cpp0x ("extended initializer lists");
14486       init = cp_parser_braced_list (parser, non_constant_p);
14487       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14488     }
14489   else
14490     {
14491       /* Anything else is an error.  */
14492       cp_parser_error (parser, "expected initializer");
14493       init = error_mark_node;
14494     }
14495
14496   return init;
14497 }
14498
14499 /* Parse an initializer-clause.
14500
14501    initializer-clause:
14502      assignment-expression
14503      braced-init-list
14504
14505    Returns an expression representing the initializer.
14506
14507    If the `assignment-expression' production is used the value
14508    returned is simply a representation for the expression.
14509
14510    Otherwise, calls cp_parser_braced_list.  */
14511
14512 static tree
14513 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14514 {
14515   tree initializer;
14516
14517   /* Assume the expression is constant.  */
14518   *non_constant_p = false;
14519
14520   /* If it is not a `{', then we are looking at an
14521      assignment-expression.  */
14522   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14523     {
14524       initializer
14525         = cp_parser_constant_expression (parser,
14526                                         /*allow_non_constant_p=*/true,
14527                                         non_constant_p);
14528       if (!*non_constant_p)
14529         initializer = fold_non_dependent_expr (initializer);
14530     }
14531   else
14532     initializer = cp_parser_braced_list (parser, non_constant_p);
14533
14534   return initializer;
14535 }
14536
14537 /* Parse a brace-enclosed initializer list.
14538
14539    braced-init-list:
14540      { initializer-list , [opt] }
14541      { }
14542
14543    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14544    the elements of the initializer-list (or NULL, if the last
14545    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14546    NULL_TREE.  There is no way to detect whether or not the optional
14547    trailing `,' was provided.  NON_CONSTANT_P is as for
14548    cp_parser_initializer.  */     
14549
14550 static tree
14551 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14552 {
14553   tree initializer;
14554
14555   /* Consume the `{' token.  */
14556   cp_lexer_consume_token (parser->lexer);
14557   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14558   initializer = make_node (CONSTRUCTOR);
14559   /* If it's not a `}', then there is a non-trivial initializer.  */
14560   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14561     {
14562       /* Parse the initializer list.  */
14563       CONSTRUCTOR_ELTS (initializer)
14564         = cp_parser_initializer_list (parser, non_constant_p);
14565       /* A trailing `,' token is allowed.  */
14566       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14567         cp_lexer_consume_token (parser->lexer);
14568     }
14569   /* Now, there should be a trailing `}'.  */
14570   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14571   TREE_TYPE (initializer) = init_list_type_node;
14572   return initializer;
14573 }
14574
14575 /* Parse an initializer-list.
14576
14577    initializer-list:
14578      initializer-clause ... [opt]
14579      initializer-list , initializer-clause ... [opt]
14580
14581    GNU Extension:
14582
14583    initializer-list:
14584      identifier : initializer-clause
14585      initializer-list, identifier : initializer-clause
14586
14587    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14588    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14589    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14590    as for cp_parser_initializer.  */
14591
14592 static VEC(constructor_elt,gc) *
14593 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14594 {
14595   VEC(constructor_elt,gc) *v = NULL;
14596
14597   /* Assume all of the expressions are constant.  */
14598   *non_constant_p = false;
14599
14600   /* Parse the rest of the list.  */
14601   while (true)
14602     {
14603       cp_token *token;
14604       tree identifier;
14605       tree initializer;
14606       bool clause_non_constant_p;
14607
14608       /* If the next token is an identifier and the following one is a
14609          colon, we are looking at the GNU designated-initializer
14610          syntax.  */
14611       if (cp_parser_allow_gnu_extensions_p (parser)
14612           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14613           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14614         {
14615           /* Warn the user that they are using an extension.  */
14616           pedwarn (input_location, OPT_pedantic, 
14617                    "ISO C++ does not allow designated initializers");
14618           /* Consume the identifier.  */
14619           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14620           /* Consume the `:'.  */
14621           cp_lexer_consume_token (parser->lexer);
14622         }
14623       else
14624         identifier = NULL_TREE;
14625
14626       /* Parse the initializer.  */
14627       initializer = cp_parser_initializer_clause (parser,
14628                                                   &clause_non_constant_p);
14629       /* If any clause is non-constant, so is the entire initializer.  */
14630       if (clause_non_constant_p)
14631         *non_constant_p = true;
14632
14633       /* If we have an ellipsis, this is an initializer pack
14634          expansion.  */
14635       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14636         {
14637           /* Consume the `...'.  */
14638           cp_lexer_consume_token (parser->lexer);
14639
14640           /* Turn the initializer into an initializer expansion.  */
14641           initializer = make_pack_expansion (initializer);
14642         }
14643
14644       /* Add it to the vector.  */
14645       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14646
14647       /* If the next token is not a comma, we have reached the end of
14648          the list.  */
14649       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14650         break;
14651
14652       /* Peek at the next token.  */
14653       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14654       /* If the next token is a `}', then we're still done.  An
14655          initializer-clause can have a trailing `,' after the
14656          initializer-list and before the closing `}'.  */
14657       if (token->type == CPP_CLOSE_BRACE)
14658         break;
14659
14660       /* Consume the `,' token.  */
14661       cp_lexer_consume_token (parser->lexer);
14662     }
14663
14664   return v;
14665 }
14666
14667 /* Classes [gram.class] */
14668
14669 /* Parse a class-name.
14670
14671    class-name:
14672      identifier
14673      template-id
14674
14675    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14676    to indicate that names looked up in dependent types should be
14677    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14678    keyword has been used to indicate that the name that appears next
14679    is a template.  TAG_TYPE indicates the explicit tag given before
14680    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14681    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14682    is the class being defined in a class-head.
14683
14684    Returns the TYPE_DECL representing the class.  */
14685
14686 static tree
14687 cp_parser_class_name (cp_parser *parser,
14688                       bool typename_keyword_p,
14689                       bool template_keyword_p,
14690                       enum tag_types tag_type,
14691                       bool check_dependency_p,
14692                       bool class_head_p,
14693                       bool is_declaration)
14694 {
14695   tree decl;
14696   tree scope;
14697   bool typename_p;
14698   cp_token *token;
14699   tree identifier = NULL_TREE;
14700
14701   /* All class-names start with an identifier.  */
14702   token = cp_lexer_peek_token (parser->lexer);
14703   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14704     {
14705       cp_parser_error (parser, "expected class-name");
14706       return error_mark_node;
14707     }
14708
14709   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14710      to a template-id, so we save it here.  */
14711   scope = parser->scope;
14712   if (scope == error_mark_node)
14713     return error_mark_node;
14714
14715   /* Any name names a type if we're following the `typename' keyword
14716      in a qualified name where the enclosing scope is type-dependent.  */
14717   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14718                 && dependent_type_p (scope));
14719   /* Handle the common case (an identifier, but not a template-id)
14720      efficiently.  */
14721   if (token->type == CPP_NAME
14722       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14723     {
14724       cp_token *identifier_token;
14725       bool ambiguous_p;
14726
14727       /* Look for the identifier.  */
14728       identifier_token = cp_lexer_peek_token (parser->lexer);
14729       ambiguous_p = identifier_token->ambiguous_p;
14730       identifier = cp_parser_identifier (parser);
14731       /* If the next token isn't an identifier, we are certainly not
14732          looking at a class-name.  */
14733       if (identifier == error_mark_node)
14734         decl = error_mark_node;
14735       /* If we know this is a type-name, there's no need to look it
14736          up.  */
14737       else if (typename_p)
14738         decl = identifier;
14739       else
14740         {
14741           tree ambiguous_decls;
14742           /* If we already know that this lookup is ambiguous, then
14743              we've already issued an error message; there's no reason
14744              to check again.  */
14745           if (ambiguous_p)
14746             {
14747               cp_parser_simulate_error (parser);
14748               return error_mark_node;
14749             }
14750           /* If the next token is a `::', then the name must be a type
14751              name.
14752
14753              [basic.lookup.qual]
14754
14755              During the lookup for a name preceding the :: scope
14756              resolution operator, object, function, and enumerator
14757              names are ignored.  */
14758           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14759             tag_type = typename_type;
14760           /* Look up the name.  */
14761           decl = cp_parser_lookup_name (parser, identifier,
14762                                         tag_type,
14763                                         /*is_template=*/false,
14764                                         /*is_namespace=*/false,
14765                                         check_dependency_p,
14766                                         &ambiguous_decls,
14767                                         identifier_token->location);
14768           if (ambiguous_decls)
14769             {
14770               error ("%Hreference to %qD is ambiguous",
14771                      &identifier_token->location, identifier);
14772               print_candidates (ambiguous_decls);
14773               if (cp_parser_parsing_tentatively (parser))
14774                 {
14775                   identifier_token->ambiguous_p = true;
14776                   cp_parser_simulate_error (parser);
14777                 }
14778               return error_mark_node;
14779             }
14780         }
14781     }
14782   else
14783     {
14784       /* Try a template-id.  */
14785       decl = cp_parser_template_id (parser, template_keyword_p,
14786                                     check_dependency_p,
14787                                     is_declaration);
14788       if (decl == error_mark_node)
14789         return error_mark_node;
14790     }
14791
14792   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14793
14794   /* If this is a typename, create a TYPENAME_TYPE.  */
14795   if (typename_p && decl != error_mark_node)
14796     {
14797       decl = make_typename_type (scope, decl, typename_type,
14798                                  /*complain=*/tf_error);
14799       if (decl != error_mark_node)
14800         decl = TYPE_NAME (decl);
14801     }
14802
14803   /* Check to see that it is really the name of a class.  */
14804   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14805       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14806       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14807     /* Situations like this:
14808
14809          template <typename T> struct A {
14810            typename T::template X<int>::I i;
14811          };
14812
14813        are problematic.  Is `T::template X<int>' a class-name?  The
14814        standard does not seem to be definitive, but there is no other
14815        valid interpretation of the following `::'.  Therefore, those
14816        names are considered class-names.  */
14817     {
14818       decl = make_typename_type (scope, decl, tag_type, tf_error);
14819       if (decl != error_mark_node)
14820         decl = TYPE_NAME (decl);
14821     }
14822   else if (TREE_CODE (decl) != TYPE_DECL
14823            || TREE_TYPE (decl) == error_mark_node
14824            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14825     decl = error_mark_node;
14826
14827   if (decl == error_mark_node)
14828     cp_parser_error (parser, "expected class-name");
14829   else if (identifier && !parser->scope)
14830     maybe_note_name_used_in_class (identifier, decl);
14831
14832   return decl;
14833 }
14834
14835 /* Parse a class-specifier.
14836
14837    class-specifier:
14838      class-head { member-specification [opt] }
14839
14840    Returns the TREE_TYPE representing the class.  */
14841
14842 static tree
14843 cp_parser_class_specifier (cp_parser* parser)
14844 {
14845   cp_token *token;
14846   tree type;
14847   tree attributes = NULL_TREE;
14848   int has_trailing_semicolon;
14849   bool nested_name_specifier_p;
14850   unsigned saved_num_template_parameter_lists;
14851   bool saved_in_function_body;
14852   tree old_scope = NULL_TREE;
14853   tree scope = NULL_TREE;
14854   tree bases;
14855
14856   push_deferring_access_checks (dk_no_deferred);
14857
14858   /* Parse the class-head.  */
14859   type = cp_parser_class_head (parser,
14860                                &nested_name_specifier_p,
14861                                &attributes,
14862                                &bases);
14863   /* If the class-head was a semantic disaster, skip the entire body
14864      of the class.  */
14865   if (!type)
14866     {
14867       cp_parser_skip_to_end_of_block_or_statement (parser);
14868       pop_deferring_access_checks ();
14869       return error_mark_node;
14870     }
14871
14872   /* Look for the `{'.  */
14873   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14874     {
14875       pop_deferring_access_checks ();
14876       return error_mark_node;
14877     }
14878
14879   /* Process the base classes. If they're invalid, skip the 
14880      entire class body.  */
14881   if (!xref_basetypes (type, bases))
14882     {
14883       /* Consuming the closing brace yields better error messages
14884          later on.  */
14885       if (cp_parser_skip_to_closing_brace (parser))
14886         cp_lexer_consume_token (parser->lexer);
14887       pop_deferring_access_checks ();
14888       return error_mark_node;
14889     }
14890
14891   /* Issue an error message if type-definitions are forbidden here.  */
14892   cp_parser_check_type_definition (parser);
14893   /* Remember that we are defining one more class.  */
14894   ++parser->num_classes_being_defined;
14895   /* Inside the class, surrounding template-parameter-lists do not
14896      apply.  */
14897   saved_num_template_parameter_lists
14898     = parser->num_template_parameter_lists;
14899   parser->num_template_parameter_lists = 0;
14900   /* We are not in a function body.  */
14901   saved_in_function_body = parser->in_function_body;
14902   parser->in_function_body = false;
14903
14904   /* Start the class.  */
14905   if (nested_name_specifier_p)
14906     {
14907       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14908       old_scope = push_inner_scope (scope);
14909     }
14910   type = begin_class_definition (type, attributes);
14911
14912   if (type == error_mark_node)
14913     /* If the type is erroneous, skip the entire body of the class.  */
14914     cp_parser_skip_to_closing_brace (parser);
14915   else
14916     /* Parse the member-specification.  */
14917     cp_parser_member_specification_opt (parser);
14918
14919   /* Look for the trailing `}'.  */
14920   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14921   /* We get better error messages by noticing a common problem: a
14922      missing trailing `;'.  */
14923   token = cp_lexer_peek_token (parser->lexer);
14924   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14925   /* Look for trailing attributes to apply to this class.  */
14926   if (cp_parser_allow_gnu_extensions_p (parser))
14927     attributes = cp_parser_attributes_opt (parser);
14928   if (type != error_mark_node)
14929     type = finish_struct (type, attributes);
14930   if (nested_name_specifier_p)
14931     pop_inner_scope (old_scope, scope);
14932   /* If this class is not itself within the scope of another class,
14933      then we need to parse the bodies of all of the queued function
14934      definitions.  Note that the queued functions defined in a class
14935      are not always processed immediately following the
14936      class-specifier for that class.  Consider:
14937
14938        struct A {
14939          struct B { void f() { sizeof (A); } };
14940        };
14941
14942      If `f' were processed before the processing of `A' were
14943      completed, there would be no way to compute the size of `A'.
14944      Note that the nesting we are interested in here is lexical --
14945      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14946      for:
14947
14948        struct A { struct B; };
14949        struct A::B { void f() { } };
14950
14951      there is no need to delay the parsing of `A::B::f'.  */
14952   if (--parser->num_classes_being_defined == 0)
14953     {
14954       tree queue_entry;
14955       tree fn;
14956       tree class_type = NULL_TREE;
14957       tree pushed_scope = NULL_TREE;
14958
14959       /* In a first pass, parse default arguments to the functions.
14960          Then, in a second pass, parse the bodies of the functions.
14961          This two-phased approach handles cases like:
14962
14963             struct S {
14964               void f() { g(); }
14965               void g(int i = 3);
14966             };
14967
14968          */
14969       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14970              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14971            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14972            TREE_PURPOSE (parser->unparsed_functions_queues)
14973              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14974         {
14975           fn = TREE_VALUE (queue_entry);
14976           /* If there are default arguments that have not yet been processed,
14977              take care of them now.  */
14978           if (class_type != TREE_PURPOSE (queue_entry))
14979             {
14980               if (pushed_scope)
14981                 pop_scope (pushed_scope);
14982               class_type = TREE_PURPOSE (queue_entry);
14983               pushed_scope = push_scope (class_type);
14984             }
14985           /* Make sure that any template parameters are in scope.  */
14986           maybe_begin_member_template_processing (fn);
14987           /* Parse the default argument expressions.  */
14988           cp_parser_late_parsing_default_args (parser, fn);
14989           /* Remove any template parameters from the symbol table.  */
14990           maybe_end_member_template_processing ();
14991         }
14992       if (pushed_scope)
14993         pop_scope (pushed_scope);
14994       /* Now parse the body of the functions.  */
14995       for (TREE_VALUE (parser->unparsed_functions_queues)
14996              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14997            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14998            TREE_VALUE (parser->unparsed_functions_queues)
14999              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15000         {
15001           /* Figure out which function we need to process.  */
15002           fn = TREE_VALUE (queue_entry);
15003           /* Parse the function.  */
15004           cp_parser_late_parsing_for_member (parser, fn);
15005         }
15006     }
15007
15008   /* Put back any saved access checks.  */
15009   pop_deferring_access_checks ();
15010
15011   /* Restore saved state.  */
15012   parser->in_function_body = saved_in_function_body;
15013   parser->num_template_parameter_lists
15014     = saved_num_template_parameter_lists;
15015
15016   return type;
15017 }
15018
15019 /* Parse a class-head.
15020
15021    class-head:
15022      class-key identifier [opt] base-clause [opt]
15023      class-key nested-name-specifier identifier base-clause [opt]
15024      class-key nested-name-specifier [opt] template-id
15025        base-clause [opt]
15026
15027    GNU Extensions:
15028      class-key attributes identifier [opt] base-clause [opt]
15029      class-key attributes nested-name-specifier identifier base-clause [opt]
15030      class-key attributes nested-name-specifier [opt] template-id
15031        base-clause [opt]
15032
15033    Upon return BASES is initialized to the list of base classes (or
15034    NULL, if there are none) in the same form returned by
15035    cp_parser_base_clause.
15036
15037    Returns the TYPE of the indicated class.  Sets
15038    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15039    involving a nested-name-specifier was used, and FALSE otherwise.
15040
15041    Returns error_mark_node if this is not a class-head.
15042
15043    Returns NULL_TREE if the class-head is syntactically valid, but
15044    semantically invalid in a way that means we should skip the entire
15045    body of the class.  */
15046
15047 static tree
15048 cp_parser_class_head (cp_parser* parser,
15049                       bool* nested_name_specifier_p,
15050                       tree *attributes_p,
15051                       tree *bases)
15052 {
15053   tree nested_name_specifier;
15054   enum tag_types class_key;
15055   tree id = NULL_TREE;
15056   tree type = NULL_TREE;
15057   tree attributes;
15058   bool template_id_p = false;
15059   bool qualified_p = false;
15060   bool invalid_nested_name_p = false;
15061   bool invalid_explicit_specialization_p = false;
15062   tree pushed_scope = NULL_TREE;
15063   unsigned num_templates;
15064   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15065   /* Assume no nested-name-specifier will be present.  */
15066   *nested_name_specifier_p = false;
15067   /* Assume no template parameter lists will be used in defining the
15068      type.  */
15069   num_templates = 0;
15070
15071   *bases = NULL_TREE;
15072
15073   /* Look for the class-key.  */
15074   class_key = cp_parser_class_key (parser);
15075   if (class_key == none_type)
15076     return error_mark_node;
15077
15078   /* Parse the attributes.  */
15079   attributes = cp_parser_attributes_opt (parser);
15080
15081   /* If the next token is `::', that is invalid -- but sometimes
15082      people do try to write:
15083
15084        struct ::S {};
15085
15086      Handle this gracefully by accepting the extra qualifier, and then
15087      issuing an error about it later if this really is a
15088      class-head.  If it turns out just to be an elaborated type
15089      specifier, remain silent.  */
15090   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15091     qualified_p = true;
15092
15093   push_deferring_access_checks (dk_no_check);
15094
15095   /* Determine the name of the class.  Begin by looking for an
15096      optional nested-name-specifier.  */
15097   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15098   nested_name_specifier
15099     = cp_parser_nested_name_specifier_opt (parser,
15100                                            /*typename_keyword_p=*/false,
15101                                            /*check_dependency_p=*/false,
15102                                            /*type_p=*/false,
15103                                            /*is_declaration=*/false);
15104   /* If there was a nested-name-specifier, then there *must* be an
15105      identifier.  */
15106   if (nested_name_specifier)
15107     {
15108       type_start_token = cp_lexer_peek_token (parser->lexer);
15109       /* Although the grammar says `identifier', it really means
15110          `class-name' or `template-name'.  You are only allowed to
15111          define a class that has already been declared with this
15112          syntax.
15113
15114          The proposed resolution for Core Issue 180 says that wherever
15115          you see `class T::X' you should treat `X' as a type-name.
15116
15117          It is OK to define an inaccessible class; for example:
15118
15119            class A { class B; };
15120            class A::B {};
15121
15122          We do not know if we will see a class-name, or a
15123          template-name.  We look for a class-name first, in case the
15124          class-name is a template-id; if we looked for the
15125          template-name first we would stop after the template-name.  */
15126       cp_parser_parse_tentatively (parser);
15127       type = cp_parser_class_name (parser,
15128                                    /*typename_keyword_p=*/false,
15129                                    /*template_keyword_p=*/false,
15130                                    class_type,
15131                                    /*check_dependency_p=*/false,
15132                                    /*class_head_p=*/true,
15133                                    /*is_declaration=*/false);
15134       /* If that didn't work, ignore the nested-name-specifier.  */
15135       if (!cp_parser_parse_definitely (parser))
15136         {
15137           invalid_nested_name_p = true;
15138           type_start_token = cp_lexer_peek_token (parser->lexer);
15139           id = cp_parser_identifier (parser);
15140           if (id == error_mark_node)
15141             id = NULL_TREE;
15142         }
15143       /* If we could not find a corresponding TYPE, treat this
15144          declaration like an unqualified declaration.  */
15145       if (type == error_mark_node)
15146         nested_name_specifier = NULL_TREE;
15147       /* Otherwise, count the number of templates used in TYPE and its
15148          containing scopes.  */
15149       else
15150         {
15151           tree scope;
15152
15153           for (scope = TREE_TYPE (type);
15154                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15155                scope = (TYPE_P (scope)
15156                         ? TYPE_CONTEXT (scope)
15157                         : DECL_CONTEXT (scope)))
15158             if (TYPE_P (scope)
15159                 && CLASS_TYPE_P (scope)
15160                 && CLASSTYPE_TEMPLATE_INFO (scope)
15161                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15162                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15163               ++num_templates;
15164         }
15165     }
15166   /* Otherwise, the identifier is optional.  */
15167   else
15168     {
15169       /* We don't know whether what comes next is a template-id,
15170          an identifier, or nothing at all.  */
15171       cp_parser_parse_tentatively (parser);
15172       /* Check for a template-id.  */
15173       type_start_token = cp_lexer_peek_token (parser->lexer);
15174       id = cp_parser_template_id (parser,
15175                                   /*template_keyword_p=*/false,
15176                                   /*check_dependency_p=*/true,
15177                                   /*is_declaration=*/true);
15178       /* If that didn't work, it could still be an identifier.  */
15179       if (!cp_parser_parse_definitely (parser))
15180         {
15181           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15182             {
15183               type_start_token = cp_lexer_peek_token (parser->lexer);
15184               id = cp_parser_identifier (parser);
15185             }
15186           else
15187             id = NULL_TREE;
15188         }
15189       else
15190         {
15191           template_id_p = true;
15192           ++num_templates;
15193         }
15194     }
15195
15196   pop_deferring_access_checks ();
15197
15198   if (id)
15199     cp_parser_check_for_invalid_template_id (parser, id,
15200                                              type_start_token->location);
15201
15202   /* If it's not a `:' or a `{' then we can't really be looking at a
15203      class-head, since a class-head only appears as part of a
15204      class-specifier.  We have to detect this situation before calling
15205      xref_tag, since that has irreversible side-effects.  */
15206   if (!cp_parser_next_token_starts_class_definition_p (parser))
15207     {
15208       cp_parser_error (parser, "expected %<{%> or %<:%>");
15209       return error_mark_node;
15210     }
15211
15212   /* At this point, we're going ahead with the class-specifier, even
15213      if some other problem occurs.  */
15214   cp_parser_commit_to_tentative_parse (parser);
15215   /* Issue the error about the overly-qualified name now.  */
15216   if (qualified_p)
15217     {
15218       cp_parser_error (parser,
15219                        "global qualification of class name is invalid");
15220       return error_mark_node;
15221     }
15222   else if (invalid_nested_name_p)
15223     {
15224       cp_parser_error (parser,
15225                        "qualified name does not name a class");
15226       return error_mark_node;
15227     }
15228   else if (nested_name_specifier)
15229     {
15230       tree scope;
15231
15232       /* Reject typedef-names in class heads.  */
15233       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15234         {
15235           error ("%Hinvalid class name in declaration of %qD",
15236                  &type_start_token->location, type);
15237           type = NULL_TREE;
15238           goto done;
15239         }
15240
15241       /* Figure out in what scope the declaration is being placed.  */
15242       scope = current_scope ();
15243       /* If that scope does not contain the scope in which the
15244          class was originally declared, the program is invalid.  */
15245       if (scope && !is_ancestor (scope, nested_name_specifier))
15246         {
15247           if (at_namespace_scope_p ())
15248             error ("%Hdeclaration of %qD in namespace %qD which does not "
15249                    "enclose %qD",
15250                    &type_start_token->location,
15251                    type, scope, nested_name_specifier);
15252           else
15253             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15254                    &type_start_token->location,
15255                    type, scope, nested_name_specifier);
15256           type = NULL_TREE;
15257           goto done;
15258         }
15259       /* [dcl.meaning]
15260
15261          A declarator-id shall not be qualified except for the
15262          definition of a ... nested class outside of its class
15263          ... [or] the definition or explicit instantiation of a
15264          class member of a namespace outside of its namespace.  */
15265       if (scope == nested_name_specifier)
15266         {
15267           permerror (input_location, "%Hextra qualification not allowed",
15268                      &nested_name_specifier_token_start->location);
15269           nested_name_specifier = NULL_TREE;
15270           num_templates = 0;
15271         }
15272     }
15273   /* An explicit-specialization must be preceded by "template <>".  If
15274      it is not, try to recover gracefully.  */
15275   if (at_namespace_scope_p ()
15276       && parser->num_template_parameter_lists == 0
15277       && template_id_p)
15278     {
15279       error ("%Han explicit specialization must be preceded by %<template <>%>",
15280              &type_start_token->location);
15281       invalid_explicit_specialization_p = true;
15282       /* Take the same action that would have been taken by
15283          cp_parser_explicit_specialization.  */
15284       ++parser->num_template_parameter_lists;
15285       begin_specialization ();
15286     }
15287   /* There must be no "return" statements between this point and the
15288      end of this function; set "type "to the correct return value and
15289      use "goto done;" to return.  */
15290   /* Make sure that the right number of template parameters were
15291      present.  */
15292   if (!cp_parser_check_template_parameters (parser, num_templates,
15293                                             type_start_token->location))
15294     {
15295       /* If something went wrong, there is no point in even trying to
15296          process the class-definition.  */
15297       type = NULL_TREE;
15298       goto done;
15299     }
15300
15301   /* Look up the type.  */
15302   if (template_id_p)
15303     {
15304       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15305           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15306               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15307         {
15308           error ("%Hfunction template %qD redeclared as a class template",
15309                  &type_start_token->location, id);
15310           type = error_mark_node;
15311         }
15312       else
15313         {
15314           type = TREE_TYPE (id);
15315           type = maybe_process_partial_specialization (type);
15316         }
15317       if (nested_name_specifier)
15318         pushed_scope = push_scope (nested_name_specifier);
15319     }
15320   else if (nested_name_specifier)
15321     {
15322       tree class_type;
15323
15324       /* Given:
15325
15326             template <typename T> struct S { struct T };
15327             template <typename T> struct S<T>::T { };
15328
15329          we will get a TYPENAME_TYPE when processing the definition of
15330          `S::T'.  We need to resolve it to the actual type before we
15331          try to define it.  */
15332       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15333         {
15334           class_type = resolve_typename_type (TREE_TYPE (type),
15335                                               /*only_current_p=*/false);
15336           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15337             type = TYPE_NAME (class_type);
15338           else
15339             {
15340               cp_parser_error (parser, "could not resolve typename type");
15341               type = error_mark_node;
15342             }
15343         }
15344
15345       if (maybe_process_partial_specialization (TREE_TYPE (type))
15346           == error_mark_node)
15347         {
15348           type = NULL_TREE;
15349           goto done;
15350         }
15351
15352       class_type = current_class_type;
15353       /* Enter the scope indicated by the nested-name-specifier.  */
15354       pushed_scope = push_scope (nested_name_specifier);
15355       /* Get the canonical version of this type.  */
15356       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15357       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15358           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15359         {
15360           type = push_template_decl (type);
15361           if (type == error_mark_node)
15362             {
15363               type = NULL_TREE;
15364               goto done;
15365             }
15366         }
15367
15368       type = TREE_TYPE (type);
15369       *nested_name_specifier_p = true;
15370     }
15371   else      /* The name is not a nested name.  */
15372     {
15373       /* If the class was unnamed, create a dummy name.  */
15374       if (!id)
15375         id = make_anon_name ();
15376       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15377                        parser->num_template_parameter_lists);
15378     }
15379
15380   /* Indicate whether this class was declared as a `class' or as a
15381      `struct'.  */
15382   if (TREE_CODE (type) == RECORD_TYPE)
15383     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15384   cp_parser_check_class_key (class_key, type);
15385
15386   /* If this type was already complete, and we see another definition,
15387      that's an error.  */
15388   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15389     {
15390       error ("%Hredefinition of %q#T",
15391              &type_start_token->location, type);
15392       error ("%Hprevious definition of %q+#T",
15393              &type_start_token->location, type);
15394       type = NULL_TREE;
15395       goto done;
15396     }
15397   else if (type == error_mark_node)
15398     type = NULL_TREE;
15399
15400   /* We will have entered the scope containing the class; the names of
15401      base classes should be looked up in that context.  For example:
15402
15403        struct A { struct B {}; struct C; };
15404        struct A::C : B {};
15405
15406      is valid.  */
15407
15408   /* Get the list of base-classes, if there is one.  */
15409   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15410     *bases = cp_parser_base_clause (parser);
15411
15412  done:
15413   /* Leave the scope given by the nested-name-specifier.  We will
15414      enter the class scope itself while processing the members.  */
15415   if (pushed_scope)
15416     pop_scope (pushed_scope);
15417
15418   if (invalid_explicit_specialization_p)
15419     {
15420       end_specialization ();
15421       --parser->num_template_parameter_lists;
15422     }
15423   *attributes_p = attributes;
15424   return type;
15425 }
15426
15427 /* Parse a class-key.
15428
15429    class-key:
15430      class
15431      struct
15432      union
15433
15434    Returns the kind of class-key specified, or none_type to indicate
15435    error.  */
15436
15437 static enum tag_types
15438 cp_parser_class_key (cp_parser* parser)
15439 {
15440   cp_token *token;
15441   enum tag_types tag_type;
15442
15443   /* Look for the class-key.  */
15444   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15445   if (!token)
15446     return none_type;
15447
15448   /* Check to see if the TOKEN is a class-key.  */
15449   tag_type = cp_parser_token_is_class_key (token);
15450   if (!tag_type)
15451     cp_parser_error (parser, "expected class-key");
15452   return tag_type;
15453 }
15454
15455 /* Parse an (optional) member-specification.
15456
15457    member-specification:
15458      member-declaration member-specification [opt]
15459      access-specifier : member-specification [opt]  */
15460
15461 static void
15462 cp_parser_member_specification_opt (cp_parser* parser)
15463 {
15464   while (true)
15465     {
15466       cp_token *token;
15467       enum rid keyword;
15468
15469       /* Peek at the next token.  */
15470       token = cp_lexer_peek_token (parser->lexer);
15471       /* If it's a `}', or EOF then we've seen all the members.  */
15472       if (token->type == CPP_CLOSE_BRACE
15473           || token->type == CPP_EOF
15474           || token->type == CPP_PRAGMA_EOL)
15475         break;
15476
15477       /* See if this token is a keyword.  */
15478       keyword = token->keyword;
15479       switch (keyword)
15480         {
15481         case RID_PUBLIC:
15482         case RID_PROTECTED:
15483         case RID_PRIVATE:
15484           /* Consume the access-specifier.  */
15485           cp_lexer_consume_token (parser->lexer);
15486           /* Remember which access-specifier is active.  */
15487           current_access_specifier = token->u.value;
15488           /* Look for the `:'.  */
15489           cp_parser_require (parser, CPP_COLON, "%<:%>");
15490           break;
15491
15492         default:
15493           /* Accept #pragmas at class scope.  */
15494           if (token->type == CPP_PRAGMA)
15495             {
15496               cp_parser_pragma (parser, pragma_external);
15497               break;
15498             }
15499
15500           /* Otherwise, the next construction must be a
15501              member-declaration.  */
15502           cp_parser_member_declaration (parser);
15503         }
15504     }
15505 }
15506
15507 /* Parse a member-declaration.
15508
15509    member-declaration:
15510      decl-specifier-seq [opt] member-declarator-list [opt] ;
15511      function-definition ; [opt]
15512      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15513      using-declaration
15514      template-declaration
15515
15516    member-declarator-list:
15517      member-declarator
15518      member-declarator-list , member-declarator
15519
15520    member-declarator:
15521      declarator pure-specifier [opt]
15522      declarator constant-initializer [opt]
15523      identifier [opt] : constant-expression
15524
15525    GNU Extensions:
15526
15527    member-declaration:
15528      __extension__ member-declaration
15529
15530    member-declarator:
15531      declarator attributes [opt] pure-specifier [opt]
15532      declarator attributes [opt] constant-initializer [opt]
15533      identifier [opt] attributes [opt] : constant-expression  
15534
15535    C++0x Extensions:
15536
15537    member-declaration:
15538      static_assert-declaration  */
15539
15540 static void
15541 cp_parser_member_declaration (cp_parser* parser)
15542 {
15543   cp_decl_specifier_seq decl_specifiers;
15544   tree prefix_attributes;
15545   tree decl;
15546   int declares_class_or_enum;
15547   bool friend_p;
15548   cp_token *token = NULL;
15549   cp_token *decl_spec_token_start = NULL;
15550   cp_token *initializer_token_start = NULL;
15551   int saved_pedantic;
15552
15553   /* Check for the `__extension__' keyword.  */
15554   if (cp_parser_extension_opt (parser, &saved_pedantic))
15555     {
15556       /* Recurse.  */
15557       cp_parser_member_declaration (parser);
15558       /* Restore the old value of the PEDANTIC flag.  */
15559       pedantic = saved_pedantic;
15560
15561       return;
15562     }
15563
15564   /* Check for a template-declaration.  */
15565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15566     {
15567       /* An explicit specialization here is an error condition, and we
15568          expect the specialization handler to detect and report this.  */
15569       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15570           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15571         cp_parser_explicit_specialization (parser);
15572       else
15573         cp_parser_template_declaration (parser, /*member_p=*/true);
15574
15575       return;
15576     }
15577
15578   /* Check for a using-declaration.  */
15579   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15580     {
15581       /* Parse the using-declaration.  */
15582       cp_parser_using_declaration (parser,
15583                                    /*access_declaration_p=*/false);
15584       return;
15585     }
15586
15587   /* Check for @defs.  */
15588   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15589     {
15590       tree ivar, member;
15591       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15592       ivar = ivar_chains;
15593       while (ivar)
15594         {
15595           member = ivar;
15596           ivar = TREE_CHAIN (member);
15597           TREE_CHAIN (member) = NULL_TREE;
15598           finish_member_declaration (member);
15599         }
15600       return;
15601     }
15602
15603   /* If the next token is `static_assert' we have a static assertion.  */
15604   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15605     {
15606       cp_parser_static_assert (parser, /*member_p=*/true);
15607       return;
15608     }
15609
15610   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15611     return;
15612
15613   /* Parse the decl-specifier-seq.  */
15614   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15615   cp_parser_decl_specifier_seq (parser,
15616                                 CP_PARSER_FLAGS_OPTIONAL,
15617                                 &decl_specifiers,
15618                                 &declares_class_or_enum);
15619   prefix_attributes = decl_specifiers.attributes;
15620   decl_specifiers.attributes = NULL_TREE;
15621   /* Check for an invalid type-name.  */
15622   if (!decl_specifiers.type
15623       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15624     return;
15625   /* If there is no declarator, then the decl-specifier-seq should
15626      specify a type.  */
15627   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15628     {
15629       /* If there was no decl-specifier-seq, and the next token is a
15630          `;', then we have something like:
15631
15632            struct S { ; };
15633
15634          [class.mem]
15635
15636          Each member-declaration shall declare at least one member
15637          name of the class.  */
15638       if (!decl_specifiers.any_specifiers_p)
15639         {
15640           cp_token *token = cp_lexer_peek_token (parser->lexer);
15641           if (!in_system_header_at (token->location))
15642             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15643         }
15644       else
15645         {
15646           tree type;
15647
15648           /* See if this declaration is a friend.  */
15649           friend_p = cp_parser_friend_p (&decl_specifiers);
15650           /* If there were decl-specifiers, check to see if there was
15651              a class-declaration.  */
15652           type = check_tag_decl (&decl_specifiers);
15653           /* Nested classes have already been added to the class, but
15654              a `friend' needs to be explicitly registered.  */
15655           if (friend_p)
15656             {
15657               /* If the `friend' keyword was present, the friend must
15658                  be introduced with a class-key.  */
15659                if (!declares_class_or_enum)
15660                  error ("%Ha class-key must be used when declaring a friend",
15661                         &decl_spec_token_start->location);
15662                /* In this case:
15663
15664                     template <typename T> struct A {
15665                       friend struct A<T>::B;
15666                     };
15667
15668                   A<T>::B will be represented by a TYPENAME_TYPE, and
15669                   therefore not recognized by check_tag_decl.  */
15670                if (!type
15671                    && decl_specifiers.type
15672                    && TYPE_P (decl_specifiers.type))
15673                  type = decl_specifiers.type;
15674                if (!type || !TYPE_P (type))
15675                  error ("%Hfriend declaration does not name a class or "
15676                         "function", &decl_spec_token_start->location);
15677                else
15678                  make_friend_class (current_class_type, type,
15679                                     /*complain=*/true);
15680             }
15681           /* If there is no TYPE, an error message will already have
15682              been issued.  */
15683           else if (!type || type == error_mark_node)
15684             ;
15685           /* An anonymous aggregate has to be handled specially; such
15686              a declaration really declares a data member (with a
15687              particular type), as opposed to a nested class.  */
15688           else if (ANON_AGGR_TYPE_P (type))
15689             {
15690               /* Remove constructors and such from TYPE, now that we
15691                  know it is an anonymous aggregate.  */
15692               fixup_anonymous_aggr (type);
15693               /* And make the corresponding data member.  */
15694               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15695               /* Add it to the class.  */
15696               finish_member_declaration (decl);
15697             }
15698           else
15699             cp_parser_check_access_in_redeclaration
15700                                               (TYPE_NAME (type),
15701                                                decl_spec_token_start->location);
15702         }
15703     }
15704   else
15705     {
15706       /* See if these declarations will be friends.  */
15707       friend_p = cp_parser_friend_p (&decl_specifiers);
15708
15709       /* Keep going until we hit the `;' at the end of the
15710          declaration.  */
15711       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15712         {
15713           tree attributes = NULL_TREE;
15714           tree first_attribute;
15715
15716           /* Peek at the next token.  */
15717           token = cp_lexer_peek_token (parser->lexer);
15718
15719           /* Check for a bitfield declaration.  */
15720           if (token->type == CPP_COLON
15721               || (token->type == CPP_NAME
15722                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15723                   == CPP_COLON))
15724             {
15725               tree identifier;
15726               tree width;
15727
15728               /* Get the name of the bitfield.  Note that we cannot just
15729                  check TOKEN here because it may have been invalidated by
15730                  the call to cp_lexer_peek_nth_token above.  */
15731               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15732                 identifier = cp_parser_identifier (parser);
15733               else
15734                 identifier = NULL_TREE;
15735
15736               /* Consume the `:' token.  */
15737               cp_lexer_consume_token (parser->lexer);
15738               /* Get the width of the bitfield.  */
15739               width
15740                 = cp_parser_constant_expression (parser,
15741                                                  /*allow_non_constant=*/false,
15742                                                  NULL);
15743
15744               /* Look for attributes that apply to the bitfield.  */
15745               attributes = cp_parser_attributes_opt (parser);
15746               /* Remember which attributes are prefix attributes and
15747                  which are not.  */
15748               first_attribute = attributes;
15749               /* Combine the attributes.  */
15750               attributes = chainon (prefix_attributes, attributes);
15751
15752               /* Create the bitfield declaration.  */
15753               decl = grokbitfield (identifier
15754                                    ? make_id_declarator (NULL_TREE,
15755                                                          identifier,
15756                                                          sfk_none)
15757                                    : NULL,
15758                                    &decl_specifiers,
15759                                    width,
15760                                    attributes);
15761             }
15762           else
15763             {
15764               cp_declarator *declarator;
15765               tree initializer;
15766               tree asm_specification;
15767               int ctor_dtor_or_conv_p;
15768
15769               /* Parse the declarator.  */
15770               declarator
15771                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15772                                         &ctor_dtor_or_conv_p,
15773                                         /*parenthesized_p=*/NULL,
15774                                         /*member_p=*/true);
15775
15776               /* If something went wrong parsing the declarator, make sure
15777                  that we at least consume some tokens.  */
15778               if (declarator == cp_error_declarator)
15779                 {
15780                   /* Skip to the end of the statement.  */
15781                   cp_parser_skip_to_end_of_statement (parser);
15782                   /* If the next token is not a semicolon, that is
15783                      probably because we just skipped over the body of
15784                      a function.  So, we consume a semicolon if
15785                      present, but do not issue an error message if it
15786                      is not present.  */
15787                   if (cp_lexer_next_token_is (parser->lexer,
15788                                               CPP_SEMICOLON))
15789                     cp_lexer_consume_token (parser->lexer);
15790                   return;
15791                 }
15792
15793               if (declares_class_or_enum & 2)
15794                 cp_parser_check_for_definition_in_return_type
15795                                             (declarator, decl_specifiers.type,
15796                                              decl_specifiers.type_location);
15797
15798               /* Look for an asm-specification.  */
15799               asm_specification = cp_parser_asm_specification_opt (parser);
15800               /* Look for attributes that apply to the declaration.  */
15801               attributes = cp_parser_attributes_opt (parser);
15802               /* Remember which attributes are prefix attributes and
15803                  which are not.  */
15804               first_attribute = attributes;
15805               /* Combine the attributes.  */
15806               attributes = chainon (prefix_attributes, attributes);
15807
15808               /* If it's an `=', then we have a constant-initializer or a
15809                  pure-specifier.  It is not correct to parse the
15810                  initializer before registering the member declaration
15811                  since the member declaration should be in scope while
15812                  its initializer is processed.  However, the rest of the
15813                  front end does not yet provide an interface that allows
15814                  us to handle this correctly.  */
15815               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15816                 {
15817                   /* In [class.mem]:
15818
15819                      A pure-specifier shall be used only in the declaration of
15820                      a virtual function.
15821
15822                      A member-declarator can contain a constant-initializer
15823                      only if it declares a static member of integral or
15824                      enumeration type.
15825
15826                      Therefore, if the DECLARATOR is for a function, we look
15827                      for a pure-specifier; otherwise, we look for a
15828                      constant-initializer.  When we call `grokfield', it will
15829                      perform more stringent semantics checks.  */
15830                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15831                   if (function_declarator_p (declarator))
15832                     initializer = cp_parser_pure_specifier (parser);
15833                   else
15834                     /* Parse the initializer.  */
15835                     initializer = cp_parser_constant_initializer (parser);
15836                 }
15837               /* Otherwise, there is no initializer.  */
15838               else
15839                 initializer = NULL_TREE;
15840
15841               /* See if we are probably looking at a function
15842                  definition.  We are certainly not looking at a
15843                  member-declarator.  Calling `grokfield' has
15844                  side-effects, so we must not do it unless we are sure
15845                  that we are looking at a member-declarator.  */
15846               if (cp_parser_token_starts_function_definition_p
15847                   (cp_lexer_peek_token (parser->lexer)))
15848                 {
15849                   /* The grammar does not allow a pure-specifier to be
15850                      used when a member function is defined.  (It is
15851                      possible that this fact is an oversight in the
15852                      standard, since a pure function may be defined
15853                      outside of the class-specifier.  */
15854                   if (initializer)
15855                     error ("%Hpure-specifier on function-definition",
15856                            &initializer_token_start->location);
15857                   decl = cp_parser_save_member_function_body (parser,
15858                                                               &decl_specifiers,
15859                                                               declarator,
15860                                                               attributes);
15861                   /* If the member was not a friend, declare it here.  */
15862                   if (!friend_p)
15863                     finish_member_declaration (decl);
15864                   /* Peek at the next token.  */
15865                   token = cp_lexer_peek_token (parser->lexer);
15866                   /* If the next token is a semicolon, consume it.  */
15867                   if (token->type == CPP_SEMICOLON)
15868                     cp_lexer_consume_token (parser->lexer);
15869                   return;
15870                 }
15871               else
15872                 if (declarator->kind == cdk_function)
15873                   declarator->id_loc = token->location;
15874                 /* Create the declaration.  */
15875                 decl = grokfield (declarator, &decl_specifiers,
15876                                   initializer, /*init_const_expr_p=*/true,
15877                                   asm_specification,
15878                                   attributes);
15879             }
15880
15881           /* Reset PREFIX_ATTRIBUTES.  */
15882           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15883             attributes = TREE_CHAIN (attributes);
15884           if (attributes)
15885             TREE_CHAIN (attributes) = NULL_TREE;
15886
15887           /* If there is any qualification still in effect, clear it
15888              now; we will be starting fresh with the next declarator.  */
15889           parser->scope = NULL_TREE;
15890           parser->qualifying_scope = NULL_TREE;
15891           parser->object_scope = NULL_TREE;
15892           /* If it's a `,', then there are more declarators.  */
15893           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15894             cp_lexer_consume_token (parser->lexer);
15895           /* If the next token isn't a `;', then we have a parse error.  */
15896           else if (cp_lexer_next_token_is_not (parser->lexer,
15897                                                CPP_SEMICOLON))
15898             {
15899               cp_parser_error (parser, "expected %<;%>");
15900               /* Skip tokens until we find a `;'.  */
15901               cp_parser_skip_to_end_of_statement (parser);
15902
15903               break;
15904             }
15905
15906           if (decl)
15907             {
15908               /* Add DECL to the list of members.  */
15909               if (!friend_p)
15910                 finish_member_declaration (decl);
15911
15912               if (TREE_CODE (decl) == FUNCTION_DECL)
15913                 cp_parser_save_default_args (parser, decl);
15914             }
15915         }
15916     }
15917
15918   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15919 }
15920
15921 /* Parse a pure-specifier.
15922
15923    pure-specifier:
15924      = 0
15925
15926    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15927    Otherwise, ERROR_MARK_NODE is returned.  */
15928
15929 static tree
15930 cp_parser_pure_specifier (cp_parser* parser)
15931 {
15932   cp_token *token;
15933
15934   /* Look for the `=' token.  */
15935   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15936     return error_mark_node;
15937   /* Look for the `0' token.  */
15938   token = cp_lexer_consume_token (parser->lexer);
15939
15940   /* Accept = default or = delete in c++0x mode.  */
15941   if (token->keyword == RID_DEFAULT
15942       || token->keyword == RID_DELETE)
15943     {
15944       maybe_warn_cpp0x ("defaulted and deleted functions");
15945       return token->u.value;
15946     }
15947
15948   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15949   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15950     {
15951       cp_parser_error (parser,
15952                        "invalid pure specifier (only %<= 0%> is allowed)");
15953       cp_parser_skip_to_end_of_statement (parser);
15954       return error_mark_node;
15955     }
15956   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15957     {
15958       error ("%Htemplates may not be %<virtual%>", &token->location);
15959       return error_mark_node;
15960     }
15961
15962   return integer_zero_node;
15963 }
15964
15965 /* Parse a constant-initializer.
15966
15967    constant-initializer:
15968      = constant-expression
15969
15970    Returns a representation of the constant-expression.  */
15971
15972 static tree
15973 cp_parser_constant_initializer (cp_parser* parser)
15974 {
15975   /* Look for the `=' token.  */
15976   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15977     return error_mark_node;
15978
15979   /* It is invalid to write:
15980
15981        struct S { static const int i = { 7 }; };
15982
15983      */
15984   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15985     {
15986       cp_parser_error (parser,
15987                        "a brace-enclosed initializer is not allowed here");
15988       /* Consume the opening brace.  */
15989       cp_lexer_consume_token (parser->lexer);
15990       /* Skip the initializer.  */
15991       cp_parser_skip_to_closing_brace (parser);
15992       /* Look for the trailing `}'.  */
15993       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15994
15995       return error_mark_node;
15996     }
15997
15998   return cp_parser_constant_expression (parser,
15999                                         /*allow_non_constant=*/false,
16000                                         NULL);
16001 }
16002
16003 /* Derived classes [gram.class.derived] */
16004
16005 /* Parse a base-clause.
16006
16007    base-clause:
16008      : base-specifier-list
16009
16010    base-specifier-list:
16011      base-specifier ... [opt]
16012      base-specifier-list , base-specifier ... [opt]
16013
16014    Returns a TREE_LIST representing the base-classes, in the order in
16015    which they were declared.  The representation of each node is as
16016    described by cp_parser_base_specifier.
16017
16018    In the case that no bases are specified, this function will return
16019    NULL_TREE, not ERROR_MARK_NODE.  */
16020
16021 static tree
16022 cp_parser_base_clause (cp_parser* parser)
16023 {
16024   tree bases = NULL_TREE;
16025
16026   /* Look for the `:' that begins the list.  */
16027   cp_parser_require (parser, CPP_COLON, "%<:%>");
16028
16029   /* Scan the base-specifier-list.  */
16030   while (true)
16031     {
16032       cp_token *token;
16033       tree base;
16034       bool pack_expansion_p = false;
16035
16036       /* Look for the base-specifier.  */
16037       base = cp_parser_base_specifier (parser);
16038       /* Look for the (optional) ellipsis. */
16039       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16040         {
16041           /* Consume the `...'. */
16042           cp_lexer_consume_token (parser->lexer);
16043
16044           pack_expansion_p = true;
16045         }
16046
16047       /* Add BASE to the front of the list.  */
16048       if (base != error_mark_node)
16049         {
16050           if (pack_expansion_p)
16051             /* Make this a pack expansion type. */
16052             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16053           
16054
16055           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16056             {
16057               TREE_CHAIN (base) = bases;
16058               bases = base;
16059             }
16060         }
16061       /* Peek at the next token.  */
16062       token = cp_lexer_peek_token (parser->lexer);
16063       /* If it's not a comma, then the list is complete.  */
16064       if (token->type != CPP_COMMA)
16065         break;
16066       /* Consume the `,'.  */
16067       cp_lexer_consume_token (parser->lexer);
16068     }
16069
16070   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16071      base class had a qualified name.  However, the next name that
16072      appears is certainly not qualified.  */
16073   parser->scope = NULL_TREE;
16074   parser->qualifying_scope = NULL_TREE;
16075   parser->object_scope = NULL_TREE;
16076
16077   return nreverse (bases);
16078 }
16079
16080 /* Parse a base-specifier.
16081
16082    base-specifier:
16083      :: [opt] nested-name-specifier [opt] class-name
16084      virtual access-specifier [opt] :: [opt] nested-name-specifier
16085        [opt] class-name
16086      access-specifier virtual [opt] :: [opt] nested-name-specifier
16087        [opt] class-name
16088
16089    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16090    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16091    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16092    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16093
16094 static tree
16095 cp_parser_base_specifier (cp_parser* parser)
16096 {
16097   cp_token *token;
16098   bool done = false;
16099   bool virtual_p = false;
16100   bool duplicate_virtual_error_issued_p = false;
16101   bool duplicate_access_error_issued_p = false;
16102   bool class_scope_p, template_p;
16103   tree access = access_default_node;
16104   tree type;
16105
16106   /* Process the optional `virtual' and `access-specifier'.  */
16107   while (!done)
16108     {
16109       /* Peek at the next token.  */
16110       token = cp_lexer_peek_token (parser->lexer);
16111       /* Process `virtual'.  */
16112       switch (token->keyword)
16113         {
16114         case RID_VIRTUAL:
16115           /* If `virtual' appears more than once, issue an error.  */
16116           if (virtual_p && !duplicate_virtual_error_issued_p)
16117             {
16118               cp_parser_error (parser,
16119                                "%<virtual%> specified more than once in base-specified");
16120               duplicate_virtual_error_issued_p = true;
16121             }
16122
16123           virtual_p = true;
16124
16125           /* Consume the `virtual' token.  */
16126           cp_lexer_consume_token (parser->lexer);
16127
16128           break;
16129
16130         case RID_PUBLIC:
16131         case RID_PROTECTED:
16132         case RID_PRIVATE:
16133           /* If more than one access specifier appears, issue an
16134              error.  */
16135           if (access != access_default_node
16136               && !duplicate_access_error_issued_p)
16137             {
16138               cp_parser_error (parser,
16139                                "more than one access specifier in base-specified");
16140               duplicate_access_error_issued_p = true;
16141             }
16142
16143           access = ridpointers[(int) token->keyword];
16144
16145           /* Consume the access-specifier.  */
16146           cp_lexer_consume_token (parser->lexer);
16147
16148           break;
16149
16150         default:
16151           done = true;
16152           break;
16153         }
16154     }
16155   /* It is not uncommon to see programs mechanically, erroneously, use
16156      the 'typename' keyword to denote (dependent) qualified types
16157      as base classes.  */
16158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16159     {
16160       token = cp_lexer_peek_token (parser->lexer);
16161       if (!processing_template_decl)
16162         error ("%Hkeyword %<typename%> not allowed outside of templates",
16163                &token->location);
16164       else
16165         error ("%Hkeyword %<typename%> not allowed in this context "
16166                "(the base class is implicitly a type)",
16167                &token->location);
16168       cp_lexer_consume_token (parser->lexer);
16169     }
16170
16171   /* Look for the optional `::' operator.  */
16172   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16173   /* Look for the nested-name-specifier.  The simplest way to
16174      implement:
16175
16176        [temp.res]
16177
16178        The keyword `typename' is not permitted in a base-specifier or
16179        mem-initializer; in these contexts a qualified name that
16180        depends on a template-parameter is implicitly assumed to be a
16181        type name.
16182
16183      is to pretend that we have seen the `typename' keyword at this
16184      point.  */
16185   cp_parser_nested_name_specifier_opt (parser,
16186                                        /*typename_keyword_p=*/true,
16187                                        /*check_dependency_p=*/true,
16188                                        typename_type,
16189                                        /*is_declaration=*/true);
16190   /* If the base class is given by a qualified name, assume that names
16191      we see are type names or templates, as appropriate.  */
16192   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16193   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16194
16195   /* Finally, look for the class-name.  */
16196   type = cp_parser_class_name (parser,
16197                                class_scope_p,
16198                                template_p,
16199                                typename_type,
16200                                /*check_dependency_p=*/true,
16201                                /*class_head_p=*/false,
16202                                /*is_declaration=*/true);
16203
16204   if (type == error_mark_node)
16205     return error_mark_node;
16206
16207   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16208 }
16209
16210 /* Exception handling [gram.exception] */
16211
16212 /* Parse an (optional) exception-specification.
16213
16214    exception-specification:
16215      throw ( type-id-list [opt] )
16216
16217    Returns a TREE_LIST representing the exception-specification.  The
16218    TREE_VALUE of each node is a type.  */
16219
16220 static tree
16221 cp_parser_exception_specification_opt (cp_parser* parser)
16222 {
16223   cp_token *token;
16224   tree type_id_list;
16225
16226   /* Peek at the next token.  */
16227   token = cp_lexer_peek_token (parser->lexer);
16228   /* If it's not `throw', then there's no exception-specification.  */
16229   if (!cp_parser_is_keyword (token, RID_THROW))
16230     return NULL_TREE;
16231
16232   /* Consume the `throw'.  */
16233   cp_lexer_consume_token (parser->lexer);
16234
16235   /* Look for the `('.  */
16236   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16237
16238   /* Peek at the next token.  */
16239   token = cp_lexer_peek_token (parser->lexer);
16240   /* If it's not a `)', then there is a type-id-list.  */
16241   if (token->type != CPP_CLOSE_PAREN)
16242     {
16243       const char *saved_message;
16244
16245       /* Types may not be defined in an exception-specification.  */
16246       saved_message = parser->type_definition_forbidden_message;
16247       parser->type_definition_forbidden_message
16248         = "types may not be defined in an exception-specification";
16249       /* Parse the type-id-list.  */
16250       type_id_list = cp_parser_type_id_list (parser);
16251       /* Restore the saved message.  */
16252       parser->type_definition_forbidden_message = saved_message;
16253     }
16254   else
16255     type_id_list = empty_except_spec;
16256
16257   /* Look for the `)'.  */
16258   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16259
16260   return type_id_list;
16261 }
16262
16263 /* Parse an (optional) type-id-list.
16264
16265    type-id-list:
16266      type-id ... [opt]
16267      type-id-list , type-id ... [opt]
16268
16269    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16270    in the order that the types were presented.  */
16271
16272 static tree
16273 cp_parser_type_id_list (cp_parser* parser)
16274 {
16275   tree types = NULL_TREE;
16276
16277   while (true)
16278     {
16279       cp_token *token;
16280       tree type;
16281
16282       /* Get the next type-id.  */
16283       type = cp_parser_type_id (parser);
16284       /* Parse the optional ellipsis. */
16285       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16286         {
16287           /* Consume the `...'. */
16288           cp_lexer_consume_token (parser->lexer);
16289
16290           /* Turn the type into a pack expansion expression. */
16291           type = make_pack_expansion (type);
16292         }
16293       /* Add it to the list.  */
16294       types = add_exception_specifier (types, type, /*complain=*/1);
16295       /* Peek at the next token.  */
16296       token = cp_lexer_peek_token (parser->lexer);
16297       /* If it is not a `,', we are done.  */
16298       if (token->type != CPP_COMMA)
16299         break;
16300       /* Consume the `,'.  */
16301       cp_lexer_consume_token (parser->lexer);
16302     }
16303
16304   return nreverse (types);
16305 }
16306
16307 /* Parse a try-block.
16308
16309    try-block:
16310      try compound-statement handler-seq  */
16311
16312 static tree
16313 cp_parser_try_block (cp_parser* parser)
16314 {
16315   tree try_block;
16316
16317   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16318   try_block = begin_try_block ();
16319   cp_parser_compound_statement (parser, NULL, true);
16320   finish_try_block (try_block);
16321   cp_parser_handler_seq (parser);
16322   finish_handler_sequence (try_block);
16323
16324   return try_block;
16325 }
16326
16327 /* Parse a function-try-block.
16328
16329    function-try-block:
16330      try ctor-initializer [opt] function-body handler-seq  */
16331
16332 static bool
16333 cp_parser_function_try_block (cp_parser* parser)
16334 {
16335   tree compound_stmt;
16336   tree try_block;
16337   bool ctor_initializer_p;
16338
16339   /* Look for the `try' keyword.  */
16340   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16341     return false;
16342   /* Let the rest of the front end know where we are.  */
16343   try_block = begin_function_try_block (&compound_stmt);
16344   /* Parse the function-body.  */
16345   ctor_initializer_p
16346     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16347   /* We're done with the `try' part.  */
16348   finish_function_try_block (try_block);
16349   /* Parse the handlers.  */
16350   cp_parser_handler_seq (parser);
16351   /* We're done with the handlers.  */
16352   finish_function_handler_sequence (try_block, compound_stmt);
16353
16354   return ctor_initializer_p;
16355 }
16356
16357 /* Parse a handler-seq.
16358
16359    handler-seq:
16360      handler handler-seq [opt]  */
16361
16362 static void
16363 cp_parser_handler_seq (cp_parser* parser)
16364 {
16365   while (true)
16366     {
16367       cp_token *token;
16368
16369       /* Parse the handler.  */
16370       cp_parser_handler (parser);
16371       /* Peek at the next token.  */
16372       token = cp_lexer_peek_token (parser->lexer);
16373       /* If it's not `catch' then there are no more handlers.  */
16374       if (!cp_parser_is_keyword (token, RID_CATCH))
16375         break;
16376     }
16377 }
16378
16379 /* Parse a handler.
16380
16381    handler:
16382      catch ( exception-declaration ) compound-statement  */
16383
16384 static void
16385 cp_parser_handler (cp_parser* parser)
16386 {
16387   tree handler;
16388   tree declaration;
16389
16390   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16391   handler = begin_handler ();
16392   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16393   declaration = cp_parser_exception_declaration (parser);
16394   finish_handler_parms (declaration, handler);
16395   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16396   cp_parser_compound_statement (parser, NULL, false);
16397   finish_handler (handler);
16398 }
16399
16400 /* Parse an exception-declaration.
16401
16402    exception-declaration:
16403      type-specifier-seq declarator
16404      type-specifier-seq abstract-declarator
16405      type-specifier-seq
16406      ...
16407
16408    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16409    ellipsis variant is used.  */
16410
16411 static tree
16412 cp_parser_exception_declaration (cp_parser* parser)
16413 {
16414   cp_decl_specifier_seq type_specifiers;
16415   cp_declarator *declarator;
16416   const char *saved_message;
16417
16418   /* If it's an ellipsis, it's easy to handle.  */
16419   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16420     {
16421       /* Consume the `...' token.  */
16422       cp_lexer_consume_token (parser->lexer);
16423       return NULL_TREE;
16424     }
16425
16426   /* Types may not be defined in exception-declarations.  */
16427   saved_message = parser->type_definition_forbidden_message;
16428   parser->type_definition_forbidden_message
16429     = "types may not be defined in exception-declarations";
16430
16431   /* Parse the type-specifier-seq.  */
16432   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16433                                 &type_specifiers);
16434   /* If it's a `)', then there is no declarator.  */
16435   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16436     declarator = NULL;
16437   else
16438     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16439                                        /*ctor_dtor_or_conv_p=*/NULL,
16440                                        /*parenthesized_p=*/NULL,
16441                                        /*member_p=*/false);
16442
16443   /* Restore the saved message.  */
16444   parser->type_definition_forbidden_message = saved_message;
16445
16446   if (!type_specifiers.any_specifiers_p)
16447     return error_mark_node;
16448
16449   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16450 }
16451
16452 /* Parse a throw-expression.
16453
16454    throw-expression:
16455      throw assignment-expression [opt]
16456
16457    Returns a THROW_EXPR representing the throw-expression.  */
16458
16459 static tree
16460 cp_parser_throw_expression (cp_parser* parser)
16461 {
16462   tree expression;
16463   cp_token* token;
16464
16465   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16466   token = cp_lexer_peek_token (parser->lexer);
16467   /* Figure out whether or not there is an assignment-expression
16468      following the "throw" keyword.  */
16469   if (token->type == CPP_COMMA
16470       || token->type == CPP_SEMICOLON
16471       || token->type == CPP_CLOSE_PAREN
16472       || token->type == CPP_CLOSE_SQUARE
16473       || token->type == CPP_CLOSE_BRACE
16474       || token->type == CPP_COLON)
16475     expression = NULL_TREE;
16476   else
16477     expression = cp_parser_assignment_expression (parser,
16478                                                   /*cast_p=*/false);
16479
16480   return build_throw (expression);
16481 }
16482
16483 /* GNU Extensions */
16484
16485 /* Parse an (optional) asm-specification.
16486
16487    asm-specification:
16488      asm ( string-literal )
16489
16490    If the asm-specification is present, returns a STRING_CST
16491    corresponding to the string-literal.  Otherwise, returns
16492    NULL_TREE.  */
16493
16494 static tree
16495 cp_parser_asm_specification_opt (cp_parser* parser)
16496 {
16497   cp_token *token;
16498   tree asm_specification;
16499
16500   /* Peek at the next token.  */
16501   token = cp_lexer_peek_token (parser->lexer);
16502   /* If the next token isn't the `asm' keyword, then there's no
16503      asm-specification.  */
16504   if (!cp_parser_is_keyword (token, RID_ASM))
16505     return NULL_TREE;
16506
16507   /* Consume the `asm' token.  */
16508   cp_lexer_consume_token (parser->lexer);
16509   /* Look for the `('.  */
16510   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16511
16512   /* Look for the string-literal.  */
16513   asm_specification = cp_parser_string_literal (parser, false, false);
16514
16515   /* Look for the `)'.  */
16516   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16517
16518   return asm_specification;
16519 }
16520
16521 /* Parse an asm-operand-list.
16522
16523    asm-operand-list:
16524      asm-operand
16525      asm-operand-list , asm-operand
16526
16527    asm-operand:
16528      string-literal ( expression )
16529      [ string-literal ] string-literal ( expression )
16530
16531    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16532    each node is the expression.  The TREE_PURPOSE is itself a
16533    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16534    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16535    is a STRING_CST for the string literal before the parenthesis. Returns
16536    ERROR_MARK_NODE if any of the operands are invalid.  */
16537
16538 static tree
16539 cp_parser_asm_operand_list (cp_parser* parser)
16540 {
16541   tree asm_operands = NULL_TREE;
16542   bool invalid_operands = false;
16543
16544   while (true)
16545     {
16546       tree string_literal;
16547       tree expression;
16548       tree name;
16549
16550       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16551         {
16552           /* Consume the `[' token.  */
16553           cp_lexer_consume_token (parser->lexer);
16554           /* Read the operand name.  */
16555           name = cp_parser_identifier (parser);
16556           if (name != error_mark_node)
16557             name = build_string (IDENTIFIER_LENGTH (name),
16558                                  IDENTIFIER_POINTER (name));
16559           /* Look for the closing `]'.  */
16560           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16561         }
16562       else
16563         name = NULL_TREE;
16564       /* Look for the string-literal.  */
16565       string_literal = cp_parser_string_literal (parser, false, false);
16566
16567       /* Look for the `('.  */
16568       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16569       /* Parse the expression.  */
16570       expression = cp_parser_expression (parser, /*cast_p=*/false);
16571       /* Look for the `)'.  */
16572       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16573
16574       if (name == error_mark_node 
16575           || string_literal == error_mark_node 
16576           || expression == error_mark_node)
16577         invalid_operands = true;
16578
16579       /* Add this operand to the list.  */
16580       asm_operands = tree_cons (build_tree_list (name, string_literal),
16581                                 expression,
16582                                 asm_operands);
16583       /* If the next token is not a `,', there are no more
16584          operands.  */
16585       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16586         break;
16587       /* Consume the `,'.  */
16588       cp_lexer_consume_token (parser->lexer);
16589     }
16590
16591   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16592 }
16593
16594 /* Parse an asm-clobber-list.
16595
16596    asm-clobber-list:
16597      string-literal
16598      asm-clobber-list , string-literal
16599
16600    Returns a TREE_LIST, indicating the clobbers in the order that they
16601    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16602
16603 static tree
16604 cp_parser_asm_clobber_list (cp_parser* parser)
16605 {
16606   tree clobbers = NULL_TREE;
16607
16608   while (true)
16609     {
16610       tree string_literal;
16611
16612       /* Look for the string literal.  */
16613       string_literal = cp_parser_string_literal (parser, false, false);
16614       /* Add it to the list.  */
16615       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16616       /* If the next token is not a `,', then the list is
16617          complete.  */
16618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16619         break;
16620       /* Consume the `,' token.  */
16621       cp_lexer_consume_token (parser->lexer);
16622     }
16623
16624   return clobbers;
16625 }
16626
16627 /* Parse an (optional) series of attributes.
16628
16629    attributes:
16630      attributes attribute
16631
16632    attribute:
16633      __attribute__ (( attribute-list [opt] ))
16634
16635    The return value is as for cp_parser_attribute_list.  */
16636
16637 static tree
16638 cp_parser_attributes_opt (cp_parser* parser)
16639 {
16640   tree attributes = NULL_TREE;
16641
16642   while (true)
16643     {
16644       cp_token *token;
16645       tree attribute_list;
16646
16647       /* Peek at the next token.  */
16648       token = cp_lexer_peek_token (parser->lexer);
16649       /* If it's not `__attribute__', then we're done.  */
16650       if (token->keyword != RID_ATTRIBUTE)
16651         break;
16652
16653       /* Consume the `__attribute__' keyword.  */
16654       cp_lexer_consume_token (parser->lexer);
16655       /* Look for the two `(' tokens.  */
16656       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16657       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16658
16659       /* Peek at the next token.  */
16660       token = cp_lexer_peek_token (parser->lexer);
16661       if (token->type != CPP_CLOSE_PAREN)
16662         /* Parse the attribute-list.  */
16663         attribute_list = cp_parser_attribute_list (parser);
16664       else
16665         /* If the next token is a `)', then there is no attribute
16666            list.  */
16667         attribute_list = NULL;
16668
16669       /* Look for the two `)' tokens.  */
16670       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16671       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16672
16673       /* Add these new attributes to the list.  */
16674       attributes = chainon (attributes, attribute_list);
16675     }
16676
16677   return attributes;
16678 }
16679
16680 /* Parse an attribute-list.
16681
16682    attribute-list:
16683      attribute
16684      attribute-list , attribute
16685
16686    attribute:
16687      identifier
16688      identifier ( identifier )
16689      identifier ( identifier , expression-list )
16690      identifier ( expression-list )
16691
16692    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16693    to an attribute.  The TREE_PURPOSE of each node is the identifier
16694    indicating which attribute is in use.  The TREE_VALUE represents
16695    the arguments, if any.  */
16696
16697 static tree
16698 cp_parser_attribute_list (cp_parser* parser)
16699 {
16700   tree attribute_list = NULL_TREE;
16701   bool save_translate_strings_p = parser->translate_strings_p;
16702
16703   parser->translate_strings_p = false;
16704   while (true)
16705     {
16706       cp_token *token;
16707       tree identifier;
16708       tree attribute;
16709
16710       /* Look for the identifier.  We also allow keywords here; for
16711          example `__attribute__ ((const))' is legal.  */
16712       token = cp_lexer_peek_token (parser->lexer);
16713       if (token->type == CPP_NAME
16714           || token->type == CPP_KEYWORD)
16715         {
16716           tree arguments = NULL_TREE;
16717
16718           /* Consume the token.  */
16719           token = cp_lexer_consume_token (parser->lexer);
16720
16721           /* Save away the identifier that indicates which attribute
16722              this is.  */
16723           identifier = token->u.value;
16724           attribute = build_tree_list (identifier, NULL_TREE);
16725
16726           /* Peek at the next token.  */
16727           token = cp_lexer_peek_token (parser->lexer);
16728           /* If it's an `(', then parse the attribute arguments.  */
16729           if (token->type == CPP_OPEN_PAREN)
16730             {
16731               arguments = cp_parser_parenthesized_expression_list
16732                           (parser, true, /*cast_p=*/false,
16733                            /*allow_expansion_p=*/false,
16734                            /*non_constant_p=*/NULL);
16735               /* Save the arguments away.  */
16736               TREE_VALUE (attribute) = arguments;
16737             }
16738
16739           if (arguments != error_mark_node)
16740             {
16741               /* Add this attribute to the list.  */
16742               TREE_CHAIN (attribute) = attribute_list;
16743               attribute_list = attribute;
16744             }
16745
16746           token = cp_lexer_peek_token (parser->lexer);
16747         }
16748       /* Now, look for more attributes.  If the next token isn't a
16749          `,', we're done.  */
16750       if (token->type != CPP_COMMA)
16751         break;
16752
16753       /* Consume the comma and keep going.  */
16754       cp_lexer_consume_token (parser->lexer);
16755     }
16756   parser->translate_strings_p = save_translate_strings_p;
16757
16758   /* We built up the list in reverse order.  */
16759   return nreverse (attribute_list);
16760 }
16761
16762 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16763    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16764    current value of the PEDANTIC flag, regardless of whether or not
16765    the `__extension__' keyword is present.  The caller is responsible
16766    for restoring the value of the PEDANTIC flag.  */
16767
16768 static bool
16769 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16770 {
16771   /* Save the old value of the PEDANTIC flag.  */
16772   *saved_pedantic = pedantic;
16773
16774   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16775     {
16776       /* Consume the `__extension__' token.  */
16777       cp_lexer_consume_token (parser->lexer);
16778       /* We're not being pedantic while the `__extension__' keyword is
16779          in effect.  */
16780       pedantic = 0;
16781
16782       return true;
16783     }
16784
16785   return false;
16786 }
16787
16788 /* Parse a label declaration.
16789
16790    label-declaration:
16791      __label__ label-declarator-seq ;
16792
16793    label-declarator-seq:
16794      identifier , label-declarator-seq
16795      identifier  */
16796
16797 static void
16798 cp_parser_label_declaration (cp_parser* parser)
16799 {
16800   /* Look for the `__label__' keyword.  */
16801   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16802
16803   while (true)
16804     {
16805       tree identifier;
16806
16807       /* Look for an identifier.  */
16808       identifier = cp_parser_identifier (parser);
16809       /* If we failed, stop.  */
16810       if (identifier == error_mark_node)
16811         break;
16812       /* Declare it as a label.  */
16813       finish_label_decl (identifier);
16814       /* If the next token is a `;', stop.  */
16815       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16816         break;
16817       /* Look for the `,' separating the label declarations.  */
16818       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16819     }
16820
16821   /* Look for the final `;'.  */
16822   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16823 }
16824
16825 /* Support Functions */
16826
16827 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16828    NAME should have one of the representations used for an
16829    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16830    is returned.  If PARSER->SCOPE is a dependent type, then a
16831    SCOPE_REF is returned.
16832
16833    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16834    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16835    was formed.  Abstractly, such entities should not be passed to this
16836    function, because they do not need to be looked up, but it is
16837    simpler to check for this special case here, rather than at the
16838    call-sites.
16839
16840    In cases not explicitly covered above, this function returns a
16841    DECL, OVERLOAD, or baselink representing the result of the lookup.
16842    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16843    is returned.
16844
16845    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16846    (e.g., "struct") that was used.  In that case bindings that do not
16847    refer to types are ignored.
16848
16849    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16850    ignored.
16851
16852    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16853    are ignored.
16854
16855    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16856    types.
16857
16858    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16859    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16860    NULL_TREE otherwise.  */
16861
16862 static tree
16863 cp_parser_lookup_name (cp_parser *parser, tree name,
16864                        enum tag_types tag_type,
16865                        bool is_template,
16866                        bool is_namespace,
16867                        bool check_dependency,
16868                        tree *ambiguous_decls,
16869                        location_t name_location)
16870 {
16871   int flags = 0;
16872   tree decl;
16873   tree object_type = parser->context->object_type;
16874
16875   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16876     flags |= LOOKUP_COMPLAIN;
16877
16878   /* Assume that the lookup will be unambiguous.  */
16879   if (ambiguous_decls)
16880     *ambiguous_decls = NULL_TREE;
16881
16882   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16883      no longer valid.  Note that if we are parsing tentatively, and
16884      the parse fails, OBJECT_TYPE will be automatically restored.  */
16885   parser->context->object_type = NULL_TREE;
16886
16887   if (name == error_mark_node)
16888     return error_mark_node;
16889
16890   /* A template-id has already been resolved; there is no lookup to
16891      do.  */
16892   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16893     return name;
16894   if (BASELINK_P (name))
16895     {
16896       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16897                   == TEMPLATE_ID_EXPR);
16898       return name;
16899     }
16900
16901   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16902      it should already have been checked to make sure that the name
16903      used matches the type being destroyed.  */
16904   if (TREE_CODE (name) == BIT_NOT_EXPR)
16905     {
16906       tree type;
16907
16908       /* Figure out to which type this destructor applies.  */
16909       if (parser->scope)
16910         type = parser->scope;
16911       else if (object_type)
16912         type = object_type;
16913       else
16914         type = current_class_type;
16915       /* If that's not a class type, there is no destructor.  */
16916       if (!type || !CLASS_TYPE_P (type))
16917         return error_mark_node;
16918       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16919         lazily_declare_fn (sfk_destructor, type);
16920       if (!CLASSTYPE_DESTRUCTORS (type))
16921           return error_mark_node;
16922       /* If it was a class type, return the destructor.  */
16923       return CLASSTYPE_DESTRUCTORS (type);
16924     }
16925
16926   /* By this point, the NAME should be an ordinary identifier.  If
16927      the id-expression was a qualified name, the qualifying scope is
16928      stored in PARSER->SCOPE at this point.  */
16929   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16930
16931   /* Perform the lookup.  */
16932   if (parser->scope)
16933     {
16934       bool dependent_p;
16935
16936       if (parser->scope == error_mark_node)
16937         return error_mark_node;
16938
16939       /* If the SCOPE is dependent, the lookup must be deferred until
16940          the template is instantiated -- unless we are explicitly
16941          looking up names in uninstantiated templates.  Even then, we
16942          cannot look up the name if the scope is not a class type; it
16943          might, for example, be a template type parameter.  */
16944       dependent_p = (TYPE_P (parser->scope)
16945                      && !(parser->in_declarator_p
16946                           && currently_open_class (parser->scope))
16947                      && dependent_type_p (parser->scope));
16948       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16949            && dependent_p)
16950         {
16951           if (tag_type)
16952             {
16953               tree type;
16954
16955               /* The resolution to Core Issue 180 says that `struct
16956                  A::B' should be considered a type-name, even if `A'
16957                  is dependent.  */
16958               type = make_typename_type (parser->scope, name, tag_type,
16959                                          /*complain=*/tf_error);
16960               decl = TYPE_NAME (type);
16961             }
16962           else if (is_template
16963                    && (cp_parser_next_token_ends_template_argument_p (parser)
16964                        || cp_lexer_next_token_is (parser->lexer,
16965                                                   CPP_CLOSE_PAREN)))
16966             decl = make_unbound_class_template (parser->scope,
16967                                                 name, NULL_TREE,
16968                                                 /*complain=*/tf_error);
16969           else
16970             decl = build_qualified_name (/*type=*/NULL_TREE,
16971                                          parser->scope, name,
16972                                          is_template);
16973         }
16974       else
16975         {
16976           tree pushed_scope = NULL_TREE;
16977
16978           /* If PARSER->SCOPE is a dependent type, then it must be a
16979              class type, and we must not be checking dependencies;
16980              otherwise, we would have processed this lookup above.  So
16981              that PARSER->SCOPE is not considered a dependent base by
16982              lookup_member, we must enter the scope here.  */
16983           if (dependent_p)
16984             pushed_scope = push_scope (parser->scope);
16985           /* If the PARSER->SCOPE is a template specialization, it
16986              may be instantiated during name lookup.  In that case,
16987              errors may be issued.  Even if we rollback the current
16988              tentative parse, those errors are valid.  */
16989           decl = lookup_qualified_name (parser->scope, name,
16990                                         tag_type != none_type,
16991                                         /*complain=*/true);
16992
16993           /* If we have a single function from a using decl, pull it out.  */
16994           if (decl
16995               && TREE_CODE (decl) == OVERLOAD
16996               && !really_overloaded_fn (decl))
16997             decl = OVL_FUNCTION (decl);
16998
16999           if (pushed_scope)
17000             pop_scope (pushed_scope);
17001         }
17002       parser->qualifying_scope = parser->scope;
17003       parser->object_scope = NULL_TREE;
17004     }
17005   else if (object_type)
17006     {
17007       tree object_decl = NULL_TREE;
17008       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17009          OBJECT_TYPE is not a class.  */
17010       if (CLASS_TYPE_P (object_type))
17011         /* If the OBJECT_TYPE is a template specialization, it may
17012            be instantiated during name lookup.  In that case, errors
17013            may be issued.  Even if we rollback the current tentative
17014            parse, those errors are valid.  */
17015         object_decl = lookup_member (object_type,
17016                                      name,
17017                                      /*protect=*/0,
17018                                      tag_type != none_type);
17019       /* Look it up in the enclosing context, too.  */
17020       decl = lookup_name_real (name, tag_type != none_type,
17021                                /*nonclass=*/0,
17022                                /*block_p=*/true, is_namespace, flags);
17023       parser->object_scope = object_type;
17024       parser->qualifying_scope = NULL_TREE;
17025       if (object_decl)
17026         decl = object_decl;
17027     }
17028   else
17029     {
17030       decl = lookup_name_real (name, tag_type != none_type,
17031                                /*nonclass=*/0,
17032                                /*block_p=*/true, is_namespace, flags);
17033       parser->qualifying_scope = NULL_TREE;
17034       parser->object_scope = NULL_TREE;
17035     }
17036
17037   /* If the lookup failed, let our caller know.  */
17038   if (!decl || decl == error_mark_node)
17039     return error_mark_node;
17040
17041   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17042   if (TREE_CODE (decl) == TREE_LIST)
17043     {
17044       if (ambiguous_decls)
17045         *ambiguous_decls = decl;
17046       /* The error message we have to print is too complicated for
17047          cp_parser_error, so we incorporate its actions directly.  */
17048       if (!cp_parser_simulate_error (parser))
17049         {
17050           error ("%Hreference to %qD is ambiguous",
17051                  &name_location, name);
17052           print_candidates (decl);
17053         }
17054       return error_mark_node;
17055     }
17056
17057   gcc_assert (DECL_P (decl)
17058               || TREE_CODE (decl) == OVERLOAD
17059               || TREE_CODE (decl) == SCOPE_REF
17060               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17061               || BASELINK_P (decl));
17062
17063   /* If we have resolved the name of a member declaration, check to
17064      see if the declaration is accessible.  When the name resolves to
17065      set of overloaded functions, accessibility is checked when
17066      overload resolution is done.
17067
17068      During an explicit instantiation, access is not checked at all,
17069      as per [temp.explicit].  */
17070   if (DECL_P (decl))
17071     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17072
17073   return decl;
17074 }
17075
17076 /* Like cp_parser_lookup_name, but for use in the typical case where
17077    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17078    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17079
17080 static tree
17081 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17082 {
17083   return cp_parser_lookup_name (parser, name,
17084                                 none_type,
17085                                 /*is_template=*/false,
17086                                 /*is_namespace=*/false,
17087                                 /*check_dependency=*/true,
17088                                 /*ambiguous_decls=*/NULL,
17089                                 location);
17090 }
17091
17092 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17093    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17094    true, the DECL indicates the class being defined in a class-head,
17095    or declared in an elaborated-type-specifier.
17096
17097    Otherwise, return DECL.  */
17098
17099 static tree
17100 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17101 {
17102   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17103      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17104
17105        struct A {
17106          template <typename T> struct B;
17107        };
17108
17109        template <typename T> struct A::B {};
17110
17111      Similarly, in an elaborated-type-specifier:
17112
17113        namespace N { struct X{}; }
17114
17115        struct A {
17116          template <typename T> friend struct N::X;
17117        };
17118
17119      However, if the DECL refers to a class type, and we are in
17120      the scope of the class, then the name lookup automatically
17121      finds the TYPE_DECL created by build_self_reference rather
17122      than a TEMPLATE_DECL.  For example, in:
17123
17124        template <class T> struct S {
17125          S s;
17126        };
17127
17128      there is no need to handle such case.  */
17129
17130   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17131     return DECL_TEMPLATE_RESULT (decl);
17132
17133   return decl;
17134 }
17135
17136 /* If too many, or too few, template-parameter lists apply to the
17137    declarator, issue an error message.  Returns TRUE if all went well,
17138    and FALSE otherwise.  */
17139
17140 static bool
17141 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17142                                                 cp_declarator *declarator,
17143                                                 location_t declarator_location)
17144 {
17145   unsigned num_templates;
17146
17147   /* We haven't seen any classes that involve template parameters yet.  */
17148   num_templates = 0;
17149
17150   switch (declarator->kind)
17151     {
17152     case cdk_id:
17153       if (declarator->u.id.qualifying_scope)
17154         {
17155           tree scope;
17156           tree member;
17157
17158           scope = declarator->u.id.qualifying_scope;
17159           member = declarator->u.id.unqualified_name;
17160
17161           while (scope && CLASS_TYPE_P (scope))
17162             {
17163               /* You're supposed to have one `template <...>'
17164                  for every template class, but you don't need one
17165                  for a full specialization.  For example:
17166
17167                  template <class T> struct S{};
17168                  template <> struct S<int> { void f(); };
17169                  void S<int>::f () {}
17170
17171                  is correct; there shouldn't be a `template <>' for
17172                  the definition of `S<int>::f'.  */
17173               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17174                 /* If SCOPE does not have template information of any
17175                    kind, then it is not a template, nor is it nested
17176                    within a template.  */
17177                 break;
17178               if (explicit_class_specialization_p (scope))
17179                 break;
17180               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17181                 ++num_templates;
17182
17183               scope = TYPE_CONTEXT (scope);
17184             }
17185         }
17186       else if (TREE_CODE (declarator->u.id.unqualified_name)
17187                == TEMPLATE_ID_EXPR)
17188         /* If the DECLARATOR has the form `X<y>' then it uses one
17189            additional level of template parameters.  */
17190         ++num_templates;
17191
17192       return cp_parser_check_template_parameters (parser,
17193                                                   num_templates,
17194                                                   declarator_location);
17195
17196     case cdk_function:
17197     case cdk_array:
17198     case cdk_pointer:
17199     case cdk_reference:
17200     case cdk_ptrmem:
17201       return (cp_parser_check_declarator_template_parameters
17202               (parser, declarator->declarator, declarator_location));
17203
17204     case cdk_error:
17205       return true;
17206
17207     default:
17208       gcc_unreachable ();
17209     }
17210   return false;
17211 }
17212
17213 /* NUM_TEMPLATES were used in the current declaration.  If that is
17214    invalid, return FALSE and issue an error messages.  Otherwise,
17215    return TRUE.  */
17216
17217 static bool
17218 cp_parser_check_template_parameters (cp_parser* parser,
17219                                      unsigned num_templates,
17220                                      location_t location)
17221 {
17222   /* If there are more template classes than parameter lists, we have
17223      something like:
17224
17225        template <class T> void S<T>::R<T>::f ();  */
17226   if (parser->num_template_parameter_lists < num_templates)
17227     {
17228       error ("%Htoo few template-parameter-lists", &location);
17229       return false;
17230     }
17231   /* If there are the same number of template classes and parameter
17232      lists, that's OK.  */
17233   if (parser->num_template_parameter_lists == num_templates)
17234     return true;
17235   /* If there are more, but only one more, then we are referring to a
17236      member template.  That's OK too.  */
17237   if (parser->num_template_parameter_lists == num_templates + 1)
17238       return true;
17239   /* Otherwise, there are too many template parameter lists.  We have
17240      something like:
17241
17242      template <class T> template <class U> void S::f();  */
17243   error ("%Htoo many template-parameter-lists", &location);
17244   return false;
17245 }
17246
17247 /* Parse an optional `::' token indicating that the following name is
17248    from the global namespace.  If so, PARSER->SCOPE is set to the
17249    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17250    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17251    Returns the new value of PARSER->SCOPE, if the `::' token is
17252    present, and NULL_TREE otherwise.  */
17253
17254 static tree
17255 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17256 {
17257   cp_token *token;
17258
17259   /* Peek at the next token.  */
17260   token = cp_lexer_peek_token (parser->lexer);
17261   /* If we're looking at a `::' token then we're starting from the
17262      global namespace, not our current location.  */
17263   if (token->type == CPP_SCOPE)
17264     {
17265       /* Consume the `::' token.  */
17266       cp_lexer_consume_token (parser->lexer);
17267       /* Set the SCOPE so that we know where to start the lookup.  */
17268       parser->scope = global_namespace;
17269       parser->qualifying_scope = global_namespace;
17270       parser->object_scope = NULL_TREE;
17271
17272       return parser->scope;
17273     }
17274   else if (!current_scope_valid_p)
17275     {
17276       parser->scope = NULL_TREE;
17277       parser->qualifying_scope = NULL_TREE;
17278       parser->object_scope = NULL_TREE;
17279     }
17280
17281   return NULL_TREE;
17282 }
17283
17284 /* Returns TRUE if the upcoming token sequence is the start of a
17285    constructor declarator.  If FRIEND_P is true, the declarator is
17286    preceded by the `friend' specifier.  */
17287
17288 static bool
17289 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17290 {
17291   bool constructor_p;
17292   tree type_decl = NULL_TREE;
17293   bool nested_name_p;
17294   cp_token *next_token;
17295
17296   /* The common case is that this is not a constructor declarator, so
17297      try to avoid doing lots of work if at all possible.  It's not
17298      valid declare a constructor at function scope.  */
17299   if (parser->in_function_body)
17300     return false;
17301   /* And only certain tokens can begin a constructor declarator.  */
17302   next_token = cp_lexer_peek_token (parser->lexer);
17303   if (next_token->type != CPP_NAME
17304       && next_token->type != CPP_SCOPE
17305       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17306       && next_token->type != CPP_TEMPLATE_ID)
17307     return false;
17308
17309   /* Parse tentatively; we are going to roll back all of the tokens
17310      consumed here.  */
17311   cp_parser_parse_tentatively (parser);
17312   /* Assume that we are looking at a constructor declarator.  */
17313   constructor_p = true;
17314
17315   /* Look for the optional `::' operator.  */
17316   cp_parser_global_scope_opt (parser,
17317                               /*current_scope_valid_p=*/false);
17318   /* Look for the nested-name-specifier.  */
17319   nested_name_p
17320     = (cp_parser_nested_name_specifier_opt (parser,
17321                                             /*typename_keyword_p=*/false,
17322                                             /*check_dependency_p=*/false,
17323                                             /*type_p=*/false,
17324                                             /*is_declaration=*/false)
17325        != NULL_TREE);
17326   /* Outside of a class-specifier, there must be a
17327      nested-name-specifier.  */
17328   if (!nested_name_p &&
17329       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17330        || friend_p))
17331     constructor_p = false;
17332   /* If we still think that this might be a constructor-declarator,
17333      look for a class-name.  */
17334   if (constructor_p)
17335     {
17336       /* If we have:
17337
17338            template <typename T> struct S { S(); };
17339            template <typename T> S<T>::S ();
17340
17341          we must recognize that the nested `S' names a class.
17342          Similarly, for:
17343
17344            template <typename T> S<T>::S<T> ();
17345
17346          we must recognize that the nested `S' names a template.  */
17347       type_decl = cp_parser_class_name (parser,
17348                                         /*typename_keyword_p=*/false,
17349                                         /*template_keyword_p=*/false,
17350                                         none_type,
17351                                         /*check_dependency_p=*/false,
17352                                         /*class_head_p=*/false,
17353                                         /*is_declaration=*/false);
17354       /* If there was no class-name, then this is not a constructor.  */
17355       constructor_p = !cp_parser_error_occurred (parser);
17356     }
17357
17358   /* If we're still considering a constructor, we have to see a `(',
17359      to begin the parameter-declaration-clause, followed by either a
17360      `)', an `...', or a decl-specifier.  We need to check for a
17361      type-specifier to avoid being fooled into thinking that:
17362
17363        S::S (f) (int);
17364
17365      is a constructor.  (It is actually a function named `f' that
17366      takes one parameter (of type `int') and returns a value of type
17367      `S::S'.  */
17368   if (constructor_p
17369       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17370     {
17371       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17372           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17373           /* A parameter declaration begins with a decl-specifier,
17374              which is either the "attribute" keyword, a storage class
17375              specifier, or (usually) a type-specifier.  */
17376           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17377         {
17378           tree type;
17379           tree pushed_scope = NULL_TREE;
17380           unsigned saved_num_template_parameter_lists;
17381
17382           /* Names appearing in the type-specifier should be looked up
17383              in the scope of the class.  */
17384           if (current_class_type)
17385             type = NULL_TREE;
17386           else
17387             {
17388               type = TREE_TYPE (type_decl);
17389               if (TREE_CODE (type) == TYPENAME_TYPE)
17390                 {
17391                   type = resolve_typename_type (type,
17392                                                 /*only_current_p=*/false);
17393                   if (TREE_CODE (type) == TYPENAME_TYPE)
17394                     {
17395                       cp_parser_abort_tentative_parse (parser);
17396                       return false;
17397                     }
17398                 }
17399               pushed_scope = push_scope (type);
17400             }
17401
17402           /* Inside the constructor parameter list, surrounding
17403              template-parameter-lists do not apply.  */
17404           saved_num_template_parameter_lists
17405             = parser->num_template_parameter_lists;
17406           parser->num_template_parameter_lists = 0;
17407
17408           /* Look for the type-specifier.  */
17409           cp_parser_type_specifier (parser,
17410                                     CP_PARSER_FLAGS_NONE,
17411                                     /*decl_specs=*/NULL,
17412                                     /*is_declarator=*/true,
17413                                     /*declares_class_or_enum=*/NULL,
17414                                     /*is_cv_qualifier=*/NULL);
17415
17416           parser->num_template_parameter_lists
17417             = saved_num_template_parameter_lists;
17418
17419           /* Leave the scope of the class.  */
17420           if (pushed_scope)
17421             pop_scope (pushed_scope);
17422
17423           constructor_p = !cp_parser_error_occurred (parser);
17424         }
17425     }
17426   else
17427     constructor_p = false;
17428   /* We did not really want to consume any tokens.  */
17429   cp_parser_abort_tentative_parse (parser);
17430
17431   return constructor_p;
17432 }
17433
17434 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17435    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17436    they must be performed once we are in the scope of the function.
17437
17438    Returns the function defined.  */
17439
17440 static tree
17441 cp_parser_function_definition_from_specifiers_and_declarator
17442   (cp_parser* parser,
17443    cp_decl_specifier_seq *decl_specifiers,
17444    tree attributes,
17445    const cp_declarator *declarator)
17446 {
17447   tree fn;
17448   bool success_p;
17449
17450   /* Begin the function-definition.  */
17451   success_p = start_function (decl_specifiers, declarator, attributes);
17452
17453   /* The things we're about to see are not directly qualified by any
17454      template headers we've seen thus far.  */
17455   reset_specialization ();
17456
17457   /* If there were names looked up in the decl-specifier-seq that we
17458      did not check, check them now.  We must wait until we are in the
17459      scope of the function to perform the checks, since the function
17460      might be a friend.  */
17461   perform_deferred_access_checks ();
17462
17463   if (!success_p)
17464     {
17465       /* Skip the entire function.  */
17466       cp_parser_skip_to_end_of_block_or_statement (parser);
17467       fn = error_mark_node;
17468     }
17469   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17470     {
17471       /* Seen already, skip it.  An error message has already been output.  */
17472       cp_parser_skip_to_end_of_block_or_statement (parser);
17473       fn = current_function_decl;
17474       current_function_decl = NULL_TREE;
17475       /* If this is a function from a class, pop the nested class.  */
17476       if (current_class_name)
17477         pop_nested_class ();
17478     }
17479   else
17480     fn = cp_parser_function_definition_after_declarator (parser,
17481                                                          /*inline_p=*/false);
17482
17483   return fn;
17484 }
17485
17486 /* Parse the part of a function-definition that follows the
17487    declarator.  INLINE_P is TRUE iff this function is an inline
17488    function defined with a class-specifier.
17489
17490    Returns the function defined.  */
17491
17492 static tree
17493 cp_parser_function_definition_after_declarator (cp_parser* parser,
17494                                                 bool inline_p)
17495 {
17496   tree fn;
17497   bool ctor_initializer_p = false;
17498   bool saved_in_unbraced_linkage_specification_p;
17499   bool saved_in_function_body;
17500   unsigned saved_num_template_parameter_lists;
17501   cp_token *token;
17502
17503   saved_in_function_body = parser->in_function_body;
17504   parser->in_function_body = true;
17505   /* If the next token is `return', then the code may be trying to
17506      make use of the "named return value" extension that G++ used to
17507      support.  */
17508   token = cp_lexer_peek_token (parser->lexer);
17509   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17510     {
17511       /* Consume the `return' keyword.  */
17512       cp_lexer_consume_token (parser->lexer);
17513       /* Look for the identifier that indicates what value is to be
17514          returned.  */
17515       cp_parser_identifier (parser);
17516       /* Issue an error message.  */
17517       error ("%Hnamed return values are no longer supported",
17518              &token->location);
17519       /* Skip tokens until we reach the start of the function body.  */
17520       while (true)
17521         {
17522           cp_token *token = cp_lexer_peek_token (parser->lexer);
17523           if (token->type == CPP_OPEN_BRACE
17524               || token->type == CPP_EOF
17525               || token->type == CPP_PRAGMA_EOL)
17526             break;
17527           cp_lexer_consume_token (parser->lexer);
17528         }
17529     }
17530   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17531      anything declared inside `f'.  */
17532   saved_in_unbraced_linkage_specification_p
17533     = parser->in_unbraced_linkage_specification_p;
17534   parser->in_unbraced_linkage_specification_p = false;
17535   /* Inside the function, surrounding template-parameter-lists do not
17536      apply.  */
17537   saved_num_template_parameter_lists
17538     = parser->num_template_parameter_lists;
17539   parser->num_template_parameter_lists = 0;
17540   /* If the next token is `try', then we are looking at a
17541      function-try-block.  */
17542   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17543     ctor_initializer_p = cp_parser_function_try_block (parser);
17544   /* A function-try-block includes the function-body, so we only do
17545      this next part if we're not processing a function-try-block.  */
17546   else
17547     ctor_initializer_p
17548       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17549
17550   /* Finish the function.  */
17551   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17552                         (inline_p ? 2 : 0));
17553   /* Generate code for it, if necessary.  */
17554   expand_or_defer_fn (fn);
17555   /* Restore the saved values.  */
17556   parser->in_unbraced_linkage_specification_p
17557     = saved_in_unbraced_linkage_specification_p;
17558   parser->num_template_parameter_lists
17559     = saved_num_template_parameter_lists;
17560   parser->in_function_body = saved_in_function_body;
17561
17562   return fn;
17563 }
17564
17565 /* Parse a template-declaration, assuming that the `export' (and
17566    `extern') keywords, if present, has already been scanned.  MEMBER_P
17567    is as for cp_parser_template_declaration.  */
17568
17569 static void
17570 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17571 {
17572   tree decl = NULL_TREE;
17573   VEC (deferred_access_check,gc) *checks;
17574   tree parameter_list;
17575   bool friend_p = false;
17576   bool need_lang_pop;
17577   cp_token *token;
17578
17579   /* Look for the `template' keyword.  */
17580   token = cp_lexer_peek_token (parser->lexer);
17581   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17582     return;
17583
17584   /* And the `<'.  */
17585   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17586     return;
17587   if (at_class_scope_p () && current_function_decl)
17588     {
17589       /* 14.5.2.2 [temp.mem]
17590
17591          A local class shall not have member templates.  */
17592       error ("%Hinvalid declaration of member template in local class",
17593              &token->location);
17594       cp_parser_skip_to_end_of_block_or_statement (parser);
17595       return;
17596     }
17597   /* [temp]
17598
17599      A template ... shall not have C linkage.  */
17600   if (current_lang_name == lang_name_c)
17601     {
17602       error ("%Htemplate with C linkage", &token->location);
17603       /* Give it C++ linkage to avoid confusing other parts of the
17604          front end.  */
17605       push_lang_context (lang_name_cplusplus);
17606       need_lang_pop = true;
17607     }
17608   else
17609     need_lang_pop = false;
17610
17611   /* We cannot perform access checks on the template parameter
17612      declarations until we know what is being declared, just as we
17613      cannot check the decl-specifier list.  */
17614   push_deferring_access_checks (dk_deferred);
17615
17616   /* If the next token is `>', then we have an invalid
17617      specialization.  Rather than complain about an invalid template
17618      parameter, issue an error message here.  */
17619   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17620     {
17621       cp_parser_error (parser, "invalid explicit specialization");
17622       begin_specialization ();
17623       parameter_list = NULL_TREE;
17624     }
17625   else
17626     /* Parse the template parameters.  */
17627     parameter_list = cp_parser_template_parameter_list (parser);
17628
17629   /* Get the deferred access checks from the parameter list.  These
17630      will be checked once we know what is being declared, as for a
17631      member template the checks must be performed in the scope of the
17632      class containing the member.  */
17633   checks = get_deferred_access_checks ();
17634
17635   /* Look for the `>'.  */
17636   cp_parser_skip_to_end_of_template_parameter_list (parser);
17637   /* We just processed one more parameter list.  */
17638   ++parser->num_template_parameter_lists;
17639   /* If the next token is `template', there are more template
17640      parameters.  */
17641   if (cp_lexer_next_token_is_keyword (parser->lexer,
17642                                       RID_TEMPLATE))
17643     cp_parser_template_declaration_after_export (parser, member_p);
17644   else
17645     {
17646       /* There are no access checks when parsing a template, as we do not
17647          know if a specialization will be a friend.  */
17648       push_deferring_access_checks (dk_no_check);
17649       token = cp_lexer_peek_token (parser->lexer);
17650       decl = cp_parser_single_declaration (parser,
17651                                            checks,
17652                                            member_p,
17653                                            /*explicit_specialization_p=*/false,
17654                                            &friend_p);
17655       pop_deferring_access_checks ();
17656
17657       /* If this is a member template declaration, let the front
17658          end know.  */
17659       if (member_p && !friend_p && decl)
17660         {
17661           if (TREE_CODE (decl) == TYPE_DECL)
17662             cp_parser_check_access_in_redeclaration (decl, token->location);
17663
17664           decl = finish_member_template_decl (decl);
17665         }
17666       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17667         make_friend_class (current_class_type, TREE_TYPE (decl),
17668                            /*complain=*/true);
17669     }
17670   /* We are done with the current parameter list.  */
17671   --parser->num_template_parameter_lists;
17672
17673   pop_deferring_access_checks ();
17674
17675   /* Finish up.  */
17676   finish_template_decl (parameter_list);
17677
17678   /* Register member declarations.  */
17679   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17680     finish_member_declaration (decl);
17681   /* For the erroneous case of a template with C linkage, we pushed an
17682      implicit C++ linkage scope; exit that scope now.  */
17683   if (need_lang_pop)
17684     pop_lang_context ();
17685   /* If DECL is a function template, we must return to parse it later.
17686      (Even though there is no definition, there might be default
17687      arguments that need handling.)  */
17688   if (member_p && decl
17689       && (TREE_CODE (decl) == FUNCTION_DECL
17690           || DECL_FUNCTION_TEMPLATE_P (decl)))
17691     TREE_VALUE (parser->unparsed_functions_queues)
17692       = tree_cons (NULL_TREE, decl,
17693                    TREE_VALUE (parser->unparsed_functions_queues));
17694 }
17695
17696 /* Perform the deferred access checks from a template-parameter-list.
17697    CHECKS is a TREE_LIST of access checks, as returned by
17698    get_deferred_access_checks.  */
17699
17700 static void
17701 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17702 {
17703   ++processing_template_parmlist;
17704   perform_access_checks (checks);
17705   --processing_template_parmlist;
17706 }
17707
17708 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17709    `function-definition' sequence.  MEMBER_P is true, this declaration
17710    appears in a class scope.
17711
17712    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17713    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17714
17715 static tree
17716 cp_parser_single_declaration (cp_parser* parser,
17717                               VEC (deferred_access_check,gc)* checks,
17718                               bool member_p,
17719                               bool explicit_specialization_p,
17720                               bool* friend_p)
17721 {
17722   int declares_class_or_enum;
17723   tree decl = NULL_TREE;
17724   cp_decl_specifier_seq decl_specifiers;
17725   bool function_definition_p = false;
17726   cp_token *decl_spec_token_start;
17727
17728   /* This function is only used when processing a template
17729      declaration.  */
17730   gcc_assert (innermost_scope_kind () == sk_template_parms
17731               || innermost_scope_kind () == sk_template_spec);
17732
17733   /* Defer access checks until we know what is being declared.  */
17734   push_deferring_access_checks (dk_deferred);
17735
17736   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17737      alternative.  */
17738   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17739   cp_parser_decl_specifier_seq (parser,
17740                                 CP_PARSER_FLAGS_OPTIONAL,
17741                                 &decl_specifiers,
17742                                 &declares_class_or_enum);
17743   if (friend_p)
17744     *friend_p = cp_parser_friend_p (&decl_specifiers);
17745
17746   /* There are no template typedefs.  */
17747   if (decl_specifiers.specs[(int) ds_typedef])
17748     {
17749       error ("%Htemplate declaration of %qs",
17750              &decl_spec_token_start->location, "typedef");
17751       decl = error_mark_node;
17752     }
17753
17754   /* Gather up the access checks that occurred the
17755      decl-specifier-seq.  */
17756   stop_deferring_access_checks ();
17757
17758   /* Check for the declaration of a template class.  */
17759   if (declares_class_or_enum)
17760     {
17761       if (cp_parser_declares_only_class_p (parser))
17762         {
17763           decl = shadow_tag (&decl_specifiers);
17764
17765           /* In this case:
17766
17767                struct C {
17768                  friend template <typename T> struct A<T>::B;
17769                };
17770
17771              A<T>::B will be represented by a TYPENAME_TYPE, and
17772              therefore not recognized by shadow_tag.  */
17773           if (friend_p && *friend_p
17774               && !decl
17775               && decl_specifiers.type
17776               && TYPE_P (decl_specifiers.type))
17777             decl = decl_specifiers.type;
17778
17779           if (decl && decl != error_mark_node)
17780             decl = TYPE_NAME (decl);
17781           else
17782             decl = error_mark_node;
17783
17784           /* Perform access checks for template parameters.  */
17785           cp_parser_perform_template_parameter_access_checks (checks);
17786         }
17787     }
17788   /* If it's not a template class, try for a template function.  If
17789      the next token is a `;', then this declaration does not declare
17790      anything.  But, if there were errors in the decl-specifiers, then
17791      the error might well have come from an attempted class-specifier.
17792      In that case, there's no need to warn about a missing declarator.  */
17793   if (!decl
17794       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17795           || decl_specifiers.type != error_mark_node))
17796     {
17797       decl = cp_parser_init_declarator (parser,
17798                                         &decl_specifiers,
17799                                         checks,
17800                                         /*function_definition_allowed_p=*/true,
17801                                         member_p,
17802                                         declares_class_or_enum,
17803                                         &function_definition_p);
17804
17805     /* 7.1.1-1 [dcl.stc]
17806
17807        A storage-class-specifier shall not be specified in an explicit
17808        specialization...  */
17809     if (decl
17810         && explicit_specialization_p
17811         && decl_specifiers.storage_class != sc_none)
17812       {
17813         error ("%Hexplicit template specialization cannot have a storage class",
17814                &decl_spec_token_start->location);
17815         decl = error_mark_node;
17816       }
17817     }
17818
17819   pop_deferring_access_checks ();
17820
17821   /* Clear any current qualification; whatever comes next is the start
17822      of something new.  */
17823   parser->scope = NULL_TREE;
17824   parser->qualifying_scope = NULL_TREE;
17825   parser->object_scope = NULL_TREE;
17826   /* Look for a trailing `;' after the declaration.  */
17827   if (!function_definition_p
17828       && (decl == error_mark_node
17829           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17830     cp_parser_skip_to_end_of_block_or_statement (parser);
17831
17832   return decl;
17833 }
17834
17835 /* Parse a cast-expression that is not the operand of a unary "&".  */
17836
17837 static tree
17838 cp_parser_simple_cast_expression (cp_parser *parser)
17839 {
17840   return cp_parser_cast_expression (parser, /*address_p=*/false,
17841                                     /*cast_p=*/false);
17842 }
17843
17844 /* Parse a functional cast to TYPE.  Returns an expression
17845    representing the cast.  */
17846
17847 static tree
17848 cp_parser_functional_cast (cp_parser* parser, tree type)
17849 {
17850   tree expression_list;
17851   tree cast;
17852   bool nonconst_p;
17853
17854   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17855     {
17856       maybe_warn_cpp0x ("extended initializer lists");
17857       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17858       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17859       if (TREE_CODE (type) == TYPE_DECL)
17860         type = TREE_TYPE (type);
17861       return finish_compound_literal (type, expression_list);
17862     }
17863
17864   expression_list
17865     = cp_parser_parenthesized_expression_list (parser, false,
17866                                                /*cast_p=*/true,
17867                                                /*allow_expansion_p=*/true,
17868                                                /*non_constant_p=*/NULL);
17869
17870   cast = build_functional_cast (type, expression_list,
17871                                 tf_warning_or_error);
17872   /* [expr.const]/1: In an integral constant expression "only type
17873      conversions to integral or enumeration type can be used".  */
17874   if (TREE_CODE (type) == TYPE_DECL)
17875     type = TREE_TYPE (type);
17876   if (cast != error_mark_node
17877       && !cast_valid_in_integral_constant_expression_p (type)
17878       && (cp_parser_non_integral_constant_expression
17879           (parser, "a call to a constructor")))
17880     return error_mark_node;
17881   return cast;
17882 }
17883
17884 /* Save the tokens that make up the body of a member function defined
17885    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17886    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17887    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17888    for the member function.  */
17889
17890 static tree
17891 cp_parser_save_member_function_body (cp_parser* parser,
17892                                      cp_decl_specifier_seq *decl_specifiers,
17893                                      cp_declarator *declarator,
17894                                      tree attributes)
17895 {
17896   cp_token *first;
17897   cp_token *last;
17898   tree fn;
17899
17900   /* Create the function-declaration.  */
17901   fn = start_method (decl_specifiers, declarator, attributes);
17902   /* If something went badly wrong, bail out now.  */
17903   if (fn == error_mark_node)
17904     {
17905       /* If there's a function-body, skip it.  */
17906       if (cp_parser_token_starts_function_definition_p
17907           (cp_lexer_peek_token (parser->lexer)))
17908         cp_parser_skip_to_end_of_block_or_statement (parser);
17909       return error_mark_node;
17910     }
17911
17912   /* Remember it, if there default args to post process.  */
17913   cp_parser_save_default_args (parser, fn);
17914
17915   /* Save away the tokens that make up the body of the
17916      function.  */
17917   first = parser->lexer->next_token;
17918   /* We can have braced-init-list mem-initializers before the fn body.  */
17919   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17920     {
17921       cp_lexer_consume_token (parser->lexer);
17922       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17923              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17924         {
17925           /* cache_group will stop after an un-nested { } pair, too.  */
17926           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17927             break;
17928
17929           /* variadic mem-inits have ... after the ')'.  */
17930           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17931             cp_lexer_consume_token (parser->lexer);
17932         }
17933     }
17934   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17935   /* Handle function try blocks.  */
17936   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17937     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17938   last = parser->lexer->next_token;
17939
17940   /* Save away the inline definition; we will process it when the
17941      class is complete.  */
17942   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17943   DECL_PENDING_INLINE_P (fn) = 1;
17944
17945   /* We need to know that this was defined in the class, so that
17946      friend templates are handled correctly.  */
17947   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17948
17949   /* We're done with the inline definition.  */
17950   finish_method (fn);
17951
17952   /* Add FN to the queue of functions to be parsed later.  */
17953   TREE_VALUE (parser->unparsed_functions_queues)
17954     = tree_cons (NULL_TREE, fn,
17955                  TREE_VALUE (parser->unparsed_functions_queues));
17956
17957   return fn;
17958 }
17959
17960 /* Parse a template-argument-list, as well as the trailing ">" (but
17961    not the opening ">").  See cp_parser_template_argument_list for the
17962    return value.  */
17963
17964 static tree
17965 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17966 {
17967   tree arguments;
17968   tree saved_scope;
17969   tree saved_qualifying_scope;
17970   tree saved_object_scope;
17971   bool saved_greater_than_is_operator_p;
17972   bool saved_skip_evaluation;
17973
17974   /* [temp.names]
17975
17976      When parsing a template-id, the first non-nested `>' is taken as
17977      the end of the template-argument-list rather than a greater-than
17978      operator.  */
17979   saved_greater_than_is_operator_p
17980     = parser->greater_than_is_operator_p;
17981   parser->greater_than_is_operator_p = false;
17982   /* Parsing the argument list may modify SCOPE, so we save it
17983      here.  */
17984   saved_scope = parser->scope;
17985   saved_qualifying_scope = parser->qualifying_scope;
17986   saved_object_scope = parser->object_scope;
17987   /* We need to evaluate the template arguments, even though this
17988      template-id may be nested within a "sizeof".  */
17989   saved_skip_evaluation = skip_evaluation;
17990   skip_evaluation = false;
17991   /* Parse the template-argument-list itself.  */
17992   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17993       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17994     arguments = NULL_TREE;
17995   else
17996     arguments = cp_parser_template_argument_list (parser);
17997   /* Look for the `>' that ends the template-argument-list. If we find
17998      a '>>' instead, it's probably just a typo.  */
17999   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18000     {
18001       if (cxx_dialect != cxx98)
18002         {
18003           /* In C++0x, a `>>' in a template argument list or cast
18004              expression is considered to be two separate `>'
18005              tokens. So, change the current token to a `>', but don't
18006              consume it: it will be consumed later when the outer
18007              template argument list (or cast expression) is parsed.
18008              Note that this replacement of `>' for `>>' is necessary
18009              even if we are parsing tentatively: in the tentative
18010              case, after calling
18011              cp_parser_enclosed_template_argument_list we will always
18012              throw away all of the template arguments and the first
18013              closing `>', either because the template argument list
18014              was erroneous or because we are replacing those tokens
18015              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18016              not have been thrown away) is needed either to close an
18017              outer template argument list or to complete a new-style
18018              cast.  */
18019           cp_token *token = cp_lexer_peek_token (parser->lexer);
18020           token->type = CPP_GREATER;
18021         }
18022       else if (!saved_greater_than_is_operator_p)
18023         {
18024           /* If we're in a nested template argument list, the '>>' has
18025             to be a typo for '> >'. We emit the error message, but we
18026             continue parsing and we push a '>' as next token, so that
18027             the argument list will be parsed correctly.  Note that the
18028             global source location is still on the token before the
18029             '>>', so we need to say explicitly where we want it.  */
18030           cp_token *token = cp_lexer_peek_token (parser->lexer);
18031           error ("%H%<>>%> should be %<> >%> "
18032                  "within a nested template argument list",
18033                  &token->location);
18034
18035           token->type = CPP_GREATER;
18036         }
18037       else
18038         {
18039           /* If this is not a nested template argument list, the '>>'
18040             is a typo for '>'. Emit an error message and continue.
18041             Same deal about the token location, but here we can get it
18042             right by consuming the '>>' before issuing the diagnostic.  */
18043           cp_token *token = cp_lexer_consume_token (parser->lexer);
18044           error ("%Hspurious %<>>%>, use %<>%> to terminate "
18045                  "a template argument list", &token->location);
18046         }
18047     }
18048   else
18049     cp_parser_skip_to_end_of_template_parameter_list (parser);
18050   /* The `>' token might be a greater-than operator again now.  */
18051   parser->greater_than_is_operator_p
18052     = saved_greater_than_is_operator_p;
18053   /* Restore the SAVED_SCOPE.  */
18054   parser->scope = saved_scope;
18055   parser->qualifying_scope = saved_qualifying_scope;
18056   parser->object_scope = saved_object_scope;
18057   skip_evaluation = saved_skip_evaluation;
18058
18059   return arguments;
18060 }
18061
18062 /* MEMBER_FUNCTION is a member function, or a friend.  If default
18063    arguments, or the body of the function have not yet been parsed,
18064    parse them now.  */
18065
18066 static void
18067 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18068 {
18069   /* If this member is a template, get the underlying
18070      FUNCTION_DECL.  */
18071   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18072     member_function = DECL_TEMPLATE_RESULT (member_function);
18073
18074   /* There should not be any class definitions in progress at this
18075      point; the bodies of members are only parsed outside of all class
18076      definitions.  */
18077   gcc_assert (parser->num_classes_being_defined == 0);
18078   /* While we're parsing the member functions we might encounter more
18079      classes.  We want to handle them right away, but we don't want
18080      them getting mixed up with functions that are currently in the
18081      queue.  */
18082   parser->unparsed_functions_queues
18083     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18084
18085   /* Make sure that any template parameters are in scope.  */
18086   maybe_begin_member_template_processing (member_function);
18087
18088   /* If the body of the function has not yet been parsed, parse it
18089      now.  */
18090   if (DECL_PENDING_INLINE_P (member_function))
18091     {
18092       tree function_scope;
18093       cp_token_cache *tokens;
18094
18095       /* The function is no longer pending; we are processing it.  */
18096       tokens = DECL_PENDING_INLINE_INFO (member_function);
18097       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18098       DECL_PENDING_INLINE_P (member_function) = 0;
18099
18100       /* If this is a local class, enter the scope of the containing
18101          function.  */
18102       function_scope = current_function_decl;
18103       if (function_scope)
18104         push_function_context ();
18105
18106       /* Push the body of the function onto the lexer stack.  */
18107       cp_parser_push_lexer_for_tokens (parser, tokens);
18108
18109       /* Let the front end know that we going to be defining this
18110          function.  */
18111       start_preparsed_function (member_function, NULL_TREE,
18112                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18113
18114       /* Don't do access checking if it is a templated function.  */
18115       if (processing_template_decl)
18116         push_deferring_access_checks (dk_no_check);
18117
18118       /* Now, parse the body of the function.  */
18119       cp_parser_function_definition_after_declarator (parser,
18120                                                       /*inline_p=*/true);
18121
18122       if (processing_template_decl)
18123         pop_deferring_access_checks ();
18124
18125       /* Leave the scope of the containing function.  */
18126       if (function_scope)
18127         pop_function_context ();
18128       cp_parser_pop_lexer (parser);
18129     }
18130
18131   /* Remove any template parameters from the symbol table.  */
18132   maybe_end_member_template_processing ();
18133
18134   /* Restore the queue.  */
18135   parser->unparsed_functions_queues
18136     = TREE_CHAIN (parser->unparsed_functions_queues);
18137 }
18138
18139 /* If DECL contains any default args, remember it on the unparsed
18140    functions queue.  */
18141
18142 static void
18143 cp_parser_save_default_args (cp_parser* parser, tree decl)
18144 {
18145   tree probe;
18146
18147   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18148        probe;
18149        probe = TREE_CHAIN (probe))
18150     if (TREE_PURPOSE (probe))
18151       {
18152         TREE_PURPOSE (parser->unparsed_functions_queues)
18153           = tree_cons (current_class_type, decl,
18154                        TREE_PURPOSE (parser->unparsed_functions_queues));
18155         break;
18156       }
18157 }
18158
18159 /* FN is a FUNCTION_DECL which may contains a parameter with an
18160    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18161    assumes that the current scope is the scope in which the default
18162    argument should be processed.  */
18163
18164 static void
18165 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18166 {
18167   bool saved_local_variables_forbidden_p;
18168   tree parm;
18169
18170   /* While we're parsing the default args, we might (due to the
18171      statement expression extension) encounter more classes.  We want
18172      to handle them right away, but we don't want them getting mixed
18173      up with default args that are currently in the queue.  */
18174   parser->unparsed_functions_queues
18175     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18176
18177   /* Local variable names (and the `this' keyword) may not appear
18178      in a default argument.  */
18179   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18180   parser->local_variables_forbidden_p = true;
18181
18182   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18183        parm;
18184        parm = TREE_CHAIN (parm))
18185     {
18186       cp_token_cache *tokens;
18187       tree default_arg = TREE_PURPOSE (parm);
18188       tree parsed_arg;
18189       VEC(tree,gc) *insts;
18190       tree copy;
18191       unsigned ix;
18192
18193       if (!default_arg)
18194         continue;
18195
18196       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18197         /* This can happen for a friend declaration for a function
18198            already declared with default arguments.  */
18199         continue;
18200
18201        /* Push the saved tokens for the default argument onto the parser's
18202           lexer stack.  */
18203       tokens = DEFARG_TOKENS (default_arg);
18204       cp_parser_push_lexer_for_tokens (parser, tokens);
18205
18206       /* Parse the assignment-expression.  */
18207       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18208
18209       if (!processing_template_decl)
18210         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18211
18212       TREE_PURPOSE (parm) = parsed_arg;
18213
18214       /* Update any instantiations we've already created.  */
18215       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18216            VEC_iterate (tree, insts, ix, copy); ix++)
18217         TREE_PURPOSE (copy) = parsed_arg;
18218
18219       /* If the token stream has not been completely used up, then
18220          there was extra junk after the end of the default
18221          argument.  */
18222       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18223         cp_parser_error (parser, "expected %<,%>");
18224
18225       /* Revert to the main lexer.  */
18226       cp_parser_pop_lexer (parser);
18227     }
18228
18229   /* Make sure no default arg is missing.  */
18230   check_default_args (fn);
18231
18232   /* Restore the state of local_variables_forbidden_p.  */
18233   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18234
18235   /* Restore the queue.  */
18236   parser->unparsed_functions_queues
18237     = TREE_CHAIN (parser->unparsed_functions_queues);
18238 }
18239
18240 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18241    either a TYPE or an expression, depending on the form of the
18242    input.  The KEYWORD indicates which kind of expression we have
18243    encountered.  */
18244
18245 static tree
18246 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18247 {
18248   tree expr = NULL_TREE;
18249   const char *saved_message;
18250   char *tmp;
18251   bool saved_integral_constant_expression_p;
18252   bool saved_non_integral_constant_expression_p;
18253   bool pack_expansion_p = false;
18254
18255   /* Types cannot be defined in a `sizeof' expression.  Save away the
18256      old message.  */
18257   saved_message = parser->type_definition_forbidden_message;
18258   /* And create the new one.  */
18259   tmp = concat ("types may not be defined in %<",
18260                 IDENTIFIER_POINTER (ridpointers[keyword]),
18261                 "%> expressions", NULL);
18262   parser->type_definition_forbidden_message = tmp;
18263
18264   /* The restrictions on constant-expressions do not apply inside
18265      sizeof expressions.  */
18266   saved_integral_constant_expression_p
18267     = parser->integral_constant_expression_p;
18268   saved_non_integral_constant_expression_p
18269     = parser->non_integral_constant_expression_p;
18270   parser->integral_constant_expression_p = false;
18271
18272   /* If it's a `...', then we are computing the length of a parameter
18273      pack.  */
18274   if (keyword == RID_SIZEOF
18275       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18276     {
18277       /* Consume the `...'.  */
18278       cp_lexer_consume_token (parser->lexer);
18279       maybe_warn_variadic_templates ();
18280
18281       /* Note that this is an expansion.  */
18282       pack_expansion_p = true;
18283     }
18284
18285   /* Do not actually evaluate the expression.  */
18286   ++skip_evaluation;
18287   /* If it's a `(', then we might be looking at the type-id
18288      construction.  */
18289   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18290     {
18291       tree type;
18292       bool saved_in_type_id_in_expr_p;
18293
18294       /* We can't be sure yet whether we're looking at a type-id or an
18295          expression.  */
18296       cp_parser_parse_tentatively (parser);
18297       /* Consume the `('.  */
18298       cp_lexer_consume_token (parser->lexer);
18299       /* Parse the type-id.  */
18300       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18301       parser->in_type_id_in_expr_p = true;
18302       type = cp_parser_type_id (parser);
18303       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18304       /* Now, look for the trailing `)'.  */
18305       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18306       /* If all went well, then we're done.  */
18307       if (cp_parser_parse_definitely (parser))
18308         {
18309           cp_decl_specifier_seq decl_specs;
18310
18311           /* Build a trivial decl-specifier-seq.  */
18312           clear_decl_specs (&decl_specs);
18313           decl_specs.type = type;
18314
18315           /* Call grokdeclarator to figure out what type this is.  */
18316           expr = grokdeclarator (NULL,
18317                                  &decl_specs,
18318                                  TYPENAME,
18319                                  /*initialized=*/0,
18320                                  /*attrlist=*/NULL);
18321         }
18322     }
18323
18324   /* If the type-id production did not work out, then we must be
18325      looking at the unary-expression production.  */
18326   if (!expr)
18327     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18328                                        /*cast_p=*/false);
18329
18330   if (pack_expansion_p)
18331     /* Build a pack expansion. */
18332     expr = make_pack_expansion (expr);
18333
18334   /* Go back to evaluating expressions.  */
18335   --skip_evaluation;
18336
18337   /* Free the message we created.  */
18338   free (tmp);
18339   /* And restore the old one.  */
18340   parser->type_definition_forbidden_message = saved_message;
18341   parser->integral_constant_expression_p
18342     = saved_integral_constant_expression_p;
18343   parser->non_integral_constant_expression_p
18344     = saved_non_integral_constant_expression_p;
18345
18346   return expr;
18347 }
18348
18349 /* If the current declaration has no declarator, return true.  */
18350
18351 static bool
18352 cp_parser_declares_only_class_p (cp_parser *parser)
18353 {
18354   /* If the next token is a `;' or a `,' then there is no
18355      declarator.  */
18356   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18357           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18358 }
18359
18360 /* Update the DECL_SPECS to reflect the storage class indicated by
18361    KEYWORD.  */
18362
18363 static void
18364 cp_parser_set_storage_class (cp_parser *parser,
18365                              cp_decl_specifier_seq *decl_specs,
18366                              enum rid keyword,
18367                              location_t location)
18368 {
18369   cp_storage_class storage_class;
18370
18371   if (parser->in_unbraced_linkage_specification_p)
18372     {
18373       error ("%Hinvalid use of %qD in linkage specification",
18374              &location, ridpointers[keyword]);
18375       return;
18376     }
18377   else if (decl_specs->storage_class != sc_none)
18378     {
18379       decl_specs->conflicting_specifiers_p = true;
18380       return;
18381     }
18382
18383   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18384       && decl_specs->specs[(int) ds_thread])
18385     {
18386       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18387       decl_specs->specs[(int) ds_thread] = 0;
18388     }
18389
18390   switch (keyword)
18391     {
18392     case RID_AUTO:
18393       storage_class = sc_auto;
18394       break;
18395     case RID_REGISTER:
18396       storage_class = sc_register;
18397       break;
18398     case RID_STATIC:
18399       storage_class = sc_static;
18400       break;
18401     case RID_EXTERN:
18402       storage_class = sc_extern;
18403       break;
18404     case RID_MUTABLE:
18405       storage_class = sc_mutable;
18406       break;
18407     default:
18408       gcc_unreachable ();
18409     }
18410   decl_specs->storage_class = storage_class;
18411
18412   /* A storage class specifier cannot be applied alongside a typedef 
18413      specifier. If there is a typedef specifier present then set 
18414      conflicting_specifiers_p which will trigger an error later
18415      on in grokdeclarator. */
18416   if (decl_specs->specs[(int)ds_typedef])
18417     decl_specs->conflicting_specifiers_p = true;
18418 }
18419
18420 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18421    is true, the type is a user-defined type; otherwise it is a
18422    built-in type specified by a keyword.  */
18423
18424 static void
18425 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18426                               tree type_spec,
18427                               location_t location,
18428                               bool user_defined_p)
18429 {
18430   decl_specs->any_specifiers_p = true;
18431
18432   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18433      (with, for example, in "typedef int wchar_t;") we remember that
18434      this is what happened.  In system headers, we ignore these
18435      declarations so that G++ can work with system headers that are not
18436      C++-safe.  */
18437   if (decl_specs->specs[(int) ds_typedef]
18438       && !user_defined_p
18439       && (type_spec == boolean_type_node
18440           || type_spec == char16_type_node
18441           || type_spec == char32_type_node
18442           || type_spec == wchar_type_node)
18443       && (decl_specs->type
18444           || decl_specs->specs[(int) ds_long]
18445           || decl_specs->specs[(int) ds_short]
18446           || decl_specs->specs[(int) ds_unsigned]
18447           || decl_specs->specs[(int) ds_signed]))
18448     {
18449       decl_specs->redefined_builtin_type = type_spec;
18450       if (!decl_specs->type)
18451         {
18452           decl_specs->type = type_spec;
18453           decl_specs->user_defined_type_p = false;
18454           decl_specs->type_location = location;
18455         }
18456     }
18457   else if (decl_specs->type)
18458     decl_specs->multiple_types_p = true;
18459   else
18460     {
18461       decl_specs->type = type_spec;
18462       decl_specs->user_defined_type_p = user_defined_p;
18463       decl_specs->redefined_builtin_type = NULL_TREE;
18464       decl_specs->type_location = location;
18465     }
18466 }
18467
18468 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18469    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18470
18471 static bool
18472 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18473 {
18474   return decl_specifiers->specs[(int) ds_friend] != 0;
18475 }
18476
18477 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18478    issue an error message indicating that TOKEN_DESC was expected.
18479
18480    Returns the token consumed, if the token had the appropriate type.
18481    Otherwise, returns NULL.  */
18482
18483 static cp_token *
18484 cp_parser_require (cp_parser* parser,
18485                    enum cpp_ttype type,
18486                    const char* token_desc)
18487 {
18488   if (cp_lexer_next_token_is (parser->lexer, type))
18489     return cp_lexer_consume_token (parser->lexer);
18490   else
18491     {
18492       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18493       if (!cp_parser_simulate_error (parser))
18494         {
18495           char *message = concat ("expected ", token_desc, NULL);
18496           cp_parser_error (parser, message);
18497           free (message);
18498         }
18499       return NULL;
18500     }
18501 }
18502
18503 /* An error message is produced if the next token is not '>'.
18504    All further tokens are skipped until the desired token is
18505    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18506
18507 static void
18508 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18509 {
18510   /* Current level of '< ... >'.  */
18511   unsigned level = 0;
18512   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18513   unsigned nesting_depth = 0;
18514
18515   /* Are we ready, yet?  If not, issue error message.  */
18516   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18517     return;
18518
18519   /* Skip tokens until the desired token is found.  */
18520   while (true)
18521     {
18522       /* Peek at the next token.  */
18523       switch (cp_lexer_peek_token (parser->lexer)->type)
18524         {
18525         case CPP_LESS:
18526           if (!nesting_depth)
18527             ++level;
18528           break;
18529
18530         case CPP_RSHIFT:
18531           if (cxx_dialect == cxx98)
18532             /* C++0x views the `>>' operator as two `>' tokens, but
18533                C++98 does not. */
18534             break;
18535           else if (!nesting_depth && level-- == 0)
18536             {
18537               /* We've hit a `>>' where the first `>' closes the
18538                  template argument list, and the second `>' is
18539                  spurious.  Just consume the `>>' and stop; we've
18540                  already produced at least one error.  */
18541               cp_lexer_consume_token (parser->lexer);
18542               return;
18543             }
18544           /* Fall through for C++0x, so we handle the second `>' in
18545              the `>>'.  */
18546
18547         case CPP_GREATER:
18548           if (!nesting_depth && level-- == 0)
18549             {
18550               /* We've reached the token we want, consume it and stop.  */
18551               cp_lexer_consume_token (parser->lexer);
18552               return;
18553             }
18554           break;
18555
18556         case CPP_OPEN_PAREN:
18557         case CPP_OPEN_SQUARE:
18558           ++nesting_depth;
18559           break;
18560
18561         case CPP_CLOSE_PAREN:
18562         case CPP_CLOSE_SQUARE:
18563           if (nesting_depth-- == 0)
18564             return;
18565           break;
18566
18567         case CPP_EOF:
18568         case CPP_PRAGMA_EOL:
18569         case CPP_SEMICOLON:
18570         case CPP_OPEN_BRACE:
18571         case CPP_CLOSE_BRACE:
18572           /* The '>' was probably forgotten, don't look further.  */
18573           return;
18574
18575         default:
18576           break;
18577         }
18578
18579       /* Consume this token.  */
18580       cp_lexer_consume_token (parser->lexer);
18581     }
18582 }
18583
18584 /* If the next token is the indicated keyword, consume it.  Otherwise,
18585    issue an error message indicating that TOKEN_DESC was expected.
18586
18587    Returns the token consumed, if the token had the appropriate type.
18588    Otherwise, returns NULL.  */
18589
18590 static cp_token *
18591 cp_parser_require_keyword (cp_parser* parser,
18592                            enum rid keyword,
18593                            const char* token_desc)
18594 {
18595   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18596
18597   if (token && token->keyword != keyword)
18598     {
18599       dyn_string_t error_msg;
18600
18601       /* Format the error message.  */
18602       error_msg = dyn_string_new (0);
18603       dyn_string_append_cstr (error_msg, "expected ");
18604       dyn_string_append_cstr (error_msg, token_desc);
18605       cp_parser_error (parser, error_msg->s);
18606       dyn_string_delete (error_msg);
18607       return NULL;
18608     }
18609
18610   return token;
18611 }
18612
18613 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18614    function-definition.  */
18615
18616 static bool
18617 cp_parser_token_starts_function_definition_p (cp_token* token)
18618 {
18619   return (/* An ordinary function-body begins with an `{'.  */
18620           token->type == CPP_OPEN_BRACE
18621           /* A ctor-initializer begins with a `:'.  */
18622           || token->type == CPP_COLON
18623           /* A function-try-block begins with `try'.  */
18624           || token->keyword == RID_TRY
18625           /* The named return value extension begins with `return'.  */
18626           || token->keyword == RID_RETURN);
18627 }
18628
18629 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18630    definition.  */
18631
18632 static bool
18633 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18634 {
18635   cp_token *token;
18636
18637   token = cp_lexer_peek_token (parser->lexer);
18638   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18639 }
18640
18641 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18642    C++0x) ending a template-argument.  */
18643
18644 static bool
18645 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18646 {
18647   cp_token *token;
18648
18649   token = cp_lexer_peek_token (parser->lexer);
18650   return (token->type == CPP_COMMA 
18651           || token->type == CPP_GREATER
18652           || token->type == CPP_ELLIPSIS
18653           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18654 }
18655
18656 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18657    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18658
18659 static bool
18660 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18661                                                      size_t n)
18662 {
18663   cp_token *token;
18664
18665   token = cp_lexer_peek_nth_token (parser->lexer, n);
18666   if (token->type == CPP_LESS)
18667     return true;
18668   /* Check for the sequence `<::' in the original code. It would be lexed as
18669      `[:', where `[' is a digraph, and there is no whitespace before
18670      `:'.  */
18671   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18672     {
18673       cp_token *token2;
18674       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18675       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18676         return true;
18677     }
18678   return false;
18679 }
18680
18681 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18682    or none_type otherwise.  */
18683
18684 static enum tag_types
18685 cp_parser_token_is_class_key (cp_token* token)
18686 {
18687   switch (token->keyword)
18688     {
18689     case RID_CLASS:
18690       return class_type;
18691     case RID_STRUCT:
18692       return record_type;
18693     case RID_UNION:
18694       return union_type;
18695
18696     default:
18697       return none_type;
18698     }
18699 }
18700
18701 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18702
18703 static void
18704 cp_parser_check_class_key (enum tag_types class_key, tree type)
18705 {
18706   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18707     permerror (input_location, "%qs tag used in naming %q#T",
18708             class_key == union_type ? "union"
18709              : class_key == record_type ? "struct" : "class",
18710              type);
18711 }
18712
18713 /* Issue an error message if DECL is redeclared with different
18714    access than its original declaration [class.access.spec/3].
18715    This applies to nested classes and nested class templates.
18716    [class.mem/1].  */
18717
18718 static void
18719 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18720 {
18721   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18722     return;
18723
18724   if ((TREE_PRIVATE (decl)
18725        != (current_access_specifier == access_private_node))
18726       || (TREE_PROTECTED (decl)
18727           != (current_access_specifier == access_protected_node)))
18728     error ("%H%qD redeclared with different access", &location, decl);
18729 }
18730
18731 /* Look for the `template' keyword, as a syntactic disambiguator.
18732    Return TRUE iff it is present, in which case it will be
18733    consumed.  */
18734
18735 static bool
18736 cp_parser_optional_template_keyword (cp_parser *parser)
18737 {
18738   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18739     {
18740       /* The `template' keyword can only be used within templates;
18741          outside templates the parser can always figure out what is a
18742          template and what is not.  */
18743       if (!processing_template_decl)
18744         {
18745           cp_token *token = cp_lexer_peek_token (parser->lexer);
18746           error ("%H%<template%> (as a disambiguator) is only allowed "
18747                  "within templates", &token->location);
18748           /* If this part of the token stream is rescanned, the same
18749              error message would be generated.  So, we purge the token
18750              from the stream.  */
18751           cp_lexer_purge_token (parser->lexer);
18752           return false;
18753         }
18754       else
18755         {
18756           /* Consume the `template' keyword.  */
18757           cp_lexer_consume_token (parser->lexer);
18758           return true;
18759         }
18760     }
18761
18762   return false;
18763 }
18764
18765 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18766    set PARSER->SCOPE, and perform other related actions.  */
18767
18768 static void
18769 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18770 {
18771   int i;
18772   struct tree_check *check_value;
18773   deferred_access_check *chk;
18774   VEC (deferred_access_check,gc) *checks;
18775
18776   /* Get the stored value.  */
18777   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18778   /* Perform any access checks that were deferred.  */
18779   checks = check_value->checks;
18780   if (checks)
18781     {
18782       for (i = 0 ;
18783            VEC_iterate (deferred_access_check, checks, i, chk) ;
18784            ++i)
18785         {
18786           perform_or_defer_access_check (chk->binfo,
18787                                          chk->decl,
18788                                          chk->diag_decl);
18789         }
18790     }
18791   /* Set the scope from the stored value.  */
18792   parser->scope = check_value->value;
18793   parser->qualifying_scope = check_value->qualifying_scope;
18794   parser->object_scope = NULL_TREE;
18795 }
18796
18797 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18798    encounter the end of a block before what we were looking for.  */
18799
18800 static bool
18801 cp_parser_cache_group (cp_parser *parser,
18802                        enum cpp_ttype end,
18803                        unsigned depth)
18804 {
18805   while (true)
18806     {
18807       cp_token *token = cp_lexer_peek_token (parser->lexer);
18808
18809       /* Abort a parenthesized expression if we encounter a semicolon.  */
18810       if ((end == CPP_CLOSE_PAREN || depth == 0)
18811           && token->type == CPP_SEMICOLON)
18812         return true;
18813       /* If we've reached the end of the file, stop.  */
18814       if (token->type == CPP_EOF
18815           || (end != CPP_PRAGMA_EOL
18816               && token->type == CPP_PRAGMA_EOL))
18817         return true;
18818       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18819         /* We've hit the end of an enclosing block, so there's been some
18820            kind of syntax error.  */
18821         return true;
18822
18823       /* Consume the token.  */
18824       cp_lexer_consume_token (parser->lexer);
18825       /* See if it starts a new group.  */
18826       if (token->type == CPP_OPEN_BRACE)
18827         {
18828           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18829           /* In theory this should probably check end == '}', but
18830              cp_parser_save_member_function_body needs it to exit
18831              after either '}' or ')' when called with ')'.  */
18832           if (depth == 0)
18833             return false;
18834         }
18835       else if (token->type == CPP_OPEN_PAREN)
18836         {
18837           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18838           if (depth == 0 && end == CPP_CLOSE_PAREN)
18839             return false;
18840         }
18841       else if (token->type == CPP_PRAGMA)
18842         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18843       else if (token->type == end)
18844         return false;
18845     }
18846 }
18847
18848 /* Begin parsing tentatively.  We always save tokens while parsing
18849    tentatively so that if the tentative parsing fails we can restore the
18850    tokens.  */
18851
18852 static void
18853 cp_parser_parse_tentatively (cp_parser* parser)
18854 {
18855   /* Enter a new parsing context.  */
18856   parser->context = cp_parser_context_new (parser->context);
18857   /* Begin saving tokens.  */
18858   cp_lexer_save_tokens (parser->lexer);
18859   /* In order to avoid repetitive access control error messages,
18860      access checks are queued up until we are no longer parsing
18861      tentatively.  */
18862   push_deferring_access_checks (dk_deferred);
18863 }
18864
18865 /* Commit to the currently active tentative parse.  */
18866
18867 static void
18868 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18869 {
18870   cp_parser_context *context;
18871   cp_lexer *lexer;
18872
18873   /* Mark all of the levels as committed.  */
18874   lexer = parser->lexer;
18875   for (context = parser->context; context->next; context = context->next)
18876     {
18877       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18878         break;
18879       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18880       while (!cp_lexer_saving_tokens (lexer))
18881         lexer = lexer->next;
18882       cp_lexer_commit_tokens (lexer);
18883     }
18884 }
18885
18886 /* Abort the currently active tentative parse.  All consumed tokens
18887    will be rolled back, and no diagnostics will be issued.  */
18888
18889 static void
18890 cp_parser_abort_tentative_parse (cp_parser* parser)
18891 {
18892   cp_parser_simulate_error (parser);
18893   /* Now, pretend that we want to see if the construct was
18894      successfully parsed.  */
18895   cp_parser_parse_definitely (parser);
18896 }
18897
18898 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18899    token stream.  Otherwise, commit to the tokens we have consumed.
18900    Returns true if no error occurred; false otherwise.  */
18901
18902 static bool
18903 cp_parser_parse_definitely (cp_parser* parser)
18904 {
18905   bool error_occurred;
18906   cp_parser_context *context;
18907
18908   /* Remember whether or not an error occurred, since we are about to
18909      destroy that information.  */
18910   error_occurred = cp_parser_error_occurred (parser);
18911   /* Remove the topmost context from the stack.  */
18912   context = parser->context;
18913   parser->context = context->next;
18914   /* If no parse errors occurred, commit to the tentative parse.  */
18915   if (!error_occurred)
18916     {
18917       /* Commit to the tokens read tentatively, unless that was
18918          already done.  */
18919       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18920         cp_lexer_commit_tokens (parser->lexer);
18921
18922       pop_to_parent_deferring_access_checks ();
18923     }
18924   /* Otherwise, if errors occurred, roll back our state so that things
18925      are just as they were before we began the tentative parse.  */
18926   else
18927     {
18928       cp_lexer_rollback_tokens (parser->lexer);
18929       pop_deferring_access_checks ();
18930     }
18931   /* Add the context to the front of the free list.  */
18932   context->next = cp_parser_context_free_list;
18933   cp_parser_context_free_list = context;
18934
18935   return !error_occurred;
18936 }
18937
18938 /* Returns true if we are parsing tentatively and are not committed to
18939    this tentative parse.  */
18940
18941 static bool
18942 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18943 {
18944   return (cp_parser_parsing_tentatively (parser)
18945           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18946 }
18947
18948 /* Returns nonzero iff an error has occurred during the most recent
18949    tentative parse.  */
18950
18951 static bool
18952 cp_parser_error_occurred (cp_parser* parser)
18953 {
18954   return (cp_parser_parsing_tentatively (parser)
18955           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18956 }
18957
18958 /* Returns nonzero if GNU extensions are allowed.  */
18959
18960 static bool
18961 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18962 {
18963   return parser->allow_gnu_extensions_p;
18964 }
18965 \f
18966 /* Objective-C++ Productions */
18967
18968
18969 /* Parse an Objective-C expression, which feeds into a primary-expression
18970    above.
18971
18972    objc-expression:
18973      objc-message-expression
18974      objc-string-literal
18975      objc-encode-expression
18976      objc-protocol-expression
18977      objc-selector-expression
18978
18979   Returns a tree representation of the expression.  */
18980
18981 static tree
18982 cp_parser_objc_expression (cp_parser* parser)
18983 {
18984   /* Try to figure out what kind of declaration is present.  */
18985   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18986
18987   switch (kwd->type)
18988     {
18989     case CPP_OPEN_SQUARE:
18990       return cp_parser_objc_message_expression (parser);
18991
18992     case CPP_OBJC_STRING:
18993       kwd = cp_lexer_consume_token (parser->lexer);
18994       return objc_build_string_object (kwd->u.value);
18995
18996     case CPP_KEYWORD:
18997       switch (kwd->keyword)
18998         {
18999         case RID_AT_ENCODE:
19000           return cp_parser_objc_encode_expression (parser);
19001
19002         case RID_AT_PROTOCOL:
19003           return cp_parser_objc_protocol_expression (parser);
19004
19005         case RID_AT_SELECTOR:
19006           return cp_parser_objc_selector_expression (parser);
19007
19008         default:
19009           break;
19010         }
19011     default:
19012       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19013              &kwd->location, kwd->u.value);
19014       cp_parser_skip_to_end_of_block_or_statement (parser);
19015     }
19016
19017   return error_mark_node;
19018 }
19019
19020 /* Parse an Objective-C message expression.
19021
19022    objc-message-expression:
19023      [ objc-message-receiver objc-message-args ]
19024
19025    Returns a representation of an Objective-C message.  */
19026
19027 static tree
19028 cp_parser_objc_message_expression (cp_parser* parser)
19029 {
19030   tree receiver, messageargs;
19031
19032   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
19033   receiver = cp_parser_objc_message_receiver (parser);
19034   messageargs = cp_parser_objc_message_args (parser);
19035   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19036
19037   return objc_build_message_expr (build_tree_list (receiver, messageargs));
19038 }
19039
19040 /* Parse an objc-message-receiver.
19041
19042    objc-message-receiver:
19043      expression
19044      simple-type-specifier
19045
19046   Returns a representation of the type or expression.  */
19047
19048 static tree
19049 cp_parser_objc_message_receiver (cp_parser* parser)
19050 {
19051   tree rcv;
19052
19053   /* An Objective-C message receiver may be either (1) a type
19054      or (2) an expression.  */
19055   cp_parser_parse_tentatively (parser);
19056   rcv = cp_parser_expression (parser, false);
19057
19058   if (cp_parser_parse_definitely (parser))
19059     return rcv;
19060
19061   rcv = cp_parser_simple_type_specifier (parser,
19062                                          /*decl_specs=*/NULL,
19063                                          CP_PARSER_FLAGS_NONE);
19064
19065   return objc_get_class_reference (rcv);
19066 }
19067
19068 /* Parse the arguments and selectors comprising an Objective-C message.
19069
19070    objc-message-args:
19071      objc-selector
19072      objc-selector-args
19073      objc-selector-args , objc-comma-args
19074
19075    objc-selector-args:
19076      objc-selector [opt] : assignment-expression
19077      objc-selector-args objc-selector [opt] : assignment-expression
19078
19079    objc-comma-args:
19080      assignment-expression
19081      objc-comma-args , assignment-expression
19082
19083    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19084    selector arguments and TREE_VALUE containing a list of comma
19085    arguments.  */
19086
19087 static tree
19088 cp_parser_objc_message_args (cp_parser* parser)
19089 {
19090   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19091   bool maybe_unary_selector_p = true;
19092   cp_token *token = cp_lexer_peek_token (parser->lexer);
19093
19094   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19095     {
19096       tree selector = NULL_TREE, arg;
19097
19098       if (token->type != CPP_COLON)
19099         selector = cp_parser_objc_selector (parser);
19100
19101       /* Detect if we have a unary selector.  */
19102       if (maybe_unary_selector_p
19103           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19104         return build_tree_list (selector, NULL_TREE);
19105
19106       maybe_unary_selector_p = false;
19107       cp_parser_require (parser, CPP_COLON, "%<:%>");
19108       arg = cp_parser_assignment_expression (parser, false);
19109
19110       sel_args
19111         = chainon (sel_args,
19112                    build_tree_list (selector, arg));
19113
19114       token = cp_lexer_peek_token (parser->lexer);
19115     }
19116
19117   /* Handle non-selector arguments, if any. */
19118   while (token->type == CPP_COMMA)
19119     {
19120       tree arg;
19121
19122       cp_lexer_consume_token (parser->lexer);
19123       arg = cp_parser_assignment_expression (parser, false);
19124
19125       addl_args
19126         = chainon (addl_args,
19127                    build_tree_list (NULL_TREE, arg));
19128
19129       token = cp_lexer_peek_token (parser->lexer);
19130     }
19131
19132   return build_tree_list (sel_args, addl_args);
19133 }
19134
19135 /* Parse an Objective-C encode expression.
19136
19137    objc-encode-expression:
19138      @encode objc-typename
19139
19140    Returns an encoded representation of the type argument.  */
19141
19142 static tree
19143 cp_parser_objc_encode_expression (cp_parser* parser)
19144 {
19145   tree type;
19146   cp_token *token;
19147
19148   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19149   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19150   token = cp_lexer_peek_token (parser->lexer);
19151   type = complete_type (cp_parser_type_id (parser));
19152   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19153
19154   if (!type)
19155     {
19156       error ("%H%<@encode%> must specify a type as an argument",
19157              &token->location);
19158       return error_mark_node;
19159     }
19160
19161   return objc_build_encode_expr (type);
19162 }
19163
19164 /* Parse an Objective-C @defs expression.  */
19165
19166 static tree
19167 cp_parser_objc_defs_expression (cp_parser *parser)
19168 {
19169   tree name;
19170
19171   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19172   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19173   name = cp_parser_identifier (parser);
19174   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19175
19176   return objc_get_class_ivars (name);
19177 }
19178
19179 /* Parse an Objective-C protocol expression.
19180
19181   objc-protocol-expression:
19182     @protocol ( identifier )
19183
19184   Returns a representation of the protocol expression.  */
19185
19186 static tree
19187 cp_parser_objc_protocol_expression (cp_parser* parser)
19188 {
19189   tree proto;
19190
19191   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19192   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19193   proto = cp_parser_identifier (parser);
19194   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19195
19196   return objc_build_protocol_expr (proto);
19197 }
19198
19199 /* Parse an Objective-C selector expression.
19200
19201    objc-selector-expression:
19202      @selector ( objc-method-signature )
19203
19204    objc-method-signature:
19205      objc-selector
19206      objc-selector-seq
19207
19208    objc-selector-seq:
19209      objc-selector :
19210      objc-selector-seq objc-selector :
19211
19212   Returns a representation of the method selector.  */
19213
19214 static tree
19215 cp_parser_objc_selector_expression (cp_parser* parser)
19216 {
19217   tree sel_seq = NULL_TREE;
19218   bool maybe_unary_selector_p = true;
19219   cp_token *token;
19220
19221   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19222   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19223   token = cp_lexer_peek_token (parser->lexer);
19224
19225   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19226          || token->type == CPP_SCOPE)
19227     {
19228       tree selector = NULL_TREE;
19229
19230       if (token->type != CPP_COLON
19231           || token->type == CPP_SCOPE)
19232         selector = cp_parser_objc_selector (parser);
19233
19234       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19235           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19236         {
19237           /* Detect if we have a unary selector.  */
19238           if (maybe_unary_selector_p)
19239             {
19240               sel_seq = selector;
19241               goto finish_selector;
19242             }
19243           else
19244             {
19245               cp_parser_error (parser, "expected %<:%>");
19246             }
19247         }
19248       maybe_unary_selector_p = false;
19249       token = cp_lexer_consume_token (parser->lexer);
19250
19251       if (token->type == CPP_SCOPE)
19252         {
19253           sel_seq
19254             = chainon (sel_seq,
19255                        build_tree_list (selector, NULL_TREE));
19256           sel_seq
19257             = chainon (sel_seq,
19258                        build_tree_list (NULL_TREE, NULL_TREE));
19259         }
19260       else
19261         sel_seq
19262           = chainon (sel_seq,
19263                      build_tree_list (selector, NULL_TREE));
19264
19265       token = cp_lexer_peek_token (parser->lexer);
19266     }
19267
19268  finish_selector:
19269   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19270
19271   return objc_build_selector_expr (sel_seq);
19272 }
19273
19274 /* Parse a list of identifiers.
19275
19276    objc-identifier-list:
19277      identifier
19278      objc-identifier-list , identifier
19279
19280    Returns a TREE_LIST of identifier nodes.  */
19281
19282 static tree
19283 cp_parser_objc_identifier_list (cp_parser* parser)
19284 {
19285   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19286   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19287
19288   while (sep->type == CPP_COMMA)
19289     {
19290       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19291       list = chainon (list,
19292                       build_tree_list (NULL_TREE,
19293                                        cp_parser_identifier (parser)));
19294       sep = cp_lexer_peek_token (parser->lexer);
19295     }
19296
19297   return list;
19298 }
19299
19300 /* Parse an Objective-C alias declaration.
19301
19302    objc-alias-declaration:
19303      @compatibility_alias identifier identifier ;
19304
19305    This function registers the alias mapping with the Objective-C front end.
19306    It returns nothing.  */
19307
19308 static void
19309 cp_parser_objc_alias_declaration (cp_parser* parser)
19310 {
19311   tree alias, orig;
19312
19313   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19314   alias = cp_parser_identifier (parser);
19315   orig = cp_parser_identifier (parser);
19316   objc_declare_alias (alias, orig);
19317   cp_parser_consume_semicolon_at_end_of_statement (parser);
19318 }
19319
19320 /* Parse an Objective-C class forward-declaration.
19321
19322    objc-class-declaration:
19323      @class objc-identifier-list ;
19324
19325    The function registers the forward declarations with the Objective-C
19326    front end.  It returns nothing.  */
19327
19328 static void
19329 cp_parser_objc_class_declaration (cp_parser* parser)
19330 {
19331   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19332   objc_declare_class (cp_parser_objc_identifier_list (parser));
19333   cp_parser_consume_semicolon_at_end_of_statement (parser);
19334 }
19335
19336 /* Parse a list of Objective-C protocol references.
19337
19338    objc-protocol-refs-opt:
19339      objc-protocol-refs [opt]
19340
19341    objc-protocol-refs:
19342      < objc-identifier-list >
19343
19344    Returns a TREE_LIST of identifiers, if any.  */
19345
19346 static tree
19347 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19348 {
19349   tree protorefs = NULL_TREE;
19350
19351   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19352     {
19353       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19354       protorefs = cp_parser_objc_identifier_list (parser);
19355       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19356     }
19357
19358   return protorefs;
19359 }
19360
19361 /* Parse a Objective-C visibility specification.  */
19362
19363 static void
19364 cp_parser_objc_visibility_spec (cp_parser* parser)
19365 {
19366   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19367
19368   switch (vis->keyword)
19369     {
19370     case RID_AT_PRIVATE:
19371       objc_set_visibility (2);
19372       break;
19373     case RID_AT_PROTECTED:
19374       objc_set_visibility (0);
19375       break;
19376     case RID_AT_PUBLIC:
19377       objc_set_visibility (1);
19378       break;
19379     default:
19380       return;
19381     }
19382
19383   /* Eat '@private'/'@protected'/'@public'.  */
19384   cp_lexer_consume_token (parser->lexer);
19385 }
19386
19387 /* Parse an Objective-C method type.  */
19388
19389 static void
19390 cp_parser_objc_method_type (cp_parser* parser)
19391 {
19392   objc_set_method_type
19393    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19394     ? PLUS_EXPR
19395     : MINUS_EXPR);
19396 }
19397
19398 /* Parse an Objective-C protocol qualifier.  */
19399
19400 static tree
19401 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19402 {
19403   tree quals = NULL_TREE, node;
19404   cp_token *token = cp_lexer_peek_token (parser->lexer);
19405
19406   node = token->u.value;
19407
19408   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19409          && (node == ridpointers [(int) RID_IN]
19410              || node == ridpointers [(int) RID_OUT]
19411              || node == ridpointers [(int) RID_INOUT]
19412              || node == ridpointers [(int) RID_BYCOPY]
19413              || node == ridpointers [(int) RID_BYREF]
19414              || node == ridpointers [(int) RID_ONEWAY]))
19415     {
19416       quals = tree_cons (NULL_TREE, node, quals);
19417       cp_lexer_consume_token (parser->lexer);
19418       token = cp_lexer_peek_token (parser->lexer);
19419       node = token->u.value;
19420     }
19421
19422   return quals;
19423 }
19424
19425 /* Parse an Objective-C typename.  */
19426
19427 static tree
19428 cp_parser_objc_typename (cp_parser* parser)
19429 {
19430   tree type_name = NULL_TREE;
19431
19432   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19433     {
19434       tree proto_quals, cp_type = NULL_TREE;
19435
19436       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19437       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19438
19439       /* An ObjC type name may consist of just protocol qualifiers, in which
19440          case the type shall default to 'id'.  */
19441       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19442         cp_type = cp_parser_type_id (parser);
19443
19444       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19445       type_name = build_tree_list (proto_quals, cp_type);
19446     }
19447
19448   return type_name;
19449 }
19450
19451 /* Check to see if TYPE refers to an Objective-C selector name.  */
19452
19453 static bool
19454 cp_parser_objc_selector_p (enum cpp_ttype type)
19455 {
19456   return (type == CPP_NAME || type == CPP_KEYWORD
19457           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19458           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19459           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19460           || type == CPP_XOR || type == CPP_XOR_EQ);
19461 }
19462
19463 /* Parse an Objective-C selector.  */
19464
19465 static tree
19466 cp_parser_objc_selector (cp_parser* parser)
19467 {
19468   cp_token *token = cp_lexer_consume_token (parser->lexer);
19469
19470   if (!cp_parser_objc_selector_p (token->type))
19471     {
19472       error ("%Hinvalid Objective-C++ selector name", &token->location);
19473       return error_mark_node;
19474     }
19475
19476   /* C++ operator names are allowed to appear in ObjC selectors.  */
19477   switch (token->type)
19478     {
19479     case CPP_AND_AND: return get_identifier ("and");
19480     case CPP_AND_EQ: return get_identifier ("and_eq");
19481     case CPP_AND: return get_identifier ("bitand");
19482     case CPP_OR: return get_identifier ("bitor");
19483     case CPP_COMPL: return get_identifier ("compl");
19484     case CPP_NOT: return get_identifier ("not");
19485     case CPP_NOT_EQ: return get_identifier ("not_eq");
19486     case CPP_OR_OR: return get_identifier ("or");
19487     case CPP_OR_EQ: return get_identifier ("or_eq");
19488     case CPP_XOR: return get_identifier ("xor");
19489     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19490     default: return token->u.value;
19491     }
19492 }
19493
19494 /* Parse an Objective-C params list.  */
19495
19496 static tree
19497 cp_parser_objc_method_keyword_params (cp_parser* parser)
19498 {
19499   tree params = NULL_TREE;
19500   bool maybe_unary_selector_p = true;
19501   cp_token *token = cp_lexer_peek_token (parser->lexer);
19502
19503   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19504     {
19505       tree selector = NULL_TREE, type_name, identifier;
19506
19507       if (token->type != CPP_COLON)
19508         selector = cp_parser_objc_selector (parser);
19509
19510       /* Detect if we have a unary selector.  */
19511       if (maybe_unary_selector_p
19512           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19513         return selector;
19514
19515       maybe_unary_selector_p = false;
19516       cp_parser_require (parser, CPP_COLON, "%<:%>");
19517       type_name = cp_parser_objc_typename (parser);
19518       identifier = cp_parser_identifier (parser);
19519
19520       params
19521         = chainon (params,
19522                    objc_build_keyword_decl (selector,
19523                                             type_name,
19524                                             identifier));
19525
19526       token = cp_lexer_peek_token (parser->lexer);
19527     }
19528
19529   return params;
19530 }
19531
19532 /* Parse the non-keyword Objective-C params.  */
19533
19534 static tree
19535 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19536 {
19537   tree params = make_node (TREE_LIST);
19538   cp_token *token = cp_lexer_peek_token (parser->lexer);
19539   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19540
19541   while (token->type == CPP_COMMA)
19542     {
19543       cp_parameter_declarator *parmdecl;
19544       tree parm;
19545
19546       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19547       token = cp_lexer_peek_token (parser->lexer);
19548
19549       if (token->type == CPP_ELLIPSIS)
19550         {
19551           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19552           *ellipsisp = true;
19553           break;
19554         }
19555
19556       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19557       parm = grokdeclarator (parmdecl->declarator,
19558                              &parmdecl->decl_specifiers,
19559                              PARM, /*initialized=*/0,
19560                              /*attrlist=*/NULL);
19561
19562       chainon (params, build_tree_list (NULL_TREE, parm));
19563       token = cp_lexer_peek_token (parser->lexer);
19564     }
19565
19566   return params;
19567 }
19568
19569 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19570
19571 static void
19572 cp_parser_objc_interstitial_code (cp_parser* parser)
19573 {
19574   cp_token *token = cp_lexer_peek_token (parser->lexer);
19575
19576   /* If the next token is `extern' and the following token is a string
19577      literal, then we have a linkage specification.  */
19578   if (token->keyword == RID_EXTERN
19579       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19580     cp_parser_linkage_specification (parser);
19581   /* Handle #pragma, if any.  */
19582   else if (token->type == CPP_PRAGMA)
19583     cp_parser_pragma (parser, pragma_external);
19584   /* Allow stray semicolons.  */
19585   else if (token->type == CPP_SEMICOLON)
19586     cp_lexer_consume_token (parser->lexer);
19587   /* Finally, try to parse a block-declaration, or a function-definition.  */
19588   else
19589     cp_parser_block_declaration (parser, /*statement_p=*/false);
19590 }
19591
19592 /* Parse a method signature.  */
19593
19594 static tree
19595 cp_parser_objc_method_signature (cp_parser* parser)
19596 {
19597   tree rettype, kwdparms, optparms;
19598   bool ellipsis = false;
19599
19600   cp_parser_objc_method_type (parser);
19601   rettype = cp_parser_objc_typename (parser);
19602   kwdparms = cp_parser_objc_method_keyword_params (parser);
19603   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19604
19605   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19606 }
19607
19608 /* Pars an Objective-C method prototype list.  */
19609
19610 static void
19611 cp_parser_objc_method_prototype_list (cp_parser* parser)
19612 {
19613   cp_token *token = cp_lexer_peek_token (parser->lexer);
19614
19615   while (token->keyword != RID_AT_END)
19616     {
19617       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19618         {
19619           objc_add_method_declaration
19620            (cp_parser_objc_method_signature (parser));
19621           cp_parser_consume_semicolon_at_end_of_statement (parser);
19622         }
19623       else
19624         /* Allow for interspersed non-ObjC++ code.  */
19625         cp_parser_objc_interstitial_code (parser);
19626
19627       token = cp_lexer_peek_token (parser->lexer);
19628     }
19629
19630   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19631   objc_finish_interface ();
19632 }
19633
19634 /* Parse an Objective-C method definition list.  */
19635
19636 static void
19637 cp_parser_objc_method_definition_list (cp_parser* parser)
19638 {
19639   cp_token *token = cp_lexer_peek_token (parser->lexer);
19640
19641   while (token->keyword != RID_AT_END)
19642     {
19643       tree meth;
19644
19645       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19646         {
19647           push_deferring_access_checks (dk_deferred);
19648           objc_start_method_definition
19649            (cp_parser_objc_method_signature (parser));
19650
19651           /* For historical reasons, we accept an optional semicolon.  */
19652           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19653             cp_lexer_consume_token (parser->lexer);
19654
19655           perform_deferred_access_checks ();
19656           stop_deferring_access_checks ();
19657           meth = cp_parser_function_definition_after_declarator (parser,
19658                                                                  false);
19659           pop_deferring_access_checks ();
19660           objc_finish_method_definition (meth);
19661         }
19662       else
19663         /* Allow for interspersed non-ObjC++ code.  */
19664         cp_parser_objc_interstitial_code (parser);
19665
19666       token = cp_lexer_peek_token (parser->lexer);
19667     }
19668
19669   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19670   objc_finish_implementation ();
19671 }
19672
19673 /* Parse Objective-C ivars.  */
19674
19675 static void
19676 cp_parser_objc_class_ivars (cp_parser* parser)
19677 {
19678   cp_token *token = cp_lexer_peek_token (parser->lexer);
19679
19680   if (token->type != CPP_OPEN_BRACE)
19681     return;     /* No ivars specified.  */
19682
19683   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19684   token = cp_lexer_peek_token (parser->lexer);
19685
19686   while (token->type != CPP_CLOSE_BRACE)
19687     {
19688       cp_decl_specifier_seq declspecs;
19689       int decl_class_or_enum_p;
19690       tree prefix_attributes;
19691
19692       cp_parser_objc_visibility_spec (parser);
19693
19694       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19695         break;
19696
19697       cp_parser_decl_specifier_seq (parser,
19698                                     CP_PARSER_FLAGS_OPTIONAL,
19699                                     &declspecs,
19700                                     &decl_class_or_enum_p);
19701       prefix_attributes = declspecs.attributes;
19702       declspecs.attributes = NULL_TREE;
19703
19704       /* Keep going until we hit the `;' at the end of the
19705          declaration.  */
19706       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19707         {
19708           tree width = NULL_TREE, attributes, first_attribute, decl;
19709           cp_declarator *declarator = NULL;
19710           int ctor_dtor_or_conv_p;
19711
19712           /* Check for a (possibly unnamed) bitfield declaration.  */
19713           token = cp_lexer_peek_token (parser->lexer);
19714           if (token->type == CPP_COLON)
19715             goto eat_colon;
19716
19717           if (token->type == CPP_NAME
19718               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19719                   == CPP_COLON))
19720             {
19721               /* Get the name of the bitfield.  */
19722               declarator = make_id_declarator (NULL_TREE,
19723                                                cp_parser_identifier (parser),
19724                                                sfk_none);
19725
19726              eat_colon:
19727               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19728               /* Get the width of the bitfield.  */
19729               width
19730                 = cp_parser_constant_expression (parser,
19731                                                  /*allow_non_constant=*/false,
19732                                                  NULL);
19733             }
19734           else
19735             {
19736               /* Parse the declarator.  */
19737               declarator
19738                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19739                                         &ctor_dtor_or_conv_p,
19740                                         /*parenthesized_p=*/NULL,
19741                                         /*member_p=*/false);
19742             }
19743
19744           /* Look for attributes that apply to the ivar.  */
19745           attributes = cp_parser_attributes_opt (parser);
19746           /* Remember which attributes are prefix attributes and
19747              which are not.  */
19748           first_attribute = attributes;
19749           /* Combine the attributes.  */
19750           attributes = chainon (prefix_attributes, attributes);
19751
19752           if (width)
19753               /* Create the bitfield declaration.  */
19754               decl = grokbitfield (declarator, &declspecs,
19755                                    width,
19756                                    attributes);
19757           else
19758             decl = grokfield (declarator, &declspecs,
19759                               NULL_TREE, /*init_const_expr_p=*/false,
19760                               NULL_TREE, attributes);
19761
19762           /* Add the instance variable.  */
19763           objc_add_instance_variable (decl);
19764
19765           /* Reset PREFIX_ATTRIBUTES.  */
19766           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19767             attributes = TREE_CHAIN (attributes);
19768           if (attributes)
19769             TREE_CHAIN (attributes) = NULL_TREE;
19770
19771           token = cp_lexer_peek_token (parser->lexer);
19772
19773           if (token->type == CPP_COMMA)
19774             {
19775               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19776               continue;
19777             }
19778           break;
19779         }
19780
19781       cp_parser_consume_semicolon_at_end_of_statement (parser);
19782       token = cp_lexer_peek_token (parser->lexer);
19783     }
19784
19785   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19786   /* For historical reasons, we accept an optional semicolon.  */
19787   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19788     cp_lexer_consume_token (parser->lexer);
19789 }
19790
19791 /* Parse an Objective-C protocol declaration.  */
19792
19793 static void
19794 cp_parser_objc_protocol_declaration (cp_parser* parser)
19795 {
19796   tree proto, protorefs;
19797   cp_token *tok;
19798
19799   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19800   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19801     {
19802       tok = cp_lexer_peek_token (parser->lexer);
19803       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19804       goto finish;
19805     }
19806
19807   /* See if we have a forward declaration or a definition.  */
19808   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19809
19810   /* Try a forward declaration first.  */
19811   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19812     {
19813       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19814      finish:
19815       cp_parser_consume_semicolon_at_end_of_statement (parser);
19816     }
19817
19818   /* Ok, we got a full-fledged definition (or at least should).  */
19819   else
19820     {
19821       proto = cp_parser_identifier (parser);
19822       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19823       objc_start_protocol (proto, protorefs);
19824       cp_parser_objc_method_prototype_list (parser);
19825     }
19826 }
19827
19828 /* Parse an Objective-C superclass or category.  */
19829
19830 static void
19831 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19832                                                           tree *categ)
19833 {
19834   cp_token *next = cp_lexer_peek_token (parser->lexer);
19835
19836   *super = *categ = NULL_TREE;
19837   if (next->type == CPP_COLON)
19838     {
19839       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19840       *super = cp_parser_identifier (parser);
19841     }
19842   else if (next->type == CPP_OPEN_PAREN)
19843     {
19844       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19845       *categ = cp_parser_identifier (parser);
19846       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19847     }
19848 }
19849
19850 /* Parse an Objective-C class interface.  */
19851
19852 static void
19853 cp_parser_objc_class_interface (cp_parser* parser)
19854 {
19855   tree name, super, categ, protos;
19856
19857   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19858   name = cp_parser_identifier (parser);
19859   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19860   protos = cp_parser_objc_protocol_refs_opt (parser);
19861
19862   /* We have either a class or a category on our hands.  */
19863   if (categ)
19864     objc_start_category_interface (name, categ, protos);
19865   else
19866     {
19867       objc_start_class_interface (name, super, protos);
19868       /* Handle instance variable declarations, if any.  */
19869       cp_parser_objc_class_ivars (parser);
19870       objc_continue_interface ();
19871     }
19872
19873   cp_parser_objc_method_prototype_list (parser);
19874 }
19875
19876 /* Parse an Objective-C class implementation.  */
19877
19878 static void
19879 cp_parser_objc_class_implementation (cp_parser* parser)
19880 {
19881   tree name, super, categ;
19882
19883   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19884   name = cp_parser_identifier (parser);
19885   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19886
19887   /* We have either a class or a category on our hands.  */
19888   if (categ)
19889     objc_start_category_implementation (name, categ);
19890   else
19891     {
19892       objc_start_class_implementation (name, super);
19893       /* Handle instance variable declarations, if any.  */
19894       cp_parser_objc_class_ivars (parser);
19895       objc_continue_implementation ();
19896     }
19897
19898   cp_parser_objc_method_definition_list (parser);
19899 }
19900
19901 /* Consume the @end token and finish off the implementation.  */
19902
19903 static void
19904 cp_parser_objc_end_implementation (cp_parser* parser)
19905 {
19906   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19907   objc_finish_implementation ();
19908 }
19909
19910 /* Parse an Objective-C declaration.  */
19911
19912 static void
19913 cp_parser_objc_declaration (cp_parser* parser)
19914 {
19915   /* Try to figure out what kind of declaration is present.  */
19916   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19917
19918   switch (kwd->keyword)
19919     {
19920     case RID_AT_ALIAS:
19921       cp_parser_objc_alias_declaration (parser);
19922       break;
19923     case RID_AT_CLASS:
19924       cp_parser_objc_class_declaration (parser);
19925       break;
19926     case RID_AT_PROTOCOL:
19927       cp_parser_objc_protocol_declaration (parser);
19928       break;
19929     case RID_AT_INTERFACE:
19930       cp_parser_objc_class_interface (parser);
19931       break;
19932     case RID_AT_IMPLEMENTATION:
19933       cp_parser_objc_class_implementation (parser);
19934       break;
19935     case RID_AT_END:
19936       cp_parser_objc_end_implementation (parser);
19937       break;
19938     default:
19939       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19940              &kwd->location, kwd->u.value);
19941       cp_parser_skip_to_end_of_block_or_statement (parser);
19942     }
19943 }
19944
19945 /* Parse an Objective-C try-catch-finally statement.
19946
19947    objc-try-catch-finally-stmt:
19948      @try compound-statement objc-catch-clause-seq [opt]
19949        objc-finally-clause [opt]
19950
19951    objc-catch-clause-seq:
19952      objc-catch-clause objc-catch-clause-seq [opt]
19953
19954    objc-catch-clause:
19955      @catch ( exception-declaration ) compound-statement
19956
19957    objc-finally-clause
19958      @finally compound-statement
19959
19960    Returns NULL_TREE.  */
19961
19962 static tree
19963 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19964   location_t location;
19965   tree stmt;
19966
19967   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19968   location = cp_lexer_peek_token (parser->lexer)->location;
19969   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19970      node, lest it get absorbed into the surrounding block.  */
19971   stmt = push_stmt_list ();
19972   cp_parser_compound_statement (parser, NULL, false);
19973   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19974
19975   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19976     {
19977       cp_parameter_declarator *parmdecl;
19978       tree parm;
19979
19980       cp_lexer_consume_token (parser->lexer);
19981       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19982       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19983       parm = grokdeclarator (parmdecl->declarator,
19984                              &parmdecl->decl_specifiers,
19985                              PARM, /*initialized=*/0,
19986                              /*attrlist=*/NULL);
19987       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19988       objc_begin_catch_clause (parm);
19989       cp_parser_compound_statement (parser, NULL, false);
19990       objc_finish_catch_clause ();
19991     }
19992
19993   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19994     {
19995       cp_lexer_consume_token (parser->lexer);
19996       location = cp_lexer_peek_token (parser->lexer)->location;
19997       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19998          node, lest it get absorbed into the surrounding block.  */
19999       stmt = push_stmt_list ();
20000       cp_parser_compound_statement (parser, NULL, false);
20001       objc_build_finally_clause (location, pop_stmt_list (stmt));
20002     }
20003
20004   return objc_finish_try_stmt ();
20005 }
20006
20007 /* Parse an Objective-C synchronized statement.
20008
20009    objc-synchronized-stmt:
20010      @synchronized ( expression ) compound-statement
20011
20012    Returns NULL_TREE.  */
20013
20014 static tree
20015 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20016   location_t location;
20017   tree lock, stmt;
20018
20019   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20020
20021   location = cp_lexer_peek_token (parser->lexer)->location;
20022   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20023   lock = cp_parser_expression (parser, false);
20024   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20025
20026   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20027      node, lest it get absorbed into the surrounding block.  */
20028   stmt = push_stmt_list ();
20029   cp_parser_compound_statement (parser, NULL, false);
20030
20031   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20032 }
20033
20034 /* Parse an Objective-C throw statement.
20035
20036    objc-throw-stmt:
20037      @throw assignment-expression [opt] ;
20038
20039    Returns a constructed '@throw' statement.  */
20040
20041 static tree
20042 cp_parser_objc_throw_statement (cp_parser *parser) {
20043   tree expr = NULL_TREE;
20044
20045   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20046
20047   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20048     expr = cp_parser_assignment_expression (parser, false);
20049
20050   cp_parser_consume_semicolon_at_end_of_statement (parser);
20051
20052   return objc_build_throw_stmt (expr);
20053 }
20054
20055 /* Parse an Objective-C statement.  */
20056
20057 static tree
20058 cp_parser_objc_statement (cp_parser * parser) {
20059   /* Try to figure out what kind of declaration is present.  */
20060   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20061
20062   switch (kwd->keyword)
20063     {
20064     case RID_AT_TRY:
20065       return cp_parser_objc_try_catch_finally_statement (parser);
20066     case RID_AT_SYNCHRONIZED:
20067       return cp_parser_objc_synchronized_statement (parser);
20068     case RID_AT_THROW:
20069       return cp_parser_objc_throw_statement (parser);
20070     default:
20071       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20072              &kwd->location, kwd->u.value);
20073       cp_parser_skip_to_end_of_block_or_statement (parser);
20074     }
20075
20076   return error_mark_node;
20077 }
20078 \f
20079 /* OpenMP 2.5 parsing routines.  */
20080
20081 /* Returns name of the next clause.
20082    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20083    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20084    returned and the token is consumed.  */
20085
20086 static pragma_omp_clause
20087 cp_parser_omp_clause_name (cp_parser *parser)
20088 {
20089   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20090
20091   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20092     result = PRAGMA_OMP_CLAUSE_IF;
20093   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20094     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20095   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20096     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20097   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20098     {
20099       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20100       const char *p = IDENTIFIER_POINTER (id);
20101
20102       switch (p[0])
20103         {
20104         case 'c':
20105           if (!strcmp ("collapse", p))
20106             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20107           else if (!strcmp ("copyin", p))
20108             result = PRAGMA_OMP_CLAUSE_COPYIN;
20109           else if (!strcmp ("copyprivate", p))
20110             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20111           break;
20112         case 'f':
20113           if (!strcmp ("firstprivate", p))
20114             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20115           break;
20116         case 'l':
20117           if (!strcmp ("lastprivate", p))
20118             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20119           break;
20120         case 'n':
20121           if (!strcmp ("nowait", p))
20122             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20123           else if (!strcmp ("num_threads", p))
20124             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20125           break;
20126         case 'o':
20127           if (!strcmp ("ordered", p))
20128             result = PRAGMA_OMP_CLAUSE_ORDERED;
20129           break;
20130         case 'r':
20131           if (!strcmp ("reduction", p))
20132             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20133           break;
20134         case 's':
20135           if (!strcmp ("schedule", p))
20136             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20137           else if (!strcmp ("shared", p))
20138             result = PRAGMA_OMP_CLAUSE_SHARED;
20139           break;
20140         case 'u':
20141           if (!strcmp ("untied", p))
20142             result = PRAGMA_OMP_CLAUSE_UNTIED;
20143           break;
20144         }
20145     }
20146
20147   if (result != PRAGMA_OMP_CLAUSE_NONE)
20148     cp_lexer_consume_token (parser->lexer);
20149
20150   return result;
20151 }
20152
20153 /* Validate that a clause of the given type does not already exist.  */
20154
20155 static void
20156 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20157                            const char *name, location_t location)
20158 {
20159   tree c;
20160
20161   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20162     if (OMP_CLAUSE_CODE (c) == code)
20163       {
20164         error ("%Htoo many %qs clauses", &location, name);
20165         break;
20166       }
20167 }
20168
20169 /* OpenMP 2.5:
20170    variable-list:
20171      identifier
20172      variable-list , identifier
20173
20174    In addition, we match a closing parenthesis.  An opening parenthesis
20175    will have been consumed by the caller.
20176
20177    If KIND is nonzero, create the appropriate node and install the decl
20178    in OMP_CLAUSE_DECL and add the node to the head of the list.
20179
20180    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20181    return the list created.  */
20182
20183 static tree
20184 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20185                                 tree list)
20186 {
20187   cp_token *token;
20188   while (1)
20189     {
20190       tree name, decl;
20191
20192       token = cp_lexer_peek_token (parser->lexer);
20193       name = cp_parser_id_expression (parser, /*template_p=*/false,
20194                                       /*check_dependency_p=*/true,
20195                                       /*template_p=*/NULL,
20196                                       /*declarator_p=*/false,
20197                                       /*optional_p=*/false);
20198       if (name == error_mark_node)
20199         goto skip_comma;
20200
20201       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20202       if (decl == error_mark_node)
20203         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20204       else if (kind != 0)
20205         {
20206           tree u = build_omp_clause (kind);
20207           OMP_CLAUSE_DECL (u) = decl;
20208           OMP_CLAUSE_CHAIN (u) = list;
20209           list = u;
20210         }
20211       else
20212         list = tree_cons (decl, NULL_TREE, list);
20213
20214     get_comma:
20215       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20216         break;
20217       cp_lexer_consume_token (parser->lexer);
20218     }
20219
20220   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20221     {
20222       int ending;
20223
20224       /* Try to resync to an unnested comma.  Copied from
20225          cp_parser_parenthesized_expression_list.  */
20226     skip_comma:
20227       ending = cp_parser_skip_to_closing_parenthesis (parser,
20228                                                       /*recovering=*/true,
20229                                                       /*or_comma=*/true,
20230                                                       /*consume_paren=*/true);
20231       if (ending < 0)
20232         goto get_comma;
20233     }
20234
20235   return list;
20236 }
20237
20238 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20239    common case for omp clauses.  */
20240
20241 static tree
20242 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20243 {
20244   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20245     return cp_parser_omp_var_list_no_open (parser, kind, list);
20246   return list;
20247 }
20248
20249 /* OpenMP 3.0:
20250    collapse ( constant-expression ) */
20251
20252 static tree
20253 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20254 {
20255   tree c, num;
20256   location_t loc;
20257   HOST_WIDE_INT n;
20258
20259   loc = cp_lexer_peek_token (parser->lexer)->location;
20260   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20261     return list;
20262
20263   num = cp_parser_constant_expression (parser, false, NULL);
20264
20265   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20266     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20267                                            /*or_comma=*/false,
20268                                            /*consume_paren=*/true);
20269
20270   if (num == error_mark_node)
20271     return list;
20272   num = fold_non_dependent_expr (num);
20273   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20274       || !host_integerp (num, 0)
20275       || (n = tree_low_cst (num, 0)) <= 0
20276       || (int) n != n)
20277     {
20278       error ("%Hcollapse argument needs positive constant integer expression",
20279              &loc);
20280       return list;
20281     }
20282
20283   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20284   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20285   OMP_CLAUSE_CHAIN (c) = list;
20286   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20287
20288   return c;
20289 }
20290
20291 /* OpenMP 2.5:
20292    default ( shared | none ) */
20293
20294 static tree
20295 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20296 {
20297   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20298   tree c;
20299
20300   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20301     return list;
20302   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20303     {
20304       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20305       const char *p = IDENTIFIER_POINTER (id);
20306
20307       switch (p[0])
20308         {
20309         case 'n':
20310           if (strcmp ("none", p) != 0)
20311             goto invalid_kind;
20312           kind = OMP_CLAUSE_DEFAULT_NONE;
20313           break;
20314
20315         case 's':
20316           if (strcmp ("shared", p) != 0)
20317             goto invalid_kind;
20318           kind = OMP_CLAUSE_DEFAULT_SHARED;
20319           break;
20320
20321         default:
20322           goto invalid_kind;
20323         }
20324
20325       cp_lexer_consume_token (parser->lexer);
20326     }
20327   else
20328     {
20329     invalid_kind:
20330       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20331     }
20332
20333   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20334     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20335                                            /*or_comma=*/false,
20336                                            /*consume_paren=*/true);
20337
20338   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20339     return list;
20340
20341   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20342   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20343   OMP_CLAUSE_CHAIN (c) = list;
20344   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20345
20346   return c;
20347 }
20348
20349 /* OpenMP 2.5:
20350    if ( expression ) */
20351
20352 static tree
20353 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20354 {
20355   tree t, c;
20356
20357   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20358     return list;
20359
20360   t = cp_parser_condition (parser);
20361
20362   if (t == error_mark_node
20363       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20364     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20365                                            /*or_comma=*/false,
20366                                            /*consume_paren=*/true);
20367
20368   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20369
20370   c = build_omp_clause (OMP_CLAUSE_IF);
20371   OMP_CLAUSE_IF_EXPR (c) = t;
20372   OMP_CLAUSE_CHAIN (c) = list;
20373
20374   return c;
20375 }
20376
20377 /* OpenMP 2.5:
20378    nowait */
20379
20380 static tree
20381 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20382                              tree list, location_t location)
20383 {
20384   tree c;
20385
20386   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20387
20388   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20389   OMP_CLAUSE_CHAIN (c) = list;
20390   return c;
20391 }
20392
20393 /* OpenMP 2.5:
20394    num_threads ( expression ) */
20395
20396 static tree
20397 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20398                                   location_t location)
20399 {
20400   tree t, c;
20401
20402   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20403     return list;
20404
20405   t = cp_parser_expression (parser, false);
20406
20407   if (t == error_mark_node
20408       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20409     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20410                                            /*or_comma=*/false,
20411                                            /*consume_paren=*/true);
20412
20413   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20414                              "num_threads", location);
20415
20416   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20417   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20418   OMP_CLAUSE_CHAIN (c) = list;
20419
20420   return c;
20421 }
20422
20423 /* OpenMP 2.5:
20424    ordered */
20425
20426 static tree
20427 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20428                               tree list, location_t location)
20429 {
20430   tree c;
20431
20432   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20433                              "ordered", location);
20434
20435   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20436   OMP_CLAUSE_CHAIN (c) = list;
20437   return c;
20438 }
20439
20440 /* OpenMP 2.5:
20441    reduction ( reduction-operator : variable-list )
20442
20443    reduction-operator:
20444      One of: + * - & ^ | && || */
20445
20446 static tree
20447 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20448 {
20449   enum tree_code code;
20450   tree nlist, c;
20451
20452   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20453     return list;
20454
20455   switch (cp_lexer_peek_token (parser->lexer)->type)
20456     {
20457     case CPP_PLUS:
20458       code = PLUS_EXPR;
20459       break;
20460     case CPP_MULT:
20461       code = MULT_EXPR;
20462       break;
20463     case CPP_MINUS:
20464       code = MINUS_EXPR;
20465       break;
20466     case CPP_AND:
20467       code = BIT_AND_EXPR;
20468       break;
20469     case CPP_XOR:
20470       code = BIT_XOR_EXPR;
20471       break;
20472     case CPP_OR:
20473       code = BIT_IOR_EXPR;
20474       break;
20475     case CPP_AND_AND:
20476       code = TRUTH_ANDIF_EXPR;
20477       break;
20478     case CPP_OR_OR:
20479       code = TRUTH_ORIF_EXPR;
20480       break;
20481     default:
20482       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20483                                "%<|%>, %<&&%>, or %<||%>");
20484     resync_fail:
20485       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20486                                              /*or_comma=*/false,
20487                                              /*consume_paren=*/true);
20488       return list;
20489     }
20490   cp_lexer_consume_token (parser->lexer);
20491
20492   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20493     goto resync_fail;
20494
20495   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20496   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20497     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20498
20499   return nlist;
20500 }
20501
20502 /* OpenMP 2.5:
20503    schedule ( schedule-kind )
20504    schedule ( schedule-kind , expression )
20505
20506    schedule-kind:
20507      static | dynamic | guided | runtime | auto  */
20508
20509 static tree
20510 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20511 {
20512   tree c, t;
20513
20514   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20515     return list;
20516
20517   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20518
20519   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20520     {
20521       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20522       const char *p = IDENTIFIER_POINTER (id);
20523
20524       switch (p[0])
20525         {
20526         case 'd':
20527           if (strcmp ("dynamic", p) != 0)
20528             goto invalid_kind;
20529           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20530           break;
20531
20532         case 'g':
20533           if (strcmp ("guided", p) != 0)
20534             goto invalid_kind;
20535           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20536           break;
20537
20538         case 'r':
20539           if (strcmp ("runtime", p) != 0)
20540             goto invalid_kind;
20541           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20542           break;
20543
20544         default:
20545           goto invalid_kind;
20546         }
20547     }
20548   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20549     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20550   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20551     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20552   else
20553     goto invalid_kind;
20554   cp_lexer_consume_token (parser->lexer);
20555
20556   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20557     {
20558       cp_token *token;
20559       cp_lexer_consume_token (parser->lexer);
20560
20561       token = cp_lexer_peek_token (parser->lexer);
20562       t = cp_parser_assignment_expression (parser, false);
20563
20564       if (t == error_mark_node)
20565         goto resync_fail;
20566       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20567         error ("%Hschedule %<runtime%> does not take "
20568                "a %<chunk_size%> parameter", &token->location);
20569       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20570         error ("%Hschedule %<auto%> does not take "
20571                "a %<chunk_size%> parameter", &token->location);
20572       else
20573         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20574
20575       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20576         goto resync_fail;
20577     }
20578   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20579     goto resync_fail;
20580
20581   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20582   OMP_CLAUSE_CHAIN (c) = list;
20583   return c;
20584
20585  invalid_kind:
20586   cp_parser_error (parser, "invalid schedule kind");
20587  resync_fail:
20588   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20589                                          /*or_comma=*/false,
20590                                          /*consume_paren=*/true);
20591   return list;
20592 }
20593
20594 /* OpenMP 3.0:
20595    untied */
20596
20597 static tree
20598 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20599                              tree list, location_t location)
20600 {
20601   tree c;
20602
20603   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20604
20605   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20606   OMP_CLAUSE_CHAIN (c) = list;
20607   return c;
20608 }
20609
20610 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20611    is a bitmask in MASK.  Return the list of clauses found; the result
20612    of clause default goes in *pdefault.  */
20613
20614 static tree
20615 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20616                            const char *where, cp_token *pragma_tok)
20617 {
20618   tree clauses = NULL;
20619   bool first = true;
20620   cp_token *token = NULL;
20621
20622   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20623     {
20624       pragma_omp_clause c_kind;
20625       const char *c_name;
20626       tree prev = clauses;
20627
20628       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20629         cp_lexer_consume_token (parser->lexer);
20630
20631       token = cp_lexer_peek_token (parser->lexer);
20632       c_kind = cp_parser_omp_clause_name (parser);
20633       first = false;
20634
20635       switch (c_kind)
20636         {
20637         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20638           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20639                                                    token->location);
20640           c_name = "collapse";
20641           break;
20642         case PRAGMA_OMP_CLAUSE_COPYIN:
20643           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20644           c_name = "copyin";
20645           break;
20646         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20647           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20648                                             clauses);
20649           c_name = "copyprivate";
20650           break;
20651         case PRAGMA_OMP_CLAUSE_DEFAULT:
20652           clauses = cp_parser_omp_clause_default (parser, clauses,
20653                                                   token->location);
20654           c_name = "default";
20655           break;
20656         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20657           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20658                                             clauses);
20659           c_name = "firstprivate";
20660           break;
20661         case PRAGMA_OMP_CLAUSE_IF:
20662           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20663           c_name = "if";
20664           break;
20665         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20666           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20667                                             clauses);
20668           c_name = "lastprivate";
20669           break;
20670         case PRAGMA_OMP_CLAUSE_NOWAIT:
20671           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20672           c_name = "nowait";
20673           break;
20674         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20675           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20676                                                       token->location);
20677           c_name = "num_threads";
20678           break;
20679         case PRAGMA_OMP_CLAUSE_ORDERED:
20680           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20681                                                   token->location);
20682           c_name = "ordered";
20683           break;
20684         case PRAGMA_OMP_CLAUSE_PRIVATE:
20685           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20686                                             clauses);
20687           c_name = "private";
20688           break;
20689         case PRAGMA_OMP_CLAUSE_REDUCTION:
20690           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20691           c_name = "reduction";
20692           break;
20693         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20694           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20695                                                    token->location);
20696           c_name = "schedule";
20697           break;
20698         case PRAGMA_OMP_CLAUSE_SHARED:
20699           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20700                                             clauses);
20701           c_name = "shared";
20702           break;
20703         case PRAGMA_OMP_CLAUSE_UNTIED:
20704           clauses = cp_parser_omp_clause_untied (parser, clauses,
20705                                                  token->location);
20706           c_name = "nowait";
20707           break;
20708         default:
20709           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20710           goto saw_error;
20711         }
20712
20713       if (((mask >> c_kind) & 1) == 0)
20714         {
20715           /* Remove the invalid clause(s) from the list to avoid
20716              confusing the rest of the compiler.  */
20717           clauses = prev;
20718           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20719         }
20720     }
20721  saw_error:
20722   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20723   return finish_omp_clauses (clauses);
20724 }
20725
20726 /* OpenMP 2.5:
20727    structured-block:
20728      statement
20729
20730    In practice, we're also interested in adding the statement to an
20731    outer node.  So it is convenient if we work around the fact that
20732    cp_parser_statement calls add_stmt.  */
20733
20734 static unsigned
20735 cp_parser_begin_omp_structured_block (cp_parser *parser)
20736 {
20737   unsigned save = parser->in_statement;
20738
20739   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20740      This preserves the "not within loop or switch" style error messages
20741      for nonsense cases like
20742         void foo() {
20743         #pragma omp single
20744           break;
20745         }
20746   */
20747   if (parser->in_statement)
20748     parser->in_statement = IN_OMP_BLOCK;
20749
20750   return save;
20751 }
20752
20753 static void
20754 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20755 {
20756   parser->in_statement = save;
20757 }
20758
20759 static tree
20760 cp_parser_omp_structured_block (cp_parser *parser)
20761 {
20762   tree stmt = begin_omp_structured_block ();
20763   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20764
20765   cp_parser_statement (parser, NULL_TREE, false, NULL);
20766
20767   cp_parser_end_omp_structured_block (parser, save);
20768   return finish_omp_structured_block (stmt);
20769 }
20770
20771 /* OpenMP 2.5:
20772    # pragma omp atomic new-line
20773      expression-stmt
20774
20775    expression-stmt:
20776      x binop= expr | x++ | ++x | x-- | --x
20777    binop:
20778      +, *, -, /, &, ^, |, <<, >>
20779
20780   where x is an lvalue expression with scalar type.  */
20781
20782 static void
20783 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20784 {
20785   tree lhs, rhs;
20786   enum tree_code code;
20787
20788   cp_parser_require_pragma_eol (parser, pragma_tok);
20789
20790   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20791                                     /*cast_p=*/false);
20792   switch (TREE_CODE (lhs))
20793     {
20794     case ERROR_MARK:
20795       goto saw_error;
20796
20797     case PREINCREMENT_EXPR:
20798     case POSTINCREMENT_EXPR:
20799       lhs = TREE_OPERAND (lhs, 0);
20800       code = PLUS_EXPR;
20801       rhs = integer_one_node;
20802       break;
20803
20804     case PREDECREMENT_EXPR:
20805     case POSTDECREMENT_EXPR:
20806       lhs = TREE_OPERAND (lhs, 0);
20807       code = MINUS_EXPR;
20808       rhs = integer_one_node;
20809       break;
20810
20811     default:
20812       switch (cp_lexer_peek_token (parser->lexer)->type)
20813         {
20814         case CPP_MULT_EQ:
20815           code = MULT_EXPR;
20816           break;
20817         case CPP_DIV_EQ:
20818           code = TRUNC_DIV_EXPR;
20819           break;
20820         case CPP_PLUS_EQ:
20821           code = PLUS_EXPR;
20822           break;
20823         case CPP_MINUS_EQ:
20824           code = MINUS_EXPR;
20825           break;
20826         case CPP_LSHIFT_EQ:
20827           code = LSHIFT_EXPR;
20828           break;
20829         case CPP_RSHIFT_EQ:
20830           code = RSHIFT_EXPR;
20831           break;
20832         case CPP_AND_EQ:
20833           code = BIT_AND_EXPR;
20834           break;
20835         case CPP_OR_EQ:
20836           code = BIT_IOR_EXPR;
20837           break;
20838         case CPP_XOR_EQ:
20839           code = BIT_XOR_EXPR;
20840           break;
20841         default:
20842           cp_parser_error (parser,
20843                            "invalid operator for %<#pragma omp atomic%>");
20844           goto saw_error;
20845         }
20846       cp_lexer_consume_token (parser->lexer);
20847
20848       rhs = cp_parser_expression (parser, false);
20849       if (rhs == error_mark_node)
20850         goto saw_error;
20851       break;
20852     }
20853   finish_omp_atomic (code, lhs, rhs);
20854   cp_parser_consume_semicolon_at_end_of_statement (parser);
20855   return;
20856
20857  saw_error:
20858   cp_parser_skip_to_end_of_block_or_statement (parser);
20859 }
20860
20861
20862 /* OpenMP 2.5:
20863    # pragma omp barrier new-line  */
20864
20865 static void
20866 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20867 {
20868   cp_parser_require_pragma_eol (parser, pragma_tok);
20869   finish_omp_barrier ();
20870 }
20871
20872 /* OpenMP 2.5:
20873    # pragma omp critical [(name)] new-line
20874      structured-block  */
20875
20876 static tree
20877 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20878 {
20879   tree stmt, name = NULL;
20880
20881   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20882     {
20883       cp_lexer_consume_token (parser->lexer);
20884
20885       name = cp_parser_identifier (parser);
20886
20887       if (name == error_mark_node
20888           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20889         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20890                                                /*or_comma=*/false,
20891                                                /*consume_paren=*/true);
20892       if (name == error_mark_node)
20893         name = NULL;
20894     }
20895   cp_parser_require_pragma_eol (parser, pragma_tok);
20896
20897   stmt = cp_parser_omp_structured_block (parser);
20898   return c_finish_omp_critical (stmt, name);
20899 }
20900
20901 /* OpenMP 2.5:
20902    # pragma omp flush flush-vars[opt] new-line
20903
20904    flush-vars:
20905      ( variable-list ) */
20906
20907 static void
20908 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20909 {
20910   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20911     (void) cp_parser_omp_var_list (parser, 0, NULL);
20912   cp_parser_require_pragma_eol (parser, pragma_tok);
20913
20914   finish_omp_flush ();
20915 }
20916
20917 /* Helper function, to parse omp for increment expression.  */
20918
20919 static tree
20920 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20921 {
20922   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20923   enum tree_code op;
20924   cp_token *token;
20925
20926   if (lhs != decl)
20927     {
20928       cp_parser_skip_to_end_of_statement (parser);
20929       return error_mark_node;
20930     }
20931
20932   token = cp_lexer_peek_token (parser->lexer);
20933   op = binops_by_token [token->type].tree_type;
20934   switch (op)
20935     {
20936     case LT_EXPR:
20937     case LE_EXPR:
20938     case GT_EXPR:
20939     case GE_EXPR:
20940       break;
20941     default:
20942       cp_parser_skip_to_end_of_statement (parser);
20943       return error_mark_node;
20944     }
20945
20946   cp_lexer_consume_token (parser->lexer);
20947   rhs = cp_parser_binary_expression (parser, false,
20948                                      PREC_RELATIONAL_EXPRESSION);
20949   if (rhs == error_mark_node
20950       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20951     {
20952       cp_parser_skip_to_end_of_statement (parser);
20953       return error_mark_node;
20954     }
20955
20956   return build2 (op, boolean_type_node, lhs, rhs);
20957 }
20958
20959 /* Helper function, to parse omp for increment expression.  */
20960
20961 static tree
20962 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20963 {
20964   cp_token *token = cp_lexer_peek_token (parser->lexer);
20965   enum tree_code op;
20966   tree lhs, rhs;
20967   cp_id_kind idk;
20968   bool decl_first;
20969
20970   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20971     {
20972       op = (token->type == CPP_PLUS_PLUS
20973             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20974       cp_lexer_consume_token (parser->lexer);
20975       lhs = cp_parser_cast_expression (parser, false, false);
20976       if (lhs != decl)
20977         return error_mark_node;
20978       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20979     }
20980
20981   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20982   if (lhs != decl)
20983     return error_mark_node;
20984
20985   token = cp_lexer_peek_token (parser->lexer);
20986   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20987     {
20988       op = (token->type == CPP_PLUS_PLUS
20989             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20990       cp_lexer_consume_token (parser->lexer);
20991       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20992     }
20993
20994   op = cp_parser_assignment_operator_opt (parser);
20995   if (op == ERROR_MARK)
20996     return error_mark_node;
20997
20998   if (op != NOP_EXPR)
20999     {
21000       rhs = cp_parser_assignment_expression (parser, false);
21001       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21002       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21003     }
21004
21005   lhs = cp_parser_binary_expression (parser, false,
21006                                      PREC_ADDITIVE_EXPRESSION);
21007   token = cp_lexer_peek_token (parser->lexer);
21008   decl_first = lhs == decl;
21009   if (decl_first)
21010     lhs = NULL_TREE;
21011   if (token->type != CPP_PLUS
21012       && token->type != CPP_MINUS)
21013     return error_mark_node;
21014
21015   do
21016     {
21017       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21018       cp_lexer_consume_token (parser->lexer);
21019       rhs = cp_parser_binary_expression (parser, false,
21020                                          PREC_ADDITIVE_EXPRESSION);
21021       token = cp_lexer_peek_token (parser->lexer);
21022       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21023         {
21024           if (lhs == NULL_TREE)
21025             {
21026               if (op == PLUS_EXPR)
21027                 lhs = rhs;
21028               else
21029                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21030             }
21031           else
21032             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21033                                      NULL, tf_warning_or_error);
21034         }
21035     }
21036   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21037
21038   if (!decl_first)
21039     {
21040       if (rhs != decl || op == MINUS_EXPR)
21041         return error_mark_node;
21042       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21043     }
21044   else
21045     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21046
21047   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21048 }
21049
21050 /* Parse the restricted form of the for statement allowed by OpenMP.  */
21051
21052 static tree
21053 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21054 {
21055   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21056   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21057   tree this_pre_body, cl;
21058   location_t loc_first;
21059   bool collapse_err = false;
21060   int i, collapse = 1, nbraces = 0;
21061
21062   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21063     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21064       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21065
21066   gcc_assert (collapse >= 1);
21067
21068   declv = make_tree_vec (collapse);
21069   initv = make_tree_vec (collapse);
21070   condv = make_tree_vec (collapse);
21071   incrv = make_tree_vec (collapse);
21072
21073   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21074
21075   for (i = 0; i < collapse; i++)
21076     {
21077       int bracecount = 0;
21078       bool add_private_clause = false;
21079       location_t loc;
21080
21081       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21082         {
21083           cp_parser_error (parser, "for statement expected");
21084           return NULL;
21085         }
21086       loc = cp_lexer_consume_token (parser->lexer)->location;
21087
21088       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21089         return NULL;
21090
21091       init = decl = real_decl = NULL;
21092       this_pre_body = push_stmt_list ();
21093       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21094         {
21095           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21096
21097              init-expr:
21098                        var = lb
21099                        integer-type var = lb
21100                        random-access-iterator-type var = lb
21101                        pointer-type var = lb
21102           */
21103           cp_decl_specifier_seq type_specifiers;
21104
21105           /* First, try to parse as an initialized declaration.  See
21106              cp_parser_condition, from whence the bulk of this is copied.  */
21107
21108           cp_parser_parse_tentatively (parser);
21109           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21110                                         &type_specifiers);
21111           if (cp_parser_parse_definitely (parser))
21112             {
21113               /* If parsing a type specifier seq succeeded, then this
21114                  MUST be a initialized declaration.  */
21115               tree asm_specification, attributes;
21116               cp_declarator *declarator;
21117
21118               declarator = cp_parser_declarator (parser,
21119                                                  CP_PARSER_DECLARATOR_NAMED,
21120                                                  /*ctor_dtor_or_conv_p=*/NULL,
21121                                                  /*parenthesized_p=*/NULL,
21122                                                  /*member_p=*/false);
21123               attributes = cp_parser_attributes_opt (parser);
21124               asm_specification = cp_parser_asm_specification_opt (parser);
21125
21126               if (declarator == cp_error_declarator) 
21127                 cp_parser_skip_to_end_of_statement (parser);
21128
21129               else 
21130                 {
21131                   tree pushed_scope, auto_node;
21132
21133                   decl = start_decl (declarator, &type_specifiers,
21134                                      SD_INITIALIZED, attributes,
21135                                      /*prefix_attributes=*/NULL_TREE,
21136                                      &pushed_scope);
21137
21138                   auto_node = type_uses_auto (TREE_TYPE (decl));
21139                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21140                     {
21141                       if (cp_lexer_next_token_is (parser->lexer, 
21142                                                   CPP_OPEN_PAREN))
21143                         error ("parenthesized initialization is not allowed in "
21144                                "OpenMP %<for%> loop");
21145                       else
21146                         /* Trigger an error.  */
21147                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21148
21149                       init = error_mark_node;
21150                       cp_parser_skip_to_end_of_statement (parser);
21151                     }
21152                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21153                            || type_dependent_expression_p (decl)
21154                            || auto_node)
21155                     {
21156                       bool is_direct_init, is_non_constant_init;
21157
21158                       init = cp_parser_initializer (parser,
21159                                                     &is_direct_init,
21160                                                     &is_non_constant_init);
21161
21162                       if (auto_node && describable_type (init))
21163                         {
21164                           TREE_TYPE (decl)
21165                             = do_auto_deduction (TREE_TYPE (decl), init,
21166                                                  auto_node);
21167
21168                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
21169                               && !type_dependent_expression_p (decl))
21170                             goto non_class;
21171                         }
21172                       
21173                       cp_finish_decl (decl, init, !is_non_constant_init,
21174                                       asm_specification,
21175                                       LOOKUP_ONLYCONVERTING);
21176                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21177                         {
21178                           for_block
21179                             = tree_cons (NULL, this_pre_body, for_block);
21180                           init = NULL_TREE;
21181                         }
21182                       else
21183                         init = pop_stmt_list (this_pre_body);
21184                       this_pre_body = NULL_TREE;
21185                     }
21186                   else
21187                     {
21188                       /* Consume '='.  */
21189                       cp_lexer_consume_token (parser->lexer);
21190                       init = cp_parser_assignment_expression (parser, false);
21191
21192                     non_class:
21193                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21194                         init = error_mark_node;
21195                       else
21196                         cp_finish_decl (decl, NULL_TREE,
21197                                         /*init_const_expr_p=*/false,
21198                                         asm_specification,
21199                                         LOOKUP_ONLYCONVERTING);
21200                     }
21201
21202                   if (pushed_scope)
21203                     pop_scope (pushed_scope);
21204                 }
21205             }
21206           else 
21207             {
21208               cp_id_kind idk;
21209               /* If parsing a type specifier sequence failed, then
21210                  this MUST be a simple expression.  */
21211               cp_parser_parse_tentatively (parser);
21212               decl = cp_parser_primary_expression (parser, false, false,
21213                                                    false, &idk);
21214               if (!cp_parser_error_occurred (parser)
21215                   && decl
21216                   && DECL_P (decl)
21217                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21218                 {
21219                   tree rhs;
21220
21221                   cp_parser_parse_definitely (parser);
21222                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21223                   rhs = cp_parser_assignment_expression (parser, false);
21224                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21225                                                          rhs,
21226                                                          tf_warning_or_error));
21227                   add_private_clause = true;
21228                 }
21229               else
21230                 {
21231                   decl = NULL;
21232                   cp_parser_abort_tentative_parse (parser);
21233                   init = cp_parser_expression (parser, false);
21234                   if (init)
21235                     {
21236                       if (TREE_CODE (init) == MODIFY_EXPR
21237                           || TREE_CODE (init) == MODOP_EXPR)
21238                         real_decl = TREE_OPERAND (init, 0);
21239                     }
21240                 }
21241             }
21242         }
21243       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21244       if (this_pre_body)
21245         {
21246           this_pre_body = pop_stmt_list (this_pre_body);
21247           if (pre_body)
21248             {
21249               tree t = pre_body;
21250               pre_body = push_stmt_list ();
21251               add_stmt (t);
21252               add_stmt (this_pre_body);
21253               pre_body = pop_stmt_list (pre_body);
21254             }
21255           else
21256             pre_body = this_pre_body;
21257         }
21258
21259       if (decl)
21260         real_decl = decl;
21261       if (par_clauses != NULL && real_decl != NULL_TREE)
21262         {
21263           tree *c;
21264           for (c = par_clauses; *c ; )
21265             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21266                 && OMP_CLAUSE_DECL (*c) == real_decl)
21267               {
21268                 error ("%Hiteration variable %qD should not be firstprivate",
21269                        &loc, real_decl);
21270                 *c = OMP_CLAUSE_CHAIN (*c);
21271               }
21272             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21273                      && OMP_CLAUSE_DECL (*c) == real_decl)
21274               {
21275                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21276                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21277                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21278                 OMP_CLAUSE_DECL (l) = real_decl;
21279                 OMP_CLAUSE_CHAIN (l) = clauses;
21280                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21281                 clauses = l;
21282                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21283                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21284                 add_private_clause = false;
21285               }
21286             else
21287               {
21288                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21289                     && OMP_CLAUSE_DECL (*c) == real_decl)
21290                   add_private_clause = false;
21291                 c = &OMP_CLAUSE_CHAIN (*c);
21292               }
21293         }
21294
21295       if (add_private_clause)
21296         {
21297           tree c;
21298           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21299             {
21300               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21301                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21302                   && OMP_CLAUSE_DECL (c) == decl)
21303                 break;
21304               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21305                        && OMP_CLAUSE_DECL (c) == decl)
21306                 error ("%Hiteration variable %qD should not be firstprivate",
21307                        &loc, decl);
21308               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21309                        && OMP_CLAUSE_DECL (c) == decl)
21310                 error ("%Hiteration variable %qD should not be reduction",
21311                        &loc, decl);
21312             }
21313           if (c == NULL)
21314             {
21315               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21316               OMP_CLAUSE_DECL (c) = decl;
21317               c = finish_omp_clauses (c);
21318               if (c)
21319                 {
21320                   OMP_CLAUSE_CHAIN (c) = clauses;
21321                   clauses = c;
21322                 }
21323             }
21324         }
21325
21326       cond = NULL;
21327       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21328         {
21329           /* If decl is an iterator, preserve LHS and RHS of the relational
21330              expr until finish_omp_for.  */
21331           if (decl
21332               && (type_dependent_expression_p (decl)
21333                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21334             cond = cp_parser_omp_for_cond (parser, decl);
21335           else
21336             cond = cp_parser_condition (parser);
21337         }
21338       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21339
21340       incr = NULL;
21341       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21342         {
21343           /* If decl is an iterator, preserve the operator on decl
21344              until finish_omp_for.  */
21345           if (decl
21346               && (type_dependent_expression_p (decl)
21347                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21348             incr = cp_parser_omp_for_incr (parser, decl);
21349           else
21350             incr = cp_parser_expression (parser, false);
21351         }
21352
21353       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21354         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21355                                                /*or_comma=*/false,
21356                                                /*consume_paren=*/true);
21357
21358       TREE_VEC_ELT (declv, i) = decl;
21359       TREE_VEC_ELT (initv, i) = init;
21360       TREE_VEC_ELT (condv, i) = cond;
21361       TREE_VEC_ELT (incrv, i) = incr;
21362
21363       if (i == collapse - 1)
21364         break;
21365
21366       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21367          in between the collapsed for loops to be still considered perfectly
21368          nested.  Hopefully the final version clarifies this.
21369          For now handle (multiple) {'s and empty statements.  */
21370       cp_parser_parse_tentatively (parser);
21371       do
21372         {
21373           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21374             break;
21375           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21376             {
21377               cp_lexer_consume_token (parser->lexer);
21378               bracecount++;
21379             }
21380           else if (bracecount
21381                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21382             cp_lexer_consume_token (parser->lexer);
21383           else
21384             {
21385               loc = cp_lexer_peek_token (parser->lexer)->location;
21386               error ("%Hnot enough collapsed for loops", &loc);
21387               collapse_err = true;
21388               cp_parser_abort_tentative_parse (parser);
21389               declv = NULL_TREE;
21390               break;
21391             }
21392         }
21393       while (1);
21394
21395       if (declv)
21396         {
21397           cp_parser_parse_definitely (parser);
21398           nbraces += bracecount;
21399         }
21400     }
21401
21402   /* Note that we saved the original contents of this flag when we entered
21403      the structured block, and so we don't need to re-save it here.  */
21404   parser->in_statement = IN_OMP_FOR;
21405
21406   /* Note that the grammar doesn't call for a structured block here,
21407      though the loop as a whole is a structured block.  */
21408   body = push_stmt_list ();
21409   cp_parser_statement (parser, NULL_TREE, false, NULL);
21410   body = pop_stmt_list (body);
21411
21412   if (declv == NULL_TREE)
21413     ret = NULL_TREE;
21414   else
21415     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21416                           pre_body, clauses);
21417
21418   while (nbraces)
21419     {
21420       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21421         {
21422           cp_lexer_consume_token (parser->lexer);
21423           nbraces--;
21424         }
21425       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21426         cp_lexer_consume_token (parser->lexer);
21427       else
21428         {
21429           if (!collapse_err)
21430             {
21431               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21432               error ("%Hcollapsed loops not perfectly nested", &loc);
21433             }
21434           collapse_err = true;
21435           cp_parser_statement_seq_opt (parser, NULL);
21436           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21437         }
21438     }
21439
21440   while (for_block)
21441     {
21442       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21443       for_block = TREE_CHAIN (for_block);
21444     }
21445
21446   return ret;
21447 }
21448
21449 /* OpenMP 2.5:
21450    #pragma omp for for-clause[optseq] new-line
21451      for-loop  */
21452
21453 #define OMP_FOR_CLAUSE_MASK                             \
21454         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21455         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21456         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21457         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21458         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21459         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21460         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21461         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21462
21463 static tree
21464 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21465 {
21466   tree clauses, sb, ret;
21467   unsigned int save;
21468
21469   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21470                                        "#pragma omp for", pragma_tok);
21471
21472   sb = begin_omp_structured_block ();
21473   save = cp_parser_begin_omp_structured_block (parser);
21474
21475   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21476
21477   cp_parser_end_omp_structured_block (parser, save);
21478   add_stmt (finish_omp_structured_block (sb));
21479
21480   return ret;
21481 }
21482
21483 /* OpenMP 2.5:
21484    # pragma omp master new-line
21485      structured-block  */
21486
21487 static tree
21488 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21489 {
21490   cp_parser_require_pragma_eol (parser, pragma_tok);
21491   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21492 }
21493
21494 /* OpenMP 2.5:
21495    # pragma omp ordered new-line
21496      structured-block  */
21497
21498 static tree
21499 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21500 {
21501   cp_parser_require_pragma_eol (parser, pragma_tok);
21502   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21503 }
21504
21505 /* OpenMP 2.5:
21506
21507    section-scope:
21508      { section-sequence }
21509
21510    section-sequence:
21511      section-directive[opt] structured-block
21512      section-sequence section-directive structured-block  */
21513
21514 static tree
21515 cp_parser_omp_sections_scope (cp_parser *parser)
21516 {
21517   tree stmt, substmt;
21518   bool error_suppress = false;
21519   cp_token *tok;
21520
21521   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21522     return NULL_TREE;
21523
21524   stmt = push_stmt_list ();
21525
21526   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21527     {
21528       unsigned save;
21529
21530       substmt = begin_omp_structured_block ();
21531       save = cp_parser_begin_omp_structured_block (parser);
21532
21533       while (1)
21534         {
21535           cp_parser_statement (parser, NULL_TREE, false, NULL);
21536
21537           tok = cp_lexer_peek_token (parser->lexer);
21538           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21539             break;
21540           if (tok->type == CPP_CLOSE_BRACE)
21541             break;
21542           if (tok->type == CPP_EOF)
21543             break;
21544         }
21545
21546       cp_parser_end_omp_structured_block (parser, save);
21547       substmt = finish_omp_structured_block (substmt);
21548       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21549       add_stmt (substmt);
21550     }
21551
21552   while (1)
21553     {
21554       tok = cp_lexer_peek_token (parser->lexer);
21555       if (tok->type == CPP_CLOSE_BRACE)
21556         break;
21557       if (tok->type == CPP_EOF)
21558         break;
21559
21560       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21561         {
21562           cp_lexer_consume_token (parser->lexer);
21563           cp_parser_require_pragma_eol (parser, tok);
21564           error_suppress = false;
21565         }
21566       else if (!error_suppress)
21567         {
21568           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21569           error_suppress = true;
21570         }
21571
21572       substmt = cp_parser_omp_structured_block (parser);
21573       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21574       add_stmt (substmt);
21575     }
21576   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21577
21578   substmt = pop_stmt_list (stmt);
21579
21580   stmt = make_node (OMP_SECTIONS);
21581   TREE_TYPE (stmt) = void_type_node;
21582   OMP_SECTIONS_BODY (stmt) = substmt;
21583
21584   add_stmt (stmt);
21585   return stmt;
21586 }
21587
21588 /* OpenMP 2.5:
21589    # pragma omp sections sections-clause[optseq] newline
21590      sections-scope  */
21591
21592 #define OMP_SECTIONS_CLAUSE_MASK                        \
21593         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21594         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21595         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21596         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21597         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21598
21599 static tree
21600 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21601 {
21602   tree clauses, ret;
21603
21604   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21605                                        "#pragma omp sections", pragma_tok);
21606
21607   ret = cp_parser_omp_sections_scope (parser);
21608   if (ret)
21609     OMP_SECTIONS_CLAUSES (ret) = clauses;
21610
21611   return ret;
21612 }
21613
21614 /* OpenMP 2.5:
21615    # pragma parallel parallel-clause new-line
21616    # pragma parallel for parallel-for-clause new-line
21617    # pragma parallel sections parallel-sections-clause new-line  */
21618
21619 #define OMP_PARALLEL_CLAUSE_MASK                        \
21620         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21621         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21622         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21623         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21624         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21625         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21626         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21627         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21628
21629 static tree
21630 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21631 {
21632   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21633   const char *p_name = "#pragma omp parallel";
21634   tree stmt, clauses, par_clause, ws_clause, block;
21635   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21636   unsigned int save;
21637
21638   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21639     {
21640       cp_lexer_consume_token (parser->lexer);
21641       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21642       p_name = "#pragma omp parallel for";
21643       mask |= OMP_FOR_CLAUSE_MASK;
21644       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21645     }
21646   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21647     {
21648       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21649       const char *p = IDENTIFIER_POINTER (id);
21650       if (strcmp (p, "sections") == 0)
21651         {
21652           cp_lexer_consume_token (parser->lexer);
21653           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21654           p_name = "#pragma omp parallel sections";
21655           mask |= OMP_SECTIONS_CLAUSE_MASK;
21656           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21657         }
21658     }
21659
21660   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21661   block = begin_omp_parallel ();
21662   save = cp_parser_begin_omp_structured_block (parser);
21663
21664   switch (p_kind)
21665     {
21666     case PRAGMA_OMP_PARALLEL:
21667       cp_parser_statement (parser, NULL_TREE, false, NULL);
21668       par_clause = clauses;
21669       break;
21670
21671     case PRAGMA_OMP_PARALLEL_FOR:
21672       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21673       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21674       break;
21675
21676     case PRAGMA_OMP_PARALLEL_SECTIONS:
21677       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21678       stmt = cp_parser_omp_sections_scope (parser);
21679       if (stmt)
21680         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21681       break;
21682
21683     default:
21684       gcc_unreachable ();
21685     }
21686
21687   cp_parser_end_omp_structured_block (parser, save);
21688   stmt = finish_omp_parallel (par_clause, block);
21689   if (p_kind != PRAGMA_OMP_PARALLEL)
21690     OMP_PARALLEL_COMBINED (stmt) = 1;
21691   return stmt;
21692 }
21693
21694 /* OpenMP 2.5:
21695    # pragma omp single single-clause[optseq] new-line
21696      structured-block  */
21697
21698 #define OMP_SINGLE_CLAUSE_MASK                          \
21699         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21700         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21701         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21702         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21703
21704 static tree
21705 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21706 {
21707   tree stmt = make_node (OMP_SINGLE);
21708   TREE_TYPE (stmt) = void_type_node;
21709
21710   OMP_SINGLE_CLAUSES (stmt)
21711     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21712                                  "#pragma omp single", pragma_tok);
21713   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21714
21715   return add_stmt (stmt);
21716 }
21717
21718 /* OpenMP 3.0:
21719    # pragma omp task task-clause[optseq] new-line
21720      structured-block  */
21721
21722 #define OMP_TASK_CLAUSE_MASK                            \
21723         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21724         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21725         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21726         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21727         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21728         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21729
21730 static tree
21731 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21732 {
21733   tree clauses, block;
21734   unsigned int save;
21735
21736   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21737                                        "#pragma omp task", pragma_tok);
21738   block = begin_omp_task ();
21739   save = cp_parser_begin_omp_structured_block (parser);
21740   cp_parser_statement (parser, NULL_TREE, false, NULL);
21741   cp_parser_end_omp_structured_block (parser, save);
21742   return finish_omp_task (clauses, block);
21743 }
21744
21745 /* OpenMP 3.0:
21746    # pragma omp taskwait new-line  */
21747
21748 static void
21749 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21750 {
21751   cp_parser_require_pragma_eol (parser, pragma_tok);
21752   finish_omp_taskwait ();
21753 }
21754
21755 /* OpenMP 2.5:
21756    # pragma omp threadprivate (variable-list) */
21757
21758 static void
21759 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21760 {
21761   tree vars;
21762
21763   vars = cp_parser_omp_var_list (parser, 0, NULL);
21764   cp_parser_require_pragma_eol (parser, pragma_tok);
21765
21766   finish_omp_threadprivate (vars);
21767 }
21768
21769 /* Main entry point to OpenMP statement pragmas.  */
21770
21771 static void
21772 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21773 {
21774   tree stmt;
21775
21776   switch (pragma_tok->pragma_kind)
21777     {
21778     case PRAGMA_OMP_ATOMIC:
21779       cp_parser_omp_atomic (parser, pragma_tok);
21780       return;
21781     case PRAGMA_OMP_CRITICAL:
21782       stmt = cp_parser_omp_critical (parser, pragma_tok);
21783       break;
21784     case PRAGMA_OMP_FOR:
21785       stmt = cp_parser_omp_for (parser, pragma_tok);
21786       break;
21787     case PRAGMA_OMP_MASTER:
21788       stmt = cp_parser_omp_master (parser, pragma_tok);
21789       break;
21790     case PRAGMA_OMP_ORDERED:
21791       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21792       break;
21793     case PRAGMA_OMP_PARALLEL:
21794       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21795       break;
21796     case PRAGMA_OMP_SECTIONS:
21797       stmt = cp_parser_omp_sections (parser, pragma_tok);
21798       break;
21799     case PRAGMA_OMP_SINGLE:
21800       stmt = cp_parser_omp_single (parser, pragma_tok);
21801       break;
21802     case PRAGMA_OMP_TASK:
21803       stmt = cp_parser_omp_task (parser, pragma_tok);
21804       break;
21805     default:
21806       gcc_unreachable ();
21807     }
21808
21809   if (stmt)
21810     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21811 }
21812 \f
21813 /* The parser.  */
21814
21815 static GTY (()) cp_parser *the_parser;
21816
21817 \f
21818 /* Special handling for the first token or line in the file.  The first
21819    thing in the file might be #pragma GCC pch_preprocess, which loads a
21820    PCH file, which is a GC collection point.  So we need to handle this
21821    first pragma without benefit of an existing lexer structure.
21822
21823    Always returns one token to the caller in *FIRST_TOKEN.  This is
21824    either the true first token of the file, or the first token after
21825    the initial pragma.  */
21826
21827 static void
21828 cp_parser_initial_pragma (cp_token *first_token)
21829 {
21830   tree name = NULL;
21831
21832   cp_lexer_get_preprocessor_token (NULL, first_token);
21833   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21834     return;
21835
21836   cp_lexer_get_preprocessor_token (NULL, first_token);
21837   if (first_token->type == CPP_STRING)
21838     {
21839       name = first_token->u.value;
21840
21841       cp_lexer_get_preprocessor_token (NULL, first_token);
21842       if (first_token->type != CPP_PRAGMA_EOL)
21843         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21844                &first_token->location);
21845     }
21846   else
21847     error ("%Hexpected string literal", &first_token->location);
21848
21849   /* Skip to the end of the pragma.  */
21850   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21851     cp_lexer_get_preprocessor_token (NULL, first_token);
21852
21853   /* Now actually load the PCH file.  */
21854   if (name)
21855     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21856
21857   /* Read one more token to return to our caller.  We have to do this
21858      after reading the PCH file in, since its pointers have to be
21859      live.  */
21860   cp_lexer_get_preprocessor_token (NULL, first_token);
21861 }
21862
21863 /* Normal parsing of a pragma token.  Here we can (and must) use the
21864    regular lexer.  */
21865
21866 static bool
21867 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21868 {
21869   cp_token *pragma_tok;
21870   unsigned int id;
21871
21872   pragma_tok = cp_lexer_consume_token (parser->lexer);
21873   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21874   parser->lexer->in_pragma = true;
21875
21876   id = pragma_tok->pragma_kind;
21877   switch (id)
21878     {
21879     case PRAGMA_GCC_PCH_PREPROCESS:
21880       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21881              &pragma_tok->location);
21882       break;
21883
21884     case PRAGMA_OMP_BARRIER:
21885       switch (context)
21886         {
21887         case pragma_compound:
21888           cp_parser_omp_barrier (parser, pragma_tok);
21889           return false;
21890         case pragma_stmt:
21891           error ("%H%<#pragma omp barrier%> may only be "
21892                  "used in compound statements", &pragma_tok->location);
21893           break;
21894         default:
21895           goto bad_stmt;
21896         }
21897       break;
21898
21899     case PRAGMA_OMP_FLUSH:
21900       switch (context)
21901         {
21902         case pragma_compound:
21903           cp_parser_omp_flush (parser, pragma_tok);
21904           return false;
21905         case pragma_stmt:
21906           error ("%H%<#pragma omp flush%> may only be "
21907                  "used in compound statements", &pragma_tok->location);
21908           break;
21909         default:
21910           goto bad_stmt;
21911         }
21912       break;
21913
21914     case PRAGMA_OMP_TASKWAIT:
21915       switch (context)
21916         {
21917         case pragma_compound:
21918           cp_parser_omp_taskwait (parser, pragma_tok);
21919           return false;
21920         case pragma_stmt:
21921           error ("%H%<#pragma omp taskwait%> may only be "
21922                  "used in compound statements",
21923                  &pragma_tok->location);
21924           break;
21925         default:
21926           goto bad_stmt;
21927         }
21928       break;
21929
21930     case PRAGMA_OMP_THREADPRIVATE:
21931       cp_parser_omp_threadprivate (parser, pragma_tok);
21932       return false;
21933
21934     case PRAGMA_OMP_ATOMIC:
21935     case PRAGMA_OMP_CRITICAL:
21936     case PRAGMA_OMP_FOR:
21937     case PRAGMA_OMP_MASTER:
21938     case PRAGMA_OMP_ORDERED:
21939     case PRAGMA_OMP_PARALLEL:
21940     case PRAGMA_OMP_SECTIONS:
21941     case PRAGMA_OMP_SINGLE:
21942     case PRAGMA_OMP_TASK:
21943       if (context == pragma_external)
21944         goto bad_stmt;
21945       cp_parser_omp_construct (parser, pragma_tok);
21946       return true;
21947
21948     case PRAGMA_OMP_SECTION:
21949       error ("%H%<#pragma omp section%> may only be used in "
21950              "%<#pragma omp sections%> construct", &pragma_tok->location);
21951       break;
21952
21953     default:
21954       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21955       c_invoke_pragma_handler (id);
21956       break;
21957
21958     bad_stmt:
21959       cp_parser_error (parser, "expected declaration specifiers");
21960       break;
21961     }
21962
21963   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21964   return false;
21965 }
21966
21967 /* The interface the pragma parsers have to the lexer.  */
21968
21969 enum cpp_ttype
21970 pragma_lex (tree *value)
21971 {
21972   cp_token *tok;
21973   enum cpp_ttype ret;
21974
21975   tok = cp_lexer_peek_token (the_parser->lexer);
21976
21977   ret = tok->type;
21978   *value = tok->u.value;
21979
21980   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21981     ret = CPP_EOF;
21982   else if (ret == CPP_STRING)
21983     *value = cp_parser_string_literal (the_parser, false, false);
21984   else
21985     {
21986       cp_lexer_consume_token (the_parser->lexer);
21987       if (ret == CPP_KEYWORD)
21988         ret = CPP_NAME;
21989     }
21990
21991   return ret;
21992 }
21993
21994 \f
21995 /* External interface.  */
21996
21997 /* Parse one entire translation unit.  */
21998
21999 void
22000 c_parse_file (void)
22001 {
22002   bool error_occurred;
22003   static bool already_called = false;
22004
22005   if (already_called)
22006     {
22007       sorry ("inter-module optimizations not implemented for C++");
22008       return;
22009     }
22010   already_called = true;
22011
22012   the_parser = cp_parser_new ();
22013   push_deferring_access_checks (flag_access_control
22014                                 ? dk_no_deferred : dk_no_check);
22015   error_occurred = cp_parser_translation_unit (the_parser);
22016   the_parser = NULL;
22017 }
22018
22019 #include "gt-cp-parser.h"