OSDN Git Service

2008-08-18 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A token's value and its associated deferred access checks and
48    qualifying scope.  */
49
50 struct tree_check GTY(())
51 {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct cp_token GTY (())
64 {
65   /* The kind of token.  */
66   ENUM_BITFIELD (cpp_ttype) type : 8;
67   /* If this token is a keyword, this value indicates which keyword.
68      Otherwise, this value is RID_MAX.  */
69   ENUM_BITFIELD (rid) keyword : 8;
70   /* Token flags.  */
71   unsigned char flags;
72   /* Identifier for the pragma.  */
73   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74   /* True if this token is from a context where it is implicitly extern "C" */
75   BOOL_BITFIELD implicit_extern_c : 1;
76   /* True for a CPP_NAME token that is not a keyword (i.e., for which
77      KEYWORD is RID_MAX) iff this name was looked up and found to be
78      ambiguous.  An error has already been reported.  */
79   BOOL_BITFIELD ambiguous_p : 1;
80   /* The value associated with this token, if any.  */
81   union cp_token_value {
82     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
83     struct tree_check* GTY((tag ("1"))) tree_check_value;
84     /* Use for all other tokens.  */
85     tree GTY((tag ("0"))) value;
86   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
87   /* The location at which this token was found.  */
88   location_t location;
89 } cp_token;
90
91 /* We use a stack of token pointer for saving token sets.  */
92 typedef struct cp_token *cp_token_position;
93 DEF_VEC_P (cp_token_position);
94 DEF_VEC_ALLOC_P (cp_token_position,heap);
95
96 static cp_token eof_token =
97 {
98   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, { NULL },
99   0
100 };
101
102 /* The cp_lexer structure represents the C++ lexer.  It is responsible
103    for managing the token stream from the preprocessor and supplying
104    it to the parser.  Tokens are never added to the cp_lexer after
105    it is created.  */
106
107 typedef struct cp_lexer GTY (())
108 {
109   /* The memory allocated for the buffer.  NULL if this lexer does not
110      own the token buffer.  */
111   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
112   /* If the lexer owns the buffer, this is the number of tokens in the
113      buffer.  */
114   size_t buffer_length;
115
116   /* A pointer just past the last available token.  The tokens
117      in this lexer are [buffer, last_token).  */
118   cp_token_position GTY ((skip)) last_token;
119
120   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
121      no more available tokens.  */
122   cp_token_position GTY ((skip)) next_token;
123
124   /* A stack indicating positions at which cp_lexer_save_tokens was
125      called.  The top entry is the most recent position at which we
126      began saving tokens.  If the stack is non-empty, we are saving
127      tokens.  */
128   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
129
130   /* The next lexer in a linked list of lexers.  */
131   struct cp_lexer *next;
132
133   /* True if we should output debugging information.  */
134   bool debugging_p;
135
136   /* True if we're in the context of parsing a pragma, and should not
137      increment past the end-of-line marker.  */
138   bool in_pragma;
139 } cp_lexer;
140
141 /* cp_token_cache is a range of tokens.  There is no need to represent
142    allocate heap memory for it, since tokens are never removed from the
143    lexer's array.  There is also no need for the GC to walk through
144    a cp_token_cache, since everything in here is referenced through
145    a lexer.  */
146
147 typedef struct cp_token_cache GTY(())
148 {
149   /* The beginning of the token range.  */
150   cp_token * GTY((skip)) first;
151
152   /* Points immediately after the last token in the range.  */
153   cp_token * GTY ((skip)) last;
154 } cp_token_cache;
155
156 /* Prototypes.  */
157
158 static cp_lexer *cp_lexer_new_main
159   (void);
160 static cp_lexer *cp_lexer_new_from_tokens
161   (cp_token_cache *tokens);
162 static void cp_lexer_destroy
163   (cp_lexer *);
164 static int cp_lexer_saving_tokens
165   (const cp_lexer *);
166 static cp_token_position cp_lexer_token_position
167   (cp_lexer *, bool);
168 static cp_token *cp_lexer_token_at
169   (cp_lexer *, cp_token_position);
170 static void cp_lexer_get_preprocessor_token
171   (cp_lexer *, cp_token *);
172 static inline cp_token *cp_lexer_peek_token
173   (cp_lexer *);
174 static cp_token *cp_lexer_peek_nth_token
175   (cp_lexer *, size_t);
176 static inline bool cp_lexer_next_token_is
177   (cp_lexer *, enum cpp_ttype);
178 static bool cp_lexer_next_token_is_not
179   (cp_lexer *, enum cpp_ttype);
180 static bool cp_lexer_next_token_is_keyword
181   (cp_lexer *, enum rid);
182 static cp_token *cp_lexer_consume_token
183   (cp_lexer *);
184 static void cp_lexer_purge_token
185   (cp_lexer *);
186 static void cp_lexer_purge_tokens_after
187   (cp_lexer *, cp_token_position);
188 static void cp_lexer_save_tokens
189   (cp_lexer *);
190 static void cp_lexer_commit_tokens
191   (cp_lexer *);
192 static void cp_lexer_rollback_tokens
193   (cp_lexer *);
194 #ifdef ENABLE_CHECKING
195 static void cp_lexer_print_token
196   (FILE *, cp_token *);
197 static inline bool cp_lexer_debugging_p
198   (cp_lexer *);
199 static void cp_lexer_start_debugging
200   (cp_lexer *) ATTRIBUTE_UNUSED;
201 static void cp_lexer_stop_debugging
202   (cp_lexer *) ATTRIBUTE_UNUSED;
203 #else
204 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
205    about passing NULL to functions that require non-NULL arguments
206    (fputs, fprintf).  It will never be used, so all we need is a value
207    of the right type that's guaranteed not to be NULL.  */
208 #define cp_lexer_debug_stream stdout
209 #define cp_lexer_print_token(str, tok) (void) 0
210 #define cp_lexer_debugging_p(lexer) 0
211 #endif /* ENABLE_CHECKING */
212
213 static cp_token_cache *cp_token_cache_new
214   (cp_token *, cp_token *);
215
216 static void cp_parser_initial_pragma
217   (cp_token *);
218
219 /* Manifest constants.  */
220 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
221 #define CP_SAVED_TOKEN_STACK 5
222
223 /* A token type for keywords, as opposed to ordinary identifiers.  */
224 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
225
226 /* A token type for template-ids.  If a template-id is processed while
227    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
228    the value of the CPP_TEMPLATE_ID is whatever was returned by
229    cp_parser_template_id.  */
230 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
231
232 /* A token type for nested-name-specifiers.  If a
233    nested-name-specifier is processed while parsing tentatively, it is
234    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
235    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
236    cp_parser_nested_name_specifier_opt.  */
237 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
238
239 /* A token type for tokens that are not tokens at all; these are used
240    to represent slots in the array where there used to be a token
241    that has now been deleted.  */
242 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
243
244 /* The number of token types, including C++-specific ones.  */
245 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
246
247 /* Variables.  */
248
249 #ifdef ENABLE_CHECKING
250 /* The stream to which debugging output should be written.  */
251 static FILE *cp_lexer_debug_stream;
252 #endif /* ENABLE_CHECKING */
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   cpp_get_options (parse_in)->client_diagnostic = true;
314   cpp_get_callbacks (parse_in)->error = cp_cpp_error;
315
316   gcc_assert (lexer->next_token->type != CPP_PURGED);
317   return lexer;
318 }
319
320 /* Create a new lexer whose token stream is primed with the tokens in
321    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
322
323 static cp_lexer *
324 cp_lexer_new_from_tokens (cp_token_cache *cache)
325 {
326   cp_token *first = cache->first;
327   cp_token *last = cache->last;
328   cp_lexer *lexer = GGC_CNEW (cp_lexer);
329
330   /* We do not own the buffer.  */
331   lexer->buffer = NULL;
332   lexer->buffer_length = 0;
333   lexer->next_token = first == last ? &eof_token : first;
334   lexer->last_token = last;
335
336   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
337                                    CP_SAVED_TOKEN_STACK);
338
339 #ifdef ENABLE_CHECKING
340   /* Initially we are not debugging.  */
341   lexer->debugging_p = false;
342 #endif
343
344   gcc_assert (lexer->next_token->type != CPP_PURGED);
345   return lexer;
346 }
347
348 /* Frees all resources associated with LEXER.  */
349
350 static void
351 cp_lexer_destroy (cp_lexer *lexer)
352 {
353   if (lexer->buffer)
354     ggc_free (lexer->buffer);
355   VEC_free (cp_token_position, heap, lexer->saved_tokens);
356   ggc_free (lexer);
357 }
358
359 /* Returns nonzero if debugging information should be output.  */
360
361 #ifdef ENABLE_CHECKING
362
363 static inline bool
364 cp_lexer_debugging_p (cp_lexer *lexer)
365 {
366   return lexer->debugging_p;
367 }
368
369 #endif /* ENABLE_CHECKING */
370
371 static inline cp_token_position
372 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
373 {
374   gcc_assert (!previous_p || lexer->next_token != &eof_token);
375
376   return lexer->next_token - previous_p;
377 }
378
379 static inline cp_token *
380 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
381 {
382   return pos;
383 }
384
385 /* nonzero if we are presently saving tokens.  */
386
387 static inline int
388 cp_lexer_saving_tokens (const cp_lexer* lexer)
389 {
390   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
391 }
392
393 /* Store the next token from the preprocessor in *TOKEN.  Return true
394    if we reach EOF.  If LEXER is NULL, assume we are handling an
395    initial #pragma pch_preprocess, and thus want the lexer to return
396    processed strings.  */
397
398 static void
399 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
400 {
401   static int is_extern_c = 0;
402
403    /* Get a new token from the preprocessor.  */
404   token->type
405     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
406                         lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
407   token->keyword = RID_MAX;
408   token->pragma_kind = PRAGMA_NONE;
409
410   /* On some systems, some header files are surrounded by an
411      implicit extern "C" block.  Set a flag in the token if it
412      comes from such a header.  */
413   is_extern_c += pending_lang_change;
414   pending_lang_change = 0;
415   token->implicit_extern_c = is_extern_c > 0;
416
417   /* Check to see if this token is a keyword.  */
418   if (token->type == CPP_NAME)
419     {
420       if (C_IS_RESERVED_WORD (token->u.value))
421         {
422           /* Mark this token as a keyword.  */
423           token->type = CPP_KEYWORD;
424           /* Record which keyword.  */
425           token->keyword = C_RID_CODE (token->u.value);
426           /* Update the value.  Some keywords are mapped to particular
427              entities, rather than simply having the value of the
428              corresponding IDENTIFIER_NODE.  For example, `__const' is
429              mapped to `const'.  */
430           token->u.value = ridpointers[token->keyword];
431         }
432       else
433         {
434           if (warn_cxx0x_compat
435               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
436               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
437             {
438               /* Warn about the C++0x keyword (but still treat it as
439                  an identifier).  */
440               warning (OPT_Wc__0x_compat, 
441                        "identifier %<%s%> will become a keyword in C++0x",
442                        IDENTIFIER_POINTER (token->u.value));
443
444               /* Clear out the C_RID_CODE so we don't warn about this
445                  particular identifier-turned-keyword again.  */
446               C_SET_RID_CODE (token->u.value, RID_MAX);
447             }
448
449           token->ambiguous_p = false;
450           token->keyword = RID_MAX;
451         }
452     }
453   /* Handle Objective-C++ keywords.  */
454   else if (token->type == CPP_AT_NAME)
455     {
456       token->type = CPP_KEYWORD;
457       switch (C_RID_CODE (token->u.value))
458         {
459         /* Map 'class' to '@class', 'private' to '@private', etc.  */
460         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
461         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
462         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
463         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
464         case RID_THROW: token->keyword = RID_AT_THROW; break;
465         case RID_TRY: token->keyword = RID_AT_TRY; break;
466         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
467         default: token->keyword = C_RID_CODE (token->u.value);
468         }
469     }
470   else if (token->type == CPP_PRAGMA)
471     {
472       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
473       token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
474       token->u.value = NULL_TREE;
475     }
476 }
477
478 /* Update the globals input_location and the input file stack from TOKEN.  */
479 static inline void
480 cp_lexer_set_source_position_from_token (cp_token *token)
481 {
482   if (token->type != CPP_EOF)
483     {
484       input_location = token->location;
485     }
486 }
487
488 /* Return a pointer to the next token in the token stream, but do not
489    consume it.  */
490
491 static inline cp_token *
492 cp_lexer_peek_token (cp_lexer *lexer)
493 {
494   if (cp_lexer_debugging_p (lexer))
495     {
496       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
497       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
498       putc ('\n', cp_lexer_debug_stream);
499     }
500   return lexer->next_token;
501 }
502
503 /* Return true if the next token has the indicated TYPE.  */
504
505 static inline bool
506 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
507 {
508   return cp_lexer_peek_token (lexer)->type == type;
509 }
510
511 /* Return true if the next token does not have the indicated TYPE.  */
512
513 static inline bool
514 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
515 {
516   return !cp_lexer_next_token_is (lexer, type);
517 }
518
519 /* Return true if the next token is the indicated KEYWORD.  */
520
521 static inline bool
522 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
523 {
524   return cp_lexer_peek_token (lexer)->keyword == keyword;
525 }
526
527 /* Return true if the next token is not the indicated KEYWORD.  */
528
529 static inline bool
530 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
531 {
532   return cp_lexer_peek_token (lexer)->keyword != keyword;
533 }
534
535 /* Return true if the next token is a keyword for a decl-specifier.  */
536
537 static bool
538 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
539 {
540   cp_token *token;
541
542   token = cp_lexer_peek_token (lexer);
543   switch (token->keyword) 
544     {
545       /* auto specifier: storage-class-specifier in C++,
546          simple-type-specifier in C++0x.  */
547     case RID_AUTO:
548       /* Storage classes.  */
549     case RID_REGISTER:
550     case RID_STATIC:
551     case RID_EXTERN:
552     case RID_MUTABLE:
553     case RID_THREAD:
554       /* Elaborated type specifiers.  */
555     case RID_ENUM:
556     case RID_CLASS:
557     case RID_STRUCT:
558     case RID_UNION:
559     case RID_TYPENAME:
560       /* Simple type specifiers.  */
561     case RID_CHAR:
562     case RID_CHAR16:
563     case RID_CHAR32:
564     case RID_WCHAR:
565     case RID_BOOL:
566     case RID_SHORT:
567     case RID_INT:
568     case RID_LONG:
569     case RID_SIGNED:
570     case RID_UNSIGNED:
571     case RID_FLOAT:
572     case RID_DOUBLE:
573     case RID_VOID:
574       /* GNU extensions.  */ 
575     case RID_ATTRIBUTE:
576     case RID_TYPEOF:
577       /* C++0x extensions.  */
578     case RID_DECLTYPE:
579       return true;
580
581     default:
582       return false;
583     }
584 }
585
586 /* Return a pointer to the Nth token in the token stream.  If N is 1,
587    then this is precisely equivalent to cp_lexer_peek_token (except
588    that it is not inline).  One would like to disallow that case, but
589    there is one case (cp_parser_nth_token_starts_template_id) where
590    the caller passes a variable for N and it might be 1.  */
591
592 static cp_token *
593 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 {
595   cp_token *token;
596
597   /* N is 1-based, not zero-based.  */
598   gcc_assert (n > 0);
599
600   if (cp_lexer_debugging_p (lexer))
601     fprintf (cp_lexer_debug_stream,
602              "cp_lexer: peeking ahead %ld at token: ", (long)n);
603
604   --n;
605   token = lexer->next_token;
606   gcc_assert (!n || token != &eof_token);
607   while (n != 0)
608     {
609       ++token;
610       if (token == lexer->last_token)
611         {
612           token = &eof_token;
613           break;
614         }
615
616       if (token->type != CPP_PURGED)
617         --n;
618     }
619
620   if (cp_lexer_debugging_p (lexer))
621     {
622       cp_lexer_print_token (cp_lexer_debug_stream, token);
623       putc ('\n', cp_lexer_debug_stream);
624     }
625
626   return token;
627 }
628
629 /* Return the next token, and advance the lexer's next_token pointer
630    to point to the next non-purged token.  */
631
632 static cp_token *
633 cp_lexer_consume_token (cp_lexer* lexer)
634 {
635   cp_token *token = lexer->next_token;
636
637   gcc_assert (token != &eof_token);
638   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
639
640   do
641     {
642       lexer->next_token++;
643       if (lexer->next_token == lexer->last_token)
644         {
645           lexer->next_token = &eof_token;
646           break;
647         }
648
649     }
650   while (lexer->next_token->type == CPP_PURGED);
651
652   cp_lexer_set_source_position_from_token (token);
653
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     {
657       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
658       cp_lexer_print_token (cp_lexer_debug_stream, token);
659       putc ('\n', cp_lexer_debug_stream);
660     }
661
662   return token;
663 }
664
665 /* Permanently remove the next token from the token stream, and
666    advance the next_token pointer to refer to the next non-purged
667    token.  */
668
669 static void
670 cp_lexer_purge_token (cp_lexer *lexer)
671 {
672   cp_token *tok = lexer->next_token;
673
674   gcc_assert (tok != &eof_token);
675   tok->type = CPP_PURGED;
676   tok->location = UNKNOWN_LOCATION;
677   tok->u.value = NULL_TREE;
678   tok->keyword = RID_MAX;
679
680   do
681     {
682       tok++;
683       if (tok == lexer->last_token)
684         {
685           tok = &eof_token;
686           break;
687         }
688     }
689   while (tok->type == CPP_PURGED);
690   lexer->next_token = tok;
691 }
692
693 /* Permanently remove all tokens after TOK, up to, but not
694    including, the token that will be returned next by
695    cp_lexer_peek_token.  */
696
697 static void
698 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
699 {
700   cp_token *peek = lexer->next_token;
701
702   if (peek == &eof_token)
703     peek = lexer->last_token;
704
705   gcc_assert (tok < peek);
706
707   for ( tok += 1; tok != peek; tok += 1)
708     {
709       tok->type = CPP_PURGED;
710       tok->location = UNKNOWN_LOCATION;
711       tok->u.value = NULL_TREE;
712       tok->keyword = RID_MAX;
713     }
714 }
715
716 /* Begin saving tokens.  All tokens consumed after this point will be
717    preserved.  */
718
719 static void
720 cp_lexer_save_tokens (cp_lexer* lexer)
721 {
722   /* Provide debugging output.  */
723   if (cp_lexer_debugging_p (lexer))
724     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
725
726   VEC_safe_push (cp_token_position, heap,
727                  lexer->saved_tokens, lexer->next_token);
728 }
729
730 /* Commit to the portion of the token stream most recently saved.  */
731
732 static void
733 cp_lexer_commit_tokens (cp_lexer* lexer)
734 {
735   /* Provide debugging output.  */
736   if (cp_lexer_debugging_p (lexer))
737     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
738
739   VEC_pop (cp_token_position, lexer->saved_tokens);
740 }
741
742 /* Return all tokens saved since the last call to cp_lexer_save_tokens
743    to the token stream.  Stop saving tokens.  */
744
745 static void
746 cp_lexer_rollback_tokens (cp_lexer* lexer)
747 {
748   /* Provide debugging output.  */
749   if (cp_lexer_debugging_p (lexer))
750     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
751
752   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
753 }
754
755 /* Print a representation of the TOKEN on the STREAM.  */
756
757 #ifdef ENABLE_CHECKING
758
759 static void
760 cp_lexer_print_token (FILE * stream, cp_token *token)
761 {
762   /* We don't use cpp_type2name here because the parser defines
763      a few tokens of its own.  */
764   static const char *const token_names[] = {
765     /* cpplib-defined token types */
766 #define OP(e, s) #e,
767 #define TK(e, s) #e,
768     TTYPE_TABLE
769 #undef OP
770 #undef TK
771     /* C++ parser token types - see "Manifest constants", above.  */
772     "KEYWORD",
773     "TEMPLATE_ID",
774     "NESTED_NAME_SPECIFIER",
775     "PURGED"
776   };
777
778   /* If we have a name for the token, print it out.  Otherwise, we
779      simply give the numeric code.  */
780   gcc_assert (token->type < ARRAY_SIZE(token_names));
781   fputs (token_names[token->type], stream);
782
783   /* For some tokens, print the associated data.  */
784   switch (token->type)
785     {
786     case CPP_KEYWORD:
787       /* Some keywords have a value that is not an IDENTIFIER_NODE.
788          For example, `struct' is mapped to an INTEGER_CST.  */
789       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
790         break;
791       /* else fall through */
792     case CPP_NAME:
793       fputs (IDENTIFIER_POINTER (token->u.value), stream);
794       break;
795
796     case CPP_STRING:
797     case CPP_STRING16:
798     case CPP_STRING32:
799     case CPP_WSTRING:
800       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
801       break;
802
803     default:
804       break;
805     }
806 }
807
808 /* Start emitting debugging information.  */
809
810 static void
811 cp_lexer_start_debugging (cp_lexer* lexer)
812 {
813   lexer->debugging_p = true;
814 }
815
816 /* Stop emitting debugging information.  */
817
818 static void
819 cp_lexer_stop_debugging (cp_lexer* lexer)
820 {
821   lexer->debugging_p = false;
822 }
823
824 #endif /* ENABLE_CHECKING */
825
826 /* Create a new cp_token_cache, representing a range of tokens.  */
827
828 static cp_token_cache *
829 cp_token_cache_new (cp_token *first, cp_token *last)
830 {
831   cp_token_cache *cache = GGC_NEW (cp_token_cache);
832   cache->first = first;
833   cache->last = last;
834   return cache;
835 }
836
837 \f
838 /* Decl-specifiers.  */
839
840 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
841
842 static void
843 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 {
845   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
846 }
847
848 /* Declarators.  */
849
850 /* Nothing other than the parser should be creating declarators;
851    declarators are a semi-syntactic representation of C++ entities.
852    Other parts of the front end that need to create entities (like
853    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
854
855 static cp_declarator *make_call_declarator
856   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
857 static cp_declarator *make_array_declarator
858   (cp_declarator *, tree);
859 static cp_declarator *make_pointer_declarator
860   (cp_cv_quals, cp_declarator *);
861 static cp_declarator *make_reference_declarator
862   (cp_cv_quals, cp_declarator *, bool);
863 static cp_parameter_declarator *make_parameter_declarator
864   (cp_decl_specifier_seq *, cp_declarator *, tree);
865 static cp_declarator *make_ptrmem_declarator
866   (cp_cv_quals, tree, cp_declarator *);
867
868 /* An erroneous declarator.  */
869 static cp_declarator *cp_error_declarator;
870
871 /* The obstack on which declarators and related data structures are
872    allocated.  */
873 static struct obstack declarator_obstack;
874
875 /* Alloc BYTES from the declarator memory pool.  */
876
877 static inline void *
878 alloc_declarator (size_t bytes)
879 {
880   return obstack_alloc (&declarator_obstack, bytes);
881 }
882
883 /* Allocate a declarator of the indicated KIND.  Clear fields that are
884    common to all declarators.  */
885
886 static cp_declarator *
887 make_declarator (cp_declarator_kind kind)
888 {
889   cp_declarator *declarator;
890
891   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
892   declarator->kind = kind;
893   declarator->attributes = NULL_TREE;
894   declarator->declarator = NULL;
895   declarator->parameter_pack_p = false;
896
897   return declarator;
898 }
899
900 /* Make a declarator for a generalized identifier.  If
901    QUALIFYING_SCOPE is non-NULL, the identifier is
902    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
903    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
904    is, if any.   */
905
906 static cp_declarator *
907 make_id_declarator (tree qualifying_scope, tree unqualified_name,
908                     special_function_kind sfk)
909 {
910   cp_declarator *declarator;
911
912   /* It is valid to write:
913
914        class C { void f(); };
915        typedef C D;
916        void D::f();
917
918      The standard is not clear about whether `typedef const C D' is
919      legal; as of 2002-09-15 the committee is considering that
920      question.  EDG 3.0 allows that syntax.  Therefore, we do as
921      well.  */
922   if (qualifying_scope && TYPE_P (qualifying_scope))
923     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924
925   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
926               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
927               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928
929   declarator = make_declarator (cdk_id);
930   declarator->u.id.qualifying_scope = qualifying_scope;
931   declarator->u.id.unqualified_name = unqualified_name;
932   declarator->u.id.sfk = sfk;
933   
934   return declarator;
935 }
936
937 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
938    of modifiers such as const or volatile to apply to the pointer
939    type, represented as identifiers.  */
940
941 cp_declarator *
942 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 {
944   cp_declarator *declarator;
945
946   declarator = make_declarator (cdk_pointer);
947   declarator->declarator = target;
948   declarator->u.pointer.qualifiers = cv_qualifiers;
949   declarator->u.pointer.class_type = NULL_TREE;
950   if (target)
951     {
952       declarator->parameter_pack_p = target->parameter_pack_p;
953       target->parameter_pack_p = false;
954     }
955   else
956     declarator->parameter_pack_p = false;
957
958   return declarator;
959 }
960
961 /* Like make_pointer_declarator -- but for references.  */
962
963 cp_declarator *
964 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
965                            bool rvalue_ref)
966 {
967   cp_declarator *declarator;
968
969   declarator = make_declarator (cdk_reference);
970   declarator->declarator = target;
971   declarator->u.reference.qualifiers = cv_qualifiers;
972   declarator->u.reference.rvalue_ref = rvalue_ref;
973   if (target)
974     {
975       declarator->parameter_pack_p = target->parameter_pack_p;
976       target->parameter_pack_p = false;
977     }
978   else
979     declarator->parameter_pack_p = false;
980
981   return declarator;
982 }
983
984 /* Like make_pointer_declarator -- but for a pointer to a non-static
985    member of CLASS_TYPE.  */
986
987 cp_declarator *
988 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
989                         cp_declarator *pointee)
990 {
991   cp_declarator *declarator;
992
993   declarator = make_declarator (cdk_ptrmem);
994   declarator->declarator = pointee;
995   declarator->u.pointer.qualifiers = cv_qualifiers;
996   declarator->u.pointer.class_type = class_type;
997
998   if (pointee)
999     {
1000       declarator->parameter_pack_p = pointee->parameter_pack_p;
1001       pointee->parameter_pack_p = false;
1002     }
1003   else
1004     declarator->parameter_pack_p = false;
1005
1006   return declarator;
1007 }
1008
1009 /* Make a declarator for the function given by TARGET, with the
1010    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1011    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1012    indicates what exceptions can be thrown.  */
1013
1014 cp_declarator *
1015 make_call_declarator (cp_declarator *target,
1016                       cp_parameter_declarator *parms,
1017                       cp_cv_quals cv_qualifiers,
1018                       tree exception_specification)
1019 {
1020   cp_declarator *declarator;
1021
1022   declarator = make_declarator (cdk_function);
1023   declarator->declarator = target;
1024   declarator->u.function.parameters = parms;
1025   declarator->u.function.qualifiers = cv_qualifiers;
1026   declarator->u.function.exception_specification = exception_specification;
1027   if (target)
1028     {
1029       declarator->parameter_pack_p = target->parameter_pack_p;
1030       target->parameter_pack_p = false;
1031     }
1032   else
1033     declarator->parameter_pack_p = false;
1034
1035   return declarator;
1036 }
1037
1038 /* Make a declarator for an array of BOUNDS elements, each of which is
1039    defined by ELEMENT.  */
1040
1041 cp_declarator *
1042 make_array_declarator (cp_declarator *element, tree bounds)
1043 {
1044   cp_declarator *declarator;
1045
1046   declarator = make_declarator (cdk_array);
1047   declarator->declarator = element;
1048   declarator->u.array.bounds = bounds;
1049   if (element)
1050     {
1051       declarator->parameter_pack_p = element->parameter_pack_p;
1052       element->parameter_pack_p = false;
1053     }
1054   else
1055     declarator->parameter_pack_p = false;
1056
1057   return declarator;
1058 }
1059
1060 /* Determine whether the declarator we've seen so far can be a
1061    parameter pack, when followed by an ellipsis.  */
1062 static bool 
1063 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 {
1065   /* Search for a declarator name, or any other declarator that goes
1066      after the point where the ellipsis could appear in a parameter
1067      pack. If we find any of these, then this declarator can not be
1068      made into a parameter pack.  */
1069   bool found = false;
1070   while (declarator && !found)
1071     {
1072       switch ((int)declarator->kind)
1073         {
1074         case cdk_id:
1075         case cdk_array:
1076           found = true;
1077           break;
1078
1079         case cdk_error:
1080           return true;
1081
1082         default:
1083           declarator = declarator->declarator;
1084           break;
1085         }
1086     }
1087
1088   return !found;
1089 }
1090
1091 cp_parameter_declarator *no_parameters;
1092
1093 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1094    DECLARATOR and DEFAULT_ARGUMENT.  */
1095
1096 cp_parameter_declarator *
1097 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1098                            cp_declarator *declarator,
1099                            tree default_argument)
1100 {
1101   cp_parameter_declarator *parameter;
1102
1103   parameter = ((cp_parameter_declarator *)
1104                alloc_declarator (sizeof (cp_parameter_declarator)));
1105   parameter->next = NULL;
1106   if (decl_specifiers)
1107     parameter->decl_specifiers = *decl_specifiers;
1108   else
1109     clear_decl_specs (&parameter->decl_specifiers);
1110   parameter->declarator = declarator;
1111   parameter->default_argument = default_argument;
1112   parameter->ellipsis_p = false;
1113
1114   return parameter;
1115 }
1116
1117 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1118
1119 static bool
1120 function_declarator_p (const cp_declarator *declarator)
1121 {
1122   while (declarator)
1123     {
1124       if (declarator->kind == cdk_function
1125           && declarator->declarator->kind == cdk_id)
1126         return true;
1127       if (declarator->kind == cdk_id
1128           || declarator->kind == cdk_error)
1129         return false;
1130       declarator = declarator->declarator;
1131     }
1132   return false;
1133 }
1134  
1135 /* The parser.  */
1136
1137 /* Overview
1138    --------
1139
1140    A cp_parser parses the token stream as specified by the C++
1141    grammar.  Its job is purely parsing, not semantic analysis.  For
1142    example, the parser breaks the token stream into declarators,
1143    expressions, statements, and other similar syntactic constructs.
1144    It does not check that the types of the expressions on either side
1145    of an assignment-statement are compatible, or that a function is
1146    not declared with a parameter of type `void'.
1147
1148    The parser invokes routines elsewhere in the compiler to perform
1149    semantic analysis and to build up the abstract syntax tree for the
1150    code processed.
1151
1152    The parser (and the template instantiation code, which is, in a
1153    way, a close relative of parsing) are the only parts of the
1154    compiler that should be calling push_scope and pop_scope, or
1155    related functions.  The parser (and template instantiation code)
1156    keeps track of what scope is presently active; everything else
1157    should simply honor that.  (The code that generates static
1158    initializers may also need to set the scope, in order to check
1159    access control correctly when emitting the initializers.)
1160
1161    Methodology
1162    -----------
1163
1164    The parser is of the standard recursive-descent variety.  Upcoming
1165    tokens in the token stream are examined in order to determine which
1166    production to use when parsing a non-terminal.  Some C++ constructs
1167    require arbitrary look ahead to disambiguate.  For example, it is
1168    impossible, in the general case, to tell whether a statement is an
1169    expression or declaration without scanning the entire statement.
1170    Therefore, the parser is capable of "parsing tentatively."  When the
1171    parser is not sure what construct comes next, it enters this mode.
1172    Then, while we attempt to parse the construct, the parser queues up
1173    error messages, rather than issuing them immediately, and saves the
1174    tokens it consumes.  If the construct is parsed successfully, the
1175    parser "commits", i.e., it issues any queued error messages and
1176    the tokens that were being preserved are permanently discarded.
1177    If, however, the construct is not parsed successfully, the parser
1178    rolls back its state completely so that it can resume parsing using
1179    a different alternative.
1180
1181    Future Improvements
1182    -------------------
1183
1184    The performance of the parser could probably be improved substantially.
1185    We could often eliminate the need to parse tentatively by looking ahead
1186    a little bit.  In some places, this approach might not entirely eliminate
1187    the need to parse tentatively, but it might still speed up the average
1188    case.  */
1189
1190 /* Flags that are passed to some parsing functions.  These values can
1191    be bitwise-ored together.  */
1192
1193 typedef enum cp_parser_flags
1194 {
1195   /* No flags.  */
1196   CP_PARSER_FLAGS_NONE = 0x0,
1197   /* The construct is optional.  If it is not present, then no error
1198      should be issued.  */
1199   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1200   /* When parsing a type-specifier, do not allow user-defined types.  */
1201   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 } cp_parser_flags;
1203
1204 /* The different kinds of declarators we want to parse.  */
1205
1206 typedef enum cp_parser_declarator_kind
1207 {
1208   /* We want an abstract declarator.  */
1209   CP_PARSER_DECLARATOR_ABSTRACT,
1210   /* We want a named declarator.  */
1211   CP_PARSER_DECLARATOR_NAMED,
1212   /* We don't mind, but the name must be an unqualified-id.  */
1213   CP_PARSER_DECLARATOR_EITHER
1214 } cp_parser_declarator_kind;
1215
1216 /* The precedence values used to parse binary expressions.  The minimum value
1217    of PREC must be 1, because zero is reserved to quickly discriminate
1218    binary operators from other tokens.  */
1219
1220 enum cp_parser_prec
1221 {
1222   PREC_NOT_OPERATOR,
1223   PREC_LOGICAL_OR_EXPRESSION,
1224   PREC_LOGICAL_AND_EXPRESSION,
1225   PREC_INCLUSIVE_OR_EXPRESSION,
1226   PREC_EXCLUSIVE_OR_EXPRESSION,
1227   PREC_AND_EXPRESSION,
1228   PREC_EQUALITY_EXPRESSION,
1229   PREC_RELATIONAL_EXPRESSION,
1230   PREC_SHIFT_EXPRESSION,
1231   PREC_ADDITIVE_EXPRESSION,
1232   PREC_MULTIPLICATIVE_EXPRESSION,
1233   PREC_PM_EXPRESSION,
1234   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 };
1236
1237 /* A mapping from a token type to a corresponding tree node type, with a
1238    precedence value.  */
1239
1240 typedef struct cp_parser_binary_operations_map_node
1241 {
1242   /* The token type.  */
1243   enum cpp_ttype token_type;
1244   /* The corresponding tree code.  */
1245   enum tree_code tree_type;
1246   /* The precedence of this operator.  */
1247   enum cp_parser_prec prec;
1248 } cp_parser_binary_operations_map_node;
1249
1250 /* The status of a tentative parse.  */
1251
1252 typedef enum cp_parser_status_kind
1253 {
1254   /* No errors have occurred.  */
1255   CP_PARSER_STATUS_KIND_NO_ERROR,
1256   /* An error has occurred.  */
1257   CP_PARSER_STATUS_KIND_ERROR,
1258   /* We are committed to this tentative parse, whether or not an error
1259      has occurred.  */
1260   CP_PARSER_STATUS_KIND_COMMITTED
1261 } cp_parser_status_kind;
1262
1263 typedef struct cp_parser_expression_stack_entry
1264 {
1265   /* Left hand side of the binary operation we are currently
1266      parsing.  */
1267   tree lhs;
1268   /* Original tree code for left hand side, if it was a binary
1269      expression itself (used for -Wparentheses).  */
1270   enum tree_code lhs_type;
1271   /* Tree code for the binary operation we are parsing.  */
1272   enum tree_code tree_type;
1273   /* Precedence of the binary operation we are parsing.  */
1274   int prec;
1275 } cp_parser_expression_stack_entry;
1276
1277 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1278    entries because precedence levels on the stack are monotonically
1279    increasing.  */
1280 typedef struct cp_parser_expression_stack_entry
1281   cp_parser_expression_stack[NUM_PREC_VALUES];
1282
1283 /* Context that is saved and restored when parsing tentatively.  */
1284 typedef struct cp_parser_context GTY (())
1285 {
1286   /* If this is a tentative parsing context, the status of the
1287      tentative parse.  */
1288   enum cp_parser_status_kind status;
1289   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1290      that are looked up in this context must be looked up both in the
1291      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1292      the context of the containing expression.  */
1293   tree object_type;
1294
1295   /* The next parsing context in the stack.  */
1296   struct cp_parser_context *next;
1297 } cp_parser_context;
1298
1299 /* Prototypes.  */
1300
1301 /* Constructors and destructors.  */
1302
1303 static cp_parser_context *cp_parser_context_new
1304   (cp_parser_context *);
1305
1306 /* Class variables.  */
1307
1308 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1309
1310 /* The operator-precedence table used by cp_parser_binary_expression.
1311    Transformed into an associative array (binops_by_token) by
1312    cp_parser_new.  */
1313
1314 static const cp_parser_binary_operations_map_node binops[] = {
1315   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1316   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1317
1318   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1319   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1321
1322   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1324
1325   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1327
1328   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1329   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1330   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1332
1333   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1334   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1335
1336   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1337
1338   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1339
1340   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1341
1342   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1343
1344   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 };
1346
1347 /* The same as binops, but initialized by cp_parser_new so that
1348    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1349    for speed.  */
1350 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1351
1352 /* Constructors and destructors.  */
1353
1354 /* Construct a new context.  The context below this one on the stack
1355    is given by NEXT.  */
1356
1357 static cp_parser_context *
1358 cp_parser_context_new (cp_parser_context* next)
1359 {
1360   cp_parser_context *context;
1361
1362   /* Allocate the storage.  */
1363   if (cp_parser_context_free_list != NULL)
1364     {
1365       /* Pull the first entry from the free list.  */
1366       context = cp_parser_context_free_list;
1367       cp_parser_context_free_list = context->next;
1368       memset (context, 0, sizeof (*context));
1369     }
1370   else
1371     context = GGC_CNEW (cp_parser_context);
1372
1373   /* No errors have occurred yet in this context.  */
1374   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1375   /* If this is not the bottommost context, copy information that we
1376      need from the previous context.  */
1377   if (next)
1378     {
1379       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1380          expression, then we are parsing one in this context, too.  */
1381       context->object_type = next->object_type;
1382       /* Thread the stack.  */
1383       context->next = next;
1384     }
1385
1386   return context;
1387 }
1388
1389 /* The cp_parser structure represents the C++ parser.  */
1390
1391 typedef struct cp_parser GTY(())
1392 {
1393   /* The lexer from which we are obtaining tokens.  */
1394   cp_lexer *lexer;
1395
1396   /* The scope in which names should be looked up.  If NULL_TREE, then
1397      we look up names in the scope that is currently open in the
1398      source program.  If non-NULL, this is either a TYPE or
1399      NAMESPACE_DECL for the scope in which we should look.  It can
1400      also be ERROR_MARK, when we've parsed a bogus scope.
1401
1402      This value is not cleared automatically after a name is looked
1403      up, so we must be careful to clear it before starting a new look
1404      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1405      will look up `Z' in the scope of `X', rather than the current
1406      scope.)  Unfortunately, it is difficult to tell when name lookup
1407      is complete, because we sometimes peek at a token, look it up,
1408      and then decide not to consume it.   */
1409   tree scope;
1410
1411   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1412      last lookup took place.  OBJECT_SCOPE is used if an expression
1413      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1414      respectively.  QUALIFYING_SCOPE is used for an expression of the
1415      form "X::Y"; it refers to X.  */
1416   tree object_scope;
1417   tree qualifying_scope;
1418
1419   /* A stack of parsing contexts.  All but the bottom entry on the
1420      stack will be tentative contexts.
1421
1422      We parse tentatively in order to determine which construct is in
1423      use in some situations.  For example, in order to determine
1424      whether a statement is an expression-statement or a
1425      declaration-statement we parse it tentatively as a
1426      declaration-statement.  If that fails, we then reparse the same
1427      token stream as an expression-statement.  */
1428   cp_parser_context *context;
1429
1430   /* True if we are parsing GNU C++.  If this flag is not set, then
1431      GNU extensions are not recognized.  */
1432   bool allow_gnu_extensions_p;
1433
1434   /* TRUE if the `>' token should be interpreted as the greater-than
1435      operator.  FALSE if it is the end of a template-id or
1436      template-parameter-list. In C++0x mode, this flag also applies to
1437      `>>' tokens, which are viewed as two consecutive `>' tokens when
1438      this flag is FALSE.  */
1439   bool greater_than_is_operator_p;
1440
1441   /* TRUE if default arguments are allowed within a parameter list
1442      that starts at this point. FALSE if only a gnu extension makes
1443      them permissible.  */
1444   bool default_arg_ok_p;
1445
1446   /* TRUE if we are parsing an integral constant-expression.  See
1447      [expr.const] for a precise definition.  */
1448   bool integral_constant_expression_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression -- but a
1451      non-constant expression should be permitted as well.  This flag
1452      is used when parsing an array bound so that GNU variable-length
1453      arrays are tolerated.  */
1454   bool allow_non_integral_constant_expression_p;
1455
1456   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1457      been seen that makes the expression non-constant.  */
1458   bool non_integral_constant_expression_p;
1459
1460   /* TRUE if local variable names and `this' are forbidden in the
1461      current context.  */
1462   bool local_variables_forbidden_p;
1463
1464   /* TRUE if the declaration we are parsing is part of a
1465      linkage-specification of the form `extern string-literal
1466      declaration'.  */
1467   bool in_unbraced_linkage_specification_p;
1468
1469   /* TRUE if we are presently parsing a declarator, after the
1470      direct-declarator.  */
1471   bool in_declarator_p;
1472
1473   /* TRUE if we are presently parsing a template-argument-list.  */
1474   bool in_template_argument_list_p;
1475
1476   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1477      to IN_OMP_BLOCK if parsing OpenMP structured block and
1478      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1479      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1480      iteration-statement, OpenMP block or loop within that switch.  */
1481 #define IN_SWITCH_STMT          1
1482 #define IN_ITERATION_STMT       2
1483 #define IN_OMP_BLOCK            4
1484 #define IN_OMP_FOR              8
1485 #define IN_IF_STMT             16
1486   unsigned char in_statement;
1487
1488   /* TRUE if we are presently parsing the body of a switch statement.
1489      Note that this doesn't quite overlap with in_statement above.
1490      The difference relates to giving the right sets of error messages:
1491      "case not in switch" vs "break statement used with OpenMP...".  */
1492   bool in_switch_statement_p;
1493
1494   /* TRUE if we are parsing a type-id in an expression context.  In
1495      such a situation, both "type (expr)" and "type (type)" are valid
1496      alternatives.  */
1497   bool in_type_id_in_expr_p;
1498
1499   /* TRUE if we are currently in a header file where declarations are
1500      implicitly extern "C".  */
1501   bool implicit_extern_c;
1502
1503   /* TRUE if strings in expressions should be translated to the execution
1504      character set.  */
1505   bool translate_strings_p;
1506
1507   /* TRUE if we are presently parsing the body of a function, but not
1508      a local class.  */
1509   bool in_function_body;
1510
1511   /* If non-NULL, then we are parsing a construct where new type
1512      definitions are not permitted.  The string stored here will be
1513      issued as an error message if a type is defined.  */
1514   const char *type_definition_forbidden_message;
1515
1516   /* A list of lists. The outer list is a stack, used for member
1517      functions of local classes. At each level there are two sub-list,
1518      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1519      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1520      TREE_VALUE's. The functions are chained in reverse declaration
1521      order.
1522
1523      The TREE_PURPOSE sublist contains those functions with default
1524      arguments that need post processing, and the TREE_VALUE sublist
1525      contains those functions with definitions that need post
1526      processing.
1527
1528      These lists can only be processed once the outermost class being
1529      defined is complete.  */
1530   tree unparsed_functions_queues;
1531
1532   /* The number of classes whose definitions are currently in
1533      progress.  */
1534   unsigned num_classes_being_defined;
1535
1536   /* The number of template parameter lists that apply directly to the
1537      current declaration.  */
1538   unsigned num_template_parameter_lists;
1539 } cp_parser;
1540
1541 /* Prototypes.  */
1542
1543 /* Constructors and destructors.  */
1544
1545 static cp_parser *cp_parser_new
1546   (void);
1547
1548 /* Routines to parse various constructs.
1549
1550    Those that return `tree' will return the error_mark_node (rather
1551    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1552    Sometimes, they will return an ordinary node if error-recovery was
1553    attempted, even though a parse error occurred.  So, to check
1554    whether or not a parse error occurred, you should always use
1555    cp_parser_error_occurred.  If the construct is optional (indicated
1556    either by an `_opt' in the name of the function that does the
1557    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1558    the construct is not present.  */
1559
1560 /* Lexical conventions [gram.lex]  */
1561
1562 static tree cp_parser_identifier
1563   (cp_parser *);
1564 static tree cp_parser_string_literal
1565   (cp_parser *, bool, bool);
1566
1567 /* Basic concepts [gram.basic]  */
1568
1569 static bool cp_parser_translation_unit
1570   (cp_parser *);
1571
1572 /* Expressions [gram.expr]  */
1573
1574 static tree cp_parser_primary_expression
1575   (cp_parser *, bool, bool, bool, cp_id_kind *);
1576 static tree cp_parser_id_expression
1577   (cp_parser *, bool, bool, bool *, bool, bool);
1578 static tree cp_parser_unqualified_id
1579   (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier_opt
1581   (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_nested_name_specifier
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_class_or_namespace_name
1585   (cp_parser *, bool, bool, bool, bool, bool);
1586 static tree cp_parser_postfix_expression
1587   (cp_parser *, bool, bool, bool);
1588 static tree cp_parser_postfix_open_square_expression
1589   (cp_parser *, tree, bool);
1590 static tree cp_parser_postfix_dot_deref_expression
1591   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1592 static tree cp_parser_parenthesized_expression_list
1593   (cp_parser *, bool, bool, bool, bool *);
1594 static void cp_parser_pseudo_destructor_name
1595   (cp_parser *, tree *, tree *);
1596 static tree cp_parser_unary_expression
1597   (cp_parser *, bool, bool);
1598 static enum tree_code cp_parser_unary_operator
1599   (cp_token *);
1600 static tree cp_parser_new_expression
1601   (cp_parser *);
1602 static tree cp_parser_new_placement
1603   (cp_parser *);
1604 static tree cp_parser_new_type_id
1605   (cp_parser *, tree *);
1606 static cp_declarator *cp_parser_new_declarator_opt
1607   (cp_parser *);
1608 static cp_declarator *cp_parser_direct_new_declarator
1609   (cp_parser *);
1610 static tree cp_parser_new_initializer
1611   (cp_parser *);
1612 static tree cp_parser_delete_expression
1613   (cp_parser *);
1614 static tree cp_parser_cast_expression
1615   (cp_parser *, bool, bool);
1616 static tree cp_parser_binary_expression
1617   (cp_parser *, bool, enum cp_parser_prec);
1618 static tree cp_parser_question_colon_clause
1619   (cp_parser *, tree);
1620 static tree cp_parser_assignment_expression
1621   (cp_parser *, bool);
1622 static enum tree_code cp_parser_assignment_operator_opt
1623   (cp_parser *);
1624 static tree cp_parser_expression
1625   (cp_parser *, bool);
1626 static tree cp_parser_constant_expression
1627   (cp_parser *, bool, bool *);
1628 static tree cp_parser_builtin_offsetof
1629   (cp_parser *);
1630
1631 /* Statements [gram.stmt.stmt]  */
1632
1633 static void cp_parser_statement
1634   (cp_parser *, tree, bool, bool *);
1635 static void cp_parser_label_for_labeled_statement
1636   (cp_parser *);
1637 static tree cp_parser_expression_statement
1638   (cp_parser *, tree);
1639 static tree cp_parser_compound_statement
1640   (cp_parser *, tree, bool);
1641 static void cp_parser_statement_seq_opt
1642   (cp_parser *, tree);
1643 static tree cp_parser_selection_statement
1644   (cp_parser *, bool *);
1645 static tree cp_parser_condition
1646   (cp_parser *);
1647 static tree cp_parser_iteration_statement
1648   (cp_parser *);
1649 static void cp_parser_for_init_statement
1650   (cp_parser *);
1651 static tree cp_parser_jump_statement
1652   (cp_parser *);
1653 static void cp_parser_declaration_statement
1654   (cp_parser *);
1655
1656 static tree cp_parser_implicitly_scoped_statement
1657   (cp_parser *, bool *);
1658 static void cp_parser_already_scoped_statement
1659   (cp_parser *);
1660
1661 /* Declarations [gram.dcl.dcl] */
1662
1663 static void cp_parser_declaration_seq_opt
1664   (cp_parser *);
1665 static void cp_parser_declaration
1666   (cp_parser *);
1667 static void cp_parser_block_declaration
1668   (cp_parser *, bool);
1669 static void cp_parser_simple_declaration
1670   (cp_parser *, bool);
1671 static void cp_parser_decl_specifier_seq
1672   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1673 static tree cp_parser_storage_class_specifier_opt
1674   (cp_parser *);
1675 static tree cp_parser_function_specifier_opt
1676   (cp_parser *, cp_decl_specifier_seq *);
1677 static tree cp_parser_type_specifier
1678   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1679    int *, bool *);
1680 static tree cp_parser_simple_type_specifier
1681   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1682 static tree cp_parser_type_name
1683   (cp_parser *);
1684 static tree cp_parser_nonclass_name 
1685   (cp_parser* parser);
1686 static tree cp_parser_elaborated_type_specifier
1687   (cp_parser *, bool, bool);
1688 static tree cp_parser_enum_specifier
1689   (cp_parser *);
1690 static void cp_parser_enumerator_list
1691   (cp_parser *, tree);
1692 static void cp_parser_enumerator_definition
1693   (cp_parser *, tree);
1694 static tree cp_parser_namespace_name
1695   (cp_parser *);
1696 static void cp_parser_namespace_definition
1697   (cp_parser *);
1698 static void cp_parser_namespace_body
1699   (cp_parser *);
1700 static tree cp_parser_qualified_namespace_specifier
1701   (cp_parser *);
1702 static void cp_parser_namespace_alias_definition
1703   (cp_parser *);
1704 static bool cp_parser_using_declaration
1705   (cp_parser *, bool);
1706 static void cp_parser_using_directive
1707   (cp_parser *);
1708 static void cp_parser_asm_definition
1709   (cp_parser *);
1710 static void cp_parser_linkage_specification
1711   (cp_parser *);
1712 static void cp_parser_static_assert
1713   (cp_parser *, bool);
1714 static tree cp_parser_decltype
1715   (cp_parser *);
1716
1717 /* Declarators [gram.dcl.decl] */
1718
1719 static tree cp_parser_init_declarator
1720   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1721 static cp_declarator *cp_parser_declarator
1722   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1723 static cp_declarator *cp_parser_direct_declarator
1724   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1725 static enum tree_code cp_parser_ptr_operator
1726   (cp_parser *, tree *, cp_cv_quals *);
1727 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1728   (cp_parser *);
1729 static tree cp_parser_declarator_id
1730   (cp_parser *, bool);
1731 static tree cp_parser_type_id
1732   (cp_parser *);
1733 static void cp_parser_type_specifier_seq
1734   (cp_parser *, bool, cp_decl_specifier_seq *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1736   (cp_parser *);
1737 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1738   (cp_parser *, bool *);
1739 static cp_parameter_declarator *cp_parser_parameter_declaration
1740   (cp_parser *, bool, bool *);
1741 static tree cp_parser_default_argument 
1742   (cp_parser *, bool);
1743 static void cp_parser_function_body
1744   (cp_parser *);
1745 static tree cp_parser_initializer
1746   (cp_parser *, bool *, bool *);
1747 static tree cp_parser_initializer_clause
1748   (cp_parser *, bool *);
1749 static tree cp_parser_braced_list
1750   (cp_parser*, bool*);
1751 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1752   (cp_parser *, bool *);
1753
1754 static bool cp_parser_ctor_initializer_opt_and_function_body
1755   (cp_parser *);
1756
1757 /* Classes [gram.class] */
1758
1759 static tree cp_parser_class_name
1760   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1761 static tree cp_parser_class_specifier
1762   (cp_parser *);
1763 static tree cp_parser_class_head
1764   (cp_parser *, bool *, tree *, tree *);
1765 static enum tag_types cp_parser_class_key
1766   (cp_parser *);
1767 static void cp_parser_member_specification_opt
1768   (cp_parser *);
1769 static void cp_parser_member_declaration
1770   (cp_parser *);
1771 static tree cp_parser_pure_specifier
1772   (cp_parser *);
1773 static tree cp_parser_constant_initializer
1774   (cp_parser *);
1775
1776 /* Derived classes [gram.class.derived] */
1777
1778 static tree cp_parser_base_clause
1779   (cp_parser *);
1780 static tree cp_parser_base_specifier
1781   (cp_parser *);
1782
1783 /* Special member functions [gram.special] */
1784
1785 static tree cp_parser_conversion_function_id
1786   (cp_parser *);
1787 static tree cp_parser_conversion_type_id
1788   (cp_parser *);
1789 static cp_declarator *cp_parser_conversion_declarator_opt
1790   (cp_parser *);
1791 static bool cp_parser_ctor_initializer_opt
1792   (cp_parser *);
1793 static void cp_parser_mem_initializer_list
1794   (cp_parser *);
1795 static tree cp_parser_mem_initializer
1796   (cp_parser *);
1797 static tree cp_parser_mem_initializer_id
1798   (cp_parser *);
1799
1800 /* Overloading [gram.over] */
1801
1802 static tree cp_parser_operator_function_id
1803   (cp_parser *);
1804 static tree cp_parser_operator
1805   (cp_parser *);
1806
1807 /* Templates [gram.temp] */
1808
1809 static void cp_parser_template_declaration
1810   (cp_parser *, bool);
1811 static tree cp_parser_template_parameter_list
1812   (cp_parser *);
1813 static tree cp_parser_template_parameter
1814   (cp_parser *, bool *, bool *);
1815 static tree cp_parser_type_parameter
1816   (cp_parser *, bool *);
1817 static tree cp_parser_template_id
1818   (cp_parser *, bool, bool, bool);
1819 static tree cp_parser_template_name
1820   (cp_parser *, bool, bool, bool, bool *);
1821 static tree cp_parser_template_argument_list
1822   (cp_parser *);
1823 static tree cp_parser_template_argument
1824   (cp_parser *);
1825 static void cp_parser_explicit_instantiation
1826   (cp_parser *);
1827 static void cp_parser_explicit_specialization
1828   (cp_parser *);
1829
1830 /* Exception handling [gram.exception] */
1831
1832 static tree cp_parser_try_block
1833   (cp_parser *);
1834 static bool cp_parser_function_try_block
1835   (cp_parser *);
1836 static void cp_parser_handler_seq
1837   (cp_parser *);
1838 static void cp_parser_handler
1839   (cp_parser *);
1840 static tree cp_parser_exception_declaration
1841   (cp_parser *);
1842 static tree cp_parser_throw_expression
1843   (cp_parser *);
1844 static tree cp_parser_exception_specification_opt
1845   (cp_parser *);
1846 static tree cp_parser_type_id_list
1847   (cp_parser *);
1848
1849 /* GNU Extensions */
1850
1851 static tree cp_parser_asm_specification_opt
1852   (cp_parser *);
1853 static tree cp_parser_asm_operand_list
1854   (cp_parser *);
1855 static tree cp_parser_asm_clobber_list
1856   (cp_parser *);
1857 static tree cp_parser_attributes_opt
1858   (cp_parser *);
1859 static tree cp_parser_attribute_list
1860   (cp_parser *);
1861 static bool cp_parser_extension_opt
1862   (cp_parser *, int *);
1863 static void cp_parser_label_declaration
1864   (cp_parser *);
1865
1866 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1867 static bool cp_parser_pragma
1868   (cp_parser *, enum pragma_context);
1869
1870 /* Objective-C++ Productions */
1871
1872 static tree cp_parser_objc_message_receiver
1873   (cp_parser *);
1874 static tree cp_parser_objc_message_args
1875   (cp_parser *);
1876 static tree cp_parser_objc_message_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_encode_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_defs_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_protocol_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_selector_expression
1885   (cp_parser *);
1886 static tree cp_parser_objc_expression
1887   (cp_parser *);
1888 static bool cp_parser_objc_selector_p
1889   (enum cpp_ttype);
1890 static tree cp_parser_objc_selector
1891   (cp_parser *);
1892 static tree cp_parser_objc_protocol_refs_opt
1893   (cp_parser *);
1894 static void cp_parser_objc_declaration
1895   (cp_parser *);
1896 static tree cp_parser_objc_statement
1897   (cp_parser *);
1898
1899 /* Utility Routines */
1900
1901 static tree cp_parser_lookup_name
1902   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1903 static tree cp_parser_lookup_name_simple
1904   (cp_parser *, tree, location_t);
1905 static tree cp_parser_maybe_treat_template_as_class
1906   (tree, bool);
1907 static bool cp_parser_check_declarator_template_parameters
1908   (cp_parser *, cp_declarator *, location_t);
1909 static bool cp_parser_check_template_parameters
1910   (cp_parser *, unsigned, location_t);
1911 static tree cp_parser_simple_cast_expression
1912   (cp_parser *);
1913 static tree cp_parser_global_scope_opt
1914   (cp_parser *, bool);
1915 static bool cp_parser_constructor_declarator_p
1916   (cp_parser *, bool);
1917 static tree cp_parser_function_definition_from_specifiers_and_declarator
1918   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1919 static tree cp_parser_function_definition_after_declarator
1920   (cp_parser *, bool);
1921 static void cp_parser_template_declaration_after_export
1922   (cp_parser *, bool);
1923 static void cp_parser_perform_template_parameter_access_checks
1924   (VEC (deferred_access_check,gc)*);
1925 static tree cp_parser_single_declaration
1926   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1927 static tree cp_parser_functional_cast
1928   (cp_parser *, tree);
1929 static tree cp_parser_save_member_function_body
1930   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1931 static tree cp_parser_enclosed_template_argument_list
1932   (cp_parser *);
1933 static void cp_parser_save_default_args
1934   (cp_parser *, tree);
1935 static void cp_parser_late_parsing_for_member
1936   (cp_parser *, tree);
1937 static void cp_parser_late_parsing_default_args
1938   (cp_parser *, tree);
1939 static tree cp_parser_sizeof_operand
1940   (cp_parser *, enum rid);
1941 static tree cp_parser_trait_expr
1942   (cp_parser *, enum rid);
1943 static bool cp_parser_declares_only_class_p
1944   (cp_parser *);
1945 static void cp_parser_set_storage_class
1946   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1947 static void cp_parser_set_decl_spec_type
1948   (cp_decl_specifier_seq *, tree, location_t, bool);
1949 static bool cp_parser_friend_p
1950   (const cp_decl_specifier_seq *);
1951 static cp_token *cp_parser_require
1952   (cp_parser *, enum cpp_ttype, const char *);
1953 static cp_token *cp_parser_require_keyword
1954   (cp_parser *, enum rid, const char *);
1955 static bool cp_parser_token_starts_function_definition_p
1956   (cp_token *);
1957 static bool cp_parser_next_token_starts_class_definition_p
1958   (cp_parser *);
1959 static bool cp_parser_next_token_ends_template_argument_p
1960   (cp_parser *);
1961 static bool cp_parser_nth_token_starts_template_argument_list_p
1962   (cp_parser *, size_t);
1963 static enum tag_types cp_parser_token_is_class_key
1964   (cp_token *);
1965 static void cp_parser_check_class_key
1966   (enum tag_types, tree type);
1967 static void cp_parser_check_access_in_redeclaration
1968   (tree type, location_t location);
1969 static bool cp_parser_optional_template_keyword
1970   (cp_parser *);
1971 static void cp_parser_pre_parsed_nested_name_specifier
1972   (cp_parser *);
1973 static bool cp_parser_cache_group
1974   (cp_parser *, enum cpp_ttype, unsigned);
1975 static void cp_parser_parse_tentatively
1976   (cp_parser *);
1977 static void cp_parser_commit_to_tentative_parse
1978   (cp_parser *);
1979 static void cp_parser_abort_tentative_parse
1980   (cp_parser *);
1981 static bool cp_parser_parse_definitely
1982   (cp_parser *);
1983 static inline bool cp_parser_parsing_tentatively
1984   (cp_parser *);
1985 static bool cp_parser_uncommitted_to_tentative_parse_p
1986   (cp_parser *);
1987 static void cp_parser_error
1988   (cp_parser *, const char *);
1989 static void cp_parser_name_lookup_error
1990   (cp_parser *, tree, tree, const char *, location_t);
1991 static bool cp_parser_simulate_error
1992   (cp_parser *);
1993 static bool cp_parser_check_type_definition
1994   (cp_parser *);
1995 static void cp_parser_check_for_definition_in_return_type
1996   (cp_declarator *, tree, location_t type_location);
1997 static void cp_parser_check_for_invalid_template_id
1998   (cp_parser *, tree, location_t location);
1999 static bool cp_parser_non_integral_constant_expression
2000   (cp_parser *, const char *);
2001 static void cp_parser_diagnose_invalid_type_name
2002   (cp_parser *, tree, tree, location_t);
2003 static bool cp_parser_parse_and_diagnose_invalid_type_name
2004   (cp_parser *);
2005 static int cp_parser_skip_to_closing_parenthesis
2006   (cp_parser *, bool, bool, bool);
2007 static void cp_parser_skip_to_end_of_statement
2008   (cp_parser *);
2009 static void cp_parser_consume_semicolon_at_end_of_statement
2010   (cp_parser *);
2011 static void cp_parser_skip_to_end_of_block_or_statement
2012   (cp_parser *);
2013 static bool cp_parser_skip_to_closing_brace
2014   (cp_parser *);
2015 static void cp_parser_skip_to_end_of_template_parameter_list
2016   (cp_parser *);
2017 static void cp_parser_skip_to_pragma_eol
2018   (cp_parser*, cp_token *);
2019 static bool cp_parser_error_occurred
2020   (cp_parser *);
2021 static bool cp_parser_allow_gnu_extensions_p
2022   (cp_parser *);
2023 static bool cp_parser_is_string_literal
2024   (cp_token *);
2025 static bool cp_parser_is_keyword
2026   (cp_token *, enum rid);
2027 static tree cp_parser_make_typename_type
2028   (cp_parser *, tree, tree, location_t location);
2029 static cp_declarator * cp_parser_make_indirect_declarator
2030   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2031
2032 /* Returns nonzero if we are parsing tentatively.  */
2033
2034 static inline bool
2035 cp_parser_parsing_tentatively (cp_parser* parser)
2036 {
2037   return parser->context->next != NULL;
2038 }
2039
2040 /* Returns nonzero if TOKEN is a string literal.  */
2041
2042 static bool
2043 cp_parser_is_string_literal (cp_token* token)
2044 {
2045   return (token->type == CPP_STRING ||
2046           token->type == CPP_STRING16 ||
2047           token->type == CPP_STRING32 ||
2048           token->type == CPP_WSTRING);
2049 }
2050
2051 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2052
2053 static bool
2054 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2055 {
2056   return token->keyword == keyword;
2057 }
2058
2059 /* If not parsing tentatively, issue a diagnostic of the form
2060       FILE:LINE: MESSAGE before TOKEN
2061    where TOKEN is the next token in the input stream.  MESSAGE
2062    (specified by the caller) is usually of the form "expected
2063    OTHER-TOKEN".  */
2064
2065 static void
2066 cp_parser_error (cp_parser* parser, const char* message)
2067 {
2068   if (!cp_parser_simulate_error (parser))
2069     {
2070       cp_token *token = cp_lexer_peek_token (parser->lexer);
2071       /* This diagnostic makes more sense if it is tagged to the line
2072          of the token we just peeked at.  */
2073       cp_lexer_set_source_position_from_token (token);
2074
2075       if (token->type == CPP_PRAGMA)
2076         {
2077           error ("%H%<#pragma%> is not allowed here", &token->location);
2078           cp_parser_skip_to_pragma_eol (parser, token);
2079           return;
2080         }
2081
2082       c_parse_error (message,
2083                      /* Because c_parser_error does not understand
2084                         CPP_KEYWORD, keywords are treated like
2085                         identifiers.  */
2086                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2087                      token->u.value);
2088     }
2089 }
2090
2091 /* Issue an error about name-lookup failing.  NAME is the
2092    IDENTIFIER_NODE DECL is the result of
2093    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2094    the thing that we hoped to find.  */
2095
2096 static void
2097 cp_parser_name_lookup_error (cp_parser* parser,
2098                              tree name,
2099                              tree decl,
2100                              const char* desired,
2101                              location_t location)
2102 {
2103   /* If name lookup completely failed, tell the user that NAME was not
2104      declared.  */
2105   if (decl == error_mark_node)
2106     {
2107       if (parser->scope && parser->scope != global_namespace)
2108         error ("%H%<%E::%E%> has not been declared",
2109                &location, parser->scope, name);
2110       else if (parser->scope == global_namespace)
2111         error ("%H%<::%E%> has not been declared", &location, name);
2112       else if (parser->object_scope
2113                && !CLASS_TYPE_P (parser->object_scope))
2114         error ("%Hrequest for member %qE in non-class type %qT",
2115                &location, name, parser->object_scope);
2116       else if (parser->object_scope)
2117         error ("%H%<%T::%E%> has not been declared",
2118                &location, parser->object_scope, name);
2119       else
2120         error ("%H%qE has not been declared", &location, name);
2121     }
2122   else if (parser->scope && parser->scope != global_namespace)
2123     error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2124   else if (parser->scope == global_namespace)
2125     error ("%H%<::%E%> %s", &location, name, desired);
2126   else
2127     error ("%H%qE %s", &location, name, desired);
2128 }
2129
2130 /* If we are parsing tentatively, remember that an error has occurred
2131    during this tentative parse.  Returns true if the error was
2132    simulated; false if a message should be issued by the caller.  */
2133
2134 static bool
2135 cp_parser_simulate_error (cp_parser* parser)
2136 {
2137   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2138     {
2139       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2140       return true;
2141     }
2142   return false;
2143 }
2144
2145 /* Check for repeated decl-specifiers.  */
2146
2147 static void
2148 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2149                            location_t location)
2150 {
2151   cp_decl_spec ds;
2152
2153   for (ds = ds_first; ds != ds_last; ++ds)
2154     {
2155       unsigned count = decl_specs->specs[(int)ds];
2156       if (count < 2)
2157         continue;
2158       /* The "long" specifier is a special case because of "long long".  */
2159       if (ds == ds_long)
2160         {
2161           if (count > 2)
2162             error ("%H%<long long long%> is too long for GCC", &location);
2163           else if (pedantic && !in_system_header && warn_long_long
2164                    && cxx_dialect == cxx98)
2165             pedwarn (OPT_Wlong_long, 
2166                      "%HISO C++ 1998 does not support %<long long%>",
2167                      &location);
2168         }
2169       else if (count > 1)
2170         {
2171           static const char *const decl_spec_names[] = {
2172             "signed",
2173             "unsigned",
2174             "short",
2175             "long",
2176             "const",
2177             "volatile",
2178             "restrict",
2179             "inline",
2180             "virtual",
2181             "explicit",
2182             "friend",
2183             "typedef",
2184             "__complex",
2185             "__thread"
2186           };
2187           error ("%Hduplicate %qs", &location, decl_spec_names[(int)ds]);
2188         }
2189     }
2190 }
2191
2192 /* This function is called when a type is defined.  If type
2193    definitions are forbidden at this point, an error message is
2194    issued.  */
2195
2196 static bool
2197 cp_parser_check_type_definition (cp_parser* parser)
2198 {
2199   /* If types are forbidden here, issue a message.  */
2200   if (parser->type_definition_forbidden_message)
2201     {
2202       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2203          in the message need to be interpreted.  */
2204       error (parser->type_definition_forbidden_message);
2205       return false;
2206     }
2207   return true;
2208 }
2209
2210 /* This function is called when the DECLARATOR is processed.  The TYPE
2211    was a type defined in the decl-specifiers.  If it is invalid to
2212    define a type in the decl-specifiers for DECLARATOR, an error is
2213    issued. TYPE_LOCATION is the location of TYPE and is used
2214    for error reporting.  */
2215
2216 static void
2217 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2218                                                tree type, location_t type_location)
2219 {
2220   /* [dcl.fct] forbids type definitions in return types.
2221      Unfortunately, it's not easy to know whether or not we are
2222      processing a return type until after the fact.  */
2223   while (declarator
2224          && (declarator->kind == cdk_pointer
2225              || declarator->kind == cdk_reference
2226              || declarator->kind == cdk_ptrmem))
2227     declarator = declarator->declarator;
2228   if (declarator
2229       && declarator->kind == cdk_function)
2230     {
2231       error ("%Hnew types may not be defined in a return type", &type_location);
2232       inform (type_location, 
2233               "(perhaps a semicolon is missing after the definition of %qT)",
2234               type);
2235     }
2236 }
2237
2238 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2239    "<" in any valid C++ program.  If the next token is indeed "<",
2240    issue a message warning the user about what appears to be an
2241    invalid attempt to form a template-id. LOCATION is the location
2242    of the type-specifier (TYPE) */
2243
2244 static void
2245 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2246                                          tree type, location_t location)
2247 {
2248   cp_token_position start = 0;
2249
2250   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2251     {
2252       if (TYPE_P (type))
2253         error ("%H%qT is not a template", &location, type);
2254       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2255         error ("%H%qE is not a template", &location, type);
2256       else
2257         error ("%Hinvalid template-id", &location);
2258       /* Remember the location of the invalid "<".  */
2259       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2260         start = cp_lexer_token_position (parser->lexer, true);
2261       /* Consume the "<".  */
2262       cp_lexer_consume_token (parser->lexer);
2263       /* Parse the template arguments.  */
2264       cp_parser_enclosed_template_argument_list (parser);
2265       /* Permanently remove the invalid template arguments so that
2266          this error message is not issued again.  */
2267       if (start)
2268         cp_lexer_purge_tokens_after (parser->lexer, start);
2269     }
2270 }
2271
2272 /* If parsing an integral constant-expression, issue an error message
2273    about the fact that THING appeared and return true.  Otherwise,
2274    return false.  In either case, set
2275    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2276
2277 static bool
2278 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2279                                             const char *thing)
2280 {
2281   parser->non_integral_constant_expression_p = true;
2282   if (parser->integral_constant_expression_p)
2283     {
2284       if (!parser->allow_non_integral_constant_expression_p)
2285         {
2286           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2287              in the message need to be interpreted.  */
2288           char *message = concat (thing,
2289                                   " cannot appear in a constant-expression",
2290                                   NULL);
2291           error (message);
2292           free (message);
2293           return true;
2294         }
2295     }
2296   return false;
2297 }
2298
2299 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2300    qualifying scope (or NULL, if none) for ID.  This function commits
2301    to the current active tentative parse, if any.  (Otherwise, the
2302    problematic construct might be encountered again later, resulting
2303    in duplicate error messages.) LOCATION is the location of ID.  */
2304
2305 static void
2306 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2307                                       tree scope, tree id,
2308                                       location_t location)
2309 {
2310   tree decl, old_scope;
2311   /* Try to lookup the identifier.  */
2312   old_scope = parser->scope;
2313   parser->scope = scope;
2314   decl = cp_parser_lookup_name_simple (parser, id, location);
2315   parser->scope = old_scope;
2316   /* If the lookup found a template-name, it means that the user forgot
2317   to specify an argument list. Emit a useful error message.  */
2318   if (TREE_CODE (decl) == TEMPLATE_DECL)
2319     error ("%Hinvalid use of template-name %qE without an argument list",
2320            &location, decl);
2321   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2322     error ("%Hinvalid use of destructor %qD as a type", &location, id);
2323   else if (TREE_CODE (decl) == TYPE_DECL)
2324     /* Something like 'unsigned A a;'  */
2325     error ("%Hinvalid combination of multiple type-specifiers",
2326            &location);
2327   else if (!parser->scope)
2328     {
2329       /* Issue an error message.  */
2330       error ("%H%qE does not name a type", &location, id);
2331       /* If we're in a template class, it's possible that the user was
2332          referring to a type from a base class.  For example:
2333
2334            template <typename T> struct A { typedef T X; };
2335            template <typename T> struct B : public A<T> { X x; };
2336
2337          The user should have said "typename A<T>::X".  */
2338       if (processing_template_decl && current_class_type
2339           && TYPE_BINFO (current_class_type))
2340         {
2341           tree b;
2342
2343           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2344                b;
2345                b = TREE_CHAIN (b))
2346             {
2347               tree base_type = BINFO_TYPE (b);
2348               if (CLASS_TYPE_P (base_type)
2349                   && dependent_type_p (base_type))
2350                 {
2351                   tree field;
2352                   /* Go from a particular instantiation of the
2353                      template (which will have an empty TYPE_FIELDs),
2354                      to the main version.  */
2355                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2356                   for (field = TYPE_FIELDS (base_type);
2357                        field;
2358                        field = TREE_CHAIN (field))
2359                     if (TREE_CODE (field) == TYPE_DECL
2360                         && DECL_NAME (field) == id)
2361                       {
2362                         inform (location, 
2363                                 "(perhaps %<typename %T::%E%> was intended)",
2364                                 BINFO_TYPE (b), id);
2365                         break;
2366                       }
2367                   if (field)
2368                     break;
2369                 }
2370             }
2371         }
2372     }
2373   /* Here we diagnose qualified-ids where the scope is actually correct,
2374      but the identifier does not resolve to a valid type name.  */
2375   else if (parser->scope != error_mark_node)
2376     {
2377       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2378         error ("%H%qE in namespace %qE does not name a type",
2379                &location, id, parser->scope);
2380       else if (TYPE_P (parser->scope))
2381         error ("%H%qE in class %qT does not name a type",
2382                &location, id, parser->scope);
2383       else
2384         gcc_unreachable ();
2385     }
2386   cp_parser_commit_to_tentative_parse (parser);
2387 }
2388
2389 /* Check for a common situation where a type-name should be present,
2390    but is not, and issue a sensible error message.  Returns true if an
2391    invalid type-name was detected.
2392
2393    The situation handled by this function are variable declarations of the
2394    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2395    Usually, `ID' should name a type, but if we got here it means that it
2396    does not. We try to emit the best possible error message depending on
2397    how exactly the id-expression looks like.  */
2398
2399 static bool
2400 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2401 {
2402   tree id;
2403   cp_token *token = cp_lexer_peek_token (parser->lexer);
2404
2405   cp_parser_parse_tentatively (parser);
2406   id = cp_parser_id_expression (parser,
2407                                 /*template_keyword_p=*/false,
2408                                 /*check_dependency_p=*/true,
2409                                 /*template_p=*/NULL,
2410                                 /*declarator_p=*/true,
2411                                 /*optional_p=*/false);
2412   /* After the id-expression, there should be a plain identifier,
2413      otherwise this is not a simple variable declaration. Also, if
2414      the scope is dependent, we cannot do much.  */
2415   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2416       || (parser->scope && TYPE_P (parser->scope)
2417           && dependent_type_p (parser->scope))
2418       || TREE_CODE (id) == TYPE_DECL)
2419     {
2420       cp_parser_abort_tentative_parse (parser);
2421       return false;
2422     }
2423   if (!cp_parser_parse_definitely (parser))
2424     return false;
2425
2426   /* Emit a diagnostic for the invalid type.  */
2427   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2428                                         id, token->location);
2429   /* Skip to the end of the declaration; there's no point in
2430      trying to process it.  */
2431   cp_parser_skip_to_end_of_block_or_statement (parser);
2432   return true;
2433 }
2434
2435 /* Consume tokens up to, and including, the next non-nested closing `)'.
2436    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2437    are doing error recovery. Returns -1 if OR_COMMA is true and we
2438    found an unnested comma.  */
2439
2440 static int
2441 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2442                                        bool recovering,
2443                                        bool or_comma,
2444                                        bool consume_paren)
2445 {
2446   unsigned paren_depth = 0;
2447   unsigned brace_depth = 0;
2448
2449   if (recovering && !or_comma
2450       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2451     return 0;
2452
2453   while (true)
2454     {
2455       cp_token * token = cp_lexer_peek_token (parser->lexer);
2456
2457       switch (token->type)
2458         {
2459         case CPP_EOF:
2460         case CPP_PRAGMA_EOL:
2461           /* If we've run out of tokens, then there is no closing `)'.  */
2462           return 0;
2463
2464         case CPP_SEMICOLON:
2465           /* This matches the processing in skip_to_end_of_statement.  */
2466           if (!brace_depth)
2467             return 0;
2468           break;
2469
2470         case CPP_OPEN_BRACE:
2471           ++brace_depth;
2472           break;
2473         case CPP_CLOSE_BRACE:
2474           if (!brace_depth--)
2475             return 0;
2476           break;
2477
2478         case CPP_COMMA:
2479           if (recovering && or_comma && !brace_depth && !paren_depth)
2480             return -1;
2481           break;
2482
2483         case CPP_OPEN_PAREN:
2484           if (!brace_depth)
2485             ++paren_depth;
2486           break;
2487
2488         case CPP_CLOSE_PAREN:
2489           if (!brace_depth && !paren_depth--)
2490             {
2491               if (consume_paren)
2492                 cp_lexer_consume_token (parser->lexer);
2493               return 1;
2494             }
2495           break;
2496
2497         default:
2498           break;
2499         }
2500
2501       /* Consume the token.  */
2502       cp_lexer_consume_token (parser->lexer);
2503     }
2504 }
2505
2506 /* Consume tokens until we reach the end of the current statement.
2507    Normally, that will be just before consuming a `;'.  However, if a
2508    non-nested `}' comes first, then we stop before consuming that.  */
2509
2510 static void
2511 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2512 {
2513   unsigned nesting_depth = 0;
2514
2515   while (true)
2516     {
2517       cp_token *token = cp_lexer_peek_token (parser->lexer);
2518
2519       switch (token->type)
2520         {
2521         case CPP_EOF:
2522         case CPP_PRAGMA_EOL:
2523           /* If we've run out of tokens, stop.  */
2524           return;
2525
2526         case CPP_SEMICOLON:
2527           /* If the next token is a `;', we have reached the end of the
2528              statement.  */
2529           if (!nesting_depth)
2530             return;
2531           break;
2532
2533         case CPP_CLOSE_BRACE:
2534           /* If this is a non-nested '}', stop before consuming it.
2535              That way, when confronted with something like:
2536
2537                { 3 + }
2538
2539              we stop before consuming the closing '}', even though we
2540              have not yet reached a `;'.  */
2541           if (nesting_depth == 0)
2542             return;
2543
2544           /* If it is the closing '}' for a block that we have
2545              scanned, stop -- but only after consuming the token.
2546              That way given:
2547
2548                 void f g () { ... }
2549                 typedef int I;
2550
2551              we will stop after the body of the erroneously declared
2552              function, but before consuming the following `typedef'
2553              declaration.  */
2554           if (--nesting_depth == 0)
2555             {
2556               cp_lexer_consume_token (parser->lexer);
2557               return;
2558             }
2559
2560         case CPP_OPEN_BRACE:
2561           ++nesting_depth;
2562           break;
2563
2564         default:
2565           break;
2566         }
2567
2568       /* Consume the token.  */
2569       cp_lexer_consume_token (parser->lexer);
2570     }
2571 }
2572
2573 /* This function is called at the end of a statement or declaration.
2574    If the next token is a semicolon, it is consumed; otherwise, error
2575    recovery is attempted.  */
2576
2577 static void
2578 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2579 {
2580   /* Look for the trailing `;'.  */
2581   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2582     {
2583       /* If there is additional (erroneous) input, skip to the end of
2584          the statement.  */
2585       cp_parser_skip_to_end_of_statement (parser);
2586       /* If the next token is now a `;', consume it.  */
2587       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2588         cp_lexer_consume_token (parser->lexer);
2589     }
2590 }
2591
2592 /* Skip tokens until we have consumed an entire block, or until we
2593    have consumed a non-nested `;'.  */
2594
2595 static void
2596 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2597 {
2598   int nesting_depth = 0;
2599
2600   while (nesting_depth >= 0)
2601     {
2602       cp_token *token = cp_lexer_peek_token (parser->lexer);
2603
2604       switch (token->type)
2605         {
2606         case CPP_EOF:
2607         case CPP_PRAGMA_EOL:
2608           /* If we've run out of tokens, stop.  */
2609           return;
2610
2611         case CPP_SEMICOLON:
2612           /* Stop if this is an unnested ';'. */
2613           if (!nesting_depth)
2614             nesting_depth = -1;
2615           break;
2616
2617         case CPP_CLOSE_BRACE:
2618           /* Stop if this is an unnested '}', or closes the outermost
2619              nesting level.  */
2620           nesting_depth--;
2621           if (!nesting_depth)
2622             nesting_depth = -1;
2623           break;
2624
2625         case CPP_OPEN_BRACE:
2626           /* Nest. */
2627           nesting_depth++;
2628           break;
2629
2630         default:
2631           break;
2632         }
2633
2634       /* Consume the token.  */
2635       cp_lexer_consume_token (parser->lexer);
2636     }
2637 }
2638
2639 /* Skip tokens until a non-nested closing curly brace is the next
2640    token, or there are no more tokens. Return true in the first case,
2641    false otherwise.  */
2642
2643 static bool
2644 cp_parser_skip_to_closing_brace (cp_parser *parser)
2645 {
2646   unsigned nesting_depth = 0;
2647
2648   while (true)
2649     {
2650       cp_token *token = cp_lexer_peek_token (parser->lexer);
2651
2652       switch (token->type)
2653         {
2654         case CPP_EOF:
2655         case CPP_PRAGMA_EOL:
2656           /* If we've run out of tokens, stop.  */
2657           return false;
2658
2659         case CPP_CLOSE_BRACE:
2660           /* If the next token is a non-nested `}', then we have reached
2661              the end of the current block.  */
2662           if (nesting_depth-- == 0)
2663             return true;
2664           break;
2665
2666         case CPP_OPEN_BRACE:
2667           /* If it the next token is a `{', then we are entering a new
2668              block.  Consume the entire block.  */
2669           ++nesting_depth;
2670           break;
2671
2672         default:
2673           break;
2674         }
2675
2676       /* Consume the token.  */
2677       cp_lexer_consume_token (parser->lexer);
2678     }
2679 }
2680
2681 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2682    parameter is the PRAGMA token, allowing us to purge the entire pragma
2683    sequence.  */
2684
2685 static void
2686 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2687 {
2688   cp_token *token;
2689
2690   parser->lexer->in_pragma = false;
2691
2692   do
2693     token = cp_lexer_consume_token (parser->lexer);
2694   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2695
2696   /* Ensure that the pragma is not parsed again.  */
2697   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2698 }
2699
2700 /* Require pragma end of line, resyncing with it as necessary.  The
2701    arguments are as for cp_parser_skip_to_pragma_eol.  */
2702
2703 static void
2704 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2705 {
2706   parser->lexer->in_pragma = false;
2707   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2708     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2709 }
2710
2711 /* This is a simple wrapper around make_typename_type. When the id is
2712    an unresolved identifier node, we can provide a superior diagnostic
2713    using cp_parser_diagnose_invalid_type_name.  */
2714
2715 static tree
2716 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2717                               tree id, location_t id_location)
2718 {
2719   tree result;
2720   if (TREE_CODE (id) == IDENTIFIER_NODE)
2721     {
2722       result = make_typename_type (scope, id, typename_type,
2723                                    /*complain=*/tf_none);
2724       if (result == error_mark_node)
2725         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2726       return result;
2727     }
2728   return make_typename_type (scope, id, typename_type, tf_error);
2729 }
2730
2731 /* This is a wrapper around the
2732    make_{pointer,ptrmem,reference}_declarator functions that decides
2733    which one to call based on the CODE and CLASS_TYPE arguments. The
2734    CODE argument should be one of the values returned by
2735    cp_parser_ptr_operator. */
2736 static cp_declarator *
2737 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2738                                     cp_cv_quals cv_qualifiers,
2739                                     cp_declarator *target)
2740 {
2741   if (code == ERROR_MARK)
2742     return cp_error_declarator;
2743
2744   if (code == INDIRECT_REF)
2745     if (class_type == NULL_TREE)
2746       return make_pointer_declarator (cv_qualifiers, target);
2747     else
2748       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2749   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2750     return make_reference_declarator (cv_qualifiers, target, false);
2751   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2752     return make_reference_declarator (cv_qualifiers, target, true);
2753   gcc_unreachable ();
2754 }
2755
2756 /* Create a new C++ parser.  */
2757
2758 static cp_parser *
2759 cp_parser_new (void)
2760 {
2761   cp_parser *parser;
2762   cp_lexer *lexer;
2763   unsigned i;
2764
2765   /* cp_lexer_new_main is called before calling ggc_alloc because
2766      cp_lexer_new_main might load a PCH file.  */
2767   lexer = cp_lexer_new_main ();
2768
2769   /* Initialize the binops_by_token so that we can get the tree
2770      directly from the token.  */
2771   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2772     binops_by_token[binops[i].token_type] = binops[i];
2773
2774   parser = GGC_CNEW (cp_parser);
2775   parser->lexer = lexer;
2776   parser->context = cp_parser_context_new (NULL);
2777
2778   /* For now, we always accept GNU extensions.  */
2779   parser->allow_gnu_extensions_p = 1;
2780
2781   /* The `>' token is a greater-than operator, not the end of a
2782      template-id.  */
2783   parser->greater_than_is_operator_p = true;
2784
2785   parser->default_arg_ok_p = true;
2786
2787   /* We are not parsing a constant-expression.  */
2788   parser->integral_constant_expression_p = false;
2789   parser->allow_non_integral_constant_expression_p = false;
2790   parser->non_integral_constant_expression_p = false;
2791
2792   /* Local variable names are not forbidden.  */
2793   parser->local_variables_forbidden_p = false;
2794
2795   /* We are not processing an `extern "C"' declaration.  */
2796   parser->in_unbraced_linkage_specification_p = false;
2797
2798   /* We are not processing a declarator.  */
2799   parser->in_declarator_p = false;
2800
2801   /* We are not processing a template-argument-list.  */
2802   parser->in_template_argument_list_p = false;
2803
2804   /* We are not in an iteration statement.  */
2805   parser->in_statement = 0;
2806
2807   /* We are not in a switch statement.  */
2808   parser->in_switch_statement_p = false;
2809
2810   /* We are not parsing a type-id inside an expression.  */
2811   parser->in_type_id_in_expr_p = false;
2812
2813   /* Declarations aren't implicitly extern "C".  */
2814   parser->implicit_extern_c = false;
2815
2816   /* String literals should be translated to the execution character set.  */
2817   parser->translate_strings_p = true;
2818
2819   /* We are not parsing a function body.  */
2820   parser->in_function_body = false;
2821
2822   /* The unparsed function queue is empty.  */
2823   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2824
2825   /* There are no classes being defined.  */
2826   parser->num_classes_being_defined = 0;
2827
2828   /* No template parameters apply.  */
2829   parser->num_template_parameter_lists = 0;
2830
2831   return parser;
2832 }
2833
2834 /* Create a cp_lexer structure which will emit the tokens in CACHE
2835    and push it onto the parser's lexer stack.  This is used for delayed
2836    parsing of in-class method bodies and default arguments, and should
2837    not be confused with tentative parsing.  */
2838 static void
2839 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2840 {
2841   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2842   lexer->next = parser->lexer;
2843   parser->lexer = lexer;
2844
2845   /* Move the current source position to that of the first token in the
2846      new lexer.  */
2847   cp_lexer_set_source_position_from_token (lexer->next_token);
2848 }
2849
2850 /* Pop the top lexer off the parser stack.  This is never used for the
2851    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2852 static void
2853 cp_parser_pop_lexer (cp_parser *parser)
2854 {
2855   cp_lexer *lexer = parser->lexer;
2856   parser->lexer = lexer->next;
2857   cp_lexer_destroy (lexer);
2858
2859   /* Put the current source position back where it was before this
2860      lexer was pushed.  */
2861   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2862 }
2863
2864 /* Lexical conventions [gram.lex]  */
2865
2866 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2867    identifier.  */
2868
2869 static tree
2870 cp_parser_identifier (cp_parser* parser)
2871 {
2872   cp_token *token;
2873
2874   /* Look for the identifier.  */
2875   token = cp_parser_require (parser, CPP_NAME, "identifier");
2876   /* Return the value.  */
2877   return token ? token->u.value : error_mark_node;
2878 }
2879
2880 /* Parse a sequence of adjacent string constants.  Returns a
2881    TREE_STRING representing the combined, nul-terminated string
2882    constant.  If TRANSLATE is true, translate the string to the
2883    execution character set.  If WIDE_OK is true, a wide string is
2884    invalid here.
2885
2886    C++98 [lex.string] says that if a narrow string literal token is
2887    adjacent to a wide string literal token, the behavior is undefined.
2888    However, C99 6.4.5p4 says that this results in a wide string literal.
2889    We follow C99 here, for consistency with the C front end.
2890
2891    This code is largely lifted from lex_string() in c-lex.c.
2892
2893    FUTURE: ObjC++ will need to handle @-strings here.  */
2894 static tree
2895 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2896 {
2897   tree value;
2898   size_t count;
2899   struct obstack str_ob;
2900   cpp_string str, istr, *strs;
2901   cp_token *tok;
2902   enum cpp_ttype type;
2903
2904   tok = cp_lexer_peek_token (parser->lexer);
2905   if (!cp_parser_is_string_literal (tok))
2906     {
2907       cp_parser_error (parser, "expected string-literal");
2908       return error_mark_node;
2909     }
2910
2911   type = tok->type;
2912
2913   /* Try to avoid the overhead of creating and destroying an obstack
2914      for the common case of just one string.  */
2915   if (!cp_parser_is_string_literal
2916       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2917     {
2918       cp_lexer_consume_token (parser->lexer);
2919
2920       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2921       str.len = TREE_STRING_LENGTH (tok->u.value);
2922       count = 1;
2923
2924       strs = &str;
2925     }
2926   else
2927     {
2928       gcc_obstack_init (&str_ob);
2929       count = 0;
2930
2931       do
2932         {
2933           cp_lexer_consume_token (parser->lexer);
2934           count++;
2935           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2936           str.len = TREE_STRING_LENGTH (tok->u.value);
2937
2938           if (type != tok->type)
2939             {
2940               if (type == CPP_STRING)
2941                 type = tok->type;
2942               else if (tok->type != CPP_STRING)
2943                 error ("%Hunsupported non-standard concatenation "
2944                        "of string literals", &tok->location);
2945             }
2946
2947           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2948
2949           tok = cp_lexer_peek_token (parser->lexer);
2950         }
2951       while (cp_parser_is_string_literal (tok));
2952
2953       strs = (cpp_string *) obstack_finish (&str_ob);
2954     }
2955
2956   if (type != CPP_STRING && !wide_ok)
2957     {
2958       cp_parser_error (parser, "a wide string is invalid in this context");
2959       type = CPP_STRING;
2960     }
2961
2962   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2963       (parse_in, strs, count, &istr, type))
2964     {
2965       value = build_string (istr.len, (const char *)istr.text);
2966       free (CONST_CAST (unsigned char *, istr.text));
2967
2968       switch (type)
2969         {
2970         default:
2971         case CPP_STRING:
2972           TREE_TYPE (value) = char_array_type_node;
2973           break;
2974         case CPP_STRING16:
2975           TREE_TYPE (value) = char16_array_type_node;
2976           break;
2977         case CPP_STRING32:
2978           TREE_TYPE (value) = char32_array_type_node;
2979           break;
2980         case CPP_WSTRING:
2981           TREE_TYPE (value) = wchar_array_type_node;
2982           break;
2983         }
2984
2985       value = fix_string_type (value);
2986     }
2987   else
2988     /* cpp_interpret_string has issued an error.  */
2989     value = error_mark_node;
2990
2991   if (count > 1)
2992     obstack_free (&str_ob, 0);
2993
2994   return value;
2995 }
2996
2997
2998 /* Basic concepts [gram.basic]  */
2999
3000 /* Parse a translation-unit.
3001
3002    translation-unit:
3003      declaration-seq [opt]
3004
3005    Returns TRUE if all went well.  */
3006
3007 static bool
3008 cp_parser_translation_unit (cp_parser* parser)
3009 {
3010   /* The address of the first non-permanent object on the declarator
3011      obstack.  */
3012   static void *declarator_obstack_base;
3013
3014   bool success;
3015
3016   /* Create the declarator obstack, if necessary.  */
3017   if (!cp_error_declarator)
3018     {
3019       gcc_obstack_init (&declarator_obstack);
3020       /* Create the error declarator.  */
3021       cp_error_declarator = make_declarator (cdk_error);
3022       /* Create the empty parameter list.  */
3023       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3024       /* Remember where the base of the declarator obstack lies.  */
3025       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3026     }
3027
3028   cp_parser_declaration_seq_opt (parser);
3029
3030   /* If there are no tokens left then all went well.  */
3031   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3032     {
3033       /* Get rid of the token array; we don't need it any more.  */
3034       cp_lexer_destroy (parser->lexer);
3035       parser->lexer = NULL;
3036
3037       /* This file might have been a context that's implicitly extern
3038          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3039       if (parser->implicit_extern_c)
3040         {
3041           pop_lang_context ();
3042           parser->implicit_extern_c = false;
3043         }
3044
3045       /* Finish up.  */
3046       finish_translation_unit ();
3047
3048       success = true;
3049     }
3050   else
3051     {
3052       cp_parser_error (parser, "expected declaration");
3053       success = false;
3054     }
3055
3056   /* Make sure the declarator obstack was fully cleaned up.  */
3057   gcc_assert (obstack_next_free (&declarator_obstack)
3058               == declarator_obstack_base);
3059
3060   /* All went well.  */
3061   return success;
3062 }
3063
3064 /* Expressions [gram.expr] */
3065
3066 /* Parse a primary-expression.
3067
3068    primary-expression:
3069      literal
3070      this
3071      ( expression )
3072      id-expression
3073
3074    GNU Extensions:
3075
3076    primary-expression:
3077      ( compound-statement )
3078      __builtin_va_arg ( assignment-expression , type-id )
3079      __builtin_offsetof ( type-id , offsetof-expression )
3080
3081    C++ Extensions:
3082      __has_nothrow_assign ( type-id )   
3083      __has_nothrow_constructor ( type-id )
3084      __has_nothrow_copy ( type-id )
3085      __has_trivial_assign ( type-id )   
3086      __has_trivial_constructor ( type-id )
3087      __has_trivial_copy ( type-id )
3088      __has_trivial_destructor ( type-id )
3089      __has_virtual_destructor ( type-id )     
3090      __is_abstract ( type-id )
3091      __is_base_of ( type-id , type-id )
3092      __is_class ( type-id )
3093      __is_convertible_to ( type-id , type-id )     
3094      __is_empty ( type-id )
3095      __is_enum ( type-id )
3096      __is_pod ( type-id )
3097      __is_polymorphic ( type-id )
3098      __is_union ( type-id )
3099
3100    Objective-C++ Extension:
3101
3102    primary-expression:
3103      objc-expression
3104
3105    literal:
3106      __null
3107
3108    ADDRESS_P is true iff this expression was immediately preceded by
3109    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3110    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3111    true iff this expression is a template argument.
3112
3113    Returns a representation of the expression.  Upon return, *IDK
3114    indicates what kind of id-expression (if any) was present.  */
3115
3116 static tree
3117 cp_parser_primary_expression (cp_parser *parser,
3118                               bool address_p,
3119                               bool cast_p,
3120                               bool template_arg_p,
3121                               cp_id_kind *idk)
3122 {
3123   cp_token *token = NULL;
3124
3125   /* Assume the primary expression is not an id-expression.  */
3126   *idk = CP_ID_KIND_NONE;
3127
3128   /* Peek at the next token.  */
3129   token = cp_lexer_peek_token (parser->lexer);
3130   switch (token->type)
3131     {
3132       /* literal:
3133            integer-literal
3134            character-literal
3135            floating-literal
3136            string-literal
3137            boolean-literal  */
3138     case CPP_CHAR:
3139     case CPP_CHAR16:
3140     case CPP_CHAR32:
3141     case CPP_WCHAR:
3142     case CPP_NUMBER:
3143       token = cp_lexer_consume_token (parser->lexer);
3144       /* Floating-point literals are only allowed in an integral
3145          constant expression if they are cast to an integral or
3146          enumeration type.  */
3147       if (TREE_CODE (token->u.value) == REAL_CST
3148           && parser->integral_constant_expression_p
3149           && pedantic)
3150         {
3151           /* CAST_P will be set even in invalid code like "int(2.7 +
3152              ...)".   Therefore, we have to check that the next token
3153              is sure to end the cast.  */
3154           if (cast_p)
3155             {
3156               cp_token *next_token;
3157
3158               next_token = cp_lexer_peek_token (parser->lexer);
3159               if (/* The comma at the end of an
3160                      enumerator-definition.  */
3161                   next_token->type != CPP_COMMA
3162                   /* The curly brace at the end of an enum-specifier.  */
3163                   && next_token->type != CPP_CLOSE_BRACE
3164                   /* The end of a statement.  */
3165                   && next_token->type != CPP_SEMICOLON
3166                   /* The end of the cast-expression.  */
3167                   && next_token->type != CPP_CLOSE_PAREN
3168                   /* The end of an array bound.  */
3169                   && next_token->type != CPP_CLOSE_SQUARE
3170                   /* The closing ">" in a template-argument-list.  */
3171                   && (next_token->type != CPP_GREATER
3172                       || parser->greater_than_is_operator_p)
3173                   /* C++0x only: A ">>" treated like two ">" tokens,
3174                      in a template-argument-list.  */
3175                   && (next_token->type != CPP_RSHIFT
3176                       || (cxx_dialect == cxx98)
3177                       || parser->greater_than_is_operator_p))
3178                 cast_p = false;
3179             }
3180
3181           /* If we are within a cast, then the constraint that the
3182              cast is to an integral or enumeration type will be
3183              checked at that point.  If we are not within a cast, then
3184              this code is invalid.  */
3185           if (!cast_p)
3186             cp_parser_non_integral_constant_expression
3187               (parser, "floating-point literal");
3188         }
3189       return token->u.value;
3190
3191     case CPP_STRING:
3192     case CPP_STRING16:
3193     case CPP_STRING32:
3194     case CPP_WSTRING:
3195       /* ??? Should wide strings be allowed when parser->translate_strings_p
3196          is false (i.e. in attributes)?  If not, we can kill the third
3197          argument to cp_parser_string_literal.  */
3198       return cp_parser_string_literal (parser,
3199                                        parser->translate_strings_p,
3200                                        true);
3201
3202     case CPP_OPEN_PAREN:
3203       {
3204         tree expr;
3205         bool saved_greater_than_is_operator_p;
3206
3207         /* Consume the `('.  */
3208         cp_lexer_consume_token (parser->lexer);
3209         /* Within a parenthesized expression, a `>' token is always
3210            the greater-than operator.  */
3211         saved_greater_than_is_operator_p
3212           = parser->greater_than_is_operator_p;
3213         parser->greater_than_is_operator_p = true;
3214         /* If we see `( { ' then we are looking at the beginning of
3215            a GNU statement-expression.  */
3216         if (cp_parser_allow_gnu_extensions_p (parser)
3217             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3218           {
3219             /* Statement-expressions are not allowed by the standard.  */
3220             pedwarn (OPT_pedantic, 
3221                      "%HISO C++ forbids braced-groups within expressions",
3222                      &token->location);
3223
3224             /* And they're not allowed outside of a function-body; you
3225                cannot, for example, write:
3226
3227                  int i = ({ int j = 3; j + 1; });
3228
3229                at class or namespace scope.  */
3230             if (!parser->in_function_body
3231                 || parser->in_template_argument_list_p)
3232               {
3233                 error ("%Hstatement-expressions are not allowed outside "
3234                        "functions nor in template-argument lists",
3235                        &token->location);
3236                 cp_parser_skip_to_end_of_block_or_statement (parser);
3237                 expr = error_mark_node;
3238               }
3239             else
3240               {
3241                 /* Start the statement-expression.  */
3242                 expr = begin_stmt_expr ();
3243                 /* Parse the compound-statement.  */
3244                 cp_parser_compound_statement (parser, expr, false);
3245                 /* Finish up.  */
3246                 expr = finish_stmt_expr (expr, false);
3247               }
3248           }
3249         else
3250           {
3251             /* Parse the parenthesized expression.  */
3252             expr = cp_parser_expression (parser, cast_p);
3253             /* Let the front end know that this expression was
3254                enclosed in parentheses. This matters in case, for
3255                example, the expression is of the form `A::B', since
3256                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3257                not.  */
3258             finish_parenthesized_expr (expr);
3259           }
3260         /* The `>' token might be the end of a template-id or
3261            template-parameter-list now.  */
3262         parser->greater_than_is_operator_p
3263           = saved_greater_than_is_operator_p;
3264         /* Consume the `)'.  */
3265         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3266           cp_parser_skip_to_end_of_statement (parser);
3267
3268         return expr;
3269       }
3270
3271     case CPP_KEYWORD:
3272       switch (token->keyword)
3273         {
3274           /* These two are the boolean literals.  */
3275         case RID_TRUE:
3276           cp_lexer_consume_token (parser->lexer);
3277           return boolean_true_node;
3278         case RID_FALSE:
3279           cp_lexer_consume_token (parser->lexer);
3280           return boolean_false_node;
3281
3282           /* The `__null' literal.  */
3283         case RID_NULL:
3284           cp_lexer_consume_token (parser->lexer);
3285           return null_node;
3286
3287           /* Recognize the `this' keyword.  */
3288         case RID_THIS:
3289           cp_lexer_consume_token (parser->lexer);
3290           if (parser->local_variables_forbidden_p)
3291             {
3292               error ("%H%<this%> may not be used in this context",
3293                      &token->location);
3294               return error_mark_node;
3295             }
3296           /* Pointers cannot appear in constant-expressions.  */
3297           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3298             return error_mark_node;
3299           return finish_this_expr ();
3300
3301           /* The `operator' keyword can be the beginning of an
3302              id-expression.  */
3303         case RID_OPERATOR:
3304           goto id_expression;
3305
3306         case RID_FUNCTION_NAME:
3307         case RID_PRETTY_FUNCTION_NAME:
3308         case RID_C99_FUNCTION_NAME:
3309           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3310              __func__ are the names of variables -- but they are
3311              treated specially.  Therefore, they are handled here,
3312              rather than relying on the generic id-expression logic
3313              below.  Grammatically, these names are id-expressions.
3314
3315              Consume the token.  */
3316           token = cp_lexer_consume_token (parser->lexer);
3317           /* Look up the name.  */
3318           return finish_fname (token->u.value);
3319
3320         case RID_VA_ARG:
3321           {
3322             tree expression;
3323             tree type;
3324
3325             /* The `__builtin_va_arg' construct is used to handle
3326                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3327             cp_lexer_consume_token (parser->lexer);
3328             /* Look for the opening `('.  */
3329             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3330             /* Now, parse the assignment-expression.  */
3331             expression = cp_parser_assignment_expression (parser,
3332                                                           /*cast_p=*/false);
3333             /* Look for the `,'.  */
3334             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3335             /* Parse the type-id.  */
3336             type = cp_parser_type_id (parser);
3337             /* Look for the closing `)'.  */
3338             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3339             /* Using `va_arg' in a constant-expression is not
3340                allowed.  */
3341             if (cp_parser_non_integral_constant_expression (parser,
3342                                                             "%<va_arg%>"))
3343               return error_mark_node;
3344             return build_x_va_arg (expression, type);
3345           }
3346
3347         case RID_OFFSETOF:
3348           return cp_parser_builtin_offsetof (parser);
3349
3350         case RID_HAS_NOTHROW_ASSIGN:
3351         case RID_HAS_NOTHROW_CONSTRUCTOR:
3352         case RID_HAS_NOTHROW_COPY:        
3353         case RID_HAS_TRIVIAL_ASSIGN:
3354         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3355         case RID_HAS_TRIVIAL_COPY:        
3356         case RID_HAS_TRIVIAL_DESTRUCTOR:
3357         case RID_HAS_VIRTUAL_DESTRUCTOR:
3358         case RID_IS_ABSTRACT:
3359         case RID_IS_BASE_OF:
3360         case RID_IS_CLASS:
3361         case RID_IS_CONVERTIBLE_TO:
3362         case RID_IS_EMPTY:
3363         case RID_IS_ENUM:
3364         case RID_IS_POD:
3365         case RID_IS_POLYMORPHIC:
3366         case RID_IS_UNION:
3367           return cp_parser_trait_expr (parser, token->keyword);
3368
3369         /* Objective-C++ expressions.  */
3370         case RID_AT_ENCODE:
3371         case RID_AT_PROTOCOL:
3372         case RID_AT_SELECTOR:
3373           return cp_parser_objc_expression (parser);
3374
3375         default:
3376           cp_parser_error (parser, "expected primary-expression");
3377           return error_mark_node;
3378         }
3379
3380       /* An id-expression can start with either an identifier, a
3381          `::' as the beginning of a qualified-id, or the "operator"
3382          keyword.  */
3383     case CPP_NAME:
3384     case CPP_SCOPE:
3385     case CPP_TEMPLATE_ID:
3386     case CPP_NESTED_NAME_SPECIFIER:
3387       {
3388         tree id_expression;
3389         tree decl;
3390         const char *error_msg;
3391         bool template_p;
3392         bool done;
3393         cp_token *id_expr_token;
3394
3395       id_expression:
3396         /* Parse the id-expression.  */
3397         id_expression
3398           = cp_parser_id_expression (parser,
3399                                      /*template_keyword_p=*/false,
3400                                      /*check_dependency_p=*/true,
3401                                      &template_p,
3402                                      /*declarator_p=*/false,
3403                                      /*optional_p=*/false);
3404         if (id_expression == error_mark_node)
3405           return error_mark_node;
3406         id_expr_token = token;
3407         token = cp_lexer_peek_token (parser->lexer);
3408         done = (token->type != CPP_OPEN_SQUARE
3409                 && token->type != CPP_OPEN_PAREN
3410                 && token->type != CPP_DOT
3411                 && token->type != CPP_DEREF
3412                 && token->type != CPP_PLUS_PLUS
3413                 && token->type != CPP_MINUS_MINUS);
3414         /* If we have a template-id, then no further lookup is
3415            required.  If the template-id was for a template-class, we
3416            will sometimes have a TYPE_DECL at this point.  */
3417         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3418                  || TREE_CODE (id_expression) == TYPE_DECL)
3419           decl = id_expression;
3420         /* Look up the name.  */
3421         else
3422           {
3423             tree ambiguous_decls;
3424
3425             decl = cp_parser_lookup_name (parser, id_expression,
3426                                           none_type,
3427                                           template_p,
3428                                           /*is_namespace=*/false,
3429                                           /*check_dependency=*/true,
3430                                           &ambiguous_decls,
3431                                           id_expr_token->location);
3432             /* If the lookup was ambiguous, an error will already have
3433                been issued.  */
3434             if (ambiguous_decls)
3435               return error_mark_node;
3436
3437             /* In Objective-C++, an instance variable (ivar) may be preferred
3438                to whatever cp_parser_lookup_name() found.  */
3439             decl = objc_lookup_ivar (decl, id_expression);
3440
3441             /* If name lookup gives us a SCOPE_REF, then the
3442                qualifying scope was dependent.  */
3443             if (TREE_CODE (decl) == SCOPE_REF)
3444               {
3445                 /* At this point, we do not know if DECL is a valid
3446                    integral constant expression.  We assume that it is
3447                    in fact such an expression, so that code like:
3448
3449                       template <int N> struct A {
3450                         int a[B<N>::i];
3451                       };
3452                      
3453                    is accepted.  At template-instantiation time, we
3454                    will check that B<N>::i is actually a constant.  */
3455                 return decl;
3456               }
3457             /* Check to see if DECL is a local variable in a context
3458                where that is forbidden.  */
3459             if (parser->local_variables_forbidden_p
3460                 && local_variable_p (decl))
3461               {
3462                 /* It might be that we only found DECL because we are
3463                    trying to be generous with pre-ISO scoping rules.
3464                    For example, consider:
3465
3466                      int i;
3467                      void g() {
3468                        for (int i = 0; i < 10; ++i) {}
3469                        extern void f(int j = i);
3470                      }
3471
3472                    Here, name look up will originally find the out
3473                    of scope `i'.  We need to issue a warning message,
3474                    but then use the global `i'.  */
3475                 decl = check_for_out_of_scope_variable (decl);
3476                 if (local_variable_p (decl))
3477                   {
3478                     error ("%Hlocal variable %qD may not appear in this context",
3479                            &id_expr_token->location, decl);
3480                     return error_mark_node;
3481                   }
3482               }
3483           }
3484
3485         decl = (finish_id_expression
3486                 (id_expression, decl, parser->scope,
3487                  idk,
3488                  parser->integral_constant_expression_p,
3489                  parser->allow_non_integral_constant_expression_p,
3490                  &parser->non_integral_constant_expression_p,
3491                  template_p, done, address_p,
3492                  template_arg_p,
3493                  &error_msg,
3494                  id_expr_token->location));
3495         if (error_msg)
3496           cp_parser_error (parser, error_msg);
3497         return decl;
3498       }
3499
3500       /* Anything else is an error.  */
3501     default:
3502       /* ...unless we have an Objective-C++ message or string literal,
3503          that is.  */
3504       if (c_dialect_objc ()
3505           && (token->type == CPP_OPEN_SQUARE
3506               || token->type == CPP_OBJC_STRING))
3507         return cp_parser_objc_expression (parser);
3508
3509       cp_parser_error (parser, "expected primary-expression");
3510       return error_mark_node;
3511     }
3512 }
3513
3514 /* Parse an id-expression.
3515
3516    id-expression:
3517      unqualified-id
3518      qualified-id
3519
3520    qualified-id:
3521      :: [opt] nested-name-specifier template [opt] unqualified-id
3522      :: identifier
3523      :: operator-function-id
3524      :: template-id
3525
3526    Return a representation of the unqualified portion of the
3527    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3528    a `::' or nested-name-specifier.
3529
3530    Often, if the id-expression was a qualified-id, the caller will
3531    want to make a SCOPE_REF to represent the qualified-id.  This
3532    function does not do this in order to avoid wastefully creating
3533    SCOPE_REFs when they are not required.
3534
3535    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3536    `template' keyword.
3537
3538    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3539    uninstantiated templates.
3540
3541    If *TEMPLATE_P is non-NULL, it is set to true iff the
3542    `template' keyword is used to explicitly indicate that the entity
3543    named is a template.
3544
3545    If DECLARATOR_P is true, the id-expression is appearing as part of
3546    a declarator, rather than as part of an expression.  */
3547
3548 static tree
3549 cp_parser_id_expression (cp_parser *parser,
3550                          bool template_keyword_p,
3551                          bool check_dependency_p,
3552                          bool *template_p,
3553                          bool declarator_p,
3554                          bool optional_p)
3555 {
3556   bool global_scope_p;
3557   bool nested_name_specifier_p;
3558
3559   /* Assume the `template' keyword was not used.  */
3560   if (template_p)
3561     *template_p = template_keyword_p;
3562
3563   /* Look for the optional `::' operator.  */
3564   global_scope_p
3565     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3566        != NULL_TREE);
3567   /* Look for the optional nested-name-specifier.  */
3568   nested_name_specifier_p
3569     = (cp_parser_nested_name_specifier_opt (parser,
3570                                             /*typename_keyword_p=*/false,
3571                                             check_dependency_p,
3572                                             /*type_p=*/false,
3573                                             declarator_p)
3574        != NULL_TREE);
3575   /* If there is a nested-name-specifier, then we are looking at
3576      the first qualified-id production.  */
3577   if (nested_name_specifier_p)
3578     {
3579       tree saved_scope;
3580       tree saved_object_scope;
3581       tree saved_qualifying_scope;
3582       tree unqualified_id;
3583       bool is_template;
3584
3585       /* See if the next token is the `template' keyword.  */
3586       if (!template_p)
3587         template_p = &is_template;
3588       *template_p = cp_parser_optional_template_keyword (parser);
3589       /* Name lookup we do during the processing of the
3590          unqualified-id might obliterate SCOPE.  */
3591       saved_scope = parser->scope;
3592       saved_object_scope = parser->object_scope;
3593       saved_qualifying_scope = parser->qualifying_scope;
3594       /* Process the final unqualified-id.  */
3595       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3596                                                  check_dependency_p,
3597                                                  declarator_p,
3598                                                  /*optional_p=*/false);
3599       /* Restore the SAVED_SCOPE for our caller.  */
3600       parser->scope = saved_scope;
3601       parser->object_scope = saved_object_scope;
3602       parser->qualifying_scope = saved_qualifying_scope;
3603
3604       return unqualified_id;
3605     }
3606   /* Otherwise, if we are in global scope, then we are looking at one
3607      of the other qualified-id productions.  */
3608   else if (global_scope_p)
3609     {
3610       cp_token *token;
3611       tree id;
3612
3613       /* Peek at the next token.  */
3614       token = cp_lexer_peek_token (parser->lexer);
3615
3616       /* If it's an identifier, and the next token is not a "<", then
3617          we can avoid the template-id case.  This is an optimization
3618          for this common case.  */
3619       if (token->type == CPP_NAME
3620           && !cp_parser_nth_token_starts_template_argument_list_p
3621                (parser, 2))
3622         return cp_parser_identifier (parser);
3623
3624       cp_parser_parse_tentatively (parser);
3625       /* Try a template-id.  */
3626       id = cp_parser_template_id (parser,
3627                                   /*template_keyword_p=*/false,
3628                                   /*check_dependency_p=*/true,
3629                                   declarator_p);
3630       /* If that worked, we're done.  */
3631       if (cp_parser_parse_definitely (parser))
3632         return id;
3633
3634       /* Peek at the next token.  (Changes in the token buffer may
3635          have invalidated the pointer obtained above.)  */
3636       token = cp_lexer_peek_token (parser->lexer);
3637
3638       switch (token->type)
3639         {
3640         case CPP_NAME:
3641           return cp_parser_identifier (parser);
3642
3643         case CPP_KEYWORD:
3644           if (token->keyword == RID_OPERATOR)
3645             return cp_parser_operator_function_id (parser);
3646           /* Fall through.  */
3647
3648         default:
3649           cp_parser_error (parser, "expected id-expression");
3650           return error_mark_node;
3651         }
3652     }
3653   else
3654     return cp_parser_unqualified_id (parser, template_keyword_p,
3655                                      /*check_dependency_p=*/true,
3656                                      declarator_p,
3657                                      optional_p);
3658 }
3659
3660 /* Parse an unqualified-id.
3661
3662    unqualified-id:
3663      identifier
3664      operator-function-id
3665      conversion-function-id
3666      ~ class-name
3667      template-id
3668
3669    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3670    keyword, in a construct like `A::template ...'.
3671
3672    Returns a representation of unqualified-id.  For the `identifier'
3673    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3674    production a BIT_NOT_EXPR is returned; the operand of the
3675    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3676    other productions, see the documentation accompanying the
3677    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3678    names are looked up in uninstantiated templates.  If DECLARATOR_P
3679    is true, the unqualified-id is appearing as part of a declarator,
3680    rather than as part of an expression.  */
3681
3682 static tree
3683 cp_parser_unqualified_id (cp_parser* parser,
3684                           bool template_keyword_p,
3685                           bool check_dependency_p,
3686                           bool declarator_p,
3687                           bool optional_p)
3688 {
3689   cp_token *token;
3690
3691   /* Peek at the next token.  */
3692   token = cp_lexer_peek_token (parser->lexer);
3693
3694   switch (token->type)
3695     {
3696     case CPP_NAME:
3697       {
3698         tree id;
3699
3700         /* We don't know yet whether or not this will be a
3701            template-id.  */
3702         cp_parser_parse_tentatively (parser);
3703         /* Try a template-id.  */
3704         id = cp_parser_template_id (parser, template_keyword_p,
3705                                     check_dependency_p,
3706                                     declarator_p);
3707         /* If it worked, we're done.  */
3708         if (cp_parser_parse_definitely (parser))
3709           return id;
3710         /* Otherwise, it's an ordinary identifier.  */
3711         return cp_parser_identifier (parser);
3712       }
3713
3714     case CPP_TEMPLATE_ID:
3715       return cp_parser_template_id (parser, template_keyword_p,
3716                                     check_dependency_p,
3717                                     declarator_p);
3718
3719     case CPP_COMPL:
3720       {
3721         tree type_decl;
3722         tree qualifying_scope;
3723         tree object_scope;
3724         tree scope;
3725         bool done;
3726
3727         /* Consume the `~' token.  */
3728         cp_lexer_consume_token (parser->lexer);
3729         /* Parse the class-name.  The standard, as written, seems to
3730            say that:
3731
3732              template <typename T> struct S { ~S (); };
3733              template <typename T> S<T>::~S() {}
3734
3735            is invalid, since `~' must be followed by a class-name, but
3736            `S<T>' is dependent, and so not known to be a class.
3737            That's not right; we need to look in uninstantiated
3738            templates.  A further complication arises from:
3739
3740              template <typename T> void f(T t) {
3741                t.T::~T();
3742              }
3743
3744            Here, it is not possible to look up `T' in the scope of `T'
3745            itself.  We must look in both the current scope, and the
3746            scope of the containing complete expression.
3747
3748            Yet another issue is:
3749
3750              struct S {
3751                int S;
3752                ~S();
3753              };
3754
3755              S::~S() {}
3756
3757            The standard does not seem to say that the `S' in `~S'
3758            should refer to the type `S' and not the data member
3759            `S::S'.  */
3760
3761         /* DR 244 says that we look up the name after the "~" in the
3762            same scope as we looked up the qualifying name.  That idea
3763            isn't fully worked out; it's more complicated than that.  */
3764         scope = parser->scope;
3765         object_scope = parser->object_scope;
3766         qualifying_scope = parser->qualifying_scope;
3767
3768         /* Check for invalid scopes.  */
3769         if (scope == error_mark_node)
3770           {
3771             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3772               cp_lexer_consume_token (parser->lexer);
3773             return error_mark_node;
3774           }
3775         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3776           {
3777             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3778               error ("%Hscope %qT before %<~%> is not a class-name",
3779                      &token->location, scope);
3780             cp_parser_simulate_error (parser);
3781             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3782               cp_lexer_consume_token (parser->lexer);
3783             return error_mark_node;
3784           }
3785         gcc_assert (!scope || TYPE_P (scope));
3786
3787         /* If the name is of the form "X::~X" it's OK.  */
3788         token = cp_lexer_peek_token (parser->lexer);
3789         if (scope
3790             && token->type == CPP_NAME
3791             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3792                 == CPP_OPEN_PAREN)
3793             && constructor_name_p (token->u.value, scope))
3794           {
3795             cp_lexer_consume_token (parser->lexer);
3796             return build_nt (BIT_NOT_EXPR, scope);
3797           }
3798
3799         /* If there was an explicit qualification (S::~T), first look
3800            in the scope given by the qualification (i.e., S).  */
3801         done = false;
3802         type_decl = NULL_TREE;
3803         if (scope)
3804           {
3805             cp_parser_parse_tentatively (parser);
3806             type_decl = cp_parser_class_name (parser,
3807                                               /*typename_keyword_p=*/false,
3808                                               /*template_keyword_p=*/false,
3809                                               none_type,
3810                                               /*check_dependency=*/false,
3811                                               /*class_head_p=*/false,
3812                                               declarator_p);
3813             if (cp_parser_parse_definitely (parser))
3814               done = true;
3815           }
3816         /* In "N::S::~S", look in "N" as well.  */
3817         if (!done && scope && qualifying_scope)
3818           {
3819             cp_parser_parse_tentatively (parser);
3820             parser->scope = qualifying_scope;
3821             parser->object_scope = NULL_TREE;
3822             parser->qualifying_scope = NULL_TREE;
3823             type_decl
3824               = cp_parser_class_name (parser,
3825                                       /*typename_keyword_p=*/false,
3826                                       /*template_keyword_p=*/false,
3827                                       none_type,
3828                                       /*check_dependency=*/false,
3829                                       /*class_head_p=*/false,
3830                                       declarator_p);
3831             if (cp_parser_parse_definitely (parser))
3832               done = true;
3833           }
3834         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3835         else if (!done && object_scope)
3836           {
3837             cp_parser_parse_tentatively (parser);
3838             parser->scope = object_scope;
3839             parser->object_scope = NULL_TREE;
3840             parser->qualifying_scope = NULL_TREE;
3841             type_decl
3842               = cp_parser_class_name (parser,
3843                                       /*typename_keyword_p=*/false,
3844                                       /*template_keyword_p=*/false,
3845                                       none_type,
3846                                       /*check_dependency=*/false,
3847                                       /*class_head_p=*/false,
3848                                       declarator_p);
3849             if (cp_parser_parse_definitely (parser))
3850               done = true;
3851           }
3852         /* Look in the surrounding context.  */
3853         if (!done)
3854           {
3855             parser->scope = NULL_TREE;
3856             parser->object_scope = NULL_TREE;
3857             parser->qualifying_scope = NULL_TREE;
3858             type_decl
3859               = cp_parser_class_name (parser,
3860                                       /*typename_keyword_p=*/false,
3861                                       /*template_keyword_p=*/false,
3862                                       none_type,
3863                                       /*check_dependency=*/false,
3864                                       /*class_head_p=*/false,
3865                                       declarator_p);
3866           }
3867         /* If an error occurred, assume that the name of the
3868            destructor is the same as the name of the qualifying
3869            class.  That allows us to keep parsing after running
3870            into ill-formed destructor names.  */
3871         if (type_decl == error_mark_node && scope)
3872           return build_nt (BIT_NOT_EXPR, scope);
3873         else if (type_decl == error_mark_node)
3874           return error_mark_node;
3875
3876         /* Check that destructor name and scope match.  */
3877         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3878           {
3879             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3880               error ("%Hdeclaration of %<~%T%> as member of %qT",
3881                      &token->location, type_decl, scope);
3882             cp_parser_simulate_error (parser);
3883             return error_mark_node;
3884           }
3885
3886         /* [class.dtor]
3887
3888            A typedef-name that names a class shall not be used as the
3889            identifier in the declarator for a destructor declaration.  */
3890         if (declarator_p
3891             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3892             && !DECL_SELF_REFERENCE_P (type_decl)
3893             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3894           error ("%Htypedef-name %qD used as destructor declarator",
3895                  &token->location, type_decl);
3896
3897         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3898       }
3899
3900     case CPP_KEYWORD:
3901       if (token->keyword == RID_OPERATOR)
3902         {
3903           tree id;
3904
3905           /* This could be a template-id, so we try that first.  */
3906           cp_parser_parse_tentatively (parser);
3907           /* Try a template-id.  */
3908           id = cp_parser_template_id (parser, template_keyword_p,
3909                                       /*check_dependency_p=*/true,
3910                                       declarator_p);
3911           /* If that worked, we're done.  */
3912           if (cp_parser_parse_definitely (parser))
3913             return id;
3914           /* We still don't know whether we're looking at an
3915              operator-function-id or a conversion-function-id.  */
3916           cp_parser_parse_tentatively (parser);
3917           /* Try an operator-function-id.  */
3918           id = cp_parser_operator_function_id (parser);
3919           /* If that didn't work, try a conversion-function-id.  */
3920           if (!cp_parser_parse_definitely (parser))
3921             id = cp_parser_conversion_function_id (parser);
3922
3923           return id;
3924         }
3925       /* Fall through.  */
3926
3927     default:
3928       if (optional_p)
3929         return NULL_TREE;
3930       cp_parser_error (parser, "expected unqualified-id");
3931       return error_mark_node;
3932     }
3933 }
3934
3935 /* Parse an (optional) nested-name-specifier.
3936
3937    nested-name-specifier:
3938      class-or-namespace-name :: nested-name-specifier [opt]
3939      class-or-namespace-name :: template nested-name-specifier [opt]
3940
3941    PARSER->SCOPE should be set appropriately before this function is
3942    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3943    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3944    in name lookups.
3945
3946    Sets PARSER->SCOPE to the class (TYPE) or namespace
3947    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3948    it unchanged if there is no nested-name-specifier.  Returns the new
3949    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3950
3951    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3952    part of a declaration and/or decl-specifier.  */
3953
3954 static tree
3955 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3956                                      bool typename_keyword_p,
3957                                      bool check_dependency_p,
3958                                      bool type_p,
3959                                      bool is_declaration)
3960 {
3961   bool success = false;
3962   cp_token_position start = 0;
3963   cp_token *token;
3964
3965   /* Remember where the nested-name-specifier starts.  */
3966   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3967     {
3968       start = cp_lexer_token_position (parser->lexer, false);
3969       push_deferring_access_checks (dk_deferred);
3970     }
3971
3972   while (true)
3973     {
3974       tree new_scope;
3975       tree old_scope;
3976       tree saved_qualifying_scope;
3977       bool template_keyword_p;
3978
3979       /* Spot cases that cannot be the beginning of a
3980          nested-name-specifier.  */
3981       token = cp_lexer_peek_token (parser->lexer);
3982
3983       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3984          the already parsed nested-name-specifier.  */
3985       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3986         {
3987           /* Grab the nested-name-specifier and continue the loop.  */
3988           cp_parser_pre_parsed_nested_name_specifier (parser);
3989           /* If we originally encountered this nested-name-specifier
3990              with IS_DECLARATION set to false, we will not have
3991              resolved TYPENAME_TYPEs, so we must do so here.  */
3992           if (is_declaration
3993               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3994             {
3995               new_scope = resolve_typename_type (parser->scope,
3996                                                  /*only_current_p=*/false);
3997               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3998                 parser->scope = new_scope;
3999             }
4000           success = true;
4001           continue;
4002         }
4003
4004       /* Spot cases that cannot be the beginning of a
4005          nested-name-specifier.  On the second and subsequent times
4006          through the loop, we look for the `template' keyword.  */
4007       if (success && token->keyword == RID_TEMPLATE)
4008         ;
4009       /* A template-id can start a nested-name-specifier.  */
4010       else if (token->type == CPP_TEMPLATE_ID)
4011         ;
4012       else
4013         {
4014           /* If the next token is not an identifier, then it is
4015              definitely not a class-or-namespace-name.  */
4016           if (token->type != CPP_NAME)
4017             break;
4018           /* If the following token is neither a `<' (to begin a
4019              template-id), nor a `::', then we are not looking at a
4020              nested-name-specifier.  */
4021           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4022           if (token->type != CPP_SCOPE
4023               && !cp_parser_nth_token_starts_template_argument_list_p
4024                   (parser, 2))
4025             break;
4026         }
4027
4028       /* The nested-name-specifier is optional, so we parse
4029          tentatively.  */
4030       cp_parser_parse_tentatively (parser);
4031
4032       /* Look for the optional `template' keyword, if this isn't the
4033          first time through the loop.  */
4034       if (success)
4035         template_keyword_p = cp_parser_optional_template_keyword (parser);
4036       else
4037         template_keyword_p = false;
4038
4039       /* Save the old scope since the name lookup we are about to do
4040          might destroy it.  */
4041       old_scope = parser->scope;
4042       saved_qualifying_scope = parser->qualifying_scope;
4043       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4044          look up names in "X<T>::I" in order to determine that "Y" is
4045          a template.  So, if we have a typename at this point, we make
4046          an effort to look through it.  */
4047       if (is_declaration
4048           && !typename_keyword_p
4049           && parser->scope
4050           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4051         parser->scope = resolve_typename_type (parser->scope,
4052                                                /*only_current_p=*/false);
4053       /* Parse the qualifying entity.  */
4054       new_scope
4055         = cp_parser_class_or_namespace_name (parser,
4056                                              typename_keyword_p,
4057                                              template_keyword_p,
4058                                              check_dependency_p,
4059                                              type_p,
4060                                              is_declaration);
4061       /* Look for the `::' token.  */
4062       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4063
4064       /* If we found what we wanted, we keep going; otherwise, we're
4065          done.  */
4066       if (!cp_parser_parse_definitely (parser))
4067         {
4068           bool error_p = false;
4069
4070           /* Restore the OLD_SCOPE since it was valid before the
4071              failed attempt at finding the last
4072              class-or-namespace-name.  */
4073           parser->scope = old_scope;
4074           parser->qualifying_scope = saved_qualifying_scope;
4075           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4076             break;
4077           /* If the next token is an identifier, and the one after
4078              that is a `::', then any valid interpretation would have
4079              found a class-or-namespace-name.  */
4080           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4081                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4082                      == CPP_SCOPE)
4083                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4084                      != CPP_COMPL))
4085             {
4086               token = cp_lexer_consume_token (parser->lexer);
4087               if (!error_p)
4088                 {
4089                   if (!token->ambiguous_p)
4090                     {
4091                       tree decl;
4092                       tree ambiguous_decls;
4093
4094                       decl = cp_parser_lookup_name (parser, token->u.value,
4095                                                     none_type,
4096                                                     /*is_template=*/false,
4097                                                     /*is_namespace=*/false,
4098                                                     /*check_dependency=*/true,
4099                                                     &ambiguous_decls,
4100                                                     token->location);
4101                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4102                         error ("%H%qD used without template parameters",
4103                                &token->location, decl);
4104                       else if (ambiguous_decls)
4105                         {
4106                           error ("%Hreference to %qD is ambiguous",
4107                                  &token->location, token->u.value);
4108                           print_candidates (ambiguous_decls);
4109                           decl = error_mark_node;
4110                         }
4111                       else
4112                         cp_parser_name_lookup_error
4113                           (parser, token->u.value, decl,
4114                            "is not a class or namespace",
4115                            token->location);
4116                     }
4117                   parser->scope = error_mark_node;
4118                   error_p = true;
4119                   /* Treat this as a successful nested-name-specifier
4120                      due to:
4121
4122                      [basic.lookup.qual]
4123
4124                      If the name found is not a class-name (clause
4125                      _class_) or namespace-name (_namespace.def_), the
4126                      program is ill-formed.  */
4127                   success = true;
4128                 }
4129               cp_lexer_consume_token (parser->lexer);
4130             }
4131           break;
4132         }
4133       /* We've found one valid nested-name-specifier.  */
4134       success = true;
4135       /* Name lookup always gives us a DECL.  */
4136       if (TREE_CODE (new_scope) == TYPE_DECL)
4137         new_scope = TREE_TYPE (new_scope);
4138       /* Uses of "template" must be followed by actual templates.  */
4139       if (template_keyword_p
4140           && !(CLASS_TYPE_P (new_scope)
4141                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4142                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4143                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4144           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4145                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4146                    == TEMPLATE_ID_EXPR)))
4147         permerror (input_location, TYPE_P (new_scope)
4148                    ? "%qT is not a template"
4149                    : "%qD is not a template",
4150                    new_scope);
4151       /* If it is a class scope, try to complete it; we are about to
4152          be looking up names inside the class.  */
4153       if (TYPE_P (new_scope)
4154           /* Since checking types for dependency can be expensive,
4155              avoid doing it if the type is already complete.  */
4156           && !COMPLETE_TYPE_P (new_scope)
4157           /* Do not try to complete dependent types.  */
4158           && !dependent_type_p (new_scope))
4159         {
4160           new_scope = complete_type (new_scope);
4161           /* If it is a typedef to current class, use the current
4162              class instead, as the typedef won't have any names inside
4163              it yet.  */
4164           if (!COMPLETE_TYPE_P (new_scope)
4165               && currently_open_class (new_scope))
4166             new_scope = TYPE_MAIN_VARIANT (new_scope);
4167         }
4168       /* Make sure we look in the right scope the next time through
4169          the loop.  */
4170       parser->scope = new_scope;
4171     }
4172
4173   /* If parsing tentatively, replace the sequence of tokens that makes
4174      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4175      token.  That way, should we re-parse the token stream, we will
4176      not have to repeat the effort required to do the parse, nor will
4177      we issue duplicate error messages.  */
4178   if (success && start)
4179     {
4180       cp_token *token;
4181
4182       token = cp_lexer_token_at (parser->lexer, start);
4183       /* Reset the contents of the START token.  */
4184       token->type = CPP_NESTED_NAME_SPECIFIER;
4185       /* Retrieve any deferred checks.  Do not pop this access checks yet
4186          so the memory will not be reclaimed during token replacing below.  */
4187       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4188       token->u.tree_check_value->value = parser->scope;
4189       token->u.tree_check_value->checks = get_deferred_access_checks ();
4190       token->u.tree_check_value->qualifying_scope =
4191         parser->qualifying_scope;
4192       token->keyword = RID_MAX;
4193
4194       /* Purge all subsequent tokens.  */
4195       cp_lexer_purge_tokens_after (parser->lexer, start);
4196     }
4197
4198   if (start)
4199     pop_to_parent_deferring_access_checks ();
4200
4201   return success ? parser->scope : NULL_TREE;
4202 }
4203
4204 /* Parse a nested-name-specifier.  See
4205    cp_parser_nested_name_specifier_opt for details.  This function
4206    behaves identically, except that it will an issue an error if no
4207    nested-name-specifier is present.  */
4208
4209 static tree
4210 cp_parser_nested_name_specifier (cp_parser *parser,
4211                                  bool typename_keyword_p,
4212                                  bool check_dependency_p,
4213                                  bool type_p,
4214                                  bool is_declaration)
4215 {
4216   tree scope;
4217
4218   /* Look for the nested-name-specifier.  */
4219   scope = cp_parser_nested_name_specifier_opt (parser,
4220                                                typename_keyword_p,
4221                                                check_dependency_p,
4222                                                type_p,
4223                                                is_declaration);
4224   /* If it was not present, issue an error message.  */
4225   if (!scope)
4226     {
4227       cp_parser_error (parser, "expected nested-name-specifier");
4228       parser->scope = NULL_TREE;
4229     }
4230
4231   return scope;
4232 }
4233
4234 /* Parse a class-or-namespace-name.
4235
4236    class-or-namespace-name:
4237      class-name
4238      namespace-name
4239
4240    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4241    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4242    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4243    TYPE_P is TRUE iff the next name should be taken as a class-name,
4244    even the same name is declared to be another entity in the same
4245    scope.
4246
4247    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4248    specified by the class-or-namespace-name.  If neither is found the
4249    ERROR_MARK_NODE is returned.  */
4250
4251 static tree
4252 cp_parser_class_or_namespace_name (cp_parser *parser,
4253                                    bool typename_keyword_p,
4254                                    bool template_keyword_p,
4255                                    bool check_dependency_p,
4256                                    bool type_p,
4257                                    bool is_declaration)
4258 {
4259   tree saved_scope;
4260   tree saved_qualifying_scope;
4261   tree saved_object_scope;
4262   tree scope;
4263   bool only_class_p;
4264
4265   /* Before we try to parse the class-name, we must save away the
4266      current PARSER->SCOPE since cp_parser_class_name will destroy
4267      it.  */
4268   saved_scope = parser->scope;
4269   saved_qualifying_scope = parser->qualifying_scope;
4270   saved_object_scope = parser->object_scope;
4271   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4272      there is no need to look for a namespace-name.  */
4273   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4274   if (!only_class_p)
4275     cp_parser_parse_tentatively (parser);
4276   scope = cp_parser_class_name (parser,
4277                                 typename_keyword_p,
4278                                 template_keyword_p,
4279                                 type_p ? class_type : none_type,
4280                                 check_dependency_p,
4281                                 /*class_head_p=*/false,
4282                                 is_declaration);
4283   /* If that didn't work, try for a namespace-name.  */
4284   if (!only_class_p && !cp_parser_parse_definitely (parser))
4285     {
4286       /* Restore the saved scope.  */
4287       parser->scope = saved_scope;
4288       parser->qualifying_scope = saved_qualifying_scope;
4289       parser->object_scope = saved_object_scope;
4290       /* If we are not looking at an identifier followed by the scope
4291          resolution operator, then this is not part of a
4292          nested-name-specifier.  (Note that this function is only used
4293          to parse the components of a nested-name-specifier.)  */
4294       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4295           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4296         return error_mark_node;
4297       scope = cp_parser_namespace_name (parser);
4298     }
4299
4300   return scope;
4301 }
4302
4303 /* Parse a postfix-expression.
4304
4305    postfix-expression:
4306      primary-expression
4307      postfix-expression [ expression ]
4308      postfix-expression ( expression-list [opt] )
4309      simple-type-specifier ( expression-list [opt] )
4310      typename :: [opt] nested-name-specifier identifier
4311        ( expression-list [opt] )
4312      typename :: [opt] nested-name-specifier template [opt] template-id
4313        ( expression-list [opt] )
4314      postfix-expression . template [opt] id-expression
4315      postfix-expression -> template [opt] id-expression
4316      postfix-expression . pseudo-destructor-name
4317      postfix-expression -> pseudo-destructor-name
4318      postfix-expression ++
4319      postfix-expression --
4320      dynamic_cast < type-id > ( expression )
4321      static_cast < type-id > ( expression )
4322      reinterpret_cast < type-id > ( expression )
4323      const_cast < type-id > ( expression )
4324      typeid ( expression )
4325      typeid ( type-id )
4326
4327    GNU Extension:
4328
4329    postfix-expression:
4330      ( type-id ) { initializer-list , [opt] }
4331
4332    This extension is a GNU version of the C99 compound-literal
4333    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4334    but they are essentially the same concept.)
4335
4336    If ADDRESS_P is true, the postfix expression is the operand of the
4337    `&' operator.  CAST_P is true if this expression is the target of a
4338    cast.
4339
4340    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4341    class member access expressions [expr.ref].
4342
4343    Returns a representation of the expression.  */
4344
4345 static tree
4346 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4347                               bool member_access_only_p)
4348 {
4349   cp_token *token;
4350   enum rid keyword;
4351   cp_id_kind idk = CP_ID_KIND_NONE;
4352   tree postfix_expression = NULL_TREE;
4353   bool is_member_access = false;
4354
4355   /* Peek at the next token.  */
4356   token = cp_lexer_peek_token (parser->lexer);
4357   /* Some of the productions are determined by keywords.  */
4358   keyword = token->keyword;
4359   switch (keyword)
4360     {
4361     case RID_DYNCAST:
4362     case RID_STATCAST:
4363     case RID_REINTCAST:
4364     case RID_CONSTCAST:
4365       {
4366         tree type;
4367         tree expression;
4368         const char *saved_message;
4369
4370         /* All of these can be handled in the same way from the point
4371            of view of parsing.  Begin by consuming the token
4372            identifying the cast.  */
4373         cp_lexer_consume_token (parser->lexer);
4374
4375         /* New types cannot be defined in the cast.  */
4376         saved_message = parser->type_definition_forbidden_message;
4377         parser->type_definition_forbidden_message
4378           = "types may not be defined in casts";
4379
4380         /* Look for the opening `<'.  */
4381         cp_parser_require (parser, CPP_LESS, "%<<%>");
4382         /* Parse the type to which we are casting.  */
4383         type = cp_parser_type_id (parser);
4384         /* Look for the closing `>'.  */
4385         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4386         /* Restore the old message.  */
4387         parser->type_definition_forbidden_message = saved_message;
4388
4389         /* And the expression which is being cast.  */
4390         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4391         expression = cp_parser_expression (parser, /*cast_p=*/true);
4392         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4393
4394         /* Only type conversions to integral or enumeration types
4395            can be used in constant-expressions.  */
4396         if (!cast_valid_in_integral_constant_expression_p (type)
4397             && (cp_parser_non_integral_constant_expression
4398                 (parser,
4399                  "a cast to a type other than an integral or "
4400                  "enumeration type")))
4401           return error_mark_node;
4402
4403         switch (keyword)
4404           {
4405           case RID_DYNCAST:
4406             postfix_expression
4407               = build_dynamic_cast (type, expression, tf_warning_or_error);
4408             break;
4409           case RID_STATCAST:
4410             postfix_expression
4411               = build_static_cast (type, expression, tf_warning_or_error);
4412             break;
4413           case RID_REINTCAST:
4414             postfix_expression
4415               = build_reinterpret_cast (type, expression, 
4416                                         tf_warning_or_error);
4417             break;
4418           case RID_CONSTCAST:
4419             postfix_expression
4420               = build_const_cast (type, expression, tf_warning_or_error);
4421             break;
4422           default:
4423             gcc_unreachable ();
4424           }
4425       }
4426       break;
4427
4428     case RID_TYPEID:
4429       {
4430         tree type;
4431         const char *saved_message;
4432         bool saved_in_type_id_in_expr_p;
4433
4434         /* Consume the `typeid' token.  */
4435         cp_lexer_consume_token (parser->lexer);
4436         /* Look for the `(' token.  */
4437         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4438         /* Types cannot be defined in a `typeid' expression.  */
4439         saved_message = parser->type_definition_forbidden_message;
4440         parser->type_definition_forbidden_message
4441           = "types may not be defined in a %<typeid%> expression";
4442         /* We can't be sure yet whether we're looking at a type-id or an
4443            expression.  */
4444         cp_parser_parse_tentatively (parser);
4445         /* Try a type-id first.  */
4446         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4447         parser->in_type_id_in_expr_p = true;
4448         type = cp_parser_type_id (parser);
4449         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4450         /* Look for the `)' token.  Otherwise, we can't be sure that
4451            we're not looking at an expression: consider `typeid (int
4452            (3))', for example.  */
4453         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4454         /* If all went well, simply lookup the type-id.  */
4455         if (cp_parser_parse_definitely (parser))
4456           postfix_expression = get_typeid (type);
4457         /* Otherwise, fall back to the expression variant.  */
4458         else
4459           {
4460             tree expression;
4461
4462             /* Look for an expression.  */
4463             expression = cp_parser_expression (parser, /*cast_p=*/false);
4464             /* Compute its typeid.  */
4465             postfix_expression = build_typeid (expression);
4466             /* Look for the `)' token.  */
4467             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4468           }
4469         /* Restore the saved message.  */
4470         parser->type_definition_forbidden_message = saved_message;
4471         /* `typeid' may not appear in an integral constant expression.  */
4472         if (cp_parser_non_integral_constant_expression(parser,
4473                                                        "%<typeid%> operator"))
4474           return error_mark_node;
4475       }
4476       break;
4477
4478     case RID_TYPENAME:
4479       {
4480         tree type;
4481         /* The syntax permitted here is the same permitted for an
4482            elaborated-type-specifier.  */
4483         type = cp_parser_elaborated_type_specifier (parser,
4484                                                     /*is_friend=*/false,
4485                                                     /*is_declaration=*/false);
4486         postfix_expression = cp_parser_functional_cast (parser, type);
4487       }
4488       break;
4489
4490     default:
4491       {
4492         tree type;
4493
4494         /* If the next thing is a simple-type-specifier, we may be
4495            looking at a functional cast.  We could also be looking at
4496            an id-expression.  So, we try the functional cast, and if
4497            that doesn't work we fall back to the primary-expression.  */
4498         cp_parser_parse_tentatively (parser);
4499         /* Look for the simple-type-specifier.  */
4500         type = cp_parser_simple_type_specifier (parser,
4501                                                 /*decl_specs=*/NULL,
4502                                                 CP_PARSER_FLAGS_NONE);
4503         /* Parse the cast itself.  */
4504         if (!cp_parser_error_occurred (parser))
4505           postfix_expression
4506             = cp_parser_functional_cast (parser, type);
4507         /* If that worked, we're done.  */
4508         if (cp_parser_parse_definitely (parser))
4509           break;
4510
4511         /* If the functional-cast didn't work out, try a
4512            compound-literal.  */
4513         if (cp_parser_allow_gnu_extensions_p (parser)
4514             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4515           {
4516             VEC(constructor_elt,gc) *initializer_list = NULL;
4517             bool saved_in_type_id_in_expr_p;
4518
4519             cp_parser_parse_tentatively (parser);
4520             /* Consume the `('.  */
4521             cp_lexer_consume_token (parser->lexer);
4522             /* Parse the type.  */
4523             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4524             parser->in_type_id_in_expr_p = true;
4525             type = cp_parser_type_id (parser);
4526             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4527             /* Look for the `)'.  */
4528             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4529             /* Look for the `{'.  */
4530             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4531             /* If things aren't going well, there's no need to
4532                keep going.  */
4533             if (!cp_parser_error_occurred (parser))
4534               {
4535                 bool non_constant_p;
4536                 /* Parse the initializer-list.  */
4537                 initializer_list
4538                   = cp_parser_initializer_list (parser, &non_constant_p);
4539                 /* Allow a trailing `,'.  */
4540                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4541                   cp_lexer_consume_token (parser->lexer);
4542                 /* Look for the final `}'.  */
4543                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4544               }
4545             /* If that worked, we're definitely looking at a
4546                compound-literal expression.  */
4547             if (cp_parser_parse_definitely (parser))
4548               {
4549                 /* Warn the user that a compound literal is not
4550                    allowed in standard C++.  */
4551                 pedwarn (OPT_pedantic, "ISO C++ forbids compound-literals");
4552                 /* For simplicity, we disallow compound literals in
4553                    constant-expressions.  We could
4554                    allow compound literals of integer type, whose
4555                    initializer was a constant, in constant
4556                    expressions.  Permitting that usage, as a further
4557                    extension, would not change the meaning of any
4558                    currently accepted programs.  (Of course, as
4559                    compound literals are not part of ISO C++, the
4560                    standard has nothing to say.)  */
4561                 if (cp_parser_non_integral_constant_expression 
4562                     (parser, "non-constant compound literals"))
4563                   {
4564                     postfix_expression = error_mark_node;
4565                     break;
4566                   }
4567                 /* Form the representation of the compound-literal.  */
4568                 postfix_expression
4569                   = (finish_compound_literal
4570                      (type, build_constructor (init_list_type_node,
4571                                                initializer_list)));
4572                 break;
4573               }
4574           }
4575
4576         /* It must be a primary-expression.  */
4577         postfix_expression
4578           = cp_parser_primary_expression (parser, address_p, cast_p,
4579                                           /*template_arg_p=*/false,
4580                                           &idk);
4581       }
4582       break;
4583     }
4584
4585   /* Keep looping until the postfix-expression is complete.  */
4586   while (true)
4587     {
4588       if (idk == CP_ID_KIND_UNQUALIFIED
4589           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4590           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4591         /* It is not a Koenig lookup function call.  */
4592         postfix_expression
4593           = unqualified_name_lookup_error (postfix_expression);
4594
4595       /* Peek at the next token.  */
4596       token = cp_lexer_peek_token (parser->lexer);
4597
4598       switch (token->type)
4599         {
4600         case CPP_OPEN_SQUARE:
4601           postfix_expression
4602             = cp_parser_postfix_open_square_expression (parser,
4603                                                         postfix_expression,
4604                                                         false);
4605           idk = CP_ID_KIND_NONE;
4606           is_member_access = false;
4607           break;
4608
4609         case CPP_OPEN_PAREN:
4610           /* postfix-expression ( expression-list [opt] ) */
4611           {
4612             bool koenig_p;
4613             bool is_builtin_constant_p;
4614             bool saved_integral_constant_expression_p = false;
4615             bool saved_non_integral_constant_expression_p = false;
4616             tree args;
4617
4618             is_member_access = false;
4619
4620             is_builtin_constant_p
4621               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4622             if (is_builtin_constant_p)
4623               {
4624                 /* The whole point of __builtin_constant_p is to allow
4625                    non-constant expressions to appear as arguments.  */
4626                 saved_integral_constant_expression_p
4627                   = parser->integral_constant_expression_p;
4628                 saved_non_integral_constant_expression_p
4629                   = parser->non_integral_constant_expression_p;
4630                 parser->integral_constant_expression_p = false;
4631               }
4632             args = (cp_parser_parenthesized_expression_list
4633                     (parser, /*is_attribute_list=*/false,
4634                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4635                      /*non_constant_p=*/NULL));
4636             if (is_builtin_constant_p)
4637               {
4638                 parser->integral_constant_expression_p
4639                   = saved_integral_constant_expression_p;
4640                 parser->non_integral_constant_expression_p
4641                   = saved_non_integral_constant_expression_p;
4642               }
4643
4644             if (args == error_mark_node)
4645               {
4646                 postfix_expression = error_mark_node;
4647                 break;
4648               }
4649
4650             /* Function calls are not permitted in
4651                constant-expressions.  */
4652             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4653                 && cp_parser_non_integral_constant_expression (parser,
4654                                                                "a function call"))
4655               {
4656                 postfix_expression = error_mark_node;
4657                 break;
4658               }
4659
4660             koenig_p = false;
4661             if (idk == CP_ID_KIND_UNQUALIFIED)
4662               {
4663                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4664                   {
4665                     if (args)
4666                       {
4667                         koenig_p = true;
4668                         postfix_expression
4669                           = perform_koenig_lookup (postfix_expression, args);
4670                       }
4671                     else
4672                       postfix_expression
4673                         = unqualified_fn_lookup_error (postfix_expression);
4674                   }
4675                 /* We do not perform argument-dependent lookup if
4676                    normal lookup finds a non-function, in accordance
4677                    with the expected resolution of DR 218.  */
4678                 else if (args && is_overloaded_fn (postfix_expression))
4679                   {
4680                     tree fn = get_first_fn (postfix_expression);
4681
4682                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4683                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4684
4685                     /* Only do argument dependent lookup if regular
4686                        lookup does not find a set of member functions.
4687                        [basic.lookup.koenig]/2a  */
4688                     if (!DECL_FUNCTION_MEMBER_P (fn))
4689                       {
4690                         koenig_p = true;
4691                         postfix_expression
4692                           = perform_koenig_lookup (postfix_expression, args);
4693                       }
4694                   }
4695               }
4696
4697             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4698               {
4699                 tree instance = TREE_OPERAND (postfix_expression, 0);
4700                 tree fn = TREE_OPERAND (postfix_expression, 1);
4701
4702                 if (processing_template_decl
4703                     && (type_dependent_expression_p (instance)
4704                         || (!BASELINK_P (fn)
4705                             && TREE_CODE (fn) != FIELD_DECL)
4706                         || type_dependent_expression_p (fn)
4707                         || any_type_dependent_arguments_p (args)))
4708                   {
4709                     postfix_expression
4710                       = build_nt_call_list (postfix_expression, args);
4711                     break;
4712                   }
4713
4714                 if (BASELINK_P (fn))
4715                   postfix_expression
4716                     = (build_new_method_call
4717                        (instance, fn, args, NULL_TREE,
4718                         (idk == CP_ID_KIND_QUALIFIED
4719                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4720                         /*fn_p=*/NULL,
4721                         tf_warning_or_error));
4722                 else
4723                   postfix_expression
4724                     = finish_call_expr (postfix_expression, args,
4725                                         /*disallow_virtual=*/false,
4726                                         /*koenig_p=*/false,
4727                                         tf_warning_or_error);
4728               }
4729             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4730                      || TREE_CODE (postfix_expression) == MEMBER_REF
4731                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4732               postfix_expression = (build_offset_ref_call_from_tree
4733                                     (postfix_expression, args));
4734             else if (idk == CP_ID_KIND_QUALIFIED)
4735               /* A call to a static class member, or a namespace-scope
4736                  function.  */
4737               postfix_expression
4738                 = finish_call_expr (postfix_expression, args,
4739                                     /*disallow_virtual=*/true,
4740                                     koenig_p,
4741                                     tf_warning_or_error);
4742             else
4743               /* All other function calls.  */
4744               postfix_expression
4745                 = finish_call_expr (postfix_expression, args,
4746                                     /*disallow_virtual=*/false,
4747                                     koenig_p,
4748                                     tf_warning_or_error);
4749
4750             if (warn_disallowed_functions)
4751               warn_if_disallowed_function_p (postfix_expression);
4752
4753             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4754             idk = CP_ID_KIND_NONE;
4755           }
4756           break;
4757
4758         case CPP_DOT:
4759         case CPP_DEREF:
4760           /* postfix-expression . template [opt] id-expression
4761              postfix-expression . pseudo-destructor-name
4762              postfix-expression -> template [opt] id-expression
4763              postfix-expression -> pseudo-destructor-name */
4764
4765           /* Consume the `.' or `->' operator.  */
4766           cp_lexer_consume_token (parser->lexer);
4767
4768           postfix_expression
4769             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4770                                                       postfix_expression,
4771                                                       false, &idk,
4772                                                       token->location);
4773
4774           is_member_access = true;
4775           break;
4776
4777         case CPP_PLUS_PLUS:
4778           /* postfix-expression ++  */
4779           /* Consume the `++' token.  */
4780           cp_lexer_consume_token (parser->lexer);
4781           /* Generate a representation for the complete expression.  */
4782           postfix_expression
4783             = finish_increment_expr (postfix_expression,
4784                                      POSTINCREMENT_EXPR);
4785           /* Increments may not appear in constant-expressions.  */
4786           if (cp_parser_non_integral_constant_expression (parser,
4787                                                           "an increment"))
4788             postfix_expression = error_mark_node;
4789           idk = CP_ID_KIND_NONE;
4790           is_member_access = false;
4791           break;
4792
4793         case CPP_MINUS_MINUS:
4794           /* postfix-expression -- */
4795           /* Consume the `--' token.  */
4796           cp_lexer_consume_token (parser->lexer);
4797           /* Generate a representation for the complete expression.  */
4798           postfix_expression
4799             = finish_increment_expr (postfix_expression,
4800                                      POSTDECREMENT_EXPR);
4801           /* Decrements may not appear in constant-expressions.  */
4802           if (cp_parser_non_integral_constant_expression (parser,
4803                                                           "a decrement"))
4804             postfix_expression = error_mark_node;
4805           idk = CP_ID_KIND_NONE;
4806           is_member_access = false;
4807           break;
4808
4809         default:
4810           if (member_access_only_p)
4811             return is_member_access? postfix_expression : error_mark_node;
4812           else
4813             return postfix_expression;
4814         }
4815     }
4816
4817   /* We should never get here.  */
4818   gcc_unreachable ();
4819   return error_mark_node;
4820 }
4821
4822 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4823    by cp_parser_builtin_offsetof.  We're looking for
4824
4825      postfix-expression [ expression ]
4826
4827    FOR_OFFSETOF is set if we're being called in that context, which
4828    changes how we deal with integer constant expressions.  */
4829
4830 static tree
4831 cp_parser_postfix_open_square_expression (cp_parser *parser,
4832                                           tree postfix_expression,
4833                                           bool for_offsetof)
4834 {
4835   tree index;
4836
4837   /* Consume the `[' token.  */
4838   cp_lexer_consume_token (parser->lexer);
4839
4840   /* Parse the index expression.  */
4841   /* ??? For offsetof, there is a question of what to allow here.  If
4842      offsetof is not being used in an integral constant expression context,
4843      then we *could* get the right answer by computing the value at runtime.
4844      If we are in an integral constant expression context, then we might
4845      could accept any constant expression; hard to say without analysis.
4846      Rather than open the barn door too wide right away, allow only integer
4847      constant expressions here.  */
4848   if (for_offsetof)
4849     index = cp_parser_constant_expression (parser, false, NULL);
4850   else
4851     index = cp_parser_expression (parser, /*cast_p=*/false);
4852
4853   /* Look for the closing `]'.  */
4854   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4855
4856   /* Build the ARRAY_REF.  */
4857   postfix_expression = grok_array_decl (postfix_expression, index);
4858
4859   /* When not doing offsetof, array references are not permitted in
4860      constant-expressions.  */
4861   if (!for_offsetof
4862       && (cp_parser_non_integral_constant_expression
4863           (parser, "an array reference")))
4864     postfix_expression = error_mark_node;
4865
4866   return postfix_expression;
4867 }
4868
4869 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4870    by cp_parser_builtin_offsetof.  We're looking for
4871
4872      postfix-expression . template [opt] id-expression
4873      postfix-expression . pseudo-destructor-name
4874      postfix-expression -> template [opt] id-expression
4875      postfix-expression -> pseudo-destructor-name
4876
4877    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4878    limits what of the above we'll actually accept, but nevermind.
4879    TOKEN_TYPE is the "." or "->" token, which will already have been
4880    removed from the stream.  */
4881
4882 static tree
4883 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4884                                         enum cpp_ttype token_type,
4885                                         tree postfix_expression,
4886                                         bool for_offsetof, cp_id_kind *idk,
4887                                         location_t location)
4888 {
4889   tree name;
4890   bool dependent_p;
4891   bool pseudo_destructor_p;
4892   tree scope = NULL_TREE;
4893
4894   /* If this is a `->' operator, dereference the pointer.  */
4895   if (token_type == CPP_DEREF)
4896     postfix_expression = build_x_arrow (postfix_expression);
4897   /* Check to see whether or not the expression is type-dependent.  */
4898   dependent_p = type_dependent_expression_p (postfix_expression);
4899   /* The identifier following the `->' or `.' is not qualified.  */
4900   parser->scope = NULL_TREE;
4901   parser->qualifying_scope = NULL_TREE;
4902   parser->object_scope = NULL_TREE;
4903   *idk = CP_ID_KIND_NONE;
4904   /* Enter the scope corresponding to the type of the object
4905      given by the POSTFIX_EXPRESSION.  */
4906   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4907     {
4908       scope = TREE_TYPE (postfix_expression);
4909       /* According to the standard, no expression should ever have
4910          reference type.  Unfortunately, we do not currently match
4911          the standard in this respect in that our internal representation
4912          of an expression may have reference type even when the standard
4913          says it does not.  Therefore, we have to manually obtain the
4914          underlying type here.  */
4915       scope = non_reference (scope);
4916       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4917       if (scope == unknown_type_node)
4918         {
4919           error ("%H%qE does not have class type", &location, postfix_expression);
4920           scope = NULL_TREE;
4921         }
4922       else
4923         scope = complete_type_or_else (scope, NULL_TREE);
4924       /* Let the name lookup machinery know that we are processing a
4925          class member access expression.  */
4926       parser->context->object_type = scope;
4927       /* If something went wrong, we want to be able to discern that case,
4928          as opposed to the case where there was no SCOPE due to the type
4929          of expression being dependent.  */
4930       if (!scope)
4931         scope = error_mark_node;
4932       /* If the SCOPE was erroneous, make the various semantic analysis
4933          functions exit quickly -- and without issuing additional error
4934          messages.  */
4935       if (scope == error_mark_node)
4936         postfix_expression = error_mark_node;
4937     }
4938
4939   /* Assume this expression is not a pseudo-destructor access.  */
4940   pseudo_destructor_p = false;
4941
4942   /* If the SCOPE is a scalar type, then, if this is a valid program,
4943      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
4944      is type dependent, it can be pseudo-destructor-name or something else.
4945      Try to parse it as pseudo-destructor-name first.  */
4946   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4947     {
4948       tree s;
4949       tree type;
4950
4951       cp_parser_parse_tentatively (parser);
4952       /* Parse the pseudo-destructor-name.  */
4953       s = NULL_TREE;
4954       cp_parser_pseudo_destructor_name (parser, &s, &type);
4955       if (dependent_p
4956           && (cp_parser_error_occurred (parser)
4957               || TREE_CODE (type) != TYPE_DECL
4958               || !SCALAR_TYPE_P (TREE_TYPE (type))))
4959         cp_parser_abort_tentative_parse (parser);
4960       else if (cp_parser_parse_definitely (parser))
4961         {
4962           pseudo_destructor_p = true;
4963           postfix_expression
4964             = finish_pseudo_destructor_expr (postfix_expression,
4965                                              s, TREE_TYPE (type));
4966         }
4967     }
4968
4969   if (!pseudo_destructor_p)
4970     {
4971       /* If the SCOPE is not a scalar type, we are looking at an
4972          ordinary class member access expression, rather than a
4973          pseudo-destructor-name.  */
4974       bool template_p;
4975       cp_token *token = cp_lexer_peek_token (parser->lexer);
4976       /* Parse the id-expression.  */
4977       name = (cp_parser_id_expression
4978               (parser,
4979                cp_parser_optional_template_keyword (parser),
4980                /*check_dependency_p=*/true,
4981                &template_p,
4982                /*declarator_p=*/false,
4983                /*optional_p=*/false));
4984       /* In general, build a SCOPE_REF if the member name is qualified.
4985          However, if the name was not dependent and has already been
4986          resolved; there is no need to build the SCOPE_REF.  For example;
4987
4988              struct X { void f(); };
4989              template <typename T> void f(T* t) { t->X::f(); }
4990
4991          Even though "t" is dependent, "X::f" is not and has been resolved
4992          to a BASELINK; there is no need to include scope information.  */
4993
4994       /* But we do need to remember that there was an explicit scope for
4995          virtual function calls.  */
4996       if (parser->scope)
4997         *idk = CP_ID_KIND_QUALIFIED;
4998
4999       /* If the name is a template-id that names a type, we will get a
5000          TYPE_DECL here.  That is invalid code.  */
5001       if (TREE_CODE (name) == TYPE_DECL)
5002         {
5003           error ("%Hinvalid use of %qD", &token->location, name);
5004           postfix_expression = error_mark_node;
5005         }
5006       else
5007         {
5008           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5009             {
5010               name = build_qualified_name (/*type=*/NULL_TREE,
5011                                            parser->scope,
5012                                            name,
5013                                            template_p);
5014               parser->scope = NULL_TREE;
5015               parser->qualifying_scope = NULL_TREE;
5016               parser->object_scope = NULL_TREE;
5017             }
5018           if (scope && name && BASELINK_P (name))
5019             adjust_result_of_qualified_name_lookup
5020               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5021           postfix_expression
5022             = finish_class_member_access_expr (postfix_expression, name,
5023                                                template_p, 
5024                                                tf_warning_or_error);
5025         }
5026     }
5027
5028   /* We no longer need to look up names in the scope of the object on
5029      the left-hand side of the `.' or `->' operator.  */
5030   parser->context->object_type = NULL_TREE;
5031
5032   /* Outside of offsetof, these operators may not appear in
5033      constant-expressions.  */
5034   if (!for_offsetof
5035       && (cp_parser_non_integral_constant_expression
5036           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5037     postfix_expression = error_mark_node;
5038
5039   return postfix_expression;
5040 }
5041
5042 /* Parse a parenthesized expression-list.
5043
5044    expression-list:
5045      assignment-expression
5046      expression-list, assignment-expression
5047
5048    attribute-list:
5049      expression-list
5050      identifier
5051      identifier, expression-list
5052
5053    CAST_P is true if this expression is the target of a cast.
5054
5055    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5056    argument pack.
5057
5058    Returns a TREE_LIST.  The TREE_VALUE of each node is a
5059    representation of an assignment-expression.  Note that a TREE_LIST
5060    is returned even if there is only a single expression in the list.
5061    error_mark_node is returned if the ( and or ) are
5062    missing. NULL_TREE is returned on no expressions. The parentheses
5063    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
5064    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
5065    indicates whether or not all of the expressions in the list were
5066    constant.  */
5067
5068 static tree
5069 cp_parser_parenthesized_expression_list (cp_parser* parser,
5070                                          bool is_attribute_list,
5071                                          bool cast_p,
5072                                          bool allow_expansion_p,
5073                                          bool *non_constant_p)
5074 {
5075   tree expression_list = NULL_TREE;
5076   bool fold_expr_p = is_attribute_list;
5077   tree identifier = NULL_TREE;
5078   bool saved_greater_than_is_operator_p;
5079
5080   /* Assume all the expressions will be constant.  */
5081   if (non_constant_p)
5082     *non_constant_p = false;
5083
5084   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5085     return error_mark_node;
5086
5087   /* Within a parenthesized expression, a `>' token is always
5088      the greater-than operator.  */
5089   saved_greater_than_is_operator_p
5090     = parser->greater_than_is_operator_p;
5091   parser->greater_than_is_operator_p = true;
5092
5093   /* Consume expressions until there are no more.  */
5094   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5095     while (true)
5096       {
5097         tree expr;
5098
5099         /* At the beginning of attribute lists, check to see if the
5100            next token is an identifier.  */
5101         if (is_attribute_list
5102             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5103           {
5104             cp_token *token;
5105
5106             /* Consume the identifier.  */
5107             token = cp_lexer_consume_token (parser->lexer);
5108             /* Save the identifier.  */
5109             identifier = token->u.value;
5110           }
5111         else
5112           {
5113             bool expr_non_constant_p;
5114
5115             /* Parse the next assignment-expression.  */
5116             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5117               {
5118                 /* A braced-init-list.  */
5119                 maybe_warn_cpp0x ("extended initializer lists");
5120                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5121                 if (non_constant_p && expr_non_constant_p)
5122                   *non_constant_p = true;
5123               }
5124             else if (non_constant_p)
5125               {
5126                 expr = (cp_parser_constant_expression
5127                         (parser, /*allow_non_constant_p=*/true,
5128                          &expr_non_constant_p));
5129                 if (expr_non_constant_p)
5130                   *non_constant_p = true;
5131               }
5132             else
5133               expr = cp_parser_assignment_expression (parser, cast_p);
5134
5135             if (fold_expr_p)
5136               expr = fold_non_dependent_expr (expr);
5137
5138             /* If we have an ellipsis, then this is an expression
5139                expansion.  */
5140             if (allow_expansion_p
5141                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5142               {
5143                 /* Consume the `...'.  */
5144                 cp_lexer_consume_token (parser->lexer);
5145
5146                 /* Build the argument pack.  */
5147                 expr = make_pack_expansion (expr);
5148               }
5149
5150              /* Add it to the list.  We add error_mark_node
5151                 expressions to the list, so that we can still tell if
5152                 the correct form for a parenthesized expression-list
5153                 is found. That gives better errors.  */
5154             expression_list = tree_cons (NULL_TREE, expr, expression_list);
5155
5156             if (expr == error_mark_node)
5157               goto skip_comma;
5158           }
5159
5160         /* After the first item, attribute lists look the same as
5161            expression lists.  */
5162         is_attribute_list = false;
5163
5164       get_comma:;
5165         /* If the next token isn't a `,', then we are done.  */
5166         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5167           break;
5168
5169         /* Otherwise, consume the `,' and keep going.  */
5170         cp_lexer_consume_token (parser->lexer);
5171       }
5172
5173   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5174     {
5175       int ending;
5176
5177     skip_comma:;
5178       /* We try and resync to an unnested comma, as that will give the
5179          user better diagnostics.  */
5180       ending = cp_parser_skip_to_closing_parenthesis (parser,
5181                                                       /*recovering=*/true,
5182                                                       /*or_comma=*/true,
5183                                                       /*consume_paren=*/true);
5184       if (ending < 0)
5185         goto get_comma;
5186       if (!ending)
5187         {
5188           parser->greater_than_is_operator_p
5189             = saved_greater_than_is_operator_p;
5190           return error_mark_node;
5191         }
5192     }
5193
5194   parser->greater_than_is_operator_p
5195     = saved_greater_than_is_operator_p;
5196
5197   /* We built up the list in reverse order so we must reverse it now.  */
5198   expression_list = nreverse (expression_list);
5199   if (identifier)
5200     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5201
5202   return expression_list;
5203 }
5204
5205 /* Parse a pseudo-destructor-name.
5206
5207    pseudo-destructor-name:
5208      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5209      :: [opt] nested-name-specifier template template-id :: ~ type-name
5210      :: [opt] nested-name-specifier [opt] ~ type-name
5211
5212    If either of the first two productions is used, sets *SCOPE to the
5213    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5214    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5215    or ERROR_MARK_NODE if the parse fails.  */
5216
5217 static void
5218 cp_parser_pseudo_destructor_name (cp_parser* parser,
5219                                   tree* scope,
5220                                   tree* type)
5221 {
5222   bool nested_name_specifier_p;
5223
5224   /* Assume that things will not work out.  */
5225   *type = error_mark_node;
5226
5227   /* Look for the optional `::' operator.  */
5228   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5229   /* Look for the optional nested-name-specifier.  */
5230   nested_name_specifier_p
5231     = (cp_parser_nested_name_specifier_opt (parser,
5232                                             /*typename_keyword_p=*/false,
5233                                             /*check_dependency_p=*/true,
5234                                             /*type_p=*/false,
5235                                             /*is_declaration=*/true)
5236        != NULL_TREE);
5237   /* Now, if we saw a nested-name-specifier, we might be doing the
5238      second production.  */
5239   if (nested_name_specifier_p
5240       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5241     {
5242       /* Consume the `template' keyword.  */
5243       cp_lexer_consume_token (parser->lexer);
5244       /* Parse the template-id.  */
5245       cp_parser_template_id (parser,
5246                              /*template_keyword_p=*/true,
5247                              /*check_dependency_p=*/false,
5248                              /*is_declaration=*/true);
5249       /* Look for the `::' token.  */
5250       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5251     }
5252   /* If the next token is not a `~', then there might be some
5253      additional qualification.  */
5254   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5255     {
5256       /* At this point, we're looking for "type-name :: ~".  The type-name
5257          must not be a class-name, since this is a pseudo-destructor.  So,
5258          it must be either an enum-name, or a typedef-name -- both of which
5259          are just identifiers.  So, we peek ahead to check that the "::"
5260          and "~" tokens are present; if they are not, then we can avoid
5261          calling type_name.  */
5262       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5263           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5264           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5265         {
5266           cp_parser_error (parser, "non-scalar type");
5267           return;
5268         }
5269
5270       /* Look for the type-name.  */
5271       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5272       if (*scope == error_mark_node)
5273         return;
5274
5275       /* Look for the `::' token.  */
5276       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5277     }
5278   else
5279     *scope = NULL_TREE;
5280
5281   /* Look for the `~'.  */
5282   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5283   /* Look for the type-name again.  We are not responsible for
5284      checking that it matches the first type-name.  */
5285   *type = cp_parser_nonclass_name (parser);
5286 }
5287
5288 /* Parse a unary-expression.
5289
5290    unary-expression:
5291      postfix-expression
5292      ++ cast-expression
5293      -- cast-expression
5294      unary-operator cast-expression
5295      sizeof unary-expression
5296      sizeof ( type-id )
5297      new-expression
5298      delete-expression
5299
5300    GNU Extensions:
5301
5302    unary-expression:
5303      __extension__ cast-expression
5304      __alignof__ unary-expression
5305      __alignof__ ( type-id )
5306      __real__ cast-expression
5307      __imag__ cast-expression
5308      && identifier
5309
5310    ADDRESS_P is true iff the unary-expression is appearing as the
5311    operand of the `&' operator.   CAST_P is true if this expression is
5312    the target of a cast.
5313
5314    Returns a representation of the expression.  */
5315
5316 static tree
5317 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5318 {
5319   cp_token *token;
5320   enum tree_code unary_operator;
5321
5322   /* Peek at the next token.  */
5323   token = cp_lexer_peek_token (parser->lexer);
5324   /* Some keywords give away the kind of expression.  */
5325   if (token->type == CPP_KEYWORD)
5326     {
5327       enum rid keyword = token->keyword;
5328
5329       switch (keyword)
5330         {
5331         case RID_ALIGNOF:
5332         case RID_SIZEOF:
5333           {
5334             tree operand;
5335             enum tree_code op;
5336
5337             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5338             /* Consume the token.  */
5339             cp_lexer_consume_token (parser->lexer);
5340             /* Parse the operand.  */
5341             operand = cp_parser_sizeof_operand (parser, keyword);
5342
5343             if (TYPE_P (operand))
5344               return cxx_sizeof_or_alignof_type (operand, op, true);
5345             else
5346               return cxx_sizeof_or_alignof_expr (operand, op, true);
5347           }
5348
5349         case RID_NEW:
5350           return cp_parser_new_expression (parser);
5351
5352         case RID_DELETE:
5353           return cp_parser_delete_expression (parser);
5354
5355         case RID_EXTENSION:
5356           {
5357             /* The saved value of the PEDANTIC flag.  */
5358             int saved_pedantic;
5359             tree expr;
5360
5361             /* Save away the PEDANTIC flag.  */
5362             cp_parser_extension_opt (parser, &saved_pedantic);
5363             /* Parse the cast-expression.  */
5364             expr = cp_parser_simple_cast_expression (parser);
5365             /* Restore the PEDANTIC flag.  */
5366             pedantic = saved_pedantic;
5367
5368             return expr;
5369           }
5370
5371         case RID_REALPART:
5372         case RID_IMAGPART:
5373           {
5374             tree expression;
5375
5376             /* Consume the `__real__' or `__imag__' token.  */
5377             cp_lexer_consume_token (parser->lexer);
5378             /* Parse the cast-expression.  */
5379             expression = cp_parser_simple_cast_expression (parser);
5380             /* Create the complete representation.  */
5381             return build_x_unary_op ((keyword == RID_REALPART
5382                                       ? REALPART_EXPR : IMAGPART_EXPR),
5383                                      expression,
5384                                      tf_warning_or_error);
5385           }
5386           break;
5387
5388         default:
5389           break;
5390         }
5391     }
5392
5393   /* Look for the `:: new' and `:: delete', which also signal the
5394      beginning of a new-expression, or delete-expression,
5395      respectively.  If the next token is `::', then it might be one of
5396      these.  */
5397   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5398     {
5399       enum rid keyword;
5400
5401       /* See if the token after the `::' is one of the keywords in
5402          which we're interested.  */
5403       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5404       /* If it's `new', we have a new-expression.  */
5405       if (keyword == RID_NEW)
5406         return cp_parser_new_expression (parser);
5407       /* Similarly, for `delete'.  */
5408       else if (keyword == RID_DELETE)
5409         return cp_parser_delete_expression (parser);
5410     }
5411
5412   /* Look for a unary operator.  */
5413   unary_operator = cp_parser_unary_operator (token);
5414   /* The `++' and `--' operators can be handled similarly, even though
5415      they are not technically unary-operators in the grammar.  */
5416   if (unary_operator == ERROR_MARK)
5417     {
5418       if (token->type == CPP_PLUS_PLUS)
5419         unary_operator = PREINCREMENT_EXPR;
5420       else if (token->type == CPP_MINUS_MINUS)
5421         unary_operator = PREDECREMENT_EXPR;
5422       /* Handle the GNU address-of-label extension.  */
5423       else if (cp_parser_allow_gnu_extensions_p (parser)
5424                && token->type == CPP_AND_AND)
5425         {
5426           tree identifier;
5427           tree expression;
5428
5429           /* Consume the '&&' token.  */
5430           cp_lexer_consume_token (parser->lexer);
5431           /* Look for the identifier.  */
5432           identifier = cp_parser_identifier (parser);
5433           /* Create an expression representing the address.  */
5434           expression = finish_label_address_expr (identifier);
5435           if (cp_parser_non_integral_constant_expression (parser,
5436                                                 "the address of a label"))
5437             expression = error_mark_node;
5438           return expression;
5439         }
5440     }
5441   if (unary_operator != ERROR_MARK)
5442     {
5443       tree cast_expression;
5444       tree expression = error_mark_node;
5445       const char *non_constant_p = NULL;
5446
5447       /* Consume the operator token.  */
5448       token = cp_lexer_consume_token (parser->lexer);
5449       /* Parse the cast-expression.  */
5450       cast_expression
5451         = cp_parser_cast_expression (parser,
5452                                      unary_operator == ADDR_EXPR,
5453                                      /*cast_p=*/false);
5454       /* Now, build an appropriate representation.  */
5455       switch (unary_operator)
5456         {
5457         case INDIRECT_REF:
5458           non_constant_p = "%<*%>";
5459           expression = build_x_indirect_ref (cast_expression, "unary *",
5460                                              tf_warning_or_error);
5461           break;
5462
5463         case ADDR_EXPR:
5464           non_constant_p = "%<&%>";
5465           /* Fall through.  */
5466         case BIT_NOT_EXPR:
5467           expression = build_x_unary_op (unary_operator, cast_expression,
5468                                          tf_warning_or_error);
5469           break;
5470
5471         case PREINCREMENT_EXPR:
5472         case PREDECREMENT_EXPR:
5473           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5474                             ? "%<++%>" : "%<--%>");
5475           /* Fall through.  */
5476         case UNARY_PLUS_EXPR:
5477         case NEGATE_EXPR:
5478         case TRUTH_NOT_EXPR:
5479           expression = finish_unary_op_expr (unary_operator, cast_expression);
5480           break;
5481
5482         default:
5483           gcc_unreachable ();
5484         }
5485
5486       if (non_constant_p
5487           && cp_parser_non_integral_constant_expression (parser,
5488                                                          non_constant_p))
5489         expression = error_mark_node;
5490
5491       return expression;
5492     }
5493
5494   return cp_parser_postfix_expression (parser, address_p, cast_p,
5495                                        /*member_access_only_p=*/false);
5496 }
5497
5498 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5499    unary-operator, the corresponding tree code is returned.  */
5500
5501 static enum tree_code
5502 cp_parser_unary_operator (cp_token* token)
5503 {
5504   switch (token->type)
5505     {
5506     case CPP_MULT:
5507       return INDIRECT_REF;
5508
5509     case CPP_AND:
5510       return ADDR_EXPR;
5511
5512     case CPP_PLUS:
5513       return UNARY_PLUS_EXPR;
5514
5515     case CPP_MINUS:
5516       return NEGATE_EXPR;
5517
5518     case CPP_NOT:
5519       return TRUTH_NOT_EXPR;
5520
5521     case CPP_COMPL:
5522       return BIT_NOT_EXPR;
5523
5524     default:
5525       return ERROR_MARK;
5526     }
5527 }
5528
5529 /* Parse a new-expression.
5530
5531    new-expression:
5532      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5533      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5534
5535    Returns a representation of the expression.  */
5536
5537 static tree
5538 cp_parser_new_expression (cp_parser* parser)
5539 {
5540   bool global_scope_p;
5541   tree placement;
5542   tree type;
5543   tree initializer;
5544   tree nelts;
5545
5546   /* Look for the optional `::' operator.  */
5547   global_scope_p
5548     = (cp_parser_global_scope_opt (parser,
5549                                    /*current_scope_valid_p=*/false)
5550        != NULL_TREE);
5551   /* Look for the `new' operator.  */
5552   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5553   /* There's no easy way to tell a new-placement from the
5554      `( type-id )' construct.  */
5555   cp_parser_parse_tentatively (parser);
5556   /* Look for a new-placement.  */
5557   placement = cp_parser_new_placement (parser);
5558   /* If that didn't work out, there's no new-placement.  */
5559   if (!cp_parser_parse_definitely (parser))
5560     placement = NULL_TREE;
5561
5562   /* If the next token is a `(', then we have a parenthesized
5563      type-id.  */
5564   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5565     {
5566       cp_token *token;
5567       /* Consume the `('.  */
5568       cp_lexer_consume_token (parser->lexer);
5569       /* Parse the type-id.  */
5570       type = cp_parser_type_id (parser);
5571       /* Look for the closing `)'.  */
5572       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5573       token = cp_lexer_peek_token (parser->lexer);
5574       /* There should not be a direct-new-declarator in this production,
5575          but GCC used to allowed this, so we check and emit a sensible error
5576          message for this case.  */
5577       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5578         {
5579           error ("%Harray bound forbidden after parenthesized type-id",
5580                  &token->location);
5581           inform (token->location, 
5582                   "try removing the parentheses around the type-id");
5583           cp_parser_direct_new_declarator (parser);
5584         }
5585       nelts = NULL_TREE;
5586     }
5587   /* Otherwise, there must be a new-type-id.  */
5588   else
5589     type = cp_parser_new_type_id (parser, &nelts);
5590
5591   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5593       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5594     initializer = cp_parser_new_initializer (parser);
5595   else
5596     initializer = NULL_TREE;
5597
5598   /* A new-expression may not appear in an integral constant
5599      expression.  */
5600   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5601     return error_mark_node;
5602
5603   /* Create a representation of the new-expression.  */
5604   return build_new (placement, type, nelts, initializer, global_scope_p,
5605                     tf_warning_or_error);
5606 }
5607
5608 /* Parse a new-placement.
5609
5610    new-placement:
5611      ( expression-list )
5612
5613    Returns the same representation as for an expression-list.  */
5614
5615 static tree
5616 cp_parser_new_placement (cp_parser* parser)
5617 {
5618   tree expression_list;
5619
5620   /* Parse the expression-list.  */
5621   expression_list = (cp_parser_parenthesized_expression_list
5622                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5623                       /*non_constant_p=*/NULL));
5624
5625   return expression_list;
5626 }
5627
5628 /* Parse a new-type-id.
5629
5630    new-type-id:
5631      type-specifier-seq new-declarator [opt]
5632
5633    Returns the TYPE allocated.  If the new-type-id indicates an array
5634    type, *NELTS is set to the number of elements in the last array
5635    bound; the TYPE will not include the last array bound.  */
5636
5637 static tree
5638 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5639 {
5640   cp_decl_specifier_seq type_specifier_seq;
5641   cp_declarator *new_declarator;
5642   cp_declarator *declarator;
5643   cp_declarator *outer_declarator;
5644   const char *saved_message;
5645   tree type;
5646
5647   /* The type-specifier sequence must not contain type definitions.
5648      (It cannot contain declarations of new types either, but if they
5649      are not definitions we will catch that because they are not
5650      complete.)  */
5651   saved_message = parser->type_definition_forbidden_message;
5652   parser->type_definition_forbidden_message
5653     = "types may not be defined in a new-type-id";
5654   /* Parse the type-specifier-seq.  */
5655   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5656                                 &type_specifier_seq);
5657   /* Restore the old message.  */
5658   parser->type_definition_forbidden_message = saved_message;
5659   /* Parse the new-declarator.  */
5660   new_declarator = cp_parser_new_declarator_opt (parser);
5661
5662   /* Determine the number of elements in the last array dimension, if
5663      any.  */
5664   *nelts = NULL_TREE;
5665   /* Skip down to the last array dimension.  */
5666   declarator = new_declarator;
5667   outer_declarator = NULL;
5668   while (declarator && (declarator->kind == cdk_pointer
5669                         || declarator->kind == cdk_ptrmem))
5670     {
5671       outer_declarator = declarator;
5672       declarator = declarator->declarator;
5673     }
5674   while (declarator
5675          && declarator->kind == cdk_array
5676          && declarator->declarator
5677          && declarator->declarator->kind == cdk_array)
5678     {
5679       outer_declarator = declarator;
5680       declarator = declarator->declarator;
5681     }
5682
5683   if (declarator && declarator->kind == cdk_array)
5684     {
5685       *nelts = declarator->u.array.bounds;
5686       if (*nelts == error_mark_node)
5687         *nelts = integer_one_node;
5688
5689       if (outer_declarator)
5690         outer_declarator->declarator = declarator->declarator;
5691       else
5692         new_declarator = NULL;
5693     }
5694
5695   type = groktypename (&type_specifier_seq, new_declarator);
5696   return type;
5697 }
5698
5699 /* Parse an (optional) new-declarator.
5700
5701    new-declarator:
5702      ptr-operator new-declarator [opt]
5703      direct-new-declarator
5704
5705    Returns the declarator.  */
5706
5707 static cp_declarator *
5708 cp_parser_new_declarator_opt (cp_parser* parser)
5709 {
5710   enum tree_code code;
5711   tree type;
5712   cp_cv_quals cv_quals;
5713
5714   /* We don't know if there's a ptr-operator next, or not.  */
5715   cp_parser_parse_tentatively (parser);
5716   /* Look for a ptr-operator.  */
5717   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5718   /* If that worked, look for more new-declarators.  */
5719   if (cp_parser_parse_definitely (parser))
5720     {
5721       cp_declarator *declarator;
5722
5723       /* Parse another optional declarator.  */
5724       declarator = cp_parser_new_declarator_opt (parser);
5725
5726       return cp_parser_make_indirect_declarator
5727         (code, type, cv_quals, declarator);
5728     }
5729
5730   /* If the next token is a `[', there is a direct-new-declarator.  */
5731   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5732     return cp_parser_direct_new_declarator (parser);
5733
5734   return NULL;
5735 }
5736
5737 /* Parse a direct-new-declarator.
5738
5739    direct-new-declarator:
5740      [ expression ]
5741      direct-new-declarator [constant-expression]
5742
5743    */
5744
5745 static cp_declarator *
5746 cp_parser_direct_new_declarator (cp_parser* parser)
5747 {
5748   cp_declarator *declarator = NULL;
5749
5750   while (true)
5751     {
5752       tree expression;
5753
5754       /* Look for the opening `['.  */
5755       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5756       /* The first expression is not required to be constant.  */
5757       if (!declarator)
5758         {
5759           cp_token *token = cp_lexer_peek_token (parser->lexer);
5760           expression = cp_parser_expression (parser, /*cast_p=*/false);
5761           /* The standard requires that the expression have integral
5762              type.  DR 74 adds enumeration types.  We believe that the
5763              real intent is that these expressions be handled like the
5764              expression in a `switch' condition, which also allows
5765              classes with a single conversion to integral or
5766              enumeration type.  */
5767           if (!processing_template_decl)
5768             {
5769               expression
5770                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5771                                               expression,
5772                                               /*complain=*/true);
5773               if (!expression)
5774                 {
5775                   error ("%Hexpression in new-declarator must have integral "
5776                          "or enumeration type", &token->location);
5777                   expression = error_mark_node;
5778                 }
5779             }
5780         }
5781       /* But all the other expressions must be.  */
5782       else
5783         expression
5784           = cp_parser_constant_expression (parser,
5785                                            /*allow_non_constant=*/false,
5786                                            NULL);
5787       /* Look for the closing `]'.  */
5788       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5789
5790       /* Add this bound to the declarator.  */
5791       declarator = make_array_declarator (declarator, expression);
5792
5793       /* If the next token is not a `[', then there are no more
5794          bounds.  */
5795       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5796         break;
5797     }
5798
5799   return declarator;
5800 }
5801
5802 /* Parse a new-initializer.
5803
5804    new-initializer:
5805      ( expression-list [opt] )
5806      braced-init-list
5807
5808    Returns a representation of the expression-list.  If there is no
5809    expression-list, VOID_ZERO_NODE is returned.  */
5810
5811 static tree
5812 cp_parser_new_initializer (cp_parser* parser)
5813 {
5814   tree expression_list;
5815
5816   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5817     {
5818       bool expr_non_constant_p;
5819       maybe_warn_cpp0x ("extended initializer lists");
5820       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
5821       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
5822       expression_list = build_tree_list (NULL_TREE, expression_list);
5823     }
5824   else
5825     expression_list = (cp_parser_parenthesized_expression_list
5826                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5827                         /*non_constant_p=*/NULL));
5828   if (!expression_list)
5829     expression_list = void_zero_node;
5830
5831   return expression_list;
5832 }
5833
5834 /* Parse a delete-expression.
5835
5836    delete-expression:
5837      :: [opt] delete cast-expression
5838      :: [opt] delete [ ] cast-expression
5839
5840    Returns a representation of the expression.  */
5841
5842 static tree
5843 cp_parser_delete_expression (cp_parser* parser)
5844 {
5845   bool global_scope_p;
5846   bool array_p;
5847   tree expression;
5848
5849   /* Look for the optional `::' operator.  */
5850   global_scope_p
5851     = (cp_parser_global_scope_opt (parser,
5852                                    /*current_scope_valid_p=*/false)
5853        != NULL_TREE);
5854   /* Look for the `delete' keyword.  */
5855   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5856   /* See if the array syntax is in use.  */
5857   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5858     {
5859       /* Consume the `[' token.  */
5860       cp_lexer_consume_token (parser->lexer);
5861       /* Look for the `]' token.  */
5862       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5863       /* Remember that this is the `[]' construct.  */
5864       array_p = true;
5865     }
5866   else
5867     array_p = false;
5868
5869   /* Parse the cast-expression.  */
5870   expression = cp_parser_simple_cast_expression (parser);
5871
5872   /* A delete-expression may not appear in an integral constant
5873      expression.  */
5874   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5875     return error_mark_node;
5876
5877   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5878 }
5879
5880 /* Parse a cast-expression.
5881
5882    cast-expression:
5883      unary-expression
5884      ( type-id ) cast-expression
5885
5886    ADDRESS_P is true iff the unary-expression is appearing as the
5887    operand of the `&' operator.   CAST_P is true if this expression is
5888    the target of a cast.
5889
5890    Returns a representation of the expression.  */
5891
5892 static tree
5893 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5894 {
5895   /* If it's a `(', then we might be looking at a cast.  */
5896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5897     {
5898       tree type = NULL_TREE;
5899       tree expr = NULL_TREE;
5900       bool compound_literal_p;
5901       const char *saved_message;
5902
5903       /* There's no way to know yet whether or not this is a cast.
5904          For example, `(int (3))' is a unary-expression, while `(int)
5905          3' is a cast.  So, we resort to parsing tentatively.  */
5906       cp_parser_parse_tentatively (parser);
5907       /* Types may not be defined in a cast.  */
5908       saved_message = parser->type_definition_forbidden_message;
5909       parser->type_definition_forbidden_message
5910         = "types may not be defined in casts";
5911       /* Consume the `('.  */
5912       cp_lexer_consume_token (parser->lexer);
5913       /* A very tricky bit is that `(struct S) { 3 }' is a
5914          compound-literal (which we permit in C++ as an extension).
5915          But, that construct is not a cast-expression -- it is a
5916          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5917          is legal; if the compound-literal were a cast-expression,
5918          you'd need an extra set of parentheses.)  But, if we parse
5919          the type-id, and it happens to be a class-specifier, then we
5920          will commit to the parse at that point, because we cannot
5921          undo the action that is done when creating a new class.  So,
5922          then we cannot back up and do a postfix-expression.
5923
5924          Therefore, we scan ahead to the closing `)', and check to see
5925          if the token after the `)' is a `{'.  If so, we are not
5926          looking at a cast-expression.
5927
5928          Save tokens so that we can put them back.  */
5929       cp_lexer_save_tokens (parser->lexer);
5930       /* Skip tokens until the next token is a closing parenthesis.
5931          If we find the closing `)', and the next token is a `{', then
5932          we are looking at a compound-literal.  */
5933       compound_literal_p
5934         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5935                                                   /*consume_paren=*/true)
5936            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5937       /* Roll back the tokens we skipped.  */
5938       cp_lexer_rollback_tokens (parser->lexer);
5939       /* If we were looking at a compound-literal, simulate an error
5940          so that the call to cp_parser_parse_definitely below will
5941          fail.  */
5942       if (compound_literal_p)
5943         cp_parser_simulate_error (parser);
5944       else
5945         {
5946           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5947           parser->in_type_id_in_expr_p = true;
5948           /* Look for the type-id.  */
5949           type = cp_parser_type_id (parser);
5950           /* Look for the closing `)'.  */
5951           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5952           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5953         }
5954
5955       /* Restore the saved message.  */
5956       parser->type_definition_forbidden_message = saved_message;
5957
5958       /* If ok so far, parse the dependent expression. We cannot be
5959          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5960          ctor of T, but looks like a cast to function returning T
5961          without a dependent expression.  */
5962       if (!cp_parser_error_occurred (parser))
5963         expr = cp_parser_cast_expression (parser,
5964                                           /*address_p=*/false,
5965                                           /*cast_p=*/true);
5966
5967       if (cp_parser_parse_definitely (parser))
5968         {
5969           /* Warn about old-style casts, if so requested.  */
5970           if (warn_old_style_cast
5971               && !in_system_header
5972               && !VOID_TYPE_P (type)
5973               && current_lang_name != lang_name_c)
5974             warning (OPT_Wold_style_cast, "use of old-style cast");
5975
5976           /* Only type conversions to integral or enumeration types
5977              can be used in constant-expressions.  */
5978           if (!cast_valid_in_integral_constant_expression_p (type)
5979               && (cp_parser_non_integral_constant_expression
5980                   (parser,
5981                    "a cast to a type other than an integral or "
5982                    "enumeration type")))
5983             return error_mark_node;
5984
5985           /* Perform the cast.  */
5986           expr = build_c_cast (type, expr);
5987           return expr;
5988         }
5989     }
5990
5991   /* If we get here, then it's not a cast, so it must be a
5992      unary-expression.  */
5993   return cp_parser_unary_expression (parser, address_p, cast_p);
5994 }
5995
5996 /* Parse a binary expression of the general form:
5997
5998    pm-expression:
5999      cast-expression
6000      pm-expression .* cast-expression
6001      pm-expression ->* cast-expression
6002
6003    multiplicative-expression:
6004      pm-expression
6005      multiplicative-expression * pm-expression
6006      multiplicative-expression / pm-expression
6007      multiplicative-expression % pm-expression
6008
6009    additive-expression:
6010      multiplicative-expression
6011      additive-expression + multiplicative-expression
6012      additive-expression - multiplicative-expression
6013
6014    shift-expression:
6015      additive-expression
6016      shift-expression << additive-expression
6017      shift-expression >> additive-expression
6018
6019    relational-expression:
6020      shift-expression
6021      relational-expression < shift-expression
6022      relational-expression > shift-expression
6023      relational-expression <= shift-expression
6024      relational-expression >= shift-expression
6025
6026   GNU Extension:
6027
6028    relational-expression:
6029      relational-expression <? shift-expression
6030      relational-expression >? shift-expression
6031
6032    equality-expression:
6033      relational-expression
6034      equality-expression == relational-expression
6035      equality-expression != relational-expression
6036
6037    and-expression:
6038      equality-expression
6039      and-expression & equality-expression
6040
6041    exclusive-or-expression:
6042      and-expression
6043      exclusive-or-expression ^ and-expression
6044
6045    inclusive-or-expression:
6046      exclusive-or-expression
6047      inclusive-or-expression | exclusive-or-expression
6048
6049    logical-and-expression:
6050      inclusive-or-expression
6051      logical-and-expression && inclusive-or-expression
6052
6053    logical-or-expression:
6054      logical-and-expression
6055      logical-or-expression || logical-and-expression
6056
6057    All these are implemented with a single function like:
6058
6059    binary-expression:
6060      simple-cast-expression
6061      binary-expression <token> binary-expression
6062
6063    CAST_P is true if this expression is the target of a cast.
6064
6065    The binops_by_token map is used to get the tree codes for each <token> type.
6066    binary-expressions are associated according to a precedence table.  */
6067
6068 #define TOKEN_PRECEDENCE(token)                              \
6069 (((token->type == CPP_GREATER                                \
6070    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6071   && !parser->greater_than_is_operator_p)                    \
6072  ? PREC_NOT_OPERATOR                                         \
6073  : binops_by_token[token->type].prec)
6074
6075 static tree
6076 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6077                              enum cp_parser_prec prec)
6078 {
6079   cp_parser_expression_stack stack;
6080   cp_parser_expression_stack_entry *sp = &stack[0];
6081   tree lhs, rhs;
6082   cp_token *token;
6083   enum tree_code tree_type, lhs_type, rhs_type;
6084   enum cp_parser_prec new_prec, lookahead_prec;
6085   bool overloaded_p;
6086
6087   /* Parse the first expression.  */
6088   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
6089   lhs_type = ERROR_MARK;
6090
6091   for (;;)
6092     {
6093       /* Get an operator token.  */
6094       token = cp_lexer_peek_token (parser->lexer);
6095
6096       if (warn_cxx0x_compat
6097           && token->type == CPP_RSHIFT
6098           && !parser->greater_than_is_operator_p)
6099         {
6100           warning (OPT_Wc__0x_compat, 
6101                    "%H%<>>%> operator will be treated as two right angle brackets in C++0x", 
6102                    &token->location);
6103           warning (OPT_Wc__0x_compat, 
6104                    "suggest parentheses around %<>>%> expression");
6105         }
6106
6107       new_prec = TOKEN_PRECEDENCE (token);
6108
6109       /* Popping an entry off the stack means we completed a subexpression:
6110          - either we found a token which is not an operator (`>' where it is not
6111            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6112            will happen repeatedly;
6113          - or, we found an operator which has lower priority.  This is the case
6114            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6115            parsing `3 * 4'.  */
6116       if (new_prec <= prec)
6117         {
6118           if (sp == stack)
6119             break;
6120           else
6121             goto pop;
6122         }
6123
6124      get_rhs:
6125       tree_type = binops_by_token[token->type].tree_type;
6126
6127       /* We used the operator token.  */
6128       cp_lexer_consume_token (parser->lexer);
6129
6130       /* Extract another operand.  It may be the RHS of this expression
6131          or the LHS of a new, higher priority expression.  */
6132       rhs = cp_parser_simple_cast_expression (parser);
6133       rhs_type = ERROR_MARK;
6134
6135       /* Get another operator token.  Look up its precedence to avoid
6136          building a useless (immediately popped) stack entry for common
6137          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6138       token = cp_lexer_peek_token (parser->lexer);
6139       lookahead_prec = TOKEN_PRECEDENCE (token);
6140       if (lookahead_prec > new_prec)
6141         {
6142           /* ... and prepare to parse the RHS of the new, higher priority
6143              expression.  Since precedence levels on the stack are
6144              monotonically increasing, we do not have to care about
6145              stack overflows.  */
6146           sp->prec = prec;
6147           sp->tree_type = tree_type;
6148           sp->lhs = lhs;
6149           sp->lhs_type = lhs_type;
6150           sp++;
6151           lhs = rhs;
6152           lhs_type = rhs_type;
6153           prec = new_prec;
6154           new_prec = lookahead_prec;
6155           goto get_rhs;
6156
6157          pop:
6158           /* If the stack is not empty, we have parsed into LHS the right side
6159              (`4' in the example above) of an expression we had suspended.
6160              We can use the information on the stack to recover the LHS (`3')
6161              from the stack together with the tree code (`MULT_EXPR'), and
6162              the precedence of the higher level subexpression
6163              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6164              which will be used to actually build the additive expression.  */
6165           --sp;
6166           prec = sp->prec;
6167           tree_type = sp->tree_type;
6168           rhs = lhs;
6169           rhs_type = lhs_type;
6170           lhs = sp->lhs;
6171           lhs_type = sp->lhs_type;
6172         }
6173
6174       overloaded_p = false;
6175       lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6176                                &overloaded_p, tf_warning_or_error);
6177       lhs_type = tree_type;
6178
6179       /* If the binary operator required the use of an overloaded operator,
6180          then this expression cannot be an integral constant-expression.
6181          An overloaded operator can be used even if both operands are
6182          otherwise permissible in an integral constant-expression if at
6183          least one of the operands is of enumeration type.  */
6184
6185       if (overloaded_p
6186           && (cp_parser_non_integral_constant_expression
6187               (parser, "calls to overloaded operators")))
6188         return error_mark_node;
6189     }
6190
6191   return lhs;
6192 }
6193
6194
6195 /* Parse the `? expression : assignment-expression' part of a
6196    conditional-expression.  The LOGICAL_OR_EXPR is the
6197    logical-or-expression that started the conditional-expression.
6198    Returns a representation of the entire conditional-expression.
6199
6200    This routine is used by cp_parser_assignment_expression.
6201
6202      ? expression : assignment-expression
6203
6204    GNU Extensions:
6205
6206      ? : assignment-expression */
6207
6208 static tree
6209 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6210 {
6211   tree expr;
6212   tree assignment_expr;
6213
6214   /* Consume the `?' token.  */
6215   cp_lexer_consume_token (parser->lexer);
6216   if (cp_parser_allow_gnu_extensions_p (parser)
6217       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6218     /* Implicit true clause.  */
6219     expr = NULL_TREE;
6220   else
6221     /* Parse the expression.  */
6222     expr = cp_parser_expression (parser, /*cast_p=*/false);
6223
6224   /* The next token should be a `:'.  */
6225   cp_parser_require (parser, CPP_COLON, "%<:%>");
6226   /* Parse the assignment-expression.  */
6227   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6228
6229   /* Build the conditional-expression.  */
6230   return build_x_conditional_expr (logical_or_expr,
6231                                    expr,
6232                                    assignment_expr,
6233                                    tf_warning_or_error);
6234 }
6235
6236 /* Parse an assignment-expression.
6237
6238    assignment-expression:
6239      conditional-expression
6240      logical-or-expression assignment-operator assignment_expression
6241      throw-expression
6242
6243    CAST_P is true if this expression is the target of a cast.
6244
6245    Returns a representation for the expression.  */
6246
6247 static tree
6248 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6249 {
6250   tree expr;
6251
6252   /* If the next token is the `throw' keyword, then we're looking at
6253      a throw-expression.  */
6254   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6255     expr = cp_parser_throw_expression (parser);
6256   /* Otherwise, it must be that we are looking at a
6257      logical-or-expression.  */
6258   else
6259     {
6260       /* Parse the binary expressions (logical-or-expression).  */
6261       expr = cp_parser_binary_expression (parser, cast_p, PREC_NOT_OPERATOR);
6262       /* If the next token is a `?' then we're actually looking at a
6263          conditional-expression.  */
6264       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6265         return cp_parser_question_colon_clause (parser, expr);
6266       else
6267         {
6268           enum tree_code assignment_operator;
6269
6270           /* If it's an assignment-operator, we're using the second
6271              production.  */
6272           assignment_operator
6273             = cp_parser_assignment_operator_opt (parser);
6274           if (assignment_operator != ERROR_MARK)
6275             {
6276               bool non_constant_p;
6277
6278               /* Parse the right-hand side of the assignment.  */
6279               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6280
6281               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6282                 maybe_warn_cpp0x ("extended initializer lists");
6283
6284               /* An assignment may not appear in a
6285                  constant-expression.  */
6286               if (cp_parser_non_integral_constant_expression (parser,
6287                                                               "an assignment"))
6288                 return error_mark_node;
6289               /* Build the assignment expression.  */
6290               expr = build_x_modify_expr (expr,
6291                                           assignment_operator,
6292                                           rhs,
6293                                           tf_warning_or_error);
6294             }
6295         }
6296     }
6297
6298   return expr;
6299 }
6300
6301 /* Parse an (optional) assignment-operator.
6302
6303    assignment-operator: one of
6304      = *= /= %= += -= >>= <<= &= ^= |=
6305
6306    GNU Extension:
6307
6308    assignment-operator: one of
6309      <?= >?=
6310
6311    If the next token is an assignment operator, the corresponding tree
6312    code is returned, and the token is consumed.  For example, for
6313    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6314    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6315    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6316    operator, ERROR_MARK is returned.  */
6317
6318 static enum tree_code
6319 cp_parser_assignment_operator_opt (cp_parser* parser)
6320 {
6321   enum tree_code op;
6322   cp_token *token;
6323
6324   /* Peek at the next token.  */
6325   token = cp_lexer_peek_token (parser->lexer);
6326
6327   switch (token->type)
6328     {
6329     case CPP_EQ:
6330       op = NOP_EXPR;
6331       break;
6332
6333     case CPP_MULT_EQ:
6334       op = MULT_EXPR;
6335       break;
6336
6337     case CPP_DIV_EQ:
6338       op = TRUNC_DIV_EXPR;
6339       break;
6340
6341     case CPP_MOD_EQ:
6342       op = TRUNC_MOD_EXPR;
6343       break;
6344
6345     case CPP_PLUS_EQ:
6346       op = PLUS_EXPR;
6347       break;
6348
6349     case CPP_MINUS_EQ:
6350       op = MINUS_EXPR;
6351       break;
6352
6353     case CPP_RSHIFT_EQ:
6354       op = RSHIFT_EXPR;
6355       break;
6356
6357     case CPP_LSHIFT_EQ:
6358       op = LSHIFT_EXPR;
6359       break;
6360
6361     case CPP_AND_EQ:
6362       op = BIT_AND_EXPR;
6363       break;
6364
6365     case CPP_XOR_EQ:
6366       op = BIT_XOR_EXPR;
6367       break;
6368
6369     case CPP_OR_EQ:
6370       op = BIT_IOR_EXPR;
6371       break;
6372
6373     default:
6374       /* Nothing else is an assignment operator.  */
6375       op = ERROR_MARK;
6376     }
6377
6378   /* If it was an assignment operator, consume it.  */
6379   if (op != ERROR_MARK)
6380     cp_lexer_consume_token (parser->lexer);
6381
6382   return op;
6383 }
6384
6385 /* Parse an expression.
6386
6387    expression:
6388      assignment-expression
6389      expression , assignment-expression
6390
6391    CAST_P is true if this expression is the target of a cast.
6392
6393    Returns a representation of the expression.  */
6394
6395 static tree
6396 cp_parser_expression (cp_parser* parser, bool cast_p)
6397 {
6398   tree expression = NULL_TREE;
6399
6400   while (true)
6401     {
6402       tree assignment_expression;
6403
6404       /* Parse the next assignment-expression.  */
6405       assignment_expression
6406         = cp_parser_assignment_expression (parser, cast_p);
6407       /* If this is the first assignment-expression, we can just
6408          save it away.  */
6409       if (!expression)
6410         expression = assignment_expression;
6411       else
6412         expression = build_x_compound_expr (expression,
6413                                             assignment_expression,
6414                                             tf_warning_or_error);
6415       /* If the next token is not a comma, then we are done with the
6416          expression.  */
6417       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6418         break;
6419       /* Consume the `,'.  */
6420       cp_lexer_consume_token (parser->lexer);
6421       /* A comma operator cannot appear in a constant-expression.  */
6422       if (cp_parser_non_integral_constant_expression (parser,
6423                                                       "a comma operator"))
6424         expression = error_mark_node;
6425     }
6426
6427   return expression;
6428 }
6429
6430 /* Parse a constant-expression.
6431
6432    constant-expression:
6433      conditional-expression
6434
6435   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6436   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6437   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6438   is false, NON_CONSTANT_P should be NULL.  */
6439
6440 static tree
6441 cp_parser_constant_expression (cp_parser* parser,
6442                                bool allow_non_constant_p,
6443                                bool *non_constant_p)
6444 {
6445   bool saved_integral_constant_expression_p;
6446   bool saved_allow_non_integral_constant_expression_p;
6447   bool saved_non_integral_constant_expression_p;
6448   tree expression;
6449
6450   /* It might seem that we could simply parse the
6451      conditional-expression, and then check to see if it were
6452      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6453      one that the compiler can figure out is constant, possibly after
6454      doing some simplifications or optimizations.  The standard has a
6455      precise definition of constant-expression, and we must honor
6456      that, even though it is somewhat more restrictive.
6457
6458      For example:
6459
6460        int i[(2, 3)];
6461
6462      is not a legal declaration, because `(2, 3)' is not a
6463      constant-expression.  The `,' operator is forbidden in a
6464      constant-expression.  However, GCC's constant-folding machinery
6465      will fold this operation to an INTEGER_CST for `3'.  */
6466
6467   /* Save the old settings.  */
6468   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6469   saved_allow_non_integral_constant_expression_p
6470     = parser->allow_non_integral_constant_expression_p;
6471   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6472   /* We are now parsing a constant-expression.  */
6473   parser->integral_constant_expression_p = true;
6474   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6475   parser->non_integral_constant_expression_p = false;
6476   /* Although the grammar says "conditional-expression", we parse an
6477      "assignment-expression", which also permits "throw-expression"
6478      and the use of assignment operators.  In the case that
6479      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6480      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6481      actually essential that we look for an assignment-expression.
6482      For example, cp_parser_initializer_clauses uses this function to
6483      determine whether a particular assignment-expression is in fact
6484      constant.  */
6485   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6486   /* Restore the old settings.  */
6487   parser->integral_constant_expression_p
6488     = saved_integral_constant_expression_p;
6489   parser->allow_non_integral_constant_expression_p
6490     = saved_allow_non_integral_constant_expression_p;
6491   if (allow_non_constant_p)
6492     *non_constant_p = parser->non_integral_constant_expression_p;
6493   else if (parser->non_integral_constant_expression_p)
6494     expression = error_mark_node;
6495   parser->non_integral_constant_expression_p
6496     = saved_non_integral_constant_expression_p;
6497
6498   return expression;
6499 }
6500
6501 /* Parse __builtin_offsetof.
6502
6503    offsetof-expression:
6504      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6505
6506    offsetof-member-designator:
6507      id-expression
6508      | offsetof-member-designator "." id-expression
6509      | offsetof-member-designator "[" expression "]"  */
6510
6511 static tree
6512 cp_parser_builtin_offsetof (cp_parser *parser)
6513 {
6514   int save_ice_p, save_non_ice_p;
6515   tree type, expr;
6516   cp_id_kind dummy;
6517   cp_token *token;
6518
6519   /* We're about to accept non-integral-constant things, but will
6520      definitely yield an integral constant expression.  Save and
6521      restore these values around our local parsing.  */
6522   save_ice_p = parser->integral_constant_expression_p;
6523   save_non_ice_p = parser->non_integral_constant_expression_p;
6524
6525   /* Consume the "__builtin_offsetof" token.  */
6526   cp_lexer_consume_token (parser->lexer);
6527   /* Consume the opening `('.  */
6528   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6529   /* Parse the type-id.  */
6530   type = cp_parser_type_id (parser);
6531   /* Look for the `,'.  */
6532   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6533   token = cp_lexer_peek_token (parser->lexer);
6534
6535   /* Build the (type *)null that begins the traditional offsetof macro.  */
6536   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6537                             tf_warning_or_error);
6538
6539   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6540   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6541                                                  true, &dummy, token->location);
6542   while (true)
6543     {
6544       token = cp_lexer_peek_token (parser->lexer);
6545       switch (token->type)
6546         {
6547         case CPP_OPEN_SQUARE:
6548           /* offsetof-member-designator "[" expression "]" */
6549           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6550           break;
6551
6552         case CPP_DOT:
6553           /* offsetof-member-designator "." identifier */
6554           cp_lexer_consume_token (parser->lexer);
6555           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6556                                                          true, &dummy,
6557                                                          token->location);
6558           break;
6559
6560         case CPP_CLOSE_PAREN:
6561           /* Consume the ")" token.  */
6562           cp_lexer_consume_token (parser->lexer);
6563           goto success;
6564
6565         default:
6566           /* Error.  We know the following require will fail, but
6567              that gives the proper error message.  */
6568           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6569           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6570           expr = error_mark_node;
6571           goto failure;
6572         }
6573     }
6574
6575  success:
6576   /* If we're processing a template, we can't finish the semantics yet.
6577      Otherwise we can fold the entire expression now.  */
6578   if (processing_template_decl)
6579     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6580   else
6581     expr = finish_offsetof (expr);
6582
6583  failure:
6584   parser->integral_constant_expression_p = save_ice_p;
6585   parser->non_integral_constant_expression_p = save_non_ice_p;
6586
6587   return expr;
6588 }
6589
6590 /* Parse a trait expression.  */
6591
6592 static tree
6593 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6594 {
6595   cp_trait_kind kind;
6596   tree type1, type2 = NULL_TREE;
6597   bool binary = false;
6598   cp_decl_specifier_seq decl_specs;
6599
6600   switch (keyword)
6601     {
6602     case RID_HAS_NOTHROW_ASSIGN:
6603       kind = CPTK_HAS_NOTHROW_ASSIGN;
6604       break;
6605     case RID_HAS_NOTHROW_CONSTRUCTOR:
6606       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6607       break;
6608     case RID_HAS_NOTHROW_COPY:
6609       kind = CPTK_HAS_NOTHROW_COPY;
6610       break;
6611     case RID_HAS_TRIVIAL_ASSIGN:
6612       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6613       break;
6614     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6615       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6616       break;
6617     case RID_HAS_TRIVIAL_COPY:
6618       kind = CPTK_HAS_TRIVIAL_COPY;
6619       break;
6620     case RID_HAS_TRIVIAL_DESTRUCTOR:
6621       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6622       break;
6623     case RID_HAS_VIRTUAL_DESTRUCTOR:
6624       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6625       break;
6626     case RID_IS_ABSTRACT:
6627       kind = CPTK_IS_ABSTRACT;
6628       break;
6629     case RID_IS_BASE_OF:
6630       kind = CPTK_IS_BASE_OF;
6631       binary = true;
6632       break;
6633     case RID_IS_CLASS:
6634       kind = CPTK_IS_CLASS;
6635       break;
6636     case RID_IS_CONVERTIBLE_TO:
6637       kind = CPTK_IS_CONVERTIBLE_TO;
6638       binary = true;
6639       break;
6640     case RID_IS_EMPTY:
6641       kind = CPTK_IS_EMPTY;
6642       break;
6643     case RID_IS_ENUM:
6644       kind = CPTK_IS_ENUM;
6645       break;
6646     case RID_IS_POD:
6647       kind = CPTK_IS_POD;
6648       break;
6649     case RID_IS_POLYMORPHIC:
6650       kind = CPTK_IS_POLYMORPHIC;
6651       break;
6652     case RID_IS_UNION:
6653       kind = CPTK_IS_UNION;
6654       break;
6655     default:
6656       gcc_unreachable ();
6657     }
6658
6659   /* Consume the token.  */
6660   cp_lexer_consume_token (parser->lexer);
6661
6662   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6663
6664   type1 = cp_parser_type_id (parser);
6665
6666   if (type1 == error_mark_node)
6667     return error_mark_node;
6668
6669   /* Build a trivial decl-specifier-seq.  */
6670   clear_decl_specs (&decl_specs);
6671   decl_specs.type = type1;
6672
6673   /* Call grokdeclarator to figure out what type this is.  */
6674   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6675                           /*initialized=*/0, /*attrlist=*/NULL);
6676
6677   if (binary)
6678     {
6679       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6680  
6681       type2 = cp_parser_type_id (parser);
6682
6683       if (type2 == error_mark_node)
6684         return error_mark_node;
6685
6686       /* Build a trivial decl-specifier-seq.  */
6687       clear_decl_specs (&decl_specs);
6688       decl_specs.type = type2;
6689
6690       /* Call grokdeclarator to figure out what type this is.  */
6691       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6692                               /*initialized=*/0, /*attrlist=*/NULL);
6693     }
6694
6695   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6696
6697   /* Complete the trait expression, which may mean either processing
6698      the trait expr now or saving it for template instantiation.  */
6699   return finish_trait_expr (kind, type1, type2);
6700 }
6701
6702 /* Statements [gram.stmt.stmt]  */
6703
6704 /* Parse a statement.
6705
6706    statement:
6707      labeled-statement
6708      expression-statement
6709      compound-statement
6710      selection-statement
6711      iteration-statement
6712      jump-statement
6713      declaration-statement
6714      try-block
6715
6716   IN_COMPOUND is true when the statement is nested inside a
6717   cp_parser_compound_statement; this matters for certain pragmas.
6718
6719   If IF_P is not NULL, *IF_P is set to indicate whether the statement
6720   is a (possibly labeled) if statement which is not enclosed in braces
6721   and has an else clause.  This is used to implement -Wparentheses.  */
6722
6723 static void
6724 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6725                      bool in_compound, bool *if_p)
6726 {
6727   tree statement;
6728   cp_token *token;
6729   location_t statement_location;
6730
6731  restart:
6732   if (if_p != NULL)
6733     *if_p = false;
6734   /* There is no statement yet.  */
6735   statement = NULL_TREE;
6736   /* Peek at the next token.  */
6737   token = cp_lexer_peek_token (parser->lexer);
6738   /* Remember the location of the first token in the statement.  */
6739   statement_location = token->location;
6740   /* If this is a keyword, then that will often determine what kind of
6741      statement we have.  */
6742   if (token->type == CPP_KEYWORD)
6743     {
6744       enum rid keyword = token->keyword;
6745
6746       switch (keyword)
6747         {
6748         case RID_CASE:
6749         case RID_DEFAULT:
6750           /* Looks like a labeled-statement with a case label.
6751              Parse the label, and then use tail recursion to parse
6752              the statement.  */
6753           cp_parser_label_for_labeled_statement (parser);
6754           goto restart;
6755
6756         case RID_IF:
6757         case RID_SWITCH:
6758           statement = cp_parser_selection_statement (parser, if_p);
6759           break;
6760
6761         case RID_WHILE:
6762         case RID_DO:
6763         case RID_FOR:
6764           statement = cp_parser_iteration_statement (parser);
6765           break;
6766
6767         case RID_BREAK:
6768         case RID_CONTINUE:
6769         case RID_RETURN:
6770         case RID_GOTO:
6771           statement = cp_parser_jump_statement (parser);
6772           break;
6773
6774           /* Objective-C++ exception-handling constructs.  */
6775         case RID_AT_TRY:
6776         case RID_AT_CATCH:
6777         case RID_AT_FINALLY:
6778         case RID_AT_SYNCHRONIZED:
6779         case RID_AT_THROW:
6780           statement = cp_parser_objc_statement (parser);
6781           break;
6782
6783         case RID_TRY:
6784           statement = cp_parser_try_block (parser);
6785           break;
6786
6787         case RID_NAMESPACE:
6788           /* This must be a namespace alias definition.  */
6789           cp_parser_declaration_statement (parser);
6790           return;
6791           
6792         default:
6793           /* It might be a keyword like `int' that can start a
6794              declaration-statement.  */
6795           break;
6796         }
6797     }
6798   else if (token->type == CPP_NAME)
6799     {
6800       /* If the next token is a `:', then we are looking at a
6801          labeled-statement.  */
6802       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6803       if (token->type == CPP_COLON)
6804         {
6805           /* Looks like a labeled-statement with an ordinary label.
6806              Parse the label, and then use tail recursion to parse
6807              the statement.  */
6808           cp_parser_label_for_labeled_statement (parser);
6809           goto restart;
6810         }
6811     }
6812   /* Anything that starts with a `{' must be a compound-statement.  */
6813   else if (token->type == CPP_OPEN_BRACE)
6814     statement = cp_parser_compound_statement (parser, NULL, false);
6815   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6816      a statement all its own.  */
6817   else if (token->type == CPP_PRAGMA)
6818     {
6819       /* Only certain OpenMP pragmas are attached to statements, and thus
6820          are considered statements themselves.  All others are not.  In
6821          the context of a compound, accept the pragma as a "statement" and
6822          return so that we can check for a close brace.  Otherwise we
6823          require a real statement and must go back and read one.  */
6824       if (in_compound)
6825         cp_parser_pragma (parser, pragma_compound);
6826       else if (!cp_parser_pragma (parser, pragma_stmt))
6827         goto restart;
6828       return;
6829     }
6830   else if (token->type == CPP_EOF)
6831     {
6832       cp_parser_error (parser, "expected statement");
6833       return;
6834     }
6835
6836   /* Everything else must be a declaration-statement or an
6837      expression-statement.  Try for the declaration-statement
6838      first, unless we are looking at a `;', in which case we know that
6839      we have an expression-statement.  */
6840   if (!statement)
6841     {
6842       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6843         {
6844           cp_parser_parse_tentatively (parser);
6845           /* Try to parse the declaration-statement.  */
6846           cp_parser_declaration_statement (parser);
6847           /* If that worked, we're done.  */
6848           if (cp_parser_parse_definitely (parser))
6849             return;
6850         }
6851       /* Look for an expression-statement instead.  */
6852       statement = cp_parser_expression_statement (parser, in_statement_expr);
6853     }
6854
6855   /* Set the line number for the statement.  */
6856   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6857     SET_EXPR_LOCATION (statement, statement_location);
6858 }
6859
6860 /* Parse the label for a labeled-statement, i.e.
6861
6862    identifier :
6863    case constant-expression :
6864    default :
6865
6866    GNU Extension:
6867    case constant-expression ... constant-expression : statement
6868
6869    When a label is parsed without errors, the label is added to the
6870    parse tree by the finish_* functions, so this function doesn't
6871    have to return the label.  */
6872
6873 static void
6874 cp_parser_label_for_labeled_statement (cp_parser* parser)
6875 {
6876   cp_token *token;
6877
6878   /* The next token should be an identifier.  */
6879   token = cp_lexer_peek_token (parser->lexer);
6880   if (token->type != CPP_NAME
6881       && token->type != CPP_KEYWORD)
6882     {
6883       cp_parser_error (parser, "expected labeled-statement");
6884       return;
6885     }
6886
6887   switch (token->keyword)
6888     {
6889     case RID_CASE:
6890       {
6891         tree expr, expr_hi;
6892         cp_token *ellipsis;
6893
6894         /* Consume the `case' token.  */
6895         cp_lexer_consume_token (parser->lexer);
6896         /* Parse the constant-expression.  */
6897         expr = cp_parser_constant_expression (parser,
6898                                               /*allow_non_constant_p=*/false,
6899                                               NULL);
6900
6901         ellipsis = cp_lexer_peek_token (parser->lexer);
6902         if (ellipsis->type == CPP_ELLIPSIS)
6903           {
6904             /* Consume the `...' token.  */
6905             cp_lexer_consume_token (parser->lexer);
6906             expr_hi =
6907               cp_parser_constant_expression (parser,
6908                                              /*allow_non_constant_p=*/false,
6909                                              NULL);
6910             /* We don't need to emit warnings here, as the common code
6911                will do this for us.  */
6912           }
6913         else
6914           expr_hi = NULL_TREE;
6915
6916         if (parser->in_switch_statement_p)
6917           finish_case_label (expr, expr_hi);
6918         else
6919           error ("%Hcase label %qE not within a switch statement",
6920                  &token->location, expr);
6921       }
6922       break;
6923
6924     case RID_DEFAULT:
6925       /* Consume the `default' token.  */
6926       cp_lexer_consume_token (parser->lexer);
6927
6928       if (parser->in_switch_statement_p)
6929         finish_case_label (NULL_TREE, NULL_TREE);
6930       else
6931         error ("%Hcase label not within a switch statement", &token->location);
6932       break;
6933
6934     default:
6935       /* Anything else must be an ordinary label.  */
6936       finish_label_stmt (cp_parser_identifier (parser));
6937       break;
6938     }
6939
6940   /* Require the `:' token.  */
6941   cp_parser_require (parser, CPP_COLON, "%<:%>");
6942 }
6943
6944 /* Parse an expression-statement.
6945
6946    expression-statement:
6947      expression [opt] ;
6948
6949    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6950    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6951    indicates whether this expression-statement is part of an
6952    expression statement.  */
6953
6954 static tree
6955 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6956 {
6957   tree statement = NULL_TREE;
6958
6959   /* If the next token is a ';', then there is no expression
6960      statement.  */
6961   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6962     statement = cp_parser_expression (parser, /*cast_p=*/false);
6963
6964   /* Consume the final `;'.  */
6965   cp_parser_consume_semicolon_at_end_of_statement (parser);
6966
6967   if (in_statement_expr
6968       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6969     /* This is the final expression statement of a statement
6970        expression.  */
6971     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6972   else if (statement)
6973     statement = finish_expr_stmt (statement);
6974   else
6975     finish_stmt ();
6976
6977   return statement;
6978 }
6979
6980 /* Parse a compound-statement.
6981
6982    compound-statement:
6983      { statement-seq [opt] }
6984
6985    GNU extension:
6986
6987    compound-statement:
6988      { label-declaration-seq [opt] statement-seq [opt] }
6989
6990    label-declaration-seq:
6991      label-declaration
6992      label-declaration-seq label-declaration
6993
6994    Returns a tree representing the statement.  */
6995
6996 static tree
6997 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6998                               bool in_try)
6999 {
7000   tree compound_stmt;
7001
7002   /* Consume the `{'.  */
7003   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7004     return error_mark_node;
7005   /* Begin the compound-statement.  */
7006   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7007   /* If the next keyword is `__label__' we have a label declaration.  */
7008   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7009     cp_parser_label_declaration (parser);
7010   /* Parse an (optional) statement-seq.  */
7011   cp_parser_statement_seq_opt (parser, in_statement_expr);
7012   /* Finish the compound-statement.  */
7013   finish_compound_stmt (compound_stmt);
7014   /* Consume the `}'.  */
7015   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7016
7017   return compound_stmt;
7018 }
7019
7020 /* Parse an (optional) statement-seq.
7021
7022    statement-seq:
7023      statement
7024      statement-seq [opt] statement  */
7025
7026 static void
7027 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7028 {
7029   /* Scan statements until there aren't any more.  */
7030   while (true)
7031     {
7032       cp_token *token = cp_lexer_peek_token (parser->lexer);
7033
7034       /* If we're looking at a `}', then we've run out of statements.  */
7035       if (token->type == CPP_CLOSE_BRACE
7036           || token->type == CPP_EOF
7037           || token->type == CPP_PRAGMA_EOL)
7038         break;
7039       
7040       /* If we are in a compound statement and find 'else' then
7041          something went wrong.  */
7042       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7043         {
7044           if (parser->in_statement & IN_IF_STMT) 
7045             break;
7046           else
7047             {
7048               token = cp_lexer_consume_token (parser->lexer);
7049               error ("%H%<else%> without a previous %<if%>", &token->location);
7050             }
7051         }
7052
7053       /* Parse the statement.  */
7054       cp_parser_statement (parser, in_statement_expr, true, NULL);
7055     }
7056 }
7057
7058 /* Parse a selection-statement.
7059
7060    selection-statement:
7061      if ( condition ) statement
7062      if ( condition ) statement else statement
7063      switch ( condition ) statement
7064
7065    Returns the new IF_STMT or SWITCH_STMT.
7066
7067    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7068    is a (possibly labeled) if statement which is not enclosed in
7069    braces and has an else clause.  This is used to implement
7070    -Wparentheses.  */
7071
7072 static tree
7073 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7074 {
7075   cp_token *token;
7076   enum rid keyword;
7077
7078   if (if_p != NULL)
7079     *if_p = false;
7080
7081   /* Peek at the next token.  */
7082   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7083
7084   /* See what kind of keyword it is.  */
7085   keyword = token->keyword;
7086   switch (keyword)
7087     {
7088     case RID_IF:
7089     case RID_SWITCH:
7090       {
7091         tree statement;
7092         tree condition;
7093
7094         /* Look for the `('.  */
7095         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7096           {
7097             cp_parser_skip_to_end_of_statement (parser);
7098             return error_mark_node;
7099           }
7100
7101         /* Begin the selection-statement.  */
7102         if (keyword == RID_IF)
7103           statement = begin_if_stmt ();
7104         else
7105           statement = begin_switch_stmt ();
7106
7107         /* Parse the condition.  */
7108         condition = cp_parser_condition (parser);
7109         /* Look for the `)'.  */
7110         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7111           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7112                                                  /*consume_paren=*/true);
7113
7114         if (keyword == RID_IF)
7115           {
7116             bool nested_if;
7117             unsigned char in_statement;
7118
7119             /* Add the condition.  */
7120             finish_if_stmt_cond (condition, statement);
7121
7122             /* Parse the then-clause.  */
7123             in_statement = parser->in_statement;
7124             parser->in_statement |= IN_IF_STMT;
7125             cp_parser_implicitly_scoped_statement (parser, &nested_if);
7126             parser->in_statement = in_statement;
7127
7128             finish_then_clause (statement);
7129
7130             /* If the next token is `else', parse the else-clause.  */
7131             if (cp_lexer_next_token_is_keyword (parser->lexer,
7132                                                 RID_ELSE))
7133               {
7134                 /* Consume the `else' keyword.  */
7135                 cp_lexer_consume_token (parser->lexer);
7136                 begin_else_clause (statement);
7137                 /* Parse the else-clause.  */
7138                 cp_parser_implicitly_scoped_statement (parser, NULL);
7139                 finish_else_clause (statement);
7140
7141                 /* If we are currently parsing a then-clause, then
7142                    IF_P will not be NULL.  We set it to true to
7143                    indicate that this if statement has an else clause.
7144                    This may trigger the Wparentheses warning below
7145                    when we get back up to the parent if statement.  */
7146                 if (if_p != NULL)
7147                   *if_p = true;
7148               }
7149             else
7150               {
7151                 /* This if statement does not have an else clause.  If
7152                    NESTED_IF is true, then the then-clause is an if
7153                    statement which does have an else clause.  We warn
7154                    about the potential ambiguity.  */
7155                 if (nested_if)
7156                   warning (OPT_Wparentheses,
7157                            ("%Hsuggest explicit braces "
7158                             "to avoid ambiguous %<else%>"),
7159                            EXPR_LOCUS (statement));
7160               }
7161
7162             /* Now we're all done with the if-statement.  */
7163             finish_if_stmt (statement);
7164           }
7165         else
7166           {
7167             bool in_switch_statement_p;
7168             unsigned char in_statement;
7169
7170             /* Add the condition.  */
7171             finish_switch_cond (condition, statement);
7172
7173             /* Parse the body of the switch-statement.  */
7174             in_switch_statement_p = parser->in_switch_statement_p;
7175             in_statement = parser->in_statement;
7176             parser->in_switch_statement_p = true;
7177             parser->in_statement |= IN_SWITCH_STMT;
7178             cp_parser_implicitly_scoped_statement (parser, NULL);
7179             parser->in_switch_statement_p = in_switch_statement_p;
7180             parser->in_statement = in_statement;
7181
7182             /* Now we're all done with the switch-statement.  */
7183             finish_switch_stmt (statement);
7184           }
7185
7186         return statement;
7187       }
7188       break;
7189
7190     default:
7191       cp_parser_error (parser, "expected selection-statement");
7192       return error_mark_node;
7193     }
7194 }
7195
7196 /* Parse a condition.
7197
7198    condition:
7199      expression
7200      type-specifier-seq declarator = initializer-clause
7201      type-specifier-seq declarator braced-init-list
7202
7203    GNU Extension:
7204
7205    condition:
7206      type-specifier-seq declarator asm-specification [opt]
7207        attributes [opt] = assignment-expression
7208
7209    Returns the expression that should be tested.  */
7210
7211 static tree
7212 cp_parser_condition (cp_parser* parser)
7213 {
7214   cp_decl_specifier_seq type_specifiers;
7215   const char *saved_message;
7216
7217   /* Try the declaration first.  */
7218   cp_parser_parse_tentatively (parser);
7219   /* New types are not allowed in the type-specifier-seq for a
7220      condition.  */
7221   saved_message = parser->type_definition_forbidden_message;
7222   parser->type_definition_forbidden_message
7223     = "types may not be defined in conditions";
7224   /* Parse the type-specifier-seq.  */
7225   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7226                                 &type_specifiers);
7227   /* Restore the saved message.  */
7228   parser->type_definition_forbidden_message = saved_message;
7229   /* If all is well, we might be looking at a declaration.  */
7230   if (!cp_parser_error_occurred (parser))
7231     {
7232       tree decl;
7233       tree asm_specification;
7234       tree attributes;
7235       cp_declarator *declarator;
7236       tree initializer = NULL_TREE;
7237
7238       /* Parse the declarator.  */
7239       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7240                                          /*ctor_dtor_or_conv_p=*/NULL,
7241                                          /*parenthesized_p=*/NULL,
7242                                          /*member_p=*/false);
7243       /* Parse the attributes.  */
7244       attributes = cp_parser_attributes_opt (parser);
7245       /* Parse the asm-specification.  */
7246       asm_specification = cp_parser_asm_specification_opt (parser);
7247       /* If the next token is not an `=' or '{', then we might still be
7248          looking at an expression.  For example:
7249
7250            if (A(a).x)
7251
7252          looks like a decl-specifier-seq and a declarator -- but then
7253          there is no `=', so this is an expression.  */
7254       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7255           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7256         cp_parser_simulate_error (parser);
7257         
7258       /* If we did see an `=' or '{', then we are looking at a declaration
7259          for sure.  */
7260       if (cp_parser_parse_definitely (parser))
7261         {
7262           tree pushed_scope;
7263           bool non_constant_p;
7264           bool flags = LOOKUP_ONLYCONVERTING;
7265
7266           /* Create the declaration.  */
7267           decl = start_decl (declarator, &type_specifiers,
7268                              /*initialized_p=*/true,
7269                              attributes, /*prefix_attributes=*/NULL_TREE,
7270                              &pushed_scope);
7271
7272           /* Parse the initializer.  */
7273           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7274             {
7275               initializer = cp_parser_braced_list (parser, &non_constant_p);
7276               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7277               flags = 0;
7278             }
7279           else
7280             {
7281               /* Consume the `='.  */
7282               cp_lexer_consume_token (parser->lexer);
7283               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7284             }
7285           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7286             maybe_warn_cpp0x ("extended initializer lists");
7287
7288           if (!non_constant_p)
7289             initializer = fold_non_dependent_expr (initializer);
7290
7291           /* Process the initializer.  */
7292           cp_finish_decl (decl,
7293                           initializer, !non_constant_p,
7294                           asm_specification,
7295                           flags);
7296
7297           if (pushed_scope)
7298             pop_scope (pushed_scope);
7299
7300           return convert_from_reference (decl);
7301         }
7302     }
7303   /* If we didn't even get past the declarator successfully, we are
7304      definitely not looking at a declaration.  */
7305   else
7306     cp_parser_abort_tentative_parse (parser);
7307
7308   /* Otherwise, we are looking at an expression.  */
7309   return cp_parser_expression (parser, /*cast_p=*/false);
7310 }
7311
7312 /* We check for a ) immediately followed by ; with no whitespacing
7313    between.  This is used to issue a warning for:
7314
7315      while (...);
7316
7317    and:
7318
7319      for (...);
7320
7321    as the semicolon is probably extraneous.
7322
7323    On parse errors, the next token might not be a ), so do nothing in
7324    that case. */
7325
7326 static void
7327 check_empty_body (cp_parser* parser, const char* type)
7328 {
7329   cp_token *token;
7330   cp_token *close_paren;
7331   expanded_location close_loc;
7332   expanded_location semi_loc;
7333   
7334   close_paren = cp_lexer_peek_token (parser->lexer);
7335   if (close_paren->type != CPP_CLOSE_PAREN)
7336     return;
7337
7338   close_loc = expand_location (close_paren->location);
7339   token = cp_lexer_peek_nth_token (parser->lexer, 2);
7340
7341   if (token->type != CPP_SEMICOLON
7342       || (token->flags & PREV_WHITE))
7343     return;
7344
7345   semi_loc =  expand_location (token->location);
7346   if (close_loc.line == semi_loc.line
7347       && close_loc.column+1 == semi_loc.column)
7348     warning (OPT_Wempty_body,
7349              "suggest a space before %<;%> or explicit braces around empty "
7350              "body in %<%s%> statement",
7351              type);
7352 }
7353
7354 /* Parse an iteration-statement.
7355
7356    iteration-statement:
7357      while ( condition ) statement
7358      do statement while ( expression ) ;
7359      for ( for-init-statement condition [opt] ; expression [opt] )
7360        statement
7361
7362    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
7363
7364 static tree
7365 cp_parser_iteration_statement (cp_parser* parser)
7366 {
7367   cp_token *token;
7368   enum rid keyword;
7369   tree statement;
7370   unsigned char in_statement;
7371
7372   /* Peek at the next token.  */
7373   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7374   if (!token)
7375     return error_mark_node;
7376
7377   /* Remember whether or not we are already within an iteration
7378      statement.  */
7379   in_statement = parser->in_statement;
7380
7381   /* See what kind of keyword it is.  */
7382   keyword = token->keyword;
7383   switch (keyword)
7384     {
7385     case RID_WHILE:
7386       {
7387         tree condition;
7388
7389         /* Begin the while-statement.  */
7390         statement = begin_while_stmt ();
7391         /* Look for the `('.  */
7392         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7393         /* Parse the condition.  */
7394         condition = cp_parser_condition (parser);
7395         finish_while_stmt_cond (condition, statement);
7396         check_empty_body (parser, "while");
7397         /* Look for the `)'.  */
7398         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7399         /* Parse the dependent statement.  */
7400         parser->in_statement = IN_ITERATION_STMT;
7401         cp_parser_already_scoped_statement (parser);
7402         parser->in_statement = in_statement;
7403         /* We're done with the while-statement.  */
7404         finish_while_stmt (statement);
7405       }
7406       break;
7407
7408     case RID_DO:
7409       {
7410         tree expression;
7411
7412         /* Begin the do-statement.  */
7413         statement = begin_do_stmt ();
7414         /* Parse the body of the do-statement.  */
7415         parser->in_statement = IN_ITERATION_STMT;
7416         cp_parser_implicitly_scoped_statement (parser, NULL);
7417         parser->in_statement = in_statement;
7418         finish_do_body (statement);
7419         /* Look for the `while' keyword.  */
7420         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7421         /* Look for the `('.  */
7422         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7423         /* Parse the expression.  */
7424         expression = cp_parser_expression (parser, /*cast_p=*/false);
7425         /* We're done with the do-statement.  */
7426         finish_do_stmt (expression, statement);
7427         /* Look for the `)'.  */
7428         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7429         /* Look for the `;'.  */
7430         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7431       }
7432       break;
7433
7434     case RID_FOR:
7435       {
7436         tree condition = NULL_TREE;
7437         tree expression = NULL_TREE;
7438
7439         /* Begin the for-statement.  */
7440         statement = begin_for_stmt ();
7441         /* Look for the `('.  */
7442         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7443         /* Parse the initialization.  */
7444         cp_parser_for_init_statement (parser);
7445         finish_for_init_stmt (statement);
7446
7447         /* If there's a condition, process it.  */
7448         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7449           condition = cp_parser_condition (parser);
7450         finish_for_cond (condition, statement);
7451         /* Look for the `;'.  */
7452         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7453
7454         /* If there's an expression, process it.  */
7455         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7456           expression = cp_parser_expression (parser, /*cast_p=*/false);
7457         finish_for_expr (expression, statement);
7458         check_empty_body (parser, "for");
7459         /* Look for the `)'.  */
7460         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7461
7462         /* Parse the body of the for-statement.  */
7463         parser->in_statement = IN_ITERATION_STMT;
7464         cp_parser_already_scoped_statement (parser);
7465         parser->in_statement = in_statement;
7466
7467         /* We're done with the for-statement.  */
7468         finish_for_stmt (statement);
7469       }
7470       break;
7471
7472     default:
7473       cp_parser_error (parser, "expected iteration-statement");
7474       statement = error_mark_node;
7475       break;
7476     }
7477
7478   return statement;
7479 }
7480
7481 /* Parse a for-init-statement.
7482
7483    for-init-statement:
7484      expression-statement
7485      simple-declaration  */
7486
7487 static void
7488 cp_parser_for_init_statement (cp_parser* parser)
7489 {
7490   /* If the next token is a `;', then we have an empty
7491      expression-statement.  Grammatically, this is also a
7492      simple-declaration, but an invalid one, because it does not
7493      declare anything.  Therefore, if we did not handle this case
7494      specially, we would issue an error message about an invalid
7495      declaration.  */
7496   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7497     {
7498       /* We're going to speculatively look for a declaration, falling back
7499          to an expression, if necessary.  */
7500       cp_parser_parse_tentatively (parser);
7501       /* Parse the declaration.  */
7502       cp_parser_simple_declaration (parser,
7503                                     /*function_definition_allowed_p=*/false);
7504       /* If the tentative parse failed, then we shall need to look for an
7505          expression-statement.  */
7506       if (cp_parser_parse_definitely (parser))
7507         return;
7508     }
7509
7510   cp_parser_expression_statement (parser, false);
7511 }
7512
7513 /* Parse a jump-statement.
7514
7515    jump-statement:
7516      break ;
7517      continue ;
7518      return expression [opt] ;
7519      return braced-init-list ;
7520      goto identifier ;
7521
7522    GNU extension:
7523
7524    jump-statement:
7525      goto * expression ;
7526
7527    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
7528
7529 static tree
7530 cp_parser_jump_statement (cp_parser* parser)
7531 {
7532   tree statement = error_mark_node;
7533   cp_token *token;
7534   enum rid keyword;
7535   unsigned char in_statement;
7536
7537   /* Peek at the next token.  */
7538   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7539   if (!token)
7540     return error_mark_node;
7541
7542   /* See what kind of keyword it is.  */
7543   keyword = token->keyword;
7544   switch (keyword)
7545     {
7546     case RID_BREAK:
7547       in_statement = parser->in_statement & ~IN_IF_STMT;      
7548       switch (in_statement)
7549         {
7550         case 0:
7551           error ("%Hbreak statement not within loop or switch", &token->location);
7552           break;
7553         default:
7554           gcc_assert ((in_statement & IN_SWITCH_STMT)
7555                       || in_statement == IN_ITERATION_STMT);
7556           statement = finish_break_stmt ();
7557           break;
7558         case IN_OMP_BLOCK:
7559           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7560           break;
7561         case IN_OMP_FOR:
7562           error ("%Hbreak statement used with OpenMP for loop", &token->location);
7563           break;
7564         }
7565       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7566       break;
7567
7568     case RID_CONTINUE:
7569       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7570         {
7571         case 0:
7572           error ("%Hcontinue statement not within a loop", &token->location);
7573           break;
7574         case IN_ITERATION_STMT:
7575         case IN_OMP_FOR:
7576           statement = finish_continue_stmt ();
7577           break;
7578         case IN_OMP_BLOCK:
7579           error ("%Hinvalid exit from OpenMP structured block", &token->location);
7580           break;
7581         default:
7582           gcc_unreachable ();
7583         }
7584       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7585       break;
7586
7587     case RID_RETURN:
7588       {
7589         tree expr;
7590         bool expr_non_constant_p;
7591
7592         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7593           {
7594             maybe_warn_cpp0x ("extended initializer lists");
7595             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7596           }
7597         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7598           expr = cp_parser_expression (parser, /*cast_p=*/false);
7599         else
7600           /* If the next token is a `;', then there is no
7601              expression.  */
7602           expr = NULL_TREE;
7603         /* Build the return-statement.  */
7604         statement = finish_return_stmt (expr);
7605         /* Look for the final `;'.  */
7606         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7607       }
7608       break;
7609
7610     case RID_GOTO:
7611       /* Create the goto-statement.  */
7612       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7613         {
7614           /* Issue a warning about this use of a GNU extension.  */
7615           pedwarn (OPT_pedantic, "%HISO C++ forbids computed gotos", &token->location);
7616           /* Consume the '*' token.  */
7617           cp_lexer_consume_token (parser->lexer);
7618           /* Parse the dependent expression.  */
7619           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7620         }
7621       else
7622         finish_goto_stmt (cp_parser_identifier (parser));
7623       /* Look for the final `;'.  */
7624       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7625       break;
7626
7627     default:
7628       cp_parser_error (parser, "expected jump-statement");
7629       break;
7630     }
7631
7632   return statement;
7633 }
7634
7635 /* Parse a declaration-statement.
7636
7637    declaration-statement:
7638      block-declaration  */
7639
7640 static void
7641 cp_parser_declaration_statement (cp_parser* parser)
7642 {
7643   void *p;
7644
7645   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7646   p = obstack_alloc (&declarator_obstack, 0);
7647
7648  /* Parse the block-declaration.  */
7649   cp_parser_block_declaration (parser, /*statement_p=*/true);
7650
7651   /* Free any declarators allocated.  */
7652   obstack_free (&declarator_obstack, p);
7653
7654   /* Finish off the statement.  */
7655   finish_stmt ();
7656 }
7657
7658 /* Some dependent statements (like `if (cond) statement'), are
7659    implicitly in their own scope.  In other words, if the statement is
7660    a single statement (as opposed to a compound-statement), it is
7661    none-the-less treated as if it were enclosed in braces.  Any
7662    declarations appearing in the dependent statement are out of scope
7663    after control passes that point.  This function parses a statement,
7664    but ensures that is in its own scope, even if it is not a
7665    compound-statement.
7666
7667    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7668    is a (possibly labeled) if statement which is not enclosed in
7669    braces and has an else clause.  This is used to implement
7670    -Wparentheses.
7671
7672    Returns the new statement.  */
7673
7674 static tree
7675 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7676 {
7677   tree statement;
7678
7679   if (if_p != NULL)
7680     *if_p = false;
7681
7682   /* Mark if () ; with a special NOP_EXPR.  */
7683   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7684     {
7685       cp_lexer_consume_token (parser->lexer);
7686       statement = add_stmt (build_empty_stmt ());
7687     }
7688   /* if a compound is opened, we simply parse the statement directly.  */
7689   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7690     statement = cp_parser_compound_statement (parser, NULL, false);
7691   /* If the token is not a `{', then we must take special action.  */
7692   else
7693     {
7694       /* Create a compound-statement.  */
7695       statement = begin_compound_stmt (0);
7696       /* Parse the dependent-statement.  */
7697       cp_parser_statement (parser, NULL_TREE, false, if_p);
7698       /* Finish the dummy compound-statement.  */
7699       finish_compound_stmt (statement);
7700     }
7701
7702   /* Return the statement.  */
7703   return statement;
7704 }
7705
7706 /* For some dependent statements (like `while (cond) statement'), we
7707    have already created a scope.  Therefore, even if the dependent
7708    statement is a compound-statement, we do not want to create another
7709    scope.  */
7710
7711 static void
7712 cp_parser_already_scoped_statement (cp_parser* parser)
7713 {
7714   /* If the token is a `{', then we must take special action.  */
7715   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7716     cp_parser_statement (parser, NULL_TREE, false, NULL);
7717   else
7718     {
7719       /* Avoid calling cp_parser_compound_statement, so that we
7720          don't create a new scope.  Do everything else by hand.  */
7721       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7722       cp_parser_statement_seq_opt (parser, NULL_TREE);
7723       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7724     }
7725 }
7726
7727 /* Declarations [gram.dcl.dcl] */
7728
7729 /* Parse an optional declaration-sequence.
7730
7731    declaration-seq:
7732      declaration
7733      declaration-seq declaration  */
7734
7735 static void
7736 cp_parser_declaration_seq_opt (cp_parser* parser)
7737 {
7738   while (true)
7739     {
7740       cp_token *token;
7741
7742       token = cp_lexer_peek_token (parser->lexer);
7743
7744       if (token->type == CPP_CLOSE_BRACE
7745           || token->type == CPP_EOF
7746           || token->type == CPP_PRAGMA_EOL)
7747         break;
7748
7749       if (token->type == CPP_SEMICOLON)
7750         {
7751           /* A declaration consisting of a single semicolon is
7752              invalid.  Allow it unless we're being pedantic.  */
7753           cp_lexer_consume_token (parser->lexer);
7754           if (!in_system_header)
7755             pedwarn (OPT_pedantic, "extra %<;%>");
7756           continue;
7757         }
7758
7759       /* If we're entering or exiting a region that's implicitly
7760          extern "C", modify the lang context appropriately.  */
7761       if (!parser->implicit_extern_c && token->implicit_extern_c)
7762         {
7763           push_lang_context (lang_name_c);
7764           parser->implicit_extern_c = true;
7765         }
7766       else if (parser->implicit_extern_c && !token->implicit_extern_c)
7767         {
7768           pop_lang_context ();
7769           parser->implicit_extern_c = false;
7770         }
7771
7772       if (token->type == CPP_PRAGMA)
7773         {
7774           /* A top-level declaration can consist solely of a #pragma.
7775              A nested declaration cannot, so this is done here and not
7776              in cp_parser_declaration.  (A #pragma at block scope is
7777              handled in cp_parser_statement.)  */
7778           cp_parser_pragma (parser, pragma_external);
7779           continue;
7780         }
7781
7782       /* Parse the declaration itself.  */
7783       cp_parser_declaration (parser);
7784     }
7785 }
7786
7787 /* Parse a declaration.
7788
7789    declaration:
7790      block-declaration
7791      function-definition
7792      template-declaration
7793      explicit-instantiation
7794      explicit-specialization
7795      linkage-specification
7796      namespace-definition
7797
7798    GNU extension:
7799
7800    declaration:
7801       __extension__ declaration */
7802
7803 static void
7804 cp_parser_declaration (cp_parser* parser)
7805 {
7806   cp_token token1;
7807   cp_token token2;
7808   int saved_pedantic;
7809   void *p;
7810
7811   /* Check for the `__extension__' keyword.  */
7812   if (cp_parser_extension_opt (parser, &saved_pedantic))
7813     {
7814       /* Parse the qualified declaration.  */
7815       cp_parser_declaration (parser);
7816       /* Restore the PEDANTIC flag.  */
7817       pedantic = saved_pedantic;
7818
7819       return;
7820     }
7821
7822   /* Try to figure out what kind of declaration is present.  */
7823   token1 = *cp_lexer_peek_token (parser->lexer);
7824
7825   if (token1.type != CPP_EOF)
7826     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7827   else
7828     {
7829       token2.type = CPP_EOF;
7830       token2.keyword = RID_MAX;
7831     }
7832
7833   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
7834   p = obstack_alloc (&declarator_obstack, 0);
7835
7836   /* If the next token is `extern' and the following token is a string
7837      literal, then we have a linkage specification.  */
7838   if (token1.keyword == RID_EXTERN
7839       && cp_parser_is_string_literal (&token2))
7840     cp_parser_linkage_specification (parser);
7841   /* If the next token is `template', then we have either a template
7842      declaration, an explicit instantiation, or an explicit
7843      specialization.  */
7844   else if (token1.keyword == RID_TEMPLATE)
7845     {
7846       /* `template <>' indicates a template specialization.  */
7847       if (token2.type == CPP_LESS
7848           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7849         cp_parser_explicit_specialization (parser);
7850       /* `template <' indicates a template declaration.  */
7851       else if (token2.type == CPP_LESS)
7852         cp_parser_template_declaration (parser, /*member_p=*/false);
7853       /* Anything else must be an explicit instantiation.  */
7854       else
7855         cp_parser_explicit_instantiation (parser);
7856     }
7857   /* If the next token is `export', then we have a template
7858      declaration.  */
7859   else if (token1.keyword == RID_EXPORT)
7860     cp_parser_template_declaration (parser, /*member_p=*/false);
7861   /* If the next token is `extern', 'static' or 'inline' and the one
7862      after that is `template', we have a GNU extended explicit
7863      instantiation directive.  */
7864   else if (cp_parser_allow_gnu_extensions_p (parser)
7865            && (token1.keyword == RID_EXTERN
7866                || token1.keyword == RID_STATIC
7867                || token1.keyword == RID_INLINE)
7868            && token2.keyword == RID_TEMPLATE)
7869     cp_parser_explicit_instantiation (parser);
7870   /* If the next token is `namespace', check for a named or unnamed
7871      namespace definition.  */
7872   else if (token1.keyword == RID_NAMESPACE
7873            && (/* A named namespace definition.  */
7874                (token2.type == CPP_NAME
7875                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7876                     != CPP_EQ))
7877                /* An unnamed namespace definition.  */
7878                || token2.type == CPP_OPEN_BRACE
7879                || token2.keyword == RID_ATTRIBUTE))
7880     cp_parser_namespace_definition (parser);
7881   /* An inline (associated) namespace definition.  */
7882   else if (token1.keyword == RID_INLINE
7883            && token2.keyword == RID_NAMESPACE)
7884     cp_parser_namespace_definition (parser);
7885   /* Objective-C++ declaration/definition.  */
7886   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7887     cp_parser_objc_declaration (parser);
7888   /* We must have either a block declaration or a function
7889      definition.  */
7890   else
7891     /* Try to parse a block-declaration, or a function-definition.  */
7892     cp_parser_block_declaration (parser, /*statement_p=*/false);
7893
7894   /* Free any declarators allocated.  */
7895   obstack_free (&declarator_obstack, p);
7896 }
7897
7898 /* Parse a block-declaration.
7899
7900    block-declaration:
7901      simple-declaration
7902      asm-definition
7903      namespace-alias-definition
7904      using-declaration
7905      using-directive
7906
7907    GNU Extension:
7908
7909    block-declaration:
7910      __extension__ block-declaration
7911
7912    C++0x Extension:
7913
7914    block-declaration:
7915      static_assert-declaration
7916
7917    If STATEMENT_P is TRUE, then this block-declaration is occurring as
7918    part of a declaration-statement.  */
7919
7920 static void
7921 cp_parser_block_declaration (cp_parser *parser,
7922                              bool      statement_p)
7923 {
7924   cp_token *token1;
7925   int saved_pedantic;
7926
7927   /* Check for the `__extension__' keyword.  */
7928   if (cp_parser_extension_opt (parser, &saved_pedantic))
7929     {
7930       /* Parse the qualified declaration.  */
7931       cp_parser_block_declaration (parser, statement_p);
7932       /* Restore the PEDANTIC flag.  */
7933       pedantic = saved_pedantic;
7934
7935       return;
7936     }
7937
7938   /* Peek at the next token to figure out which kind of declaration is
7939      present.  */
7940   token1 = cp_lexer_peek_token (parser->lexer);
7941
7942   /* If the next keyword is `asm', we have an asm-definition.  */
7943   if (token1->keyword == RID_ASM)
7944     {
7945       if (statement_p)
7946         cp_parser_commit_to_tentative_parse (parser);
7947       cp_parser_asm_definition (parser);
7948     }
7949   /* If the next keyword is `namespace', we have a
7950      namespace-alias-definition.  */
7951   else if (token1->keyword == RID_NAMESPACE)
7952     cp_parser_namespace_alias_definition (parser);
7953   /* If the next keyword is `using', we have either a
7954      using-declaration or a using-directive.  */
7955   else if (token1->keyword == RID_USING)
7956     {
7957       cp_token *token2;
7958
7959       if (statement_p)
7960         cp_parser_commit_to_tentative_parse (parser);
7961       /* If the token after `using' is `namespace', then we have a
7962          using-directive.  */
7963       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7964       if (token2->keyword == RID_NAMESPACE)
7965         cp_parser_using_directive (parser);
7966       /* Otherwise, it's a using-declaration.  */
7967       else
7968         cp_parser_using_declaration (parser,
7969                                      /*access_declaration_p=*/false);
7970     }
7971   /* If the next keyword is `__label__' we have a misplaced label
7972      declaration.  */
7973   else if (token1->keyword == RID_LABEL)
7974     {
7975       cp_lexer_consume_token (parser->lexer);
7976       error ("%H%<__label__%> not at the beginning of a block", &token1->location);
7977       cp_parser_skip_to_end_of_statement (parser);
7978       /* If the next token is now a `;', consume it.  */
7979       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7980         cp_lexer_consume_token (parser->lexer);
7981     }
7982   /* If the next token is `static_assert' we have a static assertion.  */
7983   else if (token1->keyword == RID_STATIC_ASSERT)
7984     cp_parser_static_assert (parser, /*member_p=*/false);
7985   /* Anything else must be a simple-declaration.  */
7986   else
7987     cp_parser_simple_declaration (parser, !statement_p);
7988 }
7989
7990 /* Parse a simple-declaration.
7991
7992    simple-declaration:
7993      decl-specifier-seq [opt] init-declarator-list [opt] ;
7994
7995    init-declarator-list:
7996      init-declarator
7997      init-declarator-list , init-declarator
7998
7999    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8000    function-definition as a simple-declaration.  */
8001
8002 static void
8003 cp_parser_simple_declaration (cp_parser* parser,
8004                               bool function_definition_allowed_p)
8005 {
8006   cp_decl_specifier_seq decl_specifiers;
8007   int declares_class_or_enum;
8008   bool saw_declarator;
8009
8010   /* Defer access checks until we know what is being declared; the
8011      checks for names appearing in the decl-specifier-seq should be
8012      done as if we were in the scope of the thing being declared.  */
8013   push_deferring_access_checks (dk_deferred);
8014
8015   /* Parse the decl-specifier-seq.  We have to keep track of whether
8016      or not the decl-specifier-seq declares a named class or
8017      enumeration type, since that is the only case in which the
8018      init-declarator-list is allowed to be empty.
8019
8020      [dcl.dcl]
8021
8022      In a simple-declaration, the optional init-declarator-list can be
8023      omitted only when declaring a class or enumeration, that is when
8024      the decl-specifier-seq contains either a class-specifier, an
8025      elaborated-type-specifier, or an enum-specifier.  */
8026   cp_parser_decl_specifier_seq (parser,
8027                                 CP_PARSER_FLAGS_OPTIONAL,
8028                                 &decl_specifiers,
8029                                 &declares_class_or_enum);
8030   /* We no longer need to defer access checks.  */
8031   stop_deferring_access_checks ();
8032
8033   /* In a block scope, a valid declaration must always have a
8034      decl-specifier-seq.  By not trying to parse declarators, we can
8035      resolve the declaration/expression ambiguity more quickly.  */
8036   if (!function_definition_allowed_p
8037       && !decl_specifiers.any_specifiers_p)
8038     {
8039       cp_parser_error (parser, "expected declaration");
8040       goto done;
8041     }
8042
8043   /* If the next two tokens are both identifiers, the code is
8044      erroneous. The usual cause of this situation is code like:
8045
8046        T t;
8047
8048      where "T" should name a type -- but does not.  */
8049   if (!decl_specifiers.type
8050       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8051     {
8052       /* If parsing tentatively, we should commit; we really are
8053          looking at a declaration.  */
8054       cp_parser_commit_to_tentative_parse (parser);
8055       /* Give up.  */
8056       goto done;
8057     }
8058
8059   /* If we have seen at least one decl-specifier, and the next token
8060      is not a parenthesis, then we must be looking at a declaration.
8061      (After "int (" we might be looking at a functional cast.)  */
8062   if (decl_specifiers.any_specifiers_p
8063       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8064       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8065     cp_parser_commit_to_tentative_parse (parser);
8066
8067   /* Keep going until we hit the `;' at the end of the simple
8068      declaration.  */
8069   saw_declarator = false;
8070   while (cp_lexer_next_token_is_not (parser->lexer,
8071                                      CPP_SEMICOLON))
8072     {
8073       cp_token *token;
8074       bool function_definition_p;
8075       tree decl;
8076
8077       if (saw_declarator)
8078         {
8079           /* If we are processing next declarator, coma is expected */
8080           token = cp_lexer_peek_token (parser->lexer);
8081           gcc_assert (token->type == CPP_COMMA);
8082           cp_lexer_consume_token (parser->lexer);
8083         }
8084       else
8085         saw_declarator = true;
8086
8087       /* Parse the init-declarator.  */
8088       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8089                                         /*checks=*/NULL,
8090                                         function_definition_allowed_p,
8091                                         /*member_p=*/false,
8092                                         declares_class_or_enum,
8093                                         &function_definition_p);
8094       /* If an error occurred while parsing tentatively, exit quickly.
8095          (That usually happens when in the body of a function; each
8096          statement is treated as a declaration-statement until proven
8097          otherwise.)  */
8098       if (cp_parser_error_occurred (parser))
8099         goto done;
8100       /* Handle function definitions specially.  */
8101       if (function_definition_p)
8102         {
8103           /* If the next token is a `,', then we are probably
8104              processing something like:
8105
8106                void f() {}, *p;
8107
8108              which is erroneous.  */
8109           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8110             {
8111               cp_token *token = cp_lexer_peek_token (parser->lexer);
8112               error ("%Hmixing declarations and function-definitions is forbidden",
8113                      &token->location);
8114             }
8115           /* Otherwise, we're done with the list of declarators.  */
8116           else
8117             {
8118               pop_deferring_access_checks ();
8119               return;
8120             }
8121         }
8122       /* The next token should be either a `,' or a `;'.  */
8123       token = cp_lexer_peek_token (parser->lexer);
8124       /* If it's a `,', there are more declarators to come.  */
8125       if (token->type == CPP_COMMA)
8126         /* will be consumed next time around */;
8127       /* If it's a `;', we are done.  */
8128       else if (token->type == CPP_SEMICOLON)
8129         break;
8130       /* Anything else is an error.  */
8131       else
8132         {
8133           /* If we have already issued an error message we don't need
8134              to issue another one.  */
8135           if (decl != error_mark_node
8136               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8137             cp_parser_error (parser, "expected %<,%> or %<;%>");
8138           /* Skip tokens until we reach the end of the statement.  */
8139           cp_parser_skip_to_end_of_statement (parser);
8140           /* If the next token is now a `;', consume it.  */
8141           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8142             cp_lexer_consume_token (parser->lexer);
8143           goto done;
8144         }
8145       /* After the first time around, a function-definition is not
8146          allowed -- even if it was OK at first.  For example:
8147
8148            int i, f() {}
8149
8150          is not valid.  */
8151       function_definition_allowed_p = false;
8152     }
8153
8154   /* Issue an error message if no declarators are present, and the
8155      decl-specifier-seq does not itself declare a class or
8156      enumeration.  */
8157   if (!saw_declarator)
8158     {
8159       if (cp_parser_declares_only_class_p (parser))
8160         shadow_tag (&decl_specifiers);
8161       /* Perform any deferred access checks.  */
8162       perform_deferred_access_checks ();
8163     }
8164
8165   /* Consume the `;'.  */
8166   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8167
8168  done:
8169   pop_deferring_access_checks ();
8170 }
8171
8172 /* Parse a decl-specifier-seq.
8173
8174    decl-specifier-seq:
8175      decl-specifier-seq [opt] decl-specifier
8176
8177    decl-specifier:
8178      storage-class-specifier
8179      type-specifier
8180      function-specifier
8181      friend
8182      typedef
8183
8184    GNU Extension:
8185
8186    decl-specifier:
8187      attributes
8188
8189    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8190
8191    The parser flags FLAGS is used to control type-specifier parsing.
8192
8193    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8194    flags:
8195
8196      1: one of the decl-specifiers is an elaborated-type-specifier
8197         (i.e., a type declaration)
8198      2: one of the decl-specifiers is an enum-specifier or a
8199         class-specifier (i.e., a type definition)
8200
8201    */
8202
8203 static void
8204 cp_parser_decl_specifier_seq (cp_parser* parser,
8205                               cp_parser_flags flags,
8206                               cp_decl_specifier_seq *decl_specs,
8207                               int* declares_class_or_enum)
8208 {
8209   bool constructor_possible_p = !parser->in_declarator_p;
8210   cp_token *start_token = NULL;
8211
8212   /* Clear DECL_SPECS.  */
8213   clear_decl_specs (decl_specs);
8214
8215   /* Assume no class or enumeration type is declared.  */
8216   *declares_class_or_enum = 0;
8217
8218   /* Keep reading specifiers until there are no more to read.  */
8219   while (true)
8220     {
8221       bool constructor_p;
8222       bool found_decl_spec;
8223       cp_token *token;
8224
8225       /* Peek at the next token.  */
8226       token = cp_lexer_peek_token (parser->lexer);
8227
8228       /* Save the first token of the decl spec list for error
8229          reporting.  */
8230       if (!start_token)
8231         start_token = token;
8232       /* Handle attributes.  */
8233       if (token->keyword == RID_ATTRIBUTE)
8234         {
8235           /* Parse the attributes.  */
8236           decl_specs->attributes
8237             = chainon (decl_specs->attributes,
8238                        cp_parser_attributes_opt (parser));
8239           continue;
8240         }
8241       /* Assume we will find a decl-specifier keyword.  */
8242       found_decl_spec = true;
8243       /* If the next token is an appropriate keyword, we can simply
8244          add it to the list.  */
8245       switch (token->keyword)
8246         {
8247           /* decl-specifier:
8248                friend  */
8249         case RID_FRIEND:
8250           if (!at_class_scope_p ())
8251             {
8252               error ("%H%<friend%> used outside of class", &token->location);
8253               cp_lexer_purge_token (parser->lexer);
8254             }
8255           else
8256             {
8257               ++decl_specs->specs[(int) ds_friend];
8258               /* Consume the token.  */
8259               cp_lexer_consume_token (parser->lexer);
8260             }
8261           break;
8262
8263           /* function-specifier:
8264                inline
8265                virtual
8266                explicit  */
8267         case RID_INLINE:
8268         case RID_VIRTUAL:
8269         case RID_EXPLICIT:
8270           cp_parser_function_specifier_opt (parser, decl_specs);
8271           break;
8272
8273           /* decl-specifier:
8274                typedef  */
8275         case RID_TYPEDEF:
8276           ++decl_specs->specs[(int) ds_typedef];
8277           /* Consume the token.  */
8278           cp_lexer_consume_token (parser->lexer);
8279           /* A constructor declarator cannot appear in a typedef.  */
8280           constructor_possible_p = false;
8281           /* The "typedef" keyword can only occur in a declaration; we
8282              may as well commit at this point.  */
8283           cp_parser_commit_to_tentative_parse (parser);
8284
8285           if (decl_specs->storage_class != sc_none)
8286             decl_specs->conflicting_specifiers_p = true;
8287           break;
8288
8289           /* storage-class-specifier:
8290                auto
8291                register
8292                static
8293                extern
8294                mutable
8295
8296              GNU Extension:
8297                thread  */
8298         case RID_AUTO:
8299           /* Consume the token.  */
8300           cp_lexer_consume_token (parser->lexer);
8301
8302           if (cxx_dialect == cxx98) 
8303             {
8304               /* Complain about `auto' as a storage specifier, if
8305                  we're complaining about C++0x compatibility.  */
8306               warning 
8307                 (OPT_Wc__0x_compat, 
8308                  "%H%<auto%> will change meaning in C++0x; please remove it",
8309                  &token->location);
8310
8311               /* Set the storage class anyway.  */
8312               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8313                                            token->location);
8314             }
8315           else 
8316             /* We do not yet support the use of `auto' as a
8317                type-specifier.  */
8318             error ("%HC++0x %<auto%> specifier not supported", &token->location);
8319           break;
8320
8321         case RID_REGISTER:
8322         case RID_STATIC:
8323         case RID_EXTERN:
8324         case RID_MUTABLE:
8325           /* Consume the token.  */
8326           cp_lexer_consume_token (parser->lexer);
8327           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8328                                        token->location);
8329           break;
8330         case RID_THREAD:
8331           /* Consume the token.  */
8332           cp_lexer_consume_token (parser->lexer);
8333           ++decl_specs->specs[(int) ds_thread];
8334           break;
8335
8336         default:
8337           /* We did not yet find a decl-specifier yet.  */
8338           found_decl_spec = false;
8339           break;
8340         }
8341
8342       /* Constructors are a special case.  The `S' in `S()' is not a
8343          decl-specifier; it is the beginning of the declarator.  */
8344       constructor_p
8345         = (!found_decl_spec
8346            && constructor_possible_p
8347            && (cp_parser_constructor_declarator_p
8348                (parser, decl_specs->specs[(int) ds_friend] != 0)));
8349
8350       /* If we don't have a DECL_SPEC yet, then we must be looking at
8351          a type-specifier.  */
8352       if (!found_decl_spec && !constructor_p)
8353         {
8354           int decl_spec_declares_class_or_enum;
8355           bool is_cv_qualifier;
8356           tree type_spec;
8357
8358           type_spec
8359             = cp_parser_type_specifier (parser, flags,
8360                                         decl_specs,
8361                                         /*is_declaration=*/true,
8362                                         &decl_spec_declares_class_or_enum,
8363                                         &is_cv_qualifier);
8364           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8365
8366           /* If this type-specifier referenced a user-defined type
8367              (a typedef, class-name, etc.), then we can't allow any
8368              more such type-specifiers henceforth.
8369
8370              [dcl.spec]
8371
8372              The longest sequence of decl-specifiers that could
8373              possibly be a type name is taken as the
8374              decl-specifier-seq of a declaration.  The sequence shall
8375              be self-consistent as described below.
8376
8377              [dcl.type]
8378
8379              As a general rule, at most one type-specifier is allowed
8380              in the complete decl-specifier-seq of a declaration.  The
8381              only exceptions are the following:
8382
8383              -- const or volatile can be combined with any other
8384                 type-specifier.
8385
8386              -- signed or unsigned can be combined with char, long,
8387                 short, or int.
8388
8389              -- ..
8390
8391              Example:
8392
8393                typedef char* Pc;
8394                void g (const int Pc);
8395
8396              Here, Pc is *not* part of the decl-specifier seq; it's
8397              the declarator.  Therefore, once we see a type-specifier
8398              (other than a cv-qualifier), we forbid any additional
8399              user-defined types.  We *do* still allow things like `int
8400              int' to be considered a decl-specifier-seq, and issue the
8401              error message later.  */
8402           if (type_spec && !is_cv_qualifier)
8403             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8404           /* A constructor declarator cannot follow a type-specifier.  */
8405           if (type_spec)
8406             {
8407               constructor_possible_p = false;
8408               found_decl_spec = true;
8409             }
8410         }
8411
8412       /* If we still do not have a DECL_SPEC, then there are no more
8413          decl-specifiers.  */
8414       if (!found_decl_spec)
8415         break;
8416
8417       decl_specs->any_specifiers_p = true;
8418       /* After we see one decl-specifier, further decl-specifiers are
8419          always optional.  */
8420       flags |= CP_PARSER_FLAGS_OPTIONAL;
8421     }
8422
8423   cp_parser_check_decl_spec (decl_specs, start_token->location);
8424
8425   /* Don't allow a friend specifier with a class definition.  */
8426   if (decl_specs->specs[(int) ds_friend] != 0
8427       && (*declares_class_or_enum & 2))
8428     error ("%Hclass definition may not be declared a friend",
8429             &start_token->location);
8430 }
8431
8432 /* Parse an (optional) storage-class-specifier.
8433
8434    storage-class-specifier:
8435      auto
8436      register
8437      static
8438      extern
8439      mutable
8440
8441    GNU Extension:
8442
8443    storage-class-specifier:
8444      thread
8445
8446    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
8447
8448 static tree
8449 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8450 {
8451   switch (cp_lexer_peek_token (parser->lexer)->keyword)
8452     {
8453     case RID_AUTO:
8454       if (cxx_dialect != cxx98)
8455         return NULL_TREE;
8456       /* Fall through for C++98.  */
8457
8458     case RID_REGISTER:
8459     case RID_STATIC:
8460     case RID_EXTERN:
8461     case RID_MUTABLE:
8462     case RID_THREAD:
8463       /* Consume the token.  */
8464       return cp_lexer_consume_token (parser->lexer)->u.value;
8465
8466     default:
8467       return NULL_TREE;
8468     }
8469 }
8470
8471 /* Parse an (optional) function-specifier.
8472
8473    function-specifier:
8474      inline
8475      virtual
8476      explicit
8477
8478    Returns an IDENTIFIER_NODE corresponding to the keyword used.
8479    Updates DECL_SPECS, if it is non-NULL.  */
8480
8481 static tree
8482 cp_parser_function_specifier_opt (cp_parser* parser,
8483                                   cp_decl_specifier_seq *decl_specs)
8484 {
8485   cp_token *token = cp_lexer_peek_token (parser->lexer);
8486   switch (token->keyword)
8487     {
8488     case RID_INLINE:
8489       if (decl_specs)
8490         ++decl_specs->specs[(int) ds_inline];
8491       break;
8492
8493     case RID_VIRTUAL:
8494       /* 14.5.2.3 [temp.mem]
8495
8496          A member function template shall not be virtual.  */
8497       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8498         error ("%Htemplates may not be %<virtual%>", &token->location);
8499       else if (decl_specs)
8500         ++decl_specs->specs[(int) ds_virtual];
8501       break;
8502
8503     case RID_EXPLICIT:
8504       if (decl_specs)
8505         ++decl_specs->specs[(int) ds_explicit];
8506       break;
8507
8508     default:
8509       return NULL_TREE;
8510     }
8511
8512   /* Consume the token.  */
8513   return cp_lexer_consume_token (parser->lexer)->u.value;
8514 }
8515
8516 /* Parse a linkage-specification.
8517
8518    linkage-specification:
8519      extern string-literal { declaration-seq [opt] }
8520      extern string-literal declaration  */
8521
8522 static void
8523 cp_parser_linkage_specification (cp_parser* parser)
8524 {
8525   tree linkage;
8526
8527   /* Look for the `extern' keyword.  */
8528   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8529
8530   /* Look for the string-literal.  */
8531   linkage = cp_parser_string_literal (parser, false, false);
8532
8533   /* Transform the literal into an identifier.  If the literal is a
8534      wide-character string, or contains embedded NULs, then we can't
8535      handle it as the user wants.  */
8536   if (strlen (TREE_STRING_POINTER (linkage))
8537       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8538     {
8539       cp_parser_error (parser, "invalid linkage-specification");
8540       /* Assume C++ linkage.  */
8541       linkage = lang_name_cplusplus;
8542     }
8543   else
8544     linkage = get_identifier (TREE_STRING_POINTER (linkage));
8545
8546   /* We're now using the new linkage.  */
8547   push_lang_context (linkage);
8548
8549   /* If the next token is a `{', then we're using the first
8550      production.  */
8551   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8552     {
8553       /* Consume the `{' token.  */
8554       cp_lexer_consume_token (parser->lexer);
8555       /* Parse the declarations.  */
8556       cp_parser_declaration_seq_opt (parser);
8557       /* Look for the closing `}'.  */
8558       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8559     }
8560   /* Otherwise, there's just one declaration.  */
8561   else
8562     {
8563       bool saved_in_unbraced_linkage_specification_p;
8564
8565       saved_in_unbraced_linkage_specification_p
8566         = parser->in_unbraced_linkage_specification_p;
8567       parser->in_unbraced_linkage_specification_p = true;
8568       cp_parser_declaration (parser);
8569       parser->in_unbraced_linkage_specification_p
8570         = saved_in_unbraced_linkage_specification_p;
8571     }
8572
8573   /* We're done with the linkage-specification.  */
8574   pop_lang_context ();
8575 }
8576
8577 /* Parse a static_assert-declaration.
8578
8579    static_assert-declaration:
8580      static_assert ( constant-expression , string-literal ) ; 
8581
8582    If MEMBER_P, this static_assert is a class member.  */
8583
8584 static void 
8585 cp_parser_static_assert(cp_parser *parser, bool member_p)
8586 {
8587   tree condition;
8588   tree message;
8589   cp_token *token;
8590   location_t saved_loc;
8591
8592   /* Peek at the `static_assert' token so we can keep track of exactly
8593      where the static assertion started.  */
8594   token = cp_lexer_peek_token (parser->lexer);
8595   saved_loc = token->location;
8596
8597   /* Look for the `static_assert' keyword.  */
8598   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
8599                                   "%<static_assert%>"))
8600     return;
8601
8602   /*  We know we are in a static assertion; commit to any tentative
8603       parse.  */
8604   if (cp_parser_parsing_tentatively (parser))
8605     cp_parser_commit_to_tentative_parse (parser);
8606
8607   /* Parse the `(' starting the static assertion condition.  */
8608   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8609
8610   /* Parse the constant-expression.  */
8611   condition = 
8612     cp_parser_constant_expression (parser,
8613                                    /*allow_non_constant_p=*/false,
8614                                    /*non_constant_p=*/NULL);
8615
8616   /* Parse the separating `,'.  */
8617   cp_parser_require (parser, CPP_COMMA, "%<,%>");
8618
8619   /* Parse the string-literal message.  */
8620   message = cp_parser_string_literal (parser, 
8621                                       /*translate=*/false,
8622                                       /*wide_ok=*/true);
8623
8624   /* A `)' completes the static assertion.  */
8625   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8626     cp_parser_skip_to_closing_parenthesis (parser, 
8627                                            /*recovering=*/true, 
8628                                            /*or_comma=*/false,
8629                                            /*consume_paren=*/true);
8630
8631   /* A semicolon terminates the declaration.  */
8632   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8633
8634   /* Complete the static assertion, which may mean either processing 
8635      the static assert now or saving it for template instantiation.  */
8636   finish_static_assert (condition, message, saved_loc, member_p);
8637 }
8638
8639 /* Parse a `decltype' type. Returns the type. 
8640
8641    simple-type-specifier:
8642      decltype ( expression )  */
8643
8644 static tree
8645 cp_parser_decltype (cp_parser *parser)
8646 {
8647   tree expr;
8648   bool id_expression_or_member_access_p = false;
8649   const char *saved_message;
8650   bool saved_integral_constant_expression_p;
8651   bool saved_non_integral_constant_expression_p;
8652   cp_token *id_expr_start_token;
8653
8654   /* Look for the `decltype' token.  */
8655   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8656     return error_mark_node;
8657
8658   /* Types cannot be defined in a `decltype' expression.  Save away the
8659      old message.  */
8660   saved_message = parser->type_definition_forbidden_message;
8661
8662   /* And create the new one.  */
8663   parser->type_definition_forbidden_message
8664     = "types may not be defined in %<decltype%> expressions";
8665
8666   /* The restrictions on constant-expressions do not apply inside
8667      decltype expressions.  */
8668   saved_integral_constant_expression_p
8669     = parser->integral_constant_expression_p;
8670   saved_non_integral_constant_expression_p
8671     = parser->non_integral_constant_expression_p;
8672   parser->integral_constant_expression_p = false;
8673
8674   /* Do not actually evaluate the expression.  */
8675   ++skip_evaluation;
8676
8677   /* Parse the opening `('.  */
8678   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8679     return error_mark_node;
8680   
8681   /* First, try parsing an id-expression.  */
8682   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8683   cp_parser_parse_tentatively (parser);
8684   expr = cp_parser_id_expression (parser,
8685                                   /*template_keyword_p=*/false,
8686                                   /*check_dependency_p=*/true,
8687                                   /*template_p=*/NULL,
8688                                   /*declarator_p=*/false,
8689                                   /*optional_p=*/false);
8690
8691   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8692     {
8693       bool non_integral_constant_expression_p = false;
8694       tree id_expression = expr;
8695       cp_id_kind idk;
8696       const char *error_msg;
8697
8698       if (TREE_CODE (expr) == IDENTIFIER_NODE)
8699         /* Lookup the name we got back from the id-expression.  */
8700         expr = cp_parser_lookup_name (parser, expr,
8701                                       none_type,
8702                                       /*is_template=*/false,
8703                                       /*is_namespace=*/false,
8704                                       /*check_dependency=*/true,
8705                                       /*ambiguous_decls=*/NULL,
8706                                       id_expr_start_token->location);
8707
8708       if (expr
8709           && expr != error_mark_node
8710           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8711           && TREE_CODE (expr) != TYPE_DECL
8712           && (TREE_CODE (expr) != BIT_NOT_EXPR
8713               || !TYPE_P (TREE_OPERAND (expr, 0)))
8714           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8715         {
8716           /* Complete lookup of the id-expression.  */
8717           expr = (finish_id_expression
8718                   (id_expression, expr, parser->scope, &idk,
8719                    /*integral_constant_expression_p=*/false,
8720                    /*allow_non_integral_constant_expression_p=*/true,
8721                    &non_integral_constant_expression_p,
8722                    /*template_p=*/false,
8723                    /*done=*/true,
8724                    /*address_p=*/false,
8725                    /*template_arg_p=*/false,
8726                    &error_msg,
8727                    id_expr_start_token->location));
8728
8729           if (expr == error_mark_node)
8730             /* We found an id-expression, but it was something that we
8731                should not have found. This is an error, not something
8732                we can recover from, so note that we found an
8733                id-expression and we'll recover as gracefully as
8734                possible.  */
8735             id_expression_or_member_access_p = true;
8736         }
8737
8738       if (expr 
8739           && expr != error_mark_node
8740           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8741         /* We have an id-expression.  */
8742         id_expression_or_member_access_p = true;
8743     }
8744
8745   if (!id_expression_or_member_access_p)
8746     {
8747       /* Abort the id-expression parse.  */
8748       cp_parser_abort_tentative_parse (parser);
8749
8750       /* Parsing tentatively, again.  */
8751       cp_parser_parse_tentatively (parser);
8752
8753       /* Parse a class member access.  */
8754       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8755                                            /*cast_p=*/false,
8756                                            /*member_access_only_p=*/true);
8757
8758       if (expr 
8759           && expr != error_mark_node
8760           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8761         /* We have an id-expression.  */
8762         id_expression_or_member_access_p = true;
8763     }
8764
8765   if (id_expression_or_member_access_p)
8766     /* We have parsed the complete id-expression or member access.  */
8767     cp_parser_parse_definitely (parser);
8768   else
8769     {
8770       /* Abort our attempt to parse an id-expression or member access
8771          expression.  */
8772       cp_parser_abort_tentative_parse (parser);
8773
8774       /* Parse a full expression.  */
8775       expr = cp_parser_expression (parser, /*cast_p=*/false);
8776     }
8777
8778   /* Go back to evaluating expressions.  */
8779   --skip_evaluation;
8780
8781   /* Restore the old message and the integral constant expression
8782      flags.  */
8783   parser->type_definition_forbidden_message = saved_message;
8784   parser->integral_constant_expression_p
8785     = saved_integral_constant_expression_p;
8786   parser->non_integral_constant_expression_p
8787     = saved_non_integral_constant_expression_p;
8788
8789   if (expr == error_mark_node)
8790     {
8791       /* Skip everything up to the closing `)'.  */
8792       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8793                                              /*consume_paren=*/true);
8794       return error_mark_node;
8795     }
8796   
8797   /* Parse to the closing `)'.  */
8798   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8799     {
8800       cp_parser_skip_to_closing_parenthesis (parser, true, false,
8801                                              /*consume_paren=*/true);
8802       return error_mark_node;
8803     }
8804
8805   return finish_decltype_type (expr, id_expression_or_member_access_p);
8806 }
8807
8808 /* Special member functions [gram.special] */
8809
8810 /* Parse a conversion-function-id.
8811
8812    conversion-function-id:
8813      operator conversion-type-id
8814
8815    Returns an IDENTIFIER_NODE representing the operator.  */
8816
8817 static tree
8818 cp_parser_conversion_function_id (cp_parser* parser)
8819 {
8820   tree type;
8821   tree saved_scope;
8822   tree saved_qualifying_scope;
8823   tree saved_object_scope;
8824   tree pushed_scope = NULL_TREE;
8825
8826   /* Look for the `operator' token.  */
8827   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
8828     return error_mark_node;
8829   /* When we parse the conversion-type-id, the current scope will be
8830      reset.  However, we need that information in able to look up the
8831      conversion function later, so we save it here.  */
8832   saved_scope = parser->scope;
8833   saved_qualifying_scope = parser->qualifying_scope;
8834   saved_object_scope = parser->object_scope;
8835   /* We must enter the scope of the class so that the names of
8836      entities declared within the class are available in the
8837      conversion-type-id.  For example, consider:
8838
8839        struct S {
8840          typedef int I;
8841          operator I();
8842        };
8843
8844        S::operator I() { ... }
8845
8846      In order to see that `I' is a type-name in the definition, we
8847      must be in the scope of `S'.  */
8848   if (saved_scope)
8849     pushed_scope = push_scope (saved_scope);
8850   /* Parse the conversion-type-id.  */
8851   type = cp_parser_conversion_type_id (parser);
8852   /* Leave the scope of the class, if any.  */
8853   if (pushed_scope)
8854     pop_scope (pushed_scope);
8855   /* Restore the saved scope.  */
8856   parser->scope = saved_scope;
8857   parser->qualifying_scope = saved_qualifying_scope;
8858   parser->object_scope = saved_object_scope;
8859   /* If the TYPE is invalid, indicate failure.  */
8860   if (type == error_mark_node)
8861     return error_mark_node;
8862   return mangle_conv_op_name_for_type (type);
8863 }
8864
8865 /* Parse a conversion-type-id:
8866
8867    conversion-type-id:
8868      type-specifier-seq conversion-declarator [opt]
8869
8870    Returns the TYPE specified.  */
8871
8872 static tree
8873 cp_parser_conversion_type_id (cp_parser* parser)
8874 {
8875   tree attributes;
8876   cp_decl_specifier_seq type_specifiers;
8877   cp_declarator *declarator;
8878   tree type_specified;
8879
8880   /* Parse the attributes.  */
8881   attributes = cp_parser_attributes_opt (parser);
8882   /* Parse the type-specifiers.  */
8883   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8884                                 &type_specifiers);
8885   /* If that didn't work, stop.  */
8886   if (type_specifiers.type == error_mark_node)
8887     return error_mark_node;
8888   /* Parse the conversion-declarator.  */
8889   declarator = cp_parser_conversion_declarator_opt (parser);
8890
8891   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
8892                                     /*initialized=*/0, &attributes);
8893   if (attributes)
8894     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8895   return type_specified;
8896 }
8897
8898 /* Parse an (optional) conversion-declarator.
8899
8900    conversion-declarator:
8901      ptr-operator conversion-declarator [opt]
8902
8903    */
8904
8905 static cp_declarator *
8906 cp_parser_conversion_declarator_opt (cp_parser* parser)
8907 {
8908   enum tree_code code;
8909   tree class_type;
8910   cp_cv_quals cv_quals;
8911
8912   /* We don't know if there's a ptr-operator next, or not.  */
8913   cp_parser_parse_tentatively (parser);
8914   /* Try the ptr-operator.  */
8915   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8916   /* If it worked, look for more conversion-declarators.  */
8917   if (cp_parser_parse_definitely (parser))
8918     {
8919       cp_declarator *declarator;
8920
8921       /* Parse another optional declarator.  */
8922       declarator = cp_parser_conversion_declarator_opt (parser);
8923
8924       return cp_parser_make_indirect_declarator
8925         (code, class_type, cv_quals, declarator);
8926    }
8927
8928   return NULL;
8929 }
8930
8931 /* Parse an (optional) ctor-initializer.
8932
8933    ctor-initializer:
8934      : mem-initializer-list
8935
8936    Returns TRUE iff the ctor-initializer was actually present.  */
8937
8938 static bool
8939 cp_parser_ctor_initializer_opt (cp_parser* parser)
8940 {
8941   /* If the next token is not a `:', then there is no
8942      ctor-initializer.  */
8943   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8944     {
8945       /* Do default initialization of any bases and members.  */
8946       if (DECL_CONSTRUCTOR_P (current_function_decl))
8947         finish_mem_initializers (NULL_TREE);
8948
8949       return false;
8950     }
8951
8952   /* Consume the `:' token.  */
8953   cp_lexer_consume_token (parser->lexer);
8954   /* And the mem-initializer-list.  */
8955   cp_parser_mem_initializer_list (parser);
8956
8957   return true;
8958 }
8959
8960 /* Parse a mem-initializer-list.
8961
8962    mem-initializer-list:
8963      mem-initializer ... [opt]
8964      mem-initializer ... [opt] , mem-initializer-list  */
8965
8966 static void
8967 cp_parser_mem_initializer_list (cp_parser* parser)
8968 {
8969   tree mem_initializer_list = NULL_TREE;
8970   cp_token *token = cp_lexer_peek_token (parser->lexer);
8971
8972   /* Let the semantic analysis code know that we are starting the
8973      mem-initializer-list.  */
8974   if (!DECL_CONSTRUCTOR_P (current_function_decl))
8975     error ("%Honly constructors take base initializers",
8976            &token->location);
8977
8978   /* Loop through the list.  */
8979   while (true)
8980     {
8981       tree mem_initializer;
8982
8983       token = cp_lexer_peek_token (parser->lexer);
8984       /* Parse the mem-initializer.  */
8985       mem_initializer = cp_parser_mem_initializer (parser);
8986       /* If the next token is a `...', we're expanding member initializers. */
8987       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8988         {
8989           /* Consume the `...'. */
8990           cp_lexer_consume_token (parser->lexer);
8991
8992           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8993              can be expanded but members cannot. */
8994           if (mem_initializer != error_mark_node
8995               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8996             {
8997               error ("%Hcannot expand initializer for member %<%D%>",
8998                      &token->location, TREE_PURPOSE (mem_initializer));
8999               mem_initializer = error_mark_node;
9000             }
9001
9002           /* Construct the pack expansion type. */
9003           if (mem_initializer != error_mark_node)
9004             mem_initializer = make_pack_expansion (mem_initializer);
9005         }
9006       /* Add it to the list, unless it was erroneous.  */
9007       if (mem_initializer != error_mark_node)
9008         {
9009           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9010           mem_initializer_list = mem_initializer;
9011         }
9012       /* If the next token is not a `,', we're done.  */
9013       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9014         break;
9015       /* Consume the `,' token.  */
9016       cp_lexer_consume_token (parser->lexer);
9017     }
9018
9019   /* Perform semantic analysis.  */
9020   if (DECL_CONSTRUCTOR_P (current_function_decl))
9021     finish_mem_initializers (mem_initializer_list);
9022 }
9023
9024 /* Parse a mem-initializer.
9025
9026    mem-initializer:
9027      mem-initializer-id ( expression-list [opt] )
9028      mem-initializer-id braced-init-list
9029
9030    GNU extension:
9031
9032    mem-initializer:
9033      ( expression-list [opt] )
9034
9035    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9036    class) or FIELD_DECL (for a non-static data member) to initialize;
9037    the TREE_VALUE is the expression-list.  An empty initialization
9038    list is represented by void_list_node.  */
9039
9040 static tree
9041 cp_parser_mem_initializer (cp_parser* parser)
9042 {
9043   tree mem_initializer_id;
9044   tree expression_list;
9045   tree member;
9046   cp_token *token = cp_lexer_peek_token (parser->lexer);
9047
9048   /* Find out what is being initialized.  */
9049   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9050     {
9051       permerror (token->location,
9052                  "anachronistic old-style base class initializer");
9053       mem_initializer_id = NULL_TREE;
9054     }
9055   else
9056     mem_initializer_id = cp_parser_mem_initializer_id (parser);
9057   member = expand_member_init (mem_initializer_id);
9058   if (member && !DECL_P (member))
9059     in_base_initializer = 1;
9060
9061   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9062     {
9063       bool expr_non_constant_p;
9064       maybe_warn_cpp0x ("extended initializer lists");
9065       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9066       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9067       expression_list = build_tree_list (NULL_TREE, expression_list);
9068     }
9069   else
9070     expression_list
9071       = cp_parser_parenthesized_expression_list (parser, false,
9072                                                  /*cast_p=*/false,
9073                                                  /*allow_expansion_p=*/true,
9074                                                  /*non_constant_p=*/NULL);
9075   if (expression_list == error_mark_node)
9076     return error_mark_node;
9077   if (!expression_list)
9078     expression_list = void_type_node;
9079
9080   in_base_initializer = 0;
9081
9082   return member ? build_tree_list (member, expression_list) : error_mark_node;
9083 }
9084
9085 /* Parse a mem-initializer-id.
9086
9087    mem-initializer-id:
9088      :: [opt] nested-name-specifier [opt] class-name
9089      identifier
9090
9091    Returns a TYPE indicating the class to be initializer for the first
9092    production.  Returns an IDENTIFIER_NODE indicating the data member
9093    to be initialized for the second production.  */
9094
9095 static tree
9096 cp_parser_mem_initializer_id (cp_parser* parser)
9097 {
9098   bool global_scope_p;
9099   bool nested_name_specifier_p;
9100   bool template_p = false;
9101   tree id;
9102
9103   cp_token *token = cp_lexer_peek_token (parser->lexer);
9104
9105   /* `typename' is not allowed in this context ([temp.res]).  */
9106   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9107     {
9108       error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9109              "member initializer is implicitly a type)",
9110              &token->location);
9111       cp_lexer_consume_token (parser->lexer);
9112     }
9113   /* Look for the optional `::' operator.  */
9114   global_scope_p
9115     = (cp_parser_global_scope_opt (parser,
9116                                    /*current_scope_valid_p=*/false)
9117        != NULL_TREE);
9118   /* Look for the optional nested-name-specifier.  The simplest way to
9119      implement:
9120
9121        [temp.res]
9122
9123        The keyword `typename' is not permitted in a base-specifier or
9124        mem-initializer; in these contexts a qualified name that
9125        depends on a template-parameter is implicitly assumed to be a
9126        type name.
9127
9128      is to assume that we have seen the `typename' keyword at this
9129      point.  */
9130   nested_name_specifier_p
9131     = (cp_parser_nested_name_specifier_opt (parser,
9132                                             /*typename_keyword_p=*/true,
9133                                             /*check_dependency_p=*/true,
9134                                             /*type_p=*/true,
9135                                             /*is_declaration=*/true)
9136        != NULL_TREE);
9137   if (nested_name_specifier_p)
9138     template_p = cp_parser_optional_template_keyword (parser);
9139   /* If there is a `::' operator or a nested-name-specifier, then we
9140      are definitely looking for a class-name.  */
9141   if (global_scope_p || nested_name_specifier_p)
9142     return cp_parser_class_name (parser,
9143                                  /*typename_keyword_p=*/true,
9144                                  /*template_keyword_p=*/template_p,
9145                                  none_type,
9146                                  /*check_dependency_p=*/true,
9147                                  /*class_head_p=*/false,
9148                                  /*is_declaration=*/true);
9149   /* Otherwise, we could also be looking for an ordinary identifier.  */
9150   cp_parser_parse_tentatively (parser);
9151   /* Try a class-name.  */
9152   id = cp_parser_class_name (parser,
9153                              /*typename_keyword_p=*/true,
9154                              /*template_keyword_p=*/false,
9155                              none_type,
9156                              /*check_dependency_p=*/true,
9157                              /*class_head_p=*/false,
9158                              /*is_declaration=*/true);
9159   /* If we found one, we're done.  */
9160   if (cp_parser_parse_definitely (parser))
9161     return id;
9162   /* Otherwise, look for an ordinary identifier.  */
9163   return cp_parser_identifier (parser);
9164 }
9165
9166 /* Overloading [gram.over] */
9167
9168 /* Parse an operator-function-id.
9169
9170    operator-function-id:
9171      operator operator
9172
9173    Returns an IDENTIFIER_NODE for the operator which is a
9174    human-readable spelling of the identifier, e.g., `operator +'.  */
9175
9176 static tree
9177 cp_parser_operator_function_id (cp_parser* parser)
9178 {
9179   /* Look for the `operator' keyword.  */
9180   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9181     return error_mark_node;
9182   /* And then the name of the operator itself.  */
9183   return cp_parser_operator (parser);
9184 }
9185
9186 /* Parse an operator.
9187
9188    operator:
9189      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9190      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9191      || ++ -- , ->* -> () []
9192
9193    GNU Extensions:
9194
9195    operator:
9196      <? >? <?= >?=
9197
9198    Returns an IDENTIFIER_NODE for the operator which is a
9199    human-readable spelling of the identifier, e.g., `operator +'.  */
9200
9201 static tree
9202 cp_parser_operator (cp_parser* parser)
9203 {
9204   tree id = NULL_TREE;
9205   cp_token *token;
9206
9207   /* Peek at the next token.  */
9208   token = cp_lexer_peek_token (parser->lexer);
9209   /* Figure out which operator we have.  */
9210   switch (token->type)
9211     {
9212     case CPP_KEYWORD:
9213       {
9214         enum tree_code op;
9215
9216         /* The keyword should be either `new' or `delete'.  */
9217         if (token->keyword == RID_NEW)
9218           op = NEW_EXPR;
9219         else if (token->keyword == RID_DELETE)
9220           op = DELETE_EXPR;
9221         else
9222           break;
9223
9224         /* Consume the `new' or `delete' token.  */
9225         cp_lexer_consume_token (parser->lexer);
9226
9227         /* Peek at the next token.  */
9228         token = cp_lexer_peek_token (parser->lexer);
9229         /* If it's a `[' token then this is the array variant of the
9230            operator.  */
9231         if (token->type == CPP_OPEN_SQUARE)
9232           {
9233             /* Consume the `[' token.  */
9234             cp_lexer_consume_token (parser->lexer);
9235             /* Look for the `]' token.  */
9236             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9237             id = ansi_opname (op == NEW_EXPR
9238                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9239           }
9240         /* Otherwise, we have the non-array variant.  */
9241         else
9242           id = ansi_opname (op);
9243
9244         return id;
9245       }
9246
9247     case CPP_PLUS:
9248       id = ansi_opname (PLUS_EXPR);
9249       break;
9250
9251     case CPP_MINUS:
9252       id = ansi_opname (MINUS_EXPR);
9253       break;
9254
9255     case CPP_MULT:
9256       id = ansi_opname (MULT_EXPR);
9257       break;
9258
9259     case CPP_DIV:
9260       id = ansi_opname (TRUNC_DIV_EXPR);
9261       break;
9262
9263     case CPP_MOD:
9264       id = ansi_opname (TRUNC_MOD_EXPR);
9265       break;
9266
9267     case CPP_XOR:
9268       id = ansi_opname (BIT_XOR_EXPR);
9269       break;
9270
9271     case CPP_AND:
9272       id = ansi_opname (BIT_AND_EXPR);
9273       break;
9274
9275     case CPP_OR:
9276       id = ansi_opname (BIT_IOR_EXPR);
9277       break;
9278
9279     case CPP_COMPL:
9280       id = ansi_opname (BIT_NOT_EXPR);
9281       break;
9282
9283     case CPP_NOT:
9284       id = ansi_opname (TRUTH_NOT_EXPR);
9285       break;
9286
9287     case CPP_EQ:
9288       id = ansi_assopname (NOP_EXPR);
9289       break;
9290
9291     case CPP_LESS:
9292       id = ansi_opname (LT_EXPR);
9293       break;
9294
9295     case CPP_GREATER:
9296       id = ansi_opname (GT_EXPR);
9297       break;
9298
9299     case CPP_PLUS_EQ:
9300       id = ansi_assopname (PLUS_EXPR);
9301       break;
9302
9303     case CPP_MINUS_EQ:
9304       id = ansi_assopname (MINUS_EXPR);
9305       break;
9306
9307     case CPP_MULT_EQ:
9308       id = ansi_assopname (MULT_EXPR);
9309       break;
9310
9311     case CPP_DIV_EQ:
9312       id = ansi_assopname (TRUNC_DIV_EXPR);
9313       break;
9314
9315     case CPP_MOD_EQ:
9316       id = ansi_assopname (TRUNC_MOD_EXPR);
9317       break;
9318
9319     case CPP_XOR_EQ:
9320       id = ansi_assopname (BIT_XOR_EXPR);
9321       break;
9322
9323     case CPP_AND_EQ:
9324       id = ansi_assopname (BIT_AND_EXPR);
9325       break;
9326
9327     case CPP_OR_EQ:
9328       id = ansi_assopname (BIT_IOR_EXPR);
9329       break;
9330
9331     case CPP_LSHIFT:
9332       id = ansi_opname (LSHIFT_EXPR);
9333       break;
9334
9335     case CPP_RSHIFT:
9336       id = ansi_opname (RSHIFT_EXPR);
9337       break;
9338
9339     case CPP_LSHIFT_EQ:
9340       id = ansi_assopname (LSHIFT_EXPR);
9341       break;
9342
9343     case CPP_RSHIFT_EQ:
9344       id = ansi_assopname (RSHIFT_EXPR);
9345       break;
9346
9347     case CPP_EQ_EQ:
9348       id = ansi_opname (EQ_EXPR);
9349       break;
9350
9351     case CPP_NOT_EQ:
9352       id = ansi_opname (NE_EXPR);
9353       break;
9354
9355     case CPP_LESS_EQ:
9356       id = ansi_opname (LE_EXPR);
9357       break;
9358
9359     case CPP_GREATER_EQ:
9360       id = ansi_opname (GE_EXPR);
9361       break;
9362
9363     case CPP_AND_AND:
9364       id = ansi_opname (TRUTH_ANDIF_EXPR);
9365       break;
9366
9367     case CPP_OR_OR:
9368       id = ansi_opname (TRUTH_ORIF_EXPR);
9369       break;
9370
9371     case CPP_PLUS_PLUS:
9372       id = ansi_opname (POSTINCREMENT_EXPR);
9373       break;
9374
9375     case CPP_MINUS_MINUS:
9376       id = ansi_opname (PREDECREMENT_EXPR);
9377       break;
9378
9379     case CPP_COMMA:
9380       id = ansi_opname (COMPOUND_EXPR);
9381       break;
9382
9383     case CPP_DEREF_STAR:
9384       id = ansi_opname (MEMBER_REF);
9385       break;
9386
9387     case CPP_DEREF:
9388       id = ansi_opname (COMPONENT_REF);
9389       break;
9390
9391     case CPP_OPEN_PAREN:
9392       /* Consume the `('.  */
9393       cp_lexer_consume_token (parser->lexer);
9394       /* Look for the matching `)'.  */
9395       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9396       return ansi_opname (CALL_EXPR);
9397
9398     case CPP_OPEN_SQUARE:
9399       /* Consume the `['.  */
9400       cp_lexer_consume_token (parser->lexer);
9401       /* Look for the matching `]'.  */
9402       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9403       return ansi_opname (ARRAY_REF);
9404
9405     default:
9406       /* Anything else is an error.  */
9407       break;
9408     }
9409
9410   /* If we have selected an identifier, we need to consume the
9411      operator token.  */
9412   if (id)
9413     cp_lexer_consume_token (parser->lexer);
9414   /* Otherwise, no valid operator name was present.  */
9415   else
9416     {
9417       cp_parser_error (parser, "expected operator");
9418       id = error_mark_node;
9419     }
9420
9421   return id;
9422 }
9423
9424 /* Parse a template-declaration.
9425
9426    template-declaration:
9427      export [opt] template < template-parameter-list > declaration
9428
9429    If MEMBER_P is TRUE, this template-declaration occurs within a
9430    class-specifier.
9431
9432    The grammar rule given by the standard isn't correct.  What
9433    is really meant is:
9434
9435    template-declaration:
9436      export [opt] template-parameter-list-seq
9437        decl-specifier-seq [opt] init-declarator [opt] ;
9438      export [opt] template-parameter-list-seq
9439        function-definition
9440
9441    template-parameter-list-seq:
9442      template-parameter-list-seq [opt]
9443      template < template-parameter-list >  */
9444
9445 static void
9446 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9447 {
9448   /* Check for `export'.  */
9449   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9450     {
9451       /* Consume the `export' token.  */
9452       cp_lexer_consume_token (parser->lexer);
9453       /* Warn that we do not support `export'.  */
9454       warning (0, "keyword %<export%> not implemented, and will be ignored");
9455     }
9456
9457   cp_parser_template_declaration_after_export (parser, member_p);
9458 }
9459
9460 /* Parse a template-parameter-list.
9461
9462    template-parameter-list:
9463      template-parameter
9464      template-parameter-list , template-parameter
9465
9466    Returns a TREE_LIST.  Each node represents a template parameter.
9467    The nodes are connected via their TREE_CHAINs.  */
9468
9469 static tree
9470 cp_parser_template_parameter_list (cp_parser* parser)
9471 {
9472   tree parameter_list = NULL_TREE;
9473
9474   begin_template_parm_list ();
9475   while (true)
9476     {
9477       tree parameter;
9478       bool is_non_type;
9479       bool is_parameter_pack;
9480
9481       /* Parse the template-parameter.  */
9482       parameter = cp_parser_template_parameter (parser, 
9483                                                 &is_non_type,
9484                                                 &is_parameter_pack);
9485       /* Add it to the list.  */
9486       if (parameter != error_mark_node)
9487         parameter_list = process_template_parm (parameter_list,
9488                                                 parameter,
9489                                                 is_non_type,
9490                                                 is_parameter_pack);
9491       else
9492        {
9493          tree err_parm = build_tree_list (parameter, parameter);
9494          TREE_VALUE (err_parm) = error_mark_node;
9495          parameter_list = chainon (parameter_list, err_parm);
9496        }
9497
9498       /* If the next token is not a `,', we're done.  */
9499       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9500         break;
9501       /* Otherwise, consume the `,' token.  */
9502       cp_lexer_consume_token (parser->lexer);
9503     }
9504
9505   return end_template_parm_list (parameter_list);
9506 }
9507
9508 /* Parse a template-parameter.
9509
9510    template-parameter:
9511      type-parameter
9512      parameter-declaration
9513
9514    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
9515    the parameter.  The TREE_PURPOSE is the default value, if any.
9516    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
9517    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
9518    set to true iff this parameter is a parameter pack. */
9519
9520 static tree
9521 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9522                               bool *is_parameter_pack)
9523 {
9524   cp_token *token;
9525   cp_parameter_declarator *parameter_declarator;
9526   cp_declarator *id_declarator;
9527   tree parm;
9528
9529   /* Assume it is a type parameter or a template parameter.  */
9530   *is_non_type = false;
9531   /* Assume it not a parameter pack. */
9532   *is_parameter_pack = false;
9533   /* Peek at the next token.  */
9534   token = cp_lexer_peek_token (parser->lexer);
9535   /* If it is `class' or `template', we have a type-parameter.  */
9536   if (token->keyword == RID_TEMPLATE)
9537     return cp_parser_type_parameter (parser, is_parameter_pack);
9538   /* If it is `class' or `typename' we do not know yet whether it is a
9539      type parameter or a non-type parameter.  Consider:
9540
9541        template <typename T, typename T::X X> ...
9542
9543      or:
9544
9545        template <class C, class D*> ...
9546
9547      Here, the first parameter is a type parameter, and the second is
9548      a non-type parameter.  We can tell by looking at the token after
9549      the identifier -- if it is a `,', `=', or `>' then we have a type
9550      parameter.  */
9551   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9552     {
9553       /* Peek at the token after `class' or `typename'.  */
9554       token = cp_lexer_peek_nth_token (parser->lexer, 2);
9555       /* If it's an ellipsis, we have a template type parameter
9556          pack. */
9557       if (token->type == CPP_ELLIPSIS)
9558         return cp_parser_type_parameter (parser, is_parameter_pack);
9559       /* If it's an identifier, skip it.  */
9560       if (token->type == CPP_NAME)
9561         token = cp_lexer_peek_nth_token (parser->lexer, 3);
9562       /* Now, see if the token looks like the end of a template
9563          parameter.  */
9564       if (token->type == CPP_COMMA
9565           || token->type == CPP_EQ
9566           || token->type == CPP_GREATER)
9567         return cp_parser_type_parameter (parser, is_parameter_pack);
9568     }
9569
9570   /* Otherwise, it is a non-type parameter.
9571
9572      [temp.param]
9573
9574      When parsing a default template-argument for a non-type
9575      template-parameter, the first non-nested `>' is taken as the end
9576      of the template parameter-list rather than a greater-than
9577      operator.  */
9578   *is_non_type = true;
9579   parameter_declarator
9580      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9581                                         /*parenthesized_p=*/NULL);
9582
9583   /* If the parameter declaration is marked as a parameter pack, set
9584      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9585      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9586      grokdeclarator. */
9587   if (parameter_declarator
9588       && parameter_declarator->declarator
9589       && parameter_declarator->declarator->parameter_pack_p)
9590     {
9591       *is_parameter_pack = true;
9592       parameter_declarator->declarator->parameter_pack_p = false;
9593     }
9594
9595   /* If the next token is an ellipsis, and we don't already have it
9596      marked as a parameter pack, then we have a parameter pack (that
9597      has no declarator).  */
9598   if (!*is_parameter_pack
9599       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9600       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9601     {
9602       /* Consume the `...'.  */
9603       cp_lexer_consume_token (parser->lexer);
9604       maybe_warn_variadic_templates ();
9605       
9606       *is_parameter_pack = true;
9607     }
9608   /* We might end up with a pack expansion as the type of the non-type
9609      template parameter, in which case this is a non-type template
9610      parameter pack.  */
9611   else if (parameter_declarator
9612            && parameter_declarator->decl_specifiers.type
9613            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9614     {
9615       *is_parameter_pack = true;
9616       parameter_declarator->decl_specifiers.type = 
9617         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9618     }
9619
9620   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9621     {
9622       /* Parameter packs cannot have default arguments.  However, a
9623          user may try to do so, so we'll parse them and give an
9624          appropriate diagnostic here.  */
9625
9626       /* Consume the `='.  */
9627       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9628       cp_lexer_consume_token (parser->lexer);
9629       
9630       /* Find the name of the parameter pack.  */     
9631       id_declarator = parameter_declarator->declarator;
9632       while (id_declarator && id_declarator->kind != cdk_id)
9633         id_declarator = id_declarator->declarator;
9634       
9635       if (id_declarator && id_declarator->kind == cdk_id)
9636         error ("%Htemplate parameter pack %qD cannot have a default argument",
9637                &start_token->location, id_declarator->u.id.unqualified_name);
9638       else
9639         error ("%Htemplate parameter pack cannot have a default argument",
9640                &start_token->location);
9641       
9642       /* Parse the default argument, but throw away the result.  */
9643       cp_parser_default_argument (parser, /*template_parm_p=*/true);
9644     }
9645
9646   parm = grokdeclarator (parameter_declarator->declarator,
9647                          &parameter_declarator->decl_specifiers,
9648                          PARM, /*initialized=*/0,
9649                          /*attrlist=*/NULL);
9650   if (parm == error_mark_node)
9651     return error_mark_node;
9652
9653   return build_tree_list (parameter_declarator->default_argument, parm);
9654 }
9655
9656 /* Parse a type-parameter.
9657
9658    type-parameter:
9659      class identifier [opt]
9660      class identifier [opt] = type-id
9661      typename identifier [opt]
9662      typename identifier [opt] = type-id
9663      template < template-parameter-list > class identifier [opt]
9664      template < template-parameter-list > class identifier [opt]
9665        = id-expression
9666
9667    GNU Extension (variadic templates):
9668
9669    type-parameter:
9670      class ... identifier [opt]
9671      typename ... identifier [opt]
9672
9673    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
9674    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
9675    the declaration of the parameter.
9676
9677    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9678
9679 static tree
9680 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9681 {
9682   cp_token *token;
9683   tree parameter;
9684
9685   /* Look for a keyword to tell us what kind of parameter this is.  */
9686   token = cp_parser_require (parser, CPP_KEYWORD,
9687                              "%<class%>, %<typename%>, or %<template%>");
9688   if (!token)
9689     return error_mark_node;
9690
9691   switch (token->keyword)
9692     {
9693     case RID_CLASS:
9694     case RID_TYPENAME:
9695       {
9696         tree identifier;
9697         tree default_argument;
9698
9699         /* If the next token is an ellipsis, we have a template
9700            argument pack. */
9701         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9702           {
9703             /* Consume the `...' token. */
9704             cp_lexer_consume_token (parser->lexer);
9705             maybe_warn_variadic_templates ();
9706
9707             *is_parameter_pack = true;
9708           }
9709
9710         /* If the next token is an identifier, then it names the
9711            parameter.  */
9712         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9713           identifier = cp_parser_identifier (parser);
9714         else
9715           identifier = NULL_TREE;
9716
9717         /* Create the parameter.  */
9718         parameter = finish_template_type_parm (class_type_node, identifier);
9719
9720         /* If the next token is an `=', we have a default argument.  */
9721         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9722           {
9723             /* Consume the `=' token.  */
9724             cp_lexer_consume_token (parser->lexer);
9725             /* Parse the default-argument.  */
9726             push_deferring_access_checks (dk_no_deferred);
9727             default_argument = cp_parser_type_id (parser);
9728
9729             /* Template parameter packs cannot have default
9730                arguments. */
9731             if (*is_parameter_pack)
9732               {
9733                 if (identifier)
9734                   error ("%Htemplate parameter pack %qD cannot have a "
9735                          "default argument", &token->location, identifier);
9736                 else
9737                   error ("%Htemplate parameter packs cannot have "
9738                          "default arguments", &token->location);
9739                 default_argument = NULL_TREE;
9740               }
9741             pop_deferring_access_checks ();
9742           }
9743         else
9744           default_argument = NULL_TREE;
9745
9746         /* Create the combined representation of the parameter and the
9747            default argument.  */
9748         parameter = build_tree_list (default_argument, parameter);
9749       }
9750       break;
9751
9752     case RID_TEMPLATE:
9753       {
9754         tree parameter_list;
9755         tree identifier;
9756         tree default_argument;
9757
9758         /* Look for the `<'.  */
9759         cp_parser_require (parser, CPP_LESS, "%<<%>");
9760         /* Parse the template-parameter-list.  */
9761         parameter_list = cp_parser_template_parameter_list (parser);
9762         /* Look for the `>'.  */
9763         cp_parser_require (parser, CPP_GREATER, "%<>%>");
9764         /* Look for the `class' keyword.  */
9765         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9766         /* If the next token is an ellipsis, we have a template
9767            argument pack. */
9768         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9769           {
9770             /* Consume the `...' token. */
9771             cp_lexer_consume_token (parser->lexer);
9772             maybe_warn_variadic_templates ();
9773
9774             *is_parameter_pack = true;
9775           }
9776         /* If the next token is an `=', then there is a
9777            default-argument.  If the next token is a `>', we are at
9778            the end of the parameter-list.  If the next token is a `,',
9779            then we are at the end of this parameter.  */
9780         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9781             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9782             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9783           {
9784             identifier = cp_parser_identifier (parser);
9785             /* Treat invalid names as if the parameter were nameless.  */
9786             if (identifier == error_mark_node)
9787               identifier = NULL_TREE;
9788           }
9789         else
9790           identifier = NULL_TREE;
9791
9792         /* Create the template parameter.  */
9793         parameter = finish_template_template_parm (class_type_node,
9794                                                    identifier);
9795
9796         /* If the next token is an `=', then there is a
9797            default-argument.  */
9798         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9799           {
9800             bool is_template;
9801
9802             /* Consume the `='.  */
9803             cp_lexer_consume_token (parser->lexer);
9804             /* Parse the id-expression.  */
9805             push_deferring_access_checks (dk_no_deferred);
9806             /* save token before parsing the id-expression, for error
9807                reporting */
9808             token = cp_lexer_peek_token (parser->lexer);
9809             default_argument
9810               = cp_parser_id_expression (parser,
9811                                          /*template_keyword_p=*/false,
9812                                          /*check_dependency_p=*/true,
9813                                          /*template_p=*/&is_template,
9814                                          /*declarator_p=*/false,
9815                                          /*optional_p=*/false);
9816             if (TREE_CODE (default_argument) == TYPE_DECL)
9817               /* If the id-expression was a template-id that refers to
9818                  a template-class, we already have the declaration here,
9819                  so no further lookup is needed.  */
9820                  ;
9821             else
9822               /* Look up the name.  */
9823               default_argument
9824                 = cp_parser_lookup_name (parser, default_argument,
9825                                          none_type,
9826                                          /*is_template=*/is_template,
9827                                          /*is_namespace=*/false,
9828                                          /*check_dependency=*/true,
9829                                          /*ambiguous_decls=*/NULL,
9830                                          token->location);
9831             /* See if the default argument is valid.  */
9832             default_argument
9833               = check_template_template_default_arg (default_argument);
9834
9835             /* Template parameter packs cannot have default
9836                arguments. */
9837             if (*is_parameter_pack)
9838               {
9839                 if (identifier)
9840                   error ("%Htemplate parameter pack %qD cannot "
9841                          "have a default argument",
9842                          &token->location, identifier);
9843                 else
9844                   error ("%Htemplate parameter packs cannot "
9845                          "have default arguments",
9846                          &token->location);
9847                 default_argument = NULL_TREE;
9848               }
9849             pop_deferring_access_checks ();
9850           }
9851         else
9852           default_argument = NULL_TREE;
9853
9854         /* Create the combined representation of the parameter and the
9855            default argument.  */
9856         parameter = build_tree_list (default_argument, parameter);
9857       }
9858       break;
9859
9860     default:
9861       gcc_unreachable ();
9862       break;
9863     }
9864
9865   return parameter;
9866 }
9867
9868 /* Parse a template-id.
9869
9870    template-id:
9871      template-name < template-argument-list [opt] >
9872
9873    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9874    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
9875    returned.  Otherwise, if the template-name names a function, or set
9876    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
9877    names a class, returns a TYPE_DECL for the specialization.
9878
9879    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9880    uninstantiated templates.  */
9881
9882 static tree
9883 cp_parser_template_id (cp_parser *parser,
9884                        bool template_keyword_p,
9885                        bool check_dependency_p,
9886                        bool is_declaration)
9887 {
9888   int i;
9889   tree templ;
9890   tree arguments;
9891   tree template_id;
9892   cp_token_position start_of_id = 0;
9893   deferred_access_check *chk;
9894   VEC (deferred_access_check,gc) *access_check;
9895   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
9896   bool is_identifier;
9897
9898   /* If the next token corresponds to a template-id, there is no need
9899      to reparse it.  */
9900   next_token = cp_lexer_peek_token (parser->lexer);
9901   if (next_token->type == CPP_TEMPLATE_ID)
9902     {
9903       struct tree_check *check_value;
9904
9905       /* Get the stored value.  */
9906       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9907       /* Perform any access checks that were deferred.  */
9908       access_check = check_value->checks;
9909       if (access_check)
9910         {
9911           for (i = 0 ;
9912                VEC_iterate (deferred_access_check, access_check, i, chk) ;
9913                ++i)
9914             {
9915               perform_or_defer_access_check (chk->binfo,
9916                                              chk->decl,
9917                                              chk->diag_decl);
9918             }
9919         }
9920       /* Return the stored value.  */
9921       return check_value->value;
9922     }
9923
9924   /* Avoid performing name lookup if there is no possibility of
9925      finding a template-id.  */
9926   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9927       || (next_token->type == CPP_NAME
9928           && !cp_parser_nth_token_starts_template_argument_list_p
9929                (parser, 2)))
9930     {
9931       cp_parser_error (parser, "expected template-id");
9932       return error_mark_node;
9933     }
9934
9935   /* Remember where the template-id starts.  */
9936   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9937     start_of_id = cp_lexer_token_position (parser->lexer, false);
9938
9939   push_deferring_access_checks (dk_deferred);
9940
9941   /* Parse the template-name.  */
9942   is_identifier = false;
9943   token = cp_lexer_peek_token (parser->lexer);
9944   templ = cp_parser_template_name (parser, template_keyword_p,
9945                                    check_dependency_p,
9946                                    is_declaration,
9947                                    &is_identifier);
9948   if (templ == error_mark_node || is_identifier)
9949     {
9950       pop_deferring_access_checks ();
9951       return templ;
9952     }
9953
9954   /* If we find the sequence `[:' after a template-name, it's probably
9955      a digraph-typo for `< ::'. Substitute the tokens and check if we can
9956      parse correctly the argument list.  */
9957   next_token = cp_lexer_peek_token (parser->lexer);
9958   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9959   if (next_token->type == CPP_OPEN_SQUARE
9960       && next_token->flags & DIGRAPH
9961       && next_token_2->type == CPP_COLON
9962       && !(next_token_2->flags & PREV_WHITE))
9963     {
9964       cp_parser_parse_tentatively (parser);
9965       /* Change `:' into `::'.  */
9966       next_token_2->type = CPP_SCOPE;
9967       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9968          CPP_LESS.  */
9969       cp_lexer_consume_token (parser->lexer);
9970
9971       /* Parse the arguments.  */
9972       arguments = cp_parser_enclosed_template_argument_list (parser);
9973       if (!cp_parser_parse_definitely (parser))
9974         {
9975           /* If we couldn't parse an argument list, then we revert our changes
9976              and return simply an error. Maybe this is not a template-id
9977              after all.  */
9978           next_token_2->type = CPP_COLON;
9979           cp_parser_error (parser, "expected %<<%>");
9980           pop_deferring_access_checks ();
9981           return error_mark_node;
9982         }
9983       /* Otherwise, emit an error about the invalid digraph, but continue
9984          parsing because we got our argument list.  */
9985       if (permerror (next_token->location,
9986                      "%<<::%> cannot begin a template-argument list"))
9987         {
9988           static bool hint = false;
9989           inform (next_token->location,
9990                   "%<<:%> is an alternate spelling for %<[%>."
9991                   " Insert whitespace between %<<%> and %<::%>");
9992           if (!hint && !flag_permissive)
9993             {
9994               inform (next_token->location, "(if you use %<-fpermissive%>"
9995                       " G++ will accept your code)");
9996               hint = true;
9997             }
9998         }
9999     }
10000   else
10001     {
10002       /* Look for the `<' that starts the template-argument-list.  */
10003       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10004         {
10005           pop_deferring_access_checks ();
10006           return error_mark_node;
10007         }
10008       /* Parse the arguments.  */
10009       arguments = cp_parser_enclosed_template_argument_list (parser);
10010     }
10011
10012   /* Build a representation of the specialization.  */
10013   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10014     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10015   else if (DECL_CLASS_TEMPLATE_P (templ)
10016            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10017     {
10018       bool entering_scope;
10019       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10020          template (rather than some instantiation thereof) only if
10021          is not nested within some other construct.  For example, in
10022          "template <typename T> void f(T) { A<T>::", A<T> is just an
10023          instantiation of A.  */
10024       entering_scope = (template_parm_scope_p ()
10025                         && cp_lexer_next_token_is (parser->lexer,
10026                                                    CPP_SCOPE));
10027       template_id
10028         = finish_template_type (templ, arguments, entering_scope);
10029     }
10030   else
10031     {
10032       /* If it's not a class-template or a template-template, it should be
10033          a function-template.  */
10034       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10035                    || TREE_CODE (templ) == OVERLOAD
10036                    || BASELINK_P (templ)));
10037
10038       template_id = lookup_template_function (templ, arguments);
10039     }
10040
10041   /* If parsing tentatively, replace the sequence of tokens that makes
10042      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10043      should we re-parse the token stream, we will not have to repeat
10044      the effort required to do the parse, nor will we issue duplicate
10045      error messages about problems during instantiation of the
10046      template.  */
10047   if (start_of_id)
10048     {
10049       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10050
10051       /* Reset the contents of the START_OF_ID token.  */
10052       token->type = CPP_TEMPLATE_ID;
10053       /* Retrieve any deferred checks.  Do not pop this access checks yet
10054          so the memory will not be reclaimed during token replacing below.  */
10055       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10056       token->u.tree_check_value->value = template_id;
10057       token->u.tree_check_value->checks = get_deferred_access_checks ();
10058       token->keyword = RID_MAX;
10059
10060       /* Purge all subsequent tokens.  */
10061       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10062
10063       /* ??? Can we actually assume that, if template_id ==
10064          error_mark_node, we will have issued a diagnostic to the
10065          user, as opposed to simply marking the tentative parse as
10066          failed?  */
10067       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10068         error ("%Hparse error in template argument list",
10069                &token->location);
10070     }
10071
10072   pop_deferring_access_checks ();
10073   return template_id;
10074 }
10075
10076 /* Parse a template-name.
10077
10078    template-name:
10079      identifier
10080
10081    The standard should actually say:
10082
10083    template-name:
10084      identifier
10085      operator-function-id
10086
10087    A defect report has been filed about this issue.
10088
10089    A conversion-function-id cannot be a template name because they cannot
10090    be part of a template-id. In fact, looking at this code:
10091
10092    a.operator K<int>()
10093
10094    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10095    It is impossible to call a templated conversion-function-id with an
10096    explicit argument list, since the only allowed template parameter is
10097    the type to which it is converting.
10098
10099    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10100    `template' keyword, in a construction like:
10101
10102      T::template f<3>()
10103
10104    In that case `f' is taken to be a template-name, even though there
10105    is no way of knowing for sure.
10106
10107    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10108    name refers to a set of overloaded functions, at least one of which
10109    is a template, or an IDENTIFIER_NODE with the name of the template,
10110    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10111    names are looked up inside uninstantiated templates.  */
10112
10113 static tree
10114 cp_parser_template_name (cp_parser* parser,
10115                          bool template_keyword_p,
10116                          bool check_dependency_p,
10117                          bool is_declaration,
10118                          bool *is_identifier)
10119 {
10120   tree identifier;
10121   tree decl;
10122   tree fns;
10123   cp_token *token = cp_lexer_peek_token (parser->lexer);
10124
10125   /* If the next token is `operator', then we have either an
10126      operator-function-id or a conversion-function-id.  */
10127   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10128     {
10129       /* We don't know whether we're looking at an
10130          operator-function-id or a conversion-function-id.  */
10131       cp_parser_parse_tentatively (parser);
10132       /* Try an operator-function-id.  */
10133       identifier = cp_parser_operator_function_id (parser);
10134       /* If that didn't work, try a conversion-function-id.  */
10135       if (!cp_parser_parse_definitely (parser))
10136         {
10137           cp_parser_error (parser, "expected template-name");
10138           return error_mark_node;
10139         }
10140     }
10141   /* Look for the identifier.  */
10142   else
10143     identifier = cp_parser_identifier (parser);
10144
10145   /* If we didn't find an identifier, we don't have a template-id.  */
10146   if (identifier == error_mark_node)
10147     return error_mark_node;
10148
10149   /* If the name immediately followed the `template' keyword, then it
10150      is a template-name.  However, if the next token is not `<', then
10151      we do not treat it as a template-name, since it is not being used
10152      as part of a template-id.  This enables us to handle constructs
10153      like:
10154
10155        template <typename T> struct S { S(); };
10156        template <typename T> S<T>::S();
10157
10158      correctly.  We would treat `S' as a template -- if it were `S<T>'
10159      -- but we do not if there is no `<'.  */
10160
10161   if (processing_template_decl
10162       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10163     {
10164       /* In a declaration, in a dependent context, we pretend that the
10165          "template" keyword was present in order to improve error
10166          recovery.  For example, given:
10167
10168            template <typename T> void f(T::X<int>);
10169
10170          we want to treat "X<int>" as a template-id.  */
10171       if (is_declaration
10172           && !template_keyword_p
10173           && parser->scope && TYPE_P (parser->scope)
10174           && check_dependency_p
10175           && dependent_type_p (parser->scope)
10176           /* Do not do this for dtors (or ctors), since they never
10177              need the template keyword before their name.  */
10178           && !constructor_name_p (identifier, parser->scope))
10179         {
10180           cp_token_position start = 0;
10181
10182           /* Explain what went wrong.  */
10183           error ("%Hnon-template %qD used as template",
10184                  &token->location, identifier);
10185           inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10186                   parser->scope, identifier);
10187           /* If parsing tentatively, find the location of the "<" token.  */
10188           if (cp_parser_simulate_error (parser))
10189             start = cp_lexer_token_position (parser->lexer, true);
10190           /* Parse the template arguments so that we can issue error
10191              messages about them.  */
10192           cp_lexer_consume_token (parser->lexer);
10193           cp_parser_enclosed_template_argument_list (parser);
10194           /* Skip tokens until we find a good place from which to
10195              continue parsing.  */
10196           cp_parser_skip_to_closing_parenthesis (parser,
10197                                                  /*recovering=*/true,
10198                                                  /*or_comma=*/true,
10199                                                  /*consume_paren=*/false);
10200           /* If parsing tentatively, permanently remove the
10201              template argument list.  That will prevent duplicate
10202              error messages from being issued about the missing
10203              "template" keyword.  */
10204           if (start)
10205             cp_lexer_purge_tokens_after (parser->lexer, start);
10206           if (is_identifier)
10207             *is_identifier = true;
10208           return identifier;
10209         }
10210
10211       /* If the "template" keyword is present, then there is generally
10212          no point in doing name-lookup, so we just return IDENTIFIER.
10213          But, if the qualifying scope is non-dependent then we can
10214          (and must) do name-lookup normally.  */
10215       if (template_keyword_p
10216           && (!parser->scope
10217               || (TYPE_P (parser->scope)
10218                   && dependent_type_p (parser->scope))))
10219         return identifier;
10220     }
10221
10222   /* Look up the name.  */
10223   decl = cp_parser_lookup_name (parser, identifier,
10224                                 none_type,
10225                                 /*is_template=*/false,
10226                                 /*is_namespace=*/false,
10227                                 check_dependency_p,
10228                                 /*ambiguous_decls=*/NULL,
10229                                 token->location);
10230   decl = maybe_get_template_decl_from_type_decl (decl);
10231
10232   /* If DECL is a template, then the name was a template-name.  */
10233   if (TREE_CODE (decl) == TEMPLATE_DECL)
10234     ;
10235   else
10236     {
10237       tree fn = NULL_TREE;
10238
10239       /* The standard does not explicitly indicate whether a name that
10240          names a set of overloaded declarations, some of which are
10241          templates, is a template-name.  However, such a name should
10242          be a template-name; otherwise, there is no way to form a
10243          template-id for the overloaded templates.  */
10244       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10245       if (TREE_CODE (fns) == OVERLOAD)
10246         for (fn = fns; fn; fn = OVL_NEXT (fn))
10247           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10248             break;
10249
10250       if (!fn)
10251         {
10252           /* The name does not name a template.  */
10253           cp_parser_error (parser, "expected template-name");
10254           return error_mark_node;
10255         }
10256     }
10257
10258   /* If DECL is dependent, and refers to a function, then just return
10259      its name; we will look it up again during template instantiation.  */
10260   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10261     {
10262       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10263       if (TYPE_P (scope) && dependent_type_p (scope))
10264         return identifier;
10265     }
10266
10267   return decl;
10268 }
10269
10270 /* Parse a template-argument-list.
10271
10272    template-argument-list:
10273      template-argument ... [opt]
10274      template-argument-list , template-argument ... [opt]
10275
10276    Returns a TREE_VEC containing the arguments.  */
10277
10278 static tree
10279 cp_parser_template_argument_list (cp_parser* parser)
10280 {
10281   tree fixed_args[10];
10282   unsigned n_args = 0;
10283   unsigned alloced = 10;
10284   tree *arg_ary = fixed_args;
10285   tree vec;
10286   bool saved_in_template_argument_list_p;
10287   bool saved_ice_p;
10288   bool saved_non_ice_p;
10289
10290   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10291   parser->in_template_argument_list_p = true;
10292   /* Even if the template-id appears in an integral
10293      constant-expression, the contents of the argument list do
10294      not.  */
10295   saved_ice_p = parser->integral_constant_expression_p;
10296   parser->integral_constant_expression_p = false;
10297   saved_non_ice_p = parser->non_integral_constant_expression_p;
10298   parser->non_integral_constant_expression_p = false;
10299   /* Parse the arguments.  */
10300   do
10301     {
10302       tree argument;
10303
10304       if (n_args)
10305         /* Consume the comma.  */
10306         cp_lexer_consume_token (parser->lexer);
10307
10308       /* Parse the template-argument.  */
10309       argument = cp_parser_template_argument (parser);
10310
10311       /* If the next token is an ellipsis, we're expanding a template
10312          argument pack. */
10313       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10314         {
10315           /* Consume the `...' token. */
10316           cp_lexer_consume_token (parser->lexer);
10317
10318           /* Make the argument into a TYPE_PACK_EXPANSION or
10319              EXPR_PACK_EXPANSION. */
10320           argument = make_pack_expansion (argument);
10321         }
10322
10323       if (n_args == alloced)
10324         {
10325           alloced *= 2;
10326
10327           if (arg_ary == fixed_args)
10328             {
10329               arg_ary = XNEWVEC (tree, alloced);
10330               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10331             }
10332           else
10333             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10334         }
10335       arg_ary[n_args++] = argument;
10336     }
10337   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10338
10339   vec = make_tree_vec (n_args);
10340
10341   while (n_args--)
10342     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10343
10344   if (arg_ary != fixed_args)
10345     free (arg_ary);
10346   parser->non_integral_constant_expression_p = saved_non_ice_p;
10347   parser->integral_constant_expression_p = saved_ice_p;
10348   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10349   return vec;
10350 }
10351
10352 /* Parse a template-argument.
10353
10354    template-argument:
10355      assignment-expression
10356      type-id
10357      id-expression
10358
10359    The representation is that of an assignment-expression, type-id, or
10360    id-expression -- except that the qualified id-expression is
10361    evaluated, so that the value returned is either a DECL or an
10362    OVERLOAD.
10363
10364    Although the standard says "assignment-expression", it forbids
10365    throw-expressions or assignments in the template argument.
10366    Therefore, we use "conditional-expression" instead.  */
10367
10368 static tree
10369 cp_parser_template_argument (cp_parser* parser)
10370 {
10371   tree argument;
10372   bool template_p;
10373   bool address_p;
10374   bool maybe_type_id = false;
10375   cp_token *token = NULL, *argument_start_token = NULL;
10376   cp_id_kind idk;
10377
10378   /* There's really no way to know what we're looking at, so we just
10379      try each alternative in order.
10380
10381        [temp.arg]
10382
10383        In a template-argument, an ambiguity between a type-id and an
10384        expression is resolved to a type-id, regardless of the form of
10385        the corresponding template-parameter.
10386
10387      Therefore, we try a type-id first.  */
10388   cp_parser_parse_tentatively (parser);
10389   argument = cp_parser_type_id (parser);
10390   /* If there was no error parsing the type-id but the next token is a
10391      '>>', our behavior depends on which dialect of C++ we're
10392      parsing. In C++98, we probably found a typo for '> >'. But there
10393      are type-id which are also valid expressions. For instance:
10394
10395      struct X { int operator >> (int); };
10396      template <int V> struct Foo {};
10397      Foo<X () >> 5> r;
10398
10399      Here 'X()' is a valid type-id of a function type, but the user just
10400      wanted to write the expression "X() >> 5". Thus, we remember that we
10401      found a valid type-id, but we still try to parse the argument as an
10402      expression to see what happens. 
10403
10404      In C++0x, the '>>' will be considered two separate '>'
10405      tokens.  */
10406   if (!cp_parser_error_occurred (parser)
10407       && cxx_dialect == cxx98
10408       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10409     {
10410       maybe_type_id = true;
10411       cp_parser_abort_tentative_parse (parser);
10412     }
10413   else
10414     {
10415       /* If the next token isn't a `,' or a `>', then this argument wasn't
10416       really finished. This means that the argument is not a valid
10417       type-id.  */
10418       if (!cp_parser_next_token_ends_template_argument_p (parser))
10419         cp_parser_error (parser, "expected template-argument");
10420       /* If that worked, we're done.  */
10421       if (cp_parser_parse_definitely (parser))
10422         return argument;
10423     }
10424   /* We're still not sure what the argument will be.  */
10425   cp_parser_parse_tentatively (parser);
10426   /* Try a template.  */
10427   argument_start_token = cp_lexer_peek_token (parser->lexer);
10428   argument = cp_parser_id_expression (parser,
10429                                       /*template_keyword_p=*/false,
10430                                       /*check_dependency_p=*/true,
10431                                       &template_p,
10432                                       /*declarator_p=*/false,
10433                                       /*optional_p=*/false);
10434   /* If the next token isn't a `,' or a `>', then this argument wasn't
10435      really finished.  */
10436   if (!cp_parser_next_token_ends_template_argument_p (parser))
10437     cp_parser_error (parser, "expected template-argument");
10438   if (!cp_parser_error_occurred (parser))
10439     {
10440       /* Figure out what is being referred to.  If the id-expression
10441          was for a class template specialization, then we will have a
10442          TYPE_DECL at this point.  There is no need to do name lookup
10443          at this point in that case.  */
10444       if (TREE_CODE (argument) != TYPE_DECL)
10445         argument = cp_parser_lookup_name (parser, argument,
10446                                           none_type,
10447                                           /*is_template=*/template_p,
10448                                           /*is_namespace=*/false,
10449                                           /*check_dependency=*/true,
10450                                           /*ambiguous_decls=*/NULL,
10451                                           argument_start_token->location);
10452       if (TREE_CODE (argument) != TEMPLATE_DECL
10453           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10454         cp_parser_error (parser, "expected template-name");
10455     }
10456   if (cp_parser_parse_definitely (parser))
10457     return argument;
10458   /* It must be a non-type argument.  There permitted cases are given
10459      in [temp.arg.nontype]:
10460
10461      -- an integral constant-expression of integral or enumeration
10462         type; or
10463
10464      -- the name of a non-type template-parameter; or
10465
10466      -- the name of an object or function with external linkage...
10467
10468      -- the address of an object or function with external linkage...
10469
10470      -- a pointer to member...  */
10471   /* Look for a non-type template parameter.  */
10472   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10473     {
10474       cp_parser_parse_tentatively (parser);
10475       argument = cp_parser_primary_expression (parser,
10476                                                /*address_p=*/false,
10477                                                /*cast_p=*/false,
10478                                                /*template_arg_p=*/true,
10479                                                &idk);
10480       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10481           || !cp_parser_next_token_ends_template_argument_p (parser))
10482         cp_parser_simulate_error (parser);
10483       if (cp_parser_parse_definitely (parser))
10484         return argument;
10485     }
10486
10487   /* If the next token is "&", the argument must be the address of an
10488      object or function with external linkage.  */
10489   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10490   if (address_p)
10491     cp_lexer_consume_token (parser->lexer);
10492   /* See if we might have an id-expression.  */
10493   token = cp_lexer_peek_token (parser->lexer);
10494   if (token->type == CPP_NAME
10495       || token->keyword == RID_OPERATOR
10496       || token->type == CPP_SCOPE
10497       || token->type == CPP_TEMPLATE_ID
10498       || token->type == CPP_NESTED_NAME_SPECIFIER)
10499     {
10500       cp_parser_parse_tentatively (parser);
10501       argument = cp_parser_primary_expression (parser,
10502                                                address_p,
10503                                                /*cast_p=*/false,
10504                                                /*template_arg_p=*/true,
10505                                                &idk);
10506       if (cp_parser_error_occurred (parser)
10507           || !cp_parser_next_token_ends_template_argument_p (parser))
10508         cp_parser_abort_tentative_parse (parser);
10509       else
10510         {
10511           if (TREE_CODE (argument) == INDIRECT_REF)
10512             {
10513               gcc_assert (REFERENCE_REF_P (argument));
10514               argument = TREE_OPERAND (argument, 0);
10515             }
10516
10517           if (TREE_CODE (argument) == VAR_DECL)
10518             {
10519               /* A variable without external linkage might still be a
10520                  valid constant-expression, so no error is issued here
10521                  if the external-linkage check fails.  */
10522               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10523                 cp_parser_simulate_error (parser);
10524             }
10525           else if (is_overloaded_fn (argument))
10526             /* All overloaded functions are allowed; if the external
10527                linkage test does not pass, an error will be issued
10528                later.  */
10529             ;
10530           else if (address_p
10531                    && (TREE_CODE (argument) == OFFSET_REF
10532                        || TREE_CODE (argument) == SCOPE_REF))
10533             /* A pointer-to-member.  */
10534             ;
10535           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10536             ;
10537           else
10538             cp_parser_simulate_error (parser);
10539
10540           if (cp_parser_parse_definitely (parser))
10541             {
10542               if (address_p)
10543                 argument = build_x_unary_op (ADDR_EXPR, argument,
10544                                              tf_warning_or_error);
10545               return argument;
10546             }
10547         }
10548     }
10549   /* If the argument started with "&", there are no other valid
10550      alternatives at this point.  */
10551   if (address_p)
10552     {
10553       cp_parser_error (parser, "invalid non-type template argument");
10554       return error_mark_node;
10555     }
10556
10557   /* If the argument wasn't successfully parsed as a type-id followed
10558      by '>>', the argument can only be a constant expression now.
10559      Otherwise, we try parsing the constant-expression tentatively,
10560      because the argument could really be a type-id.  */
10561   if (maybe_type_id)
10562     cp_parser_parse_tentatively (parser);
10563   argument = cp_parser_constant_expression (parser,
10564                                             /*allow_non_constant_p=*/false,
10565                                             /*non_constant_p=*/NULL);
10566   argument = fold_non_dependent_expr (argument);
10567   if (!maybe_type_id)
10568     return argument;
10569   if (!cp_parser_next_token_ends_template_argument_p (parser))
10570     cp_parser_error (parser, "expected template-argument");
10571   if (cp_parser_parse_definitely (parser))
10572     return argument;
10573   /* We did our best to parse the argument as a non type-id, but that
10574      was the only alternative that matched (albeit with a '>' after
10575      it). We can assume it's just a typo from the user, and a
10576      diagnostic will then be issued.  */
10577   return cp_parser_type_id (parser);
10578 }
10579
10580 /* Parse an explicit-instantiation.
10581
10582    explicit-instantiation:
10583      template declaration
10584
10585    Although the standard says `declaration', what it really means is:
10586
10587    explicit-instantiation:
10588      template decl-specifier-seq [opt] declarator [opt] ;
10589
10590    Things like `template int S<int>::i = 5, int S<double>::j;' are not
10591    supposed to be allowed.  A defect report has been filed about this
10592    issue.
10593
10594    GNU Extension:
10595
10596    explicit-instantiation:
10597      storage-class-specifier template
10598        decl-specifier-seq [opt] declarator [opt] ;
10599      function-specifier template
10600        decl-specifier-seq [opt] declarator [opt] ;  */
10601
10602 static void
10603 cp_parser_explicit_instantiation (cp_parser* parser)
10604 {
10605   int declares_class_or_enum;
10606   cp_decl_specifier_seq decl_specifiers;
10607   tree extension_specifier = NULL_TREE;
10608   cp_token *token;
10609
10610   /* Look for an (optional) storage-class-specifier or
10611      function-specifier.  */
10612   if (cp_parser_allow_gnu_extensions_p (parser))
10613     {
10614       extension_specifier
10615         = cp_parser_storage_class_specifier_opt (parser);
10616       if (!extension_specifier)
10617         extension_specifier
10618           = cp_parser_function_specifier_opt (parser,
10619                                               /*decl_specs=*/NULL);
10620     }
10621
10622   /* Look for the `template' keyword.  */
10623   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10624   /* Let the front end know that we are processing an explicit
10625      instantiation.  */
10626   begin_explicit_instantiation ();
10627   /* [temp.explicit] says that we are supposed to ignore access
10628      control while processing explicit instantiation directives.  */
10629   push_deferring_access_checks (dk_no_check);
10630   /* Parse a decl-specifier-seq.  */
10631   token = cp_lexer_peek_token (parser->lexer);
10632   cp_parser_decl_specifier_seq (parser,
10633                                 CP_PARSER_FLAGS_OPTIONAL,
10634                                 &decl_specifiers,
10635                                 &declares_class_or_enum);
10636   /* If there was exactly one decl-specifier, and it declared a class,
10637      and there's no declarator, then we have an explicit type
10638      instantiation.  */
10639   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10640     {
10641       tree type;
10642
10643       type = check_tag_decl (&decl_specifiers);
10644       /* Turn access control back on for names used during
10645          template instantiation.  */
10646       pop_deferring_access_checks ();
10647       if (type)
10648         do_type_instantiation (type, extension_specifier,
10649                                /*complain=*/tf_error);
10650     }
10651   else
10652     {
10653       cp_declarator *declarator;
10654       tree decl;
10655
10656       /* Parse the declarator.  */
10657       declarator
10658         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10659                                 /*ctor_dtor_or_conv_p=*/NULL,
10660                                 /*parenthesized_p=*/NULL,
10661                                 /*member_p=*/false);
10662       if (declares_class_or_enum & 2)
10663         cp_parser_check_for_definition_in_return_type (declarator,
10664                                                        decl_specifiers.type,
10665                                                        decl_specifiers.type_location);
10666       if (declarator != cp_error_declarator)
10667         {
10668           decl = grokdeclarator (declarator, &decl_specifiers,
10669                                  NORMAL, 0, &decl_specifiers.attributes);
10670           /* Turn access control back on for names used during
10671              template instantiation.  */
10672           pop_deferring_access_checks ();
10673           /* Do the explicit instantiation.  */
10674           do_decl_instantiation (decl, extension_specifier);
10675         }
10676       else
10677         {
10678           pop_deferring_access_checks ();
10679           /* Skip the body of the explicit instantiation.  */
10680           cp_parser_skip_to_end_of_statement (parser);
10681         }
10682     }
10683   /* We're done with the instantiation.  */
10684   end_explicit_instantiation ();
10685
10686   cp_parser_consume_semicolon_at_end_of_statement (parser);
10687 }
10688
10689 /* Parse an explicit-specialization.
10690
10691    explicit-specialization:
10692      template < > declaration
10693
10694    Although the standard says `declaration', what it really means is:
10695
10696    explicit-specialization:
10697      template <> decl-specifier [opt] init-declarator [opt] ;
10698      template <> function-definition
10699      template <> explicit-specialization
10700      template <> template-declaration  */
10701
10702 static void
10703 cp_parser_explicit_specialization (cp_parser* parser)
10704 {
10705   bool need_lang_pop;
10706   cp_token *token = cp_lexer_peek_token (parser->lexer);
10707
10708   /* Look for the `template' keyword.  */
10709   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10710   /* Look for the `<'.  */
10711   cp_parser_require (parser, CPP_LESS, "%<<%>");
10712   /* Look for the `>'.  */
10713   cp_parser_require (parser, CPP_GREATER, "%<>%>");
10714   /* We have processed another parameter list.  */
10715   ++parser->num_template_parameter_lists;
10716   /* [temp]
10717
10718      A template ... explicit specialization ... shall not have C
10719      linkage.  */
10720   if (current_lang_name == lang_name_c)
10721     {
10722       error ("%Htemplate specialization with C linkage", &token->location);
10723       /* Give it C++ linkage to avoid confusing other parts of the
10724          front end.  */
10725       push_lang_context (lang_name_cplusplus);
10726       need_lang_pop = true;
10727     }
10728   else
10729     need_lang_pop = false;
10730   /* Let the front end know that we are beginning a specialization.  */
10731   if (!begin_specialization ())
10732     {
10733       end_specialization ();
10734       cp_parser_skip_to_end_of_block_or_statement (parser);
10735       return;
10736     }
10737
10738   /* If the next keyword is `template', we need to figure out whether
10739      or not we're looking a template-declaration.  */
10740   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10741     {
10742       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10743           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10744         cp_parser_template_declaration_after_export (parser,
10745                                                      /*member_p=*/false);
10746       else
10747         cp_parser_explicit_specialization (parser);
10748     }
10749   else
10750     /* Parse the dependent declaration.  */
10751     cp_parser_single_declaration (parser,
10752                                   /*checks=*/NULL,
10753                                   /*member_p=*/false,
10754                                   /*explicit_specialization_p=*/true,
10755                                   /*friend_p=*/NULL);
10756   /* We're done with the specialization.  */
10757   end_specialization ();
10758   /* For the erroneous case of a template with C linkage, we pushed an
10759      implicit C++ linkage scope; exit that scope now.  */
10760   if (need_lang_pop)
10761     pop_lang_context ();
10762   /* We're done with this parameter list.  */
10763   --parser->num_template_parameter_lists;
10764 }
10765
10766 /* Parse a type-specifier.
10767
10768    type-specifier:
10769      simple-type-specifier
10770      class-specifier
10771      enum-specifier
10772      elaborated-type-specifier
10773      cv-qualifier
10774
10775    GNU Extension:
10776
10777    type-specifier:
10778      __complex__
10779
10780    Returns a representation of the type-specifier.  For a
10781    class-specifier, enum-specifier, or elaborated-type-specifier, a
10782    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10783
10784    The parser flags FLAGS is used to control type-specifier parsing.
10785
10786    If IS_DECLARATION is TRUE, then this type-specifier is appearing
10787    in a decl-specifier-seq.
10788
10789    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10790    class-specifier, enum-specifier, or elaborated-type-specifier, then
10791    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
10792    if a type is declared; 2 if it is defined.  Otherwise, it is set to
10793    zero.
10794
10795    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10796    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
10797    is set to FALSE.  */
10798
10799 static tree
10800 cp_parser_type_specifier (cp_parser* parser,
10801                           cp_parser_flags flags,
10802                           cp_decl_specifier_seq *decl_specs,
10803                           bool is_declaration,
10804                           int* declares_class_or_enum,
10805                           bool* is_cv_qualifier)
10806 {
10807   tree type_spec = NULL_TREE;
10808   cp_token *token;
10809   enum rid keyword;
10810   cp_decl_spec ds = ds_last;
10811
10812   /* Assume this type-specifier does not declare a new type.  */
10813   if (declares_class_or_enum)
10814     *declares_class_or_enum = 0;
10815   /* And that it does not specify a cv-qualifier.  */
10816   if (is_cv_qualifier)
10817     *is_cv_qualifier = false;
10818   /* Peek at the next token.  */
10819   token = cp_lexer_peek_token (parser->lexer);
10820
10821   /* If we're looking at a keyword, we can use that to guide the
10822      production we choose.  */
10823   keyword = token->keyword;
10824   switch (keyword)
10825     {
10826     case RID_ENUM:
10827       /* Look for the enum-specifier.  */
10828       type_spec = cp_parser_enum_specifier (parser);
10829       /* If that worked, we're done.  */
10830       if (type_spec)
10831         {
10832           if (declares_class_or_enum)
10833             *declares_class_or_enum = 2;
10834           if (decl_specs)
10835             cp_parser_set_decl_spec_type (decl_specs,
10836                                           type_spec,
10837                                           token->location,
10838                                           /*user_defined_p=*/true);
10839           return type_spec;
10840         }
10841       else
10842         goto elaborated_type_specifier;
10843
10844       /* Any of these indicate either a class-specifier, or an
10845          elaborated-type-specifier.  */
10846     case RID_CLASS:
10847     case RID_STRUCT:
10848     case RID_UNION:
10849       /* Parse tentatively so that we can back up if we don't find a
10850          class-specifier.  */
10851       cp_parser_parse_tentatively (parser);
10852       /* Look for the class-specifier.  */
10853       type_spec = cp_parser_class_specifier (parser);
10854       /* If that worked, we're done.  */
10855       if (cp_parser_parse_definitely (parser))
10856         {
10857           if (declares_class_or_enum)
10858             *declares_class_or_enum = 2;
10859           if (decl_specs)
10860             cp_parser_set_decl_spec_type (decl_specs,
10861                                           type_spec,
10862                                           token->location,
10863                                           /*user_defined_p=*/true);
10864           return type_spec;
10865         }
10866
10867       /* Fall through.  */
10868     elaborated_type_specifier:
10869       /* We're declaring (not defining) a class or enum.  */
10870       if (declares_class_or_enum)
10871         *declares_class_or_enum = 1;
10872
10873       /* Fall through.  */
10874     case RID_TYPENAME:
10875       /* Look for an elaborated-type-specifier.  */
10876       type_spec
10877         = (cp_parser_elaborated_type_specifier
10878            (parser,
10879             decl_specs && decl_specs->specs[(int) ds_friend],
10880             is_declaration));
10881       if (decl_specs)
10882         cp_parser_set_decl_spec_type (decl_specs,
10883                                       type_spec,
10884                                       token->location,
10885                                       /*user_defined_p=*/true);
10886       return type_spec;
10887
10888     case RID_CONST:
10889       ds = ds_const;
10890       if (is_cv_qualifier)
10891         *is_cv_qualifier = true;
10892       break;
10893
10894     case RID_VOLATILE:
10895       ds = ds_volatile;
10896       if (is_cv_qualifier)
10897         *is_cv_qualifier = true;
10898       break;
10899
10900     case RID_RESTRICT:
10901       ds = ds_restrict;
10902       if (is_cv_qualifier)
10903         *is_cv_qualifier = true;
10904       break;
10905
10906     case RID_COMPLEX:
10907       /* The `__complex__' keyword is a GNU extension.  */
10908       ds = ds_complex;
10909       break;
10910
10911     default:
10912       break;
10913     }
10914
10915   /* Handle simple keywords.  */
10916   if (ds != ds_last)
10917     {
10918       if (decl_specs)
10919         {
10920           ++decl_specs->specs[(int)ds];
10921           decl_specs->any_specifiers_p = true;
10922         }
10923       return cp_lexer_consume_token (parser->lexer)->u.value;
10924     }
10925
10926   /* If we do not already have a type-specifier, assume we are looking
10927      at a simple-type-specifier.  */
10928   type_spec = cp_parser_simple_type_specifier (parser,
10929                                                decl_specs,
10930                                                flags);
10931
10932   /* If we didn't find a type-specifier, and a type-specifier was not
10933      optional in this context, issue an error message.  */
10934   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10935     {
10936       cp_parser_error (parser, "expected type specifier");
10937       return error_mark_node;
10938     }
10939
10940   return type_spec;
10941 }
10942
10943 /* Parse a simple-type-specifier.
10944
10945    simple-type-specifier:
10946      :: [opt] nested-name-specifier [opt] type-name
10947      :: [opt] nested-name-specifier template template-id
10948      char
10949      wchar_t
10950      bool
10951      short
10952      int
10953      long
10954      signed
10955      unsigned
10956      float
10957      double
10958      void
10959
10960    C++0x Extension:
10961
10962    simple-type-specifier:
10963      auto
10964      decltype ( expression )   
10965      char16_t
10966      char32_t
10967
10968    GNU Extension:
10969
10970    simple-type-specifier:
10971      __typeof__ unary-expression
10972      __typeof__ ( type-id )
10973
10974    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
10975    appropriately updated.  */
10976
10977 static tree
10978 cp_parser_simple_type_specifier (cp_parser* parser,
10979                                  cp_decl_specifier_seq *decl_specs,
10980                                  cp_parser_flags flags)
10981 {
10982   tree type = NULL_TREE;
10983   cp_token *token;
10984
10985   /* Peek at the next token.  */
10986   token = cp_lexer_peek_token (parser->lexer);
10987
10988   /* If we're looking at a keyword, things are easy.  */
10989   switch (token->keyword)
10990     {
10991     case RID_CHAR:
10992       if (decl_specs)
10993         decl_specs->explicit_char_p = true;
10994       type = char_type_node;
10995       break;
10996     case RID_CHAR16:
10997       type = char16_type_node;
10998       break;
10999     case RID_CHAR32:
11000       type = char32_type_node;
11001       break;
11002     case RID_WCHAR:
11003       type = wchar_type_node;
11004       break;
11005     case RID_BOOL:
11006       type = boolean_type_node;
11007       break;
11008     case RID_SHORT:
11009       if (decl_specs)
11010         ++decl_specs->specs[(int) ds_short];
11011       type = short_integer_type_node;
11012       break;
11013     case RID_INT:
11014       if (decl_specs)
11015         decl_specs->explicit_int_p = true;
11016       type = integer_type_node;
11017       break;
11018     case RID_LONG:
11019       if (decl_specs)
11020         ++decl_specs->specs[(int) ds_long];
11021       type = long_integer_type_node;
11022       break;
11023     case RID_SIGNED:
11024       if (decl_specs)
11025         ++decl_specs->specs[(int) ds_signed];
11026       type = integer_type_node;
11027       break;
11028     case RID_UNSIGNED:
11029       if (decl_specs)
11030         ++decl_specs->specs[(int) ds_unsigned];
11031       type = unsigned_type_node;
11032       break;
11033     case RID_FLOAT:
11034       type = float_type_node;
11035       break;
11036     case RID_DOUBLE:
11037       type = double_type_node;
11038       break;
11039     case RID_VOID:
11040       type = void_type_node;
11041       break;
11042       
11043     case RID_AUTO:
11044       if (cxx_dialect != cxx98)
11045         {
11046           /* Consume the token.  */
11047           cp_lexer_consume_token (parser->lexer);
11048           /* We do not yet support the use of `auto' as a
11049              type-specifier.  */
11050           error ("%HC++0x %<auto%> specifier not supported", &token->location);
11051         }
11052       break;
11053
11054     case RID_DECLTYPE:
11055       /* Parse the `decltype' type.  */
11056       type = cp_parser_decltype (parser);
11057
11058       if (decl_specs)
11059         cp_parser_set_decl_spec_type (decl_specs, type,
11060                                       token->location,
11061                                       /*user_defined_p=*/true);
11062
11063       return type;
11064
11065     case RID_TYPEOF:
11066       /* Consume the `typeof' token.  */
11067       cp_lexer_consume_token (parser->lexer);
11068       /* Parse the operand to `typeof'.  */
11069       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11070       /* If it is not already a TYPE, take its type.  */
11071       if (!TYPE_P (type))
11072         type = finish_typeof (type);
11073
11074       if (decl_specs)
11075         cp_parser_set_decl_spec_type (decl_specs, type,
11076                                       token->location,
11077                                       /*user_defined_p=*/true);
11078
11079       return type;
11080
11081     default:
11082       break;
11083     }
11084
11085   /* If the type-specifier was for a built-in type, we're done.  */
11086   if (type)
11087     {
11088       tree id;
11089
11090       /* Record the type.  */
11091       if (decl_specs
11092           && (token->keyword != RID_SIGNED
11093               && token->keyword != RID_UNSIGNED
11094               && token->keyword != RID_SHORT
11095               && token->keyword != RID_LONG))
11096         cp_parser_set_decl_spec_type (decl_specs,
11097                                       type,
11098                                       token->location,
11099                                       /*user_defined=*/false);
11100       if (decl_specs)
11101         decl_specs->any_specifiers_p = true;
11102
11103       /* Consume the token.  */
11104       id = cp_lexer_consume_token (parser->lexer)->u.value;
11105
11106       /* There is no valid C++ program where a non-template type is
11107          followed by a "<".  That usually indicates that the user thought
11108          that the type was a template.  */
11109       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11110
11111       return TYPE_NAME (type);
11112     }
11113
11114   /* The type-specifier must be a user-defined type.  */
11115   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11116     {
11117       bool qualified_p;
11118       bool global_p;
11119
11120       /* Don't gobble tokens or issue error messages if this is an
11121          optional type-specifier.  */
11122       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11123         cp_parser_parse_tentatively (parser);
11124
11125       /* Look for the optional `::' operator.  */
11126       global_p
11127         = (cp_parser_global_scope_opt (parser,
11128                                        /*current_scope_valid_p=*/false)
11129            != NULL_TREE);
11130       /* Look for the nested-name specifier.  */
11131       qualified_p
11132         = (cp_parser_nested_name_specifier_opt (parser,
11133                                                 /*typename_keyword_p=*/false,
11134                                                 /*check_dependency_p=*/true,
11135                                                 /*type_p=*/false,
11136                                                 /*is_declaration=*/false)
11137            != NULL_TREE);
11138       token = cp_lexer_peek_token (parser->lexer);
11139       /* If we have seen a nested-name-specifier, and the next token
11140          is `template', then we are using the template-id production.  */
11141       if (parser->scope
11142           && cp_parser_optional_template_keyword (parser))
11143         {
11144           /* Look for the template-id.  */
11145           type = cp_parser_template_id (parser,
11146                                         /*template_keyword_p=*/true,
11147                                         /*check_dependency_p=*/true,
11148                                         /*is_declaration=*/false);
11149           /* If the template-id did not name a type, we are out of
11150              luck.  */
11151           if (TREE_CODE (type) != TYPE_DECL)
11152             {
11153               cp_parser_error (parser, "expected template-id for type");
11154               type = NULL_TREE;
11155             }
11156         }
11157       /* Otherwise, look for a type-name.  */
11158       else
11159         type = cp_parser_type_name (parser);
11160       /* Keep track of all name-lookups performed in class scopes.  */
11161       if (type
11162           && !global_p
11163           && !qualified_p
11164           && TREE_CODE (type) == TYPE_DECL
11165           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11166         maybe_note_name_used_in_class (DECL_NAME (type), type);
11167       /* If it didn't work out, we don't have a TYPE.  */
11168       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11169           && !cp_parser_parse_definitely (parser))
11170         type = NULL_TREE;
11171       if (type && decl_specs)
11172         cp_parser_set_decl_spec_type (decl_specs, type,
11173                                       token->location,
11174                                       /*user_defined=*/true);
11175     }
11176
11177   /* If we didn't get a type-name, issue an error message.  */
11178   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11179     {
11180       cp_parser_error (parser, "expected type-name");
11181       return error_mark_node;
11182     }
11183
11184   /* There is no valid C++ program where a non-template type is
11185      followed by a "<".  That usually indicates that the user thought
11186      that the type was a template.  */
11187   if (type && type != error_mark_node)
11188     {
11189       /* As a last-ditch effort, see if TYPE is an Objective-C type.
11190          If it is, then the '<'...'>' enclose protocol names rather than
11191          template arguments, and so everything is fine.  */
11192       if (c_dialect_objc ()
11193           && (objc_is_id (type) || objc_is_class_name (type)))
11194         {
11195           tree protos = cp_parser_objc_protocol_refs_opt (parser);
11196           tree qual_type = objc_get_protocol_qualified_type (type, protos);
11197
11198           /* Clobber the "unqualified" type previously entered into
11199              DECL_SPECS with the new, improved protocol-qualified version.  */
11200           if (decl_specs)
11201             decl_specs->type = qual_type;
11202
11203           return qual_type;
11204         }
11205
11206       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11207                                                token->location);
11208     }
11209
11210   return type;
11211 }
11212
11213 /* Parse a type-name.
11214
11215    type-name:
11216      class-name
11217      enum-name
11218      typedef-name
11219
11220    enum-name:
11221      identifier
11222
11223    typedef-name:
11224      identifier
11225
11226    Returns a TYPE_DECL for the type.  */
11227
11228 static tree
11229 cp_parser_type_name (cp_parser* parser)
11230 {
11231   tree type_decl;
11232
11233   /* We can't know yet whether it is a class-name or not.  */
11234   cp_parser_parse_tentatively (parser);
11235   /* Try a class-name.  */
11236   type_decl = cp_parser_class_name (parser,
11237                                     /*typename_keyword_p=*/false,
11238                                     /*template_keyword_p=*/false,
11239                                     none_type,
11240                                     /*check_dependency_p=*/true,
11241                                     /*class_head_p=*/false,
11242                                     /*is_declaration=*/false);
11243   /* If it's not a class-name, keep looking.  */
11244   if (!cp_parser_parse_definitely (parser))
11245     {
11246       /* It must be a typedef-name or an enum-name.  */
11247       return cp_parser_nonclass_name (parser);
11248     }
11249
11250   return type_decl;
11251 }
11252
11253 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11254
11255    enum-name:
11256      identifier
11257
11258    typedef-name:
11259      identifier
11260
11261    Returns a TYPE_DECL for the type.  */
11262
11263 static tree
11264 cp_parser_nonclass_name (cp_parser* parser)
11265 {
11266   tree type_decl;
11267   tree identifier;
11268
11269   cp_token *token = cp_lexer_peek_token (parser->lexer);
11270   identifier = cp_parser_identifier (parser);
11271   if (identifier == error_mark_node)
11272     return error_mark_node;
11273
11274   /* Look up the type-name.  */
11275   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11276
11277   if (TREE_CODE (type_decl) != TYPE_DECL
11278       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11279     {
11280       /* See if this is an Objective-C type.  */
11281       tree protos = cp_parser_objc_protocol_refs_opt (parser);
11282       tree type = objc_get_protocol_qualified_type (identifier, protos);
11283       if (type)
11284         type_decl = TYPE_NAME (type);
11285     }
11286   
11287   /* Issue an error if we did not find a type-name.  */
11288   if (TREE_CODE (type_decl) != TYPE_DECL)
11289     {
11290       if (!cp_parser_simulate_error (parser))
11291         cp_parser_name_lookup_error (parser, identifier, type_decl,
11292                                      "is not a type", token->location);
11293       return error_mark_node;
11294     }
11295   /* Remember that the name was used in the definition of the
11296      current class so that we can check later to see if the
11297      meaning would have been different after the class was
11298      entirely defined.  */
11299   else if (type_decl != error_mark_node
11300            && !parser->scope)
11301     maybe_note_name_used_in_class (identifier, type_decl);
11302   
11303   return type_decl;
11304 }
11305
11306 /* Parse an elaborated-type-specifier.  Note that the grammar given
11307    here incorporates the resolution to DR68.
11308
11309    elaborated-type-specifier:
11310      class-key :: [opt] nested-name-specifier [opt] identifier
11311      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11312      enum :: [opt] nested-name-specifier [opt] identifier
11313      typename :: [opt] nested-name-specifier identifier
11314      typename :: [opt] nested-name-specifier template [opt]
11315        template-id
11316
11317    GNU extension:
11318
11319    elaborated-type-specifier:
11320      class-key attributes :: [opt] nested-name-specifier [opt] identifier
11321      class-key attributes :: [opt] nested-name-specifier [opt]
11322                template [opt] template-id
11323      enum attributes :: [opt] nested-name-specifier [opt] identifier
11324
11325    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11326    declared `friend'.  If IS_DECLARATION is TRUE, then this
11327    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11328    something is being declared.
11329
11330    Returns the TYPE specified.  */
11331
11332 static tree
11333 cp_parser_elaborated_type_specifier (cp_parser* parser,
11334                                      bool is_friend,
11335                                      bool is_declaration)
11336 {
11337   enum tag_types tag_type;
11338   tree identifier;
11339   tree type = NULL_TREE;
11340   tree attributes = NULL_TREE;
11341   cp_token *token = NULL;
11342
11343   /* See if we're looking at the `enum' keyword.  */
11344   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11345     {
11346       /* Consume the `enum' token.  */
11347       cp_lexer_consume_token (parser->lexer);
11348       /* Remember that it's an enumeration type.  */
11349       tag_type = enum_type;
11350       /* Parse the attributes.  */
11351       attributes = cp_parser_attributes_opt (parser);
11352     }
11353   /* Or, it might be `typename'.  */
11354   else if (cp_lexer_next_token_is_keyword (parser->lexer,
11355                                            RID_TYPENAME))
11356     {
11357       /* Consume the `typename' token.  */
11358       cp_lexer_consume_token (parser->lexer);
11359       /* Remember that it's a `typename' type.  */
11360       tag_type = typename_type;
11361       /* The `typename' keyword is only allowed in templates.  */
11362       if (!processing_template_decl)
11363         permerror (input_location, "using %<typename%> outside of template");
11364     }
11365   /* Otherwise it must be a class-key.  */
11366   else
11367     {
11368       tag_type = cp_parser_class_key (parser);
11369       if (tag_type == none_type)
11370         return error_mark_node;
11371       /* Parse the attributes.  */
11372       attributes = cp_parser_attributes_opt (parser);
11373     }
11374
11375   /* Look for the `::' operator.  */
11376   cp_parser_global_scope_opt (parser,
11377                               /*current_scope_valid_p=*/false);
11378   /* Look for the nested-name-specifier.  */
11379   if (tag_type == typename_type)
11380     {
11381       if (!cp_parser_nested_name_specifier (parser,
11382                                            /*typename_keyword_p=*/true,
11383                                            /*check_dependency_p=*/true,
11384                                            /*type_p=*/true,
11385                                             is_declaration))
11386         return error_mark_node;
11387     }
11388   else
11389     /* Even though `typename' is not present, the proposed resolution
11390        to Core Issue 180 says that in `class A<T>::B', `B' should be
11391        considered a type-name, even if `A<T>' is dependent.  */
11392     cp_parser_nested_name_specifier_opt (parser,
11393                                          /*typename_keyword_p=*/true,
11394                                          /*check_dependency_p=*/true,
11395                                          /*type_p=*/true,
11396                                          is_declaration);
11397  /* For everything but enumeration types, consider a template-id.
11398     For an enumeration type, consider only a plain identifier.  */
11399   if (tag_type != enum_type)
11400     {
11401       bool template_p = false;
11402       tree decl;
11403
11404       /* Allow the `template' keyword.  */
11405       template_p = cp_parser_optional_template_keyword (parser);
11406       /* If we didn't see `template', we don't know if there's a
11407          template-id or not.  */
11408       if (!template_p)
11409         cp_parser_parse_tentatively (parser);
11410       /* Parse the template-id.  */
11411       token = cp_lexer_peek_token (parser->lexer);
11412       decl = cp_parser_template_id (parser, template_p,
11413                                     /*check_dependency_p=*/true,
11414                                     is_declaration);
11415       /* If we didn't find a template-id, look for an ordinary
11416          identifier.  */
11417       if (!template_p && !cp_parser_parse_definitely (parser))
11418         ;
11419       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11420          in effect, then we must assume that, upon instantiation, the
11421          template will correspond to a class.  */
11422       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11423                && tag_type == typename_type)
11424         type = make_typename_type (parser->scope, decl,
11425                                    typename_type,
11426                                    /*complain=*/tf_error);
11427       else
11428         type = TREE_TYPE (decl);
11429     }
11430
11431   if (!type)
11432     {
11433       token = cp_lexer_peek_token (parser->lexer);
11434       identifier = cp_parser_identifier (parser);
11435
11436       if (identifier == error_mark_node)
11437         {
11438           parser->scope = NULL_TREE;
11439           return error_mark_node;
11440         }
11441
11442       /* For a `typename', we needn't call xref_tag.  */
11443       if (tag_type == typename_type
11444           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11445         return cp_parser_make_typename_type (parser, parser->scope,
11446                                              identifier,
11447                                              token->location);
11448       /* Look up a qualified name in the usual way.  */
11449       if (parser->scope)
11450         {
11451           tree decl;
11452           tree ambiguous_decls;
11453
11454           decl = cp_parser_lookup_name (parser, identifier,
11455                                         tag_type,
11456                                         /*is_template=*/false,
11457                                         /*is_namespace=*/false,
11458                                         /*check_dependency=*/true,
11459                                         &ambiguous_decls,
11460                                         token->location);
11461
11462           /* If the lookup was ambiguous, an error will already have been
11463              issued.  */
11464           if (ambiguous_decls)
11465             return error_mark_node;
11466
11467           /* If we are parsing friend declaration, DECL may be a
11468              TEMPLATE_DECL tree node here.  However, we need to check
11469              whether this TEMPLATE_DECL results in valid code.  Consider
11470              the following example:
11471
11472                namespace N {
11473                  template <class T> class C {};
11474                }
11475                class X {
11476                  template <class T> friend class N::C; // #1, valid code
11477                };
11478                template <class T> class Y {
11479                  friend class N::C;                    // #2, invalid code
11480                };
11481
11482              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11483              name lookup of `N::C'.  We see that friend declaration must
11484              be template for the code to be valid.  Note that
11485              processing_template_decl does not work here since it is
11486              always 1 for the above two cases.  */
11487
11488           decl = (cp_parser_maybe_treat_template_as_class
11489                   (decl, /*tag_name_p=*/is_friend
11490                          && parser->num_template_parameter_lists));
11491
11492           if (TREE_CODE (decl) != TYPE_DECL)
11493             {
11494               cp_parser_diagnose_invalid_type_name (parser,
11495                                                     parser->scope,
11496                                                     identifier,
11497                                                     token->location);
11498               return error_mark_node;
11499             }
11500
11501           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11502             {
11503               bool allow_template = (parser->num_template_parameter_lists
11504                                       || DECL_SELF_REFERENCE_P (decl));
11505               type = check_elaborated_type_specifier (tag_type, decl, 
11506                                                       allow_template);
11507
11508               if (type == error_mark_node)
11509                 return error_mark_node;
11510             }
11511
11512           /* Forward declarations of nested types, such as
11513
11514                class C1::C2;
11515                class C1::C2::C3;
11516
11517              are invalid unless all components preceding the final '::'
11518              are complete.  If all enclosing types are complete, these
11519              declarations become merely pointless.
11520
11521              Invalid forward declarations of nested types are errors
11522              caught elsewhere in parsing.  Those that are pointless arrive
11523              here.  */
11524
11525           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11526               && !is_friend && !processing_explicit_instantiation)
11527             warning (0, "declaration %qD does not declare anything", decl);
11528
11529           type = TREE_TYPE (decl);
11530         }
11531       else
11532         {
11533           /* An elaborated-type-specifier sometimes introduces a new type and
11534              sometimes names an existing type.  Normally, the rule is that it
11535              introduces a new type only if there is not an existing type of
11536              the same name already in scope.  For example, given:
11537
11538                struct S {};
11539                void f() { struct S s; }
11540
11541              the `struct S' in the body of `f' is the same `struct S' as in
11542              the global scope; the existing definition is used.  However, if
11543              there were no global declaration, this would introduce a new
11544              local class named `S'.
11545
11546              An exception to this rule applies to the following code:
11547
11548                namespace N { struct S; }
11549
11550              Here, the elaborated-type-specifier names a new type
11551              unconditionally; even if there is already an `S' in the
11552              containing scope this declaration names a new type.
11553              This exception only applies if the elaborated-type-specifier
11554              forms the complete declaration:
11555
11556                [class.name]
11557
11558                A declaration consisting solely of `class-key identifier ;' is
11559                either a redeclaration of the name in the current scope or a
11560                forward declaration of the identifier as a class name.  It
11561                introduces the name into the current scope.
11562
11563              We are in this situation precisely when the next token is a `;'.
11564
11565              An exception to the exception is that a `friend' declaration does
11566              *not* name a new type; i.e., given:
11567
11568                struct S { friend struct T; };
11569
11570              `T' is not a new type in the scope of `S'.
11571
11572              Also, `new struct S' or `sizeof (struct S)' never results in the
11573              definition of a new type; a new type can only be declared in a
11574              declaration context.  */
11575
11576           tag_scope ts;
11577           bool template_p;
11578
11579           if (is_friend)
11580             /* Friends have special name lookup rules.  */
11581             ts = ts_within_enclosing_non_class;
11582           else if (is_declaration
11583                    && cp_lexer_next_token_is (parser->lexer,
11584                                               CPP_SEMICOLON))
11585             /* This is a `class-key identifier ;' */
11586             ts = ts_current;
11587           else
11588             ts = ts_global;
11589
11590           template_p =
11591             (parser->num_template_parameter_lists
11592              && (cp_parser_next_token_starts_class_definition_p (parser)
11593                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11594           /* An unqualified name was used to reference this type, so
11595              there were no qualifying templates.  */
11596           if (!cp_parser_check_template_parameters (parser,
11597                                                     /*num_templates=*/0,
11598                                                     token->location))
11599             return error_mark_node;
11600           type = xref_tag (tag_type, identifier, ts, template_p);
11601         }
11602     }
11603
11604   if (type == error_mark_node)
11605     return error_mark_node;
11606
11607   /* Allow attributes on forward declarations of classes.  */
11608   if (attributes)
11609     {
11610       if (TREE_CODE (type) == TYPENAME_TYPE)
11611         warning (OPT_Wattributes,
11612                  "attributes ignored on uninstantiated type");
11613       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11614                && ! processing_explicit_instantiation)
11615         warning (OPT_Wattributes,
11616                  "attributes ignored on template instantiation");
11617       else if (is_declaration && cp_parser_declares_only_class_p (parser))
11618         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11619       else
11620         warning (OPT_Wattributes,
11621                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11622     }
11623
11624   if (tag_type != enum_type)
11625     cp_parser_check_class_key (tag_type, type);
11626
11627   /* A "<" cannot follow an elaborated type specifier.  If that
11628      happens, the user was probably trying to form a template-id.  */
11629   cp_parser_check_for_invalid_template_id (parser, type, token->location);
11630
11631   return type;
11632 }
11633
11634 /* Parse an enum-specifier.
11635
11636    enum-specifier:
11637      enum identifier [opt] { enumerator-list [opt] }
11638
11639    GNU Extensions:
11640      enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11641        attributes[opt]
11642
11643    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11644    if the token stream isn't an enum-specifier after all.  */
11645
11646 static tree
11647 cp_parser_enum_specifier (cp_parser* parser)
11648 {
11649   tree identifier;
11650   tree type;
11651   tree attributes;
11652
11653   /* Parse tentatively so that we can back up if we don't find a
11654      enum-specifier.  */
11655   cp_parser_parse_tentatively (parser);
11656
11657   /* Caller guarantees that the current token is 'enum', an identifier
11658      possibly follows, and the token after that is an opening brace.
11659      If we don't have an identifier, fabricate an anonymous name for
11660      the enumeration being defined.  */
11661   cp_lexer_consume_token (parser->lexer);
11662
11663   attributes = cp_parser_attributes_opt (parser);
11664
11665   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11666     identifier = cp_parser_identifier (parser);
11667   else
11668     identifier = make_anon_name ();
11669
11670   /* Look for the `{' but don't consume it yet.  */
11671   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11672     cp_parser_simulate_error (parser);
11673
11674   if (!cp_parser_parse_definitely (parser))
11675     return NULL_TREE;
11676
11677   /* Issue an error message if type-definitions are forbidden here.  */
11678   if (!cp_parser_check_type_definition (parser))
11679     type = error_mark_node;
11680   else
11681     /* Create the new type.  We do this before consuming the opening
11682        brace so the enum will be recorded as being on the line of its
11683        tag (or the 'enum' keyword, if there is no tag).  */
11684     type = start_enum (identifier);
11685   
11686   /* Consume the opening brace.  */
11687   cp_lexer_consume_token (parser->lexer);
11688
11689   if (type == error_mark_node)
11690     {
11691       cp_parser_skip_to_end_of_block_or_statement (parser);
11692       return error_mark_node;
11693     }
11694
11695   /* If the next token is not '}', then there are some enumerators.  */
11696   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11697     cp_parser_enumerator_list (parser, type);
11698
11699   /* Consume the final '}'.  */
11700   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11701
11702   /* Look for trailing attributes to apply to this enumeration, and
11703      apply them if appropriate.  */
11704   if (cp_parser_allow_gnu_extensions_p (parser))
11705     {
11706       tree trailing_attr = cp_parser_attributes_opt (parser);
11707       cplus_decl_attributes (&type,
11708                              trailing_attr,
11709                              (int) ATTR_FLAG_TYPE_IN_PLACE);
11710     }
11711
11712   /* Finish up the enumeration.  */
11713   finish_enum (type);
11714
11715   return type;
11716 }
11717
11718 /* Parse an enumerator-list.  The enumerators all have the indicated
11719    TYPE.
11720
11721    enumerator-list:
11722      enumerator-definition
11723      enumerator-list , enumerator-definition  */
11724
11725 static void
11726 cp_parser_enumerator_list (cp_parser* parser, tree type)
11727 {
11728   while (true)
11729     {
11730       /* Parse an enumerator-definition.  */
11731       cp_parser_enumerator_definition (parser, type);
11732
11733       /* If the next token is not a ',', we've reached the end of
11734          the list.  */
11735       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11736         break;
11737       /* Otherwise, consume the `,' and keep going.  */
11738       cp_lexer_consume_token (parser->lexer);
11739       /* If the next token is a `}', there is a trailing comma.  */
11740       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11741         {
11742           if (!in_system_header)
11743             pedwarn (OPT_pedantic, "comma at end of enumerator list");
11744           break;
11745         }
11746     }
11747 }
11748
11749 /* Parse an enumerator-definition.  The enumerator has the indicated
11750    TYPE.
11751
11752    enumerator-definition:
11753      enumerator
11754      enumerator = constant-expression
11755
11756    enumerator:
11757      identifier  */
11758
11759 static void
11760 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11761 {
11762   tree identifier;
11763   tree value;
11764
11765   /* Look for the identifier.  */
11766   identifier = cp_parser_identifier (parser);
11767   if (identifier == error_mark_node)
11768     return;
11769
11770   /* If the next token is an '=', then there is an explicit value.  */
11771   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11772     {
11773       /* Consume the `=' token.  */
11774       cp_lexer_consume_token (parser->lexer);
11775       /* Parse the value.  */
11776       value = cp_parser_constant_expression (parser,
11777                                              /*allow_non_constant_p=*/false,
11778                                              NULL);
11779     }
11780   else
11781     value = NULL_TREE;
11782
11783   /* Create the enumerator.  */
11784   build_enumerator (identifier, value, type);
11785 }
11786
11787 /* Parse a namespace-name.
11788
11789    namespace-name:
11790      original-namespace-name
11791      namespace-alias
11792
11793    Returns the NAMESPACE_DECL for the namespace.  */
11794
11795 static tree
11796 cp_parser_namespace_name (cp_parser* parser)
11797 {
11798   tree identifier;
11799   tree namespace_decl;
11800
11801   cp_token *token = cp_lexer_peek_token (parser->lexer);
11802
11803   /* Get the name of the namespace.  */
11804   identifier = cp_parser_identifier (parser);
11805   if (identifier == error_mark_node)
11806     return error_mark_node;
11807
11808   /* Look up the identifier in the currently active scope.  Look only
11809      for namespaces, due to:
11810
11811        [basic.lookup.udir]
11812
11813        When looking up a namespace-name in a using-directive or alias
11814        definition, only namespace names are considered.
11815
11816      And:
11817
11818        [basic.lookup.qual]
11819
11820        During the lookup of a name preceding the :: scope resolution
11821        operator, object, function, and enumerator names are ignored.
11822
11823      (Note that cp_parser_class_or_namespace_name only calls this
11824      function if the token after the name is the scope resolution
11825      operator.)  */
11826   namespace_decl = cp_parser_lookup_name (parser, identifier,
11827                                           none_type,
11828                                           /*is_template=*/false,
11829                                           /*is_namespace=*/true,
11830                                           /*check_dependency=*/true,
11831                                           /*ambiguous_decls=*/NULL,
11832                                           token->location);
11833   /* If it's not a namespace, issue an error.  */
11834   if (namespace_decl == error_mark_node
11835       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11836     {
11837       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11838         error ("%H%qD is not a namespace-name", &token->location, identifier);
11839       cp_parser_error (parser, "expected namespace-name");
11840       namespace_decl = error_mark_node;
11841     }
11842
11843   return namespace_decl;
11844 }
11845
11846 /* Parse a namespace-definition.
11847
11848    namespace-definition:
11849      named-namespace-definition
11850      unnamed-namespace-definition
11851
11852    named-namespace-definition:
11853      original-namespace-definition
11854      extension-namespace-definition
11855
11856    original-namespace-definition:
11857      namespace identifier { namespace-body }
11858
11859    extension-namespace-definition:
11860      namespace original-namespace-name { namespace-body }
11861
11862    unnamed-namespace-definition:
11863      namespace { namespace-body } */
11864
11865 static void
11866 cp_parser_namespace_definition (cp_parser* parser)
11867 {
11868   tree identifier, attribs;
11869   bool has_visibility;
11870   bool is_inline;
11871
11872   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11873     {
11874       is_inline = true;
11875       cp_lexer_consume_token (parser->lexer);
11876     }
11877   else
11878     is_inline = false;
11879
11880   /* Look for the `namespace' keyword.  */
11881   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11882
11883   /* Get the name of the namespace.  We do not attempt to distinguish
11884      between an original-namespace-definition and an
11885      extension-namespace-definition at this point.  The semantic
11886      analysis routines are responsible for that.  */
11887   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11888     identifier = cp_parser_identifier (parser);
11889   else
11890     identifier = NULL_TREE;
11891
11892   /* Parse any specified attributes.  */
11893   attribs = cp_parser_attributes_opt (parser);
11894
11895   /* Look for the `{' to start the namespace.  */
11896   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
11897   /* Start the namespace.  */
11898   push_namespace (identifier);
11899
11900   /* "inline namespace" is equivalent to a stub namespace definition
11901      followed by a strong using directive.  */
11902   if (is_inline)
11903     {
11904       tree name_space = current_namespace;
11905       /* Set up namespace association.  */
11906       DECL_NAMESPACE_ASSOCIATIONS (name_space)
11907         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
11908                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
11909       /* Import the contents of the inline namespace.  */
11910       pop_namespace ();
11911       do_using_directive (name_space);
11912       push_namespace (identifier);
11913     }
11914
11915   has_visibility = handle_namespace_attrs (current_namespace, attribs);
11916
11917   /* Parse the body of the namespace.  */
11918   cp_parser_namespace_body (parser);
11919
11920 #ifdef HANDLE_PRAGMA_VISIBILITY
11921   if (has_visibility)
11922     pop_visibility ();
11923 #endif
11924
11925   /* Finish the namespace.  */
11926   pop_namespace ();
11927   /* Look for the final `}'.  */
11928   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11929 }
11930
11931 /* Parse a namespace-body.
11932
11933    namespace-body:
11934      declaration-seq [opt]  */
11935
11936 static void
11937 cp_parser_namespace_body (cp_parser* parser)
11938 {
11939   cp_parser_declaration_seq_opt (parser);
11940 }
11941
11942 /* Parse a namespace-alias-definition.
11943
11944    namespace-alias-definition:
11945      namespace identifier = qualified-namespace-specifier ;  */
11946
11947 static void
11948 cp_parser_namespace_alias_definition (cp_parser* parser)
11949 {
11950   tree identifier;
11951   tree namespace_specifier;
11952
11953   cp_token *token = cp_lexer_peek_token (parser->lexer);
11954
11955   /* Look for the `namespace' keyword.  */
11956   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
11957   /* Look for the identifier.  */
11958   identifier = cp_parser_identifier (parser);
11959   if (identifier == error_mark_node)
11960     return;
11961   /* Look for the `=' token.  */
11962   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11963       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
11964     {
11965       error ("%H%<namespace%> definition is not allowed here", &token->location);
11966       /* Skip the definition.  */
11967       cp_lexer_consume_token (parser->lexer);
11968       if (cp_parser_skip_to_closing_brace (parser))
11969         cp_lexer_consume_token (parser->lexer);
11970       return;
11971     }
11972   cp_parser_require (parser, CPP_EQ, "%<=%>");
11973   /* Look for the qualified-namespace-specifier.  */
11974   namespace_specifier
11975     = cp_parser_qualified_namespace_specifier (parser);
11976   /* Look for the `;' token.  */
11977   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
11978
11979   /* Register the alias in the symbol table.  */
11980   do_namespace_alias (identifier, namespace_specifier);
11981 }
11982
11983 /* Parse a qualified-namespace-specifier.
11984
11985    qualified-namespace-specifier:
11986      :: [opt] nested-name-specifier [opt] namespace-name
11987
11988    Returns a NAMESPACE_DECL corresponding to the specified
11989    namespace.  */
11990
11991 static tree
11992 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11993 {
11994   /* Look for the optional `::'.  */
11995   cp_parser_global_scope_opt (parser,
11996                               /*current_scope_valid_p=*/false);
11997
11998   /* Look for the optional nested-name-specifier.  */
11999   cp_parser_nested_name_specifier_opt (parser,
12000                                        /*typename_keyword_p=*/false,
12001                                        /*check_dependency_p=*/true,
12002                                        /*type_p=*/false,
12003                                        /*is_declaration=*/true);
12004
12005   return cp_parser_namespace_name (parser);
12006 }
12007
12008 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12009    access declaration.
12010
12011    using-declaration:
12012      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12013      using :: unqualified-id ;  
12014
12015    access-declaration:
12016      qualified-id ;  
12017
12018    */
12019
12020 static bool
12021 cp_parser_using_declaration (cp_parser* parser, 
12022                              bool access_declaration_p)
12023 {
12024   cp_token *token;
12025   bool typename_p = false;
12026   bool global_scope_p;
12027   tree decl;
12028   tree identifier;
12029   tree qscope;
12030
12031   if (access_declaration_p)
12032     cp_parser_parse_tentatively (parser);
12033   else
12034     {
12035       /* Look for the `using' keyword.  */
12036       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12037       
12038       /* Peek at the next token.  */
12039       token = cp_lexer_peek_token (parser->lexer);
12040       /* See if it's `typename'.  */
12041       if (token->keyword == RID_TYPENAME)
12042         {
12043           /* Remember that we've seen it.  */
12044           typename_p = true;
12045           /* Consume the `typename' token.  */
12046           cp_lexer_consume_token (parser->lexer);
12047         }
12048     }
12049
12050   /* Look for the optional global scope qualification.  */
12051   global_scope_p
12052     = (cp_parser_global_scope_opt (parser,
12053                                    /*current_scope_valid_p=*/false)
12054        != NULL_TREE);
12055
12056   /* If we saw `typename', or didn't see `::', then there must be a
12057      nested-name-specifier present.  */
12058   if (typename_p || !global_scope_p)
12059     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12060                                               /*check_dependency_p=*/true,
12061                                               /*type_p=*/false,
12062                                               /*is_declaration=*/true);
12063   /* Otherwise, we could be in either of the two productions.  In that
12064      case, treat the nested-name-specifier as optional.  */
12065   else
12066     qscope = cp_parser_nested_name_specifier_opt (parser,
12067                                                   /*typename_keyword_p=*/false,
12068                                                   /*check_dependency_p=*/true,
12069                                                   /*type_p=*/false,
12070                                                   /*is_declaration=*/true);
12071   if (!qscope)
12072     qscope = global_namespace;
12073
12074   if (access_declaration_p && cp_parser_error_occurred (parser))
12075     /* Something has already gone wrong; there's no need to parse
12076        further.  Since an error has occurred, the return value of
12077        cp_parser_parse_definitely will be false, as required.  */
12078     return cp_parser_parse_definitely (parser);
12079
12080   token = cp_lexer_peek_token (parser->lexer);
12081   /* Parse the unqualified-id.  */
12082   identifier = cp_parser_unqualified_id (parser,
12083                                          /*template_keyword_p=*/false,
12084                                          /*check_dependency_p=*/true,
12085                                          /*declarator_p=*/true,
12086                                          /*optional_p=*/false);
12087
12088   if (access_declaration_p)
12089     {
12090       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12091         cp_parser_simulate_error (parser);
12092       if (!cp_parser_parse_definitely (parser))
12093         return false;
12094     }
12095
12096   /* The function we call to handle a using-declaration is different
12097      depending on what scope we are in.  */
12098   if (qscope == error_mark_node || identifier == error_mark_node)
12099     ;
12100   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12101            && TREE_CODE (identifier) != BIT_NOT_EXPR)
12102     /* [namespace.udecl]
12103
12104        A using declaration shall not name a template-id.  */
12105     error ("%Ha template-id may not appear in a using-declaration",
12106             &token->location);
12107   else
12108     {
12109       if (at_class_scope_p ())
12110         {
12111           /* Create the USING_DECL.  */
12112           decl = do_class_using_decl (parser->scope, identifier);
12113
12114           if (check_for_bare_parameter_packs (decl))
12115             return false;
12116           else
12117             /* Add it to the list of members in this class.  */
12118             finish_member_declaration (decl);
12119         }
12120       else
12121         {
12122           decl = cp_parser_lookup_name_simple (parser,
12123                                                identifier,
12124                                                token->location);
12125           if (decl == error_mark_node)
12126             cp_parser_name_lookup_error (parser, identifier,
12127                                          decl, NULL,
12128                                          token->location);
12129           else if (check_for_bare_parameter_packs (decl))
12130             return false;
12131           else if (!at_namespace_scope_p ())
12132             do_local_using_decl (decl, qscope, identifier);
12133           else
12134             do_toplevel_using_decl (decl, qscope, identifier);
12135         }
12136     }
12137
12138   /* Look for the final `;'.  */
12139   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12140   
12141   return true;
12142 }
12143
12144 /* Parse a using-directive.
12145
12146    using-directive:
12147      using namespace :: [opt] nested-name-specifier [opt]
12148        namespace-name ;  */
12149
12150 static void
12151 cp_parser_using_directive (cp_parser* parser)
12152 {
12153   tree namespace_decl;
12154   tree attribs;
12155
12156   /* Look for the `using' keyword.  */
12157   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12158   /* And the `namespace' keyword.  */
12159   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12160   /* Look for the optional `::' operator.  */
12161   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12162   /* And the optional nested-name-specifier.  */
12163   cp_parser_nested_name_specifier_opt (parser,
12164                                        /*typename_keyword_p=*/false,
12165                                        /*check_dependency_p=*/true,
12166                                        /*type_p=*/false,
12167                                        /*is_declaration=*/true);
12168   /* Get the namespace being used.  */
12169   namespace_decl = cp_parser_namespace_name (parser);
12170   /* And any specified attributes.  */
12171   attribs = cp_parser_attributes_opt (parser);
12172   /* Update the symbol table.  */
12173   parse_using_directive (namespace_decl, attribs);
12174   /* Look for the final `;'.  */
12175   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12176 }
12177
12178 /* Parse an asm-definition.
12179
12180    asm-definition:
12181      asm ( string-literal ) ;
12182
12183    GNU Extension:
12184
12185    asm-definition:
12186      asm volatile [opt] ( string-literal ) ;
12187      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12188      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12189                           : asm-operand-list [opt] ) ;
12190      asm volatile [opt] ( string-literal : asm-operand-list [opt]
12191                           : asm-operand-list [opt]
12192                           : asm-operand-list [opt] ) ;  */
12193
12194 static void
12195 cp_parser_asm_definition (cp_parser* parser)
12196 {
12197   tree string;
12198   tree outputs = NULL_TREE;
12199   tree inputs = NULL_TREE;
12200   tree clobbers = NULL_TREE;
12201   tree asm_stmt;
12202   bool volatile_p = false;
12203   bool extended_p = false;
12204   bool invalid_inputs_p = false;
12205   bool invalid_outputs_p = false;
12206
12207   /* Look for the `asm' keyword.  */
12208   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12209   /* See if the next token is `volatile'.  */
12210   if (cp_parser_allow_gnu_extensions_p (parser)
12211       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12212     {
12213       /* Remember that we saw the `volatile' keyword.  */
12214       volatile_p = true;
12215       /* Consume the token.  */
12216       cp_lexer_consume_token (parser->lexer);
12217     }
12218   /* Look for the opening `('.  */
12219   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12220     return;
12221   /* Look for the string.  */
12222   string = cp_parser_string_literal (parser, false, false);
12223   if (string == error_mark_node)
12224     {
12225       cp_parser_skip_to_closing_parenthesis (parser, true, false,
12226                                              /*consume_paren=*/true);
12227       return;
12228     }
12229
12230   /* If we're allowing GNU extensions, check for the extended assembly
12231      syntax.  Unfortunately, the `:' tokens need not be separated by
12232      a space in C, and so, for compatibility, we tolerate that here
12233      too.  Doing that means that we have to treat the `::' operator as
12234      two `:' tokens.  */
12235   if (cp_parser_allow_gnu_extensions_p (parser)
12236       && parser->in_function_body
12237       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12238           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12239     {
12240       bool inputs_p = false;
12241       bool clobbers_p = false;
12242
12243       /* The extended syntax was used.  */
12244       extended_p = true;
12245
12246       /* Look for outputs.  */
12247       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12248         {
12249           /* Consume the `:'.  */
12250           cp_lexer_consume_token (parser->lexer);
12251           /* Parse the output-operands.  */
12252           if (cp_lexer_next_token_is_not (parser->lexer,
12253                                           CPP_COLON)
12254               && cp_lexer_next_token_is_not (parser->lexer,
12255                                              CPP_SCOPE)
12256               && cp_lexer_next_token_is_not (parser->lexer,
12257                                              CPP_CLOSE_PAREN))
12258             outputs = cp_parser_asm_operand_list (parser);
12259
12260             if (outputs == error_mark_node)
12261               invalid_outputs_p = true;
12262         }
12263       /* If the next token is `::', there are no outputs, and the
12264          next token is the beginning of the inputs.  */
12265       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12266         /* The inputs are coming next.  */
12267         inputs_p = true;
12268
12269       /* Look for inputs.  */
12270       if (inputs_p
12271           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12272         {
12273           /* Consume the `:' or `::'.  */
12274           cp_lexer_consume_token (parser->lexer);
12275           /* Parse the output-operands.  */
12276           if (cp_lexer_next_token_is_not (parser->lexer,
12277                                           CPP_COLON)
12278               && cp_lexer_next_token_is_not (parser->lexer,
12279                                              CPP_CLOSE_PAREN))
12280             inputs = cp_parser_asm_operand_list (parser);
12281
12282             if (inputs == error_mark_node)
12283               invalid_inputs_p = true;
12284         }
12285       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12286         /* The clobbers are coming next.  */
12287         clobbers_p = true;
12288
12289       /* Look for clobbers.  */
12290       if (clobbers_p
12291           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12292         {
12293           /* Consume the `:' or `::'.  */
12294           cp_lexer_consume_token (parser->lexer);
12295           /* Parse the clobbers.  */
12296           if (cp_lexer_next_token_is_not (parser->lexer,
12297                                           CPP_CLOSE_PAREN))
12298             clobbers = cp_parser_asm_clobber_list (parser);
12299         }
12300     }
12301   /* Look for the closing `)'.  */
12302   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12303     cp_parser_skip_to_closing_parenthesis (parser, true, false,
12304                                            /*consume_paren=*/true);
12305   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12306
12307   if (!invalid_inputs_p && !invalid_outputs_p)
12308     {
12309       /* Create the ASM_EXPR.  */
12310       if (parser->in_function_body)
12311         {
12312           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12313                                       inputs, clobbers);
12314           /* If the extended syntax was not used, mark the ASM_EXPR.  */
12315           if (!extended_p)
12316             {
12317               tree temp = asm_stmt;
12318               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12319                 temp = TREE_OPERAND (temp, 0);
12320
12321               ASM_INPUT_P (temp) = 1;
12322             }
12323         }
12324       else
12325         cgraph_add_asm_node (string);
12326     }
12327 }
12328
12329 /* Declarators [gram.dcl.decl] */
12330
12331 /* Parse an init-declarator.
12332
12333    init-declarator:
12334      declarator initializer [opt]
12335
12336    GNU Extension:
12337
12338    init-declarator:
12339      declarator asm-specification [opt] attributes [opt] initializer [opt]
12340
12341    function-definition:
12342      decl-specifier-seq [opt] declarator ctor-initializer [opt]
12343        function-body
12344      decl-specifier-seq [opt] declarator function-try-block
12345
12346    GNU Extension:
12347
12348    function-definition:
12349      __extension__ function-definition
12350
12351    The DECL_SPECIFIERS apply to this declarator.  Returns a
12352    representation of the entity declared.  If MEMBER_P is TRUE, then
12353    this declarator appears in a class scope.  The new DECL created by
12354    this declarator is returned.
12355
12356    The CHECKS are access checks that should be performed once we know
12357    what entity is being declared (and, therefore, what classes have
12358    befriended it).
12359
12360    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12361    for a function-definition here as well.  If the declarator is a
12362    declarator for a function-definition, *FUNCTION_DEFINITION_P will
12363    be TRUE upon return.  By that point, the function-definition will
12364    have been completely parsed.
12365
12366    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12367    is FALSE.  */
12368
12369 static tree
12370 cp_parser_init_declarator (cp_parser* parser,
12371                            cp_decl_specifier_seq *decl_specifiers,
12372                            VEC (deferred_access_check,gc)* checks,
12373                            bool function_definition_allowed_p,
12374                            bool member_p,
12375                            int declares_class_or_enum,
12376                            bool* function_definition_p)
12377 {
12378   cp_token *token = NULL, *asm_spec_start_token = NULL,
12379            *attributes_start_token = NULL;
12380   cp_declarator *declarator;
12381   tree prefix_attributes;
12382   tree attributes;
12383   tree asm_specification;
12384   tree initializer;
12385   tree decl = NULL_TREE;
12386   tree scope;
12387   int is_initialized;
12388   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
12389      initialized with "= ..", CPP_OPEN_PAREN if initialized with
12390      "(...)".  */
12391   enum cpp_ttype initialization_kind;
12392   bool is_direct_init = false;
12393   bool is_non_constant_init;
12394   int ctor_dtor_or_conv_p;
12395   bool friend_p;
12396   tree pushed_scope = NULL;
12397
12398   /* Gather the attributes that were provided with the
12399      decl-specifiers.  */
12400   prefix_attributes = decl_specifiers->attributes;
12401
12402   /* Assume that this is not the declarator for a function
12403      definition.  */
12404   if (function_definition_p)
12405     *function_definition_p = false;
12406
12407   /* Defer access checks while parsing the declarator; we cannot know
12408      what names are accessible until we know what is being
12409      declared.  */
12410   resume_deferring_access_checks ();
12411
12412   /* Parse the declarator.  */
12413   token = cp_lexer_peek_token (parser->lexer);
12414   declarator
12415     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12416                             &ctor_dtor_or_conv_p,
12417                             /*parenthesized_p=*/NULL,
12418                             /*member_p=*/false);
12419   /* Gather up the deferred checks.  */
12420   stop_deferring_access_checks ();
12421
12422   /* If the DECLARATOR was erroneous, there's no need to go
12423      further.  */
12424   if (declarator == cp_error_declarator)
12425     return error_mark_node;
12426
12427   /* Check that the number of template-parameter-lists is OK.  */
12428   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12429                                                        token->location))
12430     return error_mark_node;
12431
12432   if (declares_class_or_enum & 2)
12433     cp_parser_check_for_definition_in_return_type (declarator,
12434                                                    decl_specifiers->type,
12435                                                    decl_specifiers->type_location);
12436
12437   /* Figure out what scope the entity declared by the DECLARATOR is
12438      located in.  `grokdeclarator' sometimes changes the scope, so
12439      we compute it now.  */
12440   scope = get_scope_of_declarator (declarator);
12441
12442   /* If we're allowing GNU extensions, look for an asm-specification
12443      and attributes.  */
12444   if (cp_parser_allow_gnu_extensions_p (parser))
12445     {
12446       /* Look for an asm-specification.  */
12447       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12448       asm_specification = cp_parser_asm_specification_opt (parser);
12449       /* And attributes.  */
12450       attributes_start_token = cp_lexer_peek_token (parser->lexer);
12451       attributes = cp_parser_attributes_opt (parser);
12452     }
12453   else
12454     {
12455       asm_specification = NULL_TREE;
12456       attributes = NULL_TREE;
12457     }
12458
12459   /* Peek at the next token.  */
12460   token = cp_lexer_peek_token (parser->lexer);
12461   /* Check to see if the token indicates the start of a
12462      function-definition.  */
12463   if (function_declarator_p (declarator)
12464       && cp_parser_token_starts_function_definition_p (token))
12465     {
12466       if (!function_definition_allowed_p)
12467         {
12468           /* If a function-definition should not appear here, issue an
12469              error message.  */
12470           cp_parser_error (parser,
12471                            "a function-definition is not allowed here");
12472           return error_mark_node;
12473         }
12474       else
12475         {
12476           /* Neither attributes nor an asm-specification are allowed
12477              on a function-definition.  */
12478           if (asm_specification)
12479             error ("%Han asm-specification is not allowed "
12480                    "on a function-definition",
12481                    &asm_spec_start_token->location);
12482           if (attributes)
12483             error ("%Hattributes are not allowed on a function-definition",
12484                    &attributes_start_token->location);
12485           /* This is a function-definition.  */
12486           *function_definition_p = true;
12487
12488           /* Parse the function definition.  */
12489           if (member_p)
12490             decl = cp_parser_save_member_function_body (parser,
12491                                                         decl_specifiers,
12492                                                         declarator,
12493                                                         prefix_attributes);
12494           else
12495             decl
12496               = (cp_parser_function_definition_from_specifiers_and_declarator
12497                  (parser, decl_specifiers, prefix_attributes, declarator));
12498
12499           return decl;
12500         }
12501     }
12502
12503   /* [dcl.dcl]
12504
12505      Only in function declarations for constructors, destructors, and
12506      type conversions can the decl-specifier-seq be omitted.
12507
12508      We explicitly postpone this check past the point where we handle
12509      function-definitions because we tolerate function-definitions
12510      that are missing their return types in some modes.  */
12511   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12512     {
12513       cp_parser_error (parser,
12514                        "expected constructor, destructor, or type conversion");
12515       return error_mark_node;
12516     }
12517
12518   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
12519   if (token->type == CPP_EQ
12520       || token->type == CPP_OPEN_PAREN
12521       || token->type == CPP_OPEN_BRACE)
12522     {
12523       is_initialized = 1;
12524       initialization_kind = token->type;
12525
12526       if (token->type == CPP_EQ
12527           && function_declarator_p (declarator))
12528         {
12529           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12530           if (t2->keyword == RID_DEFAULT)
12531             is_initialized = 2;
12532           else if (t2->keyword == RID_DELETE)
12533             is_initialized = 3;
12534         }
12535     }
12536   else
12537     {
12538       /* If the init-declarator isn't initialized and isn't followed by a
12539          `,' or `;', it's not a valid init-declarator.  */
12540       if (token->type != CPP_COMMA
12541           && token->type != CPP_SEMICOLON)
12542         {
12543           cp_parser_error (parser, "expected initializer");
12544           return error_mark_node;
12545         }
12546       is_initialized = 0;
12547       initialization_kind = CPP_EOF;
12548     }
12549
12550   /* Because start_decl has side-effects, we should only call it if we
12551      know we're going ahead.  By this point, we know that we cannot
12552      possibly be looking at any other construct.  */
12553   cp_parser_commit_to_tentative_parse (parser);
12554
12555   /* If the decl specifiers were bad, issue an error now that we're
12556      sure this was intended to be a declarator.  Then continue
12557      declaring the variable(s), as int, to try to cut down on further
12558      errors.  */
12559   if (decl_specifiers->any_specifiers_p
12560       && decl_specifiers->type == error_mark_node)
12561     {
12562       cp_parser_error (parser, "invalid type in declaration");
12563       decl_specifiers->type = integer_type_node;
12564     }
12565
12566   /* Check to see whether or not this declaration is a friend.  */
12567   friend_p = cp_parser_friend_p (decl_specifiers);
12568
12569   /* Enter the newly declared entry in the symbol table.  If we're
12570      processing a declaration in a class-specifier, we wait until
12571      after processing the initializer.  */
12572   if (!member_p)
12573     {
12574       if (parser->in_unbraced_linkage_specification_p)
12575         decl_specifiers->storage_class = sc_extern;
12576       decl = start_decl (declarator, decl_specifiers,
12577                          is_initialized, attributes, prefix_attributes,
12578                          &pushed_scope);
12579     }
12580   else if (scope)
12581     /* Enter the SCOPE.  That way unqualified names appearing in the
12582        initializer will be looked up in SCOPE.  */
12583     pushed_scope = push_scope (scope);
12584
12585   /* Perform deferred access control checks, now that we know in which
12586      SCOPE the declared entity resides.  */
12587   if (!member_p && decl)
12588     {
12589       tree saved_current_function_decl = NULL_TREE;
12590
12591       /* If the entity being declared is a function, pretend that we
12592          are in its scope.  If it is a `friend', it may have access to
12593          things that would not otherwise be accessible.  */
12594       if (TREE_CODE (decl) == FUNCTION_DECL)
12595         {
12596           saved_current_function_decl = current_function_decl;
12597           current_function_decl = decl;
12598         }
12599
12600       /* Perform access checks for template parameters.  */
12601       cp_parser_perform_template_parameter_access_checks (checks);
12602
12603       /* Perform the access control checks for the declarator and the
12604          decl-specifiers.  */
12605       perform_deferred_access_checks ();
12606
12607       /* Restore the saved value.  */
12608       if (TREE_CODE (decl) == FUNCTION_DECL)
12609         current_function_decl = saved_current_function_decl;
12610     }
12611
12612   /* Parse the initializer.  */
12613   initializer = NULL_TREE;
12614   is_direct_init = false;
12615   is_non_constant_init = true;
12616   if (is_initialized)
12617     {
12618       if (function_declarator_p (declarator))
12619         {
12620           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12621            if (initialization_kind == CPP_EQ)
12622              initializer = cp_parser_pure_specifier (parser);
12623            else
12624              {
12625                /* If the declaration was erroneous, we don't really
12626                   know what the user intended, so just silently
12627                   consume the initializer.  */
12628                if (decl != error_mark_node)
12629                  error ("%Hinitializer provided for function",
12630                         &initializer_start_token->location);
12631                cp_parser_skip_to_closing_parenthesis (parser,
12632                                                       /*recovering=*/true,
12633                                                       /*or_comma=*/false,
12634                                                       /*consume_paren=*/true);
12635              }
12636         }
12637       else
12638         initializer = cp_parser_initializer (parser,
12639                                              &is_direct_init,
12640                                              &is_non_constant_init);
12641     }
12642
12643   /* The old parser allows attributes to appear after a parenthesized
12644      initializer.  Mark Mitchell proposed removing this functionality
12645      on the GCC mailing lists on 2002-08-13.  This parser accepts the
12646      attributes -- but ignores them.  */
12647   if (cp_parser_allow_gnu_extensions_p (parser)
12648       && initialization_kind == CPP_OPEN_PAREN)
12649     if (cp_parser_attributes_opt (parser))
12650       warning (OPT_Wattributes,
12651                "attributes after parenthesized initializer ignored");
12652
12653   /* For an in-class declaration, use `grokfield' to create the
12654      declaration.  */
12655   if (member_p)
12656     {
12657       if (pushed_scope)
12658         {
12659           pop_scope (pushed_scope);
12660           pushed_scope = false;
12661         }
12662       decl = grokfield (declarator, decl_specifiers,
12663                         initializer, !is_non_constant_init,
12664                         /*asmspec=*/NULL_TREE,
12665                         prefix_attributes);
12666       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12667         cp_parser_save_default_args (parser, decl);
12668     }
12669
12670   /* Finish processing the declaration.  But, skip friend
12671      declarations.  */
12672   if (!friend_p && decl && decl != error_mark_node)
12673     {
12674       cp_finish_decl (decl,
12675                       initializer, !is_non_constant_init,
12676                       asm_specification,
12677                       /* If the initializer is in parentheses, then this is
12678                          a direct-initialization, which means that an
12679                          `explicit' constructor is OK.  Otherwise, an
12680                          `explicit' constructor cannot be used.  */
12681                       ((is_direct_init || !is_initialized)
12682                        ? 0 : LOOKUP_ONLYCONVERTING));
12683     }
12684   else if ((cxx_dialect != cxx98) && friend_p
12685            && decl && TREE_CODE (decl) == FUNCTION_DECL)
12686     /* Core issue #226 (C++0x only): A default template-argument
12687        shall not be specified in a friend class template
12688        declaration. */
12689     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
12690                              /*is_partial=*/0, /*is_friend_decl=*/1);
12691
12692   if (!friend_p && pushed_scope)
12693     pop_scope (pushed_scope);
12694
12695   return decl;
12696 }
12697
12698 /* Parse a declarator.
12699
12700    declarator:
12701      direct-declarator
12702      ptr-operator declarator
12703
12704    abstract-declarator:
12705      ptr-operator abstract-declarator [opt]
12706      direct-abstract-declarator
12707
12708    GNU Extensions:
12709
12710    declarator:
12711      attributes [opt] direct-declarator
12712      attributes [opt] ptr-operator declarator
12713
12714    abstract-declarator:
12715      attributes [opt] ptr-operator abstract-declarator [opt]
12716      attributes [opt] direct-abstract-declarator
12717
12718    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12719    detect constructor, destructor or conversion operators. It is set
12720    to -1 if the declarator is a name, and +1 if it is a
12721    function. Otherwise it is set to zero. Usually you just want to
12722    test for >0, but internally the negative value is used.
12723
12724    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12725    a decl-specifier-seq unless it declares a constructor, destructor,
12726    or conversion.  It might seem that we could check this condition in
12727    semantic analysis, rather than parsing, but that makes it difficult
12728    to handle something like `f()'.  We want to notice that there are
12729    no decl-specifiers, and therefore realize that this is an
12730    expression, not a declaration.)
12731
12732    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12733    the declarator is a direct-declarator of the form "(...)".
12734
12735    MEMBER_P is true iff this declarator is a member-declarator.  */
12736
12737 static cp_declarator *
12738 cp_parser_declarator (cp_parser* parser,
12739                       cp_parser_declarator_kind dcl_kind,
12740                       int* ctor_dtor_or_conv_p,
12741                       bool* parenthesized_p,
12742                       bool member_p)
12743 {
12744   cp_token *token;
12745   cp_declarator *declarator;
12746   enum tree_code code;
12747   cp_cv_quals cv_quals;
12748   tree class_type;
12749   tree attributes = NULL_TREE;
12750
12751   /* Assume this is not a constructor, destructor, or type-conversion
12752      operator.  */
12753   if (ctor_dtor_or_conv_p)
12754     *ctor_dtor_or_conv_p = 0;
12755
12756   if (cp_parser_allow_gnu_extensions_p (parser))
12757     attributes = cp_parser_attributes_opt (parser);
12758
12759   /* Peek at the next token.  */
12760   token = cp_lexer_peek_token (parser->lexer);
12761
12762   /* Check for the ptr-operator production.  */
12763   cp_parser_parse_tentatively (parser);
12764   /* Parse the ptr-operator.  */
12765   code = cp_parser_ptr_operator (parser,
12766                                  &class_type,
12767                                  &cv_quals);
12768   /* If that worked, then we have a ptr-operator.  */
12769   if (cp_parser_parse_definitely (parser))
12770     {
12771       /* If a ptr-operator was found, then this declarator was not
12772          parenthesized.  */
12773       if (parenthesized_p)
12774         *parenthesized_p = true;
12775       /* The dependent declarator is optional if we are parsing an
12776          abstract-declarator.  */
12777       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12778         cp_parser_parse_tentatively (parser);
12779
12780       /* Parse the dependent declarator.  */
12781       declarator = cp_parser_declarator (parser, dcl_kind,
12782                                          /*ctor_dtor_or_conv_p=*/NULL,
12783                                          /*parenthesized_p=*/NULL,
12784                                          /*member_p=*/false);
12785
12786       /* If we are parsing an abstract-declarator, we must handle the
12787          case where the dependent declarator is absent.  */
12788       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12789           && !cp_parser_parse_definitely (parser))
12790         declarator = NULL;
12791
12792       declarator = cp_parser_make_indirect_declarator
12793         (code, class_type, cv_quals, declarator);
12794     }
12795   /* Everything else is a direct-declarator.  */
12796   else
12797     {
12798       if (parenthesized_p)
12799         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12800                                                    CPP_OPEN_PAREN);
12801       declarator = cp_parser_direct_declarator (parser, dcl_kind,
12802                                                 ctor_dtor_or_conv_p,
12803                                                 member_p);
12804     }
12805
12806   if (attributes && declarator && declarator != cp_error_declarator)
12807     declarator->attributes = attributes;
12808
12809   return declarator;
12810 }
12811
12812 /* Parse a direct-declarator or direct-abstract-declarator.
12813
12814    direct-declarator:
12815      declarator-id
12816      direct-declarator ( parameter-declaration-clause )
12817        cv-qualifier-seq [opt]
12818        exception-specification [opt]
12819      direct-declarator [ constant-expression [opt] ]
12820      ( declarator )
12821
12822    direct-abstract-declarator:
12823      direct-abstract-declarator [opt]
12824        ( parameter-declaration-clause )
12825        cv-qualifier-seq [opt]
12826        exception-specification [opt]
12827      direct-abstract-declarator [opt] [ constant-expression [opt] ]
12828      ( abstract-declarator )
12829
12830    Returns a representation of the declarator.  DCL_KIND is
12831    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12832    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
12833    we are parsing a direct-declarator.  It is
12834    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12835    of ambiguity we prefer an abstract declarator, as per
12836    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12837    cp_parser_declarator.  */
12838
12839 static cp_declarator *
12840 cp_parser_direct_declarator (cp_parser* parser,
12841                              cp_parser_declarator_kind dcl_kind,
12842                              int* ctor_dtor_or_conv_p,
12843                              bool member_p)
12844 {
12845   cp_token *token;
12846   cp_declarator *declarator = NULL;
12847   tree scope = NULL_TREE;
12848   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12849   bool saved_in_declarator_p = parser->in_declarator_p;
12850   bool first = true;
12851   tree pushed_scope = NULL_TREE;
12852
12853   while (true)
12854     {
12855       /* Peek at the next token.  */
12856       token = cp_lexer_peek_token (parser->lexer);
12857       if (token->type == CPP_OPEN_PAREN)
12858         {
12859           /* This is either a parameter-declaration-clause, or a
12860              parenthesized declarator. When we know we are parsing a
12861              named declarator, it must be a parenthesized declarator
12862              if FIRST is true. For instance, `(int)' is a
12863              parameter-declaration-clause, with an omitted
12864              direct-abstract-declarator. But `((*))', is a
12865              parenthesized abstract declarator. Finally, when T is a
12866              template parameter `(T)' is a
12867              parameter-declaration-clause, and not a parenthesized
12868              named declarator.
12869
12870              We first try and parse a parameter-declaration-clause,
12871              and then try a nested declarator (if FIRST is true).
12872
12873              It is not an error for it not to be a
12874              parameter-declaration-clause, even when FIRST is
12875              false. Consider,
12876
12877                int i (int);
12878                int i (3);
12879
12880              The first is the declaration of a function while the
12881              second is the definition of a variable, including its
12882              initializer.
12883
12884              Having seen only the parenthesis, we cannot know which of
12885              these two alternatives should be selected.  Even more
12886              complex are examples like:
12887
12888                int i (int (a));
12889                int i (int (3));
12890
12891              The former is a function-declaration; the latter is a
12892              variable initialization.
12893
12894              Thus again, we try a parameter-declaration-clause, and if
12895              that fails, we back out and return.  */
12896
12897           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12898             {
12899               cp_parameter_declarator *params;
12900               unsigned saved_num_template_parameter_lists;
12901
12902               /* In a member-declarator, the only valid interpretation
12903                  of a parenthesis is the start of a
12904                  parameter-declaration-clause.  (It is invalid to
12905                  initialize a static data member with a parenthesized
12906                  initializer; only the "=" form of initialization is
12907                  permitted.)  */
12908               if (!member_p)
12909                 cp_parser_parse_tentatively (parser);
12910
12911               /* Consume the `('.  */
12912               cp_lexer_consume_token (parser->lexer);
12913               if (first)
12914                 {
12915                   /* If this is going to be an abstract declarator, we're
12916                      in a declarator and we can't have default args.  */
12917                   parser->default_arg_ok_p = false;
12918                   parser->in_declarator_p = true;
12919                 }
12920
12921               /* Inside the function parameter list, surrounding
12922                  template-parameter-lists do not apply.  */
12923               saved_num_template_parameter_lists
12924                 = parser->num_template_parameter_lists;
12925               parser->num_template_parameter_lists = 0;
12926
12927               /* Parse the parameter-declaration-clause.  */
12928               params = cp_parser_parameter_declaration_clause (parser);
12929
12930               parser->num_template_parameter_lists
12931                 = saved_num_template_parameter_lists;
12932
12933               /* If all went well, parse the cv-qualifier-seq and the
12934                  exception-specification.  */
12935               if (member_p || cp_parser_parse_definitely (parser))
12936                 {
12937                   cp_cv_quals cv_quals;
12938                   tree exception_specification;
12939
12940                   if (ctor_dtor_or_conv_p)
12941                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12942                   first = false;
12943                   /* Consume the `)'.  */
12944                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
12945
12946                   /* Parse the cv-qualifier-seq.  */
12947                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12948                   /* And the exception-specification.  */
12949                   exception_specification
12950                     = cp_parser_exception_specification_opt (parser);
12951
12952                   /* Create the function-declarator.  */
12953                   declarator = make_call_declarator (declarator,
12954                                                      params,
12955                                                      cv_quals,
12956                                                      exception_specification);
12957                   /* Any subsequent parameter lists are to do with
12958                      return type, so are not those of the declared
12959                      function.  */
12960                   parser->default_arg_ok_p = false;
12961
12962                   /* Repeat the main loop.  */
12963                   continue;
12964                 }
12965             }
12966
12967           /* If this is the first, we can try a parenthesized
12968              declarator.  */
12969           if (first)
12970             {
12971               bool saved_in_type_id_in_expr_p;
12972
12973               parser->default_arg_ok_p = saved_default_arg_ok_p;
12974               parser->in_declarator_p = saved_in_declarator_p;
12975
12976               /* Consume the `('.  */
12977               cp_lexer_consume_token (parser->lexer);
12978               /* Parse the nested declarator.  */
12979               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12980               parser->in_type_id_in_expr_p = true;
12981               declarator
12982                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12983                                         /*parenthesized_p=*/NULL,
12984                                         member_p);
12985               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12986               first = false;
12987               /* Expect a `)'.  */
12988               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12989                 declarator = cp_error_declarator;
12990               if (declarator == cp_error_declarator)
12991                 break;
12992
12993               goto handle_declarator;
12994             }
12995           /* Otherwise, we must be done.  */
12996           else
12997             break;
12998         }
12999       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13000                && token->type == CPP_OPEN_SQUARE)
13001         {
13002           /* Parse an array-declarator.  */
13003           tree bounds;
13004
13005           if (ctor_dtor_or_conv_p)
13006             *ctor_dtor_or_conv_p = 0;
13007
13008           first = false;
13009           parser->default_arg_ok_p = false;
13010           parser->in_declarator_p = true;
13011           /* Consume the `['.  */
13012           cp_lexer_consume_token (parser->lexer);
13013           /* Peek at the next token.  */
13014           token = cp_lexer_peek_token (parser->lexer);
13015           /* If the next token is `]', then there is no
13016              constant-expression.  */
13017           if (token->type != CPP_CLOSE_SQUARE)
13018             {
13019               bool non_constant_p;
13020
13021               bounds
13022                 = cp_parser_constant_expression (parser,
13023                                                  /*allow_non_constant=*/true,
13024                                                  &non_constant_p);
13025               if (!non_constant_p)
13026                 bounds = fold_non_dependent_expr (bounds);
13027               /* Normally, the array bound must be an integral constant
13028                  expression.  However, as an extension, we allow VLAs
13029                  in function scopes.  */
13030               else if (!parser->in_function_body)
13031                 {
13032                   error ("%Harray bound is not an integer constant",
13033                          &token->location);
13034                   bounds = error_mark_node;
13035                 }
13036             }
13037           else
13038             bounds = NULL_TREE;
13039           /* Look for the closing `]'.  */
13040           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13041             {
13042               declarator = cp_error_declarator;
13043               break;
13044             }
13045
13046           declarator = make_array_declarator (declarator, bounds);
13047         }
13048       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13049         {
13050           tree qualifying_scope;
13051           tree unqualified_name;
13052           special_function_kind sfk;
13053           bool abstract_ok;
13054           bool pack_expansion_p = false;
13055           cp_token *declarator_id_start_token;
13056
13057           /* Parse a declarator-id */
13058           abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13059           if (abstract_ok)
13060             {
13061               cp_parser_parse_tentatively (parser);
13062
13063               /* If we see an ellipsis, we should be looking at a
13064                  parameter pack. */
13065               if (token->type == CPP_ELLIPSIS)
13066                 {
13067                   /* Consume the `...' */
13068                   cp_lexer_consume_token (parser->lexer);
13069
13070                   pack_expansion_p = true;
13071                 }
13072             }
13073
13074           declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13075           unqualified_name
13076             = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13077           qualifying_scope = parser->scope;
13078           if (abstract_ok)
13079             {
13080               bool okay = false;
13081
13082               if (!unqualified_name && pack_expansion_p)
13083                 {
13084                   /* Check whether an error occurred. */
13085                   okay = !cp_parser_error_occurred (parser);
13086
13087                   /* We already consumed the ellipsis to mark a
13088                      parameter pack, but we have no way to report it,
13089                      so abort the tentative parse. We will be exiting
13090                      immediately anyway. */
13091                   cp_parser_abort_tentative_parse (parser);
13092                 }
13093               else
13094                 okay = cp_parser_parse_definitely (parser);
13095
13096               if (!okay)
13097                 unqualified_name = error_mark_node;
13098               else if (unqualified_name
13099                        && (qualifying_scope
13100                            || (TREE_CODE (unqualified_name)
13101                                != IDENTIFIER_NODE)))
13102                 {
13103                   cp_parser_error (parser, "expected unqualified-id");
13104                   unqualified_name = error_mark_node;
13105                 }
13106             }
13107
13108           if (!unqualified_name)
13109             return NULL;
13110           if (unqualified_name == error_mark_node)
13111             {
13112               declarator = cp_error_declarator;
13113               pack_expansion_p = false;
13114               declarator->parameter_pack_p = false;
13115               break;
13116             }
13117
13118           if (qualifying_scope && at_namespace_scope_p ()
13119               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13120             {
13121               /* In the declaration of a member of a template class
13122                  outside of the class itself, the SCOPE will sometimes
13123                  be a TYPENAME_TYPE.  For example, given:
13124
13125                  template <typename T>
13126                  int S<T>::R::i = 3;
13127
13128                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
13129                  this context, we must resolve S<T>::R to an ordinary
13130                  type, rather than a typename type.
13131
13132                  The reason we normally avoid resolving TYPENAME_TYPEs
13133                  is that a specialization of `S' might render
13134                  `S<T>::R' not a type.  However, if `S' is
13135                  specialized, then this `i' will not be used, so there
13136                  is no harm in resolving the types here.  */
13137               tree type;
13138
13139               /* Resolve the TYPENAME_TYPE.  */
13140               type = resolve_typename_type (qualifying_scope,
13141                                             /*only_current_p=*/false);
13142               /* If that failed, the declarator is invalid.  */
13143               if (TREE_CODE (type) == TYPENAME_TYPE)
13144                 error ("%H%<%T::%E%> is not a type",
13145                        &declarator_id_start_token->location,
13146                        TYPE_CONTEXT (qualifying_scope),
13147                        TYPE_IDENTIFIER (qualifying_scope));
13148               qualifying_scope = type;
13149             }
13150
13151           sfk = sfk_none;
13152
13153           if (unqualified_name)
13154             {
13155               tree class_type;
13156
13157               if (qualifying_scope
13158                   && CLASS_TYPE_P (qualifying_scope))
13159                 class_type = qualifying_scope;
13160               else
13161                 class_type = current_class_type;
13162
13163               if (TREE_CODE (unqualified_name) == TYPE_DECL)
13164                 {
13165                   tree name_type = TREE_TYPE (unqualified_name);
13166                   if (class_type && same_type_p (name_type, class_type))
13167                     {
13168                       if (qualifying_scope
13169                           && CLASSTYPE_USE_TEMPLATE (name_type))
13170                         {
13171                           error ("%Hinvalid use of constructor as a template",
13172                                  &declarator_id_start_token->location);
13173                           inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13174                                   "name the constructor in a qualified name",
13175                                   class_type,
13176                                   DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13177                                   class_type, name_type);
13178                           declarator = cp_error_declarator;
13179                           break;
13180                         }
13181                       else
13182                         unqualified_name = constructor_name (class_type);
13183                     }
13184                   else
13185                     {
13186                       /* We do not attempt to print the declarator
13187                          here because we do not have enough
13188                          information about its original syntactic
13189                          form.  */
13190                       cp_parser_error (parser, "invalid declarator");
13191                       declarator = cp_error_declarator;
13192                       break;
13193                     }
13194                 }
13195
13196               if (class_type)
13197                 {
13198                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13199                     sfk = sfk_destructor;
13200                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13201                     sfk = sfk_conversion;
13202                   else if (/* There's no way to declare a constructor
13203                               for an anonymous type, even if the type
13204                               got a name for linkage purposes.  */
13205                            !TYPE_WAS_ANONYMOUS (class_type)
13206                            && constructor_name_p (unqualified_name,
13207                                                   class_type))
13208                     {
13209                       unqualified_name = constructor_name (class_type);
13210                       sfk = sfk_constructor;
13211                     }
13212
13213                   if (ctor_dtor_or_conv_p && sfk != sfk_none)
13214                     *ctor_dtor_or_conv_p = -1;
13215                 }
13216             }
13217           declarator = make_id_declarator (qualifying_scope,
13218                                            unqualified_name,
13219                                            sfk);
13220           declarator->id_loc = token->location;
13221           declarator->parameter_pack_p = pack_expansion_p;
13222
13223           if (pack_expansion_p)
13224             maybe_warn_variadic_templates ();
13225
13226         handle_declarator:;
13227           scope = get_scope_of_declarator (declarator);
13228           if (scope)
13229             /* Any names that appear after the declarator-id for a
13230                member are looked up in the containing scope.  */
13231             pushed_scope = push_scope (scope);
13232           parser->in_declarator_p = true;
13233           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13234               || (declarator && declarator->kind == cdk_id))
13235             /* Default args are only allowed on function
13236                declarations.  */
13237             parser->default_arg_ok_p = saved_default_arg_ok_p;
13238           else
13239             parser->default_arg_ok_p = false;
13240
13241           first = false;
13242         }
13243       /* We're done.  */
13244       else
13245         break;
13246     }
13247
13248   /* For an abstract declarator, we might wind up with nothing at this
13249      point.  That's an error; the declarator is not optional.  */
13250   if (!declarator)
13251     cp_parser_error (parser, "expected declarator");
13252
13253   /* If we entered a scope, we must exit it now.  */
13254   if (pushed_scope)
13255     pop_scope (pushed_scope);
13256
13257   parser->default_arg_ok_p = saved_default_arg_ok_p;
13258   parser->in_declarator_p = saved_in_declarator_p;
13259
13260   return declarator;
13261 }
13262
13263 /* Parse a ptr-operator.
13264
13265    ptr-operator:
13266      * cv-qualifier-seq [opt]
13267      &
13268      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13269
13270    GNU Extension:
13271
13272    ptr-operator:
13273      & cv-qualifier-seq [opt]
13274
13275    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13276    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13277    an rvalue reference. In the case of a pointer-to-member, *TYPE is
13278    filled in with the TYPE containing the member.  *CV_QUALS is
13279    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13280    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
13281    Note that the tree codes returned by this function have nothing
13282    to do with the types of trees that will be eventually be created
13283    to represent the pointer or reference type being parsed. They are
13284    just constants with suggestive names. */
13285 static enum tree_code
13286 cp_parser_ptr_operator (cp_parser* parser,
13287                         tree* type,
13288                         cp_cv_quals *cv_quals)
13289 {
13290   enum tree_code code = ERROR_MARK;
13291   cp_token *token;
13292
13293   /* Assume that it's not a pointer-to-member.  */
13294   *type = NULL_TREE;
13295   /* And that there are no cv-qualifiers.  */
13296   *cv_quals = TYPE_UNQUALIFIED;
13297
13298   /* Peek at the next token.  */
13299   token = cp_lexer_peek_token (parser->lexer);
13300
13301   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
13302   if (token->type == CPP_MULT)
13303     code = INDIRECT_REF;
13304   else if (token->type == CPP_AND)
13305     code = ADDR_EXPR;
13306   else if ((cxx_dialect != cxx98) &&
13307            token->type == CPP_AND_AND) /* C++0x only */
13308     code = NON_LVALUE_EXPR;
13309
13310   if (code != ERROR_MARK)
13311     {
13312       /* Consume the `*', `&' or `&&'.  */
13313       cp_lexer_consume_token (parser->lexer);
13314
13315       /* A `*' can be followed by a cv-qualifier-seq, and so can a
13316          `&', if we are allowing GNU extensions.  (The only qualifier
13317          that can legally appear after `&' is `restrict', but that is
13318          enforced during semantic analysis.  */
13319       if (code == INDIRECT_REF
13320           || cp_parser_allow_gnu_extensions_p (parser))
13321         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13322     }
13323   else
13324     {
13325       /* Try the pointer-to-member case.  */
13326       cp_parser_parse_tentatively (parser);
13327       /* Look for the optional `::' operator.  */
13328       cp_parser_global_scope_opt (parser,
13329                                   /*current_scope_valid_p=*/false);
13330       /* Look for the nested-name specifier.  */
13331       token = cp_lexer_peek_token (parser->lexer);
13332       cp_parser_nested_name_specifier (parser,
13333                                        /*typename_keyword_p=*/false,
13334                                        /*check_dependency_p=*/true,
13335                                        /*type_p=*/false,
13336                                        /*is_declaration=*/false);
13337       /* If we found it, and the next token is a `*', then we are
13338          indeed looking at a pointer-to-member operator.  */
13339       if (!cp_parser_error_occurred (parser)
13340           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13341         {
13342           /* Indicate that the `*' operator was used.  */
13343           code = INDIRECT_REF;
13344
13345           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13346             error ("%H%qD is a namespace", &token->location, parser->scope);
13347           else
13348             {
13349               /* The type of which the member is a member is given by the
13350                  current SCOPE.  */
13351               *type = parser->scope;
13352               /* The next name will not be qualified.  */
13353               parser->scope = NULL_TREE;
13354               parser->qualifying_scope = NULL_TREE;
13355               parser->object_scope = NULL_TREE;
13356               /* Look for the optional cv-qualifier-seq.  */
13357               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13358             }
13359         }
13360       /* If that didn't work we don't have a ptr-operator.  */
13361       if (!cp_parser_parse_definitely (parser))
13362         cp_parser_error (parser, "expected ptr-operator");
13363     }
13364
13365   return code;
13366 }
13367
13368 /* Parse an (optional) cv-qualifier-seq.
13369
13370    cv-qualifier-seq:
13371      cv-qualifier cv-qualifier-seq [opt]
13372
13373    cv-qualifier:
13374      const
13375      volatile
13376
13377    GNU Extension:
13378
13379    cv-qualifier:
13380      __restrict__
13381
13382    Returns a bitmask representing the cv-qualifiers.  */
13383
13384 static cp_cv_quals
13385 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13386 {
13387   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13388
13389   while (true)
13390     {
13391       cp_token *token;
13392       cp_cv_quals cv_qualifier;
13393
13394       /* Peek at the next token.  */
13395       token = cp_lexer_peek_token (parser->lexer);
13396       /* See if it's a cv-qualifier.  */
13397       switch (token->keyword)
13398         {
13399         case RID_CONST:
13400           cv_qualifier = TYPE_QUAL_CONST;
13401           break;
13402
13403         case RID_VOLATILE:
13404           cv_qualifier = TYPE_QUAL_VOLATILE;
13405           break;
13406
13407         case RID_RESTRICT:
13408           cv_qualifier = TYPE_QUAL_RESTRICT;
13409           break;
13410
13411         default:
13412           cv_qualifier = TYPE_UNQUALIFIED;
13413           break;
13414         }
13415
13416       if (!cv_qualifier)
13417         break;
13418
13419       if (cv_quals & cv_qualifier)
13420         {
13421           error ("%Hduplicate cv-qualifier", &token->location);
13422           cp_lexer_purge_token (parser->lexer);
13423         }
13424       else
13425         {
13426           cp_lexer_consume_token (parser->lexer);
13427           cv_quals |= cv_qualifier;
13428         }
13429     }
13430
13431   return cv_quals;
13432 }
13433
13434 /* Parse a declarator-id.
13435
13436    declarator-id:
13437      id-expression
13438      :: [opt] nested-name-specifier [opt] type-name
13439
13440    In the `id-expression' case, the value returned is as for
13441    cp_parser_id_expression if the id-expression was an unqualified-id.
13442    If the id-expression was a qualified-id, then a SCOPE_REF is
13443    returned.  The first operand is the scope (either a NAMESPACE_DECL
13444    or TREE_TYPE), but the second is still just a representation of an
13445    unqualified-id.  */
13446
13447 static tree
13448 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13449 {
13450   tree id;
13451   /* The expression must be an id-expression.  Assume that qualified
13452      names are the names of types so that:
13453
13454        template <class T>
13455        int S<T>::R::i = 3;
13456
13457      will work; we must treat `S<T>::R' as the name of a type.
13458      Similarly, assume that qualified names are templates, where
13459      required, so that:
13460
13461        template <class T>
13462        int S<T>::R<T>::i = 3;
13463
13464      will work, too.  */
13465   id = cp_parser_id_expression (parser,
13466                                 /*template_keyword_p=*/false,
13467                                 /*check_dependency_p=*/false,
13468                                 /*template_p=*/NULL,
13469                                 /*declarator_p=*/true,
13470                                 optional_p);
13471   if (id && BASELINK_P (id))
13472     id = BASELINK_FUNCTIONS (id);
13473   return id;
13474 }
13475
13476 /* Parse a type-id.
13477
13478    type-id:
13479      type-specifier-seq abstract-declarator [opt]
13480
13481    Returns the TYPE specified.  */
13482
13483 static tree
13484 cp_parser_type_id (cp_parser* parser)
13485 {
13486   cp_decl_specifier_seq type_specifier_seq;
13487   cp_declarator *abstract_declarator;
13488
13489   /* Parse the type-specifier-seq.  */
13490   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13491                                 &type_specifier_seq);
13492   if (type_specifier_seq.type == error_mark_node)
13493     return error_mark_node;
13494
13495   /* There might or might not be an abstract declarator.  */
13496   cp_parser_parse_tentatively (parser);
13497   /* Look for the declarator.  */
13498   abstract_declarator
13499     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13500                             /*parenthesized_p=*/NULL,
13501                             /*member_p=*/false);
13502   /* Check to see if there really was a declarator.  */
13503   if (!cp_parser_parse_definitely (parser))
13504     abstract_declarator = NULL;
13505
13506   return groktypename (&type_specifier_seq, abstract_declarator);
13507 }
13508
13509 /* Parse a type-specifier-seq.
13510
13511    type-specifier-seq:
13512      type-specifier type-specifier-seq [opt]
13513
13514    GNU extension:
13515
13516    type-specifier-seq:
13517      attributes type-specifier-seq [opt]
13518
13519    If IS_CONDITION is true, we are at the start of a "condition",
13520    e.g., we've just seen "if (".
13521
13522    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
13523
13524 static void
13525 cp_parser_type_specifier_seq (cp_parser* parser,
13526                               bool is_condition,
13527                               cp_decl_specifier_seq *type_specifier_seq)
13528 {
13529   bool seen_type_specifier = false;
13530   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13531   cp_token *start_token = NULL;
13532
13533   /* Clear the TYPE_SPECIFIER_SEQ.  */
13534   clear_decl_specs (type_specifier_seq);
13535
13536   /* Parse the type-specifiers and attributes.  */
13537   while (true)
13538     {
13539       tree type_specifier;
13540       bool is_cv_qualifier;
13541
13542       /* Check for attributes first.  */
13543       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13544         {
13545           type_specifier_seq->attributes =
13546             chainon (type_specifier_seq->attributes,
13547                      cp_parser_attributes_opt (parser));
13548           continue;
13549         }
13550
13551       /* record the token of the beginning of the type specifier seq,
13552          for error reporting purposes*/
13553      if (!start_token)
13554        start_token = cp_lexer_peek_token (parser->lexer);
13555
13556       /* Look for the type-specifier.  */
13557       type_specifier = cp_parser_type_specifier (parser,
13558                                                  flags,
13559                                                  type_specifier_seq,
13560                                                  /*is_declaration=*/false,
13561                                                  NULL,
13562                                                  &is_cv_qualifier);
13563       if (!type_specifier)
13564         {
13565           /* If the first type-specifier could not be found, this is not a
13566              type-specifier-seq at all.  */
13567           if (!seen_type_specifier)
13568             {
13569               cp_parser_error (parser, "expected type-specifier");
13570               type_specifier_seq->type = error_mark_node;
13571               return;
13572             }
13573           /* If subsequent type-specifiers could not be found, the
13574              type-specifier-seq is complete.  */
13575           break;
13576         }
13577
13578       seen_type_specifier = true;
13579       /* The standard says that a condition can be:
13580
13581             type-specifier-seq declarator = assignment-expression
13582
13583          However, given:
13584
13585            struct S {};
13586            if (int S = ...)
13587
13588          we should treat the "S" as a declarator, not as a
13589          type-specifier.  The standard doesn't say that explicitly for
13590          type-specifier-seq, but it does say that for
13591          decl-specifier-seq in an ordinary declaration.  Perhaps it
13592          would be clearer just to allow a decl-specifier-seq here, and
13593          then add a semantic restriction that if any decl-specifiers
13594          that are not type-specifiers appear, the program is invalid.  */
13595       if (is_condition && !is_cv_qualifier)
13596         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13597     }
13598
13599   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13600 }
13601
13602 /* Parse a parameter-declaration-clause.
13603
13604    parameter-declaration-clause:
13605      parameter-declaration-list [opt] ... [opt]
13606      parameter-declaration-list , ...
13607
13608    Returns a representation for the parameter declarations.  A return
13609    value of NULL indicates a parameter-declaration-clause consisting
13610    only of an ellipsis.  */
13611
13612 static cp_parameter_declarator *
13613 cp_parser_parameter_declaration_clause (cp_parser* parser)
13614 {
13615   cp_parameter_declarator *parameters;
13616   cp_token *token;
13617   bool ellipsis_p;
13618   bool is_error;
13619
13620   /* Peek at the next token.  */
13621   token = cp_lexer_peek_token (parser->lexer);
13622   /* Check for trivial parameter-declaration-clauses.  */
13623   if (token->type == CPP_ELLIPSIS)
13624     {
13625       /* Consume the `...' token.  */
13626       cp_lexer_consume_token (parser->lexer);
13627       return NULL;
13628     }
13629   else if (token->type == CPP_CLOSE_PAREN)
13630     /* There are no parameters.  */
13631     {
13632 #ifndef NO_IMPLICIT_EXTERN_C
13633       if (in_system_header && current_class_type == NULL
13634           && current_lang_name == lang_name_c)
13635         return NULL;
13636       else
13637 #endif
13638         return no_parameters;
13639     }
13640   /* Check for `(void)', too, which is a special case.  */
13641   else if (token->keyword == RID_VOID
13642            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13643                == CPP_CLOSE_PAREN))
13644     {
13645       /* Consume the `void' token.  */
13646       cp_lexer_consume_token (parser->lexer);
13647       /* There are no parameters.  */
13648       return no_parameters;
13649     }
13650
13651   /* Parse the parameter-declaration-list.  */
13652   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13653   /* If a parse error occurred while parsing the
13654      parameter-declaration-list, then the entire
13655      parameter-declaration-clause is erroneous.  */
13656   if (is_error)
13657     return NULL;
13658
13659   /* Peek at the next token.  */
13660   token = cp_lexer_peek_token (parser->lexer);
13661   /* If it's a `,', the clause should terminate with an ellipsis.  */
13662   if (token->type == CPP_COMMA)
13663     {
13664       /* Consume the `,'.  */
13665       cp_lexer_consume_token (parser->lexer);
13666       /* Expect an ellipsis.  */
13667       ellipsis_p
13668         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
13669     }
13670   /* It might also be `...' if the optional trailing `,' was
13671      omitted.  */
13672   else if (token->type == CPP_ELLIPSIS)
13673     {
13674       /* Consume the `...' token.  */
13675       cp_lexer_consume_token (parser->lexer);
13676       /* And remember that we saw it.  */
13677       ellipsis_p = true;
13678     }
13679   else
13680     ellipsis_p = false;
13681
13682   /* Finish the parameter list.  */
13683   if (parameters && ellipsis_p)
13684     parameters->ellipsis_p = true;
13685
13686   return parameters;
13687 }
13688
13689 /* Parse a parameter-declaration-list.
13690
13691    parameter-declaration-list:
13692      parameter-declaration
13693      parameter-declaration-list , parameter-declaration
13694
13695    Returns a representation of the parameter-declaration-list, as for
13696    cp_parser_parameter_declaration_clause.  However, the
13697    `void_list_node' is never appended to the list.  Upon return,
13698    *IS_ERROR will be true iff an error occurred.  */
13699
13700 static cp_parameter_declarator *
13701 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13702 {
13703   cp_parameter_declarator *parameters = NULL;
13704   cp_parameter_declarator **tail = &parameters;
13705   bool saved_in_unbraced_linkage_specification_p;
13706
13707   /* Assume all will go well.  */
13708   *is_error = false;
13709   /* The special considerations that apply to a function within an
13710      unbraced linkage specifications do not apply to the parameters
13711      to the function.  */
13712   saved_in_unbraced_linkage_specification_p 
13713     = parser->in_unbraced_linkage_specification_p;
13714   parser->in_unbraced_linkage_specification_p = false;
13715
13716   /* Look for more parameters.  */
13717   while (true)
13718     {
13719       cp_parameter_declarator *parameter;
13720       bool parenthesized_p;
13721       /* Parse the parameter.  */
13722       parameter
13723         = cp_parser_parameter_declaration (parser,
13724                                            /*template_parm_p=*/false,
13725                                            &parenthesized_p);
13726
13727       /* If a parse error occurred parsing the parameter declaration,
13728          then the entire parameter-declaration-list is erroneous.  */
13729       if (!parameter)
13730         {
13731           *is_error = true;
13732           parameters = NULL;
13733           break;
13734         }
13735       /* Add the new parameter to the list.  */
13736       *tail = parameter;
13737       tail = &parameter->next;
13738
13739       /* Peek at the next token.  */
13740       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13741           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13742           /* These are for Objective-C++ */
13743           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13744           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13745         /* The parameter-declaration-list is complete.  */
13746         break;
13747       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13748         {
13749           cp_token *token;
13750
13751           /* Peek at the next token.  */
13752           token = cp_lexer_peek_nth_token (parser->lexer, 2);
13753           /* If it's an ellipsis, then the list is complete.  */
13754           if (token->type == CPP_ELLIPSIS)
13755             break;
13756           /* Otherwise, there must be more parameters.  Consume the
13757              `,'.  */
13758           cp_lexer_consume_token (parser->lexer);
13759           /* When parsing something like:
13760
13761                 int i(float f, double d)
13762
13763              we can tell after seeing the declaration for "f" that we
13764              are not looking at an initialization of a variable "i",
13765              but rather at the declaration of a function "i".
13766
13767              Due to the fact that the parsing of template arguments
13768              (as specified to a template-id) requires backtracking we
13769              cannot use this technique when inside a template argument
13770              list.  */
13771           if (!parser->in_template_argument_list_p
13772               && !parser->in_type_id_in_expr_p
13773               && cp_parser_uncommitted_to_tentative_parse_p (parser)
13774               /* However, a parameter-declaration of the form
13775                  "foat(f)" (which is a valid declaration of a
13776                  parameter "f") can also be interpreted as an
13777                  expression (the conversion of "f" to "float").  */
13778               && !parenthesized_p)
13779             cp_parser_commit_to_tentative_parse (parser);
13780         }
13781       else
13782         {
13783           cp_parser_error (parser, "expected %<,%> or %<...%>");
13784           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13785             cp_parser_skip_to_closing_parenthesis (parser,
13786                                                    /*recovering=*/true,
13787                                                    /*or_comma=*/false,
13788                                                    /*consume_paren=*/false);
13789           break;
13790         }
13791     }
13792
13793   parser->in_unbraced_linkage_specification_p
13794     = saved_in_unbraced_linkage_specification_p;
13795
13796   return parameters;
13797 }
13798
13799 /* Parse a parameter declaration.
13800
13801    parameter-declaration:
13802      decl-specifier-seq ... [opt] declarator
13803      decl-specifier-seq declarator = assignment-expression
13804      decl-specifier-seq ... [opt] abstract-declarator [opt]
13805      decl-specifier-seq abstract-declarator [opt] = assignment-expression
13806
13807    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13808    declares a template parameter.  (In that case, a non-nested `>'
13809    token encountered during the parsing of the assignment-expression
13810    is not interpreted as a greater-than operator.)
13811
13812    Returns a representation of the parameter, or NULL if an error
13813    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13814    true iff the declarator is of the form "(p)".  */
13815
13816 static cp_parameter_declarator *
13817 cp_parser_parameter_declaration (cp_parser *parser,
13818                                  bool template_parm_p,
13819                                  bool *parenthesized_p)
13820 {
13821   int declares_class_or_enum;
13822   bool greater_than_is_operator_p;
13823   cp_decl_specifier_seq decl_specifiers;
13824   cp_declarator *declarator;
13825   tree default_argument;
13826   cp_token *token = NULL, *declarator_token_start = NULL;
13827   const char *saved_message;
13828
13829   /* In a template parameter, `>' is not an operator.
13830
13831      [temp.param]
13832
13833      When parsing a default template-argument for a non-type
13834      template-parameter, the first non-nested `>' is taken as the end
13835      of the template parameter-list rather than a greater-than
13836      operator.  */
13837   greater_than_is_operator_p = !template_parm_p;
13838
13839   /* Type definitions may not appear in parameter types.  */
13840   saved_message = parser->type_definition_forbidden_message;
13841   parser->type_definition_forbidden_message
13842     = "types may not be defined in parameter types";
13843
13844   /* Parse the declaration-specifiers.  */
13845   cp_parser_decl_specifier_seq (parser,
13846                                 CP_PARSER_FLAGS_NONE,
13847                                 &decl_specifiers,
13848                                 &declares_class_or_enum);
13849   /* If an error occurred, there's no reason to attempt to parse the
13850      rest of the declaration.  */
13851   if (cp_parser_error_occurred (parser))
13852     {
13853       parser->type_definition_forbidden_message = saved_message;
13854       return NULL;
13855     }
13856
13857   /* Peek at the next token.  */
13858   token = cp_lexer_peek_token (parser->lexer);
13859
13860   /* If the next token is a `)', `,', `=', `>', or `...', then there
13861      is no declarator. However, when variadic templates are enabled,
13862      there may be a declarator following `...'.  */
13863   if (token->type == CPP_CLOSE_PAREN
13864       || token->type == CPP_COMMA
13865       || token->type == CPP_EQ
13866       || token->type == CPP_GREATER)
13867     {
13868       declarator = NULL;
13869       if (parenthesized_p)
13870         *parenthesized_p = false;
13871     }
13872   /* Otherwise, there should be a declarator.  */
13873   else
13874     {
13875       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13876       parser->default_arg_ok_p = false;
13877
13878       /* After seeing a decl-specifier-seq, if the next token is not a
13879          "(", there is no possibility that the code is a valid
13880          expression.  Therefore, if parsing tentatively, we commit at
13881          this point.  */
13882       if (!parser->in_template_argument_list_p
13883           /* In an expression context, having seen:
13884
13885                (int((char ...
13886
13887              we cannot be sure whether we are looking at a
13888              function-type (taking a "char" as a parameter) or a cast
13889              of some object of type "char" to "int".  */
13890           && !parser->in_type_id_in_expr_p
13891           && cp_parser_uncommitted_to_tentative_parse_p (parser)
13892           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13893         cp_parser_commit_to_tentative_parse (parser);
13894       /* Parse the declarator.  */
13895       declarator_token_start = token;
13896       declarator = cp_parser_declarator (parser,
13897                                          CP_PARSER_DECLARATOR_EITHER,
13898                                          /*ctor_dtor_or_conv_p=*/NULL,
13899                                          parenthesized_p,
13900                                          /*member_p=*/false);
13901       parser->default_arg_ok_p = saved_default_arg_ok_p;
13902       /* After the declarator, allow more attributes.  */
13903       decl_specifiers.attributes
13904         = chainon (decl_specifiers.attributes,
13905                    cp_parser_attributes_opt (parser));
13906     }
13907
13908   /* If the next token is an ellipsis, and we have not seen a
13909      declarator name, and the type of the declarator contains parameter
13910      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13911      a parameter pack expansion expression. Otherwise, leave the
13912      ellipsis for a C-style variadic function. */
13913   token = cp_lexer_peek_token (parser->lexer);
13914   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13915     {
13916       tree type = decl_specifiers.type;
13917
13918       if (type && DECL_P (type))
13919         type = TREE_TYPE (type);
13920
13921       if (type
13922           && TREE_CODE (type) != TYPE_PACK_EXPANSION
13923           && declarator_can_be_parameter_pack (declarator)
13924           && (!declarator || !declarator->parameter_pack_p)
13925           && uses_parameter_packs (type))
13926         {
13927           /* Consume the `...'. */
13928           cp_lexer_consume_token (parser->lexer);
13929           maybe_warn_variadic_templates ();
13930           
13931           /* Build a pack expansion type */
13932           if (declarator)
13933             declarator->parameter_pack_p = true;
13934           else
13935             decl_specifiers.type = make_pack_expansion (type);
13936         }
13937     }
13938
13939   /* The restriction on defining new types applies only to the type
13940      of the parameter, not to the default argument.  */
13941   parser->type_definition_forbidden_message = saved_message;
13942
13943   /* If the next token is `=', then process a default argument.  */
13944   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13945     {
13946       /* Consume the `='.  */
13947       cp_lexer_consume_token (parser->lexer);
13948
13949       /* If we are defining a class, then the tokens that make up the
13950          default argument must be saved and processed later.  */
13951       if (!template_parm_p && at_class_scope_p ()
13952           && TYPE_BEING_DEFINED (current_class_type))
13953         {
13954           unsigned depth = 0;
13955           int maybe_template_id = 0;
13956           cp_token *first_token;
13957           cp_token *token;
13958
13959           /* Add tokens until we have processed the entire default
13960              argument.  We add the range [first_token, token).  */
13961           first_token = cp_lexer_peek_token (parser->lexer);
13962           while (true)
13963             {
13964               bool done = false;
13965
13966               /* Peek at the next token.  */
13967               token = cp_lexer_peek_token (parser->lexer);
13968               /* What we do depends on what token we have.  */
13969               switch (token->type)
13970                 {
13971                   /* In valid code, a default argument must be
13972                      immediately followed by a `,' `)', or `...'.  */
13973                 case CPP_COMMA:
13974                   if (depth == 0 && maybe_template_id)
13975                     {
13976                       /* If we've seen a '<', we might be in a
13977                          template-argument-list.  Until Core issue 325 is
13978                          resolved, we don't know how this situation ought
13979                          to be handled, so try to DTRT.  We check whether
13980                          what comes after the comma is a valid parameter
13981                          declaration list.  If it is, then the comma ends
13982                          the default argument; otherwise the default
13983                          argument continues.  */
13984                       bool error = false;
13985
13986                       /* Set ITALP so cp_parser_parameter_declaration_list
13987                          doesn't decide to commit to this parse.  */
13988                       bool saved_italp = parser->in_template_argument_list_p;
13989                       parser->in_template_argument_list_p = true;
13990
13991                       cp_parser_parse_tentatively (parser);
13992                       cp_lexer_consume_token (parser->lexer);
13993                       cp_parser_parameter_declaration_list (parser, &error);
13994                       if (!cp_parser_error_occurred (parser) && !error)
13995                         done = true;
13996                       cp_parser_abort_tentative_parse (parser);
13997
13998                       parser->in_template_argument_list_p = saved_italp;
13999                       break;
14000                     }
14001                 case CPP_CLOSE_PAREN:
14002                 case CPP_ELLIPSIS:
14003                   /* If we run into a non-nested `;', `}', or `]',
14004                      then the code is invalid -- but the default
14005                      argument is certainly over.  */
14006                 case CPP_SEMICOLON:
14007                 case CPP_CLOSE_BRACE:
14008                 case CPP_CLOSE_SQUARE:
14009                   if (depth == 0)
14010                     done = true;
14011                   /* Update DEPTH, if necessary.  */
14012                   else if (token->type == CPP_CLOSE_PAREN
14013                            || token->type == CPP_CLOSE_BRACE
14014                            || token->type == CPP_CLOSE_SQUARE)
14015                     --depth;
14016                   break;
14017
14018                 case CPP_OPEN_PAREN:
14019                 case CPP_OPEN_SQUARE:
14020                 case CPP_OPEN_BRACE:
14021                   ++depth;
14022                   break;
14023
14024                 case CPP_LESS:
14025                   if (depth == 0)
14026                     /* This might be the comparison operator, or it might
14027                        start a template argument list.  */
14028                     ++maybe_template_id;
14029                   break;
14030
14031                 case CPP_RSHIFT:
14032                   if (cxx_dialect == cxx98)
14033                     break;
14034                   /* Fall through for C++0x, which treats the `>>'
14035                      operator like two `>' tokens in certain
14036                      cases.  */
14037
14038                 case CPP_GREATER:
14039                   if (depth == 0)
14040                     {
14041                       /* This might be an operator, or it might close a
14042                          template argument list.  But if a previous '<'
14043                          started a template argument list, this will have
14044                          closed it, so we can't be in one anymore.  */
14045                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14046                       if (maybe_template_id < 0)
14047                         maybe_template_id = 0;
14048                     }
14049                   break;
14050
14051                   /* If we run out of tokens, issue an error message.  */
14052                 case CPP_EOF:
14053                 case CPP_PRAGMA_EOL:
14054                   error ("%Hfile ends in default argument", &token->location);
14055                   done = true;
14056                   break;
14057
14058                 case CPP_NAME:
14059                 case CPP_SCOPE:
14060                   /* In these cases, we should look for template-ids.
14061                      For example, if the default argument is
14062                      `X<int, double>()', we need to do name lookup to
14063                      figure out whether or not `X' is a template; if
14064                      so, the `,' does not end the default argument.
14065
14066                      That is not yet done.  */
14067                   break;
14068
14069                 default:
14070                   break;
14071                 }
14072
14073               /* If we've reached the end, stop.  */
14074               if (done)
14075                 break;
14076
14077               /* Add the token to the token block.  */
14078               token = cp_lexer_consume_token (parser->lexer);
14079             }
14080
14081           /* Create a DEFAULT_ARG to represent the unparsed default
14082              argument.  */
14083           default_argument = make_node (DEFAULT_ARG);
14084           DEFARG_TOKENS (default_argument)
14085             = cp_token_cache_new (first_token, token);
14086           DEFARG_INSTANTIATIONS (default_argument) = NULL;
14087         }
14088       /* Outside of a class definition, we can just parse the
14089          assignment-expression.  */
14090       else
14091         {
14092           token = cp_lexer_peek_token (parser->lexer);
14093           default_argument 
14094             = cp_parser_default_argument (parser, template_parm_p);
14095         }
14096
14097       if (!parser->default_arg_ok_p)
14098         {
14099           if (flag_permissive)
14100             warning (0, "deprecated use of default argument for parameter of non-function");
14101           else
14102             {
14103               error ("%Hdefault arguments are only "
14104                      "permitted for function parameters",
14105                      &token->location);
14106               default_argument = NULL_TREE;
14107             }
14108         }
14109       else if ((declarator && declarator->parameter_pack_p)
14110                || (decl_specifiers.type
14111                    && PACK_EXPANSION_P (decl_specifiers.type)))
14112         {
14113           const char* kind = template_parm_p? "template " : "";
14114           
14115           /* Find the name of the parameter pack.  */     
14116           cp_declarator *id_declarator = declarator;
14117           while (id_declarator && id_declarator->kind != cdk_id)
14118             id_declarator = id_declarator->declarator;
14119           
14120           if (id_declarator && id_declarator->kind == cdk_id)
14121             error ("%H%sparameter pack %qD cannot have a default argument",
14122                    &declarator_token_start->location,
14123                    kind, id_declarator->u.id.unqualified_name);
14124           else
14125             error ("%H%sparameter pack cannot have a default argument",
14126                    &declarator_token_start->location, kind);
14127           
14128           default_argument = NULL_TREE;
14129         }
14130     }
14131   else
14132     default_argument = NULL_TREE;
14133
14134   return make_parameter_declarator (&decl_specifiers,
14135                                     declarator,
14136                                     default_argument);
14137 }
14138
14139 /* Parse a default argument and return it.
14140
14141    TEMPLATE_PARM_P is true if this is a default argument for a
14142    non-type template parameter.  */
14143 static tree
14144 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14145 {
14146   tree default_argument = NULL_TREE;
14147   bool saved_greater_than_is_operator_p;
14148   bool saved_local_variables_forbidden_p;
14149
14150   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14151      set correctly.  */
14152   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14153   parser->greater_than_is_operator_p = !template_parm_p;
14154   /* Local variable names (and the `this' keyword) may not
14155      appear in a default argument.  */
14156   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14157   parser->local_variables_forbidden_p = true;
14158   /* The default argument expression may cause implicitly
14159      defined member functions to be synthesized, which will
14160      result in garbage collection.  We must treat this
14161      situation as if we were within the body of function so as
14162      to avoid collecting live data on the stack.  */
14163   ++function_depth;
14164   /* Parse the assignment-expression.  */
14165   if (template_parm_p)
14166     push_deferring_access_checks (dk_no_deferred);
14167   default_argument
14168     = cp_parser_assignment_expression (parser, /*cast_p=*/false);
14169   if (template_parm_p)
14170     pop_deferring_access_checks ();
14171   /* Restore saved state.  */
14172   --function_depth;
14173   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14174   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14175
14176   return default_argument;
14177 }
14178
14179 /* Parse a function-body.
14180
14181    function-body:
14182      compound_statement  */
14183
14184 static void
14185 cp_parser_function_body (cp_parser *parser)
14186 {
14187   cp_parser_compound_statement (parser, NULL, false);
14188 }
14189
14190 /* Parse a ctor-initializer-opt followed by a function-body.  Return
14191    true if a ctor-initializer was present.  */
14192
14193 static bool
14194 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14195 {
14196   tree body;
14197   bool ctor_initializer_p;
14198
14199   /* Begin the function body.  */
14200   body = begin_function_body ();
14201   /* Parse the optional ctor-initializer.  */
14202   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14203   /* Parse the function-body.  */
14204   cp_parser_function_body (parser);
14205   /* Finish the function body.  */
14206   finish_function_body (body);
14207
14208   return ctor_initializer_p;
14209 }
14210
14211 /* Parse an initializer.
14212
14213    initializer:
14214      = initializer-clause
14215      ( expression-list )
14216
14217    Returns an expression representing the initializer.  If no
14218    initializer is present, NULL_TREE is returned.
14219
14220    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14221    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
14222    set to TRUE if there is no initializer present.  If there is an
14223    initializer, and it is not a constant-expression, *NON_CONSTANT_P
14224    is set to true; otherwise it is set to false.  */
14225
14226 static tree
14227 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14228                        bool* non_constant_p)
14229 {
14230   cp_token *token;
14231   tree init;
14232
14233   /* Peek at the next token.  */
14234   token = cp_lexer_peek_token (parser->lexer);
14235
14236   /* Let our caller know whether or not this initializer was
14237      parenthesized.  */
14238   *is_direct_init = (token->type != CPP_EQ);
14239   /* Assume that the initializer is constant.  */
14240   *non_constant_p = false;
14241
14242   if (token->type == CPP_EQ)
14243     {
14244       /* Consume the `='.  */
14245       cp_lexer_consume_token (parser->lexer);
14246       /* Parse the initializer-clause.  */
14247       init = cp_parser_initializer_clause (parser, non_constant_p);
14248     }
14249   else if (token->type == CPP_OPEN_PAREN)
14250     init = cp_parser_parenthesized_expression_list (parser, false,
14251                                                     /*cast_p=*/false,
14252                                                     /*allow_expansion_p=*/true,
14253                                                     non_constant_p);
14254   else if (token->type == CPP_OPEN_BRACE)
14255     {
14256       maybe_warn_cpp0x ("extended initializer lists");
14257       init = cp_parser_braced_list (parser, non_constant_p);
14258       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14259     }
14260   else
14261     {
14262       /* Anything else is an error.  */
14263       cp_parser_error (parser, "expected initializer");
14264       init = error_mark_node;
14265     }
14266
14267   return init;
14268 }
14269
14270 /* Parse an initializer-clause.
14271
14272    initializer-clause:
14273      assignment-expression
14274      braced-init-list
14275
14276    Returns an expression representing the initializer.
14277
14278    If the `assignment-expression' production is used the value
14279    returned is simply a representation for the expression.
14280
14281    Otherwise, calls cp_parser_braced_list.  */
14282
14283 static tree
14284 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14285 {
14286   tree initializer;
14287
14288   /* Assume the expression is constant.  */
14289   *non_constant_p = false;
14290
14291   /* If it is not a `{', then we are looking at an
14292      assignment-expression.  */
14293   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14294     {
14295       initializer
14296         = cp_parser_constant_expression (parser,
14297                                         /*allow_non_constant_p=*/true,
14298                                         non_constant_p);
14299       if (!*non_constant_p)
14300         initializer = fold_non_dependent_expr (initializer);
14301     }
14302   else
14303     initializer = cp_parser_braced_list (parser, non_constant_p);
14304
14305   return initializer;
14306 }
14307
14308 /* Parse a brace-enclosed initializer list.
14309
14310    braced-init-list:
14311      { initializer-list , [opt] }
14312      { }
14313
14314    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
14315    the elements of the initializer-list (or NULL, if the last
14316    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
14317    NULL_TREE.  There is no way to detect whether or not the optional
14318    trailing `,' was provided.  NON_CONSTANT_P is as for
14319    cp_parser_initializer.  */     
14320
14321 static tree
14322 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14323 {
14324   tree initializer;
14325
14326   /* Consume the `{' token.  */
14327   cp_lexer_consume_token (parser->lexer);
14328   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
14329   initializer = make_node (CONSTRUCTOR);
14330   /* If it's not a `}', then there is a non-trivial initializer.  */
14331   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14332     {
14333       /* Parse the initializer list.  */
14334       CONSTRUCTOR_ELTS (initializer)
14335         = cp_parser_initializer_list (parser, non_constant_p);
14336       /* A trailing `,' token is allowed.  */
14337       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14338         cp_lexer_consume_token (parser->lexer);
14339     }
14340   /* Now, there should be a trailing `}'.  */
14341   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14342   TREE_TYPE (initializer) = init_list_type_node;
14343   return initializer;
14344 }
14345
14346 /* Parse an initializer-list.
14347
14348    initializer-list:
14349      initializer-clause ... [opt]
14350      initializer-list , initializer-clause ... [opt]
14351
14352    GNU Extension:
14353
14354    initializer-list:
14355      identifier : initializer-clause
14356      initializer-list, identifier : initializer-clause
14357
14358    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
14359    for the initializer.  If the INDEX of the elt is non-NULL, it is the
14360    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
14361    as for cp_parser_initializer.  */
14362
14363 static VEC(constructor_elt,gc) *
14364 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14365 {
14366   VEC(constructor_elt,gc) *v = NULL;
14367
14368   /* Assume all of the expressions are constant.  */
14369   *non_constant_p = false;
14370
14371   /* Parse the rest of the list.  */
14372   while (true)
14373     {
14374       cp_token *token;
14375       tree identifier;
14376       tree initializer;
14377       bool clause_non_constant_p;
14378
14379       /* If the next token is an identifier and the following one is a
14380          colon, we are looking at the GNU designated-initializer
14381          syntax.  */
14382       if (cp_parser_allow_gnu_extensions_p (parser)
14383           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14384           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14385         {
14386           /* Warn the user that they are using an extension.  */
14387           pedwarn (OPT_pedantic, 
14388                    "ISO C++ does not allow designated initializers");
14389           /* Consume the identifier.  */
14390           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14391           /* Consume the `:'.  */
14392           cp_lexer_consume_token (parser->lexer);
14393         }
14394       else
14395         identifier = NULL_TREE;
14396
14397       /* Parse the initializer.  */
14398       initializer = cp_parser_initializer_clause (parser,
14399                                                   &clause_non_constant_p);
14400       /* If any clause is non-constant, so is the entire initializer.  */
14401       if (clause_non_constant_p)
14402         *non_constant_p = true;
14403
14404       /* If we have an ellipsis, this is an initializer pack
14405          expansion.  */
14406       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14407         {
14408           /* Consume the `...'.  */
14409           cp_lexer_consume_token (parser->lexer);
14410
14411           /* Turn the initializer into an initializer expansion.  */
14412           initializer = make_pack_expansion (initializer);
14413         }
14414
14415       /* Add it to the vector.  */
14416       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14417
14418       /* If the next token is not a comma, we have reached the end of
14419          the list.  */
14420       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14421         break;
14422
14423       /* Peek at the next token.  */
14424       token = cp_lexer_peek_nth_token (parser->lexer, 2);
14425       /* If the next token is a `}', then we're still done.  An
14426          initializer-clause can have a trailing `,' after the
14427          initializer-list and before the closing `}'.  */
14428       if (token->type == CPP_CLOSE_BRACE)
14429         break;
14430
14431       /* Consume the `,' token.  */
14432       cp_lexer_consume_token (parser->lexer);
14433     }
14434
14435   return v;
14436 }
14437
14438 /* Classes [gram.class] */
14439
14440 /* Parse a class-name.
14441
14442    class-name:
14443      identifier
14444      template-id
14445
14446    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14447    to indicate that names looked up in dependent types should be
14448    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
14449    keyword has been used to indicate that the name that appears next
14450    is a template.  TAG_TYPE indicates the explicit tag given before
14451    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
14452    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
14453    is the class being defined in a class-head.
14454
14455    Returns the TYPE_DECL representing the class.  */
14456
14457 static tree
14458 cp_parser_class_name (cp_parser *parser,
14459                       bool typename_keyword_p,
14460                       bool template_keyword_p,
14461                       enum tag_types tag_type,
14462                       bool check_dependency_p,
14463                       bool class_head_p,
14464                       bool is_declaration)
14465 {
14466   tree decl;
14467   tree scope;
14468   bool typename_p;
14469   cp_token *token;
14470
14471   /* All class-names start with an identifier.  */
14472   token = cp_lexer_peek_token (parser->lexer);
14473   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14474     {
14475       cp_parser_error (parser, "expected class-name");
14476       return error_mark_node;
14477     }
14478
14479   /* PARSER->SCOPE can be cleared when parsing the template-arguments
14480      to a template-id, so we save it here.  */
14481   scope = parser->scope;
14482   if (scope == error_mark_node)
14483     return error_mark_node;
14484
14485   /* Any name names a type if we're following the `typename' keyword
14486      in a qualified name where the enclosing scope is type-dependent.  */
14487   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14488                 && dependent_type_p (scope));
14489   /* Handle the common case (an identifier, but not a template-id)
14490      efficiently.  */
14491   if (token->type == CPP_NAME
14492       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14493     {
14494       cp_token *identifier_token;
14495       tree identifier;
14496       bool ambiguous_p;
14497
14498       /* Look for the identifier.  */
14499       identifier_token = cp_lexer_peek_token (parser->lexer);
14500       ambiguous_p = identifier_token->ambiguous_p;
14501       identifier = cp_parser_identifier (parser);
14502       /* If the next token isn't an identifier, we are certainly not
14503          looking at a class-name.  */
14504       if (identifier == error_mark_node)
14505         decl = error_mark_node;
14506       /* If we know this is a type-name, there's no need to look it
14507          up.  */
14508       else if (typename_p)
14509         decl = identifier;
14510       else
14511         {
14512           tree ambiguous_decls;
14513           /* If we already know that this lookup is ambiguous, then
14514              we've already issued an error message; there's no reason
14515              to check again.  */
14516           if (ambiguous_p)
14517             {
14518               cp_parser_simulate_error (parser);
14519               return error_mark_node;
14520             }
14521           /* If the next token is a `::', then the name must be a type
14522              name.
14523
14524              [basic.lookup.qual]
14525
14526              During the lookup for a name preceding the :: scope
14527              resolution operator, object, function, and enumerator
14528              names are ignored.  */
14529           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14530             tag_type = typename_type;
14531           /* Look up the name.  */
14532           decl = cp_parser_lookup_name (parser, identifier,
14533                                         tag_type,
14534                                         /*is_template=*/false,
14535                                         /*is_namespace=*/false,
14536                                         check_dependency_p,
14537                                         &ambiguous_decls,
14538                                         identifier_token->location);
14539           if (ambiguous_decls)
14540             {
14541               error ("%Hreference to %qD is ambiguous",
14542                      &identifier_token->location, identifier);
14543               print_candidates (ambiguous_decls);
14544               if (cp_parser_parsing_tentatively (parser))
14545                 {
14546                   identifier_token->ambiguous_p = true;
14547                   cp_parser_simulate_error (parser);
14548                 }
14549               return error_mark_node;
14550             }
14551         }
14552     }
14553   else
14554     {
14555       /* Try a template-id.  */
14556       decl = cp_parser_template_id (parser, template_keyword_p,
14557                                     check_dependency_p,
14558                                     is_declaration);
14559       if (decl == error_mark_node)
14560         return error_mark_node;
14561     }
14562
14563   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14564
14565   /* If this is a typename, create a TYPENAME_TYPE.  */
14566   if (typename_p && decl != error_mark_node)
14567     {
14568       decl = make_typename_type (scope, decl, typename_type,
14569                                  /*complain=*/tf_error);
14570       if (decl != error_mark_node)
14571         decl = TYPE_NAME (decl);
14572     }
14573
14574   /* Check to see that it is really the name of a class.  */
14575   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14576       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14577       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14578     /* Situations like this:
14579
14580          template <typename T> struct A {
14581            typename T::template X<int>::I i;
14582          };
14583
14584        are problematic.  Is `T::template X<int>' a class-name?  The
14585        standard does not seem to be definitive, but there is no other
14586        valid interpretation of the following `::'.  Therefore, those
14587        names are considered class-names.  */
14588     {
14589       decl = make_typename_type (scope, decl, tag_type, tf_error);
14590       if (decl != error_mark_node)
14591         decl = TYPE_NAME (decl);
14592     }
14593   else if (TREE_CODE (decl) != TYPE_DECL
14594            || TREE_TYPE (decl) == error_mark_node
14595            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
14596     decl = error_mark_node;
14597
14598   if (decl == error_mark_node)
14599     cp_parser_error (parser, "expected class-name");
14600
14601   return decl;
14602 }
14603
14604 /* Parse a class-specifier.
14605
14606    class-specifier:
14607      class-head { member-specification [opt] }
14608
14609    Returns the TREE_TYPE representing the class.  */
14610
14611 static tree
14612 cp_parser_class_specifier (cp_parser* parser)
14613 {
14614   cp_token *token;
14615   tree type;
14616   tree attributes = NULL_TREE;
14617   int has_trailing_semicolon;
14618   bool nested_name_specifier_p;
14619   unsigned saved_num_template_parameter_lists;
14620   bool saved_in_function_body;
14621   tree old_scope = NULL_TREE;
14622   tree scope = NULL_TREE;
14623   tree bases;
14624
14625   push_deferring_access_checks (dk_no_deferred);
14626
14627   /* Parse the class-head.  */
14628   type = cp_parser_class_head (parser,
14629                                &nested_name_specifier_p,
14630                                &attributes,
14631                                &bases);
14632   /* If the class-head was a semantic disaster, skip the entire body
14633      of the class.  */
14634   if (!type)
14635     {
14636       cp_parser_skip_to_end_of_block_or_statement (parser);
14637       pop_deferring_access_checks ();
14638       return error_mark_node;
14639     }
14640
14641   /* Look for the `{'.  */
14642   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
14643     {
14644       pop_deferring_access_checks ();
14645       return error_mark_node;
14646     }
14647
14648   /* Process the base classes. If they're invalid, skip the 
14649      entire class body.  */
14650   if (!xref_basetypes (type, bases))
14651     {
14652       /* Consuming the closing brace yields better error messages
14653          later on.  */
14654       if (cp_parser_skip_to_closing_brace (parser))
14655         cp_lexer_consume_token (parser->lexer);
14656       pop_deferring_access_checks ();
14657       return error_mark_node;
14658     }
14659
14660   /* Issue an error message if type-definitions are forbidden here.  */
14661   cp_parser_check_type_definition (parser);
14662   /* Remember that we are defining one more class.  */
14663   ++parser->num_classes_being_defined;
14664   /* Inside the class, surrounding template-parameter-lists do not
14665      apply.  */
14666   saved_num_template_parameter_lists
14667     = parser->num_template_parameter_lists;
14668   parser->num_template_parameter_lists = 0;
14669   /* We are not in a function body.  */
14670   saved_in_function_body = parser->in_function_body;
14671   parser->in_function_body = false;
14672
14673   /* Start the class.  */
14674   if (nested_name_specifier_p)
14675     {
14676       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14677       old_scope = push_inner_scope (scope);
14678     }
14679   type = begin_class_definition (type, attributes);
14680
14681   if (type == error_mark_node)
14682     /* If the type is erroneous, skip the entire body of the class.  */
14683     cp_parser_skip_to_closing_brace (parser);
14684   else
14685     /* Parse the member-specification.  */
14686     cp_parser_member_specification_opt (parser);
14687
14688   /* Look for the trailing `}'.  */
14689   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14690   /* We get better error messages by noticing a common problem: a
14691      missing trailing `;'.  */
14692   token = cp_lexer_peek_token (parser->lexer);
14693   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14694   /* Look for trailing attributes to apply to this class.  */
14695   if (cp_parser_allow_gnu_extensions_p (parser))
14696     attributes = cp_parser_attributes_opt (parser);
14697   if (type != error_mark_node)
14698     type = finish_struct (type, attributes);
14699   if (nested_name_specifier_p)
14700     pop_inner_scope (old_scope, scope);
14701   /* If this class is not itself within the scope of another class,
14702      then we need to parse the bodies of all of the queued function
14703      definitions.  Note that the queued functions defined in a class
14704      are not always processed immediately following the
14705      class-specifier for that class.  Consider:
14706
14707        struct A {
14708          struct B { void f() { sizeof (A); } };
14709        };
14710
14711      If `f' were processed before the processing of `A' were
14712      completed, there would be no way to compute the size of `A'.
14713      Note that the nesting we are interested in here is lexical --
14714      not the semantic nesting given by TYPE_CONTEXT.  In particular,
14715      for:
14716
14717        struct A { struct B; };
14718        struct A::B { void f() { } };
14719
14720      there is no need to delay the parsing of `A::B::f'.  */
14721   if (--parser->num_classes_being_defined == 0)
14722     {
14723       tree queue_entry;
14724       tree fn;
14725       tree class_type = NULL_TREE;
14726       tree pushed_scope = NULL_TREE;
14727
14728       /* In a first pass, parse default arguments to the functions.
14729          Then, in a second pass, parse the bodies of the functions.
14730          This two-phased approach handles cases like:
14731
14732             struct S {
14733               void f() { g(); }
14734               void g(int i = 3);
14735             };
14736
14737          */
14738       for (TREE_PURPOSE (parser->unparsed_functions_queues)
14739              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14740            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14741            TREE_PURPOSE (parser->unparsed_functions_queues)
14742              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14743         {
14744           fn = TREE_VALUE (queue_entry);
14745           /* If there are default arguments that have not yet been processed,
14746              take care of them now.  */
14747           if (class_type != TREE_PURPOSE (queue_entry))
14748             {
14749               if (pushed_scope)
14750                 pop_scope (pushed_scope);
14751               class_type = TREE_PURPOSE (queue_entry);
14752               pushed_scope = push_scope (class_type);
14753             }
14754           /* Make sure that any template parameters are in scope.  */
14755           maybe_begin_member_template_processing (fn);
14756           /* Parse the default argument expressions.  */
14757           cp_parser_late_parsing_default_args (parser, fn);
14758           /* Remove any template parameters from the symbol table.  */
14759           maybe_end_member_template_processing ();
14760         }
14761       if (pushed_scope)
14762         pop_scope (pushed_scope);
14763       /* Now parse the body of the functions.  */
14764       for (TREE_VALUE (parser->unparsed_functions_queues)
14765              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14766            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14767            TREE_VALUE (parser->unparsed_functions_queues)
14768              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14769         {
14770           /* Figure out which function we need to process.  */
14771           fn = TREE_VALUE (queue_entry);
14772           /* Parse the function.  */
14773           cp_parser_late_parsing_for_member (parser, fn);
14774         }
14775     }
14776
14777   /* Put back any saved access checks.  */
14778   pop_deferring_access_checks ();
14779
14780   /* Restore saved state.  */
14781   parser->in_function_body = saved_in_function_body;
14782   parser->num_template_parameter_lists
14783     = saved_num_template_parameter_lists;
14784
14785   return type;
14786 }
14787
14788 /* Parse a class-head.
14789
14790    class-head:
14791      class-key identifier [opt] base-clause [opt]
14792      class-key nested-name-specifier identifier base-clause [opt]
14793      class-key nested-name-specifier [opt] template-id
14794        base-clause [opt]
14795
14796    GNU Extensions:
14797      class-key attributes identifier [opt] base-clause [opt]
14798      class-key attributes nested-name-specifier identifier base-clause [opt]
14799      class-key attributes nested-name-specifier [opt] template-id
14800        base-clause [opt]
14801
14802    Upon return BASES is initialized to the list of base classes (or
14803    NULL, if there are none) in the same form returned by
14804    cp_parser_base_clause.
14805
14806    Returns the TYPE of the indicated class.  Sets
14807    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14808    involving a nested-name-specifier was used, and FALSE otherwise.
14809
14810    Returns error_mark_node if this is not a class-head.
14811
14812    Returns NULL_TREE if the class-head is syntactically valid, but
14813    semantically invalid in a way that means we should skip the entire
14814    body of the class.  */
14815
14816 static tree
14817 cp_parser_class_head (cp_parser* parser,
14818                       bool* nested_name_specifier_p,
14819                       tree *attributes_p,
14820                       tree *bases)
14821 {
14822   tree nested_name_specifier;
14823   enum tag_types class_key;
14824   tree id = NULL_TREE;
14825   tree type = NULL_TREE;
14826   tree attributes;
14827   bool template_id_p = false;
14828   bool qualified_p = false;
14829   bool invalid_nested_name_p = false;
14830   bool invalid_explicit_specialization_p = false;
14831   tree pushed_scope = NULL_TREE;
14832   unsigned num_templates;
14833   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
14834   /* Assume no nested-name-specifier will be present.  */
14835   *nested_name_specifier_p = false;
14836   /* Assume no template parameter lists will be used in defining the
14837      type.  */
14838   num_templates = 0;
14839
14840   *bases = NULL_TREE;
14841
14842   /* Look for the class-key.  */
14843   class_key = cp_parser_class_key (parser);
14844   if (class_key == none_type)
14845     return error_mark_node;
14846
14847   /* Parse the attributes.  */
14848   attributes = cp_parser_attributes_opt (parser);
14849
14850   /* If the next token is `::', that is invalid -- but sometimes
14851      people do try to write:
14852
14853        struct ::S {};
14854
14855      Handle this gracefully by accepting the extra qualifier, and then
14856      issuing an error about it later if this really is a
14857      class-head.  If it turns out just to be an elaborated type
14858      specifier, remain silent.  */
14859   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14860     qualified_p = true;
14861
14862   push_deferring_access_checks (dk_no_check);
14863
14864   /* Determine the name of the class.  Begin by looking for an
14865      optional nested-name-specifier.  */
14866   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
14867   nested_name_specifier
14868     = cp_parser_nested_name_specifier_opt (parser,
14869                                            /*typename_keyword_p=*/false,
14870                                            /*check_dependency_p=*/false,
14871                                            /*type_p=*/false,
14872                                            /*is_declaration=*/false);
14873   /* If there was a nested-name-specifier, then there *must* be an
14874      identifier.  */
14875   if (nested_name_specifier)
14876     {
14877       type_start_token = cp_lexer_peek_token (parser->lexer);
14878       /* Although the grammar says `identifier', it really means
14879          `class-name' or `template-name'.  You are only allowed to
14880          define a class that has already been declared with this
14881          syntax.
14882
14883          The proposed resolution for Core Issue 180 says that wherever
14884          you see `class T::X' you should treat `X' as a type-name.
14885
14886          It is OK to define an inaccessible class; for example:
14887
14888            class A { class B; };
14889            class A::B {};
14890
14891          We do not know if we will see a class-name, or a
14892          template-name.  We look for a class-name first, in case the
14893          class-name is a template-id; if we looked for the
14894          template-name first we would stop after the template-name.  */
14895       cp_parser_parse_tentatively (parser);
14896       type = cp_parser_class_name (parser,
14897                                    /*typename_keyword_p=*/false,
14898                                    /*template_keyword_p=*/false,
14899                                    class_type,
14900                                    /*check_dependency_p=*/false,
14901                                    /*class_head_p=*/true,
14902                                    /*is_declaration=*/false);
14903       /* If that didn't work, ignore the nested-name-specifier.  */
14904       if (!cp_parser_parse_definitely (parser))
14905         {
14906           invalid_nested_name_p = true;
14907           type_start_token = cp_lexer_peek_token (parser->lexer);
14908           id = cp_parser_identifier (parser);
14909           if (id == error_mark_node)
14910             id = NULL_TREE;
14911         }
14912       /* If we could not find a corresponding TYPE, treat this
14913          declaration like an unqualified declaration.  */
14914       if (type == error_mark_node)
14915         nested_name_specifier = NULL_TREE;
14916       /* Otherwise, count the number of templates used in TYPE and its
14917          containing scopes.  */
14918       else
14919         {
14920           tree scope;
14921
14922           for (scope = TREE_TYPE (type);
14923                scope && TREE_CODE (scope) != NAMESPACE_DECL;
14924                scope = (TYPE_P (scope)
14925                         ? TYPE_CONTEXT (scope)
14926                         : DECL_CONTEXT (scope)))
14927             if (TYPE_P (scope)
14928                 && CLASS_TYPE_P (scope)
14929                 && CLASSTYPE_TEMPLATE_INFO (scope)
14930                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14931                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14932               ++num_templates;
14933         }
14934     }
14935   /* Otherwise, the identifier is optional.  */
14936   else
14937     {
14938       /* We don't know whether what comes next is a template-id,
14939          an identifier, or nothing at all.  */
14940       cp_parser_parse_tentatively (parser);
14941       /* Check for a template-id.  */
14942       type_start_token = cp_lexer_peek_token (parser->lexer);
14943       id = cp_parser_template_id (parser,
14944                                   /*template_keyword_p=*/false,
14945                                   /*check_dependency_p=*/true,
14946                                   /*is_declaration=*/true);
14947       /* If that didn't work, it could still be an identifier.  */
14948       if (!cp_parser_parse_definitely (parser))
14949         {
14950           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14951             {
14952               type_start_token = cp_lexer_peek_token (parser->lexer);
14953               id = cp_parser_identifier (parser);
14954             }
14955           else
14956             id = NULL_TREE;
14957         }
14958       else
14959         {
14960           template_id_p = true;
14961           ++num_templates;
14962         }
14963     }
14964
14965   pop_deferring_access_checks ();
14966
14967   if (id)
14968     cp_parser_check_for_invalid_template_id (parser, id,
14969                                              type_start_token->location);
14970
14971   /* If it's not a `:' or a `{' then we can't really be looking at a
14972      class-head, since a class-head only appears as part of a
14973      class-specifier.  We have to detect this situation before calling
14974      xref_tag, since that has irreversible side-effects.  */
14975   if (!cp_parser_next_token_starts_class_definition_p (parser))
14976     {
14977       cp_parser_error (parser, "expected %<{%> or %<:%>");
14978       return error_mark_node;
14979     }
14980
14981   /* At this point, we're going ahead with the class-specifier, even
14982      if some other problem occurs.  */
14983   cp_parser_commit_to_tentative_parse (parser);
14984   /* Issue the error about the overly-qualified name now.  */
14985   if (qualified_p)
14986     {
14987       cp_parser_error (parser,
14988                        "global qualification of class name is invalid");
14989       return error_mark_node;
14990     }
14991   else if (invalid_nested_name_p)
14992     {
14993       cp_parser_error (parser,
14994                        "qualified name does not name a class");
14995       return error_mark_node;
14996     }
14997   else if (nested_name_specifier)
14998     {
14999       tree scope;
15000
15001       /* Reject typedef-names in class heads.  */
15002       if (!DECL_IMPLICIT_TYPEDEF_P (type))
15003         {
15004           error ("%Hinvalid class name in declaration of %qD",
15005                  &type_start_token->location, type);
15006           type = NULL_TREE;
15007           goto done;
15008         }
15009
15010       /* Figure out in what scope the declaration is being placed.  */
15011       scope = current_scope ();
15012       /* If that scope does not contain the scope in which the
15013          class was originally declared, the program is invalid.  */
15014       if (scope && !is_ancestor (scope, nested_name_specifier))
15015         {
15016           if (at_namespace_scope_p ())
15017             error ("%Hdeclaration of %qD in namespace %qD which does not "
15018                    "enclose %qD",
15019                    &type_start_token->location,
15020                    type, scope, nested_name_specifier);
15021           else
15022             error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15023                    &type_start_token->location,
15024                    type, scope, nested_name_specifier);
15025           type = NULL_TREE;
15026           goto done;
15027         }
15028       /* [dcl.meaning]
15029
15030          A declarator-id shall not be qualified except for the
15031          definition of a ... nested class outside of its class
15032          ... [or] the definition or explicit instantiation of a
15033          class member of a namespace outside of its namespace.  */
15034       if (scope == nested_name_specifier)
15035         {
15036           permerror (input_location, "%Hextra qualification not allowed",
15037                      &nested_name_specifier_token_start->location);
15038           nested_name_specifier = NULL_TREE;
15039           num_templates = 0;
15040         }
15041     }
15042   /* An explicit-specialization must be preceded by "template <>".  If
15043      it is not, try to recover gracefully.  */
15044   if (at_namespace_scope_p ()
15045       && parser->num_template_parameter_lists == 0
15046       && template_id_p)
15047     {
15048       error ("%Han explicit specialization must be preceded by %<template <>%>",
15049              &type_start_token->location);
15050       invalid_explicit_specialization_p = true;
15051       /* Take the same action that would have been taken by
15052          cp_parser_explicit_specialization.  */
15053       ++parser->num_template_parameter_lists;
15054       begin_specialization ();
15055     }
15056   /* There must be no "return" statements between this point and the
15057      end of this function; set "type "to the correct return value and
15058      use "goto done;" to return.  */
15059   /* Make sure that the right number of template parameters were
15060      present.  */
15061   if (!cp_parser_check_template_parameters (parser, num_templates,
15062                                             type_start_token->location))
15063     {
15064       /* If something went wrong, there is no point in even trying to
15065          process the class-definition.  */
15066       type = NULL_TREE;
15067       goto done;
15068     }
15069
15070   /* Look up the type.  */
15071   if (template_id_p)
15072     {
15073       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15074           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15075               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15076         {
15077           error ("%Hfunction template %qD redeclared as a class template",
15078                  &type_start_token->location, id);
15079           type = error_mark_node;
15080         }
15081       else
15082         {
15083           type = TREE_TYPE (id);
15084           type = maybe_process_partial_specialization (type);
15085         }
15086       if (nested_name_specifier)
15087         pushed_scope = push_scope (nested_name_specifier);
15088     }
15089   else if (nested_name_specifier)
15090     {
15091       tree class_type;
15092
15093       /* Given:
15094
15095             template <typename T> struct S { struct T };
15096             template <typename T> struct S<T>::T { };
15097
15098          we will get a TYPENAME_TYPE when processing the definition of
15099          `S::T'.  We need to resolve it to the actual type before we
15100          try to define it.  */
15101       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15102         {
15103           class_type = resolve_typename_type (TREE_TYPE (type),
15104                                               /*only_current_p=*/false);
15105           if (TREE_CODE (class_type) != TYPENAME_TYPE)
15106             type = TYPE_NAME (class_type);
15107           else
15108             {
15109               cp_parser_error (parser, "could not resolve typename type");
15110               type = error_mark_node;
15111             }
15112         }
15113
15114       if (maybe_process_partial_specialization (TREE_TYPE (type))
15115           == error_mark_node)
15116         {
15117           type = NULL_TREE;
15118           goto done;
15119         }
15120
15121       class_type = current_class_type;
15122       /* Enter the scope indicated by the nested-name-specifier.  */
15123       pushed_scope = push_scope (nested_name_specifier);
15124       /* Get the canonical version of this type.  */
15125       type = TYPE_MAIN_DECL (TREE_TYPE (type));
15126       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15127           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15128         {
15129           type = push_template_decl (type);
15130           if (type == error_mark_node)
15131             {
15132               type = NULL_TREE;
15133               goto done;
15134             }
15135         }
15136
15137       type = TREE_TYPE (type);
15138       *nested_name_specifier_p = true;
15139     }
15140   else      /* The name is not a nested name.  */
15141     {
15142       /* If the class was unnamed, create a dummy name.  */
15143       if (!id)
15144         id = make_anon_name ();
15145       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15146                        parser->num_template_parameter_lists);
15147     }
15148
15149   /* Indicate whether this class was declared as a `class' or as a
15150      `struct'.  */
15151   if (TREE_CODE (type) == RECORD_TYPE)
15152     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15153   cp_parser_check_class_key (class_key, type);
15154
15155   /* If this type was already complete, and we see another definition,
15156      that's an error.  */
15157   if (type != error_mark_node && COMPLETE_TYPE_P (type))
15158     {
15159       error ("%Hredefinition of %q#T",
15160              &type_start_token->location, type);
15161       error ("%Hprevious definition of %q+#T",
15162              &type_start_token->location, type);
15163       type = NULL_TREE;
15164       goto done;
15165     }
15166   else if (type == error_mark_node)
15167     type = NULL_TREE;
15168
15169   /* We will have entered the scope containing the class; the names of
15170      base classes should be looked up in that context.  For example:
15171
15172        struct A { struct B {}; struct C; };
15173        struct A::C : B {};
15174
15175      is valid.  */
15176
15177   /* Get the list of base-classes, if there is one.  */
15178   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15179     *bases = cp_parser_base_clause (parser);
15180
15181  done:
15182   /* Leave the scope given by the nested-name-specifier.  We will
15183      enter the class scope itself while processing the members.  */
15184   if (pushed_scope)
15185     pop_scope (pushed_scope);
15186
15187   if (invalid_explicit_specialization_p)
15188     {
15189       end_specialization ();
15190       --parser->num_template_parameter_lists;
15191     }
15192   *attributes_p = attributes;
15193   return type;
15194 }
15195
15196 /* Parse a class-key.
15197
15198    class-key:
15199      class
15200      struct
15201      union
15202
15203    Returns the kind of class-key specified, or none_type to indicate
15204    error.  */
15205
15206 static enum tag_types
15207 cp_parser_class_key (cp_parser* parser)
15208 {
15209   cp_token *token;
15210   enum tag_types tag_type;
15211
15212   /* Look for the class-key.  */
15213   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15214   if (!token)
15215     return none_type;
15216
15217   /* Check to see if the TOKEN is a class-key.  */
15218   tag_type = cp_parser_token_is_class_key (token);
15219   if (!tag_type)
15220     cp_parser_error (parser, "expected class-key");
15221   return tag_type;
15222 }
15223
15224 /* Parse an (optional) member-specification.
15225
15226    member-specification:
15227      member-declaration member-specification [opt]
15228      access-specifier : member-specification [opt]  */
15229
15230 static void
15231 cp_parser_member_specification_opt (cp_parser* parser)
15232 {
15233   while (true)
15234     {
15235       cp_token *token;
15236       enum rid keyword;
15237
15238       /* Peek at the next token.  */
15239       token = cp_lexer_peek_token (parser->lexer);
15240       /* If it's a `}', or EOF then we've seen all the members.  */
15241       if (token->type == CPP_CLOSE_BRACE
15242           || token->type == CPP_EOF
15243           || token->type == CPP_PRAGMA_EOL)
15244         break;
15245
15246       /* See if this token is a keyword.  */
15247       keyword = token->keyword;
15248       switch (keyword)
15249         {
15250         case RID_PUBLIC:
15251         case RID_PROTECTED:
15252         case RID_PRIVATE:
15253           /* Consume the access-specifier.  */
15254           cp_lexer_consume_token (parser->lexer);
15255           /* Remember which access-specifier is active.  */
15256           current_access_specifier = token->u.value;
15257           /* Look for the `:'.  */
15258           cp_parser_require (parser, CPP_COLON, "%<:%>");
15259           break;
15260
15261         default:
15262           /* Accept #pragmas at class scope.  */
15263           if (token->type == CPP_PRAGMA)
15264             {
15265               cp_parser_pragma (parser, pragma_external);
15266               break;
15267             }
15268
15269           /* Otherwise, the next construction must be a
15270              member-declaration.  */
15271           cp_parser_member_declaration (parser);
15272         }
15273     }
15274 }
15275
15276 /* Parse a member-declaration.
15277
15278    member-declaration:
15279      decl-specifier-seq [opt] member-declarator-list [opt] ;
15280      function-definition ; [opt]
15281      :: [opt] nested-name-specifier template [opt] unqualified-id ;
15282      using-declaration
15283      template-declaration
15284
15285    member-declarator-list:
15286      member-declarator
15287      member-declarator-list , member-declarator
15288
15289    member-declarator:
15290      declarator pure-specifier [opt]
15291      declarator constant-initializer [opt]
15292      identifier [opt] : constant-expression
15293
15294    GNU Extensions:
15295
15296    member-declaration:
15297      __extension__ member-declaration
15298
15299    member-declarator:
15300      declarator attributes [opt] pure-specifier [opt]
15301      declarator attributes [opt] constant-initializer [opt]
15302      identifier [opt] attributes [opt] : constant-expression  
15303
15304    C++0x Extensions:
15305
15306    member-declaration:
15307      static_assert-declaration  */
15308
15309 static void
15310 cp_parser_member_declaration (cp_parser* parser)
15311 {
15312   cp_decl_specifier_seq decl_specifiers;
15313   tree prefix_attributes;
15314   tree decl;
15315   int declares_class_or_enum;
15316   bool friend_p;
15317   cp_token *token = NULL;
15318   cp_token *decl_spec_token_start = NULL;
15319   cp_token *initializer_token_start = NULL;
15320   int saved_pedantic;
15321
15322   /* Check for the `__extension__' keyword.  */
15323   if (cp_parser_extension_opt (parser, &saved_pedantic))
15324     {
15325       /* Recurse.  */
15326       cp_parser_member_declaration (parser);
15327       /* Restore the old value of the PEDANTIC flag.  */
15328       pedantic = saved_pedantic;
15329
15330       return;
15331     }
15332
15333   /* Check for a template-declaration.  */
15334   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15335     {
15336       /* An explicit specialization here is an error condition, and we
15337          expect the specialization handler to detect and report this.  */
15338       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15339           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15340         cp_parser_explicit_specialization (parser);
15341       else
15342         cp_parser_template_declaration (parser, /*member_p=*/true);
15343
15344       return;
15345     }
15346
15347   /* Check for a using-declaration.  */
15348   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15349     {
15350       /* Parse the using-declaration.  */
15351       cp_parser_using_declaration (parser,
15352                                    /*access_declaration_p=*/false);
15353       return;
15354     }
15355
15356   /* Check for @defs.  */
15357   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15358     {
15359       tree ivar, member;
15360       tree ivar_chains = cp_parser_objc_defs_expression (parser);
15361       ivar = ivar_chains;
15362       while (ivar)
15363         {
15364           member = ivar;
15365           ivar = TREE_CHAIN (member);
15366           TREE_CHAIN (member) = NULL_TREE;
15367           finish_member_declaration (member);
15368         }
15369       return;
15370     }
15371
15372   /* If the next token is `static_assert' we have a static assertion.  */
15373   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15374     {
15375       cp_parser_static_assert (parser, /*member_p=*/true);
15376       return;
15377     }
15378
15379   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15380     return;
15381
15382   /* Parse the decl-specifier-seq.  */
15383   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15384   cp_parser_decl_specifier_seq (parser,
15385                                 CP_PARSER_FLAGS_OPTIONAL,
15386                                 &decl_specifiers,
15387                                 &declares_class_or_enum);
15388   prefix_attributes = decl_specifiers.attributes;
15389   decl_specifiers.attributes = NULL_TREE;
15390   /* Check for an invalid type-name.  */
15391   if (!decl_specifiers.type
15392       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15393     return;
15394   /* If there is no declarator, then the decl-specifier-seq should
15395      specify a type.  */
15396   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15397     {
15398       /* If there was no decl-specifier-seq, and the next token is a
15399          `;', then we have something like:
15400
15401            struct S { ; };
15402
15403          [class.mem]
15404
15405          Each member-declaration shall declare at least one member
15406          name of the class.  */
15407       if (!decl_specifiers.any_specifiers_p)
15408         {
15409           cp_token *token = cp_lexer_peek_token (parser->lexer);
15410           if (!in_system_header_at (token->location))
15411             pedwarn (OPT_pedantic, "%Hextra %<;%>", &token->location);
15412         }
15413       else
15414         {
15415           tree type;
15416
15417           /* See if this declaration is a friend.  */
15418           friend_p = cp_parser_friend_p (&decl_specifiers);
15419           /* If there were decl-specifiers, check to see if there was
15420              a class-declaration.  */
15421           type = check_tag_decl (&decl_specifiers);
15422           /* Nested classes have already been added to the class, but
15423              a `friend' needs to be explicitly registered.  */
15424           if (friend_p)
15425             {
15426               /* If the `friend' keyword was present, the friend must
15427                  be introduced with a class-key.  */
15428                if (!declares_class_or_enum)
15429                  error ("%Ha class-key must be used when declaring a friend",
15430                         &decl_spec_token_start->location);
15431                /* In this case:
15432
15433                     template <typename T> struct A {
15434                       friend struct A<T>::B;
15435                     };
15436
15437                   A<T>::B will be represented by a TYPENAME_TYPE, and
15438                   therefore not recognized by check_tag_decl.  */
15439                if (!type
15440                    && decl_specifiers.type
15441                    && TYPE_P (decl_specifiers.type))
15442                  type = decl_specifiers.type;
15443                if (!type || !TYPE_P (type))
15444                  error ("%Hfriend declaration does not name a class or "
15445                         "function", &decl_spec_token_start->location);
15446                else
15447                  make_friend_class (current_class_type, type,
15448                                     /*complain=*/true);
15449             }
15450           /* If there is no TYPE, an error message will already have
15451              been issued.  */
15452           else if (!type || type == error_mark_node)
15453             ;
15454           /* An anonymous aggregate has to be handled specially; such
15455              a declaration really declares a data member (with a
15456              particular type), as opposed to a nested class.  */
15457           else if (ANON_AGGR_TYPE_P (type))
15458             {
15459               /* Remove constructors and such from TYPE, now that we
15460                  know it is an anonymous aggregate.  */
15461               fixup_anonymous_aggr (type);
15462               /* And make the corresponding data member.  */
15463               decl = build_decl (FIELD_DECL, NULL_TREE, type);
15464               /* Add it to the class.  */
15465               finish_member_declaration (decl);
15466             }
15467           else
15468             cp_parser_check_access_in_redeclaration
15469                                               (TYPE_NAME (type),
15470                                                decl_spec_token_start->location);
15471         }
15472     }
15473   else
15474     {
15475       /* See if these declarations will be friends.  */
15476       friend_p = cp_parser_friend_p (&decl_specifiers);
15477
15478       /* Keep going until we hit the `;' at the end of the
15479          declaration.  */
15480       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15481         {
15482           tree attributes = NULL_TREE;
15483           tree first_attribute;
15484
15485           /* Peek at the next token.  */
15486           token = cp_lexer_peek_token (parser->lexer);
15487
15488           /* Check for a bitfield declaration.  */
15489           if (token->type == CPP_COLON
15490               || (token->type == CPP_NAME
15491                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15492                   == CPP_COLON))
15493             {
15494               tree identifier;
15495               tree width;
15496
15497               /* Get the name of the bitfield.  Note that we cannot just
15498                  check TOKEN here because it may have been invalidated by
15499                  the call to cp_lexer_peek_nth_token above.  */
15500               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15501                 identifier = cp_parser_identifier (parser);
15502               else
15503                 identifier = NULL_TREE;
15504
15505               /* Consume the `:' token.  */
15506               cp_lexer_consume_token (parser->lexer);
15507               /* Get the width of the bitfield.  */
15508               width
15509                 = cp_parser_constant_expression (parser,
15510                                                  /*allow_non_constant=*/false,
15511                                                  NULL);
15512
15513               /* Look for attributes that apply to the bitfield.  */
15514               attributes = cp_parser_attributes_opt (parser);
15515               /* Remember which attributes are prefix attributes and
15516                  which are not.  */
15517               first_attribute = attributes;
15518               /* Combine the attributes.  */
15519               attributes = chainon (prefix_attributes, attributes);
15520
15521               /* Create the bitfield declaration.  */
15522               decl = grokbitfield (identifier
15523                                    ? make_id_declarator (NULL_TREE,
15524                                                          identifier,
15525                                                          sfk_none)
15526                                    : NULL,
15527                                    &decl_specifiers,
15528                                    width,
15529                                    attributes);
15530             }
15531           else
15532             {
15533               cp_declarator *declarator;
15534               tree initializer;
15535               tree asm_specification;
15536               int ctor_dtor_or_conv_p;
15537
15538               /* Parse the declarator.  */
15539               declarator
15540                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15541                                         &ctor_dtor_or_conv_p,
15542                                         /*parenthesized_p=*/NULL,
15543                                         /*member_p=*/true);
15544
15545               /* If something went wrong parsing the declarator, make sure
15546                  that we at least consume some tokens.  */
15547               if (declarator == cp_error_declarator)
15548                 {
15549                   /* Skip to the end of the statement.  */
15550                   cp_parser_skip_to_end_of_statement (parser);
15551                   /* If the next token is not a semicolon, that is
15552                      probably because we just skipped over the body of
15553                      a function.  So, we consume a semicolon if
15554                      present, but do not issue an error message if it
15555                      is not present.  */
15556                   if (cp_lexer_next_token_is (parser->lexer,
15557                                               CPP_SEMICOLON))
15558                     cp_lexer_consume_token (parser->lexer);
15559                   return;
15560                 }
15561
15562               if (declares_class_or_enum & 2)
15563                 cp_parser_check_for_definition_in_return_type
15564                                             (declarator, decl_specifiers.type,
15565                                              decl_specifiers.type_location);
15566
15567               /* Look for an asm-specification.  */
15568               asm_specification = cp_parser_asm_specification_opt (parser);
15569               /* Look for attributes that apply to the declaration.  */
15570               attributes = cp_parser_attributes_opt (parser);
15571               /* Remember which attributes are prefix attributes and
15572                  which are not.  */
15573               first_attribute = attributes;
15574               /* Combine the attributes.  */
15575               attributes = chainon (prefix_attributes, attributes);
15576
15577               /* If it's an `=', then we have a constant-initializer or a
15578                  pure-specifier.  It is not correct to parse the
15579                  initializer before registering the member declaration
15580                  since the member declaration should be in scope while
15581                  its initializer is processed.  However, the rest of the
15582                  front end does not yet provide an interface that allows
15583                  us to handle this correctly.  */
15584               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15585                 {
15586                   /* In [class.mem]:
15587
15588                      A pure-specifier shall be used only in the declaration of
15589                      a virtual function.
15590
15591                      A member-declarator can contain a constant-initializer
15592                      only if it declares a static member of integral or
15593                      enumeration type.
15594
15595                      Therefore, if the DECLARATOR is for a function, we look
15596                      for a pure-specifier; otherwise, we look for a
15597                      constant-initializer.  When we call `grokfield', it will
15598                      perform more stringent semantics checks.  */
15599                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
15600                   if (function_declarator_p (declarator))
15601                     initializer = cp_parser_pure_specifier (parser);
15602                   else
15603                     /* Parse the initializer.  */
15604                     initializer = cp_parser_constant_initializer (parser);
15605                 }
15606               /* Otherwise, there is no initializer.  */
15607               else
15608                 initializer = NULL_TREE;
15609
15610               /* See if we are probably looking at a function
15611                  definition.  We are certainly not looking at a
15612                  member-declarator.  Calling `grokfield' has
15613                  side-effects, so we must not do it unless we are sure
15614                  that we are looking at a member-declarator.  */
15615               if (cp_parser_token_starts_function_definition_p
15616                   (cp_lexer_peek_token (parser->lexer)))
15617                 {
15618                   /* The grammar does not allow a pure-specifier to be
15619                      used when a member function is defined.  (It is
15620                      possible that this fact is an oversight in the
15621                      standard, since a pure function may be defined
15622                      outside of the class-specifier.  */
15623                   if (initializer)
15624                     error ("%Hpure-specifier on function-definition",
15625                            &initializer_token_start->location);
15626                   decl = cp_parser_save_member_function_body (parser,
15627                                                               &decl_specifiers,
15628                                                               declarator,
15629                                                               attributes);
15630                   /* If the member was not a friend, declare it here.  */
15631                   if (!friend_p)
15632                     finish_member_declaration (decl);
15633                   /* Peek at the next token.  */
15634                   token = cp_lexer_peek_token (parser->lexer);
15635                   /* If the next token is a semicolon, consume it.  */
15636                   if (token->type == CPP_SEMICOLON)
15637                     cp_lexer_consume_token (parser->lexer);
15638                   return;
15639                 }
15640               else
15641                 /* Create the declaration.  */
15642                 decl = grokfield (declarator, &decl_specifiers,
15643                                   initializer, /*init_const_expr_p=*/true,
15644                                   asm_specification,
15645                                   attributes);
15646             }
15647
15648           /* Reset PREFIX_ATTRIBUTES.  */
15649           while (attributes && TREE_CHAIN (attributes) != first_attribute)
15650             attributes = TREE_CHAIN (attributes);
15651           if (attributes)
15652             TREE_CHAIN (attributes) = NULL_TREE;
15653
15654           /* If there is any qualification still in effect, clear it
15655              now; we will be starting fresh with the next declarator.  */
15656           parser->scope = NULL_TREE;
15657           parser->qualifying_scope = NULL_TREE;
15658           parser->object_scope = NULL_TREE;
15659           /* If it's a `,', then there are more declarators.  */
15660           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15661             cp_lexer_consume_token (parser->lexer);
15662           /* If the next token isn't a `;', then we have a parse error.  */
15663           else if (cp_lexer_next_token_is_not (parser->lexer,
15664                                                CPP_SEMICOLON))
15665             {
15666               cp_parser_error (parser, "expected %<;%>");
15667               /* Skip tokens until we find a `;'.  */
15668               cp_parser_skip_to_end_of_statement (parser);
15669
15670               break;
15671             }
15672
15673           if (decl)
15674             {
15675               /* Add DECL to the list of members.  */
15676               if (!friend_p)
15677                 finish_member_declaration (decl);
15678
15679               if (TREE_CODE (decl) == FUNCTION_DECL)
15680                 cp_parser_save_default_args (parser, decl);
15681             }
15682         }
15683     }
15684
15685   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
15686 }
15687
15688 /* Parse a pure-specifier.
15689
15690    pure-specifier:
15691      = 0
15692
15693    Returns INTEGER_ZERO_NODE if a pure specifier is found.
15694    Otherwise, ERROR_MARK_NODE is returned.  */
15695
15696 static tree
15697 cp_parser_pure_specifier (cp_parser* parser)
15698 {
15699   cp_token *token;
15700
15701   /* Look for the `=' token.  */
15702   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15703     return error_mark_node;
15704   /* Look for the `0' token.  */
15705   token = cp_lexer_consume_token (parser->lexer);
15706
15707   /* Accept = default or = delete in c++0x mode.  */
15708   if (token->keyword == RID_DEFAULT
15709       || token->keyword == RID_DELETE)
15710     {
15711       maybe_warn_cpp0x ("defaulted and deleted functions");
15712       return token->u.value;
15713     }
15714
15715   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
15716   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15717     {
15718       cp_parser_error (parser,
15719                        "invalid pure specifier (only %<= 0%> is allowed)");
15720       cp_parser_skip_to_end_of_statement (parser);
15721       return error_mark_node;
15722     }
15723   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15724     {
15725       error ("%Htemplates may not be %<virtual%>", &token->location);
15726       return error_mark_node;
15727     }
15728
15729   return integer_zero_node;
15730 }
15731
15732 /* Parse a constant-initializer.
15733
15734    constant-initializer:
15735      = constant-expression
15736
15737    Returns a representation of the constant-expression.  */
15738
15739 static tree
15740 cp_parser_constant_initializer (cp_parser* parser)
15741 {
15742   /* Look for the `=' token.  */
15743   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
15744     return error_mark_node;
15745
15746   /* It is invalid to write:
15747
15748        struct S { static const int i = { 7 }; };
15749
15750      */
15751   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15752     {
15753       cp_parser_error (parser,
15754                        "a brace-enclosed initializer is not allowed here");
15755       /* Consume the opening brace.  */
15756       cp_lexer_consume_token (parser->lexer);
15757       /* Skip the initializer.  */
15758       cp_parser_skip_to_closing_brace (parser);
15759       /* Look for the trailing `}'.  */
15760       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15761
15762       return error_mark_node;
15763     }
15764
15765   return cp_parser_constant_expression (parser,
15766                                         /*allow_non_constant=*/false,
15767                                         NULL);
15768 }
15769
15770 /* Derived classes [gram.class.derived] */
15771
15772 /* Parse a base-clause.
15773
15774    base-clause:
15775      : base-specifier-list
15776
15777    base-specifier-list:
15778      base-specifier ... [opt]
15779      base-specifier-list , base-specifier ... [opt]
15780
15781    Returns a TREE_LIST representing the base-classes, in the order in
15782    which they were declared.  The representation of each node is as
15783    described by cp_parser_base_specifier.
15784
15785    In the case that no bases are specified, this function will return
15786    NULL_TREE, not ERROR_MARK_NODE.  */
15787
15788 static tree
15789 cp_parser_base_clause (cp_parser* parser)
15790 {
15791   tree bases = NULL_TREE;
15792
15793   /* Look for the `:' that begins the list.  */
15794   cp_parser_require (parser, CPP_COLON, "%<:%>");
15795
15796   /* Scan the base-specifier-list.  */
15797   while (true)
15798     {
15799       cp_token *token;
15800       tree base;
15801       bool pack_expansion_p = false;
15802
15803       /* Look for the base-specifier.  */
15804       base = cp_parser_base_specifier (parser);
15805       /* Look for the (optional) ellipsis. */
15806       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15807         {
15808           /* Consume the `...'. */
15809           cp_lexer_consume_token (parser->lexer);
15810
15811           pack_expansion_p = true;
15812         }
15813
15814       /* Add BASE to the front of the list.  */
15815       if (base != error_mark_node)
15816         {
15817           if (pack_expansion_p)
15818             /* Make this a pack expansion type. */
15819             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15820           
15821
15822           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15823             {
15824               TREE_CHAIN (base) = bases;
15825               bases = base;
15826             }
15827         }
15828       /* Peek at the next token.  */
15829       token = cp_lexer_peek_token (parser->lexer);
15830       /* If it's not a comma, then the list is complete.  */
15831       if (token->type != CPP_COMMA)
15832         break;
15833       /* Consume the `,'.  */
15834       cp_lexer_consume_token (parser->lexer);
15835     }
15836
15837   /* PARSER->SCOPE may still be non-NULL at this point, if the last
15838      base class had a qualified name.  However, the next name that
15839      appears is certainly not qualified.  */
15840   parser->scope = NULL_TREE;
15841   parser->qualifying_scope = NULL_TREE;
15842   parser->object_scope = NULL_TREE;
15843
15844   return nreverse (bases);
15845 }
15846
15847 /* Parse a base-specifier.
15848
15849    base-specifier:
15850      :: [opt] nested-name-specifier [opt] class-name
15851      virtual access-specifier [opt] :: [opt] nested-name-specifier
15852        [opt] class-name
15853      access-specifier virtual [opt] :: [opt] nested-name-specifier
15854        [opt] class-name
15855
15856    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
15857    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15858    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
15859    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
15860
15861 static tree
15862 cp_parser_base_specifier (cp_parser* parser)
15863 {
15864   cp_token *token;
15865   bool done = false;
15866   bool virtual_p = false;
15867   bool duplicate_virtual_error_issued_p = false;
15868   bool duplicate_access_error_issued_p = false;
15869   bool class_scope_p, template_p;
15870   tree access = access_default_node;
15871   tree type;
15872
15873   /* Process the optional `virtual' and `access-specifier'.  */
15874   while (!done)
15875     {
15876       /* Peek at the next token.  */
15877       token = cp_lexer_peek_token (parser->lexer);
15878       /* Process `virtual'.  */
15879       switch (token->keyword)
15880         {
15881         case RID_VIRTUAL:
15882           /* If `virtual' appears more than once, issue an error.  */
15883           if (virtual_p && !duplicate_virtual_error_issued_p)
15884             {
15885               cp_parser_error (parser,
15886                                "%<virtual%> specified more than once in base-specified");
15887               duplicate_virtual_error_issued_p = true;
15888             }
15889
15890           virtual_p = true;
15891
15892           /* Consume the `virtual' token.  */
15893           cp_lexer_consume_token (parser->lexer);
15894
15895           break;
15896
15897         case RID_PUBLIC:
15898         case RID_PROTECTED:
15899         case RID_PRIVATE:
15900           /* If more than one access specifier appears, issue an
15901              error.  */
15902           if (access != access_default_node
15903               && !duplicate_access_error_issued_p)
15904             {
15905               cp_parser_error (parser,
15906                                "more than one access specifier in base-specified");
15907               duplicate_access_error_issued_p = true;
15908             }
15909
15910           access = ridpointers[(int) token->keyword];
15911
15912           /* Consume the access-specifier.  */
15913           cp_lexer_consume_token (parser->lexer);
15914
15915           break;
15916
15917         default:
15918           done = true;
15919           break;
15920         }
15921     }
15922   /* It is not uncommon to see programs mechanically, erroneously, use
15923      the 'typename' keyword to denote (dependent) qualified types
15924      as base classes.  */
15925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15926     {
15927       token = cp_lexer_peek_token (parser->lexer);
15928       if (!processing_template_decl)
15929         error ("%Hkeyword %<typename%> not allowed outside of templates",
15930                &token->location);
15931       else
15932         error ("%Hkeyword %<typename%> not allowed in this context "
15933                "(the base class is implicitly a type)",
15934                &token->location);
15935       cp_lexer_consume_token (parser->lexer);
15936     }
15937
15938   /* Look for the optional `::' operator.  */
15939   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15940   /* Look for the nested-name-specifier.  The simplest way to
15941      implement:
15942
15943        [temp.res]
15944
15945        The keyword `typename' is not permitted in a base-specifier or
15946        mem-initializer; in these contexts a qualified name that
15947        depends on a template-parameter is implicitly assumed to be a
15948        type name.
15949
15950      is to pretend that we have seen the `typename' keyword at this
15951      point.  */
15952   cp_parser_nested_name_specifier_opt (parser,
15953                                        /*typename_keyword_p=*/true,
15954                                        /*check_dependency_p=*/true,
15955                                        typename_type,
15956                                        /*is_declaration=*/true);
15957   /* If the base class is given by a qualified name, assume that names
15958      we see are type names or templates, as appropriate.  */
15959   class_scope_p = (parser->scope && TYPE_P (parser->scope));
15960   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15961
15962   /* Finally, look for the class-name.  */
15963   type = cp_parser_class_name (parser,
15964                                class_scope_p,
15965                                template_p,
15966                                typename_type,
15967                                /*check_dependency_p=*/true,
15968                                /*class_head_p=*/false,
15969                                /*is_declaration=*/true);
15970
15971   if (type == error_mark_node)
15972     return error_mark_node;
15973
15974   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15975 }
15976
15977 /* Exception handling [gram.exception] */
15978
15979 /* Parse an (optional) exception-specification.
15980
15981    exception-specification:
15982      throw ( type-id-list [opt] )
15983
15984    Returns a TREE_LIST representing the exception-specification.  The
15985    TREE_VALUE of each node is a type.  */
15986
15987 static tree
15988 cp_parser_exception_specification_opt (cp_parser* parser)
15989 {
15990   cp_token *token;
15991   tree type_id_list;
15992
15993   /* Peek at the next token.  */
15994   token = cp_lexer_peek_token (parser->lexer);
15995   /* If it's not `throw', then there's no exception-specification.  */
15996   if (!cp_parser_is_keyword (token, RID_THROW))
15997     return NULL_TREE;
15998
15999   /* Consume the `throw'.  */
16000   cp_lexer_consume_token (parser->lexer);
16001
16002   /* Look for the `('.  */
16003   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16004
16005   /* Peek at the next token.  */
16006   token = cp_lexer_peek_token (parser->lexer);
16007   /* If it's not a `)', then there is a type-id-list.  */
16008   if (token->type != CPP_CLOSE_PAREN)
16009     {
16010       const char *saved_message;
16011
16012       /* Types may not be defined in an exception-specification.  */
16013       saved_message = parser->type_definition_forbidden_message;
16014       parser->type_definition_forbidden_message
16015         = "types may not be defined in an exception-specification";
16016       /* Parse the type-id-list.  */
16017       type_id_list = cp_parser_type_id_list (parser);
16018       /* Restore the saved message.  */
16019       parser->type_definition_forbidden_message = saved_message;
16020     }
16021   else
16022     type_id_list = empty_except_spec;
16023
16024   /* Look for the `)'.  */
16025   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16026
16027   return type_id_list;
16028 }
16029
16030 /* Parse an (optional) type-id-list.
16031
16032    type-id-list:
16033      type-id ... [opt]
16034      type-id-list , type-id ... [opt]
16035
16036    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
16037    in the order that the types were presented.  */
16038
16039 static tree
16040 cp_parser_type_id_list (cp_parser* parser)
16041 {
16042   tree types = NULL_TREE;
16043
16044   while (true)
16045     {
16046       cp_token *token;
16047       tree type;
16048
16049       /* Get the next type-id.  */
16050       type = cp_parser_type_id (parser);
16051       /* Parse the optional ellipsis. */
16052       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16053         {
16054           /* Consume the `...'. */
16055           cp_lexer_consume_token (parser->lexer);
16056
16057           /* Turn the type into a pack expansion expression. */
16058           type = make_pack_expansion (type);
16059         }
16060       /* Add it to the list.  */
16061       types = add_exception_specifier (types, type, /*complain=*/1);
16062       /* Peek at the next token.  */
16063       token = cp_lexer_peek_token (parser->lexer);
16064       /* If it is not a `,', we are done.  */
16065       if (token->type != CPP_COMMA)
16066         break;
16067       /* Consume the `,'.  */
16068       cp_lexer_consume_token (parser->lexer);
16069     }
16070
16071   return nreverse (types);
16072 }
16073
16074 /* Parse a try-block.
16075
16076    try-block:
16077      try compound-statement handler-seq  */
16078
16079 static tree
16080 cp_parser_try_block (cp_parser* parser)
16081 {
16082   tree try_block;
16083
16084   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16085   try_block = begin_try_block ();
16086   cp_parser_compound_statement (parser, NULL, true);
16087   finish_try_block (try_block);
16088   cp_parser_handler_seq (parser);
16089   finish_handler_sequence (try_block);
16090
16091   return try_block;
16092 }
16093
16094 /* Parse a function-try-block.
16095
16096    function-try-block:
16097      try ctor-initializer [opt] function-body handler-seq  */
16098
16099 static bool
16100 cp_parser_function_try_block (cp_parser* parser)
16101 {
16102   tree compound_stmt;
16103   tree try_block;
16104   bool ctor_initializer_p;
16105
16106   /* Look for the `try' keyword.  */
16107   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16108     return false;
16109   /* Let the rest of the front end know where we are.  */
16110   try_block = begin_function_try_block (&compound_stmt);
16111   /* Parse the function-body.  */
16112   ctor_initializer_p
16113     = cp_parser_ctor_initializer_opt_and_function_body (parser);
16114   /* We're done with the `try' part.  */
16115   finish_function_try_block (try_block);
16116   /* Parse the handlers.  */
16117   cp_parser_handler_seq (parser);
16118   /* We're done with the handlers.  */
16119   finish_function_handler_sequence (try_block, compound_stmt);
16120
16121   return ctor_initializer_p;
16122 }
16123
16124 /* Parse a handler-seq.
16125
16126    handler-seq:
16127      handler handler-seq [opt]  */
16128
16129 static void
16130 cp_parser_handler_seq (cp_parser* parser)
16131 {
16132   while (true)
16133     {
16134       cp_token *token;
16135
16136       /* Parse the handler.  */
16137       cp_parser_handler (parser);
16138       /* Peek at the next token.  */
16139       token = cp_lexer_peek_token (parser->lexer);
16140       /* If it's not `catch' then there are no more handlers.  */
16141       if (!cp_parser_is_keyword (token, RID_CATCH))
16142         break;
16143     }
16144 }
16145
16146 /* Parse a handler.
16147
16148    handler:
16149      catch ( exception-declaration ) compound-statement  */
16150
16151 static void
16152 cp_parser_handler (cp_parser* parser)
16153 {
16154   tree handler;
16155   tree declaration;
16156
16157   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16158   handler = begin_handler ();
16159   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16160   declaration = cp_parser_exception_declaration (parser);
16161   finish_handler_parms (declaration, handler);
16162   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16163   cp_parser_compound_statement (parser, NULL, false);
16164   finish_handler (handler);
16165 }
16166
16167 /* Parse an exception-declaration.
16168
16169    exception-declaration:
16170      type-specifier-seq declarator
16171      type-specifier-seq abstract-declarator
16172      type-specifier-seq
16173      ...
16174
16175    Returns a VAR_DECL for the declaration, or NULL_TREE if the
16176    ellipsis variant is used.  */
16177
16178 static tree
16179 cp_parser_exception_declaration (cp_parser* parser)
16180 {
16181   cp_decl_specifier_seq type_specifiers;
16182   cp_declarator *declarator;
16183   const char *saved_message;
16184
16185   /* If it's an ellipsis, it's easy to handle.  */
16186   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16187     {
16188       /* Consume the `...' token.  */
16189       cp_lexer_consume_token (parser->lexer);
16190       return NULL_TREE;
16191     }
16192
16193   /* Types may not be defined in exception-declarations.  */
16194   saved_message = parser->type_definition_forbidden_message;
16195   parser->type_definition_forbidden_message
16196     = "types may not be defined in exception-declarations";
16197
16198   /* Parse the type-specifier-seq.  */
16199   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16200                                 &type_specifiers);
16201   /* If it's a `)', then there is no declarator.  */
16202   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16203     declarator = NULL;
16204   else
16205     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16206                                        /*ctor_dtor_or_conv_p=*/NULL,
16207                                        /*parenthesized_p=*/NULL,
16208                                        /*member_p=*/false);
16209
16210   /* Restore the saved message.  */
16211   parser->type_definition_forbidden_message = saved_message;
16212
16213   if (!type_specifiers.any_specifiers_p)
16214     return error_mark_node;
16215
16216   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16217 }
16218
16219 /* Parse a throw-expression.
16220
16221    throw-expression:
16222      throw assignment-expression [opt]
16223
16224    Returns a THROW_EXPR representing the throw-expression.  */
16225
16226 static tree
16227 cp_parser_throw_expression (cp_parser* parser)
16228 {
16229   tree expression;
16230   cp_token* token;
16231
16232   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16233   token = cp_lexer_peek_token (parser->lexer);
16234   /* Figure out whether or not there is an assignment-expression
16235      following the "throw" keyword.  */
16236   if (token->type == CPP_COMMA
16237       || token->type == CPP_SEMICOLON
16238       || token->type == CPP_CLOSE_PAREN
16239       || token->type == CPP_CLOSE_SQUARE
16240       || token->type == CPP_CLOSE_BRACE
16241       || token->type == CPP_COLON)
16242     expression = NULL_TREE;
16243   else
16244     expression = cp_parser_assignment_expression (parser,
16245                                                   /*cast_p=*/false);
16246
16247   return build_throw (expression);
16248 }
16249
16250 /* GNU Extensions */
16251
16252 /* Parse an (optional) asm-specification.
16253
16254    asm-specification:
16255      asm ( string-literal )
16256
16257    If the asm-specification is present, returns a STRING_CST
16258    corresponding to the string-literal.  Otherwise, returns
16259    NULL_TREE.  */
16260
16261 static tree
16262 cp_parser_asm_specification_opt (cp_parser* parser)
16263 {
16264   cp_token *token;
16265   tree asm_specification;
16266
16267   /* Peek at the next token.  */
16268   token = cp_lexer_peek_token (parser->lexer);
16269   /* If the next token isn't the `asm' keyword, then there's no
16270      asm-specification.  */
16271   if (!cp_parser_is_keyword (token, RID_ASM))
16272     return NULL_TREE;
16273
16274   /* Consume the `asm' token.  */
16275   cp_lexer_consume_token (parser->lexer);
16276   /* Look for the `('.  */
16277   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16278
16279   /* Look for the string-literal.  */
16280   asm_specification = cp_parser_string_literal (parser, false, false);
16281
16282   /* Look for the `)'.  */
16283   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16284
16285   return asm_specification;
16286 }
16287
16288 /* Parse an asm-operand-list.
16289
16290    asm-operand-list:
16291      asm-operand
16292      asm-operand-list , asm-operand
16293
16294    asm-operand:
16295      string-literal ( expression )
16296      [ string-literal ] string-literal ( expression )
16297
16298    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
16299    each node is the expression.  The TREE_PURPOSE is itself a
16300    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16301    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16302    is a STRING_CST for the string literal before the parenthesis. Returns
16303    ERROR_MARK_NODE if any of the operands are invalid.  */
16304
16305 static tree
16306 cp_parser_asm_operand_list (cp_parser* parser)
16307 {
16308   tree asm_operands = NULL_TREE;
16309   bool invalid_operands = false;
16310
16311   while (true)
16312     {
16313       tree string_literal;
16314       tree expression;
16315       tree name;
16316
16317       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16318         {
16319           /* Consume the `[' token.  */
16320           cp_lexer_consume_token (parser->lexer);
16321           /* Read the operand name.  */
16322           name = cp_parser_identifier (parser);
16323           if (name != error_mark_node)
16324             name = build_string (IDENTIFIER_LENGTH (name),
16325                                  IDENTIFIER_POINTER (name));
16326           /* Look for the closing `]'.  */
16327           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16328         }
16329       else
16330         name = NULL_TREE;
16331       /* Look for the string-literal.  */
16332       string_literal = cp_parser_string_literal (parser, false, false);
16333
16334       /* Look for the `('.  */
16335       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16336       /* Parse the expression.  */
16337       expression = cp_parser_expression (parser, /*cast_p=*/false);
16338       /* Look for the `)'.  */
16339       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16340
16341       if (name == error_mark_node 
16342           || string_literal == error_mark_node 
16343           || expression == error_mark_node)
16344         invalid_operands = true;
16345
16346       /* Add this operand to the list.  */
16347       asm_operands = tree_cons (build_tree_list (name, string_literal),
16348                                 expression,
16349                                 asm_operands);
16350       /* If the next token is not a `,', there are no more
16351          operands.  */
16352       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16353         break;
16354       /* Consume the `,'.  */
16355       cp_lexer_consume_token (parser->lexer);
16356     }
16357
16358   return invalid_operands ? error_mark_node : nreverse (asm_operands);
16359 }
16360
16361 /* Parse an asm-clobber-list.
16362
16363    asm-clobber-list:
16364      string-literal
16365      asm-clobber-list , string-literal
16366
16367    Returns a TREE_LIST, indicating the clobbers in the order that they
16368    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
16369
16370 static tree
16371 cp_parser_asm_clobber_list (cp_parser* parser)
16372 {
16373   tree clobbers = NULL_TREE;
16374
16375   while (true)
16376     {
16377       tree string_literal;
16378
16379       /* Look for the string literal.  */
16380       string_literal = cp_parser_string_literal (parser, false, false);
16381       /* Add it to the list.  */
16382       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16383       /* If the next token is not a `,', then the list is
16384          complete.  */
16385       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16386         break;
16387       /* Consume the `,' token.  */
16388       cp_lexer_consume_token (parser->lexer);
16389     }
16390
16391   return clobbers;
16392 }
16393
16394 /* Parse an (optional) series of attributes.
16395
16396    attributes:
16397      attributes attribute
16398
16399    attribute:
16400      __attribute__ (( attribute-list [opt] ))
16401
16402    The return value is as for cp_parser_attribute_list.  */
16403
16404 static tree
16405 cp_parser_attributes_opt (cp_parser* parser)
16406 {
16407   tree attributes = NULL_TREE;
16408
16409   while (true)
16410     {
16411       cp_token *token;
16412       tree attribute_list;
16413
16414       /* Peek at the next token.  */
16415       token = cp_lexer_peek_token (parser->lexer);
16416       /* If it's not `__attribute__', then we're done.  */
16417       if (token->keyword != RID_ATTRIBUTE)
16418         break;
16419
16420       /* Consume the `__attribute__' keyword.  */
16421       cp_lexer_consume_token (parser->lexer);
16422       /* Look for the two `(' tokens.  */
16423       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16424       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16425
16426       /* Peek at the next token.  */
16427       token = cp_lexer_peek_token (parser->lexer);
16428       if (token->type != CPP_CLOSE_PAREN)
16429         /* Parse the attribute-list.  */
16430         attribute_list = cp_parser_attribute_list (parser);
16431       else
16432         /* If the next token is a `)', then there is no attribute
16433            list.  */
16434         attribute_list = NULL;
16435
16436       /* Look for the two `)' tokens.  */
16437       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16438       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16439
16440       /* Add these new attributes to the list.  */
16441       attributes = chainon (attributes, attribute_list);
16442     }
16443
16444   return attributes;
16445 }
16446
16447 /* Parse an attribute-list.
16448
16449    attribute-list:
16450      attribute
16451      attribute-list , attribute
16452
16453    attribute:
16454      identifier
16455      identifier ( identifier )
16456      identifier ( identifier , expression-list )
16457      identifier ( expression-list )
16458
16459    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
16460    to an attribute.  The TREE_PURPOSE of each node is the identifier
16461    indicating which attribute is in use.  The TREE_VALUE represents
16462    the arguments, if any.  */
16463
16464 static tree
16465 cp_parser_attribute_list (cp_parser* parser)
16466 {
16467   tree attribute_list = NULL_TREE;
16468   bool save_translate_strings_p = parser->translate_strings_p;
16469
16470   parser->translate_strings_p = false;
16471   while (true)
16472     {
16473       cp_token *token;
16474       tree identifier;
16475       tree attribute;
16476
16477       /* Look for the identifier.  We also allow keywords here; for
16478          example `__attribute__ ((const))' is legal.  */
16479       token = cp_lexer_peek_token (parser->lexer);
16480       if (token->type == CPP_NAME
16481           || token->type == CPP_KEYWORD)
16482         {
16483           tree arguments = NULL_TREE;
16484
16485           /* Consume the token.  */
16486           token = cp_lexer_consume_token (parser->lexer);
16487
16488           /* Save away the identifier that indicates which attribute
16489              this is.  */
16490           identifier = token->u.value;
16491           attribute = build_tree_list (identifier, NULL_TREE);
16492
16493           /* Peek at the next token.  */
16494           token = cp_lexer_peek_token (parser->lexer);
16495           /* If it's an `(', then parse the attribute arguments.  */
16496           if (token->type == CPP_OPEN_PAREN)
16497             {
16498               arguments = cp_parser_parenthesized_expression_list
16499                           (parser, true, /*cast_p=*/false,
16500                            /*allow_expansion_p=*/false,
16501                            /*non_constant_p=*/NULL);
16502               /* Save the arguments away.  */
16503               TREE_VALUE (attribute) = arguments;
16504             }
16505
16506           if (arguments != error_mark_node)
16507             {
16508               /* Add this attribute to the list.  */
16509               TREE_CHAIN (attribute) = attribute_list;
16510               attribute_list = attribute;
16511             }
16512
16513           token = cp_lexer_peek_token (parser->lexer);
16514         }
16515       /* Now, look for more attributes.  If the next token isn't a
16516          `,', we're done.  */
16517       if (token->type != CPP_COMMA)
16518         break;
16519
16520       /* Consume the comma and keep going.  */
16521       cp_lexer_consume_token (parser->lexer);
16522     }
16523   parser->translate_strings_p = save_translate_strings_p;
16524
16525   /* We built up the list in reverse order.  */
16526   return nreverse (attribute_list);
16527 }
16528
16529 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
16530    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
16531    current value of the PEDANTIC flag, regardless of whether or not
16532    the `__extension__' keyword is present.  The caller is responsible
16533    for restoring the value of the PEDANTIC flag.  */
16534
16535 static bool
16536 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16537 {
16538   /* Save the old value of the PEDANTIC flag.  */
16539   *saved_pedantic = pedantic;
16540
16541   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16542     {
16543       /* Consume the `__extension__' token.  */
16544       cp_lexer_consume_token (parser->lexer);
16545       /* We're not being pedantic while the `__extension__' keyword is
16546          in effect.  */
16547       pedantic = 0;
16548
16549       return true;
16550     }
16551
16552   return false;
16553 }
16554
16555 /* Parse a label declaration.
16556
16557    label-declaration:
16558      __label__ label-declarator-seq ;
16559
16560    label-declarator-seq:
16561      identifier , label-declarator-seq
16562      identifier  */
16563
16564 static void
16565 cp_parser_label_declaration (cp_parser* parser)
16566 {
16567   /* Look for the `__label__' keyword.  */
16568   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
16569
16570   while (true)
16571     {
16572       tree identifier;
16573
16574       /* Look for an identifier.  */
16575       identifier = cp_parser_identifier (parser);
16576       /* If we failed, stop.  */
16577       if (identifier == error_mark_node)
16578         break;
16579       /* Declare it as a label.  */
16580       finish_label_decl (identifier);
16581       /* If the next token is a `;', stop.  */
16582       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16583         break;
16584       /* Look for the `,' separating the label declarations.  */
16585       cp_parser_require (parser, CPP_COMMA, "%<,%>");
16586     }
16587
16588   /* Look for the final `;'.  */
16589   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16590 }
16591
16592 /* Support Functions */
16593
16594 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16595    NAME should have one of the representations used for an
16596    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16597    is returned.  If PARSER->SCOPE is a dependent type, then a
16598    SCOPE_REF is returned.
16599
16600    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16601    returned; the name was already resolved when the TEMPLATE_ID_EXPR
16602    was formed.  Abstractly, such entities should not be passed to this
16603    function, because they do not need to be looked up, but it is
16604    simpler to check for this special case here, rather than at the
16605    call-sites.
16606
16607    In cases not explicitly covered above, this function returns a
16608    DECL, OVERLOAD, or baselink representing the result of the lookup.
16609    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16610    is returned.
16611
16612    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16613    (e.g., "struct") that was used.  In that case bindings that do not
16614    refer to types are ignored.
16615
16616    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16617    ignored.
16618
16619    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16620    are ignored.
16621
16622    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16623    types.
16624
16625    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16626    TREE_LIST of candidates if name-lookup results in an ambiguity, and
16627    NULL_TREE otherwise.  */
16628
16629 static tree
16630 cp_parser_lookup_name (cp_parser *parser, tree name,
16631                        enum tag_types tag_type,
16632                        bool is_template,
16633                        bool is_namespace,
16634                        bool check_dependency,
16635                        tree *ambiguous_decls,
16636                        location_t name_location)
16637 {
16638   int flags = 0;
16639   tree decl;
16640   tree object_type = parser->context->object_type;
16641
16642   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16643     flags |= LOOKUP_COMPLAIN;
16644
16645   /* Assume that the lookup will be unambiguous.  */
16646   if (ambiguous_decls)
16647     *ambiguous_decls = NULL_TREE;
16648
16649   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16650      no longer valid.  Note that if we are parsing tentatively, and
16651      the parse fails, OBJECT_TYPE will be automatically restored.  */
16652   parser->context->object_type = NULL_TREE;
16653
16654   if (name == error_mark_node)
16655     return error_mark_node;
16656
16657   /* A template-id has already been resolved; there is no lookup to
16658      do.  */
16659   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16660     return name;
16661   if (BASELINK_P (name))
16662     {
16663       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16664                   == TEMPLATE_ID_EXPR);
16665       return name;
16666     }
16667
16668   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
16669      it should already have been checked to make sure that the name
16670      used matches the type being destroyed.  */
16671   if (TREE_CODE (name) == BIT_NOT_EXPR)
16672     {
16673       tree type;
16674
16675       /* Figure out to which type this destructor applies.  */
16676       if (parser->scope)
16677         type = parser->scope;
16678       else if (object_type)
16679         type = object_type;
16680       else
16681         type = current_class_type;
16682       /* If that's not a class type, there is no destructor.  */
16683       if (!type || !CLASS_TYPE_P (type))
16684         return error_mark_node;
16685       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16686         lazily_declare_fn (sfk_destructor, type);
16687       if (!CLASSTYPE_DESTRUCTORS (type))
16688           return error_mark_node;
16689       /* If it was a class type, return the destructor.  */
16690       return CLASSTYPE_DESTRUCTORS (type);
16691     }
16692
16693   /* By this point, the NAME should be an ordinary identifier.  If
16694      the id-expression was a qualified name, the qualifying scope is
16695      stored in PARSER->SCOPE at this point.  */
16696   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16697
16698   /* Perform the lookup.  */
16699   if (parser->scope)
16700     {
16701       bool dependent_p;
16702
16703       if (parser->scope == error_mark_node)
16704         return error_mark_node;
16705
16706       /* If the SCOPE is dependent, the lookup must be deferred until
16707          the template is instantiated -- unless we are explicitly
16708          looking up names in uninstantiated templates.  Even then, we
16709          cannot look up the name if the scope is not a class type; it
16710          might, for example, be a template type parameter.  */
16711       dependent_p = (TYPE_P (parser->scope)
16712                      && !(parser->in_declarator_p
16713                           && currently_open_class (parser->scope))
16714                      && dependent_type_p (parser->scope));
16715       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16716            && dependent_p)
16717         {
16718           if (tag_type)
16719             {
16720               tree type;
16721
16722               /* The resolution to Core Issue 180 says that `struct
16723                  A::B' should be considered a type-name, even if `A'
16724                  is dependent.  */
16725               type = make_typename_type (parser->scope, name, tag_type,
16726                                          /*complain=*/tf_error);
16727               decl = TYPE_NAME (type);
16728             }
16729           else if (is_template
16730                    && (cp_parser_next_token_ends_template_argument_p (parser)
16731                        || cp_lexer_next_token_is (parser->lexer,
16732                                                   CPP_CLOSE_PAREN)))
16733             decl = make_unbound_class_template (parser->scope,
16734                                                 name, NULL_TREE,
16735                                                 /*complain=*/tf_error);
16736           else
16737             decl = build_qualified_name (/*type=*/NULL_TREE,
16738                                          parser->scope, name,
16739                                          is_template);
16740         }
16741       else
16742         {
16743           tree pushed_scope = NULL_TREE;
16744
16745           /* If PARSER->SCOPE is a dependent type, then it must be a
16746              class type, and we must not be checking dependencies;
16747              otherwise, we would have processed this lookup above.  So
16748              that PARSER->SCOPE is not considered a dependent base by
16749              lookup_member, we must enter the scope here.  */
16750           if (dependent_p)
16751             pushed_scope = push_scope (parser->scope);
16752           /* If the PARSER->SCOPE is a template specialization, it
16753              may be instantiated during name lookup.  In that case,
16754              errors may be issued.  Even if we rollback the current
16755              tentative parse, those errors are valid.  */
16756           decl = lookup_qualified_name (parser->scope, name,
16757                                         tag_type != none_type,
16758                                         /*complain=*/true);
16759
16760           /* If we have a single function from a using decl, pull it out.  */
16761           if (decl
16762               && TREE_CODE (decl) == OVERLOAD
16763               && !really_overloaded_fn (decl))
16764             decl = OVL_FUNCTION (decl);
16765
16766           if (pushed_scope)
16767             pop_scope (pushed_scope);
16768         }
16769       parser->qualifying_scope = parser->scope;
16770       parser->object_scope = NULL_TREE;
16771     }
16772   else if (object_type)
16773     {
16774       tree object_decl = NULL_TREE;
16775       /* Look up the name in the scope of the OBJECT_TYPE, unless the
16776          OBJECT_TYPE is not a class.  */
16777       if (CLASS_TYPE_P (object_type))
16778         /* If the OBJECT_TYPE is a template specialization, it may
16779            be instantiated during name lookup.  In that case, errors
16780            may be issued.  Even if we rollback the current tentative
16781            parse, those errors are valid.  */
16782         object_decl = lookup_member (object_type,
16783                                      name,
16784                                      /*protect=*/0,
16785                                      tag_type != none_type);
16786       /* Look it up in the enclosing context, too.  */
16787       decl = lookup_name_real (name, tag_type != none_type,
16788                                /*nonclass=*/0,
16789                                /*block_p=*/true, is_namespace, flags);
16790       parser->object_scope = object_type;
16791       parser->qualifying_scope = NULL_TREE;
16792       if (object_decl)
16793         decl = object_decl;
16794     }
16795   else
16796     {
16797       decl = lookup_name_real (name, tag_type != none_type,
16798                                /*nonclass=*/0,
16799                                /*block_p=*/true, is_namespace, flags);
16800       parser->qualifying_scope = NULL_TREE;
16801       parser->object_scope = NULL_TREE;
16802     }
16803
16804   /* If the lookup failed, let our caller know.  */
16805   if (!decl || decl == error_mark_node)
16806     return error_mark_node;
16807
16808   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
16809   if (TREE_CODE (decl) == TREE_LIST)
16810     {
16811       if (ambiguous_decls)
16812         *ambiguous_decls = decl;
16813       /* The error message we have to print is too complicated for
16814          cp_parser_error, so we incorporate its actions directly.  */
16815       if (!cp_parser_simulate_error (parser))
16816         {
16817           error ("%Hreference to %qD is ambiguous",
16818                  &name_location, name);
16819           print_candidates (decl);
16820         }
16821       return error_mark_node;
16822     }
16823
16824   gcc_assert (DECL_P (decl)
16825               || TREE_CODE (decl) == OVERLOAD
16826               || TREE_CODE (decl) == SCOPE_REF
16827               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16828               || BASELINK_P (decl));
16829
16830   /* If we have resolved the name of a member declaration, check to
16831      see if the declaration is accessible.  When the name resolves to
16832      set of overloaded functions, accessibility is checked when
16833      overload resolution is done.
16834
16835      During an explicit instantiation, access is not checked at all,
16836      as per [temp.explicit].  */
16837   if (DECL_P (decl))
16838     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16839
16840   return decl;
16841 }
16842
16843 /* Like cp_parser_lookup_name, but for use in the typical case where
16844    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16845    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
16846
16847 static tree
16848 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
16849 {
16850   return cp_parser_lookup_name (parser, name,
16851                                 none_type,
16852                                 /*is_template=*/false,
16853                                 /*is_namespace=*/false,
16854                                 /*check_dependency=*/true,
16855                                 /*ambiguous_decls=*/NULL,
16856                                 location);
16857 }
16858
16859 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16860    the current context, return the TYPE_DECL.  If TAG_NAME_P is
16861    true, the DECL indicates the class being defined in a class-head,
16862    or declared in an elaborated-type-specifier.
16863
16864    Otherwise, return DECL.  */
16865
16866 static tree
16867 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16868 {
16869   /* If the TEMPLATE_DECL is being declared as part of a class-head,
16870      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16871
16872        struct A {
16873          template <typename T> struct B;
16874        };
16875
16876        template <typename T> struct A::B {};
16877
16878      Similarly, in an elaborated-type-specifier:
16879
16880        namespace N { struct X{}; }
16881
16882        struct A {
16883          template <typename T> friend struct N::X;
16884        };
16885
16886      However, if the DECL refers to a class type, and we are in
16887      the scope of the class, then the name lookup automatically
16888      finds the TYPE_DECL created by build_self_reference rather
16889      than a TEMPLATE_DECL.  For example, in:
16890
16891        template <class T> struct S {
16892          S s;
16893        };
16894
16895      there is no need to handle such case.  */
16896
16897   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16898     return DECL_TEMPLATE_RESULT (decl);
16899
16900   return decl;
16901 }
16902
16903 /* If too many, or too few, template-parameter lists apply to the
16904    declarator, issue an error message.  Returns TRUE if all went well,
16905    and FALSE otherwise.  */
16906
16907 static bool
16908 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16909                                                 cp_declarator *declarator,
16910                                                 location_t declarator_location)
16911 {
16912   unsigned num_templates;
16913
16914   /* We haven't seen any classes that involve template parameters yet.  */
16915   num_templates = 0;
16916
16917   switch (declarator->kind)
16918     {
16919     case cdk_id:
16920       if (declarator->u.id.qualifying_scope)
16921         {
16922           tree scope;
16923           tree member;
16924
16925           scope = declarator->u.id.qualifying_scope;
16926           member = declarator->u.id.unqualified_name;
16927
16928           while (scope && CLASS_TYPE_P (scope))
16929             {
16930               /* You're supposed to have one `template <...>'
16931                  for every template class, but you don't need one
16932                  for a full specialization.  For example:
16933
16934                  template <class T> struct S{};
16935                  template <> struct S<int> { void f(); };
16936                  void S<int>::f () {}
16937
16938                  is correct; there shouldn't be a `template <>' for
16939                  the definition of `S<int>::f'.  */
16940               if (!CLASSTYPE_TEMPLATE_INFO (scope))
16941                 /* If SCOPE does not have template information of any
16942                    kind, then it is not a template, nor is it nested
16943                    within a template.  */
16944                 break;
16945               if (explicit_class_specialization_p (scope))
16946                 break;
16947               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16948                 ++num_templates;
16949
16950               scope = TYPE_CONTEXT (scope);
16951             }
16952         }
16953       else if (TREE_CODE (declarator->u.id.unqualified_name)
16954                == TEMPLATE_ID_EXPR)
16955         /* If the DECLARATOR has the form `X<y>' then it uses one
16956            additional level of template parameters.  */
16957         ++num_templates;
16958
16959       return cp_parser_check_template_parameters (parser,
16960                                                   num_templates,
16961                                                   declarator_location);
16962
16963     case cdk_function:
16964     case cdk_array:
16965     case cdk_pointer:
16966     case cdk_reference:
16967     case cdk_ptrmem:
16968       return (cp_parser_check_declarator_template_parameters
16969               (parser, declarator->declarator, declarator_location));
16970
16971     case cdk_error:
16972       return true;
16973
16974     default:
16975       gcc_unreachable ();
16976     }
16977   return false;
16978 }
16979
16980 /* NUM_TEMPLATES were used in the current declaration.  If that is
16981    invalid, return FALSE and issue an error messages.  Otherwise,
16982    return TRUE.  */
16983
16984 static bool
16985 cp_parser_check_template_parameters (cp_parser* parser,
16986                                      unsigned num_templates,
16987                                      location_t location)
16988 {
16989   /* If there are more template classes than parameter lists, we have
16990      something like:
16991
16992        template <class T> void S<T>::R<T>::f ();  */
16993   if (parser->num_template_parameter_lists < num_templates)
16994     {
16995       error ("%Htoo few template-parameter-lists", &location);
16996       return false;
16997     }
16998   /* If there are the same number of template classes and parameter
16999      lists, that's OK.  */
17000   if (parser->num_template_parameter_lists == num_templates)
17001     return true;
17002   /* If there are more, but only one more, then we are referring to a
17003      member template.  That's OK too.  */
17004   if (parser->num_template_parameter_lists == num_templates + 1)
17005       return true;
17006   /* Otherwise, there are too many template parameter lists.  We have
17007      something like:
17008
17009      template <class T> template <class U> void S::f();  */
17010   error ("%Htoo many template-parameter-lists", &location);
17011   return false;
17012 }
17013
17014 /* Parse an optional `::' token indicating that the following name is
17015    from the global namespace.  If so, PARSER->SCOPE is set to the
17016    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17017    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17018    Returns the new value of PARSER->SCOPE, if the `::' token is
17019    present, and NULL_TREE otherwise.  */
17020
17021 static tree
17022 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17023 {
17024   cp_token *token;
17025
17026   /* Peek at the next token.  */
17027   token = cp_lexer_peek_token (parser->lexer);
17028   /* If we're looking at a `::' token then we're starting from the
17029      global namespace, not our current location.  */
17030   if (token->type == CPP_SCOPE)
17031     {
17032       /* Consume the `::' token.  */
17033       cp_lexer_consume_token (parser->lexer);
17034       /* Set the SCOPE so that we know where to start the lookup.  */
17035       parser->scope = global_namespace;
17036       parser->qualifying_scope = global_namespace;
17037       parser->object_scope = NULL_TREE;
17038
17039       return parser->scope;
17040     }
17041   else if (!current_scope_valid_p)
17042     {
17043       parser->scope = NULL_TREE;
17044       parser->qualifying_scope = NULL_TREE;
17045       parser->object_scope = NULL_TREE;
17046     }
17047
17048   return NULL_TREE;
17049 }
17050
17051 /* Returns TRUE if the upcoming token sequence is the start of a
17052    constructor declarator.  If FRIEND_P is true, the declarator is
17053    preceded by the `friend' specifier.  */
17054
17055 static bool
17056 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17057 {
17058   bool constructor_p;
17059   tree type_decl = NULL_TREE;
17060   bool nested_name_p;
17061   cp_token *next_token;
17062
17063   /* The common case is that this is not a constructor declarator, so
17064      try to avoid doing lots of work if at all possible.  It's not
17065      valid declare a constructor at function scope.  */
17066   if (parser->in_function_body)
17067     return false;
17068   /* And only certain tokens can begin a constructor declarator.  */
17069   next_token = cp_lexer_peek_token (parser->lexer);
17070   if (next_token->type != CPP_NAME
17071       && next_token->type != CPP_SCOPE
17072       && next_token->type != CPP_NESTED_NAME_SPECIFIER
17073       && next_token->type != CPP_TEMPLATE_ID)
17074     return false;
17075
17076   /* Parse tentatively; we are going to roll back all of the tokens
17077      consumed here.  */
17078   cp_parser_parse_tentatively (parser);
17079   /* Assume that we are looking at a constructor declarator.  */
17080   constructor_p = true;
17081
17082   /* Look for the optional `::' operator.  */
17083   cp_parser_global_scope_opt (parser,
17084                               /*current_scope_valid_p=*/false);
17085   /* Look for the nested-name-specifier.  */
17086   nested_name_p
17087     = (cp_parser_nested_name_specifier_opt (parser,
17088                                             /*typename_keyword_p=*/false,
17089                                             /*check_dependency_p=*/false,
17090                                             /*type_p=*/false,
17091                                             /*is_declaration=*/false)
17092        != NULL_TREE);
17093   /* Outside of a class-specifier, there must be a
17094      nested-name-specifier.  */
17095   if (!nested_name_p &&
17096       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17097        || friend_p))
17098     constructor_p = false;
17099   /* If we still think that this might be a constructor-declarator,
17100      look for a class-name.  */
17101   if (constructor_p)
17102     {
17103       /* If we have:
17104
17105            template <typename T> struct S { S(); };
17106            template <typename T> S<T>::S ();
17107
17108          we must recognize that the nested `S' names a class.
17109          Similarly, for:
17110
17111            template <typename T> S<T>::S<T> ();
17112
17113          we must recognize that the nested `S' names a template.  */
17114       type_decl = cp_parser_class_name (parser,
17115                                         /*typename_keyword_p=*/false,
17116                                         /*template_keyword_p=*/false,
17117                                         none_type,
17118                                         /*check_dependency_p=*/false,
17119                                         /*class_head_p=*/false,
17120                                         /*is_declaration=*/false);
17121       /* If there was no class-name, then this is not a constructor.  */
17122       constructor_p = !cp_parser_error_occurred (parser);
17123     }
17124
17125   /* If we're still considering a constructor, we have to see a `(',
17126      to begin the parameter-declaration-clause, followed by either a
17127      `)', an `...', or a decl-specifier.  We need to check for a
17128      type-specifier to avoid being fooled into thinking that:
17129
17130        S::S (f) (int);
17131
17132      is a constructor.  (It is actually a function named `f' that
17133      takes one parameter (of type `int') and returns a value of type
17134      `S::S'.  */
17135   if (constructor_p
17136       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17137     {
17138       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17139           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17140           /* A parameter declaration begins with a decl-specifier,
17141              which is either the "attribute" keyword, a storage class
17142              specifier, or (usually) a type-specifier.  */
17143           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17144         {
17145           tree type;
17146           tree pushed_scope = NULL_TREE;
17147           unsigned saved_num_template_parameter_lists;
17148
17149           /* Names appearing in the type-specifier should be looked up
17150              in the scope of the class.  */
17151           if (current_class_type)
17152             type = NULL_TREE;
17153           else
17154             {
17155               type = TREE_TYPE (type_decl);
17156               if (TREE_CODE (type) == TYPENAME_TYPE)
17157                 {
17158                   type = resolve_typename_type (type,
17159                                                 /*only_current_p=*/false);
17160                   if (TREE_CODE (type) == TYPENAME_TYPE)
17161                     {
17162                       cp_parser_abort_tentative_parse (parser);
17163                       return false;
17164                     }
17165                 }
17166               pushed_scope = push_scope (type);
17167             }
17168
17169           /* Inside the constructor parameter list, surrounding
17170              template-parameter-lists do not apply.  */
17171           saved_num_template_parameter_lists
17172             = parser->num_template_parameter_lists;
17173           parser->num_template_parameter_lists = 0;
17174
17175           /* Look for the type-specifier.  */
17176           cp_parser_type_specifier (parser,
17177                                     CP_PARSER_FLAGS_NONE,
17178                                     /*decl_specs=*/NULL,
17179                                     /*is_declarator=*/true,
17180                                     /*declares_class_or_enum=*/NULL,
17181                                     /*is_cv_qualifier=*/NULL);
17182
17183           parser->num_template_parameter_lists
17184             = saved_num_template_parameter_lists;
17185
17186           /* Leave the scope of the class.  */
17187           if (pushed_scope)
17188             pop_scope (pushed_scope);
17189
17190           constructor_p = !cp_parser_error_occurred (parser);
17191         }
17192     }
17193   else
17194     constructor_p = false;
17195   /* We did not really want to consume any tokens.  */
17196   cp_parser_abort_tentative_parse (parser);
17197
17198   return constructor_p;
17199 }
17200
17201 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17202    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
17203    they must be performed once we are in the scope of the function.
17204
17205    Returns the function defined.  */
17206
17207 static tree
17208 cp_parser_function_definition_from_specifiers_and_declarator
17209   (cp_parser* parser,
17210    cp_decl_specifier_seq *decl_specifiers,
17211    tree attributes,
17212    const cp_declarator *declarator)
17213 {
17214   tree fn;
17215   bool success_p;
17216
17217   /* Begin the function-definition.  */
17218   success_p = start_function (decl_specifiers, declarator, attributes);
17219
17220   /* The things we're about to see are not directly qualified by any
17221      template headers we've seen thus far.  */
17222   reset_specialization ();
17223
17224   /* If there were names looked up in the decl-specifier-seq that we
17225      did not check, check them now.  We must wait until we are in the
17226      scope of the function to perform the checks, since the function
17227      might be a friend.  */
17228   perform_deferred_access_checks ();
17229
17230   if (!success_p)
17231     {
17232       /* Skip the entire function.  */
17233       cp_parser_skip_to_end_of_block_or_statement (parser);
17234       fn = error_mark_node;
17235     }
17236   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17237     {
17238       /* Seen already, skip it.  An error message has already been output.  */
17239       cp_parser_skip_to_end_of_block_or_statement (parser);
17240       fn = current_function_decl;
17241       current_function_decl = NULL_TREE;
17242       /* If this is a function from a class, pop the nested class.  */
17243       if (current_class_name)
17244         pop_nested_class ();
17245     }
17246   else
17247     fn = cp_parser_function_definition_after_declarator (parser,
17248                                                          /*inline_p=*/false);
17249
17250   return fn;
17251 }
17252
17253 /* Parse the part of a function-definition that follows the
17254    declarator.  INLINE_P is TRUE iff this function is an inline
17255    function defined with a class-specifier.
17256
17257    Returns the function defined.  */
17258
17259 static tree
17260 cp_parser_function_definition_after_declarator (cp_parser* parser,
17261                                                 bool inline_p)
17262 {
17263   tree fn;
17264   bool ctor_initializer_p = false;
17265   bool saved_in_unbraced_linkage_specification_p;
17266   bool saved_in_function_body;
17267   unsigned saved_num_template_parameter_lists;
17268   cp_token *token;
17269
17270   saved_in_function_body = parser->in_function_body;
17271   parser->in_function_body = true;
17272   /* If the next token is `return', then the code may be trying to
17273      make use of the "named return value" extension that G++ used to
17274      support.  */
17275   token = cp_lexer_peek_token (parser->lexer);
17276   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17277     {
17278       /* Consume the `return' keyword.  */
17279       cp_lexer_consume_token (parser->lexer);
17280       /* Look for the identifier that indicates what value is to be
17281          returned.  */
17282       cp_parser_identifier (parser);
17283       /* Issue an error message.  */
17284       error ("%Hnamed return values are no longer supported",
17285              &token->location);
17286       /* Skip tokens until we reach the start of the function body.  */
17287       while (true)
17288         {
17289           cp_token *token = cp_lexer_peek_token (parser->lexer);
17290           if (token->type == CPP_OPEN_BRACE
17291               || token->type == CPP_EOF
17292               || token->type == CPP_PRAGMA_EOL)
17293             break;
17294           cp_lexer_consume_token (parser->lexer);
17295         }
17296     }
17297   /* The `extern' in `extern "C" void f () { ... }' does not apply to
17298      anything declared inside `f'.  */
17299   saved_in_unbraced_linkage_specification_p
17300     = parser->in_unbraced_linkage_specification_p;
17301   parser->in_unbraced_linkage_specification_p = false;
17302   /* Inside the function, surrounding template-parameter-lists do not
17303      apply.  */
17304   saved_num_template_parameter_lists
17305     = parser->num_template_parameter_lists;
17306   parser->num_template_parameter_lists = 0;
17307   /* If the next token is `try', then we are looking at a
17308      function-try-block.  */
17309   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17310     ctor_initializer_p = cp_parser_function_try_block (parser);
17311   /* A function-try-block includes the function-body, so we only do
17312      this next part if we're not processing a function-try-block.  */
17313   else
17314     ctor_initializer_p
17315       = cp_parser_ctor_initializer_opt_and_function_body (parser);
17316
17317   /* Finish the function.  */
17318   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17319                         (inline_p ? 2 : 0));
17320   /* Generate code for it, if necessary.  */
17321   expand_or_defer_fn (fn);
17322   /* Restore the saved values.  */
17323   parser->in_unbraced_linkage_specification_p
17324     = saved_in_unbraced_linkage_specification_p;
17325   parser->num_template_parameter_lists
17326     = saved_num_template_parameter_lists;
17327   parser->in_function_body = saved_in_function_body;
17328
17329   return fn;
17330 }
17331
17332 /* Parse a template-declaration, assuming that the `export' (and
17333    `extern') keywords, if present, has already been scanned.  MEMBER_P
17334    is as for cp_parser_template_declaration.  */
17335
17336 static void
17337 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17338 {
17339   tree decl = NULL_TREE;
17340   VEC (deferred_access_check,gc) *checks;
17341   tree parameter_list;
17342   bool friend_p = false;
17343   bool need_lang_pop;
17344   cp_token *token;
17345
17346   /* Look for the `template' keyword.  */
17347   token = cp_lexer_peek_token (parser->lexer);
17348   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17349     return;
17350
17351   /* And the `<'.  */
17352   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17353     return;
17354   if (at_class_scope_p () && current_function_decl)
17355     {
17356       /* 14.5.2.2 [temp.mem]
17357
17358          A local class shall not have member templates.  */
17359       error ("%Hinvalid declaration of member template in local class",
17360              &token->location);
17361       cp_parser_skip_to_end_of_block_or_statement (parser);
17362       return;
17363     }
17364   /* [temp]
17365
17366      A template ... shall not have C linkage.  */
17367   if (current_lang_name == lang_name_c)
17368     {
17369       error ("%Htemplate with C linkage", &token->location);
17370       /* Give it C++ linkage to avoid confusing other parts of the
17371          front end.  */
17372       push_lang_context (lang_name_cplusplus);
17373       need_lang_pop = true;
17374     }
17375   else
17376     need_lang_pop = false;
17377
17378   /* We cannot perform access checks on the template parameter
17379      declarations until we know what is being declared, just as we
17380      cannot check the decl-specifier list.  */
17381   push_deferring_access_checks (dk_deferred);
17382
17383   /* If the next token is `>', then we have an invalid
17384      specialization.  Rather than complain about an invalid template
17385      parameter, issue an error message here.  */
17386   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17387     {
17388       cp_parser_error (parser, "invalid explicit specialization");
17389       begin_specialization ();
17390       parameter_list = NULL_TREE;
17391     }
17392   else
17393     /* Parse the template parameters.  */
17394     parameter_list = cp_parser_template_parameter_list (parser);
17395
17396   /* Get the deferred access checks from the parameter list.  These
17397      will be checked once we know what is being declared, as for a
17398      member template the checks must be performed in the scope of the
17399      class containing the member.  */
17400   checks = get_deferred_access_checks ();
17401
17402   /* Look for the `>'.  */
17403   cp_parser_skip_to_end_of_template_parameter_list (parser);
17404   /* We just processed one more parameter list.  */
17405   ++parser->num_template_parameter_lists;
17406   /* If the next token is `template', there are more template
17407      parameters.  */
17408   if (cp_lexer_next_token_is_keyword (parser->lexer,
17409                                       RID_TEMPLATE))
17410     cp_parser_template_declaration_after_export (parser, member_p);
17411   else
17412     {
17413       /* There are no access checks when parsing a template, as we do not
17414          know if a specialization will be a friend.  */
17415       push_deferring_access_checks (dk_no_check);
17416       token = cp_lexer_peek_token (parser->lexer);
17417       decl = cp_parser_single_declaration (parser,
17418                                            checks,
17419                                            member_p,
17420                                            /*explicit_specialization_p=*/false,
17421                                            &friend_p);
17422       pop_deferring_access_checks ();
17423
17424       /* If this is a member template declaration, let the front
17425          end know.  */
17426       if (member_p && !friend_p && decl)
17427         {
17428           if (TREE_CODE (decl) == TYPE_DECL)
17429             cp_parser_check_access_in_redeclaration (decl, token->location);
17430
17431           decl = finish_member_template_decl (decl);
17432         }
17433       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17434         make_friend_class (current_class_type, TREE_TYPE (decl),
17435                            /*complain=*/true);
17436     }
17437   /* We are done with the current parameter list.  */
17438   --parser->num_template_parameter_lists;
17439
17440   pop_deferring_access_checks ();
17441
17442   /* Finish up.  */
17443   finish_template_decl (parameter_list);
17444
17445   /* Register member declarations.  */
17446   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17447     finish_member_declaration (decl);
17448   /* For the erroneous case of a template with C linkage, we pushed an
17449      implicit C++ linkage scope; exit that scope now.  */
17450   if (need_lang_pop)
17451     pop_lang_context ();
17452   /* If DECL is a function template, we must return to parse it later.
17453      (Even though there is no definition, there might be default
17454      arguments that need handling.)  */
17455   if (member_p && decl
17456       && (TREE_CODE (decl) == FUNCTION_DECL
17457           || DECL_FUNCTION_TEMPLATE_P (decl)))
17458     TREE_VALUE (parser->unparsed_functions_queues)
17459       = tree_cons (NULL_TREE, decl,
17460                    TREE_VALUE (parser->unparsed_functions_queues));
17461 }
17462
17463 /* Perform the deferred access checks from a template-parameter-list.
17464    CHECKS is a TREE_LIST of access checks, as returned by
17465    get_deferred_access_checks.  */
17466
17467 static void
17468 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17469 {
17470   ++processing_template_parmlist;
17471   perform_access_checks (checks);
17472   --processing_template_parmlist;
17473 }
17474
17475 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17476    `function-definition' sequence.  MEMBER_P is true, this declaration
17477    appears in a class scope.
17478
17479    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
17480    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
17481
17482 static tree
17483 cp_parser_single_declaration (cp_parser* parser,
17484                               VEC (deferred_access_check,gc)* checks,
17485                               bool member_p,
17486                               bool explicit_specialization_p,
17487                               bool* friend_p)
17488 {
17489   int declares_class_or_enum;
17490   tree decl = NULL_TREE;
17491   cp_decl_specifier_seq decl_specifiers;
17492   bool function_definition_p = false;
17493   cp_token *decl_spec_token_start;
17494
17495   /* This function is only used when processing a template
17496      declaration.  */
17497   gcc_assert (innermost_scope_kind () == sk_template_parms
17498               || innermost_scope_kind () == sk_template_spec);
17499
17500   /* Defer access checks until we know what is being declared.  */
17501   push_deferring_access_checks (dk_deferred);
17502
17503   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17504      alternative.  */
17505   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17506   cp_parser_decl_specifier_seq (parser,
17507                                 CP_PARSER_FLAGS_OPTIONAL,
17508                                 &decl_specifiers,
17509                                 &declares_class_or_enum);
17510   if (friend_p)
17511     *friend_p = cp_parser_friend_p (&decl_specifiers);
17512
17513   /* There are no template typedefs.  */
17514   if (decl_specifiers.specs[(int) ds_typedef])
17515     {
17516       error ("%Htemplate declaration of %qs",
17517              &decl_spec_token_start->location, "typedef");
17518       decl = error_mark_node;
17519     }
17520
17521   /* Gather up the access checks that occurred the
17522      decl-specifier-seq.  */
17523   stop_deferring_access_checks ();
17524
17525   /* Check for the declaration of a template class.  */
17526   if (declares_class_or_enum)
17527     {
17528       if (cp_parser_declares_only_class_p (parser))
17529         {
17530           decl = shadow_tag (&decl_specifiers);
17531
17532           /* In this case:
17533
17534                struct C {
17535                  friend template <typename T> struct A<T>::B;
17536                };
17537
17538              A<T>::B will be represented by a TYPENAME_TYPE, and
17539              therefore not recognized by shadow_tag.  */
17540           if (friend_p && *friend_p
17541               && !decl
17542               && decl_specifiers.type
17543               && TYPE_P (decl_specifiers.type))
17544             decl = decl_specifiers.type;
17545
17546           if (decl && decl != error_mark_node)
17547             decl = TYPE_NAME (decl);
17548           else
17549             decl = error_mark_node;
17550
17551           /* Perform access checks for template parameters.  */
17552           cp_parser_perform_template_parameter_access_checks (checks);
17553         }
17554     }
17555   /* If it's not a template class, try for a template function.  If
17556      the next token is a `;', then this declaration does not declare
17557      anything.  But, if there were errors in the decl-specifiers, then
17558      the error might well have come from an attempted class-specifier.
17559      In that case, there's no need to warn about a missing declarator.  */
17560   if (!decl
17561       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17562           || decl_specifiers.type != error_mark_node))
17563     {
17564       decl = cp_parser_init_declarator (parser,
17565                                         &decl_specifiers,
17566                                         checks,
17567                                         /*function_definition_allowed_p=*/true,
17568                                         member_p,
17569                                         declares_class_or_enum,
17570                                         &function_definition_p);
17571
17572     /* 7.1.1-1 [dcl.stc]
17573
17574        A storage-class-specifier shall not be specified in an explicit
17575        specialization...  */
17576     if (decl
17577         && explicit_specialization_p
17578         && decl_specifiers.storage_class != sc_none)
17579       {
17580         error ("%Hexplicit template specialization cannot have a storage class",
17581                &decl_spec_token_start->location);
17582         decl = error_mark_node;
17583       }
17584     }
17585
17586   pop_deferring_access_checks ();
17587
17588   /* Clear any current qualification; whatever comes next is the start
17589      of something new.  */
17590   parser->scope = NULL_TREE;
17591   parser->qualifying_scope = NULL_TREE;
17592   parser->object_scope = NULL_TREE;
17593   /* Look for a trailing `;' after the declaration.  */
17594   if (!function_definition_p
17595       && (decl == error_mark_node
17596           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
17597     cp_parser_skip_to_end_of_block_or_statement (parser);
17598
17599   return decl;
17600 }
17601
17602 /* Parse a cast-expression that is not the operand of a unary "&".  */
17603
17604 static tree
17605 cp_parser_simple_cast_expression (cp_parser *parser)
17606 {
17607   return cp_parser_cast_expression (parser, /*address_p=*/false,
17608                                     /*cast_p=*/false);
17609 }
17610
17611 /* Parse a functional cast to TYPE.  Returns an expression
17612    representing the cast.  */
17613
17614 static tree
17615 cp_parser_functional_cast (cp_parser* parser, tree type)
17616 {
17617   tree expression_list;
17618   tree cast;
17619   bool nonconst_p;
17620
17621   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17622     {
17623       maybe_warn_cpp0x ("extended initializer lists");
17624       expression_list = cp_parser_braced_list (parser, &nonconst_p);
17625       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
17626       if (TREE_CODE (type) == TYPE_DECL)
17627         type = TREE_TYPE (type);
17628       return finish_compound_literal (type, expression_list);
17629     }
17630
17631   expression_list
17632     = cp_parser_parenthesized_expression_list (parser, false,
17633                                                /*cast_p=*/true,
17634                                                /*allow_expansion_p=*/true,
17635                                                /*non_constant_p=*/NULL);
17636
17637   cast = build_functional_cast (type, expression_list,
17638                                 tf_warning_or_error);
17639   /* [expr.const]/1: In an integral constant expression "only type
17640      conversions to integral or enumeration type can be used".  */
17641   if (TREE_CODE (type) == TYPE_DECL)
17642     type = TREE_TYPE (type);
17643   if (cast != error_mark_node
17644       && !cast_valid_in_integral_constant_expression_p (type)
17645       && (cp_parser_non_integral_constant_expression
17646           (parser, "a call to a constructor")))
17647     return error_mark_node;
17648   return cast;
17649 }
17650
17651 /* Save the tokens that make up the body of a member function defined
17652    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
17653    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
17654    specifiers applied to the declaration.  Returns the FUNCTION_DECL
17655    for the member function.  */
17656
17657 static tree
17658 cp_parser_save_member_function_body (cp_parser* parser,
17659                                      cp_decl_specifier_seq *decl_specifiers,
17660                                      cp_declarator *declarator,
17661                                      tree attributes)
17662 {
17663   cp_token *first;
17664   cp_token *last;
17665   tree fn;
17666
17667   /* Create the function-declaration.  */
17668   fn = start_method (decl_specifiers, declarator, attributes);
17669   /* If something went badly wrong, bail out now.  */
17670   if (fn == error_mark_node)
17671     {
17672       /* If there's a function-body, skip it.  */
17673       if (cp_parser_token_starts_function_definition_p
17674           (cp_lexer_peek_token (parser->lexer)))
17675         cp_parser_skip_to_end_of_block_or_statement (parser);
17676       return error_mark_node;
17677     }
17678
17679   /* Remember it, if there default args to post process.  */
17680   cp_parser_save_default_args (parser, fn);
17681
17682   /* Save away the tokens that make up the body of the
17683      function.  */
17684   first = parser->lexer->next_token;
17685   /* We can have braced-init-list mem-initializers before the fn body.  */
17686   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17687     {
17688       cp_lexer_consume_token (parser->lexer);
17689       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17690              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
17691         {
17692           /* cache_group will stop after an un-nested { } pair, too.  */
17693           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
17694             break;
17695
17696           /* variadic mem-inits have ... after the ')'.  */
17697           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17698             cp_lexer_consume_token (parser->lexer);
17699         }
17700     }
17701   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17702   /* Handle function try blocks.  */
17703   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17704     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17705   last = parser->lexer->next_token;
17706
17707   /* Save away the inline definition; we will process it when the
17708      class is complete.  */
17709   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17710   DECL_PENDING_INLINE_P (fn) = 1;
17711
17712   /* We need to know that this was defined in the class, so that
17713      friend templates are handled correctly.  */
17714   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17715
17716   /* We're done with the inline definition.  */
17717   finish_method (fn);
17718
17719   /* Add FN to the queue of functions to be parsed later.  */
17720   TREE_VALUE (parser->unparsed_functions_queues)
17721     = tree_cons (NULL_TREE, fn,
17722                  TREE_VALUE (parser->unparsed_functions_queues));
17723
17724   return fn;
17725 }
17726
17727 /* Parse a template-argument-list, as well as the trailing ">" (but
17728    not the opening ">").  See cp_parser_template_argument_list for the
17729    return value.  */
17730
17731 static tree
17732 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17733 {
17734   tree arguments;
17735   tree saved_scope;
17736   tree saved_qualifying_scope;
17737   tree saved_object_scope;
17738   bool saved_greater_than_is_operator_p;
17739   bool saved_skip_evaluation;
17740
17741   /* [temp.names]
17742
17743      When parsing a template-id, the first non-nested `>' is taken as
17744      the end of the template-argument-list rather than a greater-than
17745      operator.  */
17746   saved_greater_than_is_operator_p
17747     = parser->greater_than_is_operator_p;
17748   parser->greater_than_is_operator_p = false;
17749   /* Parsing the argument list may modify SCOPE, so we save it
17750      here.  */
17751   saved_scope = parser->scope;
17752   saved_qualifying_scope = parser->qualifying_scope;
17753   saved_object_scope = parser->object_scope;
17754   /* We need to evaluate the template arguments, even though this
17755      template-id may be nested within a "sizeof".  */
17756   saved_skip_evaluation = skip_evaluation;
17757   skip_evaluation = false;
17758   /* Parse the template-argument-list itself.  */
17759   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17760       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17761     arguments = NULL_TREE;
17762   else
17763     arguments = cp_parser_template_argument_list (parser);
17764   /* Look for the `>' that ends the template-argument-list. If we find
17765      a '>>' instead, it's probably just a typo.  */
17766   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17767     {
17768       if (cxx_dialect != cxx98)
17769         {
17770           /* In C++0x, a `>>' in a template argument list or cast
17771              expression is considered to be two separate `>'
17772              tokens. So, change the current token to a `>', but don't
17773              consume it: it will be consumed later when the outer
17774              template argument list (or cast expression) is parsed.
17775              Note that this replacement of `>' for `>>' is necessary
17776              even if we are parsing tentatively: in the tentative
17777              case, after calling
17778              cp_parser_enclosed_template_argument_list we will always
17779              throw away all of the template arguments and the first
17780              closing `>', either because the template argument list
17781              was erroneous or because we are replacing those tokens
17782              with a CPP_TEMPLATE_ID token.  The second `>' (which will
17783              not have been thrown away) is needed either to close an
17784              outer template argument list or to complete a new-style
17785              cast.  */
17786           cp_token *token = cp_lexer_peek_token (parser->lexer);
17787           token->type = CPP_GREATER;
17788         }
17789       else if (!saved_greater_than_is_operator_p)
17790         {
17791           /* If we're in a nested template argument list, the '>>' has
17792             to be a typo for '> >'. We emit the error message, but we
17793             continue parsing and we push a '>' as next token, so that
17794             the argument list will be parsed correctly.  Note that the
17795             global source location is still on the token before the
17796             '>>', so we need to say explicitly where we want it.  */
17797           cp_token *token = cp_lexer_peek_token (parser->lexer);
17798           error ("%H%<>>%> should be %<> >%> "
17799                  "within a nested template argument list",
17800                  &token->location);
17801
17802           token->type = CPP_GREATER;
17803         }
17804       else
17805         {
17806           /* If this is not a nested template argument list, the '>>'
17807             is a typo for '>'. Emit an error message and continue.
17808             Same deal about the token location, but here we can get it
17809             right by consuming the '>>' before issuing the diagnostic.  */
17810           cp_token *token = cp_lexer_consume_token (parser->lexer);
17811           error ("%Hspurious %<>>%>, use %<>%> to terminate "
17812                  "a template argument list", &token->location);
17813         }
17814     }
17815   else
17816     cp_parser_skip_to_end_of_template_parameter_list (parser);
17817   /* The `>' token might be a greater-than operator again now.  */
17818   parser->greater_than_is_operator_p
17819     = saved_greater_than_is_operator_p;
17820   /* Restore the SAVED_SCOPE.  */
17821   parser->scope = saved_scope;
17822   parser->qualifying_scope = saved_qualifying_scope;
17823   parser->object_scope = saved_object_scope;
17824   skip_evaluation = saved_skip_evaluation;
17825
17826   return arguments;
17827 }
17828
17829 /* MEMBER_FUNCTION is a member function, or a friend.  If default
17830    arguments, or the body of the function have not yet been parsed,
17831    parse them now.  */
17832
17833 static void
17834 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17835 {
17836   /* If this member is a template, get the underlying
17837      FUNCTION_DECL.  */
17838   if (DECL_FUNCTION_TEMPLATE_P (member_function))
17839     member_function = DECL_TEMPLATE_RESULT (member_function);
17840
17841   /* There should not be any class definitions in progress at this
17842      point; the bodies of members are only parsed outside of all class
17843      definitions.  */
17844   gcc_assert (parser->num_classes_being_defined == 0);
17845   /* While we're parsing the member functions we might encounter more
17846      classes.  We want to handle them right away, but we don't want
17847      them getting mixed up with functions that are currently in the
17848      queue.  */
17849   parser->unparsed_functions_queues
17850     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17851
17852   /* Make sure that any template parameters are in scope.  */
17853   maybe_begin_member_template_processing (member_function);
17854
17855   /* If the body of the function has not yet been parsed, parse it
17856      now.  */
17857   if (DECL_PENDING_INLINE_P (member_function))
17858     {
17859       tree function_scope;
17860       cp_token_cache *tokens;
17861
17862       /* The function is no longer pending; we are processing it.  */
17863       tokens = DECL_PENDING_INLINE_INFO (member_function);
17864       DECL_PENDING_INLINE_INFO (member_function) = NULL;
17865       DECL_PENDING_INLINE_P (member_function) = 0;
17866
17867       /* If this is a local class, enter the scope of the containing
17868          function.  */
17869       function_scope = current_function_decl;
17870       if (function_scope)
17871         push_function_context ();
17872
17873       /* Push the body of the function onto the lexer stack.  */
17874       cp_parser_push_lexer_for_tokens (parser, tokens);
17875
17876       /* Let the front end know that we going to be defining this
17877          function.  */
17878       start_preparsed_function (member_function, NULL_TREE,
17879                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
17880
17881       /* Don't do access checking if it is a templated function.  */
17882       if (processing_template_decl)
17883         push_deferring_access_checks (dk_no_check);
17884
17885       /* Now, parse the body of the function.  */
17886       cp_parser_function_definition_after_declarator (parser,
17887                                                       /*inline_p=*/true);
17888
17889       if (processing_template_decl)
17890         pop_deferring_access_checks ();
17891
17892       /* Leave the scope of the containing function.  */
17893       if (function_scope)
17894         pop_function_context ();
17895       cp_parser_pop_lexer (parser);
17896     }
17897
17898   /* Remove any template parameters from the symbol table.  */
17899   maybe_end_member_template_processing ();
17900
17901   /* Restore the queue.  */
17902   parser->unparsed_functions_queues
17903     = TREE_CHAIN (parser->unparsed_functions_queues);
17904 }
17905
17906 /* If DECL contains any default args, remember it on the unparsed
17907    functions queue.  */
17908
17909 static void
17910 cp_parser_save_default_args (cp_parser* parser, tree decl)
17911 {
17912   tree probe;
17913
17914   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17915        probe;
17916        probe = TREE_CHAIN (probe))
17917     if (TREE_PURPOSE (probe))
17918       {
17919         TREE_PURPOSE (parser->unparsed_functions_queues)
17920           = tree_cons (current_class_type, decl,
17921                        TREE_PURPOSE (parser->unparsed_functions_queues));
17922         break;
17923       }
17924 }
17925
17926 /* FN is a FUNCTION_DECL which may contains a parameter with an
17927    unparsed DEFAULT_ARG.  Parse the default args now.  This function
17928    assumes that the current scope is the scope in which the default
17929    argument should be processed.  */
17930
17931 static void
17932 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17933 {
17934   bool saved_local_variables_forbidden_p;
17935   tree parm;
17936
17937   /* While we're parsing the default args, we might (due to the
17938      statement expression extension) encounter more classes.  We want
17939      to handle them right away, but we don't want them getting mixed
17940      up with default args that are currently in the queue.  */
17941   parser->unparsed_functions_queues
17942     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17943
17944   /* Local variable names (and the `this' keyword) may not appear
17945      in a default argument.  */
17946   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17947   parser->local_variables_forbidden_p = true;
17948
17949   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17950        parm;
17951        parm = TREE_CHAIN (parm))
17952     {
17953       cp_token_cache *tokens;
17954       tree default_arg = TREE_PURPOSE (parm);
17955       tree parsed_arg;
17956       VEC(tree,gc) *insts;
17957       tree copy;
17958       unsigned ix;
17959
17960       if (!default_arg)
17961         continue;
17962
17963       if (TREE_CODE (default_arg) != DEFAULT_ARG)
17964         /* This can happen for a friend declaration for a function
17965            already declared with default arguments.  */
17966         continue;
17967
17968        /* Push the saved tokens for the default argument onto the parser's
17969           lexer stack.  */
17970       tokens = DEFARG_TOKENS (default_arg);
17971       cp_parser_push_lexer_for_tokens (parser, tokens);
17972
17973       /* Parse the assignment-expression.  */
17974       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17975
17976       if (!processing_template_decl)
17977         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17978
17979       TREE_PURPOSE (parm) = parsed_arg;
17980
17981       /* Update any instantiations we've already created.  */
17982       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17983            VEC_iterate (tree, insts, ix, copy); ix++)
17984         TREE_PURPOSE (copy) = parsed_arg;
17985
17986       /* If the token stream has not been completely used up, then
17987          there was extra junk after the end of the default
17988          argument.  */
17989       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17990         cp_parser_error (parser, "expected %<,%>");
17991
17992       /* Revert to the main lexer.  */
17993       cp_parser_pop_lexer (parser);
17994     }
17995
17996   /* Make sure no default arg is missing.  */
17997   check_default_args (fn);
17998
17999   /* Restore the state of local_variables_forbidden_p.  */
18000   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18001
18002   /* Restore the queue.  */
18003   parser->unparsed_functions_queues
18004     = TREE_CHAIN (parser->unparsed_functions_queues);
18005 }
18006
18007 /* Parse the operand of `sizeof' (or a similar operator).  Returns
18008    either a TYPE or an expression, depending on the form of the
18009    input.  The KEYWORD indicates which kind of expression we have
18010    encountered.  */
18011
18012 static tree
18013 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18014 {
18015   tree expr = NULL_TREE;
18016   const char *saved_message;
18017   char *tmp;
18018   bool saved_integral_constant_expression_p;
18019   bool saved_non_integral_constant_expression_p;
18020   bool pack_expansion_p = false;
18021
18022   /* Types cannot be defined in a `sizeof' expression.  Save away the
18023      old message.  */
18024   saved_message = parser->type_definition_forbidden_message;
18025   /* And create the new one.  */
18026   tmp = concat ("types may not be defined in %<",
18027                 IDENTIFIER_POINTER (ridpointers[keyword]),
18028                 "%> expressions", NULL);
18029   parser->type_definition_forbidden_message = tmp;
18030
18031   /* The restrictions on constant-expressions do not apply inside
18032      sizeof expressions.  */
18033   saved_integral_constant_expression_p
18034     = parser->integral_constant_expression_p;
18035   saved_non_integral_constant_expression_p
18036     = parser->non_integral_constant_expression_p;
18037   parser->integral_constant_expression_p = false;
18038
18039   /* If it's a `...', then we are computing the length of a parameter
18040      pack.  */
18041   if (keyword == RID_SIZEOF
18042       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18043     {
18044       /* Consume the `...'.  */
18045       cp_lexer_consume_token (parser->lexer);
18046       maybe_warn_variadic_templates ();
18047
18048       /* Note that this is an expansion.  */
18049       pack_expansion_p = true;
18050     }
18051
18052   /* Do not actually evaluate the expression.  */
18053   ++skip_evaluation;
18054   /* If it's a `(', then we might be looking at the type-id
18055      construction.  */
18056   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18057     {
18058       tree type;
18059       bool saved_in_type_id_in_expr_p;
18060
18061       /* We can't be sure yet whether we're looking at a type-id or an
18062          expression.  */
18063       cp_parser_parse_tentatively (parser);
18064       /* Consume the `('.  */
18065       cp_lexer_consume_token (parser->lexer);
18066       /* Parse the type-id.  */
18067       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18068       parser->in_type_id_in_expr_p = true;
18069       type = cp_parser_type_id (parser);
18070       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18071       /* Now, look for the trailing `)'.  */
18072       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18073       /* If all went well, then we're done.  */
18074       if (cp_parser_parse_definitely (parser))
18075         {
18076           cp_decl_specifier_seq decl_specs;
18077
18078           /* Build a trivial decl-specifier-seq.  */
18079           clear_decl_specs (&decl_specs);
18080           decl_specs.type = type;
18081
18082           /* Call grokdeclarator to figure out what type this is.  */
18083           expr = grokdeclarator (NULL,
18084                                  &decl_specs,
18085                                  TYPENAME,
18086                                  /*initialized=*/0,
18087                                  /*attrlist=*/NULL);
18088         }
18089     }
18090
18091   /* If the type-id production did not work out, then we must be
18092      looking at the unary-expression production.  */
18093   if (!expr)
18094     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18095                                        /*cast_p=*/false);
18096
18097   if (pack_expansion_p)
18098     /* Build a pack expansion. */
18099     expr = make_pack_expansion (expr);
18100
18101   /* Go back to evaluating expressions.  */
18102   --skip_evaluation;
18103
18104   /* Free the message we created.  */
18105   free (tmp);
18106   /* And restore the old one.  */
18107   parser->type_definition_forbidden_message = saved_message;
18108   parser->integral_constant_expression_p
18109     = saved_integral_constant_expression_p;
18110   parser->non_integral_constant_expression_p
18111     = saved_non_integral_constant_expression_p;
18112
18113   return expr;
18114 }
18115
18116 /* If the current declaration has no declarator, return true.  */
18117
18118 static bool
18119 cp_parser_declares_only_class_p (cp_parser *parser)
18120 {
18121   /* If the next token is a `;' or a `,' then there is no
18122      declarator.  */
18123   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18124           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18125 }
18126
18127 /* Update the DECL_SPECS to reflect the storage class indicated by
18128    KEYWORD.  */
18129
18130 static void
18131 cp_parser_set_storage_class (cp_parser *parser,
18132                              cp_decl_specifier_seq *decl_specs,
18133                              enum rid keyword,
18134                              location_t location)
18135 {
18136   cp_storage_class storage_class;
18137
18138   if (parser->in_unbraced_linkage_specification_p)
18139     {
18140       error ("%Hinvalid use of %qD in linkage specification",
18141              &location, ridpointers[keyword]);
18142       return;
18143     }
18144   else if (decl_specs->storage_class != sc_none)
18145     {
18146       decl_specs->conflicting_specifiers_p = true;
18147       return;
18148     }
18149
18150   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18151       && decl_specs->specs[(int) ds_thread])
18152     {
18153       error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18154       decl_specs->specs[(int) ds_thread] = 0;
18155     }
18156
18157   switch (keyword)
18158     {
18159     case RID_AUTO:
18160       storage_class = sc_auto;
18161       break;
18162     case RID_REGISTER:
18163       storage_class = sc_register;
18164       break;
18165     case RID_STATIC:
18166       storage_class = sc_static;
18167       break;
18168     case RID_EXTERN:
18169       storage_class = sc_extern;
18170       break;
18171     case RID_MUTABLE:
18172       storage_class = sc_mutable;
18173       break;
18174     default:
18175       gcc_unreachable ();
18176     }
18177   decl_specs->storage_class = storage_class;
18178
18179   /* A storage class specifier cannot be applied alongside a typedef 
18180      specifier. If there is a typedef specifier present then set 
18181      conflicting_specifiers_p which will trigger an error later
18182      on in grokdeclarator. */
18183   if (decl_specs->specs[(int)ds_typedef])
18184     decl_specs->conflicting_specifiers_p = true;
18185 }
18186
18187 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
18188    is true, the type is a user-defined type; otherwise it is a
18189    built-in type specified by a keyword.  */
18190
18191 static void
18192 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18193                               tree type_spec,
18194                               location_t location,
18195                               bool user_defined_p)
18196 {
18197   decl_specs->any_specifiers_p = true;
18198
18199   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18200      (with, for example, in "typedef int wchar_t;") we remember that
18201      this is what happened.  In system headers, we ignore these
18202      declarations so that G++ can work with system headers that are not
18203      C++-safe.  */
18204   if (decl_specs->specs[(int) ds_typedef]
18205       && !user_defined_p
18206       && (type_spec == boolean_type_node
18207           || type_spec == char16_type_node
18208           || type_spec == char32_type_node
18209           || type_spec == wchar_type_node)
18210       && (decl_specs->type
18211           || decl_specs->specs[(int) ds_long]
18212           || decl_specs->specs[(int) ds_short]
18213           || decl_specs->specs[(int) ds_unsigned]
18214           || decl_specs->specs[(int) ds_signed]))
18215     {
18216       decl_specs->redefined_builtin_type = type_spec;
18217       if (!decl_specs->type)
18218         {
18219           decl_specs->type = type_spec;
18220           decl_specs->user_defined_type_p = false;
18221           decl_specs->type_location = location;
18222         }
18223     }
18224   else if (decl_specs->type)
18225     decl_specs->multiple_types_p = true;
18226   else
18227     {
18228       decl_specs->type = type_spec;
18229       decl_specs->user_defined_type_p = user_defined_p;
18230       decl_specs->redefined_builtin_type = NULL_TREE;
18231       decl_specs->type_location = location;
18232     }
18233 }
18234
18235 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18236    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
18237
18238 static bool
18239 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18240 {
18241   return decl_specifiers->specs[(int) ds_friend] != 0;
18242 }
18243
18244 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
18245    issue an error message indicating that TOKEN_DESC was expected.
18246
18247    Returns the token consumed, if the token had the appropriate type.
18248    Otherwise, returns NULL.  */
18249
18250 static cp_token *
18251 cp_parser_require (cp_parser* parser,
18252                    enum cpp_ttype type,
18253                    const char* token_desc)
18254 {
18255   if (cp_lexer_next_token_is (parser->lexer, type))
18256     return cp_lexer_consume_token (parser->lexer);
18257   else
18258     {
18259       /* Output the MESSAGE -- unless we're parsing tentatively.  */
18260       if (!cp_parser_simulate_error (parser))
18261         {
18262           char *message = concat ("expected ", token_desc, NULL);
18263           cp_parser_error (parser, message);
18264           free (message);
18265         }
18266       return NULL;
18267     }
18268 }
18269
18270 /* An error message is produced if the next token is not '>'.
18271    All further tokens are skipped until the desired token is
18272    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
18273
18274 static void
18275 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18276 {
18277   /* Current level of '< ... >'.  */
18278   unsigned level = 0;
18279   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
18280   unsigned nesting_depth = 0;
18281
18282   /* Are we ready, yet?  If not, issue error message.  */
18283   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18284     return;
18285
18286   /* Skip tokens until the desired token is found.  */
18287   while (true)
18288     {
18289       /* Peek at the next token.  */
18290       switch (cp_lexer_peek_token (parser->lexer)->type)
18291         {
18292         case CPP_LESS:
18293           if (!nesting_depth)
18294             ++level;
18295           break;
18296
18297         case CPP_RSHIFT:
18298           if (cxx_dialect == cxx98)
18299             /* C++0x views the `>>' operator as two `>' tokens, but
18300                C++98 does not. */
18301             break;
18302           else if (!nesting_depth && level-- == 0)
18303             {
18304               /* We've hit a `>>' where the first `>' closes the
18305                  template argument list, and the second `>' is
18306                  spurious.  Just consume the `>>' and stop; we've
18307                  already produced at least one error.  */
18308               cp_lexer_consume_token (parser->lexer);
18309               return;
18310             }
18311           /* Fall through for C++0x, so we handle the second `>' in
18312              the `>>'.  */
18313
18314         case CPP_GREATER:
18315           if (!nesting_depth && level-- == 0)
18316             {
18317               /* We've reached the token we want, consume it and stop.  */
18318               cp_lexer_consume_token (parser->lexer);
18319               return;
18320             }
18321           break;
18322
18323         case CPP_OPEN_PAREN:
18324         case CPP_OPEN_SQUARE:
18325           ++nesting_depth;
18326           break;
18327
18328         case CPP_CLOSE_PAREN:
18329         case CPP_CLOSE_SQUARE:
18330           if (nesting_depth-- == 0)
18331             return;
18332           break;
18333
18334         case CPP_EOF:
18335         case CPP_PRAGMA_EOL:
18336         case CPP_SEMICOLON:
18337         case CPP_OPEN_BRACE:
18338         case CPP_CLOSE_BRACE:
18339           /* The '>' was probably forgotten, don't look further.  */
18340           return;
18341
18342         default:
18343           break;
18344         }
18345
18346       /* Consume this token.  */
18347       cp_lexer_consume_token (parser->lexer);
18348     }
18349 }
18350
18351 /* If the next token is the indicated keyword, consume it.  Otherwise,
18352    issue an error message indicating that TOKEN_DESC was expected.
18353
18354    Returns the token consumed, if the token had the appropriate type.
18355    Otherwise, returns NULL.  */
18356
18357 static cp_token *
18358 cp_parser_require_keyword (cp_parser* parser,
18359                            enum rid keyword,
18360                            const char* token_desc)
18361 {
18362   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18363
18364   if (token && token->keyword != keyword)
18365     {
18366       dyn_string_t error_msg;
18367
18368       /* Format the error message.  */
18369       error_msg = dyn_string_new (0);
18370       dyn_string_append_cstr (error_msg, "expected ");
18371       dyn_string_append_cstr (error_msg, token_desc);
18372       cp_parser_error (parser, error_msg->s);
18373       dyn_string_delete (error_msg);
18374       return NULL;
18375     }
18376
18377   return token;
18378 }
18379
18380 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18381    function-definition.  */
18382
18383 static bool
18384 cp_parser_token_starts_function_definition_p (cp_token* token)
18385 {
18386   return (/* An ordinary function-body begins with an `{'.  */
18387           token->type == CPP_OPEN_BRACE
18388           /* A ctor-initializer begins with a `:'.  */
18389           || token->type == CPP_COLON
18390           /* A function-try-block begins with `try'.  */
18391           || token->keyword == RID_TRY
18392           /* The named return value extension begins with `return'.  */
18393           || token->keyword == RID_RETURN);
18394 }
18395
18396 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18397    definition.  */
18398
18399 static bool
18400 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18401 {
18402   cp_token *token;
18403
18404   token = cp_lexer_peek_token (parser->lexer);
18405   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18406 }
18407
18408 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18409    C++0x) ending a template-argument.  */
18410
18411 static bool
18412 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18413 {
18414   cp_token *token;
18415
18416   token = cp_lexer_peek_token (parser->lexer);
18417   return (token->type == CPP_COMMA 
18418           || token->type == CPP_GREATER
18419           || token->type == CPP_ELLIPSIS
18420           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18421 }
18422
18423 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18424    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
18425
18426 static bool
18427 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18428                                                      size_t n)
18429 {
18430   cp_token *token;
18431
18432   token = cp_lexer_peek_nth_token (parser->lexer, n);
18433   if (token->type == CPP_LESS)
18434     return true;
18435   /* Check for the sequence `<::' in the original code. It would be lexed as
18436      `[:', where `[' is a digraph, and there is no whitespace before
18437      `:'.  */
18438   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18439     {
18440       cp_token *token2;
18441       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18442       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18443         return true;
18444     }
18445   return false;
18446 }
18447
18448 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18449    or none_type otherwise.  */
18450
18451 static enum tag_types
18452 cp_parser_token_is_class_key (cp_token* token)
18453 {
18454   switch (token->keyword)
18455     {
18456     case RID_CLASS:
18457       return class_type;
18458     case RID_STRUCT:
18459       return record_type;
18460     case RID_UNION:
18461       return union_type;
18462
18463     default:
18464       return none_type;
18465     }
18466 }
18467
18468 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
18469
18470 static void
18471 cp_parser_check_class_key (enum tag_types class_key, tree type)
18472 {
18473   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18474     permerror (input_location, "%qs tag used in naming %q#T",
18475             class_key == union_type ? "union"
18476              : class_key == record_type ? "struct" : "class",
18477              type);
18478 }
18479
18480 /* Issue an error message if DECL is redeclared with different
18481    access than its original declaration [class.access.spec/3].
18482    This applies to nested classes and nested class templates.
18483    [class.mem/1].  */
18484
18485 static void
18486 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18487 {
18488   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18489     return;
18490
18491   if ((TREE_PRIVATE (decl)
18492        != (current_access_specifier == access_private_node))
18493       || (TREE_PROTECTED (decl)
18494           != (current_access_specifier == access_protected_node)))
18495     error ("%H%qD redeclared with different access", &location, decl);
18496 }
18497
18498 /* Look for the `template' keyword, as a syntactic disambiguator.
18499    Return TRUE iff it is present, in which case it will be
18500    consumed.  */
18501
18502 static bool
18503 cp_parser_optional_template_keyword (cp_parser *parser)
18504 {
18505   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18506     {
18507       /* The `template' keyword can only be used within templates;
18508          outside templates the parser can always figure out what is a
18509          template and what is not.  */
18510       if (!processing_template_decl)
18511         {
18512           cp_token *token = cp_lexer_peek_token (parser->lexer);
18513           error ("%H%<template%> (as a disambiguator) is only allowed "
18514                  "within templates", &token->location);
18515           /* If this part of the token stream is rescanned, the same
18516              error message would be generated.  So, we purge the token
18517              from the stream.  */
18518           cp_lexer_purge_token (parser->lexer);
18519           return false;
18520         }
18521       else
18522         {
18523           /* Consume the `template' keyword.  */
18524           cp_lexer_consume_token (parser->lexer);
18525           return true;
18526         }
18527     }
18528
18529   return false;
18530 }
18531
18532 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
18533    set PARSER->SCOPE, and perform other related actions.  */
18534
18535 static void
18536 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18537 {
18538   int i;
18539   struct tree_check *check_value;
18540   deferred_access_check *chk;
18541   VEC (deferred_access_check,gc) *checks;
18542
18543   /* Get the stored value.  */
18544   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18545   /* Perform any access checks that were deferred.  */
18546   checks = check_value->checks;
18547   if (checks)
18548     {
18549       for (i = 0 ;
18550            VEC_iterate (deferred_access_check, checks, i, chk) ;
18551            ++i)
18552         {
18553           perform_or_defer_access_check (chk->binfo,
18554                                          chk->decl,
18555                                          chk->diag_decl);
18556         }
18557     }
18558   /* Set the scope from the stored value.  */
18559   parser->scope = check_value->value;
18560   parser->qualifying_scope = check_value->qualifying_scope;
18561   parser->object_scope = NULL_TREE;
18562 }
18563
18564 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
18565    encounter the end of a block before what we were looking for.  */
18566
18567 static bool
18568 cp_parser_cache_group (cp_parser *parser,
18569                        enum cpp_ttype end,
18570                        unsigned depth)
18571 {
18572   while (true)
18573     {
18574       cp_token *token = cp_lexer_peek_token (parser->lexer);
18575
18576       /* Abort a parenthesized expression if we encounter a semicolon.  */
18577       if ((end == CPP_CLOSE_PAREN || depth == 0)
18578           && token->type == CPP_SEMICOLON)
18579         return true;
18580       /* If we've reached the end of the file, stop.  */
18581       if (token->type == CPP_EOF
18582           || (end != CPP_PRAGMA_EOL
18583               && token->type == CPP_PRAGMA_EOL))
18584         return true;
18585       if (token->type == CPP_CLOSE_BRACE && depth == 0)
18586         /* We've hit the end of an enclosing block, so there's been some
18587            kind of syntax error.  */
18588         return true;
18589
18590       /* Consume the token.  */
18591       cp_lexer_consume_token (parser->lexer);
18592       /* See if it starts a new group.  */
18593       if (token->type == CPP_OPEN_BRACE)
18594         {
18595           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18596           /* In theory this should probably check end == '}', but
18597              cp_parser_save_member_function_body needs it to exit
18598              after either '}' or ')' when called with ')'.  */
18599           if (depth == 0)
18600             return false;
18601         }
18602       else if (token->type == CPP_OPEN_PAREN)
18603         {
18604           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18605           if (depth == 0 && end == CPP_CLOSE_PAREN)
18606             return false;
18607         }
18608       else if (token->type == CPP_PRAGMA)
18609         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18610       else if (token->type == end)
18611         return false;
18612     }
18613 }
18614
18615 /* Begin parsing tentatively.  We always save tokens while parsing
18616    tentatively so that if the tentative parsing fails we can restore the
18617    tokens.  */
18618
18619 static void
18620 cp_parser_parse_tentatively (cp_parser* parser)
18621 {
18622   /* Enter a new parsing context.  */
18623   parser->context = cp_parser_context_new (parser->context);
18624   /* Begin saving tokens.  */
18625   cp_lexer_save_tokens (parser->lexer);
18626   /* In order to avoid repetitive access control error messages,
18627      access checks are queued up until we are no longer parsing
18628      tentatively.  */
18629   push_deferring_access_checks (dk_deferred);
18630 }
18631
18632 /* Commit to the currently active tentative parse.  */
18633
18634 static void
18635 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18636 {
18637   cp_parser_context *context;
18638   cp_lexer *lexer;
18639
18640   /* Mark all of the levels as committed.  */
18641   lexer = parser->lexer;
18642   for (context = parser->context; context->next; context = context->next)
18643     {
18644       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18645         break;
18646       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18647       while (!cp_lexer_saving_tokens (lexer))
18648         lexer = lexer->next;
18649       cp_lexer_commit_tokens (lexer);
18650     }
18651 }
18652
18653 /* Abort the currently active tentative parse.  All consumed tokens
18654    will be rolled back, and no diagnostics will be issued.  */
18655
18656 static void
18657 cp_parser_abort_tentative_parse (cp_parser* parser)
18658 {
18659   cp_parser_simulate_error (parser);
18660   /* Now, pretend that we want to see if the construct was
18661      successfully parsed.  */
18662   cp_parser_parse_definitely (parser);
18663 }
18664
18665 /* Stop parsing tentatively.  If a parse error has occurred, restore the
18666    token stream.  Otherwise, commit to the tokens we have consumed.
18667    Returns true if no error occurred; false otherwise.  */
18668
18669 static bool
18670 cp_parser_parse_definitely (cp_parser* parser)
18671 {
18672   bool error_occurred;
18673   cp_parser_context *context;
18674
18675   /* Remember whether or not an error occurred, since we are about to
18676      destroy that information.  */
18677   error_occurred = cp_parser_error_occurred (parser);
18678   /* Remove the topmost context from the stack.  */
18679   context = parser->context;
18680   parser->context = context->next;
18681   /* If no parse errors occurred, commit to the tentative parse.  */
18682   if (!error_occurred)
18683     {
18684       /* Commit to the tokens read tentatively, unless that was
18685          already done.  */
18686       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18687         cp_lexer_commit_tokens (parser->lexer);
18688
18689       pop_to_parent_deferring_access_checks ();
18690     }
18691   /* Otherwise, if errors occurred, roll back our state so that things
18692      are just as they were before we began the tentative parse.  */
18693   else
18694     {
18695       cp_lexer_rollback_tokens (parser->lexer);
18696       pop_deferring_access_checks ();
18697     }
18698   /* Add the context to the front of the free list.  */
18699   context->next = cp_parser_context_free_list;
18700   cp_parser_context_free_list = context;
18701
18702   return !error_occurred;
18703 }
18704
18705 /* Returns true if we are parsing tentatively and are not committed to
18706    this tentative parse.  */
18707
18708 static bool
18709 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18710 {
18711   return (cp_parser_parsing_tentatively (parser)
18712           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18713 }
18714
18715 /* Returns nonzero iff an error has occurred during the most recent
18716    tentative parse.  */
18717
18718 static bool
18719 cp_parser_error_occurred (cp_parser* parser)
18720 {
18721   return (cp_parser_parsing_tentatively (parser)
18722           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18723 }
18724
18725 /* Returns nonzero if GNU extensions are allowed.  */
18726
18727 static bool
18728 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18729 {
18730   return parser->allow_gnu_extensions_p;
18731 }
18732 \f
18733 /* Objective-C++ Productions */
18734
18735
18736 /* Parse an Objective-C expression, which feeds into a primary-expression
18737    above.
18738
18739    objc-expression:
18740      objc-message-expression
18741      objc-string-literal
18742      objc-encode-expression
18743      objc-protocol-expression
18744      objc-selector-expression
18745
18746   Returns a tree representation of the expression.  */
18747
18748 static tree
18749 cp_parser_objc_expression (cp_parser* parser)
18750 {
18751   /* Try to figure out what kind of declaration is present.  */
18752   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18753
18754   switch (kwd->type)
18755     {
18756     case CPP_OPEN_SQUARE:
18757       return cp_parser_objc_message_expression (parser);
18758
18759     case CPP_OBJC_STRING:
18760       kwd = cp_lexer_consume_token (parser->lexer);
18761       return objc_build_string_object (kwd->u.value);
18762
18763     case CPP_KEYWORD:
18764       switch (kwd->keyword)
18765         {
18766         case RID_AT_ENCODE:
18767           return cp_parser_objc_encode_expression (parser);
18768
18769         case RID_AT_PROTOCOL:
18770           return cp_parser_objc_protocol_expression (parser);
18771
18772         case RID_AT_SELECTOR:
18773           return cp_parser_objc_selector_expression (parser);
18774
18775         default:
18776           break;
18777         }
18778     default:
18779       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
18780              &kwd->location, kwd->u.value);
18781       cp_parser_skip_to_end_of_block_or_statement (parser);
18782     }
18783
18784   return error_mark_node;
18785 }
18786
18787 /* Parse an Objective-C message expression.
18788
18789    objc-message-expression:
18790      [ objc-message-receiver objc-message-args ]
18791
18792    Returns a representation of an Objective-C message.  */
18793
18794 static tree
18795 cp_parser_objc_message_expression (cp_parser* parser)
18796 {
18797   tree receiver, messageargs;
18798
18799   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
18800   receiver = cp_parser_objc_message_receiver (parser);
18801   messageargs = cp_parser_objc_message_args (parser);
18802   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
18803
18804   return objc_build_message_expr (build_tree_list (receiver, messageargs));
18805 }
18806
18807 /* Parse an objc-message-receiver.
18808
18809    objc-message-receiver:
18810      expression
18811      simple-type-specifier
18812
18813   Returns a representation of the type or expression.  */
18814
18815 static tree
18816 cp_parser_objc_message_receiver (cp_parser* parser)
18817 {
18818   tree rcv;
18819
18820   /* An Objective-C message receiver may be either (1) a type
18821      or (2) an expression.  */
18822   cp_parser_parse_tentatively (parser);
18823   rcv = cp_parser_expression (parser, false);
18824
18825   if (cp_parser_parse_definitely (parser))
18826     return rcv;
18827
18828   rcv = cp_parser_simple_type_specifier (parser,
18829                                          /*decl_specs=*/NULL,
18830                                          CP_PARSER_FLAGS_NONE);
18831
18832   return objc_get_class_reference (rcv);
18833 }
18834
18835 /* Parse the arguments and selectors comprising an Objective-C message.
18836
18837    objc-message-args:
18838      objc-selector
18839      objc-selector-args
18840      objc-selector-args , objc-comma-args
18841
18842    objc-selector-args:
18843      objc-selector [opt] : assignment-expression
18844      objc-selector-args objc-selector [opt] : assignment-expression
18845
18846    objc-comma-args:
18847      assignment-expression
18848      objc-comma-args , assignment-expression
18849
18850    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18851    selector arguments and TREE_VALUE containing a list of comma
18852    arguments.  */
18853
18854 static tree
18855 cp_parser_objc_message_args (cp_parser* parser)
18856 {
18857   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18858   bool maybe_unary_selector_p = true;
18859   cp_token *token = cp_lexer_peek_token (parser->lexer);
18860
18861   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18862     {
18863       tree selector = NULL_TREE, arg;
18864
18865       if (token->type != CPP_COLON)
18866         selector = cp_parser_objc_selector (parser);
18867
18868       /* Detect if we have a unary selector.  */
18869       if (maybe_unary_selector_p
18870           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18871         return build_tree_list (selector, NULL_TREE);
18872
18873       maybe_unary_selector_p = false;
18874       cp_parser_require (parser, CPP_COLON, "%<:%>");
18875       arg = cp_parser_assignment_expression (parser, false);
18876
18877       sel_args
18878         = chainon (sel_args,
18879                    build_tree_list (selector, arg));
18880
18881       token = cp_lexer_peek_token (parser->lexer);
18882     }
18883
18884   /* Handle non-selector arguments, if any. */
18885   while (token->type == CPP_COMMA)
18886     {
18887       tree arg;
18888
18889       cp_lexer_consume_token (parser->lexer);
18890       arg = cp_parser_assignment_expression (parser, false);
18891
18892       addl_args
18893         = chainon (addl_args,
18894                    build_tree_list (NULL_TREE, arg));
18895
18896       token = cp_lexer_peek_token (parser->lexer);
18897     }
18898
18899   return build_tree_list (sel_args, addl_args);
18900 }
18901
18902 /* Parse an Objective-C encode expression.
18903
18904    objc-encode-expression:
18905      @encode objc-typename
18906
18907    Returns an encoded representation of the type argument.  */
18908
18909 static tree
18910 cp_parser_objc_encode_expression (cp_parser* parser)
18911 {
18912   tree type;
18913   cp_token *token;
18914
18915   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
18916   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18917   token = cp_lexer_peek_token (parser->lexer);
18918   type = complete_type (cp_parser_type_id (parser));
18919   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18920
18921   if (!type)
18922     {
18923       error ("%H%<@encode%> must specify a type as an argument",
18924              &token->location);
18925       return error_mark_node;
18926     }
18927
18928   return objc_build_encode_expr (type);
18929 }
18930
18931 /* Parse an Objective-C @defs expression.  */
18932
18933 static tree
18934 cp_parser_objc_defs_expression (cp_parser *parser)
18935 {
18936   tree name;
18937
18938   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
18939   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18940   name = cp_parser_identifier (parser);
18941   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18942
18943   return objc_get_class_ivars (name);
18944 }
18945
18946 /* Parse an Objective-C protocol expression.
18947
18948   objc-protocol-expression:
18949     @protocol ( identifier )
18950
18951   Returns a representation of the protocol expression.  */
18952
18953 static tree
18954 cp_parser_objc_protocol_expression (cp_parser* parser)
18955 {
18956   tree proto;
18957
18958   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
18959   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18960   proto = cp_parser_identifier (parser);
18961   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18962
18963   return objc_build_protocol_expr (proto);
18964 }
18965
18966 /* Parse an Objective-C selector expression.
18967
18968    objc-selector-expression:
18969      @selector ( objc-method-signature )
18970
18971    objc-method-signature:
18972      objc-selector
18973      objc-selector-seq
18974
18975    objc-selector-seq:
18976      objc-selector :
18977      objc-selector-seq objc-selector :
18978
18979   Returns a representation of the method selector.  */
18980
18981 static tree
18982 cp_parser_objc_selector_expression (cp_parser* parser)
18983 {
18984   tree sel_seq = NULL_TREE;
18985   bool maybe_unary_selector_p = true;
18986   cp_token *token;
18987
18988   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
18989   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
18990   token = cp_lexer_peek_token (parser->lexer);
18991
18992   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18993          || token->type == CPP_SCOPE)
18994     {
18995       tree selector = NULL_TREE;
18996
18997       if (token->type != CPP_COLON
18998           || token->type == CPP_SCOPE)
18999         selector = cp_parser_objc_selector (parser);
19000
19001       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19002           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19003         {
19004           /* Detect if we have a unary selector.  */
19005           if (maybe_unary_selector_p)
19006             {
19007               sel_seq = selector;
19008               goto finish_selector;
19009             }
19010           else
19011             {
19012               cp_parser_error (parser, "expected %<:%>");
19013             }
19014         }
19015       maybe_unary_selector_p = false;
19016       token = cp_lexer_consume_token (parser->lexer);
19017
19018       if (token->type == CPP_SCOPE)
19019         {
19020           sel_seq
19021             = chainon (sel_seq,
19022                        build_tree_list (selector, NULL_TREE));
19023           sel_seq
19024             = chainon (sel_seq,
19025                        build_tree_list (NULL_TREE, NULL_TREE));
19026         }
19027       else
19028         sel_seq
19029           = chainon (sel_seq,
19030                      build_tree_list (selector, NULL_TREE));
19031
19032       token = cp_lexer_peek_token (parser->lexer);
19033     }
19034
19035  finish_selector:
19036   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19037
19038   return objc_build_selector_expr (sel_seq);
19039 }
19040
19041 /* Parse a list of identifiers.
19042
19043    objc-identifier-list:
19044      identifier
19045      objc-identifier-list , identifier
19046
19047    Returns a TREE_LIST of identifier nodes.  */
19048
19049 static tree
19050 cp_parser_objc_identifier_list (cp_parser* parser)
19051 {
19052   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19053   cp_token *sep = cp_lexer_peek_token (parser->lexer);
19054
19055   while (sep->type == CPP_COMMA)
19056     {
19057       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19058       list = chainon (list,
19059                       build_tree_list (NULL_TREE,
19060                                        cp_parser_identifier (parser)));
19061       sep = cp_lexer_peek_token (parser->lexer);
19062     }
19063
19064   return list;
19065 }
19066
19067 /* Parse an Objective-C alias declaration.
19068
19069    objc-alias-declaration:
19070      @compatibility_alias identifier identifier ;
19071
19072    This function registers the alias mapping with the Objective-C front end.
19073    It returns nothing.  */
19074
19075 static void
19076 cp_parser_objc_alias_declaration (cp_parser* parser)
19077 {
19078   tree alias, orig;
19079
19080   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
19081   alias = cp_parser_identifier (parser);
19082   orig = cp_parser_identifier (parser);
19083   objc_declare_alias (alias, orig);
19084   cp_parser_consume_semicolon_at_end_of_statement (parser);
19085 }
19086
19087 /* Parse an Objective-C class forward-declaration.
19088
19089    objc-class-declaration:
19090      @class objc-identifier-list ;
19091
19092    The function registers the forward declarations with the Objective-C
19093    front end.  It returns nothing.  */
19094
19095 static void
19096 cp_parser_objc_class_declaration (cp_parser* parser)
19097 {
19098   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
19099   objc_declare_class (cp_parser_objc_identifier_list (parser));
19100   cp_parser_consume_semicolon_at_end_of_statement (parser);
19101 }
19102
19103 /* Parse a list of Objective-C protocol references.
19104
19105    objc-protocol-refs-opt:
19106      objc-protocol-refs [opt]
19107
19108    objc-protocol-refs:
19109      < objc-identifier-list >
19110
19111    Returns a TREE_LIST of identifiers, if any.  */
19112
19113 static tree
19114 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19115 {
19116   tree protorefs = NULL_TREE;
19117
19118   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19119     {
19120       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
19121       protorefs = cp_parser_objc_identifier_list (parser);
19122       cp_parser_require (parser, CPP_GREATER, "%<>%>");
19123     }
19124
19125   return protorefs;
19126 }
19127
19128 /* Parse a Objective-C visibility specification.  */
19129
19130 static void
19131 cp_parser_objc_visibility_spec (cp_parser* parser)
19132 {
19133   cp_token *vis = cp_lexer_peek_token (parser->lexer);
19134
19135   switch (vis->keyword)
19136     {
19137     case RID_AT_PRIVATE:
19138       objc_set_visibility (2);
19139       break;
19140     case RID_AT_PROTECTED:
19141       objc_set_visibility (0);
19142       break;
19143     case RID_AT_PUBLIC:
19144       objc_set_visibility (1);
19145       break;
19146     default:
19147       return;
19148     }
19149
19150   /* Eat '@private'/'@protected'/'@public'.  */
19151   cp_lexer_consume_token (parser->lexer);
19152 }
19153
19154 /* Parse an Objective-C method type.  */
19155
19156 static void
19157 cp_parser_objc_method_type (cp_parser* parser)
19158 {
19159   objc_set_method_type
19160    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19161     ? PLUS_EXPR
19162     : MINUS_EXPR);
19163 }
19164
19165 /* Parse an Objective-C protocol qualifier.  */
19166
19167 static tree
19168 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19169 {
19170   tree quals = NULL_TREE, node;
19171   cp_token *token = cp_lexer_peek_token (parser->lexer);
19172
19173   node = token->u.value;
19174
19175   while (node && TREE_CODE (node) == IDENTIFIER_NODE
19176          && (node == ridpointers [(int) RID_IN]
19177              || node == ridpointers [(int) RID_OUT]
19178              || node == ridpointers [(int) RID_INOUT]
19179              || node == ridpointers [(int) RID_BYCOPY]
19180              || node == ridpointers [(int) RID_BYREF]
19181              || node == ridpointers [(int) RID_ONEWAY]))
19182     {
19183       quals = tree_cons (NULL_TREE, node, quals);
19184       cp_lexer_consume_token (parser->lexer);
19185       token = cp_lexer_peek_token (parser->lexer);
19186       node = token->u.value;
19187     }
19188
19189   return quals;
19190 }
19191
19192 /* Parse an Objective-C typename.  */
19193
19194 static tree
19195 cp_parser_objc_typename (cp_parser* parser)
19196 {
19197   tree type_name = NULL_TREE;
19198
19199   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19200     {
19201       tree proto_quals, cp_type = NULL_TREE;
19202
19203       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19204       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19205
19206       /* An ObjC type name may consist of just protocol qualifiers, in which
19207          case the type shall default to 'id'.  */
19208       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19209         cp_type = cp_parser_type_id (parser);
19210
19211       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19212       type_name = build_tree_list (proto_quals, cp_type);
19213     }
19214
19215   return type_name;
19216 }
19217
19218 /* Check to see if TYPE refers to an Objective-C selector name.  */
19219
19220 static bool
19221 cp_parser_objc_selector_p (enum cpp_ttype type)
19222 {
19223   return (type == CPP_NAME || type == CPP_KEYWORD
19224           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19225           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19226           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19227           || type == CPP_XOR || type == CPP_XOR_EQ);
19228 }
19229
19230 /* Parse an Objective-C selector.  */
19231
19232 static tree
19233 cp_parser_objc_selector (cp_parser* parser)
19234 {
19235   cp_token *token = cp_lexer_consume_token (parser->lexer);
19236
19237   if (!cp_parser_objc_selector_p (token->type))
19238     {
19239       error ("%Hinvalid Objective-C++ selector name", &token->location);
19240       return error_mark_node;
19241     }
19242
19243   /* C++ operator names are allowed to appear in ObjC selectors.  */
19244   switch (token->type)
19245     {
19246     case CPP_AND_AND: return get_identifier ("and");
19247     case CPP_AND_EQ: return get_identifier ("and_eq");
19248     case CPP_AND: return get_identifier ("bitand");
19249     case CPP_OR: return get_identifier ("bitor");
19250     case CPP_COMPL: return get_identifier ("compl");
19251     case CPP_NOT: return get_identifier ("not");
19252     case CPP_NOT_EQ: return get_identifier ("not_eq");
19253     case CPP_OR_OR: return get_identifier ("or");
19254     case CPP_OR_EQ: return get_identifier ("or_eq");
19255     case CPP_XOR: return get_identifier ("xor");
19256     case CPP_XOR_EQ: return get_identifier ("xor_eq");
19257     default: return token->u.value;
19258     }
19259 }
19260
19261 /* Parse an Objective-C params list.  */
19262
19263 static tree
19264 cp_parser_objc_method_keyword_params (cp_parser* parser)
19265 {
19266   tree params = NULL_TREE;
19267   bool maybe_unary_selector_p = true;
19268   cp_token *token = cp_lexer_peek_token (parser->lexer);
19269
19270   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19271     {
19272       tree selector = NULL_TREE, type_name, identifier;
19273
19274       if (token->type != CPP_COLON)
19275         selector = cp_parser_objc_selector (parser);
19276
19277       /* Detect if we have a unary selector.  */
19278       if (maybe_unary_selector_p
19279           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19280         return selector;
19281
19282       maybe_unary_selector_p = false;
19283       cp_parser_require (parser, CPP_COLON, "%<:%>");
19284       type_name = cp_parser_objc_typename (parser);
19285       identifier = cp_parser_identifier (parser);
19286
19287       params
19288         = chainon (params,
19289                    objc_build_keyword_decl (selector,
19290                                             type_name,
19291                                             identifier));
19292
19293       token = cp_lexer_peek_token (parser->lexer);
19294     }
19295
19296   return params;
19297 }
19298
19299 /* Parse the non-keyword Objective-C params.  */
19300
19301 static tree
19302 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19303 {
19304   tree params = make_node (TREE_LIST);
19305   cp_token *token = cp_lexer_peek_token (parser->lexer);
19306   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
19307
19308   while (token->type == CPP_COMMA)
19309     {
19310       cp_parameter_declarator *parmdecl;
19311       tree parm;
19312
19313       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19314       token = cp_lexer_peek_token (parser->lexer);
19315
19316       if (token->type == CPP_ELLIPSIS)
19317         {
19318           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
19319           *ellipsisp = true;
19320           break;
19321         }
19322
19323       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19324       parm = grokdeclarator (parmdecl->declarator,
19325                              &parmdecl->decl_specifiers,
19326                              PARM, /*initialized=*/0,
19327                              /*attrlist=*/NULL);
19328
19329       chainon (params, build_tree_list (NULL_TREE, parm));
19330       token = cp_lexer_peek_token (parser->lexer);
19331     }
19332
19333   return params;
19334 }
19335
19336 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
19337
19338 static void
19339 cp_parser_objc_interstitial_code (cp_parser* parser)
19340 {
19341   cp_token *token = cp_lexer_peek_token (parser->lexer);
19342
19343   /* If the next token is `extern' and the following token is a string
19344      literal, then we have a linkage specification.  */
19345   if (token->keyword == RID_EXTERN
19346       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19347     cp_parser_linkage_specification (parser);
19348   /* Handle #pragma, if any.  */
19349   else if (token->type == CPP_PRAGMA)
19350     cp_parser_pragma (parser, pragma_external);
19351   /* Allow stray semicolons.  */
19352   else if (token->type == CPP_SEMICOLON)
19353     cp_lexer_consume_token (parser->lexer);
19354   /* Finally, try to parse a block-declaration, or a function-definition.  */
19355   else
19356     cp_parser_block_declaration (parser, /*statement_p=*/false);
19357 }
19358
19359 /* Parse a method signature.  */
19360
19361 static tree
19362 cp_parser_objc_method_signature (cp_parser* parser)
19363 {
19364   tree rettype, kwdparms, optparms;
19365   bool ellipsis = false;
19366
19367   cp_parser_objc_method_type (parser);
19368   rettype = cp_parser_objc_typename (parser);
19369   kwdparms = cp_parser_objc_method_keyword_params (parser);
19370   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19371
19372   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19373 }
19374
19375 /* Pars an Objective-C method prototype list.  */
19376
19377 static void
19378 cp_parser_objc_method_prototype_list (cp_parser* parser)
19379 {
19380   cp_token *token = cp_lexer_peek_token (parser->lexer);
19381
19382   while (token->keyword != RID_AT_END)
19383     {
19384       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19385         {
19386           objc_add_method_declaration
19387            (cp_parser_objc_method_signature (parser));
19388           cp_parser_consume_semicolon_at_end_of_statement (parser);
19389         }
19390       else
19391         /* Allow for interspersed non-ObjC++ code.  */
19392         cp_parser_objc_interstitial_code (parser);
19393
19394       token = cp_lexer_peek_token (parser->lexer);
19395     }
19396
19397   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19398   objc_finish_interface ();
19399 }
19400
19401 /* Parse an Objective-C method definition list.  */
19402
19403 static void
19404 cp_parser_objc_method_definition_list (cp_parser* parser)
19405 {
19406   cp_token *token = cp_lexer_peek_token (parser->lexer);
19407
19408   while (token->keyword != RID_AT_END)
19409     {
19410       tree meth;
19411
19412       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19413         {
19414           push_deferring_access_checks (dk_deferred);
19415           objc_start_method_definition
19416            (cp_parser_objc_method_signature (parser));
19417
19418           /* For historical reasons, we accept an optional semicolon.  */
19419           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19420             cp_lexer_consume_token (parser->lexer);
19421
19422           perform_deferred_access_checks ();
19423           stop_deferring_access_checks ();
19424           meth = cp_parser_function_definition_after_declarator (parser,
19425                                                                  false);
19426           pop_deferring_access_checks ();
19427           objc_finish_method_definition (meth);
19428         }
19429       else
19430         /* Allow for interspersed non-ObjC++ code.  */
19431         cp_parser_objc_interstitial_code (parser);
19432
19433       token = cp_lexer_peek_token (parser->lexer);
19434     }
19435
19436   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19437   objc_finish_implementation ();
19438 }
19439
19440 /* Parse Objective-C ivars.  */
19441
19442 static void
19443 cp_parser_objc_class_ivars (cp_parser* parser)
19444 {
19445   cp_token *token = cp_lexer_peek_token (parser->lexer);
19446
19447   if (token->type != CPP_OPEN_BRACE)
19448     return;     /* No ivars specified.  */
19449
19450   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
19451   token = cp_lexer_peek_token (parser->lexer);
19452
19453   while (token->type != CPP_CLOSE_BRACE)
19454     {
19455       cp_decl_specifier_seq declspecs;
19456       int decl_class_or_enum_p;
19457       tree prefix_attributes;
19458
19459       cp_parser_objc_visibility_spec (parser);
19460
19461       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19462         break;
19463
19464       cp_parser_decl_specifier_seq (parser,
19465                                     CP_PARSER_FLAGS_OPTIONAL,
19466                                     &declspecs,
19467                                     &decl_class_or_enum_p);
19468       prefix_attributes = declspecs.attributes;
19469       declspecs.attributes = NULL_TREE;
19470
19471       /* Keep going until we hit the `;' at the end of the
19472          declaration.  */
19473       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19474         {
19475           tree width = NULL_TREE, attributes, first_attribute, decl;
19476           cp_declarator *declarator = NULL;
19477           int ctor_dtor_or_conv_p;
19478
19479           /* Check for a (possibly unnamed) bitfield declaration.  */
19480           token = cp_lexer_peek_token (parser->lexer);
19481           if (token->type == CPP_COLON)
19482             goto eat_colon;
19483
19484           if (token->type == CPP_NAME
19485               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19486                   == CPP_COLON))
19487             {
19488               /* Get the name of the bitfield.  */
19489               declarator = make_id_declarator (NULL_TREE,
19490                                                cp_parser_identifier (parser),
19491                                                sfk_none);
19492
19493              eat_colon:
19494               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19495               /* Get the width of the bitfield.  */
19496               width
19497                 = cp_parser_constant_expression (parser,
19498                                                  /*allow_non_constant=*/false,
19499                                                  NULL);
19500             }
19501           else
19502             {
19503               /* Parse the declarator.  */
19504               declarator
19505                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19506                                         &ctor_dtor_or_conv_p,
19507                                         /*parenthesized_p=*/NULL,
19508                                         /*member_p=*/false);
19509             }
19510
19511           /* Look for attributes that apply to the ivar.  */
19512           attributes = cp_parser_attributes_opt (parser);
19513           /* Remember which attributes are prefix attributes and
19514              which are not.  */
19515           first_attribute = attributes;
19516           /* Combine the attributes.  */
19517           attributes = chainon (prefix_attributes, attributes);
19518
19519           if (width)
19520               /* Create the bitfield declaration.  */
19521               decl = grokbitfield (declarator, &declspecs,
19522                                    width,
19523                                    attributes);
19524           else
19525             decl = grokfield (declarator, &declspecs,
19526                               NULL_TREE, /*init_const_expr_p=*/false,
19527                               NULL_TREE, attributes);
19528
19529           /* Add the instance variable.  */
19530           objc_add_instance_variable (decl);
19531
19532           /* Reset PREFIX_ATTRIBUTES.  */
19533           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19534             attributes = TREE_CHAIN (attributes);
19535           if (attributes)
19536             TREE_CHAIN (attributes) = NULL_TREE;
19537
19538           token = cp_lexer_peek_token (parser->lexer);
19539
19540           if (token->type == CPP_COMMA)
19541             {
19542               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
19543               continue;
19544             }
19545           break;
19546         }
19547
19548       cp_parser_consume_semicolon_at_end_of_statement (parser);
19549       token = cp_lexer_peek_token (parser->lexer);
19550     }
19551
19552   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
19553   /* For historical reasons, we accept an optional semicolon.  */
19554   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19555     cp_lexer_consume_token (parser->lexer);
19556 }
19557
19558 /* Parse an Objective-C protocol declaration.  */
19559
19560 static void
19561 cp_parser_objc_protocol_declaration (cp_parser* parser)
19562 {
19563   tree proto, protorefs;
19564   cp_token *tok;
19565
19566   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
19567   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19568     {
19569       tok = cp_lexer_peek_token (parser->lexer);
19570       error ("%Hidentifier expected after %<@protocol%>", &tok->location);
19571       goto finish;
19572     }
19573
19574   /* See if we have a forward declaration or a definition.  */
19575   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19576
19577   /* Try a forward declaration first.  */
19578   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19579     {
19580       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19581      finish:
19582       cp_parser_consume_semicolon_at_end_of_statement (parser);
19583     }
19584
19585   /* Ok, we got a full-fledged definition (or at least should).  */
19586   else
19587     {
19588       proto = cp_parser_identifier (parser);
19589       protorefs = cp_parser_objc_protocol_refs_opt (parser);
19590       objc_start_protocol (proto, protorefs);
19591       cp_parser_objc_method_prototype_list (parser);
19592     }
19593 }
19594
19595 /* Parse an Objective-C superclass or category.  */
19596
19597 static void
19598 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19599                                                           tree *categ)
19600 {
19601   cp_token *next = cp_lexer_peek_token (parser->lexer);
19602
19603   *super = *categ = NULL_TREE;
19604   if (next->type == CPP_COLON)
19605     {
19606       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
19607       *super = cp_parser_identifier (parser);
19608     }
19609   else if (next->type == CPP_OPEN_PAREN)
19610     {
19611       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
19612       *categ = cp_parser_identifier (parser);
19613       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19614     }
19615 }
19616
19617 /* Parse an Objective-C class interface.  */
19618
19619 static void
19620 cp_parser_objc_class_interface (cp_parser* parser)
19621 {
19622   tree name, super, categ, protos;
19623
19624   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
19625   name = cp_parser_identifier (parser);
19626   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19627   protos = cp_parser_objc_protocol_refs_opt (parser);
19628
19629   /* We have either a class or a category on our hands.  */
19630   if (categ)
19631     objc_start_category_interface (name, categ, protos);
19632   else
19633     {
19634       objc_start_class_interface (name, super, protos);
19635       /* Handle instance variable declarations, if any.  */
19636       cp_parser_objc_class_ivars (parser);
19637       objc_continue_interface ();
19638     }
19639
19640   cp_parser_objc_method_prototype_list (parser);
19641 }
19642
19643 /* Parse an Objective-C class implementation.  */
19644
19645 static void
19646 cp_parser_objc_class_implementation (cp_parser* parser)
19647 {
19648   tree name, super, categ;
19649
19650   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
19651   name = cp_parser_identifier (parser);
19652   cp_parser_objc_superclass_or_category (parser, &super, &categ);
19653
19654   /* We have either a class or a category on our hands.  */
19655   if (categ)
19656     objc_start_category_implementation (name, categ);
19657   else
19658     {
19659       objc_start_class_implementation (name, super);
19660       /* Handle instance variable declarations, if any.  */
19661       cp_parser_objc_class_ivars (parser);
19662       objc_continue_implementation ();
19663     }
19664
19665   cp_parser_objc_method_definition_list (parser);
19666 }
19667
19668 /* Consume the @end token and finish off the implementation.  */
19669
19670 static void
19671 cp_parser_objc_end_implementation (cp_parser* parser)
19672 {
19673   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
19674   objc_finish_implementation ();
19675 }
19676
19677 /* Parse an Objective-C declaration.  */
19678
19679 static void
19680 cp_parser_objc_declaration (cp_parser* parser)
19681 {
19682   /* Try to figure out what kind of declaration is present.  */
19683   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19684
19685   switch (kwd->keyword)
19686     {
19687     case RID_AT_ALIAS:
19688       cp_parser_objc_alias_declaration (parser);
19689       break;
19690     case RID_AT_CLASS:
19691       cp_parser_objc_class_declaration (parser);
19692       break;
19693     case RID_AT_PROTOCOL:
19694       cp_parser_objc_protocol_declaration (parser);
19695       break;
19696     case RID_AT_INTERFACE:
19697       cp_parser_objc_class_interface (parser);
19698       break;
19699     case RID_AT_IMPLEMENTATION:
19700       cp_parser_objc_class_implementation (parser);
19701       break;
19702     case RID_AT_END:
19703       cp_parser_objc_end_implementation (parser);
19704       break;
19705     default:
19706       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19707              &kwd->location, kwd->u.value);
19708       cp_parser_skip_to_end_of_block_or_statement (parser);
19709     }
19710 }
19711
19712 /* Parse an Objective-C try-catch-finally statement.
19713
19714    objc-try-catch-finally-stmt:
19715      @try compound-statement objc-catch-clause-seq [opt]
19716        objc-finally-clause [opt]
19717
19718    objc-catch-clause-seq:
19719      objc-catch-clause objc-catch-clause-seq [opt]
19720
19721    objc-catch-clause:
19722      @catch ( exception-declaration ) compound-statement
19723
19724    objc-finally-clause
19725      @finally compound-statement
19726
19727    Returns NULL_TREE.  */
19728
19729 static tree
19730 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19731   location_t location;
19732   tree stmt;
19733
19734   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
19735   location = cp_lexer_peek_token (parser->lexer)->location;
19736   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19737      node, lest it get absorbed into the surrounding block.  */
19738   stmt = push_stmt_list ();
19739   cp_parser_compound_statement (parser, NULL, false);
19740   objc_begin_try_stmt (location, pop_stmt_list (stmt));
19741
19742   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19743     {
19744       cp_parameter_declarator *parmdecl;
19745       tree parm;
19746
19747       cp_lexer_consume_token (parser->lexer);
19748       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19749       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19750       parm = grokdeclarator (parmdecl->declarator,
19751                              &parmdecl->decl_specifiers,
19752                              PARM, /*initialized=*/0,
19753                              /*attrlist=*/NULL);
19754       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19755       objc_begin_catch_clause (parm);
19756       cp_parser_compound_statement (parser, NULL, false);
19757       objc_finish_catch_clause ();
19758     }
19759
19760   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19761     {
19762       cp_lexer_consume_token (parser->lexer);
19763       location = cp_lexer_peek_token (parser->lexer)->location;
19764       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19765          node, lest it get absorbed into the surrounding block.  */
19766       stmt = push_stmt_list ();
19767       cp_parser_compound_statement (parser, NULL, false);
19768       objc_build_finally_clause (location, pop_stmt_list (stmt));
19769     }
19770
19771   return objc_finish_try_stmt ();
19772 }
19773
19774 /* Parse an Objective-C synchronized statement.
19775
19776    objc-synchronized-stmt:
19777      @synchronized ( expression ) compound-statement
19778
19779    Returns NULL_TREE.  */
19780
19781 static tree
19782 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19783   location_t location;
19784   tree lock, stmt;
19785
19786   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
19787
19788   location = cp_lexer_peek_token (parser->lexer)->location;
19789   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19790   lock = cp_parser_expression (parser, false);
19791   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19792
19793   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19794      node, lest it get absorbed into the surrounding block.  */
19795   stmt = push_stmt_list ();
19796   cp_parser_compound_statement (parser, NULL, false);
19797
19798   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19799 }
19800
19801 /* Parse an Objective-C throw statement.
19802
19803    objc-throw-stmt:
19804      @throw assignment-expression [opt] ;
19805
19806    Returns a constructed '@throw' statement.  */
19807
19808 static tree
19809 cp_parser_objc_throw_statement (cp_parser *parser) {
19810   tree expr = NULL_TREE;
19811
19812   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
19813
19814   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19815     expr = cp_parser_assignment_expression (parser, false);
19816
19817   cp_parser_consume_semicolon_at_end_of_statement (parser);
19818
19819   return objc_build_throw_stmt (expr);
19820 }
19821
19822 /* Parse an Objective-C statement.  */
19823
19824 static tree
19825 cp_parser_objc_statement (cp_parser * parser) {
19826   /* Try to figure out what kind of declaration is present.  */
19827   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19828
19829   switch (kwd->keyword)
19830     {
19831     case RID_AT_TRY:
19832       return cp_parser_objc_try_catch_finally_statement (parser);
19833     case RID_AT_SYNCHRONIZED:
19834       return cp_parser_objc_synchronized_statement (parser);
19835     case RID_AT_THROW:
19836       return cp_parser_objc_throw_statement (parser);
19837     default:
19838       error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19839              &kwd->location, kwd->u.value);
19840       cp_parser_skip_to_end_of_block_or_statement (parser);
19841     }
19842
19843   return error_mark_node;
19844 }
19845 \f
19846 /* OpenMP 2.5 parsing routines.  */
19847
19848 /* Returns name of the next clause.
19849    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19850    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
19851    returned and the token is consumed.  */
19852
19853 static pragma_omp_clause
19854 cp_parser_omp_clause_name (cp_parser *parser)
19855 {
19856   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19857
19858   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19859     result = PRAGMA_OMP_CLAUSE_IF;
19860   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19861     result = PRAGMA_OMP_CLAUSE_DEFAULT;
19862   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19863     result = PRAGMA_OMP_CLAUSE_PRIVATE;
19864   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19865     {
19866       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19867       const char *p = IDENTIFIER_POINTER (id);
19868
19869       switch (p[0])
19870         {
19871         case 'c':
19872           if (!strcmp ("collapse", p))
19873             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
19874           else if (!strcmp ("copyin", p))
19875             result = PRAGMA_OMP_CLAUSE_COPYIN;
19876           else if (!strcmp ("copyprivate", p))
19877             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19878           break;
19879         case 'f':
19880           if (!strcmp ("firstprivate", p))
19881             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19882           break;
19883         case 'l':
19884           if (!strcmp ("lastprivate", p))
19885             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19886           break;
19887         case 'n':
19888           if (!strcmp ("nowait", p))
19889             result = PRAGMA_OMP_CLAUSE_NOWAIT;
19890           else if (!strcmp ("num_threads", p))
19891             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19892           break;
19893         case 'o':
19894           if (!strcmp ("ordered", p))
19895             result = PRAGMA_OMP_CLAUSE_ORDERED;
19896           break;
19897         case 'r':
19898           if (!strcmp ("reduction", p))
19899             result = PRAGMA_OMP_CLAUSE_REDUCTION;
19900           break;
19901         case 's':
19902           if (!strcmp ("schedule", p))
19903             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19904           else if (!strcmp ("shared", p))
19905             result = PRAGMA_OMP_CLAUSE_SHARED;
19906           break;
19907         case 'u':
19908           if (!strcmp ("untied", p))
19909             result = PRAGMA_OMP_CLAUSE_UNTIED;
19910           break;
19911         }
19912     }
19913
19914   if (result != PRAGMA_OMP_CLAUSE_NONE)
19915     cp_lexer_consume_token (parser->lexer);
19916
19917   return result;
19918 }
19919
19920 /* Validate that a clause of the given type does not already exist.  */
19921
19922 static void
19923 check_no_duplicate_clause (tree clauses, enum tree_code code,
19924                            const char *name, location_t location)
19925 {
19926   tree c;
19927
19928   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19929     if (OMP_CLAUSE_CODE (c) == code)
19930       {
19931         error ("%Htoo many %qs clauses", &location, name);
19932         break;
19933       }
19934 }
19935
19936 /* OpenMP 2.5:
19937    variable-list:
19938      identifier
19939      variable-list , identifier
19940
19941    In addition, we match a closing parenthesis.  An opening parenthesis
19942    will have been consumed by the caller.
19943
19944    If KIND is nonzero, create the appropriate node and install the decl
19945    in OMP_CLAUSE_DECL and add the node to the head of the list.
19946
19947    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19948    return the list created.  */
19949
19950 static tree
19951 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19952                                 tree list)
19953 {
19954   cp_token *token;
19955   while (1)
19956     {
19957       tree name, decl;
19958
19959       token = cp_lexer_peek_token (parser->lexer);
19960       name = cp_parser_id_expression (parser, /*template_p=*/false,
19961                                       /*check_dependency_p=*/true,
19962                                       /*template_p=*/NULL,
19963                                       /*declarator_p=*/false,
19964                                       /*optional_p=*/false);
19965       if (name == error_mark_node)
19966         goto skip_comma;
19967
19968       decl = cp_parser_lookup_name_simple (parser, name, token->location);
19969       if (decl == error_mark_node)
19970         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
19971       else if (kind != 0)
19972         {
19973           tree u = build_omp_clause (kind);
19974           OMP_CLAUSE_DECL (u) = decl;
19975           OMP_CLAUSE_CHAIN (u) = list;
19976           list = u;
19977         }
19978       else
19979         list = tree_cons (decl, NULL_TREE, list);
19980
19981     get_comma:
19982       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19983         break;
19984       cp_lexer_consume_token (parser->lexer);
19985     }
19986
19987   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
19988     {
19989       int ending;
19990
19991       /* Try to resync to an unnested comma.  Copied from
19992          cp_parser_parenthesized_expression_list.  */
19993     skip_comma:
19994       ending = cp_parser_skip_to_closing_parenthesis (parser,
19995                                                       /*recovering=*/true,
19996                                                       /*or_comma=*/true,
19997                                                       /*consume_paren=*/true);
19998       if (ending < 0)
19999         goto get_comma;
20000     }
20001
20002   return list;
20003 }
20004
20005 /* Similarly, but expect leading and trailing parenthesis.  This is a very
20006    common case for omp clauses.  */
20007
20008 static tree
20009 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20010 {
20011   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20012     return cp_parser_omp_var_list_no_open (parser, kind, list);
20013   return list;
20014 }
20015
20016 /* OpenMP 3.0:
20017    collapse ( constant-expression ) */
20018
20019 static tree
20020 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20021 {
20022   tree c, num;
20023   location_t loc;
20024   HOST_WIDE_INT n;
20025
20026   loc = cp_lexer_peek_token (parser->lexer)->location;
20027   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20028     return list;
20029
20030   num = cp_parser_constant_expression (parser, false, NULL);
20031
20032   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20033     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20034                                            /*or_comma=*/false,
20035                                            /*consume_paren=*/true);
20036
20037   if (num == error_mark_node)
20038     return list;
20039   num = fold_non_dependent_expr (num);
20040   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20041       || !host_integerp (num, 0)
20042       || (n = tree_low_cst (num, 0)) <= 0
20043       || (int) n != n)
20044     {
20045       error ("%Hcollapse argument needs positive constant integer expression",
20046              &loc);
20047       return list;
20048     }
20049
20050   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20051   c = build_omp_clause (OMP_CLAUSE_COLLAPSE);
20052   OMP_CLAUSE_CHAIN (c) = list;
20053   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20054
20055   return c;
20056 }
20057
20058 /* OpenMP 2.5:
20059    default ( shared | none ) */
20060
20061 static tree
20062 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20063 {
20064   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20065   tree c;
20066
20067   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20068     return list;
20069   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20070     {
20071       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20072       const char *p = IDENTIFIER_POINTER (id);
20073
20074       switch (p[0])
20075         {
20076         case 'n':
20077           if (strcmp ("none", p) != 0)
20078             goto invalid_kind;
20079           kind = OMP_CLAUSE_DEFAULT_NONE;
20080           break;
20081
20082         case 's':
20083           if (strcmp ("shared", p) != 0)
20084             goto invalid_kind;
20085           kind = OMP_CLAUSE_DEFAULT_SHARED;
20086           break;
20087
20088         default:
20089           goto invalid_kind;
20090         }
20091
20092       cp_lexer_consume_token (parser->lexer);
20093     }
20094   else
20095     {
20096     invalid_kind:
20097       cp_parser_error (parser, "expected %<none%> or %<shared%>");
20098     }
20099
20100   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20101     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20102                                            /*or_comma=*/false,
20103                                            /*consume_paren=*/true);
20104
20105   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20106     return list;
20107
20108   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20109   c = build_omp_clause (OMP_CLAUSE_DEFAULT);
20110   OMP_CLAUSE_CHAIN (c) = list;
20111   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20112
20113   return c;
20114 }
20115
20116 /* OpenMP 2.5:
20117    if ( expression ) */
20118
20119 static tree
20120 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20121 {
20122   tree t, c;
20123
20124   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20125     return list;
20126
20127   t = cp_parser_condition (parser);
20128
20129   if (t == error_mark_node
20130       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20131     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20132                                            /*or_comma=*/false,
20133                                            /*consume_paren=*/true);
20134
20135   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20136
20137   c = build_omp_clause (OMP_CLAUSE_IF);
20138   OMP_CLAUSE_IF_EXPR (c) = t;
20139   OMP_CLAUSE_CHAIN (c) = list;
20140
20141   return c;
20142 }
20143
20144 /* OpenMP 2.5:
20145    nowait */
20146
20147 static tree
20148 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20149                              tree list, location_t location)
20150 {
20151   tree c;
20152
20153   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20154
20155   c = build_omp_clause (OMP_CLAUSE_NOWAIT);
20156   OMP_CLAUSE_CHAIN (c) = list;
20157   return c;
20158 }
20159
20160 /* OpenMP 2.5:
20161    num_threads ( expression ) */
20162
20163 static tree
20164 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20165                                   location_t location)
20166 {
20167   tree t, c;
20168
20169   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20170     return list;
20171
20172   t = cp_parser_expression (parser, false);
20173
20174   if (t == error_mark_node
20175       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20176     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20177                                            /*or_comma=*/false,
20178                                            /*consume_paren=*/true);
20179
20180   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20181                              "num_threads", location);
20182
20183   c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
20184   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20185   OMP_CLAUSE_CHAIN (c) = list;
20186
20187   return c;
20188 }
20189
20190 /* OpenMP 2.5:
20191    ordered */
20192
20193 static tree
20194 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20195                               tree list, location_t location)
20196 {
20197   tree c;
20198
20199   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20200                              "ordered", location);
20201
20202   c = build_omp_clause (OMP_CLAUSE_ORDERED);
20203   OMP_CLAUSE_CHAIN (c) = list;
20204   return c;
20205 }
20206
20207 /* OpenMP 2.5:
20208    reduction ( reduction-operator : variable-list )
20209
20210    reduction-operator:
20211      One of: + * - & ^ | && || */
20212
20213 static tree
20214 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20215 {
20216   enum tree_code code;
20217   tree nlist, c;
20218
20219   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20220     return list;
20221
20222   switch (cp_lexer_peek_token (parser->lexer)->type)
20223     {
20224     case CPP_PLUS:
20225       code = PLUS_EXPR;
20226       break;
20227     case CPP_MULT:
20228       code = MULT_EXPR;
20229       break;
20230     case CPP_MINUS:
20231       code = MINUS_EXPR;
20232       break;
20233     case CPP_AND:
20234       code = BIT_AND_EXPR;
20235       break;
20236     case CPP_XOR:
20237       code = BIT_XOR_EXPR;
20238       break;
20239     case CPP_OR:
20240       code = BIT_IOR_EXPR;
20241       break;
20242     case CPP_AND_AND:
20243       code = TRUTH_ANDIF_EXPR;
20244       break;
20245     case CPP_OR_OR:
20246       code = TRUTH_ORIF_EXPR;
20247       break;
20248     default:
20249       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20250                                "%<|%>, %<&&%>, or %<||%>");
20251     resync_fail:
20252       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20253                                              /*or_comma=*/false,
20254                                              /*consume_paren=*/true);
20255       return list;
20256     }
20257   cp_lexer_consume_token (parser->lexer);
20258
20259   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20260     goto resync_fail;
20261
20262   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20263   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20264     OMP_CLAUSE_REDUCTION_CODE (c) = code;
20265
20266   return nlist;
20267 }
20268
20269 /* OpenMP 2.5:
20270    schedule ( schedule-kind )
20271    schedule ( schedule-kind , expression )
20272
20273    schedule-kind:
20274      static | dynamic | guided | runtime | auto  */
20275
20276 static tree
20277 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20278 {
20279   tree c, t;
20280
20281   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20282     return list;
20283
20284   c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
20285
20286   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20287     {
20288       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20289       const char *p = IDENTIFIER_POINTER (id);
20290
20291       switch (p[0])
20292         {
20293         case 'd':
20294           if (strcmp ("dynamic", p) != 0)
20295             goto invalid_kind;
20296           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20297           break;
20298
20299         case 'g':
20300           if (strcmp ("guided", p) != 0)
20301             goto invalid_kind;
20302           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20303           break;
20304
20305         case 'r':
20306           if (strcmp ("runtime", p) != 0)
20307             goto invalid_kind;
20308           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20309           break;
20310
20311         default:
20312           goto invalid_kind;
20313         }
20314     }
20315   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20316     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20317   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20318     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20319   else
20320     goto invalid_kind;
20321   cp_lexer_consume_token (parser->lexer);
20322
20323   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20324     {
20325       cp_token *token;
20326       cp_lexer_consume_token (parser->lexer);
20327
20328       token = cp_lexer_peek_token (parser->lexer);
20329       t = cp_parser_assignment_expression (parser, false);
20330
20331       if (t == error_mark_node)
20332         goto resync_fail;
20333       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20334         error ("%Hschedule %<runtime%> does not take "
20335                "a %<chunk_size%> parameter", &token->location);
20336       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20337         error ("%Hschedule %<auto%> does not take "
20338                "a %<chunk_size%> parameter", &token->location);
20339       else
20340         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20341
20342       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20343         goto resync_fail;
20344     }
20345   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20346     goto resync_fail;
20347
20348   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20349   OMP_CLAUSE_CHAIN (c) = list;
20350   return c;
20351
20352  invalid_kind:
20353   cp_parser_error (parser, "invalid schedule kind");
20354  resync_fail:
20355   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20356                                          /*or_comma=*/false,
20357                                          /*consume_paren=*/true);
20358   return list;
20359 }
20360
20361 /* OpenMP 3.0:
20362    untied */
20363
20364 static tree
20365 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20366                              tree list, location_t location)
20367 {
20368   tree c;
20369
20370   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20371
20372   c = build_omp_clause (OMP_CLAUSE_UNTIED);
20373   OMP_CLAUSE_CHAIN (c) = list;
20374   return c;
20375 }
20376
20377 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
20378    is a bitmask in MASK.  Return the list of clauses found; the result
20379    of clause default goes in *pdefault.  */
20380
20381 static tree
20382 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20383                            const char *where, cp_token *pragma_tok)
20384 {
20385   tree clauses = NULL;
20386   bool first = true;
20387   cp_token *token = NULL;
20388
20389   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20390     {
20391       pragma_omp_clause c_kind;
20392       const char *c_name;
20393       tree prev = clauses;
20394
20395       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20396         cp_lexer_consume_token (parser->lexer);
20397
20398       token = cp_lexer_peek_token (parser->lexer);
20399       c_kind = cp_parser_omp_clause_name (parser);
20400       first = false;
20401
20402       switch (c_kind)
20403         {
20404         case PRAGMA_OMP_CLAUSE_COLLAPSE:
20405           clauses = cp_parser_omp_clause_collapse (parser, clauses,
20406                                                    token->location);
20407           c_name = "collapse";
20408           break;
20409         case PRAGMA_OMP_CLAUSE_COPYIN:
20410           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20411           c_name = "copyin";
20412           break;
20413         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20414           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20415                                             clauses);
20416           c_name = "copyprivate";
20417           break;
20418         case PRAGMA_OMP_CLAUSE_DEFAULT:
20419           clauses = cp_parser_omp_clause_default (parser, clauses,
20420                                                   token->location);
20421           c_name = "default";
20422           break;
20423         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20424           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20425                                             clauses);
20426           c_name = "firstprivate";
20427           break;
20428         case PRAGMA_OMP_CLAUSE_IF:
20429           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20430           c_name = "if";
20431           break;
20432         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20433           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20434                                             clauses);
20435           c_name = "lastprivate";
20436           break;
20437         case PRAGMA_OMP_CLAUSE_NOWAIT:
20438           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20439           c_name = "nowait";
20440           break;
20441         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20442           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20443                                                       token->location);
20444           c_name = "num_threads";
20445           break;
20446         case PRAGMA_OMP_CLAUSE_ORDERED:
20447           clauses = cp_parser_omp_clause_ordered (parser, clauses,
20448                                                   token->location);
20449           c_name = "ordered";
20450           break;
20451         case PRAGMA_OMP_CLAUSE_PRIVATE:
20452           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20453                                             clauses);
20454           c_name = "private";
20455           break;
20456         case PRAGMA_OMP_CLAUSE_REDUCTION:
20457           clauses = cp_parser_omp_clause_reduction (parser, clauses);
20458           c_name = "reduction";
20459           break;
20460         case PRAGMA_OMP_CLAUSE_SCHEDULE:
20461           clauses = cp_parser_omp_clause_schedule (parser, clauses,
20462                                                    token->location);
20463           c_name = "schedule";
20464           break;
20465         case PRAGMA_OMP_CLAUSE_SHARED:
20466           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20467                                             clauses);
20468           c_name = "shared";
20469           break;
20470         case PRAGMA_OMP_CLAUSE_UNTIED:
20471           clauses = cp_parser_omp_clause_untied (parser, clauses,
20472                                                  token->location);
20473           c_name = "nowait";
20474           break;
20475         default:
20476           cp_parser_error (parser, "expected %<#pragma omp%> clause");
20477           goto saw_error;
20478         }
20479
20480       if (((mask >> c_kind) & 1) == 0)
20481         {
20482           /* Remove the invalid clause(s) from the list to avoid
20483              confusing the rest of the compiler.  */
20484           clauses = prev;
20485           error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20486         }
20487     }
20488  saw_error:
20489   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20490   return finish_omp_clauses (clauses);
20491 }
20492
20493 /* OpenMP 2.5:
20494    structured-block:
20495      statement
20496
20497    In practice, we're also interested in adding the statement to an
20498    outer node.  So it is convenient if we work around the fact that
20499    cp_parser_statement calls add_stmt.  */
20500
20501 static unsigned
20502 cp_parser_begin_omp_structured_block (cp_parser *parser)
20503 {
20504   unsigned save = parser->in_statement;
20505
20506   /* Only move the values to IN_OMP_BLOCK if they weren't false.
20507      This preserves the "not within loop or switch" style error messages
20508      for nonsense cases like
20509         void foo() {
20510         #pragma omp single
20511           break;
20512         }
20513   */
20514   if (parser->in_statement)
20515     parser->in_statement = IN_OMP_BLOCK;
20516
20517   return save;
20518 }
20519
20520 static void
20521 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20522 {
20523   parser->in_statement = save;
20524 }
20525
20526 static tree
20527 cp_parser_omp_structured_block (cp_parser *parser)
20528 {
20529   tree stmt = begin_omp_structured_block ();
20530   unsigned int save = cp_parser_begin_omp_structured_block (parser);
20531
20532   cp_parser_statement (parser, NULL_TREE, false, NULL);
20533
20534   cp_parser_end_omp_structured_block (parser, save);
20535   return finish_omp_structured_block (stmt);
20536 }
20537
20538 /* OpenMP 2.5:
20539    # pragma omp atomic new-line
20540      expression-stmt
20541
20542    expression-stmt:
20543      x binop= expr | x++ | ++x | x-- | --x
20544    binop:
20545      +, *, -, /, &, ^, |, <<, >>
20546
20547   where x is an lvalue expression with scalar type.  */
20548
20549 static void
20550 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
20551 {
20552   tree lhs, rhs;
20553   enum tree_code code;
20554
20555   cp_parser_require_pragma_eol (parser, pragma_tok);
20556
20557   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
20558                                     /*cast_p=*/false);
20559   switch (TREE_CODE (lhs))
20560     {
20561     case ERROR_MARK:
20562       goto saw_error;
20563
20564     case PREINCREMENT_EXPR:
20565     case POSTINCREMENT_EXPR:
20566       lhs = TREE_OPERAND (lhs, 0);
20567       code = PLUS_EXPR;
20568       rhs = integer_one_node;
20569       break;
20570
20571     case PREDECREMENT_EXPR:
20572     case POSTDECREMENT_EXPR:
20573       lhs = TREE_OPERAND (lhs, 0);
20574       code = MINUS_EXPR;
20575       rhs = integer_one_node;
20576       break;
20577
20578     default:
20579       switch (cp_lexer_peek_token (parser->lexer)->type)
20580         {
20581         case CPP_MULT_EQ:
20582           code = MULT_EXPR;
20583           break;
20584         case CPP_DIV_EQ:
20585           code = TRUNC_DIV_EXPR;
20586           break;
20587         case CPP_PLUS_EQ:
20588           code = PLUS_EXPR;
20589           break;
20590         case CPP_MINUS_EQ:
20591           code = MINUS_EXPR;
20592           break;
20593         case CPP_LSHIFT_EQ:
20594           code = LSHIFT_EXPR;
20595           break;
20596         case CPP_RSHIFT_EQ:
20597           code = RSHIFT_EXPR;
20598           break;
20599         case CPP_AND_EQ:
20600           code = BIT_AND_EXPR;
20601           break;
20602         case CPP_OR_EQ:
20603           code = BIT_IOR_EXPR;
20604           break;
20605         case CPP_XOR_EQ:
20606           code = BIT_XOR_EXPR;
20607           break;
20608         default:
20609           cp_parser_error (parser,
20610                            "invalid operator for %<#pragma omp atomic%>");
20611           goto saw_error;
20612         }
20613       cp_lexer_consume_token (parser->lexer);
20614
20615       rhs = cp_parser_expression (parser, false);
20616       if (rhs == error_mark_node)
20617         goto saw_error;
20618       break;
20619     }
20620   finish_omp_atomic (code, lhs, rhs);
20621   cp_parser_consume_semicolon_at_end_of_statement (parser);
20622   return;
20623
20624  saw_error:
20625   cp_parser_skip_to_end_of_block_or_statement (parser);
20626 }
20627
20628
20629 /* OpenMP 2.5:
20630    # pragma omp barrier new-line  */
20631
20632 static void
20633 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
20634 {
20635   cp_parser_require_pragma_eol (parser, pragma_tok);
20636   finish_omp_barrier ();
20637 }
20638
20639 /* OpenMP 2.5:
20640    # pragma omp critical [(name)] new-line
20641      structured-block  */
20642
20643 static tree
20644 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20645 {
20646   tree stmt, name = NULL;
20647
20648   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20649     {
20650       cp_lexer_consume_token (parser->lexer);
20651
20652       name = cp_parser_identifier (parser);
20653
20654       if (name == error_mark_node
20655           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20656         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20657                                                /*or_comma=*/false,
20658                                                /*consume_paren=*/true);
20659       if (name == error_mark_node)
20660         name = NULL;
20661     }
20662   cp_parser_require_pragma_eol (parser, pragma_tok);
20663
20664   stmt = cp_parser_omp_structured_block (parser);
20665   return c_finish_omp_critical (stmt, name);
20666 }
20667
20668 /* OpenMP 2.5:
20669    # pragma omp flush flush-vars[opt] new-line
20670
20671    flush-vars:
20672      ( variable-list ) */
20673
20674 static void
20675 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20676 {
20677   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20678     (void) cp_parser_omp_var_list (parser, 0, NULL);
20679   cp_parser_require_pragma_eol (parser, pragma_tok);
20680
20681   finish_omp_flush ();
20682 }
20683
20684 /* Helper function, to parse omp for increment expression.  */
20685
20686 static tree
20687 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
20688 {
20689   tree lhs = cp_parser_cast_expression (parser, false, false), rhs;
20690   enum tree_code op;
20691   cp_token *token;
20692
20693   if (lhs != decl)
20694     {
20695       cp_parser_skip_to_end_of_statement (parser);
20696       return error_mark_node;
20697     }
20698
20699   token = cp_lexer_peek_token (parser->lexer);
20700   op = binops_by_token [token->type].tree_type;
20701   switch (op)
20702     {
20703     case LT_EXPR:
20704     case LE_EXPR:
20705     case GT_EXPR:
20706     case GE_EXPR:
20707       break;
20708     default:
20709       cp_parser_skip_to_end_of_statement (parser);
20710       return error_mark_node;
20711     }
20712
20713   cp_lexer_consume_token (parser->lexer);
20714   rhs = cp_parser_binary_expression (parser, false,
20715                                      PREC_RELATIONAL_EXPRESSION);
20716   if (rhs == error_mark_node
20717       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20718     {
20719       cp_parser_skip_to_end_of_statement (parser);
20720       return error_mark_node;
20721     }
20722
20723   return build2 (op, boolean_type_node, lhs, rhs);
20724 }
20725
20726 /* Helper function, to parse omp for increment expression.  */
20727
20728 static tree
20729 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
20730 {
20731   cp_token *token = cp_lexer_peek_token (parser->lexer);
20732   enum tree_code op;
20733   tree lhs, rhs;
20734   cp_id_kind idk;
20735   bool decl_first;
20736
20737   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20738     {
20739       op = (token->type == CPP_PLUS_PLUS
20740             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
20741       cp_lexer_consume_token (parser->lexer);
20742       lhs = cp_parser_cast_expression (parser, false, false);
20743       if (lhs != decl)
20744         return error_mark_node;
20745       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20746     }
20747
20748   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
20749   if (lhs != decl)
20750     return error_mark_node;
20751
20752   token = cp_lexer_peek_token (parser->lexer);
20753   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
20754     {
20755       op = (token->type == CPP_PLUS_PLUS
20756             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
20757       cp_lexer_consume_token (parser->lexer);
20758       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
20759     }
20760
20761   op = cp_parser_assignment_operator_opt (parser);
20762   if (op == ERROR_MARK)
20763     return error_mark_node;
20764
20765   if (op != NOP_EXPR)
20766     {
20767       rhs = cp_parser_assignment_expression (parser, false);
20768       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
20769       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20770     }
20771
20772   lhs = cp_parser_binary_expression (parser, false,
20773                                      PREC_ADDITIVE_EXPRESSION);
20774   token = cp_lexer_peek_token (parser->lexer);
20775   decl_first = lhs == decl;
20776   if (decl_first)
20777     lhs = NULL_TREE;
20778   if (token->type != CPP_PLUS
20779       && token->type != CPP_MINUS)
20780     return error_mark_node;
20781
20782   do
20783     {
20784       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
20785       cp_lexer_consume_token (parser->lexer);
20786       rhs = cp_parser_binary_expression (parser, false,
20787                                          PREC_ADDITIVE_EXPRESSION);
20788       token = cp_lexer_peek_token (parser->lexer);
20789       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
20790         {
20791           if (lhs == NULL_TREE)
20792             {
20793               if (op == PLUS_EXPR)
20794                 lhs = rhs;
20795               else
20796                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
20797             }
20798           else
20799             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
20800                                      NULL, tf_warning_or_error);
20801         }
20802     }
20803   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
20804
20805   if (!decl_first)
20806     {
20807       if (rhs != decl || op == MINUS_EXPR)
20808         return error_mark_node;
20809       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
20810     }
20811   else
20812     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
20813
20814   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
20815 }
20816
20817 /* Parse the restricted form of the for statement allowed by OpenMP.  */
20818
20819 static tree
20820 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
20821 {
20822   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
20823   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
20824   tree this_pre_body, cl;
20825   location_t loc_first;
20826   bool collapse_err = false;
20827   int i, collapse = 1, nbraces = 0;
20828
20829   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
20830     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
20831       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
20832
20833   gcc_assert (collapse >= 1);
20834
20835   declv = make_tree_vec (collapse);
20836   initv = make_tree_vec (collapse);
20837   condv = make_tree_vec (collapse);
20838   incrv = make_tree_vec (collapse);
20839
20840   loc_first = cp_lexer_peek_token (parser->lexer)->location;
20841
20842   for (i = 0; i < collapse; i++)
20843     {
20844       int bracecount = 0;
20845       bool add_private_clause = false;
20846       location_t loc;
20847
20848       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20849         {
20850           cp_parser_error (parser, "for statement expected");
20851           return NULL;
20852         }
20853       loc = cp_lexer_consume_token (parser->lexer)->location;
20854
20855       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20856         return NULL;
20857
20858       init = decl = real_decl = NULL;
20859       this_pre_body = push_stmt_list ();
20860       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20861         {
20862           cp_decl_specifier_seq type_specifiers;
20863
20864           /* First, try to parse as an initialized declaration.  See
20865              cp_parser_condition, from whence the bulk of this is copied.  */
20866
20867           cp_parser_parse_tentatively (parser);
20868           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20869                                         &type_specifiers);
20870           if (!cp_parser_error_occurred (parser))
20871             {
20872               tree asm_specification, attributes;
20873               cp_declarator *declarator;
20874
20875               declarator = cp_parser_declarator (parser,
20876                                                  CP_PARSER_DECLARATOR_NAMED,
20877                                                  /*ctor_dtor_or_conv_p=*/NULL,
20878                                                  /*parenthesized_p=*/NULL,
20879                                                  /*member_p=*/false);
20880               attributes = cp_parser_attributes_opt (parser);
20881               asm_specification = cp_parser_asm_specification_opt (parser);
20882
20883               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
20884                 cp_parser_require (parser, CPP_EQ, "%<=%>");
20885               if (cp_parser_parse_definitely (parser))
20886                 {
20887                   tree pushed_scope;
20888
20889                   decl = start_decl (declarator, &type_specifiers,
20890                                      /*initialized_p=*/false, attributes,
20891                                      /*prefix_attributes=*/NULL_TREE,
20892                                      &pushed_scope);
20893
20894                   if (CLASS_TYPE_P (TREE_TYPE (decl))
20895                       || type_dependent_expression_p (decl))
20896                     {
20897                       bool is_direct_init, is_non_constant_init;
20898
20899                       init = cp_parser_initializer (parser,
20900                                                     &is_direct_init,
20901                                                     &is_non_constant_init);
20902
20903                       cp_finish_decl (decl, init, !is_non_constant_init,
20904                                       asm_specification,
20905                                       LOOKUP_ONLYCONVERTING);
20906                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
20907                         {
20908                           for_block
20909                             = tree_cons (NULL, this_pre_body, for_block);
20910                           init = NULL_TREE;
20911                         }
20912                       else
20913                         init = pop_stmt_list (this_pre_body);
20914                       this_pre_body = NULL_TREE;
20915                     }
20916                   else
20917                     {
20918                       cp_parser_require (parser, CPP_EQ, "%<=%>");
20919                       init = cp_parser_assignment_expression (parser, false);
20920
20921                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20922                         init = error_mark_node;
20923                       else
20924                         cp_finish_decl (decl, NULL_TREE,
20925                                         /*init_const_expr_p=*/false,
20926                                         asm_specification,
20927                                         LOOKUP_ONLYCONVERTING);
20928                     }
20929
20930                   if (pushed_scope)
20931                     pop_scope (pushed_scope);
20932                 }
20933             }
20934           else
20935             cp_parser_abort_tentative_parse (parser);
20936
20937           /* If parsing as an initialized declaration failed, try again as
20938              a simple expression.  */
20939           if (decl == NULL)
20940             {
20941               cp_id_kind idk;
20942               cp_parser_parse_tentatively (parser);
20943               decl = cp_parser_primary_expression (parser, false, false,
20944                                                    false, &idk);
20945               if (!cp_parser_error_occurred (parser)
20946                   && decl
20947                   && DECL_P (decl)
20948                   && CLASS_TYPE_P (TREE_TYPE (decl)))
20949                 {
20950                   tree rhs;
20951
20952                   cp_parser_parse_definitely (parser);
20953                   cp_parser_require (parser, CPP_EQ, "%<=%>");
20954                   rhs = cp_parser_assignment_expression (parser, false);
20955                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
20956                                                          rhs,
20957                                                          tf_warning_or_error));
20958                   add_private_clause = true;
20959                 }
20960               else
20961                 {
20962                   decl = NULL;
20963                   cp_parser_abort_tentative_parse (parser);
20964                   init = cp_parser_expression (parser, false);
20965                   if (init)
20966                     {
20967                       if (TREE_CODE (init) == MODIFY_EXPR
20968                           || TREE_CODE (init) == MODOP_EXPR)
20969                         real_decl = TREE_OPERAND (init, 0);
20970                     }
20971                 }
20972             }
20973         }
20974       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
20975       if (this_pre_body)
20976         {
20977           this_pre_body = pop_stmt_list (this_pre_body);
20978           if (pre_body)
20979             {
20980               tree t = pre_body;
20981               pre_body = push_stmt_list ();
20982               add_stmt (t);
20983               add_stmt (this_pre_body);
20984               pre_body = pop_stmt_list (pre_body);
20985             }
20986           else
20987             pre_body = this_pre_body;
20988         }
20989
20990       if (decl)
20991         real_decl = decl;
20992       if (par_clauses != NULL && real_decl != NULL_TREE)
20993         {
20994           tree *c;
20995           for (c = par_clauses; *c ; )
20996             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
20997                 && OMP_CLAUSE_DECL (*c) == real_decl)
20998               {
20999                 error ("%Hiteration variable %qD should not be firstprivate",
21000                        &loc, real_decl);
21001                 *c = OMP_CLAUSE_CHAIN (*c);
21002               }
21003             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21004                      && OMP_CLAUSE_DECL (*c) == real_decl)
21005               {
21006                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21007                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
21008                 tree l = build_omp_clause (OMP_CLAUSE_LASTPRIVATE);
21009                 OMP_CLAUSE_DECL (l) = real_decl;
21010                 OMP_CLAUSE_CHAIN (l) = clauses;
21011                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21012                 clauses = l;
21013                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21014                 CP_OMP_CLAUSE_INFO (*c) = NULL;
21015                 add_private_clause = false;
21016               }
21017             else
21018               {
21019                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21020                     && OMP_CLAUSE_DECL (*c) == real_decl)
21021                   add_private_clause = false;
21022                 c = &OMP_CLAUSE_CHAIN (*c);
21023               }
21024         }
21025
21026       if (add_private_clause)
21027         {
21028           tree c;
21029           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21030             {
21031               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21032                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21033                   && OMP_CLAUSE_DECL (c) == decl)
21034                 break;
21035               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21036                        && OMP_CLAUSE_DECL (c) == decl)
21037                 error ("%Hiteration variable %qD should not be firstprivate",
21038                        &loc, decl);
21039               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21040                        && OMP_CLAUSE_DECL (c) == decl)
21041                 error ("%Hiteration variable %qD should not be reduction",
21042                        &loc, decl);
21043             }
21044           if (c == NULL)
21045             {
21046               c = build_omp_clause (OMP_CLAUSE_PRIVATE);
21047               OMP_CLAUSE_DECL (c) = decl;
21048               c = finish_omp_clauses (c);
21049               if (c)
21050                 {
21051                   OMP_CLAUSE_CHAIN (c) = clauses;
21052                   clauses = c;
21053                 }
21054             }
21055         }
21056
21057       cond = NULL;
21058       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21059         {
21060           /* If decl is an iterator, preserve LHS and RHS of the relational
21061              expr until finish_omp_for.  */
21062           if (decl
21063               && (type_dependent_expression_p (decl)
21064                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21065             cond = cp_parser_omp_for_cond (parser, decl);
21066           else
21067             cond = cp_parser_condition (parser);
21068         }
21069       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21070
21071       incr = NULL;
21072       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21073         {
21074           /* If decl is an iterator, preserve the operator on decl
21075              until finish_omp_for.  */
21076           if (decl
21077               && (type_dependent_expression_p (decl)
21078                   || CLASS_TYPE_P (TREE_TYPE (decl))))
21079             incr = cp_parser_omp_for_incr (parser, decl);
21080           else
21081             incr = cp_parser_expression (parser, false);
21082         }
21083
21084       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21085         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21086                                                /*or_comma=*/false,
21087                                                /*consume_paren=*/true);
21088
21089       TREE_VEC_ELT (declv, i) = decl;
21090       TREE_VEC_ELT (initv, i) = init;
21091       TREE_VEC_ELT (condv, i) = cond;
21092       TREE_VEC_ELT (incrv, i) = incr;
21093
21094       if (i == collapse - 1)
21095         break;
21096
21097       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21098          in between the collapsed for loops to be still considered perfectly
21099          nested.  Hopefully the final version clarifies this.
21100          For now handle (multiple) {'s and empty statements.  */
21101       cp_parser_parse_tentatively (parser);
21102       do
21103         {
21104           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21105             break;
21106           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21107             {
21108               cp_lexer_consume_token (parser->lexer);
21109               bracecount++;
21110             }
21111           else if (bracecount
21112                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21113             cp_lexer_consume_token (parser->lexer);
21114           else
21115             {
21116               loc = cp_lexer_peek_token (parser->lexer)->location;
21117               error ("%Hnot enough collapsed for loops", &loc);
21118               collapse_err = true;
21119               cp_parser_abort_tentative_parse (parser);
21120               declv = NULL_TREE;
21121               break;
21122             }
21123         }
21124       while (1);
21125
21126       if (declv)
21127         {
21128           cp_parser_parse_definitely (parser);
21129           nbraces += bracecount;
21130         }
21131     }
21132
21133   /* Note that we saved the original contents of this flag when we entered
21134      the structured block, and so we don't need to re-save it here.  */
21135   parser->in_statement = IN_OMP_FOR;
21136
21137   /* Note that the grammar doesn't call for a structured block here,
21138      though the loop as a whole is a structured block.  */
21139   body = push_stmt_list ();
21140   cp_parser_statement (parser, NULL_TREE, false, NULL);
21141   body = pop_stmt_list (body);
21142
21143   if (declv == NULL_TREE)
21144     ret = NULL_TREE;
21145   else
21146     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21147                           pre_body, clauses);
21148
21149   while (nbraces)
21150     {
21151       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21152         {
21153           cp_lexer_consume_token (parser->lexer);
21154           nbraces--;
21155         }
21156       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21157         cp_lexer_consume_token (parser->lexer);
21158       else
21159         {
21160           if (!collapse_err)
21161             {
21162               location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21163               error ("%Hcollapsed loops not perfectly nested", &loc);
21164             }
21165           collapse_err = true;
21166           cp_parser_statement_seq_opt (parser, NULL);
21167           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21168         }
21169     }
21170
21171   while (for_block)
21172     {
21173       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21174       for_block = TREE_CHAIN (for_block);
21175     }
21176
21177   return ret;
21178 }
21179
21180 /* OpenMP 2.5:
21181    #pragma omp for for-clause[optseq] new-line
21182      for-loop  */
21183
21184 #define OMP_FOR_CLAUSE_MASK                             \
21185         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21186         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21187         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21188         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21189         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
21190         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
21191         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
21192         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21193
21194 static tree
21195 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21196 {
21197   tree clauses, sb, ret;
21198   unsigned int save;
21199
21200   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21201                                        "#pragma omp for", pragma_tok);
21202
21203   sb = begin_omp_structured_block ();
21204   save = cp_parser_begin_omp_structured_block (parser);
21205
21206   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21207
21208   cp_parser_end_omp_structured_block (parser, save);
21209   add_stmt (finish_omp_structured_block (sb));
21210
21211   return ret;
21212 }
21213
21214 /* OpenMP 2.5:
21215    # pragma omp master new-line
21216      structured-block  */
21217
21218 static tree
21219 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21220 {
21221   cp_parser_require_pragma_eol (parser, pragma_tok);
21222   return c_finish_omp_master (cp_parser_omp_structured_block (parser));
21223 }
21224
21225 /* OpenMP 2.5:
21226    # pragma omp ordered new-line
21227      structured-block  */
21228
21229 static tree
21230 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21231 {
21232   cp_parser_require_pragma_eol (parser, pragma_tok);
21233   return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
21234 }
21235
21236 /* OpenMP 2.5:
21237
21238    section-scope:
21239      { section-sequence }
21240
21241    section-sequence:
21242      section-directive[opt] structured-block
21243      section-sequence section-directive structured-block  */
21244
21245 static tree
21246 cp_parser_omp_sections_scope (cp_parser *parser)
21247 {
21248   tree stmt, substmt;
21249   bool error_suppress = false;
21250   cp_token *tok;
21251
21252   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21253     return NULL_TREE;
21254
21255   stmt = push_stmt_list ();
21256
21257   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21258     {
21259       unsigned save;
21260
21261       substmt = begin_omp_structured_block ();
21262       save = cp_parser_begin_omp_structured_block (parser);
21263
21264       while (1)
21265         {
21266           cp_parser_statement (parser, NULL_TREE, false, NULL);
21267
21268           tok = cp_lexer_peek_token (parser->lexer);
21269           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21270             break;
21271           if (tok->type == CPP_CLOSE_BRACE)
21272             break;
21273           if (tok->type == CPP_EOF)
21274             break;
21275         }
21276
21277       cp_parser_end_omp_structured_block (parser, save);
21278       substmt = finish_omp_structured_block (substmt);
21279       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21280       add_stmt (substmt);
21281     }
21282
21283   while (1)
21284     {
21285       tok = cp_lexer_peek_token (parser->lexer);
21286       if (tok->type == CPP_CLOSE_BRACE)
21287         break;
21288       if (tok->type == CPP_EOF)
21289         break;
21290
21291       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21292         {
21293           cp_lexer_consume_token (parser->lexer);
21294           cp_parser_require_pragma_eol (parser, tok);
21295           error_suppress = false;
21296         }
21297       else if (!error_suppress)
21298         {
21299           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21300           error_suppress = true;
21301         }
21302
21303       substmt = cp_parser_omp_structured_block (parser);
21304       substmt = build1 (OMP_SECTION, void_type_node, substmt);
21305       add_stmt (substmt);
21306     }
21307   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21308
21309   substmt = pop_stmt_list (stmt);
21310
21311   stmt = make_node (OMP_SECTIONS);
21312   TREE_TYPE (stmt) = void_type_node;
21313   OMP_SECTIONS_BODY (stmt) = substmt;
21314
21315   add_stmt (stmt);
21316   return stmt;
21317 }
21318
21319 /* OpenMP 2.5:
21320    # pragma omp sections sections-clause[optseq] newline
21321      sections-scope  */
21322
21323 #define OMP_SECTIONS_CLAUSE_MASK                        \
21324         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21325         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21326         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
21327         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21328         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21329
21330 static tree
21331 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21332 {
21333   tree clauses, ret;
21334
21335   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21336                                        "#pragma omp sections", pragma_tok);
21337
21338   ret = cp_parser_omp_sections_scope (parser);
21339   if (ret)
21340     OMP_SECTIONS_CLAUSES (ret) = clauses;
21341
21342   return ret;
21343 }
21344
21345 /* OpenMP 2.5:
21346    # pragma parallel parallel-clause new-line
21347    # pragma parallel for parallel-for-clause new-line
21348    # pragma parallel sections parallel-sections-clause new-line  */
21349
21350 #define OMP_PARALLEL_CLAUSE_MASK                        \
21351         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21352         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21353         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21354         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21355         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
21356         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
21357         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
21358         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21359
21360 static tree
21361 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21362 {
21363   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21364   const char *p_name = "#pragma omp parallel";
21365   tree stmt, clauses, par_clause, ws_clause, block;
21366   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21367   unsigned int save;
21368
21369   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21370     {
21371       cp_lexer_consume_token (parser->lexer);
21372       p_kind = PRAGMA_OMP_PARALLEL_FOR;
21373       p_name = "#pragma omp parallel for";
21374       mask |= OMP_FOR_CLAUSE_MASK;
21375       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21376     }
21377   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21378     {
21379       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21380       const char *p = IDENTIFIER_POINTER (id);
21381       if (strcmp (p, "sections") == 0)
21382         {
21383           cp_lexer_consume_token (parser->lexer);
21384           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21385           p_name = "#pragma omp parallel sections";
21386           mask |= OMP_SECTIONS_CLAUSE_MASK;
21387           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21388         }
21389     }
21390
21391   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21392   block = begin_omp_parallel ();
21393   save = cp_parser_begin_omp_structured_block (parser);
21394
21395   switch (p_kind)
21396     {
21397     case PRAGMA_OMP_PARALLEL:
21398       cp_parser_statement (parser, NULL_TREE, false, NULL);
21399       par_clause = clauses;
21400       break;
21401
21402     case PRAGMA_OMP_PARALLEL_FOR:
21403       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21404       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21405       break;
21406
21407     case PRAGMA_OMP_PARALLEL_SECTIONS:
21408       c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
21409       stmt = cp_parser_omp_sections_scope (parser);
21410       if (stmt)
21411         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21412       break;
21413
21414     default:
21415       gcc_unreachable ();
21416     }
21417
21418   cp_parser_end_omp_structured_block (parser, save);
21419   stmt = finish_omp_parallel (par_clause, block);
21420   if (p_kind != PRAGMA_OMP_PARALLEL)
21421     OMP_PARALLEL_COMBINED (stmt) = 1;
21422   return stmt;
21423 }
21424
21425 /* OpenMP 2.5:
21426    # pragma omp single single-clause[optseq] new-line
21427      structured-block  */
21428
21429 #define OMP_SINGLE_CLAUSE_MASK                          \
21430         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21431         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21432         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
21433         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21434
21435 static tree
21436 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21437 {
21438   tree stmt = make_node (OMP_SINGLE);
21439   TREE_TYPE (stmt) = void_type_node;
21440
21441   OMP_SINGLE_CLAUSES (stmt)
21442     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21443                                  "#pragma omp single", pragma_tok);
21444   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21445
21446   return add_stmt (stmt);
21447 }
21448
21449 /* OpenMP 3.0:
21450    # pragma omp task task-clause[optseq] new-line
21451      structured-block  */
21452
21453 #define OMP_TASK_CLAUSE_MASK                            \
21454         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
21455         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
21456         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
21457         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
21458         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
21459         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21460
21461 static tree
21462 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21463 {
21464   tree clauses, block;
21465   unsigned int save;
21466
21467   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21468                                        "#pragma omp task", pragma_tok);
21469   block = begin_omp_task ();
21470   save = cp_parser_begin_omp_structured_block (parser);
21471   cp_parser_statement (parser, NULL_TREE, false, NULL);
21472   cp_parser_end_omp_structured_block (parser, save);
21473   return finish_omp_task (clauses, block);
21474 }
21475
21476 /* OpenMP 3.0:
21477    # pragma omp taskwait new-line  */
21478
21479 static void
21480 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21481 {
21482   cp_parser_require_pragma_eol (parser, pragma_tok);
21483   finish_omp_taskwait ();
21484 }
21485
21486 /* OpenMP 2.5:
21487    # pragma omp threadprivate (variable-list) */
21488
21489 static void
21490 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21491 {
21492   tree vars;
21493
21494   vars = cp_parser_omp_var_list (parser, 0, NULL);
21495   cp_parser_require_pragma_eol (parser, pragma_tok);
21496
21497   finish_omp_threadprivate (vars);
21498 }
21499
21500 /* Main entry point to OpenMP statement pragmas.  */
21501
21502 static void
21503 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21504 {
21505   tree stmt;
21506
21507   switch (pragma_tok->pragma_kind)
21508     {
21509     case PRAGMA_OMP_ATOMIC:
21510       cp_parser_omp_atomic (parser, pragma_tok);
21511       return;
21512     case PRAGMA_OMP_CRITICAL:
21513       stmt = cp_parser_omp_critical (parser, pragma_tok);
21514       break;
21515     case PRAGMA_OMP_FOR:
21516       stmt = cp_parser_omp_for (parser, pragma_tok);
21517       break;
21518     case PRAGMA_OMP_MASTER:
21519       stmt = cp_parser_omp_master (parser, pragma_tok);
21520       break;
21521     case PRAGMA_OMP_ORDERED:
21522       stmt = cp_parser_omp_ordered (parser, pragma_tok);
21523       break;
21524     case PRAGMA_OMP_PARALLEL:
21525       stmt = cp_parser_omp_parallel (parser, pragma_tok);
21526       break;
21527     case PRAGMA_OMP_SECTIONS:
21528       stmt = cp_parser_omp_sections (parser, pragma_tok);
21529       break;
21530     case PRAGMA_OMP_SINGLE:
21531       stmt = cp_parser_omp_single (parser, pragma_tok);
21532       break;
21533     case PRAGMA_OMP_TASK:
21534       stmt = cp_parser_omp_task (parser, pragma_tok);
21535       break;
21536     default:
21537       gcc_unreachable ();
21538     }
21539
21540   if (stmt)
21541     SET_EXPR_LOCATION (stmt, pragma_tok->location);
21542 }
21543 \f
21544 /* The parser.  */
21545
21546 static GTY (()) cp_parser *the_parser;
21547
21548 \f
21549 /* Special handling for the first token or line in the file.  The first
21550    thing in the file might be #pragma GCC pch_preprocess, which loads a
21551    PCH file, which is a GC collection point.  So we need to handle this
21552    first pragma without benefit of an existing lexer structure.
21553
21554    Always returns one token to the caller in *FIRST_TOKEN.  This is
21555    either the true first token of the file, or the first token after
21556    the initial pragma.  */
21557
21558 static void
21559 cp_parser_initial_pragma (cp_token *first_token)
21560 {
21561   tree name = NULL;
21562
21563   cp_lexer_get_preprocessor_token (NULL, first_token);
21564   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
21565     return;
21566
21567   cp_lexer_get_preprocessor_token (NULL, first_token);
21568   if (first_token->type == CPP_STRING)
21569     {
21570       name = first_token->u.value;
21571
21572       cp_lexer_get_preprocessor_token (NULL, first_token);
21573       if (first_token->type != CPP_PRAGMA_EOL)
21574         error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
21575                &first_token->location);
21576     }
21577   else
21578     error ("%Hexpected string literal", &first_token->location);
21579
21580   /* Skip to the end of the pragma.  */
21581   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
21582     cp_lexer_get_preprocessor_token (NULL, first_token);
21583
21584   /* Now actually load the PCH file.  */
21585   if (name)
21586     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
21587
21588   /* Read one more token to return to our caller.  We have to do this
21589      after reading the PCH file in, since its pointers have to be
21590      live.  */
21591   cp_lexer_get_preprocessor_token (NULL, first_token);
21592 }
21593
21594 /* Normal parsing of a pragma token.  Here we can (and must) use the
21595    regular lexer.  */
21596
21597 static bool
21598 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
21599 {
21600   cp_token *pragma_tok;
21601   unsigned int id;
21602
21603   pragma_tok = cp_lexer_consume_token (parser->lexer);
21604   gcc_assert (pragma_tok->type == CPP_PRAGMA);
21605   parser->lexer->in_pragma = true;
21606
21607   id = pragma_tok->pragma_kind;
21608   switch (id)
21609     {
21610     case PRAGMA_GCC_PCH_PREPROCESS:
21611       error ("%H%<#pragma GCC pch_preprocess%> must be first",
21612              &pragma_tok->location);
21613       break;
21614
21615     case PRAGMA_OMP_BARRIER:
21616       switch (context)
21617         {
21618         case pragma_compound:
21619           cp_parser_omp_barrier (parser, pragma_tok);
21620           return false;
21621         case pragma_stmt:
21622           error ("%H%<#pragma omp barrier%> may only be "
21623                  "used in compound statements", &pragma_tok->location);
21624           break;
21625         default:
21626           goto bad_stmt;
21627         }
21628       break;
21629
21630     case PRAGMA_OMP_FLUSH:
21631       switch (context)
21632         {
21633         case pragma_compound:
21634           cp_parser_omp_flush (parser, pragma_tok);
21635           return false;
21636         case pragma_stmt:
21637           error ("%H%<#pragma omp flush%> may only be "
21638                  "used in compound statements", &pragma_tok->location);
21639           break;
21640         default:
21641           goto bad_stmt;
21642         }
21643       break;
21644
21645     case PRAGMA_OMP_TASKWAIT:
21646       switch (context)
21647         {
21648         case pragma_compound:
21649           cp_parser_omp_taskwait (parser, pragma_tok);
21650           return false;
21651         case pragma_stmt:
21652           error ("%H%<#pragma omp taskwait%> may only be "
21653                  "used in compound statements",
21654                  &pragma_tok->location);
21655           break;
21656         default:
21657           goto bad_stmt;
21658         }
21659       break;
21660
21661     case PRAGMA_OMP_THREADPRIVATE:
21662       cp_parser_omp_threadprivate (parser, pragma_tok);
21663       return false;
21664
21665     case PRAGMA_OMP_ATOMIC:
21666     case PRAGMA_OMP_CRITICAL:
21667     case PRAGMA_OMP_FOR:
21668     case PRAGMA_OMP_MASTER:
21669     case PRAGMA_OMP_ORDERED:
21670     case PRAGMA_OMP_PARALLEL:
21671     case PRAGMA_OMP_SECTIONS:
21672     case PRAGMA_OMP_SINGLE:
21673     case PRAGMA_OMP_TASK:
21674       if (context == pragma_external)
21675         goto bad_stmt;
21676       cp_parser_omp_construct (parser, pragma_tok);
21677       return true;
21678
21679     case PRAGMA_OMP_SECTION:
21680       error ("%H%<#pragma omp section%> may only be used in "
21681              "%<#pragma omp sections%> construct", &pragma_tok->location);
21682       break;
21683
21684     default:
21685       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
21686       c_invoke_pragma_handler (id);
21687       break;
21688
21689     bad_stmt:
21690       cp_parser_error (parser, "expected declaration specifiers");
21691       break;
21692     }
21693
21694   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21695   return false;
21696 }
21697
21698 /* The interface the pragma parsers have to the lexer.  */
21699
21700 enum cpp_ttype
21701 pragma_lex (tree *value)
21702 {
21703   cp_token *tok;
21704   enum cpp_ttype ret;
21705
21706   tok = cp_lexer_peek_token (the_parser->lexer);
21707
21708   ret = tok->type;
21709   *value = tok->u.value;
21710
21711   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
21712     ret = CPP_EOF;
21713   else if (ret == CPP_STRING)
21714     *value = cp_parser_string_literal (the_parser, false, false);
21715   else
21716     {
21717       cp_lexer_consume_token (the_parser->lexer);
21718       if (ret == CPP_KEYWORD)
21719         ret = CPP_NAME;
21720     }
21721
21722   return ret;
21723 }
21724
21725 \f
21726 /* External interface.  */
21727
21728 /* Parse one entire translation unit.  */
21729
21730 void
21731 c_parse_file (void)
21732 {
21733   bool error_occurred;
21734   static bool already_called = false;
21735
21736   if (already_called)
21737     {
21738       sorry ("inter-module optimizations not implemented for C++");
21739       return;
21740     }
21741   already_called = true;
21742
21743   the_parser = cp_parser_new ();
21744   push_deferring_access_checks (flag_access_control
21745                                 ? dk_no_deferred : dk_no_check);
21746   error_occurred = cp_parser_translation_unit (the_parser);
21747   the_parser = NULL;
21748 }
21749
21750 #include "gt-cp-parser.h"