OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, tree, cp_cv_quals, tree, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       tree parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification,
1019                       tree late_return_type)
1020 {
1021   cp_declarator *declarator;
1022
1023   declarator = make_declarator (cdk_function);
1024   declarator->declarator = target;
1025   declarator->u.function.parameters = parms;
1026   declarator->u.function.qualifiers = cv_qualifiers;
1027   declarator->u.function.exception_specification = exception_specification;
1028   declarator->u.function.late_return_type = late_return_type;
1029   if (target)
1030     {
1031       declarator->parameter_pack_p = target->parameter_pack_p;
1032       target->parameter_pack_p = false;
1033     }
1034   else
1035     declarator->parameter_pack_p = false;
1036
1037   return declarator;
1038 }
1039
1040 /* Make a declarator for an array of BOUNDS elements, each of which is
1041    defined by ELEMENT.  */
1042
1043 cp_declarator *
1044 make_array_declarator (cp_declarator *element, tree bounds)
1045 {
1046   cp_declarator *declarator;
1047
1048   declarator = make_declarator (cdk_array);
1049   declarator->declarator = element;
1050   declarator->u.array.bounds = bounds;
1051   if (element)
1052     {
1053       declarator->parameter_pack_p = element->parameter_pack_p;
1054       element->parameter_pack_p = false;
1055     }
1056   else
1057     declarator->parameter_pack_p = false;
1058
1059   return declarator;
1060 }
1061
1062 /* Determine whether the declarator we've seen so far can be a
1063    parameter pack, when followed by an ellipsis.  */
1064 static bool 
1065 declarator_can_be_parameter_pack (cp_declarator *declarator)
1066 {
1067   /* Search for a declarator name, or any other declarator that goes
1068      after the point where the ellipsis could appear in a parameter
1069      pack. If we find any of these, then this declarator can not be
1070      made into a parameter pack.  */
1071   bool found = false;
1072   while (declarator && !found)
1073     {
1074       switch ((int)declarator->kind)
1075         {
1076         case cdk_id:
1077         case cdk_array:
1078           found = true;
1079           break;
1080
1081         case cdk_error:
1082           return true;
1083
1084         default:
1085           declarator = declarator->declarator;
1086           break;
1087         }
1088     }
1089
1090   return !found;
1091 }
1092
1093 cp_parameter_declarator *no_parameters;
1094
1095 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1096    DECLARATOR and DEFAULT_ARGUMENT.  */
1097
1098 cp_parameter_declarator *
1099 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1100                            cp_declarator *declarator,
1101                            tree default_argument)
1102 {
1103   cp_parameter_declarator *parameter;
1104
1105   parameter = ((cp_parameter_declarator *)
1106                alloc_declarator (sizeof (cp_parameter_declarator)));
1107   parameter->next = NULL;
1108   if (decl_specifiers)
1109     parameter->decl_specifiers = *decl_specifiers;
1110   else
1111     clear_decl_specs (&parameter->decl_specifiers);
1112   parameter->declarator = declarator;
1113   parameter->default_argument = default_argument;
1114   parameter->ellipsis_p = false;
1115
1116   return parameter;
1117 }
1118
1119 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1120
1121 static bool
1122 function_declarator_p (const cp_declarator *declarator)
1123 {
1124   while (declarator)
1125     {
1126       if (declarator->kind == cdk_function
1127           && declarator->declarator->kind == cdk_id)
1128         return true;
1129       if (declarator->kind == cdk_id
1130           || declarator->kind == cdk_error)
1131         return false;
1132       declarator = declarator->declarator;
1133     }
1134   return false;
1135 }
1136  
1137 /* The parser.  */
1138
1139 /* Overview
1140    --------
1141
1142    A cp_parser parses the token stream as specified by the C++
1143    grammar.  Its job is purely parsing, not semantic analysis.  For
1144    example, the parser breaks the token stream into declarators,
1145    expressions, statements, and other similar syntactic constructs.
1146    It does not check that the types of the expressions on either side
1147    of an assignment-statement are compatible, or that a function is
1148    not declared with a parameter of type `void'.
1149
1150    The parser invokes routines elsewhere in the compiler to perform
1151    semantic analysis and to build up the abstract syntax tree for the
1152    code processed.
1153
1154    The parser (and the template instantiation code, which is, in a
1155    way, a close relative of parsing) are the only parts of the
1156    compiler that should be calling push_scope and pop_scope, or
1157    related functions.  The parser (and template instantiation code)
1158    keeps track of what scope is presently active; everything else
1159    should simply honor that.  (The code that generates static
1160    initializers may also need to set the scope, in order to check
1161    access control correctly when emitting the initializers.)
1162
1163    Methodology
1164    -----------
1165
1166    The parser is of the standard recursive-descent variety.  Upcoming
1167    tokens in the token stream are examined in order to determine which
1168    production to use when parsing a non-terminal.  Some C++ constructs
1169    require arbitrary look ahead to disambiguate.  For example, it is
1170    impossible, in the general case, to tell whether a statement is an
1171    expression or declaration without scanning the entire statement.
1172    Therefore, the parser is capable of "parsing tentatively."  When the
1173    parser is not sure what construct comes next, it enters this mode.
1174    Then, while we attempt to parse the construct, the parser queues up
1175    error messages, rather than issuing them immediately, and saves the
1176    tokens it consumes.  If the construct is parsed successfully, the
1177    parser "commits", i.e., it issues any queued error messages and
1178    the tokens that were being preserved are permanently discarded.
1179    If, however, the construct is not parsed successfully, the parser
1180    rolls back its state completely so that it can resume parsing using
1181    a different alternative.
1182
1183    Future Improvements
1184    -------------------
1185
1186    The performance of the parser could probably be improved substantially.
1187    We could often eliminate the need to parse tentatively by looking ahead
1188    a little bit.  In some places, this approach might not entirely eliminate
1189    the need to parse tentatively, but it might still speed up the average
1190    case.  */
1191
1192 /* Flags that are passed to some parsing functions.  These values can
1193    be bitwise-ored together.  */
1194
1195 typedef enum cp_parser_flags
1196 {
1197   /* No flags.  */
1198   CP_PARSER_FLAGS_NONE = 0x0,
1199   /* The construct is optional.  If it is not present, then no error
1200      should be issued.  */
1201   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1202   /* When parsing a type-specifier, do not allow user-defined types.  */
1203   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1204 } cp_parser_flags;
1205
1206 /* The different kinds of declarators we want to parse.  */
1207
1208 typedef enum cp_parser_declarator_kind
1209 {
1210   /* We want an abstract declarator.  */
1211   CP_PARSER_DECLARATOR_ABSTRACT,
1212   /* We want a named declarator.  */
1213   CP_PARSER_DECLARATOR_NAMED,
1214   /* We don't mind, but the name must be an unqualified-id.  */
1215   CP_PARSER_DECLARATOR_EITHER
1216 } cp_parser_declarator_kind;
1217
1218 /* The precedence values used to parse binary expressions.  The minimum value
1219    of PREC must be 1, because zero is reserved to quickly discriminate
1220    binary operators from other tokens.  */
1221
1222 enum cp_parser_prec
1223 {
1224   PREC_NOT_OPERATOR,
1225   PREC_LOGICAL_OR_EXPRESSION,
1226   PREC_LOGICAL_AND_EXPRESSION,
1227   PREC_INCLUSIVE_OR_EXPRESSION,
1228   PREC_EXCLUSIVE_OR_EXPRESSION,
1229   PREC_AND_EXPRESSION,
1230   PREC_EQUALITY_EXPRESSION,
1231   PREC_RELATIONAL_EXPRESSION,
1232   PREC_SHIFT_EXPRESSION,
1233   PREC_ADDITIVE_EXPRESSION,
1234   PREC_MULTIPLICATIVE_EXPRESSION,
1235   PREC_PM_EXPRESSION,
1236   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1237 };
1238
1239 /* A mapping from a token type to a corresponding tree node type, with a
1240    precedence value.  */
1241
1242 typedef struct cp_parser_binary_operations_map_node
1243 {
1244   /* The token type.  */
1245   enum cpp_ttype token_type;
1246   /* The corresponding tree code.  */
1247   enum tree_code tree_type;
1248   /* The precedence of this operator.  */
1249   enum cp_parser_prec prec;
1250 } cp_parser_binary_operations_map_node;
1251
1252 /* The status of a tentative parse.  */
1253
1254 typedef enum cp_parser_status_kind
1255 {
1256   /* No errors have occurred.  */
1257   CP_PARSER_STATUS_KIND_NO_ERROR,
1258   /* An error has occurred.  */
1259   CP_PARSER_STATUS_KIND_ERROR,
1260   /* We are committed to this tentative parse, whether or not an error
1261      has occurred.  */
1262   CP_PARSER_STATUS_KIND_COMMITTED
1263 } cp_parser_status_kind;
1264
1265 typedef struct cp_parser_expression_stack_entry
1266 {
1267   /* Left hand side of the binary operation we are currently
1268      parsing.  */
1269   tree lhs;
1270   /* Original tree code for left hand side, if it was a binary
1271      expression itself (used for -Wparentheses).  */
1272   enum tree_code lhs_type;
1273   /* Tree code for the binary operation we are parsing.  */
1274   enum tree_code tree_type;
1275   /* Precedence of the binary operation we are parsing.  */
1276   int prec;
1277 } cp_parser_expression_stack_entry;
1278
1279 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1280    entries because precedence levels on the stack are monotonically
1281    increasing.  */
1282 typedef struct cp_parser_expression_stack_entry
1283   cp_parser_expression_stack[NUM_PREC_VALUES];
1284
1285 /* Context that is saved and restored when parsing tentatively.  */
1286 typedef struct cp_parser_context GTY (())
1287 {
1288   /* If this is a tentative parsing context, the status of the
1289      tentative parse.  */
1290   enum cp_parser_status_kind status;
1291   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1292      that are looked up in this context must be looked up both in the
1293      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1294      the context of the containing expression.  */
1295   tree object_type;
1296
1297   /* The next parsing context in the stack.  */
1298   struct cp_parser_context *next;
1299 } cp_parser_context;
1300
1301 /* Prototypes.  */
1302
1303 /* Constructors and destructors.  */
1304
1305 static cp_parser_context *cp_parser_context_new
1306   (cp_parser_context *);
1307
1308 /* Class variables.  */
1309
1310 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1311
1312 /* The operator-precedence table used by cp_parser_binary_expression.
1313    Transformed into an associative array (binops_by_token) by
1314    cp_parser_new.  */
1315
1316 static const cp_parser_binary_operations_map_node binops[] = {
1317   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1318   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1319
1320   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1322   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1323
1324   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1325   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1326
1327   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1328   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1329
1330   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1332   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1333   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1334
1335   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1336   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1337
1338   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1339
1340   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1343
1344   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1345
1346   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1347 };
1348
1349 /* The same as binops, but initialized by cp_parser_new so that
1350    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1351    for speed.  */
1352 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1353
1354 /* Constructors and destructors.  */
1355
1356 /* Construct a new context.  The context below this one on the stack
1357    is given by NEXT.  */
1358
1359 static cp_parser_context *
1360 cp_parser_context_new (cp_parser_context* next)
1361 {
1362   cp_parser_context *context;
1363
1364   /* Allocate the storage.  */
1365   if (cp_parser_context_free_list != NULL)
1366     {
1367       /* Pull the first entry from the free list.  */
1368       context = cp_parser_context_free_list;
1369       cp_parser_context_free_list = context->next;
1370       memset (context, 0, sizeof (*context));
1371     }
1372   else
1373     context = GGC_CNEW (cp_parser_context);
1374
1375   /* No errors have occurred yet in this context.  */
1376   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1377   /* If this is not the bottommost context, copy information that we
1378      need from the previous context.  */
1379   if (next)
1380     {
1381       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1382          expression, then we are parsing one in this context, too.  */
1383       context->object_type = next->object_type;
1384       /* Thread the stack.  */
1385       context->next = next;
1386     }
1387
1388   return context;
1389 }
1390
1391 /* The cp_parser structure represents the C++ parser.  */
1392
1393 typedef struct cp_parser GTY(())
1394 {
1395   /* The lexer from which we are obtaining tokens.  */
1396   cp_lexer *lexer;
1397
1398   /* The scope in which names should be looked up.  If NULL_TREE, then
1399      we look up names in the scope that is currently open in the
1400      source program.  If non-NULL, this is either a TYPE or
1401      NAMESPACE_DECL for the scope in which we should look.  It can
1402      also be ERROR_MARK, when we've parsed a bogus scope.
1403
1404      This value is not cleared automatically after a name is looked
1405      up, so we must be careful to clear it before starting a new look
1406      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1407      will look up `Z' in the scope of `X', rather than the current
1408      scope.)  Unfortunately, it is difficult to tell when name lookup
1409      is complete, because we sometimes peek at a token, look it up,
1410      and then decide not to consume it.   */
1411   tree scope;
1412
1413   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1414      last lookup took place.  OBJECT_SCOPE is used if an expression
1415      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1416      respectively.  QUALIFYING_SCOPE is used for an expression of the
1417      form "X::Y"; it refers to X.  */
1418   tree object_scope;
1419   tree qualifying_scope;
1420
1421   /* A stack of parsing contexts.  All but the bottom entry on the
1422      stack will be tentative contexts.
1423
1424      We parse tentatively in order to determine which construct is in
1425      use in some situations.  For example, in order to determine
1426      whether a statement is an expression-statement or a
1427      declaration-statement we parse it tentatively as a
1428      declaration-statement.  If that fails, we then reparse the same
1429      token stream as an expression-statement.  */
1430   cp_parser_context *context;
1431
1432   /* True if we are parsing GNU C++.  If this flag is not set, then
1433      GNU extensions are not recognized.  */
1434   bool allow_gnu_extensions_p;
1435
1436   /* TRUE if the `>' token should be interpreted as the greater-than
1437      operator.  FALSE if it is the end of a template-id or
1438      template-parameter-list. In C++0x mode, this flag also applies to
1439      `>>' tokens, which are viewed as two consecutive `>' tokens when
1440      this flag is FALSE.  */
1441   bool greater_than_is_operator_p;
1442
1443   /* TRUE if default arguments are allowed within a parameter list
1444      that starts at this point. FALSE if only a gnu extension makes
1445      them permissible.  */
1446   bool default_arg_ok_p;
1447
1448   /* TRUE if we are parsing an integral constant-expression.  See
1449      [expr.const] for a precise definition.  */
1450   bool integral_constant_expression_p;
1451
1452   /* TRUE if we are parsing an integral constant-expression -- but a
1453      non-constant expression should be permitted as well.  This flag
1454      is used when parsing an array bound so that GNU variable-length
1455      arrays are tolerated.  */
1456   bool allow_non_integral_constant_expression_p;
1457
1458   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1459      been seen that makes the expression non-constant.  */
1460   bool non_integral_constant_expression_p;
1461
1462   /* TRUE if local variable names and `this' are forbidden in the
1463      current context.  */
1464   bool local_variables_forbidden_p;
1465
1466   /* TRUE if the declaration we are parsing is part of a
1467      linkage-specification of the form `extern string-literal
1468      declaration'.  */
1469   bool in_unbraced_linkage_specification_p;
1470
1471   /* TRUE if we are presently parsing a declarator, after the
1472      direct-declarator.  */
1473   bool in_declarator_p;
1474
1475   /* TRUE if we are presently parsing a template-argument-list.  */
1476   bool in_template_argument_list_p;
1477
1478   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1479      to IN_OMP_BLOCK if parsing OpenMP structured block and
1480      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1481      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1482      iteration-statement, OpenMP block or loop within that switch.  */
1483 #define IN_SWITCH_STMT          1
1484 #define IN_ITERATION_STMT       2
1485 #define IN_OMP_BLOCK            4
1486 #define IN_OMP_FOR              8
1487 #define IN_IF_STMT             16
1488   unsigned char in_statement;
1489
1490   /* TRUE if we are presently parsing the body of a switch statement.
1491      Note that this doesn't quite overlap with in_statement above.
1492      The difference relates to giving the right sets of error messages:
1493      "case not in switch" vs "break statement used with OpenMP...".  */
1494   bool in_switch_statement_p;
1495
1496   /* TRUE if we are parsing a type-id in an expression context.  In
1497      such a situation, both "type (expr)" and "type (type)" are valid
1498      alternatives.  */
1499   bool in_type_id_in_expr_p;
1500
1501   /* TRUE if we are currently in a header file where declarations are
1502      implicitly extern "C".  */
1503   bool implicit_extern_c;
1504
1505   /* TRUE if strings in expressions should be translated to the execution
1506      character set.  */
1507   bool translate_strings_p;
1508
1509   /* TRUE if we are presently parsing the body of a function, but not
1510      a local class.  */
1511   bool in_function_body;
1512
1513   /* If non-NULL, then we are parsing a construct where new type
1514      definitions are not permitted.  The string stored here will be
1515      issued as an error message if a type is defined.  */
1516   const char *type_definition_forbidden_message;
1517
1518   /* A list of lists. The outer list is a stack, used for member
1519      functions of local classes. At each level there are two sub-list,
1520      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1521      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1522      TREE_VALUE's. The functions are chained in reverse declaration
1523      order.
1524
1525      The TREE_PURPOSE sublist contains those functions with default
1526      arguments that need post processing, and the TREE_VALUE sublist
1527      contains those functions with definitions that need post
1528      processing.
1529
1530      These lists can only be processed once the outermost class being
1531      defined is complete.  */
1532   tree unparsed_functions_queues;
1533
1534   /* The number of classes whose definitions are currently in
1535      progress.  */
1536   unsigned num_classes_being_defined;
1537
1538   /* The number of template parameter lists that apply directly to the
1539      current declaration.  */
1540   unsigned num_template_parameter_lists;
1541 } cp_parser;
1542
1543 /* Prototypes.  */
1544
1545 /* Constructors and destructors.  */
1546
1547 static cp_parser *cp_parser_new
1548   (void);
1549
1550 /* Routines to parse various constructs.
1551
1552    Those that return `tree' will return the error_mark_node (rather
1553    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1554    Sometimes, they will return an ordinary node if error-recovery was
1555    attempted, even though a parse error occurred.  So, to check
1556    whether or not a parse error occurred, you should always use
1557    cp_parser_error_occurred.  If the construct is optional (indicated
1558    either by an `_opt' in the name of the function that does the
1559    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1560    the construct is not present.  */
1561
1562 /* Lexical conventions [gram.lex]  */
1563
1564 static tree cp_parser_identifier
1565   (cp_parser *);
1566 static tree cp_parser_string_literal
1567   (cp_parser *, bool, bool);
1568
1569 /* Basic concepts [gram.basic]  */
1570
1571 static bool cp_parser_translation_unit
1572   (cp_parser *);
1573
1574 /* Expressions [gram.expr]  */
1575
1576 static tree cp_parser_primary_expression
1577   (cp_parser *, bool, bool, bool, cp_id_kind *);
1578 static tree cp_parser_id_expression
1579   (cp_parser *, bool, bool, bool *, bool, bool);
1580 static tree cp_parser_unqualified_id
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier_opt
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_qualifying_entity
1587   (cp_parser *, bool, bool, bool, bool, bool);
1588 static tree cp_parser_postfix_expression
1589   (cp_parser *, bool, bool, bool);
1590 static tree cp_parser_postfix_open_square_expression
1591   (cp_parser *, tree, bool);
1592 static tree cp_parser_postfix_dot_deref_expression
1593   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1594 static tree cp_parser_parenthesized_expression_list
1595   (cp_parser *, bool, bool, bool, bool *);
1596 static void cp_parser_pseudo_destructor_name
1597   (cp_parser *, tree *, tree *);
1598 static tree cp_parser_unary_expression
1599   (cp_parser *, bool, bool);
1600 static enum tree_code cp_parser_unary_operator
1601   (cp_token *);
1602 static tree cp_parser_new_expression
1603   (cp_parser *);
1604 static tree cp_parser_new_placement
1605   (cp_parser *);
1606 static tree cp_parser_new_type_id
1607   (cp_parser *, tree *);
1608 static cp_declarator *cp_parser_new_declarator_opt
1609   (cp_parser *);
1610 static cp_declarator *cp_parser_direct_new_declarator
1611   (cp_parser *);
1612 static tree cp_parser_new_initializer
1613   (cp_parser *);
1614 static tree cp_parser_delete_expression
1615   (cp_parser *);
1616 static tree cp_parser_cast_expression
1617   (cp_parser *, bool, bool);
1618 static tree cp_parser_binary_expression
1619   (cp_parser *, bool, enum cp_parser_prec);
1620 static tree cp_parser_question_colon_clause
1621   (cp_parser *, tree);
1622 static tree cp_parser_assignment_expression
1623   (cp_parser *, bool);
1624 static enum tree_code cp_parser_assignment_operator_opt
1625   (cp_parser *);
1626 static tree cp_parser_expression
1627   (cp_parser *, bool);
1628 static tree cp_parser_constant_expression
1629   (cp_parser *, bool, bool *);
1630 static tree cp_parser_builtin_offsetof
1631   (cp_parser *);
1632
1633 /* Statements [gram.stmt.stmt]  */
1634
1635 static void cp_parser_statement
1636   (cp_parser *, tree, bool, bool *);
1637 static void cp_parser_label_for_labeled_statement
1638   (cp_parser *);
1639 static tree cp_parser_expression_statement
1640   (cp_parser *, tree);
1641 static tree cp_parser_compound_statement
1642   (cp_parser *, tree, bool);
1643 static void cp_parser_statement_seq_opt
1644   (cp_parser *, tree);
1645 static tree cp_parser_selection_statement
1646   (cp_parser *, bool *);
1647 static tree cp_parser_condition
1648   (cp_parser *);
1649 static tree cp_parser_iteration_statement
1650   (cp_parser *);
1651 static void cp_parser_for_init_statement
1652   (cp_parser *);
1653 static tree cp_parser_jump_statement
1654   (cp_parser *);
1655 static void cp_parser_declaration_statement
1656   (cp_parser *);
1657
1658 static tree cp_parser_implicitly_scoped_statement
1659   (cp_parser *, bool *);
1660 static void cp_parser_already_scoped_statement
1661   (cp_parser *);
1662
1663 /* Declarations [gram.dcl.dcl] */
1664
1665 static void cp_parser_declaration_seq_opt
1666   (cp_parser *);
1667 static void cp_parser_declaration
1668   (cp_parser *);
1669 static void cp_parser_block_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_simple_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_decl_specifier_seq
1674   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1675 static tree cp_parser_storage_class_specifier_opt
1676   (cp_parser *);
1677 static tree cp_parser_function_specifier_opt
1678   (cp_parser *, cp_decl_specifier_seq *);
1679 static tree cp_parser_type_specifier
1680   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1681    int *, bool *);
1682 static tree cp_parser_simple_type_specifier
1683   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1684 static tree cp_parser_type_name
1685   (cp_parser *);
1686 static tree cp_parser_nonclass_name 
1687   (cp_parser* parser);
1688 static tree cp_parser_elaborated_type_specifier
1689   (cp_parser *, bool, bool);
1690 static tree cp_parser_enum_specifier
1691   (cp_parser *);
1692 static void cp_parser_enumerator_list
1693   (cp_parser *, tree);
1694 static void cp_parser_enumerator_definition
1695   (cp_parser *, tree);
1696 static tree cp_parser_namespace_name
1697   (cp_parser *);
1698 static void cp_parser_namespace_definition
1699   (cp_parser *);
1700 static void cp_parser_namespace_body
1701   (cp_parser *);
1702 static tree cp_parser_qualified_namespace_specifier
1703   (cp_parser *);
1704 static void cp_parser_namespace_alias_definition
1705   (cp_parser *);
1706 static bool cp_parser_using_declaration
1707   (cp_parser *, bool);
1708 static void cp_parser_using_directive
1709   (cp_parser *);
1710 static void cp_parser_asm_definition
1711   (cp_parser *);
1712 static void cp_parser_linkage_specification
1713   (cp_parser *);
1714 static void cp_parser_static_assert
1715   (cp_parser *, bool);
1716 static tree cp_parser_decltype
1717   (cp_parser *);
1718
1719 /* Declarators [gram.dcl.decl] */
1720
1721 static tree cp_parser_init_declarator
1722   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1723 static cp_declarator *cp_parser_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1725 static cp_declarator *cp_parser_direct_declarator
1726   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1727 static enum tree_code cp_parser_ptr_operator
1728   (cp_parser *, tree *, cp_cv_quals *);
1729 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1730   (cp_parser *);
1731 static tree cp_parser_late_return_type_opt
1732   (cp_parser *);
1733 static tree cp_parser_declarator_id
1734   (cp_parser *, bool);
1735 static tree cp_parser_type_id
1736   (cp_parser *);
1737 static void cp_parser_type_specifier_seq
1738   (cp_parser *, bool, cp_decl_specifier_seq *);
1739 static tree cp_parser_parameter_declaration_clause
1740   (cp_parser *);
1741 static tree cp_parser_parameter_declaration_list
1742   (cp_parser *, bool *);
1743 static cp_parameter_declarator *cp_parser_parameter_declaration
1744   (cp_parser *, bool, bool *);
1745 static tree cp_parser_default_argument 
1746   (cp_parser *, bool);
1747 static void cp_parser_function_body
1748   (cp_parser *);
1749 static tree cp_parser_initializer
1750   (cp_parser *, bool *, bool *);
1751 static tree cp_parser_initializer_clause
1752   (cp_parser *, bool *);
1753 static tree cp_parser_braced_list
1754   (cp_parser*, bool*);
1755 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1756   (cp_parser *, bool *);
1757
1758 static bool cp_parser_ctor_initializer_opt_and_function_body
1759   (cp_parser *);
1760
1761 /* Classes [gram.class] */
1762
1763 static tree cp_parser_class_name
1764   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1765 static tree cp_parser_class_specifier
1766   (cp_parser *);
1767 static tree cp_parser_class_head
1768   (cp_parser *, bool *, tree *, tree *);
1769 static enum tag_types cp_parser_class_key
1770   (cp_parser *);
1771 static void cp_parser_member_specification_opt
1772   (cp_parser *);
1773 static void cp_parser_member_declaration
1774   (cp_parser *);
1775 static tree cp_parser_pure_specifier
1776   (cp_parser *);
1777 static tree cp_parser_constant_initializer
1778   (cp_parser *);
1779
1780 /* Derived classes [gram.class.derived] */
1781
1782 static tree cp_parser_base_clause
1783   (cp_parser *);
1784 static tree cp_parser_base_specifier
1785   (cp_parser *);
1786
1787 /* Special member functions [gram.special] */
1788
1789 static tree cp_parser_conversion_function_id
1790   (cp_parser *);
1791 static tree cp_parser_conversion_type_id
1792   (cp_parser *);
1793 static cp_declarator *cp_parser_conversion_declarator_opt
1794   (cp_parser *);
1795 static bool cp_parser_ctor_initializer_opt
1796   (cp_parser *);
1797 static void cp_parser_mem_initializer_list
1798   (cp_parser *);
1799 static tree cp_parser_mem_initializer
1800   (cp_parser *);
1801 static tree cp_parser_mem_initializer_id
1802   (cp_parser *);
1803
1804 /* Overloading [gram.over] */
1805
1806 static tree cp_parser_operator_function_id
1807   (cp_parser *);
1808 static tree cp_parser_operator
1809   (cp_parser *);
1810
1811 /* Templates [gram.temp] */
1812
1813 static void cp_parser_template_declaration
1814   (cp_parser *, bool);
1815 static tree cp_parser_template_parameter_list
1816   (cp_parser *);
1817 static tree cp_parser_template_parameter
1818   (cp_parser *, bool *, bool *);
1819 static tree cp_parser_type_parameter
1820   (cp_parser *, bool *);
1821 static tree cp_parser_template_id
1822   (cp_parser *, bool, bool, bool);
1823 static tree cp_parser_template_name
1824   (cp_parser *, bool, bool, bool, bool *);
1825 static tree cp_parser_template_argument_list
1826   (cp_parser *);
1827 static tree cp_parser_template_argument
1828   (cp_parser *);
1829 static void cp_parser_explicit_instantiation
1830   (cp_parser *);
1831 static void cp_parser_explicit_specialization
1832   (cp_parser *);
1833
1834 /* Exception handling [gram.exception] */
1835
1836 static tree cp_parser_try_block
1837   (cp_parser *);
1838 static bool cp_parser_function_try_block
1839   (cp_parser *);
1840 static void cp_parser_handler_seq
1841   (cp_parser *);
1842 static void cp_parser_handler
1843   (cp_parser *);
1844 static tree cp_parser_exception_declaration
1845   (cp_parser *);
1846 static tree cp_parser_throw_expression
1847   (cp_parser *);
1848 static tree cp_parser_exception_specification_opt
1849   (cp_parser *);
1850 static tree cp_parser_type_id_list
1851   (cp_parser *);
1852
1853 /* GNU Extensions */
1854
1855 static tree cp_parser_asm_specification_opt
1856   (cp_parser *);
1857 static tree cp_parser_asm_operand_list
1858   (cp_parser *);
1859 static tree cp_parser_asm_clobber_list
1860   (cp_parser *);
1861 static tree cp_parser_attributes_opt
1862   (cp_parser *);
1863 static tree cp_parser_attribute_list
1864   (cp_parser *);
1865 static bool cp_parser_extension_opt
1866   (cp_parser *, int *);
1867 static void cp_parser_label_declaration
1868   (cp_parser *);
1869
1870 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1871 static bool cp_parser_pragma
1872   (cp_parser *, enum pragma_context);
1873
1874 /* Objective-C++ Productions */
1875
1876 static tree cp_parser_objc_message_receiver
1877   (cp_parser *);
1878 static tree cp_parser_objc_message_args
1879   (cp_parser *);
1880 static tree cp_parser_objc_message_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_encode_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_defs_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_protocol_expression
1887   (cp_parser *);
1888 static tree cp_parser_objc_selector_expression
1889   (cp_parser *);
1890 static tree cp_parser_objc_expression
1891   (cp_parser *);
1892 static bool cp_parser_objc_selector_p
1893   (enum cpp_ttype);
1894 static tree cp_parser_objc_selector
1895   (cp_parser *);
1896 static tree cp_parser_objc_protocol_refs_opt
1897   (cp_parser *);
1898 static void cp_parser_objc_declaration
1899   (cp_parser *);
1900 static tree cp_parser_objc_statement
1901   (cp_parser *);
1902
1903 /* Utility Routines */
1904
1905 static tree cp_parser_lookup_name
1906   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1907 static tree cp_parser_lookup_name_simple
1908   (cp_parser *, tree, location_t);
1909 static tree cp_parser_maybe_treat_template_as_class
1910   (tree, bool);
1911 static bool cp_parser_check_declarator_template_parameters
1912   (cp_parser *, cp_declarator *, location_t);
1913 static bool cp_parser_check_template_parameters
1914   (cp_parser *, unsigned, location_t);
1915 static tree cp_parser_simple_cast_expression
1916   (cp_parser *);
1917 static tree cp_parser_global_scope_opt
1918   (cp_parser *, bool);
1919 static bool cp_parser_constructor_declarator_p
1920   (cp_parser *, bool);
1921 static tree cp_parser_function_definition_from_specifiers_and_declarator
1922   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1923 static tree cp_parser_function_definition_after_declarator
1924   (cp_parser *, bool);
1925 static void cp_parser_template_declaration_after_export
1926   (cp_parser *, bool);
1927 static void cp_parser_perform_template_parameter_access_checks
1928   (VEC (deferred_access_check,gc)*);
1929 static tree cp_parser_single_declaration
1930   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1931 static tree cp_parser_functional_cast
1932   (cp_parser *, tree);
1933 static tree cp_parser_save_member_function_body
1934   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1935 static tree cp_parser_enclosed_template_argument_list
1936   (cp_parser *);
1937 static void cp_parser_save_default_args
1938   (cp_parser *, tree);
1939 static void cp_parser_late_parsing_for_member
1940   (cp_parser *, tree);
1941 static void cp_parser_late_parsing_default_args
1942   (cp_parser *, tree);
1943 static tree cp_parser_sizeof_operand
1944   (cp_parser *, enum rid);
1945 static tree cp_parser_trait_expr
1946   (cp_parser *, enum rid);
1947 static bool cp_parser_declares_only_class_p
1948   (cp_parser *);
1949 static void cp_parser_set_storage_class
1950   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1951 static void cp_parser_set_decl_spec_type
1952   (cp_decl_specifier_seq *, tree, location_t, bool);
1953 static bool cp_parser_friend_p
1954   (const cp_decl_specifier_seq *);
1955 static cp_token *cp_parser_require
1956   (cp_parser *, enum cpp_ttype, const char *);
1957 static cp_token *cp_parser_require_keyword
1958   (cp_parser *, enum rid, const char *);
1959 static bool cp_parser_token_starts_function_definition_p
1960   (cp_token *);
1961 static bool cp_parser_next_token_starts_class_definition_p
1962   (cp_parser *);
1963 static bool cp_parser_next_token_ends_template_argument_p
1964   (cp_parser *);
1965 static bool cp_parser_nth_token_starts_template_argument_list_p
1966   (cp_parser *, size_t);
1967 static enum tag_types cp_parser_token_is_class_key
1968   (cp_token *);
1969 static void cp_parser_check_class_key
1970   (enum tag_types, tree type);
1971 static void cp_parser_check_access_in_redeclaration
1972   (tree type, location_t location);
1973 static bool cp_parser_optional_template_keyword
1974   (cp_parser *);
1975 static void cp_parser_pre_parsed_nested_name_specifier
1976   (cp_parser *);
1977 static bool cp_parser_cache_group
1978   (cp_parser *, enum cpp_ttype, unsigned);
1979 static void cp_parser_parse_tentatively
1980   (cp_parser *);
1981 static void cp_parser_commit_to_tentative_parse
1982   (cp_parser *);
1983 static void cp_parser_abort_tentative_parse
1984   (cp_parser *);
1985 static bool cp_parser_parse_definitely
1986   (cp_parser *);
1987 static inline bool cp_parser_parsing_tentatively
1988   (cp_parser *);
1989 static bool cp_parser_uncommitted_to_tentative_parse_p
1990   (cp_parser *);
1991 static void cp_parser_error
1992   (cp_parser *, const char *);
1993 static void cp_parser_name_lookup_error
1994   (cp_parser *, tree, tree, const char *, location_t);
1995 static bool cp_parser_simulate_error
1996   (cp_parser *);
1997 static bool cp_parser_check_type_definition
1998   (cp_parser *);
1999 static void cp_parser_check_for_definition_in_return_type
2000   (cp_declarator *, tree, location_t type_location);
2001 static void cp_parser_check_for_invalid_template_id
2002   (cp_parser *, tree, location_t location);
2003 static bool cp_parser_non_integral_constant_expression
2004   (cp_parser *, const char *);
2005 static void cp_parser_diagnose_invalid_type_name
2006   (cp_parser *, tree, tree, location_t);
2007 static bool cp_parser_parse_and_diagnose_invalid_type_name
2008   (cp_parser *);
2009 static int cp_parser_skip_to_closing_parenthesis
2010   (cp_parser *, bool, bool, bool);
2011 static void cp_parser_skip_to_end_of_statement
2012   (cp_parser *);
2013 static void cp_parser_consume_semicolon_at_end_of_statement
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_block_or_statement
2016   (cp_parser *);
2017 static bool cp_parser_skip_to_closing_brace
2018   (cp_parser *);
2019 static void cp_parser_skip_to_end_of_template_parameter_list
2020   (cp_parser *);
2021 static void cp_parser_skip_to_pragma_eol
2022   (cp_parser*, cp_token *);
2023 static bool cp_parser_error_occurred
2024   (cp_parser *);
2025 static bool cp_parser_allow_gnu_extensions_p
2026   (cp_parser *);
2027 static bool cp_parser_is_string_literal
2028   (cp_token *);
2029 static bool cp_parser_is_keyword
2030   (cp_token *, enum rid);
2031 static tree cp_parser_make_typename_type
2032   (cp_parser *, tree, tree, location_t location);
2033 static cp_declarator * cp_parser_make_indirect_declarator
2034   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2035
2036 /* Returns nonzero if we are parsing tentatively.  */
2037
2038 static inline bool
2039 cp_parser_parsing_tentatively (cp_parser* parser)
2040 {
2041   return parser->context->next != NULL;
2042 }
2043
2044 /* Returns nonzero if TOKEN is a string literal.  */
2045
2046 static bool
2047 cp_parser_is_string_literal (cp_token* token)
2048 {
2049   return (token->type == CPP_STRING ||
2050           token->type == CPP_STRING16 ||
2051           token->type == CPP_STRING32 ||
2052           token->type == CPP_WSTRING);
2053 }
2054
2055 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2056
2057 static bool
2058 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2059 {
2060   return token->keyword == keyword;
2061 }
2062
2063 /* If not parsing tentatively, issue a diagnostic of the form
2064       FILE:LINE: MESSAGE before TOKEN
2065    where TOKEN is the next token in the input stream.  MESSAGE
2066    (specified by the caller) is usually of the form "expected
2067    OTHER-TOKEN".  */
2068
2069 static void
2070 cp_parser_error (cp_parser* parser, const char* message)
2071 {
2072   if (!cp_parser_simulate_error (parser))
2073     {
2074       cp_token *token = cp_lexer_peek_token (parser->lexer);
2075       /* This diagnostic makes more sense if it is tagged to the line
2076          of the token we just peeked at.  */
2077       cp_lexer_set_source_position_from_token (token);
2078
2079       if (token->type == CPP_PRAGMA)
2080         {
2081           error ("%H%<#pragma%> is not allowed here", &token->location);
2082           cp_parser_skip_to_pragma_eol (parser, token);
2083           return;
2084         }
2085
2086       c_parse_error (message,
2087                      /* Because c_parser_error does not understand
2088                         CPP_KEYWORD, keywords are treated like
2089                         identifiers.  */
2090                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2091                      token->u.value);
2092     }
2093 }
2094
2095 /* Issue an error about name-lookup failing.  NAME is the
2096    IDENTIFIER_NODE DECL is the result of
2097    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2098    the thing that we hoped to find.  */
2099
2100 static void
2101 cp_parser_name_lookup_error (cp_parser* parser,
2102                              tree name,
2103                              tree decl,
2104                              const char* desired,
2105                              location_t location)
2106 {
2107   /* If name lookup completely failed, tell the user that NAME was not
2108      declared.  */
2109   if (decl == error_mark_node)
2110     {
2111       if (parser->scope && parser->scope != global_namespace)
2112         error ("%H%<%E::%E%> has not been declared",
2113                &location, parser->scope, name);
2114       else if (parser->scope == global_namespace)
2115         error ("%H%<::%E%> has not been declared", &location, name);
2116       else if (parser->object_scope
2117                && !CLASS_TYPE_P (parser->object_scope))
2118         error ("%Hrequest for member %qE in non-class type %qT",
2119                &location, name, parser->object_scope);
2120       else if (parser->object_scope)
2121         error ("%H%<%T::%E%> has not been declared",
2122                &location, parser->object_scope, name);
2123       else
2124         error ("%H%qE has not been declared", &location, name);
2125     }
2126   else if (parser->scope && parser->scope != global_namespace)
2127     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2128   else if (parser->scope == global_namespace)
2129     error ("%H%<::%E%> %s", &location, name, desired);
2130   else
2131     error ("%H%qE %s", &location, name, desired);
2132 }
2133
2134 /* If we are parsing tentatively, remember that an error has occurred
2135    during this tentative parse.  Returns true if the error was
2136    simulated; false if a message should be issued by the caller.  */
2137
2138 static bool
2139 cp_parser_simulate_error (cp_parser* parser)
2140 {
2141   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2142     {
2143       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2144       return true;
2145     }
2146   return false;
2147 }
2148
2149 /* Check for repeated decl-specifiers.  */
2150
2151 static void
2152 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2153                            location_t location)
2154 {
2155   cp_decl_spec ds;
2156
2157   for (ds = ds_first; ds != ds_last; ++ds)
2158     {
2159       unsigned count = decl_specs->specs[(int)ds];
2160       if (count < 2)
2161         continue;
2162       /* The "long" specifier is a special case because of "long long".  */
2163       if (ds == ds_long)
2164         {
2165           if (count > 2)
2166             error ("%H%<long long long%> is too long for GCC", &location);
2167           else if (pedantic && !in_system_header && warn_long_long
2168                    && cxx_dialect == cxx98)
2169             pedwarn (location, OPT_Wlong_long, 
2170                      "ISO C++ 1998 does not support %<long long%>");
2171         }
2172       else if (count > 1)
2173         {
2174           static const char *const decl_spec_names[] = {
2175             "signed",
2176             "unsigned",
2177             "short",
2178             "long",
2179             "const",
2180             "volatile",
2181             "restrict",
2182             "inline",
2183             "virtual",
2184             "explicit",
2185             "friend",
2186             "typedef",
2187             "__complex",
2188             "__thread"
2189           };
2190           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2191         }
2192     }
2193 }
2194
2195 /* This function is called when a type is defined.  If type
2196    definitions are forbidden at this point, an error message is
2197    issued.  */
2198
2199 static bool
2200 cp_parser_check_type_definition (cp_parser* parser)
2201 {
2202   /* If types are forbidden here, issue a message.  */
2203   if (parser->type_definition_forbidden_message)
2204     {
2205       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2206          in the message need to be interpreted.  */
2207       error (parser->type_definition_forbidden_message);
2208       return false;
2209     }
2210   return true;
2211 }
2212
2213 /* This function is called when the DECLARATOR is processed.  The TYPE
2214    was a type defined in the decl-specifiers.  If it is invalid to
2215    define a type in the decl-specifiers for DECLARATOR, an error is
2216    issued. TYPE_LOCATION is the location of TYPE and is used
2217    for error reporting.  */
2218
2219 static void
2220 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2221                                                tree type, location_t type_location)
2222 {
2223   /* [dcl.fct] forbids type definitions in return types.
2224      Unfortunately, it's not easy to know whether or not we are
2225      processing a return type until after the fact.  */
2226   while (declarator
2227          && (declarator->kind == cdk_pointer
2228              || declarator->kind == cdk_reference
2229              || declarator->kind == cdk_ptrmem))
2230     declarator = declarator->declarator;
2231   if (declarator
2232       && declarator->kind == cdk_function)
2233     {
2234       error ("%Hnew types may not be defined in a return type", &type_location);
2235       inform (type_location, 
2236               "(perhaps a semicolon is missing after the definition of %qT)",
2237               type);
2238     }
2239 }
2240
2241 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2242    "<" in any valid C++ program.  If the next token is indeed "<",
2243    issue a message warning the user about what appears to be an
2244    invalid attempt to form a template-id. LOCATION is the location
2245    of the type-specifier (TYPE) */
2246
2247 static void
2248 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2249                                          tree type, location_t location)
2250 {
2251   cp_token_position start = 0;
2252
2253   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2254     {
2255       if (TYPE_P (type))
2256         error ("%H%qT is not a template", &location, type);
2257       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2258         error ("%H%qE is not a template", &location, type);
2259       else
2260         error ("%Hinvalid template-id", &location);
2261       /* Remember the location of the invalid "<".  */
2262       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2263         start = cp_lexer_token_position (parser->lexer, true);
2264       /* Consume the "<".  */
2265       cp_lexer_consume_token (parser->lexer);
2266       /* Parse the template arguments.  */
2267       cp_parser_enclosed_template_argument_list (parser);
2268       /* Permanently remove the invalid template arguments so that
2269          this error message is not issued again.  */
2270       if (start)
2271         cp_lexer_purge_tokens_after (parser->lexer, start);
2272     }
2273 }
2274
2275 /* If parsing an integral constant-expression, issue an error message
2276    about the fact that THING appeared and return true.  Otherwise,
2277    return false.  In either case, set
2278    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2279
2280 static bool
2281 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2282                                             const char *thing)
2283 {
2284   parser->non_integral_constant_expression_p = true;
2285   if (parser->integral_constant_expression_p)
2286     {
2287       if (!parser->allow_non_integral_constant_expression_p)
2288         {
2289           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2290              in the message need to be interpreted.  */
2291           char *message = concat (thing,
2292                                   " cannot appear in a constant-expression",
2293                                   NULL);
2294           error (message);
2295           free (message);
2296           return true;
2297         }
2298     }
2299   return false;
2300 }
2301
2302 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2303    qualifying scope (or NULL, if none) for ID.  This function commits
2304    to the current active tentative parse, if any.  (Otherwise, the
2305    problematic construct might be encountered again later, resulting
2306    in duplicate error messages.) LOCATION is the location of ID.  */
2307
2308 static void
2309 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2310                                       tree scope, tree id,
2311                                       location_t location)
2312 {
2313   tree decl, old_scope;
2314   /* Try to lookup the identifier.  */
2315   old_scope = parser->scope;
2316   parser->scope = scope;
2317   decl = cp_parser_lookup_name_simple (parser, id, location);
2318   parser->scope = old_scope;
2319   /* If the lookup found a template-name, it means that the user forgot
2320   to specify an argument list. Emit a useful error message.  */
2321   if (TREE_CODE (decl) == TEMPLATE_DECL)
2322     error ("%Hinvalid use of template-name %qE without an argument list",
2323            &location, decl);
2324   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2325     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2326   else if (TREE_CODE (decl) == TYPE_DECL)
2327     /* Something like 'unsigned A a;'  */
2328     error ("%Hinvalid combination of multiple type-specifiers",
2329            &location);
2330   else if (!parser->scope)
2331     {
2332       /* Issue an error message.  */
2333       error ("%H%qE does not name a type", &location, id);
2334       /* If we're in a template class, it's possible that the user was
2335          referring to a type from a base class.  For example:
2336
2337            template <typename T> struct A { typedef T X; };
2338            template <typename T> struct B : public A<T> { X x; };
2339
2340          The user should have said "typename A<T>::X".  */
2341       if (processing_template_decl && current_class_type
2342           && TYPE_BINFO (current_class_type))
2343         {
2344           tree b;
2345
2346           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2347                b;
2348                b = TREE_CHAIN (b))
2349             {
2350               tree base_type = BINFO_TYPE (b);
2351               if (CLASS_TYPE_P (base_type)
2352                   && dependent_type_p (base_type))
2353                 {
2354                   tree field;
2355                   /* Go from a particular instantiation of the
2356                      template (which will have an empty TYPE_FIELDs),
2357                      to the main version.  */
2358                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2359                   for (field = TYPE_FIELDS (base_type);
2360                        field;
2361                        field = TREE_CHAIN (field))
2362                     if (TREE_CODE (field) == TYPE_DECL
2363                         && DECL_NAME (field) == id)
2364                       {
2365                         inform (location, 
2366                                 "(perhaps %<typename %T::%E%> was intended)",
2367                                 BINFO_TYPE (b), id);
2368                         break;
2369                       }
2370                   if (field)
2371                     break;
2372                 }
2373             }
2374         }
2375     }
2376   /* Here we diagnose qualified-ids where the scope is actually correct,
2377      but the identifier does not resolve to a valid type name.  */
2378   else if (parser->scope != error_mark_node)
2379     {
2380       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2381         error ("%H%qE in namespace %qE does not name a type",
2382                &location, id, parser->scope);
2383       else if (TYPE_P (parser->scope))
2384         error ("%H%qE in class %qT does not name a type",
2385                &location, id, parser->scope);
2386       else
2387         gcc_unreachable ();
2388     }
2389   cp_parser_commit_to_tentative_parse (parser);
2390 }
2391
2392 /* Check for a common situation where a type-name should be present,
2393    but is not, and issue a sensible error message.  Returns true if an
2394    invalid type-name was detected.
2395
2396    The situation handled by this function are variable declarations of the
2397    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2398    Usually, `ID' should name a type, but if we got here it means that it
2399    does not. We try to emit the best possible error message depending on
2400    how exactly the id-expression looks like.  */
2401
2402 static bool
2403 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2404 {
2405   tree id;
2406   cp_token *token = cp_lexer_peek_token (parser->lexer);
2407
2408   cp_parser_parse_tentatively (parser);
2409   id = cp_parser_id_expression (parser,
2410                                 /*template_keyword_p=*/false,
2411                                 /*check_dependency_p=*/true,
2412                                 /*template_p=*/NULL,
2413                                 /*declarator_p=*/true,
2414                                 /*optional_p=*/false);
2415   /* After the id-expression, there should be a plain identifier,
2416      otherwise this is not a simple variable declaration. Also, if
2417      the scope is dependent, we cannot do much.  */
2418   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2419       || (parser->scope && TYPE_P (parser->scope)
2420           && dependent_type_p (parser->scope))
2421       || TREE_CODE (id) == TYPE_DECL)
2422     {
2423       cp_parser_abort_tentative_parse (parser);
2424       return false;
2425     }
2426   if (!cp_parser_parse_definitely (parser))
2427     return false;
2428
2429   /* Emit a diagnostic for the invalid type.  */
2430   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2431                                         id, token->location);
2432   /* Skip to the end of the declaration; there's no point in
2433      trying to process it.  */
2434   cp_parser_skip_to_end_of_block_or_statement (parser);
2435   return true;
2436 }
2437
2438 /* Consume tokens up to, and including, the next non-nested closing `)'.
2439    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2440    are doing error recovery. Returns -1 if OR_COMMA is true and we
2441    found an unnested comma.  */
2442
2443 static int
2444 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445                                        bool recovering,
2446                                        bool or_comma,
2447                                        bool consume_paren)
2448 {
2449   unsigned paren_depth = 0;
2450   unsigned brace_depth = 0;
2451
2452   if (recovering && !or_comma
2453       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454     return 0;
2455
2456   while (true)
2457     {
2458       cp_token * token = cp_lexer_peek_token (parser->lexer);
2459
2460       switch (token->type)
2461         {
2462         case CPP_EOF:
2463         case CPP_PRAGMA_EOL:
2464           /* If we've run out of tokens, then there is no closing `)'.  */
2465           return 0;
2466
2467         case CPP_SEMICOLON:
2468           /* This matches the processing in skip_to_end_of_statement.  */
2469           if (!brace_depth)
2470             return 0;
2471           break;
2472
2473         case CPP_OPEN_BRACE:
2474           ++brace_depth;
2475           break;
2476         case CPP_CLOSE_BRACE:
2477           if (!brace_depth--)
2478             return 0;
2479           break;
2480
2481         case CPP_COMMA:
2482           if (recovering && or_comma && !brace_depth && !paren_depth)
2483             return -1;
2484           break;
2485
2486         case CPP_OPEN_PAREN:
2487           if (!brace_depth)
2488             ++paren_depth;
2489           break;
2490
2491         case CPP_CLOSE_PAREN:
2492           if (!brace_depth && !paren_depth--)
2493             {
2494               if (consume_paren)
2495                 cp_lexer_consume_token (parser->lexer);
2496               return 1;
2497             }
2498           break;
2499
2500         default:
2501           break;
2502         }
2503
2504       /* Consume the token.  */
2505       cp_lexer_consume_token (parser->lexer);
2506     }
2507 }
2508
2509 /* Consume tokens until we reach the end of the current statement.
2510    Normally, that will be just before consuming a `;'.  However, if a
2511    non-nested `}' comes first, then we stop before consuming that.  */
2512
2513 static void
2514 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2515 {
2516   unsigned nesting_depth = 0;
2517
2518   while (true)
2519     {
2520       cp_token *token = cp_lexer_peek_token (parser->lexer);
2521
2522       switch (token->type)
2523         {
2524         case CPP_EOF:
2525         case CPP_PRAGMA_EOL:
2526           /* If we've run out of tokens, stop.  */
2527           return;
2528
2529         case CPP_SEMICOLON:
2530           /* If the next token is a `;', we have reached the end of the
2531              statement.  */
2532           if (!nesting_depth)
2533             return;
2534           break;
2535
2536         case CPP_CLOSE_BRACE:
2537           /* If this is a non-nested '}', stop before consuming it.
2538              That way, when confronted with something like:
2539
2540                { 3 + }
2541
2542              we stop before consuming the closing '}', even though we
2543              have not yet reached a `;'.  */
2544           if (nesting_depth == 0)
2545             return;
2546
2547           /* If it is the closing '}' for a block that we have
2548              scanned, stop -- but only after consuming the token.
2549              That way given:
2550
2551                 void f g () { ... }
2552                 typedef int I;
2553
2554              we will stop after the body of the erroneously declared
2555              function, but before consuming the following `typedef'
2556              declaration.  */
2557           if (--nesting_depth == 0)
2558             {
2559               cp_lexer_consume_token (parser->lexer);
2560               return;
2561             }
2562
2563         case CPP_OPEN_BRACE:
2564           ++nesting_depth;
2565           break;
2566
2567         default:
2568           break;
2569         }
2570
2571       /* Consume the token.  */
2572       cp_lexer_consume_token (parser->lexer);
2573     }
2574 }
2575
2576 /* This function is called at the end of a statement or declaration.
2577    If the next token is a semicolon, it is consumed; otherwise, error
2578    recovery is attempted.  */
2579
2580 static void
2581 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2582 {
2583   /* Look for the trailing `;'.  */
2584   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2585     {
2586       /* If there is additional (erroneous) input, skip to the end of
2587          the statement.  */
2588       cp_parser_skip_to_end_of_statement (parser);
2589       /* If the next token is now a `;', consume it.  */
2590       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2591         cp_lexer_consume_token (parser->lexer);
2592     }
2593 }
2594
2595 /* Skip tokens until we have consumed an entire block, or until we
2596    have consumed a non-nested `;'.  */
2597
2598 static void
2599 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2600 {
2601   int nesting_depth = 0;
2602
2603   while (nesting_depth >= 0)
2604     {
2605       cp_token *token = cp_lexer_peek_token (parser->lexer);
2606
2607       switch (token->type)
2608         {
2609         case CPP_EOF:
2610         case CPP_PRAGMA_EOL:
2611           /* If we've run out of tokens, stop.  */
2612           return;
2613
2614         case CPP_SEMICOLON:
2615           /* Stop if this is an unnested ';'. */
2616           if (!nesting_depth)
2617             nesting_depth = -1;
2618           break;
2619
2620         case CPP_CLOSE_BRACE:
2621           /* Stop if this is an unnested '}', or closes the outermost
2622              nesting level.  */
2623           nesting_depth--;
2624           if (!nesting_depth)
2625             nesting_depth = -1;
2626           break;
2627
2628         case CPP_OPEN_BRACE:
2629           /* Nest. */
2630           nesting_depth++;
2631           break;
2632
2633         default:
2634           break;
2635         }
2636
2637       /* Consume the token.  */
2638       cp_lexer_consume_token (parser->lexer);
2639     }
2640 }
2641
2642 /* Skip tokens until a non-nested closing curly brace is the next
2643    token, or there are no more tokens. Return true in the first case,
2644    false otherwise.  */
2645
2646 static bool
2647 cp_parser_skip_to_closing_brace (cp_parser *parser)
2648 {
2649   unsigned nesting_depth = 0;
2650
2651   while (true)
2652     {
2653       cp_token *token = cp_lexer_peek_token (parser->lexer);
2654
2655       switch (token->type)
2656         {
2657         case CPP_EOF:
2658         case CPP_PRAGMA_EOL:
2659           /* If we've run out of tokens, stop.  */
2660           return false;
2661
2662         case CPP_CLOSE_BRACE:
2663           /* If the next token is a non-nested `}', then we have reached
2664              the end of the current block.  */
2665           if (nesting_depth-- == 0)
2666             return true;
2667           break;
2668
2669         case CPP_OPEN_BRACE:
2670           /* If it the next token is a `{', then we are entering a new
2671              block.  Consume the entire block.  */
2672           ++nesting_depth;
2673           break;
2674
2675         default:
2676           break;
2677         }
2678
2679       /* Consume the token.  */
2680       cp_lexer_consume_token (parser->lexer);
2681     }
2682 }
2683
2684 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2685    parameter is the PRAGMA token, allowing us to purge the entire pragma
2686    sequence.  */
2687
2688 static void
2689 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2690 {
2691   cp_token *token;
2692
2693   parser->lexer->in_pragma = false;
2694
2695   do
2696     token = cp_lexer_consume_token (parser->lexer);
2697   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2698
2699   /* Ensure that the pragma is not parsed again.  */
2700   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 }
2702
2703 /* Require pragma end of line, resyncing with it as necessary.  The
2704    arguments are as for cp_parser_skip_to_pragma_eol.  */
2705
2706 static void
2707 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2708 {
2709   parser->lexer->in_pragma = false;
2710   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2711     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 }
2713
2714 /* This is a simple wrapper around make_typename_type. When the id is
2715    an unresolved identifier node, we can provide a superior diagnostic
2716    using cp_parser_diagnose_invalid_type_name.  */
2717
2718 static tree
2719 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2720                               tree id, location_t id_location)
2721 {
2722   tree result;
2723   if (TREE_CODE (id) == IDENTIFIER_NODE)
2724     {
2725       result = make_typename_type (scope, id, typename_type,
2726                                    /*complain=*/tf_none);
2727       if (result == error_mark_node)
2728         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729       return result;
2730     }
2731   return make_typename_type (scope, id, typename_type, tf_error);
2732 }
2733
2734 /* This is a wrapper around the
2735    make_{pointer,ptrmem,reference}_declarator functions that decides
2736    which one to call based on the CODE and CLASS_TYPE arguments. The
2737    CODE argument should be one of the values returned by
2738    cp_parser_ptr_operator. */
2739 static cp_declarator *
2740 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2741                                     cp_cv_quals cv_qualifiers,
2742                                     cp_declarator *target)
2743 {
2744   if (code == ERROR_MARK)
2745     return cp_error_declarator;
2746
2747   if (code == INDIRECT_REF)
2748     if (class_type == NULL_TREE)
2749       return make_pointer_declarator (cv_qualifiers, target);
2750     else
2751       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2752   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2753     return make_reference_declarator (cv_qualifiers, target, false);
2754   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2755     return make_reference_declarator (cv_qualifiers, target, true);
2756   gcc_unreachable ();
2757 }
2758
2759 /* Create a new C++ parser.  */
2760
2761 static cp_parser *
2762 cp_parser_new (void)
2763 {
2764   cp_parser *parser;
2765   cp_lexer *lexer;
2766   unsigned i;
2767
2768   /* cp_lexer_new_main is called before calling ggc_alloc because
2769      cp_lexer_new_main might load a PCH file.  */
2770   lexer = cp_lexer_new_main ();
2771
2772   /* Initialize the binops_by_token so that we can get the tree
2773      directly from the token.  */
2774   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2775     binops_by_token[binops[i].token_type] = binops[i];
2776
2777   parser = GGC_CNEW (cp_parser);
2778   parser->lexer = lexer;
2779   parser->context = cp_parser_context_new (NULL);
2780
2781   /* For now, we always accept GNU extensions.  */
2782   parser->allow_gnu_extensions_p = 1;
2783
2784   /* The `>' token is a greater-than operator, not the end of a
2785      template-id.  */
2786   parser->greater_than_is_operator_p = true;
2787
2788   parser->default_arg_ok_p = true;
2789
2790   /* We are not parsing a constant-expression.  */
2791   parser->integral_constant_expression_p = false;
2792   parser->allow_non_integral_constant_expression_p = false;
2793   parser->non_integral_constant_expression_p = false;
2794
2795   /* Local variable names are not forbidden.  */
2796   parser->local_variables_forbidden_p = false;
2797
2798   /* We are not processing an `extern "C"' declaration.  */
2799   parser->in_unbraced_linkage_specification_p = false;
2800
2801   /* We are not processing a declarator.  */
2802   parser->in_declarator_p = false;
2803
2804   /* We are not processing a template-argument-list.  */
2805   parser->in_template_argument_list_p = false;
2806
2807   /* We are not in an iteration statement.  */
2808   parser->in_statement = 0;
2809
2810   /* We are not in a switch statement.  */
2811   parser->in_switch_statement_p = false;
2812
2813   /* We are not parsing a type-id inside an expression.  */
2814   parser->in_type_id_in_expr_p = false;
2815
2816   /* Declarations aren't implicitly extern "C".  */
2817   parser->implicit_extern_c = false;
2818
2819   /* String literals should be translated to the execution character set.  */
2820   parser->translate_strings_p = true;
2821
2822   /* We are not parsing a function body.  */
2823   parser->in_function_body = false;
2824
2825   /* The unparsed function queue is empty.  */
2826   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2827
2828   /* There are no classes being defined.  */
2829   parser->num_classes_being_defined = 0;
2830
2831   /* No template parameters apply.  */
2832   parser->num_template_parameter_lists = 0;
2833
2834   return parser;
2835 }
2836
2837 /* Create a cp_lexer structure which will emit the tokens in CACHE
2838    and push it onto the parser's lexer stack.  This is used for delayed
2839    parsing of in-class method bodies and default arguments, and should
2840    not be confused with tentative parsing.  */
2841 static void
2842 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2843 {
2844   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2845   lexer->next = parser->lexer;
2846   parser->lexer = lexer;
2847
2848   /* Move the current source position to that of the first token in the
2849      new lexer.  */
2850   cp_lexer_set_source_position_from_token (lexer->next_token);
2851 }
2852
2853 /* Pop the top lexer off the parser stack.  This is never used for the
2854    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2855 static void
2856 cp_parser_pop_lexer (cp_parser *parser)
2857 {
2858   cp_lexer *lexer = parser->lexer;
2859   parser->lexer = lexer->next;
2860   cp_lexer_destroy (lexer);
2861
2862   /* Put the current source position back where it was before this
2863      lexer was pushed.  */
2864   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 }
2866
2867 /* Lexical conventions [gram.lex]  */
2868
2869 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2870    identifier.  */
2871
2872 static tree
2873 cp_parser_identifier (cp_parser* parser)
2874 {
2875   cp_token *token;
2876
2877   /* Look for the identifier.  */
2878   token = cp_parser_require (parser, CPP_NAME, "identifier");
2879   /* Return the value.  */
2880   return token ? token->u.value : error_mark_node;
2881 }
2882
2883 /* Parse a sequence of adjacent string constants.  Returns a
2884    TREE_STRING representing the combined, nul-terminated string
2885    constant.  If TRANSLATE is true, translate the string to the
2886    execution character set.  If WIDE_OK is true, a wide string is
2887    invalid here.
2888
2889    C++98 [lex.string] says that if a narrow string literal token is
2890    adjacent to a wide string literal token, the behavior is undefined.
2891    However, C99 6.4.5p4 says that this results in a wide string literal.
2892    We follow C99 here, for consistency with the C front end.
2893
2894    This code is largely lifted from lex_string() in c-lex.c.
2895
2896    FUTURE: ObjC++ will need to handle @-strings here.  */
2897 static tree
2898 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2899 {
2900   tree value;
2901   size_t count;
2902   struct obstack str_ob;
2903   cpp_string str, istr, *strs;
2904   cp_token *tok;
2905   enum cpp_ttype type;
2906
2907   tok = cp_lexer_peek_token (parser->lexer);
2908   if (!cp_parser_is_string_literal (tok))
2909     {
2910       cp_parser_error (parser, "expected string-literal");
2911       return error_mark_node;
2912     }
2913
2914   type = tok->type;
2915
2916   /* Try to avoid the overhead of creating and destroying an obstack
2917      for the common case of just one string.  */
2918   if (!cp_parser_is_string_literal
2919       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2920     {
2921       cp_lexer_consume_token (parser->lexer);
2922
2923       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2924       str.len = TREE_STRING_LENGTH (tok->u.value);
2925       count = 1;
2926
2927       strs = &str;
2928     }
2929   else
2930     {
2931       gcc_obstack_init (&str_ob);
2932       count = 0;
2933
2934       do
2935         {
2936           cp_lexer_consume_token (parser->lexer);
2937           count++;
2938           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2939           str.len = TREE_STRING_LENGTH (tok->u.value);
2940
2941           if (type != tok->type)
2942             {
2943               if (type == CPP_STRING)
2944                 type = tok->type;
2945               else if (tok->type != CPP_STRING)
2946                 error ("%Hunsupported non-standard concatenation "
2947                        "of string literals", &tok->location);
2948             }
2949
2950           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2951
2952           tok = cp_lexer_peek_token (parser->lexer);
2953         }
2954       while (cp_parser_is_string_literal (tok));
2955
2956       strs = (cpp_string *) obstack_finish (&str_ob);
2957     }
2958
2959   if (type != CPP_STRING && !wide_ok)
2960     {
2961       cp_parser_error (parser, "a wide string is invalid in this context");
2962       type = CPP_STRING;
2963     }
2964
2965   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2966       (parse_in, strs, count, &istr, type))
2967     {
2968       value = build_string (istr.len, (const char *)istr.text);
2969       free (CONST_CAST (unsigned char *, istr.text));
2970
2971       switch (type)
2972         {
2973         default:
2974         case CPP_STRING:
2975           TREE_TYPE (value) = char_array_type_node;
2976           break;
2977         case CPP_STRING16:
2978           TREE_TYPE (value) = char16_array_type_node;
2979           break;
2980         case CPP_STRING32:
2981           TREE_TYPE (value) = char32_array_type_node;
2982           break;
2983         case CPP_WSTRING:
2984           TREE_TYPE (value) = wchar_array_type_node;
2985           break;
2986         }
2987
2988       value = fix_string_type (value);
2989     }
2990   else
2991     /* cpp_interpret_string has issued an error.  */
2992     value = error_mark_node;
2993
2994   if (count > 1)
2995     obstack_free (&str_ob, 0);
2996
2997   return value;
2998 }
2999
3000
3001 /* Basic concepts [gram.basic]  */
3002
3003 /* Parse a translation-unit.
3004
3005    translation-unit:
3006      declaration-seq [opt]
3007
3008    Returns TRUE if all went well.  */
3009
3010 static bool
3011 cp_parser_translation_unit (cp_parser* parser)
3012 {
3013   /* The address of the first non-permanent object on the declarator
3014      obstack.  */
3015   static void *declarator_obstack_base;
3016
3017   bool success;
3018
3019   /* Create the declarator obstack, if necessary.  */
3020   if (!cp_error_declarator)
3021     {
3022       gcc_obstack_init (&declarator_obstack);
3023       /* Create the error declarator.  */
3024       cp_error_declarator = make_declarator (cdk_error);
3025       /* Create the empty parameter list.  */
3026       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3027       /* Remember where the base of the declarator obstack lies.  */
3028       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029     }
3030
3031   cp_parser_declaration_seq_opt (parser);
3032
3033   /* If there are no tokens left then all went well.  */
3034   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3035     {
3036       /* Get rid of the token array; we don't need it any more.  */
3037       cp_lexer_destroy (parser->lexer);
3038       parser->lexer = NULL;
3039
3040       /* This file might have been a context that's implicitly extern
3041          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3042       if (parser->implicit_extern_c)
3043         {
3044           pop_lang_context ();
3045           parser->implicit_extern_c = false;
3046         }
3047
3048       /* Finish up.  */
3049       finish_translation_unit ();
3050
3051       success = true;
3052     }
3053   else
3054     {
3055       cp_parser_error (parser, "expected declaration");
3056       success = false;
3057     }
3058
3059   /* Make sure the declarator obstack was fully cleaned up.  */
3060   gcc_assert (obstack_next_free (&declarator_obstack)
3061               == declarator_obstack_base);
3062
3063   /* All went well.  */
3064   return success;
3065 }
3066
3067 /* Expressions [gram.expr] */
3068
3069 /* Parse a primary-expression.
3070
3071    primary-expression:
3072      literal
3073      this
3074      ( expression )
3075      id-expression
3076
3077    GNU Extensions:
3078
3079    primary-expression:
3080      ( compound-statement )
3081      __builtin_va_arg ( assignment-expression , type-id )
3082      __builtin_offsetof ( type-id , offsetof-expression )
3083
3084    C++ Extensions:
3085      __has_nothrow_assign ( type-id )   
3086      __has_nothrow_constructor ( type-id )
3087      __has_nothrow_copy ( type-id )
3088      __has_trivial_assign ( type-id )   
3089      __has_trivial_constructor ( type-id )
3090      __has_trivial_copy ( type-id )
3091      __has_trivial_destructor ( type-id )
3092      __has_virtual_destructor ( type-id )     
3093      __is_abstract ( type-id )
3094      __is_base_of ( type-id , type-id )
3095      __is_class ( type-id )
3096      __is_convertible_to ( type-id , type-id )     
3097      __is_empty ( type-id )
3098      __is_enum ( type-id )
3099      __is_pod ( type-id )
3100      __is_polymorphic ( type-id )
3101      __is_union ( type-id )
3102
3103    Objective-C++ Extension:
3104
3105    primary-expression:
3106      objc-expression
3107
3108    literal:
3109      __null
3110
3111    ADDRESS_P is true iff this expression was immediately preceded by
3112    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3113    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3114    true iff this expression is a template argument.
3115
3116    Returns a representation of the expression.  Upon return, *IDK
3117    indicates what kind of id-expression (if any) was present.  */
3118
3119 static tree
3120 cp_parser_primary_expression (cp_parser *parser,
3121                               bool address_p,
3122                               bool cast_p,
3123                               bool template_arg_p,
3124                               cp_id_kind *idk)
3125 {
3126   cp_token *token = NULL;
3127
3128   /* Assume the primary expression is not an id-expression.  */
3129   *idk = CP_ID_KIND_NONE;
3130
3131   /* Peek at the next token.  */
3132   token = cp_lexer_peek_token (parser->lexer);
3133   switch (token->type)
3134     {
3135       /* literal:
3136            integer-literal
3137            character-literal
3138            floating-literal
3139            string-literal
3140            boolean-literal  */
3141     case CPP_CHAR:
3142     case CPP_CHAR16:
3143     case CPP_CHAR32:
3144     case CPP_WCHAR:
3145     case CPP_NUMBER:
3146       token = cp_lexer_consume_token (parser->lexer);
3147       /* Floating-point literals are only allowed in an integral
3148          constant expression if they are cast to an integral or
3149          enumeration type.  */
3150       if (TREE_CODE (token->u.value) == REAL_CST
3151           && parser->integral_constant_expression_p
3152           && pedantic)
3153         {
3154           /* CAST_P will be set even in invalid code like "int(2.7 +
3155              ...)".   Therefore, we have to check that the next token
3156              is sure to end the cast.  */
3157           if (cast_p)
3158             {
3159               cp_token *next_token;
3160
3161               next_token = cp_lexer_peek_token (parser->lexer);
3162               if (/* The comma at the end of an
3163                      enumerator-definition.  */
3164                   next_token->type != CPP_COMMA
3165                   /* The curly brace at the end of an enum-specifier.  */
3166                   && next_token->type != CPP_CLOSE_BRACE
3167                   /* The end of a statement.  */
3168                   && next_token->type != CPP_SEMICOLON
3169                   /* The end of the cast-expression.  */
3170                   && next_token->type != CPP_CLOSE_PAREN
3171                   /* The end of an array bound.  */
3172                   && next_token->type != CPP_CLOSE_SQUARE
3173                   /* The closing ">" in a template-argument-list.  */
3174                   && (next_token->type != CPP_GREATER
3175                       || parser->greater_than_is_operator_p)
3176                   /* C++0x only: A ">>" treated like two ">" tokens,
3177                      in a template-argument-list.  */
3178                   && (next_token->type != CPP_RSHIFT
3179                       || (cxx_dialect == cxx98)
3180                       || parser->greater_than_is_operator_p))
3181                 cast_p = false;
3182             }
3183
3184           /* If we are within a cast, then the constraint that the
3185              cast is to an integral or enumeration type will be
3186              checked at that point.  If we are not within a cast, then
3187              this code is invalid.  */
3188           if (!cast_p)
3189             cp_parser_non_integral_constant_expression
3190               (parser, "floating-point literal");
3191         }
3192       return token->u.value;
3193
3194     case CPP_STRING:
3195     case CPP_STRING16:
3196     case CPP_STRING32:
3197     case CPP_WSTRING:
3198       /* ??? Should wide strings be allowed when parser->translate_strings_p
3199          is false (i.e. in attributes)?  If not, we can kill the third
3200          argument to cp_parser_string_literal.  */
3201       return cp_parser_string_literal (parser,
3202                                        parser->translate_strings_p,
3203                                        true);
3204
3205     case CPP_OPEN_PAREN:
3206       {
3207         tree expr;
3208         bool saved_greater_than_is_operator_p;
3209
3210         /* Consume the `('.  */
3211         cp_lexer_consume_token (parser->lexer);
3212         /* Within a parenthesized expression, a `>' token is always
3213            the greater-than operator.  */
3214         saved_greater_than_is_operator_p
3215           = parser->greater_than_is_operator_p;
3216         parser->greater_than_is_operator_p = true;
3217         /* If we see `( { ' then we are looking at the beginning of
3218            a GNU statement-expression.  */
3219         if (cp_parser_allow_gnu_extensions_p (parser)
3220             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3221           {
3222             /* Statement-expressions are not allowed by the standard.  */
3223             pedwarn (token->location, OPT_pedantic, 
3224                      "ISO C++ forbids braced-groups within expressions");
3225
3226             /* And they're not allowed outside of a function-body; you
3227                cannot, for example, write:
3228
3229                  int i = ({ int j = 3; j + 1; });
3230
3231                at class or namespace scope.  */
3232             if (!parser->in_function_body
3233                 || parser->in_template_argument_list_p)
3234               {
3235                 error ("%Hstatement-expressions are not allowed outside "
3236                        "functions nor in template-argument lists",
3237                        &token->location);
3238                 cp_parser_skip_to_end_of_block_or_statement (parser);
3239                 expr = error_mark_node;
3240               }
3241             else
3242               {
3243                 /* Start the statement-expression.  */
3244                 expr = begin_stmt_expr ();
3245                 /* Parse the compound-statement.  */
3246                 cp_parser_compound_statement (parser, expr, false);
3247                 /* Finish up.  */
3248                 expr = finish_stmt_expr (expr, false);
3249               }
3250           }
3251         else
3252           {
3253             /* Parse the parenthesized expression.  */
3254             expr = cp_parser_expression (parser, cast_p);
3255             /* Let the front end know that this expression was
3256                enclosed in parentheses. This matters in case, for
3257                example, the expression is of the form `A::B', since
3258                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3259                not.  */
3260             finish_parenthesized_expr (expr);
3261           }
3262         /* The `>' token might be the end of a template-id or
3263            template-parameter-list now.  */
3264         parser->greater_than_is_operator_p
3265           = saved_greater_than_is_operator_p;
3266         /* Consume the `)'.  */
3267         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3268           cp_parser_skip_to_end_of_statement (parser);
3269
3270         return expr;
3271       }
3272
3273     case CPP_KEYWORD:
3274       switch (token->keyword)
3275         {
3276           /* These two are the boolean literals.  */
3277         case RID_TRUE:
3278           cp_lexer_consume_token (parser->lexer);
3279           return boolean_true_node;
3280         case RID_FALSE:
3281           cp_lexer_consume_token (parser->lexer);
3282           return boolean_false_node;
3283
3284           /* The `__null' literal.  */
3285         case RID_NULL:
3286           cp_lexer_consume_token (parser->lexer);
3287           return null_node;
3288
3289           /* Recognize the `this' keyword.  */
3290         case RID_THIS:
3291           cp_lexer_consume_token (parser->lexer);
3292           if (parser->local_variables_forbidden_p)
3293             {
3294               error ("%H%<this%> may not be used in this context",
3295                      &token->location);
3296               return error_mark_node;
3297             }
3298           /* Pointers cannot appear in constant-expressions.  */
3299           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3300             return error_mark_node;
3301           return finish_this_expr ();
3302
3303           /* The `operator' keyword can be the beginning of an
3304              id-expression.  */
3305         case RID_OPERATOR:
3306           goto id_expression;
3307
3308         case RID_FUNCTION_NAME:
3309         case RID_PRETTY_FUNCTION_NAME:
3310         case RID_C99_FUNCTION_NAME:
3311           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3312              __func__ are the names of variables -- but they are
3313              treated specially.  Therefore, they are handled here,
3314              rather than relying on the generic id-expression logic
3315              below.  Grammatically, these names are id-expressions.
3316
3317              Consume the token.  */
3318           token = cp_lexer_consume_token (parser->lexer);
3319           /* Look up the name.  */
3320           return finish_fname (token->u.value);
3321
3322         case RID_VA_ARG:
3323           {
3324             tree expression;
3325             tree type;
3326
3327             /* The `__builtin_va_arg' construct is used to handle
3328                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3329             cp_lexer_consume_token (parser->lexer);
3330             /* Look for the opening `('.  */
3331             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3332             /* Now, parse the assignment-expression.  */
3333             expression = cp_parser_assignment_expression (parser,
3334                                                           /*cast_p=*/false);
3335             /* Look for the `,'.  */
3336             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3337             /* Parse the type-id.  */
3338             type = cp_parser_type_id (parser);
3339             /* Look for the closing `)'.  */
3340             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3341             /* Using `va_arg' in a constant-expression is not
3342                allowed.  */
3343             if (cp_parser_non_integral_constant_expression (parser,
3344                                                             "%<va_arg%>"))
3345               return error_mark_node;
3346             return build_x_va_arg (expression, type);
3347           }
3348
3349         case RID_OFFSETOF:
3350           return cp_parser_builtin_offsetof (parser);
3351
3352         case RID_HAS_NOTHROW_ASSIGN:
3353         case RID_HAS_NOTHROW_CONSTRUCTOR:
3354         case RID_HAS_NOTHROW_COPY:        
3355         case RID_HAS_TRIVIAL_ASSIGN:
3356         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3357         case RID_HAS_TRIVIAL_COPY:        
3358         case RID_HAS_TRIVIAL_DESTRUCTOR:
3359         case RID_HAS_VIRTUAL_DESTRUCTOR:
3360         case RID_IS_ABSTRACT:
3361         case RID_IS_BASE_OF:
3362         case RID_IS_CLASS:
3363         case RID_IS_CONVERTIBLE_TO:
3364         case RID_IS_EMPTY:
3365         case RID_IS_ENUM:
3366         case RID_IS_POD:
3367         case RID_IS_POLYMORPHIC:
3368         case RID_IS_UNION:
3369           return cp_parser_trait_expr (parser, token->keyword);
3370
3371         /* Objective-C++ expressions.  */
3372         case RID_AT_ENCODE:
3373         case RID_AT_PROTOCOL:
3374         case RID_AT_SELECTOR:
3375           return cp_parser_objc_expression (parser);
3376
3377         default:
3378           cp_parser_error (parser, "expected primary-expression");
3379           return error_mark_node;
3380         }
3381
3382       /* An id-expression can start with either an identifier, a
3383          `::' as the beginning of a qualified-id, or the "operator"
3384          keyword.  */
3385     case CPP_NAME:
3386     case CPP_SCOPE:
3387     case CPP_TEMPLATE_ID:
3388     case CPP_NESTED_NAME_SPECIFIER:
3389       {
3390         tree id_expression;
3391         tree decl;
3392         const char *error_msg;
3393         bool template_p;
3394         bool done;
3395         cp_token *id_expr_token;
3396
3397       id_expression:
3398         /* Parse the id-expression.  */
3399         id_expression
3400           = cp_parser_id_expression (parser,
3401                                      /*template_keyword_p=*/false,
3402                                      /*check_dependency_p=*/true,
3403                                      &template_p,
3404                                      /*declarator_p=*/false,
3405                                      /*optional_p=*/false);
3406         if (id_expression == error_mark_node)
3407           return error_mark_node;
3408         id_expr_token = token;
3409         token = cp_lexer_peek_token (parser->lexer);
3410         done = (token->type != CPP_OPEN_SQUARE
3411                 && token->type != CPP_OPEN_PAREN
3412                 && token->type != CPP_DOT
3413                 && token->type != CPP_DEREF
3414                 && token->type != CPP_PLUS_PLUS
3415                 && token->type != CPP_MINUS_MINUS);
3416         /* If we have a template-id, then no further lookup is
3417            required.  If the template-id was for a template-class, we
3418            will sometimes have a TYPE_DECL at this point.  */
3419         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3420                  || TREE_CODE (id_expression) == TYPE_DECL)
3421           decl = id_expression;
3422         /* Look up the name.  */
3423         else
3424           {
3425             tree ambiguous_decls;
3426
3427             decl = cp_parser_lookup_name (parser, id_expression,
3428                                           none_type,
3429                                           template_p,
3430                                           /*is_namespace=*/false,
3431                                           /*check_dependency=*/true,
3432                                           &ambiguous_decls,
3433                                           id_expr_token->location);
3434             /* If the lookup was ambiguous, an error will already have
3435                been issued.  */
3436             if (ambiguous_decls)
3437               return error_mark_node;
3438
3439             /* In Objective-C++, an instance variable (ivar) may be preferred
3440                to whatever cp_parser_lookup_name() found.  */
3441             decl = objc_lookup_ivar (decl, id_expression);
3442
3443             /* If name lookup gives us a SCOPE_REF, then the
3444                qualifying scope was dependent.  */
3445             if (TREE_CODE (decl) == SCOPE_REF)
3446               {
3447                 /* At this point, we do not know if DECL is a valid
3448                    integral constant expression.  We assume that it is
3449                    in fact such an expression, so that code like:
3450
3451                       template <int N> struct A {
3452                         int a[B<N>::i];
3453                       };
3454                      
3455                    is accepted.  At template-instantiation time, we
3456                    will check that B<N>::i is actually a constant.  */
3457                 return decl;
3458               }
3459             /* Check to see if DECL is a local variable in a context
3460                where that is forbidden.  */
3461             if (parser->local_variables_forbidden_p
3462                 && local_variable_p (decl))
3463               {
3464                 /* It might be that we only found DECL because we are
3465                    trying to be generous with pre-ISO scoping rules.
3466                    For example, consider:
3467
3468                      int i;
3469                      void g() {
3470                        for (int i = 0; i < 10; ++i) {}
3471                        extern void f(int j = i);
3472                      }
3473
3474                    Here, name look up will originally find the out
3475                    of scope `i'.  We need to issue a warning message,
3476                    but then use the global `i'.  */
3477                 decl = check_for_out_of_scope_variable (decl);
3478                 if (local_variable_p (decl))
3479                   {
3480                     error ("%Hlocal variable %qD may not appear in this context",
3481                            &id_expr_token->location, decl);
3482                     return error_mark_node;
3483                   }
3484               }
3485           }
3486
3487         decl = (finish_id_expression
3488                 (id_expression, decl, parser->scope,
3489                  idk,
3490                  parser->integral_constant_expression_p,
3491                  parser->allow_non_integral_constant_expression_p,
3492                  &parser->non_integral_constant_expression_p,
3493                  template_p, done, address_p,
3494                  template_arg_p,
3495                  &error_msg,
3496                  id_expr_token->location));
3497         if (error_msg)
3498           cp_parser_error (parser, error_msg);
3499         return decl;
3500       }
3501
3502       /* Anything else is an error.  */
3503     default:
3504       /* ...unless we have an Objective-C++ message or string literal,
3505          that is.  */
3506       if (c_dialect_objc ()
3507           && (token->type == CPP_OPEN_SQUARE
3508               || token->type == CPP_OBJC_STRING))
3509         return cp_parser_objc_expression (parser);
3510
3511       cp_parser_error (parser, "expected primary-expression");
3512       return error_mark_node;
3513     }
3514 }
3515
3516 /* Parse an id-expression.
3517
3518    id-expression:
3519      unqualified-id
3520      qualified-id
3521
3522    qualified-id:
3523      :: [opt] nested-name-specifier template [opt] unqualified-id
3524      :: identifier
3525      :: operator-function-id
3526      :: template-id
3527
3528    Return a representation of the unqualified portion of the
3529    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3530    a `::' or nested-name-specifier.
3531
3532    Often, if the id-expression was a qualified-id, the caller will
3533    want to make a SCOPE_REF to represent the qualified-id.  This
3534    function does not do this in order to avoid wastefully creating
3535    SCOPE_REFs when they are not required.
3536
3537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3538    `template' keyword.
3539
3540    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3541    uninstantiated templates.
3542
3543    If *TEMPLATE_P is non-NULL, it is set to true iff the
3544    `template' keyword is used to explicitly indicate that the entity
3545    named is a template.
3546
3547    If DECLARATOR_P is true, the id-expression is appearing as part of
3548    a declarator, rather than as part of an expression.  */
3549
3550 static tree
3551 cp_parser_id_expression (cp_parser *parser,
3552                          bool template_keyword_p,
3553                          bool check_dependency_p,
3554                          bool *template_p,
3555                          bool declarator_p,
3556                          bool optional_p)
3557 {
3558   bool global_scope_p;
3559   bool nested_name_specifier_p;
3560
3561   /* Assume the `template' keyword was not used.  */
3562   if (template_p)
3563     *template_p = template_keyword_p;
3564
3565   /* Look for the optional `::' operator.  */
3566   global_scope_p
3567     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3568        != NULL_TREE);
3569   /* Look for the optional nested-name-specifier.  */
3570   nested_name_specifier_p
3571     = (cp_parser_nested_name_specifier_opt (parser,
3572                                             /*typename_keyword_p=*/false,
3573                                             check_dependency_p,
3574                                             /*type_p=*/false,
3575                                             declarator_p)
3576        != NULL_TREE);
3577   /* If there is a nested-name-specifier, then we are looking at
3578      the first qualified-id production.  */
3579   if (nested_name_specifier_p)
3580     {
3581       tree saved_scope;
3582       tree saved_object_scope;
3583       tree saved_qualifying_scope;
3584       tree unqualified_id;
3585       bool is_template;
3586
3587       /* See if the next token is the `template' keyword.  */
3588       if (!template_p)
3589         template_p = &is_template;
3590       *template_p = cp_parser_optional_template_keyword (parser);
3591       /* Name lookup we do during the processing of the
3592          unqualified-id might obliterate SCOPE.  */
3593       saved_scope = parser->scope;
3594       saved_object_scope = parser->object_scope;
3595       saved_qualifying_scope = parser->qualifying_scope;
3596       /* Process the final unqualified-id.  */
3597       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3598                                                  check_dependency_p,
3599                                                  declarator_p,
3600                                                  /*optional_p=*/false);
3601       /* Restore the SAVED_SCOPE for our caller.  */
3602       parser->scope = saved_scope;
3603       parser->object_scope = saved_object_scope;
3604       parser->qualifying_scope = saved_qualifying_scope;
3605
3606       return unqualified_id;
3607     }
3608   /* Otherwise, if we are in global scope, then we are looking at one
3609      of the other qualified-id productions.  */
3610   else if (global_scope_p)
3611     {
3612       cp_token *token;
3613       tree id;
3614
3615       /* Peek at the next token.  */
3616       token = cp_lexer_peek_token (parser->lexer);
3617
3618       /* If it's an identifier, and the next token is not a "<", then
3619          we can avoid the template-id case.  This is an optimization
3620          for this common case.  */
3621       if (token->type == CPP_NAME
3622           && !cp_parser_nth_token_starts_template_argument_list_p
3623                (parser, 2))
3624         return cp_parser_identifier (parser);
3625
3626       cp_parser_parse_tentatively (parser);
3627       /* Try a template-id.  */
3628       id = cp_parser_template_id (parser,
3629                                   /*template_keyword_p=*/false,
3630                                   /*check_dependency_p=*/true,
3631                                   declarator_p);
3632       /* If that worked, we're done.  */
3633       if (cp_parser_parse_definitely (parser))
3634         return id;
3635
3636       /* Peek at the next token.  (Changes in the token buffer may
3637          have invalidated the pointer obtained above.)  */
3638       token = cp_lexer_peek_token (parser->lexer);
3639
3640       switch (token->type)
3641         {
3642         case CPP_NAME:
3643           return cp_parser_identifier (parser);
3644
3645         case CPP_KEYWORD:
3646           if (token->keyword == RID_OPERATOR)
3647             return cp_parser_operator_function_id (parser);
3648           /* Fall through.  */
3649
3650         default:
3651           cp_parser_error (parser, "expected id-expression");
3652           return error_mark_node;
3653         }
3654     }
3655   else
3656     return cp_parser_unqualified_id (parser, template_keyword_p,
3657                                      /*check_dependency_p=*/true,
3658                                      declarator_p,
3659                                      optional_p);
3660 }
3661
3662 /* Parse an unqualified-id.
3663
3664    unqualified-id:
3665      identifier
3666      operator-function-id
3667      conversion-function-id
3668      ~ class-name
3669      template-id
3670
3671    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3672    keyword, in a construct like `A::template ...'.
3673
3674    Returns a representation of unqualified-id.  For the `identifier'
3675    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3676    production a BIT_NOT_EXPR is returned; the operand of the
3677    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3678    other productions, see the documentation accompanying the
3679    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3680    names are looked up in uninstantiated templates.  If DECLARATOR_P
3681    is true, the unqualified-id is appearing as part of a declarator,
3682    rather than as part of an expression.  */
3683
3684 static tree
3685 cp_parser_unqualified_id (cp_parser* parser,
3686                           bool template_keyword_p,
3687                           bool check_dependency_p,
3688                           bool declarator_p,
3689                           bool optional_p)
3690 {
3691   cp_token *token;
3692
3693   /* Peek at the next token.  */
3694   token = cp_lexer_peek_token (parser->lexer);
3695
3696   switch (token->type)
3697     {
3698     case CPP_NAME:
3699       {
3700         tree id;
3701
3702         /* We don't know yet whether or not this will be a
3703            template-id.  */
3704         cp_parser_parse_tentatively (parser);
3705         /* Try a template-id.  */
3706         id = cp_parser_template_id (parser, template_keyword_p,
3707                                     check_dependency_p,
3708                                     declarator_p);
3709         /* If it worked, we're done.  */
3710         if (cp_parser_parse_definitely (parser))
3711           return id;
3712         /* Otherwise, it's an ordinary identifier.  */
3713         return cp_parser_identifier (parser);
3714       }
3715
3716     case CPP_TEMPLATE_ID:
3717       return cp_parser_template_id (parser, template_keyword_p,
3718                                     check_dependency_p,
3719                                     declarator_p);
3720
3721     case CPP_COMPL:
3722       {
3723         tree type_decl;
3724         tree qualifying_scope;
3725         tree object_scope;
3726         tree scope;
3727         bool done;
3728
3729         /* Consume the `~' token.  */
3730         cp_lexer_consume_token (parser->lexer);
3731         /* Parse the class-name.  The standard, as written, seems to
3732            say that:
3733
3734              template <typename T> struct S { ~S (); };
3735              template <typename T> S<T>::~S() {}
3736
3737            is invalid, since `~' must be followed by a class-name, but
3738            `S<T>' is dependent, and so not known to be a class.
3739            That's not right; we need to look in uninstantiated
3740            templates.  A further complication arises from:
3741
3742              template <typename T> void f(T t) {
3743                t.T::~T();
3744              }
3745
3746            Here, it is not possible to look up `T' in the scope of `T'
3747            itself.  We must look in both the current scope, and the
3748            scope of the containing complete expression.
3749
3750            Yet another issue is:
3751
3752              struct S {
3753                int S;
3754                ~S();
3755              };
3756
3757              S::~S() {}
3758
3759            The standard does not seem to say that the `S' in `~S'
3760            should refer to the type `S' and not the data member
3761            `S::S'.  */
3762
3763         /* DR 244 says that we look up the name after the "~" in the
3764            same scope as we looked up the qualifying name.  That idea
3765            isn't fully worked out; it's more complicated than that.  */
3766         scope = parser->scope;
3767         object_scope = parser->object_scope;
3768         qualifying_scope = parser->qualifying_scope;
3769
3770         /* Check for invalid scopes.  */
3771         if (scope == error_mark_node)
3772           {
3773             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3774               cp_lexer_consume_token (parser->lexer);
3775             return error_mark_node;
3776           }
3777         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3778           {
3779             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3780               error ("%Hscope %qT before %<~%> is not a class-name",
3781                      &token->location, scope);
3782             cp_parser_simulate_error (parser);
3783             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3784               cp_lexer_consume_token (parser->lexer);
3785             return error_mark_node;
3786           }
3787         gcc_assert (!scope || TYPE_P (scope));
3788
3789         /* If the name is of the form "X::~X" it's OK.  */
3790         token = cp_lexer_peek_token (parser->lexer);
3791         if (scope
3792             && token->type == CPP_NAME
3793             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3794                 == CPP_OPEN_PAREN)
3795             && constructor_name_p (token->u.value, scope))
3796           {
3797             cp_lexer_consume_token (parser->lexer);
3798             return build_nt (BIT_NOT_EXPR, scope);
3799           }
3800
3801         /* If there was an explicit qualification (S::~T), first look
3802            in the scope given by the qualification (i.e., S).  */
3803         done = false;
3804         type_decl = NULL_TREE;
3805         if (scope)
3806           {
3807             cp_parser_parse_tentatively (parser);
3808             type_decl = cp_parser_class_name (parser,
3809                                               /*typename_keyword_p=*/false,
3810                                               /*template_keyword_p=*/false,
3811                                               none_type,
3812                                               /*check_dependency=*/false,
3813                                               /*class_head_p=*/false,
3814                                               declarator_p);
3815             if (cp_parser_parse_definitely (parser))
3816               done = true;
3817           }
3818         /* In "N::S::~S", look in "N" as well.  */
3819         if (!done && scope && qualifying_scope)
3820           {
3821             cp_parser_parse_tentatively (parser);
3822             parser->scope = qualifying_scope;
3823             parser->object_scope = NULL_TREE;
3824             parser->qualifying_scope = NULL_TREE;
3825             type_decl
3826               = cp_parser_class_name (parser,
3827                                       /*typename_keyword_p=*/false,
3828                                       /*template_keyword_p=*/false,
3829                                       none_type,
3830                                       /*check_dependency=*/false,
3831                                       /*class_head_p=*/false,
3832                                       declarator_p);
3833             if (cp_parser_parse_definitely (parser))
3834               done = true;
3835           }
3836         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3837         else if (!done && object_scope)
3838           {
3839             cp_parser_parse_tentatively (parser);
3840             parser->scope = object_scope;
3841             parser->object_scope = NULL_TREE;
3842             parser->qualifying_scope = NULL_TREE;
3843             type_decl
3844               = cp_parser_class_name (parser,
3845                                       /*typename_keyword_p=*/false,
3846                                       /*template_keyword_p=*/false,
3847                                       none_type,
3848                                       /*check_dependency=*/false,
3849                                       /*class_head_p=*/false,
3850                                       declarator_p);
3851             if (cp_parser_parse_definitely (parser))
3852               done = true;
3853           }
3854         /* Look in the surrounding context.  */
3855         if (!done)
3856           {
3857             parser->scope = NULL_TREE;
3858             parser->object_scope = NULL_TREE;
3859             parser->qualifying_scope = NULL_TREE;
3860             type_decl
3861               = cp_parser_class_name (parser,
3862                                       /*typename_keyword_p=*/false,
3863                                       /*template_keyword_p=*/false,
3864                                       none_type,
3865                                       /*check_dependency=*/false,
3866                                       /*class_head_p=*/false,
3867                                       declarator_p);
3868           }
3869         /* If an error occurred, assume that the name of the
3870            destructor is the same as the name of the qualifying
3871            class.  That allows us to keep parsing after running
3872            into ill-formed destructor names.  */
3873         if (type_decl == error_mark_node && scope)
3874           return build_nt (BIT_NOT_EXPR, scope);
3875         else if (type_decl == error_mark_node)
3876           return error_mark_node;
3877
3878         /* Check that destructor name and scope match.  */
3879         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3880           {
3881             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3882               error ("%Hdeclaration of %<~%T%> as member of %qT",
3883                      &token->location, type_decl, scope);
3884             cp_parser_simulate_error (parser);
3885             return error_mark_node;
3886           }
3887
3888         /* [class.dtor]
3889
3890            A typedef-name that names a class shall not be used as the
3891            identifier in the declarator for a destructor declaration.  */
3892         if (declarator_p
3893             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3894             && !DECL_SELF_REFERENCE_P (type_decl)
3895             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3896           error ("%Htypedef-name %qD used as destructor declarator",
3897                  &token->location, type_decl);
3898
3899         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3900       }
3901
3902     case CPP_KEYWORD:
3903       if (token->keyword == RID_OPERATOR)
3904         {
3905           tree id;
3906
3907           /* This could be a template-id, so we try that first.  */
3908           cp_parser_parse_tentatively (parser);
3909           /* Try a template-id.  */
3910           id = cp_parser_template_id (parser, template_keyword_p,
3911                                       /*check_dependency_p=*/true,
3912                                       declarator_p);
3913           /* If that worked, we're done.  */
3914           if (cp_parser_parse_definitely (parser))
3915             return id;
3916           /* We still don't know whether we're looking at an
3917              operator-function-id or a conversion-function-id.  */
3918           cp_parser_parse_tentatively (parser);
3919           /* Try an operator-function-id.  */
3920           id = cp_parser_operator_function_id (parser);
3921           /* If that didn't work, try a conversion-function-id.  */
3922           if (!cp_parser_parse_definitely (parser))
3923             id = cp_parser_conversion_function_id (parser);
3924
3925           return id;
3926         }
3927       /* Fall through.  */
3928
3929     default:
3930       if (optional_p)
3931         return NULL_TREE;
3932       cp_parser_error (parser, "expected unqualified-id");
3933       return error_mark_node;
3934     }
3935 }
3936
3937 /* Parse an (optional) nested-name-specifier.
3938
3939    nested-name-specifier: [C++98]
3940      class-or-namespace-name :: nested-name-specifier [opt]
3941      class-or-namespace-name :: template nested-name-specifier [opt]
3942
3943    nested-name-specifier: [C++0x]
3944      type-name ::
3945      namespace-name ::
3946      nested-name-specifier identifier ::
3947      nested-name-specifier template [opt] simple-template-id ::
3948
3949    PARSER->SCOPE should be set appropriately before this function is
3950    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3951    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3952    in name lookups.
3953
3954    Sets PARSER->SCOPE to the class (TYPE) or namespace
3955    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3956    it unchanged if there is no nested-name-specifier.  Returns the new
3957    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3958
3959    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3960    part of a declaration and/or decl-specifier.  */
3961
3962 static tree
3963 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3964                                      bool typename_keyword_p,
3965                                      bool check_dependency_p,
3966                                      bool type_p,
3967                                      bool is_declaration)
3968 {
3969   bool success = false;
3970   cp_token_position start = 0;
3971   cp_token *token;
3972
3973   /* Remember where the nested-name-specifier starts.  */
3974   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3975     {
3976       start = cp_lexer_token_position (parser->lexer, false);
3977       push_deferring_access_checks (dk_deferred);
3978     }
3979
3980   while (true)
3981     {
3982       tree new_scope;
3983       tree old_scope;
3984       tree saved_qualifying_scope;
3985       bool template_keyword_p;
3986
3987       /* Spot cases that cannot be the beginning of a
3988          nested-name-specifier.  */
3989       token = cp_lexer_peek_token (parser->lexer);
3990
3991       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3992          the already parsed nested-name-specifier.  */
3993       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3994         {
3995           /* Grab the nested-name-specifier and continue the loop.  */
3996           cp_parser_pre_parsed_nested_name_specifier (parser);
3997           /* If we originally encountered this nested-name-specifier
3998              with IS_DECLARATION set to false, we will not have
3999              resolved TYPENAME_TYPEs, so we must do so here.  */
4000           if (is_declaration
4001               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4002             {
4003               new_scope = resolve_typename_type (parser->scope,
4004                                                  /*only_current_p=*/false);
4005               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4006                 parser->scope = new_scope;
4007             }
4008           success = true;
4009           continue;
4010         }
4011
4012       /* Spot cases that cannot be the beginning of a
4013          nested-name-specifier.  On the second and subsequent times
4014          through the loop, we look for the `template' keyword.  */
4015       if (success && token->keyword == RID_TEMPLATE)
4016         ;
4017       /* A template-id can start a nested-name-specifier.  */
4018       else if (token->type == CPP_TEMPLATE_ID)
4019         ;
4020       else
4021         {
4022           /* If the next token is not an identifier, then it is
4023              definitely not a type-name or namespace-name.  */
4024           if (token->type != CPP_NAME)
4025             break;
4026           /* If the following token is neither a `<' (to begin a
4027              template-id), nor a `::', then we are not looking at a
4028              nested-name-specifier.  */
4029           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4030           if (token->type != CPP_SCOPE
4031               && !cp_parser_nth_token_starts_template_argument_list_p
4032                   (parser, 2))
4033             break;
4034         }
4035
4036       /* The nested-name-specifier is optional, so we parse
4037          tentatively.  */
4038       cp_parser_parse_tentatively (parser);
4039
4040       /* Look for the optional `template' keyword, if this isn't the
4041          first time through the loop.  */
4042       if (success)
4043         template_keyword_p = cp_parser_optional_template_keyword (parser);
4044       else
4045         template_keyword_p = false;
4046
4047       /* Save the old scope since the name lookup we are about to do
4048          might destroy it.  */
4049       old_scope = parser->scope;
4050       saved_qualifying_scope = parser->qualifying_scope;
4051       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4052          look up names in "X<T>::I" in order to determine that "Y" is
4053          a template.  So, if we have a typename at this point, we make
4054          an effort to look through it.  */
4055       if (is_declaration
4056           && !typename_keyword_p
4057           && parser->scope
4058           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4059         parser->scope = resolve_typename_type (parser->scope,
4060                                                /*only_current_p=*/false);
4061       /* Parse the qualifying entity.  */
4062       new_scope
4063         = cp_parser_qualifying_entity (parser,
4064                                        typename_keyword_p,
4065                                        template_keyword_p,
4066                                        check_dependency_p,
4067                                        type_p,
4068                                        is_declaration);
4069       /* Look for the `::' token.  */
4070       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4071
4072       /* If we found what we wanted, we keep going; otherwise, we're
4073          done.  */
4074       if (!cp_parser_parse_definitely (parser))
4075         {
4076           bool error_p = false;
4077
4078           /* Restore the OLD_SCOPE since it was valid before the
4079              failed attempt at finding the last
4080              class-or-namespace-name.  */
4081           parser->scope = old_scope;
4082           parser->qualifying_scope = saved_qualifying_scope;
4083           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4084             break;
4085           /* If the next token is an identifier, and the one after
4086              that is a `::', then any valid interpretation would have
4087              found a class-or-namespace-name.  */
4088           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4089                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4090                      == CPP_SCOPE)
4091                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4092                      != CPP_COMPL))
4093             {
4094               token = cp_lexer_consume_token (parser->lexer);
4095               if (!error_p)
4096                 {
4097                   if (!token->ambiguous_p)
4098                     {
4099                       tree decl;
4100                       tree ambiguous_decls;
4101
4102                       decl = cp_parser_lookup_name (parser, token->u.value,
4103                                                     none_type,
4104                                                     /*is_template=*/false,
4105                                                     /*is_namespace=*/false,
4106                                                     /*check_dependency=*/true,
4107                                                     &ambiguous_decls,
4108                                                     token->location);
4109                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4110                         error ("%H%qD used without template parameters",
4111                                &token->location, decl);
4112                       else if (ambiguous_decls)
4113                         {
4114                           error ("%Hreference to %qD is ambiguous",
4115                                  &token->location, token->u.value);
4116                           print_candidates (ambiguous_decls);
4117                           decl = error_mark_node;
4118                         }
4119                       else
4120                         {
4121                           const char* msg = "is not a class or namespace";
4122                           if (cxx_dialect != cxx98)
4123                             msg = "is not a class, namespace, or enumeration";
4124                           cp_parser_name_lookup_error
4125                             (parser, token->u.value, decl, msg,
4126                              token->location);
4127                         }
4128                     }
4129                   parser->scope = error_mark_node;
4130                   error_p = true;
4131                   /* Treat this as a successful nested-name-specifier
4132                      due to:
4133
4134                      [basic.lookup.qual]
4135
4136                      If the name found is not a class-name (clause
4137                      _class_) or namespace-name (_namespace.def_), the
4138                      program is ill-formed.  */
4139                   success = true;
4140                 }
4141               cp_lexer_consume_token (parser->lexer);
4142             }
4143           break;
4144         }
4145       /* We've found one valid nested-name-specifier.  */
4146       success = true;
4147       /* Name lookup always gives us a DECL.  */
4148       if (TREE_CODE (new_scope) == TYPE_DECL)
4149         new_scope = TREE_TYPE (new_scope);
4150       /* Uses of "template" must be followed by actual templates.  */
4151       if (template_keyword_p
4152           && !(CLASS_TYPE_P (new_scope)
4153                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4154                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4155                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4156           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4157                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4158                    == TEMPLATE_ID_EXPR)))
4159         permerror (input_location, TYPE_P (new_scope)
4160                    ? "%qT is not a template"
4161                    : "%qD is not a template",
4162                    new_scope);
4163       /* If it is a class scope, try to complete it; we are about to
4164          be looking up names inside the class.  */
4165       if (TYPE_P (new_scope)
4166           /* Since checking types for dependency can be expensive,
4167              avoid doing it if the type is already complete.  */
4168           && !COMPLETE_TYPE_P (new_scope)
4169           /* Do not try to complete dependent types.  */
4170           && !dependent_type_p (new_scope))
4171         {
4172           new_scope = complete_type (new_scope);
4173           /* If it is a typedef to current class, use the current
4174              class instead, as the typedef won't have any names inside
4175              it yet.  */
4176           if (!COMPLETE_TYPE_P (new_scope)
4177               && currently_open_class (new_scope))
4178             new_scope = TYPE_MAIN_VARIANT (new_scope);
4179         }
4180       /* Make sure we look in the right scope the next time through
4181          the loop.  */
4182       parser->scope = new_scope;
4183     }
4184
4185   /* If parsing tentatively, replace the sequence of tokens that makes
4186      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4187      token.  That way, should we re-parse the token stream, we will
4188      not have to repeat the effort required to do the parse, nor will
4189      we issue duplicate error messages.  */
4190   if (success && start)
4191     {
4192       cp_token *token;
4193
4194       token = cp_lexer_token_at (parser->lexer, start);
4195       /* Reset the contents of the START token.  */
4196       token->type = CPP_NESTED_NAME_SPECIFIER;
4197       /* Retrieve any deferred checks.  Do not pop this access checks yet
4198          so the memory will not be reclaimed during token replacing below.  */
4199       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4200       token->u.tree_check_value->value = parser->scope;
4201       token->u.tree_check_value->checks = get_deferred_access_checks ();
4202       token->u.tree_check_value->qualifying_scope =
4203         parser->qualifying_scope;
4204       token->keyword = RID_MAX;
4205
4206       /* Purge all subsequent tokens.  */
4207       cp_lexer_purge_tokens_after (parser->lexer, start);
4208     }
4209
4210   if (start)
4211     pop_to_parent_deferring_access_checks ();
4212
4213   return success ? parser->scope : NULL_TREE;
4214 }
4215
4216 /* Parse a nested-name-specifier.  See
4217    cp_parser_nested_name_specifier_opt for details.  This function
4218    behaves identically, except that it will an issue an error if no
4219    nested-name-specifier is present.  */
4220
4221 static tree
4222 cp_parser_nested_name_specifier (cp_parser *parser,
4223                                  bool typename_keyword_p,
4224                                  bool check_dependency_p,
4225                                  bool type_p,
4226                                  bool is_declaration)
4227 {
4228   tree scope;
4229
4230   /* Look for the nested-name-specifier.  */
4231   scope = cp_parser_nested_name_specifier_opt (parser,
4232                                                typename_keyword_p,
4233                                                check_dependency_p,
4234                                                type_p,
4235                                                is_declaration);
4236   /* If it was not present, issue an error message.  */
4237   if (!scope)
4238     {
4239       cp_parser_error (parser, "expected nested-name-specifier");
4240       parser->scope = NULL_TREE;
4241     }
4242
4243   return scope;
4244 }
4245
4246 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4247    this is either a class-name or a namespace-name (which corresponds
4248    to the class-or-namespace-name production in the grammar). For
4249    C++0x, it can also be a type-name that refers to an enumeration
4250    type.
4251
4252    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4253    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4254    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4255    TYPE_P is TRUE iff the next name should be taken as a class-name,
4256    even the same name is declared to be another entity in the same
4257    scope.
4258
4259    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4260    specified by the class-or-namespace-name.  If neither is found the
4261    ERROR_MARK_NODE is returned.  */
4262
4263 static tree
4264 cp_parser_qualifying_entity (cp_parser *parser,
4265                              bool typename_keyword_p,
4266                              bool template_keyword_p,
4267                              bool check_dependency_p,
4268                              bool type_p,
4269                              bool is_declaration)
4270 {
4271   tree saved_scope;
4272   tree saved_qualifying_scope;
4273   tree saved_object_scope;
4274   tree scope;
4275   bool only_class_p;
4276   bool successful_parse_p;
4277
4278   /* Before we try to parse the class-name, we must save away the
4279      current PARSER->SCOPE since cp_parser_class_name will destroy
4280      it.  */
4281   saved_scope = parser->scope;
4282   saved_qualifying_scope = parser->qualifying_scope;
4283   saved_object_scope = parser->object_scope;
4284   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4285      there is no need to look for a namespace-name.  */
4286   only_class_p = template_keyword_p 
4287     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4288   if (!only_class_p)
4289     cp_parser_parse_tentatively (parser);
4290   scope = cp_parser_class_name (parser,
4291                                 typename_keyword_p,
4292                                 template_keyword_p,
4293                                 type_p ? class_type : none_type,
4294                                 check_dependency_p,
4295                                 /*class_head_p=*/false,
4296                                 is_declaration);
4297   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4298   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4299   if (!only_class_p 
4300       && cxx_dialect != cxx98
4301       && !successful_parse_p)
4302     {
4303       /* Restore the saved scope.  */
4304       parser->scope = saved_scope;
4305       parser->qualifying_scope = saved_qualifying_scope;
4306       parser->object_scope = saved_object_scope;
4307
4308       /* Parse tentatively.  */
4309       cp_parser_parse_tentatively (parser);
4310      
4311       /* Parse a typedef-name or enum-name.  */
4312       scope = cp_parser_nonclass_name (parser);
4313       successful_parse_p = cp_parser_parse_definitely (parser);
4314     }
4315   /* If that didn't work, try for a namespace-name.  */
4316   if (!only_class_p && !successful_parse_p)
4317     {
4318       /* Restore the saved scope.  */
4319       parser->scope = saved_scope;
4320       parser->qualifying_scope = saved_qualifying_scope;
4321       parser->object_scope = saved_object_scope;
4322       /* If we are not looking at an identifier followed by the scope
4323          resolution operator, then this is not part of a
4324          nested-name-specifier.  (Note that this function is only used
4325          to parse the components of a nested-name-specifier.)  */
4326       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4327           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4328         return error_mark_node;
4329       scope = cp_parser_namespace_name (parser);
4330     }
4331
4332   return scope;
4333 }
4334
4335 /* Parse a postfix-expression.
4336
4337    postfix-expression:
4338      primary-expression
4339      postfix-expression [ expression ]
4340      postfix-expression ( expression-list [opt] )
4341      simple-type-specifier ( expression-list [opt] )
4342      typename :: [opt] nested-name-specifier identifier
4343        ( expression-list [opt] )
4344      typename :: [opt] nested-name-specifier template [opt] template-id
4345        ( expression-list [opt] )
4346      postfix-expression . template [opt] id-expression
4347      postfix-expression -> template [opt] id-expression
4348      postfix-expression . pseudo-destructor-name
4349      postfix-expression -> pseudo-destructor-name
4350      postfix-expression ++
4351      postfix-expression --
4352      dynamic_cast < type-id > ( expression )
4353      static_cast < type-id > ( expression )
4354      reinterpret_cast < type-id > ( expression )
4355      const_cast < type-id > ( expression )
4356      typeid ( expression )
4357      typeid ( type-id )
4358
4359    GNU Extension:
4360
4361    postfix-expression:
4362      ( type-id ) { initializer-list , [opt] }
4363
4364    This extension is a GNU version of the C99 compound-literal
4365    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4366    but they are essentially the same concept.)
4367
4368    If ADDRESS_P is true, the postfix expression is the operand of the
4369    `&' operator.  CAST_P is true if this expression is the target of a
4370    cast.
4371
4372    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4373    class member access expressions [expr.ref].
4374
4375    Returns a representation of the expression.  */
4376
4377 static tree
4378 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4379                               bool member_access_only_p)
4380 {
4381   cp_token *token;
4382   enum rid keyword;
4383   cp_id_kind idk = CP_ID_KIND_NONE;
4384   tree postfix_expression = NULL_TREE;
4385   bool is_member_access = false;
4386
4387   /* Peek at the next token.  */
4388   token = cp_lexer_peek_token (parser->lexer);
4389   /* Some of the productions are determined by keywords.  */
4390   keyword = token->keyword;
4391   switch (keyword)
4392     {
4393     case RID_DYNCAST:
4394     case RID_STATCAST:
4395     case RID_REINTCAST:
4396     case RID_CONSTCAST:
4397       {
4398         tree type;
4399         tree expression;
4400         const char *saved_message;
4401
4402         /* All of these can be handled in the same way from the point
4403            of view of parsing.  Begin by consuming the token
4404            identifying the cast.  */
4405         cp_lexer_consume_token (parser->lexer);
4406
4407         /* New types cannot be defined in the cast.  */
4408         saved_message = parser->type_definition_forbidden_message;
4409         parser->type_definition_forbidden_message
4410           = "types may not be defined in casts";
4411
4412         /* Look for the opening `<'.  */
4413         cp_parser_require (parser, CPP_LESS, "%<<%>");
4414         /* Parse the type to which we are casting.  */
4415         type = cp_parser_type_id (parser);
4416         /* Look for the closing `>'.  */
4417         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4418         /* Restore the old message.  */
4419         parser->type_definition_forbidden_message = saved_message;
4420
4421         /* And the expression which is being cast.  */
4422         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4423         expression = cp_parser_expression (parser, /*cast_p=*/true);
4424         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4425
4426         /* Only type conversions to integral or enumeration types
4427            can be used in constant-expressions.  */
4428         if (!cast_valid_in_integral_constant_expression_p (type)
4429             && (cp_parser_non_integral_constant_expression
4430                 (parser,
4431                  "a cast to a type other than an integral or "
4432                  "enumeration type")))
4433           return error_mark_node;
4434
4435         switch (keyword)
4436           {
4437           case RID_DYNCAST:
4438             postfix_expression
4439               = build_dynamic_cast (type, expression, tf_warning_or_error);
4440             break;
4441           case RID_STATCAST:
4442             postfix_expression
4443               = build_static_cast (type, expression, tf_warning_or_error);
4444             break;
4445           case RID_REINTCAST:
4446             postfix_expression
4447               = build_reinterpret_cast (type, expression, 
4448                                         tf_warning_or_error);
4449             break;
4450           case RID_CONSTCAST:
4451             postfix_expression
4452               = build_const_cast (type, expression, tf_warning_or_error);
4453             break;
4454           default:
4455             gcc_unreachable ();
4456           }
4457       }
4458       break;
4459
4460     case RID_TYPEID:
4461       {
4462         tree type;
4463         const char *saved_message;
4464         bool saved_in_type_id_in_expr_p;
4465
4466         /* Consume the `typeid' token.  */
4467         cp_lexer_consume_token (parser->lexer);
4468         /* Look for the `(' token.  */
4469         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4470         /* Types cannot be defined in a `typeid' expression.  */
4471         saved_message = parser->type_definition_forbidden_message;
4472         parser->type_definition_forbidden_message
4473           = "types may not be defined in a %<typeid%> expression";
4474         /* We can't be sure yet whether we're looking at a type-id or an
4475            expression.  */
4476         cp_parser_parse_tentatively (parser);
4477         /* Try a type-id first.  */
4478         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4479         parser->in_type_id_in_expr_p = true;
4480         type = cp_parser_type_id (parser);
4481         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4482         /* Look for the `)' token.  Otherwise, we can't be sure that
4483            we're not looking at an expression: consider `typeid (int
4484            (3))', for example.  */
4485         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4486         /* If all went well, simply lookup the type-id.  */
4487         if (cp_parser_parse_definitely (parser))
4488           postfix_expression = get_typeid (type);
4489         /* Otherwise, fall back to the expression variant.  */
4490         else
4491           {
4492             tree expression;
4493
4494             /* Look for an expression.  */
4495             expression = cp_parser_expression (parser, /*cast_p=*/false);
4496             /* Compute its typeid.  */
4497             postfix_expression = build_typeid (expression);
4498             /* Look for the `)' token.  */
4499             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4500           }
4501         /* Restore the saved message.  */
4502         parser->type_definition_forbidden_message = saved_message;
4503         /* `typeid' may not appear in an integral constant expression.  */
4504         if (cp_parser_non_integral_constant_expression(parser,
4505                                                        "%<typeid%> operator"))
4506           return error_mark_node;
4507       }
4508       break;
4509
4510     case RID_TYPENAME:
4511       {
4512         tree type;
4513         /* The syntax permitted here is the same permitted for an
4514            elaborated-type-specifier.  */
4515         type = cp_parser_elaborated_type_specifier (parser,
4516                                                     /*is_friend=*/false,
4517                                                     /*is_declaration=*/false);
4518         postfix_expression = cp_parser_functional_cast (parser, type);
4519       }
4520       break;
4521
4522     default:
4523       {
4524         tree type;
4525
4526         /* If the next thing is a simple-type-specifier, we may be
4527            looking at a functional cast.  We could also be looking at
4528            an id-expression.  So, we try the functional cast, and if
4529            that doesn't work we fall back to the primary-expression.  */
4530         cp_parser_parse_tentatively (parser);
4531         /* Look for the simple-type-specifier.  */
4532         type = cp_parser_simple_type_specifier (parser,
4533                                                 /*decl_specs=*/NULL,
4534                                                 CP_PARSER_FLAGS_NONE);
4535         /* Parse the cast itself.  */
4536         if (!cp_parser_error_occurred (parser))
4537           postfix_expression
4538             = cp_parser_functional_cast (parser, type);
4539         /* If that worked, we're done.  */
4540         if (cp_parser_parse_definitely (parser))
4541           break;
4542
4543         /* If the functional-cast didn't work out, try a
4544            compound-literal.  */
4545         if (cp_parser_allow_gnu_extensions_p (parser)
4546             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4547           {
4548             VEC(constructor_elt,gc) *initializer_list = NULL;
4549             bool saved_in_type_id_in_expr_p;
4550
4551             cp_parser_parse_tentatively (parser);
4552             /* Consume the `('.  */
4553             cp_lexer_consume_token (parser->lexer);
4554             /* Parse the type.  */
4555             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4556             parser->in_type_id_in_expr_p = true;
4557             type = cp_parser_type_id (parser);
4558             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4559             /* Look for the `)'.  */
4560             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4561             /* Look for the `{'.  */
4562             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4563             /* If things aren't going well, there's no need to
4564                keep going.  */
4565             if (!cp_parser_error_occurred (parser))
4566               {
4567                 bool non_constant_p;
4568                 /* Parse the initializer-list.  */
4569                 initializer_list
4570                   = cp_parser_initializer_list (parser, &non_constant_p);
4571                 /* Allow a trailing `,'.  */
4572                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4573                   cp_lexer_consume_token (parser->lexer);
4574                 /* Look for the final `}'.  */
4575                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4576               }
4577             /* If that worked, we're definitely looking at a
4578                compound-literal expression.  */
4579             if (cp_parser_parse_definitely (parser))
4580               {
4581                 /* Warn the user that a compound literal is not
4582                    allowed in standard C++.  */
4583                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4584                 /* For simplicity, we disallow compound literals in
4585                    constant-expressions.  We could
4586                    allow compound literals of integer type, whose
4587                    initializer was a constant, in constant
4588                    expressions.  Permitting that usage, as a further
4589                    extension, would not change the meaning of any
4590                    currently accepted programs.  (Of course, as
4591                    compound literals are not part of ISO C++, the
4592                    standard has nothing to say.)  */
4593                 if (cp_parser_non_integral_constant_expression 
4594                     (parser, "non-constant compound literals"))
4595                   {
4596                     postfix_expression = error_mark_node;
4597                     break;
4598                   }
4599                 /* Form the representation of the compound-literal.  */
4600                 postfix_expression
4601                   = (finish_compound_literal
4602                      (type, build_constructor (init_list_type_node,
4603                                                initializer_list)));
4604                 break;
4605               }
4606           }
4607
4608         /* It must be a primary-expression.  */
4609         postfix_expression
4610           = cp_parser_primary_expression (parser, address_p, cast_p,
4611                                           /*template_arg_p=*/false,
4612                                           &idk);
4613       }
4614       break;
4615     }
4616
4617   /* Keep looping until the postfix-expression is complete.  */
4618   while (true)
4619     {
4620       if (idk == CP_ID_KIND_UNQUALIFIED
4621           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4622           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4623         /* It is not a Koenig lookup function call.  */
4624         postfix_expression
4625           = unqualified_name_lookup_error (postfix_expression);
4626
4627       /* Peek at the next token.  */
4628       token = cp_lexer_peek_token (parser->lexer);
4629
4630       switch (token->type)
4631         {
4632         case CPP_OPEN_SQUARE:
4633           postfix_expression
4634             = cp_parser_postfix_open_square_expression (parser,
4635                                                         postfix_expression,
4636                                                         false);
4637           idk = CP_ID_KIND_NONE;
4638           is_member_access = false;
4639           break;
4640
4641         case CPP_OPEN_PAREN:
4642           /* postfix-expression ( expression-list [opt] ) */
4643           {
4644             bool koenig_p;
4645             bool is_builtin_constant_p;
4646             bool saved_integral_constant_expression_p = false;
4647             bool saved_non_integral_constant_expression_p = false;
4648             tree args;
4649
4650             is_member_access = false;
4651
4652             is_builtin_constant_p
4653               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4654             if (is_builtin_constant_p)
4655               {
4656                 /* The whole point of __builtin_constant_p is to allow
4657                    non-constant expressions to appear as arguments.  */
4658                 saved_integral_constant_expression_p
4659                   = parser->integral_constant_expression_p;
4660                 saved_non_integral_constant_expression_p
4661                   = parser->non_integral_constant_expression_p;
4662                 parser->integral_constant_expression_p = false;
4663               }
4664             args = (cp_parser_parenthesized_expression_list
4665                     (parser, /*is_attribute_list=*/false,
4666                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4667                      /*non_constant_p=*/NULL));
4668             if (is_builtin_constant_p)
4669               {
4670                 parser->integral_constant_expression_p
4671                   = saved_integral_constant_expression_p;
4672                 parser->non_integral_constant_expression_p
4673                   = saved_non_integral_constant_expression_p;
4674               }
4675
4676             if (args == error_mark_node)
4677               {
4678                 postfix_expression = error_mark_node;
4679                 break;
4680               }
4681
4682             /* Function calls are not permitted in
4683                constant-expressions.  */
4684             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4685                 && cp_parser_non_integral_constant_expression (parser,
4686                                                                "a function call"))
4687               {
4688                 postfix_expression = error_mark_node;
4689                 break;
4690               }
4691
4692             koenig_p = false;
4693             if (idk == CP_ID_KIND_UNQUALIFIED)
4694               {
4695                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4696                   {
4697                     if (args)
4698                       {
4699                         koenig_p = true;
4700                         postfix_expression
4701                           = perform_koenig_lookup (postfix_expression, args);
4702                       }
4703                     else
4704                       postfix_expression
4705                         = unqualified_fn_lookup_error (postfix_expression);
4706                   }
4707                 /* We do not perform argument-dependent lookup if
4708                    normal lookup finds a non-function, in accordance
4709                    with the expected resolution of DR 218.  */
4710                 else if (args && is_overloaded_fn (postfix_expression))
4711                   {
4712                     tree fn = get_first_fn (postfix_expression);
4713
4714                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4715                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4716
4717                     /* Only do argument dependent lookup if regular
4718                        lookup does not find a set of member functions.
4719                        [basic.lookup.koenig]/2a  */
4720                     if (!DECL_FUNCTION_MEMBER_P (fn))
4721                       {
4722                         koenig_p = true;
4723                         postfix_expression
4724                           = perform_koenig_lookup (postfix_expression, args);
4725                       }
4726                   }
4727               }
4728
4729             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4730               {
4731                 tree instance = TREE_OPERAND (postfix_expression, 0);
4732                 tree fn = TREE_OPERAND (postfix_expression, 1);
4733
4734                 if (processing_template_decl
4735                     && (type_dependent_expression_p (instance)
4736                         || (!BASELINK_P (fn)
4737                             && TREE_CODE (fn) != FIELD_DECL)
4738                         || type_dependent_expression_p (fn)
4739                         || any_type_dependent_arguments_p (args)))
4740                   {
4741                     postfix_expression
4742                       = build_nt_call_list (postfix_expression, args);
4743                     break;
4744                   }
4745
4746                 if (BASELINK_P (fn))
4747                   postfix_expression
4748                     = (build_new_method_call
4749                        (instance, fn, args, NULL_TREE,
4750                         (idk == CP_ID_KIND_QUALIFIED
4751                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4752                         /*fn_p=*/NULL,
4753                         tf_warning_or_error));
4754                 else
4755                   postfix_expression
4756                     = finish_call_expr (postfix_expression, args,
4757                                         /*disallow_virtual=*/false,
4758                                         /*koenig_p=*/false,
4759                                         tf_warning_or_error);
4760               }
4761             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4762                      || TREE_CODE (postfix_expression) == MEMBER_REF
4763                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4764               postfix_expression = (build_offset_ref_call_from_tree
4765                                     (postfix_expression, args));
4766             else if (idk == CP_ID_KIND_QUALIFIED)
4767               /* A call to a static class member, or a namespace-scope
4768                  function.  */
4769               postfix_expression
4770                 = finish_call_expr (postfix_expression, args,
4771                                     /*disallow_virtual=*/true,
4772                                     koenig_p,
4773                                     tf_warning_or_error);
4774             else
4775               /* All other function calls.  */
4776               postfix_expression
4777                 = finish_call_expr (postfix_expression, args,
4778                                     /*disallow_virtual=*/false,
4779                                     koenig_p,
4780                                     tf_warning_or_error);
4781
4782             if (warn_disallowed_functions)
4783               warn_if_disallowed_function_p (postfix_expression);
4784
4785             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4786             idk = CP_ID_KIND_NONE;
4787           }
4788           break;
4789
4790         case CPP_DOT:
4791         case CPP_DEREF:
4792           /* postfix-expression . template [opt] id-expression
4793              postfix-expression . pseudo-destructor-name
4794              postfix-expression -> template [opt] id-expression
4795              postfix-expression -> pseudo-destructor-name */
4796
4797           /* Consume the `.' or `->' operator.  */
4798           cp_lexer_consume_token (parser->lexer);
4799
4800           postfix_expression
4801             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4802                                                       postfix_expression,
4803                                                       false, &idk,
4804                                                       token->location);
4805
4806           is_member_access = true;
4807           break;
4808
4809         case CPP_PLUS_PLUS:
4810           /* postfix-expression ++  */
4811           /* Consume the `++' token.  */
4812           cp_lexer_consume_token (parser->lexer);
4813           /* Generate a representation for the complete expression.  */
4814           postfix_expression
4815             = finish_increment_expr (postfix_expression,
4816                                      POSTINCREMENT_EXPR);
4817           /* Increments may not appear in constant-expressions.  */
4818           if (cp_parser_non_integral_constant_expression (parser,
4819                                                           "an increment"))
4820             postfix_expression = error_mark_node;
4821           idk = CP_ID_KIND_NONE;
4822           is_member_access = false;
4823           break;
4824
4825         case CPP_MINUS_MINUS:
4826           /* postfix-expression -- */
4827           /* Consume the `--' token.  */
4828           cp_lexer_consume_token (parser->lexer);
4829           /* Generate a representation for the complete expression.  */
4830           postfix_expression
4831             = finish_increment_expr (postfix_expression,
4832                                      POSTDECREMENT_EXPR);
4833           /* Decrements may not appear in constant-expressions.  */
4834           if (cp_parser_non_integral_constant_expression (parser,
4835                                                           "a decrement"))
4836             postfix_expression = error_mark_node;
4837           idk = CP_ID_KIND_NONE;
4838           is_member_access = false;
4839           break;
4840
4841         default:
4842           if (member_access_only_p)
4843             return is_member_access? postfix_expression : error_mark_node;
4844           else
4845             return postfix_expression;
4846         }
4847     }
4848
4849   /* We should never get here.  */
4850   gcc_unreachable ();
4851   return error_mark_node;
4852 }
4853
4854 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4855    by cp_parser_builtin_offsetof.  We're looking for
4856
4857      postfix-expression [ expression ]
4858
4859    FOR_OFFSETOF is set if we're being called in that context, which
4860    changes how we deal with integer constant expressions.  */
4861
4862 static tree
4863 cp_parser_postfix_open_square_expression (cp_parser *parser,
4864                                           tree postfix_expression,
4865                                           bool for_offsetof)
4866 {
4867   tree index;
4868
4869   /* Consume the `[' token.  */
4870   cp_lexer_consume_token (parser->lexer);
4871
4872   /* Parse the index expression.  */
4873   /* ??? For offsetof, there is a question of what to allow here.  If
4874      offsetof is not being used in an integral constant expression context,
4875      then we *could* get the right answer by computing the value at runtime.
4876      If we are in an integral constant expression context, then we might
4877      could accept any constant expression; hard to say without analysis.
4878      Rather than open the barn door too wide right away, allow only integer
4879      constant expressions here.  */
4880   if (for_offsetof)
4881     index = cp_parser_constant_expression (parser, false, NULL);
4882   else
4883     index = cp_parser_expression (parser, /*cast_p=*/false);
4884
4885   /* Look for the closing `]'.  */
4886   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4887
4888   /* Build the ARRAY_REF.  */
4889   postfix_expression = grok_array_decl (postfix_expression, index);
4890
4891   /* When not doing offsetof, array references are not permitted in
4892      constant-expressions.  */
4893   if (!for_offsetof
4894       && (cp_parser_non_integral_constant_expression
4895           (parser, "an array reference")))
4896     postfix_expression = error_mark_node;
4897
4898   return postfix_expression;
4899 }
4900
4901 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4902    by cp_parser_builtin_offsetof.  We're looking for
4903
4904      postfix-expression . template [opt] id-expression
4905      postfix-expression . pseudo-destructor-name
4906      postfix-expression -> template [opt] id-expression
4907      postfix-expression -> pseudo-destructor-name
4908
4909    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4910    limits what of the above we'll actually accept, but nevermind.
4911    TOKEN_TYPE is the "." or "->" token, which will already have been
4912    removed from the stream.  */
4913
4914 static tree
4915 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4916                                         enum cpp_ttype token_type,
4917                                         tree postfix_expression,
4918                                         bool for_offsetof, cp_id_kind *idk,
4919                                         location_t location)
4920 {
4921   tree name;
4922   bool dependent_p;
4923   bool pseudo_destructor_p;
4924   tree scope = NULL_TREE;
4925
4926   /* If this is a `->' operator, dereference the pointer.  */
4927   if (token_type == CPP_DEREF)
4928     postfix_expression = build_x_arrow (postfix_expression);
4929   /* Check to see whether or not the expression is type-dependent.  */
4930   dependent_p = type_dependent_expression_p (postfix_expression);
4931   /* The identifier following the `->' or `.' is not qualified.  */
4932   parser->scope = NULL_TREE;
4933   parser->qualifying_scope = NULL_TREE;
4934   parser->object_scope = NULL_TREE;
4935   *idk = CP_ID_KIND_NONE;
4936   /* Enter the scope corresponding to the type of the object
4937      given by the POSTFIX_EXPRESSION.  */
4938   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4939     {
4940       scope = TREE_TYPE (postfix_expression);
4941       /* According to the standard, no expression should ever have
4942          reference type.  Unfortunately, we do not currently match
4943          the standard in this respect in that our internal representation
4944          of an expression may have reference type even when the standard
4945          says it does not.  Therefore, we have to manually obtain the
4946          underlying type here.  */
4947       scope = non_reference (scope);
4948       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4949       if (scope == unknown_type_node)
4950         {
4951           error ("%H%qE does not have class type", &location, postfix_expression);
4952           scope = NULL_TREE;
4953         }
4954       else
4955         scope = complete_type_or_else (scope, NULL_TREE);
4956       /* Let the name lookup machinery know that we are processing a
4957          class member access expression.  */
4958       parser->context->object_type = scope;
4959       /* If something went wrong, we want to be able to discern that case,
4960          as opposed to the case where there was no SCOPE due to the type
4961          of expression being dependent.  */
4962       if (!scope)
4963         scope = error_mark_node;
4964       /* If the SCOPE was erroneous, make the various semantic analysis
4965          functions exit quickly -- and without issuing additional error
4966          messages.  */
4967       if (scope == error_mark_node)
4968         postfix_expression = error_mark_node;
4969     }
4970
4971   /* Assume this expression is not a pseudo-destructor access.  */
4972   pseudo_destructor_p = false;
4973
4974   /* If the SCOPE is a scalar type, then, if this is a valid program,
4975      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4976      is type dependent, it can be pseudo-destructor-name or something else.
4977      Try to parse it as pseudo-destructor-name first.  */
4978   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4979     {
4980       tree s;
4981       tree type;
4982
4983       cp_parser_parse_tentatively (parser);
4984       /* Parse the pseudo-destructor-name.  */
4985       s = NULL_TREE;
4986       cp_parser_pseudo_destructor_name (parser, &s, &type);
4987       if (dependent_p
4988           && (cp_parser_error_occurred (parser)
4989               || TREE_CODE (type) != TYPE_DECL
4990               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4991         cp_parser_abort_tentative_parse (parser);
4992       else if (cp_parser_parse_definitely (parser))
4993         {
4994           pseudo_destructor_p = true;
4995           postfix_expression
4996             = finish_pseudo_destructor_expr (postfix_expression,
4997                                              s, TREE_TYPE (type));
4998         }
4999     }
5000
5001   if (!pseudo_destructor_p)
5002     {
5003       /* If the SCOPE is not a scalar type, we are looking at an
5004          ordinary class member access expression, rather than a
5005          pseudo-destructor-name.  */
5006       bool template_p;
5007       cp_token *token = cp_lexer_peek_token (parser->lexer);
5008       /* Parse the id-expression.  */
5009       name = (cp_parser_id_expression
5010               (parser,
5011                cp_parser_optional_template_keyword (parser),
5012                /*check_dependency_p=*/true,
5013                &template_p,
5014                /*declarator_p=*/false,
5015                /*optional_p=*/false));
5016       /* In general, build a SCOPE_REF if the member name is qualified.
5017          However, if the name was not dependent and has already been
5018          resolved; there is no need to build the SCOPE_REF.  For example;
5019
5020              struct X { void f(); };
5021              template <typename T> void f(T* t) { t->X::f(); }
5022
5023          Even though "t" is dependent, "X::f" is not and has been resolved
5024          to a BASELINK; there is no need to include scope information.  */
5025
5026       /* But we do need to remember that there was an explicit scope for
5027          virtual function calls.  */
5028       if (parser->scope)
5029         *idk = CP_ID_KIND_QUALIFIED;
5030
5031       /* If the name is a template-id that names a type, we will get a
5032          TYPE_DECL here.  That is invalid code.  */
5033       if (TREE_CODE (name) == TYPE_DECL)
5034         {
5035           error ("%Hinvalid use of %qD", &token->location, name);
5036           postfix_expression = error_mark_node;
5037         }
5038       else
5039         {
5040           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5041             {
5042               name = build_qualified_name (/*type=*/NULL_TREE,
5043                                            parser->scope,
5044                                            name,
5045                                            template_p);
5046               parser->scope = NULL_TREE;
5047               parser->qualifying_scope = NULL_TREE;
5048               parser->object_scope = NULL_TREE;
5049             }
5050           if (scope && name && BASELINK_P (name))
5051             adjust_result_of_qualified_name_lookup
5052               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5053           postfix_expression
5054             = finish_class_member_access_expr (postfix_expression, name,
5055                                                template_p, 
5056                                                tf_warning_or_error);
5057         }
5058     }
5059
5060   /* We no longer need to look up names in the scope of the object on
5061      the left-hand side of the `.' or `->' operator.  */
5062   parser->context->object_type = NULL_TREE;
5063
5064   /* Outside of offsetof, these operators may not appear in
5065      constant-expressions.  */
5066   if (!for_offsetof
5067       && (cp_parser_non_integral_constant_expression
5068           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5069     postfix_expression = error_mark_node;
5070
5071   return postfix_expression;
5072 }
5073
5074 /* Parse a parenthesized expression-list.
5075
5076    expression-list:
5077      assignment-expression
5078      expression-list, assignment-expression
5079
5080    attribute-list:
5081      expression-list
5082      identifier
5083      identifier, expression-list
5084
5085    CAST_P is true if this expression is the target of a cast.
5086
5087    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5088    argument pack.
5089
5090    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5091    representation of an assignment-expression.  Note that a TREE_LIST
5092    is returned even if there is only a single expression in the list.
5093    error_mark_node is returned if the ( and or ) are
5094    missing. NULL_TREE is returned on no expressions. The parentheses
5095    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5096    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5097    indicates whether or not all of the expressions in the list were
5098    constant.  */
5099
5100 static tree
5101 cp_parser_parenthesized_expression_list (cp_parser* parser,
5102                                          bool is_attribute_list,
5103                                          bool cast_p,
5104                                          bool allow_expansion_p,
5105                                          bool *non_constant_p)
5106 {
5107   tree expression_list = NULL_TREE;
5108   bool fold_expr_p = is_attribute_list;
5109   tree identifier = NULL_TREE;
5110   bool saved_greater_than_is_operator_p;
5111
5112   /* Assume all the expressions will be constant.  */
5113   if (non_constant_p)
5114     *non_constant_p = false;
5115
5116   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5117     return error_mark_node;
5118
5119   /* Within a parenthesized expression, a `>' token is always
5120      the greater-than operator.  */
5121   saved_greater_than_is_operator_p
5122     = parser->greater_than_is_operator_p;
5123   parser->greater_than_is_operator_p = true;
5124
5125   /* Consume expressions until there are no more.  */
5126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5127     while (true)
5128       {
5129         tree expr;
5130
5131         /* At the beginning of attribute lists, check to see if the
5132            next token is an identifier.  */
5133         if (is_attribute_list
5134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5135           {
5136             cp_token *token;
5137
5138             /* Consume the identifier.  */
5139             token = cp_lexer_consume_token (parser->lexer);
5140             /* Save the identifier.  */
5141             identifier = token->u.value;
5142           }
5143         else
5144           {
5145             bool expr_non_constant_p;
5146
5147             /* Parse the next assignment-expression.  */
5148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5149               {
5150                 /* A braced-init-list.  */
5151                 maybe_warn_cpp0x ("extended initializer lists");
5152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5153                 if (non_constant_p && expr_non_constant_p)
5154                   *non_constant_p = true;
5155               }
5156             else if (non_constant_p)
5157               {
5158                 expr = (cp_parser_constant_expression
5159                         (parser, /*allow_non_constant_p=*/true,
5160                          &expr_non_constant_p));
5161                 if (expr_non_constant_p)
5162                   *non_constant_p = true;
5163               }
5164             else
5165               expr = cp_parser_assignment_expression (parser, cast_p);
5166
5167             if (fold_expr_p)
5168               expr = fold_non_dependent_expr (expr);
5169
5170             /* If we have an ellipsis, then this is an expression
5171                expansion.  */
5172             if (allow_expansion_p
5173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5174               {
5175                 /* Consume the `...'.  */
5176                 cp_lexer_consume_token (parser->lexer);
5177
5178                 /* Build the argument pack.  */
5179                 expr = make_pack_expansion (expr);
5180               }
5181
5182              /* Add it to the list.  We add error_mark_node
5183                 expressions to the list, so that we can still tell if
5184                 the correct form for a parenthesized expression-list
5185                 is found. That gives better errors.  */
5186             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5187
5188             if (expr == error_mark_node)
5189               goto skip_comma;
5190           }
5191
5192         /* After the first item, attribute lists look the same as
5193            expression lists.  */
5194         is_attribute_list = false;
5195
5196       get_comma:;
5197         /* If the next token isn't a `,', then we are done.  */
5198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5199           break;
5200
5201         /* Otherwise, consume the `,' and keep going.  */
5202         cp_lexer_consume_token (parser->lexer);
5203       }
5204
5205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5206     {
5207       int ending;
5208
5209     skip_comma:;
5210       /* We try and resync to an unnested comma, as that will give the
5211          user better diagnostics.  */
5212       ending = cp_parser_skip_to_closing_parenthesis (parser,
5213                                                       /*recovering=*/true,
5214                                                       /*or_comma=*/true,
5215                                                       /*consume_paren=*/true);
5216       if (ending < 0)
5217         goto get_comma;
5218       if (!ending)
5219         {
5220           parser->greater_than_is_operator_p
5221             = saved_greater_than_is_operator_p;
5222           return error_mark_node;
5223         }
5224     }
5225
5226   parser->greater_than_is_operator_p
5227     = saved_greater_than_is_operator_p;
5228
5229   /* We built up the list in reverse order so we must reverse it now.  */
5230   expression_list = nreverse (expression_list);
5231   if (identifier)
5232     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5233
5234   return expression_list;
5235 }
5236
5237 /* Parse a pseudo-destructor-name.
5238
5239    pseudo-destructor-name:
5240      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5241      :: [opt] nested-name-specifier template template-id :: ~ type-name
5242      :: [opt] nested-name-specifier [opt] ~ type-name
5243
5244    If either of the first two productions is used, sets *SCOPE to the
5245    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5246    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5247    or ERROR_MARK_NODE if the parse fails.  */
5248
5249 static void
5250 cp_parser_pseudo_destructor_name (cp_parser* parser,
5251                                   tree* scope,
5252                                   tree* type)
5253 {
5254   bool nested_name_specifier_p;
5255
5256   /* Assume that things will not work out.  */
5257   *type = error_mark_node;
5258
5259   /* Look for the optional `::' operator.  */
5260   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5261   /* Look for the optional nested-name-specifier.  */
5262   nested_name_specifier_p
5263     = (cp_parser_nested_name_specifier_opt (parser,
5264                                             /*typename_keyword_p=*/false,
5265                                             /*check_dependency_p=*/true,
5266                                             /*type_p=*/false,
5267                                             /*is_declaration=*/true)
5268        != NULL_TREE);
5269   /* Now, if we saw a nested-name-specifier, we might be doing the
5270      second production.  */
5271   if (nested_name_specifier_p
5272       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5273     {
5274       /* Consume the `template' keyword.  */
5275       cp_lexer_consume_token (parser->lexer);
5276       /* Parse the template-id.  */
5277       cp_parser_template_id (parser,
5278                              /*template_keyword_p=*/true,
5279                              /*check_dependency_p=*/false,
5280                              /*is_declaration=*/true);
5281       /* Look for the `::' token.  */
5282       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5283     }
5284   /* If the next token is not a `~', then there might be some
5285      additional qualification.  */
5286   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5287     {
5288       /* At this point, we're looking for "type-name :: ~".  The type-name
5289          must not be a class-name, since this is a pseudo-destructor.  So,
5290          it must be either an enum-name, or a typedef-name -- both of which
5291          are just identifiers.  So, we peek ahead to check that the "::"
5292          and "~" tokens are present; if they are not, then we can avoid
5293          calling type_name.  */
5294       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5295           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5296           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5297         {
5298           cp_parser_error (parser, "non-scalar type");
5299           return;
5300         }
5301
5302       /* Look for the type-name.  */
5303       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5304       if (*scope == error_mark_node)
5305         return;
5306
5307       /* Look for the `::' token.  */
5308       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5309     }
5310   else
5311     *scope = NULL_TREE;
5312
5313   /* Look for the `~'.  */
5314   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5315   /* Look for the type-name again.  We are not responsible for
5316      checking that it matches the first type-name.  */
5317   *type = cp_parser_nonclass_name (parser);
5318 }
5319
5320 /* Parse a unary-expression.
5321
5322    unary-expression:
5323      postfix-expression
5324      ++ cast-expression
5325      -- cast-expression
5326      unary-operator cast-expression
5327      sizeof unary-expression
5328      sizeof ( type-id )
5329      new-expression
5330      delete-expression
5331
5332    GNU Extensions:
5333
5334    unary-expression:
5335      __extension__ cast-expression
5336      __alignof__ unary-expression
5337      __alignof__ ( type-id )
5338      __real__ cast-expression
5339      __imag__ cast-expression
5340      && identifier
5341
5342    ADDRESS_P is true iff the unary-expression is appearing as the
5343    operand of the `&' operator.   CAST_P is true if this expression is
5344    the target of a cast.
5345
5346    Returns a representation of the expression.  */
5347
5348 static tree
5349 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5350 {
5351   cp_token *token;
5352   enum tree_code unary_operator;
5353
5354   /* Peek at the next token.  */
5355   token = cp_lexer_peek_token (parser->lexer);
5356   /* Some keywords give away the kind of expression.  */
5357   if (token->type == CPP_KEYWORD)
5358     {
5359       enum rid keyword = token->keyword;
5360
5361       switch (keyword)
5362         {
5363         case RID_ALIGNOF:
5364         case RID_SIZEOF:
5365           {
5366             tree operand;
5367             enum tree_code op;
5368
5369             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5370             /* Consume the token.  */
5371             cp_lexer_consume_token (parser->lexer);
5372             /* Parse the operand.  */
5373             operand = cp_parser_sizeof_operand (parser, keyword);
5374
5375             if (TYPE_P (operand))
5376               return cxx_sizeof_or_alignof_type (operand, op, true);
5377             else
5378               return cxx_sizeof_or_alignof_expr (operand, op, true);
5379           }
5380
5381         case RID_NEW:
5382           return cp_parser_new_expression (parser);
5383
5384         case RID_DELETE:
5385           return cp_parser_delete_expression (parser);
5386
5387         case RID_EXTENSION:
5388           {
5389             /* The saved value of the PEDANTIC flag.  */
5390             int saved_pedantic;
5391             tree expr;
5392
5393             /* Save away the PEDANTIC flag.  */
5394             cp_parser_extension_opt (parser, &saved_pedantic);
5395             /* Parse the cast-expression.  */
5396             expr = cp_parser_simple_cast_expression (parser);
5397             /* Restore the PEDANTIC flag.  */
5398             pedantic = saved_pedantic;
5399
5400             return expr;
5401           }
5402
5403         case RID_REALPART:
5404         case RID_IMAGPART:
5405           {
5406             tree expression;
5407
5408             /* Consume the `__real__' or `__imag__' token.  */
5409             cp_lexer_consume_token (parser->lexer);
5410             /* Parse the cast-expression.  */
5411             expression = cp_parser_simple_cast_expression (parser);
5412             /* Create the complete representation.  */
5413             return build_x_unary_op ((keyword == RID_REALPART
5414                                       ? REALPART_EXPR : IMAGPART_EXPR),
5415                                      expression,
5416                                      tf_warning_or_error);
5417           }
5418           break;
5419
5420         default:
5421           break;
5422         }
5423     }
5424
5425   /* Look for the `:: new' and `:: delete', which also signal the
5426      beginning of a new-expression, or delete-expression,
5427      respectively.  If the next token is `::', then it might be one of
5428      these.  */
5429   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5430     {
5431       enum rid keyword;
5432
5433       /* See if the token after the `::' is one of the keywords in
5434          which we're interested.  */
5435       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5436       /* If it's `new', we have a new-expression.  */
5437       if (keyword == RID_NEW)
5438         return cp_parser_new_expression (parser);
5439       /* Similarly, for `delete'.  */
5440       else if (keyword == RID_DELETE)
5441         return cp_parser_delete_expression (parser);
5442     }
5443
5444   /* Look for a unary operator.  */
5445   unary_operator = cp_parser_unary_operator (token);
5446   /* The `++' and `--' operators can be handled similarly, even though
5447      they are not technically unary-operators in the grammar.  */
5448   if (unary_operator == ERROR_MARK)
5449     {
5450       if (token->type == CPP_PLUS_PLUS)
5451         unary_operator = PREINCREMENT_EXPR;
5452       else if (token->type == CPP_MINUS_MINUS)
5453         unary_operator = PREDECREMENT_EXPR;
5454       /* Handle the GNU address-of-label extension.  */
5455       else if (cp_parser_allow_gnu_extensions_p (parser)
5456                && token->type == CPP_AND_AND)
5457         {
5458           tree identifier;
5459           tree expression;
5460           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5461
5462           /* Consume the '&&' token.  */
5463           cp_lexer_consume_token (parser->lexer);
5464           /* Look for the identifier.  */
5465           identifier = cp_parser_identifier (parser);
5466           /* Create an expression representing the address.  */
5467           expression = finish_label_address_expr (identifier, loc);
5468           if (cp_parser_non_integral_constant_expression (parser,
5469                                                 "the address of a label"))
5470             expression = error_mark_node;
5471           return expression;
5472         }
5473     }
5474   if (unary_operator != ERROR_MARK)
5475     {
5476       tree cast_expression;
5477       tree expression = error_mark_node;
5478       const char *non_constant_p = NULL;
5479
5480       /* Consume the operator token.  */
5481       token = cp_lexer_consume_token (parser->lexer);
5482       /* Parse the cast-expression.  */
5483       cast_expression
5484         = cp_parser_cast_expression (parser,
5485                                      unary_operator == ADDR_EXPR,
5486                                      /*cast_p=*/false);
5487       /* Now, build an appropriate representation.  */
5488       switch (unary_operator)
5489         {
5490         case INDIRECT_REF:
5491           non_constant_p = "%<*%>";
5492           expression = build_x_indirect_ref (cast_expression, "unary *",
5493                                              tf_warning_or_error);
5494           break;
5495
5496         case ADDR_EXPR:
5497           non_constant_p = "%<&%>";
5498           /* Fall through.  */
5499         case BIT_NOT_EXPR:
5500           expression = build_x_unary_op (unary_operator, cast_expression,
5501                                          tf_warning_or_error);
5502           break;
5503
5504         case PREINCREMENT_EXPR:
5505         case PREDECREMENT_EXPR:
5506           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5507                             ? "%<++%>" : "%<--%>");
5508           /* Fall through.  */
5509         case UNARY_PLUS_EXPR:
5510         case NEGATE_EXPR:
5511         case TRUTH_NOT_EXPR:
5512           expression = finish_unary_op_expr (unary_operator, cast_expression);
5513           break;
5514
5515         default:
5516           gcc_unreachable ();
5517         }
5518
5519       if (non_constant_p
5520           && cp_parser_non_integral_constant_expression (parser,
5521                                                          non_constant_p))
5522         expression = error_mark_node;
5523
5524       return expression;
5525     }
5526
5527   return cp_parser_postfix_expression (parser, address_p, cast_p,
5528                                        /*member_access_only_p=*/false);
5529 }
5530
5531 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5532    unary-operator, the corresponding tree code is returned.  */
5533
5534 static enum tree_code
5535 cp_parser_unary_operator (cp_token* token)
5536 {
5537   switch (token->type)
5538     {
5539     case CPP_MULT:
5540       return INDIRECT_REF;
5541
5542     case CPP_AND:
5543       return ADDR_EXPR;
5544
5545     case CPP_PLUS:
5546       return UNARY_PLUS_EXPR;
5547
5548     case CPP_MINUS:
5549       return NEGATE_EXPR;
5550
5551     case CPP_NOT:
5552       return TRUTH_NOT_EXPR;
5553
5554     case CPP_COMPL:
5555       return BIT_NOT_EXPR;
5556
5557     default:
5558       return ERROR_MARK;
5559     }
5560 }
5561
5562 /* Parse a new-expression.
5563
5564    new-expression:
5565      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5566      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5567
5568    Returns a representation of the expression.  */
5569
5570 static tree
5571 cp_parser_new_expression (cp_parser* parser)
5572 {
5573   bool global_scope_p;
5574   tree placement;
5575   tree type;
5576   tree initializer;
5577   tree nelts;
5578
5579   /* Look for the optional `::' operator.  */
5580   global_scope_p
5581     = (cp_parser_global_scope_opt (parser,
5582                                    /*current_scope_valid_p=*/false)
5583        != NULL_TREE);
5584   /* Look for the `new' operator.  */
5585   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5586   /* There's no easy way to tell a new-placement from the
5587      `( type-id )' construct.  */
5588   cp_parser_parse_tentatively (parser);
5589   /* Look for a new-placement.  */
5590   placement = cp_parser_new_placement (parser);
5591   /* If that didn't work out, there's no new-placement.  */
5592   if (!cp_parser_parse_definitely (parser))
5593     placement = NULL_TREE;
5594
5595   /* If the next token is a `(', then we have a parenthesized
5596      type-id.  */
5597   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5598     {
5599       cp_token *token;
5600       /* Consume the `('.  */
5601       cp_lexer_consume_token (parser->lexer);
5602       /* Parse the type-id.  */
5603       type = cp_parser_type_id (parser);
5604       /* Look for the closing `)'.  */
5605       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5606       token = cp_lexer_peek_token (parser->lexer);
5607       /* There should not be a direct-new-declarator in this production,
5608          but GCC used to allowed this, so we check and emit a sensible error
5609          message for this case.  */
5610       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5611         {
5612           error ("%Harray bound forbidden after parenthesized type-id",
5613                  &token->location);
5614           inform (token->location, 
5615                   "try removing the parentheses around the type-id");
5616           cp_parser_direct_new_declarator (parser);
5617         }
5618       nelts = NULL_TREE;
5619     }
5620   /* Otherwise, there must be a new-type-id.  */
5621   else
5622     type = cp_parser_new_type_id (parser, &nelts);
5623
5624   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5625   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5626       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5627     initializer = cp_parser_new_initializer (parser);
5628   else
5629     initializer = NULL_TREE;
5630
5631   /* A new-expression may not appear in an integral constant
5632      expression.  */
5633   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5634     return error_mark_node;
5635
5636   /* Create a representation of the new-expression.  */
5637   return build_new (placement, type, nelts, initializer, global_scope_p,
5638                     tf_warning_or_error);
5639 }
5640
5641 /* Parse a new-placement.
5642
5643    new-placement:
5644      ( expression-list )
5645
5646    Returns the same representation as for an expression-list.  */
5647
5648 static tree
5649 cp_parser_new_placement (cp_parser* parser)
5650 {
5651   tree expression_list;
5652
5653   /* Parse the expression-list.  */
5654   expression_list = (cp_parser_parenthesized_expression_list
5655                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5656                       /*non_constant_p=*/NULL));
5657
5658   return expression_list;
5659 }
5660
5661 /* Parse a new-type-id.
5662
5663    new-type-id:
5664      type-specifier-seq new-declarator [opt]
5665
5666    Returns the TYPE allocated.  If the new-type-id indicates an array
5667    type, *NELTS is set to the number of elements in the last array
5668    bound; the TYPE will not include the last array bound.  */
5669
5670 static tree
5671 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5672 {
5673   cp_decl_specifier_seq type_specifier_seq;
5674   cp_declarator *new_declarator;
5675   cp_declarator *declarator;
5676   cp_declarator *outer_declarator;
5677   const char *saved_message;
5678   tree type;
5679
5680   /* The type-specifier sequence must not contain type definitions.
5681      (It cannot contain declarations of new types either, but if they
5682      are not definitions we will catch that because they are not
5683      complete.)  */
5684   saved_message = parser->type_definition_forbidden_message;
5685   parser->type_definition_forbidden_message
5686     = "types may not be defined in a new-type-id";
5687   /* Parse the type-specifier-seq.  */
5688   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5689                                 &type_specifier_seq);
5690   /* Restore the old message.  */
5691   parser->type_definition_forbidden_message = saved_message;
5692   /* Parse the new-declarator.  */
5693   new_declarator = cp_parser_new_declarator_opt (parser);
5694
5695   /* Determine the number of elements in the last array dimension, if
5696      any.  */
5697   *nelts = NULL_TREE;
5698   /* Skip down to the last array dimension.  */
5699   declarator = new_declarator;
5700   outer_declarator = NULL;
5701   while (declarator && (declarator->kind == cdk_pointer
5702                         || declarator->kind == cdk_ptrmem))
5703     {
5704       outer_declarator = declarator;
5705       declarator = declarator->declarator;
5706     }
5707   while (declarator
5708          && declarator->kind == cdk_array
5709          && declarator->declarator
5710          && declarator->declarator->kind == cdk_array)
5711     {
5712       outer_declarator = declarator;
5713       declarator = declarator->declarator;
5714     }
5715
5716   if (declarator && declarator->kind == cdk_array)
5717     {
5718       *nelts = declarator->u.array.bounds;
5719       if (*nelts == error_mark_node)
5720         *nelts = integer_one_node;
5721
5722       if (outer_declarator)
5723         outer_declarator->declarator = declarator->declarator;
5724       else
5725         new_declarator = NULL;
5726     }
5727
5728   type = groktypename (&type_specifier_seq, new_declarator);
5729   return type;
5730 }
5731
5732 /* Parse an (optional) new-declarator.
5733
5734    new-declarator:
5735      ptr-operator new-declarator [opt]
5736      direct-new-declarator
5737
5738    Returns the declarator.  */
5739
5740 static cp_declarator *
5741 cp_parser_new_declarator_opt (cp_parser* parser)
5742 {
5743   enum tree_code code;
5744   tree type;
5745   cp_cv_quals cv_quals;
5746
5747   /* We don't know if there's a ptr-operator next, or not.  */
5748   cp_parser_parse_tentatively (parser);
5749   /* Look for a ptr-operator.  */
5750   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5751   /* If that worked, look for more new-declarators.  */
5752   if (cp_parser_parse_definitely (parser))
5753     {
5754       cp_declarator *declarator;
5755
5756       /* Parse another optional declarator.  */
5757       declarator = cp_parser_new_declarator_opt (parser);
5758
5759       return cp_parser_make_indirect_declarator
5760         (code, type, cv_quals, declarator);
5761     }
5762
5763   /* If the next token is a `[', there is a direct-new-declarator.  */
5764   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5765     return cp_parser_direct_new_declarator (parser);
5766
5767   return NULL;
5768 }
5769
5770 /* Parse a direct-new-declarator.
5771
5772    direct-new-declarator:
5773      [ expression ]
5774      direct-new-declarator [constant-expression]
5775
5776    */
5777
5778 static cp_declarator *
5779 cp_parser_direct_new_declarator (cp_parser* parser)
5780 {
5781   cp_declarator *declarator = NULL;
5782
5783   while (true)
5784     {
5785       tree expression;
5786
5787       /* Look for the opening `['.  */
5788       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5789       /* The first expression is not required to be constant.  */
5790       if (!declarator)
5791         {
5792           cp_token *token = cp_lexer_peek_token (parser->lexer);
5793           expression = cp_parser_expression (parser, /*cast_p=*/false);
5794           /* The standard requires that the expression have integral
5795              type.  DR 74 adds enumeration types.  We believe that the
5796              real intent is that these expressions be handled like the
5797              expression in a `switch' condition, which also allows
5798              classes with a single conversion to integral or
5799              enumeration type.  */
5800           if (!processing_template_decl)
5801             {
5802               expression
5803                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5804                                               expression,
5805                                               /*complain=*/true);
5806               if (!expression)
5807                 {
5808                   error ("%Hexpression in new-declarator must have integral "
5809                          "or enumeration type", &token->location);
5810                   expression = error_mark_node;
5811                 }
5812             }
5813         }
5814       /* But all the other expressions must be.  */
5815       else
5816         expression
5817           = cp_parser_constant_expression (parser,
5818                                            /*allow_non_constant=*/false,
5819                                            NULL);
5820       /* Look for the closing `]'.  */
5821       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5822
5823       /* Add this bound to the declarator.  */
5824       declarator = make_array_declarator (declarator, expression);
5825
5826       /* If the next token is not a `[', then there are no more
5827          bounds.  */
5828       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5829         break;
5830     }
5831
5832   return declarator;
5833 }
5834
5835 /* Parse a new-initializer.
5836
5837    new-initializer:
5838      ( expression-list [opt] )
5839      braced-init-list
5840
5841    Returns a representation of the expression-list.  If there is no
5842    expression-list, VOID_ZERO_NODE is returned.  */
5843
5844 static tree
5845 cp_parser_new_initializer (cp_parser* parser)
5846 {
5847   tree expression_list;
5848
5849   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5850     {
5851       bool expr_non_constant_p;
5852       maybe_warn_cpp0x ("extended initializer lists");
5853       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5854       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5855       expression_list = build_tree_list (NULL_TREE, expression_list);
5856     }
5857   else
5858     expression_list = (cp_parser_parenthesized_expression_list
5859                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5860                         /*non_constant_p=*/NULL));
5861   if (!expression_list)
5862     expression_list = void_zero_node;
5863
5864   return expression_list;
5865 }
5866
5867 /* Parse a delete-expression.
5868
5869    delete-expression:
5870      :: [opt] delete cast-expression
5871      :: [opt] delete [ ] cast-expression
5872
5873    Returns a representation of the expression.  */
5874
5875 static tree
5876 cp_parser_delete_expression (cp_parser* parser)
5877 {
5878   bool global_scope_p;
5879   bool array_p;
5880   tree expression;
5881
5882   /* Look for the optional `::' operator.  */
5883   global_scope_p
5884     = (cp_parser_global_scope_opt (parser,
5885                                    /*current_scope_valid_p=*/false)
5886        != NULL_TREE);
5887   /* Look for the `delete' keyword.  */
5888   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5889   /* See if the array syntax is in use.  */
5890   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5891     {
5892       /* Consume the `[' token.  */
5893       cp_lexer_consume_token (parser->lexer);
5894       /* Look for the `]' token.  */
5895       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5896       /* Remember that this is the `[]' construct.  */
5897       array_p = true;
5898     }
5899   else
5900     array_p = false;
5901
5902   /* Parse the cast-expression.  */
5903   expression = cp_parser_simple_cast_expression (parser);
5904
5905   /* A delete-expression may not appear in an integral constant
5906      expression.  */
5907   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5908     return error_mark_node;
5909
5910   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5911 }
5912
5913 /* Parse a cast-expression.
5914
5915    cast-expression:
5916      unary-expression
5917      ( type-id ) cast-expression
5918
5919    ADDRESS_P is true iff the unary-expression is appearing as the
5920    operand of the `&' operator.   CAST_P is true if this expression is
5921    the target of a cast.
5922
5923    Returns a representation of the expression.  */
5924
5925 static tree
5926 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5927 {
5928   /* If it's a `(', then we might be looking at a cast.  */
5929   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5930     {
5931       tree type = NULL_TREE;
5932       tree expr = NULL_TREE;
5933       bool compound_literal_p;
5934       const char *saved_message;
5935
5936       /* There's no way to know yet whether or not this is a cast.
5937          For example, `(int (3))' is a unary-expression, while `(int)
5938          3' is a cast.  So, we resort to parsing tentatively.  */
5939       cp_parser_parse_tentatively (parser);
5940       /* Types may not be defined in a cast.  */
5941       saved_message = parser->type_definition_forbidden_message;
5942       parser->type_definition_forbidden_message
5943         = "types may not be defined in casts";
5944       /* Consume the `('.  */
5945       cp_lexer_consume_token (parser->lexer);
5946       /* A very tricky bit is that `(struct S) { 3 }' is a
5947          compound-literal (which we permit in C++ as an extension).
5948          But, that construct is not a cast-expression -- it is a
5949          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5950          is legal; if the compound-literal were a cast-expression,
5951          you'd need an extra set of parentheses.)  But, if we parse
5952          the type-id, and it happens to be a class-specifier, then we
5953          will commit to the parse at that point, because we cannot
5954          undo the action that is done when creating a new class.  So,
5955          then we cannot back up and do a postfix-expression.
5956
5957          Therefore, we scan ahead to the closing `)', and check to see
5958          if the token after the `)' is a `{'.  If so, we are not
5959          looking at a cast-expression.
5960
5961          Save tokens so that we can put them back.  */
5962       cp_lexer_save_tokens (parser->lexer);
5963       /* Skip tokens until the next token is a closing parenthesis.
5964          If we find the closing `)', and the next token is a `{', then
5965          we are looking at a compound-literal.  */
5966       compound_literal_p
5967         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5968                                                   /*consume_paren=*/true)
5969            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5970       /* Roll back the tokens we skipped.  */
5971       cp_lexer_rollback_tokens (parser->lexer);
5972       /* If we were looking at a compound-literal, simulate an error
5973          so that the call to cp_parser_parse_definitely below will
5974          fail.  */
5975       if (compound_literal_p)
5976         cp_parser_simulate_error (parser);
5977       else
5978         {
5979           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5980           parser->in_type_id_in_expr_p = true;
5981           /* Look for the type-id.  */
5982           type = cp_parser_type_id (parser);
5983           /* Look for the closing `)'.  */
5984           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5985           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5986         }
5987
5988       /* Restore the saved message.  */
5989       parser->type_definition_forbidden_message = saved_message;
5990
5991       /* If ok so far, parse the dependent expression. We cannot be
5992          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5993          ctor of T, but looks like a cast to function returning T
5994          without a dependent expression.  */
5995       if (!cp_parser_error_occurred (parser))
5996         expr = cp_parser_cast_expression (parser,
5997                                           /*address_p=*/false,
5998                                           /*cast_p=*/true);
5999
6000       if (cp_parser_parse_definitely (parser))
6001         {
6002           /* Warn about old-style casts, if so requested.  */
6003           if (warn_old_style_cast
6004               && !in_system_header
6005               && !VOID_TYPE_P (type)
6006               && current_lang_name != lang_name_c)
6007             warning (OPT_Wold_style_cast, "use of old-style cast");
6008
6009           /* Only type conversions to integral or enumeration types
6010              can be used in constant-expressions.  */
6011           if (!cast_valid_in_integral_constant_expression_p (type)
6012               && (cp_parser_non_integral_constant_expression
6013                   (parser,
6014                    "a cast to a type other than an integral or "
6015                    "enumeration type")))
6016             return error_mark_node;
6017
6018           /* Perform the cast.  */
6019           expr = build_c_cast (type, expr);
6020           return expr;
6021         }
6022     }
6023
6024   /* If we get here, then it's not a cast, so it must be a
6025      unary-expression.  */
6026   return cp_parser_unary_expression (parser, address_p, cast_p);
6027 }
6028
6029 /* Parse a binary expression of the general form:
6030
6031    pm-expression:
6032      cast-expression
6033      pm-expression .* cast-expression
6034      pm-expression ->* cast-expression
6035
6036    multiplicative-expression:
6037      pm-expression
6038      multiplicative-expression * pm-expression
6039      multiplicative-expression / pm-expression
6040      multiplicative-expression % pm-expression
6041
6042    additive-expression:
6043      multiplicative-expression
6044      additive-expression + multiplicative-expression
6045      additive-expression - multiplicative-expression
6046
6047    shift-expression:
6048      additive-expression
6049      shift-expression << additive-expression
6050      shift-expression >> additive-expression
6051
6052    relational-expression:
6053      shift-expression
6054      relational-expression < shift-expression
6055      relational-expression > shift-expression
6056      relational-expression <= shift-expression
6057      relational-expression >= shift-expression
6058
6059   GNU Extension:
6060
6061    relational-expression:
6062      relational-expression <? shift-expression
6063      relational-expression >? shift-expression
6064
6065    equality-expression:
6066      relational-expression
6067      equality-expression == relational-expression
6068      equality-expression != relational-expression
6069
6070    and-expression:
6071      equality-expression
6072      and-expression & equality-expression
6073
6074    exclusive-or-expression:
6075      and-expression
6076      exclusive-or-expression ^ and-expression
6077
6078    inclusive-or-expression:
6079      exclusive-or-expression
6080      inclusive-or-expression | exclusive-or-expression
6081
6082    logical-and-expression:
6083      inclusive-or-expression
6084      logical-and-expression && inclusive-or-expression
6085
6086    logical-or-expression:
6087      logical-and-expression
6088      logical-or-expression || logical-and-expression
6089
6090    All these are implemented with a single function like:
6091
6092    binary-expression:
6093      simple-cast-expression
6094      binary-expression <token> binary-expression
6095
6096    CAST_P is true if this expression is the target of a cast.
6097
6098    The binops_by_token map is used to get the tree codes for each <token> type.
6099    binary-expressions are associated according to a precedence table.  */
6100
6101 #define TOKEN_PRECEDENCE(token)                              \
6102 (((token->type == CPP_GREATER                                \
6103    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6104   && !parser->greater_than_is_operator_p)                    \
6105  ? PREC_NOT_OPERATOR                                         \
6106  : binops_by_token[token->type].prec)
6107
6108 static tree
6109 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6110                              enum cp_parser_prec prec)
6111 {
6112   cp_parser_expression_stack stack;
6113   cp_parser_expression_stack_entry *sp = &stack[0];
6114   tree lhs, rhs;
6115   cp_token *token;
6116   enum tree_code tree_type, lhs_type, rhs_type;
6117   enum cp_parser_prec new_prec, lookahead_prec;
6118   bool overloaded_p;
6119
6120   /* Parse the first expression.  */
6121   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6122   lhs_type = ERROR_MARK;
6123
6124   for (;;)
6125     {
6126       /* Get an operator token.  */
6127       token = cp_lexer_peek_token (parser->lexer);
6128
6129       if (warn_cxx0x_compat
6130           && token->type == CPP_RSHIFT
6131           && !parser->greater_than_is_operator_p)
6132         {
6133           warning (OPT_Wc__0x_compat, 
6134                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6135                    &token->location);
6136           warning (OPT_Wc__0x_compat, 
6137                    "suggest parentheses around %<>>%> expression");
6138         }
6139
6140       new_prec = TOKEN_PRECEDENCE (token);
6141
6142       /* Popping an entry off the stack means we completed a subexpression:
6143          - either we found a token which is not an operator (`>' where it is not
6144            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6145            will happen repeatedly;
6146          - or, we found an operator which has lower priority.  This is the case
6147            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6148            parsing `3 * 4'.  */
6149       if (new_prec <= prec)
6150         {
6151           if (sp == stack)
6152             break;
6153           else
6154             goto pop;
6155         }
6156
6157      get_rhs:
6158       tree_type = binops_by_token[token->type].tree_type;
6159
6160       /* We used the operator token.  */
6161       cp_lexer_consume_token (parser->lexer);
6162
6163       /* Extract another operand.  It may be the RHS of this expression
6164          or the LHS of a new, higher priority expression.  */
6165       rhs = cp_parser_simple_cast_expression (parser);
6166       rhs_type = ERROR_MARK;
6167
6168       /* Get another operator token.  Look up its precedence to avoid
6169          building a useless (immediately popped) stack entry for common
6170          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6171       token = cp_lexer_peek_token (parser->lexer);
6172       lookahead_prec = TOKEN_PRECEDENCE (token);
6173       if (lookahead_prec > new_prec)
6174         {
6175           /* ... and prepare to parse the RHS of the new, higher priority
6176              expression.  Since precedence levels on the stack are
6177              monotonically increasing, we do not have to care about
6178              stack overflows.  */
6179           sp->prec = prec;
6180           sp->tree_type = tree_type;
6181           sp->lhs = lhs;
6182           sp->lhs_type = lhs_type;
6183           sp++;
6184           lhs = rhs;
6185           lhs_type = rhs_type;
6186           prec = new_prec;
6187           new_prec = lookahead_prec;
6188           goto get_rhs;
6189
6190          pop:
6191           /* If the stack is not empty, we have parsed into LHS the right side
6192              (`4' in the example above) of an expression we had suspended.
6193              We can use the information on the stack to recover the LHS (`3')
6194              from the stack together with the tree code (`MULT_EXPR'), and
6195              the precedence of the higher level subexpression
6196              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6197              which will be used to actually build the additive expression.  */
6198           --sp;
6199           prec = sp->prec;
6200           tree_type = sp->tree_type;
6201           rhs = lhs;
6202           rhs_type = lhs_type;
6203           lhs = sp->lhs;
6204           lhs_type = sp->lhs_type;
6205         }
6206
6207       overloaded_p = false;
6208       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6209                                &overloaded_p, tf_warning_or_error);
6210       lhs_type = tree_type;
6211
6212       /* If the binary operator required the use of an overloaded operator,
6213          then this expression cannot be an integral constant-expression.
6214          An overloaded operator can be used even if both operands are
6215          otherwise permissible in an integral constant-expression if at
6216          least one of the operands is of enumeration type.  */
6217
6218       if (overloaded_p
6219           && (cp_parser_non_integral_constant_expression
6220               (parser, "calls to overloaded operators")))
6221         return error_mark_node;
6222     }
6223
6224   return lhs;
6225 }
6226
6227
6228 /* Parse the `? expression : assignment-expression' part of a
6229    conditional-expression.  The LOGICAL_OR_EXPR is the
6230    logical-or-expression that started the conditional-expression.
6231    Returns a representation of the entire conditional-expression.
6232
6233    This routine is used by cp_parser_assignment_expression.
6234
6235      ? expression : assignment-expression
6236
6237    GNU Extensions:
6238
6239      ? : assignment-expression */
6240
6241 static tree
6242 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6243 {
6244   tree expr;
6245   tree assignment_expr;
6246
6247   /* Consume the `?' token.  */
6248   cp_lexer_consume_token (parser->lexer);
6249   if (cp_parser_allow_gnu_extensions_p (parser)
6250       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6251     /* Implicit true clause.  */
6252     expr = NULL_TREE;
6253   else
6254     /* Parse the expression.  */
6255     expr = cp_parser_expression (parser, /*cast_p=*/false);
6256
6257   /* The next token should be a `:'.  */
6258   cp_parser_require (parser, CPP_COLON, "%<:%>");
6259   /* Parse the assignment-expression.  */
6260   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6261
6262   /* Build the conditional-expression.  */
6263   return build_x_conditional_expr (logical_or_expr,
6264                                    expr,
6265                                    assignment_expr,
6266                                    tf_warning_or_error);
6267 }
6268
6269 /* Parse an assignment-expression.
6270
6271    assignment-expression:
6272      conditional-expression
6273      logical-or-expression assignment-operator assignment_expression
6274      throw-expression
6275
6276    CAST_P is true if this expression is the target of a cast.
6277
6278    Returns a representation for the expression.  */
6279
6280 static tree
6281 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6282 {
6283   tree expr;
6284
6285   /* If the next token is the `throw' keyword, then we're looking at
6286      a throw-expression.  */
6287   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6288     expr = cp_parser_throw_expression (parser);
6289   /* Otherwise, it must be that we are looking at a
6290      logical-or-expression.  */
6291   else
6292     {
6293       /* Parse the binary expressions (logical-or-expression).  */
6294       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6295       /* If the next token is a `?' then we're actually looking at a
6296          conditional-expression.  */
6297       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6298         return cp_parser_question_colon_clause (parser, expr);
6299       else
6300         {
6301           enum tree_code assignment_operator;
6302
6303           /* If it's an assignment-operator, we're using the second
6304              production.  */
6305           assignment_operator
6306             = cp_parser_assignment_operator_opt (parser);
6307           if (assignment_operator != ERROR_MARK)
6308             {
6309               bool non_constant_p;
6310
6311               /* Parse the right-hand side of the assignment.  */
6312               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6313
6314               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6315                 maybe_warn_cpp0x ("extended initializer lists");
6316
6317               /* An assignment may not appear in a
6318                  constant-expression.  */
6319               if (cp_parser_non_integral_constant_expression (parser,
6320                                                               "an assignment"))
6321                 return error_mark_node;
6322               /* Build the assignment expression.  */
6323               expr = build_x_modify_expr (expr,
6324                                           assignment_operator,
6325                                           rhs,
6326                                           tf_warning_or_error);
6327             }
6328         }
6329     }
6330
6331   return expr;
6332 }
6333
6334 /* Parse an (optional) assignment-operator.
6335
6336    assignment-operator: one of
6337      = *= /= %= += -= >>= <<= &= ^= |=
6338
6339    GNU Extension:
6340
6341    assignment-operator: one of
6342      <?= >?=
6343
6344    If the next token is an assignment operator, the corresponding tree
6345    code is returned, and the token is consumed.  For example, for
6346    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6347    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6348    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6349    operator, ERROR_MARK is returned.  */
6350
6351 static enum tree_code
6352 cp_parser_assignment_operator_opt (cp_parser* parser)
6353 {
6354   enum tree_code op;
6355   cp_token *token;
6356
6357   /* Peek at the next token.  */
6358   token = cp_lexer_peek_token (parser->lexer);
6359
6360   switch (token->type)
6361     {
6362     case CPP_EQ:
6363       op = NOP_EXPR;
6364       break;
6365
6366     case CPP_MULT_EQ:
6367       op = MULT_EXPR;
6368       break;
6369
6370     case CPP_DIV_EQ:
6371       op = TRUNC_DIV_EXPR;
6372       break;
6373
6374     case CPP_MOD_EQ:
6375       op = TRUNC_MOD_EXPR;
6376       break;
6377
6378     case CPP_PLUS_EQ:
6379       op = PLUS_EXPR;
6380       break;
6381
6382     case CPP_MINUS_EQ:
6383       op = MINUS_EXPR;
6384       break;
6385
6386     case CPP_RSHIFT_EQ:
6387       op = RSHIFT_EXPR;
6388       break;
6389
6390     case CPP_LSHIFT_EQ:
6391       op = LSHIFT_EXPR;
6392       break;
6393
6394     case CPP_AND_EQ:
6395       op = BIT_AND_EXPR;
6396       break;
6397
6398     case CPP_XOR_EQ:
6399       op = BIT_XOR_EXPR;
6400       break;
6401
6402     case CPP_OR_EQ:
6403       op = BIT_IOR_EXPR;
6404       break;
6405
6406     default:
6407       /* Nothing else is an assignment operator.  */
6408       op = ERROR_MARK;
6409     }
6410
6411   /* If it was an assignment operator, consume it.  */
6412   if (op != ERROR_MARK)
6413     cp_lexer_consume_token (parser->lexer);
6414
6415   return op;
6416 }
6417
6418 /* Parse an expression.
6419
6420    expression:
6421      assignment-expression
6422      expression , assignment-expression
6423
6424    CAST_P is true if this expression is the target of a cast.
6425
6426    Returns a representation of the expression.  */
6427
6428 static tree
6429 cp_parser_expression (cp_parser* parser, bool cast_p)
6430 {
6431   tree expression = NULL_TREE;
6432
6433   while (true)
6434     {
6435       tree assignment_expression;
6436
6437       /* Parse the next assignment-expression.  */
6438       assignment_expression
6439         = cp_parser_assignment_expression (parser, cast_p);
6440       /* If this is the first assignment-expression, we can just
6441          save it away.  */
6442       if (!expression)
6443         expression = assignment_expression;
6444       else
6445         expression = build_x_compound_expr (expression,
6446                                             assignment_expression,
6447                                             tf_warning_or_error);
6448       /* If the next token is not a comma, then we are done with the
6449          expression.  */
6450       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6451         break;
6452       /* Consume the `,'.  */
6453       cp_lexer_consume_token (parser->lexer);
6454       /* A comma operator cannot appear in a constant-expression.  */
6455       if (cp_parser_non_integral_constant_expression (parser,
6456                                                       "a comma operator"))
6457         expression = error_mark_node;
6458     }
6459
6460   return expression;
6461 }
6462
6463 /* Parse a constant-expression.
6464
6465    constant-expression:
6466      conditional-expression
6467
6468   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6469   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6470   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6471   is false, NON_CONSTANT_P should be NULL.  */
6472
6473 static tree
6474 cp_parser_constant_expression (cp_parser* parser,
6475                                bool allow_non_constant_p,
6476                                bool *non_constant_p)
6477 {
6478   bool saved_integral_constant_expression_p;
6479   bool saved_allow_non_integral_constant_expression_p;
6480   bool saved_non_integral_constant_expression_p;
6481   tree expression;
6482
6483   /* It might seem that we could simply parse the
6484      conditional-expression, and then check to see if it were
6485      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6486      one that the compiler can figure out is constant, possibly after
6487      doing some simplifications or optimizations.  The standard has a
6488      precise definition of constant-expression, and we must honor
6489      that, even though it is somewhat more restrictive.
6490
6491      For example:
6492
6493        int i[(2, 3)];
6494
6495      is not a legal declaration, because `(2, 3)' is not a
6496      constant-expression.  The `,' operator is forbidden in a
6497      constant-expression.  However, GCC's constant-folding machinery
6498      will fold this operation to an INTEGER_CST for `3'.  */
6499
6500   /* Save the old settings.  */
6501   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6502   saved_allow_non_integral_constant_expression_p
6503     = parser->allow_non_integral_constant_expression_p;
6504   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6505   /* We are now parsing a constant-expression.  */
6506   parser->integral_constant_expression_p = true;
6507   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6508   parser->non_integral_constant_expression_p = false;
6509   /* Although the grammar says "conditional-expression", we parse an
6510      "assignment-expression", which also permits "throw-expression"
6511      and the use of assignment operators.  In the case that
6512      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6513      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6514      actually essential that we look for an assignment-expression.
6515      For example, cp_parser_initializer_clauses uses this function to
6516      determine whether a particular assignment-expression is in fact
6517      constant.  */
6518   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6519   /* Restore the old settings.  */
6520   parser->integral_constant_expression_p
6521     = saved_integral_constant_expression_p;
6522   parser->allow_non_integral_constant_expression_p
6523     = saved_allow_non_integral_constant_expression_p;
6524   if (allow_non_constant_p)
6525     *non_constant_p = parser->non_integral_constant_expression_p;
6526   else if (parser->non_integral_constant_expression_p)
6527     expression = error_mark_node;
6528   parser->non_integral_constant_expression_p
6529     = saved_non_integral_constant_expression_p;
6530
6531   return expression;
6532 }
6533
6534 /* Parse __builtin_offsetof.
6535
6536    offsetof-expression:
6537      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6538
6539    offsetof-member-designator:
6540      id-expression
6541      | offsetof-member-designator "." id-expression
6542      | offsetof-member-designator "[" expression "]"  */
6543
6544 static tree
6545 cp_parser_builtin_offsetof (cp_parser *parser)
6546 {
6547   int save_ice_p, save_non_ice_p;
6548   tree type, expr;
6549   cp_id_kind dummy;
6550   cp_token *token;
6551
6552   /* We're about to accept non-integral-constant things, but will
6553      definitely yield an integral constant expression.  Save and
6554      restore these values around our local parsing.  */
6555   save_ice_p = parser->integral_constant_expression_p;
6556   save_non_ice_p = parser->non_integral_constant_expression_p;
6557
6558   /* Consume the "__builtin_offsetof" token.  */
6559   cp_lexer_consume_token (parser->lexer);
6560   /* Consume the opening `('.  */
6561   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6562   /* Parse the type-id.  */
6563   type = cp_parser_type_id (parser);
6564   /* Look for the `,'.  */
6565   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6566   token = cp_lexer_peek_token (parser->lexer);
6567
6568   /* Build the (type *)null that begins the traditional offsetof macro.  */
6569   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6570                             tf_warning_or_error);
6571
6572   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6573   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6574                                                  true, &dummy, token->location);
6575   while (true)
6576     {
6577       token = cp_lexer_peek_token (parser->lexer);
6578       switch (token->type)
6579         {
6580         case CPP_OPEN_SQUARE:
6581           /* offsetof-member-designator "[" expression "]" */
6582           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6583           break;
6584
6585         case CPP_DOT:
6586           /* offsetof-member-designator "." identifier */
6587           cp_lexer_consume_token (parser->lexer);
6588           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6589                                                          true, &dummy,
6590                                                          token->location);
6591           break;
6592
6593         case CPP_CLOSE_PAREN:
6594           /* Consume the ")" token.  */
6595           cp_lexer_consume_token (parser->lexer);
6596           goto success;
6597
6598         default:
6599           /* Error.  We know the following require will fail, but
6600              that gives the proper error message.  */
6601           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6602           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6603           expr = error_mark_node;
6604           goto failure;
6605         }
6606     }
6607
6608  success:
6609   /* If we're processing a template, we can't finish the semantics yet.
6610      Otherwise we can fold the entire expression now.  */
6611   if (processing_template_decl)
6612     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6613   else
6614     expr = finish_offsetof (expr);
6615
6616  failure:
6617   parser->integral_constant_expression_p = save_ice_p;
6618   parser->non_integral_constant_expression_p = save_non_ice_p;
6619
6620   return expr;
6621 }
6622
6623 /* Parse a trait expression.  */
6624
6625 static tree
6626 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6627 {
6628   cp_trait_kind kind;
6629   tree type1, type2 = NULL_TREE;
6630   bool binary = false;
6631   cp_decl_specifier_seq decl_specs;
6632
6633   switch (keyword)
6634     {
6635     case RID_HAS_NOTHROW_ASSIGN:
6636       kind = CPTK_HAS_NOTHROW_ASSIGN;
6637       break;
6638     case RID_HAS_NOTHROW_CONSTRUCTOR:
6639       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6640       break;
6641     case RID_HAS_NOTHROW_COPY:
6642       kind = CPTK_HAS_NOTHROW_COPY;
6643       break;
6644     case RID_HAS_TRIVIAL_ASSIGN:
6645       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6646       break;
6647     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6648       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6649       break;
6650     case RID_HAS_TRIVIAL_COPY:
6651       kind = CPTK_HAS_TRIVIAL_COPY;
6652       break;
6653     case RID_HAS_TRIVIAL_DESTRUCTOR:
6654       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6655       break;
6656     case RID_HAS_VIRTUAL_DESTRUCTOR:
6657       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6658       break;
6659     case RID_IS_ABSTRACT:
6660       kind = CPTK_IS_ABSTRACT;
6661       break;
6662     case RID_IS_BASE_OF:
6663       kind = CPTK_IS_BASE_OF;
6664       binary = true;
6665       break;
6666     case RID_IS_CLASS:
6667       kind = CPTK_IS_CLASS;
6668       break;
6669     case RID_IS_CONVERTIBLE_TO:
6670       kind = CPTK_IS_CONVERTIBLE_TO;
6671       binary = true;
6672       break;
6673     case RID_IS_EMPTY:
6674       kind = CPTK_IS_EMPTY;
6675       break;
6676     case RID_IS_ENUM:
6677       kind = CPTK_IS_ENUM;
6678       break;
6679     case RID_IS_POD:
6680       kind = CPTK_IS_POD;
6681       break;
6682     case RID_IS_POLYMORPHIC:
6683       kind = CPTK_IS_POLYMORPHIC;
6684       break;
6685     case RID_IS_UNION:
6686       kind = CPTK_IS_UNION;
6687       break;
6688     default:
6689       gcc_unreachable ();
6690     }
6691
6692   /* Consume the token.  */
6693   cp_lexer_consume_token (parser->lexer);
6694
6695   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6696
6697   type1 = cp_parser_type_id (parser);
6698
6699   if (type1 == error_mark_node)
6700     return error_mark_node;
6701
6702   /* Build a trivial decl-specifier-seq.  */
6703   clear_decl_specs (&decl_specs);
6704   decl_specs.type = type1;
6705
6706   /* Call grokdeclarator to figure out what type this is.  */
6707   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6708                           /*initialized=*/0, /*attrlist=*/NULL);
6709
6710   if (binary)
6711     {
6712       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6713  
6714       type2 = cp_parser_type_id (parser);
6715
6716       if (type2 == error_mark_node)
6717         return error_mark_node;
6718
6719       /* Build a trivial decl-specifier-seq.  */
6720       clear_decl_specs (&decl_specs);
6721       decl_specs.type = type2;
6722
6723       /* Call grokdeclarator to figure out what type this is.  */
6724       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6725                               /*initialized=*/0, /*attrlist=*/NULL);
6726     }
6727
6728   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6729
6730   /* Complete the trait expression, which may mean either processing
6731      the trait expr now or saving it for template instantiation.  */
6732   return finish_trait_expr (kind, type1, type2);
6733 }
6734
6735 /* Statements [gram.stmt.stmt]  */
6736
6737 /* Parse a statement.
6738
6739    statement:
6740      labeled-statement
6741      expression-statement
6742      compound-statement
6743      selection-statement
6744      iteration-statement
6745      jump-statement
6746      declaration-statement
6747      try-block
6748
6749   IN_COMPOUND is true when the statement is nested inside a
6750   cp_parser_compound_statement; this matters for certain pragmas.
6751
6752   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6753   is a (possibly labeled) if statement which is not enclosed in braces
6754   and has an else clause.  This is used to implement -Wparentheses.  */
6755
6756 static void
6757 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6758                      bool in_compound, bool *if_p)
6759 {
6760   tree statement;
6761   cp_token *token;
6762   location_t statement_location;
6763
6764  restart:
6765   if (if_p != NULL)
6766     *if_p = false;
6767   /* There is no statement yet.  */
6768   statement = NULL_TREE;
6769   /* Peek at the next token.  */
6770   token = cp_lexer_peek_token (parser->lexer);
6771   /* Remember the location of the first token in the statement.  */
6772   statement_location = token->location;
6773   /* If this is a keyword, then that will often determine what kind of
6774      statement we have.  */
6775   if (token->type == CPP_KEYWORD)
6776     {
6777       enum rid keyword = token->keyword;
6778
6779       switch (keyword)
6780         {
6781         case RID_CASE:
6782         case RID_DEFAULT:
6783           /* Looks like a labeled-statement with a case label.
6784              Parse the label, and then use tail recursion to parse
6785              the statement.  */
6786           cp_parser_label_for_labeled_statement (parser);
6787           goto restart;
6788
6789         case RID_IF:
6790         case RID_SWITCH:
6791           statement = cp_parser_selection_statement (parser, if_p);
6792           break;
6793
6794         case RID_WHILE:
6795         case RID_DO:
6796         case RID_FOR:
6797           statement = cp_parser_iteration_statement (parser);
6798           break;
6799
6800         case RID_BREAK:
6801         case RID_CONTINUE:
6802         case RID_RETURN:
6803         case RID_GOTO:
6804           statement = cp_parser_jump_statement (parser);
6805           break;
6806
6807           /* Objective-C++ exception-handling constructs.  */
6808         case RID_AT_TRY:
6809         case RID_AT_CATCH:
6810         case RID_AT_FINALLY:
6811         case RID_AT_SYNCHRONIZED:
6812         case RID_AT_THROW:
6813           statement = cp_parser_objc_statement (parser);
6814           break;
6815
6816         case RID_TRY:
6817           statement = cp_parser_try_block (parser);
6818           break;
6819
6820         case RID_NAMESPACE:
6821           /* This must be a namespace alias definition.  */
6822           cp_parser_declaration_statement (parser);
6823           return;
6824           
6825         default:
6826           /* It might be a keyword like `int' that can start a
6827              declaration-statement.  */
6828           break;
6829         }
6830     }
6831   else if (token->type == CPP_NAME)
6832     {
6833       /* If the next token is a `:', then we are looking at a
6834          labeled-statement.  */
6835       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6836       if (token->type == CPP_COLON)
6837         {
6838           /* Looks like a labeled-statement with an ordinary label.
6839              Parse the label, and then use tail recursion to parse
6840              the statement.  */
6841           cp_parser_label_for_labeled_statement (parser);
6842           goto restart;
6843         }
6844     }
6845   /* Anything that starts with a `{' must be a compound-statement.  */
6846   else if (token->type == CPP_OPEN_BRACE)
6847     statement = cp_parser_compound_statement (parser, NULL, false);
6848   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6849      a statement all its own.  */
6850   else if (token->type == CPP_PRAGMA)
6851     {
6852       /* Only certain OpenMP pragmas are attached to statements, and thus
6853          are considered statements themselves.  All others are not.  In
6854          the context of a compound, accept the pragma as a "statement" and
6855          return so that we can check for a close brace.  Otherwise we
6856          require a real statement and must go back and read one.  */
6857       if (in_compound)
6858         cp_parser_pragma (parser, pragma_compound);
6859       else if (!cp_parser_pragma (parser, pragma_stmt))
6860         goto restart;
6861       return;
6862     }
6863   else if (token->type == CPP_EOF)
6864     {
6865       cp_parser_error (parser, "expected statement");
6866       return;
6867     }
6868
6869   /* Everything else must be a declaration-statement or an
6870      expression-statement.  Try for the declaration-statement
6871      first, unless we are looking at a `;', in which case we know that
6872      we have an expression-statement.  */
6873   if (!statement)
6874     {
6875       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6876         {
6877           cp_parser_parse_tentatively (parser);
6878           /* Try to parse the declaration-statement.  */
6879           cp_parser_declaration_statement (parser);
6880           /* If that worked, we're done.  */
6881           if (cp_parser_parse_definitely (parser))
6882             return;
6883         }
6884       /* Look for an expression-statement instead.  */
6885       statement = cp_parser_expression_statement (parser, in_statement_expr);
6886     }
6887
6888   /* Set the line number for the statement.  */
6889   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6890     SET_EXPR_LOCATION (statement, statement_location);
6891 }
6892
6893 /* Parse the label for a labeled-statement, i.e.
6894
6895    identifier :
6896    case constant-expression :
6897    default :
6898
6899    GNU Extension:
6900    case constant-expression ... constant-expression : statement
6901
6902    When a label is parsed without errors, the label is added to the
6903    parse tree by the finish_* functions, so this function doesn't
6904    have to return the label.  */
6905
6906 static void
6907 cp_parser_label_for_labeled_statement (cp_parser* parser)
6908 {
6909   cp_token *token;
6910
6911   /* The next token should be an identifier.  */
6912   token = cp_lexer_peek_token (parser->lexer);
6913   if (token->type != CPP_NAME
6914       && token->type != CPP_KEYWORD)
6915     {
6916       cp_parser_error (parser, "expected labeled-statement");
6917       return;
6918     }
6919
6920   switch (token->keyword)
6921     {
6922     case RID_CASE:
6923       {
6924         tree expr, expr_hi;
6925         cp_token *ellipsis;
6926
6927         /* Consume the `case' token.  */
6928         cp_lexer_consume_token (parser->lexer);
6929         /* Parse the constant-expression.  */
6930         expr = cp_parser_constant_expression (parser,
6931                                               /*allow_non_constant_p=*/false,
6932                                               NULL);
6933
6934         ellipsis = cp_lexer_peek_token (parser->lexer);
6935         if (ellipsis->type == CPP_ELLIPSIS)
6936           {
6937             /* Consume the `...' token.  */
6938             cp_lexer_consume_token (parser->lexer);
6939             expr_hi =
6940               cp_parser_constant_expression (parser,
6941                                              /*allow_non_constant_p=*/false,
6942                                              NULL);
6943             /* We don't need to emit warnings here, as the common code
6944                will do this for us.  */
6945           }
6946         else
6947           expr_hi = NULL_TREE;
6948
6949         if (parser->in_switch_statement_p)
6950           finish_case_label (expr, expr_hi);
6951         else
6952           error ("%Hcase label %qE not within a switch statement",
6953                  &token->location, expr);
6954       }
6955       break;
6956
6957     case RID_DEFAULT:
6958       /* Consume the `default' token.  */
6959       cp_lexer_consume_token (parser->lexer);
6960
6961       if (parser->in_switch_statement_p)
6962         finish_case_label (NULL_TREE, NULL_TREE);
6963       else
6964         error ("%Hcase label not within a switch statement", &token->location);
6965       break;
6966
6967     default:
6968       /* Anything else must be an ordinary label.  */
6969       finish_label_stmt (cp_parser_identifier (parser));
6970       break;
6971     }
6972
6973   /* Require the `:' token.  */
6974   cp_parser_require (parser, CPP_COLON, "%<:%>");
6975 }
6976
6977 /* Parse an expression-statement.
6978
6979    expression-statement:
6980      expression [opt] ;
6981
6982    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6983    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6984    indicates whether this expression-statement is part of an
6985    expression statement.  */
6986
6987 static tree
6988 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6989 {
6990   tree statement = NULL_TREE;
6991
6992   /* If the next token is a ';', then there is no expression
6993      statement.  */
6994   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6995     statement = cp_parser_expression (parser, /*cast_p=*/false);
6996
6997   /* Consume the final `;'.  */
6998   cp_parser_consume_semicolon_at_end_of_statement (parser);
6999
7000   if (in_statement_expr
7001       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7002     /* This is the final expression statement of a statement
7003        expression.  */
7004     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7005   else if (statement)
7006     statement = finish_expr_stmt (statement);
7007   else
7008     finish_stmt ();
7009
7010   return statement;
7011 }
7012
7013 /* Parse a compound-statement.
7014
7015    compound-statement:
7016      { statement-seq [opt] }
7017
7018    GNU extension:
7019
7020    compound-statement:
7021      { label-declaration-seq [opt] statement-seq [opt] }
7022
7023    label-declaration-seq:
7024      label-declaration
7025      label-declaration-seq label-declaration
7026
7027    Returns a tree representing the statement.  */
7028
7029 static tree
7030 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7031                               bool in_try)
7032 {
7033   tree compound_stmt;
7034
7035   /* Consume the `{'.  */
7036   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7037     return error_mark_node;
7038   /* Begin the compound-statement.  */
7039   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7040   /* If the next keyword is `__label__' we have a label declaration.  */
7041   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7042     cp_parser_label_declaration (parser);
7043   /* Parse an (optional) statement-seq.  */
7044   cp_parser_statement_seq_opt (parser, in_statement_expr);
7045   /* Finish the compound-statement.  */
7046   finish_compound_stmt (compound_stmt);
7047   /* Consume the `}'.  */
7048   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7049
7050   return compound_stmt;
7051 }
7052
7053 /* Parse an (optional) statement-seq.
7054
7055    statement-seq:
7056      statement
7057      statement-seq [opt] statement  */
7058
7059 static void
7060 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7061 {
7062   /* Scan statements until there aren't any more.  */
7063   while (true)
7064     {
7065       cp_token *token = cp_lexer_peek_token (parser->lexer);
7066
7067       /* If we're looking at a `}', then we've run out of statements.  */
7068       if (token->type == CPP_CLOSE_BRACE
7069           || token->type == CPP_EOF
7070           || token->type == CPP_PRAGMA_EOL)
7071         break;
7072       
7073       /* If we are in a compound statement and find 'else' then
7074          something went wrong.  */
7075       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7076         {
7077           if (parser->in_statement & IN_IF_STMT) 
7078             break;
7079           else
7080             {
7081               token = cp_lexer_consume_token (parser->lexer);
7082               error ("%H%<else%> without a previous %<if%>", &token->location);
7083             }
7084         }
7085
7086       /* Parse the statement.  */
7087       cp_parser_statement (parser, in_statement_expr, true, NULL);
7088     }
7089 }
7090
7091 /* Parse a selection-statement.
7092
7093    selection-statement:
7094      if ( condition ) statement
7095      if ( condition ) statement else statement
7096      switch ( condition ) statement
7097
7098    Returns the new IF_STMT or SWITCH_STMT.
7099
7100    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7101    is a (possibly labeled) if statement which is not enclosed in
7102    braces and has an else clause.  This is used to implement
7103    -Wparentheses.  */
7104
7105 static tree
7106 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7107 {
7108   cp_token *token;
7109   enum rid keyword;
7110
7111   if (if_p != NULL)
7112     *if_p = false;
7113
7114   /* Peek at the next token.  */
7115   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7116
7117   /* See what kind of keyword it is.  */
7118   keyword = token->keyword;
7119   switch (keyword)
7120     {
7121     case RID_IF:
7122     case RID_SWITCH:
7123       {
7124         tree statement;
7125         tree condition;
7126
7127         /* Look for the `('.  */
7128         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7129           {
7130             cp_parser_skip_to_end_of_statement (parser);
7131             return error_mark_node;
7132           }
7133
7134         /* Begin the selection-statement.  */
7135         if (keyword == RID_IF)
7136           statement = begin_if_stmt ();
7137         else
7138           statement = begin_switch_stmt ();
7139
7140         /* Parse the condition.  */
7141         condition = cp_parser_condition (parser);
7142         /* Look for the `)'.  */
7143         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7144           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7145                                                  /*consume_paren=*/true);
7146
7147         if (keyword == RID_IF)
7148           {
7149             bool nested_if;
7150             unsigned char in_statement;
7151
7152             /* Add the condition.  */
7153             finish_if_stmt_cond (condition, statement);
7154
7155             /* Parse the then-clause.  */
7156             in_statement = parser->in_statement;
7157             parser->in_statement |= IN_IF_STMT;
7158             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7159             parser->in_statement = in_statement;
7160
7161             finish_then_clause (statement);
7162
7163             /* If the next token is `else', parse the else-clause.  */
7164             if (cp_lexer_next_token_is_keyword (parser->lexer,
7165                                                 RID_ELSE))
7166               {
7167                 /* Consume the `else' keyword.  */
7168                 cp_lexer_consume_token (parser->lexer);
7169                 begin_else_clause (statement);
7170                 /* Parse the else-clause.  */
7171                 cp_parser_implicitly_scoped_statement (parser, NULL);
7172                 finish_else_clause (statement);
7173
7174                 /* If we are currently parsing a then-clause, then
7175                    IF_P will not be NULL.  We set it to true to
7176                    indicate that this if statement has an else clause.
7177                    This may trigger the Wparentheses warning below
7178                    when we get back up to the parent if statement.  */
7179                 if (if_p != NULL)
7180                   *if_p = true;
7181               }
7182             else
7183               {
7184                 /* This if statement does not have an else clause.  If
7185                    NESTED_IF is true, then the then-clause is an if
7186                    statement which does have an else clause.  We warn
7187                    about the potential ambiguity.  */
7188                 if (nested_if)
7189                   warning (OPT_Wparentheses,
7190                            ("%Hsuggest explicit braces "
7191                             "to avoid ambiguous %<else%>"),
7192                            EXPR_LOCUS (statement));
7193               }
7194
7195             /* Now we're all done with the if-statement.  */
7196             finish_if_stmt (statement);
7197           }
7198         else
7199           {
7200             bool in_switch_statement_p;
7201             unsigned char in_statement;
7202
7203             /* Add the condition.  */
7204             finish_switch_cond (condition, statement);
7205
7206             /* Parse the body of the switch-statement.  */
7207             in_switch_statement_p = parser->in_switch_statement_p;
7208             in_statement = parser->in_statement;
7209             parser->in_switch_statement_p = true;
7210             parser->in_statement |= IN_SWITCH_STMT;
7211             cp_parser_implicitly_scoped_statement (parser, NULL);
7212             parser->in_switch_statement_p = in_switch_statement_p;
7213             parser->in_statement = in_statement;
7214
7215             /* Now we're all done with the switch-statement.  */
7216             finish_switch_stmt (statement);
7217           }
7218
7219         return statement;
7220       }
7221       break;
7222
7223     default:
7224       cp_parser_error (parser, "expected selection-statement");
7225       return error_mark_node;
7226     }
7227 }
7228
7229 /* Parse a condition.
7230
7231    condition:
7232      expression
7233      type-specifier-seq declarator = initializer-clause
7234      type-specifier-seq declarator braced-init-list
7235
7236    GNU Extension:
7237
7238    condition:
7239      type-specifier-seq declarator asm-specification [opt]
7240        attributes [opt] = assignment-expression
7241
7242    Returns the expression that should be tested.  */
7243
7244 static tree
7245 cp_parser_condition (cp_parser* parser)
7246 {
7247   cp_decl_specifier_seq type_specifiers;
7248   const char *saved_message;
7249
7250   /* Try the declaration first.  */
7251   cp_parser_parse_tentatively (parser);
7252   /* New types are not allowed in the type-specifier-seq for a
7253      condition.  */
7254   saved_message = parser->type_definition_forbidden_message;
7255   parser->type_definition_forbidden_message
7256     = "types may not be defined in conditions";
7257   /* Parse the type-specifier-seq.  */
7258   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7259                                 &type_specifiers);
7260   /* Restore the saved message.  */
7261   parser->type_definition_forbidden_message = saved_message;
7262   /* If all is well, we might be looking at a declaration.  */
7263   if (!cp_parser_error_occurred (parser))
7264     {
7265       tree decl;
7266       tree asm_specification;
7267       tree attributes;
7268       cp_declarator *declarator;
7269       tree initializer = NULL_TREE;
7270
7271       /* Parse the declarator.  */
7272       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7273                                          /*ctor_dtor_or_conv_p=*/NULL,
7274                                          /*parenthesized_p=*/NULL,
7275                                          /*member_p=*/false);
7276       /* Parse the attributes.  */
7277       attributes = cp_parser_attributes_opt (parser);
7278       /* Parse the asm-specification.  */
7279       asm_specification = cp_parser_asm_specification_opt (parser);
7280       /* If the next token is not an `=' or '{', then we might still be
7281          looking at an expression.  For example:
7282
7283            if (A(a).x)
7284
7285          looks like a decl-specifier-seq and a declarator -- but then
7286          there is no `=', so this is an expression.  */
7287       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7288           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7289         cp_parser_simulate_error (parser);
7290         
7291       /* If we did see an `=' or '{', then we are looking at a declaration
7292          for sure.  */
7293       if (cp_parser_parse_definitely (parser))
7294         {
7295           tree pushed_scope;
7296           bool non_constant_p;
7297           bool flags = LOOKUP_ONLYCONVERTING;
7298
7299           /* Create the declaration.  */
7300           decl = start_decl (declarator, &type_specifiers,
7301                              /*initialized_p=*/true,
7302                              attributes, /*prefix_attributes=*/NULL_TREE,
7303                              &pushed_scope);
7304
7305           /* Parse the initializer.  */
7306           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7307             {
7308               initializer = cp_parser_braced_list (parser, &non_constant_p);
7309               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7310               flags = 0;
7311             }
7312           else
7313             {
7314               /* Consume the `='.  */
7315               cp_lexer_consume_token (parser->lexer);
7316               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7317             }
7318           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7319             maybe_warn_cpp0x ("extended initializer lists");
7320
7321           if (!non_constant_p)
7322             initializer = fold_non_dependent_expr (initializer);
7323
7324           /* Process the initializer.  */
7325           cp_finish_decl (decl,
7326                           initializer, !non_constant_p,
7327                           asm_specification,
7328                           flags);
7329
7330           if (pushed_scope)
7331             pop_scope (pushed_scope);
7332
7333           return convert_from_reference (decl);
7334         }
7335     }
7336   /* If we didn't even get past the declarator successfully, we are
7337      definitely not looking at a declaration.  */
7338   else
7339     cp_parser_abort_tentative_parse (parser);
7340
7341   /* Otherwise, we are looking at an expression.  */
7342   return cp_parser_expression (parser, /*cast_p=*/false);
7343 }
7344
7345 /* We check for a ) immediately followed by ; with no whitespacing
7346    between.  This is used to issue a warning for:
7347
7348      while (...);
7349
7350    and:
7351
7352      for (...);
7353
7354    as the semicolon is probably extraneous.
7355
7356    On parse errors, the next token might not be a ), so do nothing in
7357    that case. */
7358
7359 static void
7360 check_empty_body (cp_parser* parser, const char* type)
7361 {
7362   cp_token *token;
7363   cp_token *close_paren;
7364   expanded_location close_loc;
7365   expanded_location semi_loc;
7366   
7367   close_paren = cp_lexer_peek_token (parser->lexer);
7368   if (close_paren->type != CPP_CLOSE_PAREN)
7369     return;
7370
7371   close_loc = expand_location (close_paren->location);
7372   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7373
7374   if (token->type != CPP_SEMICOLON
7375       || (token->flags & PREV_WHITE))
7376     return;
7377
7378   semi_loc =  expand_location (token->location);
7379   if (close_loc.line == semi_loc.line
7380       && close_loc.column+1 == semi_loc.column)
7381     warning (OPT_Wempty_body,
7382              "suggest a space before %<;%> or explicit braces around empty "
7383              "body in %<%s%> statement",
7384              type);
7385 }
7386
7387 /* Parse an iteration-statement.
7388
7389    iteration-statement:
7390      while ( condition ) statement
7391      do statement while ( expression ) ;
7392      for ( for-init-statement condition [opt] ; expression [opt] )
7393        statement
7394
7395    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7396
7397 static tree
7398 cp_parser_iteration_statement (cp_parser* parser)
7399 {
7400   cp_token *token;
7401   enum rid keyword;
7402   tree statement;
7403   unsigned char in_statement;
7404
7405   /* Peek at the next token.  */
7406   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7407   if (!token)
7408     return error_mark_node;
7409
7410   /* Remember whether or not we are already within an iteration
7411      statement.  */
7412   in_statement = parser->in_statement;
7413
7414   /* See what kind of keyword it is.  */
7415   keyword = token->keyword;
7416   switch (keyword)
7417     {
7418     case RID_WHILE:
7419       {
7420         tree condition;
7421
7422         /* Begin the while-statement.  */
7423         statement = begin_while_stmt ();
7424         /* Look for the `('.  */
7425         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7426         /* Parse the condition.  */
7427         condition = cp_parser_condition (parser);
7428         finish_while_stmt_cond (condition, statement);
7429         check_empty_body (parser, "while");
7430         /* Look for the `)'.  */
7431         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7432         /* Parse the dependent statement.  */
7433         parser->in_statement = IN_ITERATION_STMT;
7434         cp_parser_already_scoped_statement (parser);
7435         parser->in_statement = in_statement;
7436         /* We're done with the while-statement.  */
7437         finish_while_stmt (statement);
7438       }
7439       break;
7440
7441     case RID_DO:
7442       {
7443         tree expression;
7444
7445         /* Begin the do-statement.  */
7446         statement = begin_do_stmt ();
7447         /* Parse the body of the do-statement.  */
7448         parser->in_statement = IN_ITERATION_STMT;
7449         cp_parser_implicitly_scoped_statement (parser, NULL);
7450         parser->in_statement = in_statement;
7451         finish_do_body (statement);
7452         /* Look for the `while' keyword.  */
7453         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7454         /* Look for the `('.  */
7455         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7456         /* Parse the expression.  */
7457         expression = cp_parser_expression (parser, /*cast_p=*/false);
7458         /* We're done with the do-statement.  */
7459         finish_do_stmt (expression, statement);
7460         /* Look for the `)'.  */
7461         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7462         /* Look for the `;'.  */
7463         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7464       }
7465       break;
7466
7467     case RID_FOR:
7468       {
7469         tree condition = NULL_TREE;
7470         tree expression = NULL_TREE;
7471
7472         /* Begin the for-statement.  */
7473         statement = begin_for_stmt ();
7474         /* Look for the `('.  */
7475         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7476         /* Parse the initialization.  */
7477         cp_parser_for_init_statement (parser);
7478         finish_for_init_stmt (statement);
7479
7480         /* If there's a condition, process it.  */
7481         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7482           condition = cp_parser_condition (parser);
7483         finish_for_cond (condition, statement);
7484         /* Look for the `;'.  */
7485         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7486
7487         /* If there's an expression, process it.  */
7488         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7489           expression = cp_parser_expression (parser, /*cast_p=*/false);
7490         finish_for_expr (expression, statement);
7491         check_empty_body (parser, "for");
7492         /* Look for the `)'.  */
7493         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7494
7495         /* Parse the body of the for-statement.  */
7496         parser->in_statement = IN_ITERATION_STMT;
7497         cp_parser_already_scoped_statement (parser);
7498         parser->in_statement = in_statement;
7499
7500         /* We're done with the for-statement.  */
7501         finish_for_stmt (statement);
7502       }
7503       break;
7504
7505     default:
7506       cp_parser_error (parser, "expected iteration-statement");
7507       statement = error_mark_node;
7508       break;
7509     }
7510
7511   return statement;
7512 }
7513
7514 /* Parse a for-init-statement.
7515
7516    for-init-statement:
7517      expression-statement
7518      simple-declaration  */
7519
7520 static void
7521 cp_parser_for_init_statement (cp_parser* parser)
7522 {
7523   /* If the next token is a `;', then we have an empty
7524      expression-statement.  Grammatically, this is also a
7525      simple-declaration, but an invalid one, because it does not
7526      declare anything.  Therefore, if we did not handle this case
7527      specially, we would issue an error message about an invalid
7528      declaration.  */
7529   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7530     {
7531       /* We're going to speculatively look for a declaration, falling back
7532          to an expression, if necessary.  */
7533       cp_parser_parse_tentatively (parser);
7534       /* Parse the declaration.  */
7535       cp_parser_simple_declaration (parser,
7536                                     /*function_definition_allowed_p=*/false);
7537       /* If the tentative parse failed, then we shall need to look for an
7538          expression-statement.  */
7539       if (cp_parser_parse_definitely (parser))
7540         return;
7541     }
7542
7543   cp_parser_expression_statement (parser, false);
7544 }
7545
7546 /* Parse a jump-statement.
7547
7548    jump-statement:
7549      break ;
7550      continue ;
7551      return expression [opt] ;
7552      return braced-init-list ;
7553      goto identifier ;
7554
7555    GNU extension:
7556
7557    jump-statement:
7558      goto * expression ;
7559
7560    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7561
7562 static tree
7563 cp_parser_jump_statement (cp_parser* parser)
7564 {
7565   tree statement = error_mark_node;
7566   cp_token *token;
7567   enum rid keyword;
7568   unsigned char in_statement;
7569
7570   /* Peek at the next token.  */
7571   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7572   if (!token)
7573     return error_mark_node;
7574
7575   /* See what kind of keyword it is.  */
7576   keyword = token->keyword;
7577   switch (keyword)
7578     {
7579     case RID_BREAK:
7580       in_statement = parser->in_statement & ~IN_IF_STMT;      
7581       switch (in_statement)
7582         {
7583         case 0:
7584           error ("%Hbreak statement not within loop or switch", &token->location);
7585           break;
7586         default:
7587           gcc_assert ((in_statement & IN_SWITCH_STMT)
7588                       || in_statement == IN_ITERATION_STMT);
7589           statement = finish_break_stmt ();
7590           break;
7591         case IN_OMP_BLOCK:
7592           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7593           break;
7594         case IN_OMP_FOR:
7595           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7596           break;
7597         }
7598       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7599       break;
7600
7601     case RID_CONTINUE:
7602       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7603         {
7604         case 0:
7605           error ("%Hcontinue statement not within a loop", &token->location);
7606           break;
7607         case IN_ITERATION_STMT:
7608         case IN_OMP_FOR:
7609           statement = finish_continue_stmt ();
7610           break;
7611         case IN_OMP_BLOCK:
7612           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7613           break;
7614         default:
7615           gcc_unreachable ();
7616         }
7617       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7618       break;
7619
7620     case RID_RETURN:
7621       {
7622         tree expr;
7623         bool expr_non_constant_p;
7624
7625         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7626           {
7627             maybe_warn_cpp0x ("extended initializer lists");
7628             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7629           }
7630         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7631           expr = cp_parser_expression (parser, /*cast_p=*/false);
7632         else
7633           /* If the next token is a `;', then there is no
7634              expression.  */
7635           expr = NULL_TREE;
7636         /* Build the return-statement.  */
7637         statement = finish_return_stmt (expr);
7638         /* Look for the final `;'.  */
7639         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7640       }
7641       break;
7642
7643     case RID_GOTO:
7644       /* Create the goto-statement.  */
7645       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7646         {
7647           /* Issue a warning about this use of a GNU extension.  */
7648           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7649           /* Consume the '*' token.  */
7650           cp_lexer_consume_token (parser->lexer);
7651           /* Parse the dependent expression.  */
7652           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7653         }
7654       else
7655         finish_goto_stmt (cp_parser_identifier (parser));
7656       /* Look for the final `;'.  */
7657       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7658       break;
7659
7660     default:
7661       cp_parser_error (parser, "expected jump-statement");
7662       break;
7663     }
7664
7665   return statement;
7666 }
7667
7668 /* Parse a declaration-statement.
7669
7670    declaration-statement:
7671      block-declaration  */
7672
7673 static void
7674 cp_parser_declaration_statement (cp_parser* parser)
7675 {
7676   void *p;
7677
7678   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7679   p = obstack_alloc (&declarator_obstack, 0);
7680
7681  /* Parse the block-declaration.  */
7682   cp_parser_block_declaration (parser, /*statement_p=*/true);
7683
7684   /* Free any declarators allocated.  */
7685   obstack_free (&declarator_obstack, p);
7686
7687   /* Finish off the statement.  */
7688   finish_stmt ();
7689 }
7690
7691 /* Some dependent statements (like `if (cond) statement'), are
7692    implicitly in their own scope.  In other words, if the statement is
7693    a single statement (as opposed to a compound-statement), it is
7694    none-the-less treated as if it were enclosed in braces.  Any
7695    declarations appearing in the dependent statement are out of scope
7696    after control passes that point.  This function parses a statement,
7697    but ensures that is in its own scope, even if it is not a
7698    compound-statement.
7699
7700    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7701    is a (possibly labeled) if statement which is not enclosed in
7702    braces and has an else clause.  This is used to implement
7703    -Wparentheses.
7704
7705    Returns the new statement.  */
7706
7707 static tree
7708 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7709 {
7710   tree statement;
7711
7712   if (if_p != NULL)
7713     *if_p = false;
7714
7715   /* Mark if () ; with a special NOP_EXPR.  */
7716   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7717     {
7718       cp_lexer_consume_token (parser->lexer);
7719       statement = add_stmt (build_empty_stmt ());
7720     }
7721   /* if a compound is opened, we simply parse the statement directly.  */
7722   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7723     statement = cp_parser_compound_statement (parser, NULL, false);
7724   /* If the token is not a `{', then we must take special action.  */
7725   else
7726     {
7727       /* Create a compound-statement.  */
7728       statement = begin_compound_stmt (0);
7729       /* Parse the dependent-statement.  */
7730       cp_parser_statement (parser, NULL_TREE, false, if_p);
7731       /* Finish the dummy compound-statement.  */
7732       finish_compound_stmt (statement);
7733     }
7734
7735   /* Return the statement.  */
7736   return statement;
7737 }
7738
7739 /* For some dependent statements (like `while (cond) statement'), we
7740    have already created a scope.  Therefore, even if the dependent
7741    statement is a compound-statement, we do not want to create another
7742    scope.  */
7743
7744 static void
7745 cp_parser_already_scoped_statement (cp_parser* parser)
7746 {
7747   /* If the token is a `{', then we must take special action.  */
7748   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7749     cp_parser_statement (parser, NULL_TREE, false, NULL);
7750   else
7751     {
7752       /* Avoid calling cp_parser_compound_statement, so that we
7753          don't create a new scope.  Do everything else by hand.  */
7754       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7755       cp_parser_statement_seq_opt (parser, NULL_TREE);
7756       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7757     }
7758 }
7759
7760 /* Declarations [gram.dcl.dcl] */
7761
7762 /* Parse an optional declaration-sequence.
7763
7764    declaration-seq:
7765      declaration
7766      declaration-seq declaration  */
7767
7768 static void
7769 cp_parser_declaration_seq_opt (cp_parser* parser)
7770 {
7771   while (true)
7772     {
7773       cp_token *token;
7774
7775       token = cp_lexer_peek_token (parser->lexer);
7776
7777       if (token->type == CPP_CLOSE_BRACE
7778           || token->type == CPP_EOF
7779           || token->type == CPP_PRAGMA_EOL)
7780         break;
7781
7782       if (token->type == CPP_SEMICOLON)
7783         {
7784           /* A declaration consisting of a single semicolon is
7785              invalid.  Allow it unless we're being pedantic.  */
7786           cp_lexer_consume_token (parser->lexer);
7787           if (!in_system_header)
7788             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7789           continue;
7790         }
7791
7792       /* If we're entering or exiting a region that's implicitly
7793          extern "C", modify the lang context appropriately.  */
7794       if (!parser->implicit_extern_c && token->implicit_extern_c)
7795         {
7796           push_lang_context (lang_name_c);
7797           parser->implicit_extern_c = true;
7798         }
7799       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7800         {
7801           pop_lang_context ();
7802           parser->implicit_extern_c = false;
7803         }
7804
7805       if (token->type == CPP_PRAGMA)
7806         {
7807           /* A top-level declaration can consist solely of a #pragma.
7808              A nested declaration cannot, so this is done here and not
7809              in cp_parser_declaration.  (A #pragma at block scope is
7810              handled in cp_parser_statement.)  */
7811           cp_parser_pragma (parser, pragma_external);
7812           continue;
7813         }
7814
7815       /* Parse the declaration itself.  */
7816       cp_parser_declaration (parser);
7817     }
7818 }
7819
7820 /* Parse a declaration.
7821
7822    declaration:
7823      block-declaration
7824      function-definition
7825      template-declaration
7826      explicit-instantiation
7827      explicit-specialization
7828      linkage-specification
7829      namespace-definition
7830
7831    GNU extension:
7832
7833    declaration:
7834       __extension__ declaration */
7835
7836 static void
7837 cp_parser_declaration (cp_parser* parser)
7838 {
7839   cp_token token1;
7840   cp_token token2;
7841   int saved_pedantic;
7842   void *p;
7843
7844   /* Check for the `__extension__' keyword.  */
7845   if (cp_parser_extension_opt (parser, &saved_pedantic))
7846     {
7847       /* Parse the qualified declaration.  */
7848       cp_parser_declaration (parser);
7849       /* Restore the PEDANTIC flag.  */
7850       pedantic = saved_pedantic;
7851
7852       return;
7853     }
7854
7855   /* Try to figure out what kind of declaration is present.  */
7856   token1 = *cp_lexer_peek_token (parser->lexer);
7857
7858   if (token1.type != CPP_EOF)
7859     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7860   else
7861     {
7862       token2.type = CPP_EOF;
7863       token2.keyword = RID_MAX;
7864     }
7865
7866   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7867   p = obstack_alloc (&declarator_obstack, 0);
7868
7869   /* If the next token is `extern' and the following token is a string
7870      literal, then we have a linkage specification.  */
7871   if (token1.keyword == RID_EXTERN
7872       && cp_parser_is_string_literal (&token2))
7873     cp_parser_linkage_specification (parser);
7874   /* If the next token is `template', then we have either a template
7875      declaration, an explicit instantiation, or an explicit
7876      specialization.  */
7877   else if (token1.keyword == RID_TEMPLATE)
7878     {
7879       /* `template <>' indicates a template specialization.  */
7880       if (token2.type == CPP_LESS
7881           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7882         cp_parser_explicit_specialization (parser);
7883       /* `template <' indicates a template declaration.  */
7884       else if (token2.type == CPP_LESS)
7885         cp_parser_template_declaration (parser, /*member_p=*/false);
7886       /* Anything else must be an explicit instantiation.  */
7887       else
7888         cp_parser_explicit_instantiation (parser);
7889     }
7890   /* If the next token is `export', then we have a template
7891      declaration.  */
7892   else if (token1.keyword == RID_EXPORT)
7893     cp_parser_template_declaration (parser, /*member_p=*/false);
7894   /* If the next token is `extern', 'static' or 'inline' and the one
7895      after that is `template', we have a GNU extended explicit
7896      instantiation directive.  */
7897   else if (cp_parser_allow_gnu_extensions_p (parser)
7898            && (token1.keyword == RID_EXTERN
7899                || token1.keyword == RID_STATIC
7900                || token1.keyword == RID_INLINE)
7901            && token2.keyword == RID_TEMPLATE)
7902     cp_parser_explicit_instantiation (parser);
7903   /* If the next token is `namespace', check for a named or unnamed
7904      namespace definition.  */
7905   else if (token1.keyword == RID_NAMESPACE
7906            && (/* A named namespace definition.  */
7907                (token2.type == CPP_NAME
7908                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7909                     != CPP_EQ))
7910                /* An unnamed namespace definition.  */
7911                || token2.type == CPP_OPEN_BRACE
7912                || token2.keyword == RID_ATTRIBUTE))
7913     cp_parser_namespace_definition (parser);
7914   /* An inline (associated) namespace definition.  */
7915   else if (token1.keyword == RID_INLINE
7916            && token2.keyword == RID_NAMESPACE)
7917     cp_parser_namespace_definition (parser);
7918   /* Objective-C++ declaration/definition.  */
7919   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7920     cp_parser_objc_declaration (parser);
7921   /* We must have either a block declaration or a function
7922      definition.  */
7923   else
7924     /* Try to parse a block-declaration, or a function-definition.  */
7925     cp_parser_block_declaration (parser, /*statement_p=*/false);
7926
7927   /* Free any declarators allocated.  */
7928   obstack_free (&declarator_obstack, p);
7929 }
7930
7931 /* Parse a block-declaration.
7932
7933    block-declaration:
7934      simple-declaration
7935      asm-definition
7936      namespace-alias-definition
7937      using-declaration
7938      using-directive
7939
7940    GNU Extension:
7941
7942    block-declaration:
7943      __extension__ block-declaration
7944
7945    C++0x Extension:
7946
7947    block-declaration:
7948      static_assert-declaration
7949
7950    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7951    part of a declaration-statement.  */
7952
7953 static void
7954 cp_parser_block_declaration (cp_parser *parser,
7955                              bool      statement_p)
7956 {
7957   cp_token *token1;
7958   int saved_pedantic;
7959
7960   /* Check for the `__extension__' keyword.  */
7961   if (cp_parser_extension_opt (parser, &saved_pedantic))
7962     {
7963       /* Parse the qualified declaration.  */
7964       cp_parser_block_declaration (parser, statement_p);
7965       /* Restore the PEDANTIC flag.  */
7966       pedantic = saved_pedantic;
7967
7968       return;
7969     }
7970
7971   /* Peek at the next token to figure out which kind of declaration is
7972      present.  */
7973   token1 = cp_lexer_peek_token (parser->lexer);
7974
7975   /* If the next keyword is `asm', we have an asm-definition.  */
7976   if (token1->keyword == RID_ASM)
7977     {
7978       if (statement_p)
7979         cp_parser_commit_to_tentative_parse (parser);
7980       cp_parser_asm_definition (parser);
7981     }
7982   /* If the next keyword is `namespace', we have a
7983      namespace-alias-definition.  */
7984   else if (token1->keyword == RID_NAMESPACE)
7985     cp_parser_namespace_alias_definition (parser);
7986   /* If the next keyword is `using', we have either a
7987      using-declaration or a using-directive.  */
7988   else if (token1->keyword == RID_USING)
7989     {
7990       cp_token *token2;
7991
7992       if (statement_p)
7993         cp_parser_commit_to_tentative_parse (parser);
7994       /* If the token after `using' is `namespace', then we have a
7995          using-directive.  */
7996       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7997       if (token2->keyword == RID_NAMESPACE)
7998         cp_parser_using_directive (parser);
7999       /* Otherwise, it's a using-declaration.  */
8000       else
8001         cp_parser_using_declaration (parser,
8002                                      /*access_declaration_p=*/false);
8003     }
8004   /* If the next keyword is `__label__' we have a misplaced label
8005      declaration.  */
8006   else if (token1->keyword == RID_LABEL)
8007     {
8008       cp_lexer_consume_token (parser->lexer);
8009       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8010       cp_parser_skip_to_end_of_statement (parser);
8011       /* If the next token is now a `;', consume it.  */
8012       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8013         cp_lexer_consume_token (parser->lexer);
8014     }
8015   /* If the next token is `static_assert' we have a static assertion.  */
8016   else if (token1->keyword == RID_STATIC_ASSERT)
8017     cp_parser_static_assert (parser, /*member_p=*/false);
8018   /* Anything else must be a simple-declaration.  */
8019   else
8020     cp_parser_simple_declaration (parser, !statement_p);
8021 }
8022
8023 /* Parse a simple-declaration.
8024
8025    simple-declaration:
8026      decl-specifier-seq [opt] init-declarator-list [opt] ;
8027
8028    init-declarator-list:
8029      init-declarator
8030      init-declarator-list , init-declarator
8031
8032    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8033    function-definition as a simple-declaration.  */
8034
8035 static void
8036 cp_parser_simple_declaration (cp_parser* parser,
8037                               bool function_definition_allowed_p)
8038 {
8039   cp_decl_specifier_seq decl_specifiers;
8040   int declares_class_or_enum;
8041   bool saw_declarator;
8042
8043   /* Defer access checks until we know what is being declared; the
8044      checks for names appearing in the decl-specifier-seq should be
8045      done as if we were in the scope of the thing being declared.  */
8046   push_deferring_access_checks (dk_deferred);
8047
8048   /* Parse the decl-specifier-seq.  We have to keep track of whether
8049      or not the decl-specifier-seq declares a named class or
8050      enumeration type, since that is the only case in which the
8051      init-declarator-list is allowed to be empty.
8052
8053      [dcl.dcl]
8054
8055      In a simple-declaration, the optional init-declarator-list can be
8056      omitted only when declaring a class or enumeration, that is when
8057      the decl-specifier-seq contains either a class-specifier, an
8058      elaborated-type-specifier, or an enum-specifier.  */
8059   cp_parser_decl_specifier_seq (parser,
8060                                 CP_PARSER_FLAGS_OPTIONAL,
8061                                 &decl_specifiers,
8062                                 &declares_class_or_enum);
8063   /* We no longer need to defer access checks.  */
8064   stop_deferring_access_checks ();
8065
8066   /* In a block scope, a valid declaration must always have a
8067      decl-specifier-seq.  By not trying to parse declarators, we can
8068      resolve the declaration/expression ambiguity more quickly.  */
8069   if (!function_definition_allowed_p
8070       && !decl_specifiers.any_specifiers_p)
8071     {
8072       cp_parser_error (parser, "expected declaration");
8073       goto done;
8074     }
8075
8076   /* If the next two tokens are both identifiers, the code is
8077      erroneous. The usual cause of this situation is code like:
8078
8079        T t;
8080
8081      where "T" should name a type -- but does not.  */
8082   if (!decl_specifiers.type
8083       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8084     {
8085       /* If parsing tentatively, we should commit; we really are
8086          looking at a declaration.  */
8087       cp_parser_commit_to_tentative_parse (parser);
8088       /* Give up.  */
8089       goto done;
8090     }
8091
8092   /* If we have seen at least one decl-specifier, and the next token
8093      is not a parenthesis, then we must be looking at a declaration.
8094      (After "int (" we might be looking at a functional cast.)  */
8095   if (decl_specifiers.any_specifiers_p
8096       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8097       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8098     cp_parser_commit_to_tentative_parse (parser);
8099
8100   /* Keep going until we hit the `;' at the end of the simple
8101      declaration.  */
8102   saw_declarator = false;
8103   while (cp_lexer_next_token_is_not (parser->lexer,
8104                                      CPP_SEMICOLON))
8105     {
8106       cp_token *token;
8107       bool function_definition_p;
8108       tree decl;
8109
8110       if (saw_declarator)
8111         {
8112           /* If we are processing next declarator, coma is expected */
8113           token = cp_lexer_peek_token (parser->lexer);
8114           gcc_assert (token->type == CPP_COMMA);
8115           cp_lexer_consume_token (parser->lexer);
8116         }
8117       else
8118         saw_declarator = true;
8119
8120       /* Parse the init-declarator.  */
8121       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8122                                         /*checks=*/NULL,
8123                                         function_definition_allowed_p,
8124                                         /*member_p=*/false,
8125                                         declares_class_or_enum,
8126                                         &function_definition_p);
8127       /* If an error occurred while parsing tentatively, exit quickly.
8128          (That usually happens when in the body of a function; each
8129          statement is treated as a declaration-statement until proven
8130          otherwise.)  */
8131       if (cp_parser_error_occurred (parser))
8132         goto done;
8133       /* Handle function definitions specially.  */
8134       if (function_definition_p)
8135         {
8136           /* If the next token is a `,', then we are probably
8137              processing something like:
8138
8139                void f() {}, *p;
8140
8141              which is erroneous.  */
8142           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8143             {
8144               cp_token *token = cp_lexer_peek_token (parser->lexer);
8145               error ("%Hmixing declarations and function-definitions is forbidden",
8146                      &token->location);
8147             }
8148           /* Otherwise, we're done with the list of declarators.  */
8149           else
8150             {
8151               pop_deferring_access_checks ();
8152               return;
8153             }
8154         }
8155       /* The next token should be either a `,' or a `;'.  */
8156       token = cp_lexer_peek_token (parser->lexer);
8157       /* If it's a `,', there are more declarators to come.  */
8158       if (token->type == CPP_COMMA)
8159         /* will be consumed next time around */;
8160       /* If it's a `;', we are done.  */
8161       else if (token->type == CPP_SEMICOLON)
8162         break;
8163       /* Anything else is an error.  */
8164       else
8165         {
8166           /* If we have already issued an error message we don't need
8167              to issue another one.  */
8168           if (decl != error_mark_node
8169               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8170             cp_parser_error (parser, "expected %<,%> or %<;%>");
8171           /* Skip tokens until we reach the end of the statement.  */
8172           cp_parser_skip_to_end_of_statement (parser);
8173           /* If the next token is now a `;', consume it.  */
8174           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8175             cp_lexer_consume_token (parser->lexer);
8176           goto done;
8177         }
8178       /* After the first time around, a function-definition is not
8179          allowed -- even if it was OK at first.  For example:
8180
8181            int i, f() {}
8182
8183          is not valid.  */
8184       function_definition_allowed_p = false;
8185     }
8186
8187   /* Issue an error message if no declarators are present, and the
8188      decl-specifier-seq does not itself declare a class or
8189      enumeration.  */
8190   if (!saw_declarator)
8191     {
8192       if (cp_parser_declares_only_class_p (parser))
8193         shadow_tag (&decl_specifiers);
8194       /* Perform any deferred access checks.  */
8195       perform_deferred_access_checks ();
8196     }
8197
8198   /* Consume the `;'.  */
8199   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8200
8201  done:
8202   pop_deferring_access_checks ();
8203 }
8204
8205 /* Parse a decl-specifier-seq.
8206
8207    decl-specifier-seq:
8208      decl-specifier-seq [opt] decl-specifier
8209
8210    decl-specifier:
8211      storage-class-specifier
8212      type-specifier
8213      function-specifier
8214      friend
8215      typedef
8216
8217    GNU Extension:
8218
8219    decl-specifier:
8220      attributes
8221
8222    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8223
8224    The parser flags FLAGS is used to control type-specifier parsing.
8225
8226    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8227    flags:
8228
8229      1: one of the decl-specifiers is an elaborated-type-specifier
8230         (i.e., a type declaration)
8231      2: one of the decl-specifiers is an enum-specifier or a
8232         class-specifier (i.e., a type definition)
8233
8234    */
8235
8236 static void
8237 cp_parser_decl_specifier_seq (cp_parser* parser,
8238                               cp_parser_flags flags,
8239                               cp_decl_specifier_seq *decl_specs,
8240                               int* declares_class_or_enum)
8241 {
8242   bool constructor_possible_p = !parser->in_declarator_p;
8243   cp_token *start_token = NULL;
8244
8245   /* Clear DECL_SPECS.  */
8246   clear_decl_specs (decl_specs);
8247
8248   /* Assume no class or enumeration type is declared.  */
8249   *declares_class_or_enum = 0;
8250
8251   /* Keep reading specifiers until there are no more to read.  */
8252   while (true)
8253     {
8254       bool constructor_p;
8255       bool found_decl_spec;
8256       cp_token *token;
8257
8258       /* Peek at the next token.  */
8259       token = cp_lexer_peek_token (parser->lexer);
8260
8261       /* Save the first token of the decl spec list for error
8262          reporting.  */
8263       if (!start_token)
8264         start_token = token;
8265       /* Handle attributes.  */
8266       if (token->keyword == RID_ATTRIBUTE)
8267         {
8268           /* Parse the attributes.  */
8269           decl_specs->attributes
8270             = chainon (decl_specs->attributes,
8271                        cp_parser_attributes_opt (parser));
8272           continue;
8273         }
8274       /* Assume we will find a decl-specifier keyword.  */
8275       found_decl_spec = true;
8276       /* If the next token is an appropriate keyword, we can simply
8277          add it to the list.  */
8278       switch (token->keyword)
8279         {
8280           /* decl-specifier:
8281                friend  */
8282         case RID_FRIEND:
8283           if (!at_class_scope_p ())
8284             {
8285               error ("%H%<friend%> used outside of class", &token->location);
8286               cp_lexer_purge_token (parser->lexer);
8287             }
8288           else
8289             {
8290               ++decl_specs->specs[(int) ds_friend];
8291               /* Consume the token.  */
8292               cp_lexer_consume_token (parser->lexer);
8293             }
8294           break;
8295
8296           /* function-specifier:
8297                inline
8298                virtual
8299                explicit  */
8300         case RID_INLINE:
8301         case RID_VIRTUAL:
8302         case RID_EXPLICIT:
8303           cp_parser_function_specifier_opt (parser, decl_specs);
8304           break;
8305
8306           /* decl-specifier:
8307                typedef  */
8308         case RID_TYPEDEF:
8309           ++decl_specs->specs[(int) ds_typedef];
8310           /* Consume the token.  */
8311           cp_lexer_consume_token (parser->lexer);
8312           /* A constructor declarator cannot appear in a typedef.  */
8313           constructor_possible_p = false;
8314           /* The "typedef" keyword can only occur in a declaration; we
8315              may as well commit at this point.  */
8316           cp_parser_commit_to_tentative_parse (parser);
8317
8318           if (decl_specs->storage_class != sc_none)
8319             decl_specs->conflicting_specifiers_p = true;
8320           break;
8321
8322           /* storage-class-specifier:
8323                auto
8324                register
8325                static
8326                extern
8327                mutable
8328
8329              GNU Extension:
8330                thread  */
8331         case RID_AUTO:
8332           if (cxx_dialect == cxx98) 
8333             {
8334               /* Consume the token.  */
8335               cp_lexer_consume_token (parser->lexer);
8336
8337               /* Complain about `auto' as a storage specifier, if
8338                  we're complaining about C++0x compatibility.  */
8339               warning 
8340                 (OPT_Wc__0x_compat, 
8341                  "%H%<auto%> will change meaning in C++0x; please remove it",
8342                  &token->location);
8343
8344               /* Set the storage class anyway.  */
8345               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8346                                            token->location);
8347             }
8348           else
8349             /* C++0x auto type-specifier.  */
8350             found_decl_spec = false;
8351           break;
8352
8353         case RID_REGISTER:
8354         case RID_STATIC:
8355         case RID_EXTERN:
8356         case RID_MUTABLE:
8357           /* Consume the token.  */
8358           cp_lexer_consume_token (parser->lexer);
8359           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8360                                        token->location);
8361           break;
8362         case RID_THREAD:
8363           /* Consume the token.  */
8364           cp_lexer_consume_token (parser->lexer);
8365           ++decl_specs->specs[(int) ds_thread];
8366           break;
8367
8368         default:
8369           /* We did not yet find a decl-specifier yet.  */
8370           found_decl_spec = false;
8371           break;
8372         }
8373
8374       /* Constructors are a special case.  The `S' in `S()' is not a
8375          decl-specifier; it is the beginning of the declarator.  */
8376       constructor_p
8377         = (!found_decl_spec
8378            && constructor_possible_p
8379            && (cp_parser_constructor_declarator_p
8380                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8381
8382       /* If we don't have a DECL_SPEC yet, then we must be looking at
8383          a type-specifier.  */
8384       if (!found_decl_spec && !constructor_p)
8385         {
8386           int decl_spec_declares_class_or_enum;
8387           bool is_cv_qualifier;
8388           tree type_spec;
8389
8390           type_spec
8391             = cp_parser_type_specifier (parser, flags,
8392                                         decl_specs,
8393                                         /*is_declaration=*/true,
8394                                         &decl_spec_declares_class_or_enum,
8395                                         &is_cv_qualifier);
8396           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8397
8398           /* If this type-specifier referenced a user-defined type
8399              (a typedef, class-name, etc.), then we can't allow any
8400              more such type-specifiers henceforth.
8401
8402              [dcl.spec]
8403
8404              The longest sequence of decl-specifiers that could
8405              possibly be a type name is taken as the
8406              decl-specifier-seq of a declaration.  The sequence shall
8407              be self-consistent as described below.
8408
8409              [dcl.type]
8410
8411              As a general rule, at most one type-specifier is allowed
8412              in the complete decl-specifier-seq of a declaration.  The
8413              only exceptions are the following:
8414
8415              -- const or volatile can be combined with any other
8416                 type-specifier.
8417
8418              -- signed or unsigned can be combined with char, long,
8419                 short, or int.
8420
8421              -- ..
8422
8423              Example:
8424
8425                typedef char* Pc;
8426                void g (const int Pc);
8427
8428              Here, Pc is *not* part of the decl-specifier seq; it's
8429              the declarator.  Therefore, once we see a type-specifier
8430              (other than a cv-qualifier), we forbid any additional
8431              user-defined types.  We *do* still allow things like `int
8432              int' to be considered a decl-specifier-seq, and issue the
8433              error message later.  */
8434           if (type_spec && !is_cv_qualifier)
8435             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8436           /* A constructor declarator cannot follow a type-specifier.  */
8437           if (type_spec)
8438             {
8439               constructor_possible_p = false;
8440               found_decl_spec = true;
8441             }
8442         }
8443
8444       /* If we still do not have a DECL_SPEC, then there are no more
8445          decl-specifiers.  */
8446       if (!found_decl_spec)
8447         break;
8448
8449       decl_specs->any_specifiers_p = true;
8450       /* After we see one decl-specifier, further decl-specifiers are
8451          always optional.  */
8452       flags |= CP_PARSER_FLAGS_OPTIONAL;
8453     }
8454
8455   cp_parser_check_decl_spec (decl_specs, start_token->location);
8456
8457   /* Don't allow a friend specifier with a class definition.  */
8458   if (decl_specs->specs[(int) ds_friend] != 0
8459       && (*declares_class_or_enum & 2))
8460     error ("%Hclass definition may not be declared a friend",
8461             &start_token->location);
8462 }
8463
8464 /* Parse an (optional) storage-class-specifier.
8465
8466    storage-class-specifier:
8467      auto
8468      register
8469      static
8470      extern
8471      mutable
8472
8473    GNU Extension:
8474
8475    storage-class-specifier:
8476      thread
8477
8478    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8479
8480 static tree
8481 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8482 {
8483   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8484     {
8485     case RID_AUTO:
8486       if (cxx_dialect != cxx98)
8487         return NULL_TREE;
8488       /* Fall through for C++98.  */
8489
8490     case RID_REGISTER:
8491     case RID_STATIC:
8492     case RID_EXTERN:
8493     case RID_MUTABLE:
8494     case RID_THREAD:
8495       /* Consume the token.  */
8496       return cp_lexer_consume_token (parser->lexer)->u.value;
8497
8498     default:
8499       return NULL_TREE;
8500     }
8501 }
8502
8503 /* Parse an (optional) function-specifier.
8504
8505    function-specifier:
8506      inline
8507      virtual
8508      explicit
8509
8510    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8511    Updates DECL_SPECS, if it is non-NULL.  */
8512
8513 static tree
8514 cp_parser_function_specifier_opt (cp_parser* parser,
8515                                   cp_decl_specifier_seq *decl_specs)
8516 {
8517   cp_token *token = cp_lexer_peek_token (parser->lexer);
8518   switch (token->keyword)
8519     {
8520     case RID_INLINE:
8521       if (decl_specs)
8522         ++decl_specs->specs[(int) ds_inline];
8523       break;
8524
8525     case RID_VIRTUAL:
8526       /* 14.5.2.3 [temp.mem]
8527
8528          A member function template shall not be virtual.  */
8529       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8530         error ("%Htemplates may not be %<virtual%>", &token->location);
8531       else if (decl_specs)
8532         ++decl_specs->specs[(int) ds_virtual];
8533       break;
8534
8535     case RID_EXPLICIT:
8536       if (decl_specs)
8537         ++decl_specs->specs[(int) ds_explicit];
8538       break;
8539
8540     default:
8541       return NULL_TREE;
8542     }
8543
8544   /* Consume the token.  */
8545   return cp_lexer_consume_token (parser->lexer)->u.value;
8546 }
8547
8548 /* Parse a linkage-specification.
8549
8550    linkage-specification:
8551      extern string-literal { declaration-seq [opt] }
8552      extern string-literal declaration  */
8553
8554 static void
8555 cp_parser_linkage_specification (cp_parser* parser)
8556 {
8557   tree linkage;
8558
8559   /* Look for the `extern' keyword.  */
8560   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8561
8562   /* Look for the string-literal.  */
8563   linkage = cp_parser_string_literal (parser, false, false);
8564
8565   /* Transform the literal into an identifier.  If the literal is a
8566      wide-character string, or contains embedded NULs, then we can't
8567      handle it as the user wants.  */
8568   if (strlen (TREE_STRING_POINTER (linkage))
8569       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8570     {
8571       cp_parser_error (parser, "invalid linkage-specification");
8572       /* Assume C++ linkage.  */
8573       linkage = lang_name_cplusplus;
8574     }
8575   else
8576     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8577
8578   /* We're now using the new linkage.  */
8579   push_lang_context (linkage);
8580
8581   /* If the next token is a `{', then we're using the first
8582      production.  */
8583   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8584     {
8585       /* Consume the `{' token.  */
8586       cp_lexer_consume_token (parser->lexer);
8587       /* Parse the declarations.  */
8588       cp_parser_declaration_seq_opt (parser);
8589       /* Look for the closing `}'.  */
8590       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8591     }
8592   /* Otherwise, there's just one declaration.  */
8593   else
8594     {
8595       bool saved_in_unbraced_linkage_specification_p;
8596
8597       saved_in_unbraced_linkage_specification_p
8598         = parser->in_unbraced_linkage_specification_p;
8599       parser->in_unbraced_linkage_specification_p = true;
8600       cp_parser_declaration (parser);
8601       parser->in_unbraced_linkage_specification_p
8602         = saved_in_unbraced_linkage_specification_p;
8603     }
8604
8605   /* We're done with the linkage-specification.  */
8606   pop_lang_context ();
8607 }
8608
8609 /* Parse a static_assert-declaration.
8610
8611    static_assert-declaration:
8612      static_assert ( constant-expression , string-literal ) ; 
8613
8614    If MEMBER_P, this static_assert is a class member.  */
8615
8616 static void 
8617 cp_parser_static_assert(cp_parser *parser, bool member_p)
8618 {
8619   tree condition;
8620   tree message;
8621   cp_token *token;
8622   location_t saved_loc;
8623
8624   /* Peek at the `static_assert' token so we can keep track of exactly
8625      where the static assertion started.  */
8626   token = cp_lexer_peek_token (parser->lexer);
8627   saved_loc = token->location;
8628
8629   /* Look for the `static_assert' keyword.  */
8630   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8631                                   "%<static_assert%>"))
8632     return;
8633
8634   /*  We know we are in a static assertion; commit to any tentative
8635       parse.  */
8636   if (cp_parser_parsing_tentatively (parser))
8637     cp_parser_commit_to_tentative_parse (parser);
8638
8639   /* Parse the `(' starting the static assertion condition.  */
8640   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8641
8642   /* Parse the constant-expression.  */
8643   condition = 
8644     cp_parser_constant_expression (parser,
8645                                    /*allow_non_constant_p=*/false,
8646                                    /*non_constant_p=*/NULL);
8647
8648   /* Parse the separating `,'.  */
8649   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8650
8651   /* Parse the string-literal message.  */
8652   message = cp_parser_string_literal (parser, 
8653                                       /*translate=*/false,
8654                                       /*wide_ok=*/true);
8655
8656   /* A `)' completes the static assertion.  */
8657   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8658     cp_parser_skip_to_closing_parenthesis (parser, 
8659                                            /*recovering=*/true, 
8660                                            /*or_comma=*/false,
8661                                            /*consume_paren=*/true);
8662
8663   /* A semicolon terminates the declaration.  */
8664   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8665
8666   /* Complete the static assertion, which may mean either processing 
8667      the static assert now or saving it for template instantiation.  */
8668   finish_static_assert (condition, message, saved_loc, member_p);
8669 }
8670
8671 /* Parse a `decltype' type. Returns the type. 
8672
8673    simple-type-specifier:
8674      decltype ( expression )  */
8675
8676 static tree
8677 cp_parser_decltype (cp_parser *parser)
8678 {
8679   tree expr;
8680   bool id_expression_or_member_access_p = false;
8681   const char *saved_message;
8682   bool saved_integral_constant_expression_p;
8683   bool saved_non_integral_constant_expression_p;
8684   cp_token *id_expr_start_token;
8685
8686   /* Look for the `decltype' token.  */
8687   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8688     return error_mark_node;
8689
8690   /* Types cannot be defined in a `decltype' expression.  Save away the
8691      old message.  */
8692   saved_message = parser->type_definition_forbidden_message;
8693
8694   /* And create the new one.  */
8695   parser->type_definition_forbidden_message
8696     = "types may not be defined in %<decltype%> expressions";
8697
8698   /* The restrictions on constant-expressions do not apply inside
8699      decltype expressions.  */
8700   saved_integral_constant_expression_p
8701     = parser->integral_constant_expression_p;
8702   saved_non_integral_constant_expression_p
8703     = parser->non_integral_constant_expression_p;
8704   parser->integral_constant_expression_p = false;
8705
8706   /* Do not actually evaluate the expression.  */
8707   ++skip_evaluation;
8708
8709   /* Parse the opening `('.  */
8710   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8711     return error_mark_node;
8712   
8713   /* First, try parsing an id-expression.  */
8714   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8715   cp_parser_parse_tentatively (parser);
8716   expr = cp_parser_id_expression (parser,
8717                                   /*template_keyword_p=*/false,
8718                                   /*check_dependency_p=*/true,
8719                                   /*template_p=*/NULL,
8720                                   /*declarator_p=*/false,
8721                                   /*optional_p=*/false);
8722
8723   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8724     {
8725       bool non_integral_constant_expression_p = false;
8726       tree id_expression = expr;
8727       cp_id_kind idk;
8728       const char *error_msg;
8729
8730       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8731         /* Lookup the name we got back from the id-expression.  */
8732         expr = cp_parser_lookup_name (parser, expr,
8733                                       none_type,
8734                                       /*is_template=*/false,
8735                                       /*is_namespace=*/false,
8736                                       /*check_dependency=*/true,
8737                                       /*ambiguous_decls=*/NULL,
8738                                       id_expr_start_token->location);
8739
8740       if (expr
8741           && expr != error_mark_node
8742           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8743           && TREE_CODE (expr) != TYPE_DECL
8744           && (TREE_CODE (expr) != BIT_NOT_EXPR
8745               || !TYPE_P (TREE_OPERAND (expr, 0)))
8746           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8747         {
8748           /* Complete lookup of the id-expression.  */
8749           expr = (finish_id_expression
8750                   (id_expression, expr, parser->scope, &idk,
8751                    /*integral_constant_expression_p=*/false,
8752                    /*allow_non_integral_constant_expression_p=*/true,
8753                    &non_integral_constant_expression_p,
8754                    /*template_p=*/false,
8755                    /*done=*/true,
8756                    /*address_p=*/false,
8757                    /*template_arg_p=*/false,
8758                    &error_msg,
8759                    id_expr_start_token->location));
8760
8761           if (expr == error_mark_node)
8762             /* We found an id-expression, but it was something that we
8763                should not have found. This is an error, not something
8764                we can recover from, so note that we found an
8765                id-expression and we'll recover as gracefully as
8766                possible.  */
8767             id_expression_or_member_access_p = true;
8768         }
8769
8770       if (expr 
8771           && expr != error_mark_node
8772           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8773         /* We have an id-expression.  */
8774         id_expression_or_member_access_p = true;
8775     }
8776
8777   if (!id_expression_or_member_access_p)
8778     {
8779       /* Abort the id-expression parse.  */
8780       cp_parser_abort_tentative_parse (parser);
8781
8782       /* Parsing tentatively, again.  */
8783       cp_parser_parse_tentatively (parser);
8784
8785       /* Parse a class member access.  */
8786       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8787                                            /*cast_p=*/false,
8788                                            /*member_access_only_p=*/true);
8789
8790       if (expr 
8791           && expr != error_mark_node
8792           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8793         /* We have an id-expression.  */
8794         id_expression_or_member_access_p = true;
8795     }
8796
8797   if (id_expression_or_member_access_p)
8798     /* We have parsed the complete id-expression or member access.  */
8799     cp_parser_parse_definitely (parser);
8800   else
8801     {
8802       /* Abort our attempt to parse an id-expression or member access
8803          expression.  */
8804       cp_parser_abort_tentative_parse (parser);
8805
8806       /* Parse a full expression.  */
8807       expr = cp_parser_expression (parser, /*cast_p=*/false);
8808     }
8809
8810   /* Go back to evaluating expressions.  */
8811   --skip_evaluation;
8812
8813   /* Restore the old message and the integral constant expression
8814      flags.  */
8815   parser->type_definition_forbidden_message = saved_message;
8816   parser->integral_constant_expression_p
8817     = saved_integral_constant_expression_p;
8818   parser->non_integral_constant_expression_p
8819     = saved_non_integral_constant_expression_p;
8820
8821   if (expr == error_mark_node)
8822     {
8823       /* Skip everything up to the closing `)'.  */
8824       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8825                                              /*consume_paren=*/true);
8826       return error_mark_node;
8827     }
8828   
8829   /* Parse to the closing `)'.  */
8830   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8831     {
8832       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8833                                              /*consume_paren=*/true);
8834       return error_mark_node;
8835     }
8836
8837   return finish_decltype_type (expr, id_expression_or_member_access_p);
8838 }
8839
8840 /* Special member functions [gram.special] */
8841
8842 /* Parse a conversion-function-id.
8843
8844    conversion-function-id:
8845      operator conversion-type-id
8846
8847    Returns an IDENTIFIER_NODE representing the operator.  */
8848
8849 static tree
8850 cp_parser_conversion_function_id (cp_parser* parser)
8851 {
8852   tree type;
8853   tree saved_scope;
8854   tree saved_qualifying_scope;
8855   tree saved_object_scope;
8856   tree pushed_scope = NULL_TREE;
8857
8858   /* Look for the `operator' token.  */
8859   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8860     return error_mark_node;
8861   /* When we parse the conversion-type-id, the current scope will be
8862      reset.  However, we need that information in able to look up the
8863      conversion function later, so we save it here.  */
8864   saved_scope = parser->scope;
8865   saved_qualifying_scope = parser->qualifying_scope;
8866   saved_object_scope = parser->object_scope;
8867   /* We must enter the scope of the class so that the names of
8868      entities declared within the class are available in the
8869      conversion-type-id.  For example, consider:
8870
8871        struct S {
8872          typedef int I;
8873          operator I();
8874        };
8875
8876        S::operator I() { ... }
8877
8878      In order to see that `I' is a type-name in the definition, we
8879      must be in the scope of `S'.  */
8880   if (saved_scope)
8881     pushed_scope = push_scope (saved_scope);
8882   /* Parse the conversion-type-id.  */
8883   type = cp_parser_conversion_type_id (parser);
8884   /* Leave the scope of the class, if any.  */
8885   if (pushed_scope)
8886     pop_scope (pushed_scope);
8887   /* Restore the saved scope.  */
8888   parser->scope = saved_scope;
8889   parser->qualifying_scope = saved_qualifying_scope;
8890   parser->object_scope = saved_object_scope;
8891   /* If the TYPE is invalid, indicate failure.  */
8892   if (type == error_mark_node)
8893     return error_mark_node;
8894   return mangle_conv_op_name_for_type (type);
8895 }
8896
8897 /* Parse a conversion-type-id:
8898
8899    conversion-type-id:
8900      type-specifier-seq conversion-declarator [opt]
8901
8902    Returns the TYPE specified.  */
8903
8904 static tree
8905 cp_parser_conversion_type_id (cp_parser* parser)
8906 {
8907   tree attributes;
8908   cp_decl_specifier_seq type_specifiers;
8909   cp_declarator *declarator;
8910   tree type_specified;
8911
8912   /* Parse the attributes.  */
8913   attributes = cp_parser_attributes_opt (parser);
8914   /* Parse the type-specifiers.  */
8915   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8916                                 &type_specifiers);
8917   /* If that didn't work, stop.  */
8918   if (type_specifiers.type == error_mark_node)
8919     return error_mark_node;
8920   /* Parse the conversion-declarator.  */
8921   declarator = cp_parser_conversion_declarator_opt (parser);
8922
8923   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8924                                     /*initialized=*/0, &attributes);
8925   if (attributes)
8926     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8927   return type_specified;
8928 }
8929
8930 /* Parse an (optional) conversion-declarator.
8931
8932    conversion-declarator:
8933      ptr-operator conversion-declarator [opt]
8934
8935    */
8936
8937 static cp_declarator *
8938 cp_parser_conversion_declarator_opt (cp_parser* parser)
8939 {
8940   enum tree_code code;
8941   tree class_type;
8942   cp_cv_quals cv_quals;
8943
8944   /* We don't know if there's a ptr-operator next, or not.  */
8945   cp_parser_parse_tentatively (parser);
8946   /* Try the ptr-operator.  */
8947   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8948   /* If it worked, look for more conversion-declarators.  */
8949   if (cp_parser_parse_definitely (parser))
8950     {
8951       cp_declarator *declarator;
8952
8953       /* Parse another optional declarator.  */
8954       declarator = cp_parser_conversion_declarator_opt (parser);
8955
8956       return cp_parser_make_indirect_declarator
8957         (code, class_type, cv_quals, declarator);
8958    }
8959
8960   return NULL;
8961 }
8962
8963 /* Parse an (optional) ctor-initializer.
8964
8965    ctor-initializer:
8966      : mem-initializer-list
8967
8968    Returns TRUE iff the ctor-initializer was actually present.  */
8969
8970 static bool
8971 cp_parser_ctor_initializer_opt (cp_parser* parser)
8972 {
8973   /* If the next token is not a `:', then there is no
8974      ctor-initializer.  */
8975   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8976     {
8977       /* Do default initialization of any bases and members.  */
8978       if (DECL_CONSTRUCTOR_P (current_function_decl))
8979         finish_mem_initializers (NULL_TREE);
8980
8981       return false;
8982     }
8983
8984   /* Consume the `:' token.  */
8985   cp_lexer_consume_token (parser->lexer);
8986   /* And the mem-initializer-list.  */
8987   cp_parser_mem_initializer_list (parser);
8988
8989   return true;
8990 }
8991
8992 /* Parse a mem-initializer-list.
8993
8994    mem-initializer-list:
8995      mem-initializer ... [opt]
8996      mem-initializer ... [opt] , mem-initializer-list  */
8997
8998 static void
8999 cp_parser_mem_initializer_list (cp_parser* parser)
9000 {
9001   tree mem_initializer_list = NULL_TREE;
9002   cp_token *token = cp_lexer_peek_token (parser->lexer);
9003
9004   /* Let the semantic analysis code know that we are starting the
9005      mem-initializer-list.  */
9006   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9007     error ("%Honly constructors take base initializers",
9008            &token->location);
9009
9010   /* Loop through the list.  */
9011   while (true)
9012     {
9013       tree mem_initializer;
9014
9015       token = cp_lexer_peek_token (parser->lexer);
9016       /* Parse the mem-initializer.  */
9017       mem_initializer = cp_parser_mem_initializer (parser);
9018       /* If the next token is a `...', we're expanding member initializers. */
9019       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9020         {
9021           /* Consume the `...'. */
9022           cp_lexer_consume_token (parser->lexer);
9023
9024           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9025              can be expanded but members cannot. */
9026           if (mem_initializer != error_mark_node
9027               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9028             {
9029               error ("%Hcannot expand initializer for member %<%D%>",
9030                      &token->location, TREE_PURPOSE (mem_initializer));
9031               mem_initializer = error_mark_node;
9032             }
9033
9034           /* Construct the pack expansion type. */
9035           if (mem_initializer != error_mark_node)
9036             mem_initializer = make_pack_expansion (mem_initializer);
9037         }
9038       /* Add it to the list, unless it was erroneous.  */
9039       if (mem_initializer != error_mark_node)
9040         {
9041           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9042           mem_initializer_list = mem_initializer;
9043         }
9044       /* If the next token is not a `,', we're done.  */
9045       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9046         break;
9047       /* Consume the `,' token.  */
9048       cp_lexer_consume_token (parser->lexer);
9049     }
9050
9051   /* Perform semantic analysis.  */
9052   if (DECL_CONSTRUCTOR_P (current_function_decl))
9053     finish_mem_initializers (mem_initializer_list);
9054 }
9055
9056 /* Parse a mem-initializer.
9057
9058    mem-initializer:
9059      mem-initializer-id ( expression-list [opt] )
9060      mem-initializer-id braced-init-list
9061
9062    GNU extension:
9063
9064    mem-initializer:
9065      ( expression-list [opt] )
9066
9067    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9068    class) or FIELD_DECL (for a non-static data member) to initialize;
9069    the TREE_VALUE is the expression-list.  An empty initialization
9070    list is represented by void_list_node.  */
9071
9072 static tree
9073 cp_parser_mem_initializer (cp_parser* parser)
9074 {
9075   tree mem_initializer_id;
9076   tree expression_list;
9077   tree member;
9078   cp_token *token = cp_lexer_peek_token (parser->lexer);
9079
9080   /* Find out what is being initialized.  */
9081   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9082     {
9083       permerror (token->location,
9084                  "anachronistic old-style base class initializer");
9085       mem_initializer_id = NULL_TREE;
9086     }
9087   else
9088     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9089   member = expand_member_init (mem_initializer_id);
9090   if (member && !DECL_P (member))
9091     in_base_initializer = 1;
9092
9093   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9094     {
9095       bool expr_non_constant_p;
9096       maybe_warn_cpp0x ("extended initializer lists");
9097       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9098       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9099       expression_list = build_tree_list (NULL_TREE, expression_list);
9100     }
9101   else
9102     expression_list
9103       = cp_parser_parenthesized_expression_list (parser, false,
9104                                                  /*cast_p=*/false,
9105                                                  /*allow_expansion_p=*/true,
9106                                                  /*non_constant_p=*/NULL);
9107   if (expression_list == error_mark_node)
9108     return error_mark_node;
9109   if (!expression_list)
9110     expression_list = void_type_node;
9111
9112   in_base_initializer = 0;
9113
9114   return member ? build_tree_list (member, expression_list) : error_mark_node;
9115 }
9116
9117 /* Parse a mem-initializer-id.
9118
9119    mem-initializer-id:
9120      :: [opt] nested-name-specifier [opt] class-name
9121      identifier
9122
9123    Returns a TYPE indicating the class to be initializer for the first
9124    production.  Returns an IDENTIFIER_NODE indicating the data member
9125    to be initialized for the second production.  */
9126
9127 static tree
9128 cp_parser_mem_initializer_id (cp_parser* parser)
9129 {
9130   bool global_scope_p;
9131   bool nested_name_specifier_p;
9132   bool template_p = false;
9133   tree id;
9134
9135   cp_token *token = cp_lexer_peek_token (parser->lexer);
9136
9137   /* `typename' is not allowed in this context ([temp.res]).  */
9138   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9139     {
9140       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9141              "member initializer is implicitly a type)",
9142              &token->location);
9143       cp_lexer_consume_token (parser->lexer);
9144     }
9145   /* Look for the optional `::' operator.  */
9146   global_scope_p
9147     = (cp_parser_global_scope_opt (parser,
9148                                    /*current_scope_valid_p=*/false)
9149        != NULL_TREE);
9150   /* Look for the optional nested-name-specifier.  The simplest way to
9151      implement:
9152
9153        [temp.res]
9154
9155        The keyword `typename' is not permitted in a base-specifier or
9156        mem-initializer; in these contexts a qualified name that
9157        depends on a template-parameter is implicitly assumed to be a
9158        type name.
9159
9160      is to assume that we have seen the `typename' keyword at this
9161      point.  */
9162   nested_name_specifier_p
9163     = (cp_parser_nested_name_specifier_opt (parser,
9164                                             /*typename_keyword_p=*/true,
9165                                             /*check_dependency_p=*/true,
9166                                             /*type_p=*/true,
9167                                             /*is_declaration=*/true)
9168        != NULL_TREE);
9169   if (nested_name_specifier_p)
9170     template_p = cp_parser_optional_template_keyword (parser);
9171   /* If there is a `::' operator or a nested-name-specifier, then we
9172      are definitely looking for a class-name.  */
9173   if (global_scope_p || nested_name_specifier_p)
9174     return cp_parser_class_name (parser,
9175                                  /*typename_keyword_p=*/true,
9176                                  /*template_keyword_p=*/template_p,
9177                                  none_type,
9178                                  /*check_dependency_p=*/true,
9179                                  /*class_head_p=*/false,
9180                                  /*is_declaration=*/true);
9181   /* Otherwise, we could also be looking for an ordinary identifier.  */
9182   cp_parser_parse_tentatively (parser);
9183   /* Try a class-name.  */
9184   id = cp_parser_class_name (parser,
9185                              /*typename_keyword_p=*/true,
9186                              /*template_keyword_p=*/false,
9187                              none_type,
9188                              /*check_dependency_p=*/true,
9189                              /*class_head_p=*/false,
9190                              /*is_declaration=*/true);
9191   /* If we found one, we're done.  */
9192   if (cp_parser_parse_definitely (parser))
9193     return id;
9194   /* Otherwise, look for an ordinary identifier.  */
9195   return cp_parser_identifier (parser);
9196 }
9197
9198 /* Overloading [gram.over] */
9199
9200 /* Parse an operator-function-id.
9201
9202    operator-function-id:
9203      operator operator
9204
9205    Returns an IDENTIFIER_NODE for the operator which is a
9206    human-readable spelling of the identifier, e.g., `operator +'.  */
9207
9208 static tree
9209 cp_parser_operator_function_id (cp_parser* parser)
9210 {
9211   /* Look for the `operator' keyword.  */
9212   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9213     return error_mark_node;
9214   /* And then the name of the operator itself.  */
9215   return cp_parser_operator (parser);
9216 }
9217
9218 /* Parse an operator.
9219
9220    operator:
9221      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9222      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9223      || ++ -- , ->* -> () []
9224
9225    GNU Extensions:
9226
9227    operator:
9228      <? >? <?= >?=
9229
9230    Returns an IDENTIFIER_NODE for the operator which is a
9231    human-readable spelling of the identifier, e.g., `operator +'.  */
9232
9233 static tree
9234 cp_parser_operator (cp_parser* parser)
9235 {
9236   tree id = NULL_TREE;
9237   cp_token *token;
9238
9239   /* Peek at the next token.  */
9240   token = cp_lexer_peek_token (parser->lexer);
9241   /* Figure out which operator we have.  */
9242   switch (token->type)
9243     {
9244     case CPP_KEYWORD:
9245       {
9246         enum tree_code op;
9247
9248         /* The keyword should be either `new' or `delete'.  */
9249         if (token->keyword == RID_NEW)
9250           op = NEW_EXPR;
9251         else if (token->keyword == RID_DELETE)
9252           op = DELETE_EXPR;
9253         else
9254           break;
9255
9256         /* Consume the `new' or `delete' token.  */
9257         cp_lexer_consume_token (parser->lexer);
9258
9259         /* Peek at the next token.  */
9260         token = cp_lexer_peek_token (parser->lexer);
9261         /* If it's a `[' token then this is the array variant of the
9262            operator.  */
9263         if (token->type == CPP_OPEN_SQUARE)
9264           {
9265             /* Consume the `[' token.  */
9266             cp_lexer_consume_token (parser->lexer);
9267             /* Look for the `]' token.  */
9268             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9269             id = ansi_opname (op == NEW_EXPR
9270                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9271           }
9272         /* Otherwise, we have the non-array variant.  */
9273         else
9274           id = ansi_opname (op);
9275
9276         return id;
9277       }
9278
9279     case CPP_PLUS:
9280       id = ansi_opname (PLUS_EXPR);
9281       break;
9282
9283     case CPP_MINUS:
9284       id = ansi_opname (MINUS_EXPR);
9285       break;
9286
9287     case CPP_MULT:
9288       id = ansi_opname (MULT_EXPR);
9289       break;
9290
9291     case CPP_DIV:
9292       id = ansi_opname (TRUNC_DIV_EXPR);
9293       break;
9294
9295     case CPP_MOD:
9296       id = ansi_opname (TRUNC_MOD_EXPR);
9297       break;
9298
9299     case CPP_XOR:
9300       id = ansi_opname (BIT_XOR_EXPR);
9301       break;
9302
9303     case CPP_AND:
9304       id = ansi_opname (BIT_AND_EXPR);
9305       break;
9306
9307     case CPP_OR:
9308       id = ansi_opname (BIT_IOR_EXPR);
9309       break;
9310
9311     case CPP_COMPL:
9312       id = ansi_opname (BIT_NOT_EXPR);
9313       break;
9314
9315     case CPP_NOT:
9316       id = ansi_opname (TRUTH_NOT_EXPR);
9317       break;
9318
9319     case CPP_EQ:
9320       id = ansi_assopname (NOP_EXPR);
9321       break;
9322
9323     case CPP_LESS:
9324       id = ansi_opname (LT_EXPR);
9325       break;
9326
9327     case CPP_GREATER:
9328       id = ansi_opname (GT_EXPR);
9329       break;
9330
9331     case CPP_PLUS_EQ:
9332       id = ansi_assopname (PLUS_EXPR);
9333       break;
9334
9335     case CPP_MINUS_EQ:
9336       id = ansi_assopname (MINUS_EXPR);
9337       break;
9338
9339     case CPP_MULT_EQ:
9340       id = ansi_assopname (MULT_EXPR);
9341       break;
9342
9343     case CPP_DIV_EQ:
9344       id = ansi_assopname (TRUNC_DIV_EXPR);
9345       break;
9346
9347     case CPP_MOD_EQ:
9348       id = ansi_assopname (TRUNC_MOD_EXPR);
9349       break;
9350
9351     case CPP_XOR_EQ:
9352       id = ansi_assopname (BIT_XOR_EXPR);
9353       break;
9354
9355     case CPP_AND_EQ:
9356       id = ansi_assopname (BIT_AND_EXPR);
9357       break;
9358
9359     case CPP_OR_EQ:
9360       id = ansi_assopname (BIT_IOR_EXPR);
9361       break;
9362
9363     case CPP_LSHIFT:
9364       id = ansi_opname (LSHIFT_EXPR);
9365       break;
9366
9367     case CPP_RSHIFT:
9368       id = ansi_opname (RSHIFT_EXPR);
9369       break;
9370
9371     case CPP_LSHIFT_EQ:
9372       id = ansi_assopname (LSHIFT_EXPR);
9373       break;
9374
9375     case CPP_RSHIFT_EQ:
9376       id = ansi_assopname (RSHIFT_EXPR);
9377       break;
9378
9379     case CPP_EQ_EQ:
9380       id = ansi_opname (EQ_EXPR);
9381       break;
9382
9383     case CPP_NOT_EQ:
9384       id = ansi_opname (NE_EXPR);
9385       break;
9386
9387     case CPP_LESS_EQ:
9388       id = ansi_opname (LE_EXPR);
9389       break;
9390
9391     case CPP_GREATER_EQ:
9392       id = ansi_opname (GE_EXPR);
9393       break;
9394
9395     case CPP_AND_AND:
9396       id = ansi_opname (TRUTH_ANDIF_EXPR);
9397       break;
9398
9399     case CPP_OR_OR:
9400       id = ansi_opname (TRUTH_ORIF_EXPR);
9401       break;
9402
9403     case CPP_PLUS_PLUS:
9404       id = ansi_opname (POSTINCREMENT_EXPR);
9405       break;
9406
9407     case CPP_MINUS_MINUS:
9408       id = ansi_opname (PREDECREMENT_EXPR);
9409       break;
9410
9411     case CPP_COMMA:
9412       id = ansi_opname (COMPOUND_EXPR);
9413       break;
9414
9415     case CPP_DEREF_STAR:
9416       id = ansi_opname (MEMBER_REF);
9417       break;
9418
9419     case CPP_DEREF:
9420       id = ansi_opname (COMPONENT_REF);
9421       break;
9422
9423     case CPP_OPEN_PAREN:
9424       /* Consume the `('.  */
9425       cp_lexer_consume_token (parser->lexer);
9426       /* Look for the matching `)'.  */
9427       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9428       return ansi_opname (CALL_EXPR);
9429
9430     case CPP_OPEN_SQUARE:
9431       /* Consume the `['.  */
9432       cp_lexer_consume_token (parser->lexer);
9433       /* Look for the matching `]'.  */
9434       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9435       return ansi_opname (ARRAY_REF);
9436
9437     default:
9438       /* Anything else is an error.  */
9439       break;
9440     }
9441
9442   /* If we have selected an identifier, we need to consume the
9443      operator token.  */
9444   if (id)
9445     cp_lexer_consume_token (parser->lexer);
9446   /* Otherwise, no valid operator name was present.  */
9447   else
9448     {
9449       cp_parser_error (parser, "expected operator");
9450       id = error_mark_node;
9451     }
9452
9453   return id;
9454 }
9455
9456 /* Parse a template-declaration.
9457
9458    template-declaration:
9459      export [opt] template < template-parameter-list > declaration
9460
9461    If MEMBER_P is TRUE, this template-declaration occurs within a
9462    class-specifier.
9463
9464    The grammar rule given by the standard isn't correct.  What
9465    is really meant is:
9466
9467    template-declaration:
9468      export [opt] template-parameter-list-seq
9469        decl-specifier-seq [opt] init-declarator [opt] ;
9470      export [opt] template-parameter-list-seq
9471        function-definition
9472
9473    template-parameter-list-seq:
9474      template-parameter-list-seq [opt]
9475      template < template-parameter-list >  */
9476
9477 static void
9478 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9479 {
9480   /* Check for `export'.  */
9481   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9482     {
9483       /* Consume the `export' token.  */
9484       cp_lexer_consume_token (parser->lexer);
9485       /* Warn that we do not support `export'.  */
9486       warning (0, "keyword %<export%> not implemented, and will be ignored");
9487     }
9488
9489   cp_parser_template_declaration_after_export (parser, member_p);
9490 }
9491
9492 /* Parse a template-parameter-list.
9493
9494    template-parameter-list:
9495      template-parameter
9496      template-parameter-list , template-parameter
9497
9498    Returns a TREE_LIST.  Each node represents a template parameter.
9499    The nodes are connected via their TREE_CHAINs.  */
9500
9501 static tree
9502 cp_parser_template_parameter_list (cp_parser* parser)
9503 {
9504   tree parameter_list = NULL_TREE;
9505
9506   begin_template_parm_list ();
9507   while (true)
9508     {
9509       tree parameter;
9510       bool is_non_type;
9511       bool is_parameter_pack;
9512
9513       /* Parse the template-parameter.  */
9514       parameter = cp_parser_template_parameter (parser, 
9515                                                 &is_non_type,
9516                                                 &is_parameter_pack);
9517       /* Add it to the list.  */
9518       if (parameter != error_mark_node)
9519         parameter_list = process_template_parm (parameter_list,
9520                                                 parameter,
9521                                                 is_non_type,
9522                                                 is_parameter_pack);
9523       else
9524        {
9525          tree err_parm = build_tree_list (parameter, parameter);
9526          TREE_VALUE (err_parm) = error_mark_node;
9527          parameter_list = chainon (parameter_list, err_parm);
9528        }
9529
9530       /* If the next token is not a `,', we're done.  */
9531       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9532         break;
9533       /* Otherwise, consume the `,' token.  */
9534       cp_lexer_consume_token (parser->lexer);
9535     }
9536
9537   return end_template_parm_list (parameter_list);
9538 }
9539
9540 /* Parse a template-parameter.
9541
9542    template-parameter:
9543      type-parameter
9544      parameter-declaration
9545
9546    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9547    the parameter.  The TREE_PURPOSE is the default value, if any.
9548    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9549    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9550    set to true iff this parameter is a parameter pack. */
9551
9552 static tree
9553 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9554                               bool *is_parameter_pack)
9555 {
9556   cp_token *token;
9557   cp_parameter_declarator *parameter_declarator;
9558   cp_declarator *id_declarator;
9559   tree parm;
9560
9561   /* Assume it is a type parameter or a template parameter.  */
9562   *is_non_type = false;
9563   /* Assume it not a parameter pack. */
9564   *is_parameter_pack = false;
9565   /* Peek at the next token.  */
9566   token = cp_lexer_peek_token (parser->lexer);
9567   /* If it is `class' or `template', we have a type-parameter.  */
9568   if (token->keyword == RID_TEMPLATE)
9569     return cp_parser_type_parameter (parser, is_parameter_pack);
9570   /* If it is `class' or `typename' we do not know yet whether it is a
9571      type parameter or a non-type parameter.  Consider:
9572
9573        template <typename T, typename T::X X> ...
9574
9575      or:
9576
9577        template <class C, class D*> ...
9578
9579      Here, the first parameter is a type parameter, and the second is
9580      a non-type parameter.  We can tell by looking at the token after
9581      the identifier -- if it is a `,', `=', or `>' then we have a type
9582      parameter.  */
9583   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9584     {
9585       /* Peek at the token after `class' or `typename'.  */
9586       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9587       /* If it's an ellipsis, we have a template type parameter
9588          pack. */
9589       if (token->type == CPP_ELLIPSIS)
9590         return cp_parser_type_parameter (parser, is_parameter_pack);
9591       /* If it's an identifier, skip it.  */
9592       if (token->type == CPP_NAME)
9593         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9594       /* Now, see if the token looks like the end of a template
9595          parameter.  */
9596       if (token->type == CPP_COMMA
9597           || token->type == CPP_EQ
9598           || token->type == CPP_GREATER)
9599         return cp_parser_type_parameter (parser, is_parameter_pack);
9600     }
9601
9602   /* Otherwise, it is a non-type parameter.
9603
9604      [temp.param]
9605
9606      When parsing a default template-argument for a non-type
9607      template-parameter, the first non-nested `>' is taken as the end
9608      of the template parameter-list rather than a greater-than
9609      operator.  */
9610   *is_non_type = true;
9611   parameter_declarator
9612      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9613                                         /*parenthesized_p=*/NULL);
9614
9615   /* If the parameter declaration is marked as a parameter pack, set
9616      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9617      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9618      grokdeclarator. */
9619   if (parameter_declarator
9620       && parameter_declarator->declarator
9621       && parameter_declarator->declarator->parameter_pack_p)
9622     {
9623       *is_parameter_pack = true;
9624       parameter_declarator->declarator->parameter_pack_p = false;
9625     }
9626
9627   /* If the next token is an ellipsis, and we don't already have it
9628      marked as a parameter pack, then we have a parameter pack (that
9629      has no declarator).  */
9630   if (!*is_parameter_pack
9631       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9632       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9633     {
9634       /* Consume the `...'.  */
9635       cp_lexer_consume_token (parser->lexer);
9636       maybe_warn_variadic_templates ();
9637       
9638       *is_parameter_pack = true;
9639     }
9640   /* We might end up with a pack expansion as the type of the non-type
9641      template parameter, in which case this is a non-type template
9642      parameter pack.  */
9643   else if (parameter_declarator
9644            && parameter_declarator->decl_specifiers.type
9645            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9646     {
9647       *is_parameter_pack = true;
9648       parameter_declarator->decl_specifiers.type = 
9649         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9650     }
9651
9652   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9653     {
9654       /* Parameter packs cannot have default arguments.  However, a
9655          user may try to do so, so we'll parse them and give an
9656          appropriate diagnostic here.  */
9657
9658       /* Consume the `='.  */
9659       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9660       cp_lexer_consume_token (parser->lexer);
9661       
9662       /* Find the name of the parameter pack.  */     
9663       id_declarator = parameter_declarator->declarator;
9664       while (id_declarator && id_declarator->kind != cdk_id)
9665         id_declarator = id_declarator->declarator;
9666       
9667       if (id_declarator && id_declarator->kind == cdk_id)
9668         error ("%Htemplate parameter pack %qD cannot have a default argument",
9669                &start_token->location, id_declarator->u.id.unqualified_name);
9670       else
9671         error ("%Htemplate parameter pack cannot have a default argument",
9672                &start_token->location);
9673       
9674       /* Parse the default argument, but throw away the result.  */
9675       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9676     }
9677
9678   parm = grokdeclarator (parameter_declarator->declarator,
9679                          &parameter_declarator->decl_specifiers,
9680                          PARM, /*initialized=*/0,
9681                          /*attrlist=*/NULL);
9682   if (parm == error_mark_node)
9683     return error_mark_node;
9684
9685   return build_tree_list (parameter_declarator->default_argument, parm);
9686 }
9687
9688 /* Parse a type-parameter.
9689
9690    type-parameter:
9691      class identifier [opt]
9692      class identifier [opt] = type-id
9693      typename identifier [opt]
9694      typename identifier [opt] = type-id
9695      template < template-parameter-list > class identifier [opt]
9696      template < template-parameter-list > class identifier [opt]
9697        = id-expression
9698
9699    GNU Extension (variadic templates):
9700
9701    type-parameter:
9702      class ... identifier [opt]
9703      typename ... identifier [opt]
9704
9705    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9706    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9707    the declaration of the parameter.
9708
9709    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9710
9711 static tree
9712 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9713 {
9714   cp_token *token;
9715   tree parameter;
9716
9717   /* Look for a keyword to tell us what kind of parameter this is.  */
9718   token = cp_parser_require (parser, CPP_KEYWORD,
9719                              "%<class%>, %<typename%>, or %<template%>");
9720   if (!token)
9721     return error_mark_node;
9722
9723   switch (token->keyword)
9724     {
9725     case RID_CLASS:
9726     case RID_TYPENAME:
9727       {
9728         tree identifier;
9729         tree default_argument;
9730
9731         /* If the next token is an ellipsis, we have a template
9732            argument pack. */
9733         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9734           {
9735             /* Consume the `...' token. */
9736             cp_lexer_consume_token (parser->lexer);
9737             maybe_warn_variadic_templates ();
9738
9739             *is_parameter_pack = true;
9740           }
9741
9742         /* If the next token is an identifier, then it names the
9743            parameter.  */
9744         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9745           identifier = cp_parser_identifier (parser);
9746         else
9747           identifier = NULL_TREE;
9748
9749         /* Create the parameter.  */
9750         parameter = finish_template_type_parm (class_type_node, identifier);
9751
9752         /* If the next token is an `=', we have a default argument.  */
9753         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9754           {
9755             /* Consume the `=' token.  */
9756             cp_lexer_consume_token (parser->lexer);
9757             /* Parse the default-argument.  */
9758             push_deferring_access_checks (dk_no_deferred);
9759             default_argument = cp_parser_type_id (parser);
9760
9761             /* Template parameter packs cannot have default
9762                arguments. */
9763             if (*is_parameter_pack)
9764               {
9765                 if (identifier)
9766                   error ("%Htemplate parameter pack %qD cannot have a "
9767                          "default argument", &token->location, identifier);
9768                 else
9769                   error ("%Htemplate parameter packs cannot have "
9770                          "default arguments", &token->location);
9771                 default_argument = NULL_TREE;
9772               }
9773             pop_deferring_access_checks ();
9774           }
9775         else
9776           default_argument = NULL_TREE;
9777
9778         /* Create the combined representation of the parameter and the
9779            default argument.  */
9780         parameter = build_tree_list (default_argument, parameter);
9781       }
9782       break;
9783
9784     case RID_TEMPLATE:
9785       {
9786         tree parameter_list;
9787         tree identifier;
9788         tree default_argument;
9789
9790         /* Look for the `<'.  */
9791         cp_parser_require (parser, CPP_LESS, "%<<%>");
9792         /* Parse the template-parameter-list.  */
9793         parameter_list = cp_parser_template_parameter_list (parser);
9794         /* Look for the `>'.  */
9795         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9796         /* Look for the `class' keyword.  */
9797         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9798         /* If the next token is an ellipsis, we have a template
9799            argument pack. */
9800         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9801           {
9802             /* Consume the `...' token. */
9803             cp_lexer_consume_token (parser->lexer);
9804             maybe_warn_variadic_templates ();
9805
9806             *is_parameter_pack = true;
9807           }
9808         /* If the next token is an `=', then there is a
9809            default-argument.  If the next token is a `>', we are at
9810            the end of the parameter-list.  If the next token is a `,',
9811            then we are at the end of this parameter.  */
9812         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9813             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9814             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9815           {
9816             identifier = cp_parser_identifier (parser);
9817             /* Treat invalid names as if the parameter were nameless.  */
9818             if (identifier == error_mark_node)
9819               identifier = NULL_TREE;
9820           }
9821         else
9822           identifier = NULL_TREE;
9823
9824         /* Create the template parameter.  */
9825         parameter = finish_template_template_parm (class_type_node,
9826                                                    identifier);
9827
9828         /* If the next token is an `=', then there is a
9829            default-argument.  */
9830         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9831           {
9832             bool is_template;
9833
9834             /* Consume the `='.  */
9835             cp_lexer_consume_token (parser->lexer);
9836             /* Parse the id-expression.  */
9837             push_deferring_access_checks (dk_no_deferred);
9838             /* save token before parsing the id-expression, for error
9839                reporting */
9840             token = cp_lexer_peek_token (parser->lexer);
9841             default_argument
9842               = cp_parser_id_expression (parser,
9843                                          /*template_keyword_p=*/false,
9844                                          /*check_dependency_p=*/true,
9845                                          /*template_p=*/&is_template,
9846                                          /*declarator_p=*/false,
9847                                          /*optional_p=*/false);
9848             if (TREE_CODE (default_argument) == TYPE_DECL)
9849               /* If the id-expression was a template-id that refers to
9850                  a template-class, we already have the declaration here,
9851                  so no further lookup is needed.  */
9852                  ;
9853             else
9854               /* Look up the name.  */
9855               default_argument
9856                 = cp_parser_lookup_name (parser, default_argument,
9857                                          none_type,
9858                                          /*is_template=*/is_template,
9859                                          /*is_namespace=*/false,
9860                                          /*check_dependency=*/true,
9861                                          /*ambiguous_decls=*/NULL,
9862                                          token->location);
9863             /* See if the default argument is valid.  */
9864             default_argument
9865               = check_template_template_default_arg (default_argument);
9866
9867             /* Template parameter packs cannot have default
9868                arguments. */
9869             if (*is_parameter_pack)
9870               {
9871                 if (identifier)
9872                   error ("%Htemplate parameter pack %qD cannot "
9873                          "have a default argument",
9874                          &token->location, identifier);
9875                 else
9876                   error ("%Htemplate parameter packs cannot "
9877                          "have default arguments",
9878                          &token->location);
9879                 default_argument = NULL_TREE;
9880               }
9881             pop_deferring_access_checks ();
9882           }
9883         else
9884           default_argument = NULL_TREE;
9885
9886         /* Create the combined representation of the parameter and the
9887            default argument.  */
9888         parameter = build_tree_list (default_argument, parameter);
9889       }
9890       break;
9891
9892     default:
9893       gcc_unreachable ();
9894       break;
9895     }
9896
9897   return parameter;
9898 }
9899
9900 /* Parse a template-id.
9901
9902    template-id:
9903      template-name < template-argument-list [opt] >
9904
9905    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9906    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9907    returned.  Otherwise, if the template-name names a function, or set
9908    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9909    names a class, returns a TYPE_DECL for the specialization.
9910
9911    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9912    uninstantiated templates.  */
9913
9914 static tree
9915 cp_parser_template_id (cp_parser *parser,
9916                        bool template_keyword_p,
9917                        bool check_dependency_p,
9918                        bool is_declaration)
9919 {
9920   int i;
9921   tree templ;
9922   tree arguments;
9923   tree template_id;
9924   cp_token_position start_of_id = 0;
9925   deferred_access_check *chk;
9926   VEC (deferred_access_check,gc) *access_check;
9927   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9928   bool is_identifier;
9929
9930   /* If the next token corresponds to a template-id, there is no need
9931      to reparse it.  */
9932   next_token = cp_lexer_peek_token (parser->lexer);
9933   if (next_token->type == CPP_TEMPLATE_ID)
9934     {
9935       struct tree_check *check_value;
9936
9937       /* Get the stored value.  */
9938       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9939       /* Perform any access checks that were deferred.  */
9940       access_check = check_value->checks;
9941       if (access_check)
9942         {
9943           for (i = 0 ;
9944                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9945                ++i)
9946             {
9947               perform_or_defer_access_check (chk->binfo,
9948                                              chk->decl,
9949                                              chk->diag_decl);
9950             }
9951         }
9952       /* Return the stored value.  */
9953       return check_value->value;
9954     }
9955
9956   /* Avoid performing name lookup if there is no possibility of
9957      finding a template-id.  */
9958   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9959       || (next_token->type == CPP_NAME
9960           && !cp_parser_nth_token_starts_template_argument_list_p
9961                (parser, 2)))
9962     {
9963       cp_parser_error (parser, "expected template-id");
9964       return error_mark_node;
9965     }
9966
9967   /* Remember where the template-id starts.  */
9968   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9969     start_of_id = cp_lexer_token_position (parser->lexer, false);
9970
9971   push_deferring_access_checks (dk_deferred);
9972
9973   /* Parse the template-name.  */
9974   is_identifier = false;
9975   token = cp_lexer_peek_token (parser->lexer);
9976   templ = cp_parser_template_name (parser, template_keyword_p,
9977                                    check_dependency_p,
9978                                    is_declaration,
9979                                    &is_identifier);
9980   if (templ == error_mark_node || is_identifier)
9981     {
9982       pop_deferring_access_checks ();
9983       return templ;
9984     }
9985
9986   /* If we find the sequence `[:' after a template-name, it's probably
9987      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9988      parse correctly the argument list.  */
9989   next_token = cp_lexer_peek_token (parser->lexer);
9990   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9991   if (next_token->type == CPP_OPEN_SQUARE
9992       && next_token->flags & DIGRAPH
9993       && next_token_2->type == CPP_COLON
9994       && !(next_token_2->flags & PREV_WHITE))
9995     {
9996       cp_parser_parse_tentatively (parser);
9997       /* Change `:' into `::'.  */
9998       next_token_2->type = CPP_SCOPE;
9999       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10000          CPP_LESS.  */
10001       cp_lexer_consume_token (parser->lexer);
10002
10003       /* Parse the arguments.  */
10004       arguments = cp_parser_enclosed_template_argument_list (parser);
10005       if (!cp_parser_parse_definitely (parser))
10006         {
10007           /* If we couldn't parse an argument list, then we revert our changes
10008              and return simply an error. Maybe this is not a template-id
10009              after all.  */
10010           next_token_2->type = CPP_COLON;
10011           cp_parser_error (parser, "expected %<<%>");
10012           pop_deferring_access_checks ();
10013           return error_mark_node;
10014         }
10015       /* Otherwise, emit an error about the invalid digraph, but continue
10016          parsing because we got our argument list.  */
10017       if (permerror (next_token->location,
10018                      "%<<::%> cannot begin a template-argument list"))
10019         {
10020           static bool hint = false;
10021           inform (next_token->location,
10022                   "%<<:%> is an alternate spelling for %<[%>."
10023                   " Insert whitespace between %<<%> and %<::%>");
10024           if (!hint && !flag_permissive)
10025             {
10026               inform (next_token->location, "(if you use %<-fpermissive%>"
10027                       " G++ will accept your code)");
10028               hint = true;
10029             }
10030         }
10031     }
10032   else
10033     {
10034       /* Look for the `<' that starts the template-argument-list.  */
10035       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10036         {
10037           pop_deferring_access_checks ();
10038           return error_mark_node;
10039         }
10040       /* Parse the arguments.  */
10041       arguments = cp_parser_enclosed_template_argument_list (parser);
10042     }
10043
10044   /* Build a representation of the specialization.  */
10045   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10046     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10047   else if (DECL_CLASS_TEMPLATE_P (templ)
10048            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10049     {
10050       bool entering_scope;
10051       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10052          template (rather than some instantiation thereof) only if
10053          is not nested within some other construct.  For example, in
10054          "template <typename T> void f(T) { A<T>::", A<T> is just an
10055          instantiation of A.  */
10056       entering_scope = (template_parm_scope_p ()
10057                         && cp_lexer_next_token_is (parser->lexer,
10058                                                    CPP_SCOPE));
10059       template_id
10060         = finish_template_type (templ, arguments, entering_scope);
10061     }
10062   else
10063     {
10064       /* If it's not a class-template or a template-template, it should be
10065          a function-template.  */
10066       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10067                    || TREE_CODE (templ) == OVERLOAD
10068                    || BASELINK_P (templ)));
10069
10070       template_id = lookup_template_function (templ, arguments);
10071     }
10072
10073   /* If parsing tentatively, replace the sequence of tokens that makes
10074      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10075      should we re-parse the token stream, we will not have to repeat
10076      the effort required to do the parse, nor will we issue duplicate
10077      error messages about problems during instantiation of the
10078      template.  */
10079   if (start_of_id)
10080     {
10081       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10082
10083       /* Reset the contents of the START_OF_ID token.  */
10084       token->type = CPP_TEMPLATE_ID;
10085       /* Retrieve any deferred checks.  Do not pop this access checks yet
10086          so the memory will not be reclaimed during token replacing below.  */
10087       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10088       token->u.tree_check_value->value = template_id;
10089       token->u.tree_check_value->checks = get_deferred_access_checks ();
10090       token->keyword = RID_MAX;
10091
10092       /* Purge all subsequent tokens.  */
10093       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10094
10095       /* ??? Can we actually assume that, if template_id ==
10096          error_mark_node, we will have issued a diagnostic to the
10097          user, as opposed to simply marking the tentative parse as
10098          failed?  */
10099       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10100         error ("%Hparse error in template argument list",
10101                &token->location);
10102     }
10103
10104   pop_deferring_access_checks ();
10105   return template_id;
10106 }
10107
10108 /* Parse a template-name.
10109
10110    template-name:
10111      identifier
10112
10113    The standard should actually say:
10114
10115    template-name:
10116      identifier
10117      operator-function-id
10118
10119    A defect report has been filed about this issue.
10120
10121    A conversion-function-id cannot be a template name because they cannot
10122    be part of a template-id. In fact, looking at this code:
10123
10124    a.operator K<int>()
10125
10126    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10127    It is impossible to call a templated conversion-function-id with an
10128    explicit argument list, since the only allowed template parameter is
10129    the type to which it is converting.
10130
10131    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10132    `template' keyword, in a construction like:
10133
10134      T::template f<3>()
10135
10136    In that case `f' is taken to be a template-name, even though there
10137    is no way of knowing for sure.
10138
10139    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10140    name refers to a set of overloaded functions, at least one of which
10141    is a template, or an IDENTIFIER_NODE with the name of the template,
10142    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10143    names are looked up inside uninstantiated templates.  */
10144
10145 static tree
10146 cp_parser_template_name (cp_parser* parser,
10147                          bool template_keyword_p,
10148                          bool check_dependency_p,
10149                          bool is_declaration,
10150                          bool *is_identifier)
10151 {
10152   tree identifier;
10153   tree decl;
10154   tree fns;
10155   cp_token *token = cp_lexer_peek_token (parser->lexer);
10156
10157   /* If the next token is `operator', then we have either an
10158      operator-function-id or a conversion-function-id.  */
10159   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10160     {
10161       /* We don't know whether we're looking at an
10162          operator-function-id or a conversion-function-id.  */
10163       cp_parser_parse_tentatively (parser);
10164       /* Try an operator-function-id.  */
10165       identifier = cp_parser_operator_function_id (parser);
10166       /* If that didn't work, try a conversion-function-id.  */
10167       if (!cp_parser_parse_definitely (parser))
10168         {
10169           cp_parser_error (parser, "expected template-name");
10170           return error_mark_node;
10171         }
10172     }
10173   /* Look for the identifier.  */
10174   else
10175     identifier = cp_parser_identifier (parser);
10176
10177   /* If we didn't find an identifier, we don't have a template-id.  */
10178   if (identifier == error_mark_node)
10179     return error_mark_node;
10180
10181   /* If the name immediately followed the `template' keyword, then it
10182      is a template-name.  However, if the next token is not `<', then
10183      we do not treat it as a template-name, since it is not being used
10184      as part of a template-id.  This enables us to handle constructs
10185      like:
10186
10187        template <typename T> struct S { S(); };
10188        template <typename T> S<T>::S();
10189
10190      correctly.  We would treat `S' as a template -- if it were `S<T>'
10191      -- but we do not if there is no `<'.  */
10192
10193   if (processing_template_decl
10194       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10195     {
10196       /* In a declaration, in a dependent context, we pretend that the
10197          "template" keyword was present in order to improve error
10198          recovery.  For example, given:
10199
10200            template <typename T> void f(T::X<int>);
10201
10202          we want to treat "X<int>" as a template-id.  */
10203       if (is_declaration
10204           && !template_keyword_p
10205           && parser->scope && TYPE_P (parser->scope)
10206           && check_dependency_p
10207           && dependent_type_p (parser->scope)
10208           /* Do not do this for dtors (or ctors), since they never
10209              need the template keyword before their name.  */
10210           && !constructor_name_p (identifier, parser->scope))
10211         {
10212           cp_token_position start = 0;
10213
10214           /* Explain what went wrong.  */
10215           error ("%Hnon-template %qD used as template",
10216                  &token->location, identifier);
10217           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10218                   parser->scope, identifier);
10219           /* If parsing tentatively, find the location of the "<" token.  */
10220           if (cp_parser_simulate_error (parser))
10221             start = cp_lexer_token_position (parser->lexer, true);
10222           /* Parse the template arguments so that we can issue error
10223              messages about them.  */
10224           cp_lexer_consume_token (parser->lexer);
10225           cp_parser_enclosed_template_argument_list (parser);
10226           /* Skip tokens until we find a good place from which to
10227              continue parsing.  */
10228           cp_parser_skip_to_closing_parenthesis (parser,
10229                                                  /*recovering=*/true,
10230                                                  /*or_comma=*/true,
10231                                                  /*consume_paren=*/false);
10232           /* If parsing tentatively, permanently remove the
10233              template argument list.  That will prevent duplicate
10234              error messages from being issued about the missing
10235              "template" keyword.  */
10236           if (start)
10237             cp_lexer_purge_tokens_after (parser->lexer, start);
10238           if (is_identifier)
10239             *is_identifier = true;
10240           return identifier;
10241         }
10242
10243       /* If the "template" keyword is present, then there is generally
10244          no point in doing name-lookup, so we just return IDENTIFIER.
10245          But, if the qualifying scope is non-dependent then we can
10246          (and must) do name-lookup normally.  */
10247       if (template_keyword_p
10248           && (!parser->scope
10249               || (TYPE_P (parser->scope)
10250                   && dependent_type_p (parser->scope))))
10251         return identifier;
10252     }
10253
10254   /* Look up the name.  */
10255   decl = cp_parser_lookup_name (parser, identifier,
10256                                 none_type,
10257                                 /*is_template=*/false,
10258                                 /*is_namespace=*/false,
10259                                 check_dependency_p,
10260                                 /*ambiguous_decls=*/NULL,
10261                                 token->location);
10262   decl = maybe_get_template_decl_from_type_decl (decl);
10263
10264   /* If DECL is a template, then the name was a template-name.  */
10265   if (TREE_CODE (decl) == TEMPLATE_DECL)
10266     ;
10267   else
10268     {
10269       tree fn = NULL_TREE;
10270
10271       /* The standard does not explicitly indicate whether a name that
10272          names a set of overloaded declarations, some of which are
10273          templates, is a template-name.  However, such a name should
10274          be a template-name; otherwise, there is no way to form a
10275          template-id for the overloaded templates.  */
10276       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10277       if (TREE_CODE (fns) == OVERLOAD)
10278         for (fn = fns; fn; fn = OVL_NEXT (fn))
10279           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10280             break;
10281
10282       if (!fn)
10283         {
10284           /* The name does not name a template.  */
10285           cp_parser_error (parser, "expected template-name");
10286           return error_mark_node;
10287         }
10288     }
10289
10290   /* If DECL is dependent, and refers to a function, then just return
10291      its name; we will look it up again during template instantiation.  */
10292   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10293     {
10294       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10295       if (TYPE_P (scope) && dependent_type_p (scope))
10296         return identifier;
10297     }
10298
10299   return decl;
10300 }
10301
10302 /* Parse a template-argument-list.
10303
10304    template-argument-list:
10305      template-argument ... [opt]
10306      template-argument-list , template-argument ... [opt]
10307
10308    Returns a TREE_VEC containing the arguments.  */
10309
10310 static tree
10311 cp_parser_template_argument_list (cp_parser* parser)
10312 {
10313   tree fixed_args[10];
10314   unsigned n_args = 0;
10315   unsigned alloced = 10;
10316   tree *arg_ary = fixed_args;
10317   tree vec;
10318   bool saved_in_template_argument_list_p;
10319   bool saved_ice_p;
10320   bool saved_non_ice_p;
10321
10322   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10323   parser->in_template_argument_list_p = true;
10324   /* Even if the template-id appears in an integral
10325      constant-expression, the contents of the argument list do
10326      not.  */
10327   saved_ice_p = parser->integral_constant_expression_p;
10328   parser->integral_constant_expression_p = false;
10329   saved_non_ice_p = parser->non_integral_constant_expression_p;
10330   parser->non_integral_constant_expression_p = false;
10331   /* Parse the arguments.  */
10332   do
10333     {
10334       tree argument;
10335
10336       if (n_args)
10337         /* Consume the comma.  */
10338         cp_lexer_consume_token (parser->lexer);
10339
10340       /* Parse the template-argument.  */
10341       argument = cp_parser_template_argument (parser);
10342
10343       /* If the next token is an ellipsis, we're expanding a template
10344          argument pack. */
10345       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10346         {
10347           /* Consume the `...' token. */
10348           cp_lexer_consume_token (parser->lexer);
10349
10350           /* Make the argument into a TYPE_PACK_EXPANSION or
10351              EXPR_PACK_EXPANSION. */
10352           argument = make_pack_expansion (argument);
10353         }
10354
10355       if (n_args == alloced)
10356         {
10357           alloced *= 2;
10358
10359           if (arg_ary == fixed_args)
10360             {
10361               arg_ary = XNEWVEC (tree, alloced);
10362               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10363             }
10364           else
10365             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10366         }
10367       arg_ary[n_args++] = argument;
10368     }
10369   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10370
10371   vec = make_tree_vec (n_args);
10372
10373   while (n_args--)
10374     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10375
10376   if (arg_ary != fixed_args)
10377     free (arg_ary);
10378   parser->non_integral_constant_expression_p = saved_non_ice_p;
10379   parser->integral_constant_expression_p = saved_ice_p;
10380   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10381   return vec;
10382 }
10383
10384 /* Parse a template-argument.
10385
10386    template-argument:
10387      assignment-expression
10388      type-id
10389      id-expression
10390
10391    The representation is that of an assignment-expression, type-id, or
10392    id-expression -- except that the qualified id-expression is
10393    evaluated, so that the value returned is either a DECL or an
10394    OVERLOAD.
10395
10396    Although the standard says "assignment-expression", it forbids
10397    throw-expressions or assignments in the template argument.
10398    Therefore, we use "conditional-expression" instead.  */
10399
10400 static tree
10401 cp_parser_template_argument (cp_parser* parser)
10402 {
10403   tree argument;
10404   bool template_p;
10405   bool address_p;
10406   bool maybe_type_id = false;
10407   cp_token *token = NULL, *argument_start_token = NULL;
10408   cp_id_kind idk;
10409
10410   /* There's really no way to know what we're looking at, so we just
10411      try each alternative in order.
10412
10413        [temp.arg]
10414
10415        In a template-argument, an ambiguity between a type-id and an
10416        expression is resolved to a type-id, regardless of the form of
10417        the corresponding template-parameter.
10418
10419      Therefore, we try a type-id first.  */
10420   cp_parser_parse_tentatively (parser);
10421   argument = cp_parser_type_id (parser);
10422   /* If there was no error parsing the type-id but the next token is a
10423      '>>', our behavior depends on which dialect of C++ we're
10424      parsing. In C++98, we probably found a typo for '> >'. But there
10425      are type-id which are also valid expressions. For instance:
10426
10427      struct X { int operator >> (int); };
10428      template <int V> struct Foo {};
10429      Foo<X () >> 5> r;
10430
10431      Here 'X()' is a valid type-id of a function type, but the user just
10432      wanted to write the expression "X() >> 5". Thus, we remember that we
10433      found a valid type-id, but we still try to parse the argument as an
10434      expression to see what happens. 
10435
10436      In C++0x, the '>>' will be considered two separate '>'
10437      tokens.  */
10438   if (!cp_parser_error_occurred (parser)
10439       && cxx_dialect == cxx98
10440       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10441     {
10442       maybe_type_id = true;
10443       cp_parser_abort_tentative_parse (parser);
10444     }
10445   else
10446     {
10447       /* If the next token isn't a `,' or a `>', then this argument wasn't
10448       really finished. This means that the argument is not a valid
10449       type-id.  */
10450       if (!cp_parser_next_token_ends_template_argument_p (parser))
10451         cp_parser_error (parser, "expected template-argument");
10452       /* If that worked, we're done.  */
10453       if (cp_parser_parse_definitely (parser))
10454         return argument;
10455     }
10456   /* We're still not sure what the argument will be.  */
10457   cp_parser_parse_tentatively (parser);
10458   /* Try a template.  */
10459   argument_start_token = cp_lexer_peek_token (parser->lexer);
10460   argument = cp_parser_id_expression (parser,
10461                                       /*template_keyword_p=*/false,
10462                                       /*check_dependency_p=*/true,
10463                                       &template_p,
10464                                       /*declarator_p=*/false,
10465                                       /*optional_p=*/false);
10466   /* If the next token isn't a `,' or a `>', then this argument wasn't
10467      really finished.  */
10468   if (!cp_parser_next_token_ends_template_argument_p (parser))
10469     cp_parser_error (parser, "expected template-argument");
10470   if (!cp_parser_error_occurred (parser))
10471     {
10472       /* Figure out what is being referred to.  If the id-expression
10473          was for a class template specialization, then we will have a
10474          TYPE_DECL at this point.  There is no need to do name lookup
10475          at this point in that case.  */
10476       if (TREE_CODE (argument) != TYPE_DECL)
10477         argument = cp_parser_lookup_name (parser, argument,
10478                                           none_type,
10479                                           /*is_template=*/template_p,
10480                                           /*is_namespace=*/false,
10481                                           /*check_dependency=*/true,
10482                                           /*ambiguous_decls=*/NULL,
10483                                           argument_start_token->location);
10484       if (TREE_CODE (argument) != TEMPLATE_DECL
10485           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10486         cp_parser_error (parser, "expected template-name");
10487     }
10488   if (cp_parser_parse_definitely (parser))
10489     return argument;
10490   /* It must be a non-type argument.  There permitted cases are given
10491      in [temp.arg.nontype]:
10492
10493      -- an integral constant-expression of integral or enumeration
10494         type; or
10495
10496      -- the name of a non-type template-parameter; or
10497
10498      -- the name of an object or function with external linkage...
10499
10500      -- the address of an object or function with external linkage...
10501
10502      -- a pointer to member...  */
10503   /* Look for a non-type template parameter.  */
10504   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10505     {
10506       cp_parser_parse_tentatively (parser);
10507       argument = cp_parser_primary_expression (parser,
10508                                                /*address_p=*/false,
10509                                                /*cast_p=*/false,
10510                                                /*template_arg_p=*/true,
10511                                                &idk);
10512       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10513           || !cp_parser_next_token_ends_template_argument_p (parser))
10514         cp_parser_simulate_error (parser);
10515       if (cp_parser_parse_definitely (parser))
10516         return argument;
10517     }
10518
10519   /* If the next token is "&", the argument must be the address of an
10520      object or function with external linkage.  */
10521   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10522   if (address_p)
10523     cp_lexer_consume_token (parser->lexer);
10524   /* See if we might have an id-expression.  */
10525   token = cp_lexer_peek_token (parser->lexer);
10526   if (token->type == CPP_NAME
10527       || token->keyword == RID_OPERATOR
10528       || token->type == CPP_SCOPE
10529       || token->type == CPP_TEMPLATE_ID
10530       || token->type == CPP_NESTED_NAME_SPECIFIER)
10531     {
10532       cp_parser_parse_tentatively (parser);
10533       argument = cp_parser_primary_expression (parser,
10534                                                address_p,
10535                                                /*cast_p=*/false,
10536                                                /*template_arg_p=*/true,
10537                                                &idk);
10538       if (cp_parser_error_occurred (parser)
10539           || !cp_parser_next_token_ends_template_argument_p (parser))
10540         cp_parser_abort_tentative_parse (parser);
10541       else
10542         {
10543           if (TREE_CODE (argument) == INDIRECT_REF)
10544             {
10545               gcc_assert (REFERENCE_REF_P (argument));
10546               argument = TREE_OPERAND (argument, 0);
10547             }
10548
10549           if (TREE_CODE (argument) == VAR_DECL)
10550             {
10551               /* A variable without external linkage might still be a
10552                  valid constant-expression, so no error is issued here
10553                  if the external-linkage check fails.  */
10554               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10555                 cp_parser_simulate_error (parser);
10556             }
10557           else if (is_overloaded_fn (argument))
10558             /* All overloaded functions are allowed; if the external
10559                linkage test does not pass, an error will be issued
10560                later.  */
10561             ;
10562           else if (address_p
10563                    && (TREE_CODE (argument) == OFFSET_REF
10564                        || TREE_CODE (argument) == SCOPE_REF))
10565             /* A pointer-to-member.  */
10566             ;
10567           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10568             ;
10569           else
10570             cp_parser_simulate_error (parser);
10571
10572           if (cp_parser_parse_definitely (parser))
10573             {
10574               if (address_p)
10575                 argument = build_x_unary_op (ADDR_EXPR, argument,
10576                                              tf_warning_or_error);
10577               return argument;
10578             }
10579         }
10580     }
10581   /* If the argument started with "&", there are no other valid
10582      alternatives at this point.  */
10583   if (address_p)
10584     {
10585       cp_parser_error (parser, "invalid non-type template argument");
10586       return error_mark_node;
10587     }
10588
10589   /* If the argument wasn't successfully parsed as a type-id followed
10590      by '>>', the argument can only be a constant expression now.
10591      Otherwise, we try parsing the constant-expression tentatively,
10592      because the argument could really be a type-id.  */
10593   if (maybe_type_id)
10594     cp_parser_parse_tentatively (parser);
10595   argument = cp_parser_constant_expression (parser,
10596                                             /*allow_non_constant_p=*/false,
10597                                             /*non_constant_p=*/NULL);
10598   argument = fold_non_dependent_expr (argument);
10599   if (!maybe_type_id)
10600     return argument;
10601   if (!cp_parser_next_token_ends_template_argument_p (parser))
10602     cp_parser_error (parser, "expected template-argument");
10603   if (cp_parser_parse_definitely (parser))
10604     return argument;
10605   /* We did our best to parse the argument as a non type-id, but that
10606      was the only alternative that matched (albeit with a '>' after
10607      it). We can assume it's just a typo from the user, and a
10608      diagnostic will then be issued.  */
10609   return cp_parser_type_id (parser);
10610 }
10611
10612 /* Parse an explicit-instantiation.
10613
10614    explicit-instantiation:
10615      template declaration
10616
10617    Although the standard says `declaration', what it really means is:
10618
10619    explicit-instantiation:
10620      template decl-specifier-seq [opt] declarator [opt] ;
10621
10622    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10623    supposed to be allowed.  A defect report has been filed about this
10624    issue.
10625
10626    GNU Extension:
10627
10628    explicit-instantiation:
10629      storage-class-specifier template
10630        decl-specifier-seq [opt] declarator [opt] ;
10631      function-specifier template
10632        decl-specifier-seq [opt] declarator [opt] ;  */
10633
10634 static void
10635 cp_parser_explicit_instantiation (cp_parser* parser)
10636 {
10637   int declares_class_or_enum;
10638   cp_decl_specifier_seq decl_specifiers;
10639   tree extension_specifier = NULL_TREE;
10640   cp_token *token;
10641
10642   /* Look for an (optional) storage-class-specifier or
10643      function-specifier.  */
10644   if (cp_parser_allow_gnu_extensions_p (parser))
10645     {
10646       extension_specifier
10647         = cp_parser_storage_class_specifier_opt (parser);
10648       if (!extension_specifier)
10649         extension_specifier
10650           = cp_parser_function_specifier_opt (parser,
10651                                               /*decl_specs=*/NULL);
10652     }
10653
10654   /* Look for the `template' keyword.  */
10655   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10656   /* Let the front end know that we are processing an explicit
10657      instantiation.  */
10658   begin_explicit_instantiation ();
10659   /* [temp.explicit] says that we are supposed to ignore access
10660      control while processing explicit instantiation directives.  */
10661   push_deferring_access_checks (dk_no_check);
10662   /* Parse a decl-specifier-seq.  */
10663   token = cp_lexer_peek_token (parser->lexer);
10664   cp_parser_decl_specifier_seq (parser,
10665                                 CP_PARSER_FLAGS_OPTIONAL,
10666                                 &decl_specifiers,
10667                                 &declares_class_or_enum);
10668   /* If there was exactly one decl-specifier, and it declared a class,
10669      and there's no declarator, then we have an explicit type
10670      instantiation.  */
10671   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10672     {
10673       tree type;
10674
10675       type = check_tag_decl (&decl_specifiers);
10676       /* Turn access control back on for names used during
10677          template instantiation.  */
10678       pop_deferring_access_checks ();
10679       if (type)
10680         do_type_instantiation (type, extension_specifier,
10681                                /*complain=*/tf_error);
10682     }
10683   else
10684     {
10685       cp_declarator *declarator;
10686       tree decl;
10687
10688       /* Parse the declarator.  */
10689       declarator
10690         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10691                                 /*ctor_dtor_or_conv_p=*/NULL,
10692                                 /*parenthesized_p=*/NULL,
10693                                 /*member_p=*/false);
10694       if (declares_class_or_enum & 2)
10695         cp_parser_check_for_definition_in_return_type (declarator,
10696                                                        decl_specifiers.type,
10697                                                        decl_specifiers.type_location);
10698       if (declarator != cp_error_declarator)
10699         {
10700           decl = grokdeclarator (declarator, &decl_specifiers,
10701                                  NORMAL, 0, &decl_specifiers.attributes);
10702           /* Turn access control back on for names used during
10703              template instantiation.  */
10704           pop_deferring_access_checks ();
10705           /* Do the explicit instantiation.  */
10706           do_decl_instantiation (decl, extension_specifier);
10707         }
10708       else
10709         {
10710           pop_deferring_access_checks ();
10711           /* Skip the body of the explicit instantiation.  */
10712           cp_parser_skip_to_end_of_statement (parser);
10713         }
10714     }
10715   /* We're done with the instantiation.  */
10716   end_explicit_instantiation ();
10717
10718   cp_parser_consume_semicolon_at_end_of_statement (parser);
10719 }
10720
10721 /* Parse an explicit-specialization.
10722
10723    explicit-specialization:
10724      template < > declaration
10725
10726    Although the standard says `declaration', what it really means is:
10727
10728    explicit-specialization:
10729      template <> decl-specifier [opt] init-declarator [opt] ;
10730      template <> function-definition
10731      template <> explicit-specialization
10732      template <> template-declaration  */
10733
10734 static void
10735 cp_parser_explicit_specialization (cp_parser* parser)
10736 {
10737   bool need_lang_pop;
10738   cp_token *token = cp_lexer_peek_token (parser->lexer);
10739
10740   /* Look for the `template' keyword.  */
10741   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10742   /* Look for the `<'.  */
10743   cp_parser_require (parser, CPP_LESS, "%<<%>");
10744   /* Look for the `>'.  */
10745   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10746   /* We have processed another parameter list.  */
10747   ++parser->num_template_parameter_lists;
10748   /* [temp]
10749
10750      A template ... explicit specialization ... shall not have C
10751      linkage.  */
10752   if (current_lang_name == lang_name_c)
10753     {
10754       error ("%Htemplate specialization with C linkage", &token->location);
10755       /* Give it C++ linkage to avoid confusing other parts of the
10756          front end.  */
10757       push_lang_context (lang_name_cplusplus);
10758       need_lang_pop = true;
10759     }
10760   else
10761     need_lang_pop = false;
10762   /* Let the front end know that we are beginning a specialization.  */
10763   if (!begin_specialization ())
10764     {
10765       end_specialization ();
10766       cp_parser_skip_to_end_of_block_or_statement (parser);
10767       return;
10768     }
10769
10770   /* If the next keyword is `template', we need to figure out whether
10771      or not we're looking a template-declaration.  */
10772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10773     {
10774       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10775           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10776         cp_parser_template_declaration_after_export (parser,
10777                                                      /*member_p=*/false);
10778       else
10779         cp_parser_explicit_specialization (parser);
10780     }
10781   else
10782     /* Parse the dependent declaration.  */
10783     cp_parser_single_declaration (parser,
10784                                   /*checks=*/NULL,
10785                                   /*member_p=*/false,
10786                                   /*explicit_specialization_p=*/true,
10787                                   /*friend_p=*/NULL);
10788   /* We're done with the specialization.  */
10789   end_specialization ();
10790   /* For the erroneous case of a template with C linkage, we pushed an
10791      implicit C++ linkage scope; exit that scope now.  */
10792   if (need_lang_pop)
10793     pop_lang_context ();
10794   /* We're done with this parameter list.  */
10795   --parser->num_template_parameter_lists;
10796 }
10797
10798 /* Parse a type-specifier.
10799
10800    type-specifier:
10801      simple-type-specifier
10802      class-specifier
10803      enum-specifier
10804      elaborated-type-specifier
10805      cv-qualifier
10806
10807    GNU Extension:
10808
10809    type-specifier:
10810      __complex__
10811
10812    Returns a representation of the type-specifier.  For a
10813    class-specifier, enum-specifier, or elaborated-type-specifier, a
10814    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10815
10816    The parser flags FLAGS is used to control type-specifier parsing.
10817
10818    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10819    in a decl-specifier-seq.
10820
10821    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10822    class-specifier, enum-specifier, or elaborated-type-specifier, then
10823    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10824    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10825    zero.
10826
10827    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10828    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10829    is set to FALSE.  */
10830
10831 static tree
10832 cp_parser_type_specifier (cp_parser* parser,
10833                           cp_parser_flags flags,
10834                           cp_decl_specifier_seq *decl_specs,
10835                           bool is_declaration,
10836                           int* declares_class_or_enum,
10837                           bool* is_cv_qualifier)
10838 {
10839   tree type_spec = NULL_TREE;
10840   cp_token *token;
10841   enum rid keyword;
10842   cp_decl_spec ds = ds_last;
10843
10844   /* Assume this type-specifier does not declare a new type.  */
10845   if (declares_class_or_enum)
10846     *declares_class_or_enum = 0;
10847   /* And that it does not specify a cv-qualifier.  */
10848   if (is_cv_qualifier)
10849     *is_cv_qualifier = false;
10850   /* Peek at the next token.  */
10851   token = cp_lexer_peek_token (parser->lexer);
10852
10853   /* If we're looking at a keyword, we can use that to guide the
10854      production we choose.  */
10855   keyword = token->keyword;
10856   switch (keyword)
10857     {
10858     case RID_ENUM:
10859       /* Look for the enum-specifier.  */
10860       type_spec = cp_parser_enum_specifier (parser);
10861       /* If that worked, we're done.  */
10862       if (type_spec)
10863         {
10864           if (declares_class_or_enum)
10865             *declares_class_or_enum = 2;
10866           if (decl_specs)
10867             cp_parser_set_decl_spec_type (decl_specs,
10868                                           type_spec,
10869                                           token->location,
10870                                           /*user_defined_p=*/true);
10871           return type_spec;
10872         }
10873       else
10874         goto elaborated_type_specifier;
10875
10876       /* Any of these indicate either a class-specifier, or an
10877          elaborated-type-specifier.  */
10878     case RID_CLASS:
10879     case RID_STRUCT:
10880     case RID_UNION:
10881       /* Parse tentatively so that we can back up if we don't find a
10882          class-specifier.  */
10883       cp_parser_parse_tentatively (parser);
10884       /* Look for the class-specifier.  */
10885       type_spec = cp_parser_class_specifier (parser);
10886       /* If that worked, we're done.  */
10887       if (cp_parser_parse_definitely (parser))
10888         {
10889           if (declares_class_or_enum)
10890             *declares_class_or_enum = 2;
10891           if (decl_specs)
10892             cp_parser_set_decl_spec_type (decl_specs,
10893                                           type_spec,
10894                                           token->location,
10895                                           /*user_defined_p=*/true);
10896           return type_spec;
10897         }
10898
10899       /* Fall through.  */
10900     elaborated_type_specifier:
10901       /* We're declaring (not defining) a class or enum.  */
10902       if (declares_class_or_enum)
10903         *declares_class_or_enum = 1;
10904
10905       /* Fall through.  */
10906     case RID_TYPENAME:
10907       /* Look for an elaborated-type-specifier.  */
10908       type_spec
10909         = (cp_parser_elaborated_type_specifier
10910            (parser,
10911             decl_specs && decl_specs->specs[(int) ds_friend],
10912             is_declaration));
10913       if (decl_specs)
10914         cp_parser_set_decl_spec_type (decl_specs,
10915                                       type_spec,
10916                                       token->location,
10917                                       /*user_defined_p=*/true);
10918       return type_spec;
10919
10920     case RID_CONST:
10921       ds = ds_const;
10922       if (is_cv_qualifier)
10923         *is_cv_qualifier = true;
10924       break;
10925
10926     case RID_VOLATILE:
10927       ds = ds_volatile;
10928       if (is_cv_qualifier)
10929         *is_cv_qualifier = true;
10930       break;
10931
10932     case RID_RESTRICT:
10933       ds = ds_restrict;
10934       if (is_cv_qualifier)
10935         *is_cv_qualifier = true;
10936       break;
10937
10938     case RID_COMPLEX:
10939       /* The `__complex__' keyword is a GNU extension.  */
10940       ds = ds_complex;
10941       break;
10942
10943     default:
10944       break;
10945     }
10946
10947   /* Handle simple keywords.  */
10948   if (ds != ds_last)
10949     {
10950       if (decl_specs)
10951         {
10952           ++decl_specs->specs[(int)ds];
10953           decl_specs->any_specifiers_p = true;
10954         }
10955       return cp_lexer_consume_token (parser->lexer)->u.value;
10956     }
10957
10958   /* If we do not already have a type-specifier, assume we are looking
10959      at a simple-type-specifier.  */
10960   type_spec = cp_parser_simple_type_specifier (parser,
10961                                                decl_specs,
10962                                                flags);
10963
10964   /* If we didn't find a type-specifier, and a type-specifier was not
10965      optional in this context, issue an error message.  */
10966   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10967     {
10968       cp_parser_error (parser, "expected type specifier");
10969       return error_mark_node;
10970     }
10971
10972   return type_spec;
10973 }
10974
10975 /* Parse a simple-type-specifier.
10976
10977    simple-type-specifier:
10978      :: [opt] nested-name-specifier [opt] type-name
10979      :: [opt] nested-name-specifier template template-id
10980      char
10981      wchar_t
10982      bool
10983      short
10984      int
10985      long
10986      signed
10987      unsigned
10988      float
10989      double
10990      void
10991
10992    C++0x Extension:
10993
10994    simple-type-specifier:
10995      auto
10996      decltype ( expression )   
10997      char16_t
10998      char32_t
10999
11000    GNU Extension:
11001
11002    simple-type-specifier:
11003      __typeof__ unary-expression
11004      __typeof__ ( type-id )
11005
11006    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11007    appropriately updated.  */
11008
11009 static tree
11010 cp_parser_simple_type_specifier (cp_parser* parser,
11011                                  cp_decl_specifier_seq *decl_specs,
11012                                  cp_parser_flags flags)
11013 {
11014   tree type = NULL_TREE;
11015   cp_token *token;
11016
11017   /* Peek at the next token.  */
11018   token = cp_lexer_peek_token (parser->lexer);
11019
11020   /* If we're looking at a keyword, things are easy.  */
11021   switch (token->keyword)
11022     {
11023     case RID_CHAR:
11024       if (decl_specs)
11025         decl_specs->explicit_char_p = true;
11026       type = char_type_node;
11027       break;
11028     case RID_CHAR16:
11029       type = char16_type_node;
11030       break;
11031     case RID_CHAR32:
11032       type = char32_type_node;
11033       break;
11034     case RID_WCHAR:
11035       type = wchar_type_node;
11036       break;
11037     case RID_BOOL:
11038       type = boolean_type_node;
11039       break;
11040     case RID_SHORT:
11041       if (decl_specs)
11042         ++decl_specs->specs[(int) ds_short];
11043       type = short_integer_type_node;
11044       break;
11045     case RID_INT:
11046       if (decl_specs)
11047         decl_specs->explicit_int_p = true;
11048       type = integer_type_node;
11049       break;
11050     case RID_LONG:
11051       if (decl_specs)
11052         ++decl_specs->specs[(int) ds_long];
11053       type = long_integer_type_node;
11054       break;
11055     case RID_SIGNED:
11056       if (decl_specs)
11057         ++decl_specs->specs[(int) ds_signed];
11058       type = integer_type_node;
11059       break;
11060     case RID_UNSIGNED:
11061       if (decl_specs)
11062         ++decl_specs->specs[(int) ds_unsigned];
11063       type = unsigned_type_node;
11064       break;
11065     case RID_FLOAT:
11066       type = float_type_node;
11067       break;
11068     case RID_DOUBLE:
11069       type = double_type_node;
11070       break;
11071     case RID_VOID:
11072       type = void_type_node;
11073       break;
11074       
11075     case RID_AUTO:
11076       maybe_warn_cpp0x ("C++0x auto");
11077       type = make_auto ();
11078       break;
11079
11080     case RID_DECLTYPE:
11081       /* Parse the `decltype' type.  */
11082       type = cp_parser_decltype (parser);
11083
11084       if (decl_specs)
11085         cp_parser_set_decl_spec_type (decl_specs, type,
11086                                       token->location,
11087                                       /*user_defined_p=*/true);
11088
11089       return type;
11090
11091     case RID_TYPEOF:
11092       /* Consume the `typeof' token.  */
11093       cp_lexer_consume_token (parser->lexer);
11094       /* Parse the operand to `typeof'.  */
11095       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11096       /* If it is not already a TYPE, take its type.  */
11097       if (!TYPE_P (type))
11098         type = finish_typeof (type);
11099
11100       if (decl_specs)
11101         cp_parser_set_decl_spec_type (decl_specs, type,
11102                                       token->location,
11103                                       /*user_defined_p=*/true);
11104
11105       return type;
11106
11107     default:
11108       break;
11109     }
11110
11111   /* If the type-specifier was for a built-in type, we're done.  */
11112   if (type)
11113     {
11114       tree id;
11115
11116       /* Record the type.  */
11117       if (decl_specs
11118           && (token->keyword != RID_SIGNED
11119               && token->keyword != RID_UNSIGNED
11120               && token->keyword != RID_SHORT
11121               && token->keyword != RID_LONG))
11122         cp_parser_set_decl_spec_type (decl_specs,
11123                                       type,
11124                                       token->location,
11125                                       /*user_defined=*/false);
11126       if (decl_specs)
11127         decl_specs->any_specifiers_p = true;
11128
11129       /* Consume the token.  */
11130       id = cp_lexer_consume_token (parser->lexer)->u.value;
11131
11132       /* There is no valid C++ program where a non-template type is
11133          followed by a "<".  That usually indicates that the user thought
11134          that the type was a template.  */
11135       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11136
11137       return TYPE_NAME (type);
11138     }
11139
11140   /* The type-specifier must be a user-defined type.  */
11141   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11142     {
11143       bool qualified_p;
11144       bool global_p;
11145
11146       /* Don't gobble tokens or issue error messages if this is an
11147          optional type-specifier.  */
11148       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11149         cp_parser_parse_tentatively (parser);
11150
11151       /* Look for the optional `::' operator.  */
11152       global_p
11153         = (cp_parser_global_scope_opt (parser,
11154                                        /*current_scope_valid_p=*/false)
11155            != NULL_TREE);
11156       /* Look for the nested-name specifier.  */
11157       qualified_p
11158         = (cp_parser_nested_name_specifier_opt (parser,
11159                                                 /*typename_keyword_p=*/false,
11160                                                 /*check_dependency_p=*/true,
11161                                                 /*type_p=*/false,
11162                                                 /*is_declaration=*/false)
11163            != NULL_TREE);
11164       token = cp_lexer_peek_token (parser->lexer);
11165       /* If we have seen a nested-name-specifier, and the next token
11166          is `template', then we are using the template-id production.  */
11167       if (parser->scope
11168           && cp_parser_optional_template_keyword (parser))
11169         {
11170           /* Look for the template-id.  */
11171           type = cp_parser_template_id (parser,
11172                                         /*template_keyword_p=*/true,
11173                                         /*check_dependency_p=*/true,
11174                                         /*is_declaration=*/false);
11175           /* If the template-id did not name a type, we are out of
11176              luck.  */
11177           if (TREE_CODE (type) != TYPE_DECL)
11178             {
11179               cp_parser_error (parser, "expected template-id for type");
11180               type = NULL_TREE;
11181             }
11182         }
11183       /* Otherwise, look for a type-name.  */
11184       else
11185         type = cp_parser_type_name (parser);
11186       /* Keep track of all name-lookups performed in class scopes.  */
11187       if (type
11188           && !global_p
11189           && !qualified_p
11190           && TREE_CODE (type) == TYPE_DECL
11191           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11192         maybe_note_name_used_in_class (DECL_NAME (type), type);
11193       /* If it didn't work out, we don't have a TYPE.  */
11194       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11195           && !cp_parser_parse_definitely (parser))
11196         type = NULL_TREE;
11197       if (type && decl_specs)
11198         cp_parser_set_decl_spec_type (decl_specs, type,
11199                                       token->location,
11200                                       /*user_defined=*/true);
11201     }
11202
11203   /* If we didn't get a type-name, issue an error message.  */
11204   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11205     {
11206       cp_parser_error (parser, "expected type-name");
11207       return error_mark_node;
11208     }
11209
11210   /* There is no valid C++ program where a non-template type is
11211      followed by a "<".  That usually indicates that the user thought
11212      that the type was a template.  */
11213   if (type && type != error_mark_node)
11214     {
11215       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11216          If it is, then the '<'...'>' enclose protocol names rather than
11217          template arguments, and so everything is fine.  */
11218       if (c_dialect_objc ()
11219           && (objc_is_id (type) || objc_is_class_name (type)))
11220         {
11221           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11222           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11223
11224           /* Clobber the "unqualified" type previously entered into
11225              DECL_SPECS with the new, improved protocol-qualified version.  */
11226           if (decl_specs)
11227             decl_specs->type = qual_type;
11228
11229           return qual_type;
11230         }
11231
11232       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11233                                                token->location);
11234     }
11235
11236   return type;
11237 }
11238
11239 /* Parse a type-name.
11240
11241    type-name:
11242      class-name
11243      enum-name
11244      typedef-name
11245
11246    enum-name:
11247      identifier
11248
11249    typedef-name:
11250      identifier
11251
11252    Returns a TYPE_DECL for the type.  */
11253
11254 static tree
11255 cp_parser_type_name (cp_parser* parser)
11256 {
11257   tree type_decl;
11258
11259   /* We can't know yet whether it is a class-name or not.  */
11260   cp_parser_parse_tentatively (parser);
11261   /* Try a class-name.  */
11262   type_decl = cp_parser_class_name (parser,
11263                                     /*typename_keyword_p=*/false,
11264                                     /*template_keyword_p=*/false,
11265                                     none_type,
11266                                     /*check_dependency_p=*/true,
11267                                     /*class_head_p=*/false,
11268                                     /*is_declaration=*/false);
11269   /* If it's not a class-name, keep looking.  */
11270   if (!cp_parser_parse_definitely (parser))
11271     {
11272       /* It must be a typedef-name or an enum-name.  */
11273       return cp_parser_nonclass_name (parser);
11274     }
11275
11276   return type_decl;
11277 }
11278
11279 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11280
11281    enum-name:
11282      identifier
11283
11284    typedef-name:
11285      identifier
11286
11287    Returns a TYPE_DECL for the type.  */
11288
11289 static tree
11290 cp_parser_nonclass_name (cp_parser* parser)
11291 {
11292   tree type_decl;
11293   tree identifier;
11294
11295   cp_token *token = cp_lexer_peek_token (parser->lexer);
11296   identifier = cp_parser_identifier (parser);
11297   if (identifier == error_mark_node)
11298     return error_mark_node;
11299
11300   /* Look up the type-name.  */
11301   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11302
11303   if (TREE_CODE (type_decl) != TYPE_DECL
11304       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11305     {
11306       /* See if this is an Objective-C type.  */
11307       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11308       tree type = objc_get_protocol_qualified_type (identifier, protos);
11309       if (type)
11310         type_decl = TYPE_NAME (type);
11311     }
11312   
11313   /* Issue an error if we did not find a type-name.  */
11314   if (TREE_CODE (type_decl) != TYPE_DECL)
11315     {
11316       if (!cp_parser_simulate_error (parser))
11317         cp_parser_name_lookup_error (parser, identifier, type_decl,
11318                                      "is not a type", token->location);
11319       return error_mark_node;
11320     }
11321   /* Remember that the name was used in the definition of the
11322      current class so that we can check later to see if the
11323      meaning would have been different after the class was
11324      entirely defined.  */
11325   else if (type_decl != error_mark_node
11326            && !parser->scope)
11327     maybe_note_name_used_in_class (identifier, type_decl);
11328   
11329   return type_decl;
11330 }
11331
11332 /* Parse an elaborated-type-specifier.  Note that the grammar given
11333    here incorporates the resolution to DR68.
11334
11335    elaborated-type-specifier:
11336      class-key :: [opt] nested-name-specifier [opt] identifier
11337      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11338      enum-key :: [opt] nested-name-specifier [opt] identifier
11339      typename :: [opt] nested-name-specifier identifier
11340      typename :: [opt] nested-name-specifier template [opt]
11341        template-id
11342
11343    GNU extension:
11344
11345    elaborated-type-specifier:
11346      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11347      class-key attributes :: [opt] nested-name-specifier [opt]
11348                template [opt] template-id
11349      enum attributes :: [opt] nested-name-specifier [opt] identifier
11350
11351    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11352    declared `friend'.  If IS_DECLARATION is TRUE, then this
11353    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11354    something is being declared.
11355
11356    Returns the TYPE specified.  */
11357
11358 static tree
11359 cp_parser_elaborated_type_specifier (cp_parser* parser,
11360                                      bool is_friend,
11361                                      bool is_declaration)
11362 {
11363   enum tag_types tag_type;
11364   tree identifier;
11365   tree type = NULL_TREE;
11366   tree attributes = NULL_TREE;
11367   cp_token *token = NULL;
11368
11369   /* See if we're looking at the `enum' keyword.  */
11370   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11371     {
11372       /* Consume the `enum' token.  */
11373       cp_lexer_consume_token (parser->lexer);
11374       /* Remember that it's an enumeration type.  */
11375       tag_type = enum_type;
11376       /* Parse the optional `struct' or `class' key (for C++0x scoped
11377          enums).  */
11378       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11379           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11380         {
11381           if (cxx_dialect == cxx98)
11382             maybe_warn_cpp0x ("scoped enums");
11383
11384           /* Consume the `struct' or `class'.  */
11385           cp_lexer_consume_token (parser->lexer);
11386         }
11387       /* Parse the attributes.  */
11388       attributes = cp_parser_attributes_opt (parser);
11389     }
11390   /* Or, it might be `typename'.  */
11391   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11392                                            RID_TYPENAME))
11393     {
11394       /* Consume the `typename' token.  */
11395       cp_lexer_consume_token (parser->lexer);
11396       /* Remember that it's a `typename' type.  */
11397       tag_type = typename_type;
11398       /* The `typename' keyword is only allowed in templates.  */
11399       if (!processing_template_decl)
11400         permerror (input_location, "using %<typename%> outside of template");
11401     }
11402   /* Otherwise it must be a class-key.  */
11403   else
11404     {
11405       tag_type = cp_parser_class_key (parser);
11406       if (tag_type == none_type)
11407         return error_mark_node;
11408       /* Parse the attributes.  */
11409       attributes = cp_parser_attributes_opt (parser);
11410     }
11411
11412   /* Look for the `::' operator.  */
11413   cp_parser_global_scope_opt (parser,
11414                               /*current_scope_valid_p=*/false);
11415   /* Look for the nested-name-specifier.  */
11416   if (tag_type == typename_type)
11417     {
11418       if (!cp_parser_nested_name_specifier (parser,
11419                                            /*typename_keyword_p=*/true,
11420                                            /*check_dependency_p=*/true,
11421                                            /*type_p=*/true,
11422                                             is_declaration))
11423         return error_mark_node;
11424     }
11425   else
11426     /* Even though `typename' is not present, the proposed resolution
11427        to Core Issue 180 says that in `class A<T>::B', `B' should be
11428        considered a type-name, even if `A<T>' is dependent.  */
11429     cp_parser_nested_name_specifier_opt (parser,
11430                                          /*typename_keyword_p=*/true,
11431                                          /*check_dependency_p=*/true,
11432                                          /*type_p=*/true,
11433                                          is_declaration);
11434  /* For everything but enumeration types, consider a template-id.
11435     For an enumeration type, consider only a plain identifier.  */
11436   if (tag_type != enum_type)
11437     {
11438       bool template_p = false;
11439       tree decl;
11440
11441       /* Allow the `template' keyword.  */
11442       template_p = cp_parser_optional_template_keyword (parser);
11443       /* If we didn't see `template', we don't know if there's a
11444          template-id or not.  */
11445       if (!template_p)
11446         cp_parser_parse_tentatively (parser);
11447       /* Parse the template-id.  */
11448       token = cp_lexer_peek_token (parser->lexer);
11449       decl = cp_parser_template_id (parser, template_p,
11450                                     /*check_dependency_p=*/true,
11451                                     is_declaration);
11452       /* If we didn't find a template-id, look for an ordinary
11453          identifier.  */
11454       if (!template_p && !cp_parser_parse_definitely (parser))
11455         ;
11456       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11457          in effect, then we must assume that, upon instantiation, the
11458          template will correspond to a class.  */
11459       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11460                && tag_type == typename_type)
11461         type = make_typename_type (parser->scope, decl,
11462                                    typename_type,
11463                                    /*complain=*/tf_error);
11464       else
11465         type = TREE_TYPE (decl);
11466     }
11467
11468   if (!type)
11469     {
11470       token = cp_lexer_peek_token (parser->lexer);
11471       identifier = cp_parser_identifier (parser);
11472
11473       if (identifier == error_mark_node)
11474         {
11475           parser->scope = NULL_TREE;
11476           return error_mark_node;
11477         }
11478
11479       /* For a `typename', we needn't call xref_tag.  */
11480       if (tag_type == typename_type
11481           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11482         return cp_parser_make_typename_type (parser, parser->scope,
11483                                              identifier,
11484                                              token->location);
11485       /* Look up a qualified name in the usual way.  */
11486       if (parser->scope)
11487         {
11488           tree decl;
11489           tree ambiguous_decls;
11490
11491           decl = cp_parser_lookup_name (parser, identifier,
11492                                         tag_type,
11493                                         /*is_template=*/false,
11494                                         /*is_namespace=*/false,
11495                                         /*check_dependency=*/true,
11496                                         &ambiguous_decls,
11497                                         token->location);
11498
11499           /* If the lookup was ambiguous, an error will already have been
11500              issued.  */
11501           if (ambiguous_decls)
11502             return error_mark_node;
11503
11504           /* If we are parsing friend declaration, DECL may be a
11505              TEMPLATE_DECL tree node here.  However, we need to check
11506              whether this TEMPLATE_DECL results in valid code.  Consider
11507              the following example:
11508
11509                namespace N {
11510                  template <class T> class C {};
11511                }
11512                class X {
11513                  template <class T> friend class N::C; // #1, valid code
11514                };
11515                template <class T> class Y {
11516                  friend class N::C;                    // #2, invalid code
11517                };
11518
11519              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11520              name lookup of `N::C'.  We see that friend declaration must
11521              be template for the code to be valid.  Note that
11522              processing_template_decl does not work here since it is
11523              always 1 for the above two cases.  */
11524
11525           decl = (cp_parser_maybe_treat_template_as_class
11526                   (decl, /*tag_name_p=*/is_friend
11527                          && parser->num_template_parameter_lists));
11528
11529           if (TREE_CODE (decl) != TYPE_DECL)
11530             {
11531               cp_parser_diagnose_invalid_type_name (parser,
11532                                                     parser->scope,
11533                                                     identifier,
11534                                                     token->location);
11535               return error_mark_node;
11536             }
11537
11538           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11539             {
11540               bool allow_template = (parser->num_template_parameter_lists
11541                                       || DECL_SELF_REFERENCE_P (decl));
11542               type = check_elaborated_type_specifier (tag_type, decl, 
11543                                                       allow_template);
11544
11545               if (type == error_mark_node)
11546                 return error_mark_node;
11547             }
11548
11549           /* Forward declarations of nested types, such as
11550
11551                class C1::C2;
11552                class C1::C2::C3;
11553
11554              are invalid unless all components preceding the final '::'
11555              are complete.  If all enclosing types are complete, these
11556              declarations become merely pointless.
11557
11558              Invalid forward declarations of nested types are errors
11559              caught elsewhere in parsing.  Those that are pointless arrive
11560              here.  */
11561
11562           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11563               && !is_friend && !processing_explicit_instantiation)
11564             warning (0, "declaration %qD does not declare anything", decl);
11565
11566           type = TREE_TYPE (decl);
11567         }
11568       else
11569         {
11570           /* An elaborated-type-specifier sometimes introduces a new type and
11571              sometimes names an existing type.  Normally, the rule is that it
11572              introduces a new type only if there is not an existing type of
11573              the same name already in scope.  For example, given:
11574
11575                struct S {};
11576                void f() { struct S s; }
11577
11578              the `struct S' in the body of `f' is the same `struct S' as in
11579              the global scope; the existing definition is used.  However, if
11580              there were no global declaration, this would introduce a new
11581              local class named `S'.
11582
11583              An exception to this rule applies to the following code:
11584
11585                namespace N { struct S; }
11586
11587              Here, the elaborated-type-specifier names a new type
11588              unconditionally; even if there is already an `S' in the
11589              containing scope this declaration names a new type.
11590              This exception only applies if the elaborated-type-specifier
11591              forms the complete declaration:
11592
11593                [class.name]
11594
11595                A declaration consisting solely of `class-key identifier ;' is
11596                either a redeclaration of the name in the current scope or a
11597                forward declaration of the identifier as a class name.  It
11598                introduces the name into the current scope.
11599
11600              We are in this situation precisely when the next token is a `;'.
11601
11602              An exception to the exception is that a `friend' declaration does
11603              *not* name a new type; i.e., given:
11604
11605                struct S { friend struct T; };
11606
11607              `T' is not a new type in the scope of `S'.
11608
11609              Also, `new struct S' or `sizeof (struct S)' never results in the
11610              definition of a new type; a new type can only be declared in a
11611              declaration context.  */
11612
11613           tag_scope ts;
11614           bool template_p;
11615
11616           if (is_friend)
11617             /* Friends have special name lookup rules.  */
11618             ts = ts_within_enclosing_non_class;
11619           else if (is_declaration
11620                    && cp_lexer_next_token_is (parser->lexer,
11621                                               CPP_SEMICOLON))
11622             /* This is a `class-key identifier ;' */
11623             ts = ts_current;
11624           else
11625             ts = ts_global;
11626
11627           template_p =
11628             (parser->num_template_parameter_lists
11629              && (cp_parser_next_token_starts_class_definition_p (parser)
11630                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11631           /* An unqualified name was used to reference this type, so
11632              there were no qualifying templates.  */
11633           if (!cp_parser_check_template_parameters (parser,
11634                                                     /*num_templates=*/0,
11635                                                     token->location))
11636             return error_mark_node;
11637           type = xref_tag (tag_type, identifier, ts, template_p);
11638         }
11639     }
11640
11641   if (type == error_mark_node)
11642     return error_mark_node;
11643
11644   /* Allow attributes on forward declarations of classes.  */
11645   if (attributes)
11646     {
11647       if (TREE_CODE (type) == TYPENAME_TYPE)
11648         warning (OPT_Wattributes,
11649                  "attributes ignored on uninstantiated type");
11650       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11651                && ! processing_explicit_instantiation)
11652         warning (OPT_Wattributes,
11653                  "attributes ignored on template instantiation");
11654       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11655         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11656       else
11657         warning (OPT_Wattributes,
11658                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11659     }
11660
11661   if (tag_type != enum_type)
11662     cp_parser_check_class_key (tag_type, type);
11663
11664   /* A "<" cannot follow an elaborated type specifier.  If that
11665      happens, the user was probably trying to form a template-id.  */
11666   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11667
11668   return type;
11669 }
11670
11671 /* Parse an enum-specifier.
11672
11673    enum-specifier:
11674      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11675
11676    enum-key:
11677      enum
11678      enum class   [C++0x]
11679      enum struct  [C++0x]
11680
11681    enum-base:   [C++0x]
11682      : type-specifier-seq
11683
11684    GNU Extensions:
11685      enum-key attributes[opt] identifier [opt] enum-base [opt] 
11686        { enumerator-list [opt] }attributes[opt]
11687
11688    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11689    if the token stream isn't an enum-specifier after all.  */
11690
11691 static tree
11692 cp_parser_enum_specifier (cp_parser* parser)
11693 {
11694   tree identifier;
11695   tree type;
11696   tree attributes;
11697   bool scoped_enum_p = false;
11698   tree underlying_type = NULL_TREE;
11699
11700   /* Parse tentatively so that we can back up if we don't find a
11701      enum-specifier.  */
11702   cp_parser_parse_tentatively (parser);
11703
11704   /* Caller guarantees that the current token is 'enum', an identifier
11705      possibly follows, and the token after that is an opening brace.
11706      If we don't have an identifier, fabricate an anonymous name for
11707      the enumeration being defined.  */
11708   cp_lexer_consume_token (parser->lexer);
11709
11710   /* Parse the "class" or "struct", which indicates a scoped
11711      enumeration type in C++0x.  */
11712   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11713       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11714     {
11715       if (cxx_dialect == cxx98)
11716         maybe_warn_cpp0x ("scoped enums");
11717
11718       /* Consume the `struct' or `class' token.  */
11719       cp_lexer_consume_token (parser->lexer);
11720
11721       scoped_enum_p = true;
11722     }
11723       
11724   attributes = cp_parser_attributes_opt (parser);
11725
11726   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11727     identifier = cp_parser_identifier (parser);
11728   else
11729     identifier = make_anon_name ();
11730
11731   /* Check for the `:' that denotes a specified underlying type in C++0x.  */
11732   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11733     {
11734       cp_decl_specifier_seq type_specifiers;
11735
11736       if (cxx_dialect == cxx98)
11737         maybe_warn_cpp0x ("scoped enums");
11738
11739       /* Consume the `:'.  */
11740       cp_lexer_consume_token (parser->lexer);
11741
11742       /* Parse the type-specifier-seq.  */
11743       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11744                                     &type_specifiers);
11745       if (type_specifiers.type == error_mark_node)
11746         return error_mark_node;
11747      
11748       /* If that didn't work, stop.  */
11749       if (type_specifiers.type != error_mark_node)
11750         {
11751           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11752                                             /*initialized=*/0, NULL);
11753           if (underlying_type == error_mark_node)
11754             underlying_type = NULL_TREE;
11755         }
11756       else
11757         cp_parser_error (parser, "expected underlying type of enumeration");
11758     }
11759
11760   /* Look for the `{' but don't consume it yet.  */
11761   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11762     cp_parser_simulate_error (parser);
11763
11764   if (!cp_parser_parse_definitely (parser))
11765     return NULL_TREE;
11766
11767   /* Issue an error message if type-definitions are forbidden here.  */
11768   if (!cp_parser_check_type_definition (parser))
11769     type = error_mark_node;
11770   else
11771     /* Create the new type.  We do this before consuming the opening
11772        brace so the enum will be recorded as being on the line of its
11773        tag (or the 'enum' keyword, if there is no tag).  */
11774     type = start_enum (identifier, underlying_type, scoped_enum_p);
11775   
11776   /* Consume the opening brace.  */
11777   cp_lexer_consume_token (parser->lexer);
11778
11779   if (type == error_mark_node)
11780     {
11781       cp_parser_skip_to_end_of_block_or_statement (parser);
11782       return error_mark_node;
11783     }
11784
11785   /* If the next token is not '}', then there are some enumerators.  */
11786   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11787     cp_parser_enumerator_list (parser, type);
11788
11789   /* Consume the final '}'.  */
11790   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11791
11792   /* Look for trailing attributes to apply to this enumeration, and
11793      apply them if appropriate.  */
11794   if (cp_parser_allow_gnu_extensions_p (parser))
11795     {
11796       tree trailing_attr = cp_parser_attributes_opt (parser);
11797       cplus_decl_attributes (&type,
11798                              trailing_attr,
11799                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11800     }
11801
11802   /* Finish up the enumeration.  */
11803   finish_enum (type);
11804
11805   return type;
11806 }
11807
11808 /* Parse an enumerator-list.  The enumerators all have the indicated
11809    TYPE.
11810
11811    enumerator-list:
11812      enumerator-definition
11813      enumerator-list , enumerator-definition  */
11814
11815 static void
11816 cp_parser_enumerator_list (cp_parser* parser, tree type)
11817 {
11818   while (true)
11819     {
11820       /* Parse an enumerator-definition.  */
11821       cp_parser_enumerator_definition (parser, type);
11822
11823       /* If the next token is not a ',', we've reached the end of
11824          the list.  */
11825       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11826         break;
11827       /* Otherwise, consume the `,' and keep going.  */
11828       cp_lexer_consume_token (parser->lexer);
11829       /* If the next token is a `}', there is a trailing comma.  */
11830       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11831         {
11832           if (!in_system_header)
11833             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
11834           break;
11835         }
11836     }
11837 }
11838
11839 /* Parse an enumerator-definition.  The enumerator has the indicated
11840    TYPE.
11841
11842    enumerator-definition:
11843      enumerator
11844      enumerator = constant-expression
11845
11846    enumerator:
11847      identifier  */
11848
11849 static void
11850 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11851 {
11852   tree identifier;
11853   tree value;
11854
11855   /* Look for the identifier.  */
11856   identifier = cp_parser_identifier (parser);
11857   if (identifier == error_mark_node)
11858     return;
11859
11860   /* If the next token is an '=', then there is an explicit value.  */
11861   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11862     {
11863       /* Consume the `=' token.  */
11864       cp_lexer_consume_token (parser->lexer);
11865       /* Parse the value.  */
11866       value = cp_parser_constant_expression (parser,
11867                                              /*allow_non_constant_p=*/false,
11868                                              NULL);
11869     }
11870   else
11871     value = NULL_TREE;
11872
11873   /* Create the enumerator.  */
11874   build_enumerator (identifier, value, type);
11875 }
11876
11877 /* Parse a namespace-name.
11878
11879    namespace-name:
11880      original-namespace-name
11881      namespace-alias
11882
11883    Returns the NAMESPACE_DECL for the namespace.  */
11884
11885 static tree
11886 cp_parser_namespace_name (cp_parser* parser)
11887 {
11888   tree identifier;
11889   tree namespace_decl;
11890
11891   cp_token *token = cp_lexer_peek_token (parser->lexer);
11892
11893   /* Get the name of the namespace.  */
11894   identifier = cp_parser_identifier (parser);
11895   if (identifier == error_mark_node)
11896     return error_mark_node;
11897
11898   /* Look up the identifier in the currently active scope.  Look only
11899      for namespaces, due to:
11900
11901        [basic.lookup.udir]
11902
11903        When looking up a namespace-name in a using-directive or alias
11904        definition, only namespace names are considered.
11905
11906      And:
11907
11908        [basic.lookup.qual]
11909
11910        During the lookup of a name preceding the :: scope resolution
11911        operator, object, function, and enumerator names are ignored.
11912
11913      (Note that cp_parser_qualifying_entity only calls this
11914      function if the token after the name is the scope resolution
11915      operator.)  */
11916   namespace_decl = cp_parser_lookup_name (parser, identifier,
11917                                           none_type,
11918                                           /*is_template=*/false,
11919                                           /*is_namespace=*/true,
11920                                           /*check_dependency=*/true,
11921                                           /*ambiguous_decls=*/NULL,
11922                                           token->location);
11923   /* If it's not a namespace, issue an error.  */
11924   if (namespace_decl == error_mark_node
11925       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11926     {
11927       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11928         error ("%H%qD is not a namespace-name", &token->location, identifier);
11929       cp_parser_error (parser, "expected namespace-name");
11930       namespace_decl = error_mark_node;
11931     }
11932
11933   return namespace_decl;
11934 }
11935
11936 /* Parse a namespace-definition.
11937
11938    namespace-definition:
11939      named-namespace-definition
11940      unnamed-namespace-definition
11941
11942    named-namespace-definition:
11943      original-namespace-definition
11944      extension-namespace-definition
11945
11946    original-namespace-definition:
11947      namespace identifier { namespace-body }
11948
11949    extension-namespace-definition:
11950      namespace original-namespace-name { namespace-body }
11951
11952    unnamed-namespace-definition:
11953      namespace { namespace-body } */
11954
11955 static void
11956 cp_parser_namespace_definition (cp_parser* parser)
11957 {
11958   tree identifier, attribs;
11959   bool has_visibility;
11960   bool is_inline;
11961
11962   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11963     {
11964       is_inline = true;
11965       cp_lexer_consume_token (parser->lexer);
11966     }
11967   else
11968     is_inline = false;
11969
11970   /* Look for the `namespace' keyword.  */
11971   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11972
11973   /* Get the name of the namespace.  We do not attempt to distinguish
11974      between an original-namespace-definition and an
11975      extension-namespace-definition at this point.  The semantic
11976      analysis routines are responsible for that.  */
11977   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11978     identifier = cp_parser_identifier (parser);
11979   else
11980     identifier = NULL_TREE;
11981
11982   /* Parse any specified attributes.  */
11983   attribs = cp_parser_attributes_opt (parser);
11984
11985   /* Look for the `{' to start the namespace.  */
11986   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11987   /* Start the namespace.  */
11988   push_namespace (identifier);
11989
11990   /* "inline namespace" is equivalent to a stub namespace definition
11991      followed by a strong using directive.  */
11992   if (is_inline)
11993     {
11994       tree name_space = current_namespace;
11995       /* Set up namespace association.  */
11996       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11997         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11998                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11999       /* Import the contents of the inline namespace.  */
12000       pop_namespace ();
12001       do_using_directive (name_space);
12002       push_namespace (identifier);
12003     }
12004
12005   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12006
12007   /* Parse the body of the namespace.  */
12008   cp_parser_namespace_body (parser);
12009
12010 #ifdef HANDLE_PRAGMA_VISIBILITY
12011   if (has_visibility)
12012     pop_visibility ();
12013 #endif
12014
12015   /* Finish the namespace.  */
12016   pop_namespace ();
12017   /* Look for the final `}'.  */
12018   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12019 }
12020
12021 /* Parse a namespace-body.
12022
12023    namespace-body:
12024      declaration-seq [opt]  */
12025
12026 static void
12027 cp_parser_namespace_body (cp_parser* parser)
12028 {
12029   cp_parser_declaration_seq_opt (parser);
12030 }
12031
12032 /* Parse a namespace-alias-definition.
12033
12034    namespace-alias-definition:
12035      namespace identifier = qualified-namespace-specifier ;  */
12036
12037 static void
12038 cp_parser_namespace_alias_definition (cp_parser* parser)
12039 {
12040   tree identifier;
12041   tree namespace_specifier;
12042
12043   cp_token *token = cp_lexer_peek_token (parser->lexer);
12044
12045   /* Look for the `namespace' keyword.  */
12046   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12047   /* Look for the identifier.  */
12048   identifier = cp_parser_identifier (parser);
12049   if (identifier == error_mark_node)
12050     return;
12051   /* Look for the `=' token.  */
12052   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12053       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12054     {
12055       error ("%H%<namespace%> definition is not allowed here", &token->location);
12056       /* Skip the definition.  */
12057       cp_lexer_consume_token (parser->lexer);
12058       if (cp_parser_skip_to_closing_brace (parser))
12059         cp_lexer_consume_token (parser->lexer);
12060       return;
12061     }
12062   cp_parser_require (parser, CPP_EQ, "%<=%>");
12063   /* Look for the qualified-namespace-specifier.  */
12064   namespace_specifier
12065     = cp_parser_qualified_namespace_specifier (parser);
12066   /* Look for the `;' token.  */
12067   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12068
12069   /* Register the alias in the symbol table.  */
12070   do_namespace_alias (identifier, namespace_specifier);
12071 }
12072
12073 /* Parse a qualified-namespace-specifier.
12074
12075    qualified-namespace-specifier:
12076      :: [opt] nested-name-specifier [opt] namespace-name
12077
12078    Returns a NAMESPACE_DECL corresponding to the specified
12079    namespace.  */
12080
12081 static tree
12082 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12083 {
12084   /* Look for the optional `::'.  */
12085   cp_parser_global_scope_opt (parser,
12086                               /*current_scope_valid_p=*/false);
12087
12088   /* Look for the optional nested-name-specifier.  */
12089   cp_parser_nested_name_specifier_opt (parser,
12090                                        /*typename_keyword_p=*/false,
12091                                        /*check_dependency_p=*/true,
12092                                        /*type_p=*/false,
12093                                        /*is_declaration=*/true);
12094
12095   return cp_parser_namespace_name (parser);
12096 }
12097
12098 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12099    access declaration.
12100
12101    using-declaration:
12102      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12103      using :: unqualified-id ;  
12104
12105    access-declaration:
12106      qualified-id ;  
12107
12108    */
12109
12110 static bool
12111 cp_parser_using_declaration (cp_parser* parser, 
12112                              bool access_declaration_p)
12113 {
12114   cp_token *token;
12115   bool typename_p = false;
12116   bool global_scope_p;
12117   tree decl;
12118   tree identifier;
12119   tree qscope;
12120
12121   if (access_declaration_p)
12122     cp_parser_parse_tentatively (parser);
12123   else
12124     {
12125       /* Look for the `using' keyword.  */
12126       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12127       
12128       /* Peek at the next token.  */
12129       token = cp_lexer_peek_token (parser->lexer);
12130       /* See if it's `typename'.  */
12131       if (token->keyword == RID_TYPENAME)
12132         {
12133           /* Remember that we've seen it.  */
12134           typename_p = true;
12135           /* Consume the `typename' token.  */
12136           cp_lexer_consume_token (parser->lexer);
12137         }
12138     }
12139
12140   /* Look for the optional global scope qualification.  */
12141   global_scope_p
12142     = (cp_parser_global_scope_opt (parser,
12143                                    /*current_scope_valid_p=*/false)
12144        != NULL_TREE);
12145
12146   /* If we saw `typename', or didn't see `::', then there must be a
12147      nested-name-specifier present.  */
12148   if (typename_p || !global_scope_p)
12149     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12150                                               /*check_dependency_p=*/true,
12151                                               /*type_p=*/false,
12152                                               /*is_declaration=*/true);
12153   /* Otherwise, we could be in either of the two productions.  In that
12154      case, treat the nested-name-specifier as optional.  */
12155   else
12156     qscope = cp_parser_nested_name_specifier_opt (parser,
12157                                                   /*typename_keyword_p=*/false,
12158                                                   /*check_dependency_p=*/true,
12159                                                   /*type_p=*/false,
12160                                                   /*is_declaration=*/true);
12161   if (!qscope)
12162     qscope = global_namespace;
12163
12164   if (access_declaration_p && cp_parser_error_occurred (parser))
12165     /* Something has already gone wrong; there's no need to parse
12166        further.  Since an error has occurred, the return value of
12167        cp_parser_parse_definitely will be false, as required.  */
12168     return cp_parser_parse_definitely (parser);
12169
12170   token = cp_lexer_peek_token (parser->lexer);
12171   /* Parse the unqualified-id.  */
12172   identifier = cp_parser_unqualified_id (parser,
12173                                          /*template_keyword_p=*/false,
12174                                          /*check_dependency_p=*/true,
12175                                          /*declarator_p=*/true,
12176                                          /*optional_p=*/false);
12177
12178   if (access_declaration_p)
12179     {
12180       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12181         cp_parser_simulate_error (parser);
12182       if (!cp_parser_parse_definitely (parser))
12183         return false;
12184     }
12185
12186   /* The function we call to handle a using-declaration is different
12187      depending on what scope we are in.  */
12188   if (qscope == error_mark_node || identifier == error_mark_node)
12189     ;
12190   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12191            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12192     /* [namespace.udecl]
12193
12194        A using declaration shall not name a template-id.  */
12195     error ("%Ha template-id may not appear in a using-declaration",
12196             &token->location);
12197   else
12198     {
12199       if (at_class_scope_p ())
12200         {
12201           /* Create the USING_DECL.  */
12202           decl = do_class_using_decl (parser->scope, identifier);
12203
12204           if (check_for_bare_parameter_packs (decl))
12205             return false;
12206           else
12207             /* Add it to the list of members in this class.  */
12208             finish_member_declaration (decl);
12209         }
12210       else
12211         {
12212           decl = cp_parser_lookup_name_simple (parser,
12213                                                identifier,
12214                                                token->location);
12215           if (decl == error_mark_node)
12216             cp_parser_name_lookup_error (parser, identifier,
12217                                          decl, NULL,
12218                                          token->location);
12219           else if (check_for_bare_parameter_packs (decl))
12220             return false;
12221           else if (!at_namespace_scope_p ())
12222             do_local_using_decl (decl, qscope, identifier);
12223           else
12224             do_toplevel_using_decl (decl, qscope, identifier);
12225         }
12226     }
12227
12228   /* Look for the final `;'.  */
12229   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12230   
12231   return true;
12232 }
12233
12234 /* Parse a using-directive.
12235
12236    using-directive:
12237      using namespace :: [opt] nested-name-specifier [opt]
12238        namespace-name ;  */
12239
12240 static void
12241 cp_parser_using_directive (cp_parser* parser)
12242 {
12243   tree namespace_decl;
12244   tree attribs;
12245
12246   /* Look for the `using' keyword.  */
12247   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12248   /* And the `namespace' keyword.  */
12249   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12250   /* Look for the optional `::' operator.  */
12251   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12252   /* And the optional nested-name-specifier.  */
12253   cp_parser_nested_name_specifier_opt (parser,
12254                                        /*typename_keyword_p=*/false,
12255                                        /*check_dependency_p=*/true,
12256                                        /*type_p=*/false,
12257                                        /*is_declaration=*/true);
12258   /* Get the namespace being used.  */
12259   namespace_decl = cp_parser_namespace_name (parser);
12260   /* And any specified attributes.  */
12261   attribs = cp_parser_attributes_opt (parser);
12262   /* Update the symbol table.  */
12263   parse_using_directive (namespace_decl, attribs);
12264   /* Look for the final `;'.  */
12265   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12266 }
12267
12268 /* Parse an asm-definition.
12269
12270    asm-definition:
12271      asm ( string-literal ) ;
12272
12273    GNU Extension:
12274
12275    asm-definition:
12276      asm volatile [opt] ( string-literal ) ;
12277      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12278      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12279                           : asm-operand-list [opt] ) ;
12280      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12281                           : asm-operand-list [opt]
12282                           : asm-operand-list [opt] ) ;  */
12283
12284 static void
12285 cp_parser_asm_definition (cp_parser* parser)
12286 {
12287   tree string;
12288   tree outputs = NULL_TREE;
12289   tree inputs = NULL_TREE;
12290   tree clobbers = NULL_TREE;
12291   tree asm_stmt;
12292   bool volatile_p = false;
12293   bool extended_p = false;
12294   bool invalid_inputs_p = false;
12295   bool invalid_outputs_p = false;
12296
12297   /* Look for the `asm' keyword.  */
12298   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12299   /* See if the next token is `volatile'.  */
12300   if (cp_parser_allow_gnu_extensions_p (parser)
12301       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12302     {
12303       /* Remember that we saw the `volatile' keyword.  */
12304       volatile_p = true;
12305       /* Consume the token.  */
12306       cp_lexer_consume_token (parser->lexer);
12307     }
12308   /* Look for the opening `('.  */
12309   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12310     return;
12311   /* Look for the string.  */
12312   string = cp_parser_string_literal (parser, false, false);
12313   if (string == error_mark_node)
12314     {
12315       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12316                                              /*consume_paren=*/true);
12317       return;
12318     }
12319
12320   /* If we're allowing GNU extensions, check for the extended assembly
12321      syntax.  Unfortunately, the `:' tokens need not be separated by
12322      a space in C, and so, for compatibility, we tolerate that here
12323      too.  Doing that means that we have to treat the `::' operator as
12324      two `:' tokens.  */
12325   if (cp_parser_allow_gnu_extensions_p (parser)
12326       && parser->in_function_body
12327       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12328           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12329     {
12330       bool inputs_p = false;
12331       bool clobbers_p = false;
12332
12333       /* The extended syntax was used.  */
12334       extended_p = true;
12335
12336       /* Look for outputs.  */
12337       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12338         {
12339           /* Consume the `:'.  */
12340           cp_lexer_consume_token (parser->lexer);
12341           /* Parse the output-operands.  */
12342           if (cp_lexer_next_token_is_not (parser->lexer,
12343                                           CPP_COLON)
12344               && cp_lexer_next_token_is_not (parser->lexer,
12345                                              CPP_SCOPE)
12346               && cp_lexer_next_token_is_not (parser->lexer,
12347                                              CPP_CLOSE_PAREN))
12348             outputs = cp_parser_asm_operand_list (parser);
12349
12350             if (outputs == error_mark_node)
12351               invalid_outputs_p = true;
12352         }
12353       /* If the next token is `::', there are no outputs, and the
12354          next token is the beginning of the inputs.  */
12355       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12356         /* The inputs are coming next.  */
12357         inputs_p = true;
12358
12359       /* Look for inputs.  */
12360       if (inputs_p
12361           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12362         {
12363           /* Consume the `:' or `::'.  */
12364           cp_lexer_consume_token (parser->lexer);
12365           /* Parse the output-operands.  */
12366           if (cp_lexer_next_token_is_not (parser->lexer,
12367                                           CPP_COLON)
12368               && cp_lexer_next_token_is_not (parser->lexer,
12369                                              CPP_CLOSE_PAREN))
12370             inputs = cp_parser_asm_operand_list (parser);
12371
12372             if (inputs == error_mark_node)
12373               invalid_inputs_p = true;
12374         }
12375       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12376         /* The clobbers are coming next.  */
12377         clobbers_p = true;
12378
12379       /* Look for clobbers.  */
12380       if (clobbers_p
12381           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12382         {
12383           /* Consume the `:' or `::'.  */
12384           cp_lexer_consume_token (parser->lexer);
12385           /* Parse the clobbers.  */
12386           if (cp_lexer_next_token_is_not (parser->lexer,
12387                                           CPP_CLOSE_PAREN))
12388             clobbers = cp_parser_asm_clobber_list (parser);
12389         }
12390     }
12391   /* Look for the closing `)'.  */
12392   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12393     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12394                                            /*consume_paren=*/true);
12395   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12396
12397   if (!invalid_inputs_p && !invalid_outputs_p)
12398     {
12399       /* Create the ASM_EXPR.  */
12400       if (parser->in_function_body)
12401         {
12402           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12403                                       inputs, clobbers);
12404           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12405           if (!extended_p)
12406             {
12407               tree temp = asm_stmt;
12408               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12409                 temp = TREE_OPERAND (temp, 0);
12410
12411               ASM_INPUT_P (temp) = 1;
12412             }
12413         }
12414       else
12415         cgraph_add_asm_node (string);
12416     }
12417 }
12418
12419 /* Declarators [gram.dcl.decl] */
12420
12421 /* Parse an init-declarator.
12422
12423    init-declarator:
12424      declarator initializer [opt]
12425
12426    GNU Extension:
12427
12428    init-declarator:
12429      declarator asm-specification [opt] attributes [opt] initializer [opt]
12430
12431    function-definition:
12432      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12433        function-body
12434      decl-specifier-seq [opt] declarator function-try-block
12435
12436    GNU Extension:
12437
12438    function-definition:
12439      __extension__ function-definition
12440
12441    The DECL_SPECIFIERS apply to this declarator.  Returns a
12442    representation of the entity declared.  If MEMBER_P is TRUE, then
12443    this declarator appears in a class scope.  The new DECL created by
12444    this declarator is returned.
12445
12446    The CHECKS are access checks that should be performed once we know
12447    what entity is being declared (and, therefore, what classes have
12448    befriended it).
12449
12450    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12451    for a function-definition here as well.  If the declarator is a
12452    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12453    be TRUE upon return.  By that point, the function-definition will
12454    have been completely parsed.
12455
12456    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12457    is FALSE.  */
12458
12459 static tree
12460 cp_parser_init_declarator (cp_parser* parser,
12461                            cp_decl_specifier_seq *decl_specifiers,
12462                            VEC (deferred_access_check,gc)* checks,
12463                            bool function_definition_allowed_p,
12464                            bool member_p,
12465                            int declares_class_or_enum,
12466                            bool* function_definition_p)
12467 {
12468   cp_token *token = NULL, *asm_spec_start_token = NULL,
12469            *attributes_start_token = NULL;
12470   cp_declarator *declarator;
12471   tree prefix_attributes;
12472   tree attributes;
12473   tree asm_specification;
12474   tree initializer;
12475   tree decl = NULL_TREE;
12476   tree scope;
12477   int is_initialized;
12478   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12479      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12480      "(...)".  */
12481   enum cpp_ttype initialization_kind;
12482   bool is_direct_init = false;
12483   bool is_non_constant_init;
12484   int ctor_dtor_or_conv_p;
12485   bool friend_p;
12486   tree pushed_scope = NULL;
12487
12488   /* Gather the attributes that were provided with the
12489      decl-specifiers.  */
12490   prefix_attributes = decl_specifiers->attributes;
12491
12492   /* Assume that this is not the declarator for a function
12493      definition.  */
12494   if (function_definition_p)
12495     *function_definition_p = false;
12496
12497   /* Defer access checks while parsing the declarator; we cannot know
12498      what names are accessible until we know what is being
12499      declared.  */
12500   resume_deferring_access_checks ();
12501
12502   /* Parse the declarator.  */
12503   token = cp_lexer_peek_token (parser->lexer);
12504   declarator
12505     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12506                             &ctor_dtor_or_conv_p,
12507                             /*parenthesized_p=*/NULL,
12508                             /*member_p=*/false);
12509   /* Gather up the deferred checks.  */
12510   stop_deferring_access_checks ();
12511
12512   /* If the DECLARATOR was erroneous, there's no need to go
12513      further.  */
12514   if (declarator == cp_error_declarator)
12515     return error_mark_node;
12516
12517   /* Check that the number of template-parameter-lists is OK.  */
12518   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12519                                                        token->location))
12520     return error_mark_node;
12521
12522   if (declares_class_or_enum & 2)
12523     cp_parser_check_for_definition_in_return_type (declarator,
12524                                                    decl_specifiers->type,
12525                                                    decl_specifiers->type_location);
12526
12527   /* Figure out what scope the entity declared by the DECLARATOR is
12528      located in.  `grokdeclarator' sometimes changes the scope, so
12529      we compute it now.  */
12530   scope = get_scope_of_declarator (declarator);
12531
12532   /* If we're allowing GNU extensions, look for an asm-specification
12533      and attributes.  */
12534   if (cp_parser_allow_gnu_extensions_p (parser))
12535     {
12536       /* Look for an asm-specification.  */
12537       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12538       asm_specification = cp_parser_asm_specification_opt (parser);
12539       /* And attributes.  */
12540       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12541       attributes = cp_parser_attributes_opt (parser);
12542     }
12543   else
12544     {
12545       asm_specification = NULL_TREE;
12546       attributes = NULL_TREE;
12547     }
12548
12549   /* Peek at the next token.  */
12550   token = cp_lexer_peek_token (parser->lexer);
12551   /* Check to see if the token indicates the start of a
12552      function-definition.  */
12553   if (function_declarator_p (declarator)
12554       && cp_parser_token_starts_function_definition_p (token))
12555     {
12556       if (!function_definition_allowed_p)
12557         {
12558           /* If a function-definition should not appear here, issue an
12559              error message.  */
12560           cp_parser_error (parser,
12561                            "a function-definition is not allowed here");
12562           return error_mark_node;
12563         }
12564       else
12565         {
12566           location_t func_brace_location
12567             = cp_lexer_peek_token (parser->lexer)->location;
12568
12569           /* Neither attributes nor an asm-specification are allowed
12570              on a function-definition.  */
12571           if (asm_specification)
12572             error ("%Han asm-specification is not allowed "
12573                    "on a function-definition",
12574                    &asm_spec_start_token->location);
12575           if (attributes)
12576             error ("%Hattributes are not allowed on a function-definition",
12577                    &attributes_start_token->location);
12578           /* This is a function-definition.  */
12579           *function_definition_p = true;
12580
12581           /* Parse the function definition.  */
12582           if (member_p)
12583             decl = cp_parser_save_member_function_body (parser,
12584                                                         decl_specifiers,
12585                                                         declarator,
12586                                                         prefix_attributes);
12587           else
12588             decl
12589               = (cp_parser_function_definition_from_specifiers_and_declarator
12590                  (parser, decl_specifiers, prefix_attributes, declarator));
12591
12592           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12593             {
12594               /* This is where the prologue starts...  */
12595               DECL_STRUCT_FUNCTION (decl)->function_start_locus
12596                 = func_brace_location;
12597             }
12598
12599           return decl;
12600         }
12601     }
12602
12603   /* [dcl.dcl]
12604
12605      Only in function declarations for constructors, destructors, and
12606      type conversions can the decl-specifier-seq be omitted.
12607
12608      We explicitly postpone this check past the point where we handle
12609      function-definitions because we tolerate function-definitions
12610      that are missing their return types in some modes.  */
12611   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12612     {
12613       cp_parser_error (parser,
12614                        "expected constructor, destructor, or type conversion");
12615       return error_mark_node;
12616     }
12617
12618   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12619   if (token->type == CPP_EQ
12620       || token->type == CPP_OPEN_PAREN
12621       || token->type == CPP_OPEN_BRACE)
12622     {
12623       is_initialized = SD_INITIALIZED;
12624       initialization_kind = token->type;
12625
12626       if (token->type == CPP_EQ
12627           && function_declarator_p (declarator))
12628         {
12629           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12630           if (t2->keyword == RID_DEFAULT)
12631             is_initialized = SD_DEFAULTED;
12632           else if (t2->keyword == RID_DELETE)
12633             is_initialized = SD_DELETED;
12634         }
12635     }
12636   else
12637     {
12638       /* If the init-declarator isn't initialized and isn't followed by a
12639          `,' or `;', it's not a valid init-declarator.  */
12640       if (token->type != CPP_COMMA
12641           && token->type != CPP_SEMICOLON)
12642         {
12643           cp_parser_error (parser, "expected initializer");
12644           return error_mark_node;
12645         }
12646       is_initialized = SD_UNINITIALIZED;
12647       initialization_kind = CPP_EOF;
12648     }
12649
12650   /* Because start_decl has side-effects, we should only call it if we
12651      know we're going ahead.  By this point, we know that we cannot
12652      possibly be looking at any other construct.  */
12653   cp_parser_commit_to_tentative_parse (parser);
12654
12655   /* If the decl specifiers were bad, issue an error now that we're
12656      sure this was intended to be a declarator.  Then continue
12657      declaring the variable(s), as int, to try to cut down on further
12658      errors.  */
12659   if (decl_specifiers->any_specifiers_p
12660       && decl_specifiers->type == error_mark_node)
12661     {
12662       cp_parser_error (parser, "invalid type in declaration");
12663       decl_specifiers->type = integer_type_node;
12664     }
12665
12666   /* Check to see whether or not this declaration is a friend.  */
12667   friend_p = cp_parser_friend_p (decl_specifiers);
12668
12669   /* Enter the newly declared entry in the symbol table.  If we're
12670      processing a declaration in a class-specifier, we wait until
12671      after processing the initializer.  */
12672   if (!member_p)
12673     {
12674       if (parser->in_unbraced_linkage_specification_p)
12675         decl_specifiers->storage_class = sc_extern;
12676       decl = start_decl (declarator, decl_specifiers,
12677                          is_initialized, attributes, prefix_attributes,
12678                          &pushed_scope);
12679     }
12680   else if (scope)
12681     /* Enter the SCOPE.  That way unqualified names appearing in the
12682        initializer will be looked up in SCOPE.  */
12683     pushed_scope = push_scope (scope);
12684
12685   /* Perform deferred access control checks, now that we know in which
12686      SCOPE the declared entity resides.  */
12687   if (!member_p && decl)
12688     {
12689       tree saved_current_function_decl = NULL_TREE;
12690
12691       /* If the entity being declared is a function, pretend that we
12692          are in its scope.  If it is a `friend', it may have access to
12693          things that would not otherwise be accessible.  */
12694       if (TREE_CODE (decl) == FUNCTION_DECL)
12695         {
12696           saved_current_function_decl = current_function_decl;
12697           current_function_decl = decl;
12698         }
12699
12700       /* Perform access checks for template parameters.  */
12701       cp_parser_perform_template_parameter_access_checks (checks);
12702
12703       /* Perform the access control checks for the declarator and the
12704          decl-specifiers.  */
12705       perform_deferred_access_checks ();
12706
12707       /* Restore the saved value.  */
12708       if (TREE_CODE (decl) == FUNCTION_DECL)
12709         current_function_decl = saved_current_function_decl;
12710     }
12711
12712   /* Parse the initializer.  */
12713   initializer = NULL_TREE;
12714   is_direct_init = false;
12715   is_non_constant_init = true;
12716   if (is_initialized)
12717     {
12718       if (function_declarator_p (declarator))
12719         {
12720           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12721            if (initialization_kind == CPP_EQ)
12722              initializer = cp_parser_pure_specifier (parser);
12723            else
12724              {
12725                /* If the declaration was erroneous, we don't really
12726                   know what the user intended, so just silently
12727                   consume the initializer.  */
12728                if (decl != error_mark_node)
12729                  error ("%Hinitializer provided for function",
12730                         &initializer_start_token->location);
12731                cp_parser_skip_to_closing_parenthesis (parser,
12732                                                       /*recovering=*/true,
12733                                                       /*or_comma=*/false,
12734                                                       /*consume_paren=*/true);
12735              }
12736         }
12737       else
12738         initializer = cp_parser_initializer (parser,
12739                                              &is_direct_init,
12740                                              &is_non_constant_init);
12741     }
12742
12743   /* The old parser allows attributes to appear after a parenthesized
12744      initializer.  Mark Mitchell proposed removing this functionality
12745      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12746      attributes -- but ignores them.  */
12747   if (cp_parser_allow_gnu_extensions_p (parser)
12748       && initialization_kind == CPP_OPEN_PAREN)
12749     if (cp_parser_attributes_opt (parser))
12750       warning (OPT_Wattributes,
12751                "attributes after parenthesized initializer ignored");
12752
12753   /* For an in-class declaration, use `grokfield' to create the
12754      declaration.  */
12755   if (member_p)
12756     {
12757       if (pushed_scope)
12758         {
12759           pop_scope (pushed_scope);
12760           pushed_scope = false;
12761         }
12762       decl = grokfield (declarator, decl_specifiers,
12763                         initializer, !is_non_constant_init,
12764                         /*asmspec=*/NULL_TREE,
12765                         prefix_attributes);
12766       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12767         cp_parser_save_default_args (parser, decl);
12768     }
12769
12770   /* Finish processing the declaration.  But, skip friend
12771      declarations.  */
12772   if (!friend_p && decl && decl != error_mark_node)
12773     {
12774       cp_finish_decl (decl,
12775                       initializer, !is_non_constant_init,
12776                       asm_specification,
12777                       /* If the initializer is in parentheses, then this is
12778                          a direct-initialization, which means that an
12779                          `explicit' constructor is OK.  Otherwise, an
12780                          `explicit' constructor cannot be used.  */
12781                       ((is_direct_init || !is_initialized)
12782                        ? 0 : LOOKUP_ONLYCONVERTING));
12783     }
12784   else if ((cxx_dialect != cxx98) && friend_p
12785            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12786     /* Core issue #226 (C++0x only): A default template-argument
12787        shall not be specified in a friend class template
12788        declaration. */
12789     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12790                              /*is_partial=*/0, /*is_friend_decl=*/1);
12791
12792   if (!friend_p && pushed_scope)
12793     pop_scope (pushed_scope);
12794
12795   return decl;
12796 }
12797
12798 /* Parse a declarator.
12799
12800    declarator:
12801      direct-declarator
12802      ptr-operator declarator
12803
12804    abstract-declarator:
12805      ptr-operator abstract-declarator [opt]
12806      direct-abstract-declarator
12807
12808    GNU Extensions:
12809
12810    declarator:
12811      attributes [opt] direct-declarator
12812      attributes [opt] ptr-operator declarator
12813
12814    abstract-declarator:
12815      attributes [opt] ptr-operator abstract-declarator [opt]
12816      attributes [opt] direct-abstract-declarator
12817
12818    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12819    detect constructor, destructor or conversion operators. It is set
12820    to -1 if the declarator is a name, and +1 if it is a
12821    function. Otherwise it is set to zero. Usually you just want to
12822    test for >0, but internally the negative value is used.
12823
12824    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12825    a decl-specifier-seq unless it declares a constructor, destructor,
12826    or conversion.  It might seem that we could check this condition in
12827    semantic analysis, rather than parsing, but that makes it difficult
12828    to handle something like `f()'.  We want to notice that there are
12829    no decl-specifiers, and therefore realize that this is an
12830    expression, not a declaration.)
12831
12832    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12833    the declarator is a direct-declarator of the form "(...)".
12834
12835    MEMBER_P is true iff this declarator is a member-declarator.  */
12836
12837 static cp_declarator *
12838 cp_parser_declarator (cp_parser* parser,
12839                       cp_parser_declarator_kind dcl_kind,
12840                       int* ctor_dtor_or_conv_p,
12841                       bool* parenthesized_p,
12842                       bool member_p)
12843 {
12844   cp_token *token;
12845   cp_declarator *declarator;
12846   enum tree_code code;
12847   cp_cv_quals cv_quals;
12848   tree class_type;
12849   tree attributes = NULL_TREE;
12850
12851   /* Assume this is not a constructor, destructor, or type-conversion
12852      operator.  */
12853   if (ctor_dtor_or_conv_p)
12854     *ctor_dtor_or_conv_p = 0;
12855
12856   if (cp_parser_allow_gnu_extensions_p (parser))
12857     attributes = cp_parser_attributes_opt (parser);
12858
12859   /* Peek at the next token.  */
12860   token = cp_lexer_peek_token (parser->lexer);
12861
12862   /* Check for the ptr-operator production.  */
12863   cp_parser_parse_tentatively (parser);
12864   /* Parse the ptr-operator.  */
12865   code = cp_parser_ptr_operator (parser,
12866                                  &class_type,
12867                                  &cv_quals);
12868   /* If that worked, then we have a ptr-operator.  */
12869   if (cp_parser_parse_definitely (parser))
12870     {
12871       /* If a ptr-operator was found, then this declarator was not
12872          parenthesized.  */
12873       if (parenthesized_p)
12874         *parenthesized_p = true;
12875       /* The dependent declarator is optional if we are parsing an
12876          abstract-declarator.  */
12877       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12878         cp_parser_parse_tentatively (parser);
12879
12880       /* Parse the dependent declarator.  */
12881       declarator = cp_parser_declarator (parser, dcl_kind,
12882                                          /*ctor_dtor_or_conv_p=*/NULL,
12883                                          /*parenthesized_p=*/NULL,
12884                                          /*member_p=*/false);
12885
12886       /* If we are parsing an abstract-declarator, we must handle the
12887          case where the dependent declarator is absent.  */
12888       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12889           && !cp_parser_parse_definitely (parser))
12890         declarator = NULL;
12891
12892       declarator = cp_parser_make_indirect_declarator
12893         (code, class_type, cv_quals, declarator);
12894     }
12895   /* Everything else is a direct-declarator.  */
12896   else
12897     {
12898       if (parenthesized_p)
12899         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12900                                                    CPP_OPEN_PAREN);
12901       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12902                                                 ctor_dtor_or_conv_p,
12903                                                 member_p);
12904     }
12905
12906   if (attributes && declarator && declarator != cp_error_declarator)
12907     declarator->attributes = attributes;
12908
12909   return declarator;
12910 }
12911
12912 /* Parse a direct-declarator or direct-abstract-declarator.
12913
12914    direct-declarator:
12915      declarator-id
12916      direct-declarator ( parameter-declaration-clause )
12917        cv-qualifier-seq [opt]
12918        exception-specification [opt]
12919      direct-declarator [ constant-expression [opt] ]
12920      ( declarator )
12921
12922    direct-abstract-declarator:
12923      direct-abstract-declarator [opt]
12924        ( parameter-declaration-clause )
12925        cv-qualifier-seq [opt]
12926        exception-specification [opt]
12927      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12928      ( abstract-declarator )
12929
12930    Returns a representation of the declarator.  DCL_KIND is
12931    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12932    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12933    we are parsing a direct-declarator.  It is
12934    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12935    of ambiguity we prefer an abstract declarator, as per
12936    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12937    cp_parser_declarator.  */
12938
12939 static cp_declarator *
12940 cp_parser_direct_declarator (cp_parser* parser,
12941                              cp_parser_declarator_kind dcl_kind,
12942                              int* ctor_dtor_or_conv_p,
12943                              bool member_p)
12944 {
12945   cp_token *token;
12946   cp_declarator *declarator = NULL;
12947   tree scope = NULL_TREE;
12948   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12949   bool saved_in_declarator_p = parser->in_declarator_p;
12950   bool first = true;
12951   tree pushed_scope = NULL_TREE;
12952
12953   while (true)
12954     {
12955       /* Peek at the next token.  */
12956       token = cp_lexer_peek_token (parser->lexer);
12957       if (token->type == CPP_OPEN_PAREN)
12958         {
12959           /* This is either a parameter-declaration-clause, or a
12960              parenthesized declarator. When we know we are parsing a
12961              named declarator, it must be a parenthesized declarator
12962              if FIRST is true. For instance, `(int)' is a
12963              parameter-declaration-clause, with an omitted
12964              direct-abstract-declarator. But `((*))', is a
12965              parenthesized abstract declarator. Finally, when T is a
12966              template parameter `(T)' is a
12967              parameter-declaration-clause, and not a parenthesized
12968              named declarator.
12969
12970              We first try and parse a parameter-declaration-clause,
12971              and then try a nested declarator (if FIRST is true).
12972
12973              It is not an error for it not to be a
12974              parameter-declaration-clause, even when FIRST is
12975              false. Consider,
12976
12977                int i (int);
12978                int i (3);
12979
12980              The first is the declaration of a function while the
12981              second is the definition of a variable, including its
12982              initializer.
12983
12984              Having seen only the parenthesis, we cannot know which of
12985              these two alternatives should be selected.  Even more
12986              complex are examples like:
12987
12988                int i (int (a));
12989                int i (int (3));
12990
12991              The former is a function-declaration; the latter is a
12992              variable initialization.
12993
12994              Thus again, we try a parameter-declaration-clause, and if
12995              that fails, we back out and return.  */
12996
12997           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12998             {
12999               tree params;
13000               unsigned saved_num_template_parameter_lists;
13001               bool is_declarator = false;
13002               tree t;
13003
13004               /* In a member-declarator, the only valid interpretation
13005                  of a parenthesis is the start of a
13006                  parameter-declaration-clause.  (It is invalid to
13007                  initialize a static data member with a parenthesized
13008                  initializer; only the "=" form of initialization is
13009                  permitted.)  */
13010               if (!member_p)
13011                 cp_parser_parse_tentatively (parser);
13012
13013               /* Consume the `('.  */
13014               cp_lexer_consume_token (parser->lexer);
13015               if (first)
13016                 {
13017                   /* If this is going to be an abstract declarator, we're
13018                      in a declarator and we can't have default args.  */
13019                   parser->default_arg_ok_p = false;
13020                   parser->in_declarator_p = true;
13021                 }
13022
13023               /* Inside the function parameter list, surrounding
13024                  template-parameter-lists do not apply.  */
13025               saved_num_template_parameter_lists
13026                 = parser->num_template_parameter_lists;
13027               parser->num_template_parameter_lists = 0;
13028
13029               begin_scope (sk_function_parms, NULL_TREE);
13030
13031               /* Parse the parameter-declaration-clause.  */
13032               params = cp_parser_parameter_declaration_clause (parser);
13033
13034               parser->num_template_parameter_lists
13035                 = saved_num_template_parameter_lists;
13036
13037               /* If all went well, parse the cv-qualifier-seq and the
13038                  exception-specification.  */
13039               if (member_p || cp_parser_parse_definitely (parser))
13040                 {
13041                   cp_cv_quals cv_quals;
13042                   tree exception_specification;
13043                   tree late_return;
13044
13045                   is_declarator = true;
13046
13047                   if (ctor_dtor_or_conv_p)
13048                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13049                   first = false;
13050                   /* Consume the `)'.  */
13051                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13052
13053                   /* Parse the cv-qualifier-seq.  */
13054                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13055                   /* And the exception-specification.  */
13056                   exception_specification
13057                     = cp_parser_exception_specification_opt (parser);
13058
13059                   late_return
13060                     = cp_parser_late_return_type_opt (parser);
13061
13062                   /* Create the function-declarator.  */
13063                   declarator = make_call_declarator (declarator,
13064                                                      params,
13065                                                      cv_quals,
13066                                                      exception_specification,
13067                                                      late_return);
13068                   /* Any subsequent parameter lists are to do with
13069                      return type, so are not those of the declared
13070                      function.  */
13071                   parser->default_arg_ok_p = false;
13072                 }
13073
13074               /* Remove the function parms from scope.  */
13075               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13076                 pop_binding (DECL_NAME (t), t);
13077               leave_scope();
13078
13079               if (is_declarator)
13080                 /* Repeat the main loop.  */
13081                 continue;
13082             }
13083
13084           /* If this is the first, we can try a parenthesized
13085              declarator.  */
13086           if (first)
13087             {
13088               bool saved_in_type_id_in_expr_p;
13089
13090               parser->default_arg_ok_p = saved_default_arg_ok_p;
13091               parser->in_declarator_p = saved_in_declarator_p;
13092
13093               /* Consume the `('.  */
13094               cp_lexer_consume_token (parser->lexer);
13095               /* Parse the nested declarator.  */
13096               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13097               parser->in_type_id_in_expr_p = true;
13098               declarator
13099                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13100                                         /*parenthesized_p=*/NULL,
13101                                         member_p);
13102               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13103               first = false;
13104               /* Expect a `)'.  */
13105               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13106                 declarator = cp_error_declarator;
13107               if (declarator == cp_error_declarator)
13108                 break;
13109
13110               goto handle_declarator;
13111             }
13112           /* Otherwise, we must be done.  */
13113           else
13114             break;
13115         }
13116       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13117                && token->type == CPP_OPEN_SQUARE)
13118         {
13119           /* Parse an array-declarator.  */
13120           tree bounds;
13121
13122           if (ctor_dtor_or_conv_p)
13123             *ctor_dtor_or_conv_p = 0;
13124
13125           first = false;
13126           parser->default_arg_ok_p = false;
13127           parser->in_declarator_p = true;
13128           /* Consume the `['.  */
13129           cp_lexer_consume_token (parser->lexer);
13130           /* Peek at the next token.  */
13131           token = cp_lexer_peek_token (parser->lexer);
13132           /* If the next token is `]', then there is no
13133              constant-expression.  */
13134           if (token->type != CPP_CLOSE_SQUARE)
13135             {
13136               bool non_constant_p;
13137
13138               bounds
13139                 = cp_parser_constant_expression (parser,
13140                                                  /*allow_non_constant=*/true,
13141                                                  &non_constant_p);
13142               if (!non_constant_p)
13143                 bounds = fold_non_dependent_expr (bounds);
13144               /* Normally, the array bound must be an integral constant
13145                  expression.  However, as an extension, we allow VLAs
13146                  in function scopes.  */
13147               else if (!parser->in_function_body)
13148                 {
13149                   error ("%Harray bound is not an integer constant",
13150                          &token->location);
13151                   bounds = error_mark_node;
13152                 }
13153             }
13154           else
13155             bounds = NULL_TREE;
13156           /* Look for the closing `]'.  */
13157           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13158             {
13159               declarator = cp_error_declarator;
13160               break;
13161             }
13162
13163           declarator = make_array_declarator (declarator, bounds);
13164         }
13165       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13166         {
13167           tree qualifying_scope;
13168           tree unqualified_name;
13169           special_function_kind sfk;
13170           bool abstract_ok;
13171           bool pack_expansion_p = false;
13172           cp_token *declarator_id_start_token;
13173
13174           /* Parse a declarator-id */
13175           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13176           if (abstract_ok)
13177             {
13178               cp_parser_parse_tentatively (parser);
13179
13180               /* If we see an ellipsis, we should be looking at a
13181                  parameter pack. */
13182               if (token->type == CPP_ELLIPSIS)
13183                 {
13184                   /* Consume the `...' */
13185                   cp_lexer_consume_token (parser->lexer);
13186
13187                   pack_expansion_p = true;
13188                 }
13189             }
13190
13191           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13192           unqualified_name
13193             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13194           qualifying_scope = parser->scope;
13195           if (abstract_ok)
13196             {
13197               bool okay = false;
13198
13199               if (!unqualified_name && pack_expansion_p)
13200                 {
13201                   /* Check whether an error occurred. */
13202                   okay = !cp_parser_error_occurred (parser);
13203
13204                   /* We already consumed the ellipsis to mark a
13205                      parameter pack, but we have no way to report it,
13206                      so abort the tentative parse. We will be exiting
13207                      immediately anyway. */
13208                   cp_parser_abort_tentative_parse (parser);
13209                 }
13210               else
13211                 okay = cp_parser_parse_definitely (parser);
13212
13213               if (!okay)
13214                 unqualified_name = error_mark_node;
13215               else if (unqualified_name
13216                        && (qualifying_scope
13217                            || (TREE_CODE (unqualified_name)
13218                                != IDENTIFIER_NODE)))
13219                 {
13220                   cp_parser_error (parser, "expected unqualified-id");
13221                   unqualified_name = error_mark_node;
13222                 }
13223             }
13224
13225           if (!unqualified_name)
13226             return NULL;
13227           if (unqualified_name == error_mark_node)
13228             {
13229               declarator = cp_error_declarator;
13230               pack_expansion_p = false;
13231               declarator->parameter_pack_p = false;
13232               break;
13233             }
13234
13235           if (qualifying_scope && at_namespace_scope_p ()
13236               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13237             {
13238               /* In the declaration of a member of a template class
13239                  outside of the class itself, the SCOPE will sometimes
13240                  be a TYPENAME_TYPE.  For example, given:
13241
13242                  template <typename T>
13243                  int S<T>::R::i = 3;
13244
13245                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13246                  this context, we must resolve S<T>::R to an ordinary
13247                  type, rather than a typename type.
13248
13249                  The reason we normally avoid resolving TYPENAME_TYPEs
13250                  is that a specialization of `S' might render
13251                  `S<T>::R' not a type.  However, if `S' is
13252                  specialized, then this `i' will not be used, so there
13253                  is no harm in resolving the types here.  */
13254               tree type;
13255
13256               /* Resolve the TYPENAME_TYPE.  */
13257               type = resolve_typename_type (qualifying_scope,
13258                                             /*only_current_p=*/false);
13259               /* If that failed, the declarator is invalid.  */
13260               if (TREE_CODE (type) == TYPENAME_TYPE)
13261                 error ("%H%<%T::%E%> is not a type",
13262                        &declarator_id_start_token->location,
13263                        TYPE_CONTEXT (qualifying_scope),
13264                        TYPE_IDENTIFIER (qualifying_scope));
13265               qualifying_scope = type;
13266             }
13267
13268           sfk = sfk_none;
13269
13270           if (unqualified_name)
13271             {
13272               tree class_type;
13273
13274               if (qualifying_scope
13275                   && CLASS_TYPE_P (qualifying_scope))
13276                 class_type = qualifying_scope;
13277               else
13278                 class_type = current_class_type;
13279
13280               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13281                 {
13282                   tree name_type = TREE_TYPE (unqualified_name);
13283                   if (class_type && same_type_p (name_type, class_type))
13284                     {
13285                       if (qualifying_scope
13286                           && CLASSTYPE_USE_TEMPLATE (name_type))
13287                         {
13288                           error ("%Hinvalid use of constructor as a template",
13289                                  &declarator_id_start_token->location);
13290                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13291                                   "name the constructor in a qualified name",
13292                                   class_type,
13293                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13294                                   class_type, name_type);
13295                           declarator = cp_error_declarator;
13296                           break;
13297                         }
13298                       else
13299                         unqualified_name = constructor_name (class_type);
13300                     }
13301                   else
13302                     {
13303                       /* We do not attempt to print the declarator
13304                          here because we do not have enough
13305                          information about its original syntactic
13306                          form.  */
13307                       cp_parser_error (parser, "invalid declarator");
13308                       declarator = cp_error_declarator;
13309                       break;
13310                     }
13311                 }
13312
13313               if (class_type)
13314                 {
13315                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13316                     sfk = sfk_destructor;
13317                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13318                     sfk = sfk_conversion;
13319                   else if (/* There's no way to declare a constructor
13320                               for an anonymous type, even if the type
13321                               got a name for linkage purposes.  */
13322                            !TYPE_WAS_ANONYMOUS (class_type)
13323                            && constructor_name_p (unqualified_name,
13324                                                   class_type))
13325                     {
13326                       unqualified_name = constructor_name (class_type);
13327                       sfk = sfk_constructor;
13328                     }
13329
13330                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13331                     *ctor_dtor_or_conv_p = -1;
13332                 }
13333             }
13334           declarator = make_id_declarator (qualifying_scope,
13335                                            unqualified_name,
13336                                            sfk);
13337           declarator->id_loc = token->location;
13338           declarator->parameter_pack_p = pack_expansion_p;
13339
13340           if (pack_expansion_p)
13341             maybe_warn_variadic_templates ();
13342
13343         handle_declarator:;
13344           scope = get_scope_of_declarator (declarator);
13345           if (scope)
13346             /* Any names that appear after the declarator-id for a
13347                member are looked up in the containing scope.  */
13348             pushed_scope = push_scope (scope);
13349           parser->in_declarator_p = true;
13350           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13351               || (declarator && declarator->kind == cdk_id))
13352             /* Default args are only allowed on function
13353                declarations.  */
13354             parser->default_arg_ok_p = saved_default_arg_ok_p;
13355           else
13356             parser->default_arg_ok_p = false;
13357
13358           first = false;
13359         }
13360       /* We're done.  */
13361       else
13362         break;
13363     }
13364
13365   /* For an abstract declarator, we might wind up with nothing at this
13366      point.  That's an error; the declarator is not optional.  */
13367   if (!declarator)
13368     cp_parser_error (parser, "expected declarator");
13369
13370   /* If we entered a scope, we must exit it now.  */
13371   if (pushed_scope)
13372     pop_scope (pushed_scope);
13373
13374   parser->default_arg_ok_p = saved_default_arg_ok_p;
13375   parser->in_declarator_p = saved_in_declarator_p;
13376
13377   return declarator;
13378 }
13379
13380 /* Parse a ptr-operator.
13381
13382    ptr-operator:
13383      * cv-qualifier-seq [opt]
13384      &
13385      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13386
13387    GNU Extension:
13388
13389    ptr-operator:
13390      & cv-qualifier-seq [opt]
13391
13392    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13393    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13394    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13395    filled in with the TYPE containing the member.  *CV_QUALS is
13396    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13397    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13398    Note that the tree codes returned by this function have nothing
13399    to do with the types of trees that will be eventually be created
13400    to represent the pointer or reference type being parsed. They are
13401    just constants with suggestive names. */
13402 static enum tree_code
13403 cp_parser_ptr_operator (cp_parser* parser,
13404                         tree* type,
13405                         cp_cv_quals *cv_quals)
13406 {
13407   enum tree_code code = ERROR_MARK;
13408   cp_token *token;
13409
13410   /* Assume that it's not a pointer-to-member.  */
13411   *type = NULL_TREE;
13412   /* And that there are no cv-qualifiers.  */
13413   *cv_quals = TYPE_UNQUALIFIED;
13414
13415   /* Peek at the next token.  */
13416   token = cp_lexer_peek_token (parser->lexer);
13417
13418   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13419   if (token->type == CPP_MULT)
13420     code = INDIRECT_REF;
13421   else if (token->type == CPP_AND)
13422     code = ADDR_EXPR;
13423   else if ((cxx_dialect != cxx98) &&
13424            token->type == CPP_AND_AND) /* C++0x only */
13425     code = NON_LVALUE_EXPR;
13426
13427   if (code != ERROR_MARK)
13428     {
13429       /* Consume the `*', `&' or `&&'.  */
13430       cp_lexer_consume_token (parser->lexer);
13431
13432       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13433          `&', if we are allowing GNU extensions.  (The only qualifier
13434          that can legally appear after `&' is `restrict', but that is
13435          enforced during semantic analysis.  */
13436       if (code == INDIRECT_REF
13437           || cp_parser_allow_gnu_extensions_p (parser))
13438         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13439     }
13440   else
13441     {
13442       /* Try the pointer-to-member case.  */
13443       cp_parser_parse_tentatively (parser);
13444       /* Look for the optional `::' operator.  */
13445       cp_parser_global_scope_opt (parser,
13446                                   /*current_scope_valid_p=*/false);
13447       /* Look for the nested-name specifier.  */
13448       token = cp_lexer_peek_token (parser->lexer);
13449       cp_parser_nested_name_specifier (parser,
13450                                        /*typename_keyword_p=*/false,
13451                                        /*check_dependency_p=*/true,
13452                                        /*type_p=*/false,
13453                                        /*is_declaration=*/false);
13454       /* If we found it, and the next token is a `*', then we are
13455          indeed looking at a pointer-to-member operator.  */
13456       if (!cp_parser_error_occurred (parser)
13457           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13458         {
13459           /* Indicate that the `*' operator was used.  */
13460           code = INDIRECT_REF;
13461
13462           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13463             error ("%H%qD is a namespace", &token->location, parser->scope);
13464           else
13465             {
13466               /* The type of which the member is a member is given by the
13467                  current SCOPE.  */
13468               *type = parser->scope;
13469               /* The next name will not be qualified.  */
13470               parser->scope = NULL_TREE;
13471               parser->qualifying_scope = NULL_TREE;
13472               parser->object_scope = NULL_TREE;
13473               /* Look for the optional cv-qualifier-seq.  */
13474               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13475             }
13476         }
13477       /* If that didn't work we don't have a ptr-operator.  */
13478       if (!cp_parser_parse_definitely (parser))
13479         cp_parser_error (parser, "expected ptr-operator");
13480     }
13481
13482   return code;
13483 }
13484
13485 /* Parse an (optional) cv-qualifier-seq.
13486
13487    cv-qualifier-seq:
13488      cv-qualifier cv-qualifier-seq [opt]
13489
13490    cv-qualifier:
13491      const
13492      volatile
13493
13494    GNU Extension:
13495
13496    cv-qualifier:
13497      __restrict__
13498
13499    Returns a bitmask representing the cv-qualifiers.  */
13500
13501 static cp_cv_quals
13502 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13503 {
13504   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13505
13506   while (true)
13507     {
13508       cp_token *token;
13509       cp_cv_quals cv_qualifier;
13510
13511       /* Peek at the next token.  */
13512       token = cp_lexer_peek_token (parser->lexer);
13513       /* See if it's a cv-qualifier.  */
13514       switch (token->keyword)
13515         {
13516         case RID_CONST:
13517           cv_qualifier = TYPE_QUAL_CONST;
13518           break;
13519
13520         case RID_VOLATILE:
13521           cv_qualifier = TYPE_QUAL_VOLATILE;
13522           break;
13523
13524         case RID_RESTRICT:
13525           cv_qualifier = TYPE_QUAL_RESTRICT;
13526           break;
13527
13528         default:
13529           cv_qualifier = TYPE_UNQUALIFIED;
13530           break;
13531         }
13532
13533       if (!cv_qualifier)
13534         break;
13535
13536       if (cv_quals & cv_qualifier)
13537         {
13538           error ("%Hduplicate cv-qualifier", &token->location);
13539           cp_lexer_purge_token (parser->lexer);
13540         }
13541       else
13542         {
13543           cp_lexer_consume_token (parser->lexer);
13544           cv_quals |= cv_qualifier;
13545         }
13546     }
13547
13548   return cv_quals;
13549 }
13550
13551 /* Parse a late-specified return type, if any.  This is not a separate
13552    non-terminal, but part of a function declarator, which looks like
13553
13554    -> type-id
13555
13556    Returns the type indicated by the type-id.  */
13557
13558 static tree
13559 cp_parser_late_return_type_opt (cp_parser* parser)
13560 {
13561   cp_token *token;
13562
13563   /* Peek at the next token.  */
13564   token = cp_lexer_peek_token (parser->lexer);
13565   /* A late-specified return type is indicated by an initial '->'. */
13566   if (token->type != CPP_DEREF)
13567     return NULL_TREE;
13568
13569   /* Consume the ->.  */
13570   cp_lexer_consume_token (parser->lexer);
13571
13572   return cp_parser_type_id (parser);
13573 }
13574
13575 /* Parse a declarator-id.
13576
13577    declarator-id:
13578      id-expression
13579      :: [opt] nested-name-specifier [opt] type-name
13580
13581    In the `id-expression' case, the value returned is as for
13582    cp_parser_id_expression if the id-expression was an unqualified-id.
13583    If the id-expression was a qualified-id, then a SCOPE_REF is
13584    returned.  The first operand is the scope (either a NAMESPACE_DECL
13585    or TREE_TYPE), but the second is still just a representation of an
13586    unqualified-id.  */
13587
13588 static tree
13589 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13590 {
13591   tree id;
13592   /* The expression must be an id-expression.  Assume that qualified
13593      names are the names of types so that:
13594
13595        template <class T>
13596        int S<T>::R::i = 3;
13597
13598      will work; we must treat `S<T>::R' as the name of a type.
13599      Similarly, assume that qualified names are templates, where
13600      required, so that:
13601
13602        template <class T>
13603        int S<T>::R<T>::i = 3;
13604
13605      will work, too.  */
13606   id = cp_parser_id_expression (parser,
13607                                 /*template_keyword_p=*/false,
13608                                 /*check_dependency_p=*/false,
13609                                 /*template_p=*/NULL,
13610                                 /*declarator_p=*/true,
13611                                 optional_p);
13612   if (id && BASELINK_P (id))
13613     id = BASELINK_FUNCTIONS (id);
13614   return id;
13615 }
13616
13617 /* Parse a type-id.
13618
13619    type-id:
13620      type-specifier-seq abstract-declarator [opt]
13621
13622    Returns the TYPE specified.  */
13623
13624 static tree
13625 cp_parser_type_id (cp_parser* parser)
13626 {
13627   cp_decl_specifier_seq type_specifier_seq;
13628   cp_declarator *abstract_declarator;
13629
13630   /* Parse the type-specifier-seq.  */
13631   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13632                                 &type_specifier_seq);
13633   if (type_specifier_seq.type == error_mark_node)
13634     return error_mark_node;
13635
13636   /* There might or might not be an abstract declarator.  */
13637   cp_parser_parse_tentatively (parser);
13638   /* Look for the declarator.  */
13639   abstract_declarator
13640     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13641                             /*parenthesized_p=*/NULL,
13642                             /*member_p=*/false);
13643   /* Check to see if there really was a declarator.  */
13644   if (!cp_parser_parse_definitely (parser))
13645     abstract_declarator = NULL;
13646
13647   return groktypename (&type_specifier_seq, abstract_declarator);
13648 }
13649
13650 /* Parse a type-specifier-seq.
13651
13652    type-specifier-seq:
13653      type-specifier type-specifier-seq [opt]
13654
13655    GNU extension:
13656
13657    type-specifier-seq:
13658      attributes type-specifier-seq [opt]
13659
13660    If IS_CONDITION is true, we are at the start of a "condition",
13661    e.g., we've just seen "if (".
13662
13663    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13664
13665 static void
13666 cp_parser_type_specifier_seq (cp_parser* parser,
13667                               bool is_condition,
13668                               cp_decl_specifier_seq *type_specifier_seq)
13669 {
13670   bool seen_type_specifier = false;
13671   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13672   cp_token *start_token = NULL;
13673
13674   /* Clear the TYPE_SPECIFIER_SEQ.  */
13675   clear_decl_specs (type_specifier_seq);
13676
13677   /* Parse the type-specifiers and attributes.  */
13678   while (true)
13679     {
13680       tree type_specifier;
13681       bool is_cv_qualifier;
13682
13683       /* Check for attributes first.  */
13684       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13685         {
13686           type_specifier_seq->attributes =
13687             chainon (type_specifier_seq->attributes,
13688                      cp_parser_attributes_opt (parser));
13689           continue;
13690         }
13691
13692       /* record the token of the beginning of the type specifier seq,
13693          for error reporting purposes*/
13694      if (!start_token)
13695        start_token = cp_lexer_peek_token (parser->lexer);
13696
13697       /* Look for the type-specifier.  */
13698       type_specifier = cp_parser_type_specifier (parser,
13699                                                  flags,
13700                                                  type_specifier_seq,
13701                                                  /*is_declaration=*/false,
13702                                                  NULL,
13703                                                  &is_cv_qualifier);
13704       if (!type_specifier)
13705         {
13706           /* If the first type-specifier could not be found, this is not a
13707              type-specifier-seq at all.  */
13708           if (!seen_type_specifier)
13709             {
13710               cp_parser_error (parser, "expected type-specifier");
13711               type_specifier_seq->type = error_mark_node;
13712               return;
13713             }
13714           /* If subsequent type-specifiers could not be found, the
13715              type-specifier-seq is complete.  */
13716           break;
13717         }
13718
13719       seen_type_specifier = true;
13720       /* The standard says that a condition can be:
13721
13722             type-specifier-seq declarator = assignment-expression
13723
13724          However, given:
13725
13726            struct S {};
13727            if (int S = ...)
13728
13729          we should treat the "S" as a declarator, not as a
13730          type-specifier.  The standard doesn't say that explicitly for
13731          type-specifier-seq, but it does say that for
13732          decl-specifier-seq in an ordinary declaration.  Perhaps it
13733          would be clearer just to allow a decl-specifier-seq here, and
13734          then add a semantic restriction that if any decl-specifiers
13735          that are not type-specifiers appear, the program is invalid.  */
13736       if (is_condition && !is_cv_qualifier)
13737         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13738     }
13739
13740   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13741 }
13742
13743 /* Parse a parameter-declaration-clause.
13744
13745    parameter-declaration-clause:
13746      parameter-declaration-list [opt] ... [opt]
13747      parameter-declaration-list , ...
13748
13749    Returns a representation for the parameter declarations.  A return
13750    value of NULL indicates a parameter-declaration-clause consisting
13751    only of an ellipsis.  */
13752
13753 static tree
13754 cp_parser_parameter_declaration_clause (cp_parser* parser)
13755 {
13756   tree parameters;
13757   cp_token *token;
13758   bool ellipsis_p;
13759   bool is_error;
13760
13761   /* Peek at the next token.  */
13762   token = cp_lexer_peek_token (parser->lexer);
13763   /* Check for trivial parameter-declaration-clauses.  */
13764   if (token->type == CPP_ELLIPSIS)
13765     {
13766       /* Consume the `...' token.  */
13767       cp_lexer_consume_token (parser->lexer);
13768       return NULL_TREE;
13769     }
13770   else if (token->type == CPP_CLOSE_PAREN)
13771     /* There are no parameters.  */
13772     {
13773 #ifndef NO_IMPLICIT_EXTERN_C
13774       if (in_system_header && current_class_type == NULL
13775           && current_lang_name == lang_name_c)
13776         return NULL_TREE;
13777       else
13778 #endif
13779         return void_list_node;
13780     }
13781   /* Check for `(void)', too, which is a special case.  */
13782   else if (token->keyword == RID_VOID
13783            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13784                == CPP_CLOSE_PAREN))
13785     {
13786       /* Consume the `void' token.  */
13787       cp_lexer_consume_token (parser->lexer);
13788       /* There are no parameters.  */
13789       return void_list_node;
13790     }
13791
13792   /* Parse the parameter-declaration-list.  */
13793   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13794   /* If a parse error occurred while parsing the
13795      parameter-declaration-list, then the entire
13796      parameter-declaration-clause is erroneous.  */
13797   if (is_error)
13798     return NULL;
13799
13800   /* Peek at the next token.  */
13801   token = cp_lexer_peek_token (parser->lexer);
13802   /* If it's a `,', the clause should terminate with an ellipsis.  */
13803   if (token->type == CPP_COMMA)
13804     {
13805       /* Consume the `,'.  */
13806       cp_lexer_consume_token (parser->lexer);
13807       /* Expect an ellipsis.  */
13808       ellipsis_p
13809         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13810     }
13811   /* It might also be `...' if the optional trailing `,' was
13812      omitted.  */
13813   else if (token->type == CPP_ELLIPSIS)
13814     {
13815       /* Consume the `...' token.  */
13816       cp_lexer_consume_token (parser->lexer);
13817       /* And remember that we saw it.  */
13818       ellipsis_p = true;
13819     }
13820   else
13821     ellipsis_p = false;
13822
13823   /* Finish the parameter list.  */
13824   if (!ellipsis_p)
13825     parameters = chainon (parameters, void_list_node);
13826
13827   return parameters;
13828 }
13829
13830 /* Parse a parameter-declaration-list.
13831
13832    parameter-declaration-list:
13833      parameter-declaration
13834      parameter-declaration-list , parameter-declaration
13835
13836    Returns a representation of the parameter-declaration-list, as for
13837    cp_parser_parameter_declaration_clause.  However, the
13838    `void_list_node' is never appended to the list.  Upon return,
13839    *IS_ERROR will be true iff an error occurred.  */
13840
13841 static tree
13842 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13843 {
13844   tree parameters = NULL_TREE;
13845   tree *tail = &parameters; 
13846   bool saved_in_unbraced_linkage_specification_p;
13847
13848   /* Assume all will go well.  */
13849   *is_error = false;
13850   /* The special considerations that apply to a function within an
13851      unbraced linkage specifications do not apply to the parameters
13852      to the function.  */
13853   saved_in_unbraced_linkage_specification_p 
13854     = parser->in_unbraced_linkage_specification_p;
13855   parser->in_unbraced_linkage_specification_p = false;
13856
13857   /* Look for more parameters.  */
13858   while (true)
13859     {
13860       cp_parameter_declarator *parameter;
13861       tree decl = error_mark_node;
13862       bool parenthesized_p;
13863       /* Parse the parameter.  */
13864       parameter
13865         = cp_parser_parameter_declaration (parser,
13866                                            /*template_parm_p=*/false,
13867                                            &parenthesized_p);
13868
13869       /* We don't know yet if the enclosing context is deprecated, so wait
13870          and warn in grokparms if appropriate.  */
13871       deprecated_state = DEPRECATED_SUPPRESS;
13872
13873       if (parameter)
13874         decl = grokdeclarator (parameter->declarator,
13875                                &parameter->decl_specifiers,
13876                                PARM,
13877                                parameter->default_argument != NULL_TREE,
13878                                &parameter->decl_specifiers.attributes);
13879
13880       deprecated_state = DEPRECATED_NORMAL;
13881
13882       /* If a parse error occurred parsing the parameter declaration,
13883          then the entire parameter-declaration-list is erroneous.  */
13884       if (decl == error_mark_node)
13885         {
13886           *is_error = true;
13887           parameters = error_mark_node;
13888           break;
13889         }
13890
13891       if (parameter->decl_specifiers.attributes)
13892         cplus_decl_attributes (&decl,
13893                                parameter->decl_specifiers.attributes,
13894                                0);
13895       if (DECL_NAME (decl))
13896         decl = pushdecl (decl);
13897
13898       /* Add the new parameter to the list.  */
13899       *tail = build_tree_list (parameter->default_argument, decl);
13900       tail = &TREE_CHAIN (*tail);
13901
13902       /* Peek at the next token.  */
13903       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13904           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13905           /* These are for Objective-C++ */
13906           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13907           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13908         /* The parameter-declaration-list is complete.  */
13909         break;
13910       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13911         {
13912           cp_token *token;
13913
13914           /* Peek at the next token.  */
13915           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13916           /* If it's an ellipsis, then the list is complete.  */
13917           if (token->type == CPP_ELLIPSIS)
13918             break;
13919           /* Otherwise, there must be more parameters.  Consume the
13920              `,'.  */
13921           cp_lexer_consume_token (parser->lexer);
13922           /* When parsing something like:
13923
13924                 int i(float f, double d)
13925
13926              we can tell after seeing the declaration for "f" that we
13927              are not looking at an initialization of a variable "i",
13928              but rather at the declaration of a function "i".
13929
13930              Due to the fact that the parsing of template arguments
13931              (as specified to a template-id) requires backtracking we
13932              cannot use this technique when inside a template argument
13933              list.  */
13934           if (!parser->in_template_argument_list_p
13935               && !parser->in_type_id_in_expr_p
13936               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13937               /* However, a parameter-declaration of the form
13938                  "foat(f)" (which is a valid declaration of a
13939                  parameter "f") can also be interpreted as an
13940                  expression (the conversion of "f" to "float").  */
13941               && !parenthesized_p)
13942             cp_parser_commit_to_tentative_parse (parser);
13943         }
13944       else
13945         {
13946           cp_parser_error (parser, "expected %<,%> or %<...%>");
13947           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13948             cp_parser_skip_to_closing_parenthesis (parser,
13949                                                    /*recovering=*/true,
13950                                                    /*or_comma=*/false,
13951                                                    /*consume_paren=*/false);
13952           break;
13953         }
13954     }
13955
13956   parser->in_unbraced_linkage_specification_p
13957     = saved_in_unbraced_linkage_specification_p;
13958
13959   return parameters;
13960 }
13961
13962 /* Parse a parameter declaration.
13963
13964    parameter-declaration:
13965      decl-specifier-seq ... [opt] declarator
13966      decl-specifier-seq declarator = assignment-expression
13967      decl-specifier-seq ... [opt] abstract-declarator [opt]
13968      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13969
13970    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13971    declares a template parameter.  (In that case, a non-nested `>'
13972    token encountered during the parsing of the assignment-expression
13973    is not interpreted as a greater-than operator.)
13974
13975    Returns a representation of the parameter, or NULL if an error
13976    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13977    true iff the declarator is of the form "(p)".  */
13978
13979 static cp_parameter_declarator *
13980 cp_parser_parameter_declaration (cp_parser *parser,
13981                                  bool template_parm_p,
13982                                  bool *parenthesized_p)
13983 {
13984   int declares_class_or_enum;
13985   bool greater_than_is_operator_p;
13986   cp_decl_specifier_seq decl_specifiers;
13987   cp_declarator *declarator;
13988   tree default_argument;
13989   cp_token *token = NULL, *declarator_token_start = NULL;
13990   const char *saved_message;
13991
13992   /* In a template parameter, `>' is not an operator.
13993
13994      [temp.param]
13995
13996      When parsing a default template-argument for a non-type
13997      template-parameter, the first non-nested `>' is taken as the end
13998      of the template parameter-list rather than a greater-than
13999      operator.  */
14000   greater_than_is_operator_p = !template_parm_p;
14001
14002   /* Type definitions may not appear in parameter types.  */
14003   saved_message = parser->type_definition_forbidden_message;
14004   parser->type_definition_forbidden_message
14005     = "types may not be defined in parameter types";
14006
14007   /* Parse the declaration-specifiers.  */
14008   cp_parser_decl_specifier_seq (parser,
14009                                 CP_PARSER_FLAGS_NONE,
14010                                 &decl_specifiers,
14011                                 &declares_class_or_enum);
14012   /* If an error occurred, there's no reason to attempt to parse the
14013      rest of the declaration.  */
14014   if (cp_parser_error_occurred (parser))
14015     {
14016       parser->type_definition_forbidden_message = saved_message;
14017       return NULL;
14018     }
14019
14020   /* Peek at the next token.  */
14021   token = cp_lexer_peek_token (parser->lexer);
14022
14023   /* If the next token is a `)', `,', `=', `>', or `...', then there
14024      is no declarator. However, when variadic templates are enabled,
14025      there may be a declarator following `...'.  */
14026   if (token->type == CPP_CLOSE_PAREN
14027       || token->type == CPP_COMMA
14028       || token->type == CPP_EQ
14029       || token->type == CPP_GREATER)
14030     {
14031       declarator = NULL;
14032       if (parenthesized_p)
14033         *parenthesized_p = false;
14034     }
14035   /* Otherwise, there should be a declarator.  */
14036   else
14037     {
14038       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14039       parser->default_arg_ok_p = false;
14040
14041       /* After seeing a decl-specifier-seq, if the next token is not a
14042          "(", there is no possibility that the code is a valid
14043          expression.  Therefore, if parsing tentatively, we commit at
14044          this point.  */
14045       if (!parser->in_template_argument_list_p
14046           /* In an expression context, having seen:
14047
14048                (int((char ...
14049
14050              we cannot be sure whether we are looking at a
14051              function-type (taking a "char" as a parameter) or a cast
14052              of some object of type "char" to "int".  */
14053           && !parser->in_type_id_in_expr_p
14054           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14055           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14056         cp_parser_commit_to_tentative_parse (parser);
14057       /* Parse the declarator.  */
14058       declarator_token_start = token;
14059       declarator = cp_parser_declarator (parser,
14060                                          CP_PARSER_DECLARATOR_EITHER,
14061                                          /*ctor_dtor_or_conv_p=*/NULL,
14062                                          parenthesized_p,
14063                                          /*member_p=*/false);
14064       parser->default_arg_ok_p = saved_default_arg_ok_p;
14065       /* After the declarator, allow more attributes.  */
14066       decl_specifiers.attributes
14067         = chainon (decl_specifiers.attributes,
14068                    cp_parser_attributes_opt (parser));
14069     }
14070
14071   /* If the next token is an ellipsis, and we have not seen a
14072      declarator name, and the type of the declarator contains parameter
14073      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14074      a parameter pack expansion expression. Otherwise, leave the
14075      ellipsis for a C-style variadic function. */
14076   token = cp_lexer_peek_token (parser->lexer);
14077   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14078     {
14079       tree type = decl_specifiers.type;
14080
14081       if (type && DECL_P (type))
14082         type = TREE_TYPE (type);
14083
14084       if (type
14085           && TREE_CODE (type) != TYPE_PACK_EXPANSION
14086           && declarator_can_be_parameter_pack (declarator)
14087           && (!declarator || !declarator->parameter_pack_p)
14088           && uses_parameter_packs (type))
14089         {
14090           /* Consume the `...'. */
14091           cp_lexer_consume_token (parser->lexer);
14092           maybe_warn_variadic_templates ();
14093           
14094           /* Build a pack expansion type */
14095           if (declarator)
14096             declarator->parameter_pack_p = true;
14097           else
14098             decl_specifiers.type = make_pack_expansion (type);
14099         }
14100     }
14101
14102   /* The restriction on defining new types applies only to the type
14103      of the parameter, not to the default argument.  */
14104   parser->type_definition_forbidden_message = saved_message;
14105
14106   /* If the next token is `=', then process a default argument.  */
14107   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14108     {
14109       /* Consume the `='.  */
14110       cp_lexer_consume_token (parser->lexer);
14111
14112       /* If we are defining a class, then the tokens that make up the
14113          default argument must be saved and processed later.  */
14114       if (!template_parm_p && at_class_scope_p ()
14115           && TYPE_BEING_DEFINED (current_class_type))
14116         {
14117           unsigned depth = 0;
14118           int maybe_template_id = 0;
14119           cp_token *first_token;
14120           cp_token *token;
14121
14122           /* Add tokens until we have processed the entire default
14123              argument.  We add the range [first_token, token).  */
14124           first_token = cp_lexer_peek_token (parser->lexer);
14125           while (true)
14126             {
14127               bool done = false;
14128
14129               /* Peek at the next token.  */
14130               token = cp_lexer_peek_token (parser->lexer);
14131               /* What we do depends on what token we have.  */
14132               switch (token->type)
14133                 {
14134                   /* In valid code, a default argument must be
14135                      immediately followed by a `,' `)', or `...'.  */
14136                 case CPP_COMMA:
14137                   if (depth == 0 && maybe_template_id)
14138                     {
14139                       /* If we've seen a '<', we might be in a
14140                          template-argument-list.  Until Core issue 325 is
14141                          resolved, we don't know how this situation ought
14142                          to be handled, so try to DTRT.  We check whether
14143                          what comes after the comma is a valid parameter
14144                          declaration list.  If it is, then the comma ends
14145                          the default argument; otherwise the default
14146                          argument continues.  */
14147                       bool error = false;
14148
14149                       /* Set ITALP so cp_parser_parameter_declaration_list
14150                          doesn't decide to commit to this parse.  */
14151                       bool saved_italp = parser->in_template_argument_list_p;
14152                       parser->in_template_argument_list_p = true;
14153
14154                       cp_parser_parse_tentatively (parser);
14155                       cp_lexer_consume_token (parser->lexer);
14156                       cp_parser_parameter_declaration_list (parser, &error);
14157                       if (!cp_parser_error_occurred (parser) && !error)
14158                         done = true;
14159                       cp_parser_abort_tentative_parse (parser);
14160
14161                       parser->in_template_argument_list_p = saved_italp;
14162                       break;
14163                     }
14164                 case CPP_CLOSE_PAREN:
14165                 case CPP_ELLIPSIS:
14166                   /* If we run into a non-nested `;', `}', or `]',
14167                      then the code is invalid -- but the default
14168                      argument is certainly over.  */
14169                 case CPP_SEMICOLON:
14170                 case CPP_CLOSE_BRACE:
14171                 case CPP_CLOSE_SQUARE:
14172                   if (depth == 0)
14173                     done = true;
14174                   /* Update DEPTH, if necessary.  */
14175                   else if (token->type == CPP_CLOSE_PAREN
14176                            || token->type == CPP_CLOSE_BRACE
14177                            || token->type == CPP_CLOSE_SQUARE)
14178                     --depth;
14179                   break;
14180
14181                 case CPP_OPEN_PAREN:
14182                 case CPP_OPEN_SQUARE:
14183                 case CPP_OPEN_BRACE:
14184                   ++depth;
14185                   break;
14186
14187                 case CPP_LESS:
14188                   if (depth == 0)
14189                     /* This might be the comparison operator, or it might
14190                        start a template argument list.  */
14191                     ++maybe_template_id;
14192                   break;
14193
14194                 case CPP_RSHIFT:
14195                   if (cxx_dialect == cxx98)
14196                     break;
14197                   /* Fall through for C++0x, which treats the `>>'
14198                      operator like two `>' tokens in certain
14199                      cases.  */
14200
14201                 case CPP_GREATER:
14202                   if (depth == 0)
14203                     {
14204                       /* This might be an operator, or it might close a
14205                          template argument list.  But if a previous '<'
14206                          started a template argument list, this will have
14207                          closed it, so we can't be in one anymore.  */
14208                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14209                       if (maybe_template_id < 0)
14210                         maybe_template_id = 0;
14211                     }
14212                   break;
14213
14214                   /* If we run out of tokens, issue an error message.  */
14215                 case CPP_EOF:
14216                 case CPP_PRAGMA_EOL:
14217                   error ("%Hfile ends in default argument", &token->location);
14218                   done = true;
14219                   break;
14220
14221                 case CPP_NAME:
14222                 case CPP_SCOPE:
14223                   /* In these cases, we should look for template-ids.
14224                      For example, if the default argument is
14225                      `X<int, double>()', we need to do name lookup to
14226                      figure out whether or not `X' is a template; if
14227                      so, the `,' does not end the default argument.
14228
14229                      That is not yet done.  */
14230                   break;
14231
14232                 default:
14233                   break;
14234                 }
14235
14236               /* If we've reached the end, stop.  */
14237               if (done)
14238                 break;
14239
14240               /* Add the token to the token block.  */
14241               token = cp_lexer_consume_token (parser->lexer);
14242             }
14243
14244           /* Create a DEFAULT_ARG to represent the unparsed default
14245              argument.  */
14246           default_argument = make_node (DEFAULT_ARG);
14247           DEFARG_TOKENS (default_argument)
14248             = cp_token_cache_new (first_token, token);
14249           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14250         }
14251       /* Outside of a class definition, we can just parse the
14252          assignment-expression.  */
14253       else
14254         {
14255           token = cp_lexer_peek_token (parser->lexer);
14256           default_argument 
14257             = cp_parser_default_argument (parser, template_parm_p);
14258         }
14259
14260       if (!parser->default_arg_ok_p)
14261         {
14262           if (flag_permissive)
14263             warning (0, "deprecated use of default argument for parameter of non-function");
14264           else
14265             {
14266               error ("%Hdefault arguments are only "
14267                      "permitted for function parameters",
14268                      &token->location);
14269               default_argument = NULL_TREE;
14270             }
14271         }
14272       else if ((declarator && declarator->parameter_pack_p)
14273                || (decl_specifiers.type
14274                    && PACK_EXPANSION_P (decl_specifiers.type)))
14275         {
14276           const char* kind = template_parm_p? "template " : "";
14277           
14278           /* Find the name of the parameter pack.  */     
14279           cp_declarator *id_declarator = declarator;
14280           while (id_declarator && id_declarator->kind != cdk_id)
14281             id_declarator = id_declarator->declarator;
14282           
14283           if (id_declarator && id_declarator->kind == cdk_id)
14284             error ("%H%sparameter pack %qD cannot have a default argument",
14285                    &declarator_token_start->location,
14286                    kind, id_declarator->u.id.unqualified_name);
14287           else
14288             error ("%H%sparameter pack cannot have a default argument",
14289                    &declarator_token_start->location, kind);
14290           
14291           default_argument = NULL_TREE;
14292         }
14293     }
14294   else
14295     default_argument = NULL_TREE;
14296
14297   return make_parameter_declarator (&decl_specifiers,
14298                                     declarator,
14299                                     default_argument);
14300 }
14301
14302 /* Parse a default argument and return it.
14303
14304    TEMPLATE_PARM_P is true if this is a default argument for a
14305    non-type template parameter.  */
14306 static tree
14307 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14308 {
14309   tree default_argument = NULL_TREE;
14310   bool saved_greater_than_is_operator_p;
14311   bool saved_local_variables_forbidden_p;
14312
14313   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14314      set correctly.  */
14315   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14316   parser->greater_than_is_operator_p = !template_parm_p;
14317   /* Local variable names (and the `this' keyword) may not
14318      appear in a default argument.  */
14319   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14320   parser->local_variables_forbidden_p = true;
14321   /* The default argument expression may cause implicitly
14322      defined member functions to be synthesized, which will
14323      result in garbage collection.  We must treat this
14324      situation as if we were within the body of function so as
14325      to avoid collecting live data on the stack.  */
14326   ++function_depth;
14327   /* Parse the assignment-expression.  */
14328   if (template_parm_p)
14329     push_deferring_access_checks (dk_no_deferred);
14330   default_argument
14331     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14332   if (template_parm_p)
14333     pop_deferring_access_checks ();
14334   /* Restore saved state.  */
14335   --function_depth;
14336   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14337   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14338
14339   return default_argument;
14340 }
14341
14342 /* Parse a function-body.
14343
14344    function-body:
14345      compound_statement  */
14346
14347 static void
14348 cp_parser_function_body (cp_parser *parser)
14349 {
14350   cp_parser_compound_statement (parser, NULL, false);
14351 }
14352
14353 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14354    true if a ctor-initializer was present.  */
14355
14356 static bool
14357 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14358 {
14359   tree body;
14360   bool ctor_initializer_p;
14361
14362   /* Begin the function body.  */
14363   body = begin_function_body ();
14364   /* Parse the optional ctor-initializer.  */
14365   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14366   /* Parse the function-body.  */
14367   cp_parser_function_body (parser);
14368   /* Finish the function body.  */
14369   finish_function_body (body);
14370
14371   return ctor_initializer_p;
14372 }
14373
14374 /* Parse an initializer.
14375
14376    initializer:
14377      = initializer-clause
14378      ( expression-list )
14379
14380    Returns an expression representing the initializer.  If no
14381    initializer is present, NULL_TREE is returned.
14382
14383    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14384    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14385    set to TRUE if there is no initializer present.  If there is an
14386    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14387    is set to true; otherwise it is set to false.  */
14388
14389 static tree
14390 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14391                        bool* non_constant_p)
14392 {
14393   cp_token *token;
14394   tree init;
14395
14396   /* Peek at the next token.  */
14397   token = cp_lexer_peek_token (parser->lexer);
14398
14399   /* Let our caller know whether or not this initializer was
14400      parenthesized.  */
14401   *is_direct_init = (token->type != CPP_EQ);
14402   /* Assume that the initializer is constant.  */
14403   *non_constant_p = false;
14404
14405   if (token->type == CPP_EQ)
14406     {
14407       /* Consume the `='.  */
14408       cp_lexer_consume_token (parser->lexer);
14409       /* Parse the initializer-clause.  */
14410       init = cp_parser_initializer_clause (parser, non_constant_p);
14411     }
14412   else if (token->type == CPP_OPEN_PAREN)
14413     init = cp_parser_parenthesized_expression_list (parser, false,
14414                                                     /*cast_p=*/false,
14415                                                     /*allow_expansion_p=*/true,
14416                                                     non_constant_p);
14417   else if (token->type == CPP_OPEN_BRACE)
14418     {
14419       maybe_warn_cpp0x ("extended initializer lists");
14420       init = cp_parser_braced_list (parser, non_constant_p);
14421       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14422     }
14423   else
14424     {
14425       /* Anything else is an error.  */
14426       cp_parser_error (parser, "expected initializer");
14427       init = error_mark_node;
14428     }
14429
14430   return init;
14431 }
14432
14433 /* Parse an initializer-clause.
14434
14435    initializer-clause:
14436      assignment-expression
14437      braced-init-list
14438
14439    Returns an expression representing the initializer.
14440
14441    If the `assignment-expression' production is used the value
14442    returned is simply a representation for the expression.
14443
14444    Otherwise, calls cp_parser_braced_list.  */
14445
14446 static tree
14447 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14448 {
14449   tree initializer;
14450
14451   /* Assume the expression is constant.  */
14452   *non_constant_p = false;
14453
14454   /* If it is not a `{', then we are looking at an
14455      assignment-expression.  */
14456   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14457     {
14458       initializer
14459         = cp_parser_constant_expression (parser,
14460                                         /*allow_non_constant_p=*/true,
14461                                         non_constant_p);
14462       if (!*non_constant_p)
14463         initializer = fold_non_dependent_expr (initializer);
14464     }
14465   else
14466     initializer = cp_parser_braced_list (parser, non_constant_p);
14467
14468   return initializer;
14469 }
14470
14471 /* Parse a brace-enclosed initializer list.
14472
14473    braced-init-list:
14474      { initializer-list , [opt] }
14475      { }
14476
14477    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14478    the elements of the initializer-list (or NULL, if the last
14479    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14480    NULL_TREE.  There is no way to detect whether or not the optional
14481    trailing `,' was provided.  NON_CONSTANT_P is as for
14482    cp_parser_initializer.  */     
14483
14484 static tree
14485 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14486 {
14487   tree initializer;
14488
14489   /* Consume the `{' token.  */
14490   cp_lexer_consume_token (parser->lexer);
14491   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14492   initializer = make_node (CONSTRUCTOR);
14493   /* If it's not a `}', then there is a non-trivial initializer.  */
14494   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14495     {
14496       /* Parse the initializer list.  */
14497       CONSTRUCTOR_ELTS (initializer)
14498         = cp_parser_initializer_list (parser, non_constant_p);
14499       /* A trailing `,' token is allowed.  */
14500       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14501         cp_lexer_consume_token (parser->lexer);
14502     }
14503   /* Now, there should be a trailing `}'.  */
14504   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14505   TREE_TYPE (initializer) = init_list_type_node;
14506   return initializer;
14507 }
14508
14509 /* Parse an initializer-list.
14510
14511    initializer-list:
14512      initializer-clause ... [opt]
14513      initializer-list , initializer-clause ... [opt]
14514
14515    GNU Extension:
14516
14517    initializer-list:
14518      identifier : initializer-clause
14519      initializer-list, identifier : initializer-clause
14520
14521    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14522    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14523    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14524    as for cp_parser_initializer.  */
14525
14526 static VEC(constructor_elt,gc) *
14527 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14528 {
14529   VEC(constructor_elt,gc) *v = NULL;
14530
14531   /* Assume all of the expressions are constant.  */
14532   *non_constant_p = false;
14533
14534   /* Parse the rest of the list.  */
14535   while (true)
14536     {
14537       cp_token *token;
14538       tree identifier;
14539       tree initializer;
14540       bool clause_non_constant_p;
14541
14542       /* If the next token is an identifier and the following one is a
14543          colon, we are looking at the GNU designated-initializer
14544          syntax.  */
14545       if (cp_parser_allow_gnu_extensions_p (parser)
14546           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14547           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14548         {
14549           /* Warn the user that they are using an extension.  */
14550           pedwarn (input_location, OPT_pedantic, 
14551                    "ISO C++ does not allow designated initializers");
14552           /* Consume the identifier.  */
14553           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14554           /* Consume the `:'.  */
14555           cp_lexer_consume_token (parser->lexer);
14556         }
14557       else
14558         identifier = NULL_TREE;
14559
14560       /* Parse the initializer.  */
14561       initializer = cp_parser_initializer_clause (parser,
14562                                                   &clause_non_constant_p);
14563       /* If any clause is non-constant, so is the entire initializer.  */
14564       if (clause_non_constant_p)
14565         *non_constant_p = true;
14566
14567       /* If we have an ellipsis, this is an initializer pack
14568          expansion.  */
14569       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14570         {
14571           /* Consume the `...'.  */
14572           cp_lexer_consume_token (parser->lexer);
14573
14574           /* Turn the initializer into an initializer expansion.  */
14575           initializer = make_pack_expansion (initializer);
14576         }
14577
14578       /* Add it to the vector.  */
14579       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14580
14581       /* If the next token is not a comma, we have reached the end of
14582          the list.  */
14583       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14584         break;
14585
14586       /* Peek at the next token.  */
14587       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14588       /* If the next token is a `}', then we're still done.  An
14589          initializer-clause can have a trailing `,' after the
14590          initializer-list and before the closing `}'.  */
14591       if (token->type == CPP_CLOSE_BRACE)
14592         break;
14593
14594       /* Consume the `,' token.  */
14595       cp_lexer_consume_token (parser->lexer);
14596     }
14597
14598   return v;
14599 }
14600
14601 /* Classes [gram.class] */
14602
14603 /* Parse a class-name.
14604
14605    class-name:
14606      identifier
14607      template-id
14608
14609    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14610    to indicate that names looked up in dependent types should be
14611    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14612    keyword has been used to indicate that the name that appears next
14613    is a template.  TAG_TYPE indicates the explicit tag given before
14614    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14615    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14616    is the class being defined in a class-head.
14617
14618    Returns the TYPE_DECL representing the class.  */
14619
14620 static tree
14621 cp_parser_class_name (cp_parser *parser,
14622                       bool typename_keyword_p,
14623                       bool template_keyword_p,
14624                       enum tag_types tag_type,
14625                       bool check_dependency_p,
14626                       bool class_head_p,
14627                       bool is_declaration)
14628 {
14629   tree decl;
14630   tree scope;
14631   bool typename_p;
14632   cp_token *token;
14633
14634   /* All class-names start with an identifier.  */
14635   token = cp_lexer_peek_token (parser->lexer);
14636   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14637     {
14638       cp_parser_error (parser, "expected class-name");
14639       return error_mark_node;
14640     }
14641
14642   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14643      to a template-id, so we save it here.  */
14644   scope = parser->scope;
14645   if (scope == error_mark_node)
14646     return error_mark_node;
14647
14648   /* Any name names a type if we're following the `typename' keyword
14649      in a qualified name where the enclosing scope is type-dependent.  */
14650   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14651                 && dependent_type_p (scope));
14652   /* Handle the common case (an identifier, but not a template-id)
14653      efficiently.  */
14654   if (token->type == CPP_NAME
14655       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14656     {
14657       cp_token *identifier_token;
14658       tree identifier;
14659       bool ambiguous_p;
14660
14661       /* Look for the identifier.  */
14662       identifier_token = cp_lexer_peek_token (parser->lexer);
14663       ambiguous_p = identifier_token->ambiguous_p;
14664       identifier = cp_parser_identifier (parser);
14665       /* If the next token isn't an identifier, we are certainly not
14666          looking at a class-name.  */
14667       if (identifier == error_mark_node)
14668         decl = error_mark_node;
14669       /* If we know this is a type-name, there's no need to look it
14670          up.  */
14671       else if (typename_p)
14672         decl = identifier;
14673       else
14674         {
14675           tree ambiguous_decls;
14676           /* If we already know that this lookup is ambiguous, then
14677              we've already issued an error message; there's no reason
14678              to check again.  */
14679           if (ambiguous_p)
14680             {
14681               cp_parser_simulate_error (parser);
14682               return error_mark_node;
14683             }
14684           /* If the next token is a `::', then the name must be a type
14685              name.
14686
14687              [basic.lookup.qual]
14688
14689              During the lookup for a name preceding the :: scope
14690              resolution operator, object, function, and enumerator
14691              names are ignored.  */
14692           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14693             tag_type = typename_type;
14694           /* Look up the name.  */
14695           decl = cp_parser_lookup_name (parser, identifier,
14696                                         tag_type,
14697                                         /*is_template=*/false,
14698                                         /*is_namespace=*/false,
14699                                         check_dependency_p,
14700                                         &ambiguous_decls,
14701                                         identifier_token->location);
14702           if (ambiguous_decls)
14703             {
14704               error ("%Hreference to %qD is ambiguous",
14705                      &identifier_token->location, identifier);
14706               print_candidates (ambiguous_decls);
14707               if (cp_parser_parsing_tentatively (parser))
14708                 {
14709                   identifier_token->ambiguous_p = true;
14710                   cp_parser_simulate_error (parser);
14711                 }
14712               return error_mark_node;
14713             }
14714         }
14715     }
14716   else
14717     {
14718       /* Try a template-id.  */
14719       decl = cp_parser_template_id (parser, template_keyword_p,
14720                                     check_dependency_p,
14721                                     is_declaration);
14722       if (decl == error_mark_node)
14723         return error_mark_node;
14724     }
14725
14726   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14727
14728   /* If this is a typename, create a TYPENAME_TYPE.  */
14729   if (typename_p && decl != error_mark_node)
14730     {
14731       decl = make_typename_type (scope, decl, typename_type,
14732                                  /*complain=*/tf_error);
14733       if (decl != error_mark_node)
14734         decl = TYPE_NAME (decl);
14735     }
14736
14737   /* Check to see that it is really the name of a class.  */
14738   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14739       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14740       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14741     /* Situations like this:
14742
14743          template <typename T> struct A {
14744            typename T::template X<int>::I i;
14745          };
14746
14747        are problematic.  Is `T::template X<int>' a class-name?  The
14748        standard does not seem to be definitive, but there is no other
14749        valid interpretation of the following `::'.  Therefore, those
14750        names are considered class-names.  */
14751     {
14752       decl = make_typename_type (scope, decl, tag_type, tf_error);
14753       if (decl != error_mark_node)
14754         decl = TYPE_NAME (decl);
14755     }
14756   else if (TREE_CODE (decl) != TYPE_DECL
14757            || TREE_TYPE (decl) == error_mark_node
14758            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14759     decl = error_mark_node;
14760
14761   if (decl == error_mark_node)
14762     cp_parser_error (parser, "expected class-name");
14763
14764   return decl;
14765 }
14766
14767 /* Parse a class-specifier.
14768
14769    class-specifier:
14770      class-head { member-specification [opt] }
14771
14772    Returns the TREE_TYPE representing the class.  */
14773
14774 static tree
14775 cp_parser_class_specifier (cp_parser* parser)
14776 {
14777   cp_token *token;
14778   tree type;
14779   tree attributes = NULL_TREE;
14780   int has_trailing_semicolon;
14781   bool nested_name_specifier_p;
14782   unsigned saved_num_template_parameter_lists;
14783   bool saved_in_function_body;
14784   tree old_scope = NULL_TREE;
14785   tree scope = NULL_TREE;
14786   tree bases;
14787
14788   push_deferring_access_checks (dk_no_deferred);
14789
14790   /* Parse the class-head.  */
14791   type = cp_parser_class_head (parser,
14792                                &nested_name_specifier_p,
14793                                &attributes,
14794                                &bases);
14795   /* If the class-head was a semantic disaster, skip the entire body
14796      of the class.  */
14797   if (!type)
14798     {
14799       cp_parser_skip_to_end_of_block_or_statement (parser);
14800       pop_deferring_access_checks ();
14801       return error_mark_node;
14802     }
14803
14804   /* Look for the `{'.  */
14805   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14806     {
14807       pop_deferring_access_checks ();
14808       return error_mark_node;
14809     }
14810
14811   /* Process the base classes. If they're invalid, skip the 
14812      entire class body.  */
14813   if (!xref_basetypes (type, bases))
14814     {
14815       /* Consuming the closing brace yields better error messages
14816          later on.  */
14817       if (cp_parser_skip_to_closing_brace (parser))
14818         cp_lexer_consume_token (parser->lexer);
14819       pop_deferring_access_checks ();
14820       return error_mark_node;
14821     }
14822
14823   /* Issue an error message if type-definitions are forbidden here.  */
14824   cp_parser_check_type_definition (parser);
14825   /* Remember that we are defining one more class.  */
14826   ++parser->num_classes_being_defined;
14827   /* Inside the class, surrounding template-parameter-lists do not
14828      apply.  */
14829   saved_num_template_parameter_lists
14830     = parser->num_template_parameter_lists;
14831   parser->num_template_parameter_lists = 0;
14832   /* We are not in a function body.  */
14833   saved_in_function_body = parser->in_function_body;
14834   parser->in_function_body = false;
14835
14836   /* Start the class.  */
14837   if (nested_name_specifier_p)
14838     {
14839       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14840       old_scope = push_inner_scope (scope);
14841     }
14842   type = begin_class_definition (type, attributes);
14843
14844   if (type == error_mark_node)
14845     /* If the type is erroneous, skip the entire body of the class.  */
14846     cp_parser_skip_to_closing_brace (parser);
14847   else
14848     /* Parse the member-specification.  */
14849     cp_parser_member_specification_opt (parser);
14850
14851   /* Look for the trailing `}'.  */
14852   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14853   /* We get better error messages by noticing a common problem: a
14854      missing trailing `;'.  */
14855   token = cp_lexer_peek_token (parser->lexer);
14856   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14857   /* Look for trailing attributes to apply to this class.  */
14858   if (cp_parser_allow_gnu_extensions_p (parser))
14859     attributes = cp_parser_attributes_opt (parser);
14860   if (type != error_mark_node)
14861     type = finish_struct (type, attributes);
14862   if (nested_name_specifier_p)
14863     pop_inner_scope (old_scope, scope);
14864   /* If this class is not itself within the scope of another class,
14865      then we need to parse the bodies of all of the queued function
14866      definitions.  Note that the queued functions defined in a class
14867      are not always processed immediately following the
14868      class-specifier for that class.  Consider:
14869
14870        struct A {
14871          struct B { void f() { sizeof (A); } };
14872        };
14873
14874      If `f' were processed before the processing of `A' were
14875      completed, there would be no way to compute the size of `A'.
14876      Note that the nesting we are interested in here is lexical --
14877      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14878      for:
14879
14880        struct A { struct B; };
14881        struct A::B { void f() { } };
14882
14883      there is no need to delay the parsing of `A::B::f'.  */
14884   if (--parser->num_classes_being_defined == 0)
14885     {
14886       tree queue_entry;
14887       tree fn;
14888       tree class_type = NULL_TREE;
14889       tree pushed_scope = NULL_TREE;
14890
14891       /* In a first pass, parse default arguments to the functions.
14892          Then, in a second pass, parse the bodies of the functions.
14893          This two-phased approach handles cases like:
14894
14895             struct S {
14896               void f() { g(); }
14897               void g(int i = 3);
14898             };
14899
14900          */
14901       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14902              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14903            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14904            TREE_PURPOSE (parser->unparsed_functions_queues)
14905              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14906         {
14907           fn = TREE_VALUE (queue_entry);
14908           /* If there are default arguments that have not yet been processed,
14909              take care of them now.  */
14910           if (class_type != TREE_PURPOSE (queue_entry))
14911             {
14912               if (pushed_scope)
14913                 pop_scope (pushed_scope);
14914               class_type = TREE_PURPOSE (queue_entry);
14915               pushed_scope = push_scope (class_type);
14916             }
14917           /* Make sure that any template parameters are in scope.  */
14918           maybe_begin_member_template_processing (fn);
14919           /* Parse the default argument expressions.  */
14920           cp_parser_late_parsing_default_args (parser, fn);
14921           /* Remove any template parameters from the symbol table.  */
14922           maybe_end_member_template_processing ();
14923         }
14924       if (pushed_scope)
14925         pop_scope (pushed_scope);
14926       /* Now parse the body of the functions.  */
14927       for (TREE_VALUE (parser->unparsed_functions_queues)
14928              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14929            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14930            TREE_VALUE (parser->unparsed_functions_queues)
14931              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14932         {
14933           /* Figure out which function we need to process.  */
14934           fn = TREE_VALUE (queue_entry);
14935           /* Parse the function.  */
14936           cp_parser_late_parsing_for_member (parser, fn);
14937         }
14938     }
14939
14940   /* Put back any saved access checks.  */
14941   pop_deferring_access_checks ();
14942
14943   /* Restore saved state.  */
14944   parser->in_function_body = saved_in_function_body;
14945   parser->num_template_parameter_lists
14946     = saved_num_template_parameter_lists;
14947
14948   return type;
14949 }
14950
14951 /* Parse a class-head.
14952
14953    class-head:
14954      class-key identifier [opt] base-clause [opt]
14955      class-key nested-name-specifier identifier base-clause [opt]
14956      class-key nested-name-specifier [opt] template-id
14957        base-clause [opt]
14958
14959    GNU Extensions:
14960      class-key attributes identifier [opt] base-clause [opt]
14961      class-key attributes nested-name-specifier identifier base-clause [opt]
14962      class-key attributes nested-name-specifier [opt] template-id
14963        base-clause [opt]
14964
14965    Upon return BASES is initialized to the list of base classes (or
14966    NULL, if there are none) in the same form returned by
14967    cp_parser_base_clause.
14968
14969    Returns the TYPE of the indicated class.  Sets
14970    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14971    involving a nested-name-specifier was used, and FALSE otherwise.
14972
14973    Returns error_mark_node if this is not a class-head.
14974
14975    Returns NULL_TREE if the class-head is syntactically valid, but
14976    semantically invalid in a way that means we should skip the entire
14977    body of the class.  */
14978
14979 static tree
14980 cp_parser_class_head (cp_parser* parser,
14981                       bool* nested_name_specifier_p,
14982                       tree *attributes_p,
14983                       tree *bases)
14984 {
14985   tree nested_name_specifier;
14986   enum tag_types class_key;
14987   tree id = NULL_TREE;
14988   tree type = NULL_TREE;
14989   tree attributes;
14990   bool template_id_p = false;
14991   bool qualified_p = false;
14992   bool invalid_nested_name_p = false;
14993   bool invalid_explicit_specialization_p = false;
14994   tree pushed_scope = NULL_TREE;
14995   unsigned num_templates;
14996   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14997   /* Assume no nested-name-specifier will be present.  */
14998   *nested_name_specifier_p = false;
14999   /* Assume no template parameter lists will be used in defining the
15000      type.  */
15001   num_templates = 0;
15002
15003   *bases = NULL_TREE;
15004
15005   /* Look for the class-key.  */
15006   class_key = cp_parser_class_key (parser);
15007   if (class_key == none_type)
15008     return error_mark_node;
15009
15010   /* Parse the attributes.  */
15011   attributes = cp_parser_attributes_opt (parser);
15012
15013   /* If the next token is `::', that is invalid -- but sometimes
15014      people do try to write:
15015
15016        struct ::S {};
15017
15018      Handle this gracefully by accepting the extra qualifier, and then
15019      issuing an error about it later if this really is a
15020      class-head.  If it turns out just to be an elaborated type
15021      specifier, remain silent.  */
15022   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15023     qualified_p = true;
15024
15025   push_deferring_access_checks (dk_no_check);
15026
15027   /* Determine the name of the class.  Begin by looking for an
15028      optional nested-name-specifier.  */
15029   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15030   nested_name_specifier
15031     = cp_parser_nested_name_specifier_opt (parser,
15032                                            /*typename_keyword_p=*/false,
15033                                            /*check_dependency_p=*/false,
15034                                            /*type_p=*/false,
15035                                            /*is_declaration=*/false);
15036   /* If there was a nested-name-specifier, then there *must* be an
15037      identifier.  */
15038   if (nested_name_specifier)
15039     {
15040       type_start_token = cp_lexer_peek_token (parser->lexer);
15041       /* Although the grammar says `identifier', it really means
15042          `class-name' or `template-name'.  You are only allowed to
15043          define a class that has already been declared with this
15044          syntax.
15045
15046          The proposed resolution for Core Issue 180 says that wherever
15047          you see `class T::X' you should treat `X' as a type-name.
15048
15049          It is OK to define an inaccessible class; for example:
15050
15051            class A { class B; };
15052            class A::B {};
15053
15054          We do not know if we will see a class-name, or a
15055          template-name.  We look for a class-name first, in case the
15056          class-name is a template-id; if we looked for the
15057          template-name first we would stop after the template-name.  */
15058       cp_parser_parse_tentatively (parser);
15059       type = cp_parser_class_name (parser,
15060                                    /*typename_keyword_p=*/false,
15061                                    /*template_keyword_p=*/false,
15062                                    class_type,
15063                                    /*check_dependency_p=*/false,
15064                                    /*class_head_p=*/true,
15065                                    /*is_declaration=*/false);
15066       /* If that didn't work, ignore the nested-name-specifier.  */
15067       if (!cp_parser_parse_definitely (parser))
15068         {
15069           invalid_nested_name_p = true;
15070           type_start_token = cp_lexer_peek_token (parser->lexer);
15071           id = cp_parser_identifier (parser);
15072           if (id == error_mark_node)
15073             id = NULL_TREE;
15074         }
15075       /* If we could not find a corresponding TYPE, treat this
15076          declaration like an unqualified declaration.  */
15077       if (type == error_mark_node)
15078         nested_name_specifier = NULL_TREE;
15079       /* Otherwise, count the number of templates used in TYPE and its
15080          containing scopes.  */
15081       else
15082         {
15083           tree scope;
15084
15085           for (scope = TREE_TYPE (type);
15086                scope && TREE_CODE (scope) != NAMESPACE_DECL;
15087                scope = (TYPE_P (scope)
15088                         ? TYPE_CONTEXT (scope)
15089                         : DECL_CONTEXT (scope)))
15090             if (TYPE_P (scope)
15091                 && CLASS_TYPE_P (scope)
15092                 && CLASSTYPE_TEMPLATE_INFO (scope)
15093                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15094                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15095               ++num_templates;
15096         }
15097     }
15098   /* Otherwise, the identifier is optional.  */
15099   else
15100     {
15101       /* We don't know whether what comes next is a template-id,
15102          an identifier, or nothing at all.  */
15103       cp_parser_parse_tentatively (parser);
15104       /* Check for a template-id.  */
15105       type_start_token = cp_lexer_peek_token (parser->lexer);
15106       id = cp_parser_template_id (parser,
15107                                   /*template_keyword_p=*/false,
15108                                   /*check_dependency_p=*/true,
15109                                   /*is_declaration=*/true);
15110       /* If that didn't work, it could still be an identifier.  */
15111       if (!cp_parser_parse_definitely (parser))
15112         {
15113           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15114             {
15115               type_start_token = cp_lexer_peek_token (parser->lexer);
15116               id = cp_parser_identifier (parser);
15117             }
15118           else
15119             id = NULL_TREE;
15120         }
15121       else
15122         {
15123           template_id_p = true;
15124           ++num_templates;
15125         }
15126     }
15127
15128   pop_deferring_access_checks ();
15129
15130   if (id)
15131     cp_parser_check_for_invalid_template_id (parser, id,
15132                                              type_start_token->location);
15133
15134   /* If it's not a `:' or a `{' then we can't really be looking at a
15135      class-head, since a class-head only appears as part of a
15136      class-specifier.  We have to detect this situation before calling
15137      xref_tag, since that has irreversible side-effects.  */
15138   if (!cp_parser_next_token_starts_class_definition_p (parser))
15139     {
15140       cp_parser_error (parser, "expected %<{%> or %<:%>");
15141       return error_mark_node;
15142     }
15143
15144   /* At this point, we're going ahead with the class-specifier, even
15145      if some other problem occurs.  */
15146   cp_parser_commit_to_tentative_parse (parser);
15147   /* Issue the error about the overly-qualified name now.  */
15148   if (qualified_p)
15149     {
15150       cp_parser_error (parser,
15151                        "global qualification of class name is invalid");
15152       return error_mark_node;
15153     }
15154   else if (invalid_nested_name_p)
15155     {
15156       cp_parser_error (parser,
15157                        "qualified name does not name a class");
15158       return error_mark_node;
15159     }
15160   else if (nested_name_specifier)
15161     {
15162       tree scope;
15163
15164       /* Reject typedef-names in class heads.  */
15165       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15166         {
15167           error ("%Hinvalid class name in declaration of %qD",
15168                  &type_start_token->location, type);
15169           type = NULL_TREE;
15170           goto done;
15171         }
15172
15173       /* Figure out in what scope the declaration is being placed.  */
15174       scope = current_scope ();
15175       /* If that scope does not contain the scope in which the
15176          class was originally declared, the program is invalid.  */
15177       if (scope && !is_ancestor (scope, nested_name_specifier))
15178         {
15179           if (at_namespace_scope_p ())
15180             error ("%Hdeclaration of %qD in namespace %qD which does not "
15181                    "enclose %qD",
15182                    &type_start_token->location,
15183                    type, scope, nested_name_specifier);
15184           else
15185             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15186                    &type_start_token->location,
15187                    type, scope, nested_name_specifier);
15188           type = NULL_TREE;
15189           goto done;
15190         }
15191       /* [dcl.meaning]
15192
15193          A declarator-id shall not be qualified except for the
15194          definition of a ... nested class outside of its class
15195          ... [or] the definition or explicit instantiation of a
15196          class member of a namespace outside of its namespace.  */
15197       if (scope == nested_name_specifier)
15198         {
15199           permerror (input_location, "%Hextra qualification not allowed",
15200                      &nested_name_specifier_token_start->location);
15201           nested_name_specifier = NULL_TREE;
15202           num_templates = 0;
15203         }
15204     }
15205   /* An explicit-specialization must be preceded by "template <>".  If
15206      it is not, try to recover gracefully.  */
15207   if (at_namespace_scope_p ()
15208       && parser->num_template_parameter_lists == 0
15209       && template_id_p)
15210     {
15211       error ("%Han explicit specialization must be preceded by %<template <>%>",
15212              &type_start_token->location);
15213       invalid_explicit_specialization_p = true;
15214       /* Take the same action that would have been taken by
15215          cp_parser_explicit_specialization.  */
15216       ++parser->num_template_parameter_lists;
15217       begin_specialization ();
15218     }
15219   /* There must be no "return" statements between this point and the
15220      end of this function; set "type "to the correct return value and
15221      use "goto done;" to return.  */
15222   /* Make sure that the right number of template parameters were
15223      present.  */
15224   if (!cp_parser_check_template_parameters (parser, num_templates,
15225                                             type_start_token->location))
15226     {
15227       /* If something went wrong, there is no point in even trying to
15228          process the class-definition.  */
15229       type = NULL_TREE;
15230       goto done;
15231     }
15232
15233   /* Look up the type.  */
15234   if (template_id_p)
15235     {
15236       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15237           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15238               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15239         {
15240           error ("%Hfunction template %qD redeclared as a class template",
15241                  &type_start_token->location, id);
15242           type = error_mark_node;
15243         }
15244       else
15245         {
15246           type = TREE_TYPE (id);
15247           type = maybe_process_partial_specialization (type);
15248         }
15249       if (nested_name_specifier)
15250         pushed_scope = push_scope (nested_name_specifier);
15251     }
15252   else if (nested_name_specifier)
15253     {
15254       tree class_type;
15255
15256       /* Given:
15257
15258             template <typename T> struct S { struct T };
15259             template <typename T> struct S<T>::T { };
15260
15261          we will get a TYPENAME_TYPE when processing the definition of
15262          `S::T'.  We need to resolve it to the actual type before we
15263          try to define it.  */
15264       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15265         {
15266           class_type = resolve_typename_type (TREE_TYPE (type),
15267                                               /*only_current_p=*/false);
15268           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15269             type = TYPE_NAME (class_type);
15270           else
15271             {
15272               cp_parser_error (parser, "could not resolve typename type");
15273               type = error_mark_node;
15274             }
15275         }
15276
15277       if (maybe_process_partial_specialization (TREE_TYPE (type))
15278           == error_mark_node)
15279         {
15280           type = NULL_TREE;
15281           goto done;
15282         }
15283
15284       class_type = current_class_type;
15285       /* Enter the scope indicated by the nested-name-specifier.  */
15286       pushed_scope = push_scope (nested_name_specifier);
15287       /* Get the canonical version of this type.  */
15288       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15289       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15290           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15291         {
15292           type = push_template_decl (type);
15293           if (type == error_mark_node)
15294             {
15295               type = NULL_TREE;
15296               goto done;
15297             }
15298         }
15299
15300       type = TREE_TYPE (type);
15301       *nested_name_specifier_p = true;
15302     }
15303   else      /* The name is not a nested name.  */
15304     {
15305       /* If the class was unnamed, create a dummy name.  */
15306       if (!id)
15307         id = make_anon_name ();
15308       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15309                        parser->num_template_parameter_lists);
15310     }
15311
15312   /* Indicate whether this class was declared as a `class' or as a
15313      `struct'.  */
15314   if (TREE_CODE (type) == RECORD_TYPE)
15315     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15316   cp_parser_check_class_key (class_key, type);
15317
15318   /* If this type was already complete, and we see another definition,
15319      that's an error.  */
15320   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15321     {
15322       error ("%Hredefinition of %q#T",
15323              &type_start_token->location, type);
15324       error ("%Hprevious definition of %q+#T",
15325              &type_start_token->location, type);
15326       type = NULL_TREE;
15327       goto done;
15328     }
15329   else if (type == error_mark_node)
15330     type = NULL_TREE;
15331
15332   /* We will have entered the scope containing the class; the names of
15333      base classes should be looked up in that context.  For example:
15334
15335        struct A { struct B {}; struct C; };
15336        struct A::C : B {};
15337
15338      is valid.  */
15339
15340   /* Get the list of base-classes, if there is one.  */
15341   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15342     *bases = cp_parser_base_clause (parser);
15343
15344  done:
15345   /* Leave the scope given by the nested-name-specifier.  We will
15346      enter the class scope itself while processing the members.  */
15347   if (pushed_scope)
15348     pop_scope (pushed_scope);
15349
15350   if (invalid_explicit_specialization_p)
15351     {
15352       end_specialization ();
15353       --parser->num_template_parameter_lists;
15354     }
15355   *attributes_p = attributes;
15356   return type;
15357 }
15358
15359 /* Parse a class-key.
15360
15361    class-key:
15362      class
15363      struct
15364      union
15365
15366    Returns the kind of class-key specified, or none_type to indicate
15367    error.  */
15368
15369 static enum tag_types
15370 cp_parser_class_key (cp_parser* parser)
15371 {
15372   cp_token *token;
15373   enum tag_types tag_type;
15374
15375   /* Look for the class-key.  */
15376   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15377   if (!token)
15378     return none_type;
15379
15380   /* Check to see if the TOKEN is a class-key.  */
15381   tag_type = cp_parser_token_is_class_key (token);
15382   if (!tag_type)
15383     cp_parser_error (parser, "expected class-key");
15384   return tag_type;
15385 }
15386
15387 /* Parse an (optional) member-specification.
15388
15389    member-specification:
15390      member-declaration member-specification [opt]
15391      access-specifier : member-specification [opt]  */
15392
15393 static void
15394 cp_parser_member_specification_opt (cp_parser* parser)
15395 {
15396   while (true)
15397     {
15398       cp_token *token;
15399       enum rid keyword;
15400
15401       /* Peek at the next token.  */
15402       token = cp_lexer_peek_token (parser->lexer);
15403       /* If it's a `}', or EOF then we've seen all the members.  */
15404       if (token->type == CPP_CLOSE_BRACE
15405           || token->type == CPP_EOF
15406           || token->type == CPP_PRAGMA_EOL)
15407         break;
15408
15409       /* See if this token is a keyword.  */
15410       keyword = token->keyword;
15411       switch (keyword)
15412         {
15413         case RID_PUBLIC:
15414         case RID_PROTECTED:
15415         case RID_PRIVATE:
15416           /* Consume the access-specifier.  */
15417           cp_lexer_consume_token (parser->lexer);
15418           /* Remember which access-specifier is active.  */
15419           current_access_specifier = token->u.value;
15420           /* Look for the `:'.  */
15421           cp_parser_require (parser, CPP_COLON, "%<:%>");
15422           break;
15423
15424         default:
15425           /* Accept #pragmas at class scope.  */
15426           if (token->type == CPP_PRAGMA)
15427             {
15428               cp_parser_pragma (parser, pragma_external);
15429               break;
15430             }
15431
15432           /* Otherwise, the next construction must be a
15433              member-declaration.  */
15434           cp_parser_member_declaration (parser);
15435         }
15436     }
15437 }
15438
15439 /* Parse a member-declaration.
15440
15441    member-declaration:
15442      decl-specifier-seq [opt] member-declarator-list [opt] ;
15443      function-definition ; [opt]
15444      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15445      using-declaration
15446      template-declaration
15447
15448    member-declarator-list:
15449      member-declarator
15450      member-declarator-list , member-declarator
15451
15452    member-declarator:
15453      declarator pure-specifier [opt]
15454      declarator constant-initializer [opt]
15455      identifier [opt] : constant-expression
15456
15457    GNU Extensions:
15458
15459    member-declaration:
15460      __extension__ member-declaration
15461
15462    member-declarator:
15463      declarator attributes [opt] pure-specifier [opt]
15464      declarator attributes [opt] constant-initializer [opt]
15465      identifier [opt] attributes [opt] : constant-expression  
15466
15467    C++0x Extensions:
15468
15469    member-declaration:
15470      static_assert-declaration  */
15471
15472 static void
15473 cp_parser_member_declaration (cp_parser* parser)
15474 {
15475   cp_decl_specifier_seq decl_specifiers;
15476   tree prefix_attributes;
15477   tree decl;
15478   int declares_class_or_enum;
15479   bool friend_p;
15480   cp_token *token = NULL;
15481   cp_token *decl_spec_token_start = NULL;
15482   cp_token *initializer_token_start = NULL;
15483   int saved_pedantic;
15484
15485   /* Check for the `__extension__' keyword.  */
15486   if (cp_parser_extension_opt (parser, &saved_pedantic))
15487     {
15488       /* Recurse.  */
15489       cp_parser_member_declaration (parser);
15490       /* Restore the old value of the PEDANTIC flag.  */
15491       pedantic = saved_pedantic;
15492
15493       return;
15494     }
15495
15496   /* Check for a template-declaration.  */
15497   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15498     {
15499       /* An explicit specialization here is an error condition, and we
15500          expect the specialization handler to detect and report this.  */
15501       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15502           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15503         cp_parser_explicit_specialization (parser);
15504       else
15505         cp_parser_template_declaration (parser, /*member_p=*/true);
15506
15507       return;
15508     }
15509
15510   /* Check for a using-declaration.  */
15511   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15512     {
15513       /* Parse the using-declaration.  */
15514       cp_parser_using_declaration (parser,
15515                                    /*access_declaration_p=*/false);
15516       return;
15517     }
15518
15519   /* Check for @defs.  */
15520   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15521     {
15522       tree ivar, member;
15523       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15524       ivar = ivar_chains;
15525       while (ivar)
15526         {
15527           member = ivar;
15528           ivar = TREE_CHAIN (member);
15529           TREE_CHAIN (member) = NULL_TREE;
15530           finish_member_declaration (member);
15531         }
15532       return;
15533     }
15534
15535   /* If the next token is `static_assert' we have a static assertion.  */
15536   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15537     {
15538       cp_parser_static_assert (parser, /*member_p=*/true);
15539       return;
15540     }
15541
15542   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15543     return;
15544
15545   /* Parse the decl-specifier-seq.  */
15546   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15547   cp_parser_decl_specifier_seq (parser,
15548                                 CP_PARSER_FLAGS_OPTIONAL,
15549                                 &decl_specifiers,
15550                                 &declares_class_or_enum);
15551   prefix_attributes = decl_specifiers.attributes;
15552   decl_specifiers.attributes = NULL_TREE;
15553   /* Check for an invalid type-name.  */
15554   if (!decl_specifiers.type
15555       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15556     return;
15557   /* If there is no declarator, then the decl-specifier-seq should
15558      specify a type.  */
15559   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15560     {
15561       /* If there was no decl-specifier-seq, and the next token is a
15562          `;', then we have something like:
15563
15564            struct S { ; };
15565
15566          [class.mem]
15567
15568          Each member-declaration shall declare at least one member
15569          name of the class.  */
15570       if (!decl_specifiers.any_specifiers_p)
15571         {
15572           cp_token *token = cp_lexer_peek_token (parser->lexer);
15573           if (!in_system_header_at (token->location))
15574             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15575         }
15576       else
15577         {
15578           tree type;
15579
15580           /* See if this declaration is a friend.  */
15581           friend_p = cp_parser_friend_p (&decl_specifiers);
15582           /* If there were decl-specifiers, check to see if there was
15583              a class-declaration.  */
15584           type = check_tag_decl (&decl_specifiers);
15585           /* Nested classes have already been added to the class, but
15586              a `friend' needs to be explicitly registered.  */
15587           if (friend_p)
15588             {
15589               /* If the `friend' keyword was present, the friend must
15590                  be introduced with a class-key.  */
15591                if (!declares_class_or_enum)
15592                  error ("%Ha class-key must be used when declaring a friend",
15593                         &decl_spec_token_start->location);
15594                /* In this case:
15595
15596                     template <typename T> struct A {
15597                       friend struct A<T>::B;
15598                     };
15599
15600                   A<T>::B will be represented by a TYPENAME_TYPE, and
15601                   therefore not recognized by check_tag_decl.  */
15602                if (!type
15603                    && decl_specifiers.type
15604                    && TYPE_P (decl_specifiers.type))
15605                  type = decl_specifiers.type;
15606                if (!type || !TYPE_P (type))
15607                  error ("%Hfriend declaration does not name a class or "
15608                         "function", &decl_spec_token_start->location);
15609                else
15610                  make_friend_class (current_class_type, type,
15611                                     /*complain=*/true);
15612             }
15613           /* If there is no TYPE, an error message will already have
15614              been issued.  */
15615           else if (!type || type == error_mark_node)
15616             ;
15617           /* An anonymous aggregate has to be handled specially; such
15618              a declaration really declares a data member (with a
15619              particular type), as opposed to a nested class.  */
15620           else if (ANON_AGGR_TYPE_P (type))
15621             {
15622               /* Remove constructors and such from TYPE, now that we
15623                  know it is an anonymous aggregate.  */
15624               fixup_anonymous_aggr (type);
15625               /* And make the corresponding data member.  */
15626               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15627               /* Add it to the class.  */
15628               finish_member_declaration (decl);
15629             }
15630           else
15631             cp_parser_check_access_in_redeclaration
15632                                               (TYPE_NAME (type),
15633                                                decl_spec_token_start->location);
15634         }
15635     }
15636   else
15637     {
15638       /* See if these declarations will be friends.  */
15639       friend_p = cp_parser_friend_p (&decl_specifiers);
15640
15641       /* Keep going until we hit the `;' at the end of the
15642          declaration.  */
15643       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15644         {
15645           tree attributes = NULL_TREE;
15646           tree first_attribute;
15647
15648           /* Peek at the next token.  */
15649           token = cp_lexer_peek_token (parser->lexer);
15650
15651           /* Check for a bitfield declaration.  */
15652           if (token->type == CPP_COLON
15653               || (token->type == CPP_NAME
15654                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15655                   == CPP_COLON))
15656             {
15657               tree identifier;
15658               tree width;
15659
15660               /* Get the name of the bitfield.  Note that we cannot just
15661                  check TOKEN here because it may have been invalidated by
15662                  the call to cp_lexer_peek_nth_token above.  */
15663               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15664                 identifier = cp_parser_identifier (parser);
15665               else
15666                 identifier = NULL_TREE;
15667
15668               /* Consume the `:' token.  */
15669               cp_lexer_consume_token (parser->lexer);
15670               /* Get the width of the bitfield.  */
15671               width
15672                 = cp_parser_constant_expression (parser,
15673                                                  /*allow_non_constant=*/false,
15674                                                  NULL);
15675
15676               /* Look for attributes that apply to the bitfield.  */
15677               attributes = cp_parser_attributes_opt (parser);
15678               /* Remember which attributes are prefix attributes and
15679                  which are not.  */
15680               first_attribute = attributes;
15681               /* Combine the attributes.  */
15682               attributes = chainon (prefix_attributes, attributes);
15683
15684               /* Create the bitfield declaration.  */
15685               decl = grokbitfield (identifier
15686                                    ? make_id_declarator (NULL_TREE,
15687                                                          identifier,
15688                                                          sfk_none)
15689                                    : NULL,
15690                                    &decl_specifiers,
15691                                    width,
15692                                    attributes);
15693             }
15694           else
15695             {
15696               cp_declarator *declarator;
15697               tree initializer;
15698               tree asm_specification;
15699               int ctor_dtor_or_conv_p;
15700
15701               /* Parse the declarator.  */
15702               declarator
15703                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15704                                         &ctor_dtor_or_conv_p,
15705                                         /*parenthesized_p=*/NULL,
15706                                         /*member_p=*/true);
15707
15708               /* If something went wrong parsing the declarator, make sure
15709                  that we at least consume some tokens.  */
15710               if (declarator == cp_error_declarator)
15711                 {
15712                   /* Skip to the end of the statement.  */
15713                   cp_parser_skip_to_end_of_statement (parser);
15714                   /* If the next token is not a semicolon, that is
15715                      probably because we just skipped over the body of
15716                      a function.  So, we consume a semicolon if
15717                      present, but do not issue an error message if it
15718                      is not present.  */
15719                   if (cp_lexer_next_token_is (parser->lexer,
15720                                               CPP_SEMICOLON))
15721                     cp_lexer_consume_token (parser->lexer);
15722                   return;
15723                 }
15724
15725               if (declares_class_or_enum & 2)
15726                 cp_parser_check_for_definition_in_return_type
15727                                             (declarator, decl_specifiers.type,
15728                                              decl_specifiers.type_location);
15729
15730               /* Look for an asm-specification.  */
15731               asm_specification = cp_parser_asm_specification_opt (parser);
15732               /* Look for attributes that apply to the declaration.  */
15733               attributes = cp_parser_attributes_opt (parser);
15734               /* Remember which attributes are prefix attributes and
15735                  which are not.  */
15736               first_attribute = attributes;
15737               /* Combine the attributes.  */
15738               attributes = chainon (prefix_attributes, attributes);
15739
15740               /* If it's an `=', then we have a constant-initializer or a
15741                  pure-specifier.  It is not correct to parse the
15742                  initializer before registering the member declaration
15743                  since the member declaration should be in scope while
15744                  its initializer is processed.  However, the rest of the
15745                  front end does not yet provide an interface that allows
15746                  us to handle this correctly.  */
15747               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15748                 {
15749                   /* In [class.mem]:
15750
15751                      A pure-specifier shall be used only in the declaration of
15752                      a virtual function.
15753
15754                      A member-declarator can contain a constant-initializer
15755                      only if it declares a static member of integral or
15756                      enumeration type.
15757
15758                      Therefore, if the DECLARATOR is for a function, we look
15759                      for a pure-specifier; otherwise, we look for a
15760                      constant-initializer.  When we call `grokfield', it will
15761                      perform more stringent semantics checks.  */
15762                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15763                   if (function_declarator_p (declarator))
15764                     initializer = cp_parser_pure_specifier (parser);
15765                   else
15766                     /* Parse the initializer.  */
15767                     initializer = cp_parser_constant_initializer (parser);
15768                 }
15769               /* Otherwise, there is no initializer.  */
15770               else
15771                 initializer = NULL_TREE;
15772
15773               /* See if we are probably looking at a function
15774                  definition.  We are certainly not looking at a
15775                  member-declarator.  Calling `grokfield' has
15776                  side-effects, so we must not do it unless we are sure
15777                  that we are looking at a member-declarator.  */
15778               if (cp_parser_token_starts_function_definition_p
15779                   (cp_lexer_peek_token (parser->lexer)))
15780                 {
15781                   /* The grammar does not allow a pure-specifier to be
15782                      used when a member function is defined.  (It is
15783                      possible that this fact is an oversight in the
15784                      standard, since a pure function may be defined
15785                      outside of the class-specifier.  */
15786                   if (initializer)
15787                     error ("%Hpure-specifier on function-definition",
15788                            &initializer_token_start->location);
15789                   decl = cp_parser_save_member_function_body (parser,
15790                                                               &decl_specifiers,
15791                                                               declarator,
15792                                                               attributes);
15793                   /* If the member was not a friend, declare it here.  */
15794                   if (!friend_p)
15795                     finish_member_declaration (decl);
15796                   /* Peek at the next token.  */
15797                   token = cp_lexer_peek_token (parser->lexer);
15798                   /* If the next token is a semicolon, consume it.  */
15799                   if (token->type == CPP_SEMICOLON)
15800                     cp_lexer_consume_token (parser->lexer);
15801                   return;
15802                 }
15803               else
15804                 if (declarator->kind == cdk_function)
15805                   declarator->id_loc = token->location;
15806                 /* Create the declaration.  */
15807                 decl = grokfield (declarator, &decl_specifiers,
15808                                   initializer, /*init_const_expr_p=*/true,
15809                                   asm_specification,
15810                                   attributes);
15811             }
15812
15813           /* Reset PREFIX_ATTRIBUTES.  */
15814           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15815             attributes = TREE_CHAIN (attributes);
15816           if (attributes)
15817             TREE_CHAIN (attributes) = NULL_TREE;
15818
15819           /* If there is any qualification still in effect, clear it
15820              now; we will be starting fresh with the next declarator.  */
15821           parser->scope = NULL_TREE;
15822           parser->qualifying_scope = NULL_TREE;
15823           parser->object_scope = NULL_TREE;
15824           /* If it's a `,', then there are more declarators.  */
15825           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15826             cp_lexer_consume_token (parser->lexer);
15827           /* If the next token isn't a `;', then we have a parse error.  */
15828           else if (cp_lexer_next_token_is_not (parser->lexer,
15829                                                CPP_SEMICOLON))
15830             {
15831               cp_parser_error (parser, "expected %<;%>");
15832               /* Skip tokens until we find a `;'.  */
15833               cp_parser_skip_to_end_of_statement (parser);
15834
15835               break;
15836             }
15837
15838           if (decl)
15839             {
15840               /* Add DECL to the list of members.  */
15841               if (!friend_p)
15842                 finish_member_declaration (decl);
15843
15844               if (TREE_CODE (decl) == FUNCTION_DECL)
15845                 cp_parser_save_default_args (parser, decl);
15846             }
15847         }
15848     }
15849
15850   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15851 }
15852
15853 /* Parse a pure-specifier.
15854
15855    pure-specifier:
15856      = 0
15857
15858    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15859    Otherwise, ERROR_MARK_NODE is returned.  */
15860
15861 static tree
15862 cp_parser_pure_specifier (cp_parser* parser)
15863 {
15864   cp_token *token;
15865
15866   /* Look for the `=' token.  */
15867   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15868     return error_mark_node;
15869   /* Look for the `0' token.  */
15870   token = cp_lexer_consume_token (parser->lexer);
15871
15872   /* Accept = default or = delete in c++0x mode.  */
15873   if (token->keyword == RID_DEFAULT
15874       || token->keyword == RID_DELETE)
15875     {
15876       maybe_warn_cpp0x ("defaulted and deleted functions");
15877       return token->u.value;
15878     }
15879
15880   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15881   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15882     {
15883       cp_parser_error (parser,
15884                        "invalid pure specifier (only %<= 0%> is allowed)");
15885       cp_parser_skip_to_end_of_statement (parser);
15886       return error_mark_node;
15887     }
15888   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15889     {
15890       error ("%Htemplates may not be %<virtual%>", &token->location);
15891       return error_mark_node;
15892     }
15893
15894   return integer_zero_node;
15895 }
15896
15897 /* Parse a constant-initializer.
15898
15899    constant-initializer:
15900      = constant-expression
15901
15902    Returns a representation of the constant-expression.  */
15903
15904 static tree
15905 cp_parser_constant_initializer (cp_parser* parser)
15906 {
15907   /* Look for the `=' token.  */
15908   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15909     return error_mark_node;
15910
15911   /* It is invalid to write:
15912
15913        struct S { static const int i = { 7 }; };
15914
15915      */
15916   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15917     {
15918       cp_parser_error (parser,
15919                        "a brace-enclosed initializer is not allowed here");
15920       /* Consume the opening brace.  */
15921       cp_lexer_consume_token (parser->lexer);
15922       /* Skip the initializer.  */
15923       cp_parser_skip_to_closing_brace (parser);
15924       /* Look for the trailing `}'.  */
15925       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15926
15927       return error_mark_node;
15928     }
15929
15930   return cp_parser_constant_expression (parser,
15931                                         /*allow_non_constant=*/false,
15932                                         NULL);
15933 }
15934
15935 /* Derived classes [gram.class.derived] */
15936
15937 /* Parse a base-clause.
15938
15939    base-clause:
15940      : base-specifier-list
15941
15942    base-specifier-list:
15943      base-specifier ... [opt]
15944      base-specifier-list , base-specifier ... [opt]
15945
15946    Returns a TREE_LIST representing the base-classes, in the order in
15947    which they were declared.  The representation of each node is as
15948    described by cp_parser_base_specifier.
15949
15950    In the case that no bases are specified, this function will return
15951    NULL_TREE, not ERROR_MARK_NODE.  */
15952
15953 static tree
15954 cp_parser_base_clause (cp_parser* parser)
15955 {
15956   tree bases = NULL_TREE;
15957
15958   /* Look for the `:' that begins the list.  */
15959   cp_parser_require (parser, CPP_COLON, "%<:%>");
15960
15961   /* Scan the base-specifier-list.  */
15962   while (true)
15963     {
15964       cp_token *token;
15965       tree base;
15966       bool pack_expansion_p = false;
15967
15968       /* Look for the base-specifier.  */
15969       base = cp_parser_base_specifier (parser);
15970       /* Look for the (optional) ellipsis. */
15971       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15972         {
15973           /* Consume the `...'. */
15974           cp_lexer_consume_token (parser->lexer);
15975
15976           pack_expansion_p = true;
15977         }
15978
15979       /* Add BASE to the front of the list.  */
15980       if (base != error_mark_node)
15981         {
15982           if (pack_expansion_p)
15983             /* Make this a pack expansion type. */
15984             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15985           
15986
15987           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15988             {
15989               TREE_CHAIN (base) = bases;
15990               bases = base;
15991             }
15992         }
15993       /* Peek at the next token.  */
15994       token = cp_lexer_peek_token (parser->lexer);
15995       /* If it's not a comma, then the list is complete.  */
15996       if (token->type != CPP_COMMA)
15997         break;
15998       /* Consume the `,'.  */
15999       cp_lexer_consume_token (parser->lexer);
16000     }
16001
16002   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16003      base class had a qualified name.  However, the next name that
16004      appears is certainly not qualified.  */
16005   parser->scope = NULL_TREE;
16006   parser->qualifying_scope = NULL_TREE;
16007   parser->object_scope = NULL_TREE;
16008
16009   return nreverse (bases);
16010 }
16011
16012 /* Parse a base-specifier.
16013
16014    base-specifier:
16015      :: [opt] nested-name-specifier [opt] class-name
16016      virtual access-specifier [opt] :: [opt] nested-name-specifier
16017        [opt] class-name
16018      access-specifier virtual [opt] :: [opt] nested-name-specifier
16019        [opt] class-name
16020
16021    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16022    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16023    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16024    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16025
16026 static tree
16027 cp_parser_base_specifier (cp_parser* parser)
16028 {
16029   cp_token *token;
16030   bool done = false;
16031   bool virtual_p = false;
16032   bool duplicate_virtual_error_issued_p = false;
16033   bool duplicate_access_error_issued_p = false;
16034   bool class_scope_p, template_p;
16035   tree access = access_default_node;
16036   tree type;
16037
16038   /* Process the optional `virtual' and `access-specifier'.  */
16039   while (!done)
16040     {
16041       /* Peek at the next token.  */
16042       token = cp_lexer_peek_token (parser->lexer);
16043       /* Process `virtual'.  */
16044       switch (token->keyword)
16045         {
16046         case RID_VIRTUAL:
16047           /* If `virtual' appears more than once, issue an error.  */
16048           if (virtual_p && !duplicate_virtual_error_issued_p)
16049             {
16050               cp_parser_error (parser,
16051                                "%<virtual%> specified more than once in base-specified");
16052               duplicate_virtual_error_issued_p = true;
16053             }
16054
16055           virtual_p = true;
16056
16057           /* Consume the `virtual' token.  */
16058           cp_lexer_consume_token (parser->lexer);
16059
16060           break;
16061
16062         case RID_PUBLIC:
16063         case RID_PROTECTED:
16064         case RID_PRIVATE:
16065           /* If more than one access specifier appears, issue an
16066              error.  */
16067           if (access != access_default_node
16068               && !duplicate_access_error_issued_p)
16069             {
16070               cp_parser_error (parser,
16071                                "more than one access specifier in base-specified");
16072               duplicate_access_error_issued_p = true;
16073             }
16074
16075           access = ridpointers[(int) token->keyword];
16076
16077           /* Consume the access-specifier.  */
16078           cp_lexer_consume_token (parser->lexer);
16079
16080           break;
16081
16082         default:
16083           done = true;
16084           break;
16085         }
16086     }
16087   /* It is not uncommon to see programs mechanically, erroneously, use
16088      the 'typename' keyword to denote (dependent) qualified types
16089      as base classes.  */
16090   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16091     {
16092       token = cp_lexer_peek_token (parser->lexer);
16093       if (!processing_template_decl)
16094         error ("%Hkeyword %<typename%> not allowed outside of templates",
16095                &token->location);
16096       else
16097         error ("%Hkeyword %<typename%> not allowed in this context "
16098                "(the base class is implicitly a type)",
16099                &token->location);
16100       cp_lexer_consume_token (parser->lexer);
16101     }
16102
16103   /* Look for the optional `::' operator.  */
16104   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16105   /* Look for the nested-name-specifier.  The simplest way to
16106      implement:
16107
16108        [temp.res]
16109
16110        The keyword `typename' is not permitted in a base-specifier or
16111        mem-initializer; in these contexts a qualified name that
16112        depends on a template-parameter is implicitly assumed to be a
16113        type name.
16114
16115      is to pretend that we have seen the `typename' keyword at this
16116      point.  */
16117   cp_parser_nested_name_specifier_opt (parser,
16118                                        /*typename_keyword_p=*/true,
16119                                        /*check_dependency_p=*/true,
16120                                        typename_type,
16121                                        /*is_declaration=*/true);
16122   /* If the base class is given by a qualified name, assume that names
16123      we see are type names or templates, as appropriate.  */
16124   class_scope_p = (parser->scope && TYPE_P (parser->scope));
16125   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16126
16127   /* Finally, look for the class-name.  */
16128   type = cp_parser_class_name (parser,
16129                                class_scope_p,
16130                                template_p,
16131                                typename_type,
16132                                /*check_dependency_p=*/true,
16133                                /*class_head_p=*/false,
16134                                /*is_declaration=*/true);
16135
16136   if (type == error_mark_node)
16137     return error_mark_node;
16138
16139   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16140 }
16141
16142 /* Exception handling [gram.exception] */
16143
16144 /* Parse an (optional) exception-specification.
16145
16146    exception-specification:
16147      throw ( type-id-list [opt] )
16148
16149    Returns a TREE_LIST representing the exception-specification.  The
16150    TREE_VALUE of each node is a type.  */
16151
16152 static tree
16153 cp_parser_exception_specification_opt (cp_parser* parser)
16154 {
16155   cp_token *token;
16156   tree type_id_list;
16157
16158   /* Peek at the next token.  */
16159   token = cp_lexer_peek_token (parser->lexer);
16160   /* If it's not `throw', then there's no exception-specification.  */
16161   if (!cp_parser_is_keyword (token, RID_THROW))
16162     return NULL_TREE;
16163
16164   /* Consume the `throw'.  */
16165   cp_lexer_consume_token (parser->lexer);
16166
16167   /* Look for the `('.  */
16168   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16169
16170   /* Peek at the next token.  */
16171   token = cp_lexer_peek_token (parser->lexer);
16172   /* If it's not a `)', then there is a type-id-list.  */
16173   if (token->type != CPP_CLOSE_PAREN)
16174     {
16175       const char *saved_message;
16176
16177       /* Types may not be defined in an exception-specification.  */
16178       saved_message = parser->type_definition_forbidden_message;
16179       parser->type_definition_forbidden_message
16180         = "types may not be defined in an exception-specification";
16181       /* Parse the type-id-list.  */
16182       type_id_list = cp_parser_type_id_list (parser);
16183       /* Restore the saved message.  */
16184       parser->type_definition_forbidden_message = saved_message;
16185     }
16186   else
16187     type_id_list = empty_except_spec;
16188
16189   /* Look for the `)'.  */
16190   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16191
16192   return type_id_list;
16193 }
16194
16195 /* Parse an (optional) type-id-list.
16196
16197    type-id-list:
16198      type-id ... [opt]
16199      type-id-list , type-id ... [opt]
16200
16201    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16202    in the order that the types were presented.  */
16203
16204 static tree
16205 cp_parser_type_id_list (cp_parser* parser)
16206 {
16207   tree types = NULL_TREE;
16208
16209   while (true)
16210     {
16211       cp_token *token;
16212       tree type;
16213
16214       /* Get the next type-id.  */
16215       type = cp_parser_type_id (parser);
16216       /* Parse the optional ellipsis. */
16217       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16218         {
16219           /* Consume the `...'. */
16220           cp_lexer_consume_token (parser->lexer);
16221
16222           /* Turn the type into a pack expansion expression. */
16223           type = make_pack_expansion (type);
16224         }
16225       /* Add it to the list.  */
16226       types = add_exception_specifier (types, type, /*complain=*/1);
16227       /* Peek at the next token.  */
16228       token = cp_lexer_peek_token (parser->lexer);
16229       /* If it is not a `,', we are done.  */
16230       if (token->type != CPP_COMMA)
16231         break;
16232       /* Consume the `,'.  */
16233       cp_lexer_consume_token (parser->lexer);
16234     }
16235
16236   return nreverse (types);
16237 }
16238
16239 /* Parse a try-block.
16240
16241    try-block:
16242      try compound-statement handler-seq  */
16243
16244 static tree
16245 cp_parser_try_block (cp_parser* parser)
16246 {
16247   tree try_block;
16248
16249   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16250   try_block = begin_try_block ();
16251   cp_parser_compound_statement (parser, NULL, true);
16252   finish_try_block (try_block);
16253   cp_parser_handler_seq (parser);
16254   finish_handler_sequence (try_block);
16255
16256   return try_block;
16257 }
16258
16259 /* Parse a function-try-block.
16260
16261    function-try-block:
16262      try ctor-initializer [opt] function-body handler-seq  */
16263
16264 static bool
16265 cp_parser_function_try_block (cp_parser* parser)
16266 {
16267   tree compound_stmt;
16268   tree try_block;
16269   bool ctor_initializer_p;
16270
16271   /* Look for the `try' keyword.  */
16272   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16273     return false;
16274   /* Let the rest of the front end know where we are.  */
16275   try_block = begin_function_try_block (&compound_stmt);
16276   /* Parse the function-body.  */
16277   ctor_initializer_p
16278     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16279   /* We're done with the `try' part.  */
16280   finish_function_try_block (try_block);
16281   /* Parse the handlers.  */
16282   cp_parser_handler_seq (parser);
16283   /* We're done with the handlers.  */
16284   finish_function_handler_sequence (try_block, compound_stmt);
16285
16286   return ctor_initializer_p;
16287 }
16288
16289 /* Parse a handler-seq.
16290
16291    handler-seq:
16292      handler handler-seq [opt]  */
16293
16294 static void
16295 cp_parser_handler_seq (cp_parser* parser)
16296 {
16297   while (true)
16298     {
16299       cp_token *token;
16300
16301       /* Parse the handler.  */
16302       cp_parser_handler (parser);
16303       /* Peek at the next token.  */
16304       token = cp_lexer_peek_token (parser->lexer);
16305       /* If it's not `catch' then there are no more handlers.  */
16306       if (!cp_parser_is_keyword (token, RID_CATCH))
16307         break;
16308     }
16309 }
16310
16311 /* Parse a handler.
16312
16313    handler:
16314      catch ( exception-declaration ) compound-statement  */
16315
16316 static void
16317 cp_parser_handler (cp_parser* parser)
16318 {
16319   tree handler;
16320   tree declaration;
16321
16322   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16323   handler = begin_handler ();
16324   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16325   declaration = cp_parser_exception_declaration (parser);
16326   finish_handler_parms (declaration, handler);
16327   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16328   cp_parser_compound_statement (parser, NULL, false);
16329   finish_handler (handler);
16330 }
16331
16332 /* Parse an exception-declaration.
16333
16334    exception-declaration:
16335      type-specifier-seq declarator
16336      type-specifier-seq abstract-declarator
16337      type-specifier-seq
16338      ...
16339
16340    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16341    ellipsis variant is used.  */
16342
16343 static tree
16344 cp_parser_exception_declaration (cp_parser* parser)
16345 {
16346   cp_decl_specifier_seq type_specifiers;
16347   cp_declarator *declarator;
16348   const char *saved_message;
16349
16350   /* If it's an ellipsis, it's easy to handle.  */
16351   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16352     {
16353       /* Consume the `...' token.  */
16354       cp_lexer_consume_token (parser->lexer);
16355       return NULL_TREE;
16356     }
16357
16358   /* Types may not be defined in exception-declarations.  */
16359   saved_message = parser->type_definition_forbidden_message;
16360   parser->type_definition_forbidden_message
16361     = "types may not be defined in exception-declarations";
16362
16363   /* Parse the type-specifier-seq.  */
16364   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16365                                 &type_specifiers);
16366   /* If it's a `)', then there is no declarator.  */
16367   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16368     declarator = NULL;
16369   else
16370     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16371                                        /*ctor_dtor_or_conv_p=*/NULL,
16372                                        /*parenthesized_p=*/NULL,
16373                                        /*member_p=*/false);
16374
16375   /* Restore the saved message.  */
16376   parser->type_definition_forbidden_message = saved_message;
16377
16378   if (!type_specifiers.any_specifiers_p)
16379     return error_mark_node;
16380
16381   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16382 }
16383
16384 /* Parse a throw-expression.
16385
16386    throw-expression:
16387      throw assignment-expression [opt]
16388
16389    Returns a THROW_EXPR representing the throw-expression.  */
16390
16391 static tree
16392 cp_parser_throw_expression (cp_parser* parser)
16393 {
16394   tree expression;
16395   cp_token* token;
16396
16397   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16398   token = cp_lexer_peek_token (parser->lexer);
16399   /* Figure out whether or not there is an assignment-expression
16400      following the "throw" keyword.  */
16401   if (token->type == CPP_COMMA
16402       || token->type == CPP_SEMICOLON
16403       || token->type == CPP_CLOSE_PAREN
16404       || token->type == CPP_CLOSE_SQUARE
16405       || token->type == CPP_CLOSE_BRACE
16406       || token->type == CPP_COLON)
16407     expression = NULL_TREE;
16408   else
16409     expression = cp_parser_assignment_expression (parser,
16410                                                   /*cast_p=*/false);
16411
16412   return build_throw (expression);
16413 }
16414
16415 /* GNU Extensions */
16416
16417 /* Parse an (optional) asm-specification.
16418
16419    asm-specification:
16420      asm ( string-literal )
16421
16422    If the asm-specification is present, returns a STRING_CST
16423    corresponding to the string-literal.  Otherwise, returns
16424    NULL_TREE.  */
16425
16426 static tree
16427 cp_parser_asm_specification_opt (cp_parser* parser)
16428 {
16429   cp_token *token;
16430   tree asm_specification;
16431
16432   /* Peek at the next token.  */
16433   token = cp_lexer_peek_token (parser->lexer);
16434   /* If the next token isn't the `asm' keyword, then there's no
16435      asm-specification.  */
16436   if (!cp_parser_is_keyword (token, RID_ASM))
16437     return NULL_TREE;
16438
16439   /* Consume the `asm' token.  */
16440   cp_lexer_consume_token (parser->lexer);
16441   /* Look for the `('.  */
16442   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16443
16444   /* Look for the string-literal.  */
16445   asm_specification = cp_parser_string_literal (parser, false, false);
16446
16447   /* Look for the `)'.  */
16448   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16449
16450   return asm_specification;
16451 }
16452
16453 /* Parse an asm-operand-list.
16454
16455    asm-operand-list:
16456      asm-operand
16457      asm-operand-list , asm-operand
16458
16459    asm-operand:
16460      string-literal ( expression )
16461      [ string-literal ] string-literal ( expression )
16462
16463    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16464    each node is the expression.  The TREE_PURPOSE is itself a
16465    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16466    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16467    is a STRING_CST for the string literal before the parenthesis. Returns
16468    ERROR_MARK_NODE if any of the operands are invalid.  */
16469
16470 static tree
16471 cp_parser_asm_operand_list (cp_parser* parser)
16472 {
16473   tree asm_operands = NULL_TREE;
16474   bool invalid_operands = false;
16475
16476   while (true)
16477     {
16478       tree string_literal;
16479       tree expression;
16480       tree name;
16481
16482       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16483         {
16484           /* Consume the `[' token.  */
16485           cp_lexer_consume_token (parser->lexer);
16486           /* Read the operand name.  */
16487           name = cp_parser_identifier (parser);
16488           if (name != error_mark_node)
16489             name = build_string (IDENTIFIER_LENGTH (name),
16490                                  IDENTIFIER_POINTER (name));
16491           /* Look for the closing `]'.  */
16492           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16493         }
16494       else
16495         name = NULL_TREE;
16496       /* Look for the string-literal.  */
16497       string_literal = cp_parser_string_literal (parser, false, false);
16498
16499       /* Look for the `('.  */
16500       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16501       /* Parse the expression.  */
16502       expression = cp_parser_expression (parser, /*cast_p=*/false);
16503       /* Look for the `)'.  */
16504       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16505
16506       if (name == error_mark_node 
16507           || string_literal == error_mark_node 
16508           || expression == error_mark_node)
16509         invalid_operands = true;
16510
16511       /* Add this operand to the list.  */
16512       asm_operands = tree_cons (build_tree_list (name, string_literal),
16513                                 expression,
16514                                 asm_operands);
16515       /* If the next token is not a `,', there are no more
16516          operands.  */
16517       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16518         break;
16519       /* Consume the `,'.  */
16520       cp_lexer_consume_token (parser->lexer);
16521     }
16522
16523   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16524 }
16525
16526 /* Parse an asm-clobber-list.
16527
16528    asm-clobber-list:
16529      string-literal
16530      asm-clobber-list , string-literal
16531
16532    Returns a TREE_LIST, indicating the clobbers in the order that they
16533    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16534
16535 static tree
16536 cp_parser_asm_clobber_list (cp_parser* parser)
16537 {
16538   tree clobbers = NULL_TREE;
16539
16540   while (true)
16541     {
16542       tree string_literal;
16543
16544       /* Look for the string literal.  */
16545       string_literal = cp_parser_string_literal (parser, false, false);
16546       /* Add it to the list.  */
16547       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16548       /* If the next token is not a `,', then the list is
16549          complete.  */
16550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16551         break;
16552       /* Consume the `,' token.  */
16553       cp_lexer_consume_token (parser->lexer);
16554     }
16555
16556   return clobbers;
16557 }
16558
16559 /* Parse an (optional) series of attributes.
16560
16561    attributes:
16562      attributes attribute
16563
16564    attribute:
16565      __attribute__ (( attribute-list [opt] ))
16566
16567    The return value is as for cp_parser_attribute_list.  */
16568
16569 static tree
16570 cp_parser_attributes_opt (cp_parser* parser)
16571 {
16572   tree attributes = NULL_TREE;
16573
16574   while (true)
16575     {
16576       cp_token *token;
16577       tree attribute_list;
16578
16579       /* Peek at the next token.  */
16580       token = cp_lexer_peek_token (parser->lexer);
16581       /* If it's not `__attribute__', then we're done.  */
16582       if (token->keyword != RID_ATTRIBUTE)
16583         break;
16584
16585       /* Consume the `__attribute__' keyword.  */
16586       cp_lexer_consume_token (parser->lexer);
16587       /* Look for the two `(' tokens.  */
16588       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16589       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16590
16591       /* Peek at the next token.  */
16592       token = cp_lexer_peek_token (parser->lexer);
16593       if (token->type != CPP_CLOSE_PAREN)
16594         /* Parse the attribute-list.  */
16595         attribute_list = cp_parser_attribute_list (parser);
16596       else
16597         /* If the next token is a `)', then there is no attribute
16598            list.  */
16599         attribute_list = NULL;
16600
16601       /* Look for the two `)' tokens.  */
16602       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16603       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16604
16605       /* Add these new attributes to the list.  */
16606       attributes = chainon (attributes, attribute_list);
16607     }
16608
16609   return attributes;
16610 }
16611
16612 /* Parse an attribute-list.
16613
16614    attribute-list:
16615      attribute
16616      attribute-list , attribute
16617
16618    attribute:
16619      identifier
16620      identifier ( identifier )
16621      identifier ( identifier , expression-list )
16622      identifier ( expression-list )
16623
16624    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16625    to an attribute.  The TREE_PURPOSE of each node is the identifier
16626    indicating which attribute is in use.  The TREE_VALUE represents
16627    the arguments, if any.  */
16628
16629 static tree
16630 cp_parser_attribute_list (cp_parser* parser)
16631 {
16632   tree attribute_list = NULL_TREE;
16633   bool save_translate_strings_p = parser->translate_strings_p;
16634
16635   parser->translate_strings_p = false;
16636   while (true)
16637     {
16638       cp_token *token;
16639       tree identifier;
16640       tree attribute;
16641
16642       /* Look for the identifier.  We also allow keywords here; for
16643          example `__attribute__ ((const))' is legal.  */
16644       token = cp_lexer_peek_token (parser->lexer);
16645       if (token->type == CPP_NAME
16646           || token->type == CPP_KEYWORD)
16647         {
16648           tree arguments = NULL_TREE;
16649
16650           /* Consume the token.  */
16651           token = cp_lexer_consume_token (parser->lexer);
16652
16653           /* Save away the identifier that indicates which attribute
16654              this is.  */
16655           identifier = token->u.value;
16656           attribute = build_tree_list (identifier, NULL_TREE);
16657
16658           /* Peek at the next token.  */
16659           token = cp_lexer_peek_token (parser->lexer);
16660           /* If it's an `(', then parse the attribute arguments.  */
16661           if (token->type == CPP_OPEN_PAREN)
16662             {
16663               arguments = cp_parser_parenthesized_expression_list
16664                           (parser, true, /*cast_p=*/false,
16665                            /*allow_expansion_p=*/false,
16666                            /*non_constant_p=*/NULL);
16667               /* Save the arguments away.  */
16668               TREE_VALUE (attribute) = arguments;
16669             }
16670
16671           if (arguments != error_mark_node)
16672             {
16673               /* Add this attribute to the list.  */
16674               TREE_CHAIN (attribute) = attribute_list;
16675               attribute_list = attribute;
16676             }
16677
16678           token = cp_lexer_peek_token (parser->lexer);
16679         }
16680       /* Now, look for more attributes.  If the next token isn't a
16681          `,', we're done.  */
16682       if (token->type != CPP_COMMA)
16683         break;
16684
16685       /* Consume the comma and keep going.  */
16686       cp_lexer_consume_token (parser->lexer);
16687     }
16688   parser->translate_strings_p = save_translate_strings_p;
16689
16690   /* We built up the list in reverse order.  */
16691   return nreverse (attribute_list);
16692 }
16693
16694 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16695    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16696    current value of the PEDANTIC flag, regardless of whether or not
16697    the `__extension__' keyword is present.  The caller is responsible
16698    for restoring the value of the PEDANTIC flag.  */
16699
16700 static bool
16701 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16702 {
16703   /* Save the old value of the PEDANTIC flag.  */
16704   *saved_pedantic = pedantic;
16705
16706   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16707     {
16708       /* Consume the `__extension__' token.  */
16709       cp_lexer_consume_token (parser->lexer);
16710       /* We're not being pedantic while the `__extension__' keyword is
16711          in effect.  */
16712       pedantic = 0;
16713
16714       return true;
16715     }
16716
16717   return false;
16718 }
16719
16720 /* Parse a label declaration.
16721
16722    label-declaration:
16723      __label__ label-declarator-seq ;
16724
16725    label-declarator-seq:
16726      identifier , label-declarator-seq
16727      identifier  */
16728
16729 static void
16730 cp_parser_label_declaration (cp_parser* parser)
16731 {
16732   /* Look for the `__label__' keyword.  */
16733   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16734
16735   while (true)
16736     {
16737       tree identifier;
16738
16739       /* Look for an identifier.  */
16740       identifier = cp_parser_identifier (parser);
16741       /* If we failed, stop.  */
16742       if (identifier == error_mark_node)
16743         break;
16744       /* Declare it as a label.  */
16745       finish_label_decl (identifier);
16746       /* If the next token is a `;', stop.  */
16747       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16748         break;
16749       /* Look for the `,' separating the label declarations.  */
16750       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16751     }
16752
16753   /* Look for the final `;'.  */
16754   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16755 }
16756
16757 /* Support Functions */
16758
16759 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16760    NAME should have one of the representations used for an
16761    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16762    is returned.  If PARSER->SCOPE is a dependent type, then a
16763    SCOPE_REF is returned.
16764
16765    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16766    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16767    was formed.  Abstractly, such entities should not be passed to this
16768    function, because they do not need to be looked up, but it is
16769    simpler to check for this special case here, rather than at the
16770    call-sites.
16771
16772    In cases not explicitly covered above, this function returns a
16773    DECL, OVERLOAD, or baselink representing the result of the lookup.
16774    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16775    is returned.
16776
16777    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16778    (e.g., "struct") that was used.  In that case bindings that do not
16779    refer to types are ignored.
16780
16781    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16782    ignored.
16783
16784    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16785    are ignored.
16786
16787    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16788    types.
16789
16790    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16791    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16792    NULL_TREE otherwise.  */
16793
16794 static tree
16795 cp_parser_lookup_name (cp_parser *parser, tree name,
16796                        enum tag_types tag_type,
16797                        bool is_template,
16798                        bool is_namespace,
16799                        bool check_dependency,
16800                        tree *ambiguous_decls,
16801                        location_t name_location)
16802 {
16803   int flags = 0;
16804   tree decl;
16805   tree object_type = parser->context->object_type;
16806
16807   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16808     flags |= LOOKUP_COMPLAIN;
16809
16810   /* Assume that the lookup will be unambiguous.  */
16811   if (ambiguous_decls)
16812     *ambiguous_decls = NULL_TREE;
16813
16814   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16815      no longer valid.  Note that if we are parsing tentatively, and
16816      the parse fails, OBJECT_TYPE will be automatically restored.  */
16817   parser->context->object_type = NULL_TREE;
16818
16819   if (name == error_mark_node)
16820     return error_mark_node;
16821
16822   /* A template-id has already been resolved; there is no lookup to
16823      do.  */
16824   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16825     return name;
16826   if (BASELINK_P (name))
16827     {
16828       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16829                   == TEMPLATE_ID_EXPR);
16830       return name;
16831     }
16832
16833   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16834      it should already have been checked to make sure that the name
16835      used matches the type being destroyed.  */
16836   if (TREE_CODE (name) == BIT_NOT_EXPR)
16837     {
16838       tree type;
16839
16840       /* Figure out to which type this destructor applies.  */
16841       if (parser->scope)
16842         type = parser->scope;
16843       else if (object_type)
16844         type = object_type;
16845       else
16846         type = current_class_type;
16847       /* If that's not a class type, there is no destructor.  */
16848       if (!type || !CLASS_TYPE_P (type))
16849         return error_mark_node;
16850       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16851         lazily_declare_fn (sfk_destructor, type);
16852       if (!CLASSTYPE_DESTRUCTORS (type))
16853           return error_mark_node;
16854       /* If it was a class type, return the destructor.  */
16855       return CLASSTYPE_DESTRUCTORS (type);
16856     }
16857
16858   /* By this point, the NAME should be an ordinary identifier.  If
16859      the id-expression was a qualified name, the qualifying scope is
16860      stored in PARSER->SCOPE at this point.  */
16861   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16862
16863   /* Perform the lookup.  */
16864   if (parser->scope)
16865     {
16866       bool dependent_p;
16867
16868       if (parser->scope == error_mark_node)
16869         return error_mark_node;
16870
16871       /* If the SCOPE is dependent, the lookup must be deferred until
16872          the template is instantiated -- unless we are explicitly
16873          looking up names in uninstantiated templates.  Even then, we
16874          cannot look up the name if the scope is not a class type; it
16875          might, for example, be a template type parameter.  */
16876       dependent_p = (TYPE_P (parser->scope)
16877                      && !(parser->in_declarator_p
16878                           && currently_open_class (parser->scope))
16879                      && dependent_type_p (parser->scope));
16880       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16881            && dependent_p)
16882         {
16883           if (tag_type)
16884             {
16885               tree type;
16886
16887               /* The resolution to Core Issue 180 says that `struct
16888                  A::B' should be considered a type-name, even if `A'
16889                  is dependent.  */
16890               type = make_typename_type (parser->scope, name, tag_type,
16891                                          /*complain=*/tf_error);
16892               decl = TYPE_NAME (type);
16893             }
16894           else if (is_template
16895                    && (cp_parser_next_token_ends_template_argument_p (parser)
16896                        || cp_lexer_next_token_is (parser->lexer,
16897                                                   CPP_CLOSE_PAREN)))
16898             decl = make_unbound_class_template (parser->scope,
16899                                                 name, NULL_TREE,
16900                                                 /*complain=*/tf_error);
16901           else
16902             decl = build_qualified_name (/*type=*/NULL_TREE,
16903                                          parser->scope, name,
16904                                          is_template);
16905         }
16906       else
16907         {
16908           tree pushed_scope = NULL_TREE;
16909
16910           /* If PARSER->SCOPE is a dependent type, then it must be a
16911              class type, and we must not be checking dependencies;
16912              otherwise, we would have processed this lookup above.  So
16913              that PARSER->SCOPE is not considered a dependent base by
16914              lookup_member, we must enter the scope here.  */
16915           if (dependent_p)
16916             pushed_scope = push_scope (parser->scope);
16917           /* If the PARSER->SCOPE is a template specialization, it
16918              may be instantiated during name lookup.  In that case,
16919              errors may be issued.  Even if we rollback the current
16920              tentative parse, those errors are valid.  */
16921           decl = lookup_qualified_name (parser->scope, name,
16922                                         tag_type != none_type,
16923                                         /*complain=*/true);
16924
16925           /* If we have a single function from a using decl, pull it out.  */
16926           if (decl
16927               && TREE_CODE (decl) == OVERLOAD
16928               && !really_overloaded_fn (decl))
16929             decl = OVL_FUNCTION (decl);
16930
16931           if (pushed_scope)
16932             pop_scope (pushed_scope);
16933         }
16934       parser->qualifying_scope = parser->scope;
16935       parser->object_scope = NULL_TREE;
16936     }
16937   else if (object_type)
16938     {
16939       tree object_decl = NULL_TREE;
16940       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16941          OBJECT_TYPE is not a class.  */
16942       if (CLASS_TYPE_P (object_type))
16943         /* If the OBJECT_TYPE is a template specialization, it may
16944            be instantiated during name lookup.  In that case, errors
16945            may be issued.  Even if we rollback the current tentative
16946            parse, those errors are valid.  */
16947         object_decl = lookup_member (object_type,
16948                                      name,
16949                                      /*protect=*/0,
16950                                      tag_type != none_type);
16951       /* Look it up in the enclosing context, too.  */
16952       decl = lookup_name_real (name, tag_type != none_type,
16953                                /*nonclass=*/0,
16954                                /*block_p=*/true, is_namespace, flags);
16955       parser->object_scope = object_type;
16956       parser->qualifying_scope = NULL_TREE;
16957       if (object_decl)
16958         decl = object_decl;
16959     }
16960   else
16961     {
16962       decl = lookup_name_real (name, tag_type != none_type,
16963                                /*nonclass=*/0,
16964                                /*block_p=*/true, is_namespace, flags);
16965       parser->qualifying_scope = NULL_TREE;
16966       parser->object_scope = NULL_TREE;
16967     }
16968
16969   /* If the lookup failed, let our caller know.  */
16970   if (!decl || decl == error_mark_node)
16971     return error_mark_node;
16972
16973   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16974   if (TREE_CODE (decl) == TREE_LIST)
16975     {
16976       if (ambiguous_decls)
16977         *ambiguous_decls = decl;
16978       /* The error message we have to print is too complicated for
16979          cp_parser_error, so we incorporate its actions directly.  */
16980       if (!cp_parser_simulate_error (parser))
16981         {
16982           error ("%Hreference to %qD is ambiguous",
16983                  &name_location, name);
16984           print_candidates (decl);
16985         }
16986       return error_mark_node;
16987     }
16988
16989   gcc_assert (DECL_P (decl)
16990               || TREE_CODE (decl) == OVERLOAD
16991               || TREE_CODE (decl) == SCOPE_REF
16992               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16993               || BASELINK_P (decl));
16994
16995   /* If we have resolved the name of a member declaration, check to
16996      see if the declaration is accessible.  When the name resolves to
16997      set of overloaded functions, accessibility is checked when
16998      overload resolution is done.
16999
17000      During an explicit instantiation, access is not checked at all,
17001      as per [temp.explicit].  */
17002   if (DECL_P (decl))
17003     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17004
17005   return decl;
17006 }
17007
17008 /* Like cp_parser_lookup_name, but for use in the typical case where
17009    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17010    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
17011
17012 static tree
17013 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17014 {
17015   return cp_parser_lookup_name (parser, name,
17016                                 none_type,
17017                                 /*is_template=*/false,
17018                                 /*is_namespace=*/false,
17019                                 /*check_dependency=*/true,
17020                                 /*ambiguous_decls=*/NULL,
17021                                 location);
17022 }
17023
17024 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17025    the current context, return the TYPE_DECL.  If TAG_NAME_P is
17026    true, the DECL indicates the class being defined in a class-head,
17027    or declared in an elaborated-type-specifier.
17028
17029    Otherwise, return DECL.  */
17030
17031 static tree
17032 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17033 {
17034   /* If the TEMPLATE_DECL is being declared as part of a class-head,
17035      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17036
17037        struct A {
17038          template <typename T> struct B;
17039        };
17040
17041        template <typename T> struct A::B {};
17042
17043      Similarly, in an elaborated-type-specifier:
17044
17045        namespace N { struct X{}; }
17046
17047        struct A {
17048          template <typename T> friend struct N::X;
17049        };
17050
17051      However, if the DECL refers to a class type, and we are in
17052      the scope of the class, then the name lookup automatically
17053      finds the TYPE_DECL created by build_self_reference rather
17054      than a TEMPLATE_DECL.  For example, in:
17055
17056        template <class T> struct S {
17057          S s;
17058        };
17059
17060      there is no need to handle such case.  */
17061
17062   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17063     return DECL_TEMPLATE_RESULT (decl);
17064
17065   return decl;
17066 }
17067
17068 /* If too many, or too few, template-parameter lists apply to the
17069    declarator, issue an error message.  Returns TRUE if all went well,
17070    and FALSE otherwise.  */
17071
17072 static bool
17073 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17074                                                 cp_declarator *declarator,
17075                                                 location_t declarator_location)
17076 {
17077   unsigned num_templates;
17078
17079   /* We haven't seen any classes that involve template parameters yet.  */
17080   num_templates = 0;
17081
17082   switch (declarator->kind)
17083     {
17084     case cdk_id:
17085       if (declarator->u.id.qualifying_scope)
17086         {
17087           tree scope;
17088           tree member;
17089
17090           scope = declarator->u.id.qualifying_scope;
17091           member = declarator->u.id.unqualified_name;
17092
17093           while (scope && CLASS_TYPE_P (scope))
17094             {
17095               /* You're supposed to have one `template <...>'
17096                  for every template class, but you don't need one
17097                  for a full specialization.  For example:
17098
17099                  template <class T> struct S{};
17100                  template <> struct S<int> { void f(); };
17101                  void S<int>::f () {}
17102
17103                  is correct; there shouldn't be a `template <>' for
17104                  the definition of `S<int>::f'.  */
17105               if (!CLASSTYPE_TEMPLATE_INFO (scope))
17106                 /* If SCOPE does not have template information of any
17107                    kind, then it is not a template, nor is it nested
17108                    within a template.  */
17109                 break;
17110               if (explicit_class_specialization_p (scope))
17111                 break;
17112               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17113                 ++num_templates;
17114
17115               scope = TYPE_CONTEXT (scope);
17116             }
17117         }
17118       else if (TREE_CODE (declarator->u.id.unqualified_name)
17119                == TEMPLATE_ID_EXPR)
17120         /* If the DECLARATOR has the form `X<y>' then it uses one
17121            additional level of template parameters.  */
17122         ++num_templates;
17123
17124       return cp_parser_check_template_parameters (parser,
17125                                                   num_templates,
17126                                                   declarator_location);
17127
17128     case cdk_function:
17129     case cdk_array:
17130     case cdk_pointer:
17131     case cdk_reference:
17132     case cdk_ptrmem:
17133       return (cp_parser_check_declarator_template_parameters
17134               (parser, declarator->declarator, declarator_location));
17135
17136     case cdk_error:
17137       return true;
17138
17139     default:
17140       gcc_unreachable ();
17141     }
17142   return false;
17143 }
17144
17145 /* NUM_TEMPLATES were used in the current declaration.  If that is
17146    invalid, return FALSE and issue an error messages.  Otherwise,
17147    return TRUE.  */
17148
17149 static bool
17150 cp_parser_check_template_parameters (cp_parser* parser,
17151                                      unsigned num_templates,
17152                                      location_t location)
17153 {
17154   /* If there are more template classes than parameter lists, we have
17155      something like:
17156
17157        template <class T> void S<T>::R<T>::f ();  */
17158   if (parser->num_template_parameter_lists < num_templates)
17159     {
17160       error ("%Htoo few template-parameter-lists", &location);
17161       return false;
17162     }
17163   /* If there are the same number of template classes and parameter
17164      lists, that's OK.  */
17165   if (parser->num_template_parameter_lists == num_templates)
17166     return true;
17167   /* If there are more, but only one more, then we are referring to a
17168      member template.  That's OK too.  */
17169   if (parser->num_template_parameter_lists == num_templates + 1)
17170       return true;
17171   /* Otherwise, there are too many template parameter lists.  We have
17172      something like:
17173
17174      template <class T> template <class U> void S::f();  */
17175   error ("%Htoo many template-parameter-lists", &location);
17176   return false;
17177 }
17178
17179 /* Parse an optional `::' token indicating that the following name is
17180    from the global namespace.  If so, PARSER->SCOPE is set to the
17181    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17182    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17183    Returns the new value of PARSER->SCOPE, if the `::' token is
17184    present, and NULL_TREE otherwise.  */
17185
17186 static tree
17187 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17188 {
17189   cp_token *token;
17190
17191   /* Peek at the next token.  */
17192   token = cp_lexer_peek_token (parser->lexer);
17193   /* If we're looking at a `::' token then we're starting from the
17194      global namespace, not our current location.  */
17195   if (token->type == CPP_SCOPE)
17196     {
17197       /* Consume the `::' token.  */
17198       cp_lexer_consume_token (parser->lexer);
17199       /* Set the SCOPE so that we know where to start the lookup.  */
17200       parser->scope = global_namespace;
17201       parser->qualifying_scope = global_namespace;
17202       parser->object_scope = NULL_TREE;
17203
17204       return parser->scope;
17205     }
17206   else if (!current_scope_valid_p)
17207     {
17208       parser->scope = NULL_TREE;
17209       parser->qualifying_scope = NULL_TREE;
17210       parser->object_scope = NULL_TREE;
17211     }
17212
17213   return NULL_TREE;
17214 }
17215
17216 /* Returns TRUE if the upcoming token sequence is the start of a
17217    constructor declarator.  If FRIEND_P is true, the declarator is
17218    preceded by the `friend' specifier.  */
17219
17220 static bool
17221 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17222 {
17223   bool constructor_p;
17224   tree type_decl = NULL_TREE;
17225   bool nested_name_p;
17226   cp_token *next_token;
17227
17228   /* The common case is that this is not a constructor declarator, so
17229      try to avoid doing lots of work if at all possible.  It's not
17230      valid declare a constructor at function scope.  */
17231   if (parser->in_function_body)
17232     return false;
17233   /* And only certain tokens can begin a constructor declarator.  */
17234   next_token = cp_lexer_peek_token (parser->lexer);
17235   if (next_token->type != CPP_NAME
17236       && next_token->type != CPP_SCOPE
17237       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17238       && next_token->type != CPP_TEMPLATE_ID)
17239     return false;
17240
17241   /* Parse tentatively; we are going to roll back all of the tokens
17242      consumed here.  */
17243   cp_parser_parse_tentatively (parser);
17244   /* Assume that we are looking at a constructor declarator.  */
17245   constructor_p = true;
17246
17247   /* Look for the optional `::' operator.  */
17248   cp_parser_global_scope_opt (parser,
17249                               /*current_scope_valid_p=*/false);
17250   /* Look for the nested-name-specifier.  */
17251   nested_name_p
17252     = (cp_parser_nested_name_specifier_opt (parser,
17253                                             /*typename_keyword_p=*/false,
17254                                             /*check_dependency_p=*/false,
17255                                             /*type_p=*/false,
17256                                             /*is_declaration=*/false)
17257        != NULL_TREE);
17258   /* Outside of a class-specifier, there must be a
17259      nested-name-specifier.  */
17260   if (!nested_name_p &&
17261       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17262        || friend_p))
17263     constructor_p = false;
17264   /* If we still think that this might be a constructor-declarator,
17265      look for a class-name.  */
17266   if (constructor_p)
17267     {
17268       /* If we have:
17269
17270            template <typename T> struct S { S(); };
17271            template <typename T> S<T>::S ();
17272
17273          we must recognize that the nested `S' names a class.
17274          Similarly, for:
17275
17276            template <typename T> S<T>::S<T> ();
17277
17278          we must recognize that the nested `S' names a template.  */
17279       type_decl = cp_parser_class_name (parser,
17280                                         /*typename_keyword_p=*/false,
17281                                         /*template_keyword_p=*/false,
17282                                         none_type,
17283                                         /*check_dependency_p=*/false,
17284                                         /*class_head_p=*/false,
17285                                         /*is_declaration=*/false);
17286       /* If there was no class-name, then this is not a constructor.  */
17287       constructor_p = !cp_parser_error_occurred (parser);
17288     }
17289
17290   /* If we're still considering a constructor, we have to see a `(',
17291      to begin the parameter-declaration-clause, followed by either a
17292      `)', an `...', or a decl-specifier.  We need to check for a
17293      type-specifier to avoid being fooled into thinking that:
17294
17295        S::S (f) (int);
17296
17297      is a constructor.  (It is actually a function named `f' that
17298      takes one parameter (of type `int') and returns a value of type
17299      `S::S'.  */
17300   if (constructor_p
17301       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17302     {
17303       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17304           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17305           /* A parameter declaration begins with a decl-specifier,
17306              which is either the "attribute" keyword, a storage class
17307              specifier, or (usually) a type-specifier.  */
17308           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17309         {
17310           tree type;
17311           tree pushed_scope = NULL_TREE;
17312           unsigned saved_num_template_parameter_lists;
17313
17314           /* Names appearing in the type-specifier should be looked up
17315              in the scope of the class.  */
17316           if (current_class_type)
17317             type = NULL_TREE;
17318           else
17319             {
17320               type = TREE_TYPE (type_decl);
17321               if (TREE_CODE (type) == TYPENAME_TYPE)
17322                 {
17323                   type = resolve_typename_type (type,
17324                                                 /*only_current_p=*/false);
17325                   if (TREE_CODE (type) == TYPENAME_TYPE)
17326                     {
17327                       cp_parser_abort_tentative_parse (parser);
17328                       return false;
17329                     }
17330                 }
17331               pushed_scope = push_scope (type);
17332             }
17333
17334           /* Inside the constructor parameter list, surrounding
17335              template-parameter-lists do not apply.  */
17336           saved_num_template_parameter_lists
17337             = parser->num_template_parameter_lists;
17338           parser->num_template_parameter_lists = 0;
17339
17340           /* Look for the type-specifier.  */
17341           cp_parser_type_specifier (parser,
17342                                     CP_PARSER_FLAGS_NONE,
17343                                     /*decl_specs=*/NULL,
17344                                     /*is_declarator=*/true,
17345                                     /*declares_class_or_enum=*/NULL,
17346                                     /*is_cv_qualifier=*/NULL);
17347
17348           parser->num_template_parameter_lists
17349             = saved_num_template_parameter_lists;
17350
17351           /* Leave the scope of the class.  */
17352           if (pushed_scope)
17353             pop_scope (pushed_scope);
17354
17355           constructor_p = !cp_parser_error_occurred (parser);
17356         }
17357     }
17358   else
17359     constructor_p = false;
17360   /* We did not really want to consume any tokens.  */
17361   cp_parser_abort_tentative_parse (parser);
17362
17363   return constructor_p;
17364 }
17365
17366 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17367    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17368    they must be performed once we are in the scope of the function.
17369
17370    Returns the function defined.  */
17371
17372 static tree
17373 cp_parser_function_definition_from_specifiers_and_declarator
17374   (cp_parser* parser,
17375    cp_decl_specifier_seq *decl_specifiers,
17376    tree attributes,
17377    const cp_declarator *declarator)
17378 {
17379   tree fn;
17380   bool success_p;
17381
17382   /* Begin the function-definition.  */
17383   success_p = start_function (decl_specifiers, declarator, attributes);
17384
17385   /* The things we're about to see are not directly qualified by any
17386      template headers we've seen thus far.  */
17387   reset_specialization ();
17388
17389   /* If there were names looked up in the decl-specifier-seq that we
17390      did not check, check them now.  We must wait until we are in the
17391      scope of the function to perform the checks, since the function
17392      might be a friend.  */
17393   perform_deferred_access_checks ();
17394
17395   if (!success_p)
17396     {
17397       /* Skip the entire function.  */
17398       cp_parser_skip_to_end_of_block_or_statement (parser);
17399       fn = error_mark_node;
17400     }
17401   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17402     {
17403       /* Seen already, skip it.  An error message has already been output.  */
17404       cp_parser_skip_to_end_of_block_or_statement (parser);
17405       fn = current_function_decl;
17406       current_function_decl = NULL_TREE;
17407       /* If this is a function from a class, pop the nested class.  */
17408       if (current_class_name)
17409         pop_nested_class ();
17410     }
17411   else
17412     fn = cp_parser_function_definition_after_declarator (parser,
17413                                                          /*inline_p=*/false);
17414
17415   return fn;
17416 }
17417
17418 /* Parse the part of a function-definition that follows the
17419    declarator.  INLINE_P is TRUE iff this function is an inline
17420    function defined with a class-specifier.
17421
17422    Returns the function defined.  */
17423
17424 static tree
17425 cp_parser_function_definition_after_declarator (cp_parser* parser,
17426                                                 bool inline_p)
17427 {
17428   tree fn;
17429   bool ctor_initializer_p = false;
17430   bool saved_in_unbraced_linkage_specification_p;
17431   bool saved_in_function_body;
17432   unsigned saved_num_template_parameter_lists;
17433   cp_token *token;
17434
17435   saved_in_function_body = parser->in_function_body;
17436   parser->in_function_body = true;
17437   /* If the next token is `return', then the code may be trying to
17438      make use of the "named return value" extension that G++ used to
17439      support.  */
17440   token = cp_lexer_peek_token (parser->lexer);
17441   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17442     {
17443       /* Consume the `return' keyword.  */
17444       cp_lexer_consume_token (parser->lexer);
17445       /* Look for the identifier that indicates what value is to be
17446          returned.  */
17447       cp_parser_identifier (parser);
17448       /* Issue an error message.  */
17449       error ("%Hnamed return values are no longer supported",
17450              &token->location);
17451       /* Skip tokens until we reach the start of the function body.  */
17452       while (true)
17453         {
17454           cp_token *token = cp_lexer_peek_token (parser->lexer);
17455           if (token->type == CPP_OPEN_BRACE
17456               || token->type == CPP_EOF
17457               || token->type == CPP_PRAGMA_EOL)
17458             break;
17459           cp_lexer_consume_token (parser->lexer);
17460         }
17461     }
17462   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17463      anything declared inside `f'.  */
17464   saved_in_unbraced_linkage_specification_p
17465     = parser->in_unbraced_linkage_specification_p;
17466   parser->in_unbraced_linkage_specification_p = false;
17467   /* Inside the function, surrounding template-parameter-lists do not
17468      apply.  */
17469   saved_num_template_parameter_lists
17470     = parser->num_template_parameter_lists;
17471   parser->num_template_parameter_lists = 0;
17472   /* If the next token is `try', then we are looking at a
17473      function-try-block.  */
17474   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17475     ctor_initializer_p = cp_parser_function_try_block (parser);
17476   /* A function-try-block includes the function-body, so we only do
17477      this next part if we're not processing a function-try-block.  */
17478   else
17479     ctor_initializer_p
17480       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17481
17482   /* Finish the function.  */
17483   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17484                         (inline_p ? 2 : 0));
17485   /* Generate code for it, if necessary.  */
17486   expand_or_defer_fn (fn);
17487   /* Restore the saved values.  */
17488   parser->in_unbraced_linkage_specification_p
17489     = saved_in_unbraced_linkage_specification_p;
17490   parser->num_template_parameter_lists
17491     = saved_num_template_parameter_lists;
17492   parser->in_function_body = saved_in_function_body;
17493
17494   return fn;
17495 }
17496
17497 /* Parse a template-declaration, assuming that the `export' (and
17498    `extern') keywords, if present, has already been scanned.  MEMBER_P
17499    is as for cp_parser_template_declaration.  */
17500
17501 static void
17502 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17503 {
17504   tree decl = NULL_TREE;
17505   VEC (deferred_access_check,gc) *checks;
17506   tree parameter_list;
17507   bool friend_p = false;
17508   bool need_lang_pop;
17509   cp_token *token;
17510
17511   /* Look for the `template' keyword.  */
17512   token = cp_lexer_peek_token (parser->lexer);
17513   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17514     return;
17515
17516   /* And the `<'.  */
17517   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17518     return;
17519   if (at_class_scope_p () && current_function_decl)
17520     {
17521       /* 14.5.2.2 [temp.mem]
17522
17523          A local class shall not have member templates.  */
17524       error ("%Hinvalid declaration of member template in local class",
17525              &token->location);
17526       cp_parser_skip_to_end_of_block_or_statement (parser);
17527       return;
17528     }
17529   /* [temp]
17530
17531      A template ... shall not have C linkage.  */
17532   if (current_lang_name == lang_name_c)
17533     {
17534       error ("%Htemplate with C linkage", &token->location);
17535       /* Give it C++ linkage to avoid confusing other parts of the
17536          front end.  */
17537       push_lang_context (lang_name_cplusplus);
17538       need_lang_pop = true;
17539     }
17540   else
17541     need_lang_pop = false;
17542
17543   /* We cannot perform access checks on the template parameter
17544      declarations until we know what is being declared, just as we
17545      cannot check the decl-specifier list.  */
17546   push_deferring_access_checks (dk_deferred);
17547
17548   /* If the next token is `>', then we have an invalid
17549      specialization.  Rather than complain about an invalid template
17550      parameter, issue an error message here.  */
17551   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17552     {
17553       cp_parser_error (parser, "invalid explicit specialization");
17554       begin_specialization ();
17555       parameter_list = NULL_TREE;
17556     }
17557   else
17558     /* Parse the template parameters.  */
17559     parameter_list = cp_parser_template_parameter_list (parser);
17560
17561   /* Get the deferred access checks from the parameter list.  These
17562      will be checked once we know what is being declared, as for a
17563      member template the checks must be performed in the scope of the
17564      class containing the member.  */
17565   checks = get_deferred_access_checks ();
17566
17567   /* Look for the `>'.  */
17568   cp_parser_skip_to_end_of_template_parameter_list (parser);
17569   /* We just processed one more parameter list.  */
17570   ++parser->num_template_parameter_lists;
17571   /* If the next token is `template', there are more template
17572      parameters.  */
17573   if (cp_lexer_next_token_is_keyword (parser->lexer,
17574                                       RID_TEMPLATE))
17575     cp_parser_template_declaration_after_export (parser, member_p);
17576   else
17577     {
17578       /* There are no access checks when parsing a template, as we do not
17579          know if a specialization will be a friend.  */
17580       push_deferring_access_checks (dk_no_check);
17581       token = cp_lexer_peek_token (parser->lexer);
17582       decl = cp_parser_single_declaration (parser,
17583                                            checks,
17584                                            member_p,
17585                                            /*explicit_specialization_p=*/false,
17586                                            &friend_p);
17587       pop_deferring_access_checks ();
17588
17589       /* If this is a member template declaration, let the front
17590          end know.  */
17591       if (member_p && !friend_p && decl)
17592         {
17593           if (TREE_CODE (decl) == TYPE_DECL)
17594             cp_parser_check_access_in_redeclaration (decl, token->location);
17595
17596           decl = finish_member_template_decl (decl);
17597         }
17598       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17599         make_friend_class (current_class_type, TREE_TYPE (decl),
17600                            /*complain=*/true);
17601     }
17602   /* We are done with the current parameter list.  */
17603   --parser->num_template_parameter_lists;
17604
17605   pop_deferring_access_checks ();
17606
17607   /* Finish up.  */
17608   finish_template_decl (parameter_list);
17609
17610   /* Register member declarations.  */
17611   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17612     finish_member_declaration (decl);
17613   /* For the erroneous case of a template with C linkage, we pushed an
17614      implicit C++ linkage scope; exit that scope now.  */
17615   if (need_lang_pop)
17616     pop_lang_context ();
17617   /* If DECL is a function template, we must return to parse it later.
17618      (Even though there is no definition, there might be default
17619      arguments that need handling.)  */
17620   if (member_p && decl
17621       && (TREE_CODE (decl) == FUNCTION_DECL
17622           || DECL_FUNCTION_TEMPLATE_P (decl)))
17623     TREE_VALUE (parser->unparsed_functions_queues)
17624       = tree_cons (NULL_TREE, decl,
17625                    TREE_VALUE (parser->unparsed_functions_queues));
17626 }
17627
17628 /* Perform the deferred access checks from a template-parameter-list.
17629    CHECKS is a TREE_LIST of access checks, as returned by
17630    get_deferred_access_checks.  */
17631
17632 static void
17633 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17634 {
17635   ++processing_template_parmlist;
17636   perform_access_checks (checks);
17637   --processing_template_parmlist;
17638 }
17639
17640 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17641    `function-definition' sequence.  MEMBER_P is true, this declaration
17642    appears in a class scope.
17643
17644    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17645    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17646
17647 static tree
17648 cp_parser_single_declaration (cp_parser* parser,
17649                               VEC (deferred_access_check,gc)* checks,
17650                               bool member_p,
17651                               bool explicit_specialization_p,
17652                               bool* friend_p)
17653 {
17654   int declares_class_or_enum;
17655   tree decl = NULL_TREE;
17656   cp_decl_specifier_seq decl_specifiers;
17657   bool function_definition_p = false;
17658   cp_token *decl_spec_token_start;
17659
17660   /* This function is only used when processing a template
17661      declaration.  */
17662   gcc_assert (innermost_scope_kind () == sk_template_parms
17663               || innermost_scope_kind () == sk_template_spec);
17664
17665   /* Defer access checks until we know what is being declared.  */
17666   push_deferring_access_checks (dk_deferred);
17667
17668   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17669      alternative.  */
17670   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17671   cp_parser_decl_specifier_seq (parser,
17672                                 CP_PARSER_FLAGS_OPTIONAL,
17673                                 &decl_specifiers,
17674                                 &declares_class_or_enum);
17675   if (friend_p)
17676     *friend_p = cp_parser_friend_p (&decl_specifiers);
17677
17678   /* There are no template typedefs.  */
17679   if (decl_specifiers.specs[(int) ds_typedef])
17680     {
17681       error ("%Htemplate declaration of %qs",
17682              &decl_spec_token_start->location, "typedef");
17683       decl = error_mark_node;
17684     }
17685
17686   /* Gather up the access checks that occurred the
17687      decl-specifier-seq.  */
17688   stop_deferring_access_checks ();
17689
17690   /* Check for the declaration of a template class.  */
17691   if (declares_class_or_enum)
17692     {
17693       if (cp_parser_declares_only_class_p (parser))
17694         {
17695           decl = shadow_tag (&decl_specifiers);
17696
17697           /* In this case:
17698
17699                struct C {
17700                  friend template <typename T> struct A<T>::B;
17701                };
17702
17703              A<T>::B will be represented by a TYPENAME_TYPE, and
17704              therefore not recognized by shadow_tag.  */
17705           if (friend_p && *friend_p
17706               && !decl
17707               && decl_specifiers.type
17708               && TYPE_P (decl_specifiers.type))
17709             decl = decl_specifiers.type;
17710
17711           if (decl && decl != error_mark_node)
17712             decl = TYPE_NAME (decl);
17713           else
17714             decl = error_mark_node;
17715
17716           /* Perform access checks for template parameters.  */
17717           cp_parser_perform_template_parameter_access_checks (checks);
17718         }
17719     }
17720   /* If it's not a template class, try for a template function.  If
17721      the next token is a `;', then this declaration does not declare
17722      anything.  But, if there were errors in the decl-specifiers, then
17723      the error might well have come from an attempted class-specifier.
17724      In that case, there's no need to warn about a missing declarator.  */
17725   if (!decl
17726       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17727           || decl_specifiers.type != error_mark_node))
17728     {
17729       decl = cp_parser_init_declarator (parser,
17730                                         &decl_specifiers,
17731                                         checks,
17732                                         /*function_definition_allowed_p=*/true,
17733                                         member_p,
17734                                         declares_class_or_enum,
17735                                         &function_definition_p);
17736
17737     /* 7.1.1-1 [dcl.stc]
17738
17739        A storage-class-specifier shall not be specified in an explicit
17740        specialization...  */
17741     if (decl
17742         && explicit_specialization_p
17743         && decl_specifiers.storage_class != sc_none)
17744       {
17745         error ("%Hexplicit template specialization cannot have a storage class",
17746                &decl_spec_token_start->location);
17747         decl = error_mark_node;
17748       }
17749     }
17750
17751   pop_deferring_access_checks ();
17752
17753   /* Clear any current qualification; whatever comes next is the start
17754      of something new.  */
17755   parser->scope = NULL_TREE;
17756   parser->qualifying_scope = NULL_TREE;
17757   parser->object_scope = NULL_TREE;
17758   /* Look for a trailing `;' after the declaration.  */
17759   if (!function_definition_p
17760       && (decl == error_mark_node
17761           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17762     cp_parser_skip_to_end_of_block_or_statement (parser);
17763
17764   return decl;
17765 }
17766
17767 /* Parse a cast-expression that is not the operand of a unary "&".  */
17768
17769 static tree
17770 cp_parser_simple_cast_expression (cp_parser *parser)
17771 {
17772   return cp_parser_cast_expression (parser, /*address_p=*/false,
17773                                     /*cast_p=*/false);
17774 }
17775
17776 /* Parse a functional cast to TYPE.  Returns an expression
17777    representing the cast.  */
17778
17779 static tree
17780 cp_parser_functional_cast (cp_parser* parser, tree type)
17781 {
17782   tree expression_list;
17783   tree cast;
17784   bool nonconst_p;
17785
17786   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17787     {
17788       maybe_warn_cpp0x ("extended initializer lists");
17789       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17790       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17791       if (TREE_CODE (type) == TYPE_DECL)
17792         type = TREE_TYPE (type);
17793       return finish_compound_literal (type, expression_list);
17794     }
17795
17796   expression_list
17797     = cp_parser_parenthesized_expression_list (parser, false,
17798                                                /*cast_p=*/true,
17799                                                /*allow_expansion_p=*/true,
17800                                                /*non_constant_p=*/NULL);
17801
17802   cast = build_functional_cast (type, expression_list,
17803                                 tf_warning_or_error);
17804   /* [expr.const]/1: In an integral constant expression "only type
17805      conversions to integral or enumeration type can be used".  */
17806   if (TREE_CODE (type) == TYPE_DECL)
17807     type = TREE_TYPE (type);
17808   if (cast != error_mark_node
17809       && !cast_valid_in_integral_constant_expression_p (type)
17810       && (cp_parser_non_integral_constant_expression
17811           (parser, "a call to a constructor")))
17812     return error_mark_node;
17813   return cast;
17814 }
17815
17816 /* Save the tokens that make up the body of a member function defined
17817    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17818    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17819    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17820    for the member function.  */
17821
17822 static tree
17823 cp_parser_save_member_function_body (cp_parser* parser,
17824                                      cp_decl_specifier_seq *decl_specifiers,
17825                                      cp_declarator *declarator,
17826                                      tree attributes)
17827 {
17828   cp_token *first;
17829   cp_token *last;
17830   tree fn;
17831
17832   /* Create the function-declaration.  */
17833   fn = start_method (decl_specifiers, declarator, attributes);
17834   /* If something went badly wrong, bail out now.  */
17835   if (fn == error_mark_node)
17836     {
17837       /* If there's a function-body, skip it.  */
17838       if (cp_parser_token_starts_function_definition_p
17839           (cp_lexer_peek_token (parser->lexer)))
17840         cp_parser_skip_to_end_of_block_or_statement (parser);
17841       return error_mark_node;
17842     }
17843
17844   /* Remember it, if there default args to post process.  */
17845   cp_parser_save_default_args (parser, fn);
17846
17847   /* Save away the tokens that make up the body of the
17848      function.  */
17849   first = parser->lexer->next_token;
17850   /* We can have braced-init-list mem-initializers before the fn body.  */
17851   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17852     {
17853       cp_lexer_consume_token (parser->lexer);
17854       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17855              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17856         {
17857           /* cache_group will stop after an un-nested { } pair, too.  */
17858           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17859             break;
17860
17861           /* variadic mem-inits have ... after the ')'.  */
17862           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17863             cp_lexer_consume_token (parser->lexer);
17864         }
17865     }
17866   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17867   /* Handle function try blocks.  */
17868   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17869     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17870   last = parser->lexer->next_token;
17871
17872   /* Save away the inline definition; we will process it when the
17873      class is complete.  */
17874   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17875   DECL_PENDING_INLINE_P (fn) = 1;
17876
17877   /* We need to know that this was defined in the class, so that
17878      friend templates are handled correctly.  */
17879   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17880
17881   /* We're done with the inline definition.  */
17882   finish_method (fn);
17883
17884   /* Add FN to the queue of functions to be parsed later.  */
17885   TREE_VALUE (parser->unparsed_functions_queues)
17886     = tree_cons (NULL_TREE, fn,
17887                  TREE_VALUE (parser->unparsed_functions_queues));
17888
17889   return fn;
17890 }
17891
17892 /* Parse a template-argument-list, as well as the trailing ">" (but
17893    not the opening ">").  See cp_parser_template_argument_list for the
17894    return value.  */
17895
17896 static tree
17897 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17898 {
17899   tree arguments;
17900   tree saved_scope;
17901   tree saved_qualifying_scope;
17902   tree saved_object_scope;
17903   bool saved_greater_than_is_operator_p;
17904   bool saved_skip_evaluation;
17905
17906   /* [temp.names]
17907
17908      When parsing a template-id, the first non-nested `>' is taken as
17909      the end of the template-argument-list rather than a greater-than
17910      operator.  */
17911   saved_greater_than_is_operator_p
17912     = parser->greater_than_is_operator_p;
17913   parser->greater_than_is_operator_p = false;
17914   /* Parsing the argument list may modify SCOPE, so we save it
17915      here.  */
17916   saved_scope = parser->scope;
17917   saved_qualifying_scope = parser->qualifying_scope;
17918   saved_object_scope = parser->object_scope;
17919   /* We need to evaluate the template arguments, even though this
17920      template-id may be nested within a "sizeof".  */
17921   saved_skip_evaluation = skip_evaluation;
17922   skip_evaluation = false;
17923   /* Parse the template-argument-list itself.  */
17924   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17925       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17926     arguments = NULL_TREE;
17927   else
17928     arguments = cp_parser_template_argument_list (parser);
17929   /* Look for the `>' that ends the template-argument-list. If we find
17930      a '>>' instead, it's probably just a typo.  */
17931   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17932     {
17933       if (cxx_dialect != cxx98)
17934         {
17935           /* In C++0x, a `>>' in a template argument list or cast
17936              expression is considered to be two separate `>'
17937              tokens. So, change the current token to a `>', but don't
17938              consume it: it will be consumed later when the outer
17939              template argument list (or cast expression) is parsed.
17940              Note that this replacement of `>' for `>>' is necessary
17941              even if we are parsing tentatively: in the tentative
17942              case, after calling
17943              cp_parser_enclosed_template_argument_list we will always
17944              throw away all of the template arguments and the first
17945              closing `>', either because the template argument list
17946              was erroneous or because we are replacing those tokens
17947              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17948              not have been thrown away) is needed either to close an
17949              outer template argument list or to complete a new-style
17950              cast.  */
17951           cp_token *token = cp_lexer_peek_token (parser->lexer);
17952           token->type = CPP_GREATER;
17953         }
17954       else if (!saved_greater_than_is_operator_p)
17955         {
17956           /* If we're in a nested template argument list, the '>>' has
17957             to be a typo for '> >'. We emit the error message, but we
17958             continue parsing and we push a '>' as next token, so that
17959             the argument list will be parsed correctly.  Note that the
17960             global source location is still on the token before the
17961             '>>', so we need to say explicitly where we want it.  */
17962           cp_token *token = cp_lexer_peek_token (parser->lexer);
17963           error ("%H%<>>%> should be %<> >%> "
17964                  "within a nested template argument list",
17965                  &token->location);
17966
17967           token->type = CPP_GREATER;
17968         }
17969       else
17970         {
17971           /* If this is not a nested template argument list, the '>>'
17972             is a typo for '>'. Emit an error message and continue.
17973             Same deal about the token location, but here we can get it
17974             right by consuming the '>>' before issuing the diagnostic.  */
17975           cp_token *token = cp_lexer_consume_token (parser->lexer);
17976           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17977                  "a template argument list", &token->location);
17978         }
17979     }
17980   else
17981     cp_parser_skip_to_end_of_template_parameter_list (parser);
17982   /* The `>' token might be a greater-than operator again now.  */
17983   parser->greater_than_is_operator_p
17984     = saved_greater_than_is_operator_p;
17985   /* Restore the SAVED_SCOPE.  */
17986   parser->scope = saved_scope;
17987   parser->qualifying_scope = saved_qualifying_scope;
17988   parser->object_scope = saved_object_scope;
17989   skip_evaluation = saved_skip_evaluation;
17990
17991   return arguments;
17992 }
17993
17994 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17995    arguments, or the body of the function have not yet been parsed,
17996    parse them now.  */
17997
17998 static void
17999 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18000 {
18001   /* If this member is a template, get the underlying
18002      FUNCTION_DECL.  */
18003   if (DECL_FUNCTION_TEMPLATE_P (member_function))
18004     member_function = DECL_TEMPLATE_RESULT (member_function);
18005
18006   /* There should not be any class definitions in progress at this
18007      point; the bodies of members are only parsed outside of all class
18008      definitions.  */
18009   gcc_assert (parser->num_classes_being_defined == 0);
18010   /* While we're parsing the member functions we might encounter more
18011      classes.  We want to handle them right away, but we don't want
18012      them getting mixed up with functions that are currently in the
18013      queue.  */
18014   parser->unparsed_functions_queues
18015     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18016
18017   /* Make sure that any template parameters are in scope.  */
18018   maybe_begin_member_template_processing (member_function);
18019
18020   /* If the body of the function has not yet been parsed, parse it
18021      now.  */
18022   if (DECL_PENDING_INLINE_P (member_function))
18023     {
18024       tree function_scope;
18025       cp_token_cache *tokens;
18026
18027       /* The function is no longer pending; we are processing it.  */
18028       tokens = DECL_PENDING_INLINE_INFO (member_function);
18029       DECL_PENDING_INLINE_INFO (member_function) = NULL;
18030       DECL_PENDING_INLINE_P (member_function) = 0;
18031
18032       /* If this is a local class, enter the scope of the containing
18033          function.  */
18034       function_scope = current_function_decl;
18035       if (function_scope)
18036         push_function_context ();
18037
18038       /* Push the body of the function onto the lexer stack.  */
18039       cp_parser_push_lexer_for_tokens (parser, tokens);
18040
18041       /* Let the front end know that we going to be defining this
18042          function.  */
18043       start_preparsed_function (member_function, NULL_TREE,
18044                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
18045
18046       /* Don't do access checking if it is a templated function.  */
18047       if (processing_template_decl)
18048         push_deferring_access_checks (dk_no_check);
18049
18050       /* Now, parse the body of the function.  */
18051       cp_parser_function_definition_after_declarator (parser,
18052                                                       /*inline_p=*/true);
18053
18054       if (processing_template_decl)
18055         pop_deferring_access_checks ();
18056
18057       /* Leave the scope of the containing function.  */
18058       if (function_scope)
18059         pop_function_context ();
18060       cp_parser_pop_lexer (parser);
18061     }
18062
18063   /* Remove any template parameters from the symbol table.  */
18064   maybe_end_member_template_processing ();
18065
18066   /* Restore the queue.  */
18067   parser->unparsed_functions_queues
18068     = TREE_CHAIN (parser->unparsed_functions_queues);
18069 }
18070
18071 /* If DECL contains any default args, remember it on the unparsed
18072    functions queue.  */
18073
18074 static void
18075 cp_parser_save_default_args (cp_parser* parser, tree decl)
18076 {
18077   tree probe;
18078
18079   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18080        probe;
18081        probe = TREE_CHAIN (probe))
18082     if (TREE_PURPOSE (probe))
18083       {
18084         TREE_PURPOSE (parser->unparsed_functions_queues)
18085           = tree_cons (current_class_type, decl,
18086                        TREE_PURPOSE (parser->unparsed_functions_queues));
18087         break;
18088       }
18089 }
18090
18091 /* FN is a FUNCTION_DECL which may contains a parameter with an
18092    unparsed DEFAULT_ARG.  Parse the default args now.  This function
18093    assumes that the current scope is the scope in which the default
18094    argument should be processed.  */
18095
18096 static void
18097 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18098 {
18099   bool saved_local_variables_forbidden_p;
18100   tree parm;
18101
18102   /* While we're parsing the default args, we might (due to the
18103      statement expression extension) encounter more classes.  We want
18104      to handle them right away, but we don't want them getting mixed
18105      up with default args that are currently in the queue.  */
18106   parser->unparsed_functions_queues
18107     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18108
18109   /* Local variable names (and the `this' keyword) may not appear
18110      in a default argument.  */
18111   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18112   parser->local_variables_forbidden_p = true;
18113
18114   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18115        parm;
18116        parm = TREE_CHAIN (parm))
18117     {
18118       cp_token_cache *tokens;
18119       tree default_arg = TREE_PURPOSE (parm);
18120       tree parsed_arg;
18121       VEC(tree,gc) *insts;
18122       tree copy;
18123       unsigned ix;
18124
18125       if (!default_arg)
18126         continue;
18127
18128       if (TREE_CODE (default_arg) != DEFAULT_ARG)
18129         /* This can happen for a friend declaration for a function
18130            already declared with default arguments.  */
18131         continue;
18132
18133        /* Push the saved tokens for the default argument onto the parser's
18134           lexer stack.  */
18135       tokens = DEFARG_TOKENS (default_arg);
18136       cp_parser_push_lexer_for_tokens (parser, tokens);
18137
18138       /* Parse the assignment-expression.  */
18139       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
18140
18141       if (!processing_template_decl)
18142         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18143
18144       TREE_PURPOSE (parm) = parsed_arg;
18145
18146       /* Update any instantiations we've already created.  */
18147       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18148            VEC_iterate (tree, insts, ix, copy); ix++)
18149         TREE_PURPOSE (copy) = parsed_arg;
18150
18151       /* If the token stream has not been completely used up, then
18152          there was extra junk after the end of the default
18153          argument.  */
18154       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18155         cp_parser_error (parser, "expected %<,%>");
18156
18157       /* Revert to the main lexer.  */
18158       cp_parser_pop_lexer (parser);
18159     }
18160
18161   /* Make sure no default arg is missing.  */
18162   check_default_args (fn);
18163
18164   /* Restore the state of local_variables_forbidden_p.  */
18165   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18166
18167   /* Restore the queue.  */
18168   parser->unparsed_functions_queues
18169     = TREE_CHAIN (parser->unparsed_functions_queues);
18170 }
18171
18172 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18173    either a TYPE or an expression, depending on the form of the
18174    input.  The KEYWORD indicates which kind of expression we have
18175    encountered.  */
18176
18177 static tree
18178 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18179 {
18180   tree expr = NULL_TREE;
18181   const char *saved_message;
18182   char *tmp;
18183   bool saved_integral_constant_expression_p;
18184   bool saved_non_integral_constant_expression_p;
18185   bool pack_expansion_p = false;
18186
18187   /* Types cannot be defined in a `sizeof' expression.  Save away the
18188      old message.  */
18189   saved_message = parser->type_definition_forbidden_message;
18190   /* And create the new one.  */
18191   tmp = concat ("types may not be defined in %<",
18192                 IDENTIFIER_POINTER (ridpointers[keyword]),
18193                 "%> expressions", NULL);
18194   parser->type_definition_forbidden_message = tmp;
18195
18196   /* The restrictions on constant-expressions do not apply inside
18197      sizeof expressions.  */
18198   saved_integral_constant_expression_p
18199     = parser->integral_constant_expression_p;
18200   saved_non_integral_constant_expression_p
18201     = parser->non_integral_constant_expression_p;
18202   parser->integral_constant_expression_p = false;
18203
18204   /* If it's a `...', then we are computing the length of a parameter
18205      pack.  */
18206   if (keyword == RID_SIZEOF
18207       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18208     {
18209       /* Consume the `...'.  */
18210       cp_lexer_consume_token (parser->lexer);
18211       maybe_warn_variadic_templates ();
18212
18213       /* Note that this is an expansion.  */
18214       pack_expansion_p = true;
18215     }
18216
18217   /* Do not actually evaluate the expression.  */
18218   ++skip_evaluation;
18219   /* If it's a `(', then we might be looking at the type-id
18220      construction.  */
18221   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18222     {
18223       tree type;
18224       bool saved_in_type_id_in_expr_p;
18225
18226       /* We can't be sure yet whether we're looking at a type-id or an
18227          expression.  */
18228       cp_parser_parse_tentatively (parser);
18229       /* Consume the `('.  */
18230       cp_lexer_consume_token (parser->lexer);
18231       /* Parse the type-id.  */
18232       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18233       parser->in_type_id_in_expr_p = true;
18234       type = cp_parser_type_id (parser);
18235       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18236       /* Now, look for the trailing `)'.  */
18237       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18238       /* If all went well, then we're done.  */
18239       if (cp_parser_parse_definitely (parser))
18240         {
18241           cp_decl_specifier_seq decl_specs;
18242
18243           /* Build a trivial decl-specifier-seq.  */
18244           clear_decl_specs (&decl_specs);
18245           decl_specs.type = type;
18246
18247           /* Call grokdeclarator to figure out what type this is.  */
18248           expr = grokdeclarator (NULL,
18249                                  &decl_specs,
18250                                  TYPENAME,
18251                                  /*initialized=*/0,
18252                                  /*attrlist=*/NULL);
18253         }
18254     }
18255
18256   /* If the type-id production did not work out, then we must be
18257      looking at the unary-expression production.  */
18258   if (!expr)
18259     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18260                                        /*cast_p=*/false);
18261
18262   if (pack_expansion_p)
18263     /* Build a pack expansion. */
18264     expr = make_pack_expansion (expr);
18265
18266   /* Go back to evaluating expressions.  */
18267   --skip_evaluation;
18268
18269   /* Free the message we created.  */
18270   free (tmp);
18271   /* And restore the old one.  */
18272   parser->type_definition_forbidden_message = saved_message;
18273   parser->integral_constant_expression_p
18274     = saved_integral_constant_expression_p;
18275   parser->non_integral_constant_expression_p
18276     = saved_non_integral_constant_expression_p;
18277
18278   return expr;
18279 }
18280
18281 /* If the current declaration has no declarator, return true.  */
18282
18283 static bool
18284 cp_parser_declares_only_class_p (cp_parser *parser)
18285 {
18286   /* If the next token is a `;' or a `,' then there is no
18287      declarator.  */
18288   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18289           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18290 }
18291
18292 /* Update the DECL_SPECS to reflect the storage class indicated by
18293    KEYWORD.  */
18294
18295 static void
18296 cp_parser_set_storage_class (cp_parser *parser,
18297                              cp_decl_specifier_seq *decl_specs,
18298                              enum rid keyword,
18299                              location_t location)
18300 {
18301   cp_storage_class storage_class;
18302
18303   if (parser->in_unbraced_linkage_specification_p)
18304     {
18305       error ("%Hinvalid use of %qD in linkage specification",
18306              &location, ridpointers[keyword]);
18307       return;
18308     }
18309   else if (decl_specs->storage_class != sc_none)
18310     {
18311       decl_specs->conflicting_specifiers_p = true;
18312       return;
18313     }
18314
18315   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18316       && decl_specs->specs[(int) ds_thread])
18317     {
18318       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18319       decl_specs->specs[(int) ds_thread] = 0;
18320     }
18321
18322   switch (keyword)
18323     {
18324     case RID_AUTO:
18325       storage_class = sc_auto;
18326       break;
18327     case RID_REGISTER:
18328       storage_class = sc_register;
18329       break;
18330     case RID_STATIC:
18331       storage_class = sc_static;
18332       break;
18333     case RID_EXTERN:
18334       storage_class = sc_extern;
18335       break;
18336     case RID_MUTABLE:
18337       storage_class = sc_mutable;
18338       break;
18339     default:
18340       gcc_unreachable ();
18341     }
18342   decl_specs->storage_class = storage_class;
18343
18344   /* A storage class specifier cannot be applied alongside a typedef 
18345      specifier. If there is a typedef specifier present then set 
18346      conflicting_specifiers_p which will trigger an error later
18347      on in grokdeclarator. */
18348   if (decl_specs->specs[(int)ds_typedef])
18349     decl_specs->conflicting_specifiers_p = true;
18350 }
18351
18352 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18353    is true, the type is a user-defined type; otherwise it is a
18354    built-in type specified by a keyword.  */
18355
18356 static void
18357 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18358                               tree type_spec,
18359                               location_t location,
18360                               bool user_defined_p)
18361 {
18362   decl_specs->any_specifiers_p = true;
18363
18364   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18365      (with, for example, in "typedef int wchar_t;") we remember that
18366      this is what happened.  In system headers, we ignore these
18367      declarations so that G++ can work with system headers that are not
18368      C++-safe.  */
18369   if (decl_specs->specs[(int) ds_typedef]
18370       && !user_defined_p
18371       && (type_spec == boolean_type_node
18372           || type_spec == char16_type_node
18373           || type_spec == char32_type_node
18374           || type_spec == wchar_type_node)
18375       && (decl_specs->type
18376           || decl_specs->specs[(int) ds_long]
18377           || decl_specs->specs[(int) ds_short]
18378           || decl_specs->specs[(int) ds_unsigned]
18379           || decl_specs->specs[(int) ds_signed]))
18380     {
18381       decl_specs->redefined_builtin_type = type_spec;
18382       if (!decl_specs->type)
18383         {
18384           decl_specs->type = type_spec;
18385           decl_specs->user_defined_type_p = false;
18386           decl_specs->type_location = location;
18387         }
18388     }
18389   else if (decl_specs->type)
18390     decl_specs->multiple_types_p = true;
18391   else
18392     {
18393       decl_specs->type = type_spec;
18394       decl_specs->user_defined_type_p = user_defined_p;
18395       decl_specs->redefined_builtin_type = NULL_TREE;
18396       decl_specs->type_location = location;
18397     }
18398 }
18399
18400 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18401    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18402
18403 static bool
18404 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18405 {
18406   return decl_specifiers->specs[(int) ds_friend] != 0;
18407 }
18408
18409 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18410    issue an error message indicating that TOKEN_DESC was expected.
18411
18412    Returns the token consumed, if the token had the appropriate type.
18413    Otherwise, returns NULL.  */
18414
18415 static cp_token *
18416 cp_parser_require (cp_parser* parser,
18417                    enum cpp_ttype type,
18418                    const char* token_desc)
18419 {
18420   if (cp_lexer_next_token_is (parser->lexer, type))
18421     return cp_lexer_consume_token (parser->lexer);
18422   else
18423     {
18424       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18425       if (!cp_parser_simulate_error (parser))
18426         {
18427           char *message = concat ("expected ", token_desc, NULL);
18428           cp_parser_error (parser, message);
18429           free (message);
18430         }
18431       return NULL;
18432     }
18433 }
18434
18435 /* An error message is produced if the next token is not '>'.
18436    All further tokens are skipped until the desired token is
18437    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18438
18439 static void
18440 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18441 {
18442   /* Current level of '< ... >'.  */
18443   unsigned level = 0;
18444   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18445   unsigned nesting_depth = 0;
18446
18447   /* Are we ready, yet?  If not, issue error message.  */
18448   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18449     return;
18450
18451   /* Skip tokens until the desired token is found.  */
18452   while (true)
18453     {
18454       /* Peek at the next token.  */
18455       switch (cp_lexer_peek_token (parser->lexer)->type)
18456         {
18457         case CPP_LESS:
18458           if (!nesting_depth)
18459             ++level;
18460           break;
18461
18462         case CPP_RSHIFT:
18463           if (cxx_dialect == cxx98)
18464             /* C++0x views the `>>' operator as two `>' tokens, but
18465                C++98 does not. */
18466             break;
18467           else if (!nesting_depth && level-- == 0)
18468             {
18469               /* We've hit a `>>' where the first `>' closes the
18470                  template argument list, and the second `>' is
18471                  spurious.  Just consume the `>>' and stop; we've
18472                  already produced at least one error.  */
18473               cp_lexer_consume_token (parser->lexer);
18474               return;
18475             }
18476           /* Fall through for C++0x, so we handle the second `>' in
18477              the `>>'.  */
18478
18479         case CPP_GREATER:
18480           if (!nesting_depth && level-- == 0)
18481             {
18482               /* We've reached the token we want, consume it and stop.  */
18483               cp_lexer_consume_token (parser->lexer);
18484               return;
18485             }
18486           break;
18487
18488         case CPP_OPEN_PAREN:
18489         case CPP_OPEN_SQUARE:
18490           ++nesting_depth;
18491           break;
18492
18493         case CPP_CLOSE_PAREN:
18494         case CPP_CLOSE_SQUARE:
18495           if (nesting_depth-- == 0)
18496             return;
18497           break;
18498
18499         case CPP_EOF:
18500         case CPP_PRAGMA_EOL:
18501         case CPP_SEMICOLON:
18502         case CPP_OPEN_BRACE:
18503         case CPP_CLOSE_BRACE:
18504           /* The '>' was probably forgotten, don't look further.  */
18505           return;
18506
18507         default:
18508           break;
18509         }
18510
18511       /* Consume this token.  */
18512       cp_lexer_consume_token (parser->lexer);
18513     }
18514 }
18515
18516 /* If the next token is the indicated keyword, consume it.  Otherwise,
18517    issue an error message indicating that TOKEN_DESC was expected.
18518
18519    Returns the token consumed, if the token had the appropriate type.
18520    Otherwise, returns NULL.  */
18521
18522 static cp_token *
18523 cp_parser_require_keyword (cp_parser* parser,
18524                            enum rid keyword,
18525                            const char* token_desc)
18526 {
18527   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18528
18529   if (token && token->keyword != keyword)
18530     {
18531       dyn_string_t error_msg;
18532
18533       /* Format the error message.  */
18534       error_msg = dyn_string_new (0);
18535       dyn_string_append_cstr (error_msg, "expected ");
18536       dyn_string_append_cstr (error_msg, token_desc);
18537       cp_parser_error (parser, error_msg->s);
18538       dyn_string_delete (error_msg);
18539       return NULL;
18540     }
18541
18542   return token;
18543 }
18544
18545 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18546    function-definition.  */
18547
18548 static bool
18549 cp_parser_token_starts_function_definition_p (cp_token* token)
18550 {
18551   return (/* An ordinary function-body begins with an `{'.  */
18552           token->type == CPP_OPEN_BRACE
18553           /* A ctor-initializer begins with a `:'.  */
18554           || token->type == CPP_COLON
18555           /* A function-try-block begins with `try'.  */
18556           || token->keyword == RID_TRY
18557           /* The named return value extension begins with `return'.  */
18558           || token->keyword == RID_RETURN);
18559 }
18560
18561 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18562    definition.  */
18563
18564 static bool
18565 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18566 {
18567   cp_token *token;
18568
18569   token = cp_lexer_peek_token (parser->lexer);
18570   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18571 }
18572
18573 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18574    C++0x) ending a template-argument.  */
18575
18576 static bool
18577 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18578 {
18579   cp_token *token;
18580
18581   token = cp_lexer_peek_token (parser->lexer);
18582   return (token->type == CPP_COMMA 
18583           || token->type == CPP_GREATER
18584           || token->type == CPP_ELLIPSIS
18585           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18586 }
18587
18588 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18589    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18590
18591 static bool
18592 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18593                                                      size_t n)
18594 {
18595   cp_token *token;
18596
18597   token = cp_lexer_peek_nth_token (parser->lexer, n);
18598   if (token->type == CPP_LESS)
18599     return true;
18600   /* Check for the sequence `<::' in the original code. It would be lexed as
18601      `[:', where `[' is a digraph, and there is no whitespace before
18602      `:'.  */
18603   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18604     {
18605       cp_token *token2;
18606       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18607       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18608         return true;
18609     }
18610   return false;
18611 }
18612
18613 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18614    or none_type otherwise.  */
18615
18616 static enum tag_types
18617 cp_parser_token_is_class_key (cp_token* token)
18618 {
18619   switch (token->keyword)
18620     {
18621     case RID_CLASS:
18622       return class_type;
18623     case RID_STRUCT:
18624       return record_type;
18625     case RID_UNION:
18626       return union_type;
18627
18628     default:
18629       return none_type;
18630     }
18631 }
18632
18633 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18634
18635 static void
18636 cp_parser_check_class_key (enum tag_types class_key, tree type)
18637 {
18638   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18639     permerror (input_location, "%qs tag used in naming %q#T",
18640             class_key == union_type ? "union"
18641              : class_key == record_type ? "struct" : "class",
18642              type);
18643 }
18644
18645 /* Issue an error message if DECL is redeclared with different
18646    access than its original declaration [class.access.spec/3].
18647    This applies to nested classes and nested class templates.
18648    [class.mem/1].  */
18649
18650 static void
18651 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18652 {
18653   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18654     return;
18655
18656   if ((TREE_PRIVATE (decl)
18657        != (current_access_specifier == access_private_node))
18658       || (TREE_PROTECTED (decl)
18659           != (current_access_specifier == access_protected_node)))
18660     error ("%H%qD redeclared with different access", &location, decl);
18661 }
18662
18663 /* Look for the `template' keyword, as a syntactic disambiguator.
18664    Return TRUE iff it is present, in which case it will be
18665    consumed.  */
18666
18667 static bool
18668 cp_parser_optional_template_keyword (cp_parser *parser)
18669 {
18670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18671     {
18672       /* The `template' keyword can only be used within templates;
18673          outside templates the parser can always figure out what is a
18674          template and what is not.  */
18675       if (!processing_template_decl)
18676         {
18677           cp_token *token = cp_lexer_peek_token (parser->lexer);
18678           error ("%H%<template%> (as a disambiguator) is only allowed "
18679                  "within templates", &token->location);
18680           /* If this part of the token stream is rescanned, the same
18681              error message would be generated.  So, we purge the token
18682              from the stream.  */
18683           cp_lexer_purge_token (parser->lexer);
18684           return false;
18685         }
18686       else
18687         {
18688           /* Consume the `template' keyword.  */
18689           cp_lexer_consume_token (parser->lexer);
18690           return true;
18691         }
18692     }
18693
18694   return false;
18695 }
18696
18697 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18698    set PARSER->SCOPE, and perform other related actions.  */
18699
18700 static void
18701 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18702 {
18703   int i;
18704   struct tree_check *check_value;
18705   deferred_access_check *chk;
18706   VEC (deferred_access_check,gc) *checks;
18707
18708   /* Get the stored value.  */
18709   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18710   /* Perform any access checks that were deferred.  */
18711   checks = check_value->checks;
18712   if (checks)
18713     {
18714       for (i = 0 ;
18715            VEC_iterate (deferred_access_check, checks, i, chk) ;
18716            ++i)
18717         {
18718           perform_or_defer_access_check (chk->binfo,
18719                                          chk->decl,
18720                                          chk->diag_decl);
18721         }
18722     }
18723   /* Set the scope from the stored value.  */
18724   parser->scope = check_value->value;
18725   parser->qualifying_scope = check_value->qualifying_scope;
18726   parser->object_scope = NULL_TREE;
18727 }
18728
18729 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18730    encounter the end of a block before what we were looking for.  */
18731
18732 static bool
18733 cp_parser_cache_group (cp_parser *parser,
18734                        enum cpp_ttype end,
18735                        unsigned depth)
18736 {
18737   while (true)
18738     {
18739       cp_token *token = cp_lexer_peek_token (parser->lexer);
18740
18741       /* Abort a parenthesized expression if we encounter a semicolon.  */
18742       if ((end == CPP_CLOSE_PAREN || depth == 0)
18743           && token->type == CPP_SEMICOLON)
18744         return true;
18745       /* If we've reached the end of the file, stop.  */
18746       if (token->type == CPP_EOF
18747           || (end != CPP_PRAGMA_EOL
18748               && token->type == CPP_PRAGMA_EOL))
18749         return true;
18750       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18751         /* We've hit the end of an enclosing block, so there's been some
18752            kind of syntax error.  */
18753         return true;
18754
18755       /* Consume the token.  */
18756       cp_lexer_consume_token (parser->lexer);
18757       /* See if it starts a new group.  */
18758       if (token->type == CPP_OPEN_BRACE)
18759         {
18760           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18761           /* In theory this should probably check end == '}', but
18762              cp_parser_save_member_function_body needs it to exit
18763              after either '}' or ')' when called with ')'.  */
18764           if (depth == 0)
18765             return false;
18766         }
18767       else if (token->type == CPP_OPEN_PAREN)
18768         {
18769           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18770           if (depth == 0 && end == CPP_CLOSE_PAREN)
18771             return false;
18772         }
18773       else if (token->type == CPP_PRAGMA)
18774         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18775       else if (token->type == end)
18776         return false;
18777     }
18778 }
18779
18780 /* Begin parsing tentatively.  We always save tokens while parsing
18781    tentatively so that if the tentative parsing fails we can restore the
18782    tokens.  */
18783
18784 static void
18785 cp_parser_parse_tentatively (cp_parser* parser)
18786 {
18787   /* Enter a new parsing context.  */
18788   parser->context = cp_parser_context_new (parser->context);
18789   /* Begin saving tokens.  */
18790   cp_lexer_save_tokens (parser->lexer);
18791   /* In order to avoid repetitive access control error messages,
18792      access checks are queued up until we are no longer parsing
18793      tentatively.  */
18794   push_deferring_access_checks (dk_deferred);
18795 }
18796
18797 /* Commit to the currently active tentative parse.  */
18798
18799 static void
18800 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18801 {
18802   cp_parser_context *context;
18803   cp_lexer *lexer;
18804
18805   /* Mark all of the levels as committed.  */
18806   lexer = parser->lexer;
18807   for (context = parser->context; context->next; context = context->next)
18808     {
18809       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18810         break;
18811       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18812       while (!cp_lexer_saving_tokens (lexer))
18813         lexer = lexer->next;
18814       cp_lexer_commit_tokens (lexer);
18815     }
18816 }
18817
18818 /* Abort the currently active tentative parse.  All consumed tokens
18819    will be rolled back, and no diagnostics will be issued.  */
18820
18821 static void
18822 cp_parser_abort_tentative_parse (cp_parser* parser)
18823 {
18824   cp_parser_simulate_error (parser);
18825   /* Now, pretend that we want to see if the construct was
18826      successfully parsed.  */
18827   cp_parser_parse_definitely (parser);
18828 }
18829
18830 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18831    token stream.  Otherwise, commit to the tokens we have consumed.
18832    Returns true if no error occurred; false otherwise.  */
18833
18834 static bool
18835 cp_parser_parse_definitely (cp_parser* parser)
18836 {
18837   bool error_occurred;
18838   cp_parser_context *context;
18839
18840   /* Remember whether or not an error occurred, since we are about to
18841      destroy that information.  */
18842   error_occurred = cp_parser_error_occurred (parser);
18843   /* Remove the topmost context from the stack.  */
18844   context = parser->context;
18845   parser->context = context->next;
18846   /* If no parse errors occurred, commit to the tentative parse.  */
18847   if (!error_occurred)
18848     {
18849       /* Commit to the tokens read tentatively, unless that was
18850          already done.  */
18851       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18852         cp_lexer_commit_tokens (parser->lexer);
18853
18854       pop_to_parent_deferring_access_checks ();
18855     }
18856   /* Otherwise, if errors occurred, roll back our state so that things
18857      are just as they were before we began the tentative parse.  */
18858   else
18859     {
18860       cp_lexer_rollback_tokens (parser->lexer);
18861       pop_deferring_access_checks ();
18862     }
18863   /* Add the context to the front of the free list.  */
18864   context->next = cp_parser_context_free_list;
18865   cp_parser_context_free_list = context;
18866
18867   return !error_occurred;
18868 }
18869
18870 /* Returns true if we are parsing tentatively and are not committed to
18871    this tentative parse.  */
18872
18873 static bool
18874 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18875 {
18876   return (cp_parser_parsing_tentatively (parser)
18877           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18878 }
18879
18880 /* Returns nonzero iff an error has occurred during the most recent
18881    tentative parse.  */
18882
18883 static bool
18884 cp_parser_error_occurred (cp_parser* parser)
18885 {
18886   return (cp_parser_parsing_tentatively (parser)
18887           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18888 }
18889
18890 /* Returns nonzero if GNU extensions are allowed.  */
18891
18892 static bool
18893 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18894 {
18895   return parser->allow_gnu_extensions_p;
18896 }
18897 \f
18898 /* Objective-C++ Productions */
18899
18900
18901 /* Parse an Objective-C expression, which feeds into a primary-expression
18902    above.
18903
18904    objc-expression:
18905      objc-message-expression
18906      objc-string-literal
18907      objc-encode-expression
18908      objc-protocol-expression
18909      objc-selector-expression
18910
18911   Returns a tree representation of the expression.  */
18912
18913 static tree
18914 cp_parser_objc_expression (cp_parser* parser)
18915 {
18916   /* Try to figure out what kind of declaration is present.  */
18917   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18918
18919   switch (kwd->type)
18920     {
18921     case CPP_OPEN_SQUARE:
18922       return cp_parser_objc_message_expression (parser);
18923
18924     case CPP_OBJC_STRING:
18925       kwd = cp_lexer_consume_token (parser->lexer);
18926       return objc_build_string_object (kwd->u.value);
18927
18928     case CPP_KEYWORD:
18929       switch (kwd->keyword)
18930         {
18931         case RID_AT_ENCODE:
18932           return cp_parser_objc_encode_expression (parser);
18933
18934         case RID_AT_PROTOCOL:
18935           return cp_parser_objc_protocol_expression (parser);
18936
18937         case RID_AT_SELECTOR:
18938           return cp_parser_objc_selector_expression (parser);
18939
18940         default:
18941           break;
18942         }
18943     default:
18944       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18945              &kwd->location, kwd->u.value);
18946       cp_parser_skip_to_end_of_block_or_statement (parser);
18947     }
18948
18949   return error_mark_node;
18950 }
18951
18952 /* Parse an Objective-C message expression.
18953
18954    objc-message-expression:
18955      [ objc-message-receiver objc-message-args ]
18956
18957    Returns a representation of an Objective-C message.  */
18958
18959 static tree
18960 cp_parser_objc_message_expression (cp_parser* parser)
18961 {
18962   tree receiver, messageargs;
18963
18964   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18965   receiver = cp_parser_objc_message_receiver (parser);
18966   messageargs = cp_parser_objc_message_args (parser);
18967   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18968
18969   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18970 }
18971
18972 /* Parse an objc-message-receiver.
18973
18974    objc-message-receiver:
18975      expression
18976      simple-type-specifier
18977
18978   Returns a representation of the type or expression.  */
18979
18980 static tree
18981 cp_parser_objc_message_receiver (cp_parser* parser)
18982 {
18983   tree rcv;
18984
18985   /* An Objective-C message receiver may be either (1) a type
18986      or (2) an expression.  */
18987   cp_parser_parse_tentatively (parser);
18988   rcv = cp_parser_expression (parser, false);
18989
18990   if (cp_parser_parse_definitely (parser))
18991     return rcv;
18992
18993   rcv = cp_parser_simple_type_specifier (parser,
18994                                          /*decl_specs=*/NULL,
18995                                          CP_PARSER_FLAGS_NONE);
18996
18997   return objc_get_class_reference (rcv);
18998 }
18999
19000 /* Parse the arguments and selectors comprising an Objective-C message.
19001
19002    objc-message-args:
19003      objc-selector
19004      objc-selector-args
19005      objc-selector-args , objc-comma-args
19006
19007    objc-selector-args:
19008      objc-selector [opt] : assignment-expression
19009      objc-selector-args objc-selector [opt] : assignment-expression
19010
19011    objc-comma-args:
19012      assignment-expression
19013      objc-comma-args , assignment-expression
19014
19015    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19016    selector arguments and TREE_VALUE containing a list of comma
19017    arguments.  */
19018
19019 static tree
19020 cp_parser_objc_message_args (cp_parser* parser)
19021 {
19022   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19023   bool maybe_unary_selector_p = true;
19024   cp_token *token = cp_lexer_peek_token (parser->lexer);
19025
19026   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19027     {
19028       tree selector = NULL_TREE, arg;
19029
19030       if (token->type != CPP_COLON)
19031         selector = cp_parser_objc_selector (parser);
19032
19033       /* Detect if we have a unary selector.  */
19034       if (maybe_unary_selector_p
19035           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19036         return build_tree_list (selector, NULL_TREE);
19037
19038       maybe_unary_selector_p = false;
19039       cp_parser_require (parser, CPP_COLON, "%<:%>");
19040       arg = cp_parser_assignment_expression (parser, false);
19041
19042       sel_args
19043         = chainon (sel_args,
19044                    build_tree_list (selector, arg));
19045
19046       token = cp_lexer_peek_token (parser->lexer);
19047     }
19048
19049   /* Handle non-selector arguments, if any. */
19050   while (token->type == CPP_COMMA)
19051     {
19052       tree arg;
19053
19054       cp_lexer_consume_token (parser->lexer);
19055       arg = cp_parser_assignment_expression (parser, false);
19056
19057       addl_args
19058         = chainon (addl_args,
19059                    build_tree_list (NULL_TREE, arg));
19060
19061       token = cp_lexer_peek_token (parser->lexer);
19062     }
19063
19064   return build_tree_list (sel_args, addl_args);
19065 }
19066
19067 /* Parse an Objective-C encode expression.
19068
19069    objc-encode-expression:
19070      @encode objc-typename
19071
19072    Returns an encoded representation of the type argument.  */
19073
19074 static tree
19075 cp_parser_objc_encode_expression (cp_parser* parser)
19076 {
19077   tree type;
19078   cp_token *token;
19079
19080   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
19081   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19082   token = cp_lexer_peek_token (parser->lexer);
19083   type = complete_type (cp_parser_type_id (parser));
19084   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19085
19086   if (!type)
19087     {
19088       error ("%H%<@encode%> must specify a type as an argument",
19089              &token->location);
19090       return error_mark_node;
19091     }
19092
19093   return objc_build_encode_expr (type);
19094 }
19095
19096 /* Parse an Objective-C @defs expression.  */
19097
19098 static tree
19099 cp_parser_objc_defs_expression (cp_parser *parser)
19100 {
19101   tree name;
19102
19103   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
19104   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19105   name = cp_parser_identifier (parser);
19106   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19107
19108   return objc_get_class_ivars (name);
19109 }
19110
19111 /* Parse an Objective-C protocol expression.
19112
19113   objc-protocol-expression:
19114     @protocol ( identifier )
19115
19116   Returns a representation of the protocol expression.  */
19117
19118 static tree
19119 cp_parser_objc_protocol_expression (cp_parser* parser)
19120 {
19121   tree proto;
19122
19123   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19124   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19125   proto = cp_parser_identifier (parser);
19126   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19127
19128   return objc_build_protocol_expr (proto);
19129 }
19130
19131 /* Parse an Objective-C selector expression.
19132
19133    objc-selector-expression:
19134      @selector ( objc-method-signature )
19135
19136    objc-method-signature:
19137      objc-selector
19138      objc-selector-seq
19139
19140    objc-selector-seq:
19141      objc-selector :
19142      objc-selector-seq objc-selector :
19143
19144   Returns a representation of the method selector.  */
19145
19146 static tree
19147 cp_parser_objc_selector_expression (cp_parser* parser)
19148 {
19149   tree sel_seq = NULL_TREE;
19150   bool maybe_unary_selector_p = true;
19151   cp_token *token;
19152
19153   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
19154   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19155   token = cp_lexer_peek_token (parser->lexer);
19156
19157   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19158          || token->type == CPP_SCOPE)
19159     {
19160       tree selector = NULL_TREE;
19161
19162       if (token->type != CPP_COLON
19163           || token->type == CPP_SCOPE)
19164         selector = cp_parser_objc_selector (parser);
19165
19166       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19167           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19168         {
19169           /* Detect if we have a unary selector.  */
19170           if (maybe_unary_selector_p)
19171             {
19172               sel_seq = selector;
19173               goto finish_selector;
19174             }
19175           else
19176             {
19177               cp_parser_error (parser, "expected %<:%>");
19178             }
19179         }
19180       maybe_unary_selector_p = false;
19181       token = cp_lexer_consume_token (parser->lexer);
19182
19183       if (token->type == CPP_SCOPE)
19184         {
19185           sel_seq
19186             = chainon (sel_seq,
19187                        build_tree_list (selector, NULL_TREE));
19188           sel_seq
19189             = chainon (sel_seq,
19190                        build_tree_list (NULL_TREE, NULL_TREE));
19191         }
19192       else
19193         sel_seq
19194           = chainon (sel_seq,
19195                      build_tree_list (selector, NULL_TREE));
19196
19197       token = cp_lexer_peek_token (parser->lexer);
19198     }
19199
19200  finish_selector:
19201   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19202
19203   return objc_build_selector_expr (sel_seq);
19204 }
19205
19206 /* Parse a list of identifiers.
19207
19208    objc-identifier-list:
19209      identifier
19210      objc-identifier-list , identifier
19211
19212    Returns a TREE_LIST of identifier nodes.  */
19213
19214 static tree
19215 cp_parser_objc_identifier_list (cp_parser* parser)
19216 {
19217   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19218   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19219
19220   while (sep->type == CPP_COMMA)
19221     {
19222       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19223       list = chainon (list,
19224                       build_tree_list (NULL_TREE,
19225                                        cp_parser_identifier (parser)));
19226       sep = cp_lexer_peek_token (parser->lexer);
19227     }
19228
19229   return list;
19230 }
19231
19232 /* Parse an Objective-C alias declaration.
19233
19234    objc-alias-declaration:
19235      @compatibility_alias identifier identifier ;
19236
19237    This function registers the alias mapping with the Objective-C front end.
19238    It returns nothing.  */
19239
19240 static void
19241 cp_parser_objc_alias_declaration (cp_parser* parser)
19242 {
19243   tree alias, orig;
19244
19245   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19246   alias = cp_parser_identifier (parser);
19247   orig = cp_parser_identifier (parser);
19248   objc_declare_alias (alias, orig);
19249   cp_parser_consume_semicolon_at_end_of_statement (parser);
19250 }
19251
19252 /* Parse an Objective-C class forward-declaration.
19253
19254    objc-class-declaration:
19255      @class objc-identifier-list ;
19256
19257    The function registers the forward declarations with the Objective-C
19258    front end.  It returns nothing.  */
19259
19260 static void
19261 cp_parser_objc_class_declaration (cp_parser* parser)
19262 {
19263   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19264   objc_declare_class (cp_parser_objc_identifier_list (parser));
19265   cp_parser_consume_semicolon_at_end_of_statement (parser);
19266 }
19267
19268 /* Parse a list of Objective-C protocol references.
19269
19270    objc-protocol-refs-opt:
19271      objc-protocol-refs [opt]
19272
19273    objc-protocol-refs:
19274      < objc-identifier-list >
19275
19276    Returns a TREE_LIST of identifiers, if any.  */
19277
19278 static tree
19279 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19280 {
19281   tree protorefs = NULL_TREE;
19282
19283   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19284     {
19285       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19286       protorefs = cp_parser_objc_identifier_list (parser);
19287       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19288     }
19289
19290   return protorefs;
19291 }
19292
19293 /* Parse a Objective-C visibility specification.  */
19294
19295 static void
19296 cp_parser_objc_visibility_spec (cp_parser* parser)
19297 {
19298   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19299
19300   switch (vis->keyword)
19301     {
19302     case RID_AT_PRIVATE:
19303       objc_set_visibility (2);
19304       break;
19305     case RID_AT_PROTECTED:
19306       objc_set_visibility (0);
19307       break;
19308     case RID_AT_PUBLIC:
19309       objc_set_visibility (1);
19310       break;
19311     default:
19312       return;
19313     }
19314
19315   /* Eat '@private'/'@protected'/'@public'.  */
19316   cp_lexer_consume_token (parser->lexer);
19317 }
19318
19319 /* Parse an Objective-C method type.  */
19320
19321 static void
19322 cp_parser_objc_method_type (cp_parser* parser)
19323 {
19324   objc_set_method_type
19325    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19326     ? PLUS_EXPR
19327     : MINUS_EXPR);
19328 }
19329
19330 /* Parse an Objective-C protocol qualifier.  */
19331
19332 static tree
19333 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19334 {
19335   tree quals = NULL_TREE, node;
19336   cp_token *token = cp_lexer_peek_token (parser->lexer);
19337
19338   node = token->u.value;
19339
19340   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19341          && (node == ridpointers [(int) RID_IN]
19342              || node == ridpointers [(int) RID_OUT]
19343              || node == ridpointers [(int) RID_INOUT]
19344              || node == ridpointers [(int) RID_BYCOPY]
19345              || node == ridpointers [(int) RID_BYREF]
19346              || node == ridpointers [(int) RID_ONEWAY]))
19347     {
19348       quals = tree_cons (NULL_TREE, node, quals);
19349       cp_lexer_consume_token (parser->lexer);
19350       token = cp_lexer_peek_token (parser->lexer);
19351       node = token->u.value;
19352     }
19353
19354   return quals;
19355 }
19356
19357 /* Parse an Objective-C typename.  */
19358
19359 static tree
19360 cp_parser_objc_typename (cp_parser* parser)
19361 {
19362   tree type_name = NULL_TREE;
19363
19364   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19365     {
19366       tree proto_quals, cp_type = NULL_TREE;
19367
19368       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19369       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19370
19371       /* An ObjC type name may consist of just protocol qualifiers, in which
19372          case the type shall default to 'id'.  */
19373       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19374         cp_type = cp_parser_type_id (parser);
19375
19376       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19377       type_name = build_tree_list (proto_quals, cp_type);
19378     }
19379
19380   return type_name;
19381 }
19382
19383 /* Check to see if TYPE refers to an Objective-C selector name.  */
19384
19385 static bool
19386 cp_parser_objc_selector_p (enum cpp_ttype type)
19387 {
19388   return (type == CPP_NAME || type == CPP_KEYWORD
19389           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19390           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19391           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19392           || type == CPP_XOR || type == CPP_XOR_EQ);
19393 }
19394
19395 /* Parse an Objective-C selector.  */
19396
19397 static tree
19398 cp_parser_objc_selector (cp_parser* parser)
19399 {
19400   cp_token *token = cp_lexer_consume_token (parser->lexer);
19401
19402   if (!cp_parser_objc_selector_p (token->type))
19403     {
19404       error ("%Hinvalid Objective-C++ selector name", &token->location);
19405       return error_mark_node;
19406     }
19407
19408   /* C++ operator names are allowed to appear in ObjC selectors.  */
19409   switch (token->type)
19410     {
19411     case CPP_AND_AND: return get_identifier ("and");
19412     case CPP_AND_EQ: return get_identifier ("and_eq");
19413     case CPP_AND: return get_identifier ("bitand");
19414     case CPP_OR: return get_identifier ("bitor");
19415     case CPP_COMPL: return get_identifier ("compl");
19416     case CPP_NOT: return get_identifier ("not");
19417     case CPP_NOT_EQ: return get_identifier ("not_eq");
19418     case CPP_OR_OR: return get_identifier ("or");
19419     case CPP_OR_EQ: return get_identifier ("or_eq");
19420     case CPP_XOR: return get_identifier ("xor");
19421     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19422     default: return token->u.value;
19423     }
19424 }
19425
19426 /* Parse an Objective-C params list.  */
19427
19428 static tree
19429 cp_parser_objc_method_keyword_params (cp_parser* parser)
19430 {
19431   tree params = NULL_TREE;
19432   bool maybe_unary_selector_p = true;
19433   cp_token *token = cp_lexer_peek_token (parser->lexer);
19434
19435   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19436     {
19437       tree selector = NULL_TREE, type_name, identifier;
19438
19439       if (token->type != CPP_COLON)
19440         selector = cp_parser_objc_selector (parser);
19441
19442       /* Detect if we have a unary selector.  */
19443       if (maybe_unary_selector_p
19444           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19445         return selector;
19446
19447       maybe_unary_selector_p = false;
19448       cp_parser_require (parser, CPP_COLON, "%<:%>");
19449       type_name = cp_parser_objc_typename (parser);
19450       identifier = cp_parser_identifier (parser);
19451
19452       params
19453         = chainon (params,
19454                    objc_build_keyword_decl (selector,
19455                                             type_name,
19456                                             identifier));
19457
19458       token = cp_lexer_peek_token (parser->lexer);
19459     }
19460
19461   return params;
19462 }
19463
19464 /* Parse the non-keyword Objective-C params.  */
19465
19466 static tree
19467 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19468 {
19469   tree params = make_node (TREE_LIST);
19470   cp_token *token = cp_lexer_peek_token (parser->lexer);
19471   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19472
19473   while (token->type == CPP_COMMA)
19474     {
19475       cp_parameter_declarator *parmdecl;
19476       tree parm;
19477
19478       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19479       token = cp_lexer_peek_token (parser->lexer);
19480
19481       if (token->type == CPP_ELLIPSIS)
19482         {
19483           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19484           *ellipsisp = true;
19485           break;
19486         }
19487
19488       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19489       parm = grokdeclarator (parmdecl->declarator,
19490                              &parmdecl->decl_specifiers,
19491                              PARM, /*initialized=*/0,
19492                              /*attrlist=*/NULL);
19493
19494       chainon (params, build_tree_list (NULL_TREE, parm));
19495       token = cp_lexer_peek_token (parser->lexer);
19496     }
19497
19498   return params;
19499 }
19500
19501 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19502
19503 static void
19504 cp_parser_objc_interstitial_code (cp_parser* parser)
19505 {
19506   cp_token *token = cp_lexer_peek_token (parser->lexer);
19507
19508   /* If the next token is `extern' and the following token is a string
19509      literal, then we have a linkage specification.  */
19510   if (token->keyword == RID_EXTERN
19511       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19512     cp_parser_linkage_specification (parser);
19513   /* Handle #pragma, if any.  */
19514   else if (token->type == CPP_PRAGMA)
19515     cp_parser_pragma (parser, pragma_external);
19516   /* Allow stray semicolons.  */
19517   else if (token->type == CPP_SEMICOLON)
19518     cp_lexer_consume_token (parser->lexer);
19519   /* Finally, try to parse a block-declaration, or a function-definition.  */
19520   else
19521     cp_parser_block_declaration (parser, /*statement_p=*/false);
19522 }
19523
19524 /* Parse a method signature.  */
19525
19526 static tree
19527 cp_parser_objc_method_signature (cp_parser* parser)
19528 {
19529   tree rettype, kwdparms, optparms;
19530   bool ellipsis = false;
19531
19532   cp_parser_objc_method_type (parser);
19533   rettype = cp_parser_objc_typename (parser);
19534   kwdparms = cp_parser_objc_method_keyword_params (parser);
19535   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19536
19537   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19538 }
19539
19540 /* Pars an Objective-C method prototype list.  */
19541
19542 static void
19543 cp_parser_objc_method_prototype_list (cp_parser* parser)
19544 {
19545   cp_token *token = cp_lexer_peek_token (parser->lexer);
19546
19547   while (token->keyword != RID_AT_END)
19548     {
19549       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19550         {
19551           objc_add_method_declaration
19552            (cp_parser_objc_method_signature (parser));
19553           cp_parser_consume_semicolon_at_end_of_statement (parser);
19554         }
19555       else
19556         /* Allow for interspersed non-ObjC++ code.  */
19557         cp_parser_objc_interstitial_code (parser);
19558
19559       token = cp_lexer_peek_token (parser->lexer);
19560     }
19561
19562   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19563   objc_finish_interface ();
19564 }
19565
19566 /* Parse an Objective-C method definition list.  */
19567
19568 static void
19569 cp_parser_objc_method_definition_list (cp_parser* parser)
19570 {
19571   cp_token *token = cp_lexer_peek_token (parser->lexer);
19572
19573   while (token->keyword != RID_AT_END)
19574     {
19575       tree meth;
19576
19577       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19578         {
19579           push_deferring_access_checks (dk_deferred);
19580           objc_start_method_definition
19581            (cp_parser_objc_method_signature (parser));
19582
19583           /* For historical reasons, we accept an optional semicolon.  */
19584           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19585             cp_lexer_consume_token (parser->lexer);
19586
19587           perform_deferred_access_checks ();
19588           stop_deferring_access_checks ();
19589           meth = cp_parser_function_definition_after_declarator (parser,
19590                                                                  false);
19591           pop_deferring_access_checks ();
19592           objc_finish_method_definition (meth);
19593         }
19594       else
19595         /* Allow for interspersed non-ObjC++ code.  */
19596         cp_parser_objc_interstitial_code (parser);
19597
19598       token = cp_lexer_peek_token (parser->lexer);
19599     }
19600
19601   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19602   objc_finish_implementation ();
19603 }
19604
19605 /* Parse Objective-C ivars.  */
19606
19607 static void
19608 cp_parser_objc_class_ivars (cp_parser* parser)
19609 {
19610   cp_token *token = cp_lexer_peek_token (parser->lexer);
19611
19612   if (token->type != CPP_OPEN_BRACE)
19613     return;     /* No ivars specified.  */
19614
19615   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19616   token = cp_lexer_peek_token (parser->lexer);
19617
19618   while (token->type != CPP_CLOSE_BRACE)
19619     {
19620       cp_decl_specifier_seq declspecs;
19621       int decl_class_or_enum_p;
19622       tree prefix_attributes;
19623
19624       cp_parser_objc_visibility_spec (parser);
19625
19626       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19627         break;
19628
19629       cp_parser_decl_specifier_seq (parser,
19630                                     CP_PARSER_FLAGS_OPTIONAL,
19631                                     &declspecs,
19632                                     &decl_class_or_enum_p);
19633       prefix_attributes = declspecs.attributes;
19634       declspecs.attributes = NULL_TREE;
19635
19636       /* Keep going until we hit the `;' at the end of the
19637          declaration.  */
19638       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19639         {
19640           tree width = NULL_TREE, attributes, first_attribute, decl;
19641           cp_declarator *declarator = NULL;
19642           int ctor_dtor_or_conv_p;
19643
19644           /* Check for a (possibly unnamed) bitfield declaration.  */
19645           token = cp_lexer_peek_token (parser->lexer);
19646           if (token->type == CPP_COLON)
19647             goto eat_colon;
19648
19649           if (token->type == CPP_NAME
19650               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19651                   == CPP_COLON))
19652             {
19653               /* Get the name of the bitfield.  */
19654               declarator = make_id_declarator (NULL_TREE,
19655                                                cp_parser_identifier (parser),
19656                                                sfk_none);
19657
19658              eat_colon:
19659               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19660               /* Get the width of the bitfield.  */
19661               width
19662                 = cp_parser_constant_expression (parser,
19663                                                  /*allow_non_constant=*/false,
19664                                                  NULL);
19665             }
19666           else
19667             {
19668               /* Parse the declarator.  */
19669               declarator
19670                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19671                                         &ctor_dtor_or_conv_p,
19672                                         /*parenthesized_p=*/NULL,
19673                                         /*member_p=*/false);
19674             }
19675
19676           /* Look for attributes that apply to the ivar.  */
19677           attributes = cp_parser_attributes_opt (parser);
19678           /* Remember which attributes are prefix attributes and
19679              which are not.  */
19680           first_attribute = attributes;
19681           /* Combine the attributes.  */
19682           attributes = chainon (prefix_attributes, attributes);
19683
19684           if (width)
19685               /* Create the bitfield declaration.  */
19686               decl = grokbitfield (declarator, &declspecs,
19687                                    width,
19688                                    attributes);
19689           else
19690             decl = grokfield (declarator, &declspecs,
19691                               NULL_TREE, /*init_const_expr_p=*/false,
19692                               NULL_TREE, attributes);
19693
19694           /* Add the instance variable.  */
19695           objc_add_instance_variable (decl);
19696
19697           /* Reset PREFIX_ATTRIBUTES.  */
19698           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19699             attributes = TREE_CHAIN (attributes);
19700           if (attributes)
19701             TREE_CHAIN (attributes) = NULL_TREE;
19702
19703           token = cp_lexer_peek_token (parser->lexer);
19704
19705           if (token->type == CPP_COMMA)
19706             {
19707               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19708               continue;
19709             }
19710           break;
19711         }
19712
19713       cp_parser_consume_semicolon_at_end_of_statement (parser);
19714       token = cp_lexer_peek_token (parser->lexer);
19715     }
19716
19717   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19718   /* For historical reasons, we accept an optional semicolon.  */
19719   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19720     cp_lexer_consume_token (parser->lexer);
19721 }
19722
19723 /* Parse an Objective-C protocol declaration.  */
19724
19725 static void
19726 cp_parser_objc_protocol_declaration (cp_parser* parser)
19727 {
19728   tree proto, protorefs;
19729   cp_token *tok;
19730
19731   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19732   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19733     {
19734       tok = cp_lexer_peek_token (parser->lexer);
19735       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19736       goto finish;
19737     }
19738
19739   /* See if we have a forward declaration or a definition.  */
19740   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19741
19742   /* Try a forward declaration first.  */
19743   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19744     {
19745       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19746      finish:
19747       cp_parser_consume_semicolon_at_end_of_statement (parser);
19748     }
19749
19750   /* Ok, we got a full-fledged definition (or at least should).  */
19751   else
19752     {
19753       proto = cp_parser_identifier (parser);
19754       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19755       objc_start_protocol (proto, protorefs);
19756       cp_parser_objc_method_prototype_list (parser);
19757     }
19758 }
19759
19760 /* Parse an Objective-C superclass or category.  */
19761
19762 static void
19763 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19764                                                           tree *categ)
19765 {
19766   cp_token *next = cp_lexer_peek_token (parser->lexer);
19767
19768   *super = *categ = NULL_TREE;
19769   if (next->type == CPP_COLON)
19770     {
19771       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19772       *super = cp_parser_identifier (parser);
19773     }
19774   else if (next->type == CPP_OPEN_PAREN)
19775     {
19776       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19777       *categ = cp_parser_identifier (parser);
19778       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19779     }
19780 }
19781
19782 /* Parse an Objective-C class interface.  */
19783
19784 static void
19785 cp_parser_objc_class_interface (cp_parser* parser)
19786 {
19787   tree name, super, categ, protos;
19788
19789   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19790   name = cp_parser_identifier (parser);
19791   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19792   protos = cp_parser_objc_protocol_refs_opt (parser);
19793
19794   /* We have either a class or a category on our hands.  */
19795   if (categ)
19796     objc_start_category_interface (name, categ, protos);
19797   else
19798     {
19799       objc_start_class_interface (name, super, protos);
19800       /* Handle instance variable declarations, if any.  */
19801       cp_parser_objc_class_ivars (parser);
19802       objc_continue_interface ();
19803     }
19804
19805   cp_parser_objc_method_prototype_list (parser);
19806 }
19807
19808 /* Parse an Objective-C class implementation.  */
19809
19810 static void
19811 cp_parser_objc_class_implementation (cp_parser* parser)
19812 {
19813   tree name, super, categ;
19814
19815   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19816   name = cp_parser_identifier (parser);
19817   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19818
19819   /* We have either a class or a category on our hands.  */
19820   if (categ)
19821     objc_start_category_implementation (name, categ);
19822   else
19823     {
19824       objc_start_class_implementation (name, super);
19825       /* Handle instance variable declarations, if any.  */
19826       cp_parser_objc_class_ivars (parser);
19827       objc_continue_implementation ();
19828     }
19829
19830   cp_parser_objc_method_definition_list (parser);
19831 }
19832
19833 /* Consume the @end token and finish off the implementation.  */
19834
19835 static void
19836 cp_parser_objc_end_implementation (cp_parser* parser)
19837 {
19838   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19839   objc_finish_implementation ();
19840 }
19841
19842 /* Parse an Objective-C declaration.  */
19843
19844 static void
19845 cp_parser_objc_declaration (cp_parser* parser)
19846 {
19847   /* Try to figure out what kind of declaration is present.  */
19848   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19849
19850   switch (kwd->keyword)
19851     {
19852     case RID_AT_ALIAS:
19853       cp_parser_objc_alias_declaration (parser);
19854       break;
19855     case RID_AT_CLASS:
19856       cp_parser_objc_class_declaration (parser);
19857       break;
19858     case RID_AT_PROTOCOL:
19859       cp_parser_objc_protocol_declaration (parser);
19860       break;
19861     case RID_AT_INTERFACE:
19862       cp_parser_objc_class_interface (parser);
19863       break;
19864     case RID_AT_IMPLEMENTATION:
19865       cp_parser_objc_class_implementation (parser);
19866       break;
19867     case RID_AT_END:
19868       cp_parser_objc_end_implementation (parser);
19869       break;
19870     default:
19871       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19872              &kwd->location, kwd->u.value);
19873       cp_parser_skip_to_end_of_block_or_statement (parser);
19874     }
19875 }
19876
19877 /* Parse an Objective-C try-catch-finally statement.
19878
19879    objc-try-catch-finally-stmt:
19880      @try compound-statement objc-catch-clause-seq [opt]
19881        objc-finally-clause [opt]
19882
19883    objc-catch-clause-seq:
19884      objc-catch-clause objc-catch-clause-seq [opt]
19885
19886    objc-catch-clause:
19887      @catch ( exception-declaration ) compound-statement
19888
19889    objc-finally-clause
19890      @finally compound-statement
19891
19892    Returns NULL_TREE.  */
19893
19894 static tree
19895 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19896   location_t location;
19897   tree stmt;
19898
19899   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19900   location = cp_lexer_peek_token (parser->lexer)->location;
19901   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19902      node, lest it get absorbed into the surrounding block.  */
19903   stmt = push_stmt_list ();
19904   cp_parser_compound_statement (parser, NULL, false);
19905   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19906
19907   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19908     {
19909       cp_parameter_declarator *parmdecl;
19910       tree parm;
19911
19912       cp_lexer_consume_token (parser->lexer);
19913       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19914       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19915       parm = grokdeclarator (parmdecl->declarator,
19916                              &parmdecl->decl_specifiers,
19917                              PARM, /*initialized=*/0,
19918                              /*attrlist=*/NULL);
19919       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19920       objc_begin_catch_clause (parm);
19921       cp_parser_compound_statement (parser, NULL, false);
19922       objc_finish_catch_clause ();
19923     }
19924
19925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19926     {
19927       cp_lexer_consume_token (parser->lexer);
19928       location = cp_lexer_peek_token (parser->lexer)->location;
19929       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19930          node, lest it get absorbed into the surrounding block.  */
19931       stmt = push_stmt_list ();
19932       cp_parser_compound_statement (parser, NULL, false);
19933       objc_build_finally_clause (location, pop_stmt_list (stmt));
19934     }
19935
19936   return objc_finish_try_stmt ();
19937 }
19938
19939 /* Parse an Objective-C synchronized statement.
19940
19941    objc-synchronized-stmt:
19942      @synchronized ( expression ) compound-statement
19943
19944    Returns NULL_TREE.  */
19945
19946 static tree
19947 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19948   location_t location;
19949   tree lock, stmt;
19950
19951   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19952
19953   location = cp_lexer_peek_token (parser->lexer)->location;
19954   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19955   lock = cp_parser_expression (parser, false);
19956   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19957
19958   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19959      node, lest it get absorbed into the surrounding block.  */
19960   stmt = push_stmt_list ();
19961   cp_parser_compound_statement (parser, NULL, false);
19962
19963   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19964 }
19965
19966 /* Parse an Objective-C throw statement.
19967
19968    objc-throw-stmt:
19969      @throw assignment-expression [opt] ;
19970
19971    Returns a constructed '@throw' statement.  */
19972
19973 static tree
19974 cp_parser_objc_throw_statement (cp_parser *parser) {
19975   tree expr = NULL_TREE;
19976
19977   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19978
19979   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19980     expr = cp_parser_assignment_expression (parser, false);
19981
19982   cp_parser_consume_semicolon_at_end_of_statement (parser);
19983
19984   return objc_build_throw_stmt (expr);
19985 }
19986
19987 /* Parse an Objective-C statement.  */
19988
19989 static tree
19990 cp_parser_objc_statement (cp_parser * parser) {
19991   /* Try to figure out what kind of declaration is present.  */
19992   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19993
19994   switch (kwd->keyword)
19995     {
19996     case RID_AT_TRY:
19997       return cp_parser_objc_try_catch_finally_statement (parser);
19998     case RID_AT_SYNCHRONIZED:
19999       return cp_parser_objc_synchronized_statement (parser);
20000     case RID_AT_THROW:
20001       return cp_parser_objc_throw_statement (parser);
20002     default:
20003       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20004              &kwd->location, kwd->u.value);
20005       cp_parser_skip_to_end_of_block_or_statement (parser);
20006     }
20007
20008   return error_mark_node;
20009 }
20010 \f
20011 /* OpenMP 2.5 parsing routines.  */
20012
20013 /* Returns name of the next clause.
20014    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20015    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
20016    returned and the token is consumed.  */
20017
20018 static pragma_omp_clause
20019 cp_parser_omp_clause_name (cp_parser *parser)
20020 {
20021   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20022
20023   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20024     result = PRAGMA_OMP_CLAUSE_IF;
20025   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20026     result = PRAGMA_OMP_CLAUSE_DEFAULT;
20027   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20028     result = PRAGMA_OMP_CLAUSE_PRIVATE;
20029   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20030     {
20031       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20032       const char *p = IDENTIFIER_POINTER (id);
20033
20034       switch (p[0])
20035         {
20036         case 'c':
20037           if (!strcmp ("collapse", p))
20038             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20039           else if (!strcmp ("copyin", p))
20040             result = PRAGMA_OMP_CLAUSE_COPYIN;
20041           else if (!strcmp ("copyprivate", p))
20042             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20043           break;
20044         case 'f':
20045           if (!strcmp ("firstprivate", p))
20046             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20047           break;
20048         case 'l':
20049           if (!strcmp ("lastprivate", p))
20050             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20051           break;
20052         case 'n':
20053           if (!strcmp ("nowait", p))
20054             result = PRAGMA_OMP_CLAUSE_NOWAIT;
20055           else if (!strcmp ("num_threads", p))
20056             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20057           break;
20058         case 'o':
20059           if (!strcmp ("ordered", p))
20060             result = PRAGMA_OMP_CLAUSE_ORDERED;
20061           break;
20062         case 'r':
20063           if (!strcmp ("reduction", p))
20064             result = PRAGMA_OMP_CLAUSE_REDUCTION;
20065           break;
20066         case 's':
20067           if (!strcmp ("schedule", p))
20068             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20069           else if (!strcmp ("shared", p))
20070             result = PRAGMA_OMP_CLAUSE_SHARED;
20071           break;
20072         case 'u':
20073           if (!strcmp ("untied", p))
20074             result = PRAGMA_OMP_CLAUSE_UNTIED;
20075           break;
20076         }
20077     }
20078
20079   if (result != PRAGMA_OMP_CLAUSE_NONE)
20080     cp_lexer_consume_token (parser->lexer);
20081
20082   return result;
20083 }
20084
20085 /* Validate that a clause of the given type does not already exist.  */
20086
20087 static void
20088 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20089                            const char *name, location_t location)
20090 {
20091   tree c;
20092
20093   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20094     if (OMP_CLAUSE_CODE (c) == code)
20095       {
20096         error ("%Htoo many %qs clauses", &location, name);
20097         break;
20098       }
20099 }
20100
20101 /* OpenMP 2.5:
20102    variable-list:
20103      identifier
20104      variable-list , identifier
20105
20106    In addition, we match a closing parenthesis.  An opening parenthesis
20107    will have been consumed by the caller.
20108
20109    If KIND is nonzero, create the appropriate node and install the decl
20110    in OMP_CLAUSE_DECL and add the node to the head of the list.
20111
20112    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20113    return the list created.  */
20114
20115 static tree
20116 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20117                                 tree list)
20118 {
20119   cp_token *token;
20120   while (1)
20121     {
20122       tree name, decl;
20123
20124       token = cp_lexer_peek_token (parser->lexer);
20125       name = cp_parser_id_expression (parser, /*template_p=*/false,
20126                                       /*check_dependency_p=*/true,
20127                                       /*template_p=*/NULL,
20128                                       /*declarator_p=*/false,
20129                                       /*optional_p=*/false);
20130       if (name == error_mark_node)
20131         goto skip_comma;
20132
20133       decl = cp_parser_lookup_name_simple (parser, name, token->location);
20134       if (decl == error_mark_node)
20135         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20136       else if (kind != 0)
20137         {
20138           tree u = build_omp_clause (kind);
20139           OMP_CLAUSE_DECL (u) = decl;
20140           OMP_CLAUSE_CHAIN (u) = list;
20141           list = u;
20142         }
20143       else
20144         list = tree_cons (decl, NULL_TREE, list);
20145
20146     get_comma:
20147       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20148         break;
20149       cp_lexer_consume_token (parser->lexer);
20150     }
20151
20152   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20153     {
20154       int ending;
20155
20156       /* Try to resync to an unnested comma.  Copied from
20157          cp_parser_parenthesized_expression_list.  */
20158     skip_comma:
20159       ending = cp_parser_skip_to_closing_parenthesis (parser,
20160                                                       /*recovering=*/true,
20161                                                       /*or_comma=*/true,
20162                                                       /*consume_paren=*/true);
20163       if (ending < 0)
20164         goto get_comma;
20165     }
20166
20167   return list;
20168 }
20169
20170 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20171    common case for omp clauses.  */
20172
20173 static tree
20174 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20175 {
20176   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20177     return cp_parser_omp_var_list_no_open (parser, kind, list);
20178   return list;
20179 }
20180
20181 /* OpenMP 3.0:
20182    collapse ( constant-expression ) */
20183
20184 static tree
20185 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20186 {
20187   tree c, num;
20188   location_t loc;
20189   HOST_WIDE_INT n;
20190
20191   loc = cp_lexer_peek_token (parser->lexer)->location;
20192   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20193     return list;
20194
20195   num = cp_parser_constant_expression (parser, false, NULL);
20196
20197   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20198     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20199                                            /*or_comma=*/false,
20200                                            /*consume_paren=*/true);
20201
20202   if (num == error_mark_node)
20203     return list;
20204   num = fold_non_dependent_expr (num);
20205   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20206       || !host_integerp (num, 0)
20207       || (n = tree_low_cst (num, 0)) <= 0
20208       || (int) n != n)
20209     {
20210       error ("%Hcollapse argument needs positive constant integer expression",
20211              &loc);
20212       return list;
20213     }
20214
20215   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20216   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20217   OMP_CLAUSE_CHAIN (c) = list;
20218   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20219
20220   return c;
20221 }
20222
20223 /* OpenMP 2.5:
20224    default ( shared | none ) */
20225
20226 static tree
20227 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20228 {
20229   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20230   tree c;
20231
20232   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20233     return list;
20234   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20235     {
20236       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20237       const char *p = IDENTIFIER_POINTER (id);
20238
20239       switch (p[0])
20240         {
20241         case 'n':
20242           if (strcmp ("none", p) != 0)
20243             goto invalid_kind;
20244           kind = OMP_CLAUSE_DEFAULT_NONE;
20245           break;
20246
20247         case 's':
20248           if (strcmp ("shared", p) != 0)
20249             goto invalid_kind;
20250           kind = OMP_CLAUSE_DEFAULT_SHARED;
20251           break;
20252
20253         default:
20254           goto invalid_kind;
20255         }
20256
20257       cp_lexer_consume_token (parser->lexer);
20258     }
20259   else
20260     {
20261     invalid_kind:
20262       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20263     }
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 (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20271     return list;
20272
20273   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20274   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20275   OMP_CLAUSE_CHAIN (c) = list;
20276   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20277
20278   return c;
20279 }
20280
20281 /* OpenMP 2.5:
20282    if ( expression ) */
20283
20284 static tree
20285 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20286 {
20287   tree t, c;
20288
20289   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20290     return list;
20291
20292   t = cp_parser_condition (parser);
20293
20294   if (t == error_mark_node
20295       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20296     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20297                                            /*or_comma=*/false,
20298                                            /*consume_paren=*/true);
20299
20300   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20301
20302   c = build_omp_clause (OMP_CLAUSE_IF);
20303   OMP_CLAUSE_IF_EXPR (c) = t;
20304   OMP_CLAUSE_CHAIN (c) = list;
20305
20306   return c;
20307 }
20308
20309 /* OpenMP 2.5:
20310    nowait */
20311
20312 static tree
20313 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20314                              tree list, location_t location)
20315 {
20316   tree c;
20317
20318   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20319
20320   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20321   OMP_CLAUSE_CHAIN (c) = list;
20322   return c;
20323 }
20324
20325 /* OpenMP 2.5:
20326    num_threads ( expression ) */
20327
20328 static tree
20329 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20330                                   location_t location)
20331 {
20332   tree t, c;
20333
20334   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20335     return list;
20336
20337   t = cp_parser_expression (parser, false);
20338
20339   if (t == error_mark_node
20340       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20341     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20342                                            /*or_comma=*/false,
20343                                            /*consume_paren=*/true);
20344
20345   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20346                              "num_threads", location);
20347
20348   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20349   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20350   OMP_CLAUSE_CHAIN (c) = list;
20351
20352   return c;
20353 }
20354
20355 /* OpenMP 2.5:
20356    ordered */
20357
20358 static tree
20359 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20360                               tree list, location_t location)
20361 {
20362   tree c;
20363
20364   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20365                              "ordered", location);
20366
20367   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20368   OMP_CLAUSE_CHAIN (c) = list;
20369   return c;
20370 }
20371
20372 /* OpenMP 2.5:
20373    reduction ( reduction-operator : variable-list )
20374
20375    reduction-operator:
20376      One of: + * - & ^ | && || */
20377
20378 static tree
20379 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20380 {
20381   enum tree_code code;
20382   tree nlist, c;
20383
20384   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20385     return list;
20386
20387   switch (cp_lexer_peek_token (parser->lexer)->type)
20388     {
20389     case CPP_PLUS:
20390       code = PLUS_EXPR;
20391       break;
20392     case CPP_MULT:
20393       code = MULT_EXPR;
20394       break;
20395     case CPP_MINUS:
20396       code = MINUS_EXPR;
20397       break;
20398     case CPP_AND:
20399       code = BIT_AND_EXPR;
20400       break;
20401     case CPP_XOR:
20402       code = BIT_XOR_EXPR;
20403       break;
20404     case CPP_OR:
20405       code = BIT_IOR_EXPR;
20406       break;
20407     case CPP_AND_AND:
20408       code = TRUTH_ANDIF_EXPR;
20409       break;
20410     case CPP_OR_OR:
20411       code = TRUTH_ORIF_EXPR;
20412       break;
20413     default:
20414       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20415                                "%<|%>, %<&&%>, or %<||%>");
20416     resync_fail:
20417       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20418                                              /*or_comma=*/false,
20419                                              /*consume_paren=*/true);
20420       return list;
20421     }
20422   cp_lexer_consume_token (parser->lexer);
20423
20424   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20425     goto resync_fail;
20426
20427   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20428   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20429     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20430
20431   return nlist;
20432 }
20433
20434 /* OpenMP 2.5:
20435    schedule ( schedule-kind )
20436    schedule ( schedule-kind , expression )
20437
20438    schedule-kind:
20439      static | dynamic | guided | runtime | auto  */
20440
20441 static tree
20442 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20443 {
20444   tree c, t;
20445
20446   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20447     return list;
20448
20449   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20450
20451   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20452     {
20453       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20454       const char *p = IDENTIFIER_POINTER (id);
20455
20456       switch (p[0])
20457         {
20458         case 'd':
20459           if (strcmp ("dynamic", p) != 0)
20460             goto invalid_kind;
20461           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20462           break;
20463
20464         case 'g':
20465           if (strcmp ("guided", p) != 0)
20466             goto invalid_kind;
20467           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20468           break;
20469
20470         case 'r':
20471           if (strcmp ("runtime", p) != 0)
20472             goto invalid_kind;
20473           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20474           break;
20475
20476         default:
20477           goto invalid_kind;
20478         }
20479     }
20480   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20481     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20482   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20483     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20484   else
20485     goto invalid_kind;
20486   cp_lexer_consume_token (parser->lexer);
20487
20488   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20489     {
20490       cp_token *token;
20491       cp_lexer_consume_token (parser->lexer);
20492
20493       token = cp_lexer_peek_token (parser->lexer);
20494       t = cp_parser_assignment_expression (parser, false);
20495
20496       if (t == error_mark_node)
20497         goto resync_fail;
20498       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20499         error ("%Hschedule %<runtime%> does not take "
20500                "a %<chunk_size%> parameter", &token->location);
20501       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20502         error ("%Hschedule %<auto%> does not take "
20503                "a %<chunk_size%> parameter", &token->location);
20504       else
20505         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20506
20507       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20508         goto resync_fail;
20509     }
20510   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20511     goto resync_fail;
20512
20513   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20514   OMP_CLAUSE_CHAIN (c) = list;
20515   return c;
20516
20517  invalid_kind:
20518   cp_parser_error (parser, "invalid schedule kind");
20519  resync_fail:
20520   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20521                                          /*or_comma=*/false,
20522                                          /*consume_paren=*/true);
20523   return list;
20524 }
20525
20526 /* OpenMP 3.0:
20527    untied */
20528
20529 static tree
20530 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20531                              tree list, location_t location)
20532 {
20533   tree c;
20534
20535   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20536
20537   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20538   OMP_CLAUSE_CHAIN (c) = list;
20539   return c;
20540 }
20541
20542 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20543    is a bitmask in MASK.  Return the list of clauses found; the result
20544    of clause default goes in *pdefault.  */
20545
20546 static tree
20547 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20548                            const char *where, cp_token *pragma_tok)
20549 {
20550   tree clauses = NULL;
20551   bool first = true;
20552   cp_token *token = NULL;
20553
20554   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20555     {
20556       pragma_omp_clause c_kind;
20557       const char *c_name;
20558       tree prev = clauses;
20559
20560       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20561         cp_lexer_consume_token (parser->lexer);
20562
20563       token = cp_lexer_peek_token (parser->lexer);
20564       c_kind = cp_parser_omp_clause_name (parser);
20565       first = false;
20566
20567       switch (c_kind)
20568         {
20569         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20570           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20571                                                    token->location);
20572           c_name = "collapse";
20573           break;
20574         case PRAGMA_OMP_CLAUSE_COPYIN:
20575           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20576           c_name = "copyin";
20577           break;
20578         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20579           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20580                                             clauses);
20581           c_name = "copyprivate";
20582           break;
20583         case PRAGMA_OMP_CLAUSE_DEFAULT:
20584           clauses = cp_parser_omp_clause_default (parser, clauses,
20585                                                   token->location);
20586           c_name = "default";
20587           break;
20588         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20589           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20590                                             clauses);
20591           c_name = "firstprivate";
20592           break;
20593         case PRAGMA_OMP_CLAUSE_IF:
20594           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20595           c_name = "if";
20596           break;
20597         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20598           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20599                                             clauses);
20600           c_name = "lastprivate";
20601           break;
20602         case PRAGMA_OMP_CLAUSE_NOWAIT:
20603           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20604           c_name = "nowait";
20605           break;
20606         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20607           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20608                                                       token->location);
20609           c_name = "num_threads";
20610           break;
20611         case PRAGMA_OMP_CLAUSE_ORDERED:
20612           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20613                                                   token->location);
20614           c_name = "ordered";
20615           break;
20616         case PRAGMA_OMP_CLAUSE_PRIVATE:
20617           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20618                                             clauses);
20619           c_name = "private";
20620           break;
20621         case PRAGMA_OMP_CLAUSE_REDUCTION:
20622           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20623           c_name = "reduction";
20624           break;
20625         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20626           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20627                                                    token->location);
20628           c_name = "schedule";
20629           break;
20630         case PRAGMA_OMP_CLAUSE_SHARED:
20631           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20632                                             clauses);
20633           c_name = "shared";
20634           break;
20635         case PRAGMA_OMP_CLAUSE_UNTIED:
20636           clauses = cp_parser_omp_clause_untied (parser, clauses,
20637                                                  token->location);
20638           c_name = "nowait";
20639           break;
20640         default:
20641           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20642           goto saw_error;
20643         }
20644
20645       if (((mask >> c_kind) & 1) == 0)
20646         {
20647           /* Remove the invalid clause(s) from the list to avoid
20648              confusing the rest of the compiler.  */
20649           clauses = prev;
20650           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20651         }
20652     }
20653  saw_error:
20654   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20655   return finish_omp_clauses (clauses);
20656 }
20657
20658 /* OpenMP 2.5:
20659    structured-block:
20660      statement
20661
20662    In practice, we're also interested in adding the statement to an
20663    outer node.  So it is convenient if we work around the fact that
20664    cp_parser_statement calls add_stmt.  */
20665
20666 static unsigned
20667 cp_parser_begin_omp_structured_block (cp_parser *parser)
20668 {
20669   unsigned save = parser->in_statement;
20670
20671   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20672      This preserves the "not within loop or switch" style error messages
20673      for nonsense cases like
20674         void foo() {
20675         #pragma omp single
20676           break;
20677         }
20678   */
20679   if (parser->in_statement)
20680     parser->in_statement = IN_OMP_BLOCK;
20681
20682   return save;
20683 }
20684
20685 static void
20686 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20687 {
20688   parser->in_statement = save;
20689 }
20690
20691 static tree
20692 cp_parser_omp_structured_block (cp_parser *parser)
20693 {
20694   tree stmt = begin_omp_structured_block ();
20695   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20696
20697   cp_parser_statement (parser, NULL_TREE, false, NULL);
20698
20699   cp_parser_end_omp_structured_block (parser, save);
20700   return finish_omp_structured_block (stmt);
20701 }
20702
20703 /* OpenMP 2.5:
20704    # pragma omp atomic new-line
20705      expression-stmt
20706
20707    expression-stmt:
20708      x binop= expr | x++ | ++x | x-- | --x
20709    binop:
20710      +, *, -, /, &, ^, |, <<, >>
20711
20712   where x is an lvalue expression with scalar type.  */
20713
20714 static void
20715 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20716 {
20717   tree lhs, rhs;
20718   enum tree_code code;
20719
20720   cp_parser_require_pragma_eol (parser, pragma_tok);
20721
20722   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20723                                     /*cast_p=*/false);
20724   switch (TREE_CODE (lhs))
20725     {
20726     case ERROR_MARK:
20727       goto saw_error;
20728
20729     case PREINCREMENT_EXPR:
20730     case POSTINCREMENT_EXPR:
20731       lhs = TREE_OPERAND (lhs, 0);
20732       code = PLUS_EXPR;
20733       rhs = integer_one_node;
20734       break;
20735
20736     case PREDECREMENT_EXPR:
20737     case POSTDECREMENT_EXPR:
20738       lhs = TREE_OPERAND (lhs, 0);
20739       code = MINUS_EXPR;
20740       rhs = integer_one_node;
20741       break;
20742
20743     default:
20744       switch (cp_lexer_peek_token (parser->lexer)->type)
20745         {
20746         case CPP_MULT_EQ:
20747           code = MULT_EXPR;
20748           break;
20749         case CPP_DIV_EQ:
20750           code = TRUNC_DIV_EXPR;
20751           break;
20752         case CPP_PLUS_EQ:
20753           code = PLUS_EXPR;
20754           break;
20755         case CPP_MINUS_EQ:
20756           code = MINUS_EXPR;
20757           break;
20758         case CPP_LSHIFT_EQ:
20759           code = LSHIFT_EXPR;
20760           break;
20761         case CPP_RSHIFT_EQ:
20762           code = RSHIFT_EXPR;
20763           break;
20764         case CPP_AND_EQ:
20765           code = BIT_AND_EXPR;
20766           break;
20767         case CPP_OR_EQ:
20768           code = BIT_IOR_EXPR;
20769           break;
20770         case CPP_XOR_EQ:
20771           code = BIT_XOR_EXPR;
20772           break;
20773         default:
20774           cp_parser_error (parser,
20775                            "invalid operator for %<#pragma omp atomic%>");
20776           goto saw_error;
20777         }
20778       cp_lexer_consume_token (parser->lexer);
20779
20780       rhs = cp_parser_expression (parser, false);
20781       if (rhs == error_mark_node)
20782         goto saw_error;
20783       break;
20784     }
20785   finish_omp_atomic (code, lhs, rhs);
20786   cp_parser_consume_semicolon_at_end_of_statement (parser);
20787   return;
20788
20789  saw_error:
20790   cp_parser_skip_to_end_of_block_or_statement (parser);
20791 }
20792
20793
20794 /* OpenMP 2.5:
20795    # pragma omp barrier new-line  */
20796
20797 static void
20798 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20799 {
20800   cp_parser_require_pragma_eol (parser, pragma_tok);
20801   finish_omp_barrier ();
20802 }
20803
20804 /* OpenMP 2.5:
20805    # pragma omp critical [(name)] new-line
20806      structured-block  */
20807
20808 static tree
20809 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20810 {
20811   tree stmt, name = NULL;
20812
20813   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20814     {
20815       cp_lexer_consume_token (parser->lexer);
20816
20817       name = cp_parser_identifier (parser);
20818
20819       if (name == error_mark_node
20820           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20821         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20822                                                /*or_comma=*/false,
20823                                                /*consume_paren=*/true);
20824       if (name == error_mark_node)
20825         name = NULL;
20826     }
20827   cp_parser_require_pragma_eol (parser, pragma_tok);
20828
20829   stmt = cp_parser_omp_structured_block (parser);
20830   return c_finish_omp_critical (stmt, name);
20831 }
20832
20833 /* OpenMP 2.5:
20834    # pragma omp flush flush-vars[opt] new-line
20835
20836    flush-vars:
20837      ( variable-list ) */
20838
20839 static void
20840 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20841 {
20842   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20843     (void) cp_parser_omp_var_list (parser, 0, NULL);
20844   cp_parser_require_pragma_eol (parser, pragma_tok);
20845
20846   finish_omp_flush ();
20847 }
20848
20849 /* Helper function, to parse omp for increment expression.  */
20850
20851 static tree
20852 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20853 {
20854   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20855   enum tree_code op;
20856   cp_token *token;
20857
20858   if (lhs != decl)
20859     {
20860       cp_parser_skip_to_end_of_statement (parser);
20861       return error_mark_node;
20862     }
20863
20864   token = cp_lexer_peek_token (parser->lexer);
20865   op = binops_by_token [token->type].tree_type;
20866   switch (op)
20867     {
20868     case LT_EXPR:
20869     case LE_EXPR:
20870     case GT_EXPR:
20871     case GE_EXPR:
20872       break;
20873     default:
20874       cp_parser_skip_to_end_of_statement (parser);
20875       return error_mark_node;
20876     }
20877
20878   cp_lexer_consume_token (parser->lexer);
20879   rhs = cp_parser_binary_expression (parser, false,
20880                                      PREC_RELATIONAL_EXPRESSION);
20881   if (rhs == error_mark_node
20882       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20883     {
20884       cp_parser_skip_to_end_of_statement (parser);
20885       return error_mark_node;
20886     }
20887
20888   return build2 (op, boolean_type_node, lhs, rhs);
20889 }
20890
20891 /* Helper function, to parse omp for increment expression.  */
20892
20893 static tree
20894 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20895 {
20896   cp_token *token = cp_lexer_peek_token (parser->lexer);
20897   enum tree_code op;
20898   tree lhs, rhs;
20899   cp_id_kind idk;
20900   bool decl_first;
20901
20902   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20903     {
20904       op = (token->type == CPP_PLUS_PLUS
20905             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20906       cp_lexer_consume_token (parser->lexer);
20907       lhs = cp_parser_cast_expression (parser, false, false);
20908       if (lhs != decl)
20909         return error_mark_node;
20910       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20911     }
20912
20913   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20914   if (lhs != decl)
20915     return error_mark_node;
20916
20917   token = cp_lexer_peek_token (parser->lexer);
20918   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20919     {
20920       op = (token->type == CPP_PLUS_PLUS
20921             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20922       cp_lexer_consume_token (parser->lexer);
20923       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20924     }
20925
20926   op = cp_parser_assignment_operator_opt (parser);
20927   if (op == ERROR_MARK)
20928     return error_mark_node;
20929
20930   if (op != NOP_EXPR)
20931     {
20932       rhs = cp_parser_assignment_expression (parser, false);
20933       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20934       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20935     }
20936
20937   lhs = cp_parser_binary_expression (parser, false,
20938                                      PREC_ADDITIVE_EXPRESSION);
20939   token = cp_lexer_peek_token (parser->lexer);
20940   decl_first = lhs == decl;
20941   if (decl_first)
20942     lhs = NULL_TREE;
20943   if (token->type != CPP_PLUS
20944       && token->type != CPP_MINUS)
20945     return error_mark_node;
20946
20947   do
20948     {
20949       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20950       cp_lexer_consume_token (parser->lexer);
20951       rhs = cp_parser_binary_expression (parser, false,
20952                                          PREC_ADDITIVE_EXPRESSION);
20953       token = cp_lexer_peek_token (parser->lexer);
20954       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20955         {
20956           if (lhs == NULL_TREE)
20957             {
20958               if (op == PLUS_EXPR)
20959                 lhs = rhs;
20960               else
20961                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20962             }
20963           else
20964             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20965                                      NULL, tf_warning_or_error);
20966         }
20967     }
20968   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20969
20970   if (!decl_first)
20971     {
20972       if (rhs != decl || op == MINUS_EXPR)
20973         return error_mark_node;
20974       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20975     }
20976   else
20977     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20978
20979   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20980 }
20981
20982 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20983
20984 static tree
20985 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20986 {
20987   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20988   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20989   tree this_pre_body, cl;
20990   location_t loc_first;
20991   bool collapse_err = false;
20992   int i, collapse = 1, nbraces = 0;
20993
20994   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20995     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20996       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20997
20998   gcc_assert (collapse >= 1);
20999
21000   declv = make_tree_vec (collapse);
21001   initv = make_tree_vec (collapse);
21002   condv = make_tree_vec (collapse);
21003   incrv = make_tree_vec (collapse);
21004
21005   loc_first = cp_lexer_peek_token (parser->lexer)->location;
21006
21007   for (i = 0; i < collapse; i++)
21008     {
21009       int bracecount = 0;
21010       bool add_private_clause = false;
21011       location_t loc;
21012
21013       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21014         {
21015           cp_parser_error (parser, "for statement expected");
21016           return NULL;
21017         }
21018       loc = cp_lexer_consume_token (parser->lexer)->location;
21019
21020       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21021         return NULL;
21022
21023       init = decl = real_decl = NULL;
21024       this_pre_body = push_stmt_list ();
21025       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21026         {
21027           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21028
21029              init-expr:
21030                        var = lb
21031                        integer-type var = lb
21032                        random-access-iterator-type var = lb
21033                        pointer-type var = lb
21034           */
21035           cp_decl_specifier_seq type_specifiers;
21036
21037           /* First, try to parse as an initialized declaration.  See
21038              cp_parser_condition, from whence the bulk of this is copied.  */
21039
21040           cp_parser_parse_tentatively (parser);
21041           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21042                                         &type_specifiers);
21043           if (cp_parser_parse_definitely (parser))
21044             {
21045               /* If parsing a type specifier seq succeeded, then this
21046                  MUST be a initialized declaration.  */
21047               tree asm_specification, attributes;
21048               cp_declarator *declarator;
21049
21050               declarator = cp_parser_declarator (parser,
21051                                                  CP_PARSER_DECLARATOR_NAMED,
21052                                                  /*ctor_dtor_or_conv_p=*/NULL,
21053                                                  /*parenthesized_p=*/NULL,
21054                                                  /*member_p=*/false);
21055               attributes = cp_parser_attributes_opt (parser);
21056               asm_specification = cp_parser_asm_specification_opt (parser);
21057
21058               if (declarator == cp_error_declarator) 
21059                 cp_parser_skip_to_end_of_statement (parser);
21060
21061               else 
21062                 {
21063                   tree pushed_scope;
21064
21065                   decl = start_decl (declarator, &type_specifiers,
21066                                      /*initialized_p=*/false, attributes,
21067                                      /*prefix_attributes=*/NULL_TREE,
21068                                      &pushed_scope);
21069
21070                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21071                     {
21072                       if (cp_lexer_next_token_is (parser->lexer, 
21073                                                   CPP_OPEN_PAREN))
21074                         error ("parenthesized initialization is not allowed in "
21075                                "OpenMP %<for%> loop");
21076                       else
21077                         /* Trigger an error.  */
21078                         cp_parser_require (parser, CPP_EQ, "%<=%>");
21079
21080                       init = error_mark_node;
21081                       cp_parser_skip_to_end_of_statement (parser);
21082                     }
21083                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
21084                            || type_dependent_expression_p (decl))
21085                     {
21086                       bool is_direct_init, is_non_constant_init;
21087
21088                       init = cp_parser_initializer (parser,
21089                                                     &is_direct_init,
21090                                                     &is_non_constant_init);
21091
21092                       cp_finish_decl (decl, init, !is_non_constant_init,
21093                                       asm_specification,
21094                                       LOOKUP_ONLYCONVERTING);
21095                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
21096                         {
21097                           for_block
21098                             = tree_cons (NULL, this_pre_body, for_block);
21099                           init = NULL_TREE;
21100                         }
21101                       else
21102                         init = pop_stmt_list (this_pre_body);
21103                       this_pre_body = NULL_TREE;
21104                     }
21105                   else
21106                     {
21107                       /* Consume '='.  */
21108                       cp_lexer_consume_token (parser->lexer);
21109                       init = cp_parser_assignment_expression (parser, false);
21110
21111                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21112                         init = error_mark_node;
21113                       else
21114                         cp_finish_decl (decl, NULL_TREE,
21115                                         /*init_const_expr_p=*/false,
21116                                         asm_specification,
21117                                         LOOKUP_ONLYCONVERTING);
21118                     }
21119
21120                   if (pushed_scope)
21121                     pop_scope (pushed_scope);
21122                 }
21123             }
21124           else 
21125             {
21126               cp_id_kind idk;
21127               /* If parsing a type specifier sequence failed, then
21128                  this MUST be a simple expression.  */
21129               cp_parser_parse_tentatively (parser);
21130               decl = cp_parser_primary_expression (parser, false, false,
21131                                                    false, &idk);
21132               if (!cp_parser_error_occurred (parser)
21133                   && decl
21134                   && DECL_P (decl)
21135                   && CLASS_TYPE_P (TREE_TYPE (decl)))
21136                 {
21137                   tree rhs;
21138
21139                   cp_parser_parse_definitely (parser);
21140                   cp_parser_require (parser, CPP_EQ, "%<=%>");
21141                   rhs = cp_parser_assignment_expression (parser, false);
21142                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21143                                                          rhs,
21144                                                          tf_warning_or_error));
21145                   add_private_clause = true;
21146                 }
21147               else
21148                 {
21149                   decl = NULL;
21150                   cp_parser_abort_tentative_parse (parser);
21151                   init = cp_parser_expression (parser, false);
21152                   if (init)
21153                     {
21154                       if (TREE_CODE (init) == MODIFY_EXPR
21155                           || TREE_CODE (init) == MODOP_EXPR)
21156                         real_decl = TREE_OPERAND (init, 0);
21157                     }
21158                 }
21159             }
21160         }
21161       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21162       if (this_pre_body)
21163         {
21164           this_pre_body = pop_stmt_list (this_pre_body);
21165           if (pre_body)
21166             {
21167               tree t = pre_body;
21168               pre_body = push_stmt_list ();
21169               add_stmt (t);
21170               add_stmt (this_pre_body);
21171               pre_body = pop_stmt_list (pre_body);
21172             }
21173           else
21174             pre_body = this_pre_body;
21175         }
21176
21177       if (decl)
21178         real_decl = decl;
21179       if (par_clauses != NULL && real_decl != NULL_TREE)
21180         {
21181           tree *c;
21182           for (c = par_clauses; *c ; )
21183             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21184                 && OMP_CLAUSE_DECL (*c) == real_decl)
21185               {
21186                 error ("%Hiteration variable %qD should not be firstprivate",
21187                        &loc, real_decl);
21188                 *c = OMP_CLAUSE_CHAIN (*c);
21189               }
21190             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21191                      && OMP_CLAUSE_DECL (*c) == real_decl)
21192               {
21193                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21194                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21195                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21196                 OMP_CLAUSE_DECL (l) = real_decl;
21197                 OMP_CLAUSE_CHAIN (l) = clauses;
21198                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21199                 clauses = l;
21200                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21201                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21202                 add_private_clause = false;
21203               }
21204             else
21205               {
21206                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21207                     && OMP_CLAUSE_DECL (*c) == real_decl)
21208                   add_private_clause = false;
21209                 c = &OMP_CLAUSE_CHAIN (*c);
21210               }
21211         }
21212
21213       if (add_private_clause)
21214         {
21215           tree c;
21216           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21217             {
21218               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21219                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21220                   && OMP_CLAUSE_DECL (c) == decl)
21221                 break;
21222               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21223                        && OMP_CLAUSE_DECL (c) == decl)
21224                 error ("%Hiteration variable %qD should not be firstprivate",
21225                        &loc, decl);
21226               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21227                        && OMP_CLAUSE_DECL (c) == decl)
21228                 error ("%Hiteration variable %qD should not be reduction",
21229                        &loc, decl);
21230             }
21231           if (c == NULL)
21232             {
21233               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21234               OMP_CLAUSE_DECL (c) = decl;
21235               c = finish_omp_clauses (c);
21236               if (c)
21237                 {
21238                   OMP_CLAUSE_CHAIN (c) = clauses;
21239                   clauses = c;
21240                 }
21241             }
21242         }
21243
21244       cond = NULL;
21245       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21246         {
21247           /* If decl is an iterator, preserve LHS and RHS of the relational
21248              expr until finish_omp_for.  */
21249           if (decl
21250               && (type_dependent_expression_p (decl)
21251                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21252             cond = cp_parser_omp_for_cond (parser, decl);
21253           else
21254             cond = cp_parser_condition (parser);
21255         }
21256       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21257
21258       incr = NULL;
21259       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21260         {
21261           /* If decl is an iterator, preserve the operator on decl
21262              until finish_omp_for.  */
21263           if (decl
21264               && (type_dependent_expression_p (decl)
21265                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21266             incr = cp_parser_omp_for_incr (parser, decl);
21267           else
21268             incr = cp_parser_expression (parser, false);
21269         }
21270
21271       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21272         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21273                                                /*or_comma=*/false,
21274                                                /*consume_paren=*/true);
21275
21276       TREE_VEC_ELT (declv, i) = decl;
21277       TREE_VEC_ELT (initv, i) = init;
21278       TREE_VEC_ELT (condv, i) = cond;
21279       TREE_VEC_ELT (incrv, i) = incr;
21280
21281       if (i == collapse - 1)
21282         break;
21283
21284       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21285          in between the collapsed for loops to be still considered perfectly
21286          nested.  Hopefully the final version clarifies this.
21287          For now handle (multiple) {'s and empty statements.  */
21288       cp_parser_parse_tentatively (parser);
21289       do
21290         {
21291           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21292             break;
21293           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21294             {
21295               cp_lexer_consume_token (parser->lexer);
21296               bracecount++;
21297             }
21298           else if (bracecount
21299                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21300             cp_lexer_consume_token (parser->lexer);
21301           else
21302             {
21303               loc = cp_lexer_peek_token (parser->lexer)->location;
21304               error ("%Hnot enough collapsed for loops", &loc);
21305               collapse_err = true;
21306               cp_parser_abort_tentative_parse (parser);
21307               declv = NULL_TREE;
21308               break;
21309             }
21310         }
21311       while (1);
21312
21313       if (declv)
21314         {
21315           cp_parser_parse_definitely (parser);
21316           nbraces += bracecount;
21317         }
21318     }
21319
21320   /* Note that we saved the original contents of this flag when we entered
21321      the structured block, and so we don't need to re-save it here.  */
21322   parser->in_statement = IN_OMP_FOR;
21323
21324   /* Note that the grammar doesn't call for a structured block here,
21325      though the loop as a whole is a structured block.  */
21326   body = push_stmt_list ();
21327   cp_parser_statement (parser, NULL_TREE, false, NULL);
21328   body = pop_stmt_list (body);
21329
21330   if (declv == NULL_TREE)
21331     ret = NULL_TREE;
21332   else
21333     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21334                           pre_body, clauses);
21335
21336   while (nbraces)
21337     {
21338       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21339         {
21340           cp_lexer_consume_token (parser->lexer);
21341           nbraces--;
21342         }
21343       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21344         cp_lexer_consume_token (parser->lexer);
21345       else
21346         {
21347           if (!collapse_err)
21348             {
21349               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21350               error ("%Hcollapsed loops not perfectly nested", &loc);
21351             }
21352           collapse_err = true;
21353           cp_parser_statement_seq_opt (parser, NULL);
21354           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21355         }
21356     }
21357
21358   while (for_block)
21359     {
21360       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21361       for_block = TREE_CHAIN (for_block);
21362     }
21363
21364   return ret;
21365 }
21366
21367 /* OpenMP 2.5:
21368    #pragma omp for for-clause[optseq] new-line
21369      for-loop  */
21370
21371 #define OMP_FOR_CLAUSE_MASK                             \
21372         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21373         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21374         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21375         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21376         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21377         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21378         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21379         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21380
21381 static tree
21382 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21383 {
21384   tree clauses, sb, ret;
21385   unsigned int save;
21386
21387   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21388                                        "#pragma omp for", pragma_tok);
21389
21390   sb = begin_omp_structured_block ();
21391   save = cp_parser_begin_omp_structured_block (parser);
21392
21393   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21394
21395   cp_parser_end_omp_structured_block (parser, save);
21396   add_stmt (finish_omp_structured_block (sb));
21397
21398   return ret;
21399 }
21400
21401 /* OpenMP 2.5:
21402    # pragma omp master new-line
21403      structured-block  */
21404
21405 static tree
21406 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21407 {
21408   cp_parser_require_pragma_eol (parser, pragma_tok);
21409   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21410 }
21411
21412 /* OpenMP 2.5:
21413    # pragma omp ordered new-line
21414      structured-block  */
21415
21416 static tree
21417 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21418 {
21419   cp_parser_require_pragma_eol (parser, pragma_tok);
21420   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21421 }
21422
21423 /* OpenMP 2.5:
21424
21425    section-scope:
21426      { section-sequence }
21427
21428    section-sequence:
21429      section-directive[opt] structured-block
21430      section-sequence section-directive structured-block  */
21431
21432 static tree
21433 cp_parser_omp_sections_scope (cp_parser *parser)
21434 {
21435   tree stmt, substmt;
21436   bool error_suppress = false;
21437   cp_token *tok;
21438
21439   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21440     return NULL_TREE;
21441
21442   stmt = push_stmt_list ();
21443
21444   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21445     {
21446       unsigned save;
21447
21448       substmt = begin_omp_structured_block ();
21449       save = cp_parser_begin_omp_structured_block (parser);
21450
21451       while (1)
21452         {
21453           cp_parser_statement (parser, NULL_TREE, false, NULL);
21454
21455           tok = cp_lexer_peek_token (parser->lexer);
21456           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21457             break;
21458           if (tok->type == CPP_CLOSE_BRACE)
21459             break;
21460           if (tok->type == CPP_EOF)
21461             break;
21462         }
21463
21464       cp_parser_end_omp_structured_block (parser, save);
21465       substmt = finish_omp_structured_block (substmt);
21466       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21467       add_stmt (substmt);
21468     }
21469
21470   while (1)
21471     {
21472       tok = cp_lexer_peek_token (parser->lexer);
21473       if (tok->type == CPP_CLOSE_BRACE)
21474         break;
21475       if (tok->type == CPP_EOF)
21476         break;
21477
21478       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21479         {
21480           cp_lexer_consume_token (parser->lexer);
21481           cp_parser_require_pragma_eol (parser, tok);
21482           error_suppress = false;
21483         }
21484       else if (!error_suppress)
21485         {
21486           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21487           error_suppress = true;
21488         }
21489
21490       substmt = cp_parser_omp_structured_block (parser);
21491       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21492       add_stmt (substmt);
21493     }
21494   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21495
21496   substmt = pop_stmt_list (stmt);
21497
21498   stmt = make_node (OMP_SECTIONS);
21499   TREE_TYPE (stmt) = void_type_node;
21500   OMP_SECTIONS_BODY (stmt) = substmt;
21501
21502   add_stmt (stmt);
21503   return stmt;
21504 }
21505
21506 /* OpenMP 2.5:
21507    # pragma omp sections sections-clause[optseq] newline
21508      sections-scope  */
21509
21510 #define OMP_SECTIONS_CLAUSE_MASK                        \
21511         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21512         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21513         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21514         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21515         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21516
21517 static tree
21518 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21519 {
21520   tree clauses, ret;
21521
21522   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21523                                        "#pragma omp sections", pragma_tok);
21524
21525   ret = cp_parser_omp_sections_scope (parser);
21526   if (ret)
21527     OMP_SECTIONS_CLAUSES (ret) = clauses;
21528
21529   return ret;
21530 }
21531
21532 /* OpenMP 2.5:
21533    # pragma parallel parallel-clause new-line
21534    # pragma parallel for parallel-for-clause new-line
21535    # pragma parallel sections parallel-sections-clause new-line  */
21536
21537 #define OMP_PARALLEL_CLAUSE_MASK                        \
21538         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21539         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21540         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21541         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21542         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21543         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21544         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21545         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21546
21547 static tree
21548 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21549 {
21550   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21551   const char *p_name = "#pragma omp parallel";
21552   tree stmt, clauses, par_clause, ws_clause, block;
21553   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21554   unsigned int save;
21555
21556   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21557     {
21558       cp_lexer_consume_token (parser->lexer);
21559       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21560       p_name = "#pragma omp parallel for";
21561       mask |= OMP_FOR_CLAUSE_MASK;
21562       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21563     }
21564   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21565     {
21566       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21567       const char *p = IDENTIFIER_POINTER (id);
21568       if (strcmp (p, "sections") == 0)
21569         {
21570           cp_lexer_consume_token (parser->lexer);
21571           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21572           p_name = "#pragma omp parallel sections";
21573           mask |= OMP_SECTIONS_CLAUSE_MASK;
21574           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21575         }
21576     }
21577
21578   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21579   block = begin_omp_parallel ();
21580   save = cp_parser_begin_omp_structured_block (parser);
21581
21582   switch (p_kind)
21583     {
21584     case PRAGMA_OMP_PARALLEL:
21585       cp_parser_statement (parser, NULL_TREE, false, NULL);
21586       par_clause = clauses;
21587       break;
21588
21589     case PRAGMA_OMP_PARALLEL_FOR:
21590       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21591       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21592       break;
21593
21594     case PRAGMA_OMP_PARALLEL_SECTIONS:
21595       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21596       stmt = cp_parser_omp_sections_scope (parser);
21597       if (stmt)
21598         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21599       break;
21600
21601     default:
21602       gcc_unreachable ();
21603     }
21604
21605   cp_parser_end_omp_structured_block (parser, save);
21606   stmt = finish_omp_parallel (par_clause, block);
21607   if (p_kind != PRAGMA_OMP_PARALLEL)
21608     OMP_PARALLEL_COMBINED (stmt) = 1;
21609   return stmt;
21610 }
21611
21612 /* OpenMP 2.5:
21613    # pragma omp single single-clause[optseq] new-line
21614      structured-block  */
21615
21616 #define OMP_SINGLE_CLAUSE_MASK                          \
21617         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21618         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21619         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21620         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21621
21622 static tree
21623 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21624 {
21625   tree stmt = make_node (OMP_SINGLE);
21626   TREE_TYPE (stmt) = void_type_node;
21627
21628   OMP_SINGLE_CLAUSES (stmt)
21629     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21630                                  "#pragma omp single", pragma_tok);
21631   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21632
21633   return add_stmt (stmt);
21634 }
21635
21636 /* OpenMP 3.0:
21637    # pragma omp task task-clause[optseq] new-line
21638      structured-block  */
21639
21640 #define OMP_TASK_CLAUSE_MASK                            \
21641         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21642         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21643         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21644         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21645         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21646         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21647
21648 static tree
21649 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21650 {
21651   tree clauses, block;
21652   unsigned int save;
21653
21654   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21655                                        "#pragma omp task", pragma_tok);
21656   block = begin_omp_task ();
21657   save = cp_parser_begin_omp_structured_block (parser);
21658   cp_parser_statement (parser, NULL_TREE, false, NULL);
21659   cp_parser_end_omp_structured_block (parser, save);
21660   return finish_omp_task (clauses, block);
21661 }
21662
21663 /* OpenMP 3.0:
21664    # pragma omp taskwait new-line  */
21665
21666 static void
21667 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21668 {
21669   cp_parser_require_pragma_eol (parser, pragma_tok);
21670   finish_omp_taskwait ();
21671 }
21672
21673 /* OpenMP 2.5:
21674    # pragma omp threadprivate (variable-list) */
21675
21676 static void
21677 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21678 {
21679   tree vars;
21680
21681   vars = cp_parser_omp_var_list (parser, 0, NULL);
21682   cp_parser_require_pragma_eol (parser, pragma_tok);
21683
21684   finish_omp_threadprivate (vars);
21685 }
21686
21687 /* Main entry point to OpenMP statement pragmas.  */
21688
21689 static void
21690 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21691 {
21692   tree stmt;
21693
21694   switch (pragma_tok->pragma_kind)
21695     {
21696     case PRAGMA_OMP_ATOMIC:
21697       cp_parser_omp_atomic (parser, pragma_tok);
21698       return;
21699     case PRAGMA_OMP_CRITICAL:
21700       stmt = cp_parser_omp_critical (parser, pragma_tok);
21701       break;
21702     case PRAGMA_OMP_FOR:
21703       stmt = cp_parser_omp_for (parser, pragma_tok);
21704       break;
21705     case PRAGMA_OMP_MASTER:
21706       stmt = cp_parser_omp_master (parser, pragma_tok);
21707       break;
21708     case PRAGMA_OMP_ORDERED:
21709       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21710       break;
21711     case PRAGMA_OMP_PARALLEL:
21712       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21713       break;
21714     case PRAGMA_OMP_SECTIONS:
21715       stmt = cp_parser_omp_sections (parser, pragma_tok);
21716       break;
21717     case PRAGMA_OMP_SINGLE:
21718       stmt = cp_parser_omp_single (parser, pragma_tok);
21719       break;
21720     case PRAGMA_OMP_TASK:
21721       stmt = cp_parser_omp_task (parser, pragma_tok);
21722       break;
21723     default:
21724       gcc_unreachable ();
21725     }
21726
21727   if (stmt)
21728     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21729 }
21730 \f
21731 /* The parser.  */
21732
21733 static GTY (()) cp_parser *the_parser;
21734
21735 \f
21736 /* Special handling for the first token or line in the file.  The first
21737    thing in the file might be #pragma GCC pch_preprocess, which loads a
21738    PCH file, which is a GC collection point.  So we need to handle this
21739    first pragma without benefit of an existing lexer structure.
21740
21741    Always returns one token to the caller in *FIRST_TOKEN.  This is
21742    either the true first token of the file, or the first token after
21743    the initial pragma.  */
21744
21745 static void
21746 cp_parser_initial_pragma (cp_token *first_token)
21747 {
21748   tree name = NULL;
21749
21750   cp_lexer_get_preprocessor_token (NULL, first_token);
21751   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21752     return;
21753
21754   cp_lexer_get_preprocessor_token (NULL, first_token);
21755   if (first_token->type == CPP_STRING)
21756     {
21757       name = first_token->u.value;
21758
21759       cp_lexer_get_preprocessor_token (NULL, first_token);
21760       if (first_token->type != CPP_PRAGMA_EOL)
21761         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21762                &first_token->location);
21763     }
21764   else
21765     error ("%Hexpected string literal", &first_token->location);
21766
21767   /* Skip to the end of the pragma.  */
21768   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21769     cp_lexer_get_preprocessor_token (NULL, first_token);
21770
21771   /* Now actually load the PCH file.  */
21772   if (name)
21773     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21774
21775   /* Read one more token to return to our caller.  We have to do this
21776      after reading the PCH file in, since its pointers have to be
21777      live.  */
21778   cp_lexer_get_preprocessor_token (NULL, first_token);
21779 }
21780
21781 /* Normal parsing of a pragma token.  Here we can (and must) use the
21782    regular lexer.  */
21783
21784 static bool
21785 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21786 {
21787   cp_token *pragma_tok;
21788   unsigned int id;
21789
21790   pragma_tok = cp_lexer_consume_token (parser->lexer);
21791   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21792   parser->lexer->in_pragma = true;
21793
21794   id = pragma_tok->pragma_kind;
21795   switch (id)
21796     {
21797     case PRAGMA_GCC_PCH_PREPROCESS:
21798       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21799              &pragma_tok->location);
21800       break;
21801
21802     case PRAGMA_OMP_BARRIER:
21803       switch (context)
21804         {
21805         case pragma_compound:
21806           cp_parser_omp_barrier (parser, pragma_tok);
21807           return false;
21808         case pragma_stmt:
21809           error ("%H%<#pragma omp barrier%> may only be "
21810                  "used in compound statements", &pragma_tok->location);
21811           break;
21812         default:
21813           goto bad_stmt;
21814         }
21815       break;
21816
21817     case PRAGMA_OMP_FLUSH:
21818       switch (context)
21819         {
21820         case pragma_compound:
21821           cp_parser_omp_flush (parser, pragma_tok);
21822           return false;
21823         case pragma_stmt:
21824           error ("%H%<#pragma omp flush%> may only be "
21825                  "used in compound statements", &pragma_tok->location);
21826           break;
21827         default:
21828           goto bad_stmt;
21829         }
21830       break;
21831
21832     case PRAGMA_OMP_TASKWAIT:
21833       switch (context)
21834         {
21835         case pragma_compound:
21836           cp_parser_omp_taskwait (parser, pragma_tok);
21837           return false;
21838         case pragma_stmt:
21839           error ("%H%<#pragma omp taskwait%> may only be "
21840                  "used in compound statements",
21841                  &pragma_tok->location);
21842           break;
21843         default:
21844           goto bad_stmt;
21845         }
21846       break;
21847
21848     case PRAGMA_OMP_THREADPRIVATE:
21849       cp_parser_omp_threadprivate (parser, pragma_tok);
21850       return false;
21851
21852     case PRAGMA_OMP_ATOMIC:
21853     case PRAGMA_OMP_CRITICAL:
21854     case PRAGMA_OMP_FOR:
21855     case PRAGMA_OMP_MASTER:
21856     case PRAGMA_OMP_ORDERED:
21857     case PRAGMA_OMP_PARALLEL:
21858     case PRAGMA_OMP_SECTIONS:
21859     case PRAGMA_OMP_SINGLE:
21860     case PRAGMA_OMP_TASK:
21861       if (context == pragma_external)
21862         goto bad_stmt;
21863       cp_parser_omp_construct (parser, pragma_tok);
21864       return true;
21865
21866     case PRAGMA_OMP_SECTION:
21867       error ("%H%<#pragma omp section%> may only be used in "
21868              "%<#pragma omp sections%> construct", &pragma_tok->location);
21869       break;
21870
21871     default:
21872       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21873       c_invoke_pragma_handler (id);
21874       break;
21875
21876     bad_stmt:
21877       cp_parser_error (parser, "expected declaration specifiers");
21878       break;
21879     }
21880
21881   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21882   return false;
21883 }
21884
21885 /* The interface the pragma parsers have to the lexer.  */
21886
21887 enum cpp_ttype
21888 pragma_lex (tree *value)
21889 {
21890   cp_token *tok;
21891   enum cpp_ttype ret;
21892
21893   tok = cp_lexer_peek_token (the_parser->lexer);
21894
21895   ret = tok->type;
21896   *value = tok->u.value;
21897
21898   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21899     ret = CPP_EOF;
21900   else if (ret == CPP_STRING)
21901     *value = cp_parser_string_literal (the_parser, false, false);
21902   else
21903     {
21904       cp_lexer_consume_token (the_parser->lexer);
21905       if (ret == CPP_KEYWORD)
21906         ret = CPP_NAME;
21907     }
21908
21909   return ret;
21910 }
21911
21912 \f
21913 /* External interface.  */
21914
21915 /* Parse one entire translation unit.  */
21916
21917 void
21918 c_parse_file (void)
21919 {
21920   bool error_occurred;
21921   static bool already_called = false;
21922
21923   if (already_called)
21924     {
21925       sorry ("inter-module optimizations not implemented for C++");
21926       return;
21927     }
21928   already_called = true;
21929
21930   the_parser = cp_parser_new ();
21931   push_deferring_access_checks (flag_access_control
21932                                 ? dk_no_deferred : dk_no_check);
21933   error_occurred = cp_parser_translation_unit (the_parser);
21934   the_parser = NULL;
21935 }
21936
21937 #include "gt-cp-parser.h"